Skip to content

Commit 50350d9

Browse files
committed
add MBSampledReactor write input file test
1 parent d5285be commit 50350d9

File tree

3 files changed

+96
-38
lines changed

3 files changed

+96
-38
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: 85 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
###############################################################################
2929

3030
from unittest.mock import patch
31-
31+
import rmgpy
3232
import rmgpy.rmg.input as inp
3333
from rmgpy.rmg.main import RMG
3434
from rmgpy.rmg.model import CoreEdgeReactionModel
@@ -472,8 +472,9 @@ def test_write_superminimal_input(self):
472472
"""
473473
Test that we can write superminimal input file and read it back in with the same values.
474474
"""
475-
476-
superminimal_input_file = '../../../examples/rmg/superminimal/input.py'
475+
import os
476+
import rmgpy
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()
@@ -506,7 +507,6 @@ def test_write_superminimal_input(self):
506507
assert term.species.label == termination_converstion_species
507508

508509
# clean up
509-
import os
510510
os.remove(superminimal_output_file)
511511

512512
@pytest.mark.skip(reason="Slow test that runs a full RMG job")
@@ -515,9 +515,10 @@ def test_write_superminimal_and_run(self):
515515
Test that we can write superminimal input file and then run RMG without errors
516516
"""
517517
import os
518+
import rmgpy
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')
@@ -528,7 +529,7 @@ def test_write_superminimal_and_run(self):
528529

529530
# run RMG with the new input file
530531
import subprocess
531-
subprocess.run(['python', '../../../rmg.py', superminimal_output_file], check=True)
532+
subprocess.run(['python', os.path.join(rmgpy.settings['test_data.directory'], '../../../rmg.py'), superminimal_output_file], check=True)
532533

533534
# clean up
534535
shutil.rmtree(new_run_dir)
@@ -537,8 +538,9 @@ def test_write_min_surf_input(self):
537538
"""
538539
Test that we can write the minimal surface input file and read it back in with the same values.
539540
"""
540-
541-
min_surf_input_file = '../../../examples/rmg/minimal_surface/input.py'
541+
import os
542+
import rmgpy
543+
min_surf_input_file = os.path.join(rmgpy.settings['test_data.directory'], '../../../examples/rmg/minimal_surface/input.py')
542544
min_surf_output_file = 'temp_min_surf_input.py'
543545

544546
rmg = RMG()
@@ -587,7 +589,6 @@ def test_write_min_surf_input(self):
587589
assert term.ratio == termination_ratio
588590

589591
# clean up
590-
import os
591592
os.remove(min_surf_output_file)
592593

593594
@pytest.mark.skip(reason="Slow test that runs a full RMG job")
@@ -596,9 +597,10 @@ def test_write_min_surf_and_run(self):
596597
Test that we can write minimal surface input file and then run RMG without errors
597598
"""
598599
import os
600+
import rmgpy
599601
import shutil
600602

601-
min_surf_input_file = '../../../examples/rmg/minimal_surface/input.py'
603+
min_surf_input_file = os.path.join(rmgpy.settings['test_data.directory'], '../../../examples/rmg/minimal_surface/input.py')
602604
new_run_dir = 'temp_min_surf_run'
603605
os.makedirs(new_run_dir, exist_ok=True)
604606
min_surf_output_file = os.path.join(new_run_dir, 'temp_min_surf_input.py')
@@ -609,7 +611,7 @@ def test_write_min_surf_and_run(self):
609611

610612
# run RMG with the new input file
611613
import subprocess
612-
subprocess.run(['python', '../../../rmg.py', min_surf_output_file], check=True)
614+
subprocess.run(['python', os.path.join(rmgpy.settings['test_data.directory'], '../../../rmg.py'), min_surf_output_file], check=True)
613615

614616
# clean up
615617
shutil.rmtree(new_run_dir)
@@ -619,8 +621,9 @@ def test_write_liquid_cat_input(self):
619621
"""
620622
Test that we can write liquid catalyst input file and read it back in with the same values.
621623
"""
622-
623-
liquid_cat_input_file = '../../../examples/rmg/liquid_cat/input.py'
624+
import os
625+
import rmgpy
626+
liquid_cat_input_file = os.path.join(rmgpy.settings['test_data.directory'], '../../../examples/rmg/liquid_cat/input.py')
624627
liquid_cat_output_file = 'temp_liquid_cat_input.py'
625628

626629
rmg = RMG()
@@ -651,7 +654,6 @@ def test_write_liquid_cat_input(self):
651654
assert rmg1.reaction_systems[0].terminations[0][1] == termination_conversion
652655

653656
# clean up
654-
import os
655657
os.remove(liquid_cat_output_file)
656658

657659
@pytest.mark.skip(reason="Slow test that runs a full RMG job")
@@ -660,9 +662,10 @@ def test_write_liquid_cat_and_run(self):
660662
Test that we can write liquid catalyst input file and then run RMG without errors
661663
"""
662664
import os
665+
import rmgpy
663666
import shutil
664667

665-
liquid_cat_input_file = '../../../examples/rmg/liquid_cat/input.py'
668+
liquid_cat_input_file = os.path.join(rmgpy.settings['test_data.directory'], '../../../examples/rmg/liquid_cat/input.py')
666669
new_run_dir = 'temp_liquid_cat_run'
667670
os.makedirs(new_run_dir, exist_ok=True)
668671
liquid_cat_output_file = os.path.join(new_run_dir, 'temp_liquid_cat_input.py')
@@ -673,7 +676,7 @@ def test_write_liquid_cat_and_run(self):
673676

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

678681
# clean up
679682
shutil.rmtree(new_run_dir)
@@ -682,8 +685,9 @@ def test_write_liquid_input(self):
682685
"""
683686
Test that we can write the liquid reactor input file and read it back in with the same values.
684687
"""
685-
686-
liquid_input_file = '../../../examples/rmg/liquid_phase/input.py'
688+
import os
689+
import rmgpy
690+
liquid_input_file = os.path.join(rmgpy.settings['test_data.directory'], '../../../examples/rmg/liquid_phase/input.py')
687691
liquid_output_file = 'temp_liquid_input.py'
688692

689693
rmg = RMG()
@@ -712,7 +716,6 @@ def test_write_liquid_input(self):
712716
assert rmg1.solvent == solvent
713717

714718
# clean up
715-
import os
716719
os.remove(liquid_output_file)
717720

718721
@pytest.mark.skip(reason="Slow test that runs a full RMG job")
@@ -721,9 +724,10 @@ def test_write_liquid_and_run(self):
721724
Test that we can write liquid reactor input file and then run RMG without errors
722725
"""
723726
import os
727+
import rmgpy
724728
import shutil
725729

726-
liquid_input_file = '../../../examples/rmg/liquid_phase/input.py'
730+
liquid_input_file = os.path.join(rmgpy.settings['test_data.directory'], '../../../examples/rmg/liquid_phase/input.py')
727731
new_run_dir = 'temp_liquid_run'
728732
os.makedirs(new_run_dir, exist_ok=True)
729733
liquid_output_file = os.path.join(new_run_dir, 'temp_liquid_input.py')
@@ -734,7 +738,7 @@ def test_write_liquid_and_run(self):
734738

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

739743
# clean up
740744
shutil.rmtree(new_run_dir)
@@ -744,8 +748,9 @@ def test_write_constantVIdealGasReactor(self):
744748
"""
745749
Test that we can write constant volume ideal gas reactor input file and read it back in with the same values.
746750
"""
747-
748-
rms_constant_V_input_file = '../../../examples/rmg/rms_constant_V/input.py'
751+
import os
752+
import rmgpy
753+
rms_constant_V_input_file = os.path.join(rmgpy.settings['test_data.directory'], '../../../examples/rmg/rms_constant_V/input.py')
749754
rms_constant_V_output_file = 'temp_rms_constant_V_input.py'
750755

751756
rmg = RMG()
@@ -777,7 +782,6 @@ def test_write_constantVIdealGasReactor(self):
777782
assert rmg1.reaction_systems[0].terminations[1].time == termination_time
778783

779784
# clean up
780-
import os
781785
os.remove(rms_constant_V_output_file)
782786

783787
@pytest.mark.skip(reason="Slow test that runs a full RMG job")
@@ -786,9 +790,10 @@ def test_write_constantVIdealGasReactor_and_run(self):
786790
Test that we can write constant volume ideal gas reactor input file and then run RMG without errors
787791
"""
788792
import os
793+
import rmgpy
789794
import shutil
790795

791-
constant_V_input_file = '../../../examples/rmg/rms_constant_V/input.py'
796+
constant_V_input_file = os.path.join(rmgpy.settings['test_data.directory'], '../../../examples/rmg/rms_constant_V/input.py')
792797
new_run_dir = 'temp_constant_V_run'
793798
os.makedirs(new_run_dir, exist_ok=True)
794799
constant_V_output_file = os.path.join(new_run_dir, 'temp_constant_V_input.py')
@@ -799,7 +804,7 @@ def test_write_constantVIdealGasReactor_and_run(self):
799804

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

804809
# clean up
805810
shutil.rmtree(new_run_dir)
@@ -809,8 +814,9 @@ def test_write_constantTPdealGasReactor(self):
809814
"""
810815
Test that we can write constant TP ideal gas reactor input file and read it back in with the same values.
811816
"""
812-
813-
rms_constant_TP_input_file = '../../../examples/rmg/nox_transitory_edge/input.py'
817+
import os
818+
import rmgpy
819+
rms_constant_TP_input_file = os.path.join(rmgpy.settings['test_data.directory'], '../../../examples/rmg/nox_transitory_edge/input.py')
814820
rms_constant_TP_output_file = 'temp_constant_TP_input.py'
815821

816822
rmg = RMG()
@@ -842,7 +848,6 @@ def test_write_constantTPdealGasReactor(self):
842848
assert rmg1.reaction_systems[0].terminations[1].time == termination_time
843849

844850
# clean up
845-
import os
846851
os.remove(rms_constant_TP_output_file)
847852

848853
@pytest.mark.skip(reason="Slow test that runs a full RMG job")
@@ -851,9 +856,10 @@ def test_write_constantTPIdealGasReactor_and_run(self):
851856
Test that we can write constant TP ideal gas reactor input file and then run RMG without errors
852857
"""
853858
import os
859+
import rmgpy
854860
import shutil
855861

856-
constant_TP_input_file = '../../../examples/rmg/nox_transitory_edge/input.py'
862+
constant_TP_input_file = os.path.join(rmgpy.settings['test_data.directory'], '../../../examples/rmg/nox_transitory_edge/input.py')
857863
new_run_dir = 'temp_constant_TP_run'
858864
os.makedirs(new_run_dir, exist_ok=True)
859865
constant_TP_output_file = os.path.join(new_run_dir, 'temp_constant_TP_input.py')
@@ -864,7 +870,7 @@ def test_write_constantTPIdealGasReactor_and_run(self):
864870

865871
# run RMG with the new input file
866872
import subprocess
867-
subprocess.run(['python', '../../../rmg.py', '-t', '00:00:01:30', constant_TP_output_file], check=True)
873+
subprocess.run(['python', os.path.join(rmgpy.settings['test_data.directory'], '../../../rmg.py'), '-t', '00:00:01:30', constant_TP_output_file], check=True)
868874

869875
# clean up
870876
shutil.rmtree(new_run_dir)
@@ -874,8 +880,9 @@ def test_write_constantTVLiquidReactor(self):
874880
"""
875881
Test that we can write constant TV liquid reactor input file and read it back in with the same values.
876882
"""
877-
878-
rms_constant_TV_input_file = '../../../test/regression/RMS_CSTR_liquid_oxidation/input.py'
883+
import os
884+
import rmgpy
885+
rms_constant_TV_input_file = os.path.join(rmgpy.settings['test_data.directory'], '../../../test/regression/RMS_CSTR_liquid_oxidation/input.py')
879886
rms_constant_TV_output_file = 'temp_constant_TV_liquid_input.py'
880887

881888
rmg = RMG()
@@ -907,7 +914,6 @@ def test_write_constantTVLiquidReactor(self):
907914
assert rmg1.reaction_systems[0].terminations[1].time == termination_time
908915

909916
# clean up
910-
import os
911917
os.remove(rms_constant_TV_output_file)
912918

913919
@pytest.mark.skip(reason="Slow test that runs a full RMG job")
@@ -916,9 +922,10 @@ def test_write_constantTVLiquidReactor_and_run(self):
916922
Test that we can write constant TV liquid reactor input file and then run RMG without errors
917923
"""
918924
import os
925+
import rmgpy
919926
import shutil
920927

921-
constant_TV_input_file = '../../../test/regression/RMS_CSTR_liquid_oxidation/input.py'
928+
constant_TV_input_file = os.path.join(rmgpy.settings['test_data.directory'], '../../../test/regression/RMS_CSTR_liquid_oxidation/input.py')
922929
new_run_dir = 'temp_constant_TV_run'
923930
os.makedirs(new_run_dir, exist_ok=True)
924931
constant_TV_output_file = os.path.join(new_run_dir, 'temp_constant_TV_input.py')
@@ -929,7 +936,49 @@ def test_write_constantTVLiquidReactor_and_run(self):
929936

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

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

0 commit comments

Comments
 (0)