Skip to content

Commit

Permalink
Merge pull request #1022 from MichaelPesce/ui-parameter-sweep
Browse files Browse the repository at this point in the history
needed for demo
  • Loading branch information
dangunter authored May 17, 2023
2 parents a607709 + fdc7d4c commit 9f208f2
Show file tree
Hide file tree
Showing 16 changed files with 35 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -443,7 +443,7 @@ def build_flowsheet():

results = solve(m)
assert_optimal_termination(results)
return m.fs
return m


def solve_flowsheet(flowsheet=None):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -779,7 +779,7 @@ def build_flowsheet():

results = solve(m)
assert_optimal_termination(results)
return m.fs
return m


def solve_flowsheet(flowsheet=None):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -713,7 +713,7 @@ def build_flowsheet():

results = solve(m)
assert_optimal_termination(results)
return m.fs
return m


def solve_flowsheet(flowsheet=None):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -826,7 +826,7 @@ def build_flowsheet():

results = solve(m)
assert_optimal_termination(results)
return m.fs
return m


def solve_flowsheet(flowsheet=None):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -925,7 +925,7 @@ def build_flowsheet():

results = solve(m)
assert_optimal_termination(results)
return m.fs
return m


def solve_flowsheet(flowsheet=None):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -676,7 +676,7 @@ def build_flowsheet():

results = solve(m)
assert_optimal_termination(results)
return m.fs
return m


def solve_flowsheet(flowsheet=None):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -741,7 +741,7 @@ def build_flowsheet():

results = solve(m)
assert_optimal_termination(results)
return m.fs
return m


def solve_flowsheet(flowsheet=None):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -560,7 +560,7 @@ def build_flowsheet():

results = solve(m)
assert_optimal_termination(results)
return m.fs
return m


def solve_flowsheet(flowsheet=None):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -744,7 +744,7 @@ def build_flowsheet():

results = solve(m)
assert_optimal_termination(results)
return m.fs
return m


def solve_flowsheet(flowsheet=None):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -908,7 +908,7 @@ def build_flowsheet():

results = solve(m)
assert_optimal_termination(results)
return m.fs
return m


def solve_flowsheet(flowsheet=None):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -481,7 +481,7 @@ def build_flowsheet():

results = solve(m)
assert_optimal_termination(results)
return m.fs
return m


def solve_flowsheet(flowsheet=None):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -557,7 +557,7 @@ def build_flowsheet():

results = solve(m)
assert_optimal_termination(results)
return m.fs
return m


def solve_flowsheet(flowsheet=None):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -627,7 +627,7 @@ def build_flowsheet():

results = solve(m)
assert_optimal_termination(results)
return m.fs
return m


def solve_flowsheet(flowsheet=None):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1173,7 +1173,7 @@ def build_flowsheet():

results = solve(m)
assert_optimal_termination(results)
return m.fs
return m


def solve_flowsheet(flowsheet=None):
Expand Down
8 changes: 7 additions & 1 deletion watertap/ui/fsapi.py
Original file line number Diff line number Diff line change
Expand Up @@ -182,13 +182,15 @@ def set_obj_key_default(cls, v, values):
class FlowsheetExport(BaseModel):
"""A flowsheet and its contained exported model objects."""

m: object = Field(default=None, exclude=True)
obj: object = Field(default=None, exclude=True)
name: str = ""
description: str = ""
model_objects: Dict[str, ModelExport] = {}
version: int = 2
requires_idaes_solver: bool = False
dof: int = 0
sweep_results: Union[None, dict] = {}

# set name dynamically from object
@validator("name", always=True)
Expand Down Expand Up @@ -474,7 +476,9 @@ def action_wrapper(**kwargs):
f"Flowsheet `{Actions.build}` action failed. "
f"See logs for details."
)
self.fs_exp.obj = action_result
self.fs_exp.obj = action_result.fs
self.fs_exp.m = action_result

# [re-]create exports (new model object)
if Actions.export not in self._actions:
raise KeyError(
Expand Down Expand Up @@ -541,6 +545,7 @@ def export_values(self):
"""
_log.info("Exporting values from flowsheet model to UI")
u = pyo.units
self.fs_exp.dof = degrees_of_freedom(self.fs_exp.obj)
for key, mo in self.fs_exp.model_objects.items():
mo.value = pyo.value(u.convert(mo.obj, to_units=mo.ui_units))
if not isinstance(
Expand All @@ -565,6 +570,7 @@ def export_values(self):
tmp = pyo.Var(initialize=mo.obj.lb, units=u.get_units(mo.obj))
tmp.construct()
mo.lb = pyo.value(u.convert(tmp, to_units=mo.ui_units))
mo.fixed = mo.obj.fixed
else:
mo.has_bounds = False

Expand Down
19 changes: 14 additions & 5 deletions watertap/ui/tests/test_fsapi.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,11 @@
# Fake status=OK solver result


class FAKE_FLOWSHEET:
fs = "fs"
trash = "true"


class SOLVE_RESULT_OK:
class SOLVE_STATUS:
status = SolverStatus.ok
Expand All @@ -47,7 +52,7 @@ class SOLVE_STATUS:

def build_ro(**kwargs):
model = RO.build_flowsheet(erd_type=ERD_TYPE)
return model.fs
return model


def solve_ro(flowsheet=None):
Expand Down Expand Up @@ -149,7 +154,9 @@ def test_build():
def test_actions(add_variant: str):
fsi = flowsheet_interface()
built = False
garbage = {"trash": True}
# garbage = {"trash": True}
garbage = FAKE_FLOWSHEET
m = FAKE_FLOWSHEET
v1 = Var(name="variable1")
v1.construct()
v1.value = 1
Expand All @@ -158,11 +165,13 @@ def test_actions(add_variant: str):
def fake_build():
nonlocal built
built = True
return garbage
nonlocal m
m = build_ro()
return m

def fake_solve(flowsheet=None):
# flowsheet passed in here should be what fake_build() returns
assert flowsheet == garbage
assert flowsheet == m.fs
return SOLVE_RESULT_OK

def fake_export(flowsheet=None, exports=None):
Expand Down Expand Up @@ -289,8 +298,8 @@ def test_export_values_build():
def test_empty_solve():
# try a fake solve
fsi = flowsheet_interface()
fsi.build()
with pytest.raises(RuntimeError) as excinfo:
fsi.build()
fsi.solve()
print(f"* RuntimeError: {excinfo.value}")

Expand Down

0 comments on commit 9f208f2

Please sign in to comment.