Skip to content

Commit

Permalink
Adding tests for scaling workshop
Browse files Browse the repository at this point in the history
  • Loading branch information
Andrew Lee committed Oct 24, 2024
1 parent fbd92b1 commit 60ae09f
Show file tree
Hide file tree
Showing 4 changed files with 293 additions and 81 deletions.
98 changes: 77 additions & 21 deletions idaes_examples/notebooks/docs/scaling/scaler_workshop.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,17 @@
"dt.report_structural_issues()"
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "c7b4d6f9",
"metadata": {},
"outputs": [],
"source": [
"# Make sure base model is constructed propertly\n",
"dt.assert_no_structural_warnings()"
]
},
{
"cell_type": "markdown",
"id": "242909ef",
Expand All @@ -187,7 +198,7 @@
},
{
"cell_type": "code",
"execution_count": 3,
"execution_count": 4,
"id": "e00f12e5",
"metadata": {},
"outputs": [],
Expand Down Expand Up @@ -216,7 +227,7 @@
},
{
"cell_type": "code",
"execution_count": 4,
"execution_count": 5,
"id": "7db4db04",
"metadata": {},
"outputs": [
Expand Down Expand Up @@ -330,7 +341,7 @@
"Number of equality constraint Jacobian evaluations = 44\n",
"Number of inequality constraint Jacobian evaluations = 0\n",
"Number of Lagrangian Hessian evaluations = 42\n",
"Total CPU secs in IPOPT (w/o function evaluations) = 0.005\n",
"Total CPU secs in IPOPT (w/o function evaluations) = 0.007\n",
"Total CPU secs in NLP function evaluations = 0.000\n",
"\n",
"EXIT: Converged to a point of local infeasibility. Problem may be infeasible.\n"
Expand Down Expand Up @@ -369,7 +380,7 @@
},
{
"cell_type": "code",
"execution_count": 5,
"execution_count": 6,
"id": "72492bd6",
"metadata": {},
"outputs": [],
Expand Down Expand Up @@ -492,7 +503,7 @@
},
{
"cell_type": "code",
"execution_count": 6,
"execution_count": 7,
"id": "96ca1083",
"metadata": {},
"outputs": [
Expand Down Expand Up @@ -546,7 +557,7 @@
},
{
"cell_type": "code",
"execution_count": 7,
"execution_count": 8,
"id": "85175874",
"metadata": {},
"outputs": [
Expand Down Expand Up @@ -595,7 +606,7 @@
},
{
"cell_type": "code",
"execution_count": 8,
"execution_count": 9,
"id": "43fc4d67",
"metadata": {},
"outputs": [],
Expand Down Expand Up @@ -645,7 +656,7 @@
},
{
"cell_type": "code",
"execution_count": 9,
"execution_count": 10,
"id": "b0363a58",
"metadata": {},
"outputs": [
Expand Down Expand Up @@ -709,7 +720,7 @@
},
{
"cell_type": "code",
"execution_count": 10,
"execution_count": 11,
"id": "aa684f2e",
"metadata": {},
"outputs": [
Expand Down Expand Up @@ -763,7 +774,7 @@
},
{
"cell_type": "code",
"execution_count": 11,
"execution_count": 12,
"id": "f845d90e",
"metadata": {},
"outputs": [],
Expand Down Expand Up @@ -815,7 +826,7 @@
},
{
"cell_type": "code",
"execution_count": 12,
"execution_count": 13,
"id": "cbad67e9",
"metadata": {},
"outputs": [],
Expand Down Expand Up @@ -884,7 +895,7 @@
},
{
"cell_type": "code",
"execution_count": 13,
"execution_count": 14,
"id": "c67fa218",
"metadata": {},
"outputs": [],
Expand Down Expand Up @@ -928,7 +939,7 @@
},
{
"cell_type": "code",
"execution_count": 14,
"execution_count": 15,
"id": "781f06d0",
"metadata": {},
"outputs": [
Expand Down Expand Up @@ -1109,7 +1120,7 @@
},
{
"cell_type": "code",
"execution_count": 15,
"execution_count": 16,
"id": "a1ae2667",
"metadata": {},
"outputs": [],
Expand Down Expand Up @@ -1216,7 +1227,7 @@
},
{
"cell_type": "code",
"execution_count": 16,
"execution_count": 17,
"id": "5f8e0e6f",
"metadata": {},
"outputs": [
Expand Down Expand Up @@ -1356,7 +1367,7 @@
},
{
"cell_type": "code",
"execution_count": 17,
"execution_count": 18,
"id": "389332f0",
"metadata": {},
"outputs": [],
Expand Down Expand Up @@ -1477,7 +1488,7 @@
},
{
"cell_type": "code",
"execution_count": 18,
"execution_count": 19,
"id": "75d11627",
"metadata": {},
"outputs": [
Expand Down Expand Up @@ -1565,6 +1576,27 @@
"check_scaling()"
]
},
{
"cell_type": "code",
"execution_count": 20,
"id": "77fe3766",
"metadata": {},
"outputs": [],
"source": [
"# Build a new isntance of the model for testing\n",
"test_model = build_model()\n",
"scaler = EquilibriumReactorScaler()\n",
"scaler.scale_model(test_model.fs.equil)\n",
"\n",
"# Check that the number of scaling facotrs assgined matches expectations\n",
"# We will assume they were set correctly\n",
"assert len(test_model.fs.equil.scaling_factor) == 1\n",
"assert len(test_model.fs.equil.control_volume.scaling_factor) == 14\n",
"assert len(test_model.fs.equil.control_volume.properties_in[0].scaling_factor) == 8\n",
"assert len(test_model.fs.equil.control_volume.properties_out[0].scaling_factor) == 9\n",
"assert len(test_model.fs.equil.control_volume.reactions[0].scaling_factor) == 4"
]
},
{
"cell_type": "markdown",
"id": "35e8d1ad",
Expand All @@ -1586,7 +1618,7 @@
},
{
"cell_type": "code",
"execution_count": 19,
"execution_count": 21,
"id": "80e47573",
"metadata": {},
"outputs": [
Expand Down Expand Up @@ -1650,6 +1682,17 @@
"dt.report_numerical_issues()"
]
},
{
"cell_type": "code",
"execution_count": 22,
"id": "dfec0c62",
"metadata": {},
"outputs": [],
"source": [
"# Test to ensure autoscaled model solved\n",
"assert check_optimal_termination(results)"
]
},
{
"cell_type": "markdown",
"id": "522076eb",
Expand Down Expand Up @@ -1679,7 +1722,7 @@
},
{
"cell_type": "code",
"execution_count": 20,
"execution_count": 23,
"id": "a5d82e55",
"metadata": {},
"outputs": [
Expand Down Expand Up @@ -1760,7 +1803,7 @@
"Number of equality constraint Jacobian evaluations = 12\n",
"Number of inequality constraint Jacobian evaluations = 0\n",
"Number of Lagrangian Hessian evaluations = 11\n",
"Total CPU secs in IPOPT (w/o function evaluations) = 0.000\n",
"Total CPU secs in IPOPT (w/o function evaluations) = 0.002\n",
"Total CPU secs in NLP function evaluations = 0.000\n",
"\n",
"EXIT: Optimal Solution Found.\n"
Expand Down Expand Up @@ -1806,14 +1849,27 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 24,
"id": "c03a51d5",
"metadata": {},
"outputs": [],
"source": [
"# Test that scaled model re-solves\n",
"results = solver.solve(m)\n",
"assert check_optimal_termination(results)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "682a983a",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"celltoolbar": "Tags",
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"language": "python",
Expand Down
Loading

0 comments on commit 60ae09f

Please sign in to comment.