pharmsol 0.27.1

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
use serde::{Deserialize, Serialize};

use super::model_info::NativeModelInfo;
use pharmsol_dsl::execution::{ExecutionModel, KernelRole};

#[cfg(any(
    test,
    feature = "dsl-aot",
    feature = "dsl-aot-load",
    feature = "dsl-wasm",
    feature = "dsl-wasm-compile"
))]
pub const API_VERSION_SYMBOL: &str = "pharmsol_dsl_api_version";
#[cfg(any(
    test,
    feature = "dsl-aot",
    feature = "dsl-aot-load",
    feature = "dsl-wasm",
    feature = "dsl-wasm-compile"
))]
pub const MODEL_INFO_JSON_PTR_SYMBOL: &str = "pharmsol_dsl_model_info_json_ptr";
#[cfg(any(
    test,
    feature = "dsl-aot",
    feature = "dsl-aot-load",
    feature = "dsl-wasm",
    feature = "dsl-wasm-compile"
))]
pub const MODEL_INFO_JSON_LEN_SYMBOL: &str = "pharmsol_dsl_model_info_json_len";
#[cfg(any(
    test,
    feature = "dsl-aot",
    feature = "dsl-aot-load",
    feature = "dsl-wasm",
    feature = "dsl-wasm-compile"
))]
pub const DERIVE_SYMBOL: &str = "pharmsol_dsl_kernel_derive";
#[cfg(any(
    test,
    feature = "dsl-aot",
    feature = "dsl-aot-load",
    feature = "dsl-wasm",
    feature = "dsl-wasm-compile"
))]
pub const DYNAMICS_SYMBOL: &str = "pharmsol_dsl_kernel_dynamics";
#[cfg(any(
    test,
    feature = "dsl-aot",
    feature = "dsl-aot-load",
    feature = "dsl-wasm",
    feature = "dsl-wasm-compile"
))]
pub const OUTPUTS_SYMBOL: &str = "pharmsol_dsl_kernel_outputs";
#[cfg(any(
    test,
    feature = "dsl-aot",
    feature = "dsl-aot-load",
    feature = "dsl-wasm",
    feature = "dsl-wasm-compile"
))]
pub const INIT_SYMBOL: &str = "pharmsol_dsl_kernel_init";
#[cfg(any(
    test,
    feature = "dsl-aot",
    feature = "dsl-aot-load",
    feature = "dsl-wasm",
    feature = "dsl-wasm-compile"
))]
pub const DRIFT_SYMBOL: &str = "pharmsol_dsl_kernel_drift";
#[cfg(any(
    test,
    feature = "dsl-aot",
    feature = "dsl-aot-load",
    feature = "dsl-wasm",
    feature = "dsl-wasm-compile"
))]
pub const DIFFUSION_SYMBOL: &str = "pharmsol_dsl_kernel_diffusion";
#[cfg(any(
    test,
    feature = "dsl-aot",
    feature = "dsl-aot-load",
    feature = "dsl-wasm",
    feature = "dsl-wasm-compile"
))]
pub const ROUTE_LAG_SYMBOL: &str = "pharmsol_dsl_kernel_route_lag";
#[cfg(any(
    test,
    feature = "dsl-aot",
    feature = "dsl-aot-load",
    feature = "dsl-wasm",
    feature = "dsl-wasm-compile"
))]
pub const ROUTE_BIOAVAILABILITY_SYMBOL: &str = "pharmsol_dsl_kernel_route_bioavailability";
#[cfg(any(
    test,
    feature = "dsl-aot",
    feature = "dsl-aot-load",
    feature = "dsl-wasm",
    feature = "dsl-wasm-compile"
))]
pub const ALLOC_F64_BUFFER_SYMBOL: &str = "pharmsol_dsl_alloc_f64_buffer";
#[cfg(any(
    test,
    feature = "dsl-aot",
    feature = "dsl-aot-load",
    feature = "dsl-wasm",
    feature = "dsl-wasm-compile"
))]
pub const FREE_F64_BUFFER_SYMBOL: &str = "pharmsol_dsl_free_f64_buffer";

#[cfg(any(test, feature = "dsl-wasm-compile"))]
pub const JS_KERNEL_EXPORTS: [(&str, &str); 8] = [
    ("derive", DERIVE_SYMBOL),
    ("dynamics", DYNAMICS_SYMBOL),
    ("outputs", OUTPUTS_SYMBOL),
    ("init", INIT_SYMBOL),
    ("drift", DRIFT_SYMBOL),
    ("diffusion", DIFFUSION_SYMBOL),
    ("route_lag", ROUTE_LAG_SYMBOL),
    ("route_bioavailability", ROUTE_BIOAVAILABILITY_SYMBOL),
];

#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Serialize, Deserialize)]
pub struct CompiledKernelAvailability {
    pub derive: bool,
    pub dynamics: bool,
    pub outputs: bool,
    pub init: bool,
    pub drift: bool,
    pub diffusion: bool,
    pub route_lag: bool,
    pub route_bioavailability: bool,
}

impl CompiledKernelAvailability {
    pub fn from_execution_model(model: &ExecutionModel) -> Self {
        let mut availability = Self::default();
        for kernel in &model.kernels {
            match kernel.role {
                KernelRole::Derive => availability.derive = true,
                KernelRole::Dynamics => availability.dynamics = true,
                KernelRole::Outputs => availability.outputs = true,
                KernelRole::Init => availability.init = true,
                KernelRole::Drift => availability.drift = true,
                KernelRole::Diffusion => availability.diffusion = true,
                KernelRole::RouteLag => availability.route_lag = true,
                KernelRole::RouteBioavailability => availability.route_bioavailability = true,
                KernelRole::Analytical => {}
            }
        }
        availability
    }

    pub fn has(self, role: KernelRole) -> bool {
        match role {
            KernelRole::Derive => self.derive,
            KernelRole::Dynamics => self.dynamics,
            KernelRole::Outputs => self.outputs,
            KernelRole::Init => self.init,
            KernelRole::Drift => self.drift,
            KernelRole::Diffusion => self.diffusion,
            KernelRole::RouteLag => self.route_lag,
            KernelRole::RouteBioavailability => self.route_bioavailability,
            KernelRole::Analytical => false,
        }
    }
}

#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
pub struct CompiledModelInfoEnvelope {
    pub abi_version: u32,
    pub model: NativeModelInfo,
    pub kernels: CompiledKernelAvailability,
}

#[cfg(any(feature = "dsl-aot", feature = "dsl-wasm-compile"))]
pub fn compiled_model_info_envelope(
    model: &ExecutionModel,
    abi_version: u32,
) -> CompiledModelInfoEnvelope {
    CompiledModelInfoEnvelope {
        abi_version,
        model: NativeModelInfo::from_execution_model(model),
        kernels: CompiledKernelAvailability::from_execution_model(model),
    }
}

#[cfg(any(feature = "dsl-aot", feature = "dsl-wasm-compile"))]
pub fn encode_compiled_model_info(
    model: &ExecutionModel,
    abi_version: u32,
) -> Result<String, serde_json::Error> {
    serde_json::to_string(&compiled_model_info_envelope(model, abi_version))
}

#[cfg(any(
    test,
    feature = "dsl-aot-load",
    all(
        feature = "dsl-wasm",
        not(all(target_arch = "wasm32", target_os = "unknown"))
    )
))]
pub fn decode_compiled_model_info(
    bytes: &[u8],
) -> Result<CompiledModelInfoEnvelope, serde_json::Error> {
    serde_json::from_slice(bytes)
}

#[cfg(any(feature = "dsl-aot", feature = "dsl-wasm-compile"))]
pub fn compiled_kernel_symbol(role: KernelRole) -> Option<&'static str> {
    match role {
        KernelRole::Derive => Some(DERIVE_SYMBOL),
        KernelRole::Dynamics => Some(DYNAMICS_SYMBOL),
        KernelRole::Outputs => Some(OUTPUTS_SYMBOL),
        KernelRole::Init => Some(INIT_SYMBOL),
        KernelRole::Drift => Some(DRIFT_SYMBOL),
        KernelRole::Diffusion => Some(DIFFUSION_SYMBOL),
        KernelRole::RouteLag => Some(ROUTE_LAG_SYMBOL),
        KernelRole::RouteBioavailability => Some(ROUTE_BIOAVAILABILITY_SYMBOL),
        KernelRole::Analytical => None,
    }
}

#[cfg(test)]
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum OutputBufferBinding {
    States,
    Derived,
    Scratch,
}

#[cfg(test)]
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct OutputBufferPlan {
    pub binding: OutputBufferBinding,
    pub len: usize,
    pub zero_before_call: bool,
}

#[cfg(test)]
pub fn output_buffer_plan(
    info: &NativeModelInfo,
    role: KernelRole,
    aliases_states: bool,
    aliases_derived: bool,
) -> OutputBufferPlan {
    let binding = if aliases_states {
        OutputBufferBinding::States
    } else if aliases_derived {
        OutputBufferBinding::Derived
    } else {
        OutputBufferBinding::Scratch
    };

    OutputBufferPlan {
        binding,
        len: kernel_output_len(info, role),
        zero_before_call: matches!(binding, OutputBufferBinding::Scratch),
    }
}

#[cfg(test)]
fn kernel_output_len(info: &NativeModelInfo, role: KernelRole) -> usize {
    match role {
        KernelRole::Derive => info.derived_len,
        KernelRole::Dynamics | KernelRole::Init | KernelRole::Drift | KernelRole::Diffusion => {
            info.state_len
        }
        KernelRole::Outputs => info.output_len,
        KernelRole::RouteLag | KernelRole::RouteBioavailability => info.route_len,
        KernelRole::Analytical => 0,
    }
}

#[cfg(test)]
mod tests {
    use super::super::model_info::{
        NativeCovariateInfo, NativeOutputInfo, NativeRouteInfo, NativeStateInfo,
    };
    use super::*;
    use pharmsol_dsl::{ModelKind, RouteKind};

    #[test]
    fn compiled_backend_symbol_names_are_frozen() {
        assert_eq!(API_VERSION_SYMBOL, "pharmsol_dsl_api_version");
        assert_eq!(
            MODEL_INFO_JSON_PTR_SYMBOL,
            "pharmsol_dsl_model_info_json_ptr"
        );
        assert_eq!(
            MODEL_INFO_JSON_LEN_SYMBOL,
            "pharmsol_dsl_model_info_json_len"
        );
        assert_eq!(DERIVE_SYMBOL, "pharmsol_dsl_kernel_derive");
        assert_eq!(DYNAMICS_SYMBOL, "pharmsol_dsl_kernel_dynamics");
        assert_eq!(OUTPUTS_SYMBOL, "pharmsol_dsl_kernel_outputs");
        assert_eq!(INIT_SYMBOL, "pharmsol_dsl_kernel_init");
        assert_eq!(DRIFT_SYMBOL, "pharmsol_dsl_kernel_drift");
        assert_eq!(DIFFUSION_SYMBOL, "pharmsol_dsl_kernel_diffusion");
        assert_eq!(ROUTE_LAG_SYMBOL, "pharmsol_dsl_kernel_route_lag");
        assert_eq!(
            ROUTE_BIOAVAILABILITY_SYMBOL,
            "pharmsol_dsl_kernel_route_bioavailability"
        );
        assert_eq!(ALLOC_F64_BUFFER_SYMBOL, "pharmsol_dsl_alloc_f64_buffer");
        assert_eq!(FREE_F64_BUFFER_SYMBOL, "pharmsol_dsl_free_f64_buffer");
        assert_eq!(JS_KERNEL_EXPORTS[0], ("derive", DERIVE_SYMBOL));
        assert_eq!(
            JS_KERNEL_EXPORTS[7],
            ("route_bioavailability", ROUTE_BIOAVAILABILITY_SYMBOL)
        );
    }

    #[test]
    fn compiled_model_info_round_trips_kernel_availability_and_dimensions() {
        let envelope = CompiledModelInfoEnvelope {
            abi_version: 7,
            model: NativeModelInfo {
                name: "example".to_string(),
                kind: ModelKind::Ode,
                parameters: vec!["ke".to_string(), "v".to_string()],
                derived: vec!["ke_i".to_string(), "v_i".to_string(), "cl_i".to_string()],
                covariates: vec![NativeCovariateInfo {
                    name: "wt".to_string(),
                    index: 0,
                    interpolation: None,
                }],
                states: vec![
                    NativeStateInfo {
                        name: "depot".to_string(),
                        offset: 0,
                    },
                    NativeStateInfo {
                        name: "central".to_string(),
                        offset: 1,
                    },
                ],
                routes: vec![NativeRouteInfo {
                    name: "iv".to_string(),
                    declaration_index: 0,
                    index: 0,
                    kind: Some(RouteKind::Infusion),
                    destination_offset: 1,
                    destination_name: "central".to_string(),
                    has_lag: false,
                    has_bioavailability: false,
                    inject_input_to_destination: true,
                }],
                outputs: vec![NativeOutputInfo {
                    name: "cp".to_string(),
                    index: 0,
                }],
                state_len: 2,
                derived_len: 3,
                output_len: 1,
                route_len: 1,
                analytical: None,
                particles: Some(32),
            },
            kernels: CompiledKernelAvailability {
                derive: true,
                dynamics: true,
                outputs: true,
                init: true,
                drift: false,
                diffusion: false,
                route_lag: true,
                route_bioavailability: false,
            },
        };

        let json = serde_json::to_vec(&envelope).expect("serialize envelope");
        let decoded = decode_compiled_model_info(&json).expect("decode envelope");
        assert_eq!(decoded, envelope);
    }

    #[test]
    fn output_buffer_plan_tracks_aliasing_and_zeroing_rules() {
        let info = NativeModelInfo {
            name: "example".to_string(),
            kind: ModelKind::Ode,
            parameters: vec![],
            derived: vec!["ke_i".to_string(), "v_i".to_string(), "cl_i".to_string()],
            covariates: vec![],
            states: vec![],
            routes: vec![],
            outputs: vec![],
            state_len: 2,
            derived_len: 3,
            output_len: 4,
            route_len: 1,
            analytical: None,
            particles: None,
        };

        let scratch = output_buffer_plan(&info, KernelRole::Diffusion, false, false);
        assert_eq!(scratch.binding, OutputBufferBinding::Scratch);
        assert_eq!(scratch.len, 2);
        assert!(scratch.zero_before_call);

        let states = output_buffer_plan(&info, KernelRole::Dynamics, true, false);
        assert_eq!(states.binding, OutputBufferBinding::States);
        assert_eq!(states.len, 2);
        assert!(!states.zero_before_call);

        let derived = output_buffer_plan(&info, KernelRole::Derive, false, true);
        assert_eq!(derived.binding, OutputBufferBinding::Derived);
        assert_eq!(derived.len, 3);
        assert!(!derived.zero_before_call);
    }
}