Architecture¶
nervus/
core/ Block, ports, Param, SampleTime, Diagram, SimulationSettings, errors (pure Python)
blocks/ the block library and the registry (type_id -> class)
sim/ compiler (diagram -> CompiledModel), simulator loop, SimResult
solvers/ fixed-step (Euler, RK4) and variable-step (SciPy solve_ivp) integration
io/ model file load/save, schema migration
codegen/ C code generation (generator, per-block templates) and software-in-the-loop testing
help.py block help / reference generation
gui/ PySide6 desktop editor, Model-View-Controller
models/ DiagramModel, SimulationModel: Qt wrappers that emit change signals
views/ canvas, library, properties, diagnostics, scope, help (render and forward input)
controllers/ EditorController (undoable commands), FileController, SimulationController (worker thread)
Rule: nothing outside nervus.gui imports Qt. The engine must run headless in scripts, tests and CI.
Data flow¶
Diagram ──compile_diagram()──▶ CompiledModel ──simulate()──▶ SimResult
(editable) (sorted, sized, states (time + logged signals)
allocated; executable)
Compilation (nervus.sim.compiler)¶
Diagram.check(): every input connected, no dangling ports.- Width propagation: repeatedly ask each block
output_widths(input_widths)until nothing changes. Blocks with state (Integrator, Unit Delay) give a provisional width so feedback loops resolve. configure(in_widths, out_widths)on each block, which validates sizes.- Sorting: a topological sort over direct-feedthrough edges. A cycle is an algebraic loop.
- Sample times: inherited (
-1) times are resolved from the drivers. - States: the continuous states of all blocks are packed into one vector for the solver. Discrete states are kept per block.
The CompiledModel is also where C code generation will start: it holds a sorted list of blocks with known
sizes, rates and states.
Simulation (nervus.sim.simulator)¶
The simulator owns all state. Blocks are pure: outputs, derivatives and update get the state passed
in and return new values. This lets variable-step solvers evaluate trial points and throw them away, and it
maps directly onto generated C (model_step() with a state struct).
Discrete rates are scheduled with integer counters (offset + k·period), so sample hits never drift.
GUI (Model-View-Controller)¶
user ──▶ View ──intent──▶ Controller ──command──▶ Model ──signal──▶ View
(canvas, (EditorController, (DiagramModel (scene adds/moves
panels) QUndoStack) wraps Diagram) items, panels refresh)
- Models (
gui/models):DiagramModelwraps the coreDiagram. Its methods are primitive edits (add_block,set_params,add_connection, ...), and each emits a Qt signal (blockAdded,paramsChanged,connectionAdded, ...).SimulationModelholds the run state and the latestSimResult. - Views (
gui/views):DiagramScene/DiagramView(canvas),LibraryBrowser,PropertyPanel,DiagnosticsPanel,ScopeWindow,HelpBrowser. Views never change a model directly. A finished drag becomescontroller.move_blocks(...), a drawn wire becomescontroller.connect_ports(...), and so on. - Controllers (
gui/controllers):EditorControllerturns every edit into aQUndoCommand(seecommands.py), so everything is undoable.FileControllerhandles new/open/save and recent files.SimulationControllercompiles on the GUI thread and simulates on a workerQThread; editing is locked while a run is active.
Commands catch their own exceptions and mark themselves obsolete, because an exception raised in a Python
redo() does not propagate out of QUndoStack.push().
Help, equations and figures¶
Block help comes from the block docstrings (nervus.help). The same Markdown feeds:
- the MkDocs block reference (
scripts/gen_block_reference.py), where$$...$$is rendered by MathJax, and - the in-app
HelpBrowser, where$$...$$is typeset to images with matplotlib's math engine (gui/views/math_render.py), since Qt's Markdown renderer has no math support.
Figures referenced as figures/<name>.png are generated by scripts/gen_figures.py. They go into
gui/resources/figures (bundled with the app) and docs/images/blocks (docs). Screenshots in the docs are
generated by scripts/screenshots.py.
Code generation¶
nervus.codegen.generate_c(target) compiles the target (a model or a subsystem's system) with the normal
compiler, so the generated code uses exactly the simulation's execution order, widths and sample times.
CGenerator then asks each block's template (codegen/templates.py, registered with @template) for
its output and update statements. An Emitter gives templates access to input and output signals,
parameters (registered in <name>_P) and states (in <name>_DW).
The C step function mirrors one major step of the simulator: outputs in execution order (discrete blocks
only at their rate's hits), then state updates, then the rate counters. codegen.sil.run_python() steps the
compiled Python model the same way, and back_to_back() compares the two. The test suite runs this for every
block with a template.