Advanced API
A Julia style scripting API that handles low level API calls.
ModelicaSystem
OMJulia.ModelicaSystem — FunctionModelicaSystem(omc, fileName, modelName, library=nothing;
commandLineOptions=nothing, variableFilter=nothing, customBuildDirectory=nothing)Set command line options for OMCSession and build model modelName to prepare for a simulation.
Arguments
omc: OpenModelica compiler session, seeOMCSession().fileName: Path to Modelica file.modelName: Name of Modelica model to build, including namespace if the model is wrappen within a Modelica package.library: List of dependent libraries or Modelica files. This argument can be passed as string (e.g."Modelica") or tuple (e.g.("Modelica", "4.0")or array (e.g.["Modelica", "SystemDynamics"]or[("Modelica", "4.0"), "SystemDynamics"]).
Keyword Arguments
commandLineOptions: OpenModelica command line options, see OpenModelica Compiler Flags.variableFilter: Regex to filter variables in result file.
Usage
using OMJulia
mod = OMJulia.OMCSession()
ModelicaSystem(mod, "BouncingBall.mo", "BouncingBall", ["Modelica", "SystemDynamics"], commandLineOptions="-d=newInst")Providing dependent libaries:
using OMJulia
mod = OMJulia.OMCSession()
ModelicaSystem(mod, "BouncingBall.mo", "BouncingBall", ["Modelica", "SystemDynamics", "dcmotor.mo"])See also OMCSession().
ModelicaSystem(omc; modelName, library=nothing,
commandLineOptions=nothing, variableFilter=nothing, customBuildDirectory=nothing)Set command line options for OMCSession and build model modelname to prepare for a simulation.
Arguments
omc: OpenModelica compiler session, seeOMCSession().
Keyword Arguments
modelName: Name of Modelica model to build, including namespace if the model is wrappen within a Modelica package.library: List of dependent libraries or Modelica files. This argument can be passed as string (e.g."Modelica") or tuple (e.g.("Modelica", "4.0")or array (e.g.["Modelica", "SystemDynamics"]or[("Modelica", "4.0"), "SystemDynamics"]).commandLineOptions: OpenModelica command line options, see OpenModelica Compiler Flags.variableFilter: Regex to filter variables in result file.
Usage
using OMJulia
mod = OMJulia.OMCSession()
ModelicaSystem(mod, modelName="Modelica.Electrical.Analog.Examples.CauerLowPassAnalog", library="Modelica")See also OMCSession().
OMJulia.OMCSession — TypeOMCSession <: AnyOMC session struct.
OMCSession(omc=nothing)Create new OpenModelica session.
Arguments
omc::Union{String, Nothing}: Path to OpenModelica compiler. Use omc fromPATHif nothing is provided.
See also ModelicaSystem, OMJulia.quit.
OMJulia.quit — Functionquit(omc::OMCSession; timeout=4::Integer)Quit OMCSession.
Arguments
- `omc::OMCSession`: OMC session.Keywords
- `timeout=4::Integer`: Timeout in seconds.See also OMJulia.OMCSession.
Example
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 ModSeborgCSTRorgjulia> using OMJuliajulia> mod = OMJulia.OMCSession()[ Info: Path to zmq file="/tmp/openmodelica.runner.port.julia.MH7P9ekONR" OMJulia.OMCSession(false, false, Dict{Any, Any}(), Dict{Any, Any}(), Dict{Any, Any}(), "", "/home/runner/work/OMJulia.jl/OMJulia.jl", "", "", "", "", "", nothing, Any[], Dict{Any, Any}(), Dict{Any, Any}(), Dict{Any, Any}(), Dict{Any, Any}(), OMJulia.ZMQSession(ZMQ.Context(WeakRef[WeakRef(ZMQ.Socket(REQ, tcp://127.0.0.1:37621))]), ZMQ.Socket(REQ, tcp://127.0.0.1:37621), Process(`omc --interactive=zmq -z=julia.MH7P9ekONR`, ProcessRunning)), OMJulia.Linearization("", "", Dict{AbstractString, AbstractString}("startTime" => "0.0", "stopTime" => "1.0", "stepSize" => "0.002", "tolerance" => "1e-6"), false, missing, missing, missing))julia> ModelicaSystem(mod, joinpath("docs", "testmodels", "ModSeborgCSTRorg.mo"), "ModSeborgCSTRorg")
WorkDirectory
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.
OMJulia.getWorkDirectory — FunctiongetWorkDirectory(omc)Return working directory of OMJulia.OMCsession omc.
OMJulia.setWorkDirectory — FunctionsetWorkDirectory(omc, customBuildDirectory=nothing)Set the directory the model is built and simulated in.
Pass nothing for a fresh temporary directory, which is what ModelicaSystem does when it is not told otherwise.
Arguments
omc::OMCSession: OpenModelica compiler session.customBuildDirectory::Union{<:AbstractString, Nothing}: An existing directory to work in.
See also getWorkDirectory.
julia> getWorkDirectory(mod)"/tmp/jl_ot3pzR"
Build Model
OMJulia.buildModel — FunctionbuildModel(omc; variableFilter=nothing)Build modelica model.
Arguments
omc::OMCSession: OpenModelica compiler session.
Keyword Arguments
variableFilter: Regex to filter variables in result file.
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")Get Methods
OMJulia.getQuantities — FunctiongetQuantities(omc, name=nothing)Return list of all variables parsed from xml file.
Arguments
omc::OMCSession: OpenModelica compiler session.name::Union{<:AbstractString, Array{<:AbstractString,1}, Nothing}: Names of variables to read from xml file. If nothing is provided read all variables.
See also showQuantities.
OMJulia.showQuantities — FunctionshowQuantities(omc, name=nothing)Return DataFrame of all variables parsed from xml file.
Arguments
omc::OMCSession: OpenModelica compiler session.name::Union{<:AbstractString, Array{<:AbstractString,1}, Nothing}: Names of variables to read from xml file. If nothing is provided read all variables.
See also getQuantities.
OMJulia.getContinuous — FunctiongetContinuous(omc, name=nothing)Return continuous variables parsed from xml file.
Arguments
omc::OMCSession: OpenModelica compiler session.name::Union{<:AbstractString, Array{<:AbstractString,1}, Nothing}: Names of continuous variables to read from xml file. If nothing is provided read all continuous variables.
OMJulia.getInputs — FunctiongetInputs(omc, name=nothing)Return input variables parsed from xml file. If input variables have no start value the returned value is "None".
Arguments
omc::OMCSession: OpenModelica compiler session.name::Union{<:AbstractString, Array{<:AbstractString,1}, Nothing}: Names of input variables to read from xml file. If nothing is provided read all input variables.
OMJulia.getOutputs — FunctiongetInputs(omc, name=nothing)Return output variables parsed from xml file. If output variables have no start value the returned value is "None".
Arguments
omc::OMCSession: OpenModelica compiler session.name::Union{<:AbstractString, Array{<:AbstractString,1}, Nothing}: Names of output variables to read from xml file. If nothing is provided read all output variables.
OMJulia.getParameters — FunctiongetParameters(omc, name=nothing)Return parameter variables parsed from xml file.
Arguments
omc::OMCSession: OpenModelica compiler session.name::Union{<:AbstractString, Array{<:AbstractString,1}, Nothing}: Names of parameters to read from xml file. If nothing is provided read all parameters.
OMJulia.getSimulationOptions — FunctiongetSimulationOptions(omc, name=nothing)Return SimulationOption variables parsed from xml file.
Arguments
omc::OMCSession: OpenModelica compiler session.name::Union{<:AbstractString, Array{<:AbstractString,1}, Nothing}: Names of parameters to read from xml file. If nothing is provided read all parameters.
OMJulia.getSolutions — FunctiongetSolutions(omc::OMCSession, name=nothing; resultfile=nothing)Read the result file and return the simulation results as a DataFrames.DataFrame.
time is always the first column, whether or not it was asked for, so the returned frame stands on its own.
Arguments
omc::OMCSession: OpenModelica compiler session.name::Union{<:AbstractString, AbstractVector{<:AbstractString}, Nothing}: Names of variables to read from result file. If nothing is provided read all variables, parameters included, which for a large model is a large read.
Keyword Arguments
resultfile::Union{AbstractString, Nothing}: Path to result file. If nothing is provided use saved result file.
See also getSolutionNames to list what a result file holds without reading it.
OMJulia.getSolutionNames — FunctiongetSolutionNames(omc::OMCSession; resultfile=nothing)Return the names of the variables stored in the result file.
time is not part of the list: it is the independent variable, and getSolutions always returns it. Neither are omc's internal $-prefixed names, which cannot be read back.
Arguments
omc::OMCSession: OpenModelica compiler session.
Keyword Arguments
resultfile::Union{AbstractString, Nothing}: Path to result file. If nothing is provided use saved result file.
Examples
julia> getQuantities(mod)23-element Vector{Any}: Dict{Any, Any}("name" => "T", "max" => "None", "start" => "None", "min" => "None", "aliasvariable" => nothing, "variability" => "continuous", "changeable" => "false", "causality" => "local", "description" => "Initializing temperature in reactor, K", "alias" => "noAlias"…) Dict{Any, Any}("name" => "cA", "max" => "None", "start" => "None", "min" => "None", "aliasvariable" => nothing, "variability" => "continuous", "changeable" => "false", "causality" => "local", "description" => "Initializing concentration of A in reactor, mol/L", "alias" => "noAlias"…) Dict{Any, Any}("name" => "der(T)", "max" => "None", "start" => "None", "min" => "None", "aliasvariable" => nothing, "variability" => "continuous", "changeable" => "false", "causality" => "local", "description" => "der(Initializing temperature in reactor, K)", "alias" => "noAlias"…) Dict{Any, Any}("name" => "der(cA)", "max" => "None", "start" => "None", "min" => "None", "aliasvariable" => nothing, "variability" => "continuous", "changeable" => "false", "causality" => "local", "description" => "der(Initializing concentration of A in reactor, mol/L)", "alias" => "noAlias"…) Dict{Any, Any}("name" => "\$cse1", "max" => "None", "start" => "None", "min" => "None", "aliasvariable" => nothing, "variability" => "continuous", "changeable" => "false", "causality" => "local", "description" => nothing, "alias" => "noAlias"…) Dict{Any, Any}("name" => "Qd", "max" => "None", "start" => "None", "min" => "None", "aliasvariable" => nothing, "variability" => "continuous", "changeable" => "false", "causality" => "local", "description" => "Heat flow rate, J/min", "alias" => "noAlias"…) Dict{Any, Any}("name" => "Tc", "max" => "None", "start" => "None", "min" => "None", "aliasvariable" => nothing, "variability" => "continuous", "changeable" => "true", "causality" => "input", "description" => "Cooling temperature', K", "alias" => "noAlias"…) Dict{Any, Any}("name" => "Ti", "max" => "None", "start" => "None", "min" => "None", "aliasvariable" => nothing, "variability" => "continuous", "changeable" => "true", "causality" => "input", "description" => "Influent temperature, K", "alias" => "noAlias"…) Dict{Any, Any}("name" => "Vdi", "max" => "None", "start" => "None", "min" => "None", "aliasvariable" => nothing, "variability" => "continuous", "changeable" => "true", "causality" => "input", "description" => "Volumetric flow rate through reactor, L/min", "alias" => "noAlias"…) Dict{Any, Any}("name" => "cAi", "max" => "None", "start" => "None", "min" => "None", "aliasvariable" => nothing, "variability" => "continuous", "changeable" => "true", "causality" => "input", "description" => "Influent molar concentration of A, mol/L", "alias" => "noAlias"…) ⋮ Dict{Any, Any}("name" => "EdR", "max" => "None", "start" => "8750.0", "min" => "None", "aliasvariable" => nothing, "variability" => "parameter", "changeable" => "true", "causality" => "parameter", "description" => "Activation temperature, K", "alias" => "noAlias"…) Dict{Any, Any}("name" => "T0", "max" => "None", "start" => "350.0", "min" => "None", "aliasvariable" => nothing, "variability" => "parameter", "changeable" => "true", "causality" => "parameter", "description" => "Initial temperature, K", "alias" => "noAlias"…) Dict{Any, Any}("name" => "UA", "max" => "None", "start" => "5e4", "min" => "None", "aliasvariable" => nothing, "variability" => "parameter", "changeable" => "true", "causality" => "parameter", "description" => "Heat transfer parameter, J/(min.K)", "alias" => "noAlias"…) Dict{Any, Any}("name" => "V", "max" => "None", "start" => "100.0", "min" => "None", "aliasvariable" => nothing, "variability" => "parameter", "changeable" => "true", "causality" => "parameter", "description" => "Reactor volume, L", "alias" => "noAlias"…) Dict{Any, Any}("name" => "a", "max" => "None", "start" => "1.0", "min" => "None", "aliasvariable" => nothing, "variability" => "parameter", "changeable" => "true", "causality" => "parameter", "description" => "Stoichiometric constant, -", "alias" => "noAlias"…) Dict{Any, Any}("name" => "cA0", "max" => "None", "start" => "0.5", "min" => "None", "aliasvariable" => nothing, "variability" => "parameter", "changeable" => "true", "causality" => "parameter", "description" => "Initial concentration of A, mol/L", "alias" => "noAlias"…) Dict{Any, Any}("name" => "cph", "max" => "None", "start" => "0.239", "min" => "None", "aliasvariable" => nothing, "variability" => "parameter", "changeable" => "true", "causality" => "parameter", "description" => "Specific heat capacity of mixture, J.g-1.K-1", "alias" => "noAlias"…) Dict{Any, Any}("name" => "k0", "max" => "None", "start" => "None", "min" => "None", "aliasvariable" => nothing, "variability" => "parameter", "changeable" => "false", "causality" => "calculatedParameter", "description" => "Pre-exponential factor, 1/min", "alias" => "noAlias"…) Dict{Any, Any}("name" => "rho", "max" => "None", "start" => "1000.0", "min" => "None", "aliasvariable" => nothing, "variability" => "parameter", "changeable" => "true", "causality" => "parameter", "description" => "Liquid density, g/L", "alias" => "noAlias"…)julia> getQuantities(mod, "T")1-element Vector{Dict{Any, Any}}: Dict("name" => "T", "max" => "None", "start" => "None", "min" => "None", "aliasvariable" => nothing, "variability" => "continuous", "changeable" => "false", "causality" => "local", "description" => "Initializing temperature in reactor, K", "alias" => "noAlias"…)julia> getQuantities(mod, ["T","cA"])2-element Vector{Dict{Any, Any}}: Dict("name" => "T", "max" => "None", "start" => "None", "min" => "None", "aliasvariable" => nothing, "variability" => "continuous", "changeable" => "false", "causality" => "local", "description" => "Initializing temperature in reactor, K", "alias" => "noAlias"…) Dict("name" => "cA", "max" => "None", "start" => "None", "min" => "None", "aliasvariable" => nothing, "variability" => "continuous", "changeable" => "false", "causality" => "local", "description" => "Initializing concentration of A in reactor, mol/L", "alias" => "noAlias"…)julia> showQuantities(mod)23×10 DataFrame Row │ max description name start variability ⋯ │ String Union… String String String ⋯ ─────┼────────────────────────────────────────────────────────────────────────── 1 │ None Initializing temperature in reac… T None continuous ⋯ 2 │ None Initializing concentration of A … cA None continuous 3 │ None der(Initializing temperature in … der(T) None continuous 4 │ None der(Initializing concentration o… der(cA) None continuous 5 │ None $cse1 None continuous ⋯ 6 │ None Heat flow rate, J/min Qd None continuous 7 │ None Cooling temperature', K Tc None continuous 8 │ None Influent temperature, K Ti None continuous ⋮ │ ⋮ ⋮ ⋮ ⋮ ⋮ ⋱ 17 │ None Heat transfer parameter, J/(min.… UA 5e4 parameter ⋯ 18 │ None Reactor volume, L V 100.0 parameter 19 │ None Stoichiometric constant, - a 1.0 parameter 20 │ None Initial concentration of A, mol/L cA0 0.5 parameter 21 │ None Specific heat capacity of mixtur… cph 0.239 parameter ⋯ 22 │ None Pre-exponential factor, 1/min k0 None parameter 23 │ None Liquid density, g/L rho 1000.0 parameter 5 columns and 8 rows omitted
julia> getContinuous(mod)Dict{Any, Any} with 13 entries: "T" => "None" "der(T)" => "None" "Tc" => "None" "r" => "None" "cA" => "None" "\$cse1" => "None" "cAi" => "None" "k" => "None" "Qd" => "None" "der(cA)" => "None" "Ti" => "None" "Vdi" => "None" "y_T" => "None"julia> getContinuous(mod, ["Qd","Tc"])2-element Vector{String}: "None" "None"
julia> getInputs(mod)Dict{Any, Any} with 4 entries: "Ti" => "None" "cAi" => "None" "Vdi" => "None" "Tc" => "None"julia> getOutputs(mod)Dict{Any, Any} with 1 entry: "y_T" => "None"
julia> getParameters(mod)Dict{Any, Any} with 10 entries: "DrHt" => "-5e4" "T0" => "350.0" "cph" => "0.239" "k0" => "None" "V" => "100.0" "EdR" => "8750.0" "UA" => "5e4" "a" => "1.0" "cA0" => "0.5" "rho" => "1000.0"julia> getParameters(mod, ["a","V"])2-element Vector{String}: "1.0" "100.0"
julia> getSimulationOptions(mod)Dict{Any, Any} with 5 entries: "startTime" => "0" "stopTime" => "1" "solver" => "dassl" "stepSize" => "0.002" "tolerance" => "1e-06"julia> getSimulationOptions(mod, ["stepSize","tolerance"])2-element Vector{String}: "0.002" "1e-06"
Reading Simulation Results
To read the simulation results, we need to simulate the model first and use the getSolution() API to read the results
julia> simulate(mod)true
getSolutions returns a DataFrame. The time column is always there, whether or not you asked for it, so a returned frame stands on its own. Pass no variable names to read the whole result file.
julia> getSolutions(mod, ["a"])502×2 DataFrame Row │ time a │ Float64 Float64 ─────┼────────────────── 1 │ 0.0 1.0 2 │ 0.002 1.0 3 │ 0.004 1.0 4 │ 0.006 1.0 5 │ 0.008 1.0 6 │ 0.01 1.0 7 │ 0.012 1.0 8 │ 0.014 1.0 ⋮ │ ⋮ ⋮ 496 │ 0.99 1.0 497 │ 0.992 1.0 498 │ 0.994 1.0 499 │ 0.996 1.0 500 │ 0.998 1.0 501 │ 1.0 1.0 502 │ 1.0 1.0 487 rows omittedjulia> getSolutions(mod, "Tc")502×2 DataFrame Row │ time Tc │ Float64 Float64 ─────┼────────────────── 1 │ 0.0 0.0 2 │ 0.002 0.0 3 │ 0.004 0.0 4 │ 0.006 0.0 5 │ 0.008 0.0 6 │ 0.01 0.0 7 │ 0.012 0.0 8 │ 0.014 0.0 ⋮ │ ⋮ ⋮ 496 │ 0.99 0.0 497 │ 0.992 0.0 498 │ 0.994 0.0 499 │ 0.996 0.0 500 │ 0.998 0.0 501 │ 1.0 0.0 502 │ 1.0 0.0 487 rows omitted
Use getSolutionNames when you only want to know which variables a result file holds, without reading them.
julia> getSolutionNames(mod)22-element Vector{String}: "DrHt" "EdR" "Qd" "T" "T0" "Tc" "Ti" "UA" "V" "Vdi" ⋮ "cAi" "cph" "der(T)" "der(cA)" "k" "k0" "r" "rho" "y_T"
The result file can also be one you provide, which is how two runs are compared and how regression tests are written.
getSolutionNames(mod, resultfile="C:/BouncingBall/tmpbouncingBall.mat")
getSolutions(mod, ["h"], resultfile="C:/BouncingBall/tmpbouncingBall.mat")Set Methods
OMJulia.setInputs — FunctionsetInputs(omc, inputs)Set new values for input variables.
Arguments
omc::OMCSession: OpenModelica compiler session.inputs::AbstractDict: Input names mapped to their new values, e.g.Dict("cAi" => 100, "Ti" => 200). A value may also be a vector of(time, value)points, e.g.Dict("cAi" => [(0, 100), (1, 50)]), to vary the input over the simulation.
setInputs(omc, name)Deprecated. Use the AbstractDict method of setInputs instead:
setInputs(omc, Dict("cAi" => 100))OMJulia.setParameters — FunctionsetParameters(omc, parameters; verbose=true)Set parameter values for parameter variables defined by users.
Arguments
omc::OMCSession: OpenModelica compiler session.parameters::AbstractDict: Parameter names mapped to their new values, e.g.Dict("a" => 3, "V" => 200). Values are converted withstring.
Keyword Arguments
verbose::Bool: Explain why a parameter could not be changed.
An unknown parameter name is an error: a typo would otherwise simulate the model with its default value and say nothing.
setParameters(omc, name; verbose=true)Deprecated. Use the AbstractDict method of setParameters instead:
setParameters(omc, Dict("a" => 3, "V" => 200))OMJulia.setContinuous — FunctionsetContinuous(omc, continuous)Set start values for continuous variables.
Arguments
omc::OMCSession: OpenModelica compiler session.continuous::AbstractDict: Variable names mapped to their new start values, e.g.Dict("T" => 350). Values are converted withstring.
setContinuous(omc, name)Deprecated. Use the AbstractDict method of setContinuous instead:
setContinuous(omc, Dict("T" => 350))OMJulia.setSimulationOptions — FunctionsetSimulationOptions(omc; startTime=nothing, stopTime=nothing, stepSize=nothing, tolerance=nothing, solver=nothing)Set simulation option values. Options left at nothing keep their current value.
Arguments
omc::OMCSession: OpenModelica compiler session.
Keyword Arguments
startTime: Start time of the simulation.stopTime: Stop time of the simulation.stepSize: Step size of the output interval,intervalin the Modelicaexperimentannotation.tolerance: Solver tolerance.solver: Name of the integration method, e.g."dassl".
setSimulationOptions(omc, options)Set simulation option values from a Dict, e.g. Dict("stopTime" => 2.0, "tolerance" => 1e-8).
Arguments
omc::OMCSession: OpenModelica compiler session.options::AbstractDict: Option names mapped to their new values. Values are converted withstring.
setSimulationOptions(omc, name)Deprecated. Use the keyword arguments of setSimulationOptions instead:
setSimulationOptions(omc, stopTime = 2.0, tolerance = 1e-8)OMJulia.isParameterChangeable — FunctionisParameterChangeable(omc, name)Whether the parameter name can be overridden without rebuilding the model.
A structural, final, protected or evaluated parameter cannot, nor can one with a non-constant binding.
Arguments
omc::OMCSession: OpenModelica compiler session.name::AbstractString: Name of the parameter.
Examples
julia> setInputs(mod, Dict("cAi" => 100))julia> setInputs(mod, Dict("cAi" => 100, "Ti" => 200, "Vdi" => 300, "Tc" => 250))
An input may also vary over the simulation, given as (time, value) points.
julia> setInputs(mod, Dict("cAi" => [(0, 100), (1, 50)]))
julia> setParameters(mod, Dict("a" => 3))julia> setParameters(mod, Dict("a" => 4, "V" => 200))
A parameter that omc cannot override – a structural, final, protected or evaluated one, or one with a non-constant binding – is an error rather than a warning, since the alternative is simulating with the old value and saying nothing. Ask first with isParameterChangeable if you need to.
Continuous variables take start values the same way.
julia> setContinuous(mod, Dict("T" => 350))
Simulation options are keyword arguments. stepSize is what the Modelica experiment annotation calls Interval.
julia> setSimulationOptions(mod, stopTime = 2.0, tolerance = 1e-08)
The "name=value" string form all four set methods used to take still works, but it is deprecated and will be removed in a future breaking release.
Advanced Simulation
OMJulia.simulate — Functionsimulate(omc; resultfile=nothing, simflags="", verbose=false)Simulate modelica model.
Arguments
omc::OMCSession: OpenModelica compiler session, seeOMCSession().
Keyword Arguments
resultFile::Union{String, Nothing}: Result file to write simulation results into.simflags::String: Simulation flags, see Simulation Runtime Flags.verbose::Bool: [debug] Log cmd call tolog.txtanderror.txt.
Examples
simulate(omc)Specify result file:
simulate(omc, resultfile="tmpresult.mat")Set simulation runtime flags:
simulate(omc, simflags="-noEmitEvent -override=e=0.3,g=9.3")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 .
julia> getParameters(mod)Dict{Any, Any} with 10 entries: "DrHt" => "-5e4" "T0" => "350.0" "cph" => "0.239" "k0" => "None" "V" => "200" "EdR" => "8750.0" "UA" => "5e4" "a" => "4" "cA0" => "0.5" "rho" => "1000.0"julia> setParameters(mod, Dict("a" => 3.0))
To check whether new values are updated to model , we can again query the getParameters().
julia> getParameters(mod)Dict{Any, Any} with 10 entries: "DrHt" => "-5e4" "T0" => "350.0" "cph" => "0.239" "k0" => "None" "V" => "200" "EdR" => "8750.0" "UA" => "5e4" "a" => "3.0" "cA0" => "0.5" "rho" => "1000.0"
Similary we can also use setInputs() to set a value for the inputs during various time interval can also be done using the following.
julia> setInputs(mod, Dict("cAi" => 100))
And finally we simulate the model
julia> simulate(mod)T=350 stopTime=2.0 V=200 a=3.0 tolerance=1.0e-8 true
Linearization
OMJulia.linearize — Functionlinearize(omc; lintime = nothing, simflags= nothing, verbose=true)Linearize the model and return a LinearizationResult.
It destructures into the four state space matrices, A, B, C, D = linearize(omc), which is what this returned before; the result also carries the dimensions and the operating point.
Arguments
omc::OMCSession: OpenModelica compiler session.
Keyword Arguments
lintime: Value specifies a time where the linearization of the model should be performedsimflags: Simulation flags, see Simulation Runtime Flags.
Examples of using linearize() API
linearize(omc)Specify result file:
linearize(omc, lintime="0.5")Set simulation runtime flags:
linearize(omc, simflags="-noEmitEvent")OMJulia.LinearizationResult — TypeLinearizationResult <: AnyWhat linearize produced: the state space matrices, their dimensions, and the operating point they were taken at.
linearize used to return only [A, B, C, D] and throw the rest away, even though it had already read it out of the generated model. Destructuring and indexing still mean what they did, so A, B, C, D = linearize(omc) and linearize(omc)[1] keep working.
OMJulia.getLinearizationOptions — FunctiongetLinearizationOptions(omc, name=nothing)Return linearization options.
Arguments
omc::OMCSession: OpenModelica compiler session.name::Union{<:AbstractString, Array{<:AbstractString,1}, Nothing}: Names of linearization options. If nothing is provided return all linearization options.
OMJulia.setLinearizationOptions — FunctionsetLinearizationOptions(omc; startTime=nothing, stopTime=nothing, stepSize=nothing, tolerance=nothing)Set linearization options. Options left at nothing keep their current value.
Arguments
omc::OMCSession: OpenModelica compiler session.
Keyword Arguments
startTime: Start time of the linearization.stopTime: Stop time of the linearization.stepSize: Step size of the output interval.tolerance: Solver tolerance.
setLinearizationOptions(omc, options)Set linearization options from a Dict, e.g. Dict("stopTime" => 2.0, "tolerance" => 1e-6).
Arguments
omc::OMCSession: OpenModelica compiler session.options::AbstractDict: Option names mapped to their new values. Values are converted withstring.
setLinearizationOptions(omc, name)Deprecated. Use the keyword arguments of setLinearizationOptions instead:
setLinearizationOptions(omc, stopTime = 2.0, tolerance = 1e-6)OMJulia.getLinearInputs — FunctiongetLinearInputs(omc)Return linear input variables after the model is linearized
Arguments
omc::OMCSession: OpenModelica compiler session.
OMJulia.getLinearOutputs — FunctiongetLinearOutputs(omc)Return linear output variables after the model is linearized
Arguments
omc::OMCSession: OpenModelica compiler session.
OMJulia.getLinearStates — FunctiongetLinearStates(omc)Return linear state variables after the model is linearized
Arguments
omc::OMCSession: OpenModelica compiler session.
Examples
julia> getLinearizationOptions(mod)Dict{AbstractString, AbstractString} with 4 entries: "startTime" => "0.0" "stopTime" => "1.0" "stepSize" => "0.002" "tolerance" => "1e-6"julia> getLinearizationOptions(mod, ["startTime","stopTime"])2-element Vector{String}: "0.0" "1.0"
julia> setLinearizationOptions(mod, stopTime = 2.0, tolerance = 1e-06)
julia> res = linearize(mod)LOG_STDOUT | warning | It is not possible to override the following quantity: T | | | | It seems to be structural, final, protected or evaluated or has a non-constant binding. LOG_STDOUT | warning | simulation_input_xml.c: override variable name not found in model: startTime LOG_STDOUT | warning | simulation_input_xml.c: override variable name not found in model: stopTime LOG_STDOUT | warning | simulation_input_xml.c: override variable name not found in model: stepSize LOG_STDOUT | warning | simulation_input_xml.c: override variable name not found in model: tolerance LOG_STDOUT | info | Linearization will be performed at point of time: 2.000000 LOG_SUCCESS | info | The initialization finished successfully without homotopy method. LOG_SUCCESS | info | The simulation finished successfully. LOG_STDOUT | info | Linear model is created at /tmp/jl_ot3pzR/linearized_model.jl LOG_STDOUT | info | The output format can be changed with the command line option --linearizationDumpLanguage. LOG_STDOUT | info | The options are: --linearizationDumpLanguage=none, modelica, matlab, julia, python. LOG_STDOUT | info | In OMEdit Simulation Setup->Linearize->Target language for linearized model. WARNING: Detected access to binding `OMJulia.linearized_model` in a world prior to its definition world. Julia 1.12 has introduced more strict world age semantics for global bindings. !!! This code may malfunction under Revise. !!! This code will error in future versions of Julia. Hint: Add an appropriate `invokelatest` around the access to this binding. To make this warning an error, and hence obtain a stack trace, use `julia --depwarn=error`. OMJulia.LinearizationResult(2, 4, 1, [4304.709851526914, 0.001743608185874734], [250.0, 200.0, 300.0, 100.0], [2.392863998572054 1.799622214985262e7; -0.07082366973952324 -258067.3256288866], [1.046025104602511 1.5 -20.52354925763457 0.0; 0.0 0.0 0.4999912819590706 1.5], [1.0 0.0], [0.0 0.0 0.0 0.0], ["T", "cA"], ["Tc", "Ti", "Vdi", "cAi"], ["y_T"])
julia> getLinearInputs(mod)4-element Vector{String}: "Tc" "Ti" "Vdi" "cAi"julia> getLinearOutputs(mod)1-element Vector{String}: "y_T"julia> getLinearStates(mod)2-element Vector{String}: "T" "cA"
FMI
OMJulia.convertMo2Fmu — Functionfunction which converts modelica model to FMU
convertMo2Fmu(omc; version::String = "2.0", fmuType::String = "me_cs", fileNamePrefix::String = "<default>", includeResources::Bool = true)Arguments
omc::OMCSession: OpenModelica compiler session, seeOMCSession().
Keyword Arguments
version::String: version 1.0 or 2.0fmuType::String: FMU type, me (model exchange), cs (co-simulation), me_cs (both model exchange and co-simulation)"fileNamePrefix::String: modelname will be used as default.
Examples
convertMo2Fmu(omc)OMJulia.convertFmu2Mo — Functionfunction which converts FMU to modelicamodel
Sensitivity Analysis
OMJulia.sensitivity — Functionsensitivity(omc::OMCSession, Vp, Vv, Ve=[1e-2])Method for computing numeric sensitivity of OpenModelica object.
Arguments
omc::OMCSession: OpenModelica compiler session.Vp::Array{<:AbstractString, 1}: Modelica Parameter names.Vv::Array{<:AbstractString, 1}: Modelica Variable names.Ve::Array{Float64, 1}: Excitations of parameters; defaults to scalar 1e-2
Return
VSname::Vector{Vector{String}}: Vector of sensitivity namesVSarray::Vector{Vector{Vector{Float64}}}: Vector of sensitivies: vector of elements per parameter
Each element containing time series per variable
Examples
julia> (Sn, Sa) = sensitivity(mod, ["UA","EdR"], ["T","cA"], [1e-2,1e-4])T=350 stopTime=2.0 V=200 a=3.0 tolerance=1.0e-8 T=350 stopTime=2.0 V=200 UA=50500.0 a=3.0 tolerance=1.0e-8 T=350 stopTime=2.0 V=200 EdR=8750.875 UA=50000.0 a=3.0 tolerance=1.0e-8 ([["Sensitivity.UA.T", "Sensitivity.UA.cA"], ["Sensitivity.EdR.T", "Sensitivity.EdR.cA"]], [[[0.0, -4.18314664727859e-6, -8.465104608831098e-6, -1.3138094795067446e-5, -1.88014740891731e-5, -2.6764871823729664e-5, -4.055488525546025e-5, -7.421106365268315e-5, -0.0002800878724630138, -8.671261061408586e-5 … -0.02408746364230956, -0.024121420161302923, -0.02415524811211526, -0.024188942377976672, -0.024222499631590837, -0.024256012310161168, -0.02428940849377068, -0.024322685011974498, -0.024355838220724763, -0.024355838220724763], [0.0, 3.0880059265214753e-10, 2.6049781749470924e-9, 1.089740961734975e-8, 3.34824264354161e-8, 8.866840948451581e-8, 2.2624948089777774e-7, 6.460992222683259e-7, 3.5294282973739046e-6, 6.416571588722108e-7 … 8.481905589606629e-9, 8.472469753644223e-9, 8.439057174328048e-9, 8.379132128268594e-9, 8.291944665154808e-9, 8.25270205343084e-9, 8.207263207489316e-9, 8.156122180465013e-9, 8.09977262259667e-9, 8.09977262259667e-9]], [[0.0, -3.299156300532299e-7, -2.4162545742001384e-6, -7.690115580771817e-6, -1.30661745580645e-5, 4.73043824708189e-6, 0.0001570644542750545, 0.0010987339720226633, 0.01320456932671732, 0.011213385238858922 … 0.00031939632182392027, 0.0003534731975897947, 0.00039548291689633127, 0.00044731957314070314, 0.0004970625034599964, 0.0005548803502668827, 0.0006226019998263967, 0.000702326975442702, 0.0007964254327816889, 0.0007964254327816889], [0.0, 4.733819812331928e-9, 3.4686287112403336e-8, 1.1045635974125747e-7, 1.8787095511031892e-7, -6.708594213478136e-8, -2.2533290281298183e-6, -1.577146324243423e-5, -0.00018949809454531824, -0.00016264102610381265 … -1.7129598655126616e-6, -1.6606196756456724e-6, -1.576848307949836e-6, -1.4576103427318073e-6, -1.4378611064933683e-6, -1.4372558718192343e-6, -1.4604149047303672e-6, -1.5119587014730422e-6, -1.5965079885210776e-6, -1.5965079885210776e-6]]])