# A Modular, Extensible, and Modelica-Standard-Compliant OpenModelica Compiler Framework in Julia Supporting Structural Variability

^{*}

## Abstract

**:**

## 1. Introduction

- Expanded the description of the compiler design.
- Introduced initial support for handling variable structure systems.
- Improved the frontend to support a subset of the MSL.
- Expanded the performance experiments to not just include simulation time performance, but also compile time performance using a standard benchmark.
- Provided a performance experiment concerning VSS simulation.

## 2. Motivation and Research Aim

- RQ-1. Can a Modelica compiler written in the Julia language have reasonable performance?
- RQ-2. How can the Modelica language be extended to simulate variable structured systems?
- RQ-3. What are possible advantages when blurring the line between compilation, modeling and simulation?

## 3. Background

#### 3.1. Modelica Language and Modelica Tools

Listing 1. An example of how to model the RLC circuit in Figure 1 using MSL components. |

#### 3.2. MetaModelica

#### 3.3. The Julia Language

Listing 2. An example of a Julia function: addition of two variables. |

#### Equation-Based Modeling in Julia

#### 3.4. Variable Structure Systems in the Context of Equation-Based Languages

Listing 3. The ArrayGrow model. |

## 4. Related Work

#### 4.1. Mosilab

#### 4.2. Sol

Listing 4. A machine model with a structural change from ([29], p. 78). |

#### 4.3. Hydra

#### 4.4. Compiling Modelica: Model Composition Language and NanoModelica

#### 4.5. Other Related Work within the Context of Variable Structure Systems

## 5. OpenModelica.jl

#### 5.1. MetaModelica.jl

Listing 5. Original code written in MetaModelica to type check array expressions. |

Listing 6. A function used in the type checking phase of our Modelica compiler. This code make use of the @match equation constructs from MetaModelica; for comparison, the original MetaModelica code is available in Listing 5. |

#### 5.2. OMParser.jl

Listing 7. An example of how the parser could be used, in this case to parse the Modelica Standard
Library (MSL). |

#### 5.3. OMFrontend.jl

#### 5.4. Testing the Frontend by Using Flat-Modelica

Listing 8. A water tank model. |

Listing 9. The flat model of the water tank. |

#### 5.5. Library Support

#### 5.6. OMBackend.jl

#### 5.7. OpenModelica.jl as a Compiler Architecture

Listing 10. The Modelica HelloWorld model. |

Listing 11. Simulating a Modelica model in OpenModelica.jl. |

## 6. Extending the Modelica Language to Support Systems with Variable Structure

#### 6.1. Explicit Variable Structured Systems

`initialStructuralState(state)``structuralTransition(fromState, toState, condition)`

`initialStructuralState`represents an initial structural state, and

`structuralTransition`is used to specify the transition between one structural state to another such state.

`initialStructuralState`operator. In the case of the SimpleTwoModes model, this consists of the single variable, x.

Listing 12. An example of a simple explicit variable structured systems with two modes. |

#### Modeling a Breaking Pendulum Explicitly

Listing 13. An example of the breaking pendulum model using structural transitions. |

#### 6.2. Implicit Variable Structured Systems

Listing 14. The ArrayShrink model. |

Listing 15. The breaking pendulum model using the new recompilation keyword to activate and deactivate components via just-in-time compilation during simulation. |

## 7. Materials and Methods

#### Instrumentation

## 8. Results and Benchmarking

#### 8.1. Simulation of Large Modelica Models

#### 8.2. Evaluating Compile-Time Overhead

#### Evaluating the Cost of Structural Changes and JIT Compilation

Listing 16. SimpleClockArrayGrow. This model initially starts out with N equations and variables; however, every 15 s, the structure of the model changes and K new equations and variables are added to the system. |

#### 8.3. Summary

## 9. Discussion

- Automatically translating MetaModelica to Julia is possible.
- A Modelica compiler written in the Julia language is possible, and can have reasonable performance.
- The Modelica language can be extended to simulate variable structured systems (VSS) with minor changes.
- The advantage concerning the expressive power of a modeling language when blurring the lines between compilation, modeling and simulation.

#### 9.1. Comparison to Related Work

#### 9.2. Future Work

#### 9.3. Conclusions

## Author Contributions

## Funding

## Institutional Review Board Statement

## Informed Consent Statement

## Data Availability Statement

## Acknowledgments

## Conflicts of Interest

## Abbreviations

AST | Abstract Syntax Tree |

CPS | Cyber Physical System |

DAE | Differential Algebraic Equations |

EOOL | Equation-Based Object-Oriented Modeling Language |

HIR | High-level Intermediate Representation |

JIT | Just-In-Time |

LIR | Low-Level Intermediate Representation |

MIR | Mid-level Intermediate Representation |

MSL | Modelica Standard Library |

MTK | ModelingToolkit |

OMC | The OpenModelica Compiler |

VSS | Variable Structure Systems |

## Appendix A

#### Appendix A.1. Program to Simulate the Simple Two Modes Model

Listing A1. A program to simulate and plot SimpleTwoModes from Listing 12 using OpenModelica.jl with associated modules. |

#### Appendix A.2. Program to Simulate the Implicit Breaking Pendulum Model

Listing A2. Program to simulate the implicit breaking pendulum model, ArrayGrow and ArrayShrink models. |

#### Appendix A.3. The Flat Model of the RLC Circuit Produced by OpenModelica.jl When Using the Modelica Standard Library

Listing A3. The flat Modelica model of the RLC Circuit. |

#### Appendix A.4. The Cascading First Order System

Listing A4. The scalable Cascading first Order system from the ScalableTestSuite library. |

#### Appendix A.5. The Transmission Line Model

Listing A5. A Modelica model representing an electrical transmission line. |

## References

- 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] - Elmqvist, H.; Otter, M. Innovations for future Modelica. In Proceedings of the 12th International Modelica Conference, Prague, Czech, 15–17 May 2017; Linköping University Electronic Press: Linköping, Sweden, 2017. [Google Scholar] [CrossRef] [Green Version]
- Ma, Y.; Gowda, S.; Anantharaman, R.; Laughman, C.; Shah, V.; Rackauckas, C. ModelingToolkit: A Composable Graph Transformation System For Equation-Based Modeling. arXiv
**2021**. [Google Scholar] [CrossRef] - Fritzson, P.; Pop, A.; Abdelhak, K.; Asghar, A.; Bachmann, B.; Braun, W.; Bouskela, D.; Braun, R.; Buffoni, L.; Casella, F.; et al. The OpenModelica integrated environment for modeling, simulation, and model-based development. Model. Identif. Control
**2020**, 41, 241–295. [Google Scholar] [CrossRef] - Tinnerholm, J.; Pop, A.; Sjölund, M.; Heuermann, A.; Abdelhak, K. Towards an Open-Source Modelica Compiler in Julia. In Proceedings of the Asian Modelica Conference, Tokyo, Japan, 8–9 October 2020. [Google Scholar] [CrossRef]
- Tinnerholm, J.; Pop, A.; Heuermann, A.; Sjölund, M. OpenModelica.jl: A modular and extensible Modelica compiler framework in Julia targeting ModelingToolkit.jl. In Proceedings of the 14th International Modelica Conference, Linköping, Sweden, 20–24 September 2021; pp. 109–117. [Google Scholar] [CrossRef]
- Pop, A.; Östlund, P.; Casella, F.; Sjölund, M.; Franke, R. A new openmodelica compiler high performance frontend. In Proceedings of the 13th International Modelica Conference, Regensburg, Germany, 4–6 March 2019; pp. 689–698. [Google Scholar] [CrossRef] [Green Version]
- Antoulas, A.C.; Sorensen, D.C.; Gugercin, S. A Survey of Model Reduction Methods for Large-Scale Systems; Technical report; RICE University: Houston, TX, USA, 2000. [Google Scholar]
- Mikelsons, L.; Ji, H.; Brandt, T.; Lenord, O. Symbolic model reduction applied to realtime simulation of a construction Machine. In Proceedings of the 7th International Modelica Conference, Como, Italy, 20–22 September 2009; Casella, F., Ed.; Linköping University Electronic Press: Linköping, Sweden, 2009. [Google Scholar] [CrossRef] [Green Version]
- Davoudi, F.; Lenord, O.; Worschech, N.; Durak, U.; Hartmann, S. Redesign and evaluation of an equation-based model reduction method in OpenModelica. Int. J. Eng. Syst. Model. Simul.
**2019**, 11, 91–101. [Google Scholar] [CrossRef] - Rai, R.; Sahu, C.K. Driven by data or derived through physics? a review of hybrid physics guided machine learning techniques with cyber-physical system (cps) focus. IEEE Access
**2020**, 8, 71050–71073. [Google Scholar] [CrossRef] - Bruder, F.; Mikelsons, L. Modia and Julia for Grey Box Modeling. In Proceedings of the 14th International Modelica Conference, Linköping, Sweden, 20–24 September 2021; pp. 87–95. [Google Scholar] [CrossRef]
- Rackauckas, C.; Anantharaman, R.; Edelman, A.; Gowda, S.; Gwozdz, M.; Jain, A.; Laughman, C.; Ma, Y.; Martinuzzi, F.; Pal, A.; et al. Composing Modeling and Simulation with Machine Learning in Julia. In Proceedings of the 14th International Modelica Conference, Linköping, Sweden, 20–24 September 2021; pp. 97–107. [Google Scholar] [CrossRef]
- Minhas, R.; De Kleer, J.; Matei, I.; Saha, B.; Janssen, B.; Bobrow, D.G.; Kurtoglu, T. Using Fault Augmented Modelica Models for Diagnostics. In Proceedings of the 10th International Modelica Conference, Lund, Sweden, 10–12 March 2014; pp. 437–445. [Google Scholar] [CrossRef]
- Bäck, O. Modelling for Diagnosis in Modelica: Implementation and Analysis; Institutionen för Systemteknik: Linköping, Sweden, 2008; p. 65. [Google Scholar]
- 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; Springer: Berlin/Heidelberg, Germany, 2019; pp. 283–310. [Google Scholar] [CrossRef] [Green Version]
- Benveniste, A.; Caillaud, B.; Malandain, M. The mathematical foundations of physical systems modeling languages. Annu. Rev. Control
**2020**, 50, 72–118. [Google Scholar] [CrossRef] - Benveniste, A.; Caillaud, B.; Malandain, M. Handling Multimode Models and Mode Changes in Modelica. In Proceedings of the 14th International Modelica Conference, Linköping, Sweden, 20–24 September 2021; pp. 507–517. [Google Scholar] [CrossRef]
- Casella, F. Simulation of Large-Scale Models in Modelica: State of the Art and Future Perspectives. In Proceedings of the 11th International Modelica Conference, Palais des Congrès de Versailles, France, 21–23 September 2015; pp. 459–468. [Google Scholar] [CrossRef] [Green Version]
- Broman, D. Interactive Programmatic Modeling. ACM Trans. Embed. Comput. Syst. (TECS)
**2021**, 20, 1–26. [Google Scholar] [CrossRef] - Peffers, K.; Tuunanen, T.; Rothenberger, M.A.; Chatterjee, S. A design science research methodology for information systems research. J. Manag. Inf. Syst.
**2007**, 24, 45–77. [Google Scholar] [CrossRef] - Tinnerholm, J.; Sjölund, M.; Pop, A. Towards introducing just-in-time compilation in a Modelica compiler. In Proceedings of the 9th International Workshop on Equation-based Object-oriented Modeling Languages and Tools, Berlin, Germany, 5 November 2019; pp. 11–19. [Google Scholar] [CrossRef]
- Fritzson, P. Principles of Object-oriented Modeling and Simulation with Modelica 3.3: A Cyber-Physical Approach; John Wiley & Sons: Hoboken, NJ, USA, 2014. [Google Scholar]
- Pop, A.; Fritzson, P. MetaModelica: A Unified Equation-Based Semantical and Mathematical Modeling Language. In Proceedings of the Joint Modular Languages Conference, Oxford, UK, 13–15 September 2006; Springer: Berlin/Heidelberg, Germany, 2006; pp. 211–229. [Google Scholar] [CrossRef]
- 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] [Green Version] - 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]
- Cellier, F.E.; Kofman, E. Continuous System Simulation; Springer Science & Business Media: New York, NY, USA, 2006. [Google Scholar]
- Nytsch-Geusen, C.; Ernst, T.; Nordwig, A.; Schneider, P.; Schwarz, P.; Vetter, M.; Wittwer, C.; Holm, A.; Nouidui, T.; Leopold, J.; et al. MOSILAB: Development of a Modelica based generic simulation tool supporting model structural dynamics. In Proceedings of the 4th International Modelica Conference, Hamburg, Germany, 7–8 March 2005. [Google Scholar]
- Zimmer, D. Equation-Based Modeling of Variable-Structure Systems. Ph.D. Thesis, ETH Zürich, Zurich, Switzerland, 2010. [Google Scholar] [CrossRef]
- Giorgidze, G. First-Class Models: On A Noncausal Language for Higher-Order and Structurally Dynamic Modelling and Simulation. Ph.D. Thesis, University of Nottingham, Nottingham, UK, 2012. [Google Scholar]
- Nilsson, H.; Peterson, J.; Hudak, P. Functional hybrid modeling. In Proceedings of the International Symposium on Practical Aspects of Declarative Languages, New Orleans, LA, USA, 13–14 January 2003; Springer: Berlin/Heidelberg, Germany, 2003; pp. 376–390. [Google Scholar]
- Höger, C. Compiling Modelica: About the Separate Translation of Models from Modelica to OCaml and Its Impact on Variable-Structure Modeling. Doctoral Thesis, Technische Universität Berlin, Berlin, Germany, 2019. [Google Scholar] [CrossRef]
- Broman, D. Meta-Languages and Semantics for Equation-Based Modeling and Simulation. Ph.D. Thesis, Linköping University Electronic Press, Linköping, Sweden, 2010. [Google Scholar]
- 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]
- 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] - Lattner, C.; Adve, V. LLVM: A compilation framework for lifelong program analysis & transformation. In Proceedings of the International Symposium on Code Generation and Optimizatio, CGO, San Jose, CA, USA, 20–24 March 2004; IEEE: Piscataway, NJ, USA, 2004; pp. 75–86. [Google Scholar] [CrossRef]
- Nystrom, N.; Clarkson, M.R.; Myers, A.C. Polyglot: An extensible compiler framework for Java. In Proceedings of the International Conference on Compiler Construction, Warsaw, Poland, 7–11 April 2003; Springer: Berlin/Heidelberg, Germany, 2003; pp. 138–152. [Google Scholar] [CrossRef] [Green Version]
- Lee, S.; Min, S.J.; Eigenmann, R. OpenMP to GPGPU: A compiler framework for automatic translation and optimization. ACM Sigplan Not.
**2009**, 44, 101–110. [Google Scholar] [CrossRef] - Fritzson, P.; Pop, A.; Sjölund, M. Towards Modelica 4 Meta-Programming and Language Modeling with MetaModelica 2.0; Technical Report 2011:10, Linköping University, PELAB–Programming Environment Laboratory: Linköping, Sweden, 2011. [Google Scholar]
- Fritzson, P.; Pop, A.; Sjölund, M.; Asghar, A. MetaModelica—A Symbolic-Numeric Modelica Language and Comparison to Julia. In Proceedings of the 13th International Modelica Conference, Regensburg, Germany, 4–6 March 2019. [Google Scholar] [CrossRef] [Green Version]
- Parr, T.J.; Quong, R.W. ANTLR: A predicated-LL (k) parser generator. Softw. Pract. Exp.
**1995**, 25, 789–810. [Google Scholar] [CrossRef] - Hindmarsh, A.C.; Brown, P.N.; Grant, K.E.; Lee, S.L.; Serban, R.; Shumaker, D.E.; Woodward, C.S. SUNDIALS: Suite of nonlinear and differential/algebraic equation solvers. ACM Trans. Math. Softw. (TOMS)
**2005**, 31, 363–396. [Google Scholar] [CrossRef] - Braun, W.; Casella, F.; Bachmann, B. Solving large-scale Modelica models: New approaches and experimental results using OpenModelica. In Proceedings of the 12th International Modelica Conference, Prague, Czech, 15–17 May 2017; Linköping University Electronic Press: Linköping, Sweden, 2017; pp. 557–563. [Google Scholar] [CrossRef] [Green Version]
- Christ, S.; Schwabeneder, D.; Rackauckas, C. Plots.jl—A user extendable plotting API for the julia programming language. arXiv
**2022**, arXiv:2204.08775. [Google Scholar] [CrossRef] - Muchnick, S. Advanced Compiler Design & Implementation; Morgan Kaufmann: San Mateo, CA, USA, 1997. [Google Scholar]
- Tsitouras, C. Runge–Kutta pairs of order 5 (4) satisfying only the first column simplifying assumption. Comput. Math. Appl.
**2011**, 62, 770–775. [Google Scholar] [CrossRef] [Green Version] - Chen, J.; Revels, J. Robust benchmarking in noisy environments. arXiv
**2016**, arXiv:1608.04295. [Google Scholar] [CrossRef] - Marzorati, D.; Fernández, J.; Kofman, E. Efficient connection processing in equation–based object–oriented models. Appl. Math. Comput.
**2022**, 418, 126842. [Google Scholar] [CrossRef] - Henningsson, E.; Olsson, H.; Vanfretti, L. DAE Solvers for Large-Scale Hybrid Models. In Proceedings of the 13th International Modelica Conference, Regensburg, Germany, 4–6 March 2019. [Google Scholar] [CrossRef] [Green Version]
- Donida, F.; Casella, F.; Ferretti, G. Model order reduction for object-oriented models: A control systems perspective. Math. Comput. Model. Dyn. Syst.
**2010**, 16, 269–284. [Google Scholar] [CrossRef]

**Figure 1.**An RLC circuit that is connected to a sine voltage source. The figure is based on the simple circuit model in ([23], p. 37).

**Figure 2.**An overview of the dependencies between the components in OpenModelica.jl. Absyn.jl, SCode.jl and DAE.jl are the compilers’ intermediate representations. The compiler runtime is implemented by MetaModelica.jl and auxiliary libraries. The frontend is provided by OMFrontend.jl and the backend is defined by OMBackend.jl.

**Figure 3.**A high-level overview of a design separating the intermediate representation from the frontend to allow several hypothetical frontends to use the same backend.

**Figure 4.**A mapping of the compiler components in OpenModelica.jl to a modern compiler architecture. OMParser.jl is responsible for parsing and lexing, OMFrontend.jl is responsible for semantic analysis and optimization of the high-level intermediate representation (HIR). Finally, OMBackend.jl performs the final code optimization and generation. This figure is adapted from ([45], p. 8).

**Figure 5.**The compilation process of a JIT capable Modelica compiler. During simulation, a structural change may trigger recompilation, and re-initialization of the system that is simulated.

**Figure 7.**The result of simulating Listing 13. Note that the legend provides the prefix of the last active model which was FreeFall. The behavior before the structural transition at $t=5$ is described by the equations of the pendulum model.

**Figure 8.**The results of simulating Listing 3 (Top) and Listing 14 (Bottom). The structural change and subsequent recompilation of the model occur at $t=0.5$ seconds. The curve in the graph represents all ${x}_{i}$ variables of $\underrightarrow{x}$ in Listing 3 and Listing 14.

**Figure 9.**The result of simulating the breaking pendulum using recompilation during simulation. The code to obtain this plot is presented in Listing A2.

**Figure 10.**Time spent during numerical simulation for OpenModelica.jl (pink) and for OMC (blue). Lower is better.

**Figure 11.**Time spent translating the transmission line model in Listing A5 to flat Modelica for OMFrontend.jl (pink) and for OMC (blue). Lower is better.

**Figure 12.**Median time spent in seconds in frontend, backend and code generation phases when simulating the model in Listing 16.

Publisher’s Note: MDPI stays neutral with regard to jurisdictional claims in published maps and institutional affiliations. |

© 2022 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**

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.
https://doi.org/10.3390/electronics11111772

**AMA Style**

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(11):1772.
https://doi.org/10.3390/electronics11111772

**Chicago/Turabian Style**

Tinnerholm, John, Adrian Pop, and Martin Sjölund.
2022. "A Modular, Extensible, and Modelica-Standard-Compliant OpenModelica Compiler Framework in Julia Supporting Structural Variability" *Electronics* 11, no. 11: 1772.
https://doi.org/10.3390/electronics11111772