Skip to content

Commit 421b6b5

Browse files
committed
added variables
1 parent 7bf8daa commit 421b6b5

File tree

2 files changed

+49
-1
lines changed

2 files changed

+49
-1
lines changed

idaes/core/util/structfs/fsrunner.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -280,12 +280,13 @@ def _ipython_display_(self):
280280
self._a._ipython_display_()
281281

282282
def __init__(self, **kwargs):
283-
from .runner_actions import CaptureSolverOutput
283+
from .runner_actions import CaptureSolverOutput, ModelVariables
284284

285285
super().__init__(**kwargs)
286286
self.dof = self.DegreesOfFreedom(self)
287287
self.timings = self.Timings(self)
288288
self.add_action("capture_solver_output", CaptureSolverOutput)
289+
self.add_action("model_variables", ModelVariables)
289290

290291
def build(self):
291292
"""Run just the build step"""

idaes/core/util/structfs/runner_actions.py

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -349,3 +349,50 @@ def _is_solve_step(self, name: str):
349349

350350
def report(self):
351351
return {"solver_logs": self._logs}
352+
353+
354+
class ModelVariables(Action):
355+
"""Extract and format model variables."""
356+
357+
VAR_TYPE = "var"
358+
PARAM_TYPE = "param"
359+
360+
def __init__(self, runner, **kwargs):
361+
assert isinstance(runner, FlowsheetRunner) # makes no sense otherwise
362+
super().__init__(runner, **kwargs)
363+
364+
def after_run(self):
365+
self._blocks = self._extract_vars(self._runner.model)
366+
367+
def _extract_vars(self, m):
368+
model_vars = []
369+
for c in m.component_objects():
370+
id_c = id(c)
371+
if c.is_variable_type():
372+
subtype = self.VAR_TYPE
373+
elif c.is_parameter_type():
374+
subtype = self.PARAM_TYPE
375+
else:
376+
continue # we just don't care!
377+
# XXX block: subtype, id, name, parent_id, [is_indexed, num]
378+
# XXX b = [subtype, id_c, c.name, id(c.parent_block())]
379+
b = [subtype, c.name]
380+
items = []
381+
for index in c:
382+
v = c[index]
383+
if index is None:
384+
b.append(False) # not indexed
385+
else:
386+
b.append(True) # indexed
387+
# item: index, value, [fixed, stale, lb, ub]
388+
if subtype == self.VAR_TYPE:
389+
item = (index, v.value, v.fixed, v.stale, v.lb, v.ub)
390+
else:
391+
item = (index, v.value)
392+
items.append(item)
393+
b.append(items)
394+
model_vars.append(b)
395+
return model_vars
396+
397+
def report(self) -> dict:
398+
return {"blocks": self._blocks}

0 commit comments

Comments
 (0)