diff --git a/src/box_model.py b/src/box_model.py index eea435a0..95211d59 100644 --- a/src/box_model.py +++ b/src/box_model.py @@ -41,8 +41,8 @@ def __init__(self, box_model_options=None, species_list=None, reaction_list=None config_file (String): File path for the configuration file to be located. Default is "camp_data/config.json". """ self.box_model_options = box_model_options - self.species_list = species_list - self.reaction_list = reaction_list + self.species_list = species_list if species_list is not None else [] + self.reaction_list = reaction_list if reaction_list is not None else [] self.initial_conditions = initial_conditions self.evolving_conditions = evolving_conditions if evolving_conditions is not None else [] self.config_file = config_file if config_file is not None else "camp_data/config.json" @@ -279,6 +279,8 @@ def solve(self): #sets up initial concentraion values curr_concentrations = self.initial_conditions.get_concentration_array() + + #sets up next condition if evolving conditions is not empty @@ -304,19 +306,21 @@ def solve(self): next_conditions_index += 1 next_conditions = self.evolving_conditions.conditions[next_conditions_index] next_conditions_time = self.evolving_conditions.times[next_conditions_index] + + #overrides concentrations if specified by conditions + if(len(curr_conditions.get_concentration_array()) != 0): + curr_concentrations = curr_conditions.get_concentration_array() else: next_conditions = None - #updates concentrations if they are present in the evolving conditions - if(len(curr_conditions.get_concentration_array()) != 0): - curr_concentrations = curr_conditions.get_concentration_array() - - curr_concentations = musica.micm_solve(self.solver, self.box_model_options.chem_step_time, curr_conditions.temperature, curr_conditions.pressure, curr_concentrations) - + #solves and updates concentration values in concentration array + musica.micm_solve(self.solver, self.box_model_options.chem_step_time, curr_conditions.temperature, curr_conditions.pressure, curr_concentrations) + #increments time curr_time += self.box_model_options.chem_step_time - #print(curr_concentrations) + print(curr_concentrations) + def readFromJson(self, path_to_json): """ @@ -324,7 +328,7 @@ def readFromJson(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 @@ -342,20 +346,24 @@ def readFromJson(self, path_to_json): # Set evolving conditions self.evolving_conditions = EvolvingConditions.from_UI_JSON(data, self.species_list, self.reaction_list) + def readConditionsFromJson(self, path_to_json): + + with open(path_to_json, 'r') as json_file: + data = json.load(json_file) + # Set box model options + self.box_model_options = BoxModelOptions.from_config_JSON(data) + + # Set initial conditions + self.initial_conditions = Conditions.from_config_JSON(data) + + # for testing purposes def __main__(): # Create a new instance of the BoxModel class. - options = BoxModelOptions(5, 1, 2.5) - inital_condtions = Conditions(1.0, 298.0) - inital_condtions.add_species_concentration(SpeciesConcentration(Species(name="N2"), 3.29e1 )) - inital_condtions.add_species_concentration(SpeciesConcentration(Species(name="O2"), 8.84e0)) - inital_condtions.add_species_concentration(SpeciesConcentration(Species(name="Ar"), 3.92e-1)) - inital_condtions.add_species_concentration(SpeciesConcentration(Species(name="CO2"), 1.69e-2)) - inital_condtions.add_species_concentration(SpeciesConcentration(Species(name="O"), 1.0e-5)) - box_model = BoxModel(box_model_options=options, initial_conditions=inital_condtions) - - box_model.create_solver("configs/chapman") + box_model = BoxModel() + box_model.readConditionsFromJson("configs/test_config/my_config.json") + box_model.create_solver("configs/test_config/camp_data") box_model.solve() diff --git a/src/configs/test_config/camp_data/config.json b/src/configs/test_config/camp_data/config.json new file mode 100644 index 00000000..03622f15 --- /dev/null +++ b/src/configs/test_config/camp_data/config.json @@ -0,0 +1 @@ +{"camp-files": ["species.json", "reactions.json"]} \ No newline at end of file diff --git a/src/configs/test_config/camp_data/reactions.json b/src/configs/test_config/camp_data/reactions.json new file mode 100644 index 00000000..ea8f5420 --- /dev/null +++ b/src/configs/test_config/camp_data/reactions.json @@ -0,0 +1 @@ +{"camp-data": [{"type": "MECHANISM", "name": "music box interactive configuration", "reactions": [{"type": "ARRHENIUS", "A": 0.00012, "Ea": 1.0354868e-21, "B": 7, "D": 50, "E": 0.5, "reactants": {"B": {"qty": 1}}, "products": {"C": {"yield": 1}, "irr__089f1f45-4cd8-4278-83d5-d638e98e4315": {"yield": 1}}}, {"type": "ARRHENIUS", "A": 0.004, "Ea": -6.903245e-22, "B": 0, "D": 300, "E": 0, "reactants": {"A": {"qty": 1}}, "products": {"B": {"yield": 1}, "irr__2a109b21-bb24-41ae-8f06-7485fd36f1a7": {"yield": 1}}}]}]} \ No newline at end of file diff --git a/src/configs/test_config/camp_data/species.json b/src/configs/test_config/camp_data/species.json new file mode 100644 index 00000000..58ede0a7 --- /dev/null +++ b/src/configs/test_config/camp_data/species.json @@ -0,0 +1 @@ +{"camp-data": [{"name": "A", "type": "CHEM_SPEC"}, {"name": "B", "type": "CHEM_SPEC"}, {"name": "C", "type": "CHEM_SPEC"}, {"name": "irr__089f1f45-4cd8-4278-83d5-d638e98e4315", "type": "CHEM_SPEC"}, {"name": "irr__2a109b21-bb24-41ae-8f06-7485fd36f1a7", "type": "CHEM_SPEC"}]} \ No newline at end of file diff --git a/src/configs/test_config/evolving_conditions.csv b/src/configs/test_config/evolving_conditions.csv new file mode 100644 index 00000000..5c133edf --- /dev/null +++ b/src/configs/test_config/evolving_conditions.csv @@ -0,0 +1,2 @@ +time.s +0 diff --git a/src/configs/test_config/my_config.json b/src/configs/test_config/my_config.json new file mode 100644 index 00000000..dc10326f --- /dev/null +++ b/src/configs/test_config/my_config.json @@ -0,0 +1,46 @@ + +{ + "box model options": { + "grid": "box", + "chemistry time step [sec]": 1, + "output time step [sec]": 1, + "simulation length [sec]": 100 + }, + "chemical species": { + "A": { + "initial value [mol m-3]": 1 + }, + "B": { + "initial value [mol m-3]": 0 + }, + "C": { + "initial value [mol m-3]": 0 + } + }, + "environmental conditions": { + "pressure": { + "initial value [Pa]": 101253.3 + }, + "temperature": { + "initial value [K]": 272.5 + } + }, + "evolving conditions": { + "evolving_conditions.csv": {} + }, + "initial conditions": {}, + "model components": [ + { + "type": "CAMP", + "configuration file": "camp_data/config.json", + "override species": { + "M": { + "mixing ratio mol mol-1": 1 + } + }, + "suppress output": { + "M": {} + } + } + ] +} diff --git a/src/music_box_conditions.py b/src/music_box_conditions.py index eccaa386..a4a89a37 100644 --- a/src/music_box_conditions.py +++ b/src/music_box_conditions.py @@ -1,5 +1,6 @@ from typing import List from music_box_reaction_rate import ReactionRate +from music_box_species import Species from music_box_species_concentration import SpeciesConcentration import utils @@ -67,6 +68,36 @@ def from_UI_JSON(cls, UI_JSON, species_list, reaction_list): reaction_rates.append(ReactionRate(reaction_from_list, rate)) return cls(pressure, temperature, species_concentrations, reaction_rates) + + @classmethod + def from_config_JSON(cls, config_JSON): + pressure = utils.convert_pressure(config_JSON['environmental conditions']['pressure'], 'initial value') + + temperature = utils.convert_temperature(config_JSON['environmental conditions']['temperature'], 'initial value') + + # Set initial species concentrations + species_concentrations = [] + for chem_spec in config_JSON['chemical species']: + species = Species(name = chem_spec) + + concentration = utils.convert_concentration(config_JSON['chemical species'][chem_spec], 'initial value') + + species_concentrations.append(SpeciesConcentration(species, concentration)) + + # Set initial reaction rates + reaction_rates = [] + + for reaction in config_JSON['initial conditions']: + + reaction_from_list = Species(name=reaction) + + rate = config_JSON['conditions']['initial conditions'][reaction] + + reaction_rates.append(ReactionRate(reaction_from_list, rate)) + + return cls(pressure, temperature, species_concentrations, reaction_rates) + + def add_species_concentration(self, species_concentration): """ diff --git a/src/music_box_model_options.py b/src/music_box_model_options.py index 579210af..9607fa2d 100644 --- a/src/music_box_model_options.py +++ b/src/music_box_model_options.py @@ -44,3 +44,15 @@ def from_UI_JSON(cls, UI_JSON): grid = UI_JSON['conditions']['box model options']['grid'] return cls(chem_step_time, output_step_time, simulation_length, grid) + + + @classmethod + def from_config_JSON(cls, config_JSON): + + chem_step_time = utils.convert_time(config_JSON['box model options'], 'chemistry time step') * 60 + output_step_time = utils.convert_time(config_JSON['box model options'], 'output time step') + simulation_length = utils.convert_time(config_JSON['box model options'], 'simulation length') + + grid = config_JSON['box model options']['grid'] + + return cls(chem_step_time, output_step_time, simulation_length, grid)