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 levelsendExpressioncalls 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 OMJuliajulia> using CSV, DataFrames, PlotlyJSjulia> mod = OMJulia.OMCSession();[ Info: Path to zmq file="/tmp/openmodelica.runner.port.julia.uuPiwroA8N"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_Avk2Af/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 OMJuliajulia> using OMJulia.API: APIjulia> using CSV, DataFrames, PlotlyJSjulia> omc = OMJulia.OMCSession();[ Info: Path to zmq file="/tmp/openmodelica.runner.port.julia.nDyBThtsrG"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)truejulia> res = API.simulate(omc, "BouncingBall"; stopTime=3.0, outputFormat = "csv")Dict{String, Any} with 10 entries: "timeCompile" => 0.943306 "simulationOptions" => "startTime = 0.0, stopTime = 3.0, numberOfIntervals = … "messages" => "LOG_SUCCESS | info | The initialization fini… "timeFrontend" => 0.0022798 "timeTotal" => 0.972704 "timeTemplates" => 0.00202762 "timeSimulation" => 0.0161775 "resultFile" => "/home/runner/work/OMJulia.jl/OMJulia.jl/BouncingBall_… "timeSimCode" => 0.00491671 "timeBackend" => 0.00389011julia> 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 OMJuliajulia> omc = OMJulia.OMCSession();[ Info: Path to zmq file="/tmp/openmodelica.runner.port.julia.wq7ftBPaA7"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, "\\" => "/") endjulia> sendExpression(omc, "loadFile(\"$(bouncingBallFile)\")")truejulia> sendExpression(omc, "simulate(BouncingBall)")Dict{String, Any} with 10 entries: "timeCompile" => 0.965267 "simulationOptions" => "startTime = 0.0, stopTime = 1.0, numberOfIntervals = … "messages" => "LOG_SUCCESS | info | The initialization fini… "timeFrontend" => 0.00223665 "timeTotal" => 0.992586 "timeTemplates" => 0.00227051 "timeSimulation" => 0.0140609 "resultFile" => "/home/runner/work/OMJulia.jl/OMJulia.jl/docs/omc-temp… "timeSimCode" => 0.0010661 "timeBackend" => 0.00757906julia> OMJulia.quit(omc)