-
Notifications
You must be signed in to change notification settings - Fork 1
/
test_chemReduce.py
202 lines (149 loc) · 6.35 KB
/
test_chemReduce.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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
import chemReduce
import Cantera
import unittest
import numpy
class TestCoeffIdentities(unittest.TestCase):
def setUp(self):
#methodName='runTest', file_name='gri30.cti', temp=1000,
#press=Cantera.OneAtm, mass_frac='CH4:.05, O2:.075, N2:.9', atol=1e-6,
#rtol=1e-6):
file_name = 'gri30.cti'
temp = 1000
press = Cantera.OneAtm
mass_frac = 'CH4:.05, O2:.075, N2:.9"'
atol = 1e-6
rtol = 1e-6
# Initialize thermodynamic and kinetic data and set state
self.gas=Cantera.IdealGasMix('gri30.cti')
self.gas.set(T=temp, P=press, Y=mass_frac)
self.state = numpy.zeros(self.gas.nSpecies() + 1)
self.state[0] = self.gas.temperature()
self.state[1:] = self.gas.massFractions()
# Calculate condition-independent data and store
(self.stoich_matrix,
self.mass_stoich_prod) = chemReduce.calc_cond_indep_data(self.gas)
# Calculate condition-dependent data and store
(self.rxn_rate,
self.cp_mass,
self.enthalpy_mass,
self.rho) = chemReduce.calc_cond_dep_data(self.state, self.gas)
self.atol = numpy.ones(self.gas.nSpecies() + 1) * atol
self.rtol = numpy.ones(self.gas.nSpecies() + 1) * rtol
self.float_tol = 1e-6
def test_row_sums(self):
"""
Purpose: The sum of the entries in coeffs_temp should equal temp_dot.
The sum of the entries in each row of coeffs_y should equal
numpy.asarray([ydot]).transpose().
Arguments:
None
Returns:
None
"""
(coeffs_temp,
coeffs_y,
rhs_temp,
rhs_y) = chemReduce.error_constraint_data(self.state,
self.gas, self.mass_stoich_prod, self.atol, self.rtol)
# Test identity for temperature
rhs_temp_test = self.atol[0] + self.rtol[0] * numpy.sum(coeffs_temp)
self.assertAlmostEqual(numpy.max(abs(rhs_temp_test - rhs_temp)), 0,
delta=self.float_tol)
# Test identity for each species
rhs_y_test = numpy.zeros(self.gas.nSpecies())
for j in range(0, self.gas.nSpecies()):
rhs_y_test[j] = (self.atol[j + 1] + self.rtol[j + 1] *
numpy.sum(coeffs_y[j,:]))
self.assertAlmostEqual(numpy.max(abs(rhs_y_test - rhs_y)), 0,
delta=self.float_tol)
def test_col_sums(self):
"""
Purpose: The sum of over each column in coeffs_y, where each row is
scaled by enthalpy_mass[j] / cp_mass, should equal coeffs_t.
Arguments:
None
Returns:
None
"""
(coeffs_temp,
coeffs_y,
_,
_) = chemReduce.error_constraint_data(self.state,
self.gas, self.mass_stoich_prod, self.atol, self.rtol)
row_total = numpy.zeros(self.gas.nReactions())
for j in range(0, self.gas.nSpecies()):
row_total += self.enthalpy_mass[j] * coeffs_y[j] / self.cp_mass
self.assertAlmostEqual(numpy.max(abs(row_total - coeffs_temp)), 0,
delta = self.float_tol)
def test_naive_summation(self):
"""
Purpose: Calculate the entries of coeffs_temp, coeffs_y, rhs_temp,
rhs_y using loops instead of vectorizing. Will be slow, but should
yield same answer.
Arguments:
None
Returns:
None
"""
molarMass = self.gas.molarMasses()
stoichMatrix = (self.gas.productStoichCoeffs() -
self.gas.reactantStoichCoeffs())
coeffs_y_loop = numpy.zeros((self.gas.nSpecies(),
self.gas.nReactions()))
coeffs_temp_loop = numpy.zeros(self.gas.nReactions())
for i in range(0, self.gas.nReactions()):
coeffs_temp_loop[i] = numpy.sum(
[self.enthalpy_mass[j] * molarMass[j] * stoichMatrix[j,i] *
self.rxn_rate[i] / (self.cp_mass * self.rho)
for j in range(0, self.gas.nSpecies())])
for j in range(0, self.gas.nSpecies()):
coeffs_y_loop[j,i] = (molarMass[j] * stoichMatrix[j,i] *
self.rxn_rate[i] / self.rho)
temp_dot = numpy.sum(coeffs_temp_loop)
y_dot = numpy.sum(coeffs_y_loop, axis=1)
rhs_temp_loop = self.atol[0] + self.rtol[0] * abs(temp_dot)
rhs_y_loop = numpy.zeros(self.gas.nSpecies())
for j in range(0, self.gas.nSpecies()):
rhs_y_loop[j] = self.atol[j+1] + self.rtol[j+1] * abs(y_dot[j])
(coeffs_temp,
coeffs_y,
rhs_temp,
rhs_y) = chemReduce.error_constraint_data(self.state,
self.gas, self.mass_stoich_prod, self.atol, self.rtol)
self.assertAlmostEqual(rhs_temp, rhs_temp_loop, delta=self.float_tol)
self.assertAlmostEqual(numpy.max(abs(rhs_y - rhs_y_loop)), 0,
delta=self.float_tol)
self.assertAlmostEqual(numpy.max(abs(coeffs_temp - coeffs_temp)), 0,
delta=self.float_tol)
self.assertAlmostEqual(numpy.max(abs(coeffs_y - coeffs_y_loop)), 0,
delta=self.float_tol)
def test_run_reaction_elim(self):
"""
Purpose: Just run reaction_elim on a simple test case to make sure
there are no syntax errors.
Arguments:
None
Returns:
None
"""
chemReduce.reaction_elim([self.state], self.gas, self.atol, self.rtol)
def test_run_reaction_and_species_elim(self):
"""
Purpose: Just run reaction_and_species_elim on a simple test case
to make sure there are no syntax errors.
Arguments:
None
Returns:
None
"""
chemReduce.reaction_and_species_elim([self.state], self.gas,
self.atol, self.rtol)
if __name__ == '__main__':
unittest.main()
#suite = unittest.TestSuite()
#suite.addTest(TestCoeffIdentities('test_naive_summation'))
#suite.addTest(TestCoeffIdentities('test_col_sums'))
#suite.addTest(TestCoeffIdentities('test_row_sums'))
#suite.addTest(TestCoeffIdentities('test_run_reaction_elim'))
#suite.addTest(TestCoeffIdentities('test_run_reaction_and_species_elim'))
#suite.debug()