In this code the variables pressure
and enthalpy
, that come from a [float(n) for n in line.split()]
command, are not read into a function:
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()
Only two files in this folder are necessary to run this code:
pressure_enthalpy_all_points.dat
:
# pressure enthalpy
2 3
5 4
3.5 2
entropies_parsed.dat
:
# entropies
0.5
0.2
0.47
This is the best I could achieve, and the indentation position is right, from my knowledge.
However,
this code gives a file, 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
which is wrong.
I would appreciate if you could help me please.
your output file seems to show values that match the math in your code, so the only thing I can see is that you have 9 calculations where you were expecting 3. This is because you have a nested loop, so you are first looping over pressure, and and then looping over entropy. So you calculate Gibbs for 3 values of entropy at p = 2.0, then again for p = 5.0, and finally for p = 3.5, so you have 9 calculations. If you are only looking for 3 calculations:
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))