Skip to content

C Code

SIMBA 2021.01 Introduced C Code support for control systems. The "C Code" model allows the user to write C Code directly in SIMBA to build custom control models.

The "C Code" model is available in the Control>Code library.

Get started

Simply click the "Edit" button of a C Code device to access the code editor.

C Code

The C code is compiled when the simulation starts after each code change. The interface of the C Code is composed of three main functions described below:

void initialize()

initialize() is called once at the beginning of the simulation. All the variables used in the model must be initialized and resources must be allocated here.

void calculate_outputs(double* outputs, double* inputs, double time, double time_step)

calculate_outputs is called once at each time step to calculate model outputs. Function parameters are listed below:

  • outputs: array of outputs to be calculated. Array size is equal to the number of outputs.
  • inputs: array of inputs values. Array size is equal to the number of inputs.
  • time: current simulation time
  • time_step: current simulation time-step


In the case of zero-crossing interpolation (switching), a time point is added at the zero-crossing time (even using the fixed-time step solver).

void terminate()

terminate() is called once at the end of the simulation. The resources allocated in the initialize function must be released here.

Model data

It is recommended to place model variables in the model_data structure. This allows the predictive time-step solver to take and restore snapshots of the model state.

Compilation errors

Compilation error messages are displayed in the top console.

Compiler installation

The first time a C Code model is used, SIMBA will install the compiler. This task is indicated in the console and lasts about 30 seconds.

Time-Step Control

The C Code device does not control the time step in SIMBA. To call the C Code model at a given frequency, an external clock can be used (see SR Buck Converter Design example).

Runtime errors

SIMBA will likely crash in case of runtime error (like any other program). You can use the (debug) feature to investigate the cause of the issue.

Snapshot and Time-travel

With the predictive time-step solver, the simulation time does not always elapse in one direction. Therefore, The C Code model needs to support time-travel and a snapshot mechanism is implemented with the snapshot function. The predictive time-step solver uses the snapshot function to restore a model to a previous state when needed. it is not recommended to modify this function.

Under the hood

The C Code in SIMBA is not interpreted but is compiled using a powerful compiler (GCC 64-bit). This gives major advantages:

  • Possibility to include your own header (#include "MyCode.h"...)
  • Optimal runtime performance
  • Possibility to debug your code


It is possible to debug your code using the C Code (External File) model and an external debugger (more information).