numpy函数给出不正确的结果 - 手工检查和excel

时间:2017-12-05 18:48:21

标签: numpy math geo

我正在为岩石物理建模编写numpy中的一些函数,并注意到我的一个函数会产生错误的结果。这个函数是我对Hertz-Mindlin球体建模的启示:

Summary of the Hertz-Mindlin model

这是我目前的职能:

# Hertz-Mindlin sphere pack model: 

import numpy as np 

def hertzmindlin(K0, G0, PHIC, P, f=1.0):
'''
Hertz-Mindlin sphere-pack model, adapted from:
'Dvorkin, J. and Nur, A., 1996. Elasticity of high-porosity sandstones: 
Theory for two North Sea data sets. Geophysics, 61(5), pp.1363-1370."

Arguments:
K0 = Bulk modulus of mineral in GPa
G0 = Shear modulus of mineral in GPa
PHIC = Critical porosity for mineral-fluid mixture. Calculate using Dvorkin-Nuir (1995) or use literature
P = Confining pressure in GPa
f = Shear modulus correction factor. Default = 1

Results:
V0 = Theoretical poissons ratio of mineral
n = Coordination number of sphere-pack, calculated from Murphy's (1982) empirical relation
K_HM = Hertz-Mindlin effective dry Bulk modulus at pressure, P, in GPa
G_HM = Hertz-Mindlin effective dry Shear modulus at pressure, P, in GPa

'''
V0 = (3*K0-2*G0)/(6*K0+2*G0) # Calculated theoretical poissons ratio of bulk rock
n = 20-(34*PHIC)+(14*(PHIC**2)) # Coordination number at critical porosity (Murphy 1982)
K_HM = (P*(n**2*(1-PHIC)**2*G0**2) / (18*np.pi**2*(1-V0)**2))**(1/3)
G_HM = ((2+3*f-V0*(1+3*f))/(5*(2-V0))) * ((P*(3*n**2*(1-PHIC)**2*G0**2)/(2*np.pi**2*(1-V0)**2)))**(1/3)
return K_HM, G_HM

问题是当我为以下的输入运行此函数时:

K,G,= 36,45

PHIC = 0.4

P = 0.001

我得到的结果是K_HM = 1.0,G_HM = 0.49009009009009

手计算和excel计算值显示这是不正确的,我应该输出K_HM = 0.763265313,G_HM = 1.081083984

我相当肯定在函数中出现了问题,因为对于输入K,G,输出G应该大于K(目前它更小)

任何帮助将不胜感激!我可以在excel中执行此操作,但理想情况下希望所有内容都在python中运行。

1 个答案:

答案 0 :(得分:2)

在Python2中,整数除法(使用/)返回一个整数。例如,1/3 = 0。 在Python3中,整数除法(使用/)可能会返回一个浮点数。

看来你正在使用Python2。要获得浮点除法(在Python2和Python3中),请确保每个除法运算至少包含一个浮点数:例如,将1/3更改为1.0/31/3.0或(可接受但可能不太可读,1/3.):

import numpy as np
def hertzmindlin(K0, G0, PHIC, P, f=1.0):
    K0, G0 = map(float, (K0, G0))
    V0 = (3*K0-2*G0)/(6*K0+2*G0) # Calculated theoretical poissons ratio of bulk rock
    n = 20-(34*PHIC)+(14*(PHIC**2)) # Coordination number at critical porosity (Murphy 1982)
    K_HM = (P*(n**2*(1-PHIC)**2*G0**2) / (18*np.pi**2*(1-V0)**2))**(1/3.0)
    G_HM = ((2+3*f-V0*(1+3*f))/(5*(2-V0))) * ((P*(3*n**2*(1-PHIC)**2*G0**2)/(2*np.pi**2*(1-V0)**2)))**(1/3.0)
    return K_HM, G_HM

K, G, = 36, 45
PHIC = 0.4
P = 0.001

print(hertzmindlin(K, G, PHIC, P))

或者,在Python2的更高版本(例如Python2.7)中,您可以放置​​

from __future__ import division

位于脚本顶部(在所有其他导入语句之前)到activate Python3-style floating-point division