2828###############################################################################
2929
3030from unittest .mock import patch
31-
31+ import os
32+ import rmgpy
3233import rmgpy .rmg .input as inp
3334from rmgpy .rmg .main import RMG
3435from 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