Skip to content

Commit 1d3b548

Browse files
committed
better solver / results behavior
1 parent c4d61b7 commit 1d3b548

File tree

8 files changed

+412
-64
lines changed

8 files changed

+412
-64
lines changed

docs/examples/structfs/flash_flowsheet.py

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
from idaes.models.unit_models import Flash
2727
from idaes.core.util.structfs.fsrunner import FlowsheetRunner
2828

29-
FS = FlowsheetRunner()
29+
FS = FlowsheetRunner(solver=SolverFactory("ipopt"))
3030

3131
# # Flash Unit Model
3232
#
@@ -68,18 +68,12 @@ def init_model(ctx):
6868
m.fs.flash.initialize(outlvl=idaeslog.INFO)
6969

7070

71-
@FS.step("set_solver")
72-
def set_solver(ctx):
73-
"""Set the solver."""
74-
ctx.solver = SolverFactory("ipopt")
75-
76-
7771
@FS.step("solve_initial")
7872
def solve(ctx):
7973
"""Perform the initial model solve."""
80-
ctx["results"] = ctx.solver.solve(ctx.model, tee=ctx["tee"])
74+
ctx.solve()
8175

8276

8377
@FS.step("solve_optimization")
8478
def solve_o(ctx):
85-
ctx["results"] = ctx.solver.solve(ctx.model, tee=ctx["tee"])
79+
ctx.solve()

docs/examples/structfs/flash_flowsheet_nb.ipynb

Lines changed: 45 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
},
2424
{
2525
"cell_type": "code",
26-
"execution_count": 2,
26+
"execution_count": 1,
2727
"id": "854989ae",
2828
"metadata": {},
2929
"outputs": [],
@@ -50,7 +50,7 @@
5050
},
5151
{
5252
"cell_type": "code",
53-
"execution_count": 3,
53+
"execution_count": 2,
5454
"id": "4f58d1b7",
5555
"metadata": {},
5656
"outputs": [
@@ -62,24 +62,30 @@
6262
"build\n",
6363
"set_operating_conditions\n",
6464
"initialize\n",
65-
"set_solver\n",
6665
"solve_initial\n",
6766
"solve_optimization\n",
6867
"========================================\n",
69-
"2025-12-06 05:31:08 [INFO] idaes.init.fs.flash.control_volume.properties_in: Initialization Step 1 optimal - Optimal Solution Found.\n",
70-
"2025-12-06 05:31:08 [INFO] idaes.init.fs.flash.control_volume.properties_in: Initialization Step 2 optimal - Optimal Solution Found.\n",
71-
"2025-12-06 05:31:08 [INFO] idaes.init.fs.flash.control_volume.properties_in: Initialization Step 3 optimal - Optimal Solution Found.\n",
72-
"2025-12-06 05:31:08 [INFO] idaes.init.fs.flash.control_volume.properties_in: Initialization Step 4 optimal - Optimal Solution Found.\n",
73-
"2025-12-06 05:31:08 [INFO] idaes.init.fs.flash.control_volume.properties_in: Initialization Step 5 optimal - Optimal Solution Found.\n",
74-
"2025-12-06 05:31:08 [INFO] idaes.init.fs.flash.control_volume.properties_out: Initialization Step 1 optimal - Optimal Solution Found.\n",
75-
"2025-12-06 05:31:08 [INFO] idaes.init.fs.flash.control_volume.properties_out: Initialization Step 2 optimal - Optimal Solution Found.\n",
76-
"2025-12-06 05:31:08 [INFO] idaes.init.fs.flash.control_volume.properties_out: Initialization Step 3 optimal - Optimal Solution Found.\n",
77-
"2025-12-06 05:31:08 [INFO] idaes.init.fs.flash.control_volume.properties_out: Initialization Step 4 optimal - Optimal Solution Found.\n",
78-
"2025-12-06 05:31:08 [INFO] idaes.init.fs.flash.control_volume.properties_out: Initialization Step 5 optimal - Optimal Solution Found.\n",
79-
"2025-12-06 05:31:08 [INFO] idaes.init.fs.flash.control_volume.properties_out: State Released.\n",
80-
"2025-12-06 05:31:08 [INFO] idaes.init.fs.flash.control_volume: Initialization Complete\n",
81-
"2025-12-06 05:31:08 [INFO] idaes.init.fs.flash.control_volume.properties_in: State Released.\n",
82-
"2025-12-06 05:31:08 [INFO] idaes.init.fs.flash: Initialization Complete: optimal - Optimal Solution Found\n"
68+
"2026-03-06 04:01:42 [INFO] idaes.init.fs.flash.control_volume.properties_in: Initialization Step 1 optimal - Optimal Solution Found.\n",
69+
"2026-03-06 04:01:42 [INFO] idaes.init.fs.flash.control_volume.properties_in: Initialization Step 2 optimal - Optimal Solution Found.\n",
70+
"2026-03-06 04:01:42 [INFO] idaes.init.fs.flash.control_volume.properties_in: Initialization Step 3 optimal - Optimal Solution Found.\n",
71+
"2026-03-06 04:01:42 [INFO] idaes.init.fs.flash.control_volume.properties_in: Initialization Step 4 optimal - Optimal Solution Found.\n",
72+
"2026-03-06 04:01:42 [INFO] idaes.init.fs.flash.control_volume.properties_in: Initialization Step 5 optimal - Optimal Solution Found.\n",
73+
"2026-03-06 04:01:42 [INFO] idaes.init.fs.flash.control_volume.properties_out: Initialization Step 1 optimal - Optimal Solution Found.\n",
74+
"2026-03-06 04:01:42 [INFO] idaes.init.fs.flash.control_volume.properties_out: Initialization Step 2 optimal - Optimal Solution Found.\n",
75+
"2026-03-06 04:01:42 [INFO] idaes.init.fs.flash.control_volume.properties_out: Initialization Step 3 optimal - Optimal Solution Found.\n",
76+
"2026-03-06 04:01:42 [INFO] idaes.init.fs.flash.control_volume.properties_out: Initialization Step 4 optimal - Optimal Solution Found.\n",
77+
"2026-03-06 04:01:42 [INFO] idaes.init.fs.flash.control_volume.properties_out: Initialization Step 5 optimal - Optimal Solution Found.\n",
78+
"2026-03-06 04:01:42 [INFO] idaes.init.fs.flash.control_volume.properties_out: State Released.\n",
79+
"2026-03-06 04:01:42 [INFO] idaes.init.fs.flash.control_volume: Initialization Complete\n",
80+
"2026-03-06 04:01:42 [INFO] idaes.init.fs.flash.control_volume.properties_in: State Released.\n",
81+
"2026-03-06 04:01:42 [INFO] idaes.init.fs.flash: Initialization Complete: optimal - Optimal Solution Found\n"
82+
]
83+
},
84+
{
85+
"name": "stderr",
86+
"output_type": "stream",
87+
"text": [
88+
"2026-03-06 04:01:43,983 [INFO] - Server is already running PID=89224\n"
8389
]
8490
}
8591
],
@@ -101,7 +107,7 @@
101107
},
102108
{
103109
"cell_type": "code",
104-
"execution_count": 3,
110+
"execution_count": 8,
105111
"id": "02d7f383",
106112
"metadata": {},
107113
"outputs": [
@@ -115,15 +121,15 @@
115121
" Upper bound: .inf\n",
116122
" Number of objectives: 1\n",
117123
" Number of constraints: 41\n",
118-
" Number of variables: 41\n",
124+
" Number of variables: 42\n",
119125
" Sense: unknown\n",
120126
"Solver: \n",
121127
"- Status: ok\n",
122128
" Message: Ipopt 3.13.2\\x3a Optimal Solution Found\n",
123129
" Termination condition: optimal\n",
124130
" Id: 0\n",
125131
" Error rc: 0\n",
126-
" Time: 0.005049467086791992\n",
132+
" Time: 0.00626683235168457\n",
127133
"Solution: \n",
128134
"- number of solutions: 0\n",
129135
" number of solutions displayed: 0\n",
@@ -155,13 +161,12 @@
155161
"text": [
156162
"Time per step:\n",
157163
"\n",
158-
" build : 0.022 14.5%\n",
164+
" build : 0.018 12.4%\n",
159165
" set_operating_conditions : 0.000 0.1%\n",
160-
" initialize : 0.111 74.7%\n",
161-
" set_solver : 0.000 0.0%\n",
162-
" solve_initial : 0.016 10.7%\n",
166+
" initialize : 0.108 75.0%\n",
167+
" solve_initial : 0.018 12.5%\n",
163168
"\n",
164-
"Total time: 0.151 s\n",
169+
"Total time: 0.145 s\n",
165170
"\n"
166171
]
167172
}
@@ -236,7 +241,20 @@
236241
"name": "stdout",
237242
"output_type": "stream",
238243
"text": [
239-
"Before: 368\n",
244+
"Before: 368\n"
245+
]
246+
},
247+
{
248+
"name": "stderr",
249+
"output_type": "stream",
250+
"text": [
251+
"2026-03-06 04:01:46,069 [INFO] - Server is already running PID=89224\n"
252+
]
253+
},
254+
{
255+
"name": "stdout",
256+
"output_type": "stream",
257+
"text": [
240258
"After : 368.85306111169916\n"
241259
]
242260
}
@@ -301,7 +319,7 @@
301319
],
302320
"metadata": {
303321
"kernelspec": {
304-
"display_name": "idaes-py3.12",
322+
"display_name": "Python 3 (ipykernel)",
305323
"language": "python",
306324
"name": "python3"
307325
},

docs/examples/structfs/hda_flowsheet.py

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -323,16 +323,11 @@ def init_function(unit):
323323
seq.run(m, init_function)
324324

325325

326-
@FS.step("set_solver")
327-
def set_solver(ctx):
328-
ctx.solver = SolverFactory("ipopt")
329-
330-
331326
@FS.step("solve_initial")
332327
def solve(ctx):
333328
"""Perform the initial model solve."""
334-
ctx["status"] = results = ctx.solver.solve(ctx.model, tee=ctx["tee"])
335-
assert results.solver.termination_condition == TerminationCondition.optimal
329+
ctx.solve()
330+
assert ctx.results.solver.termination_condition == TerminationCondition.optimal
336331

337332

338333
@FS.step("solve_optimization")
@@ -373,5 +368,4 @@ def solve_opt(ctx):
373368

374369
m.fs.product_purity = Constraint(expr=m.fs.purity >= 0.80)
375370

376-
results = ctx.solver.solve(ctx.model, tee=ctx["tee"])
377-
ctx["results"] = results
371+
ctx.solve()

0 commit comments

Comments
 (0)