Declaring variable compositions in a loop for gas.equilibrate

288 views
Skip to first unread message

Colin Gore

unread,
Apr 1, 2014, 4:25:16 PM4/1/14
to canter...@googlegroups.com
I'm having two problems that I'm sure relate to how I'm defining and calling variables in python.

I have successfully made a program (adapted from the adiabatic flame w/ soot formation tutorial) to calculate the equilibrium composition at different temperatures for a single starting composition and write that in addition to some other calculations to a .csv file.  What I can't do is output the mole fraction of graphite in the mixture (or other properties of the mixture for that matter).  I have annotated the attached file with where I want that to happen in the for loop.  I looked here but still can't seem to figure it out without getting errors.  Apologies for what is likely a problem with a simple solution.

What I want to do next is modify the for loop to repeat for incrementally changing initial compositions.  Whenever I try defining a gas composition with variables in it, I get 'nan' (not a number) for all species concentrations, presumably because my variables are being read as strings. 

For example, inserting variables like this:
import cantera as ct
gas = ct.Solution('gri30.xml')
c1= 0.1
c2= 0.9
gas.X= 'H2O:c1, CH4:c2'
gas()

gives 'nan' for all outputs.  How can I define species compositions with variable in a way that this does not happen?

Thanks,
Colin
hydrocarbon equilibria- with for loop.py

Bryan W. Weber

unread,
Apr 1, 2014, 8:22:30 PM4/1/14
to canter...@googlegroups.com
Hi Colin,

The easiest way to use variables in Python strings is the format method of strings. In your case, this would be

gas.X = 'H2O:{0}, CH4:{1}'.format(c1,c2)

I haven't looked at your python script yet :-)

Hope it helps,
Bryan

Colin Gore

unread,
Apr 1, 2014, 11:27:28 PM4/1/14
to canter...@googlegroups.com
Bryan,

That solved my second problem.  Thanks for an easy fix.

Thanks kindly,
Colin

Bryan W. Weber

unread,
Apr 2, 2014, 8:32:50 AM4/2/14
to canter...@googlegroups.com
Hi Colin,

So the problem seems to be that you're mixing using the gas object and the mix object. First of all, getting the species mole fractions from a Solution object in a Mixture is not a good idea, because it may not give you the correct answer. For instance, you define your mixture to have 0 moles of the carbon Solution, but if you type carbon['C(gr)'].X you get 1, which is incorrect (it should be zero). Instead, you should index the whole array of Mixture species moles, like so

cgr_idx = mix.species_index(1,'C(gr)')
o2_idx
= mix.species_index(0,'O2')
xeq
= mix.species_moles
Cs = xeq[cgr_idx]
PO2
= xeq[o2_idx]

Then you don't need to index the variables when you print them either (i.e. PO2[0] can be removed). Unfortunately, the Mixture class isn't documented in the online docs (yet), but you can read the source code here: https://code.google.com/p/cantera/source/browse/cantera/trunk/interfaces/cython/cantera/mixture.pyx

Finally (and you didn't ask for this advice, but I'm going to tell you anyways :-D), when working with files in Python, it is better to use the with construct to handle the file opening/closing. In your case, you can replace the

csvfile = open(csv_file,'a')

line with

with open(csv_file,'a') as csvfile:

and indent all the remaining lines by four spaces. You should also delete the line csvfile.close(). The with construct automatically handles opening and closing the file, even if your script generates an error, so you don't get randomly locked files.

Hope it helps,
Bryan

Bryan W. Weber

unread,
Apr 2, 2014, 8:41:34 AM4/2/14
to canter...@googlegroups.com
I wish Google Groups had an edit button....

Anyways, I should note that you won't get mole fractions from the Mixture methods, you'll get the number of moles. You'll have to compute the mole fraction yourself. 

Also, when I said "...carbon['C(gr)'].X you get 1..." of course, this is the expected answer for the Solution. What I meant was, that's not the number of moles of graphite in the Mixture because you said the number of moles of graphite should be zero.

Finally, I should have said "the *new* Mixture class isn't documented", because the old one is indeed documented, as you linked. The main difference should be the names of the methods/properties, which you can find in the source code I linked to.

Bryan

Ray Speth

unread,
Apr 2, 2014, 12:16:21 PM4/2/14
to canter...@googlegroups.com
I've updated the online docs (both for the 2.1.x branch and the development version) to include the documentation for the Mixture class of the new Python module.

Ray

Colin Gore

unread,
Apr 2, 2014, 4:36:53 PM4/2/14
to canter...@googlegroups.com
Excellent, thanks to both of you.  I made modifications last evening so it will easily increment the compositions.  I'll get to work on the other parts now that the online docs are updated. 

I intentionally mixed the use of gas and mix objects because I'm calculating a voltage based solely on the gas phase PO2, but then want to know the total # moles or mole fraction of solid carbon that formed.  I like that by indexing the whole shebang like you instruct, I can calculate those myself and be certain each value is normalized correctly, as well as pick out particular values much more easily.

And thanks for the additional tip on using the with construct, Bryan.  I'll improve that.  I borrowed that chunk from one of the tutorial files, so perhaps it should be updated there as well. 
Reply all
Reply to author
Forward
0 new messages