fn main() -> Result<(), pharmsol::PharmsolError> {
use pharmsol::{prelude::*, Parameters};
let ode = ode! {
name: "two_cmt_wt",
params: [cl, v, vp, q],
covariates: [wt],
states: [central, peripheral],
outputs: [cp],
routes: [
infusion(iv) -> central,
],
diffeq: |x, _t, dx| {
let wt_ratio = wt / 85.0;
let v_scaled = v * wt_ratio;
let vp_scaled = vp * wt_ratio;
let cl_scaled = cl * wt_ratio.powf(0.75);
let q_scaled = q * wt_ratio.powf(0.75);
let ke = cl_scaled / v_scaled; let kcp = q_scaled / v_scaled; let kpc = q_scaled / vp_scaled;
dx[central] = -ke * x[central] - kcp * x[central] + kpc * x[peripheral];
dx[peripheral] = kcp * x[central] - kpc * x[peripheral];
},
out: |x, _t, y| {
let wt_ratio = wt / 85.0;
let v_scaled = v * wt_ratio;
y[cp] = x[central] / v_scaled;
},
};
let subject = Subject::builder("subject_001")
.infusion(0.0, 500.0, "iv", 0.5)
.covariate("wt", 0.0, 70.0)
.observation(0.5, 8.5, "cp")
.observation(1.0, 6.2, "cp")
.observation(2.0, 4.1, "cp")
.observation(4.0, 2.3, "cp")
.observation(6.0, 1.5, "cp")
.observation(8.0, 1.1, "cp")
.observation(12.0, 0.7, "cp")
.missing_observation(24.0, "cp")
.build();
let cl = 5.0; let v = 50.0; let vp = 100.0; let q = 10.0; let params = Parameters::with_model(&ode, [("cl", cl), ("v", v), ("vp", vp), ("q", q)])
.expect("valid named parameters");
let predictions = ode.estimate_predictions(&subject, ¶ms)?;
println!("\n╔════════════════════════════════════════════════════════════╗");
println!("║ Two-Compartment Model Predictions ║");
println!("╠════════════════════════════════════════════════════════════╣");
println!("║ Parameters: ║");
println!(
"║ CL = {:.1} L/hr, V = {:.1} L, Vp = {:.1} L, Q = {:.1} L/hr ║",
cl, v, vp, q
);
println!("║ Weight = 70 kg (scaled from 85 kg reference) ║");
println!("╠═════════════╦══════════════════════════════════════════════╣");
println!("║ Time (hr) ║ Predicted Concentration ║");
println!("╠═════════════╬══════════════════════════════════════════════╣");
for pred in predictions.predictions() {
println!(
"║ {:>10.2} ║ {:>18.4} mg/L ║",
pred.time(),
pred.prediction()
);
}
println!("╚═════════════╩══════════════════════════════════════════════╝\n");
Ok(())
}