jivanu 1.0.0

Jivanu — microbiology engine for growth kinetics, metabolism, genetics, and epidemiology
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
//! Pharmacokinetics — drug absorption, distribution, and elimination.
//!
//! Models for predicting drug concentration over time in biological
//! compartments. Used alongside the [`resistance`](crate::resistance) module
//! to connect antibiotic dosing to bactericidal effect.

use serde::{Deserialize, Serialize};

use crate::error::{JivanuError, Result, validate_non_negative, validate_positive};

/// Elimination half-life from an elimination rate constant.
///
/// `t½ = ln(2) / k_e`
///
/// # Errors
///
/// Returns error if `k_e` is non-positive.
#[inline]
#[must_use = "returns the half-life without side effects"]
pub fn half_life(k_e: f64) -> Result<f64> {
    validate_positive(k_e, "k_e")?;
    Ok(core::f64::consts::LN_2 / k_e)
}

/// Elimination rate constant from a half-life.
///
/// `k_e = ln(2) / t½`
///
/// # Errors
///
/// Returns error if `t_half` is non-positive.
#[inline]
#[must_use = "returns the elimination rate constant without side effects"]
pub fn elimination_rate(t_half: f64) -> Result<f64> {
    validate_positive(t_half, "t_half")?;
    Ok(core::f64::consts::LN_2 / t_half)
}

/// One-compartment IV bolus: plasma concentration at time `t`.
///
/// `C(t) = (dose / V_d) × e^(-k_e × t)`
///
/// Models instantaneous drug distribution after intravenous injection.
///
/// # Arguments
///
/// - `dose` — administered dose (mg)
/// - `v_d` — volume of distribution (L)
/// - `k_e` — elimination rate constant (1/hr)
/// - `t` — time after administration (hr)
///
/// # Errors
///
/// Returns error if parameters are invalid.
#[inline]
#[must_use = "returns the plasma concentration without side effects"]
pub fn iv_bolus_concentration(dose: f64, v_d: f64, k_e: f64, t: f64) -> Result<f64> {
    validate_positive(dose, "dose")?;
    validate_positive(v_d, "v_d")?;
    validate_positive(k_e, "k_e")?;
    validate_non_negative(t, "t")?;
    let c0 = dose / v_d;
    Ok(c0 * (-k_e * t).exp())
}

/// One-compartment oral administration: plasma concentration at time `t`.
///
/// `C(t) = (F × dose × k_a) / (V_d × (k_a - k_e)) × (e^(-k_e×t) - e^(-k_a×t))`
///
/// Bateman equation for first-order absorption and elimination.
///
/// # Arguments
///
/// - `dose` — administered dose (mg)
/// - `bioavailability` — fraction absorbed (F, 0–1)
/// - `v_d` — volume of distribution (L)
/// - `k_a` — absorption rate constant (1/hr)
/// - `k_e` — elimination rate constant (1/hr)
/// - `t` — time after administration (hr)
///
/// # Errors
///
/// Returns error if parameters are invalid or `k_a == k_e`.
#[inline]
#[must_use = "returns the plasma concentration without side effects"]
pub fn oral_concentration(
    dose: f64,
    bioavailability: f64,
    v_d: f64,
    k_a: f64,
    k_e: f64,
    t: f64,
) -> Result<f64> {
    validate_positive(dose, "dose")?;
    validate_positive(bioavailability, "bioavailability")?;
    if bioavailability > 1.0 {
        return Err(JivanuError::ComputationError(
            "bioavailability must be in (0, 1]".into(),
        ));
    }
    validate_positive(v_d, "v_d")?;
    validate_positive(k_a, "k_a")?;
    validate_positive(k_e, "k_e")?;
    validate_non_negative(t, "t")?;

    if (k_a - k_e).abs() < 1e-15 {
        return Err(JivanuError::ComputationError(
            "k_a must differ from k_e (flip-flop kinetics not supported)".into(),
        ));
    }

    let numerator = bioavailability * dose * k_a;
    let denominator = v_d * (k_a - k_e);
    Ok(numerator / denominator * ((-k_e * t).exp() - (-k_a * t).exp()))
}

/// Time to maximum concentration (Tmax) for oral one-compartment model.
///
/// `T_max = ln(k_a / k_e) / (k_a - k_e)`
///
/// # Errors
///
/// Returns error if rate constants are invalid or equal.
#[inline]
#[must_use = "returns the Tmax without side effects"]
pub fn oral_tmax(k_a: f64, k_e: f64) -> Result<f64> {
    validate_positive(k_a, "k_a")?;
    validate_positive(k_e, "k_e")?;
    if (k_a - k_e).abs() < 1e-15 {
        return Err(JivanuError::ComputationError(
            "k_a must differ from k_e".into(),
        ));
    }
    Ok((k_a / k_e).ln() / (k_a - k_e))
}

/// Maximum concentration (Cmax) for oral one-compartment model.
///
/// Evaluates `oral_concentration` at `T_max`.
///
/// # Errors
///
/// Returns error if parameters are invalid.
#[must_use = "returns the Cmax without side effects"]
pub fn oral_cmax(dose: f64, bioavailability: f64, v_d: f64, k_a: f64, k_e: f64) -> Result<f64> {
    let tmax = oral_tmax(k_a, k_e)?;
    oral_concentration(dose, bioavailability, v_d, k_a, k_e, tmax)
}

/// Area under the concentration-time curve (AUC) via the trapezoidal rule.
///
/// Computes the definite integral of concentration over time from paired
/// `(time, concentration)` data points. Data must be sorted by time.
///
/// AUC is the primary measure of total drug exposure.
///
/// # Errors
///
/// Returns error if fewer than 2 data points or times are not increasing.
#[must_use = "returns the AUC without side effects"]
pub fn auc_trapezoidal(times: &[f64], concentrations: &[f64]) -> Result<f64> {
    if times.len() != concentrations.len() {
        return Err(JivanuError::ComputationError(
            "times and concentrations must have the same length".into(),
        ));
    }
    if times.len() < 2 {
        return Err(JivanuError::ComputationError(
            "at least 2 data points required for AUC".into(),
        ));
    }
    let mut auc = 0.0;
    for i in 1..times.len() {
        let dt = times[i] - times[i - 1];
        if dt < 0.0 {
            return Err(JivanuError::ComputationError(
                "times must be monotonically increasing".into(),
            ));
        }
        auc += 0.5 * (concentrations[i - 1] + concentrations[i]) * dt;
    }
    Ok(auc)
}

/// Analytical AUC (0→∞) for one-compartment IV bolus.
///
/// `AUC = dose / (V_d × k_e) = C_0 / k_e`
///
/// # Errors
///
/// Returns error if parameters are invalid.
#[inline]
#[must_use = "returns the AUC without side effects"]
pub fn auc_iv_bolus(dose: f64, v_d: f64, k_e: f64) -> Result<f64> {
    validate_positive(dose, "dose")?;
    validate_positive(v_d, "v_d")?;
    validate_positive(k_e, "k_e")?;
    Ok(dose / (v_d * k_e))
}

/// Two-compartment IV bolus model state.
#[derive(Debug, Clone, Copy, Serialize, Deserialize)]
pub struct TwoCompartmentState {
    /// Drug amount in the central compartment (mg).
    pub central: f64,
    /// Drug amount in the peripheral compartment (mg).
    pub peripheral: f64,
}

/// Parameters for the two-compartment IV bolus model.
///
/// ```text
/// dA_c/dt = -(k_e + k_12) × A_c + k_21 × A_p
/// dA_p/dt =  k_12 × A_c - k_21 × A_p
/// ```
///
/// Plasma concentration is `A_c / V_c`.
#[derive(Debug, Clone, Copy, Serialize, Deserialize)]
pub struct TwoCompartmentParams {
    /// Volume of the central compartment (L).
    pub v_c: f64,
    /// Elimination rate constant from central compartment (1/hr).
    pub k_e: f64,
    /// Transfer rate central → peripheral (1/hr).
    pub k_12: f64,
    /// Transfer rate peripheral → central (1/hr).
    pub k_21: f64,
}

/// One step of the two-compartment IV bolus model.
///
/// When the `hisab` feature is enabled, uses RK4 integration.
///
/// # Errors
///
/// Returns error if parameters are invalid.
#[must_use = "returns the new state without side effects"]
pub fn two_compartment_step(
    state: &TwoCompartmentState,
    params: &TwoCompartmentParams,
    dt: f64,
) -> Result<TwoCompartmentState> {
    validate_non_negative(state.central, "central")?;
    validate_non_negative(state.peripheral, "peripheral")?;
    validate_positive(params.v_c, "v_c")?;
    validate_positive(params.k_e, "k_e")?;
    validate_non_negative(params.k_12, "k_12")?;
    validate_non_negative(params.k_21, "k_21")?;
    validate_positive(dt, "dt")?;

    two_compartment_step_inner(state, params, dt)
}

#[cfg(feature = "hisab")]
fn two_compartment_step_inner(
    state: &TwoCompartmentState,
    params: &TwoCompartmentParams,
    dt: f64,
) -> Result<TwoCompartmentState> {
    let TwoCompartmentParams {
        k_e, k_12, k_21, ..
    } = *params;
    let y0 = [state.central, state.peripheral];
    let result = hisab::num::rk4(
        |_t, y, dy| {
            dy[0] = -(k_e + k_12) * y[0] + k_21 * y[1];
            dy[1] = k_12 * y[0] - k_21 * y[1];
        },
        0.0,
        &y0,
        dt,
        1,
    )
    .map_err(|e| JivanuError::SimulationFailed(e.to_string()))?;
    Ok(TwoCompartmentState {
        central: result[0].max(0.0),
        peripheral: result[1].max(0.0),
    })
}

#[cfg(not(feature = "hisab"))]
fn two_compartment_step_inner(
    state: &TwoCompartmentState,
    params: &TwoCompartmentParams,
    dt: f64,
) -> Result<TwoCompartmentState> {
    let ac = state.central;
    let ap = state.peripheral;
    let dac = -(params.k_e + params.k_12) * ac + params.k_21 * ap;
    let dap = params.k_12 * ac - params.k_21 * ap;
    Ok(TwoCompartmentState {
        central: (ac + dac * dt).max(0.0),
        peripheral: (ap + dap * dt).max(0.0),
    })
}

/// Plasma concentration from a two-compartment state.
///
/// `C_plasma = A_central / V_c`
///
/// # Errors
///
/// Returns error if `v_c` is non-positive.
#[inline]
#[must_use = "returns the plasma concentration without side effects"]
pub fn plasma_concentration(state: &TwoCompartmentState, v_c: f64) -> Result<f64> {
    validate_positive(v_c, "v_c")?;
    Ok(state.central / v_c)
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_half_life() {
        let t = half_life(core::f64::consts::LN_2).unwrap();
        assert!((t - 1.0).abs() < 1e-10);
    }

    #[test]
    fn test_elimination_rate() {
        let k = elimination_rate(1.0).unwrap();
        assert!((k - core::f64::consts::LN_2).abs() < 1e-10);
    }

    #[test]
    fn test_half_life_elimination_rate_roundtrip() {
        let k = 0.35;
        let t = half_life(k).unwrap();
        let k_back = elimination_rate(t).unwrap();
        assert!((k - k_back).abs() < 1e-10);
    }

    #[test]
    fn test_iv_bolus_at_t0() {
        let c = iv_bolus_concentration(500.0, 50.0, 0.1, 0.0).unwrap();
        assert!((c - 10.0).abs() < 1e-10); // C0 = dose/Vd = 10
    }

    #[test]
    fn test_iv_bolus_decay() {
        let c0 = iv_bolus_concentration(500.0, 50.0, 0.1, 0.0).unwrap();
        let c1 = iv_bolus_concentration(500.0, 50.0, 0.1, 1.0).unwrap();
        assert!(c1 < c0);
        assert!(c1 > 0.0);
    }

    #[test]
    fn test_iv_bolus_at_half_life() {
        // At t = t½, concentration should be C0/2
        let k_e = 0.1;
        let t_half = half_life(k_e).unwrap();
        let c0 = iv_bolus_concentration(500.0, 50.0, k_e, 0.0).unwrap();
        let c_half = iv_bolus_concentration(500.0, 50.0, k_e, t_half).unwrap();
        assert!((c_half - c0 / 2.0).abs() < 1e-6);
    }

    #[test]
    fn test_oral_concentration_at_t0() {
        let c = oral_concentration(500.0, 1.0, 50.0, 1.0, 0.1, 0.0).unwrap();
        assert!((c - 0.0).abs() < 1e-10); // No absorption yet
    }

    #[test]
    fn test_oral_concentration_rises_then_falls() {
        let c1 = oral_concentration(500.0, 1.0, 50.0, 1.0, 0.1, 1.0).unwrap();
        let c_peak = oral_concentration(500.0, 1.0, 50.0, 1.0, 0.1, 3.0).unwrap();
        let c_late = oral_concentration(500.0, 1.0, 50.0, 1.0, 0.1, 20.0).unwrap();
        assert!(c1 > 0.0);
        assert!(c_late < c_peak);
    }

    #[test]
    fn test_oral_tmax() {
        let tmax = oral_tmax(1.0, 0.1).unwrap();
        assert!(tmax > 0.0);
        // Tmax = ln(1.0/0.1) / (1.0 - 0.1) = ln(10) / 0.9 ≈ 2.558
        assert!((tmax - 10.0_f64.ln() / 0.9).abs() < 1e-10);
    }

    #[test]
    fn test_oral_tmax_equal_rates_error() {
        assert!(oral_tmax(0.5, 0.5).is_err());
    }

    #[test]
    fn test_oral_cmax() {
        let cmax = oral_cmax(500.0, 1.0, 50.0, 1.0, 0.1).unwrap();
        assert!(cmax > 0.0);
        // Cmax should be less than C0 of equivalent IV bolus
        let c0_iv = 500.0 / 50.0;
        assert!(cmax < c0_iv);
    }

    #[test]
    fn test_auc_trapezoidal_rectangle() {
        // Constant concentration of 10 from t=0 to t=5
        let auc = auc_trapezoidal(&[0.0, 5.0], &[10.0, 10.0]).unwrap();
        assert!((auc - 50.0).abs() < 1e-10);
    }

    #[test]
    fn test_auc_trapezoidal_triangle() {
        // Linear decay from 10 to 0 over t=0..10
        let auc = auc_trapezoidal(&[0.0, 10.0], &[10.0, 0.0]).unwrap();
        assert!((auc - 50.0).abs() < 1e-10);
    }

    #[test]
    fn test_auc_trapezoidal_multipoint() {
        let times = [0.0, 1.0, 2.0, 3.0];
        let concs = [10.0, 8.0, 4.0, 1.0];
        let auc = auc_trapezoidal(&times, &concs).unwrap();
        // Trapezoids: (10+8)/2*1 + (8+4)/2*1 + (4+1)/2*1 = 9 + 6 + 2.5 = 17.5
        assert!((auc - 17.5).abs() < 1e-10);
    }

    #[test]
    fn test_auc_trapezoidal_too_few_points() {
        assert!(auc_trapezoidal(&[0.0], &[10.0]).is_err());
    }

    #[test]
    fn test_auc_trapezoidal_non_monotonic() {
        assert!(auc_trapezoidal(&[0.0, 2.0, 1.0], &[10.0, 5.0, 3.0]).is_err());
    }

    #[test]
    fn test_auc_iv_bolus_analytical() {
        // AUC = dose / (Vd * ke) = 500 / (50 * 0.1) = 100
        let auc = auc_iv_bolus(500.0, 50.0, 0.1).unwrap();
        assert!((auc - 100.0).abs() < 1e-10);
    }

    #[test]
    fn test_two_compartment_step_elimination() {
        let state = TwoCompartmentState {
            central: 500.0,
            peripheral: 0.0,
        };
        let params = TwoCompartmentParams {
            v_c: 50.0,
            k_e: 0.1,
            k_12: 0.2,
            k_21: 0.1,
        };
        let next = two_compartment_step(&state, &params, 0.1).unwrap();
        // Central should decrease (elimination + distribution)
        assert!(next.central < 500.0);
        // Peripheral should increase (distribution in)
        assert!(next.peripheral > 0.0);
    }

    #[test]
    fn test_two_compartment_mass_decreases() {
        // Total drug mass should decrease due to elimination
        let state = TwoCompartmentState {
            central: 500.0,
            peripheral: 100.0,
        };
        let params = TwoCompartmentParams {
            v_c: 50.0,
            k_e: 0.1,
            k_12: 0.2,
            k_21: 0.1,
        };
        let next = two_compartment_step(&state, &params, 0.1).unwrap();
        let total_before = state.central + state.peripheral;
        let total_after = next.central + next.peripheral;
        assert!(total_after < total_before);
    }

    #[test]
    fn test_two_compartment_no_transfer() {
        // k_12 = k_21 = 0 → behaves like one-compartment
        let state = TwoCompartmentState {
            central: 500.0,
            peripheral: 0.0,
        };
        let params = TwoCompartmentParams {
            v_c: 50.0,
            k_e: 0.1,
            k_12: 0.0,
            k_21: 0.0,
        };
        let next = two_compartment_step(&state, &params, 0.1).unwrap();
        assert!(next.central < 500.0);
        assert!((next.peripheral - 0.0).abs() < 1e-10);
    }

    #[test]
    fn test_plasma_concentration() {
        let state = TwoCompartmentState {
            central: 500.0,
            peripheral: 100.0,
        };
        let c = plasma_concentration(&state, 50.0).unwrap();
        assert!((c - 10.0).abs() < 1e-10);
    }

    #[test]
    fn test_plasma_concentration_invalid_vc() {
        let state = TwoCompartmentState {
            central: 500.0,
            peripheral: 0.0,
        };
        assert!(plasma_concentration(&state, 0.0).is_err());
    }

    #[test]
    fn test_two_compartment_state_serde_roundtrip() {
        let state = TwoCompartmentState {
            central: 500.0,
            peripheral: 100.0,
        };
        let json = serde_json::to_string(&state).unwrap();
        let back: TwoCompartmentState = serde_json::from_str(&json).unwrap();
        assert!((state.central - back.central).abs() < 1e-10);
    }

    #[test]
    fn test_two_compartment_params_serde_roundtrip() {
        let params = TwoCompartmentParams {
            v_c: 50.0,
            k_e: 0.1,
            k_12: 0.2,
            k_21: 0.1,
        };
        let json = serde_json::to_string(&params).unwrap();
        let back: TwoCompartmentParams = serde_json::from_str(&json).unwrap();
        assert!((params.k_e - back.k_e).abs() < 1e-10);
    }
}