# OpenModelica Compiler¶

The OpenModelica Compiler (OMC) consists of a frontend, backend, code generation and the runtimes.

**Lexical Analysis**Keywords, operators and identifiers are extracted from the model.

**Parsing**An abstract syntax tree represented in Meta-Modelica is created from the operators and identifiers.

**Semantic Analysis**The abstract syntax tree gets tested for semantic errors.

**Intermediate Representation**Translation of the abstract syntax tree to an intermediate representation called SCode in MetaModelica. This is further processed by the frontend producing DAE intermediate representation code.

**Symbolic Optimization Backend**The intermediate representation gets optimized and preprocessed.

**Code Generation**Executable code gets generated from the low level intermediate representation.

For more details see [FPA+20]. A full list of compiler flags can be found in OpenModelica Compiler Flags.

## Frontend Modules¶

## Backend Modules¶

**Pre-Optimization**Partitioning

Alias removal

**Causalization**Matching

Sorting

Index reduction

**Post-Optimization**Tearing

Jacobian

### Backend DAE Info¶

With compiler debug flag backenddaeinfo it is possible to get additional information from the Backend modules.

Number of equations / variables

Number of states

Information about initialization and simulation system

Equation types

Equation system details (linear and non-linear)

The output of backenddaeinfo can be expanded by using additional compiler debug flags stateselection and discreteinfo.

**Example**

```
>>> loadFile(getInstallationDirectoryPath() + "/share/doc/omc/testmodels/BouncingBall.mo")
true
>>> setCommandLineOptions("-d=backenddaeinfo,stateselection,discreteinfo")
true
>>> translateModel(BouncingBall)
true
"Notification: Model statistics after passing the front-end and creating the data structures used by the back-end:
* Number of equations: 6
* Number of variables: 6
Notification: Model statistics after passing the back-end for initialization:
* Number of independent subsystems: 3
* Number of states: 0 ()
* Number of discrete variables: 9 (v_new,$PRE.v_new,flying,$PRE.flying,foo,impact,$whenCondition1,$whenCondition2,$whenCondition3)
* Number of discrete states: 0 ()
* Number of clocked states: 0 ()
* Top-level inputs: 0
Notification: Strong component statistics for initialization (13):
* Single equations (assignments): 13
* Array equations: 0
* Algorithm blocks: 0
* Record equations: 0
* When equations: 0
* If-equations: 0
* Equation systems (not torn): 0
* Torn equation systems: 0
* Mixed (continuous/discrete) equation systems: 0
Notification: Model statistics after passing the back-end for simulation:
* Number of independent subsystems: 1
* Number of states: 2 (h,v)
* Number of discrete variables: 7 ($whenCondition3,$whenCondition2,$whenCondition1,flying,impact,v_new,foo)
* Number of discrete states: 2 (impact,v)
* Number of clocked states: 0 ()
* Top-level inputs: 0
Notification: Strong component statistics for simulation (9):
* Single equations (assignments): 7
* Array equations: 0
* Algorithm blocks: 0
* Record equations: 0
* When equations: 2
* If-equations: 0
* Equation systems (not torn): 0
* Torn equation systems: 0
* Mixed (continuous/discrete) equation systems: 0
"
```

## Code generation¶

From the low level intermediate representation from the backend code will be generated. The default code generation target is C and offers the largest model coverage. An alternative is the C++ (Cpp) which can produce significant faster executables in some cases.

The target language can be changed with compiler flag --simCodeTarget.

Depending on the target the compiler will write code and compile everything into a single simulation executable.

## Simulation Runtimes¶

The generated code is linked with the appropriate runtime.

### C Runtime¶

In Solving Modelica Models the methods implemented in the C runtime are described. In C Runtime Simulation Flags the runtime flags are documented.

### C++ Runtime¶

Solver methods and runtime flags are currently undocumented. Refer to the source code

### References¶

Peter Fritzson, Adrian Pop, Karim Abdelhak, Adeel Ashgar, Bernhard Bachmann, Willi Braun, Daniel Bouskela, Robert Braun, Lena Buffoni, Francesco Casella, Rodrigo Castro, Rüdiger Franke, Dag Fritzson, Mahder Gebremedhin, Andreas Heuermann, Bernt Lie, Alachew Mengist, Lars Mikelsons, Kannan Moudgalya, Lennart Ochel, Arunkumar Palanisamy, Vitalij Ruge, Wladimir Schamai, Martin Sjölund, Bernhard Thiele, John Tinnerholm, and Per Östlund. The OpenModelica Integrated Environment for Modeling, Simulation, and Model-Based Development. *Modeling, Identification and Control*, 41(4):241–295, 2020. doi:10.4173/mic.2020.4.1.