## Comparison of Syntax

## Main.CompareModelingLanguages History

Hide minor edits - Show changes to markup

(:title Comparison of Dynamic Modeling Language Syntax:)

(:title Comparison of Syntax:)

</span></div><br><br><br><br><br><br>

</span></div><br><br><br><br><br><br><br><br><br><br><br><br>

</span></div><br><br>

</span></div><br><br><br><br><br><br>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/1999/REC-html401-19991224/strict.dtd"> <html> <head> <META http-equiv=Content-Type content="text/html; charset=UTF-8"> <title>Exported from Notepad++</title>

</head> <body>

</span></div><br><br></body> </html>

</span></div><br><br>

</span></div></body>

</span></div><br><br></body>

Parameters percent_open c1 = 0.25 ! m^3/sec c2 = 0.14 ! m^1.5/sec Variables inlet_flow ! m^3/sec outlet_flow ! m^3/sec volume ! m^3 Equations inlet_flow = a1 * percent_open outlet_flow = a2 * SQRT(volume) $volume = inlet_flow - outlet_flow 200 < volume < 5000

(:html:) <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/1999/REC-html401-19991224/strict.dtd"> <html> <head> <META http-equiv=Content-Type content="text/html; charset=UTF-8"> <title>Exported from Notepad++</title> <style type="text/css"> span {

font-family: 'Courier New'; font-size: 10pt; color: #000000;

} .sc0 { } .sc2 {

font-style: italic; color: #008000;

} .sc3 {

font-weight: bold; font-style: italic; color: #800000;

} .sc5 {

font-weight: bold; font-style: italic; color: #0080FF;

} .sc6 {

font-weight: bold; font-style: italic; color: #004000;

} .sc24 { } </style> </head> <body> <div style="float: left; white-space: pre; line-height: 1; background: #FFFFFF; "><span class="sc5">Parameters</span><span class="sc24">

</span><span class="sc0">percent_open</span><span class="sc24"> </span><span class="sc0">a1</span><span class="sc24"> </span><span class="sc6">=</span><span class="sc24"> </span><span class="sc3">0.25</span><span class="sc24"> </span><span class="sc2">! m^3/sec

</span><span class="sc24"> </span><span class="sc0">a2</span><span class="sc24"> </span><span class="sc6">=</span><span class="sc24"> </span><span class="sc3">0.14</span><span class="sc24"> </span><span class="sc2">! m^1.5/sec </span><span class="sc5">Variables</span><span class="sc24">

</span><span class="sc0">inlet_flow</span><span class="sc24"> </span><span class="sc2">! m^3/sec

</span><span class="sc24"> </span><span class="sc0">outlet_flow</span><span class="sc24"> </span><span class="sc2">! m^3/sec </span><span class="sc24"> </span><span class="sc0">volume</span><span class="sc24"> </span><span class="sc2">! m^3 </span><span class="sc5">Equations</span><span class="sc24">

</span><span class="sc0">inlet_flow</span><span class="sc24"> </span><span class="sc6">=</span><span class="sc24"> </span><span class="sc0">a1</span><span class="sc24"> </span><span class="sc0">*</span><span class="sc24"> </span><span class="sc0">percent_open</span><span class="sc24"> </span><span class="sc0">outlet_flow</span><span class="sc24"> </span><span class="sc6">=</span><span class="sc24"> </span><span class="sc0">a2</span><span class="sc24"> </span><span class="sc0">*</span><span class="sc24"> </span><span class="sc0">SQRT(volume)</span><span class="sc24"> </span><span class="sc0">$volume</span><span class="sc24"> </span><span class="sc6">=</span><span class="sc24"> </span><span class="sc0">inlet_flow</span><span class="sc24"> </span><span class="sc0">-</span><span class="sc24"> </span><span class="sc0">outlet_flow</span><span class="sc24"> </span><span class="sc3">200</span><span class="sc24"> </span><span class="sc6"><</span><span class="sc24"> </span><span class="sc0">volume</span><span class="sc24"> </span><span class="sc6"><</span><span class="sc24"> </span><span class="sc3">5000</span><span class="sc24">

</span></div></body> </html> (:htmlend:)

a1 = 0.25 ! m^3/sec a2 = 0.14 ! m^1.5/sec

c1 = 0.25 ! m^3/sec c2 = 0.14 ! m^1.5/sec

a2 = 0.14 ! m^1.5/sec Variables inlet_flow ! m^3/sec outlet_flow ! m^3/sec volume ! m^3 Equations inlet_flow = a1 * percent_open outlet_flow = a2 * SQRT(volume) $volume = inlet_flow - outlet_flow 200 < volume < 5000

Parameters percent_open a1 = 0.25 ! m^3/sec a2 = 0.14 ! m^1.5/sec Variables inlet_flow ! m^3/sec outlet_flow ! m^3/sec volume ! m^3 Equations inlet_flow = a1 * percent_open outlet_flow = a2 * SQRT(volume) $volume = inlet_flow - outlet_flow 200 < volume < 5000

Parameters percent_open ! % a1 = 0.25 ! m^3/sec

c1 = 0.25 ! m^3/sec c2 = 0.14 ! m^1.5/sec

a1 = 0.25 ! m^3/sec a2 = 0.14 ! m^1.5/sec

inlet_flow = c1 * percent_open outlet_flow = c2 * SQRT(volume)

inlet_flow = a1 * percent_open outlet_flow = a2 * SQRT(volume)

Model tank Constants ! valve (c1) and outflow (c2) constants c1 = 0.25 ! m^3/sec c2 = 0.14 ! m^1.5/sec End Constants Parameters percent_open ! % End Parameters Variables inlet_flow ! m^3/sec outlet_flow ! m^3/sec volume ! m^3 End Variables Equations inlet_flow = c1 * percent_open outlet_flow = c2 * SQRT(volume) $volume = inlet_flow - outlet_flow 200 < volume < 5000 End Equations End Model

Parameters c1 = 0.25 ! m^3/sec c2 = 0.14 ! m^1.5/sec percent_open ! % Variables inlet_flow ! m^3/sec outlet_flow ! m^3/sec volume ! m^3 Equations inlet_flow = c1 * percent_open outlet_flow = c2 * SQRT(volume) $volume = inlet_flow - outlet_flow 200 < volume < 5000

### Tank Model in APMonitor

### Tank Model in APMonitor

Constants ! valve (c1) and outflow (c2) constants

Constants ! valve (c1) and outflow (c2) constants

### Tank Model in MATLAB

### Tank Model in MATLAB

if (volume < low_volume) & (percent_open < (c_outflow * low_volume^0.5)/c_inflow),

low_open = c_outflow * low_volume^0.5)/c_inflow; if (volume < low_volume) & (percent_open < low_open),

if (volume > high_volume) & (percent_open > (c_outflow * high_volume^0.5)/c_inflow),

high_open = c_outflow * high_volume^0.5)/c_inflow; if (volume > high_volume) & (percent_open > high_open),

### Tank Model in gProms

### Tank Model in gProms

### Tank Model in Modelica

### Tank Model in Modelica

inlet_flow = c1 * percent_open "Inlet flow as a linear function of valve position"; outlet_flow = c2 * volume^0.5 "Outlet flow as a square root function of volume"; der(volume) = inlet_flow - outlet_flow "Mass balance (assuming constant density)";

inlet_flow = c1 * percent_open "Inlet flow"; outlet_flow = c2 * volume^0.5 "Outlet flow"; der(volume) = inlet_flow - outlet_flow "Mass balance";

(:title Comparison of Dynamic Modeling Language Syntax:) (:keywords modeling language, differential algebraic equations, nonlinear control, dynamic estimation, parameter estimation, dynamic optimization, engineering optimization, MATLAB, Python, differential, algebraic:) (:description Comparison of APMonitor, MATLAB, gProms, Modelica:)

## Tank Model

The same dynamic tank model is written in the following 4 modeling languages for a direct comparison between the syntax for the solution of differential and algebraic equations.

- APMonitor
- MATLAB
- gProms
- Modelica

### Tank Model in APMonitor

Model tank

- Constants

! valve (c1) and outflow (c2) constants c1 = 0.25 ! m^3/sec c2 = 0.14 ! m^1.5/sec End Constants Parameters percent_open ! % End Parameters Variables inlet_flow ! m^3/sec outlet_flow ! m^3/sec volume ! m^3 End Variables Equations inlet_flow = c1 * percent_open outlet_flow = c2 * SQRT(volume) $volume = inlet_flow - outlet_flow 200 < volume < 5000 End Equations End Model

### Tank Model in MATLAB

function xdot = tank(t,x) global u % Input (1): % Inlet Valve State (% Open) percent_open = u; % State (1): % Volume in the Tank (m^3) volume = x; % Parameters (2): % Inflow Constant (m^3/sec) c1 = 0.25; % Outflow Constant (m^1.5/sec) c2 = 0.14; % Intermediate variables inlet_flow = (c1 * percent_open); outlet_flow = (c2 * volume^0.5); % Compute xdot (dx/dt) xdot(1,1) = inlet_flow - outlet_flow; % adjust xdot to remain within constraints low_volume = 200; if (volume < low_volume) & (percent_open < (c_outflow * low_volume^0.5)/c_inflow), xdot(1,1) = 0; end high_volume = 5000; if (volume > high_volume) & (percent_open > (c_outflow * high_volume^0.5)/c_inflow), xdot(1,1) = 0; end

### Tank Model in gProms

MODEL Tank DECLARE TYPE Vol = 500.0 : 200.0 : 5000 UNIT = "m^3" END PARAMETER # parameters can be specified at run-time # with Tank.c1 := 0.25; # Tank.c2 := 0.14; c1 AS REAL c2 AS REAL VARIABLE # Volume in the Tank (m^3) volume AS Vol # Inlet flow (m^3/sec) inlet_flow AS REAL # Outlet flow (m3/sec) outlet_flow AS REAL # Inlet Valve State (% Open) percent_open AS REAL EQUATION # Inlet flow is a linear function of valve position inlet_flow = c1 * percent_open ; # Square root pressure drop flow relation outlet_flow = c2 * SQRT ( volume ) ; # Mass balance (assuming constant density) $volume = inlet_flow - outlet_flow ; END # Model Tank

### Tank Model in Modelica

model Tank parameter Real c1=0.25 "Inflow Constant"; parameter Real c2=0.14 "Outflow Constant"; Real percent_open "Percent Open"; Real inlet_flow "Inlet Flow"; Real outlet_flow "Outlet Flow"; Real volume "Tank Volume"; equation inlet_flow = c1 * percent_open "Inlet flow as a linear function of valve position"; outlet_flow = c2 * volume^0.5 "Outlet flow as a square root function of volume"; der(volume) = inlet_flow - outlet_flow "Mass balance (assuming constant density)"; end Tank;