{ "cells": [ { "cell_type": "markdown", "id": "6ce7f3b0", "metadata": {}, "source": [ "\n", "*This notebook contains material from [CBE60499](https://ndcbe.github.io/CBE60499);\n", "content is available [on Github](git@github.com:ndcbe/CBE60499.git).*\n" ] }, { "cell_type": "markdown", "id": "19a4bd73", "metadata": {}, "source": [ "\n", "< [2.11 Pyomo Homework 2](https://ndcbe.github.io/CBE60499/02.11-Pyomo2.html) | [Contents](toc.html) | [Tag Index](tag_index.html) | [3.0 Unconstrained Nonlinear Optimization: Theory and Algorithms](https://ndcbe.github.io/CBE60499/03.00-Unconstrained.html) >
"
]
},
{
"cell_type": "markdown",
"metadata": {
"nbpages": {
"level": 1,
"link": "[2.12 Pyomo Homework 3](https://ndcbe.github.io/CBE60499/02.12-Pyomo3.html#2.12-Pyomo-Homework-3)",
"section": "2.12 Pyomo Homework 3"
}
},
"source": [
"# 2.12 Pyomo Homework 3"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"nbpages": {
"level": 1,
"link": "[2.12 Pyomo Homework 3](https://ndcbe.github.io/CBE60499/02.12-Pyomo3.html#2.12-Pyomo-Homework-3)",
"section": "2.12 Pyomo Homework 3"
}
},
"outputs": [],
"source": [
"# Import the libraries you need here for the assignment\n",
"# YOUR SOLUTION HERE"
]
},
{
"cell_type": "markdown",
"metadata": {
"nbpages": {
"level": 2,
"link": "[2.12.1 Pyomo.DAE: Reaction Kinetics](https://ndcbe.github.io/CBE60499/02.12-Pyomo3.html#2.12.1-Pyomo.DAE:-Reaction-Kinetics)",
"section": "2.12.1 Pyomo.DAE: Reaction Kinetics"
}
},
"source": [
"## 2.12.1 Pyomo.DAE: Reaction Kinetics"
]
},
{
"cell_type": "markdown",
"metadata": {
"nbpages": {
"level": 2,
"link": "[2.12.1 Pyomo.DAE: Reaction Kinetics](https://ndcbe.github.io/CBE60499/02.12-Pyomo3.html#2.12.1-Pyomo.DAE:-Reaction-Kinetics)",
"section": "2.12.1 Pyomo.DAE: Reaction Kinetics"
}
},
"source": [
"Consider the chemical reaction $$A \\Leftrightarrow B \\Leftrightarrow C$$\n",
"\n",
"which is modeling with the following differential algebraic equations:\n",
"\n",
"$$\\begin{align*}\n",
"\\frac{dz_A}{dt} &= -p_1 z_A(t) + p_2 z_B(t),\\quad z_A(0)=1 \\\\\n",
"\\frac{dz_B}{dt} &= p_1 z_A(t) - (p_2 + p_3) z_B(t) + p_4 z_C(t), \\quad z_B(0)=0 \\\\\n",
"1 &= z_A(t) + z_B(t) + z_C(t)\n",
"\\end{align*}$$\n",
"\n",
"where $p_1=4$, $p_2=2$, $p_3=40$, and $p_4=20$ are parameters with the appropriate units. $z_A(t)$, $z_B(t)$, and $z_C(t)$ are time varying concentrations of species $A$, $B$, and $C$ respectively."
]
},
{
"cell_type": "markdown",
"metadata": {
"nbpages": {
"level": 3,
"link": "[2.12.1.1 Index analysis](https://ndcbe.github.io/CBE60499/02.12-Pyomo3.html#2.12.1.1-Index-analysis)",
"section": "2.12.1.1 Index analysis"
}
},
"source": [
"### 2.12.1.1 Index analysis"
]
},
{
"cell_type": "markdown",
"metadata": {
"nbpages": {
"level": 3,
"link": "[2.12.1.1 Index analysis](https://ndcbe.github.io/CBE60499/02.12-Pyomo3.html#2.12.1.1-Index-analysis)",
"section": "2.12.1.1 Index analysis"
}
},
"source": [
"Determine the index of the above differential algebraic equation (DAE) system above.\n",
"\n",
"Tip: do this on paper. On Gradescope, there will be a separate assignment for you to turn in your handwritten work."
]
},
{
"cell_type": "markdown",
"metadata": {
"nbpages": {
"level": 3,
"link": "[2.12.1.2 Model reformulation](https://ndcbe.github.io/CBE60499/02.12-Pyomo3.html#2.12.1.2-Model-reformulation)",
"section": "2.12.1.2 Model reformulation"
}
},
"source": [
"### 2.12.1.2 Model reformulation"
]
},
{
"cell_type": "markdown",
"metadata": {
"nbpages": {
"level": 3,
"link": "[2.12.1.2 Model reformulation](https://ndcbe.github.io/CBE60499/02.12-Pyomo3.html#2.12.1.2-Model-reformulation)",
"section": "2.12.1.2 Model reformulation"
}
},
"source": [
"Apply the index reduction algorithm from class as needed. Ultimately identify two versions of the model: one that is index 1 and another that is index 0. Find a consistent initial condition $z_C(0)$."
]
},
{
"cell_type": "markdown",
"metadata": {
"nbpages": {
"level": 3,
"link": "[2.12.1.3 Implement index 1 model in Pyomo](https://ndcbe.github.io/CBE60499/02.12-Pyomo3.html#2.12.1.3-Implement-index-1-model-in-Pyomo)",
"section": "2.12.1.3 Implement index 1 model in Pyomo"
}
},
"source": [
"### 2.12.1.3 Implement index 1 model in Pyomo"
]
},
{
"cell_type": "markdown",
"metadata": {
"nbpages": {
"level": 3,
"link": "[2.12.1.3 Implement index 1 model in Pyomo](https://ndcbe.github.io/CBE60499/02.12-Pyomo3.html#2.12.1.3-Implement-index-1-model-in-Pyomo)",
"section": "2.12.1.3 Implement index 1 model in Pyomo"
}
},
"source": [
"We will be building a library of functions."
]
},
{
"cell_type": "markdown",
"metadata": {
"nbpages": {
"level": 4,
"link": "[2.12.1.3.1 Create model and set initial conditions](https://ndcbe.github.io/CBE60499/02.12-Pyomo3.html#2.12.1.3.1-Create-model-and-set-initial-conditions)",
"section": "2.12.1.3.1 Create model and set initial conditions"
}
},
"source": [
"#### 2.12.1.3.1 Create model and set initial conditions"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"nbpages": {
"level": 4,
"link": "[2.12.1.3.1 Create model and set initial conditions](https://ndcbe.github.io/CBE60499/02.12-Pyomo3.html#2.12.1.3.1-Create-model-and-set-initial-conditions)",
"section": "2.12.1.3.1 Create model and set initial conditions"
}
},
"outputs": [],
"source": [
"def create_model():\n",
" ''' Create index 1 model and set initial conditions\n",
" \n",
" Return:\n",
" m: Pyomo model\n",
" '''\n",
" \n",
" # Tip: Set time to go from 0 to 1 when creating the model.\n",
" \n",
" # YOUR SOLUTION HERE\n",
" \n",
" return m"
]
},
{
"cell_type": "markdown",
"metadata": {
"nbpages": {
"level": 3,
"link": "[2.12.1.4 Simulate, discretize, and initialize collocation model](https://ndcbe.github.io/CBE60499/02.12-Pyomo3.html#2.12.1.4-Simulate,-discretize,-and-initialize-collocation-model)",
"section": "2.12.1.4 Simulate, discretize, and initialize collocation model"
}
},
"source": [
"### 2.12.1.4 Simulate, discretize, and initialize collocation model"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"nbpages": {
"level": 3,
"link": "[2.12.1.4 Simulate, discretize, and initialize collocation model](https://ndcbe.github.io/CBE60499/02.12-Pyomo3.html#2.12.1.4-Simulate,-discretize,-and-initialize-collocation-model)",
"section": "2.12.1.4 Simulate, discretize, and initialize collocation model"
}
},
"outputs": [],
"source": [
"def simulate_discretize_model(m,NFE,initialize):\n",
" ''' Simulation, discretize, and initialize the Pyomo model\n",
" \n",
" Arguments:\n",
" m: Pyomo model\n",
" NFE: number of finite elements to consider (integer)\n",
" initialize: if True, initialize the discretized model with the \n",
" integrator solution (boolean)\n",
" \n",
" Returns:\n",
" sim: Simulator object from Pyomo.DAE\n",
" tsim: Timesteps returned from simulator\n",
" profiles: Results returned from simulator\n",
" \n",
" Overall Steps:\n",
" 1. Create Pyomo.DAE simulator and integrate with casadi/idas\n",
" 2. Transform model using 'dae.collocation' strategy. Use 3 collocation points\n",
" per finite elemebt\n",
" 3. If initialize is true, call 'sim.initialize_model()'. This will use the\n",
" Simulator solution to initialize the discretized Pyomo model. Really cool!\n",
" '''\n",
" \n",
" # YOUR SOLUTION HERE\n",
" \n",
" return sim, tsim, profiles"
]
},
{
"cell_type": "markdown",
"metadata": {
"nbpages": {
"level": 3,
"link": "[2.12.1.5 Plot results](https://ndcbe.github.io/CBE60499/02.12-Pyomo3.html#2.12.1.5-Plot-results)",
"section": "2.12.1.5 Plot results"
}
},
"source": [
"### 2.12.1.5 Plot results"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"nbpages": {
"level": 3,
"link": "[2.12.1.5 Plot results](https://ndcbe.github.io/CBE60499/02.12-Pyomo3.html#2.12.1.5-Plot-results)",
"section": "2.12.1.5 Plot results"
}
},
"outputs": [],
"source": [
"def plot_result(m, sim, tsim, profiles, include_model_values):\n",
" \"\"\" Plot the results from the simulator (and optionally Pyomo model)\n",
" \n",
" Arguments:\n",
" m: Pyomo model\n",
" sim: Pyomo.DAE simulator\n",
" tsim: timesteps from simulator\n",
" profiles: results from simulation\n",
" include_model_values: if True, also plot the values from the Pyomo model m\n",
" \n",
" Returns:\n",
" nothing\n",
" \n",
" Actions/Steps/Tips:\n",
" 1. Plot the results stored in tsim and profiles as solid lines. Recycle code from class.\n",
" 2. If 'include_model_values' is true, plot za, zb, and zc values stored in Pyomo model 'm'.\n",
" Use a solid symbol.\n",
" 3. Add a legend and axes labels\n",
" \"\"\"\n",
" \n",
" # YOUR SOLUTION HERE\n",
" \n",
" # Tip: Do not forget to include `plt.show()` (assuming you imported matplotlib.pyplot as plt)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"nbpages": {
"level": 3,
"link": "[2.12.1.5 Plot results](https://ndcbe.github.io/CBE60499/02.12-Pyomo3.html#2.12.1.5-Plot-results)",
"section": "2.12.1.5 Plot results"
}
},
"outputs": [],
"source": [
"def solve_model(m):\n",
" \"\"\" Solve discretized model with Ipopt\n",
" \n",
" Arguments:\n",
" m: Pyomo model\n",
" \n",
" Returns:\n",
" nothing\n",
" \"\"\"\n",
" \n",
" # Specify initial conditions\n",
" def _init(m):\n",
" yield m.za[0] == 1\n",
" yield m.zb[0] == 0\n",
" model.initcon = ConstraintList(rule=_init)\n",
"\n",
" # Solve collocation formulation (no objective, we are just simulating)\n",
" solver = SolverFactory('ipopt')\n",
" solver.solve(model,tee=True)"
]
},
{
"cell_type": "markdown",
"metadata": {
"nbpages": {
"level": 3,
"link": "[2.12.1.6 Simulate and solve Pyomo model with initialization](https://ndcbe.github.io/CBE60499/02.12-Pyomo3.html#2.12.1.6-Simulate-and-solve-Pyomo-model-with-initialization)",
"section": "2.12.1.6 Simulate and solve Pyomo model with initialization"
}
},
"source": [
"### 2.12.1.6 Simulate and solve Pyomo model with initialization"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"nbpages": {
"level": 3,
"link": "[2.12.1.6 Simulate and solve Pyomo model with initialization](https://ndcbe.github.io/CBE60499/02.12-Pyomo3.html#2.12.1.6-Simulate-and-solve-Pyomo-model-with-initialization)",
"section": "2.12.1.6 Simulate and solve Pyomo model with initialization"
}
},
"outputs": [],
"source": [
"# Create Pyomo model\n",
"model = create_model()\n",
"\n",
"# Initialize discretized model with simulation result?\n",
"init = True\n",
"\n",
"# Number of finite elements\n",
"NFE = 6\n",
"\n",
"# Simulate model\n",
"sim, tsim, profiles = simulate_discretize_model(model,NFE,init)\n",
"\n",
"# Plot simulation results\n",
"plot_result(model, sim, tsim, profiles,True)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"nbpages": {
"level": 3,
"link": "[2.12.1.6 Simulate and solve Pyomo model with initialization](https://ndcbe.github.io/CBE60499/02.12-Pyomo3.html#2.12.1.6-Simulate-and-solve-Pyomo-model-with-initialization)",
"section": "2.12.1.6 Simulate and solve Pyomo model with initialization"
}
},
"outputs": [],
"source": [
"# Solve collocation formulation with Ipopt\n",
"solve_model(model)\n",
"\n",
"# Plot results\n",
"plot_result(model, sim, tsim, profiles, True)"
]
},
{
"cell_type": "markdown",
"metadata": {
"nbpages": {
"level": 3,
"link": "[2.12.1.7 Simulate and solve Pyomo model without initialization](https://ndcbe.github.io/CBE60499/02.12-Pyomo3.html#2.12.1.7-Simulate-and-solve-Pyomo-model-without-initialization)",
"section": "2.12.1.7 Simulate and solve Pyomo model without initialization"
}
},
"source": [
"### 2.12.1.7 Simulate and solve Pyomo model without initialization"
]
},
{
"cell_type": "markdown",
"metadata": {
"nbpages": {
"level": 3,
"link": "[2.12.1.7 Simulate and solve Pyomo model without initialization](https://ndcbe.github.io/CBE60499/02.12-Pyomo3.html#2.12.1.7-Simulate-and-solve-Pyomo-model-without-initialization)",
"section": "2.12.1.7 Simulate and solve Pyomo model without initialization"
}
},
"source": [
"Repeat the analysis from above, but do NOT initialize the discretized model with the simulation result. (Tip: you only need to change two small things.)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"nbpages": {
"level": 3,
"link": "[2.12.1.7 Simulate and solve Pyomo model without initialization](https://ndcbe.github.io/CBE60499/02.12-Pyomo3.html#2.12.1.7-Simulate-and-solve-Pyomo-model-without-initialization)",
"section": "2.12.1.7 Simulate and solve Pyomo model without initialization"
}
},
"outputs": [],
"source": [
"# YOUR SOLUTION HERE"
]
},
{
"cell_type": "markdown",
"metadata": {
"nbpages": {
"level": 3,
"link": "[2.12.1.8 Discussion: Does initialization matter?](https://ndcbe.github.io/CBE60499/02.12-Pyomo3.html#2.12.1.8-Discussion:-Does-initialization-matter?)",
"section": "2.12.1.8 Discussion: Does initialization matter?"
}
},
"source": [
"### 2.12.1.8 Discussion: Does initialization matter?\n",
"\n",
"Write 1 to 3 sentences for each of the following questions:\n",
"\n",
"*What happens if we disable initialization? Does the number of interactions Ipopt need change?*\n",
"\n",
"*Why is this specific problem robust to poor initialization? What about this specific DAE system makes it easy to solve?*"
]
},
{
"cell_type": "markdown",
"metadata": {
"nbpages": {
"level": 3,
"link": "[2.12.1.9 Degree of Freedom Analysis](https://ndcbe.github.io/CBE60499/02.12-Pyomo3.html#2.12.1.9-Degree-of-Freedom-Analysis)",
"section": "2.12.1.9 Degree of Freedom Analysis"
}
},
"source": [
"### 2.12.1.9 Degree of Freedom Analysis"
]
},
{
"cell_type": "markdown",
"metadata": {
"nbpages": {
"level": 3,
"link": "[2.12.1.9 Degree of Freedom Analysis](https://ndcbe.github.io/CBE60499/02.12-Pyomo3.html#2.12.1.9-Degree-of-Freedom-Analysis)",
"section": "2.12.1.9 Degree of Freedom Analysis"
}
},
"source": [
"Please answer on paper and turn in via Gradescope."
]
},
{
"cell_type": "markdown",
"metadata": {
"nbpages": {
"level": 3,
"link": "[2.12.1.9 Degree of Freedom Analysis](https://ndcbe.github.io/CBE60499/02.12-Pyomo3.html#2.12.1.9-Degree-of-Freedom-Analysis)",
"section": "2.12.1.9 Degree of Freedom Analysis"
}
},
"source": [
"Rerun the code above for two different numbers of finite elements. Record the total number of variables and equality constraints."
]
},
{
"cell_type": "markdown",
"metadata": {
"nbpages": {
"level": 3,
"link": "[2.12.1.9 Degree of Freedom Analysis](https://ndcbe.github.io/CBE60499/02.12-Pyomo3.html#2.12.1.9-Degree-of-Freedom-Analysis)",
"section": "2.12.1.9 Degree of Freedom Analysis"
}
},
"source": [
"How many degrees of freedom are in the simulation problem? (1 sentence is fine.)"
]
},
{
"cell_type": "markdown",
"metadata": {
"nbpages": {
"level": 3,
"link": "[2.12.1.9 Degree of Freedom Analysis](https://ndcbe.github.io/CBE60499/02.12-Pyomo3.html#2.12.1.9-Degree-of-Freedom-Analysis)",
"section": "2.12.1.9 Degree of Freedom Analysis"
}
},
"source": [
"Choose $N=3$ or a simular number of finite elements. Using the collocation equations from class, show that the discretized Pyomo model has the correct number of variables and algebriac equations. This will be a little tedious, but good to practice once on a simple model. You might need to do this when debugging a DAE model for research."
]
},
{
"cell_type": "markdown",
"id": "cfacbbd1",
"metadata": {},
"source": [
"\n",
"< [2.11 Pyomo Homework 2](https://ndcbe.github.io/CBE60499/02.11-Pyomo2.html) | [Contents](toc.html) | [Tag Index](tag_index.html) | [3.0 Unconstrained Nonlinear Optimization: Theory and Algorithms](https://ndcbe.github.io/CBE60499/03.00-Unconstrained.html) >
"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.8.6"
}
},
"nbformat": 4,
"nbformat_minor": 2
}