Quickstart

There are three ways to interact with OpenModelica:

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;
Info

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 DataFrames, PlotlyJS
julia> mod = OMJulia.OMCSession();[ Info: Path to zmq file="/tmp/openmodelica.runner.port.julia.6Ff2ZWyPTJ"
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> setSimulationOptions(mod, stopTime = 3.0)
julia> simulate(mod)stopTime=3.0 true
julia> df = getSolutions(mod, ["h"])508×2 DataFrame Row │ time h │ Float64 Float64 ─────┼─────────────────── 1 │ 0.0 1.0 2 │ 0.002 0.99998 3 │ 0.004 0.999922 4 │ 0.006 0.999823 5 │ 0.008 0.999686 6 │ 0.01 0.999509 7 │ 0.012 0.999294 8 │ 0.014 0.999039 ⋮ │ ⋮ ⋮ 502 │ 0.99 0.247369 503 │ 0.992 0.242985 504 │ 0.994 0.238563 505 │ 0.996 0.234101 506 │ 0.998 0.2296 507 │ 1.0 0.22506 508 │ 1.0 0.22506 493 rows omitted
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.PHGRkUUYgf"
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" => 1.01545 "simulationOptions" => "startTime = 0.0, stopTime = 3.0, numberOfIntervals = … "messages" => "LOG_SUCCESS | info | The initialization fini… "timeFrontend" => 0.00234765 "timeTotal" => 1.04392 "timeTemplates" => 0.0023959 "timeSimulation" => 0.0157058 "resultFile" => "/home/runner/work/OMJulia.jl/OMJulia.jl/BouncingBall_… "timeSimCode" => 0.00102284 "timeBackend" => 0.00685022
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().

Warn

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\")")
Info

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.TIv229CYaX"
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" => 1.02795 "simulationOptions" => "startTime = 0.0, stopTime = 1.0, numberOfIntervals = … "messages" => "LOG_SUCCESS | info | The initialization fini… "timeFrontend" => 0.00203636 "timeTotal" => 1.05319 "timeTemplates" => 0.00233227 "timeSimulation" => 0.0135957 "resultFile" => "/home/runner/work/OMJulia.jl/OMJulia.jl/docs/omc-temp… "timeSimCode" => 0.000972613 "timeBackend" => 0.00618444
julia> OMJulia.quit(omc)