2016-06-28 71 views
0

在该代码中的变量pressureenthalpy,即来自一个[float(n) for n in line.split()]命令,不读入的函数:已在一个line.split()命令被分配的变量,不读入一个函数

import numpy as np 

pressure_gibbs = open('pressure_gibbs_all_points.dat', 'w') 

pressure_gibbs.write('#pressure gibbs\n') 

## FUNCTIONS: 

def G(H,S): 
# G = H - TS 
# Then: 
    gibbs = H - 298.15 * S/4.0 
    return gibbs 



with open('entropies_parsed.dat') as entropies_parsed, open('pressure_enthalpy_all_points.dat') as enthalpy_pressure: # open the file 
    entropies_parsed.next() # skip the first line 
    enthalpy_pressure.next() 

    entropy = [float(line) for line in entropies_parsed] 
    #print entropy 
    for line in enthalpy_pressure: 
     #print entropy 
     pressure, enthalpy = [float(n) for n in line.split()] 
     #print pressure 
     #print enthalpy 
     for i in entropy: 
      #print i 
      gibbs = G(enthalpy, i) 
      #print gibbs 
      pressure_gibbs.write('{}\t{}\n'.format (pressure, gibbs)) 

pressure_gibbs.close() 

此文件夹中只有两个文件是需要运行该代码:

pressure_enthalpy_all_points.dat

# pressure enthalpy 
2   3 
5   4 
3.5  2 

entropies_parsed.dat

# entropies 
0.5   
0.2   
0.47  

这是我能做到的最好,和压痕位置是正确的,从我的知识。

然而, 这个代码给出了一个文件pressure_gibbs_all_points.dat

#pressure gibbs 
2.0  -34.26875 
2.0  -11.9075 
2.0  -32.032625 
5.0  -33.26875 
5.0  -10.9075 
5.0  -31.032625 
3.5  -35.26875 
3.5  -12.9075 
3.5  -33.032625 

这是不对的。

如果你能帮助我,我将不胜感激。

+2

为什么它错了?顺便说一下,它看起来像是在吉布斯函数之前将熵除以4,并且还在吉布斯函数之内。你有意这么做吗? –

+0

@Vince West'pressure_gibbs_all_points.dat'是错误的,因为压力的值不是文件'pressure_enthalpy_all_points.dat'中的值更正了'i/4'到'i' –

+0

看到下面的答案,并且让我知道如果我正确理解你的问题 –

回答

2

你的输出文件似乎显示了与你的代码中的数学相匹配的值,所以我唯一能看到的就是你有9个计算,你期望3个。这是因为你有一个嵌套循环,所以你是首先在压力上循环,然后在熵上循环。所以你在p = 2.0时计算Gibbs的3个熵值,然后再计算p = 5.0,最后计算p = 3.5,所以你有9个计算。如果你只是在寻找3个计算:

for i, line in zip(entropy, enthalpy_pressure): 
    #print entropy 
    pressure, enthalpy = [float(n) for n in line.split()] 
    #print pressure 
    #print enthalpy 
     #print i 
    gibbs = G(enthalpy, i) 
    #print gibbs 
    pressure_gibbs.write('{}\t{}\n'.format (pressure, gibbs)) 
+0

谢谢,这工作。嵌套循环和'zip'有什么区别? –

+1

Zip允许您使用方便的语法一次循环多个列表或元组。你也可以在范围内完成(len(entropy)),然后使用entropy [i],并且enthalpy_pressure [i] –

+0

谢谢,'entropy'已经通过'[float(line)for line in entropies_parsed] '但是,'line_split()]'中的''float'已经产生了'[float(n)''。如果我要求'print type(entropy)',它会返回''。但是,'print type(pressure)'会返回'我怎样才能让'entropy'变成''?如果我设法这样做,就不需要运行'for i,in entropy' –

1

我认为它的时间,你挖了一点到numpy的,为什么numpy的和Python的组合是真棒。这段代码可以做你正在寻找的东西。这里有很多,所以你只需花时间消化它。我创建了一个新的答案,因为原始答案有关于你的第一个问题的细节,但下面的代码实际上是你应该这样做的。如果您遇到错误,请确保您为分隔符等输入了正确的值。

import numpy as np 

# read in the data, and tranpose the columns into rows for easy unpacking 
entropy = np.loadtxt('entropies_parsed.dat', skiprows=1).T 
enthalpy, pressure = np.loadtxt('pressure_enthalpy_all_points.dat', skiprows=1).T 

gibbs = enthalpy - 298.15 * entropy/4.0 

# stack the data together into a new, 2-row array, and then transpose back into column format for file writing 
output_array = np.vstack((pressure, gibbs)).T 
np.savetxt('pressure_gibbs_all_points.dat', output_array, header="pressure\tgibbs", fmt="%0.06g")