pharmsol
A high-performance Rust library for pharmacokinetic/pharmacodynamic (PK/PD) simulation using analytical solutions, ordinary differential equations (ODEs), or stochastic differential equations (SDEs).
Installation
Add pharmsol to Cargo.toml:
Quick Start
Most Rust-first workflows start with one of the equation macros: analytical!,
ode!, or sde!. Here is a one-compartment oral model using analytical!
with a derived analytical structure input:
use ;
let analytical = analytical! ;
let subject = builder
.bolus
.missing_observation
.missing_observation
.missing_observation
.missing_observation
.covariate
.build;
let parameters = with_model
.unwrap;
let predictions = analytical
.estimate_predictions
.unwrap;
Modeling Surfaces
Here is the same one-compartment IV setup written as an ODE:
use *;
let ode = ode! ;
See examples/analytical_readme.rs, examples/ode_readme.rs, examples/sde_readme.rs, examples/dsl_jit_analytical_covariates.rs, examples/analytical_vs_ode.rs, and examples/compare_solvers.rs. For migration-oriented notes, see docs/analytical-authoring-migration.md and docs/ode-authoring-migration.md.
Built-In Analytical Kernels
- One-compartment with IV infusion
- One-compartment with IV infusion and oral absorption
- Two-compartment with IV infusion
- Two-compartment with IV infusion and oral absorption
- Three-compartment with IV infusion
- Three-compartment with IV infusion and oral absorption
DSL and Runtime Targets
If the model needs to be loaded or compiled at runtime, pharmsol also provides a DSL with the same broad modeling coverage: ODE, analytical, and SDE authoring. The DSL can target an in-process JIT runtime, native ahead-of-time artifacts, or WASM bundles depending on how you want to ship and execute the model.
dsl-jit: compile DSL source into a runtime model inside the current process.dsl-aotanddsl-aot-load: emit a native artifact and load it later.dsl-wasm: compile and execute portable WASM model artifacts.
See examples/dsl_runtime_jit.rs for the in-repo JIT flow and
examples/dsl_jit_analytical_covariates.rs for a
small analytical covariate example written both as DSL JIT source and as an
analytical! model.
The companion pharmsol-examples crate includes end-to-end native AOT and WASM runtime
examples.
Performance
Analytical solutions provide 20-33× speedups compared to equivalent ODE formulations. See benchmarks for details.
Non-Compartmental Analysis (NCA)
pharmsol includes a complete NCA module for calculating standard pharmacokinetic parameters.
use *;
use NCAOptions;
let subject = builder
.bolus // 100 mg oral dose
.observation
.observation
.observation
.observation
.observation
.build;
let result = subject.nca.expect;
println!;
println!;
println!;
if let Some = result.terminal
Supported NCA Parameters:
- Exposure: Cmax, Tmax, Clast, Tlast, AUClast, AUCinf, tlag
- Terminal: λz, t½, MRT
- Clearance: CL/F, Vz/F, Vss
- IV-specific: C0 (back-extrapolation), Vd
- Steady-state: AUCtau, Cmin, Cavg, fluctuation, swing