{ "cells": [ { "cell_type": "markdown", "id": "507f5c71", "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": "2ad910a6", "metadata": {}, "source": [ "\n", "< [2.3 Logical Modeling and Generalized Disjunctive Programs](https://ndcbe.github.io/CBE60499/02.03-GDP.html) | [Contents](toc.html) | [Tag Index](tag_index.html) | [2.5 Numeric Integration for DAEs](https://ndcbe.github.io/CBE60499/02.05-Numeric-Integration.html) >
"
]
},
{
"cell_type": "markdown",
"metadata": {
"nbpages": {
"level": 1,
"link": "[2.4 Dynamic Optimization: Differential Algebraic Equations (DAEs)](https://ndcbe.github.io/CBE60499/02.04-DAE-modeling.html#2.4-Dynamic-Optimization:-Differential-Algebraic-Equations-(DAEs))",
"section": "2.4 Dynamic Optimization: Differential Algebraic Equations (DAEs)"
}
},
"source": [
"# 2.4 Dynamic Optimization: Differential Algebraic Equations (DAEs)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"nbpages": {
"level": 1,
"link": "[2.4 Dynamic Optimization: Differential Algebraic Equations (DAEs)](https://ndcbe.github.io/CBE60499/02.04-DAE-modeling.html#2.4-Dynamic-Optimization:-Differential-Algebraic-Equations-(DAEs))",
"section": "2.4 Dynamic Optimization: Differential Algebraic Equations (DAEs)"
}
},
"outputs": [],
"source": [
"import sys\n",
"if \"google.colab\" in sys.modules:\n",
" !wget \"https://raw.githubusercontent.com/ndcbe/CBE60499/main/notebooks/helper.py\"\n",
" import helper\n",
" !pip install casadi\n",
" helper.install_idaes()\n",
" #helper.install_ipopt()"
]
},
{
"cell_type": "markdown",
"metadata": {
"nbpages": {
"level": 2,
"link": "[2.4.1 Dynamic Optimization Overview](https://ndcbe.github.io/CBE60499/02.04-DAE-modeling.html#2.4.1-Dynamic-Optimization-Overview)",
"section": "2.4.1 Dynamic Optimization Overview"
}
},
"source": [
"## 2.4.1 Dynamic Optimization Overview"
]
},
{
"cell_type": "markdown",
"metadata": {
"nbpages": {
"level": 2,
"link": "[2.4.1 Dynamic Optimization Overview](https://ndcbe.github.io/CBE60499/02.04-DAE-modeling.html#2.4.1-Dynamic-Optimization-Overview)",
"section": "2.4.1 Dynamic Optimization Overview"
}
},
"source": [
"**Chapter 8: Dynamic Optimization Introduction** (Biegler, 2010)\n",
"-\tChemical engineering examples\n",
"-\tClassical (variational) approaches including Hamiltonian and Euler-Lagrange equations\n",
"-\tDAE background (this notebook)\n",
"\n",
"**Chapter 9: Sequential Methods** (Biegler, 2010)\n",
"-\tDAE integration (this notebook, next notebook)\n",
"-\tSingle Shooting\n",
"-\tMultiple Shooting\n",
"\n",
"**Chapter 10: Simultaneous Methods** (Biegler, 2010)\n",
"-\tGauss quadrature (next notebook)\n",
"-\tOrthogonal collocation on finite elements (next notebook, next next notebook)\n",
"-\tExamples, benchmarks, and large-scale extensions"
]
},
{
"cell_type": "markdown",
"metadata": {
"nbpages": {
"level": 2,
"link": "[2.4.1 Dynamic Optimization Overview](https://ndcbe.github.io/CBE60499/02.04-DAE-modeling.html#2.4.1-Dynamic-Optimization-Overview)",
"section": "2.4.1 Dynamic Optimization Overview"
}
},
"source": [
""
]
},
{
"cell_type": "markdown",
"metadata": {
"nbpages": {
"level": 2,
"link": "[2.4.1 Dynamic Optimization Overview](https://ndcbe.github.io/CBE60499/02.04-DAE-modeling.html#2.4.1-Dynamic-Optimization-Overview)",
"section": "2.4.1 Dynamic Optimization Overview"
}
},
"source": [
""
]
},
{
"cell_type": "markdown",
"metadata": {
"nbpages": {
"level": 2,
"link": "[2.4.2 DAE Index Reduction](https://ndcbe.github.io/CBE60499/02.04-DAE-modeling.html#2.4.2-DAE-Index-Reduction)",
"section": "2.4.2 DAE Index Reduction"
}
},
"source": [
"## 2.4.2 DAE Index Reduction"
]
},
{
"cell_type": "markdown",
"metadata": {
"nbpages": {
"level": 2,
"link": "[2.4.2 DAE Index Reduction](https://ndcbe.github.io/CBE60499/02.04-DAE-modeling.html#2.4.2-DAE-Index-Reduction)",
"section": "2.4.2 DAE Index Reduction"
}
},
"source": [
"Excerpts from Chapter 8 of Biegler (2010)."
]
},
{
"cell_type": "markdown",
"metadata": {
"nbpages": {
"level": 2,
"link": "[2.4.2 DAE Index Reduction](https://ndcbe.github.io/CBE60499/02.04-DAE-modeling.html#2.4.2-DAE-Index-Reduction)",
"section": "2.4.2 DAE Index Reduction"
}
},
"source": [
"\n",
""
]
},
{
"cell_type": "markdown",
"metadata": {
"nbpages": {
"level": 2,
"link": "[2.4.2 DAE Index Reduction](https://ndcbe.github.io/CBE60499/02.04-DAE-modeling.html#2.4.2-DAE-Index-Reduction)",
"section": "2.4.2 DAE Index Reduction"
}
},
"source": [
""
]
},
{
"cell_type": "markdown",
"metadata": {
"nbpages": {
"level": 2,
"link": "[2.4.3 DAE Formulations for Simple Pendulum Example](https://ndcbe.github.io/CBE60499/02.04-DAE-modeling.html#2.4.3-DAE-Formulations-for-Simple-Pendulum-Example)",
"section": "2.4.3 DAE Formulations for Simple Pendulum Example"
}
},
"source": [
"## 2.4.3 DAE Formulations for Simple Pendulum Example\n",
"\n",
"Pyomo.dae documentation:\n",
"* https://pyomo.readthedocs.io/en/latest/modeling_extensions/dae.html\n",
"\n",
"Pendulum example:\n",
"* http://apmonitor.com/wiki/index.php/Apps/PendulumMotion\n",
"* https://www.lehigh.edu/~wes1/apci/11may00.pdf\n",
"\n",
"CasADi (need to integrate DAEs):\n",
"* https://web.casadi.org/get/\n",
"* For local installation: ```pip install casadi```. Warning: installing `CasADi` with `conda` will install an \"okay\" version of Ipopt. If you really want to install `CasADi` with `conda`, you'll likely need to add `import idaes` to your notebook to load the \"good\" version of Ipopt (Linux and Windows users).\n",
"\n",
"DAE simulation example:\n",
"* https://github.com/Pyomo/pyomo/blob/master/examples/dae/simulator_dae_example.py"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"nbpages": {
"level": 2,
"link": "[2.4.3 DAE Formulations for Simple Pendulum Example](https://ndcbe.github.io/CBE60499/02.04-DAE-modeling.html#2.4.3-DAE-Formulations-for-Simple-Pendulum-Example)",
"section": "2.4.3 DAE Formulations for Simple Pendulum Example"
}
},
"outputs": [],
"source": [
"## Load libraries\n",
"from pyomo.environ import *\n",
"from pyomo.dae import *\n",
"from pyomo.dae.simulator import Simulator\n",
"import matplotlib.pyplot as plt\n",
"import numpy as np\n",
"\n",
"## Define function for plotting results\n",
"def plot_results(sim, tsim, profiles):\n",
"\n",
" '''\n",
" time = list(m.t)\n",
" x = [value(m.x[t]) for t in m.t]\n",
" y = [value(m.y[t]) for t in m.t]\n",
"\n",
" plt.plot(time, x, '-b', label='x')\n",
" plt.plot(time, y, '-r', label='y')\n",
" plt.xlabel('Time')\n",
" plt.ylabel('Position')\n",
" plt.legend(loc='best')\n",
" plt.show()\n",
" '''\n",
" \n",
" plt.figure(1)\n",
" varorder = sim.get_variable_order()\n",
" algorder = sim.get_variable_order(vartype='algebraic')\n",
"\n",
" # Create empty dictionary\n",
" results = {}\n",
" \n",
" for idx1, v in enumerate(varorder):\n",
" i = idx1\n",
" v_ = str(v)\n",
" results[v_] = profiles[:, i]\n",
" plt.plot(tsim, results[v_], label=v)\n",
"\n",
" for idx2, v in enumerate(algorder):\n",
" i = len(varorder) + idx2\n",
" v_ = str(v)\n",
" results[v_] = profiles[:, i]\n",
" plt.plot(tsim, results[v_], label=v)\n",
" \n",
" plt.xlabel('t')\n",
" plt.legend(loc='best')\n",
" \n",
" plt.show()\n",
" \n",
" plt.figure(2)\n",
" x_ = results['x[{t}]']\n",
" y_ = results['y[{t}]']\n",
" plt.plot(tsim, np.sqrt(x_**2 + y_**2), '-b', label='length')\n",
" plt.xlabel('t')\n",
" plt.ylabel('$\\sqrt{x^2 + y^2}$')\n",
" plt.show()\n",
" \n",
" #return results"
]
},
{
"cell_type": "markdown",
"metadata": {
"nbpages": {
"level": 3,
"link": "[2.4.3.1 Formulation 1: Index-3 DAE](https://ndcbe.github.io/CBE60499/02.04-DAE-modeling.html#2.4.3.1-Formulation-1:-Index-3-DAE)",
"section": "2.4.3.1 Formulation 1: Index-3 DAE"
}
},
"source": [
"### 2.4.3.1 Formulation 1: Index-3 DAE\n",
"\n",
"Consider the following model:\n",
"\n",
"$$\\begin{align}\n",
"\\frac{d x}{dt} &= u \\\\\n",
"\\frac{d y}{dt} &= v \\\\\n",
"\\frac{d u}{dt} &= -T x \\\\\n",
"\\frac{d v}{dt} &= g - Ty \\\\\n",
"& x^2 + y^2 = 1\n",
"\\end{align}$$\n",
"\n",
"This assumes mass and length of unity."
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {
"nbpages": {
"level": 3,
"link": "[2.4.3.1 Formulation 1: Index-3 DAE](https://ndcbe.github.io/CBE60499/02.04-DAE-modeling.html#2.4.3.1-Formulation-1:-Index-3-DAE)",
"section": "2.4.3.1 Formulation 1: Index-3 DAE"
}
},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"psetup failed: .../casadi/interfaces/sundials/idas_interface.cpp:852: Linear solve failed\n",
"psetup failed: .../casadi/interfaces/sundials/idas_interface.cpp:852: Linear solve failed\n",
"psetup failed: .../casadi/interfaces/sundials/idas_interface.cpp:852: Linear solve failed\n",
"psetup failed: .../casadi/interfaces/sundials/idas_interface.cpp:852: Linear solve failed\n",
"psetup failed: .../casadi/interfaces/sundials/idas_interface.cpp:852: Linear solve failed\n",
"psetup failed: .../casadi/interfaces/sundials/idas_interface.cpp:852: Linear solve failed\n",
"psetup failed: .../casadi/interfaces/sundials/idas_interface.cpp:852: Linear solve failed\n",
"psetup failed: .../casadi/interfaces/sundials/idas_interface.cpp:852: Linear solve failed\n",
"psetup failed: .../casadi/interfaces/sundials/idas_interface.cpp:852: Linear solve failed\n",
"psetup failed: .../casadi/interfaces/sundials/idas_interface.cpp:852: Linear solve failed\n",
"At t = 0 and h = 1.06624e-14, the corrector convergence failed repeatedly or with |h| = hmin.\n"
]
},
{
"ename": "RuntimeError",
"evalue": ".../casadi/interfaces/sundials/idas_interface.cpp:591: IDASolve returned \"IDA_CONV_FAIL\". Consult IDAS documentation.",
"output_type": "error",
"traceback": [
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[0;31mRuntimeError\u001b[0m Traceback (most recent call last)",
"\u001b[0;32m
"
]
}
],
"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
}