Skip to content

Commit

Permalink
adds function to read config json and tests basic reaction
Browse files Browse the repository at this point in the history
  • Loading branch information
alexjamesgarza committed Mar 26, 2024
1 parent bbe8d0f commit 74ee215
Show file tree
Hide file tree
Showing 8 changed files with 122 additions and 20 deletions.
48 changes: 28 additions & 20 deletions src/box_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
Expand All @@ -304,27 +306,29 @@ 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):
"""
TODO: Read the box model configuration from json and sets config
"""
# 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
Expand All @@ -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()


Expand Down
1 change: 1 addition & 0 deletions src/configs/test_config/camp_data/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"camp-files": ["species.json", "reactions.json"]}
1 change: 1 addition & 0 deletions src/configs/test_config/camp_data/reactions.json
Original file line number Diff line number Diff line change
@@ -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}}}]}]}
1 change: 1 addition & 0 deletions src/configs/test_config/camp_data/species.json
Original file line number Diff line number Diff line change
@@ -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"}]}
2 changes: 2 additions & 0 deletions src/configs/test_config/evolving_conditions.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
time.s
0
46 changes: 46 additions & 0 deletions src/configs/test_config/my_config.json
Original file line number Diff line number Diff line change
@@ -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": {}
}
}
]
}
31 changes: 31 additions & 0 deletions src/music_box_conditions.py
Original file line number Diff line number Diff line change
@@ -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

Expand Down Expand Up @@ -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):
"""
Expand Down
12 changes: 12 additions & 0 deletions src/music_box_model_options.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

0 comments on commit 74ee215

Please sign in to comment.