Skip to content

Commit

Permalink
Update parameter sweep tool to support indexed vars (#1387)
Browse files Browse the repository at this point in the history
* Adding support for indexed variables in parameter sweep

* Adding test for indexed vars
  • Loading branch information
Andrew Lee authored Apr 11, 2024
1 parent 40d3dc7 commit 45c8ff0
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 10 deletions.
25 changes: 18 additions & 7 deletions idaes/core/util/parameter_sweep.py
Original file line number Diff line number Diff line change
Expand Up @@ -625,13 +625,24 @@ def set_input_values(self, model, sample_id: int):
)
comp.set_value(v)
elif ctype is Var:
if not comp.is_fixed():
raise ValueError(
f"Convergence testing found an input of type Var that "
f"was not fixed ({comp.name}). Please make sure all "
f"sampled inputs are either mutable params or fixed vars."
)
comp.set_value(float(v))
try:
if not comp.is_fixed():
raise ValueError(
f"Convergence testing found an input of type Var that "
f"was not fixed ({comp.name}). Please make sure all "
f"sampled inputs are either mutable params or fixed vars."
)
comp.set_value(float(v))
except AttributeError:
# Component might be indexed, try iterating and setting value
for i, c in comp.items():
if not c.is_fixed():
raise ValueError(
f"Convergence testing found an input of type IndexedVar that "
f"was not fixed ({comp.name}, index {i}). Please make sure all "
f"sampled inputs are either mutable params or fixed vars."
)
c.set_value(float(v))
else:
raise ValueError(
f"Failed to find a valid input component (must be "
Expand Down
34 changes: 34 additions & 0 deletions idaes/core/util/tests/test_parameter_sweep.py
Original file line number Diff line number Diff line change
Expand Up @@ -836,6 +836,40 @@ def bm():
):
psweep.set_input_values(model, 0)

@pytest.mark.unit
def test_set_input_indexed_var(self):
def bm():
m = ConcreteModel()
m.v1 = Var([1, 2])
return m

spec2 = ParameterSweepSpecification()
spec2.set_sampling_method(UniformSampling)
spec2.add_sampled_input("v1", 0, 10)
spec2.set_sample_size([2])

psweep = ParameterSweepBase(
build_model=bm,
input_specification=spec2,
)

model = psweep.get_initialized_model()

with pytest.raises(
ValueError,
match="Convergence testing found an input of type IndexedVar that "
"was not fixed \(v1, index 1\). Please make sure all "
"sampled inputs are either mutable params or fixed vars.",
):
psweep.set_input_values(model, 0)

model.v1.fix(20)

psweep.set_input_values(model, 0)

assert value(model.v1[1]) == 0
assert value(model.v1[2]) == 0

@pytest.mark.unit
def test_set_input_invalid_ctype(self):
def bm():
Expand Down
1 change: 0 additions & 1 deletion idaes/models/unit_models/tests/test_heat_exchanger.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
units as pyunits,
)
from pyomo.common.config import ConfigBlock
from pyomo.util.check_units import assert_units_consistent, assert_units_equivalent

from idaes.core import (
FlowsheetBlock,
Expand Down
2 changes: 0 additions & 2 deletions idaes/models/unit_models/tests/test_heat_exchanger_1D.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
units as pyunits,
)
from pyomo.common.config import ConfigBlock
from pyomo.util.check_units import assert_units_consistent, assert_units_equivalent
import pyomo.common.unittest as unittest

import idaes
Expand Down Expand Up @@ -55,7 +54,6 @@

from idaes.core.util.exceptions import ConfigurationError, InitializationError
from idaes.core.util.model_statistics import (
degrees_of_freedom,
number_variables,
number_total_constraints,
number_unused_variables,
Expand Down

0 comments on commit 45c8ff0

Please sign in to comment.