# Modelling and Simulation of Physical Systems with Dynamically Changing Degrees of Freedom

^{*}

## Abstract

**:**

## 1. Introduction

- The sizes of array equations can be changed after code generation. A simple example is shown in Section 2, where the parameter matrices of an LTI (Linear Time-Invariant) system can still be provided with different dimensions after generation and compilation of code.
- Built-in model components are introduced that scale for large systems because the component equations are split into a (usually) large part that is encoded in a small set of pre-compiled functions and into a (usually) small part in form of standard equations which is processed with the entire model. A simple example is shown in Appendix B.1, where the core part of a discretized partial differential equation is present in pre-compiled functions and the (acausal) component is defined with four scalar equations that are independent of the discretization.
- Variables and equations of built-in model components can appear and disappear during simulation, without re-generation and re-compilation of code, provided these variables and equations are part of the pre-compiled functions. Simple examples are shown in Appendix B, where (a) the number of volumes of a discretized partial differential equation can be changed after generation and compilation of code, and (b) the number of equations and states of a two-stage rocket are changed during simulation due to the separation of stages.

## 2. Changing Number of States after Model Translation

Listing 1. An array equation must be defined for an array variable and an array must be declared
with an init or start array value when its sizes cannot be inferred. |

`init`or

`start`attributes can be changed in Modia after code generation, provided they are not defined as static arrays. Some examples are given in Listing 2.

Listing 2. Examples of array equations. The sizes of the statically sized arrays A1, y1 cannot be changed after compilation. The sizes of arrays A1, y1 can be changed after compilation. |

`A1`,

`y1`are statically sized arrays, and their dimensions cannot be changed after

`@instantiateModel(..)`has been called. In contrast,

`A2,y2`are standard Julia arrays, and their dimensions can be changed with the

`merge`attribute of the

`simulate!(..)`command after compilation of the generated

`getDerivatives`function.

`getDerivatives`, the statically sized state vector

`y1`is always newly generated by utilizing the corresponding elements from the model state vector

`_x`in the

`SVector`constructor. This constructor allocates memory on the stack, and operations on

`y1`are efficiently implemented in package

`StaticArrays`. New arrays needed in calculations are automatically constructed again on the stack (which is an efficient operation). For example, Julia’s multiple dispatch feature will deduce at compile time, that

`A1 * y1`is a static array (because

`A1`and

`y1`are static arrays and the operator * is overloaded to return a static array), and therefore,

`var"der(y1)"`is generated on the stack as a static array.

`_m.x_vec[1]`is allocated for the state vector

`y2`whenever the

`merge`-attribute has been processed. Before function

`getDerivatives`is called, the corresponding elements of the model state vector

`_x`are copied into

`_m.x_vec[1]`, and this vector is then accessed with the alias name

`y2`due to

`y2 =_m.x_vec[1]`(

`y2`is a reference to

`_m.x_vec[1]`). The generated array equations’ code does not depend on the array sizes of the involved variables. The drawback of non-static arrays is that intermediate computations, and left-hand side variables such as

`var"der(y2)"`, allocate new memory on the heap, whenever the corresponding statements are executed. If there are many such statements, this can reduce the efficiency of the simulation. In the next section, built-in components are introduced that do not have this drawback. Arrays used in functions of built-in components operate on memory that is allocated once on the heap and not in every model evaluation.

## 3. Built-In Components

#### 3.1. Acausal Components

`equation`section of a Modelica model or the equations in the quoted vector that are assigned to variable

`equations`in a Modia model. It can be connected with other components via inputs

**u**, outputs

**y**, and connectors containing pairs of potential ${\mathit{c}}_{p}$ and flow variables ${\mathit{c}}_{f}$. As usual, when connecting potential and flow variables via connectors, the corresponding potential variables are set equal, and the sum of the corresponding flow variables is set to zero. It is required that a connector has only equal pairs of potential and flow variables to ensure that any connection of acausal components is globally balanced. In other words, the number of equations and the number of unknowns of any set of connected components is equal, provided every component is locally balanced. A component is called locally balanced if $dim\left({\mathit{c}}_{p}\right)=dim\left({\mathit{c}}_{f}\right)={n}_{c}$ and $dim\left({\mathit{f}}_{c}\right)={n}_{y}+{n}_{c}+{n}_{w}+{n}_{z}$. For details, see [21].

**x**are supplied to the model by the solver. Using the symbolically processed equations, the derivative of the states $\dot{\mathit{x}}$ and the event indicators

**z**are calculated and returned to the solver. Hereby, linear and/or non-linear algebraic equation systems might be solved within the model; see, e.g., [1] Appendix B.

#### 3.2. Acausal Components with Pre-Translated Mathematical Functions

**u**and

**y**would be arguments of ${\mathit{g}}_{c}$ too. However, this results usually in a larger acausal part. Sorting the following equations,

#### 3.3. Acausal Built-In Components

**x**and the output variables ${\dot{{\mathit{x}}}}_{1},{{\mathit{z}}}_{1},{{\mathit{w}}}_{1}$ of function ${\mathit{f}}_{c,1}$ are (possibly) present in ${\mathit{f}}_{c,\mathrm{eq}}$, as seen in Figure 2. It would then not be possible to add or remove these variables during simulation without re-translation. In such a case, re-formulations are needed, e.g., by computing some part of the expressions present in ${\mathit{f}}_{c,\mathrm{eq}}$ in a function that has the memory m as argument, so that variables ${\dot{{\mathit{x}}}}_{1},{{\mathit{z}}}_{1},{{\mathit{w}}}_{1},{{\mathit{x}}}_{1},{{\mathit{x}}}_{2}$ are no longer visible in ${\mathit{f}}_{c,\mathrm{eq}}$. It might also be necessary to hide some of these variables in (new) local, algebraic variables ${\mathit{w}}_{3}$ that are returned from ${\mathit{f}}_{c,1}$ and used in ${\mathit{f}}_{c,\mathrm{eq}}$; see Figure 3.

#### 3.4. Application: Capacitor

## 4. Changing the Number of States during Simulation

**x**and the vector of event indicators

**z**) are split into an invariant and a variant part: $\mathit{x}=({\mathit{x}}^{\mathrm{inv}},{\mathit{x}}^{\mathrm{var}})$ and $\mathit{z}=({\mathit{z}}^{\mathrm{inv}},{\mathit{z}}^{\mathrm{var}})$. The dimensions of the invariant parts were fixed before the simulation starts. The dimensions of the variant parts can change during events in the simulation. The variables of a model are characterized by the following attributes:

- Invariance (inv): Variable name, type, and number of dimensions are fixed before translation. The dimensions of an invariant variable (e.g., length of a vector) can be changed before simulation starts. The solver provides vector ${\mathit{x}}^{\mathrm{inv}}$ to the model function that contains the sorted and solved equations. The elements of ${\mathit{x}}^{\mathrm{inv}}$ are copied into the elements of the invariant state variables of the model. The computed derivatives of the invariant state variables are copied into vector ${\dot{\mathit{x}}}^{\mathrm{inv}}$ and the computed invariant event indicators are copied into ${\mathit{z}}^{\mathrm{inv}}$ before the model’s function returns. All variables defined and used in an equation section are invariant variables. This includes all input/output arguments of the called functions.
- Variant (var): Variables can appear and existing variables can disappear during events. The type and the number of dimensions of a variant variable cannot be changed after it is first introduced in a simulation run. However, its dimensions (e.g., the length of a vector) can be changed at event initiation. All model variables defined inside functions of built-in components are variant variables. Before a variant state variable of the model is used in a function of a built-in component, its elements are retrieved from vector ${\mathit{x}}^{\mathrm{var}}$ provided by the solver. After the derivatives of variant state variables of a model are computed, they are copied into vector ${\dot{\mathit{x}}}^{\mathrm{var}}$ provided by the solver.

`missing`values. This format was developed and evaluated with the open source Julia package SignalTables (https://github.com/ModiaSim/SignalTables.jl, accessed on 12 December 2022) and is used in Modia.

`Model`that defines the interface and the equations of the built-in component. This data structure and the functions to evaluate it are called the execution scheme in Figure 7.

## 5. Segmented Simulation with Built-In Component Modia3D

#### 5.1. Overview Modia3D

`Scene`is the root of all other Object3Ds and defines a global inertial system. The feature

`Visual`is for 3D animation and defines shapes such as box, sphere, cylinder, beam, and 3D meshes with visualization properties. The feature

`Solid`defines solid bodies. It has mass properties and can be considered in collision situations if

`collision = true`is set. It can have a shape and visualization properties. For a more extended description, see [4] and the Modia3D Tutorial (https://modiasim.github.io/Modia3D.jl/stable/, accessed on 11 December 2022).

`Damper`is connected to a fixed point and to the flange of the revolute joint, see Listing 4, to model damping in the joint.

Listing 3. Simple damped pendulum defined with constructors of Modia3D (Object3D, Revolute-WithFlange) and equation based components of Modia (Damper, Fixed). |

Listing 4. Definition of a Modia3D revolute joint containing a Modia 1D rotational flange that can be
connected with Modia components. |

`RevoluteWithFlange`joint (a revolute joint that has a Modia 1D rotational

`Flange`) is shown in Listing 4; for more details, see [4]. During the instantiation of an overall model, the model is traversed, and each built-in component can inject equations into the model definition that is used during symbolic processing. As a result, in Listing 5, the code generated for the pendulum of Listing 3 is shown.

Listing 5. Generated code for pendulum of Listing 3. |

`_x`of the solver are copied into the Modia variables

`phi`and

`w`of the revolute joint which are in turn passed to function

`Modia3D.setStatesRevolute!(..)`of the Modia3D built-in component.

`fixedToParent`with a default value of true. In this case, the Object3D is rigidly connected to its parent Object3D; this means it has zero degrees of freedom. If the value is set to false, the Object3D is allowed to move freely with respect to its parent, meaning it has 6 degrees of freedom and 12 variant states. At events, keyword

`fixedToParent`can be changed from

`false`to

`true`and vice versa, as will be shown below.

#### 5.2. Super-Objects

#### 5.3. Segmented Simulation of Modia3D Models

`assemblyRoot = true`for the freely moving Object3D, i.e.,

`fixedToParent = false`. All rigidly connected children of such an Object3D belong to the assembly. Additionally, any Object3D, whether it is part of an assembly or not, can be a locking mechanism by setting

`lockable = true`in the Object3D constructor. In Figure 9, segment 1, superObject5 is an assembly because obj9 is marked as an assembly root. This assembly is able to interact with superObject2 because obj10 of the assembly and obj9 of superObject2 have

`lockable = true`defined.

`modelProgram`). This function is passed as

`actions`argument to the Modia3D constructor

`ModelActions`that returns a reference (e.g., currentAction) to an internal data structure. This data structure is passed to executeActions, which is called in a Modia

`equations`section.

Listing 6. Defining actions for a Modia3D built-in model with commands of Table 2 and Table 3 to interact
with Modia. |

`ActionAttach, ActionReleaseAndAttach`) are only possible if the referenced lockable Object3Ds are close together and the relative velocity and angular velocity are close to zero. Currently, the following cases are treated:

- A freely moving assembly is rigidly connected to an Object3D with
`ActionAttach`. This action reduces the number of degrees of freedom by 6. - If an assembly has at least two lockable Object3Ds (objA, objB) and is rigidly connected via objA, this rigid connection is removed, and another rigid connection via objB is introduced with
`ActionReleaseAndAttach`. This action does not affect the number of degrees of freedom but changes the structure of the super-objects. - A rigidly connected assembly, i.e., rigid connection to an Object3D, is unlocked with
`ActionRelease`to get a freely moving assembly. This action increases the number of degrees of freedom by six. - An assembly that is either freely moving or is rigidly connected to an Object3D is deleted with
`ActionDelete`. All Object3Ds of this assembly are removed from the Modia3D model.

`ActionAttach(..., obj10, obj2),`see Table 2, provided both lockable Object3Ds (obj2 and obj10) are close to each other. Hereby, the joint connecting obj9 with obj5 is removed and obj10 is rigidly connected to obj2. The re-instantiation reduces the number of super objects and states and results in the execution scheme of Figure 9 (segment 2).

## 6. Applications

#### 6.1. Two-Stage Rocket

`RocketStage`consisting of a cylinder with lockable Object3Ds at the top and at the bottom and a thrust at the bottom. Model

`TwoStageRocket`builds up the rocket system with the

`world`object, two instances of

`RocketStage`, and a

`ModelActions`constructor that defines the actions with function rocketProgram:

- Initially, the two stages are not rigidly connected. At initialization, the top of stage1 and the bottom of stage2 are attached with
`ActionAttach(actions, "stage1.top", "stage2.bottom")`; see the visualization in the left part of Figure 10. - An event is triggered after 5 s with
`EventAfterPeriod(actions, 5)`to release`state1.top`from`state2.bottom`with`ActionRelease(actions, "stage1.top")`. This separates the two stages. Furthermore, thrust1 is switched off. - Again, an event is triggered after 5 s with
`EventAfterPeriod(actions, 5)`; see the right part of Figure 10. Since the movement of stage1 is no longer of interest in this scenario,`state1.top`and all Object3Ds connected to it are deleted with`ActionDelete(actions, "stage1.top")`. Thus, stage1 is removed from the simulation run.

Listing 7. Modia3D model of a simple two stage rocket. The stages are instances of sub-model
RocketStage that is a cylinder of length L, diameter d, mass m, with lockable Object3Ds at the top and
at the bottom, and a thrust at the bottom. |

#### 6.2. Gripping Robot

Listing 8. Action definitions for the KUKA YouBot robot. The transportation procedure starts with
initializing a new reference path for driving 5 arm angles and the gripper itself, with unique names,
initial positions, and maximum angular velocity v_{max}, and maximum angular acceleration a_{max} of
the revolute joints. The arms and gripper are driven to the given points to rigidly attach the resting
sphere with the gripper. The robot transports the rigidly attached sphere until it is released, falls
freely and bounces on the plate. |

## 7. Conclusions

## Author Contributions

## Funding

## Data Availability Statement

## Conflicts of Interest

## Abbreviations

3D | three dimensional |

AST | abstract syntax tree |

DAE | differential algebraic equation |

DoF | degrees of freedom |

FR | full restart |

MDPI | Multidisciplinary Digital Publishing Institute |

MPR | Minkowski portal refinement |

ODE | ordinary differential equation |

PDE | partial differential equation |

inv | invariant |

var | variant |

## Appendix A. Overview of Modia

#### Appendix A.1. Variables and Models

`Var`allows one to define variables with attributes.

`Var`is a function taking name/value pairs, building and returning a corresponding dictionary. The currently introduced attributes are:

`value; min; max; init; start;`and the Booleans:

`parameter, constant, input, output, potential,`and

`flow`.

`Par`is an abbreviation for

`Var(parameter=true)`. Example:

`:( )`. A parameter can also be defined by

`name = literal-value`.

`Model`.

Listing A1. Syntax of a Modia model. |

`equations`is quoted, since it is enclosed in

`:[ ]`. This enables later processing, such as symbolically solving the equation, since an AST is built-up, instead of evaluating the expressions. The Modia-specific operator

`der(v)`defines the time derivative $dv/dt$ of variable v. Definition of units is done with a string macro

`u"..."`from Julia package Unitful.jl [34]. For example, a low pass filter can be defined as in Listing A2.

Listing A2. Modia model of a low pass filter. |

Listing A3. Modelica model of a low pass filter. |

#### Appendix A.2. Connectors and Components

`flow = true`) are considered connectors. Connectors must have an equal number of flow and potential variables (with attribute

`potential = true`) and have matching array sizes. Connectors may not have any equations. An electrical connector with potential

`v`and current

`i`is defined as:

`p`and

`n`can be described as in Listing A4.

Listing A4. Capacitor model. |

#### Appendix A.3. Merging

`Map`is used for that. For example, modifying the parameter

`T`of the

`LowPassFilter`model defined in Listing A2 can be implemented by:

`|`is an overloaded binary operator of bitwise or with recursive merge semantics. Merging of

`equations`is handled in a special way by concatenating the equations vectors.

#### Appendix A.4. Connections

Listing A5. Filter model defined with electrical components is instantiated, simulated and plotted. |

`@instantiateModel`symbolically processes the model, generates, and compiles Julia code. Symbolic processing is performed with standard algorithms of object-oriented modelling languages and with extensions described in [35]. Function

`simulate!`performs one simulation of the instantiated model with a solver from the Julia package DifferentialEquations.jl [36,37]. This package contains a large set of solvers. In Listing A5, the Modia default solver

`CVODE_BDF`is used. With various keyword arguments, the simulation run can be defined, e.g., the stop time is set to 2 s. Parameters and initial values can be provided by a hierarchical

`Map`that is merged with the current values via the

`merge`keyword. The simulation results are stored within the instantiated model and are plotted with function call

`plot`.

## Appendix B. Examples

#### Appendix B.1. Heated Rod with Acausal Built-In Component

**Figure A1.**Space discretized partial differential equation of one-dimensional heat transfer in a rod with an isolated surface, defined with parameters L (length of rod), n (number of volumes), A (area), $\varrho $ (density), c (specific heat capacity), $\lambda $ (thermal conductivity), ${T}_{0}$ (initial value in every volume), states ${T}_{i}$ (temperatures in the middle of every volume), thermal connectors $a,b$ with potential variables ${a}_{T},{b}_{T}$ (temperatures), and flow variables ${a}_{{Q}_{\mathrm{flow}}},{b}_{{Q}_{\mathrm{flow}}}$ (heat flow rates).

`InsulatedRod`of the rod that is connected at its left thermal connector a with a fixed temperature source

`FixedTemperature`and at its right thermal connector b with a fixed heat-flow source

`fixedHeatFlow`with the default zero heat-flow rate (so the rod is totally insulated on the right side and has a fixed temperature on the left side).

Listing A6. Simple usage of isolated rod InsulatedRod with one-dimensional heat-transfer that is
connected on the left side with a fixed temperature source FixedHeatFlow with T = 220 °C = 493.15 K,
and on the right side with a fixed heat flow source FixedHeatFlow with Q_{flow} = 0. |

`@instantiateModel(HeatedRod)`symbolically processes this model, and generates and translates Julia code. The

`simulate!(..)`statement changes the discretization (and therefore, the dimension of the temperature vector

`T`) from 5 to 8 volumes before simulation starts (and without a new translation). With function

`plot(heatedRod, ...)`, the plot of Figure A2 is generated, showing the temperatures at the temperature source and in the rod volumes. Two different implementations of the

`InsulatedRod`model are shown in Table A1 in the form of pseudo-code. In the left column, the model’s implementation with mathematical functions is shown. In the right column, the model’s implementation is shown as a built-in component using functions of a programming language that have internal memory.

**Table A1.**Built-in component of an insulated rod with parameters $L,n,A,\varrho ,\lambda ,c,{T}_{0}$, state vector

**T**of length n (temperatures at rod volumes), connectors $a,b$ on the left and right sides of the rod with potential variables ${a}_{T},{b}_{T}$ (temperatures), and flow variables ${a}_{{Q}_{\mathrm{flow}}},{b}_{{Q}_{\mathrm{flow}}}$ (heat flow rates).

as component | as built-in component |
---|---|

(with mathematical functions) | (with prog. lang. functions that have internal memory m) |

evaluated once | |

$\begin{array}{cc}\hfill dx& =L/n\hfill \\ \hfill {k}_{1}& =(\lambda /dx)/\left(c\phantom{\rule{0.166667em}{0ex}}\xb7\phantom{\rule{0.166667em}{0ex}}\varrho \phantom{\rule{0.166667em}{0ex}}\xb7\phantom{\rule{0.166667em}{0ex}}dx\right)\hfill \\ \hfill {k}_{2}& =2\phantom{\rule{0.166667em}{0ex}}\xb7\phantom{\rule{0.166667em}{0ex}}\lambda \phantom{\rule{0.166667em}{0ex}}\xb7\phantom{\rule{0.166667em}{0ex}}A/dx\hfill \end{array}$ | $m={f}_{c,0}(\mathrm{sim},\mathrm{id},L,n,A,\varrho ,\lambda ,c,{T}_{0})$ |

equation section | |

$\begin{array}{cc}\hfill ({T}_{1},{T}_{n})& ={f}_{c,1}\left({\mathit{T}}\right)\hfill \\ \hfill {a}_{{Q}_{\mathrm{flow}}}& ={k}_{2}\phantom{\rule{0.166667em}{0ex}}\xb7\phantom{\rule{0.166667em}{0ex}}({a}_{T}-{T}_{1})\hfill \\ \hfill {b}_{{Q}_{\mathrm{flow}}}& ={k}_{2}\phantom{\rule{0.166667em}{0ex}}\xb7\phantom{\rule{0.166667em}{0ex}}({b}_{T}-{T}_{n})\hfill \\ \hfill \dot{{\mathit{T}}}& ={f}_{c,2}({\mathit{T}},{{k}}_{{1}}{,}{a}_{T},{b}_{T})\hfill \end{array}$ | $\begin{array}{cc}\hfill ({k}_{2},{T}_{1},{T}_{n})=\phantom{\rule{3.33333pt}{0ex}}& {f}_{c,1}\left(m\right)\hfill \\ \hfill {a}_{{Q}_{\mathrm{flow}}}=\phantom{\rule{3.33333pt}{0ex}}& {k}_{2}\phantom{\rule{0.166667em}{0ex}}\xb7\phantom{\rule{0.166667em}{0ex}}({a}_{T}-{T}_{1})\hfill \\ \hfill {b}_{{Q}_{\mathrm{flow}}}=\phantom{\rule{3.33333pt}{0ex}}& {k}_{2}\phantom{\rule{0.166667em}{0ex}}\xb7\phantom{\rule{0.166667em}{0ex}}({b}_{T}-{T}_{n})\hfill \\ & {f}_{c,2}(m,{a}_{T},{b}_{T})\phantom{\rule{3.33333pt}{0ex}}\phantom{\rule{3.33333pt}{0ex}}\#\phantom{\rule{3.33333pt}{0ex}}\mathrm{no}\phantom{\rule{4.pt}{0ex}}\mathrm{return}\phantom{\rule{4.pt}{0ex}}\mathrm{arguments}\hfill \end{array}$ |

function definitions | |

$\begin{array}{cc}& \mathbf{function}\phantom{\rule{3.33333pt}{0ex}}{f}_{c,0}(\mathrm{sim},\mathrm{id},L,n,A,\varrho ,\lambda ,c,{T}_{0})\hfill \\ & \phantom{\rule{3.33333pt}{0ex}}\phantom{\rule{3.33333pt}{0ex}}\phantom{\rule{3.33333pt}{0ex}}\phantom{\rule{3.33333pt}{0ex}}<\mathrm{allocate}\phantom{\rule{4.pt}{0ex}}\mathrm{new}\phantom{\rule{4.pt}{0ex}}\mathrm{record}\phantom{\rule{4.pt}{0ex}}m>;\phantom{\rule{3.33333pt}{0ex}}\phantom{\rule{3.33333pt}{0ex}}\phantom{\rule{3.33333pt}{0ex}}\phantom{\rule{3.33333pt}{0ex}}\mathrm{assert}(n\ge 2)\hfill \\ & \phantom{\rule{3.33333pt}{0ex}}\phantom{\rule{3.33333pt}{0ex}}\phantom{\rule{3.33333pt}{0ex}}\phantom{\rule{3.33333pt}{0ex}}{m}_{\mathrm{sim}}:=\mathrm{sim};\phantom{\rule{3.33333pt}{0ex}}{m}_{\mathrm{id}}:=\mathrm{id};\phantom{\rule{3.33333pt}{0ex}}{m}_{n}:=n;\phantom{\rule{3.33333pt}{0ex}}\Delta x:=L/n\hfill \\ & \phantom{\rule{3.33333pt}{0ex}}\phantom{\rule{3.33333pt}{0ex}}\phantom{\rule{3.33333pt}{0ex}}\phantom{\rule{3.33333pt}{0ex}}{m}_{{k}_{1}}:=(\lambda /\Delta x)/\left(c\phantom{\rule{0.166667em}{0ex}}\xb7\phantom{\rule{0.166667em}{0ex}}\varrho \phantom{\rule{0.166667em}{0ex}}\xb7\phantom{\rule{0.166667em}{0ex}}\Delta x\right)\hfill \\ & \phantom{\rule{3.33333pt}{0ex}}\phantom{\rule{3.33333pt}{0ex}}\phantom{\rule{3.33333pt}{0ex}}\phantom{\rule{3.33333pt}{0ex}}{m}_{{k}_{2}}:=2\phantom{\rule{0.166667em}{0ex}}\xb7\phantom{\rule{0.166667em}{0ex}}\lambda \phantom{\rule{0.166667em}{0ex}}\xb7\phantom{\rule{0.166667em}{0ex}}A/\Delta x;\phantom{\rule{3.33333pt}{0ex}}\phantom{\rule{3.33333pt}{0ex}}\phantom{\rule{3.33333pt}{0ex}}\phantom{\rule{3.33333pt}{0ex}}\mathbf{return}\phantom{\rule{3.33333pt}{0ex}}m\hfill \end{array}$ | |

$\begin{array}{cc}& \mathbf{function}\phantom{\rule{3.33333pt}{0ex}}{f}_{c,1}\left({\mathit{T}}\right)\hfill \\ & \phantom{\rule{3.33333pt}{0ex}}\phantom{\rule{3.33333pt}{0ex}}\phantom{\rule{3.33333pt}{0ex}}\phantom{\rule{3.33333pt}{0ex}}\mathbf{return}\phantom{\rule{3.33333pt}{0ex}}({T}_{1},{T}_{n})\hfill \end{array}$ | $\begin{array}{cc}& \mathbf{function}\phantom{\rule{3.33333pt}{0ex}}{f}_{c,1}\left(m\right)\hfill \\ & \phantom{\rule{3.33333pt}{0ex}}\phantom{\rule{3.33333pt}{0ex}}\phantom{\rule{3.33333pt}{0ex}}\phantom{\rule{3.33333pt}{0ex}}<\mathrm{copy}\phantom{\rule{4.pt}{0ex}}{m}_{\mathit{T}}\phantom{\rule{4.pt}{0ex}}\mathrm{from}\phantom{\rule{4.pt}{0ex}}\mathrm{states}\phantom{\rule{4.pt}{0ex}}\mathrm{in}\phantom{\rule{4.pt}{0ex}}{m}_{\mathrm{sim}}\phantom{\rule{4.pt}{0ex}}\mathrm{for}\phantom{\rule{4.pt}{0ex}}{m}_{\mathrm{id}}>\hfill \\ & \phantom{\rule{3.33333pt}{0ex}}\phantom{\rule{3.33333pt}{0ex}}\phantom{\rule{3.33333pt}{0ex}}\phantom{\rule{3.33333pt}{0ex}}\mathbf{return}\phantom{\rule{3.33333pt}{0ex}}({m}_{{k}_{2}},{m}_{{T}_{1}},{m}_{{T}_{n}})\hfill \end{array}$ |

$\begin{array}{cc}& \mathbf{function}\phantom{\rule{3.33333pt}{0ex}}{f}_{c,2}({\mathit{T}}{,}{{k}}_{{1}}{,}{a}_{T},{b}_{T})\hfill \\ & \phantom{\rule{3.33333pt}{0ex}}\phantom{\rule{3.33333pt}{0ex}}\phantom{\rule{3.33333pt}{0ex}}\phantom{\rule{3.33333pt}{0ex}}{\dot{T}}_{1}:={k}_{1}\phantom{\rule{0.166667em}{0ex}}\xb7\phantom{\rule{0.166667em}{0ex}}\left(2\phantom{\rule{0.166667em}{0ex}}\xb7\phantom{\rule{0.166667em}{0ex}}({a}_{T}-{T}_{1})-({T}_{1}-{T}_{2})\right)\hfill \\ & \phantom{\rule{3.33333pt}{0ex}}\phantom{\rule{3.33333pt}{0ex}}\phantom{\rule{3.33333pt}{0ex}}\phantom{\rule{3.33333pt}{0ex}}{\dot{T}}_{n}:={k}_{1}\phantom{\rule{0.166667em}{0ex}}\xb7\phantom{\rule{0.166667em}{0ex}}\left({T}_{n-1}-{T}_{n}-2\phantom{\rule{0.166667em}{0ex}}\xb7\phantom{\rule{0.166667em}{0ex}}({T}_{n}-{b}_{T})\right)\hfill \\ & \phantom{\rule{3.33333pt}{0ex}}\phantom{\rule{3.33333pt}{0ex}}\phantom{\rule{3.33333pt}{0ex}}\phantom{\rule{3.33333pt}{0ex}}\mathbf{for}\phantom{\rule{3.33333pt}{0ex}}i=2:n-1\hfill \\ & \phantom{\rule{3.33333pt}{0ex}}\phantom{\rule{3.33333pt}{0ex}}\phantom{\rule{3.33333pt}{0ex}}\phantom{\rule{3.33333pt}{0ex}}\phantom{\rule{3.33333pt}{0ex}}\phantom{\rule{3.33333pt}{0ex}}\phantom{\rule{3.33333pt}{0ex}}\phantom{\rule{3.33333pt}{0ex}}{\dot{T}}_{i}:={k}_{1}\phantom{\rule{0.166667em}{0ex}}\xb7\phantom{\rule{0.166667em}{0ex}}\left({T}_{i+1}-{T}_{i}-({T}_{i}-{T}_{i-1})\right)\hfill \\ & \phantom{\rule{3.33333pt}{0ex}}\phantom{\rule{3.33333pt}{0ex}}\phantom{\rule{3.33333pt}{0ex}}\phantom{\rule{3.33333pt}{0ex}}\mathbf{return}\phantom{\rule{3.33333pt}{0ex}}\dot{{\mathit{T}}}\hfill \end{array}$ | $\begin{array}{cc}& \mathbf{function}\phantom{\rule{3.33333pt}{0ex}}{f}_{c,2}(m,{a}_{T},{b}_{T})\hfill \\ & \phantom{\rule{3.33333pt}{0ex}}\phantom{\rule{3.33333pt}{0ex}}\phantom{\rule{3.33333pt}{0ex}}\phantom{\rule{3.33333pt}{0ex}}\mathit{T}:={m}_{\mathit{T}};\phantom{\rule{0.277778em}{0ex}}n:={m}_{n};\phantom{\rule{0.277778em}{0ex}}{k}_{1}={m}_{{k}_{1}}\hfill \\ & \phantom{\rule{3.33333pt}{0ex}}\phantom{\rule{3.33333pt}{0ex}}\phantom{\rule{3.33333pt}{0ex}}\phantom{\rule{3.33333pt}{0ex}}{m}_{{\dot{T}}_{1}}:={k}_{1}\phantom{\rule{0.166667em}{0ex}}\xb7\phantom{\rule{0.166667em}{0ex}}\left(2\phantom{\rule{0.166667em}{0ex}}\xb7\phantom{\rule{0.166667em}{0ex}}({a}_{T}-{T}_{1})-({T}_{1}-{T}_{2})\right)\hfill \\ & \phantom{\rule{3.33333pt}{0ex}}\phantom{\rule{3.33333pt}{0ex}}\phantom{\rule{3.33333pt}{0ex}}\phantom{\rule{3.33333pt}{0ex}}{m}_{{\dot{T}}_{n}}:={k}_{1}\phantom{\rule{0.166667em}{0ex}}\xb7\phantom{\rule{0.166667em}{0ex}}\left({T}_{n-1}-{T}_{n}-2\phantom{\rule{0.166667em}{0ex}}\xb7\phantom{\rule{0.166667em}{0ex}}({T}_{n}-{b}_{T})\right)\hfill \\ & \phantom{\rule{3.33333pt}{0ex}}\phantom{\rule{3.33333pt}{0ex}}\phantom{\rule{3.33333pt}{0ex}}\phantom{\rule{3.33333pt}{0ex}}\mathbf{for}\phantom{\rule{3.33333pt}{0ex}}i=2:n-1\hfill \\ & \phantom{\rule{3.33333pt}{0ex}}\phantom{\rule{3.33333pt}{0ex}}\phantom{\rule{3.33333pt}{0ex}}\phantom{\rule{3.33333pt}{0ex}}\phantom{\rule{3.33333pt}{0ex}}\phantom{\rule{3.33333pt}{0ex}}\phantom{\rule{3.33333pt}{0ex}}\phantom{\rule{3.33333pt}{0ex}}{m}_{{\dot{T}}_{i}}:={k}_{1}\phantom{\rule{0.166667em}{0ex}}\xb7\phantom{\rule{0.166667em}{0ex}}\left({T}_{i+1}-{T}_{i}-({T}_{i}-{T}_{i-1})\right)\hfill \\ & \phantom{\rule{3.33333pt}{0ex}}\phantom{\rule{3.33333pt}{0ex}}\phantom{\rule{3.33333pt}{0ex}}\phantom{\rule{3.33333pt}{0ex}}<\mathrm{copy}\phantom{\rule{4.pt}{0ex}}{m}_{\dot{\mathit{T}}}\phantom{\rule{4.pt}{0ex}}\mathrm{into}\phantom{\rule{4.pt}{0ex}}\mathrm{state}\phantom{\rule{4.pt}{0ex}}\mathrm{derivatives}\phantom{\rule{4.pt}{0ex}}\mathrm{of}\phantom{\rule{4.pt}{0ex}}{m}_{\mathrm{sim}}\phantom{\rule{4.pt}{0ex}}\mathrm{for}\phantom{\rule{4.pt}{0ex}}{m}_{\mathrm{id}}>\hfill \end{array}$ |

#### Appendix B.2. Two-Stage Rocket with Acausal Built-In Component

`RocketSystem`implementing these equations is given in Listing A7 and is available from (https://github.com/ModiaSim/Modia.jl, accessed on 17 January 2023, release v0.10.0, TestTwoStageRocket.jl). The utilized built-in component

`TwoStageRocket`has varying number of states and its pseude-code is shown in Table A2. Simulation results are shown in Figure A3.

Listing A7. Modia model of simple two stage rocket. TwoStageRocket is a built-in component with
varying number of states. |

# equation section (events are triggered at $t=={t}_{1}$ and $t=={t}_{2}$) |

$\begin{array}{cc}& \mathrm{phase}=\mathbf{if}\phantom{\rule{3.33333pt}{0ex}}t<{t}_{1}\phantom{\rule{3.33333pt}{0ex}}\mathbf{then}\phantom{\rule{3.33333pt}{0ex}}1\phantom{\rule{3.33333pt}{0ex}}\mathbf{elseif}\phantom{\rule{3.33333pt}{0ex}}t<{t}_{2}\phantom{\rule{3.33333pt}{0ex}}\mathbf{then}\phantom{\rule{3.33333pt}{0ex}}2\phantom{\rule{3.33333pt}{0ex}}\mathbf{else}\phantom{\rule{3.33333pt}{0ex}}3\phantom{\rule{3.33333pt}{0ex}}\mathbf{end}\hfill \\ & {\mathit{f}}_{c,2}(m,\mathrm{phase})\hfill \end{array}$ |

# function definitions |

$\begin{array}{cc}& \mathbf{function}\phantom{\rule{3.33333pt}{0ex}}{f}_{c,0}(\mathrm{sim},\mathrm{id},{m}_{1},{m}_{2},g,{f}_{1,\mathrm{max}},{f}_{2,\mathrm{max}},{t}_{1},{t}_{2},{t}_{3})\phantom{\rule{3.33333pt}{0ex}}\phantom{\rule{3.33333pt}{0ex}}\#\phantom{\rule{3.33333pt}{0ex}}\mathrm{called}\phantom{\rule{4.pt}{0ex}}\mathrm{once}\hfill \\ & \phantom{\rule{3.33333pt}{0ex}}\phantom{\rule{3.33333pt}{0ex}}\phantom{\rule{3.33333pt}{0ex}}\phantom{\rule{3.33333pt}{0ex}}<\mathrm{allocate}\phantom{\rule{4.pt}{0ex}}\mathrm{new}\phantom{\rule{4.pt}{0ex}}\mathrm{record}\phantom{\rule{4.pt}{0ex}}m\phantom{\rule{4.pt}{0ex}}\mathrm{and}\phantom{\rule{4.pt}{0ex}}\mathrm{store}\phantom{\rule{4.pt}{0ex}}\mathrm{parameters}\phantom{\rule{4.pt}{0ex}}\mathrm{in}\phantom{\rule{4.pt}{0ex}}m\phantom{\rule{4.pt}{0ex}}>\hfill \\ & \phantom{\rule{3.33333pt}{0ex}}\phantom{\rule{3.33333pt}{0ex}}\phantom{\rule{3.33333pt}{0ex}}\phantom{\rule{3.33333pt}{0ex}}{m}_{\mathrm{phase}}:=0;\phantom{\rule{0.277778em}{0ex}}{m}_{\mathrm{nextPhase}}:=1;\phantom{\rule{0.277778em}{0ex}}\mathbf{return}\phantom{\rule{3.33333pt}{0ex}}m\hfill \end{array}$ |

$\begin{array}{cc}& \mathbf{function}\phantom{\rule{3.33333pt}{0ex}}{f}_{c,\mathrm{init}}\left(m\right)\phantom{\rule{3.33333pt}{0ex}}\phantom{\rule{3.33333pt}{0ex}}\phantom{\rule{3.33333pt}{0ex}}\#\phantom{\rule{4.pt}{0ex}}\mathrm{called}\phantom{\rule{4.pt}{0ex}}\mathrm{before}\phantom{\rule{4.pt}{0ex}}\mathrm{a}\phantom{\rule{4.pt}{0ex}}\mathrm{segment}\phantom{\rule{4.pt}{0ex}}\mathrm{is}\phantom{\rule{4.pt}{0ex}}\mathrm{initialized}\hfill \\ & \phantom{\rule{3.33333pt}{0ex}}\phantom{\rule{3.33333pt}{0ex}}\phantom{\rule{3.33333pt}{0ex}}\phantom{\rule{3.33333pt}{0ex}}{m}_{\mathrm{phase}}:={m}_{\mathrm{nextPhase}}\hfill \\ & \phantom{\rule{3.33333pt}{0ex}}\phantom{\rule{3.33333pt}{0ex}}\phantom{\rule{3.33333pt}{0ex}}\phantom{\rule{3.33333pt}{0ex}}\mathbf{if}\phantom{\rule{3.33333pt}{0ex}}{m}_{\mathrm{phase}}==1\hfill \\ & \phantom{\rule{3.33333pt}{0ex}}\phantom{\rule{3.33333pt}{0ex}}\phantom{\rule{3.33333pt}{0ex}}\phantom{\rule{3.33333pt}{0ex}}\phantom{\rule{3.33333pt}{0ex}}\phantom{\rule{3.33333pt}{0ex}}\phantom{\rule{3.33333pt}{0ex}}\phantom{\rule{3.33333pt}{0ex}}<\mathrm{define}\phantom{\rule{4.pt}{0ex}}\mathrm{new}\phantom{\rule{4.pt}{0ex}}\mathrm{variables}\phantom{\rule{4.pt}{0ex}}{h}_{1}(\mathrm{init}=0),{v}_{1}(\mathrm{init}=0),{\dot{h}}_{1},{\dot{v}}_{1},{F}_{1}\phantom{\rule{4.pt}{0ex}}>\hfill \\ & \phantom{\rule{3.33333pt}{0ex}}\phantom{\rule{3.33333pt}{0ex}}\phantom{\rule{3.33333pt}{0ex}}\phantom{\rule{3.33333pt}{0ex}}\mathbf{elseif}\phantom{\rule{3.33333pt}{0ex}}{m}_{\mathrm{phase}}==2\hfill \\ & \phantom{\rule{3.33333pt}{0ex}}\phantom{\rule{3.33333pt}{0ex}}\phantom{\rule{3.33333pt}{0ex}}\phantom{\rule{3.33333pt}{0ex}}\phantom{\rule{3.33333pt}{0ex}}\phantom{\rule{3.33333pt}{0ex}}\phantom{\rule{3.33333pt}{0ex}}\phantom{\rule{3.33333pt}{0ex}}<\mathrm{define}\phantom{\rule{4.pt}{0ex}}\mathrm{new}\phantom{\rule{4.pt}{0ex}}\mathrm{variables}\phantom{\rule{4.pt}{0ex}}{h}_{1}(\mathrm{init}={m}_{{h}_{1}}),{h}_{2}(\mathrm{init}={m}_{{h}_{1}}),\phantom{\rule{4.pt}{0ex}}\hfill \\ & \phantom{\rule{3.33333pt}{0ex}}\phantom{\rule{3.33333pt}{0ex}}\phantom{\rule{3.33333pt}{0ex}}\phantom{\rule{3.33333pt}{0ex}}\phantom{\rule{3.33333pt}{0ex}}\phantom{\rule{3.33333pt}{0ex}}\phantom{\rule{3.33333pt}{0ex}}\phantom{\rule{3.33333pt}{0ex}}\phantom{\rule{3.33333pt}{0ex}}\phantom{\rule{3.33333pt}{0ex}}\phantom{\rule{3.33333pt}{0ex}}\phantom{\rule{3.33333pt}{0ex}}{v}_{1}(\mathrm{init}={m}_{{v}_{1}}),{v}_{2}(\mathrm{init}={m}_{{v}_{1}}),{\dot{v}}_{1},{\dot{v}}_{2},{F}_{1},{F}_{2}>\hfill \\ & \phantom{\rule{3.33333pt}{0ex}}\phantom{\rule{3.33333pt}{0ex}}\phantom{\rule{3.33333pt}{0ex}}\phantom{\rule{3.33333pt}{0ex}}\mathbf{elseif}\phantom{\rule{3.33333pt}{0ex}}{m}_{\mathrm{phase}}==3\hfill \\ & \phantom{\rule{3.33333pt}{0ex}}\phantom{\rule{3.33333pt}{0ex}}\phantom{\rule{3.33333pt}{0ex}}\phantom{\rule{3.33333pt}{0ex}}\phantom{\rule{3.33333pt}{0ex}}\phantom{\rule{3.33333pt}{0ex}}\phantom{\rule{3.33333pt}{0ex}}\phantom{\rule{3.33333pt}{0ex}}<\mathrm{define}\phantom{\rule{4.pt}{0ex}}\mathrm{new}\phantom{\rule{4.pt}{0ex}}\mathrm{variables}\phantom{\rule{4.pt}{0ex}}{h}_{2}(\mathrm{init}={m}_{{h}_{2}}),{v}_{2}(\mathrm{init}={m}_{{v}_{2}}),{\dot{h}}_{2},{\dot{v}}_{2},{F}_{2}\phantom{\rule{4.pt}{0ex}}>\hfill \\ & \phantom{\rule{3.33333pt}{0ex}}\phantom{\rule{3.33333pt}{0ex}}\phantom{\rule{3.33333pt}{0ex}}\phantom{\rule{3.33333pt}{0ex}}\mathbf{end}\hfill \end{array}$ |

$\begin{array}{cc}& \mathbf{function}\phantom{\rule{3.33333pt}{0ex}}{f}_{c,2}(m,\mathrm{phase})\phantom{\rule{3.33333pt}{0ex}}\phantom{\rule{3.33333pt}{0ex}}\#\phantom{\rule{3.33333pt}{0ex}}\mathrm{called}\phantom{\rule{4.pt}{0ex}}\mathrm{in}\phantom{\rule{4.pt}{0ex}}\mathrm{equation}\phantom{\rule{4.pt}{0ex}}\mathrm{sec}\mathrm{tion}\hfill \\ & \phantom{\rule{3.33333pt}{0ex}}\phantom{\rule{3.33333pt}{0ex}}\phantom{\rule{3.33333pt}{0ex}}\phantom{\rule{3.33333pt}{0ex}}\mathbf{if}\phantom{\rule{3.33333pt}{0ex}}\mathrm{isEvent}\left({m}_{\mathrm{sim}}\right)\phantom{\rule{3.33333pt}{0ex}}\mathbf{and}\phantom{\rule{3.33333pt}{0ex}}{m}_{\mathrm{phase}}!=\mathrm{phase}\hfill \\ & \phantom{\rule{3.33333pt}{0ex}}\phantom{\rule{3.33333pt}{0ex}}\phantom{\rule{3.33333pt}{0ex}}\phantom{\rule{3.33333pt}{0ex}}\phantom{\rule{3.33333pt}{0ex}}\phantom{\rule{3.33333pt}{0ex}}\phantom{\rule{3.33333pt}{0ex}}\phantom{\rule{3.33333pt}{0ex}}{m}_{\mathrm{nextPhase}}:=\mathrm{phase};\phantom{\rule{3.33333pt}{0ex}}\phantom{\rule{3.33333pt}{0ex}}\phantom{\rule{3.33333pt}{0ex}}\phantom{\rule{3.33333pt}{0ex}}\mathrm{setFullRestartEvent}\left({m}_{\mathrm{sim}}\right);\phantom{\rule{3.33333pt}{0ex}}\phantom{\rule{3.33333pt}{0ex}}\phantom{\rule{3.33333pt}{0ex}}\phantom{\rule{3.33333pt}{0ex}}\mathbf{end}\hfill \\ & \phantom{\rule{3.33333pt}{0ex}}\phantom{\rule{3.33333pt}{0ex}}\phantom{\rule{3.33333pt}{0ex}}\phantom{\rule{3.33333pt}{0ex}}\mathbf{if}\phantom{\rule{3.33333pt}{0ex}}{m}_{\mathrm{phase}}==1\hfill \\ & \phantom{\rule{3.33333pt}{0ex}}\phantom{\rule{3.33333pt}{0ex}}\phantom{\rule{3.33333pt}{0ex}}\phantom{\rule{3.33333pt}{0ex}}\phantom{\rule{3.33333pt}{0ex}}\phantom{\rule{3.33333pt}{0ex}}\phantom{\rule{3.33333pt}{0ex}}\phantom{\rule{3.33333pt}{0ex}}<\mathrm{copy}\phantom{\rule{4.pt}{0ex}}{m}_{{h}_{1}},{m}_{{v}_{1}}\phantom{\rule{4.pt}{0ex}}\mathrm{from}\phantom{\rule{4.pt}{0ex}}\mathrm{states}\phantom{\rule{4.pt}{0ex}}\mathrm{in}\phantom{\rule{4.pt}{0ex}}{m}_{\mathrm{sim}}\phantom{\rule{4.pt}{0ex}}\mathrm{for}\phantom{\rule{4.pt}{0ex}}{m}_{\mathrm{id}}>\hfill \\ & \phantom{\rule{3.33333pt}{0ex}}\phantom{\rule{3.33333pt}{0ex}}\phantom{\rule{3.33333pt}{0ex}}\phantom{\rule{3.33333pt}{0ex}}\phantom{\rule{3.33333pt}{0ex}}\phantom{\rule{3.33333pt}{0ex}}\phantom{\rule{3.33333pt}{0ex}}\phantom{\rule{3.33333pt}{0ex}}{\dot{h}}_{1}={m}_{{v}_{1}}\hfill \\ & \phantom{\rule{3.33333pt}{0ex}}\phantom{\rule{3.33333pt}{0ex}}\phantom{\rule{3.33333pt}{0ex}}\phantom{\rule{3.33333pt}{0ex}}\phantom{\rule{3.33333pt}{0ex}}\phantom{\rule{3.33333pt}{0ex}}\phantom{\rule{3.33333pt}{0ex}}\phantom{\rule{3.33333pt}{0ex}}{F}_{1}=F({m}_{{F}_{1,\mathrm{max}}},0,{m}_{{t}_{1}})\phantom{\rule{3.33333pt}{0ex}}\phantom{\rule{3.33333pt}{0ex}}\#\phantom{\rule{3.33333pt}{0ex}}\mathrm{Function}\phantom{\rule{4.pt}{0ex}}\mathrm{F}(..)\phantom{\rule{4.pt}{0ex}}\mathrm{to}\phantom{\rule{4.pt}{0ex}}\mathrm{compute}\phantom{\rule{4.pt}{0ex}}\mathrm{thrust}\phantom{\rule{4.pt}{0ex}}\mathrm{is}\phantom{\rule{4.pt}{0ex}}\mathrm{not}\phantom{\rule{4.pt}{0ex}}\mathrm{shown}\hfill \\ & \phantom{\rule{3.33333pt}{0ex}}\phantom{\rule{3.33333pt}{0ex}}\phantom{\rule{3.33333pt}{0ex}}\phantom{\rule{3.33333pt}{0ex}}\phantom{\rule{3.33333pt}{0ex}}\phantom{\rule{3.33333pt}{0ex}}\phantom{\rule{3.33333pt}{0ex}}\phantom{\rule{3.33333pt}{0ex}}{\dot{v}}_{1}={F}_{1}/({m}_{1}+{m}_{2})-g\hfill \\ & \phantom{\rule{3.33333pt}{0ex}}\phantom{\rule{3.33333pt}{0ex}}\phantom{\rule{3.33333pt}{0ex}}\phantom{\rule{3.33333pt}{0ex}}\phantom{\rule{3.33333pt}{0ex}}\phantom{\rule{3.33333pt}{0ex}}\phantom{\rule{3.33333pt}{0ex}}\phantom{\rule{3.33333pt}{0ex}}<\mathrm{copy}\phantom{\rule{4.pt}{0ex}}{\dot{h}}_{1},{\dot{v}}_{1},{F}_{1}\phantom{\rule{4.pt}{0ex}}\mathrm{into}\phantom{\rule{4.pt}{0ex}}\mathrm{states}/\mathrm{local}\phantom{\rule{4.pt}{0ex}}\mathrm{variables}\phantom{\rule{4.pt}{0ex}}\mathrm{of}\phantom{\rule{4.pt}{0ex}}{m}_{\mathrm{sim}}\phantom{\rule{4.pt}{0ex}}\mathrm{for}\phantom{\rule{4.pt}{0ex}}{m}_{\mathrm{id}}>\hfill \\ & \phantom{\rule{3.33333pt}{0ex}}\phantom{\rule{3.33333pt}{0ex}}\phantom{\rule{3.33333pt}{0ex}}\phantom{\rule{3.33333pt}{0ex}}\mathbf{elseif}\phantom{\rule{3.33333pt}{0ex}}{m}_{\mathrm{phase}}==2\hfill \\ & \phantom{\rule{3.33333pt}{0ex}}\phantom{\rule{3.33333pt}{0ex}}\phantom{\rule{3.33333pt}{0ex}}\phantom{\rule{3.33333pt}{0ex}}\phantom{\rule{3.33333pt}{0ex}}\phantom{\rule{3.33333pt}{0ex}}\phantom{\rule{3.33333pt}{0ex}}\phantom{\rule{3.33333pt}{0ex}}<\mathrm{copy}\phantom{\rule{4.pt}{0ex}}{m}_{{h}_{1}},{m}_{{v}_{1}},{m}_{{h}_{2}},{m}_{{v}_{2}}\phantom{\rule{4.pt}{0ex}}\mathrm{from}\phantom{\rule{4.pt}{0ex}}\mathrm{states}\phantom{\rule{4.pt}{0ex}}\mathrm{in}\phantom{\rule{4.pt}{0ex}}{m}_{\mathrm{sim}}\phantom{\rule{4.pt}{0ex}}\mathrm{for}\phantom{\rule{4.pt}{0ex}}{m}_{\mathrm{id}}>\hfill \\ & \phantom{\rule{3.33333pt}{0ex}}\phantom{\rule{3.33333pt}{0ex}}\phantom{\rule{3.33333pt}{0ex}}\phantom{\rule{3.33333pt}{0ex}}\phantom{\rule{3.33333pt}{0ex}}\phantom{\rule{3.33333pt}{0ex}}\phantom{\rule{3.33333pt}{0ex}}\phantom{\rule{3.33333pt}{0ex}}{\dot{h}}_{1}={m}_{{v}_{1}}\hfill \\ & \phantom{\rule{3.33333pt}{0ex}}\phantom{\rule{3.33333pt}{0ex}}\phantom{\rule{3.33333pt}{0ex}}\phantom{\rule{3.33333pt}{0ex}}\phantom{\rule{3.33333pt}{0ex}}\phantom{\rule{3.33333pt}{0ex}}\phantom{\rule{3.33333pt}{0ex}}\phantom{\rule{3.33333pt}{0ex}}{F}_{1}=F({m}_{{F}_{1,\mathrm{max}}},0,{m}_{{t}_{1}})\phantom{\rule{3.33333pt}{0ex}}\phantom{\rule{3.33333pt}{0ex}}\#\phantom{\rule{3.33333pt}{0ex}}\mathrm{Function}\phantom{\rule{4.pt}{0ex}}\mathrm{F}(..)\phantom{\rule{4.pt}{0ex}}\mathrm{to}\phantom{\rule{4.pt}{0ex}}\mathrm{compute}\phantom{\rule{4.pt}{0ex}}\mathrm{thrust}\phantom{\rule{4.pt}{0ex}}\mathrm{is}\phantom{\rule{4.pt}{0ex}}\mathrm{not}\phantom{\rule{4.pt}{0ex}}\mathrm{shown}\hfill \\ & \phantom{\rule{3.33333pt}{0ex}}\phantom{\rule{3.33333pt}{0ex}}\phantom{\rule{3.33333pt}{0ex}}\phantom{\rule{3.33333pt}{0ex}}\phantom{\rule{3.33333pt}{0ex}}\phantom{\rule{3.33333pt}{0ex}}\phantom{\rule{3.33333pt}{0ex}}\phantom{\rule{3.33333pt}{0ex}}{\dot{v}}_{1}={F}_{1}/{m}_{1}-g\hfill \\ & \phantom{\rule{3.33333pt}{0ex}}\phantom{\rule{3.33333pt}{0ex}}\phantom{\rule{3.33333pt}{0ex}}\phantom{\rule{3.33333pt}{0ex}}\phantom{\rule{3.33333pt}{0ex}}\phantom{\rule{3.33333pt}{0ex}}\phantom{\rule{3.33333pt}{0ex}}\phantom{\rule{3.33333pt}{0ex}}{\dot{h}}_{2}={m}_{{v}_{2}}\hfill \\ & \phantom{\rule{3.33333pt}{0ex}}\phantom{\rule{3.33333pt}{0ex}}\phantom{\rule{3.33333pt}{0ex}}\phantom{\rule{3.33333pt}{0ex}}\phantom{\rule{3.33333pt}{0ex}}\phantom{\rule{3.33333pt}{0ex}}\phantom{\rule{3.33333pt}{0ex}}\phantom{\rule{3.33333pt}{0ex}}{F}_{2}=F({m}_{{F}_{2,\mathrm{max}}},{m}_{{t}_{1}},{m}_{{t}_{3}})\phantom{\rule{3.33333pt}{0ex}}\phantom{\rule{3.33333pt}{0ex}}\#\phantom{\rule{3.33333pt}{0ex}}\mathrm{Function}\phantom{\rule{4.pt}{0ex}}\mathrm{F}(..)\phantom{\rule{4.pt}{0ex}}\mathrm{to}\phantom{\rule{4.pt}{0ex}}\mathrm{compute}\phantom{\rule{4.pt}{0ex}}\mathrm{thrust}\phantom{\rule{4.pt}{0ex}}\mathrm{is}\phantom{\rule{4.pt}{0ex}}\mathrm{not}\phantom{\rule{4.pt}{0ex}}\mathrm{shown}\hfill \\ & \phantom{\rule{3.33333pt}{0ex}}\phantom{\rule{3.33333pt}{0ex}}\phantom{\rule{3.33333pt}{0ex}}\phantom{\rule{3.33333pt}{0ex}}\phantom{\rule{3.33333pt}{0ex}}\phantom{\rule{3.33333pt}{0ex}}\phantom{\rule{3.33333pt}{0ex}}\phantom{\rule{3.33333pt}{0ex}}{\dot{v}}_{2}={F}_{2}/{m}_{2}-g\hfill \\ & \phantom{\rule{3.33333pt}{0ex}}\phantom{\rule{3.33333pt}{0ex}}\phantom{\rule{3.33333pt}{0ex}}\phantom{\rule{3.33333pt}{0ex}}\phantom{\rule{3.33333pt}{0ex}}\phantom{\rule{3.33333pt}{0ex}}\phantom{\rule{3.33333pt}{0ex}}\phantom{\rule{3.33333pt}{0ex}}<\mathrm{copy}\phantom{\rule{4.pt}{0ex}}{\dot{h}}_{1},{\dot{v}}_{1},{\dot{h}}_{2},{\dot{v}}_{2},{F}_{1},{F}_{2}\phantom{\rule{4.pt}{0ex}}\mathrm{into}\phantom{\rule{4.pt}{0ex}}\mathrm{states}/\mathrm{local}\phantom{\rule{4.pt}{0ex}}\mathrm{variables}\phantom{\rule{4.pt}{0ex}}\mathrm{of}\phantom{\rule{4.pt}{0ex}}{m}_{\mathrm{sim}}\phantom{\rule{4.pt}{0ex}}\mathrm{for}\phantom{\rule{4.pt}{0ex}}{m}_{\mathrm{id}}>\hfill \\ & \phantom{\rule{3.33333pt}{0ex}}\phantom{\rule{3.33333pt}{0ex}}\phantom{\rule{3.33333pt}{0ex}}\phantom{\rule{3.33333pt}{0ex}}\mathbf{elseif}\phantom{\rule{3.33333pt}{0ex}}{m}_{\mathrm{phase}}==3\hfill \\ & \phantom{\rule{3.33333pt}{0ex}}\phantom{\rule{3.33333pt}{0ex}}\phantom{\rule{3.33333pt}{0ex}}\phantom{\rule{3.33333pt}{0ex}}\phantom{\rule{3.33333pt}{0ex}}\phantom{\rule{3.33333pt}{0ex}}\phantom{\rule{3.33333pt}{0ex}}\phantom{\rule{3.33333pt}{0ex}}<\mathrm{copy}\phantom{\rule{4.pt}{0ex}}{m}_{{h}_{2}},{m}_{{v}_{2}}\phantom{\rule{4.pt}{0ex}}\mathrm{from}\phantom{\rule{4.pt}{0ex}}\mathrm{states}\phantom{\rule{4.pt}{0ex}}\mathrm{in}\phantom{\rule{4.pt}{0ex}}{m}_{\mathrm{sim}}\phantom{\rule{4.pt}{0ex}}\mathrm{for}\phantom{\rule{4.pt}{0ex}}{m}_{\mathrm{id}}>\hfill \\ & \phantom{\rule{3.33333pt}{0ex}}\phantom{\rule{3.33333pt}{0ex}}\phantom{\rule{3.33333pt}{0ex}}\phantom{\rule{3.33333pt}{0ex}}\phantom{\rule{3.33333pt}{0ex}}\phantom{\rule{3.33333pt}{0ex}}\phantom{\rule{3.33333pt}{0ex}}\phantom{\rule{3.33333pt}{0ex}}{\dot{h}}_{2}={m}_{{v}_{2}}\hfill \\ & \phantom{\rule{3.33333pt}{0ex}}\phantom{\rule{3.33333pt}{0ex}}\phantom{\rule{3.33333pt}{0ex}}\phantom{\rule{3.33333pt}{0ex}}\phantom{\rule{3.33333pt}{0ex}}\phantom{\rule{3.33333pt}{0ex}}\phantom{\rule{3.33333pt}{0ex}}\phantom{\rule{3.33333pt}{0ex}}{F}_{2}=F({m}_{{F}_{2,\mathrm{max}}},{m}_{{t}_{1}},{m}_{{t}_{3}})\phantom{\rule{3.33333pt}{0ex}}\phantom{\rule{3.33333pt}{0ex}}\#\phantom{\rule{3.33333pt}{0ex}}\mathrm{Function}\phantom{\rule{4.pt}{0ex}}\mathrm{F}(..)\phantom{\rule{4.pt}{0ex}}\mathrm{to}\phantom{\rule{4.pt}{0ex}}\mathrm{compute}\phantom{\rule{4.pt}{0ex}}\mathrm{thrust}\phantom{\rule{4.pt}{0ex}}\mathrm{is}\phantom{\rule{4.pt}{0ex}}\mathrm{not}\phantom{\rule{4.pt}{0ex}}\mathrm{shown}\hfill \\ & \phantom{\rule{3.33333pt}{0ex}}\phantom{\rule{3.33333pt}{0ex}}\phantom{\rule{3.33333pt}{0ex}}\phantom{\rule{3.33333pt}{0ex}}\phantom{\rule{3.33333pt}{0ex}}\phantom{\rule{3.33333pt}{0ex}}\phantom{\rule{3.33333pt}{0ex}}\phantom{\rule{3.33333pt}{0ex}}{\dot{v}}_{2}={F}_{2}/{m}_{2}-g\hfill \\ & \phantom{\rule{3.33333pt}{0ex}}\phantom{\rule{3.33333pt}{0ex}}\phantom{\rule{3.33333pt}{0ex}}\phantom{\rule{3.33333pt}{0ex}}\phantom{\rule{3.33333pt}{0ex}}\phantom{\rule{3.33333pt}{0ex}}\phantom{\rule{3.33333pt}{0ex}}\phantom{\rule{3.33333pt}{0ex}}<\mathrm{copy}\phantom{\rule{4.pt}{0ex}}{\dot{h}}_{2},{\dot{v}}_{2},{F}_{2}\phantom{\rule{4.pt}{0ex}}\mathrm{into}\phantom{\rule{4.pt}{0ex}}\mathrm{states}/\mathrm{local}\phantom{\rule{4.pt}{0ex}}\mathrm{variables}\phantom{\rule{4.pt}{0ex}}\mathrm{of}\phantom{\rule{4.pt}{0ex}}{m}_{\mathrm{sim}}\phantom{\rule{4.pt}{0ex}}\mathrm{for}\phantom{\rule{4.pt}{0ex}}{m}_{\mathrm{id}}>\hfill \\ & \phantom{\rule{3.33333pt}{0ex}}\phantom{\rule{3.33333pt}{0ex}}\phantom{\rule{3.33333pt}{0ex}}\phantom{\rule{3.33333pt}{0ex}}\mathbf{end}\hfill \end{array}$ |

**Figure A3.**Plots of the variables from the simulation of model RocketSystem of Listing A7. Variable values are not shown in the plots, if they are not defined in the respective phase.

## References

- Modelica Association. Modelica—A Unified Object-Oriented Language for Systems Modeling, Language Specification, Version 3.5. 2021. Available online: https://specification.modelica.org/maint/3.5/MLS.pdf (accessed on 13 January 2023).
- Modelica Tools. Available online: https://modelica.org/tools.html (accessed on 11 December 2022).
- Arnold, M. DAE Aspects of Multibody System Dynamics. In Surveys in Differential-Algebraic Equations IV; Ilchmann, A., Reis, T., Eds.; Springer International Publishing: Cham, Switzerland, 2017; pp. 41–106. [Google Scholar] [CrossRef]
- Elmqvist, H.; Otter, M.; Neumayr, A.; Hippmann, G. Modia—Equation Based Modeling and Domain Specific Algorithms. In Proceedings of the 14th International Modelica Conference, Linköping, Sweden, 20–24 September 2021; pp. 73–86. [Google Scholar] [CrossRef]
- Bezanson, J.; Edelman, A.; Karpinski, S.; Shah, V.B. Julia: A fresh approach to numerical computing. SIAM Rev.
**2017**, 59, 65–98. [Google Scholar] [CrossRef] [Green Version] - Pantelides, C.C. The Consistent Initialization of Differential-Algebraic Systems. SIAM J. Sci. Stat. Comput.
**1988**, 9, 213–231. [Google Scholar] [CrossRef] - Pryce, J.D. A simple structural analysis method for DAEs. BIT Numer. Math.
**2001**, 41, 364–394. [Google Scholar] [CrossRef] - Elmqvist, H.; Matsson, S.E.; Otter, M. Modelica extensions for multi-mode DAE systems. In Proceedings of the 10th International Modelica Conference, Lund, Sweden, 10–12 March 2014; Linköping University Electronic Press: Linköping, Sweden, 2014; pp. 183–193. [Google Scholar] [CrossRef] [Green Version]
- Benveniste, A.; Caillaud, B.; Malandain, M.; Thibault, J. Algorithms for the Structural Analysis of Multimode Modelica Models. Electronics
**2022**, 11, 2755. [Google Scholar] [CrossRef] - Caillaud, B.; Malandain, M.; Thibault, J. Implicit Structural Analysis of Multimode DAE Systems. In Proceedings of the 23rd International Conference on Hybrid Systems: Computation and Control, HSCC ’20, Sydney, NSW, Australia, 22–24 April 2020. [Google Scholar] [CrossRef]
- Benveniste, A.; Caillaud, B.; Elmqvist, H.; Ghorbal, K.; Otter, M.; Pouzet, M. Multi-Mode DAE Models—Challenges, Theory and Implementation. In Computing and Software Science: State of the Art and Perspectives; Springer International Publishing: Cham, Switzerland, 2019; pp. 283–310. [Google Scholar] [CrossRef] [Green Version]
- Höger, C. Dynamic Structural Analysis for DAEs. In Proceedings of the 2014 Summer Simulation Multiconference, SummerSim’14, Monterey, CA, USA, 6–10 July 2014; Society for Computer Simulation International: San Diego, CA, USA, 2014. [Google Scholar]
- Zimmer, D. Equation-Based Modeling of Variable-Structure Systems. Ph.D. Thesis, ETH Zürich, Zürich, Switzerland, 2010. [Google Scholar] [CrossRef]
- Pepper, P.; Mehlhase, A.; Höger, C.; Scholz, L. A Compositional Semantics for Modelica-style Variable-structureModeling. In Proceedings of the 4th International Workshop on Equation-Based Object-Oriented Modeling Languages and Tools, EOOLT’11, Zürich, Switzerland, 5 December 2011; pp. 45–54. [Google Scholar]
- Mehlhase, A. A Python framework to create and simulate models with variable structure in common simulation environments. Math. Comput. Model. Dyn. Syst.
**2014**, 20, 566–583. [Google Scholar] [CrossRef] - Mattsson, S.E.; Otter, M.; Elmqvist, H. Multi-mode DAE systems with varying index. In Proceedings of the 11th International Modelica Conference, Versailles, France, 21–23 September 2015; pp. 89–98. [Google Scholar] [CrossRef] [Green Version]
- Tinnerholm, J.; Pop, A.; Sjölund, M. A Modular, Extensible, and Modelica-Standard-Compliant OpenModelica Compiler Framework in Julia Supporting Structural Variability. Electronics
**2022**, 11, 1772. [Google Scholar] [CrossRef] - Modelica Association. Functional Mock-Up Interface for Model Exchange and Co-Simulation—Version 2.0. 2014. Available online: https://fmi-standard.org/assets/releases/FMI_for_ModelExchange_and_CoSimulation_v2.0.pdf (accessed on 13 January 2023).
- Steinbach, O. Numerical Approximation Methods for Elliptic Boundary Value Problems: Finite and Boundary Elements; Springer: New York, NY, USA, 2007. [Google Scholar] [CrossRef]
- Campbell, S.L.; Linh, V.H.; Petzold, L.R. Differential-algebraic equations. Scholarpedia
**2008**, 3, 2849. [Google Scholar] [CrossRef] - Olsson, H.; Otter, M.; Mattsson, S.; Elmqvist, H. Balanced Models in Modelica 3.0 for Increased Model Quality. In Proceedings of the 8th International Modelica Conference, Bielefeld, Germany, 3–4 March 2008; pp. 21–33. [Google Scholar]
- Elmqvist, H. Pages 7–10 of Modia—A Prototyping Platform for Next Generation Modeling and Simulation Based on Julia. Jubilee Symposium 2019: Future Directions of System Modeling and Simulation. Available online: https://modelica.github.io/Symposium2019/slides/jubilee-symposium-2019-slides-elmqvist.pdf (accessed on 4 December 2022).
- Otter, M. Signal Tables: An Extensible Exchange Format for Simulation Data. Electronics
**2022**, 11, 2811. [Google Scholar] [CrossRef] - Snethen, G. Xenocollide: Complex collision made simple. In Game Programming Gems 7; Course Technology; Charles River Media: Newton, MA, USA, 2008; pp. 165–178. [Google Scholar]
- Neumayr, A.; Otter, M. Collision Handling with Variable-step Integrators. In Proceedings of the 8th International Workshop on Equation-Based Object-Oriented Modeling Languages and Tools, EOOLT’17, Weßling, Germany, 1 December 2017; pp. 9–18. [Google Scholar] [CrossRef]
- Hertz, H. On the contact of solids—On the contact of rigid elastic solids and on hardness. In Miscellaneous Papers; MacMillan: Stuttgart, Germany, 1896; pp. 146–183. Available online: https://archive.org/details/cu31924012500306 (accessed on 13 January 2023).
- Flores, P.; Machado, M.; Silva, M.T.; Martins, J.M. On the continuous contact force models for soft materials in multibody dynamics. Multibody Syst. Dyn.
**2011**, 25, 357–375. [Google Scholar] [CrossRef] - Neumayr, A.; Otter, M. Collision Handling with Elastic Response Calculation and Zero-Crossing Functions. In Proceedings of the 9th International Workshop on Equation-Based Object-Oriented Modeling Languages and Tools, EOOLT’19, Berlin, Germany, 5 November 2019; pp. 57–65. [Google Scholar] [CrossRef]
- Nystrom, R. Game Programming Patterns; Genever Benning; 2014. [Google Scholar]
- Unity Technologies. Unity—Manual: Unity User Manual 2021.3 (LTS). Available online: https://docs.unity3d.com/Manual/index.html (accessed on 27 April 2022).
- Epic Games. Unreal Engine 5 Documentation | Unreal Engine Documentation. Available online: https://docs.unrealengine.com (accessed on 27 April 2022).
- Three.js. Available online: https://threejs.org/docs/#api/en/core/Object3D (accessed on 13 October 2022).
- Neumayr, A.; Otter, M. Algorithms for Component-Based 3D Modeling. In Proceedings of the 13th International Modelica Conference, Regensburg, Germany, 4–6 March 2019; Linköping University Electronic Press: Linköping, Sweden, 2019. [Google Scholar] [CrossRef]
- Keller, A. Unitful.jl. Available online: https://github.com/PainterQubits/Unitful.jl (accessed on 12 December 2022).
- Otter, M.; Elmqvist, H. Transformation of Differential Algebraic Array Equations to Index One Form. In Proceedings of the 12th International Modelica Conference, Prague, Czech Republic, 15–17 May 2017; Linköping University Electronic Press: Linköping, Sweden, 2017. [Google Scholar] [CrossRef] [Green Version]
- Rackauckas, C.; Nie, Q. DifferentialEquations.jl—A Performant and Feature-Rich Ecosystem for Solving Differential Equations in Julia. J. Open Res. Softw.
**2017**, 5, 15. [Google Scholar] [CrossRef] - DifferentialEquations.jl. Available online: https://github.com/SciML/DifferentialEquations.jl (accessed on 12 December 2022).

**Figure 1.**Mathematical description of an acausal component. Components can be connected via $\mathit{u},\mathit{y},{\mathit{c}}_{p},{\mathit{c}}_{f}$. Events are defined by zero crossings of elements of

**z**. At events, variable values can change discontinuously. $\mathbb{R}$ is the set of real numbers. ${C}_{\mathrm{pw}}^{k}{\left(\mathbb{R}\right)}^{n}$ is the space of piecewise (pw) k-times continuously differentiable functions in n dimensions. $0={\mathit{f}}_{c}(\dots )$ is a set of implicit equations. Variables in dark blue are assumed to be known: variables t,

**x**are provided by the ODE solver,

**p**are parameters that get constant values before simulation starts,

**u**are inputs and are provided externally to the component. There are ${n}_{c}$ equations missing in order to solve $0={\mathit{f}}_{c}(\dots )$ for its unknowns $\dot{\mathit{x}},\mathit{z},\mathit{w},{\mathit{c}}_{p},{\mathit{c}}_{f},\mathit{y}$. These missing equations are provided when connecting the component via the connection equations of ${\mathit{c}}_{p},{\mathit{c}}_{f}$.

**Figure 2.**Mathematical description of an acausal component with pre-translated functions with the same interface as in Figure 1. These components can be connected via $\mathit{u},\mathit{y},{\mathit{c}}_{p},{\mathit{c}}_{f}$. Arguments given in grey will be moved into an internal memory in Section 3.3. ${\mathit{f}}_{c,1},{\mathit{f}}_{c,2}$ are explicit functions, whereas $0={\mathit{f}}_{c,\mathrm{eq}}(\dots )$ is a set of implicit equations. Variables $\dot{\mathit{x}},\mathit{z},\mathit{w},{\mathit{c}}_{p},{\mathit{c}}_{f},\mathit{y}$ are unknown and are split into three parts, e.g., $\mathit{y}=({\mathit{y}}_{1},{\mathit{y}}_{\mathrm{eq}},{\mathit{y}}_{2})$, where ${\mathit{y}}_{1}$ is an output argument of function ${\mathit{f}}_{c,1}$, ${\mathit{y}}_{\mathrm{eq}}$ is computed from the implicit equation system $0={\mathit{f}}_{c,\mathrm{eq}}(\dots )$, and ${\mathit{y}}_{2}$ is an output argument of function ${\mathit{f}}_{c,2}$.

**Figure 3.**Description of an acausal built-in component with functions of a programming language that has internal memory and with the same interface as in Figure 1. ${m}={f}_{c,0}({\mathrm{sim}},{\mathrm{id}},{\mathit{p}})$ is an instance of the built-in component. It is constructed before simulation starts given a reference to the simulation engine sim, a unique identification id of the instance, and the parameters

**p**. The hidden state derivatives ${\mathit{m}}_{{\dot{x}}_{1}}$ are computed in ${\mathit{f}}_{c,1}$ and ${m}_{{\dot{x}}_{2}}$ in ${\mathit{f}}_{c,2}$. In some cases, (new) local, algebraic variables ${\mathit{w}}_{3}$ need to be introduced to hide, e.g., state variables in ${\mathit{f}}_{c,\mathrm{eq}}$.

**Figure 4.**Equation-based model of a capacitor with parameter C, state v, connectors $p,n$ with potential variables ${p}_{v},{n}_{v}$ (electrical potentials), and flow variables ${p}_{i},{n}_{i}$ (electric currents).

**Figure 5.**Communication between the solver, the sorted and solved equations, and the functions of the built-in components. The state vector

**x**and the event indicators

**z**are split into an invariant and a variant part: $\mathit{x}=({\mathit{x}}^{\mathrm{inv}},{\mathit{x}}^{\mathrm{var}})$, $\mathit{z}=({\mathit{z}}^{\mathrm{inv}},{\mathit{z}}^{\mathrm{var}})$. The variant parts consist of ${\mathit{x}}_{j,1},{\mathit{x}}_{j,2},{\mathit{z}}_{j,1},{\mathit{z}}_{j,2}$ from all built-in components j (see Figure 3) present in the overall model. The dimensions of the invariant parts are fixed before simulation starts. The dimensions of the variant parts can change at events during simulation.

**Figure 6.**State machine of segmented simulation. The first mode $i=1$ is initialized with its start values. The ODEs of the actual mode i are solved until they are terminated or interrupted by a full restart zero-crossing event indicator ${z}_{{\mathrm{FR}}_{i},j}$ that becomes positive. In the latter case, the model is re-initialized in mode $i+1$. Hereby, variant variables can appear or disappear. The number of modes is unknown beforehand.

**Figure 8.**Object3D defined relative to its parent with translation and rotation. An Object3D can have one optional feature: Scene, Visual or Solid.

**Figure 9.**Internal execution scheme at initialization (

**segment 1**) and after re-initialization (

**segment 2**). (

**Segment 1**): Fourteen Object3Ds with different properties are defined: They are allowed to collide, can have a mass, are visible and/or can be connected by force elements. They are grouped into six super-objects that are disjunct via tree- and cut-joints; see [33]. SuperObject5 is an assembly with a lockable Object3D (obj10). Only the assembly root (obj9) is allowed to change joints and states during simulation. SuperObject2 is able to interact with this assembly via its rigidly attached locking mechanism (obj2). (

**Segment 2**): A full restart is triggered with

`ActionAttach(..., obj10, obj2)`to initialize the second segment if both locking mechanisms (obj10 and obj2) are close to each other with negligible relative velocity. The lockable obj10 identifies its assembly root, which is obj9. The joint and states of the assembly root obj9 are removed, and all Object3Ds of the assembly are attached to superObject2. This results in five rigidly attached super objects after re-instantiation.

**Figure 10.**A two-stage rocket (stage 1: blue cylinder, stage 2: red cylinder). (

**Left**): At initialization. (

**Right**): After separating (release). The locking mechanisms at the top and bottom are each visualized with a coordinate system.

**Figure 11.**Plots of relevant variables from the simulation of model TwoStageRocket of Listing 7. Variable values are not shown in the plots, if they are not defined in the respective phase.

**Figure 12.**(

**Left**) Scenario 1: A YouBot robot after releasing a free falling sphere (six DoF). The sphere will bounce on the grey plate. (

**Right**) Scenario 3: A YouBot transporting a box with segmented simulation.

**Table 1.**The capacitor is defined as a component with mathematical functions (left column) and with functions that have an internal memory (right column) to hide the state and the state derivative of the component in the equation section.

as component | as built-in component |
---|---|

(with math. functions) | (with functions of a prog. language that have internal memory m) |

equation section | |

$\begin{array}{cc}\hfill w& ={f}_{c,1}\left({v}\right)\hfill \\ \hfill w& ={p}_{v}-{n}_{v}\hfill \\ \hfill 0& ={p}_{i}+{n}_{i}\hfill \\ \hfill \dot{{v}}& ={f}_{c,2}(C,{p}_{i})\hfill \end{array}$ | $\begin{array}{cc}\hfill w& ={f}_{c,1}\left(m\right)\hfill \\ \hfill w& ={p}_{v}-{n}_{v}\hfill \\ \hfill 0& ={p}_{i}+{n}_{i}\hfill \\ & \phantom{\rule{0.277778em}{0ex}}{f}_{c,2}(m,{p}_{i})\phantom{\rule{3.33333pt}{0ex}}\phantom{\rule{3.33333pt}{0ex}}\#\phantom{\rule{3.33333pt}{0ex}}\mathrm{prog}.\phantom{\rule{4.pt}{0ex}}\mathrm{language}\phantom{\rule{4.pt}{0ex}}\mathrm{function}\phantom{\rule{4.pt}{0ex}}\mathrm{without}\phantom{\rule{4.pt}{0ex}}\mathrm{return}\phantom{\rule{4.pt}{0ex}}\mathrm{argument}\hfill \end{array}$ |

function definitions | |

$\begin{array}{cc}& \mathbf{function}\phantom{\rule{3.33333pt}{0ex}}{f}_{c,1}\left({v}\right)\hfill \\ & \phantom{\rule{3.33333pt}{0ex}}\phantom{\rule{3.33333pt}{0ex}}\phantom{\rule{3.33333pt}{0ex}}\phantom{\rule{3.33333pt}{0ex}}\mathbf{return}\phantom{\rule{3.33333pt}{0ex}}{v}\hfill \\ & \mathbf{function}\phantom{\rule{3.33333pt}{0ex}}{f}_{c,2}(C,{p}_{i})\hfill \\ & \phantom{\rule{3.33333pt}{0ex}}\phantom{\rule{3.33333pt}{0ex}}\phantom{\rule{3.33333pt}{0ex}}\phantom{\rule{3.33333pt}{0ex}}\mathbf{return}\phantom{\rule{3.33333pt}{0ex}}{p}_{i}/C\hfill \end{array}$ | $\begin{array}{cc}& \mathbf{function}\phantom{\rule{3.33333pt}{0ex}}{f}_{c,0}(\mathrm{sim},\mathrm{id},C)\phantom{\rule{3.33333pt}{0ex}}\phantom{\rule{3.33333pt}{0ex}}\phantom{\rule{3.33333pt}{0ex}}\phantom{\rule{3.33333pt}{0ex}}\phantom{\rule{3.33333pt}{0ex}}\phantom{\rule{3.33333pt}{0ex}}\#\phantom{\rule{3.33333pt}{0ex}}\mathrm{called}\phantom{\rule{4.pt}{0ex}}\mathrm{once}\hfill \\ & \phantom{\rule{3.33333pt}{0ex}}\phantom{\rule{3.33333pt}{0ex}}\phantom{\rule{3.33333pt}{0ex}}\phantom{\rule{3.33333pt}{0ex}}<\mathrm{allocate}\phantom{\rule{4.pt}{0ex}}\mathrm{new}\phantom{\rule{4.pt}{0ex}}\mathrm{record}\phantom{\rule{4.pt}{0ex}}m>\hfill \\ & \phantom{\rule{3.33333pt}{0ex}}\phantom{\rule{3.33333pt}{0ex}}\phantom{\rule{3.33333pt}{0ex}}\phantom{\rule{3.33333pt}{0ex}}{m}_{\mathrm{sim}}:=\mathrm{sim};\phantom{\rule{0.277778em}{0ex}}{m}_{\mathrm{id}}:=\mathrm{id};\phantom{\rule{0.277778em}{0ex}}{m}_{C}=C;\phantom{\rule{3.33333pt}{0ex}}\phantom{\rule{3.33333pt}{0ex}}\phantom{\rule{3.33333pt}{0ex}}\phantom{\rule{3.33333pt}{0ex}}\mathbf{return}\phantom{\rule{3.33333pt}{0ex}}m\hfill \\ & \mathbf{function}\phantom{\rule{3.33333pt}{0ex}}{f}_{c,1}\left(m\right)\hfill \\ & \phantom{\rule{3.33333pt}{0ex}}\phantom{\rule{3.33333pt}{0ex}}\phantom{\rule{3.33333pt}{0ex}}\phantom{\rule{3.33333pt}{0ex}}<\mathrm{copy}\phantom{\rule{4.pt}{0ex}}{m}_{v}\phantom{\rule{4.pt}{0ex}}\mathrm{from}\phantom{\rule{4.pt}{0ex}}\mathrm{states}\phantom{\rule{4.pt}{0ex}}\mathrm{in}\phantom{\rule{4.pt}{0ex}}{m}_{\mathrm{sim}}\phantom{\rule{4.pt}{0ex}}\mathrm{for}\phantom{\rule{4.pt}{0ex}}{m}_{\mathrm{id}}>;\phantom{\rule{3.33333pt}{0ex}}\phantom{\rule{3.33333pt}{0ex}}\phantom{\rule{3.33333pt}{0ex}}\phantom{\rule{3.33333pt}{0ex}}\mathbf{return}\phantom{\rule{3.33333pt}{0ex}}{m}_{v}\hfill \\ & \mathbf{function}\phantom{\rule{3.33333pt}{0ex}}{f}_{c,2}(m,{n}_{i})\hfill \\ & \phantom{\rule{3.33333pt}{0ex}}\phantom{\rule{3.33333pt}{0ex}}\phantom{\rule{3.33333pt}{0ex}}\phantom{\rule{3.33333pt}{0ex}}{m}_{\dot{v}}:={p}_{i}/{m}_{C};\phantom{\rule{3.33333pt}{0ex}}\phantom{\rule{3.33333pt}{0ex}}\phantom{\rule{3.33333pt}{0ex}}\phantom{\rule{3.33333pt}{0ex}}<\mathrm{copy}\phantom{\rule{4.pt}{0ex}}{m}_{\dot{v}}\phantom{\rule{4.pt}{0ex}}\mathrm{into}\phantom{\rule{4.pt}{0ex}}\mathrm{state}\phantom{\rule{4.pt}{0ex}}\mathrm{derivatives}\phantom{\rule{4.pt}{0ex}}\mathrm{of}\phantom{\rule{4.pt}{0ex}}{m}_{\mathrm{sim}}\phantom{\rule{4.pt}{0ex}}\mathrm{for}\phantom{\rule{4.pt}{0ex}}{m}_{\mathrm{id}}>\hfill \end{array}$ |

**Table 2.**Modia3D actions that trigger a full restart for a structural change. For applications, see Section 6.

Function | Description |
---|---|

ActionAttach(...) | Rigidly attaches the specified assembly. |

ActionReleaseAndAttach(...) | Changes one rigid connection to another rigid connection. |

ActionRelease(...) | Releases the specified assembly. |

ActionDelete(...) | Deletes the specified assembly. |

Function | Description |
---|---|

EventAfterPeriod(...) | Triggers an event after a specific period of time. |

ActionWait(...) | Waits a specific period of time. |

addReferencePath(...) | Adds a new reference path. |

ptpJointSpace(...) | Generates a point-to-point trajectory. |

Disclaimer/Publisher’s Note: The statements, opinions and data contained in all publications are solely those of the individual author(s) and contributor(s) and not of MDPI and/or the editor(s). MDPI and/or the editor(s) disclaim responsibility for any injury to people or property resulting from any ideas, methods, instructions or products referred to in the content. |

© 2023 by the authors. Licensee MDPI, Basel, Switzerland. This article is an open access article distributed under the terms and conditions of the Creative Commons Attribution (CC BY) license (https://creativecommons.org/licenses/by/4.0/).

## Share and Cite

**MDPI and ACS Style**

Neumayr, A.; Otter, M.
Modelling and Simulation of Physical Systems with Dynamically Changing Degrees of Freedom. *Electronics* **2023**, *12*, 500.
https://doi.org/10.3390/electronics12030500

**AMA Style**

Neumayr A, Otter M.
Modelling and Simulation of Physical Systems with Dynamically Changing Degrees of Freedom. *Electronics*. 2023; 12(3):500.
https://doi.org/10.3390/electronics12030500

**Chicago/Turabian Style**

Neumayr, Andrea, and Martin Otter.
2023. "Modelling and Simulation of Physical Systems with Dynamically Changing Degrees of Freedom" *Electronics* 12, no. 3: 500.
https://doi.org/10.3390/electronics12030500