Skip to content

Commit

Permalink
outputs results to file and creates file for initial test
Browse files Browse the repository at this point in the history
  • Loading branch information
alexjamesgarza committed Mar 26, 2024
1 parent a9dca12 commit 128528b
Show file tree
Hide file tree
Showing 13 changed files with 3,051 additions and 13 deletions.
48 changes: 35 additions & 13 deletions src/box_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
from music_box_conditions import Conditions
from music_box_species_concentration import SpeciesConcentration
from music_box_reaction_rate import ReactionRate
import utils
import csv
import musica

class BoxModel:
Expand Down Expand Up @@ -263,7 +263,7 @@ def create_solver(self, path_to_config):
self.solver = musica.create_micm(path_to_config)


def solve(self):
def solve(self, path_to_output = None):
"""
TODO: Solve the box model simulation.
"""
Expand All @@ -275,9 +275,6 @@ 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
next_conditions = None
Expand All @@ -287,11 +284,32 @@ def solve(self):
next_conditions_index = 0
next_conditions = self.evolving_conditions.conditions[0]
next_conditions_time = self.evolving_conditions.times[0]

#initializes file headers if output file is present
output_array = []
if(path_to_output != None):
headers = []
headers.append("time")
headers.append("ENV.temperature")
headers.append("ENV.pressure")
for spec in self.species_list:
headers.append("CONC." + spec.name)

output_array.append(headers)

#runs the simulation at each timestep
curr_time = 0
while(curr_time <= (self.box_model_options.simulation_length)):
print(curr_concentrations)
while(curr_time <= self.box_model_options.simulation_length):

#appends row to output if file is present
if(path_to_output != None):
row = []
row.append(curr_time)
row.append(curr_conditions.temperature)
row.append(curr_conditions.pressure)
for conc in curr_concentrations:
row.append(conc)
output_array.append(row)

#iterates evolvings conditons if enough time has elapsed
if(next_conditions != None and next_conditions_time <= curr_time):
Expand All @@ -311,10 +329,19 @@ def solve(self):

#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

#outputs to file if output is present
if(path_to_output != None):
with open(path_to_output, 'w', newline='') as output:
writer = csv.writer(output)
writer.writerows(output_array)

def readFromUIJson(self, path_to_json):
"""
TODO: Read the box model configuration from json and sets config
Expand Down Expand Up @@ -354,13 +381,8 @@ def readConditionsFromJson(self, path_to_json):
def __main__():
# Create a new instance of the BoxModel class.

box_model = BoxModel()
box_model.readConditionsFromJson("configs/test_config/my_config.json")
box_model.create_solver("configs/test_config/camp_data")
box_model.solve()

pass

box_model.solve()



Expand Down
File renamed without changes.
1 change: 1 addition & 0 deletions src/configs/test_config_2/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_2/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": "PHOTOLYSIS", "scaling_factor": 1, "MUSICA name": "O3_2", "reactants": {"O3": {}}, "products": {"O": {"yield": 1}, "O2": {"yield": 1}, "irr__0665f4f2-132e-4f99-a2d7-7b0cf35b1b5f": {"yield": 1}}}, {"type": "ARRHENIUS", "A": 3.3e-11, "Ea": -7.59e-22, "B": 0, "D": 300, "E": 0, "reactants": {"O1D": {"qty": 1}, "O2": {"qty": 1}}, "products": {"O": {"yield": 1}, "O2": {"yield": 1}, "irr__21732b9a-3601-4739-acdc-48466559aeee": {"yield": 1}}}, {"type": "ARRHENIUS", "A": 6e-34, "Ea": 0, "B": -2.4, "D": 300, "E": 0, "reactants": {"O": {"qty": 1}, "O2": {"qty": 1}, "M": {"qty": 1}}, "products": {"O3": {"yield": 1}, "M": {"yield": 1}, "irr__6b0bb4ae-f671-4f2a-92c9-e88992346583": {"yield": 1}}}, {"type": "ARRHENIUS", "A": 2.15e-11, "Ea": -1.518e-21, "B": 0, "D": 300, "E": 0, "reactants": {"O1D": {"qty": 1}, "N2": {"qty": 1}}, "products": {"O": {"yield": 1}, "N2": {"yield": 1}, "irr__8be08086-a796-4c88-b56f-a695a0c22ddd": {"yield": 1}}}, {"type": "PHOTOLYSIS", "scaling_factor": 1, "MUSICA name": "O3_1", "reactants": {"O3": {}}, "products": {"O1D": {"yield": 1}, "O2": {"yield": 1}, "irr__90a4d7ba-c694-424b-a0eb-1146f2afee8e": {"yield": 1}}}, {"type": "ARRHENIUS", "A": 8e-12, "Ea": 2.8428e-20, "B": 0, "D": 300, "E": 0, "reactants": {"O": {"qty": 1}, "O3": {"qty": 1}}, "products": {"O2": {"yield": 2}, "irr__b3c93679-40cf-419a-8914-818b5d40b117": {"yield": 1}}}, {"type": "PHOTOLYSIS", "scaling_factor": 1, "MUSICA name": "O2_1", "reactants": {"O2": {}}, "products": {"O": {"yield": 2}, "irr__ce5b7d7f-dd7e-4daa-8e49-f1db924b50f6": {"yield": 1}}}]}]}
1 change: 1 addition & 0 deletions src/configs/test_config_2/camp_data/species.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"camp-data": [{"name": "M", "type": "CHEM_SPEC", "tracer type": "CONSTANT"}, {"name": "Ar", "type": "CHEM_SPEC", "absolute tolerance": 1e-12}, {"name": "CO2", "type": "CHEM_SPEC", "absolute tolerance": 1e-12}, {"name": "H2O", "type": "CHEM_SPEC", "absolute tolerance": 1e-12}, {"name": "O1D", "type": "CHEM_SPEC", "absolute tolerance": 1e-12}, {"name": "O", "type": "CHEM_SPEC", "absolute tolerance": 1e-12}, {"name": "O2", "type": "CHEM_SPEC", "absolute tolerance": 1e-12}, {"name": "O3", "type": "CHEM_SPEC", "absolute tolerance": 1e-12}, {"name": "N2", "type": "CHEM_SPEC", "absolute tolerance": 1e-12}, {"name": "irr__0665f4f2-132e-4f99-a2d7-7b0cf35b1b5f", "type": "CHEM_SPEC"}, {"name": "irr__21732b9a-3601-4739-acdc-48466559aeee", "type": "CHEM_SPEC"}, {"name": "irr__6b0bb4ae-f671-4f2a-92c9-e88992346583", "type": "CHEM_SPEC"}, {"name": "irr__8be08086-a796-4c88-b56f-a695a0c22ddd", "type": "CHEM_SPEC"}, {"name": "irr__90a4d7ba-c694-424b-a0eb-1146f2afee8e", "type": "CHEM_SPEC"}, {"name": "irr__b3c93679-40cf-419a-8914-818b5d40b117", "type": "CHEM_SPEC"}, {"name": "irr__ce5b7d7f-dd7e-4daa-8e49-f1db924b50f6", "type": "CHEM_SPEC"}]}
Loading

0 comments on commit 128528b

Please sign in to comment.