Model Parameters and Code Generation
Learn about the code generation process and the role of parameters.
Code generation is a utility that can be applied to a Super Block to generate a C code realizing the behavior of the Super Block, and essentially converting the Super Block into a basic block with a C simulation function. Replacing the Super Block with the block obtained from code generation is done mainly for hiding the content of the Super Block (IP protection) or for performance improvement. Code generation can also be used to generate FMUs for usage in other simulation tools. Code generation may also be performed to create C code for embedded applications.
When code is generated for a Super Block (or more specifically for the diagram inside), the resulting block will have the same inputs and outputs as the original Super Block. Depending on the application, the user may want to expose, or not, some of the diagram parameters. If parameters are not exposed, their numerical values are used during the generation of the code, resulting in a high-performance code. The drawback is that if a parameter is to be changed, the code must be re-generated. So, in many situations, it is desirable to expose some diagram parameters and allow their modifications in the generated code.
Not all parameters can be exposed. For some parameters, the value can be changed without requiring re-compilation of the model and thus re-generation of the code; for others, in particular for any parameter affecting the structure of the model, or data types and sizes, it cannot. Parameters that can be exposed are called exposable parameters.
A diagram in general contains many potentially exposable parameters, but users often want to select a subset of them to expose. There are various ways to select the exposable parameters. The main method is closely related to the auto-masking operation presented previously.
Choice of Code Generator
Activate provides two code generation facilities: the standard code generator, which uses the block simulation functions, and one that inlines the block codes producing efficient compact code with very little dependency. The former generator supports all the blocks available in Activate, including Custom blocks, FMU blocks and Modelica components. Since for the blocks, the generated code uses the same code used by the simulator, its behavior is, in most cases, identical to that of the simulation code.
Generating inlined code on the other hand, used for producing simple efficient code with little dependency, is only possible under certain conditions. In particular, the Super Block cannot include blocks interacting with the system, such as Scope blocks using graphics, or Custom blocks relying on interpreters (OML, Python). Similarly, blocks producing activation signals are not supported.
The choice of the generator then depends on the application, the target, and the complexity of the Super Block. The Atomic option must be used when possible, especially for generating inlined code, to reduce the complexity of the code generation process and the generated code. Both generators support exposable parameters.