Quickstart
There are three ways to interact with OpenModelica:
ModelicaSystem
: A Julia style scripting API that handles low level API calls.OMJulia.API
: A Julia style scripting API that handles low levelsendExpression
calls and has some degree of error handling.- Scripting API with sendExpression: Send expressions to the low level OpenModelica scripting API.
The following examples demonstrate how to simulate Modelica model BouncingBall
in both ways.
model BouncingBall
parameter Real e=0.7 "coefficient of restitution";
parameter Real g=9.81 "gravity acceleration";
Real h(fixed=true, start=1) "height of ball";
Real v(fixed=true) "velocity of ball";
Boolean flying(fixed=true, start=true) "true, if ball is flying";
Boolean impact;
Real v_new(fixed=true);
Integer foo;
equation
impact = h <= 0.0;
foo = if impact then 1 else 2;
der(v) = if flying then -g else 0;
der(h) = v;
when {h <= 0.0 and v <= 0.0,impact} then
v_new = if edge(impact) then -e*pre(v) else 0;
flying = v_new > 0;
reinit(v, v_new);
end when;
end BouncingBall;
The BouncingBall.mo file can be found in your OpenModelica installation directory in <OpenModelcia>/share/doc/omc/testmodels/BouncingBall.mo
.
ModelicaSystem
Start a new OMJulia.OMCSession
and create a new ModelicaSystem
to build and simulate the BouncingBall
model. Afterwards the result can be plotted in Julia.
julia> using OMJulia
julia> using CSV, DataFrames, PlotlyJS
julia> mod = OMJulia.OMCSession();
[ Info: Path to zmq file="/tmp/openmodelica.runner.port.julia.G583539kQ1"
julia> installDir = sendExpression(mod, "getInstallationDirectoryPath()")
"/usr/bin/.."
julia> bouncingBallFile = joinpath(installDir, "share", "doc", "omc", "testmodels", "BouncingBall.mo")
"/usr/bin/../share/doc/omc/testmodels/BouncingBall.mo"
julia> ModelicaSystem(mod, bouncingBallFile, "BouncingBall")
julia> simulate(mod, resultfile = "BouncingBall_ref.csv", simflags = "-override=outputFormat=csv,stopTime=3")
julia> resultfile = joinpath(getWorkDirectory(mod), "BouncingBall_ref.csv")
"/tmp/jl_vBtOkd/BouncingBall_ref.csv"
julia> df = DataFrame(CSV.File(resultfile));
julia> plt = plot(df, x=:time, y=:h, mode="lines", Layout(title="Bouncing Ball", height = 700))
data: [ "scatter with fields mode, type, x, xaxis, y, and yaxis" ] layout: "layout with fields height, legend, margin, template, title, xaxis, and yaxis"
julia> OMJulia.quit(mod)
OMJulia.API
Example
Start a new OMJulia.OMCSession
and call scripting API directly using the OMJulia.API
module.
julia> using OMJulia
julia> using OMJulia.API: API
julia> using CSV, DataFrames, PlotlyJS
julia> omc = OMJulia.OMCSession();
[ Info: Path to zmq file="/tmp/openmodelica.runner.port.julia.gQOyHlPc1M"
julia> installDir = API.getInstallationDirectoryPath(omc)
"/usr/bin/.."
julia> bouncingBallFile = joinpath(installDir, "share", "doc", "omc", "testmodels", "BouncingBall.mo")
"/usr/bin/../share/doc/omc/testmodels/BouncingBall.mo"
julia> API.loadFile(omc, bouncingBallFile)
true
julia> res = API.simulate(omc, "BouncingBall"; stopTime=3.0, outputFormat = "csv")
Dict{String, Any} with 10 entries: "timeCompile" => 0.967801 "simulationOptions" => "startTime = 0.0, stopTime = 3.0, numberOfIntervals = … "messages" => "LOG_SUCCESS | info | The initialization fini… "timeFrontend" => 0.00240373 "timeTotal" => 0.999711 "timeTemplates" => 0.00217259 "timeSimulation" => 0.0177268 "resultFile" => "/home/runner/work/OMJulia.jl/OMJulia.jl/BouncingBall_… "timeSimCode" => 0.00562627 "timeBackend" => 0.00386602
julia> resultfile = res["resultFile"]
"/home/runner/work/OMJulia.jl/OMJulia.jl/BouncingBall_res.csv"
julia> df = DataFrame(CSV.File(resultfile));
julia> plt = plot(df, x=:time, y=:h, mode="lines", Layout(title="Bouncing Ball", height = 700))
data: [ "scatter with fields mode, type, x, xaxis, y, and yaxis" ] layout: "layout with fields height, legend, margin, template, title, xaxis, and yaxis"
julia> OMJulia.quit(omc)
Scripting API with sendExpression
Start a new OMJulia.OMCSession
and send scripting API expressions to the omc session with sendExpression()
.
All special characters inside a string argument for an API function need to be escaped when passing to sendExpression
.
E.g. MOS command
loadFile("/some/path/to/BouncingBall.mo");
becomes Julia code
sendExpression(omc, "loadFile(\"/some/path/to/BouncingBall.mo\")")
On Windows path separation symbol \
needs to be escaped \\
or replaced to Unix style path /
to prevent warnings.
julia> using OMJulia
julia> omc = OMJulia.OMCSession();
[ Info: Path to zmq file="/tmp/openmodelica.runner.port.julia.IF6xWHYjSN"
julia> installDir = sendExpression(omc, "getInstallationDirectoryPath()")
"/usr/bin/.."
julia> bouncingBallFile = joinpath(installDir, "share", "doc", "omc", "testmodels", "BouncingBall.mo")
"/usr/bin/../share/doc/omc/testmodels/BouncingBall.mo"
julia> if Sys.iswindows() bouncingBallFile = replace(bouncingBallFile, "\\" => "/") end
julia> sendExpression(omc, "loadFile(\"$(bouncingBallFile)\")")
true
julia> sendExpression(omc, "simulate(BouncingBall)")
Dict{String, Any} with 10 entries: "timeCompile" => 0.931791 "simulationOptions" => "startTime = 0.0, stopTime = 1.0, numberOfIntervals = … "messages" => "LOG_SUCCESS | info | The initialization fini… "timeFrontend" => 0.00240417 "timeTotal" => 0.958704 "timeTemplates" => 0.00256859 "timeSimulation" => 0.0133569 "resultFile" => "/home/runner/work/OMJulia.jl/OMJulia.jl/docs/omc-temp… "timeSimCode" => 0.0046896 "timeBackend" => 0.00379785
julia> OMJulia.quit(omc)