odelia is an ODE solver implemented in C++ header files, with an interface to R via Rcpp. Four adaptive steppers (Cash-Karp RK4(5), Dormand-Prince 5(4), and for stiff systems RODAS4(3) and the Rosenbrock-W ROS34PW2) run entirely in compiled code, so it is fast. ODE systems are templated on their scalar type, so a run can be recorded and differentiated exactly, for optimisation and calibration: in reverse mode, one sweep over the recorded run gives the derivative of a solution with respect to every parameter and initial condition at once; in forward mode, the run’s steps are walked again carrying a few directions, which is cheaper for a few parameters and many output times, and works under every stepper but ROS34PW2.
The core solver was first developed by Rich FitzJohn as part of the plant package. This package spins that code out so it can be used more widely.
Features
- Four adaptive steppers running entirely in C++: explicit Cash-Karp RK4(5), Dormand-Prince 5(4) with dense output, the implicit RODAS4(3) for stiff systems, and ROS34PW2, a Rosenbrock-W method of order 3 that keeps its Jacobian across steps, for a stiff system whose Jacobian costs many right-hand sides (on regnans’s two-trait canonical equation it formed 3 Jacobians where RODAS formed 21). A compiled system solves about a hundred times faster than the same right-hand side written in R under an explicit stepper (Lorenz: 1 ms against 25-60 ms under
deSolve). On a stiff problem the comparison is RODAS againstdeSolve::lsoda, which switches to a multistep method and takes far fewer steps: there the gain is nearer fifteen times (the shipped consumer-resource model with the resource turning over fifty times faster than the consumer, 96 output times: 0.05 ms against 0.7 ms). - Reverse-mode automatic differentiation of a recorded run, via the vendored XAD library: one sweep, every parameter and initial condition. Sweeps need an explicit stepper (Cash-Karp or Dormand-Prince). See the article Reverse mode.
-
Forward-mode sensitivities
dy(t)/dθat every output time, under every stepper but ROS34PW2, RODAS included: cheaper than a sweep for up to a few tens of parameters. See the article Forward mode. - External drivers: time-varying forcing variables, interpolated with a cubic Hermite spline and queried by the system at each step.
- A C++ core (header-only but for the XAD tape runtime) that other packages link against, with no R needed to compile or test it.
- Friendly R6 wrappers around the C++ objects.
- A right-hand side written in R can be solved by the same steppers, through
ode_solve()(shaped likedeSolve::ode()) or the step-at-a-timeOdeSolver, with genuinely adaptive Runge-Kutta stepping, dense output of the method’s order (Dormand-Prince 5(4)), the accepted steps exposed, and a count of what the solve cost. On a fine output grid that is about twice as fast asdeSolve::ode45with the same R function.
Installation
odelia compiles C++ from source, so you need a working C++ toolchain (Rtools on Windows, Xcode command-line tools on macOS, a recent g++/clang on Linux). Then:
# install.packages("remotes")
remotes::install_github("traitecoevo/odelia")Quick start
odelia ships with one ready-to-run system, the classic Lorenz system, so you can try the solver out of the box. It is bundled purely as a demonstration — to model your own problem you write a small C++ system class (and a thin Rcpp/R6 interface) that the same solver then drives. See Building your own model for a complete worked example.
The example below solves the bundled Lorenz system. (A right-hand side written in R can be solved without any C++ at all; see ode_solve() and vignette("odelia").)
library(odelia)
# Define the system (parameters) and its initial state
lz <- LorenzSystem$new(sigma = 10, R = 28, b = 8 / 3)
lz$set_state(c(1, 1, 1)) # autonomous system, so no time needed
# Solver control settings (tolerances, step sizes) use sensible defaults
ctrl <- OdeControl$new()
# Build a solver (a "runner") and advance with adaptive stepping
runner <- Lorenz_Solver$new(lz$ptr, ctrl$ptr)
runner$advance_adaptive(seq(0, 100, by = 0.01))
# Collect output as a tibble
out <- runner$history()For more, see the vignettes (also rendered on the package website):
-
vignette("odelia")— getting started with the core abstractions (source). -
vignette("parameter-fitting")— recovering parameters with automatic differentiation (source).
And the worked examples:
- Building your own model with external drivers — a leaf-thermal model showing how to define your own ODE system in C++ and drive it with time-varying forcing (source).
- Reverse mode: one solve, every parameter — the gradient machinery from C++: what a run records, how the sweep walks it, what a System must provide (source).
- Forward mode: few parameters, many outputs — sensitivities at every output time, through RODAS too, and when they cost less than a sweep (source).
From C++
A System is a class templated on its scalar type that holds parameters and state and knows its rates. The solver drives it:
// [[Rcpp::plugins(cpp20)]] (or CXX_STD = CXX20 in a package's Makevars)
#include <odelia/ode_solver.hpp>
#include <examples/lorenz_system.hpp> // the shipped example System
LorenzSystem<double> system(10.0, 28.0, 8.0 / 3.0);
odelia::ode::Solver<LorenzSystem<double>> s(system, odelia::ode::OdeControl());
s.advance_adaptive({0.0, 10.0});
std::vector<double> y = s.state();A gradient of that run is three more lines: keep the record, set the output’s adjoint wanted, and sweep.
s.set_keep_states(true); // before the run
s.advance_adaptive({0.0, 10.0});
auto lambda = odelia::ode::adjoint_batch::one({1.0, 0.0, 0.0}); // d x(10)
odelia::ode::adjoint_batch dp(1, 3); // one per output, zeroed
s.sweep_adjoint(lambda, dp); // dp[0][j] = d x(10) / d parameter jA package that uses the headers adds odelia to LinkingTo and Imports, compiles as C++20 with -DXAD_NO_THREADLOCAL -DXAD_USE_STRONG_INLINE, and on Windows links against odelia’s DLL; ARCHITECTURE.md has the details and a map of the headers. inst/include/examples/lorenz_system.hpp is the template for a System of your own, with its members marked by which contract they serve; the article Building your own model walks through one with external drivers.
Your own model in one file
A package is the shape for a model that others will install. For a model of your own, one file compiled with Rcpp::sourceCpp() is enough: the System class, then the [[Rcpp::export]] functions that build a Solver on it and return what R wants. The shipped examples/consumer_resource/consumer_resource.cpp is that file for a consumer-resource model (a consumer taking up a resource with a saturating response, the resource regrowing logistically), with three exports: a solver, a solve, and forward-mode sensitivities. Copy it and replace the rates.
Load odelia first: the compiled tape runtime lives in odelia’s library, and loading the package makes it visible to the file you compile. The flags are the ones the package itself is built with, and PKG_LIBS links the file against odelia’s library, which Windows needs and the other platforms tolerate:
library(odelia)
Sys.setenv(PKG_CPPFLAGS = paste0(
"-I", shQuote(system.file("include", package = "odelia")),
" -DXAD_NO_THREADLOCAL -DXAD_USE_STRONG_INLINE"),
PKG_LIBS = shQuote(odelia_load_dll()))
Rcpp::sourceCpp(system.file("examples/consumer_resource/consumer_resource.cpp",
package = "odelia"))
p <- c(a = 1, h = 0.5, q = 0.5, m = 0.4, r = 1, K = 1)
s <- consumer_resource_solver(p, y0 = c(N = 0.5, R = 0.5), method = "rodas")
consumer_resource_solve(s, times = seq(0, 40, by = 5)) # N and R at each time
consumer_resource_sensitivities(s, times = c(0, 20, 40), # and d(N, R)/d(a, ..., K, N0, R0)
parameters = 0:5, initial = 0:1)Two habits worth keeping when you write the rates: call exp(x) and the other scalar functions unqualified, after using std::exp;, so that the AD scalar’s overloads are found (std::exp(x) would not find them); and carry a quantity that must stay positive, or a state whose components differ by orders of magnitude, as its logarithm, so that the stepper cannot cross zero and the tolerances mean the same for every component. The website articles Reverse mode and Forward mode compile their examples the same way, without PKG_LIBS, which Linux and macOS do not need.
Parameter fitting with automatic differentiation
The shipped Lorenz and leaf-thermal runners expose a $loss_and_gradient() method that returns the least-squares loss against a target trajectory and its exact gradient with respect to the parameters (and initial conditions), by one reverse sweep over a replay of the reference run, or by forward mode with mode = "forward" (the default under "rodas"). Hand it to a gradient-based optimiser such as optim():
fit_runner <- Lorenz_Solver$new(lz$ptr, ctrl$ptr)
fit_runner$set_target(times, target_vals, obs_index)
res <- fit_runner$loss_and_gradient(params = c(sigma = 12, R = 30, b = 3))
res$loss # scalar mismatch with the target trajectory
res$gradient # exact gradient w.r.t. each parameterA complete optimisation workflow (recovering known Lorenz parameters) is walked through in vignette("parameter-fitting"). A runner for your own System gets $loss_and_gradient() by instantiating Solver_loss_and_gradient_impl from solver_interface.hpp, as the leaf-thermal example does.
Vocabulary
odelia is organised around a few core abstractions:
-
System — your ODE model. A C++ class that holds parameters and state and knows how to compute its rates (right-hand side)
dy/dt. Templated on its scalar type so it works with bothdoubleand AD types. - Stepper — the numerical integration scheme (Cash-Karp RK4(5), Dormand-Prince 5(4) or RODAS4(3)) that takes one step of the system, estimating the error to choose the next step size.
-
Solver —
Solver<System>in C++ drives the stepper forward over a requested set of times, applying step-size control, and sweeps a recorded run backward for its gradient. In R,Lorenz_Solverwraps that for the shipped system andOdeSolversteps a right-hand side written in R. - Drivers — external, time-varying forcing variables (e.g. air temperature) that the system queries during integration. Supplied as time series and interpolated with a cubic Hermite spline.
-
Control (
OdeControl) — the solver’s tuning knobs: absolute and relative tolerances, state/derivative scaling, minimum/maximum/initial step sizes, the step-size rule, and the most steps an interval may take (max_steps). -
Recording — what a run keeps when asked (
set_keep_states): one row per accepted step with its time, step size, state, and whatever the step solved for inside a stage. A sweep reads it; a replay reproduces the run from it. -
Sweep — the backward pass over a recording (
sweep_adjoint). It starts from the derivative wanted of the final state, one adjoint per output, and returns one row of derivatives per output against the parameters and the initial state;adjoint_batchis the batch. - Insertion — a scheduled growth of the state vector mid-run. The sweep transposes the System’s own widening map across it.
-
Supplied derivative — a value put on the tape with rows obtained by other means (
supply_derivatives,implicit_value), for a quantity a submodel solved for by iteration.
License
This package is released under the GNU Affero General Public License v3 (AGPL-3). The vendored XAD library retains its own license; see inst/include/XAD/LICENSE.md.
Contributing
Contributions are welcome. By submitting a pull request or code to this repository, you agree to the terms of the Contributor License Agreement.
Linking from another package? If you
LinkingTo: odeliaand instantiateSolver, read ARCHITECTURE.md — it documents how the compiled XADTapesymbols are resolved per platform, what your package must do, and the invariants that must not be broken.
ODE System Structure
An ODE system in odelia consists of:
A system header (
inst/include/examples/lorenz_system.hpp,inst/examples/leaf_thermal/src/leaf_thermal_system.hpp) — a C++ class templated on its scalar type. To be solved it implementsode_size(),set_ode_state(),ode_state()andode_rates(); to be swept it addsrebind<U>(),ad_parameters(),for_each_active(f)andset_recorded_state(y, time)(theSweepableconcept inode_interface.hpp).An Rcpp interface (
src/*_interface.cpp,inst/examples/leaf_thermal/src/*_interface.cpp) —[[Rcpp::export]]functions exposing the system to R, built on theSolver_*_impltemplates insolver_interface.hpp.An R wrapper (
R/*-interface.R, optional) — R6 classes providing a friendlier API around the external pointers.A worked example — the article for the leaf-thermal model, built by pkgdown.
Testing
Installs the package with its tests and runs the full suite against the installed copy. The AD and DLL-lifecycle tests need an installed package.
The fast development loop: testthat::test_local() via load_all(), skipping those tests.
Builds and runs the solver core as plain C++ with no R on the include path (tests/standalone/), and compiles every core header on its own. Anything compiled against the headers outside the package’s own src/ needs C++20 and -DXAD_NO_THREADLOCAL -DXAD_USE_STRONG_INLINE, matching src/Makevars.
Plant family
odelia is part of the plant family of packages in the traitecoevo org, built around the plant forest model. Docs hub: https://traitecoevo.github.io/overstorey/.
Contributing: please skim the family issue guide before filing — issues across the family are triaged on board #5, and cross-package context lives in plant-meta.