Skip to content

Examples

anvil ships with full application examples and minimal integration tests. Together they cover every major feature and serve as starting points for your own projects.

Examples

Full applications demonstrating real use cases. Each example has its own README with detailed explanations.

Tracking NMPC

examples/tracking_nmpc/

A closed-loop NMPC application that tracks a centerline trajectory around a Formula Student track. Demonstrates a complete optimal control workflow:

  • MultistageProblem + SparseSQPFunction for nonlinear MPC
  • Runtime parameters for initial state and reference trajectory
  • RK4 dynamics discretization of a kinematic bicycle model
  • Comparison against a CasADi/Ipopt baseline

Unbumpercars

examples/unbumpercars/

A Control Barrier Function (CBF) safety filter for bumper car racing. Demonstrates using neural network models (MLP with learned weights) inside an OCP formulation -- the NN weights are captured as Tensor constants in the dynamics function, and anvil differentiates through the full model for the SQP solver.

Integration tests

tests/integration/

Minimal end-to-end examples that generate C++ code, compile it, and verify correctness. Each test has a codegen.py that generates the code and a CMake project that compiles and runs it. These are the simplest references for each feature:

Test Feature
do_square/ Basic NumericalFunction + sparse Jacobian codegen
cumsum_sin/ Cumulative sum and trigonometric ops + sparse Jacobian
basic_dense_sqp/ Dense SQP solver with equality constraints
basic_sparse_sqp/ Sparse SQP solver
bicycle_ocp/ MultistageProblem for a bicycle model OCP
lti_ocp/ Linear time-invariant system OCP