From 8238211aaa1e8531f1d5f23c5884d7db2e66552c Mon Sep 17 00:00:00 2001 From: Montek Thind Date: Thu, 19 Sep 2024 15:04:34 -0700 Subject: [PATCH 1/3] changed the method to do create the solver in one method --- src/acom_music_box/main.py | 9 ++----- src/acom_music_box/music_box.py | 36 ++++++++++--------------- tests/integration/test_analytical.py | 8 +----- tests/integration/test_carbon_bond_5.py | 10 ++----- tests/integration/test_chapman.py | 10 ++----- tests/integration/test_flow_tube.py | 10 ++----- tests/unit/test_callback.py | 9 ++----- 7 files changed, 25 insertions(+), 67 deletions(-) diff --git a/src/acom_music_box/main.py b/src/acom_music_box/main.py index 9e1c7f8e..131fc817 100644 --- a/src/acom_music_box/main.py +++ b/src/acom_music_box/main.py @@ -151,13 +151,8 @@ def main(): # Create and load a MusicBox object myBox = MusicBox() logger.debug(f"Configuration file = {musicBoxConfigFile}") - myBox.readConditionsFromJson(musicBoxConfigFile) - - # Create solver and solve - config_path = os.path.join( - os.path.dirname(musicBoxConfigFile), - myBox.config_file) - myBox.create_solver(config_path) + myBox.loadJson(musicBoxConfigFile, myBox) + result = myBox.solve(musicBoxOutputPath) if musicBoxOutputPath is None: diff --git a/src/acom_music_box/music_box.py b/src/acom_music_box/music_box.py index be34c57e..02c433a6 100644 --- a/src/acom_music_box/music_box.py +++ b/src/acom_music_box/music_box.py @@ -69,26 +69,6 @@ def add_evolving_condition(self, time_point, conditions): time=[time_point], conditions=[conditions]) self.evolvingConditions.append(evolving_condition) - def create_solver( - self, - path_to_config, - solver_type=musica.micmsolver.rosenbrock, - number_of_grid_cells=1): - """ - Creates a micm solver object using the CAMP configuration files. - - Args: - path_to_config (str): The path to CAMP configuration directory. - - Returns: - None - """ - # Create a solver object using the configuration file - self.solver = musica.create_solver( - path_to_config, - solver_type, - number_of_grid_cells) - def solve(self, output_path=None, callback=None): """ Solves the box model simulation and optionally writes the output to a file. @@ -246,12 +226,13 @@ def solve(self, output_path=None, callback=None): return df - def readConditionsFromJson(self, path_to_json): + def loadJson(self, path_to_json, box_model): """ - Reads and parses a JSON file from the CAMP JSON file to set up the box model simulation. + Reads and parses a JSON file and create a solver Args: path_to_json (str): The JSON path to the JSON file. + box_model: Instance of the MusicBox class Returns: None @@ -279,6 +260,17 @@ def readConditionsFromJson(self, path_to_json): # Set initial conditions self.evolving_conditions = EvolvingConditions.from_config_JSON( path_to_json, data, self.species_list, self.reaction_list) + + camp_path = os.path.join( + os.path.dirname(path_to_json), + box_model.config_file) + + # Create a solver object using the configuration file + self.solver = musica.create_solver( + camp_path, + musica.micmsolver.rosenbrock, + 1) + def speciesOrdering(self): """ diff --git a/tests/integration/test_analytical.py b/tests/integration/test_analytical.py index b1bf43fe..2eb4aef9 100644 --- a/tests/integration/test_analytical.py +++ b/tests/integration/test_analytical.py @@ -8,15 +8,9 @@ class TestAnalytical: def test_run(self): box_model = MusicBox() - # configures box model conditions_path = Examples.Analytical.path - box_model.readConditionsFromJson(conditions_path) - camp_path = os.path.join( - os.path.dirname(conditions_path), - box_model.config_file) - - box_model.create_solver(camp_path) + box_model.loadJson(conditions_path, box_model) # solves and saves output df = box_model.solve() diff --git a/tests/integration/test_carbon_bond_5.py b/tests/integration/test_carbon_bond_5.py index cc90b64d..0c25d67e 100644 --- a/tests/integration/test_carbon_bond_5.py +++ b/tests/integration/test_carbon_bond_5.py @@ -8,16 +8,10 @@ class TestCarbonBond5: def test_run(self): box_model = MusicBox() - - # configures box model + conditions_path = Examples.CarbonBond5.path - box_model.readConditionsFromJson(conditions_path) - - camp_path = os.path.join( - os.path.dirname(conditions_path), - box_model.config_file) - box_model.create_solver(camp_path) + box_model.loadJson(conditions_path, box_model) # solves and saves output df = box_model.solve() diff --git a/tests/integration/test_chapman.py b/tests/integration/test_chapman.py index 930fa393..f649e494 100644 --- a/tests/integration/test_chapman.py +++ b/tests/integration/test_chapman.py @@ -9,15 +9,9 @@ class TestChapman: def test_run(self): box_model = MusicBox() - # configures box model conditions_path = Examples.Chapman.path - box_model.readConditionsFromJson(conditions_path) - - camp_path = os.path.join( - os.path.dirname(conditions_path), - box_model.config_file) - - box_model.create_solver(camp_path) + + box_model.loadJson(conditions_path, box_model) # solves and saves output df = box_model.solve() diff --git a/tests/integration/test_flow_tube.py b/tests/integration/test_flow_tube.py index 3e00a3e1..3631abb2 100644 --- a/tests/integration/test_flow_tube.py +++ b/tests/integration/test_flow_tube.py @@ -9,16 +9,10 @@ class TestWallLoss: def test_run(self): box_model = MusicBox() - # configures box model conditions_path = Examples.FlowTube.path - box_model.readConditionsFromJson(conditions_path) - - camp_path = os.path.join( - os.path.dirname(conditions_path), - box_model.config_file) - - box_model.create_solver(camp_path) + box_model.loadJson(conditions_path, box_model) + # solves and saves output df = box_model.solve() model_output = [df.columns.values.tolist()] + df.values.tolist() diff --git a/tests/unit/test_callback.py b/tests/unit/test_callback.py index ade31847..5527626d 100644 --- a/tests/unit/test_callback.py +++ b/tests/unit/test_callback.py @@ -11,13 +11,8 @@ def test_run(self, mocker): box_model = MusicBox() conditions_path = Examples.Analytical.path - box_model.readConditionsFromJson(conditions_path) - - camp_path = os.path.join( - os.path.dirname(conditions_path), - box_model.config_file) - - box_model.create_solver(camp_path) + + box_model.loadJson(conditions_path, box_model) # Mock the callback function callback_mock = mocker.Mock(side_effect=callback) From 1faad21c667af4df6c410a649812607d268f8815 Mon Sep 17 00:00:00 2001 From: Montek Thind Date: Thu, 19 Sep 2024 15:06:08 -0700 Subject: [PATCH 2/3] added a better comment --- src/acom_music_box/music_box.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/acom_music_box/music_box.py b/src/acom_music_box/music_box.py index 02c433a6..a4527400 100644 --- a/src/acom_music_box/music_box.py +++ b/src/acom_music_box/music_box.py @@ -265,7 +265,7 @@ def loadJson(self, path_to_json, box_model): os.path.dirname(path_to_json), box_model.config_file) - # Create a solver object using the configuration file + # Creates a micm solver object using the CAMP configuration files. self.solver = musica.create_solver( camp_path, musica.micmsolver.rosenbrock, From 1969517e4b40e20f1fbbaa94a0e198b545040edd Mon Sep 17 00:00:00 2001 From: Montek Thind Date: Fri, 20 Sep 2024 15:11:32 -0700 Subject: [PATCH 3/3] removed box_mode instance --- src/acom_music_box/main.py | 4 ++-- src/acom_music_box/music_box.py | 7 +++---- tests/integration/test_analytical.py | 2 +- tests/integration/test_carbon_bond_5.py | 2 +- tests/integration/test_chapman.py | 2 +- tests/integration/test_flow_tube.py | 2 +- tests/unit/test_callback.py | 2 +- 7 files changed, 10 insertions(+), 11 deletions(-) diff --git a/src/acom_music_box/main.py b/src/acom_music_box/main.py index 131fc817..f53010c1 100644 --- a/src/acom_music_box/main.py +++ b/src/acom_music_box/main.py @@ -151,8 +151,8 @@ def main(): # Create and load a MusicBox object myBox = MusicBox() logger.debug(f"Configuration file = {musicBoxConfigFile}") - myBox.loadJson(musicBoxConfigFile, myBox) - + myBox.loadJson(musicBoxConfigFile) + result = myBox.solve(musicBoxOutputPath) if musicBoxOutputPath is None: diff --git a/src/acom_music_box/music_box.py b/src/acom_music_box/music_box.py index a4527400..561693cf 100644 --- a/src/acom_music_box/music_box.py +++ b/src/acom_music_box/music_box.py @@ -226,14 +226,13 @@ def solve(self, output_path=None, callback=None): return df - def loadJson(self, path_to_json, box_model): + def loadJson(self, path_to_json): """ Reads and parses a JSON file and create a solver Args: path_to_json (str): The JSON path to the JSON file. - box_model: Instance of the MusicBox class - + Returns: None @@ -263,7 +262,7 @@ def loadJson(self, path_to_json, box_model): camp_path = os.path.join( os.path.dirname(path_to_json), - box_model.config_file) + self.config_file) # Creates a micm solver object using the CAMP configuration files. self.solver = musica.create_solver( diff --git a/tests/integration/test_analytical.py b/tests/integration/test_analytical.py index 2eb4aef9..7a293bcd 100644 --- a/tests/integration/test_analytical.py +++ b/tests/integration/test_analytical.py @@ -10,7 +10,7 @@ def test_run(self): conditions_path = Examples.Analytical.path - box_model.loadJson(conditions_path, box_model) + box_model.loadJson(conditions_path) # solves and saves output df = box_model.solve() diff --git a/tests/integration/test_carbon_bond_5.py b/tests/integration/test_carbon_bond_5.py index 0c25d67e..5676dc25 100644 --- a/tests/integration/test_carbon_bond_5.py +++ b/tests/integration/test_carbon_bond_5.py @@ -11,7 +11,7 @@ def test_run(self): conditions_path = Examples.CarbonBond5.path - box_model.loadJson(conditions_path, box_model) + box_model.loadJson(conditions_path) # solves and saves output df = box_model.solve() diff --git a/tests/integration/test_chapman.py b/tests/integration/test_chapman.py index f649e494..c2b68241 100644 --- a/tests/integration/test_chapman.py +++ b/tests/integration/test_chapman.py @@ -11,7 +11,7 @@ def test_run(self): conditions_path = Examples.Chapman.path - box_model.loadJson(conditions_path, box_model) + box_model.loadJson(conditions_path) # solves and saves output df = box_model.solve() diff --git a/tests/integration/test_flow_tube.py b/tests/integration/test_flow_tube.py index 3631abb2..cf47fd79 100644 --- a/tests/integration/test_flow_tube.py +++ b/tests/integration/test_flow_tube.py @@ -11,7 +11,7 @@ def test_run(self): conditions_path = Examples.FlowTube.path - box_model.loadJson(conditions_path, box_model) + box_model.loadJson(conditions_path) # solves and saves output df = box_model.solve() diff --git a/tests/unit/test_callback.py b/tests/unit/test_callback.py index 5527626d..7e8966ab 100644 --- a/tests/unit/test_callback.py +++ b/tests/unit/test_callback.py @@ -12,7 +12,7 @@ def test_run(self, mocker): conditions_path = Examples.Analytical.path - box_model.loadJson(conditions_path, box_model) + box_model.loadJson(conditions_path) # Mock the callback function callback_mock = mocker.Mock(side_effect=callback)