Profiling Modelica Models

The profiling functionalities of OpenModelica are used to decide if an equation is slow enough to be replaced by a surrogate.

Functions

Profiling

Simulate Modelica model to find slowest equations and what variables are used and what values these variables have during simulation.

NonLinearSystemNeuralNetworkFMU.profilingFunction
profiling(modelName, moFiles; options, threshold = 0.01, ignoreInit = true)

Find equations of Modelica model that are slower then threashold.

Arguments

  • modelName::String: Name of the Modelica model.
  • moFiles::Array{String}: Path to the *.mo file(s) containing the model.

Keywords

  • options::OMOptions: Options for OpenModelica compiler.
  • threshold=0.01: Slowest equations that need more then threshold of total simulation time.
  • ignoreInit::Bool=true: Ignore equations from initialization system if true.

Returns

  • profilingInfo::Vector{ProfilingInfo}: Profiling information with non-linear equation systems slower than threshold.
source
NonLinearSystemNeuralNetworkFMU.minMaxValuesReSimFunction
minMaxValuesReSim(vars, modelName, moFiles; options)

(Re-)simulate Modelica model and find miminum and maximum value each variable has during simulation.

Arguments

  • vars::Array{String}: Array of variables to get min-max values for.
  • modelName::String: Name of Modelica model to simulate.
  • moFiles::Array{String}: Path to .mo file(s).

Keywords

  • options::OMOptions: Options for OpenModelica compiler.

Returns

  • min::Array{Float64}: Minimum values for each variable listed in vars, minus some small epsilon.
  • max::Array{Float64}: Maximum values for each variable listed in vars, plus some small epsilon.

See also profiling.

source

Getting Profiling

The main function will save profiling artifacts that can be loaded with the following functions.

NonLinearSystemNeuralNetworkFMU.getMinMaxFunction
getMinMax(bsonFile, eqNumber, inputArray)

Arguments

  • bsonFile::String: name of the binary JSON file
  • eqNumber::Int: number of the slowest equation
  • inputArray::Vector{String}: array of input variables as String

Return:

  • array of the min and max values of each input from input array
source
getMinMax(bsonFile, eqNumber, inputArray)

Arguments

  • bsonFile::String: name of the binary JSON file
  • eqNumber::Int: number of the slowest equation
  • inputArray::Vector{Int}: array of input variables as Integers

Return:

  • array of the min and max values of each input from input array
source

Structures

NonLinearSystemNeuralNetworkFMU.OMOptionsType
OMOptions <: Any

Settings for profiling and simulating with the OpenModelica Compiler (OMC).

  • pathToOmc::String: Path to omc used for simulating the model.

  • workingDir::String: Working directory for omc. Defaults to the current directory.

  • outputFormat::Union{Nothing, String}: Output format for result file. Can be "mat" or "csv".

  • clean::Bool: Remove everything in workingDir when set to true.

  • commandLineOptions::String: Additional comannd line options for setCommandLineOptions.

  • simFlags::String: Additional simulation flags.

source
NonLinearSystemNeuralNetworkFMU.ProfilingInfoType
ProfilingInfo <: Any

Profiling information for single non-linear equation.

  • eqInfo::EqInfo: Non-linear equation

  • iterationVariables::Array{String}: Iteration (output) variables of non-linear system

  • innerEquations::Array{Int64}: Inner (torn) equations of non-linear system.

  • usingVars::Array{String}: Used (input) variables of non-linear system.

  • parameterVars::Array{String}: Parameter variables of non-linear system.

  • boundary::MinMaxBoundaryValues{Float64}: Minimum and maximum boundary values of usingVars.

source
NonLinearSystemNeuralNetworkFMU.EqInfoType
EqInfo <: Any

Equation info struct.

  • id::Int64: Unique equation id

  • ncall::Int64: Number of calls during simulation

  • time::Float64: Total time [s] spend on evaluating this equation.

  • maxTime::Float64: Maximum time [s] needed for single evaluation of equation.

  • fraction::Float64: Fraction of total simulation time spend on evaluating this equation.

source

Examples

Find Slowest Non-linear Equation Systems

We have a Modelica model SimpleLoop, see test/simpleLoop.mo with some non-linear equation system

\[\begin{align*} r^2 &= x^2 + y^2 \\ rs &= x + y \end{align*}\]

We want to see how much simulation time is spend solving this equation. So let's start profiling:

julia> using NonLinearSystemNeuralNetworkFMU
julia> modelName = "simpleLoop";
julia> moFiles = [joinpath("test","simpleLoop.mo")];
julia> options = OMOptions(workingDir = "tempDir")OMOptions("/usr/bin/omc", "tempDir", "csv", false, " --preOptModules-=wrapFunctionCalls --postOptModules-=wrapFunctionCalls", "")
julia> profilingInfo = profiling(modelName, moFiles; options=options, threshold=0)[ Info: Slowest eq 14: ncall: 2512, time: 0.000450058, maxTime: 1.5746e-5, fraction without overhead: 0.8138642902480882, fraction with overhead: 0.13578130562544877 1-element Vector{ProfilingInfo}: ProfilingInfo(EqInfo(14, 2512, 0.000450058, 1.5746e-5, 0.13578130562544877), iterationVariables: ["y"], innerEquations: [11], usingVars: ["s", "r"], parameterVars: String[], boundary: MinMaxBoundaryValues{Float64}([0.0, 0.95], [1.4087228258248679, 3.15]))

We can see that non-linear equation system 14 is using variables s and r as input and has iteration variable y. x will be computed in the inner equation.

julia> profilingInfo[1].usingVars2-element Vector{String}:
 "s"
 "r"
julia> profilingInfo[1].iterationVariables1-element Vector{String}: "y"

So we can see, that equations 14 is the slowest non-linear equation system. It is called 2512 times and needs around 15% of the total simulation time, in this case that is around 592 $\mu s$.

During profiling function minMaxValuesReSim is called to re-simulate the Modelica model and read the simulation results to find the smallest and largest values for each given variable.

We can check them by looking into

julia> profilingInfo[1].boundary.min2-element Vector{Float64}:
 0.0
 0.95
julia> profilingInfo[1].boundary.min2-element Vector{Float64}: 0.0 0.95

It's possible to save and later load the profilingInfo in binary JSON format:

julia> using BSON
julia> BSON.@save "simpleLoop.bson" profilingInfo
julia> getProfilingInfo("simpleLoop.bson")1-element Vector{ProfilingInfo}: ProfilingInfo(EqInfo(14, 2512, 0.000450058, 1.5746e-5, 0.13578130562544877), iterationVariables: ["y"], innerEquations: [11], usingVars: ["s", "r"], parameterVars: String[], boundary: MinMaxBoundaryValues{Float64}([0.0, 0.95], [1.4087228258248679, 3.15]))