Skip to content

Commit 59197e8

Browse files
author
Sufi Kaur
committed
add scalar value fix from paramest
1 parent 0b4c00b commit 59197e8

6 files changed

Lines changed: 320 additions & 52 deletions

File tree

idaes_examples/notebooks/docs/param_est/parameter_estimation_nrtl_using_state_block_test.ipynb

Lines changed: 26 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"cells": [
33
{
44
"cell_type": "code",
5-
"execution_count": 10,
5+
"execution_count": null,
66
"metadata": {
77
"tags": [
88
"header",
@@ -45,8 +45,7 @@
4545
"\n",
4646
"## Key links to documentation:\n",
4747
"* NRTL Model - https://idaes-pse.readthedocs.io/en/stable/reference_guides/model_libraries/generic/property_models/activity_coefficient.html\n",
48-
"* parmest - https://pyomo.readthedocs.io/en/stable/contributed_packages/parmest/index.html\n",
49-
""
48+
"* parmest - https://pyomo.readthedocs.io/en/stable/contributed_packages/parmest/index.html\n"
5049
]
5150
},
5251
{
@@ -61,7 +60,7 @@
6160
},
6261
{
6362
"cell_type": "code",
64-
"execution_count": 12,
63+
"execution_count": null,
6564
"metadata": {
6665
"tags": [
6766
"solution"
@@ -85,7 +84,7 @@
8584
},
8685
{
8786
"cell_type": "code",
88-
"execution_count": 13,
87+
"execution_count": null,
8988
"metadata": {
9089
"tags": []
9190
},
@@ -106,7 +105,7 @@
106105
},
107106
{
108107
"cell_type": "code",
109-
"execution_count": 14,
108+
"execution_count": null,
110109
"metadata": {
111110
"tags": []
112111
},
@@ -137,7 +136,18 @@
137136
},
138137
{
139138
"cell_type": "code",
140-
"execution_count": 16,
139+
"execution_count": null,
140+
"metadata": {},
141+
"outputs": [],
142+
"source": [
143+
"def get_scalar(data, key):\n",
144+
" v = data[key]\n",
145+
" return float(v.iloc[0] if hasattr(v, \"iloc\") else v)"
146+
]
147+
},
148+
{
149+
"cell_type": "code",
150+
"execution_count": null,
141151
"metadata": {
142152
"tags": [
143153
"solution"
@@ -188,7 +198,7 @@
188198
" m.fs.state_block.initialize(outlvl=idaeslog.INFO_LOW)\n",
189199
"\n",
190200
" # Fix at actual temperature\n",
191-
" m.fs.state_block.temperature.fix(float(data[\"temperature\"]))\n",
201+
" m.fs.state_block.temperature.fix(get_scalar(data, \"temperature\"))\n",
192202
"\n",
193203
" # Set bounds on variables to be estimated\n",
194204
" m.fs.properties.tau[\"benzene\", \"toluene\"].setlb(-5)\n",
@@ -203,7 +213,7 @@
203213
},
204214
{
205215
"cell_type": "code",
206-
"execution_count": 17,
216+
"execution_count": null,
207217
"metadata": {
208218
"tags": [
209219
"testing"
@@ -273,7 +283,7 @@
273283
},
274284
{
275285
"cell_type": "code",
276-
"execution_count": 19,
286+
"execution_count": null,
277287
"metadata": {
278288
"tags": [
279289
"solution"
@@ -304,7 +314,7 @@
304314
},
305315
{
306316
"cell_type": "code",
307-
"execution_count": 20,
317+
"execution_count": null,
308318
"metadata": {
309319
"tags": []
310320
},
@@ -345,10 +355,10 @@
345355
" # and vapor phase. For example, the squared error for the vapor phase is:\n",
346356
" # (float(data[\"vap_benzene\"]) - m.fs.state_block.mole_frac_phase_comp[\"Vap\", \"benzene\"])**2\n",
347357
" expr = (\n",
348-
" float(data[\"vap_benzene\"])\n",
358+
" get_scalar(data, \"vap_benzene\")\n",
349359
" - m.fs.state_block.mole_frac_phase_comp[\"Vap\", \"benzene\"]\n",
350360
" ) ** 2 + (\n",
351-
" float(data[\"liq_benzene\"])\n",
361+
" get_scalar(data, \"liq_benzene\")\n",
352362
" - m.fs.state_block.mole_frac_phase_comp[\"Liq\", \"benzene\"]\n",
353363
" ) ** 2\n",
354364
" return expr * 1e4"
@@ -465,7 +475,7 @@
465475
"metadata": {
466476
"celltoolbar": "Tags",
467477
"kernelspec": {
468-
"display_name": "Python 3 (ipykernel)",
478+
"display_name": "idaes-examples-dev",
469479
"language": "python",
470480
"name": "python3"
471481
},
@@ -479,9 +489,9 @@
479489
"name": "python",
480490
"nbconvert_exporter": "python",
481491
"pygments_lexer": "ipython3",
482-
"version": "3.10.9"
492+
"version": "3.12.13"
483493
}
484494
},
485495
"nbformat": 4,
486496
"nbformat_minor": 3
487-
}
497+
}

idaes_examples/notebooks/docs/param_est/parameter_estimation_nrtl_using_unit_model_test.ipynb

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,17 @@
133133
"</div>"
134134
]
135135
},
136+
{
137+
"cell_type": "code",
138+
"execution_count": null,
139+
"metadata": {},
140+
"outputs": [],
141+
"source": [
142+
"def get_scalar(data, key):\n",
143+
" v = data[key]\n",
144+
" return float(v.iloc[0] if hasattr(v, \"iloc\") else v)"
145+
]
146+
},
136147
{
137148
"cell_type": "code",
138149
"execution_count": null,
@@ -187,7 +198,7 @@
187198
" m.fs.flash.initialize(outlvl=idaeslog.INFO_LOW)\n",
188199
"\n",
189200
" # Fix at actual temperature\n",
190-
" m.fs.flash.inlet.temperature.fix(float(data[\"temperature\"]))\n",
201+
" m.fs.flash.inlet.temperature.fix(get_scalar(data, \"temperature\"))\n",
191202
"\n",
192203
" # Set bounds on variables to be estimated\n",
193204
" m.fs.properties.tau[\"benzene\", \"toluene\"].setlb(-5)\n",
@@ -343,9 +354,9 @@
343354
" # and vapor phase. For example, the squared error for the vapor phase is:\n",
344355
" # (float(data[\"vap_benzene\"]) - m.fs.flash.vap_outlet.mole_frac_comp[0, \"benzene\"])**2\n",
345356
" expr = (\n",
346-
" float(data[\"vap_benzene\"]) - m.fs.flash.vap_outlet.mole_frac_comp[0, \"benzene\"]\n",
357+
" get_scalar(data, \"vap_benzene\") - m.fs.flash.vap_outlet.mole_frac_comp[0, \"benzene\"]\n",
347358
" ) ** 2 + (\n",
348-
" float(data[\"liq_benzene\"]) - m.fs.flash.liq_outlet.mole_frac_comp[0, \"benzene\"]\n",
359+
" get_scalar(data, \"liq_benzene\") - m.fs.flash.liq_outlet.mole_frac_comp[0, \"benzene\"]\n",
349360
" ) ** 2\n",
350361
" return expr * 1e4"
351362
]
@@ -461,7 +472,7 @@
461472
"metadata": {
462473
"celltoolbar": "Tags",
463474
"kernelspec": {
464-
"display_name": "Python 3 (ipykernel)",
475+
"display_name": "idaes-examples-dev",
465476
"language": "python",
466477
"name": "python3"
467478
},
@@ -475,7 +486,7 @@
475486
"name": "python",
476487
"nbconvert_exporter": "python",
477488
"pygments_lexer": "ipython3",
478-
"version": "3.9.12"
489+
"version": "3.12.13"
479490
}
480491
},
481492
"nbformat": 4,

idaes_examples/notebooks/docs/properties/parameter_estimation_pr_test.ipynb

Lines changed: 34 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@
5757
},
5858
{
5959
"cell_type": "code",
60-
"execution_count": 1,
60+
"execution_count": null,
6161
"metadata": {},
6262
"outputs": [],
6363
"source": [
@@ -83,7 +83,7 @@
8383
},
8484
{
8585
"cell_type": "code",
86-
"execution_count": 2,
86+
"execution_count": null,
8787
"metadata": {},
8888
"outputs": [],
8989
"source": [
@@ -99,7 +99,7 @@
9999
},
100100
{
101101
"cell_type": "code",
102-
"execution_count": 3,
102+
"execution_count": null,
103103
"metadata": {},
104104
"outputs": [],
105105
"source": [
@@ -121,7 +121,7 @@
121121
},
122122
{
123123
"cell_type": "code",
124-
"execution_count": 4,
124+
"execution_count": null,
125125
"metadata": {},
126126
"outputs": [],
127127
"source": [
@@ -146,12 +146,24 @@
146146
},
147147
{
148148
"cell_type": "code",
149-
"execution_count": 5,
149+
"execution_count": null,
150150
"metadata": {},
151151
"outputs": [],
152152
"source": [
153-
"def PR_model(data):\n",
154-
"\n",
153+
"def get_scalar(data, key):\n",
154+
" v = data[key]\n",
155+
" return float(v.iloc[0] if hasattr(v, \"iloc\") else v)"
156+
]
157+
},
158+
{
159+
"cell_type": "code",
160+
"execution_count": null,
161+
"metadata": {},
162+
"outputs": [],
163+
"source": [
164+
"def PR_model(experiment_number=None, cb_data=None):\n",
165+
" # cb_data is typically the row/record for this experiment\n",
166+
" data = cb_data if cb_data is not None else experiment_number\n",
155167
" m = ConcreteModel()\n",
156168
"\n",
157169
" m.fs = FlowsheetBlock(dynamic=False)\n",
@@ -161,9 +173,9 @@
161173
" m.fs.state_block = m.fs.properties.build_state_block([1], defined_state=True)\n",
162174
"\n",
163175
" m.fs.state_block[1].flow_mol.fix(1)\n",
164-
" x = float(data[\"x_carbon_dioxide\"]) + 0.5\n",
165-
" m.fs.state_block[1].temperature.fix(float(data[\"temperature\"]))\n",
166-
" m.fs.state_block[1].pressure.fix(float(data[\"pressure\"]))\n",
176+
" x = get_scalar(data, \"x_carbon_dioxide\") + 0.5\n",
177+
" m.fs.state_block[1].temperature.fix(get_scalar(data, \"temperature\"))\n",
178+
" m.fs.state_block[1].pressure.fix(get_scalar(data, \"pressure\"))\n",
167179
" m.fs.state_block[1].mole_frac_comp[\"bmimPF6\"].fix(1 - x)\n",
168180
" m.fs.state_block[1].mole_frac_comp[\"carbon_dioxide\"].fix(x)\n",
169181
"\n",
@@ -178,14 +190,14 @@
178190
"\n",
179191
" # Fix the state variables on the state block\n",
180192
" m.fs.state_block[1].pressure.unfix()\n",
181-
" m.fs.state_block[1].temperature.fix(float(data[\"temperature\"]))\n",
193+
" m.fs.state_block[1].temperature.fix(get_scalar(data, \"temperature\"))\n",
182194
" m.fs.state_block[1].mole_frac_phase_comp[\"Liq\", \"bmimPF6\"].fix(\n",
183-
" float(data[\"x_bmimPF6\"])\n",
195+
" get_scalar(data, \"x_bmimPF6\")\n",
184196
" )\n",
185197
" m.fs.state_block[1].mole_frac_phase_comp[\"Liq\", \"carbon_dioxide\"].fix(\n",
186-
" float(data[\"x_carbon_dioxide\"])\n",
198+
" get_scalar(data, \"x_carbon_dioxide\")\n",
187199
" )\n",
188-
" m.fs.state_block[1].mole_frac_comp[\"bmimPF6\"].fix(float(data[\"x_bmimPF6\"]))\n",
200+
" m.fs.state_block[1].mole_frac_comp[\"bmimPF6\"].fix(get_scalar(data, \"x_bmimPF6\"))\n",
189201
" m.fs.state_block[1].mole_frac_comp[\"carbon_dioxide\"].unfix()\n",
190202
" # Set bounds on variables to be estimated\n",
191203
" m.fs.properties.PR_kappa[\"bmimPF6\", \"carbon_dioxide\"].setlb(-5)\n",
@@ -207,7 +219,7 @@
207219
},
208220
{
209221
"cell_type": "code",
210-
"execution_count": 6,
222+
"execution_count": null,
211223
"metadata": {
212224
"scrolled": false
213225
},
@@ -242,7 +254,7 @@
242254
},
243255
{
244256
"cell_type": "code",
245-
"execution_count": 7,
257+
"execution_count": null,
246258
"metadata": {},
247259
"outputs": [],
248260
"source": [
@@ -263,12 +275,12 @@
263275
},
264276
{
265277
"cell_type": "code",
266-
"execution_count": 8,
278+
"execution_count": null,
267279
"metadata": {},
268280
"outputs": [],
269281
"source": [
270282
"def SSE(m, data):\n",
271-
" expr = (float(data[\"pressure\"]) - m.fs.state_block[1].pressure) ** 2\n",
283+
" expr = (get_scalar(data, \"pressure\") - m.fs.state_block[1].pressure) ** 2\n",
272284
" return expr * 1e-7"
273285
]
274286
},
@@ -283,7 +295,7 @@
283295
},
284296
{
285297
"cell_type": "code",
286-
"execution_count": 9,
298+
"execution_count": null,
287299
"metadata": {
288300
"scrolled": false
289301
},
@@ -305,7 +317,7 @@
305317
},
306318
{
307319
"cell_type": "code",
308-
"execution_count": 10,
320+
"execution_count": null,
309321
"metadata": {},
310322
"outputs": [],
311323
"source": [
@@ -322,18 +334,11 @@
322334
"source": [
323335
"Now we can use this parameters and include them in the configuration dictionary. We can also use `m.fs.properties = GenericParameterBlock(**configuration)` to solve unit models."
324336
]
325-
},
326-
{
327-
"cell_type": "code",
328-
"execution_count": null,
329-
"metadata": {},
330-
"outputs": [],
331-
"source": []
332337
}
333338
],
334339
"metadata": {
335340
"kernelspec": {
336-
"display_name": "Python 3 (ipykernel)",
341+
"display_name": "idaes-examples-dev",
337342
"language": "python",
338343
"name": "python3"
339344
},
@@ -347,7 +352,7 @@
347352
"name": "python",
348353
"nbconvert_exporter": "python",
349354
"pygments_lexer": "ipython3",
350-
"version": "3.9.12"
355+
"version": "3.12.13"
351356
}
352357
},
353358
"nbformat": 4,

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ omlt = [
5454
"tensorflow >= 2.16.1",
5555
]
5656
idaes = [
57-
"idaes-pse", # installing IDAES (from release) is opt-in
57+
"idaes-pse @ git+https://github.com/sufikaur/idaes-pse@issue-1802", # installing IDAES (from release) is opt-in
5858
]
5959
testing = [
6060
# parallel pytest

requirements-dev.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
--editable .[dev,omlt]
22

3-
idaes-pse @ git+https://github.com/IDAES/idaes-pse@main
3+
idaes-pse @ git+https://github.com/sufikaur/idaes-pse@issue-1802
44
# if you want to install idaes-pse from a PR instead of the main branch,
55
# uncomment the line below replacing XYZ with the PR number
66
# idaes-pse @ git+https://github.com/IDAES/idaes-pse@refs/pull/XYZ/merge

0 commit comments

Comments
 (0)