Skip to content

Commit 7bf8daa

Browse files
committed
capture solver output
1 parent 0d93c47 commit 7bf8daa

File tree

2 files changed

+30
-3
lines changed

2 files changed

+30
-3
lines changed

idaes/core/util/structfs/fsrunner.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ class BaseFlowsheetRunner(Runner):
8181
"check_model_numerics",
8282
)
8383

84-
def __init__(self, solver=None, tee=False):
84+
def __init__(self, solver=None, tee=True):
8585
self.build_step = self.STEPS[0]
8686
self._solver, self._tee = solver, tee
8787
self._ann = {}
@@ -230,7 +230,7 @@ def __init__(self, runner):
230230

231231
# check DoF after build, initial solve, and optimization solve
232232
self._a = runner.add_action(
233-
"dof",
233+
"degrees_of_freedom",
234234
UnitDofChecker,
235235
"fs",
236236
["build", "solve_initial", "solve_optimization"],
@@ -258,7 +258,7 @@ class Timings:
258258
def __init__(self, runner):
259259
from .runner_actions import Timer
260260

261-
self._a: Timer = runner.add_action("t", Timer)
261+
self._a: Timer = runner.add_action("timings", Timer)
262262

263263
@property
264264
def values(self) -> list[dict]:
@@ -280,9 +280,12 @@ def _ipython_display_(self):
280280
self._a._ipython_display_()
281281

282282
def __init__(self, **kwargs):
283+
from .runner_actions import CaptureSolverOutput
284+
283285
super().__init__(**kwargs)
284286
self.dof = self.DegreesOfFreedom(self)
285287
self.timings = self.Timings(self)
288+
self.add_action("capture_solver_output", CaptureSolverOutput)
286289

287290
def build(self):
288291
"""Run just the build step"""

idaes/core/util/structfs/runner_actions.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -325,3 +325,27 @@ def _get_dof(block, fix_inlets: bool = True):
325325
inlet.free()
326326

327327
return dof
328+
329+
330+
class CaptureSolverOutput(Action):
331+
def __init__(self, runner, **kwargs):
332+
super().__init__(runner, **kwargs)
333+
self._logs = {}
334+
self._solver_out = None
335+
336+
def before_step(self, step_name: str):
337+
if self._is_solve_step(step_name):
338+
self._solver_out = StringIO()
339+
self._save_stdout, sys.stdout = sys.stdout, self._solver_out
340+
341+
def after_step(self, step_name: str):
342+
if self._is_solve_step(step_name):
343+
self._logs[step_name] = self._solver_out.getvalue()
344+
self._solver_out = None
345+
sys.stdout = self._save_stdout
346+
347+
def _is_solve_step(self, name: str):
348+
return name.startswith("solve")
349+
350+
def report(self):
351+
return {"solver_logs": self._logs}

0 commit comments

Comments
 (0)