Skip to content

Commit

Permalink
reads species from json from camp_data
Browse files Browse the repository at this point in the history
  • Loading branch information
alexjamesgarza committed Mar 28, 2024
1 parent cccd3b0 commit 3056132
Show file tree
Hide file tree
Showing 7 changed files with 3,749 additions and 3,616 deletions.
9 changes: 6 additions & 3 deletions src/box_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,7 @@ def solve(self, path_to_output = None):
headers.append("time")
headers.append("ENV.temperature")
headers.append("ENV.pressure")

for spec in self.species_list.species:
headers.append("CONC." + spec.name)

Expand Down Expand Up @@ -377,11 +378,13 @@ def readConditionsFromJson(self, path_to_json):
# Set box model options
self.box_model_options = BoxModelOptions.from_config_JSON(data)

# Set species list
self.species_list = SpeciesList.from_config_JSON(path_to_json, data)

# Set initial conditions
self.initial_conditions = Conditions.from_config_JSON(data)
self.initial_conditions = Conditions.from_config_JSON(data, self.species_list )

# Set species list
self.species_list = SpeciesList.from_config_JSON(data)



# for testing purposes
Expand Down
38 changes: 37 additions & 1 deletion src/configs/test_config_2/camp_data/reactions.json
Original file line number Diff line number Diff line change
@@ -1 +1,37 @@
{"camp-data": [{"type": "MECHANISM", "name": "music box interactive configuration", "reactions": [{"type": "ARRHENIUS", "A": 8.8e-17, "Ea": 0, "B": 0, "D": 300, "E": 0, "reactants": {"O3": {"qty": 1}, "a-pinene": {"qty": 1}}, "products": {"SOA1": {"yield": 0.18}, "SOA2": {"yield": 0.09}, "irr__bab3e78d-d1d7-4c6b-9f6f-bbbd303c0901": {"yield": 1}}}]}]}
{
"camp-data": [
{
"type": "MECHANISM",
"name": "music box interactive configuration",
"reactions": [
{
"type": "ARRHENIUS",
"A": 8.8e-17,
"Ea": 0,
"B": 0,
"D": 300,
"E": 0,
"reactants": {
"O3": {
"qty": 1
},
"a-pinene": {
"qty": 1
}
},
"products": {
"SOA1": {
"yield": 0.18
},
"SOA2": {
"yield": 0.09
},
"irr__bab3e78d-d1d7-4c6b-9f6f-bbbd303c0901": {
"yield": 1
}
}
}
]
}
]
}
25 changes: 24 additions & 1 deletion src/configs/test_config_2/camp_data/species.json
Original file line number Diff line number Diff line change
@@ -1 +1,24 @@
{"camp-data": [ {"name": "a-pinene", "type": "CHEM_SPEC"}, {"name": "O3", "type": "CHEM_SPEC"}, {"name": "SOA1", "type": "CHEM_SPEC"}, {"name": "SOA2", "type": "CHEM_SPEC"}, {"name": "irr__bab3e78d-d1d7-4c6b-9f6f-bbbd303c0901", "type": "CHEM_SPEC"}]}
{
"camp-data": [
{
"name": "a-pinene",
"type": "CHEM_SPEC"
},
{
"name": "O3",
"type": "CHEM_SPEC"
},
{
"name": "SOA1",
"type": "CHEM_SPEC"
},
{
"name": "SOA2",
"type": "CHEM_SPEC"
},
{
"name": "irr__bab3e78d-d1d7-4c6b-9f6f-bbbd303c0901",
"type": "CHEM_SPEC"
}
]
}
40 changes: 39 additions & 1 deletion src/configs/test_config_2/my_config.json
Original file line number Diff line number Diff line change
@@ -1 +1,39 @@
{"box model options": {"grid": "box", "chemistry time step [sec]": 1, "output time step [sec]": 1, "simulation length [hr]": 1}, "chemical species": {"a-pinene": {"initial value [mol m-3]": 8e-08}, "O3": {"initial value [mol m-3]": 2e-05}}, "environmental conditions": {"temperature": {"initial value [K]": 298.15}, "pressure": {"initial value [Pa]": 101325}}, "initial conditions": {}, "model components": [{"type": "CAMP", "configuration file": "camp_data/config.json", "override species": {"M": {"mixing ratio mol mol-1": 1}}, "suppress output": {"M": {}}}]}
{
"box model options": {
"grid": "box",
"chemistry time step [sec]": 1,
"output time step [sec]": 1,
"simulation length [hr]": 1
},
"chemical species": {
"a-pinene": {
"initial value [mol m-3]": 8e-08
},
"O3": {
"initial value [mol m-3]": 2e-05
}
},
"environmental conditions": {
"temperature": {
"initial value [K]": 298.15
},
"pressure": {
"initial value [Pa]": 101325
}
},
"initial conditions": {},
"model components": [
{
"type": "CAMP",
"configuration file": "camp_data/config.json",
"override species": {
"M": {
"mixing ratio mol mol-1": 1
}
},
"suppress output": {
"M": {}
}
}
]
}
7 changes: 6 additions & 1 deletion src/music_box_conditions.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ def from_UI_JSON(cls, UI_JSON, species_list, reaction_list):
return cls(pressure, temperature, species_concentrations, reaction_rates)

@classmethod
def from_config_JSON(cls, config_JSON):
def from_config_JSON(cls, config_JSON, species_list):
pressure = utils.convert_pressure(config_JSON['environmental conditions']['pressure'], 'initial value')

temperature = utils.convert_temperature(config_JSON['environmental conditions']['temperature'], 'initial value')
Expand All @@ -91,6 +91,11 @@ def from_config_JSON(cls, config_JSON):
concentration = utils.convert_concentration(config_JSON['chemical species'][chem_spec], 'initial value')

species_concentrations.append(SpeciesConcentration(species, concentration))

for species in species_list.species:
if not any(conc.species.name == species.name for conc in species_concentrations):
species_concentrations.append(SpeciesConcentration(species, 0))


#TODO: may or may not be necessary
# Set initial reaction rates
Expand Down
42 changes: 35 additions & 7 deletions src/music_box_species_list.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import json
import os
from typing import List
from music_box_species import Species

Expand Down Expand Up @@ -46,17 +48,43 @@ def from_UI_JSON(cls, UI_JSON):
return cls(species_from_json)

@classmethod
def from_config_JSON(cls, config_JSON):
def from_config_JSON(cls, path_to_json, config_JSON):

species_from_json = []

for name, properties in config_JSON['chemical species'].items():
absolute_tolerance = properties.get('absolute tolerance')
molecular_weight = properties.get('molecular weight')
#gets config file path
config_file_path = os.path.dirname(path_to_json) + "/" + config_JSON['model components'][0]['configuration file']

#opnens config path to read species file
with open(config_file_path, 'r') as json_file:
config = json.load(json_file)

#assumes species file is first in the list
if(len(config['camp-files']) > 0):
species_file_path = os.path.dirname(config_file_path) + "/" + config['camp-files'][0]
with open(species_file_path, 'r') as species_file:
species_data = json.load(species_file)
#loads species by names from camp files
for properties in species_data['camp-data']:
name = properties.get('name')
species_from_json.append(Species(name, None, None, None, None))


#chceks if species are in the config file and updates attributes accordingly
for chem_spec in config_JSON['chemical species']:
for species in species_from_json:
if species.name == chem_spec:
# Add attributes to species
if 'absolute tolerance' in chem_spec:
species.absolute_tolerance = chem_spec['absolute tolerance']
if 'molecular weight' in chem_spec:
species.molecular_weight = chem_spec['molecular weight']
if 'density' in chem_spec:
species.density = chem_spec['density']
if 'phase' in chem_spec:
species.phase = chem_spec['phase']


# TODO: Add phase and density to species

species_from_json.append(Species(name, absolute_tolerance, None, molecular_weight, None))

return cls(species_from_json)

Expand Down
Loading

0 comments on commit 3056132

Please sign in to comment.