nflow_codegenerate
Generate standalone C or Rust code from an nflow model.
📝Syntax
nflow_codegenerate(model)
nflow_codegenerate(model, destination)
nflow_codegenerate(model, destination, includeMain)
nflow_codegenerate(model, destination, includeMain, lang)
r = nflow_codegenerate(model, destination, includeMain, lang, options)
📥Input Arguments
Parameter Description
model a string: path of the .nflow model file.
destination a string: destination directory (a subdirectory named after the model is created). Default: the model's directory.
includeMain a logical: also emit a runnable main (CSV writer) and build scaffolding (CMakeLists.txt for C, Cargo.toml for Rust). Default: true.
lang a string: 'C' (default) or 'rust'.
options a scalar struct: fields 'build' (logical, compile the generated code via CMake / cargo), 'buildConfig' ('Release' default), 'failOnWarning' (logical, turn codegen warnings into errors).
📤Output Arguments
Parameter Description
r a struct: 'dir' (generated directory) and 'warnings' (cell of codegen warnings, e.g. a solver downgrade).
📄Description

nflow_codegenerate lowers an nflow diagram to standalone C or Rust with no runtime dependency: a ModelState struct, nflow_init / nflow_step / nflow_terminate, one ModelInput field per external label source and one ModelOutput field per external label sink (and per fileSink).

Solvers. Fixed-step ode1 (per-block forward Euler) and ode4 (a single global Runge–Kutta 4 over the packed continuous state), plus the embedded adaptive ode45 (Dormand–Prince 5(4)). Under ode4 / ode45 zero-crossing surfaces (bounded integrator, saturation, dead zone, switch, step) are localized by bisection exactly like the simulator. Runtime-only solvers (dae, ...) are rejected. When a block cannot join the RK4 scheme the model is demoted to Euler with an explicit solverDowngrade warning; ode1 warns that crossings are not localized.

Coverage. Discrete, math, logic, lookup (splines included), routing, bus and complex lowering, small integer/boolean quantization, exact 64-bit integer lanes, multirate discrete blocks, acausal islands (linear, embedded-Newton and joint-free planar mechanics), and conditional subsystems (enable, trigger, reset, action gates). Unsupported constructs are rejected with a typed message naming the offending block, never silently-wrong code.

Verification. The generated code is validated locally by the numeric harness (nflow_codegen_check_numeric): compile, run, and compare trajectories sample for sample against __nflow_simulate__.

💡Examples
Generate C, then Rust, from a model file.
% model = 'C:/models/lowpass.nflow';
% nflow_codegenerate(model, tempdir(), true, 'C');
% nflow_codegenerate(model, tempdir(), true, 'rust');
% r = nflow_codegenerate(model, tempdir(), true, 'C', struct('build', true));
🔗See Also
sim
🕔Version History
Version Description
1.0.0 initial version
Edit this page on GitHub