From 7073097deec2db40688ad5260d6afc742a3d8738 Mon Sep 17 00:00:00 2001 From: Evan De la Garza Date: Thu, 28 Mar 2024 12:48:36 -0500 Subject: [PATCH 1/2] config files generatable --- src/box_model.py | 102 ++++++++++++++++++++++++++++----- src/music_box_reaction_list.py | 4 +- 2 files changed, 91 insertions(+), 15 deletions(-) diff --git a/src/box_model.py b/src/box_model.py index a7b7661b..dc882484 100644 --- a/src/box_model.py +++ b/src/box_model.py @@ -1,4 +1,5 @@ import json +import os from music_box_evolving_conditions import EvolvingConditions from music_box_reaction_list import ReactionList @@ -59,18 +60,86 @@ def add_evolving_condition(self, time_point, conditions): evolving_condition = EvolvingConditions(time=[time_point], conditions=[conditions]) self.evolvingConditions.append(evolving_condition) - def generateConfig(self): + def generateConfig(self, directory): """ Generate configuration JSON for the box model simulation. Returns: tuple: A tuple containing the species configuration JSON and the reaction configuration JSON. """ - - speciesConfig = self.generateSpeciesConfig() - reactionConfig = self.generateReactionConfig() - - return speciesConfig, reactionConfig + output_path = "./src/configs/" + directory + + # Check if directory exists and create it if it doesn't + if not os.path.exists(output_path): + os.makedirs(output_path) + os.makedirs(output_path + "/camp_data") + + # Make camp_data config + with open(output_path + "/camp_data/config.json", 'w') as camp_config_file: + data = { + "camp-files": [ + "species.json", + "reactions.json" + ] + } + + camp_config_file.write(json.dumps(data)) + + # Make species and reactions configs + with open(output_path + "/camp_data/species.json", 'w') as species_file: + species_file.write(self.generateSpeciesConfig()) + + with open(output_path + "/camp_data/reactions.json", 'w') as reactions_file: + reactions_file.write(self.generateReactionConfig()) + + # Make box model options config + with open(output_path + "/" + directory + "_config.json", 'w') as config_file: + data = {} + + data["box model options"] = { + "grid": self.box_model_options.grid, + "chemistry time step [sec]": self.box_model_options.chem_step_time, + "output time step [sec]": self.box_model_options.output_step_time, + "simulation length [sec]": self.box_model_options.simulation_length, + } + + data["chemical species"] = {} + + if self.initial_conditions.species_concentrations is not None: + for species_concentration in self.initial_conditions.species_concentrations: + data["chemical species"][species_concentration.species.name] = { "initial value [mol m-3]": species_concentration.concentration } + + data["environmental conditions"] = { + "pressure": { + "initial value [Pa]": self.initial_conditions.pressure, + }, + "temperature": { + "initial value [K]": self.initial_conditions.temperature, + }, + } + + data["evolving conditions"] = { + "evolving_conditions.csv": {}, + } + + data["initial conditions"] = {} + + data["model components"] = [ + { + "type": "CAMP", + "configuration file": "camp_data/config.json", + "override species": { + "M": { + "mixing ratio mol mol-1": 1 + } + }, + "suppress output": { + "M": {} + } + } + ] + + config_file.write(json.dumps(data)) def generateSpeciesConfig(self): """ @@ -83,11 +152,11 @@ def generateSpeciesConfig(self): speciesArray = [] #Adds relative tolerance if value is set - if(self.species_list.relative_tolerance != None): - relativeTolerance = {} - relativeTolerance["Type"] = "RELATIVE_TOLERANCE" - relativeTolerance["value"] = self.species_list.relative_tolerance - speciesArray.append(relativeTolerance) + # if(self.species_list.relative_tolerance != None): + # relativeTolerance = {} + # relativeTolerance["Type"] = "RELATIVE_TOLERANCE" + # relativeTolerance["value"] = self.species_list.relative_tolerance + # speciesArray.append(relativeTolerance) #Adds species to config for species in self.species_list.species: @@ -157,7 +226,7 @@ def generateReactionConfig(self): #Adds reactant quantity if value is set if reactant.quantity != None: - quantity["quantity"] = reactant.quantity + quantity["qty"] = reactant.quantity reactants[reactant.name] = quantity reac["reactants"] = reactants @@ -348,7 +417,7 @@ def readFromUIJson(self, path_to_json): """ # TODO: Implement the logic to update the box model config using a json. - with open(path_to_json, 'r') as json_file: + with open(path_to_json, 'r', encoding='UTF-16') as json_file: data = json.load(json_file) # Set box model options @@ -383,8 +452,13 @@ def readConditionsFromJson(self, path_to_json): # for testing purposes def __main__(): # Create a new instance of the BoxModel class. + box_model = BoxModel() + + # Read the box model configuration from a json file. + box_model.readFromUIJson("./pretty_json.json") - pass + # Generate configuration JSON for the box model simulation. + box_model.generateConfig("reactions_config_test") diff --git a/src/music_box_reaction_list.py b/src/music_box_reaction_list.py index 4c05135b..dfdb5ebd 100644 --- a/src/music_box_reaction_list.py +++ b/src/music_box_reaction_list.py @@ -33,6 +33,8 @@ def from_UI_JSON(cls, UI_JSON, species_list): Returns: ReactionList: A new instance of the ReactionList class. """ + list_name = UI_JSON['mechanism']['reactions']['camp-data'][0]['name'] + reactions = [] for reaction in UI_JSON['mechanism']['reactions']['camp-data'][0]['reactions']: @@ -107,7 +109,7 @@ def from_UI_JSON(cls, UI_JSON, species_list): else: reactions.append(Reaction(name, reaction_type, reactants, products)) - return cls(reactions=reactions) + return cls(list_name, reactions) def add_reaction(self, reaction): """ From 25d103ba5b61edf245e7c661dc81d37eca6a3bc6 Mon Sep 17 00:00:00 2001 From: Evan De la Garza Date: Thu, 28 Mar 2024 13:10:05 -0500 Subject: [PATCH 2/2] initial reaction rates added to generate config --- src/box_model.py | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/src/box_model.py b/src/box_model.py index ed953b19..acd3c720 100644 --- a/src/box_model.py +++ b/src/box_model.py @@ -124,6 +124,10 @@ def generateConfig(self, directory): data["initial conditions"] = {} + for reaction_rate in self.initial_conditions.reaction_rates: + name = "PHOT." + reaction_rate.reaction.name + ".s-1" + data["initial conditions"][name] = reaction_rate.rate + data["model components"] = [ { "type": "CAMP", @@ -421,7 +425,7 @@ def readFromUIJson(self, path_to_json): """ # TODO: Implement the logic to update the box model config using a json. - with open(path_to_json, 'r', encoding='UTF-16') as json_file: + with open(path_to_json, 'r') as json_file: data = json.load(json_file) # Set box model options @@ -459,10 +463,10 @@ def __main__(): box_model = BoxModel() # Read the box model configuration from a json file. - box_model.readFromUIJson("./pretty_json.json") + box_model.readFromUIJson("../initial.json") # Generate configuration JSON for the box model simulation. - box_model.generateConfig("reactions_config_test") + box_model.generateConfig("UI_test")