AOT compilation#

pybamm.codegen.compilation.aot_compile(fn_or_fns, **kwargs)[source]#

Ahead-of-time compile one or more casadi Function objects to a single shared library and return casadi.external wrappers.

Accepts either a single casadi.Function (returns a Function) or a list/tuple of Functions (returns a list, one per input, in order). In either case everything is lowered in one CodeGenerator / gcc invocation – a single fn is a bundle of size one.

Intended for the outermost Functions a solver hands off (e.g. rhs_algebraic, jac_times_cjmass, rootfn, output-variable evaluators). Intermediate Functions should stay as MX/SX so casadi.CodeGenerator can inline them into one translation unit. Wrapping inner Functions as Externals forces cross-dylib dispatch and produces unresolvable extern declarations.

Results are cached in-process (by a hash of the serialised forms) and on disk under $PYBAMM_CASADI_AOT_CACHE (default $TMPDIR/pybamm_casadi_aot). Inputs already of class External are returned unchanged. On any failure, the original Function(s) are returned and a warning is logged.

Parameters:
  • fn_or_fns (casadi.Function or list of casadi.Function)

  • **kwargscache_dir, compiler and flags overrides.

Environment variables#

PYBAMM_CASADI_AOT_CACHE

On-disk cache directory. Defaults to $TMPDIR/pybamm_casadi_aot.

PYBAMM_CASADI_AOT_KEEP_C

If set, retain the generated .c source next to each compiled library. Useful for debugging codegen output.