# Preparing models for use in Real-Time

A “Real-Time” function exists in the Claytex Library to ease the simulation of a model in real-time. This function allows to set number flags that are available in Dymola for inline integration and to improve performance for real-time simulation.

Figure 1: Real-time function from the Claytex library

Inline integration is a combined symbolic and numeric approach to solving differential-algebraic equations systems. Discretization expressions representing the numerical integration algorithm are symbolically inserted into the differential algebraic equation model. The symbolic power of the Dymola translator is then exploited to transform the equations into a representation that is efficient for numeric simulation. The idea of inline integration is to let the Dymola translator manipulate the discretized problem to reduce and simplify the problem that has to be solved numerically at each step. A number of inline integration methods for real-time simulation can be used in Dymola, either as settings or as a built-in variable.

Table 1: Choices of inline integration methods and associated values

The implicit Runge-Kutta order can be chosen between 2 and 4.

General options:

• Advanced.InlineMethod: Inline integration method as seen above.
• Advanced.InlineOrder: Runge-Kutta integration order as seen above.
• Advanced.InlineFixedStep: Pre-set step size for optimization of inline method.

Options for non-linear solvers:

• Advanced.Define.AimForHighAccuracy: Normally the non-linear systems of equations are solved with additional accuracy. That additional accuracy not only improves the accuracy of the simulation itself but also avoids spurious events. Setting the flag to false can thus cause loss of accuracy and spurious events.
• Advanced.Define.NonLinearIterations: Limits the number of non-linear iterations of the non-linear systems of equations. In combination with Advanced.Define.NewJacobian=false, this improves performance substantially but manual verification of the result is needed. The default of zero ensures that iterations are until convergence. Values 0, 2-8 are supported but values 6-8 are not recommended; but the result will be better using value 0 in such cases.
• Advanced.Define.NewJacobian: This avoids recomputing the Jacobian from scratch at every time step. The problem with keeping the Jacobian from the previous step is that at certain points major updates of the Jacobian are needed. Thus even if keeping the Jacobian is on average better, it is not clear that it will improve real-time performance.

Profiling of execution time:

• Advanced.PrecisionTiming: Set this flag to enable precision timing. This functionality uses the frequency counter of the processor to provide high-resolution time measurements.
• Advanced.DymosimRealTimePriority: Use this flag to enable profiling of execution time for a simulation. Be aware that processes of this class may interrupt system threads managing mouse or keyboard and that they may also be difficult to terminate.

Options for implicit inline integration:

• Advanced.InterpolateStatesForInline: In case of an event we will take a smaller step to step past the event point. This shorter step would normally slow down the non-linear solver but this flag changes it so that we first compute as for a normal step and then only scale the states. This avoids the loss of efficiency. This is important to set for implicit inline integration.
• Advanced.InlineIntegrationJacobian: This is only useful if Advanced.Define.NewJacobian is false, and basically avoids generating the large amount of code needed for a Jacobian that is only needed at the initial step.

Other options:

• Advanced.GenerateBlockTimers: Generates profiling timers for block of C-code.
• Advanced.GenerateTimers: Generates profiling timers in C-code.
• Advanced.AutoRemoveAuxiliaries: Removes code for auxiliary variables that neither influence the simulation state nor the outputs. This improves performance a bit. If the auxiliary code is used to assert correct behaviour or to generate external outputs, that code will not be run.
• Advanced.SubstituteVariablesUsedOnce: Substitutes variables used only once.
• Advanced.UseModelicaNamesForC: Uses Modelica variable names in dsmodel.c
• Advanced.Evaluate: Evaluates the code based on the given parameter values (excluding top-level parameters) preventing further modifications.
• Advanced.GenerateXMLInterface: Outputs interface description in XML.

Options not in the real-time function

• Advanced.Define.GlobalOptimizations: This flag works the same for all Visual Studio compilers. Note that the simulation performance is somewhat improved by setting the flag but the compilation of the code might take substantially longer for large models. The setting corresponds to the compiler command /Og. Default value is 0.
• Advanced.InlineAsDiscrete: Normally the inlined block is run based on the sampling time. With this setting the sampling time for this block is given by the fixed-size setting of the Realtime tab (Advanced.InlineFixedStep as described above). If Advanced.InlineAsDiscrete is set and the fixed step-size is set at the default zero, the block will have inherited sampling time in Simulink.
• Advanced.BreakDelayLoops: Use this flag to break loops involving the delay The delay operator is normally treated as if there were a direct coupling between the input and the output. Be aware that setting of this flag may lead to wrong simulation results if the delay times are too short.
• Advanced.Define.UseRDTSCFrequency: This variable is used to specify the frequency (in Hz) of the processor. It is recommended to keep it at the default value of 0.0 (auto detection), which means that the frequency is automatically read from the text description of the processor.
• Advanced.EfficientMinorEvents: This enables a better handling of minor events (i.e. events that do not influence the continuous model at all or events that do not require a new Jacobian). The efficient event handling is not approximate in itself, but due to fewer Jacobians and slight changes in the simulation intervals for the numerical solvers, the results may change within the tolerance.

Since these options are not currently available through the real-time function, they will need to be set manually.

Note for dSpace: If the model has to be later used in dSpace, we need to make sure it is not parameterized using any external file as dSpace does not accept data files.

Written by: Romain Gillot  – Project Engineer