Skip to content

Commit 317db5f

Browse files
committed
add MBSampledReactor write input file test
1 parent d5285be commit 317db5f

File tree

3 files changed

+73
-21
lines changed

3 files changed

+73
-21
lines changed

rmgpy/rmg/input.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1828,6 +1828,15 @@ def format_initial_mole_fractions(system):
18281828
continue
18291829
f.write(' "{0!s}": ({1:g},"{2!s}"),\n'.format(spcs, conc, 'mol/m^3'))
18301830
f.write(' },\n')
1831+
elif isinstance(system, MBSampledReactor):
1832+
f.write('mbsampledReactor(\n')
1833+
f.write(' temperature = ' + format_temperature(system) + '\n')
1834+
f.write(' pressure = ' + format_pressure(system) + '\n')
1835+
f.write(' initialMoleFractions={\n')
1836+
f.write(format_initial_mole_fractions(system))
1837+
f.write(' },\n')
1838+
f.write(' mbsamplingRate = ' + str(system.k_sampling.value_si) + ',\n')
1839+
f.write(' constantSpecies = ' + str([x.label for x in system.constantSpeciesList]) + ',\n')
18311840
else:
18321841
f.write('simpleReactor(\n')
18331842
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
@@ -807,8 +807,8 @@ def execute(self, initialize=True, **kwargs):
807807
self.done = False
808808

809809
# determine min and max values for T and P (don't determine P values for liquid reactors)
810-
self.Tmin = min([x.Trange[0].value_si if x.Trange else x.T.value_si for x in self.reaction_systems])
811-
self.Tmax = max([x.Trange[1].value_si if x.Trange else x.T.value_si for x in self.reaction_systems])
810+
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])
811+
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])
812812
try:
813813
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])
814814
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: 62 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,8 @@
2828
###############################################################################
2929

3030
from unittest.mock import patch
31-
31+
import os
32+
import rmgpy
3233
import rmgpy.rmg.input as inp
3334
from rmgpy.rmg.main import RMG
3435
from rmgpy.rmg.model import CoreEdgeReactionModel
@@ -473,7 +474,7 @@ def test_write_superminimal_input(self):
473474
Test that we can write superminimal input file and read it back in with the same values.
474475
"""
475476

476-
superminimal_input_file = '../../../examples/rmg/superminimal/input.py'
477+
superminimal_input_file = os.path.join(rmgpy.settings['test_data.directory'], '../../../examples/rmg/superminimal/input.py')
477478
superminimal_output_file = 'temp_superminimal_input.py'
478479

479480
rmg = RMG()
@@ -517,7 +518,7 @@ def test_write_superminimal_and_run(self):
517518
import os
518519
import shutil
519520

520-
superminimal_input_file = '../../../examples/rmg/superminimal/input.py'
521+
superminimal_input_file = os.path.join(rmgpy.settings['test_data.directory'], '../../../examples/rmg/superminimal/input.py')
521522
new_run_dir = 'temp_superminimal_run'
522523
os.makedirs(new_run_dir, exist_ok=True)
523524
superminimal_output_file = os.path.join(new_run_dir, 'temp_superminimal_input.py')
@@ -538,7 +539,7 @@ def test_write_min_surf_input(self):
538539
Test that we can write the minimal surface input file and read it back in with the same values.
539540
"""
540541

541-
min_surf_input_file = '../../../examples/rmg/minimal_surface/input.py'
542+
min_surf_input_file = os.path.join(rmgpy.settings['test_data.directory'], '../../../examples/rmg/minimal_surface/input.py')
542543
min_surf_output_file = 'temp_min_surf_input.py'
543544

544545
rmg = RMG()
@@ -598,7 +599,7 @@ def test_write_min_surf_and_run(self):
598599
import os
599600
import shutil
600601

601-
min_surf_input_file = '../../../examples/rmg/minimal_surface/input.py'
602+
min_surf_input_file = os.path.join(rmgpy.settings['test_data.directory'], '../../../examples/rmg/minimal_surface/input.py')
602603
new_run_dir = 'temp_min_surf_run'
603604
os.makedirs(new_run_dir, exist_ok=True)
604605
min_surf_output_file = os.path.join(new_run_dir, 'temp_min_surf_input.py')
@@ -620,7 +621,7 @@ def test_write_liquid_cat_input(self):
620621
Test that we can write liquid catalyst input file and read it back in with the same values.
621622
"""
622623

623-
liquid_cat_input_file = '../../../examples/rmg/liquid_cat/input.py'
624+
liquid_cat_input_file = os.path.join(rmgpy.settings['test_data.directory'], '../../../examples/rmg/liquid_cat/input.py')
624625
liquid_cat_output_file = 'temp_liquid_cat_input.py'
625626

626627
rmg = RMG()
@@ -662,7 +663,7 @@ def test_write_liquid_cat_and_run(self):
662663
import os
663664
import shutil
664665

665-
liquid_cat_input_file = '../../../examples/rmg/liquid_cat/input.py'
666+
liquid_cat_input_file = os.path.join(rmgpy.settings['test_data.directory'], '../../../examples/rmg/liquid_cat/input.py')
666667
new_run_dir = 'temp_liquid_cat_run'
667668
os.makedirs(new_run_dir, exist_ok=True)
668669
liquid_cat_output_file = os.path.join(new_run_dir, 'temp_liquid_cat_input.py')
@@ -673,7 +674,7 @@ def test_write_liquid_cat_and_run(self):
673674

674675
# run RMG with the new input file
675676
import subprocess
676-
subprocess.run(['python', '../../../rmg.py', '-t', '00:00:01:30', liquid_cat_output_file], check=True)
677+
subprocess.run(['python', os.path.join(rmgpy.settings['test_data.directory'], '../../../rmg.py'), '-t', '00:00:01:30', liquid_cat_output_file], check=True)
677678

678679
# clean up
679680
shutil.rmtree(new_run_dir)
@@ -683,7 +684,7 @@ def test_write_liquid_input(self):
683684
Test that we can write the liquid reactor input file and read it back in with the same values.
684685
"""
685686

686-
liquid_input_file = '../../../examples/rmg/liquid_phase/input.py'
687+
liquid_input_file = os.path.join(rmgpy.settings['test_data.directory'], '../../../examples/rmg/liquid_phase/input.py')
687688
liquid_output_file = 'temp_liquid_input.py'
688689

689690
rmg = RMG()
@@ -723,7 +724,7 @@ def test_write_liquid_and_run(self):
723724
import os
724725
import shutil
725726

726-
liquid_input_file = '../../../examples/rmg/liquid_phase/input.py'
727+
liquid_input_file = os.path.join(rmgpy.settings['test_data.directory'], '../../../examples/rmg/liquid_phase/input.py')
727728
new_run_dir = 'temp_liquid_run'
728729
os.makedirs(new_run_dir, exist_ok=True)
729730
liquid_output_file = os.path.join(new_run_dir, 'temp_liquid_input.py')
@@ -734,7 +735,7 @@ def test_write_liquid_and_run(self):
734735

735736
# run RMG with the new input file
736737
import subprocess
737-
subprocess.run(['python', '../../../rmg.py', '-t', '00:00:01:30', liquid_output_file], check=True)
738+
subprocess.run(['python', os.path.join(rmgpy.settings['test_data.directory'], '../../../rmg.py'), '-t', '00:00:01:30', liquid_output_file], check=True)
738739

739740
# clean up
740741
shutil.rmtree(new_run_dir)
@@ -745,7 +746,7 @@ def test_write_constantVIdealGasReactor(self):
745746
Test that we can write constant volume ideal gas reactor input file and read it back in with the same values.
746747
"""
747748

748-
rms_constant_V_input_file = '../../../examples/rmg/rms_constant_V/input.py'
749+
rms_constant_V_input_file = os.path.join(rmgpy.settings['test_data.directory'], '../../../examples/rmg/rms_constant_V/input.py')
749750
rms_constant_V_output_file = 'temp_rms_constant_V_input.py'
750751

751752
rmg = RMG()
@@ -788,7 +789,7 @@ def test_write_constantVIdealGasReactor_and_run(self):
788789
import os
789790
import shutil
790791

791-
constant_V_input_file = '../../../examples/rmg/rms_constant_V/input.py'
792+
constant_V_input_file = os.path.join(rmgpy.settings['test_data.directory'], '../../../examples/rmg/rms_constant_V/input.py')
792793
new_run_dir = 'temp_constant_V_run'
793794
os.makedirs(new_run_dir, exist_ok=True)
794795
constant_V_output_file = os.path.join(new_run_dir, 'temp_constant_V_input.py')
@@ -799,7 +800,7 @@ def test_write_constantVIdealGasReactor_and_run(self):
799800

800801
# run RMG with the new input file
801802
import subprocess
802-
subprocess.run(['python', '../../../rmg.py', '-t', '00:00:01:30', constant_V_output_file], check=True)
803+
subprocess.run(['python', os.path.join(rmgpy.settings['test_data.directory'], '../../../rmg.py'), '-t', '00:00:01:30', constant_V_output_file], check=True)
803804

804805
# clean up
805806
shutil.rmtree(new_run_dir)
@@ -810,7 +811,7 @@ def test_write_constantTPdealGasReactor(self):
810811
Test that we can write constant TP ideal gas reactor input file and read it back in with the same values.
811812
"""
812813

813-
rms_constant_TP_input_file = '../../../examples/rmg/nox_transitory_edge/input.py'
814+
rms_constant_TP_input_file = os.path.join(rmgpy.settings['test_data.directory'], '../../../examples/rmg/nox_transitory_edge/input.py')
814815
rms_constant_TP_output_file = 'temp_constant_TP_input.py'
815816

816817
rmg = RMG()
@@ -853,7 +854,7 @@ def test_write_constantTPIdealGasReactor_and_run(self):
853854
import os
854855
import shutil
855856

856-
constant_TP_input_file = '../../../examples/rmg/nox_transitory_edge/input.py'
857+
constant_TP_input_file = os.path.join(rmgpy.settings['test_data.directory'], '../../../examples/rmg/nox_transitory_edge/input.py')
857858
new_run_dir = 'temp_constant_TP_run'
858859
os.makedirs(new_run_dir, exist_ok=True)
859860
constant_TP_output_file = os.path.join(new_run_dir, 'temp_constant_TP_input.py')
@@ -875,7 +876,7 @@ def test_write_constantTVLiquidReactor(self):
875876
Test that we can write constant TV liquid reactor input file and read it back in with the same values.
876877
"""
877878

878-
rms_constant_TV_input_file = '../../../test/regression/RMS_CSTR_liquid_oxidation/input.py'
879+
rms_constant_TV_input_file = os.path.join(rmgpy.settings['test_data.directory'], '../../../test/regression/RMS_CSTR_liquid_oxidation/input.py')
879880
rms_constant_TV_output_file = 'temp_constant_TV_liquid_input.py'
880881

881882
rmg = RMG()
@@ -918,7 +919,7 @@ def test_write_constantTVLiquidReactor_and_run(self):
918919
import os
919920
import shutil
920921

921-
constant_TV_input_file = '../../../test/regression/RMS_CSTR_liquid_oxidation/input.py'
922+
constant_TV_input_file = os.path.join(rmgpy.settings['test_data.directory'], '../../../test/regression/RMS_CSTR_liquid_oxidation/input.py')
922923
new_run_dir = 'temp_constant_TV_run'
923924
os.makedirs(new_run_dir, exist_ok=True)
924925
constant_TV_output_file = os.path.join(new_run_dir, 'temp_constant_TV_input.py')
@@ -929,7 +930,49 @@ def test_write_constantTVLiquidReactor_and_run(self):
929930

930931
# run RMG with the new input file
931932
import subprocess
932-
subprocess.run(['python', '../../../rmg.py', '-t', '00:00:01:30', constant_TV_output_file], check=True)
933+
subprocess.run(['python', os.path.join(rmgpy.settings['test_data.directory'], '../../../rmg.py'), '-t', '00:00:01:30', constant_TV_output_file], check=True)
933934

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

0 commit comments

Comments
 (0)