Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes and updates for differential parmater sweep tool #1240

Merged
merged 18 commits into from
Dec 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -185,8 +185,8 @@ With the flowsheet defined and suitably initialized, along with the definitions

# Run the parameter sweep
global_results = differential_parameter_sweep(
model,
sweep_params,
build_model,
build_sweep_params,
differential_sweep_specs,
outputs,
h5_results_file_name='monte_carlo_results.h5',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ This enables user to have multiple setup files that can be used to run simulatio
* *build_defaults* – default arguments that are passed for every sweep. For example in ro_erd flowsheet, shown here, this could be erd_type, but in other flowsheets, this could define any default options, from file locations to the number of stages, etc. The defaults will be passed along with loop values unless the loop value overrides them.
* *init_defaults* - defaults for initialization_function, same behavior as build-defaults but used for initialization_function
* *optimize_defaults* - defaults for optimize_function, same behavior as build or init defaults, but for optimize function.
* *build_output_kwargs * - a list of keys to only save in .h5 file. loopTool provides a default build_outputs function that takes in a dict containing model key name and model key (e.g., to output only m.fs.costing.LCOW user would include following dict *LCOW : fs.costing.LCOW*). The default function uses model.find_component to construct an output dict. Alternatively user can provide kwargs to a user-provided build_outputs function that would be linked to loopTool (shown below).
* *build_output_kwargs* - a list of keys to only save in .h5 file. loopTool provides a default build_outputs function that takes in a dict containing model key name and model key (e.g., to output only m.fs.costing.LCOW user would include following dict *LCOW : fs.costing.LCOW*). The default function uses model.find_component to construct an output dict. Alternatively user can provide kwargs to a user-provided build_outputs function that would be linked to loopTool (shown below).

**loop options:**

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@ def build_sweep_params(m, num_samples=1, scenario="A_comp_vs_LCOW"):


def run_parameter_sweep(num_samples=100, num_procs=1):

ps1, kwargs_dict1 = create_parameter_sweep_object(
num_samples, num_procs, parallel_backend="ConcurrentFutures"
)
Expand Down Expand Up @@ -103,7 +102,6 @@ def create_parameter_sweep_object(
num_procs,
parallel_backend="ConcurrentFutures",
):

solver = get_solver()
kwargs_dict = {
"debugging_data_dir": None,
Expand Down Expand Up @@ -149,7 +147,6 @@ def create_recursive_parameter_sweep_object(
num_procs,
parallel_backend="ConcurrentFutures",
):

solver = get_solver()
kwargs_dict = {
"debugging_data_dir": None,
Expand Down Expand Up @@ -195,19 +192,20 @@ def create_differential_parameter_sweep_object(
num_procs,
parallel_backend="ConcurrentFutures",
):

solver = get_solver()
m = build_model(read_model_defauls_from_file=False)

differential_sweep_specs = {
"A_comp": {
"diff_mode": "sum",
"diff_sample_type": UniformSample,
"relative_lb": 0.01,
"relative_ub": 0.01,
"pyomo_object": m.fs.RO.A_comp,
def build_spec(model):
differential_sweep_specs = {
"A_comp": {
"diff_mode": "sum",
"diff_sample_type": UniformSample,
"relative_lb": 0.01,
"relative_ub": 0.01,
"pyomo_object": model.fs.RO.A_comp,
}
}
}
return differential_sweep_specs

kwargs_dict = {
"debugging_data_dir": None,
Expand All @@ -228,8 +226,8 @@ def create_differential_parameter_sweep_object(
"build_outputs_kwargs": {},
"optimize_function": optimize,
"optimize_kwargs": {"solver": solver, "check_termination": False},
"num_diff_samples": 2,
"differential_sweep_specs": differential_sweep_specs,
"num_diff_samples": 1,
"build_differential_sweep_specs": build_spec,
"initialize_function": None,
"update_sweep_params_before_init": False,
"initialize_kwargs": {},
Expand All @@ -252,7 +250,6 @@ def create_differential_parameter_sweep_object(


if __name__ == "__main__":

import sys
import time
import numpy as np
Expand Down
85 changes: 3 additions & 82 deletions tutorials/parameter_sweep_demo.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -192,62 +192,6 @@
")"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "a9de9f80",
"metadata": {
"slideshow": {
"slide_type": "subslide"
}
},
"outputs": [],
"source": [
"??build_model"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "d106ae65",
"metadata": {
"slideshow": {
"slide_type": "subslide"
}
},
"outputs": [],
"source": [
"??build_outputs"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "b07c1657",
"metadata": {
"slideshow": {
"slide_type": "subslide"
}
},
"outputs": [],
"source": [
"??optimize"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "015e21aa",
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"outputs": [],
"source": [
"??build_sweep_params"
]
},
{
"cell_type": "code",
"execution_count": null,
Expand Down Expand Up @@ -442,20 +386,6 @@
"*Run the simple parameter sweep in recursion if some runs fail to ensure that a user-specified number of sample results are generated. This involves resampling the input parameter space to compensate for the failed runs.*"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "0d963217",
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"outputs": [],
"source": [
"??create_recursive_parameter_sweep_object"
]
},
{
"cell_type": "code",
"execution_count": null,
Expand Down Expand Up @@ -537,16 +467,6 @@
"* The differential sweep is a simple parameter sweep that occurs at every nominal value, where one of the sweep parameters is perturbed keeping the others fixed to their nominal values."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "5030e928",
"metadata": {},
"outputs": [],
"source": [
"??create_differential_parameter_sweep_object"
]
},
{
"cell_type": "code",
"execution_count": null,
Expand All @@ -557,7 +477,8 @@
"outputs": [],
"source": [
"num_samples = 2\n",
"num_procs = 2\n",
"num_procs = 1\n",
"\n",
"model, dps, dkwargs_dict = create_differential_parameter_sweep_object(num_samples, num_procs)\n",
"dps_results_array, dps_results_dict = dps.parameter_sweep(\n",
" dkwargs_dict[\"build_model\"],\n",
Expand Down Expand Up @@ -874,7 +795,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.11.5"
"version": "3.9.16"
},
"rise": {
"enable_chalkboard": true,
Expand Down
8 changes: 6 additions & 2 deletions watertap/tools/analysis_tools/loop_tool/loop_tool.py
Original file line number Diff line number Diff line change
Expand Up @@ -723,8 +723,12 @@ def run_diff_parameter_sweep(self):
ps_kwargs["build_outputs_kwargs"] = self.build_outputs_kwargs

ps_kwargs[
"differential_sweep_specs"
] = ParameterSweepReader()._dict_to_diff_spec(m, self.differential_sweep_specs)
"build_differential_sweep_specs"
] = ParameterSweepReader()._dict_to_diff_spec
ps_kwargs["build_differential_sweep_specs_kwargs"] = {
"input_dict": self.differential_sweep_specs
}
ps_kwargs["num_diff_samples"] = self.diff_samples
ps = DifferentialParameterSweep(**ps_kwargs)

ps.parameter_sweep(
Expand Down
3 changes: 3 additions & 0 deletions watertap/tools/parameter_sweep/model_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ def __init__(self, ps_instance):
self.is_initialized = False
self.is_solved = False
self.is_prior_parameter_solved = False
# internal state option to be used by differntial sweep tool to
# prevent model reintialization and state reset
self._is_rebuild_and_init_enabled = True
# this is isused for loggin states if enabled
self.solved_states = {"state": [], "local_value_k": []}
self.initialized_states = {"state": [], "local_value_k": []}
Expand Down
Loading