A Julia style scripting API that handles low level API calls.
ModelicaSystem
OMJulia.OMCSession
OMJulia.quit
Let us see the usage of ModelicaSystem
with the help of Modelica model
ModSeborgCSTRorg
model ModSeborgCSTRorg
// Model of original Seborg CSTR in ode form
// author: Bernt Lie, University of Southeast Norway,November 7, 2017
// Parameters
parameter Real V = 100 "Reactor volume, L";
parameter Real rho = 1e3 "Liquid density, g/L";
parameter Real a = 1 "Stoichiometric constant, -";
parameter Real EdR = 8750 "Activation temperature, K";
parameter Real k0 = exp(EdR/350) "Pre-exponential factor, 1/min";
parameter Real cph = 0.239 "Specific heat capacity of mixture, J.g-1.K-1";
parameter Real DrHt = -5e4 "Molar enthalpy of reaction, J/mol";
parameter Real UA = 5e4 "Heat transfer parameter, J/(min.K)";
// Initial state parameters
parameter Real cA0 = 0.5 "Initial concentration of A, mol/L";
parameter Real T0 = 350 "Initial temperature, K";
// Declaring variables
// -- states
Real cA(start = cA0, fixed = true) "Initializing concentration of A in reactor, mol/L";
Real T(start = T0, fixed = true) "Initializing temperature in reactor, K";
// -- auxiliary variables
Real r "Rate of reaction, mol/(L.s)";
Real k "Reaction 'constant', ...";
Real Qd "Heat flow rate, J/min";
// -- input variables
input Real Vdi "Volumetric flow rate through reactor, L/min";
input Real cAi "Influent molar concentration of A, mol/L";
input Real Ti "Influent temperature, K";
input Real Tc "Cooling temperature', K";
// -- output variables
output Real y_T "Reactor temperature, K";
// Equations constituting the model
equation
// Differential equations
der(cA) = Vdi*(cAi-cA)/V- a*r;
der(T) = Vdi*(Ti-T)/V + (-DrHt)*r/(rho*cph) + Qd/(rho*V*cph);
// Algebraic equations
r = k*cA^a;
k = k0*exp(-EdR/T);
Qd = UA*(Tc-T);
// Outputs
y_T = T;
end ModSeborgCSTRorg
using OMJulia
mod = OMJulia.OMCSession()
omcWorkDir = mkpath(joinpath("docs", "omc-temp")) # hide
mkpath(omcWorkDir) # hide
sendExpression(mod, "cd(\"$(omcWorkDir)\")") # hide
ModelicaSystem(mod,
joinpath("docs", "testmodels", "ModSeborgCSTRorg.mo"),
"ModSeborgCSTRorg")
For each OMJulia.OMCSession
session a temporary work directory is created and
the results are published in that working directory.
In order to get the work directory use getWorkDirectory
.
getWorkDirectory
getWorkDirectory(mod)
buildModel
In case the Modelica model needs to be updated or additional simulation flags needs to be
set using sendExpression
The buildModel
API can be used after
ModelicaSystem
.
buildModel(omc)
buildModel(omc, variableFilter="a|T")
getQuantities
showQuantities
getContinuous
getInputs
getOutputs
getParameters
getSimulationOptions
getSolutions
getQuantities(mod)
getQuantities(mod, "T")
getQuantities(mod, ["T","cA"])
showQuantities(mod)
getContinuous(mod)
getContinuous(mod, ["Qd","Tc"])
getInputs(mod)
getOutputs(mod)
getParameters(mod)
getParameters(mod, ["a","V"])
getSimulationOptions(mod)
getSimulationOptions(mod, ["stepSize","tolerance"])
To read the simulation results, we need to simulate the model first and use the getSolution() API to read the results
simulate(mod)
The getSolution method can be used in two different ways.
- using default result filename
- use the result filenames provided by user
This provides a way to compare simulation results and perform regression testing
getSolutions(mod)
getSolutions(mod, ["time","a"])
getSolutions(mod, resultfile="C:/BouncingBal/tmpbouncingBall.mat") //returns list of simulation variables for which results are available , the resulfile location is provided by user
getSolutions(mod, ["time","h"], resultfile="C:/BouncingBal/tmpbouncingBall.mat") // return list of array
setInputs
setParameters
setSimulationOptions
setInputs(mod, "cAi=100")
setInputs(mod, ["cAi=100","Ti=200","Vdi=300","Tc=250"])
setParameters(mod, "a=3")
setParameters(mod, ["a=4","V=200"])
setSimulationOptions(mod, ["stopTime=2.0", "tolerance=1e-08"])
simulate
An example of how to do advanced simulation to set parameter values using set methods and finally simulate the "ModSeborgCSTRorg.mo" model is given below .
getParameters(mod)
setParameters(mod, "a=3.0")
To check whether new values are updated to model , we can again query the getParameters().
getParameters(mod)
Similary we can also use setInputs() to set a value for the inputs during various time interval can also be done using the following.
setInputs(mod, "cAi=100")
And finally we simulate the model
simulate(mod)
linearize
getLinearizationOptions
setLinearizationOptions
getLinearInputs
getLinearOutputs
getLinearStates
getLinearizationOptions(mod)
getLinearizationOptions(mod, ["startTime","stopTime"])
setLinearizationOptions(mod,["stopTime=2.0","tolerance=1e-06"])
res = linearize(mod)
getLinearInputs(mod)
getLinearOutputs(mod)
getLinearStates(mod)
sensitivity
(Sn, Sa) = sensitivity(mod, ["UA","EdR"], ["T","cA"], [1e-2,1e-4])
OMJulia.quit(mod) # hide