Main

----

(:html:)

<iframe width="560" height="315" src="https://www.youtube.com/embed/BHxNe86_TXc" frameborder="0" gesture="media" allow="encrypted-media" allowfullscreen></iframe>

(:htmlend:)

(:toggle hide gekko button show="Show GEKKO (Python) Code":)

(:div id=gekko:)

(:source lang=python:)

from __future__ import division

from gekko import GEKKO

import numpy as np

#Initial conditions

c = np.array([0.03,0.015,0.06,0])

areas = np.array([13.4, 12, 384.5, 4400])

V0 = np.array([0.26, 0.18, 0.68, 22])

h0 = 1000 * V0 / areas

Vout0 = c * np.sqrt(h0)

vin = [0.13,0.13,0.13,0.21,0.21,0.21,0.13,\

0.13,0.13,0.13,0.13,0.13,0.13]

Vin = [0,0,0,0]

#Initialize model

m = GEKKO()

#time array

m.time = np.linspace(0,1,13)

#define constants

c = m.Array(m.Const,4,value=0)

c[0].value = 0.03

c[1].value = c[0] / 2

c[2].value = c[0] * 2

c[3].value = 0

Vuse = [0.03,0.05,0.02,0.00]

#Parameters

evap_c = m.Array(m.Param,4,value=1e-5)

evap_c[-1].value = 0.5e-5

A = [m.Param(value=i) for i in areas]

Vin[0] = m.Param(value=vin)

#Variables

V = [m.Var(value=i) for i in V0]

h = [m.Var(value=i) for i in h0]

Vout = [m.Var(value=i) for i in Vout0]

#Intermediates

Vin[1:4] = [m.Intermediate(Vout[i]) for i in range(3)]

Vevap = [m.Intermediate(evap_c[i] * A[i]) for i in range(4)]

#Equations

m.Equations([V[i].dt() == \

Vin[i] - Vout[i] - Vevap[i] - Vuse[i] \

for i in range(4)])

m.Equations([1000*V[i] == h[i]*A[i] for i in range(4)])

m.Equations([Vout[i]**2 == c[i]**2 * h[i] for i in range(4)])

#Set to simulation mode

m.options.imode = 4

#Solve

m.solve()

#%% Plot results

time = [x * 12 for x in m.time]

# plot results

import matplotlib.pyplot as plt

plt.figure(1)

plt.subplot(311)

plt.plot(time,h[0].value,'r-')

plt.plot(time,h[1].value,'b--')

plt.ylabel('Level (m)')

plt.legend(['Jordanelle Reservoir','Deer Creek Reservoir'])

plt.subplot(312)

plt.plot(time,h[3].value,'g-')

plt.plot(time,h[2].value,'k:')

plt.ylabel('Level (m)')

plt.legend(['Great Salt Lake','Utah Lake'])

plt.subplot(313)

plt.plot(time,Vin[0].value,'k-')

plt.plot(time,Vout[0].value,'r-')

plt.plot(time,Vout[1].value,'b--')

plt.plot(time,Vout[2].value,'g-')

plt.xlabel('Time (month)')

plt.ylabel('Flow (km3/yr)')

plt.legend(['Supply Flow','Upper Provo River', \

'Lower Provo River','Jordan River'])

plt.show()

(:sourceend:)

(:divend:)

'''Objective:''' Formulate a dynamic model with model quantities such as constants, parameters, and variables and model expressions such as intermediates and equations. Use time-varying inputs, initial conditions, and mass balance equations to specify the problem inputs and dynamics. Create a MATLAB or Python script to simulate and display the results.

Area of Reservoir / Lake (km'^2^')

A'_1_' = 13.4

A'_2_' = 12.0

A'_3_' = 384.5

A'_4_' = 4400

Attach:utah_reservoir_graphic.png
~~ Evaporation Rates (km'^3^'/yr)~~

V'_evap1_' = 0.01 A'_1_'

V'_evap2_' = 0.01 A'_2_'

V'_evap3_' = 0.01 A'_3_'

V'_evap4_' = 0.005 A'_4_'

Attach:utah_reservoirs.png

Attach:utah_reservoirs.png

!!!!~~Solution~~

(:html:)

<iframe width="560" height="315" src="https://www.youtube.com/embed/ELEUFKzwUj0" frameborder="0" allowfullscreen></iframe>

(:htmlend:)

* [[http://apmonitor.com/online/view_pass.php?f=slack.apm | Click to Solve a Slack Variable Optimization Problem]]

(:html:)

<iframe width="560" height="315" src="https://www.youtube.com/embed/jh6BK0BqqIs?rel=0" frameborder="0" allowfullscreen></iframe>

(:html:)
~~(--include link to additional symbols--)~~

MPECs - max, min, abs, if ~~statements~~, ~~etc~~

~~Collections of the above elements constitute a model and are~~ stored in a text file with extension ''apm''. Several text editors are available that support syntax highlighting such as Notepad++ and gEdit (see installation instructions).

!!!!Model Complexity

Model complexity can range from detailed finite element analysis to simple reduced order models. An important aspect of modeling is the overall goal of capturing the input to output relationships for a particular target application. In the case of real-time embedded systems, the complexity of the model may need to be limited to meet simulation or optimization speed requirements. Other times there is no computational time target for a solution and more sophisticated models can be solved. In each case, the correct level of sophistication should be carefully considered. One strategy for finding the appropriate level of complexity is to start with simple models and add complexity only as needed.
~~ y = x[5]~~

!!!!Exercise

!!!!Solution
~~Collections of the above elements constitute a model and are stored in a text file with extension ''apm''. Several text editors are available that support syntax highlighting such as Notepad++ and gEdit (see installation instructions). ~~

Discretization

Divide by zero

Scaling

Slack variables

One of the most important factors in efficient and reliable solution of dynamic systems is the model formulation. Changes in model formulation are not intended to change the equations, only to put them into a form that allows solvers to more easily find an accurate solution. In each section below are some of the key strategies related to model creation and formulation. The discussion begins with a basic introduction to

Models consist of sections including constants, parameters, variables, intermediates, and equations. All expressions used in the equations section must be created in one of the prior sections. The initialization of individual parameters or variables is sequential in the order that they are listed in the model file. Equations, however, can be listed in any order because equations are solved simultaneously.

* '''Constants''' are values that never change. Integer values may be defined to give sizes to arrays.

* '''Parameters''' are values that are nominally fixed at initial values but can be changed with input data, by the user, or can become calculated by the optimizer to minimize an objective function if they are indicated as degrees of freedom.

* '''Variables''' are always calculated values as determined by the set of equations. Some variables are either measured and/or controlled to a desired target value.

* '''Intermediates''' are explicit equations where the variable is set equal to an expression that may include constants, parameters, variables, or other intermediate values that are defined previously. Intermediates are not implicit equations but are explicitly calculated with each model function evaluation.

* '''Equations''' are either equality constraints as ''f(dx/dt,x,p)=0'', inequality constraints as ''g(dx/dt,x,p)>0'', or expression of the objective with statements that begin with keywords ''maximize'' or ''minimize''.

* '''Objects''' are object-oriented extensions of APMonitor that may include parameters, variables, equations, and objective terms.

* '''Connections''' are simple equality constraints that relate object variables to model parameter or variables.

Collections of the above elements constitute a model and are stored in a text file with extension ''apm''. Several text editors are available that support syntax highlighting such as Notepad++ and gEdit (see installation instructions).

Discretization

Divide by zero

Scaling

Slack variables

MPECs - max, min, abs, if statements, etc

## Formulation Strategies

## Main.ModelFormulation History

Hide minor edits - Show changes to output

Changed line 95 from:

V'_flow_in1_' = 0.13 (~~July~~-~~Mar~~), 0.21 (~~Apr~~-~~June~~)

to:

V'_flow_in1_' = 0.13 (June-Feb), 0.21 (Mar-May)

Changed line 146 from:

vin = [0.13,0.13,0.13,0.~~13~~,0.21,0.21,0.~~21~~,\

to:

vin = [0.13,0.13,0.13,0.21,0.21,0.21,0.13,\

Changed line 146 from:

vin = [0.13,0.13,0.13,0.~~21~~,0.21,0.21,0.~~13~~,\

to:

vin = [0.13,0.13,0.13,0.13,0.21,0.21,0.21,\

Changed line 84 from:

Outflow River Rates (km'^3^'/yr)

to:

Outflow River Rates (km'^3^'/yr) with height in meters

Added lines 126-131:

----

(:html:)

<iframe width="560" height="315" src="https://www.youtube.com/embed/BHxNe86_TXc" frameborder="0" gesture="media" allow="encrypted-media" allowfullscreen></iframe>

(:htmlend:)

Added lines 126-218:

(:toggle hide gekko button show="Show GEKKO (Python) Code":)

(:div id=gekko:)

(:source lang=python:)

from __future__ import division

from gekko import GEKKO

import numpy as np

#Initial conditions

c = np.array([0.03,0.015,0.06,0])

areas = np.array([13.4, 12, 384.5, 4400])

V0 = np.array([0.26, 0.18, 0.68, 22])

h0 = 1000 * V0 / areas

Vout0 = c * np.sqrt(h0)

vin = [0.13,0.13,0.13,0.21,0.21,0.21,0.13,\

0.13,0.13,0.13,0.13,0.13,0.13]

Vin = [0,0,0,0]

#Initialize model

m = GEKKO()

#time array

m.time = np.linspace(0,1,13)

#define constants

c = m.Array(m.Const,4,value=0)

c[0].value = 0.03

c[1].value = c[0] / 2

c[2].value = c[0] * 2

c[3].value = 0

Vuse = [0.03,0.05,0.02,0.00]

#Parameters

evap_c = m.Array(m.Param,4,value=1e-5)

evap_c[-1].value = 0.5e-5

A = [m.Param(value=i) for i in areas]

Vin[0] = m.Param(value=vin)

#Variables

V = [m.Var(value=i) for i in V0]

h = [m.Var(value=i) for i in h0]

Vout = [m.Var(value=i) for i in Vout0]

#Intermediates

Vin[1:4] = [m.Intermediate(Vout[i]) for i in range(3)]

Vevap = [m.Intermediate(evap_c[i] * A[i]) for i in range(4)]

#Equations

m.Equations([V[i].dt() == \

Vin[i] - Vout[i] - Vevap[i] - Vuse[i] \

for i in range(4)])

m.Equations([1000*V[i] == h[i]*A[i] for i in range(4)])

m.Equations([Vout[i]**2 == c[i]**2 * h[i] for i in range(4)])

#Set to simulation mode

m.options.imode = 4

#Solve

m.solve()

#%% Plot results

time = [x * 12 for x in m.time]

# plot results

import matplotlib.pyplot as plt

plt.figure(1)

plt.subplot(311)

plt.plot(time,h[0].value,'r-')

plt.plot(time,h[1].value,'b--')

plt.ylabel('Level (m)')

plt.legend(['Jordanelle Reservoir','Deer Creek Reservoir'])

plt.subplot(312)

plt.plot(time,h[3].value,'g-')

plt.plot(time,h[2].value,'k:')

plt.ylabel('Level (m)')

plt.legend(['Great Salt Lake','Utah Lake'])

plt.subplot(313)

plt.plot(time,Vin[0].value,'k-')

plt.plot(time,Vout[0].value,'r-')

plt.plot(time,Vout[1].value,'b--')

plt.plot(time,Vout[2].value,'g-')

plt.xlabel('Time (month)')

plt.ylabel('Flow (km3/yr)')

plt.legend(['Supply Flow','Upper Provo River', \

'Lower Provo River','Jordan River'])

plt.show()

(:sourceend:)

(:divend:)

Changed lines 101-105 from:

V'_~~evap1~~_' = 0.03

V'_~~evap2~~_' = 0.05

V'_~~evap3~~_' = 0.02

V'_~~evap4~~_' = 0.00

V'_

V'_

V'_

to:

V'_use1_' = 0.03

V'_use2_' = 0.05

V'_use3_' = 0.02

V'_use4_' = 0.00

V'_use2_' = 0.05

V'_use3_' = 0.02

V'_use4_' = 0.00

Changed line 118 from:

Simulate the height of the reservoirs over the course of a year, starting in January, with higher inlet flowrates in the spring due to melting snow. Use a [[https://youtu.be/6Cc693cmqCc|mass balance]] to describe the change in volume and height of each body of water. This is a simple simulation model that assumes no active control. In actual practice, water outlet flows are actively managed to maintain reservoir levels for recreation, provide sufficient river flow rates, limit river flow rates to avoid flooding, and serve agricultural and community needs. Utah Lake and the Great Salt Lake also have additional inlet sources such as the Payson River (Utah Lake) and the Weber River and Bear River (Great Salt Lake) that are not considered in this simulation.

to:

Simulate the height of the reservoirs (in meters) over the course of a year, starting in January, with higher inlet flowrates in the spring due to melting snow. Use a [[https://youtu.be/6Cc693cmqCc|mass balance]] to describe the change in volume and height of each body of water. This is a simple simulation model that assumes no active control. In actual practice, water outlet flows are actively managed to maintain reservoir levels for recreation, provide sufficient river flow rates, limit river flow rates to avoid flooding, and serve agricultural and community needs. Utah Lake and the Great Salt Lake also have additional inlet sources such as the Payson River (Utah Lake) and the Weber River and Bear River (Great Salt Lake) that are not considered in this simulation.

Changed line 118 from:

Simulate the height of the reservoirs over the course of a year, starting in January, with higher inlet flowrates in the spring due to melting snow. This is a simple simulation model that assumes no active control. In actual practice, water outlet flows are actively managed to maintain reservoir levels for recreation, provide sufficient river flow rates, limit river flow rates to avoid flooding, and serve agricultural and community needs. Utah Lake and the Great Salt Lake also have additional inlet sources such as the Payson River (Utah Lake) and the Weber River and Bear River (Great Salt Lake) that are not considered in this simulation.

to:

Simulate the height of the reservoirs over the course of a year, starting in January, with higher inlet flowrates in the spring due to melting snow. Use a [[https://youtu.be/6Cc693cmqCc|mass balance]] to describe the change in volume and height of each body of water. This is a simple simulation model that assumes no active control. In actual practice, water outlet flows are actively managed to maintain reservoir levels for recreation, provide sufficient river flow rates, limit river flow rates to avoid flooding, and serve agricultural and community needs. Utah Lake and the Great Salt Lake also have additional inlet sources such as the Payson River (Utah Lake) and the Weber River and Bear River (Great Salt Lake) that are not considered in this simulation.

Changed lines 125-126 from:

<iframe width="560" height="315" src="https://www.youtube.com/embed/~~WCTTY4baYLk~~?rel=0" frameborder="0" allowfullscreen></iframe>

(:htmlend:)

(:htmlend:)

to:

<iframe width="560" height="315" src="https://www.youtube.com/embed/ZzYURrtD4YI?rel=0" frameborder="0" allowfullscreen></iframe>(:htmlend:)

Changed line 71 from:

'''Objective:''' Formulate a dynamic model with model quantities such as constants, parameters, and variables and model expressions such as intermediates and equations. Use time-varying inputs, initial conditions, and mass balance equations to specify the problem inputs and dynamics. Create a MATLAB or Python script to simulate and display the results.

to:

'''Objective:''' Formulate a dynamic model with model quantities such as constants, parameters, and variables and model expressions such as intermediates and equations. Use time-varying inputs, initial conditions, and mass balance equations to specify the problem inputs and dynamics. Create a MATLAB or Python script to simulate and display the results. ''Estimated Time: 2 hours''

Added lines 70-71:

'''Objective:''' Formulate a dynamic model with model quantities such as constants, parameters, and variables and model expressions such as intermediates and equations. Use time-varying inputs, initial conditions, and mass balance equations to specify the problem inputs and dynamics. Create a MATLAB or Python script to simulate and display the results.

Changed line 116 from:

Simulate the height of the reservoirs over the course of a year, starting in January, with higher inlet flowrates in the spring due to melting snow. This is a simple simulation model that assumes no active control. In actual practice, water outlet flows are actively managed to maintain reservoir levels for recreation, provide sufficient river flow rates, limit river flow rates to avoid flooding, and serve agricultural and community needs.

to:

Simulate the height of the reservoirs over the course of a year, starting in January, with higher inlet flowrates in the spring due to melting snow. This is a simple simulation model that assumes no active control. In actual practice, water outlet flows are actively managed to maintain reservoir levels for recreation, provide sufficient river flow rates, limit river flow rates to avoid flooding, and serve agricultural and community needs. Utah Lake and the Great Salt Lake also have additional inlet sources such as the Payson River (Utah Lake) and the Weber River and Bear River (Great Salt Lake) that are not considered in this simulation.

Deleted lines 102-107:

Area of Reservoir / Lake (km'^2^')

A'_1_' = 13.4

A'_2_' = 12.0

A'_3_' = 384.5

A'_4_' = 4400

Changed line 80 from:

Suppose that there is a spillway from each upstream body of water to the lower body of water with a flow that is proportional to the square root of the reservoir height. There is no outflow from the Great Salt Lake except due to evaporation. Develop a simplified dynamic model of the height change in each reservoir from water mass balances. Below are ~~relevant ~~constants ~~to use for outflow correlations ~~and ~~initial holdups. Below are constants such as area ~~(km'^~~2~~^') and ~~usage requirements ~~(km'^3^'/yr), ~~inlet~~ and ~~outlet flow correlations ~~(km'^3^'~~/yr~~)~~, and initial conditions for the volumes (km'^3^')~~.~~ ~~

to:

Suppose that there is a spillway from each upstream body of water to the lower body of water with a flow that is proportional to the square root of the reservoir height. There is no outflow from the Great Salt Lake except due to evaporation. Develop a simplified dynamic model of the height change in each reservoir from water mass balances. Below are constants such as area (km'^2^') and usage requirements (km'^3^'/yr), inlet and outlet flow correlations (km'^3^'/yr), evaporation correlations, and initial conditions for the volumes (km'^3^').

Added lines 73-76:

(:table border=0 width=100%:)

(:cell width=20%:)

Attach:utah_reservoir_graphic.png

(:cell width=80%:)

(:cell width=20%:)

Attach:utah_reservoir_graphic.png

(:cell width=80%:)

Changed lines 78-79 from:

to:

(:tableend:)

Deleted lines 120-121:

Attach:utah_reservoir_graphic.png

Added lines 123-127:

Attach:download.png [[Attach:simulate_reservoirs.zip|Reservoir Simulation in MATLAB and Python]]

(:html:)

<iframe width="560" height="315" src="https://www.youtube.com/embed/WCTTY4baYLk?rel=0" frameborder="0" allowfullscreen></iframe>

(:htmlend:)

(:html:)

<iframe width="560" height="315" src="https://www.youtube.com/embed/WCTTY4baYLk?rel=0" frameborder="0" allowfullscreen></iframe>

(:htmlend:)

Changed lines 84-85 from:

V'_evap' = ~~1e~~-5 * Area, for ~~fresh~~ water

~~ V'_evap' = 0.5e-5 * Area, for salt water~~

to:

V'_evap_' = 0.5e-5 * Area, for salt water (Great Salt Lake)

V'_evap_' = 1e-5 * Area, for fresh water (all others)

V'_evap_' = 1e-5 * Area, for fresh water (all others)

Changed lines 71-76 from:

In Utah, water flows into the (1) [[http://en.wikipedia.org/wiki/Jordanelle_Reservoir|Jordanelle reservoir]], to the (2) [[http://en.wikipedia.org/wiki/Deer_Creek_Dam_and_Reservoir|Deer Creek reservoir]], to (3) [[http://en.wikipedia.org/wiki/Utah_Lake|Utah Lake]], and finally to the (4) [[http://en.wikipedia.org/wiki/Great_Salt_Lake|Great Salt Lake]]. ~~Suppose ~~that there is a spillway from each upstream body of water to the lower body of water with a flow that is proportional to the square root of the reservoir height. There is no outflow from the Great Salt Lake except due to evaporation. Develop a simplified dynamic model of the height change in each reservoir from water mass balances. Below are relevant constants to use for outflow correlations and initial holdups. Below are constants such as area (km'^2^') and usage requirements (km'^3^'/yr), inlet and outlet flow correlations (km'^3^'/yr), and initial conditions for the volumes (km'^3^').

Outflow Rates (km'^3^'/yr)

V'_flow_out1_' = 0.~~1~~ sqrt(h'_1_')

V'_flow_out2_' = 0.~~05~~ sqrt(h'_2_')

V'_flow_out3_' = 0.~~2~~ sqrt(h'_3_')

Outflow Rates (km'^3^'/yr)

V'_flow_out1_' = 0.

V'_flow_out2_' = 0.

V'_flow_out3_' = 0.

to:

In Utah, water flows into the (1) [[http://en.wikipedia.org/wiki/Jordanelle_Reservoir|Jordanelle reservoir]], to the (2) [[http://en.wikipedia.org/wiki/Deer_Creek_Dam_and_Reservoir|Deer Creek reservoir]], to (3) [[http://en.wikipedia.org/wiki/Utah_Lake|Utah Lake]], and finally to the (4) [[http://en.wikipedia.org/wiki/Great_Salt_Lake|Great Salt Lake]].

Attach:utah_reservoirs.png

Suppose that there is a spillway from each upstream body of water to the lower body of water with a flow that is proportional to the square root of the reservoir height. There is no outflow from the Great Salt Lake except due to evaporation. Develop a simplified dynamic model of the height change in each reservoir from water mass balances. Below are relevant constants to use for outflow correlations and initial holdups. Below are constants such as area (km'^2^') and usage requirements (km'^3^'/yr), inlet and outlet flow correlations (km'^3^'/yr), and initial conditions for the volumes (km'^3^').

Outflow River Rates (km'^3^'/yr)

V'_flow_out1_' = 0.030 sqrt(h'_1_')

V'_flow_out2_' = 0.015 sqrt(h'_2_')

V'_flow_out3_' = 0.060 sqrt(h'_3_')

Attach:utah_reservoirs.png

Suppose that there is a spillway from each upstream body of water to the lower body of water with a flow that is proportional to the square root of the reservoir height. There is no outflow from the Great Salt Lake except due to evaporation. Develop a simplified dynamic model of the height change in each reservoir from water mass balances. Below are relevant constants to use for outflow correlations and initial holdups. Below are constants such as area (km'^2^') and usage requirements (km'^3^'/yr), inlet and outlet flow correlations (km'^3^'/yr), and initial conditions for the volumes (km'^3^').

Outflow River Rates (km'^3^'/yr)

V'_flow_out1_' = 0.030 sqrt(h'_1_')

V'_flow_out2_' = 0.015 sqrt(h'_2_')

V'_flow_out3_' = 0.060 sqrt(h'_3_')

Added lines 83-86:

Evaporation Rates (km'^3^'/yr)

V'_evap' = 1e-5 * Area, for fresh water

V'_evap' = 0.5e-5 * Area, for salt water

V'_evap' = 1e-5 * Area, for fresh water

V'_evap' = 0.5e-5 * Area, for salt water

Deleted lines 92-97:

V'_evap1_' = 0.01 A'_1_'

V'_evap2_' = 0.01 A'_2_'

V'_evap3_' = 0.01 A'_3_'

V'_evap4_' = 0.005 A'_4_'

Changed lines 94-98 from:

V'_evap1_' = ~~20~~

V'_evap2_' = ~~100~~

V'_evap3_' = ~~30~~

V'_evap4_' = 0

to:

V'_evap1_' = 0.03

V'_evap2_' = 0.05

V'_evap3_' = 0.02

V'_evap4_' = 0.00

Area of Reservoir / Lake (km'^2^')

A'_1_' = 13.4

A'_2_' = 12.0

A'_3_' = 384.5

A'_4_' = 4400

Area of Reservoir / Lake (km'^2^')

A'_1_' = 13.4

A'_2_' = 12.0

A'_3_' = 384.5

A'_4_' = 4400

Initial Volume of Reservoir / Lake (km'^3^')

V'_1_' = 0.26

V'_2_' = 0.18

V'_3_' = 0.68

V'_4_' = 22.0

V'_evap2_' = 0.05

V'_evap3_' = 0.02

V'_evap4_' = 0.00

Area of Reservoir / Lake (km'^2^')

A'_1_' = 13.4

A'_2_' = 12.0

A'_3_' = 384.5

A'_4_' = 4400

Area of Reservoir / Lake (km'^2^')

A'_1_' = 13.4

A'_2_' = 12.0

A'_3_' = 384.5

A'_4_' = 4400

Initial Volume of Reservoir / Lake (km'^3^')

V'_1_' = 0.26

V'_2_' = 0.18

V'_3_' = 0.68

V'_4_' = 22.0

Deleted lines 119-120:

Attach:utah_reservoirs.png

Changed lines 71-77 from:

In Utah, water flows into the (1) [[http://en.wikipedia.org/wiki/Jordanelle_Reservoir|Jordanelle reservoir]], to the (2) [[http://en.wikipedia.org/wiki/Deer_Creek_Dam_and_Reservoir|Deer Creek reservoir]], to (3) [[http://en.wikipedia.org/wiki/Utah_Lake|Utah Lake]], and finally to the (4) [[http://en.wikipedia.org/wiki/Great_Salt_Lake|Great Salt Lake]]. Suppose that there is a spillway from each upstream body of water to the lower body of water with a flow that is proportional to the square root of the reservoir height. There is no outflow from the Great Salt Lake except due to evaporation. Develop a simplified dynamic model of the height change in each reservoir from water mass balances. Below are relevant constants to use for outflow correlations and initial holdups. ~~Lookup estimates for the height ~~(km)~~, area ~~(km'^~~2~~^'), and ~~initial volumes ~~(km'^3^')~~. Simulate the height of~~ the ~~reservoirs over the course of a year with higher inlet flowrates in the spring due to melting snow.~~

V'~~_flow_in1_~~'~~ = variable ~~

V'_flow_in2_' = V'_flow_out1_'

V'_flow_in3_' = V'_flow_out2_'

V'_flow_in4_' = V'_flow_out3_'

V

V'_flow_in2_' = V'_flow_out1_'

V'_flow_in3_' = V'_flow_out2_'

V'_flow_in4_' = V'_flow_out3_'

to:

In Utah, water flows into the (1) [[http://en.wikipedia.org/wiki/Jordanelle_Reservoir|Jordanelle reservoir]], to the (2) [[http://en.wikipedia.org/wiki/Deer_Creek_Dam_and_Reservoir|Deer Creek reservoir]], to (3) [[http://en.wikipedia.org/wiki/Utah_Lake|Utah Lake]], and finally to the (4) [[http://en.wikipedia.org/wiki/Great_Salt_Lake|Great Salt Lake]]. Suppose that there is a spillway from each upstream body of water to the lower body of water with a flow that is proportional to the square root of the reservoir height. There is no outflow from the Great Salt Lake except due to evaporation. Develop a simplified dynamic model of the height change in each reservoir from water mass balances. Below are relevant constants to use for outflow correlations and initial holdups. Below are constants such as area (km'^2^') and usage requirements (km'^3^'/yr), inlet and outlet flow correlations (km'^3^'/yr), and initial conditions for the volumes (km'^3^').

Outflow Rates (km'^3^'/yr)

Outflow Rates (km'^3^'/yr)

Added lines 79-85:

Inflow Rates (km'^3^'/yr)

V'_flow_in1_' = 0.13 (July-Mar), 0.21 (Apr-June)

V'_flow_in2_' = V'_flow_out1_'

V'_flow_in3_' = V'_flow_out2_'

V'_flow_in4_' = V'_flow_out3_'

Evaporation Rates (km'^3^'/yr)

V'_flow_in1_' = 0.13 (July-Mar), 0.21 (Apr-June)

V'_flow_in2_' = V'_flow_out1_'

V'_flow_in3_' = V'_flow_out2_'

V'_flow_in4_' = V'_flow_out3_'

Evaporation Rates (km'^3^'/yr)

Added lines 91-96:

Usage Requirements (km'^3^'/yr)

V'_evap1_' = 20

V'_evap2_' = 100

V'_evap3_' = 30

V'_evap4_' = 0

V'_evap1_' = 20

V'_evap2_' = 100

V'_evap3_' = 30

V'_evap4_' = 0

Changed line 99 from:

This is a simple simulation model that assumes no active control. In actual practice, water outlet flows are actively managed to maintain reservoir levels for recreation, provide sufficient river flow rates, limit river flow rates to avoid flooding, and serve agricultural and community needs.

to:

Simulate the height of the reservoirs over the course of a year, starting in January, with higher inlet flowrates in the spring due to melting snow. This is a simple simulation model that assumes no active control. In actual practice, water outlet flows are actively managed to maintain reservoir levels for recreation, provide sufficient river flow rates, limit river flow rates to avoid flooding, and serve agricultural and community needs.

Changed line 71 from:

In Utah, water flows into the (1) [[http://en.wikipedia.org/wiki/Jordanelle_Reservoir|Jordanelle reservoir]], to the (2) [[http://en.wikipedia.org/wiki/Deer_Creek_Dam_and_Reservoir|Deer Creek reservoir]], to (3) [[http://en.wikipedia.org/wiki/Utah_Lake|Utah Lake]], and finally to the (4) [[http://en.wikipedia.org/wiki/Great_Salt_Lake|Great Salt Lake]]. Suppose that there is a spillway from each upstream body of water to the lower body of water with a flow that is proportional to the square root of the reservoir height. There is no outflow from the Great Salt Lake except due to evaporation. Develop a simplified dynamic model of the height change in each reservoir from water mass balances. Below are relevant constants to use for outflow correlations and initial holdups. Lookup estimates for the height (~~m~~), area (~~m~~'^2^'), and initial volumes (~~m~~'^3^'). Simulate the height of the reservoirs over the course of a year with higher inlet flowrates in the spring due to melting snow.

to:

In Utah, water flows into the (1) [[http://en.wikipedia.org/wiki/Jordanelle_Reservoir|Jordanelle reservoir]], to the (2) [[http://en.wikipedia.org/wiki/Deer_Creek_Dam_and_Reservoir|Deer Creek reservoir]], to (3) [[http://en.wikipedia.org/wiki/Utah_Lake|Utah Lake]], and finally to the (4) [[http://en.wikipedia.org/wiki/Great_Salt_Lake|Great Salt Lake]]. Suppose that there is a spillway from each upstream body of water to the lower body of water with a flow that is proportional to the square root of the reservoir height. There is no outflow from the Great Salt Lake except due to evaporation. Develop a simplified dynamic model of the height change in each reservoir from water mass balances. Below are relevant constants to use for outflow correlations and initial holdups. Lookup estimates for the height (km), area (km'^2^'), and initial volumes (km'^3^'). Simulate the height of the reservoirs over the course of a year with higher inlet flowrates in the spring due to melting snow.

Added lines 88-89:

Attach:utah_reservoir_graphic.png

Added lines 91-92:

Attach:utah_reservoirs.png

Changed line 84 from:

V'_~~evap3~~_' = 0.005 A'_4_'

to:

V'_evap4_' = 0.005 A'_4_'

Changed lines 74-84 from:

V'_flow_in2_' = ~~m~~'_out1_'

V'flow_in3' =~~m~~'_out2_'

V'flow_in4' =~~m~~'_out3_'

V'~~^flow~~_out1~~^~~' = 0.1 sqrt(h'_1_')

V'~~^flow~~_out2~~^~~' = 0.05 sqrt(h'_2_')

V'~~^flow~~_out3~~^~~' = 0.2 sqrt(h'_3_')

V'~~^flow~~_out4~~^~~' = 0

V'~~^~~evap1~~^~~' = 0.01 A'_1_'

V'~~^~~evap2~~^~~' = 0.01 A'_2_'

V'~~^~~evap3~~^~~' = 0.01 A'_3_'

V'~~^~~evap3~~^~~' = 0.005 A'_4_'

V'flow_in3' =

V'flow_in4' =

V'

V'

V'

V'

V'

V'

V'

V'

to:

V'_flow_in2_' = V'_flow_out1_'

V'_flow_in3_' = V'_flow_out2_'

V'_flow_in4_' = V'_flow_out3_'

V'_flow_out1_' = 0.1 sqrt(h'_1_')

V'_flow_out2_' = 0.05 sqrt(h'_2_')

V'_flow_out3_' = 0.2 sqrt(h'_3_')

V'_flow_out4_' = 0

V'_evap1_' = 0.01 A'_1_'

V'_evap2_' = 0.01 A'_2_'

V'_evap3_' = 0.01 A'_3_'

V'_evap3_' = 0.005 A'_4_'

V'_flow_in3_' = V'_flow_out2_'

V'_flow_in4_' = V'_flow_out3_'

V'_flow_out1_' = 0.1 sqrt(h'_1_')

V'_flow_out2_' = 0.05 sqrt(h'_2_')

V'_flow_out3_' = 0.2 sqrt(h'_3_')

V'_flow_out4_' = 0

V'_evap1_' = 0.01 A'_1_'

V'_evap2_' = 0.01 A'_2_'

V'_evap3_' = 0.01 A'_3_'

V'_evap3_' = 0.005 A'_4_'

Changed lines 73-76 from:

V'flow_in1' = variable

V'flow_in2' = m'_out1'

V'flow_in3' = m'_out2'

V'flow_in4' = m'_out3'

V'flow_in2' = m'_out1'

V'flow_in3' = m'_out2'

V'flow_in4' = m'_out3'

to:

V'_flow_in1_' = variable

V'_flow_in2_' = m'_out1_'

V'flow_in3' = m'_out2_'

V'flow_in4' = m'_out3_'

V'_flow_in2_' = m'_out1_'

V'flow_in3' = m'_out2_'

V'flow_in4' = m'_out3_'

Changed lines 71-80 from:

In Utah, water flows into the (1) [[http://en.wikipedia.org/wiki/Jordanelle_Reservoir|Jordanelle reservoir]], to the (2) [[http://en.wikipedia.org/wiki/Deer_Creek_Dam_and_Reservoir|Deer Creek reservoir]], to (3) [[http://en.wikipedia.org/wiki/Utah_Lake|Utah Lake]], and finally to the (4) [[http://en.wikipedia.org/wiki/Great_Salt_Lake|Great Salt Lake]]. Suppose that there is a spillway from each upstream body of water to the lower body of water with a flow that is proportional to the square root of the reservoir height. There is no outflow from the Great Salt Lake except due to evaporation. Develop a simplified dynamic model of the height change in each reservoir from water mass balances. Below are relevant constants to use for outflow correlations and initial holdups. Lookup estimates for the height (m) ~~and ~~area (m^~~_~~2~~_~~^)~~. ~~

m^~~_out1~~^ ~~= 0.1 h^_1_^ ~~

m^_out2^ = 0.05 h^_2_^

m^_out3^ = 0.2 h^_3_^

m^_out4^ = 0

m^_evap1^ = 0.01 A^_1_^

m^_evap2^ = 0.~~01 A^~~_~~2_^~~

m^_evap3^= ~~0.01 A^~~_~~3_^~~

m~~^~~_~~evap3^ ~~= ~~0.005 A^~~_~~4~~_^

m^_out2^ = 0.05 h^_2_^

m^_out3^ = 0.2 h^_3_^

m^_out4^ = 0

m^_evap1^ = 0.01 A^_1_^

m^_evap2^ = 0

m^_evap3^

to:

In Utah, water flows into the (1) [[http://en.wikipedia.org/wiki/Jordanelle_Reservoir|Jordanelle reservoir]], to the (2) [[http://en.wikipedia.org/wiki/Deer_Creek_Dam_and_Reservoir|Deer Creek reservoir]], to (3) [[http://en.wikipedia.org/wiki/Utah_Lake|Utah Lake]], and finally to the (4) [[http://en.wikipedia.org/wiki/Great_Salt_Lake|Great Salt Lake]]. Suppose that there is a spillway from each upstream body of water to the lower body of water with a flow that is proportional to the square root of the reservoir height. There is no outflow from the Great Salt Lake except due to evaporation. Develop a simplified dynamic model of the height change in each reservoir from water mass balances. Below are relevant constants to use for outflow correlations and initial holdups. Lookup estimates for the height (m), area (m'^2^'), and initial volumes (m'^3^'). Simulate the height of the reservoirs over the course of a year with higher inlet flowrates in the spring due to melting snow.

V'flow_in1' = variable

V'flow_in2' = m'_out1'

V'flow_in3' = m'_out2'

V'flow_in4' = m'_out3'

V'^flow_out1^' = 0.1 sqrt(h'_1_')

V'^flow_out2^' = 0.05 sqrt(h'_2_')

V'^flow_out3^' = 0.2 sqrt(h'_3_')

V'^flow_out4^' = 0

V'^evap1^' = 0.01 A'_1_'

V'^evap2^' = 0.01 A'_2_'

V'^evap3^' = 0.01 A'_3_'

V'^evap3^' = 0.005 A'_4_'

V'flow_in1' = variable

V'flow_in2' = m'_out1'

V'flow_in3' = m'_out2'

V'flow_in4' = m'_out3'

V'^flow_out1^' = 0.1 sqrt(h'_1_')

V'^flow_out2^' = 0.05 sqrt(h'_2_')

V'^flow_out3^' = 0.2 sqrt(h'_3_')

V'^flow_out4^' = 0

V'^evap1^' = 0.01 A'_1_'

V'^evap2^' = 0.01 A'_2_'

V'^evap3^' = 0.01 A'_3_'

V'^evap3^' = 0.005 A'_4_'

Changed lines 71-73 from:

to:

In Utah, water flows into the (1) [[http://en.wikipedia.org/wiki/Jordanelle_Reservoir|Jordanelle reservoir]], to the (2) [[http://en.wikipedia.org/wiki/Deer_Creek_Dam_and_Reservoir|Deer Creek reservoir]], to (3) [[http://en.wikipedia.org/wiki/Utah_Lake|Utah Lake]], and finally to the (4) [[http://en.wikipedia.org/wiki/Great_Salt_Lake|Great Salt Lake]]. Suppose that there is a spillway from each upstream body of water to the lower body of water with a flow that is proportional to the square root of the reservoir height. There is no outflow from the Great Salt Lake except due to evaporation. Develop a simplified dynamic model of the height change in each reservoir from water mass balances. Below are relevant constants to use for outflow correlations and initial holdups. Lookup estimates for the height (m) and area (m^_2_^).

m^_out1^ = 0.1 h^_1_^

m^_out2^ = 0.05 h^_2_^

m^_out3^ = 0.2 h^_3_^

m^_out4^ = 0

m^_evap1^ = 0.01 A^_1_^

m^_evap2^ = 0.01 A^_2_^

m^_evap3^ = 0.01 A^_3_^

m^_evap3^ = 0.005 A^_4_^

This is a simple simulation model that assumes no active control. In actual practice, water outlet flows are actively managed to maintain reservoir levels for recreation, provide sufficient river flow rates, limit river flow rates to avoid flooding, and serve agricultural and community needs.

!!!!Solution

m^_out1^ = 0.1 h^_1_^

m^_out2^ = 0.05 h^_2_^

m^_out3^ = 0.2 h^_3_^

m^_out4^ = 0

m^_evap1^ = 0.01 A^_1_^

m^_evap2^ = 0.01 A^_2_^

m^_evap3^ = 0.01 A^_3_^

m^_evap3^ = 0.005 A^_4_^

This is a simple simulation model that assumes no active control. In actual practice, water outlet flows are actively managed to maintain reservoir levels for recreation, provide sufficient river flow rates, limit river flow rates to avoid flooding, and serve agricultural and community needs.

!!!!Solution

Changed line 49 from:

Slack variables are defined to transform an inequality expression into an equality expression with an added slack variable. The general expression for inequality constraints in the DAE expression is ''g(dx/dt,x,p)>0''. An equivalent mathematical expression of the general inequality is ''g(dx/dt,x,p)=s'' and ''s>0''. This form is desirable with solvers such as interior point methods where the initial guess must satisfy all inequality constraints or be on the inside of the feasible region. In the slack variable form, an initial guess value greater than zero for the new slack variable ''s'' satisfies this requirement. The APMonitor Modeling Language automatically transforms all inequality constraints into equivalent equality constraints with added slack variables.

to:

[[http://apmonitor.com/wiki/index.php/Main/SlackVariables|Slack variables]] are defined to transform an inequality expression into an equality expression with an added slack variable. The general expression for inequality constraints in the DAE expression is ''g(dx/dt,x,p)>0''. An equivalent mathematical expression of the general inequality is ''g(dx/dt,x,p)=s'' and ''s>0''. This form is desirable with solvers such as interior point methods where the initial guess must satisfy all inequality constraints or be on the inside of the feasible region. In the slack variable form, an initial guess value greater than zero for the new slack variable ''s'' satisfies this requirement. The APMonitor Modeling Language automatically transforms all inequality constraints into equivalent equality constraints with added slack variables.

Added lines 64-67:

(:html:)

<iframe width="560" height="315" src="https://www.youtube.com/embed/ELEUFKzwUj0" frameborder="0" allowfullscreen></iframe>

(:htmlend:)

Added lines 50-55:

* [[http://apmonitor.com/online/view_pass.php?f=slack.apm | Click to Solve a Slack Variable Optimization Problem]]

(:html:)

<iframe width="560" height="315" src="https://www.youtube.com/embed/jh6BK0BqqIs?rel=0" frameborder="0" allowfullscreen></iframe>

(:html:)

Changed lines 17-19 from:

There is an additional optional designation of parameters as either fixed values (FVs) or manipulated variables (MVs). Variables can be optionally designated as state variables (SVs) or controlled variables (CVs). The terminology of ''FV, MV, SV,'' and ''CV'' is from the process systems engineering community. In this community the ''MVs'' are designated as the inputs that are potentially changed by the controller and ''CVs'' are model outputs that are driven to target conditions. The terms ''FVs'' refer to either measured or unmeasured disturbances to the system and ''SVs'' are simply designated for viewing purposes as variables of importance. These parameter and variable classifications are specified in MATLAB or Python scripts (See [[http://apmonitor.com/wiki/index.php/Main/INFO|additional details on FV, MV, SV, and CV classification]]).

Collections of constants, parameters, variables, intermediates, equations, objects, and connections constitute a model. The model file is created and stored in a text file with extension ''apm''. Several text editors are available that support syntax highlighting such as Notepad++ and gEdit (~~---see installation~~ instructions). Below is an example model that demonstrates the use of many of these sections to create a 5th order differential equation model.

Collections of constants, parameters, variables, intermediates, equations, objects, and connections constitute a model. The model file is created and stored in a text file with extension ''apm''. Several text editors are available that support syntax highlighting such as Notepad++ and gEdit (

to:

There is an additional optional designation of parameters as either fixed values (''FVs'') or manipulated variables (''MVs''). Variables can be optionally designated as state variables (SVs) or controlled variables (''CVs''). The terminology of ''FV, MV, SV,'' and ''CV'' is from the process systems engineering community. In this community the ''MVs'' are designated as the inputs that are potentially changed by the controller and ''CVs'' are model outputs that are driven to target conditions. The terms ''FVs'' refer to either measured or unmeasured disturbances to the system and ''SVs'' are simply designated for viewing purposes as variables of importance. These parameter and variable classifications are specified in MATLAB or Python scripts (See [[http://apmonitor.com/wiki/index.php/Main/INFO|additional details on FV, MV, SV, and CV classification]]).

Collections of constants, parameters, variables, intermediates, equations, objects, and connections constitute a model. The model file is created and stored in a text file with extension ''apm''. Several text editors are available that support syntax highlighting such as Notepad++ and gEdit ([[http://apmonitor.com/wiki/index.php/Main/SyntaxHighlighting | see installation instructions]]). Below is an example model that demonstrates the use of many of these sections to create a 5th order differential equation model.

Collections of constants, parameters, variables, intermediates, equations, objects, and connections constitute a model. The model file is created and stored in a text file with extension ''apm''. Several text editors are available that support syntax highlighting such as Notepad++ and gEdit ([[http://apmonitor.com/wiki/index.php/Main/SyntaxHighlighting | see installation instructions]]). Below is an example model that demonstrates the use of many of these sections to create a 5th order differential equation model.

Changed line 53 from:

Certain functions such as ''abs'', ''if..then'', ''min'', ''max'', ''signum'', and discontinuous functions can be included in models but need to be posed in a way to allow efficient solution by solvers that perform better with continuous first and second derivatives. There are alternative methods to reformulate the problems. Two popular approaches are as [[http://apmonitor.com/wiki/index.php/Apps/MpecExamples|MPECs (Mathematical Programs with Equilibrium Constraints)~~]]~~ or with [[http://apmonitor.com/me575/index.php/Main/LogicalConditions|binary variables that switch on or off certain elements of the equations~~]]~~.

to:

Certain functions such as ''abs'', ''if..then'', ''min'', ''max'', ''signum'', and discontinuous functions can be included in models but need to be posed in a way to allow efficient solution by solvers that perform better with continuous first and second derivatives. There are alternative methods to reformulate the problems. Two popular approaches are as [[http://apmonitor.com/wiki/index.php/Apps/MpecExamples|MPECs]] (Mathematical Programs with Equilibrium Constraints) or with [[http://apmonitor.com/me575/index.php/Main/LogicalConditions|binary variables]] that switch on or off certain elements of the equations.

Changed lines 17-18 from:

There is an additional optional designation of parameters as either fixed values (FVs) or manipulated variables (MVs). Variables can be optionally designated as state variables (SVs) or controlled variables (CVs). The terminology of ''FV, MV, SV,'' and ''CV'' is from the process systems engineering community. In this community the ''MVs'' are designated as the inputs that are potentially changed by the controller and ''CVs'' are model outputs that are driven to target conditions. The terms ''FVs'' refer to either measured or unmeasured disturbances to the system and ''SVs'' are simply designated for viewing purposes as variables of importance. These parameter and variable classifications are specified in MATLAB or Python scripts.

to:

There is an additional optional designation of parameters as either fixed values (FVs) or manipulated variables (MVs). Variables can be optionally designated as state variables (SVs) or controlled variables (CVs). The terminology of ''FV, MV, SV,'' and ''CV'' is from the process systems engineering community. In this community the ''MVs'' are designated as the inputs that are potentially changed by the controller and ''CVs'' are model outputs that are driven to target conditions. The terms ''FVs'' refer to either measured or unmeasured disturbances to the system and ''SVs'' are simply designated for viewing purposes as variables of importance. These parameter and variable classifications are specified in MATLAB or Python scripts (See [[http://apmonitor.com/wiki/index.php/Main/INFO|additional details on FV, MV, SV, and CV classification]]).

Deleted lines 39-40:

Changed lines 53-55 from:

MPECs - max, min,

to:

Certain functions such as ''abs'', ''if..then'', ''min'', ''max'', ''signum'', and discontinuous functions can be included in models but need to be posed in a way to allow efficient solution by solvers that perform better with continuous first and second derivatives. There are alternative methods to reformulate the problems. Two popular approaches are as [[http://apmonitor.com/wiki/index.php/Apps/MpecExamples|MPECs (Mathematical Programs with Equilibrium Constraints)]] or with [[http://apmonitor.com/me575/index.php/Main/LogicalConditions|binary variables that switch on or off certain elements of the equations]].

Changed line 13 from:

* '''Equations''' are either equality constraints as ''f(dx/dt,x,p)=0'', inequality constraints as ''g(dx/dt,x,p)>0'', or expression of the objective with statements that begin with keywords ''maximize'' or ''minimize''.

to:

* '''Equations''' are either equality constraints as ''f(dx/dt,x,p)=0'', inequality constraints as ''g(dx/dt,x,p)>0'', or expression of the objective with statements that begin with keywords ''maximize'' or ''minimize'' (See [[http://apmonitor.com/wiki/index.php/Main/Equations|additional details on equations]]).

Changed line 9 from:

* '''Constants''' are values that never change. Integer values may be defined to give sizes to arrays (See [[http://apmonitor.com/wiki/index.php/Main/Constants|additional details on constants]])~~.~~.

to:

* '''Constants''' are values that never change. Integer values may be defined to give sizes to arrays (See [[http://apmonitor.com/wiki/index.php/Main/Constants|additional details on constants]]).

Changed lines 9-12 from:

* '''Constants''' are values that never change. Integer values may be defined to give sizes to arrays~~.~~

* '''Parameters''' are values that are nominally fixed at initial values but can be changed with input data, by the user, or can become calculated by the optimizer to minimize an objective function if they are indicated as decision variables.

* '''Variables'''are ~~always calculated values as determined by the set of equations~~.~~ Some variables are either measured and~~/~~or controlled to a desired target value.~~

*'''~~Intermediates~~''' are ~~explicit equations where the variable is~~ set ~~equal to an expression that may include constants, parameters, variables, or other intermediate values that are defined previously~~.~~ Intermediates are not implicit equations but are explicitly calculated with each model function evaluation~~.

* '''Parameters''' are values that are nominally fixed at initial values but can be changed with input data, by the user, or can become calculated by the optimizer to minimize an objective function if they are indicated as decision variables.

* '''Variables'''

*

to:

* '''Constants''' are values that never change. Integer values may be defined to give sizes to arrays (See [[http://apmonitor.com/wiki/index.php/Main/Constants|additional details on constants]])..

* '''Parameters''' are values that are nominally fixed at initial values but can be changed with input data, by the user, or can become calculated by the optimizer to minimize an objective function if they are indicated as decision variables (See [[http://apmonitor.com/wiki/index.php/Main/Parameters|additional details on parameters]]).

* '''Variables''' are always calculated values as determined by the set of equations. Some variables are either measured and/or controlled to a desired target value (See [[http://apmonitor.com/wiki/index.php/Main/Variables|additional details on variables]]).

* '''Intermediates''' are explicit equations where the variable is set equal to an expression that may include constants, parameters, variables, or other intermediate values that are defined previously. Intermediates are not implicit equations but are explicitly calculated with each model function evaluation (See [[http://apmonitor.com/wiki/index.php/Main/Intermediates|additional details on intermediates]]).

* '''Parameters''' are values that are nominally fixed at initial values but can be changed with input data, by the user, or can become calculated by the optimizer to minimize an objective function if they are indicated as decision variables (See [[http://apmonitor.com/wiki/index.php/Main/Parameters|additional details on parameters]]).

* '''Variables''' are always calculated values as determined by the set of equations. Some variables are either measured and/or controlled to a desired target value (See [[http://apmonitor.com/wiki/index.php/Main/Variables|additional details on variables]]).

* '''Intermediates''' are explicit equations where the variable is set equal to an expression that may include constants, parameters, variables, or other intermediate values that are defined previously. Intermediates are not implicit equations but are explicitly calculated with each model function evaluation (See [[http://apmonitor.com/wiki/index.php/Main/Intermediates|additional details on intermediates]]).

Changed lines 14-21 from:

* '''Objects''' are object-oriented extensions of APMonitor that ~~may include parameters, variables, equations, and objective terms~~.

~~* '''Connections''' are simple equality constraints that relate object ~~variables ~~to model parameter or variables.~~

There is an additional optional designation of parameters as either fixed values (FVs) or manipulated variables (MVs). Variables can be optionally designated as state variables (SVs) ~~or controlled variables (CVs). The terminology ~~of ~~''FV, MV, SV,'' and ''CV'' is from the process systems engineering community. In this community the ''MVs'' are designated as the inputs that are potentially changed by the controller~~ and ''~~CVs~~'' ~~are model outputs that are driven to target conditions. The terms~~ ''~~FVs~~'' ~~refer to either measured or unmeasured disturbances to the system and ~~''~~SVs~~'' are ~~simply designated for viewing purposes as variables of importance~~.

Collections of constants, parameters, variables, intermediates, equations, objects, and connections constitute a model. The model file is created and stored in a text file with extension ''apm''. Several text editors are ~~available that support syntax highlighting such as Notepad++ and gEdit (see installation instructions).~~

! 5th order differential equationmodel with~~:~~

There is an additional optional designation of parameters as either fixed values (FVs) or manipulated variables (MVs). Variables can be optionally designated as state variables (SVs

Collections of constants, parameters, variables, intermediates, equations, objects, and connections constitute a model. The model file is created and stored in a text file with extension ''apm''. Several text editors

! 5th order differential equation

to:

* '''Objects''' are object-oriented extensions of APMonitor that are stand-alone models that are instantiated from parent objects. The children objects may include parameters, variables, equations, and objective terms (See [[http://apmonitor.com/wiki/index.php/Main/Objects|additional details on objects]]).

* '''Connections''' are equality constraints that relate object variables to model parameter or variables from other models (See [[http://apmonitor.com/wiki/index.php/Main/Connections|additional details on connections]]).

There is an additional optional designation of parameters as either fixed values (FVs) or manipulated variables (MVs). Variables can be optionally designated as state variables (SVs) or controlled variables (CVs). The terminology of ''FV, MV, SV,'' and ''CV'' is from the process systems engineering community. In this community the ''MVs'' are designated as the inputs that are potentially changed by the controller and ''CVs'' are model outputs that are driven to target conditions. The terms ''FVs'' refer to either measured or unmeasured disturbances to the system and ''SVs'' are simply designated for viewing purposes as variables of importance. These parameter and variable classifications are specified in MATLAB or Python scripts.

Collections of constants, parameters, variables, intermediates, equations, objects, and connections constitute a model. The model file is created and stored in a text file with extension ''apm''. Several text editors are available that support syntax highlighting such as Notepad++ and gEdit (---see installation instructions). Below is an example model that demonstrates the use of many of these sections to create a 5th order differential equation model.

* '''Connections''' are equality constraints that relate object variables to model parameter or variables from other models (See [[http://apmonitor.com/wiki/index.php/Main/Connections|additional details on connections]]).

There is an additional optional designation of parameters as either fixed values (FVs) or manipulated variables (MVs). Variables can be optionally designated as state variables (SVs) or controlled variables (CVs). The terminology of ''FV, MV, SV,'' and ''CV'' is from the process systems engineering community. In this community the ''MVs'' are designated as the inputs that are potentially changed by the controller and ''CVs'' are model outputs that are driven to target conditions. The terms ''FVs'' refer to either measured or unmeasured disturbances to the system and ''SVs'' are simply designated for viewing purposes as variables of importance. These parameter and variable classifications are specified in MATLAB or Python scripts.

Collections of constants, parameters, variables, intermediates, equations, objects, and connections constitute a model. The model file is created and stored in a text file with extension ''apm''. Several text editors are available that support syntax highlighting such as Notepad++ and gEdit (---see installation instructions). Below is an example model that demonstrates the use of many of these sections to create a 5th order differential equation model.

Changed lines 49-52 from:

!!!!Slack variables

include slack variable sections from optimization course

include slack variable sections from optimization course

to:

!!!!Slack Variables

Slack variables are defined to transform an inequality expression into an equality expression with an added slack variable. The general expression for inequality constraints in the DAE expression is ''g(dx/dt,x,p)>0''. An equivalent mathematical expression of the general inequality is ''g(dx/dt,x,p)=s'' and ''s>0''. This form is desirable with solvers such as interior point methods where the initial guess must satisfy all inequality constraints or be on the inside of the feasible region. In the slack variable form, an initial guess value greater than zero for the new slack variable ''s'' satisfies this requirement. The APMonitor Modeling Language automatically transforms all inequality constraints into equivalent equality constraints with added slack variables.

Slack variables are defined to transform an inequality expression into an equality expression with an added slack variable. The general expression for inequality constraints in the DAE expression is ''g(dx/dt,x,p)>0''. An equivalent mathematical expression of the general inequality is ''g(dx/dt,x,p)=s'' and ''s>0''. This form is desirable with solvers such as interior point methods where the initial guess must satisfy all inequality constraints or be on the inside of the feasible region. In the slack variable form, an initial guess value greater than zero for the new slack variable ''s'' satisfies this requirement. The APMonitor Modeling Language automatically transforms all inequality constraints into equivalent equality constraints with added slack variables.

Added lines 54-55:

Changed lines 7-8 from:

Models consist of sections including constants, parameters, variables, intermediates, ~~and ~~equations. All expressions used in the equations section must be created in one of the prior sections. The initialization of individual parameters or variables is sequential in the order that they are listed in the model file. Equations, however, can be listed in any order because equations are solved simultaneously.

to:

Models consist of sections including constants, parameters, variables, intermediates, equations, objects, and connections. All expressions used in the equations section must be created in one of the prior sections. The initialization of individual parameters or variables is sequential in the order that they are listed in the model file. Equations, however, can be listed in any order because equations are solved simultaneously.

Changed line 10 from:

* '''Parameters''' are values that are nominally fixed at initial values but can be changed with input data, by the user, or can become calculated by the optimizer to minimize an objective function if they are indicated as ~~degrees of freedom~~.

to:

* '''Parameters''' are values that are nominally fixed at initial values but can be changed with input data, by the user, or can become calculated by the optimizer to minimize an objective function if they are indicated as decision variables.

Changed lines 17-19 from:

to:

There is an additional optional designation of parameters as either fixed values (FVs) or manipulated variables (MVs). Variables can be optionally designated as state variables (SVs) or controlled variables (CVs). The terminology of ''FV, MV, SV,'' and ''CV'' is from the process systems engineering community. In this community the ''MVs'' are designated as the inputs that are potentially changed by the controller and ''CVs'' are model outputs that are driven to target conditions. The terms ''FVs'' refer to either measured or unmeasured disturbances to the system and ''SVs'' are simply designated for viewing purposes as variables of importance.

Collections of constants, parameters, variables, intermediates, equations, objects, and connections constitute a model. The model file is created and stored in a text file with extension ''apm''. Several text editors are available that support syntax highlighting such as Notepad++ and gEdit (see installation instructions).

Collections of constants, parameters, variables, intermediates, equations, objects, and connections constitute a model. The model file is created and stored in a text file with extension ''apm''. Several text editors are available that support syntax highlighting such as Notepad++ and gEdit (see installation instructions).

Changed lines 37-38 from:

In the above model comments are designated with ''!, %,'' or ''#''. Another symbol is the dollar sign ''$'' that indicates a differential variable or ''dx'_i_'/dt''. The ~~square brackets with the range of ~~''[1:~~n~~]'' ~~creates 5 separate ~~variables ~~or ''x[1], x[2], x[3],~~ x[~~4~~],~~'' and ''~~x[~~5~~]~~''. Each variable is initialized with a lower bound of ~~''~~0~~'' ~~and an initial condition of ~~''~~u=3~~''~~. The variable ~~''~~y~~'' ~~is defined in the intermediates section. This variable is a renaming of~~ x[5] and is used in the objective function~~. The quantity x[n] could also be used in the objective function instead with the same result. There are no degrees of freedom for this problem~~.

to:

In the above model comments are designated with ''!, %,'' or ''#''. Another symbol is the dollar sign ''$'' that indicates a differential variable or ''dx'_i_'/dt''. The definition of ''x[1:5]'' in the variables section creates 5 separate variables or ''x[1], x[2], x[3], x[4],'' and ''x[5]''. Each variable is initialized with a lower bound of ''0'' and an initial condition of ''u=3''. The variable ''y'' is defined in the intermediates section. This variable is a copy of x[5] and is used in the objective function as an output with a desired target value of ''5''. The quantity ''x[n]'' could also be used in the objective function instead with the same result. However, there are no degrees of freedom for this problem so the objective function has no effect on the solution.

Added lines 55-58:

!!!!Model Complexity

Model complexity can range from detailed finite element analysis to simple reduced order models. An important aspect of modeling is the overall goal of capturing the input to output relationships for a particular target application. In the case of real-time embedded systems, the complexity of the model may need to be limited to meet simulation or optimization speed requirements. Other times there is no computational time target for a solution and more sophisticated models can be solved. In each case, the correct level of sophistication should be carefully considered. One strategy for finding the appropriate level of complexity is to start with simple models and add complexity only as needed.

Changed line 19 from:

! ~~state space~~ model with:

to:

! 5th order differential equation model with:

Added line 25:

K = 4

Changed line 29 from:

x[1:n] = ~~0~~, >=0

to:

x[1:n] = u, >=0

Changed line 31 from:

y = ~~K * u~~

to:

y = x[n]

Deleted line 34:

Changed lines 37-40 from:

In the above model comments are designated with !, %, or #. ~~The other ~~symbol ~~that ~~is the ~~$ that indicates a differential~~ variable.

(--include additional symbols--)

(--include

to:

In the above model comments are designated with ''!, %,'' or ''#''. Another symbol is the dollar sign ''$'' that indicates a differential variable or ''dx'_i_'/dt''. The square brackets with the range of ''[1:n]'' creates 5 separate variables or ''x[1], x[2], x[3], x[4],'' and ''x[5]''. Each variable is initialized with a lower bound of ''0'' and an initial condition of ''u=3''. The variable ''y'' is defined in the intermediates section. This variable is a renaming of x[5] and is used in the objective function. The quantity x[n] could also be used in the objective function instead with the same result. There are no degrees of freedom for this problem.

(--include link to additional symbols--)

(--include link to additional symbols--)

Changed lines 50-51 from:

to:

include slack variable sections from optimization course

Added lines 55-60:

!!!!Exercise

!!!!Solution

Changed lines 5-6 from:

One of the most important factors in efficient and reliable solution of dynamic systems is the model formulation. Changes in model formulation are not intended to change the equations, only to put them into a form that allows solvers to more easily find an accurate solution. In each section below are some of the key strategies related to model creation and formulation. The discussion begins with a basic introduction to

to:

One of the most important factors in efficient and reliable solution of dynamic systems is the model formulation. Changes in model formulation are not intended to change the equations, only to put them into a form that allows solvers to more easily find an accurate solution. In each section below are some of the key strategies related to model creation and formulation. The discussion begins with a basic introduction to the APMonitor Modeling Language.

Changed lines 17-26 from:

Discretization

Divide by zero

Scaling

Slack variables

to:

Collections of the above elements constitute a model and are stored in a text file with extension ''apm''. Several text editors are available that support syntax highlighting such as Notepad++ and gEdit (see installation instructions).

! state space model with:

! input (1) = u

! states (5) = x[1] to x[5]

! output (1) = y

Constants

n = 5

Parameters

u = 3, >=0, <=10

Variables

x[1:n] = 0, >=0

Intermediates

y = K * u

Equations

$x[1] + x[1] = K * u

$x[2:n] + x[2:n] = x[1:n-1]

y = x[5]

minimize (y-5)^2

In the above model comments are designated with !, %, or #. The other symbol that is the $ that indicates a differential variable.

(--include additional symbols--)

!!!!Time Discretization

There is an inherent trade-off between accuracy and computational speed for numerical solution. Additional time discretization points generally improve the accuracy of a solution but also create additional computational burden. Fewer discretization points are needed when the dynamics are slow or the system is near a steady state solution. As a compromise finer discretization points are often used in regions of fast dynamics and more coarse discretization is used in regions of slow dynamics. Often the fast dynamics are present after a step change in an input or near the beginning of a horizon. Fast dynamics naturally decay as the system exceeds two dominant time constants after a change. The dominant time constant is generally dictated by the slowest process in the system to reach steady state. A dominant time constant is often empirically obtained by introducing a step input and simulating the system until it reaches steady state. The dominant time constant is approximately the amount of time necessary to reach (1-e'^-1^') or 63% of the total response change from initial value to steady state.

There are also cases where dynamic data has been collected from a prior event. In these cases the model predictions are desired for comparison with the dynamic data. To compare model and data at each time point, the simulation step size of the simulation is adjusted to match the data frequency. These replay simulations can take excessive computational effort when large amounts of data are available. For these cases of dynamic data reconciliation, downsampling or less frequent time steps of the data may be used by collecting moving averages, infrequent points, or

simply predicting at less frequent intervals than the data set.

!!!!Slack variables

!!!!Conditional Statements

! state space model with:

! input (1) = u

! states (5) = x[1] to x[5]

! output (1) = y

Constants

n = 5

Parameters

u = 3, >=0, <=10

Variables

x[1:n] = 0, >=0

Intermediates

y = K * u

Equations

$x[1] + x[1] = K * u

$x[2:n] + x[2:n] = x[1:n-1]

y = x[5]

minimize (y-5)^2

In the above model comments are designated with !, %, or #. The other symbol that is the $ that indicates a differential variable.

(--include additional symbols--)

!!!!Time Discretization

There is an inherent trade-off between accuracy and computational speed for numerical solution. Additional time discretization points generally improve the accuracy of a solution but also create additional computational burden. Fewer discretization points are needed when the dynamics are slow or the system is near a steady state solution. As a compromise finer discretization points are often used in regions of fast dynamics and more coarse discretization is used in regions of slow dynamics. Often the fast dynamics are present after a step change in an input or near the beginning of a horizon. Fast dynamics naturally decay as the system exceeds two dominant time constants after a change. The dominant time constant is generally dictated by the slowest process in the system to reach steady state. A dominant time constant is often empirically obtained by introducing a step input and simulating the system until it reaches steady state. The dominant time constant is approximately the amount of time necessary to reach (1-e'^-1^') or 63% of the total response change from initial value to steady state.

There are also cases where dynamic data has been collected from a prior event. In these cases the model predictions are desired for comparison with the dynamic data. To compare model and data at each time point, the simulation step size of the simulation is adjusted to match the data frequency. These replay simulations can take excessive computational effort when large amounts of data are available. For these cases of dynamic data reconciliation, downsampling or less frequent time steps of the data may be used by collecting moving averages, infrequent points, or

simply predicting at less frequent intervals than the data set.

!!!!Slack variables

!!!!Conditional Statements

Deleted line 54:

Added lines 4-28:

One of the most important factors in efficient and reliable solution of dynamic systems is the model formulation. Changes in model formulation are not intended to change the equations, only to put them into a form that allows solvers to more easily find an accurate solution. In each section below are some of the key strategies related to model creation and formulation. The discussion begins with a basic introduction to

Models consist of sections including constants, parameters, variables, intermediates, and equations. All expressions used in the equations section must be created in one of the prior sections. The initialization of individual parameters or variables is sequential in the order that they are listed in the model file. Equations, however, can be listed in any order because equations are solved simultaneously.

* '''Constants''' are values that never change. Integer values may be defined to give sizes to arrays.

* '''Parameters''' are values that are nominally fixed at initial values but can be changed with input data, by the user, or can become calculated by the optimizer to minimize an objective function if they are indicated as degrees of freedom.

* '''Variables''' are always calculated values as determined by the set of equations. Some variables are either measured and/or controlled to a desired target value.

* '''Intermediates''' are explicit equations where the variable is set equal to an expression that may include constants, parameters, variables, or other intermediate values that are defined previously. Intermediates are not implicit equations but are explicitly calculated with each model function evaluation.

* '''Equations''' are either equality constraints as ''f(dx/dt,x,p)=0'', inequality constraints as ''g(dx/dt,x,p)>0'', or expression of the objective with statements that begin with keywords ''maximize'' or ''minimize''.

* '''Objects''' are object-oriented extensions of APMonitor that may include parameters, variables, equations, and objective terms.

* '''Connections''' are simple equality constraints that relate object variables to model parameter or variables.

Collections of the above elements constitute a model and are stored in a text file with extension ''apm''. Several text editors are available that support syntax highlighting such as Notepad++ and gEdit (see installation instructions).

Discretization

Divide by zero

Scaling

Slack variables

MPECs - max, min, abs, if statements, etc

Changed line 1 from:

(:title Formulation Strategies~~ for Simulation of Dynamic Systems~~:)

to:

(:title Formulation Strategies:)

Added lines 1-3:

(:title Formulation Strategies for Simulation of Dynamic Systems:)

(:keywords formulation, strategy, well-posed, modeling language, differential, algebraic, tutorial:)

(:description Model formulation strategies for Differential Algebraic Equations (DAEs) with use in dynamic simulation, estimation, and control:)

(:keywords formulation, strategy, well-posed, modeling language, differential, algebraic, tutorial:)

(:description Model formulation strategies for Differential Algebraic Equations (DAEs) with use in dynamic simulation, estimation, and control:)