Internal
This chapter documents internal functions that are typically only for use of the developers of package TinyModia.
Code Generation
This section provides functions to generate Julia code of the transformed equations.
TinyModia.SimulationModel
— TypesimulationModel = SimulationModel{FloatType}(
modelModule, modelName, getDerivatives!, equationInfo, x_startValues,
parameters, variableNames;
modelModule = nothing,
vSolvedWithInitValuesAndUnit::OrderedDict{String,Any}(),
vEliminated::Vector{Int}=Int[],
vProperty::Vector{Int}=Int[],
var_name::Function = v->nothing)
Arguments
modelModule
: Module in which@instantiateModel
is invoked (it is used forCore.eval(modelModule, ...)
), that is evaluation of expressions in the environment of the user.modelName::String
: Name of the modelgetDerivatives::Function
: Function that is used to evaluate the model equations, typically generated with [TinyModia.generate_getDerivatives!
].equationInfo::ModiaBase.EquationInfo
: Information about the states and the equations.x_startValues
:: Deprecated (is no longer used).parameters
: A hierarchical NamedTuple of (key, value) pairs defining the parameter and init/start values.- variableNames: A vector of variable names. A name can be a Symbol or a String.
TinyModia.generate_getDerivatives!
— Functioncode = generate_getDerivatives!(AST, equationInfo, parameters, variables, functionName;
hasUnits=false)
Return the code of the getDerivatives!
function as Expr
using the Symbol functionName
as function name. By eval(code)
or fc = @RuntimeGeneratedFunction(code)
the function is compiled and can afterwards be called.
Arguments
AST::Vector{Expr}
: Abstract Syntax Tree of the equations as vector ofExpr
.equationInfo::ModiaBase.EquationInfo
: Data structure returned by `ModiaBase.getSortedAndSolvedAST holding information about the states.parameters
: Vector of parameter names (as vector of symbols)variables
: Vector of variable names (as vector of symbols). The first entry is expected to be time, sovariables[1] = :time
.functionName::Function
: The name of the function that shall be generated.
Optional Arguments
hasUnits::Bool
: = true, if variables have units. Note, the units of the state vector are defined in equationinfo.
TinyModia.init!
— Functionsuccess = init!(simulationModel, startTime, tolerance, merge, log, logParameters, logStates)
Initialize simulationModel::SimulationModel
at startTime
. In particular:
Empty result data structure.
Merge parameter and init/start values into simulationModel.
Construct x_start.
Call simulationModel.getDerivatives! once with isInitial = true to compute and store all variables in the result data structure at
startTime
and initialize simulationModel.linearEquations.Check whether explicitly solved variables that have init-values defined, have the required value after initialization (-> otherwise error).
If initialization is successful return true, otherwise false.
TinyModia.outputs!
— Functionoutputs!(x, t, integrator)
DifferentialEquations FunctionCallingCallback function for SimulationModel
that is used to store results at communication points.
TinyModia.addToResult!
— FunctionaddToResult!(simulationModel, variableValues...)
Add variableValues...
to simulationModel::SimulationModel
. It is assumed that the first variable in variableValues
is time
.
TinyModia.getFloatType
— FunctionfloatType = getFloatType(simulationModel::SimulationModel)
Return the floating point type with which simulationModel
is parameterized (for example returns: Float64, Float32, DoubleFloat, Measurements.Measurement{Float64}
).
TinyModia.baseType
— FunctionbaseType(T)
Return the base type of a type T.
Examples
baseType(Float32) # Float32
baseType(Measurement{Float64}) # Float64
TinyModia.measurementToString
— Functionstr = measurementToString(v)
Return variable v::Measurements.Measurement{FloatType}
or a vector of such variables in form of a string will the full number of significant digits.