pharmsol 0.25.0

Rust library for solving analytic and ode-defined pharmacometric models.
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
//! Core type definitions for JSON models

use serde::{Deserialize, Serialize};
use std::collections::HashMap;

fn default_required() -> bool {
    true
}

fn is_default_required(value: &bool) -> bool {
    *value
}

// ═══════════════════════════════════════════════════════════════════════════════
// Model Type
// ═══════════════════════════════════════════════════════════════════════════════

/// The type of equation system used by the model
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum ModelType {
    /// Analytical (closed-form) solution
    Analytical,
    /// Ordinary differential equations
    Ode,
    /// Stochastic differential equations
    Sde,
}

impl std::fmt::Display for ModelType {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Self::Analytical => write!(f, "analytical"),
            Self::Ode => write!(f, "ode"),
            Self::Sde => write!(f, "sde"),
        }
    }
}

// ═══════════════════════════════════════════════════════════════════════════════
// Analytical Functions
// ═══════════════════════════════════════════════════════════════════════════════

/// Built-in analytical solution functions
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum AnalyticalFunction {
    /// One compartment IV (ke)
    OneCompartment,
    /// One compartment with first-order absorption (ka, ke)
    OneCompartmentWithAbsorption,
    /// Two compartments IV (ke, kcp, kpc)
    TwoCompartments,
    /// Two compartments with absorption (ke, ka, kcp, kpc)
    TwoCompartmentsWithAbsorption,
    /// Three compartments IV (k10, k12, k13, k21, k31)
    ThreeCompartments,
    /// Three compartments with absorption (ka, k10, k12, k13, k21, k31)
    ThreeCompartmentsWithAbsorption,
}

impl AnalyticalFunction {
    /// Get the Rust function name for code generation
    pub fn rust_name(&self) -> &'static str {
        match self {
            Self::OneCompartment => "one_compartment",
            Self::OneCompartmentWithAbsorption => "one_compartment_with_absorption",
            Self::TwoCompartments => "two_compartments",
            Self::TwoCompartmentsWithAbsorption => "two_compartments_with_absorption",
            Self::ThreeCompartments => "three_compartments",
            Self::ThreeCompartmentsWithAbsorption => "three_compartments_with_absorption",
        }
    }

    /// Get the expected parameter names for this function (in order)
    pub fn expected_parameters(&self) -> Vec<&'static str> {
        match self {
            Self::OneCompartment => vec!["ke"],
            Self::OneCompartmentWithAbsorption => vec!["ka", "ke"],
            Self::TwoCompartments => vec!["ke", "kcp", "kpc"],
            Self::TwoCompartmentsWithAbsorption => vec!["ke", "ka", "kcp", "kpc"],
            Self::ThreeCompartments => vec!["k10", "k12", "k13", "k21", "k31"],
            Self::ThreeCompartmentsWithAbsorption => {
                vec!["ka", "k10", "k12", "k13", "k21", "k31"]
            }
        }
    }

    /// Get the number of states for this function
    pub fn num_states(&self) -> usize {
        match self {
            Self::OneCompartment => 1,
            Self::OneCompartmentWithAbsorption => 2,
            Self::TwoCompartments => 2,
            Self::TwoCompartmentsWithAbsorption => 3,
            Self::ThreeCompartments => 3,
            Self::ThreeCompartmentsWithAbsorption => 4,
        }
    }
}

// ═══════════════════════════════════════════════════════════════════════════════
// Expression Types
// ═══════════════════════════════════════════════════════════════════════════════

/// A Rust expression string
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(transparent)]
pub struct Expression(pub String);

impl Expression {
    /// Create a new expression
    pub fn new(s: impl Into<String>) -> Self {
        Self(s.into())
    }

    /// Get the expression string
    pub fn as_str(&self) -> &str {
        &self.0
    }

    /// Check if the expression is empty
    pub fn is_empty(&self) -> bool {
        self.0.trim().is_empty()
    }
}

impl From<String> for Expression {
    fn from(s: String) -> Self {
        Self(s)
    }
}

impl From<&str> for Expression {
    fn from(s: &str) -> Self {
        Self(s.to_string())
    }
}

impl AsRef<str> for Expression {
    fn as_ref(&self) -> &str {
        &self.0
    }
}

/// Either an expression or a numeric value
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(untagged)]
pub enum ExpressionOrNumber {
    /// A numeric constant
    Number(f64),
    /// A Rust expression
    Expression(String),
}

impl ExpressionOrNumber {
    /// Convert to a Rust expression string
    pub fn to_rust_expr(&self) -> String {
        match self {
            Self::Number(n) => format!("{:.6}", n),
            Self::Expression(s) => s.clone(),
        }
    }
}

// ═══════════════════════════════════════════════════════════════════════════════
// Differential Equation Specification
// ═══════════════════════════════════════════════════════════════════════════════

/// Differential equation specification keyed by compartment or state id.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(untagged)]
pub enum DiffEqSpec {
    /// Map of compartment name to equation
    Object(HashMap<String, String>),
}

impl DiffEqSpec {
    /// Check if empty
    pub fn is_empty(&self) -> bool {
        match self {
            Self::Object(m) => m.is_empty(),
        }
    }
}

// ═══════════════════════════════════════════════════════════════════════════════
// Initial Conditions
// ═══════════════════════════════════════════════════════════════════════════════

/// Initial condition specification
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(untagged)]
pub enum InitSpec {
    /// Map of compartment/state name to initial value
    Object(HashMap<String, ExpressionOrNumber>),
}

// ═══════════════════════════════════════════════════════════════════════════════
// Output Definition
// ═══════════════════════════════════════════════════════════════════════════════

/// Definition of a model output
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct OutputDefinition {
    /// Output identifier
    pub id: String,

    /// Output equation expression
    pub equation: String,

    /// Human-readable name
    #[serde(skip_serializing_if = "Option::is_none")]
    pub name: Option<String>,

    /// Output units
    #[serde(skip_serializing_if = "Option::is_none")]
    pub units: Option<String>,
}

/// Ordered named equation used for canonical secondary expressions.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct NamedEquation {
    /// Symbol introduced by this equation.
    pub id: String,

    /// Expression assigned to the symbol.
    pub equation: String,
}

/// Covariate definition used by model expressions and UI consumers.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct CovariateDefinition {
    /// Symbol used inside expressions.
    pub id: String,

    /// Dataset column backing the symbol. Defaults to `id` when omitted.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub column: Option<String>,

    /// Whether the covariate must be present in incoming data.
    #[serde(
        default = "default_required",
        skip_serializing_if = "is_default_required"
    )]
    pub required: bool,

    /// Optional reference value used by UI consumers or scaling conventions.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub reference: Option<f64>,
}

impl CovariateDefinition {
    pub fn symbol(&self) -> &str {
        &self.id
    }

    pub fn column_name(&self) -> &str {
        self.column.as_deref().unwrap_or(&self.id)
    }
}

/// Normalized executable representation consumed by validation and code generation.
///
/// This provides the compile-time shape after validation and normalization.
/// Compiler-ignored editor metadata is excluded.
#[derive(Debug, Clone, PartialEq, Serialize)]
pub struct ExecutableModel {
    /// Unique model identifier.
    pub id: String,

    /// Model equation type.
    #[serde(rename = "type")]
    pub model_type: ModelType,

    /// Parameter names in fetch order.
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub parameters: Vec<String>,

    /// Compartment names in declaration order.
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub compartments: Vec<String>,

    /// State names in declaration order.
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub states: Vec<String>,

    /// Built-in analytical function, when applicable.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub analytical: Option<AnalyticalFunction>,

    /// Differential equations for ODE models.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub diffeq: Option<DiffEqSpec>,

    /// Drift equations for SDE models.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub drift: Option<DiffEqSpec>,

    /// Diffusion definitions for SDE models.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub diffusion: Option<HashMap<String, ExpressionOrNumber>>,

    /// Ordered executable calculations evaluated before outputs and equations.
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub calculations: Vec<NamedEquation>,

    /// Canonicalized outputs with stable identifiers.
    pub outputs: Vec<OutputDefinition>,

    /// Initial conditions.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub init: Option<InitSpec>,

    /// Lag times keyed by compartment/state id.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub lag: Option<HashMap<String, ExpressionOrNumber>>,

    /// Bioavailability keyed by compartment/state id.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub fa: Option<HashMap<String, ExpressionOrNumber>>,

    /// Optional explicit equation dimensions.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub neqs: Option<(usize, usize)>,

    /// Number of particles for SDE simulation.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub particles: Option<usize>,

    /// Covariates used by executable expressions.
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub covariates: Vec<CovariateDefinition>,
}

// ═══════════════════════════════════════════════════════════════════════════════
// UI Metadata (ignored by compiler)
// ═══════════════════════════════════════════════════════════════════════════════

/// Model complexity level
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum Complexity {
    Basic,
    Intermediate,
    Advanced,
}

/// Model category
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum Category {
    Pk,
    Pd,
    Pkpd,
    Disease,
    Other,
}

/// Position for layout
#[derive(Debug, Clone, Copy, PartialEq, Serialize, Deserialize)]
pub struct Position {
    pub x: f64,
    pub y: f64,
}

/// Display information for UI
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, Default)]
pub struct DisplayInfo {
    /// Human-readable model name
    #[serde(skip_serializing_if = "Option::is_none")]
    pub name: Option<String>,

    /// Abbreviated name
    #[serde(
        alias = "shortName",
        rename = "shortName",
        skip_serializing_if = "Option::is_none"
    )]
    pub short_name: Option<String>,

    /// Model category
    #[serde(skip_serializing_if = "Option::is_none")]
    pub category: Option<Category>,

    /// Model subcategory
    #[serde(skip_serializing_if = "Option::is_none")]
    pub subcategory: Option<String>,

    /// Complexity level
    #[serde(skip_serializing_if = "Option::is_none")]
    pub complexity: Option<Complexity>,

    /// Icon identifier
    #[serde(skip_serializing_if = "Option::is_none")]
    pub icon: Option<String>,

    /// Searchable tags
    #[serde(skip_serializing_if = "Option::is_none")]
    pub tags: Option<Vec<String>>,
}

/// Literature reference
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct Reference {
    #[serde(skip_serializing_if = "Option::is_none")]
    pub authors: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub title: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub journal: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub year: Option<i32>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub doi: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub pmid: Option<String>,
}

/// LaTeX equations for display
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, Default)]
pub struct EquationDocs {
    #[serde(skip_serializing_if = "Option::is_none")]
    pub differential: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub solution: Option<String>,
}

/// Rich documentation
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, Default)]
pub struct Documentation {
    /// One-line summary
    #[serde(skip_serializing_if = "Option::is_none")]
    pub summary: Option<String>,

    /// Detailed description
    #[serde(skip_serializing_if = "Option::is_none")]
    pub description: Option<String>,

    /// LaTeX equations
    #[serde(skip_serializing_if = "Option::is_none")]
    pub equations: Option<EquationDocs>,

    /// Model assumptions
    #[serde(skip_serializing_if = "Option::is_none")]
    pub assumptions: Option<Vec<String>>,

    /// When to use this model
    #[serde(
        alias = "whenToUse",
        rename = "whenToUse",
        skip_serializing_if = "Option::is_none"
    )]
    pub when_to_use: Option<Vec<String>>,

    /// When NOT to use this model
    #[serde(
        alias = "whenNotToUse",
        rename = "whenNotToUse",
        skip_serializing_if = "Option::is_none"
    )]
    pub when_not_to_use: Option<Vec<String>>,

    /// Literature references
    #[serde(skip_serializing_if = "Option::is_none")]
    pub references: Option<Vec<Reference>>,
}

/// Compiler-ignored editor metadata used by richer consumers like papir.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, Default)]
pub struct EditorInfo {
    #[serde(skip_serializing_if = "Option::is_none")]
    pub display: Option<DisplayInfo>,

    #[serde(skip_serializing_if = "Option::is_none")]
    pub layout: Option<HashMap<String, Position>>,

    #[serde(skip_serializing_if = "Option::is_none")]
    pub documentation: Option<Documentation>,
}

/// Optional features that can be enabled
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum Feature {
    LagTime,
    Bioavailability,
    InitialConditions,
}