Hopsan
Automatically generating Component Code

Component Generator Dialog

Hopsan has a component skeleton generator functionality, from where new components can be written without deeper knowledge of the Hopsan simulation core interface. You will find it by choosing "Create external library" in the library widget. After a library has been created you can right click it and select to "Add new component". Note This functionality was previously present in the separate HoLC (Hopsan Library Creator) application, which has now been removed from Hopsan. The functionality is replaced by the generator and editor in the regular graphical user interface.

In this dialog it is possible to specify component type (used by simulation core, must be unique not contain spaces), display name (shown in graphical interface) and component type (C,Q or S - see the transmission line element method). It is also possible to add ports, parameters, utility functions and static variables. Finally, the code for the component can be written in two ways, either directly from C++ code, or from algebraic equations using a subset of the Modelica language.

When writing C++ code, there are three functions available: Initialize, SimulateOneTimeStep and Finalize. Initialize runs before the first time step of the simulation, SimulateOneTimeStep runs every time step, and finalize runs after the simulation is finished. Available and sought parameters are displayed above the code boxes. When clicking compile, the component will be converted to Hopsan interface syntax and compiled to an external library.

The other option is to use equations. This function is currently an experimental feature, and full functionality is not guaranteed. Equation-based modeling has several advantages over plain code modeling. It allows equations written in arbitrary ways, i.e. more than one variable on the left side. Equations can also be written in any order, as long as the number of equations equals the number of unknown variables. The language used is a subset of Modelica. It allows most basic functionality in the equation section. There is also an algorithm section, that will be executed prior to the equation solver each time step. This section does not support all Modelica algorithm functionality, and is basically intended only for defining explicit expressions that does not need to be part of the equation system. When compiling, a symbolic Jacobian matrix is generated and displayed in a popup window. This is then transformed to Hopsan syntax, together with a fixed-step equation system solver.

To be able to use the created components, you must have the Hopsan version with a compiler included or have configured Hopsan to use the system compiler (must be compatible, same used to build Hopsan).