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.profiling — Functionprofiling(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 thenthresholdof total simulation time.ignoreInit::Bool=true: Ignore equations from initialization system iftrue.
Returns
profilingInfo::Vector{ProfilingInfo}: Profiling information with non-linear equation systems slower thanthreshold.
NonLinearSystemNeuralNetworkFMU.minMaxValuesReSim — FunctionminMaxValuesReSim(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 invars, minus some small epsilon.max::Array{Float64}: Maximum values for each variable listed invars, plus some small epsilon.
See also profiling.
Getting Profiling
The main function will save profiling artifacts that can be loaded with the following functions.
NonLinearSystemNeuralNetworkFMU.getProfilingInfo — FunctiongetProfilingInfo(bsonFile)Read ProfilingInfo array from binary JSON file.
NonLinearSystemNeuralNetworkFMU.getUsingVars — FunctiongetUsingVars(bsonFile, eqNumber)Arguments
bsonFile::String: name of the binary JSON fileeqNumber::Int: number of the slowest equation
Return:
- Array of used variables
NonLinearSystemNeuralNetworkFMU.getIterationVars — FunctiongetIterationVars(bsonFile, eqNumber)Arguments
bsonFile::String: name of the binary JSON fileeqNumber::Int: number of the slowest equation
Return:
- Array of iteration variables
NonLinearSystemNeuralNetworkFMU.getInnerEquations — FunctiongetInnerEquations(bsonFile, eqNumber)Arguments
bsonFile::String: name of the binary JSON fileeqNumber::Int: number of the slowest equation
Return:
- Array of inner equation indices
NonLinearSystemNeuralNetworkFMU.getMinMax — FunctiongetMinMax(bsonFile, eqNumber, inputArray)Arguments
bsonFile::String: name of the binary JSON fileeqNumber::Int: number of the slowest equationinputArray::Vector{String}: array of input variables as String
Return:
- array of the min and max values of each input from input array
getMinMax(bsonFile, eqNumber, inputArray)Arguments
bsonFile::String: name of the binary JSON fileeqNumber::Int: number of the slowest equationinputArray::Vector{Int}: array of input variables as Integers
Return:
- array of the min and max values of each input from input array
Structures
NonLinearSystemNeuralNetworkFMU.OMOptions — TypeOMOptions <: AnySettings 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 inworkingDirwhen set totrue.commandLineOptions::String: Additional comannd line options forsetCommandLineOptions.simFlags::String: Additional simulation flags.
NonLinearSystemNeuralNetworkFMU.ProfilingInfo — TypeProfilingInfo <: AnyProfiling information for single non-linear equation.
eqInfo::EqInfo: Non-linear equationiterationVariables::Array{String}: Iteration (output) variables of non-linear systeminnerEquations::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 ofusingVars.
NonLinearSystemNeuralNetworkFMU.EqInfo — TypeEqInfo <: AnyEquation info struct.
id::Int64: Unique equation idncall::Int64: Number of calls during simulationtime::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.
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 NonLinearSystemNeuralNetworkFMUjulia> 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.95julia> 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 BSONjulia> BSON.@save "simpleLoop.bson" profilingInfojulia> 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]))