Both the target configuration file and the code generation dialog revealed the most important part of the 20-sim code generation process, the use of tokens!
A token is a placeholder for model-dependent information. For instance in the code generation dialog, the target destination directory contains the name of the selected submodel by default. Since this information is not yet known when the targets.ini file is created, a specific token that refers to this name is used instead.
The idea is to create targets from template source files that contain tokens instead of actual model-dependent information (like equations, names, parameters, inputs etc). The files that are specified in a target section will be scanned for these tokens and tokens will be replaced by the corresponding model-dependent information.
The following variables should be declared in the C-Code template, because 20-sim uses these names in the generation of the equations:
c: constant array
P: parameters array
V: variables array
s: states array
R: rates array
M: matrix array
U: unnamed variables array
F: favorite variables array
f: favorite parameters array
Note that these parameters are case-sensitive!
The following tokens will be replaced by numbers indicating the number of model parameters etc., typically used in memory allocation parts and loops.
The number of constants in the model
The number of parameters in the model
The number of variables in the model
The number of states in the model
The number of inputs of the model
The number of outputs of the model
The number of matrices used in the model
The number of unnamed variables in the model
The number of favorite parameters in the model
The number of favorite variables in the model
The size of the largest workarray necessary in calculating some matrix functions
The following tokens are already reserved for future use:
The number of dependent states in the model
The number of algebraic loop variables pairs in the model
The number of constraint variable pairs in the model
The number of import variables in the model
The number of export variables in the model
If you want to use parameters etc. that are specific to a model you can use arrays of names using the following tokens:
All the constants used in the model.
All the parameters used in the model.
All the variables used in the model.
All the states used in the model.
All the rates used in the model.
All the dependent states used in the model.
All the dependent rates used in the model.
All the algebraic loop variables used in the model.
All the constraint variables used in the model.
All the inputs used in the model.
All the outputs used in the model.
All the matrices used in the model.
All the favorite parameters used in the model.
All the favorite variables used in the model.
An important part of a model are the parameters, initial values states etc. To match these with the generated model code and use the correct values the following tokens can be used:
The following tokens can be used to place 20-sim simulation model equations into the C-Code:
Equations that should be calculated once for initialization of the model
Equations that should be calculated once
Equations that should be calculated once at the beginning of every simulation step
Equations that calculates the dynamic part of the model, calculates the rates
Equations that should be calculated once at the end of every simulation step
Reduced set of equations that should be calculated at the end of every simulation step
Equations that should be calculated once for termination of the model
To match model inputs and model outputs to for example sensor signals and actuator signals you can use:
A string representing the name of the selected integration method, for now only Euler, RungeKutta4 and Discrete are available.
The name of the simulation time variable (combined with %VARPREFIX%)
The name of the variable that indicates the initialization phase(combined with %VARPREFIX%).
Start time of the simulation (floating point notation).
Finish time of the simulation (floating point notation).
Step size of the simulation (floating point notation).
Either TRUE or FALSE, depending if the model is discrete in time or continuous in time.
Tokens which hold information about the model, experiment, user and system:
The file name of the generated C-Code.
The complete path and file name of the original 20-sim model.
The name of the 20-sim model out of which C-Code was generated.
The name of the 20-sim submodel out of which C-Code was generated.
The experiment name accompanying the 20-sim (sub)model.
The time of C-Code generation.
The date of C-Code generation.
The 20-sim version and build number.
The directory where the generated C-Code files are stored.
The user name given in the 20-sim license.
The company name given in the 20-sim license.
The directory where 20-sim is installed.