Skip to content

Commit 5ef7ef2

Browse files
committed
Liquid reactor concentration accepts Quantity init
It's very inconvenient to make a round trip read/write of RMG input files when the units of the concentration aren't saved in the reactor system. So this updates the liquid reactor (and input tests) to accept Quantity types. It still accepts floats, but RMG's input module now passes it in as a quantity. This also fixes an error where someone forgot to use deepcopy and so the Temperature was getting added to the dictionary of initial conditions instead of just to sensitive conditions
1 parent 9ceea20 commit 5ef7ef2

File tree

3 files changed

+18
-9
lines changed

3 files changed

+18
-9
lines changed

rmgpy/rmg/input.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -984,7 +984,7 @@ def liquid_reactor(temperature,
984984
if sensitivityConcentrations is None or sensitivityTemperature is None:
985985
sens_conditions = None
986986
else:
987-
sens_conditions = sensitivityConcentrations
987+
sens_conditions = deepcopy(sensitivityConcentrations)
988988
sens_conditions['T'] = Quantity(sensitivityTemperature).value_si
989989

990990
system = LiquidReactor(T, initialConcentrations, nSims, termination, sensitive_species, sensitivityThreshold,

rmgpy/solver/liquid.pyx

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,10 @@ cdef class LiquidReactor(ReactionSystem):
7373
self.Trange = [Quantity(t) for t in T]
7474

7575
self.P = Quantity(100000., 'kPa') # Arbitrary high pressure (1000 Bar) to get reactions in the high-pressure limit!
76-
self.initial_concentrations = initial_concentrations # should be passed in SI
76+
for spec, conc in initial_concentrations.items(): # convert to Quantity if given as float (SI mol/m^3)
77+
if isinstance(initial_concentrations[spec], float):
78+
initial_concentrations[spec] = ScalarQuantity(initial_concentrations[spec], 'mol/m^3')
79+
self.initial_concentrations = initial_concentrations # should be passed in as Quantity to preserve units
7780
self.V = 0 # will be set from initial_concentrations in initialize_model
7881
self.constant_volume = True
7982
self.viscosity = 0 # in Pa*s
@@ -91,8 +94,6 @@ cdef class LiquidReactor(ReactionSystem):
9194
"""
9295
initial_concentrations = {}
9396
for label, moleFrac in self.initial_concentrations.items():
94-
if label == 'T':
95-
continue
9697
initial_concentrations[species_dict[label]] = moleFrac
9798
self.initial_concentrations = initial_concentrations
9899

@@ -202,7 +203,7 @@ cdef class LiquidReactor(ReactionSystem):
202203

203204
for spec, conc in self.initial_concentrations.items():
204205
i = self.get_species_index(spec)
205-
self.core_species_concentrations[i] = conc
206+
self.core_species_concentrations[i] = conc.value_si
206207

207208
V = 1.0 / np.sum(self.core_species_concentrations)
208209
self.V = V

test/rmgpy/rmg/inputTest.py

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -280,10 +280,18 @@ def test_liquid_reactor_concentrations(self):
280280
global rmg
281281
reactor = rmg.reaction_systems[0]
282282

283-
# Values get converted to default SI units, mol/m^3
284-
assert reactor.initial_concentrations["A"] == 300
285-
assert reactor.initial_concentrations["B"] == 200
286-
assert reactor.initial_concentrations["C"] == 100
283+
# Values are saved as Quantity to preserve units, but get changed to SI units, mol/m^3 in the set_initial_conditions function
284+
assert reactor.initial_concentrations["A"].value_si == 300
285+
assert reactor.initial_concentrations["B"].value_si == 200
286+
assert reactor.initial_concentrations["C"].value_si == 100
287+
288+
rmg.reaction_systems[0].initialize_model(list(reactor.initial_concentrations.keys()), [], [], [])
289+
rmg.reaction_systems[0].set_initial_conditions()
290+
assert rmg.reaction_systems[0].core_species_concentrations[0] == 300
291+
assert rmg.reaction_systems[0].core_species_concentrations[1] == 200
292+
assert rmg.reaction_systems[0].core_species_concentrations[2] == 100
293+
# Test that the units are saved internally in the reactor as mol/m^3
294+
287295

288296
def test_surface_reactor_mole_fractions(self):
289297
"""Test that SurfaceReactor mole fractions are set properly"""

0 commit comments

Comments
 (0)