Skip to content

Commit

Permalink
correcting reaction rate setting
Browse files Browse the repository at this point in the history
  • Loading branch information
K20shores committed Aug 23, 2024
1 parent 7e374cd commit 172800d
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 3,626 deletions.
3,602 changes: 0 additions & 3,602 deletions mySolution.csv

This file was deleted.

16 changes: 5 additions & 11 deletions src/acom_music_box/conditions.py
Original file line number Diff line number Diff line change
Expand Up @@ -222,18 +222,12 @@ def read_initial_rates_from_file(cls, file_path, reaction_list):
# The second row of the CSV contains rates
rates = initial_conditions[1]

for i in range(0, len(headers)):
for reaction_rate, rate in zip(headers, rates):
type, name, *rest = reaction_rate.split('.')
for reaction in reaction_list.reactions:
if reaction.name == name and reaction.short_type() == type:
reaction_rates.append(ReactionRate(reaction, rate))

reaction_rate = headers[i]

match = filter(
lambda x: x.name == reaction_rate.split('.')[1],
reaction_list.reactions)

reaction = next(match, None)
rate = rates[i]

reaction_rates.append(ReactionRate(reaction, rate))
return reaction_rates

def add_species_concentration(self, species_concentration):
Expand Down
17 changes: 7 additions & 10 deletions src/acom_music_box/music_box.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import os

import logging
logging.basicConfig(level=logging.DEBUG)
logger = logging.getLogger(__name__)


Expand Down Expand Up @@ -554,6 +555,7 @@ def solve(self, output_path=None):
# outputs to file if output is present
if (output_path is not None):
logger.info("path_to_output = {}".format(output_path))
os.makedirs(os.path.dirname(output_path), exist_ok=True)
with open(output_path, 'w', newline='') as output:
writer = csv.writer(output)
writer.writerows(output_array)
Expand Down Expand Up @@ -712,21 +714,16 @@ def order_reaction_rates(self, curr_conditions, rate_constant_ordering):
key = "EMIS." + rate.reaction.name
rate_constants[key] = rate.rate

# print(curr_conditions.reaction_rates)
print(len(rate_constant_ordering.keys()))
print(len(rate_constants.keys()))
logger.debug(f"MUSICA provided {len(rate_constant_ordering.keys())} user defined reaction rates")
logger.debug(f"Music box expects {len(rate_constants.keys())}")

musica_rates = set(rate_constants.keys())
box_rates = set(rate_constant_ordering.keys())
musica_rates = set(rate_constant_ordering.keys())
box_rates = set(rate_constants.keys())

print(box_rates.difference(musica_rates))
logger.debug(f"Musica contains these rates, and music box doesn't' {musica_rates.difference(box_rates)}")

ordered_rate_constants = len(rate_constants.keys()) * [0.0]
# print(sorted(rate_constant_ordering.values()))
# print(len(ordered_rate_constants))
for key, value in rate_constants.items():
# print(f"key: {key}, rate_constant_ordering[key]: {rate_constant_ordering[key]}")
# print(ordered_rate_constants)
ordered_rate_constants[rate_constant_ordering[key]] = float(value)
return ordered_rate_constants

Expand Down
18 changes: 18 additions & 0 deletions src/acom_music_box/reaction.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,24 @@ def add_product(self, product):
product (Product): The Product instance to be added.
"""
self.products.append(product)

def short_type(self):
"""
Return the first letter of the reaction type.
Returns:
str: The first letter of the reaction type.
"""
type_map = {
"EMISSION": "EMIS",
"PHOTOLYSIS": "PHOT",
"FIRST_ORDER_LOSS": "LOSS",
"BRANCHED": "BRAN",
"ARRHENIUS": "ARRH",
"TUNNELING": "TUNN",
"TROE_TERNARY": "TROE",
}
return type_map.get(self.reaction_type, "UNKNOWN")


class Branched(Reaction):
Expand Down
2 changes: 1 addition & 1 deletion tests/test_chapman.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ def test_run(self):
float(test_output_concs[i][j]),
rel_tol=1e-8,
abs_tol=1e-15,
), f"Arrays differ at index ({i}, {j}) for "
), f"Arrays differ at index ({i}, {j}) for species {concs_to_test[j]}"


if __name__ == "__main__":
Expand Down
5 changes: 3 additions & 2 deletions tests/test_wall_loss.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ def test_run(self):
box_model.create_solver(camp_path)

# solves and saves output
model_output = box_model.solve()
model_output = box_model.solve(output_path="output/wall_loss_test.csv")

# read wall_loss_test.csv into test_output
with open("expected_results/wall_loss_test.csv", "r") as file:
Expand Down Expand Up @@ -46,7 +46,8 @@ def test_run(self):
float(model_output_concs[i][j]),
float(test_output_concs[i][j]),
rel_tol=1e-8,
), f"Arrays differ at index ({i}, {j}) for "
abs_tol=1e-8,
), f"Arrays differ at index ({i}, {j}) for species {concs_to_test[j]}"


if __name__ == "__main__":
Expand Down

0 comments on commit 172800d

Please sign in to comment.