C Code Generation
Turning a block diagram into standalone C99, and proving the C still solves the same problem.
What comes out
to_c lowers the assembled model to self-contained C99 — libm and nothing else. All state lives in one instance struct, so the code is reentrant and several generated models can link into the same binary. That is what makes it usable on a microcontroller, in a HIL rig, or inside an FMU.
The generated code is not a transcription of the Python. The model first becomes the typed IR, where every block is reduced to scalar operations; the C is emitted from that.
Simulation.to_c() returns the sources as a dict, and Simulation.verify_c() compiles them, integrates both the C and the engine over the same trajectory, and compares state by state.
The Model
A DC motor under PI speed control — a small system with a controller, an integrator and feedback, which is the shape most embedded models have:
MATHDISPLAY0ENDMATH
fastsim 0.25.0
The Reference Run
ω(2.0) = 98.353527 rad/s

Generating the C
to_c returns the sources keyed by file name.
motor.h 93 lines, 3.4 KiB motor.c 278 lines, 9.0 KiB
The interface
The header is the whole contract: one struct holding the state, an enum naming every addressable signal, and a handful of functions.
typedef struct {
double time; /* simulation time */
double x[2]; /* continuous states */
double sig[7]; /* block output signals */
double p[2]; /* tunable parameters */
} motor_t;
/* Addressable signals: states (read/write), outputs (read), params (read/write). */
enum {
MOTOR_SIG_Integrator = 0u,
MOTOR_SIG_ODE = 1u,
MOTOR_SIG_Source_y = 2u,
MOTOR_SIG_Integrator_y = 3u,
MOTOR_SIG_ODE_y = 4u,
MOTOR_SIG_Adder_y = 5u,
MOTOR_SIG_Amplifier_y = 6u,
MOTOR_SIG_Amplifier_y_1 = 7u,
MOTOR_SIG_Adder_y_1 = 8u,
MOTOR_SIG_Amplifier_p0 = 9u,
MOTOR_SIG_Amplifier_p0_1 = 10u,
MOTOR_SIG_COUNT = 11u
};
void motor_init(motor_t * restrict m);
void motor_run(motor_t * restrict m, double t_end, double dt);
/* Advance by exactly ONE step of `dt` (events + step + output refresh).
Fixed work per call — no time loops, no allocation — f
void motor_init(motor_t * restrict m); void motor_run(motor_t * restrict m, double t_end, double dt); void motor_step(motor_t * restrict m, double dt); double motor_get_signal(const motor_t * restrict m, uint16_t id); int motor_set_signal(motor_t * restrict m, uint16_t id, double value); void motor_jvp(motor_t * restrict m,
Nothing is global, so two instances of the model can run side by side in one process — the property that makes the code safe to drop into an RTOS task or an FMU.
Signals are reached by name through the enum rather than by digging into the struct layout: motor_get_signal(&m, MOTOR_SIG_ODE_y) is the motor's output wherever the code generator decided to put it.
Verification: Software in the Loop
Reading the C is not evidence. verify_c compiles it with a local C compiler, integrates the binary and the engine over the same trajectory, and compares the state vector at every step.
The figure it reports is a scaled error, MATHINLINE0ENDMATH, so a value below 1 means the C stayed inside the tolerance at every step of every state.
passed True compiler C:\MinGW\bin\gcc.exe n_steps 2000 n_states 2 max_scaled_error 5.477687598375871e-10 worst_state ODE worst_time 0.012000000000000004
Compiling and Running It Ourselves
The same C, driven the way an application would: write the files out, add a main that steps the model, compile, and run it.
compiler: C:\MinGW\bin\gcc.exe
#include <stdio.h>
#include "motor.h"
int main(void) {
motor_t m;
motor_init(&m); ...
compile: ok 2000 steps from the compiled binary, ω(end) = 98.352557 rad/s
at t = 2.000000 s engine : 98.352557002 rad/s C : 98.352557002 rad/s differ : 0.000e+00 rad/s

Code Generation Options
The same model lowers several ways. The axes are independent, and every combination is checked by the test suite against the engine.
double, hierarchical 371 lines double, flat 335 lines float, hierarchical 371 lines double, vectorized 383 lines
numeric="float" is the one that changes the answer rather than just the shape: single precision costs about seven digits, which matters on a target without a double-precision FPU. The verification above is what tells you whether that is affordable for a given model.