简体   繁体   中英

Variables that have been assigned in a line.split() command, are not read into a function

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))

I think its time you dug a bit more into numpy and why the combination of numpy and python are awesome. This code does what you are looking for. There's a lot here, so you'll have to just spend time digesting it. I created a new answer because the original answer has specifics about your first question, but the below code is really how you should be doing this. If you get errors make sure you are putting in the correct values for delimiters, etc.

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")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM