Skip to content

Commit

Permalink
add NO_JULIA and requires_rms throughout
Browse files Browse the repository at this point in the history
  • Loading branch information
JacksonBurns committed Mar 11, 2024
1 parent bfaee1c commit 859dad7
Show file tree
Hide file tree
Showing 5 changed files with 118 additions and 108 deletions.
5 changes: 4 additions & 1 deletion rmgpy/rmg/input.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,10 @@
from rmgpy.solver.surface import SurfaceReactor
from rmgpy.util import as_list
from rmgpy.data.surface import MetalDatabase
from rmgpy.rmg.reactors import Reactor, ConstantVIdealGasReactor, ConstantTLiquidSurfaceReactor, ConstantTVLiquidReactor, ConstantTPIdealGasReactor
from rmgpy.data.vaporLiquidMassTransfer import liquidVolumetricMassTransferCoefficientPowerLaw
from rmgpy.molecule.fragment import Fragment
from rmgpy.rmg.reactionmechanismsimulator_reactors import Reactor, ConstantVIdealGasReactor, ConstantTLiquidSurfaceReactor, ConstantTVLiquidReactor, ConstantTPIdealGasReactor, NO_JULIA


################################################################################

Expand Down Expand Up @@ -1558,6 +1559,8 @@ def read_input_file(path, rmg0):
exec(f.read(), global_context, local_context)
except (NameError, TypeError, SyntaxError) as e:
logging.error('The input file "{0}" was invalid:'.format(full_path))
if NO_JULIA:
logging.error("During runtime, import of Julia dependencies failed. To use phase systems and RMS reactors, install RMG-Py with RMS.")
logging.exception(e)
raise
finally:
Expand Down
61 changes: 36 additions & 25 deletions rmgpy/rmg/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,8 @@
from rmgpy.tools.plot import plot_sensitivity
from rmgpy.tools.uncertainty import Uncertainty, process_local_results
from rmgpy.yml import RMSWriter
from rmgpy.rmg.reactors import Reactor
from rmgpy.rmg.reactionmechanismsimulator_reactors import Reactor as RMSReactor
from rmgpy.rmg.reactionmechanismsimulator_reactors import NO_JULIA

################################################################################

Expand Down Expand Up @@ -507,6 +508,12 @@ def initialize(self, **kwargs):

# Read input file
self.load_input(self.input_file)

# Check if ReactionMechanismSimulator reactors are being used
# if RMS is not installed but the user attempted to use it, the load_input_file would have failed
# if RMS is installed but they did not use it, we can avoid extra work
# if RMS is not installed and they did not use it, we avoid calling certain functions that would raise an error
requires_rms = any(isinstance(reactor_system, RMSReactor) for reactor_system in self.reaction_systems)

if kwargs.get("restart", ""):
import rmgpy.rmg.input
Expand Down Expand Up @@ -550,10 +557,10 @@ def initialize(self, **kwargs):
self.load_database()

for spec in self.initial_species:
self.reaction_model.add_species_to_edge(spec)
self.reaction_model.add_species_to_edge(spec, requires_rms=requires_rms)

for reaction_system in self.reaction_systems:
if isinstance(reaction_system, Reactor):
if not NO_JULIA and isinstance(reaction_system, RMSReactor):
reaction_system.finish_termination_criteria()

# Load restart seed mechanism (if specified)
Expand Down Expand Up @@ -618,12 +625,12 @@ def initialize(self, **kwargs):
# Seed mechanisms: add species and reactions from seed mechanism
# DON'T generate any more reactions for the seed species at this time
for seed_mechanism in self.seed_mechanisms:
self.reaction_model.add_seed_mechanism_to_core(seed_mechanism, react=False)
self.reaction_model.add_seed_mechanism_to_core(seed_mechanism, react=False, requires_rms=requires_rms)

# Reaction libraries: add species and reactions from reaction library to the edge so
# that RMG can find them if their rates are large enough
for library, option in self.reaction_libraries:
self.reaction_model.add_reaction_library_to_edge(library)
self.reaction_model.add_reaction_library_to_edge(library, requires_rms=requires_rms)

# Also always add in a few bath gases (since RMG-Java does)
for label, smiles in [("Ar", "[Ar]"), ("He", "[He]"), ("Ne", "[Ne]"), ("N2", "N#N")]:
Expand Down Expand Up @@ -695,35 +702,35 @@ def initialize(self, **kwargs):
# This is necessary so that the PDep algorithm can identify the bath gas
for spec in self.initial_species:
if not spec.reactive:
self.reaction_model.enlarge(spec)
self.reaction_model.enlarge(spec, requires_rms=requires_rms)
for spec in self.initial_species:
if spec.reactive:
self.reaction_model.enlarge(spec)
self.reaction_model.enlarge(spec, requires_rms=requires_rms)

# chatelak: store constant SPC indices in the reactor attributes if any constant SPC provided in the input file
# advantages to write it here: this is run only once (as species indexes does not change over the generation)
if self.solvent is not None:
for index, reaction_system in enumerate(self.reaction_systems):
if (
not isinstance(reaction_system, Reactor) and reaction_system.const_spc_names is not None
): # if no constant species provided do nothing
if ((NO_JULIA or not isinstance(reaction_system, RMSReactor)) and reaction_system.const_spc_names is not None): # if no constant species provided do nothing
reaction_system.get_const_spc_indices(self.reaction_model.core.species) # call the function to identify indices in the solver

self.initialize_reaction_threshold_and_react_flags()
if self.filter_reactions and self.init_react_tuples:
self.react_init_tuples()
self.react_init_tuples(requires_rms=requires_rms)
self.reaction_model.initialize_index_species_dict()

self.initialize_seed_mech()
return requires_rms

def register_listeners(self):
def register_listeners(self, requires_rms=False):
"""
Attaches listener classes depending on the options
found in the RMG input file.
"""

self.attach(ChemkinWriter(self.output_directory))
self.attach(RMSWriter(self.output_directory))
if not NO_JULIA and requires_rms:
self.attach(RMSWriter(self.output_directory))

if self.generate_output_html:
self.attach(OutputHTMLWriter(self.output_directory))
Expand All @@ -735,7 +742,7 @@ def register_listeners(self):

if self.save_simulation_profiles:
for index, reaction_system in enumerate(self.reaction_systems):
if isinstance(reaction_system, Reactor):
if not NO_JULIA and requires_rms and isinstance(reaction_system, RMSReactor):
typ = type(reaction_system)
raise InputError(f"save_simulation_profiles=True not compatible with reactor of type {typ}")
reaction_system.attach(SimulationProfileWriter(self.output_directory, index, self.reaction_model.core.species))
Expand All @@ -749,10 +756,10 @@ def execute(self, initialize=True, **kwargs):
"""

if initialize:
self.initialize(**kwargs)
requires_rms = self.initialize(**kwargs)

# register listeners
self.register_listeners()
self.register_listeners(requires_rms=requires_rms)

self.done = False

Expand All @@ -779,7 +786,7 @@ def execute(self, initialize=True, **kwargs):
# Update react flags
if self.filter_reactions:
# Run the reaction system to update threshold and react flags
if isinstance(reaction_system, Reactor):
if not NO_JULIA and requires_rms and isinstance(reaction_system, RMSReactor):
self.update_reaction_threshold_and_react_flags(
rxn_sys_unimol_threshold=np.zeros((len(self.reaction_model.core.species),), bool),
rxn_sys_bimol_threshold=np.zeros((len(self.reaction_model.core.species), len(self.reaction_model.core.species)), bool),
Expand Down Expand Up @@ -822,6 +829,7 @@ def execute(self, initialize=True, **kwargs):
unimolecular_react=self.unimolecular_react,
bimolecular_react=self.bimolecular_react,
trimolecular_react=self.trimolecular_react,
requires_rms=requires_rms,
)

if not np.isinf(self.model_settings_list[0].thermo_tol_keep_spc_in_edge):
Expand All @@ -834,7 +842,7 @@ def execute(self, initialize=True, **kwargs):
)

if not np.isinf(self.model_settings_list[0].thermo_tol_keep_spc_in_edge):
self.reaction_model.thermo_filter_down(maximum_edge_species=self.model_settings_list[0].maximum_edge_species)
self.reaction_model.thermo_filter_down(maximum_edge_species=self.model_settings_list[0].maximum_edge_species, requires_rms=requires_rms)

logging.info("Completed initial enlarge edge step.\n")

Expand Down Expand Up @@ -900,7 +908,7 @@ def execute(self, initialize=True, **kwargs):
prune = False

try:
if isinstance(reaction_system, Reactor):
if not NO_JULIA and requires_rms and isinstance(reaction_system, RMSReactor):
(
terminated,
resurrected,
Expand Down Expand Up @@ -993,7 +1001,7 @@ def execute(self, initialize=True, **kwargs):

# Add objects to enlarge to the core first
for objectToEnlarge in objects_to_enlarge:
self.reaction_model.enlarge(objectToEnlarge)
self.reaction_model.enlarge(objectToEnlarge, requires_rms=requires_rms)

if model_settings.filter_reactions:
# Run a raw simulation to get updated reaction system threshold values
Expand All @@ -1002,7 +1010,7 @@ def execute(self, initialize=True, **kwargs):
temp_model_settings.tol_keep_in_edge = 0
if not resurrected:
try:
if isinstance(reaction_system, Reactor):
if not NO_JULIA and requires_rms and isinstance(reaction_system, RMSReactor):
(
terminated,
resurrected,
Expand Down Expand Up @@ -1071,7 +1079,7 @@ def execute(self, initialize=True, **kwargs):
skip_update=True,
)
logging.warning(
"Reaction thresholds/flags for Reaction System {0} was not updated due " "to resurrection".format(index + 1)
"Reaction thresholds/flags for Reaction System {0} was not updated due to resurrection".format(index + 1)
)

logging.info("")
Expand All @@ -1094,13 +1102,14 @@ def execute(self, initialize=True, **kwargs):
unimolecular_react=self.unimolecular_react,
bimolecular_react=self.bimolecular_react,
trimolecular_react=self.trimolecular_react,
requires_rms=requires_rms,
)

if old_edge_size != len(self.reaction_model.edge.reactions) or old_core_size != len(self.reaction_model.core.reactions):
reactor_done = False

if not np.isinf(self.model_settings_list[0].thermo_tol_keep_spc_in_edge):
self.reaction_model.thermo_filter_down(maximum_edge_species=model_settings.maximum_edge_species)
self.reaction_model.thermo_filter_down(maximum_edge_species=model_settings.maximum_edge_species, requires_rms=requires_rms)

max_num_spcs_hit = len(self.reaction_model.core.species) >= model_settings.max_num_species

Expand All @@ -1127,6 +1136,7 @@ def execute(self, initialize=True, **kwargs):
model_settings.tol_move_to_core,
model_settings.maximum_edge_species,
model_settings.min_species_exist_iterations_for_prune,
requires_rms=requires_rms,
)
# Perform garbage collection after pruning
collected = gc.collect()
Expand Down Expand Up @@ -1891,7 +1901,7 @@ def initialize_reaction_threshold_and_react_flags(self):
if self.trimolecular:
self.trimolecular_react[:num_restart_spcs, :num_restart_spcs, :num_restart_spcs] = False

def react_init_tuples(self):
def react_init_tuples(self, requires_rms=False):
"""
Reacts tuples given in the react block
"""
Expand Down Expand Up @@ -1924,6 +1934,7 @@ def react_init_tuples(self):
unimolecular_react=self.unimolecular_react,
bimolecular_react=self.bimolecular_react,
trimolecular_react=self.trimolecular_react,
requires_rms=requires_rms,
)

def update_reaction_threshold_and_react_flags(
Expand Down Expand Up @@ -2218,7 +2229,7 @@ def __init__(self, reaction_system, bspc):
if isinstance(value, list):
self.Ranges[key] = [v.value_si for v in value]

if isinstance(reaction_system, Reactor):
if not NO_JULIA and isinstance(reaction_system, RMSReactor):
self.tmax = reaction_system.tf
else:
for term in reaction_system.termination:
Expand Down
Loading

0 comments on commit 859dad7

Please sign in to comment.