Skip to content

Commit fb8b428

Browse files
committed
add MBSampledReactor write input file test
1 parent c38d947 commit fb8b428

File tree

3 files changed

+53
-2
lines changed

3 files changed

+53
-2
lines changed

rmgpy/rmg/input.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1824,6 +1824,15 @@ def format_initial_mole_fractions(system):
18241824
continue
18251825
f.write(' "{0!s}": ({1:g},"{2!s}"),\n'.format(spcs, conc, 'mol/m^3'))
18261826
f.write(' },\n')
1827+
elif isinstance(system, MBSampledReactor):
1828+
f.write('mbsampledReactor(\n')
1829+
f.write(' temperature = ' + format_temperature(system) + '\n')
1830+
f.write(' pressure = ' + format_pressure(system) + '\n')
1831+
f.write(' initialMoleFractions={\n')
1832+
f.write(format_initial_mole_fractions(system))
1833+
f.write(' },\n')
1834+
f.write(' mbsamplingRate = ' + str(system.k_sampling.value_si) + ',\n')
1835+
f.write(' constantSpecies = ' + str([x.label for x in system.constantSpeciesList]) + ',\n')
18271836
else:
18281837
f.write('simpleReactor(\n')
18291838
f.write(' temperature = ' + format_temperature(system) + '\n')

rmgpy/rmg/main.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -804,8 +804,8 @@ def execute(self, initialize=True, **kwargs):
804804
self.done = False
805805

806806
# determine min and max values for T and P (don't determine P values for liquid reactors)
807-
self.Tmin = min([x.Trange[0].value_si if x.Trange else x.T.value_si for x in self.reaction_systems])
808-
self.Tmax = max([x.Trange[1].value_si if x.Trange else x.T.value_si for x in self.reaction_systems])
807+
self.Tmin = min([x.Trange[0].value_si if hasattr(x, "Trange") and x.Trange else x.T.value_si for x in self.reaction_systems])
808+
self.Tmax = max([x.Trange[1].value_si if hasattr(x, "Trange") and x.Trange else x.T.value_si for x in self.reaction_systems])
809809
try:
810810
self.Pmin = min([x.Prange[0].value_si if hasattr(x, "Prange") and x.Prange else x.P.value_si for x in self.reaction_systems])
811811
self.Pmax = max([x.Prange[1].value_si if hasattr(x, "Prange") and x.Prange else x.P.value_si for x in self.reaction_systems])

test/rmgpy/rmg/inputTest.py

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -933,3 +933,45 @@ def test_write_constantTVLiquidReactor_and_run(self):
933933

934934
# clean up
935935
shutil.rmtree(new_run_dir)
936+
937+
def test_MBSampledReactor_write(self):
938+
"""
939+
Test that we can write MB sampled reactor input file and read it back in with the same values.
940+
Note that the MBSampledReactor is not intended to be used with a standard RMG job, so there's no point in running it as a test
941+
"""
942+
943+
mbsampled_input_file = '../../../rmgpy/tools/data/sim/mbSampled/input.py'
944+
mbsampled_output_file = 'temp_mbsampled_input.py'
945+
946+
rmg = RMG()
947+
inp.read_input_file(mbsampled_input_file, rmg)
948+
949+
# read a bunch of values in from input file to check they are the same after writing
950+
T = rmg.reaction_systems[0].T.value_si
951+
P = rmg.reaction_systems[0].P.value_si
952+
sampling_rate = rmg.reaction_systems[0].k_sampling.value_si
953+
954+
initialMoleFractions = {k.label: v for k, v in rmg.reaction_systems[0].initial_mole_fractions.items()}
955+
956+
for term in rmg.reaction_systems[0].termination:
957+
if hasattr(term, 'time'):
958+
termination_time = term.time.value_si
959+
960+
inp.save_input_file(mbsampled_output_file, rmg)
961+
# read it back in and confirm all the values match
962+
rmg1 = RMG()
963+
inp.read_input_file(mbsampled_output_file, rmg1)
964+
assert rmg1.reaction_systems[0].T.value_si == T
965+
assert rmg1.reaction_systems[0].P.value_si == P
966+
assert rmg1.reaction_systems[0].k_sampling.value_si == sampling_rate
967+
968+
new_initialMoleFractions = {k.label: v for k, v in rmg1.reaction_systems[0].initial_mole_fractions.items()}
969+
assert pytest.approx(new_initialMoleFractions, rel=1e-4) == initialMoleFractions
970+
971+
for term in rmg1.reaction_systems[0].termination:
972+
if hasattr(term, 'time'):
973+
assert term.time.value_si == termination_time
974+
975+
# clean up
976+
import os
977+
os.remove(mbsampled_output_file)

0 commit comments

Comments
 (0)