forked from rwest/RMG-Visualizer
-
Notifications
You must be signed in to change notification settings - Fork 1
/
toMixMaster.py
140 lines (120 loc) · 3.91 KB
/
toMixMaster.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
# this version updated by RHW in January 2009
"""
load the chem.inp file from RMG and get it ready for mixmaster
"""
import os, sys, shutil
#from Cantera import *
#from Cantera.Reactor import *o
#from Cantera.Func import *
#from math import *
#import random
#import pylab
#import scipy
#from pylab import *
#from scipy import *
#from scipy.optimize import leastsq
# Get folder with RMG results in
from RMG_results_path import RMGworkingDir
# convert the chemkin file from RMG into a cantera file chem.cti
from Cantera import ck2cti
infile='chem.inp'
oldpath=os.path.join(RMGworkingDir,'chemkin',infile)
#oldpath=os.path.join(RMGworkingDir,infile)
newpath=os.path.join(os.getcwd(),infile)
print "copying %s to %s"%(oldpath,newpath)
shutil.copy2(oldpath, newpath) # copy it to the current folder
thermodb=''
trandb=''
nm='chem'
ck2cti.ck2cti(infile = infile, thermodb = thermodb, trandb = trandb, idtag = nm, debug=0, validate=1)
# convert the Final_Model.txt into approprita CSV file
#print "NB ForMixMaster.csv is wrong. MixMaster wants MASS fractions and we are giving it MOLE fractions"
print "ForMixMaster.csv now contains mass fractions, as required by MixMaster"
massesfilename='MolarMasses.txt'
print "Reading molar masses from",massesfilename
massesfile=file(massesfilename)
massesdict=dict()
for line in massesfile:
(species,mass)=line.split()
massesdict[species]=mass
massesfile.close()
temperature=273+150
pressure=208*101325
print " using these settings:\n Temperature: %f K \t Pressure: %f Pa\n"%(temperature,pressure)
# load file
resultFile='Final_Model.txt'
oldpath=os.path.join(RMGworkingDir,resultFile)
newpath=os.path.join(os.getcwd(),resultFile)
print "copying %s to %s"%(oldpath,newpath)
shutil.copy2(oldpath, newpath) # copy it to the current folder
resultFile=file(newpath)
# search for "Mole Fraction Profile Output"
line=resultFile.next()
while (line.find('Mole Fraction Profile Output')<0):
line=resultFile.next()
# add "T \t P" to the following line
titles=resultFile.next()
print "Species:",titles
output=titles.strip()+"\tT\tP\tnothing\n"
items=titles.split()
assert items[0]=='Time'
speciesnames=items[1:]
masses=list()
for species in speciesnames:
masses.append(float(massesdict[species]))
# add the temperature IN KELVIN and pressure IN PASCAL to all the following nonblank lines
line=resultFile.next()
while (line.strip()):
massfractions=[]
massfractionsum = 0
items = line.split()
time = items[0]
molefracs = items[1:]
for i,molefrac in enumerate(molefracs):
massfrac = float(molefrac)*masses[i]
massfractions.append(massfrac)
massfractionsum += massfrac
massfractions = [str(m/massfractionsum) for m in massfractions]
output += str(time)+'\t'
output += '\t'.join(massfractions)
output += "\t%f\t%f\t0\n"%(temperature,pressure)
line=resultFile.next()
# turn whitespaces into commas
# save the output
outputFile=file('ForMixMaster.csv','w')
outputFile.write(output.replace('\t',','))
outputFile.close()
### make pretty table of species
import ctml_writer
from ctml_writer import *
# these lists store top-level entries
ctml_writer._elements = []
ctml_writer._species = []
ctml_writer._speciesnames = []
ctml_writer._phases = []
ctml_writer._reactions = []
ctml_writer._atw = {}
ctml_writer._enames = {}
ctml_writer._valsp = ''
ctml_writer._valrxn = ''
ctml_writer._valexport = ''
ctml_writer._valfmt = ''
execfile('chem.cti')
#for rxn in ctml_writer._reactions:
# for side in [rxn._r,rxn._p]:
# for sp in side:
# num=side[sp]
# if num!=1: print num,
# print sp
import jinja2
env = jinja2.Environment(loader = jinja2.FileSystemLoader('templates'))
template = env.get_template('rxnlist.html')
outstring=template.render(title=infile, reactionList=ctml_writer._reactions)
outfile=file('ReactionList'+'.html','w')
outfile.write(outstring)
outfile.close()
#print outstring
# load mixmaster
from MixMaster import MixMaster
#o=MixMaster()
#o.loadmech('','chem.cti')