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
pub
use ;
use crate::;
use HashMap;
/// Type alias for floating point values used in simulations
pub type T = f64;
/// Type alias for state vector type used in simulations
pub type V = ;
/// Type alias for matrix type used in simulations
pub type M = ;
/// This closure represents the differential equation of the model.
///
/// # Parameters
/// - `x`: The state vector at time t
/// - `p`: The parameters of the model; Use the `fetch_params!` macro to extract the parameters
/// - `t`: The time at which the differential equation is evaluated
/// - `dx`: A mutable reference to the derivative of the state vector at time t
/// - `bolus`: A vector of bolus amounts at time t
/// - `rateiv`: A vector of infusion rates at time t
/// - `cov`: A reference to the covariates at time t; Use the `fetch_cov!` macro to extract the covariates
///
/// # Example
/// ```ignore
/// use pharmsol::*;
/// let diff_eq = |x, p, t, dx, bolus, rateiv, cov| {
/// fetch_params!(p, ka, ke, v);
/// fetch_cov!(cov, t, wt);
/// dx[0] = -ka * x[0];
/// dx[1] = ka * x[0] - ke * x[1];
/// };
/// ```
pub type DiffEq = fn;
/// This closure represents an Analytical solution of the model.
/// See [`equation::analytical`] module for examples.
///
/// # Parameters
/// - `x`: The state vector at time t
/// - `p`: The parameters of the model; Use the `fetch_params!` macro to extract the parameters
/// - `t`: The time at which the output equation is evaluated
/// - `rateiv`: A reference to a vector of infusion rates at time t
/// - `cov`: A reference to the covariates at time t; Use the `fetch_cov!` macro to extract the covariates
///
/// TODO: Remove covariates. They are not used in the analytical solution
pub type AnalyticalEq = fn ;
/// This closure represents the drift term of a stochastic differential equation model.
///
/// # Parameters
/// - `x`: The state vector at time t
/// - `p`: The parameters of the model; Use the `fetch_params!` macro to extract the parameters
/// - `t`: The time at which the drift term is evaluated
/// - `dx`: A mutable reference to the derivative of the state vector at time t
/// - `rateiv`: A vector of infusion rates at time t
/// - `cov`: A reference to the covariates at time t; Use the `fetch_cov!` macro to extract the covariates
///
/// # Example
/// ```ignore
/// use pharmsol::*;
/// let drift = |x, p, t, dx, rateiv, cov| {
/// fetch_params!(p, mka, mke, v);
/// fetch_cov!(cov, t, wt);
/// ka = dx[2];
/// ke = dx[3];
/// dx[0] = -ka * x[0];
/// dx[1] = ka * x[0] - ke * x[1];
/// dx[2] = -dx[2] + mka; // Mean reverting to mka
/// dx[3] = -dx[3] + mke; // Mean reverting to mke
/// };
/// ```
// pub type Drift = DiffEq;
pub type Drift = fn;
/// This closure represents the diffusion term of a stochastic differential equation model.
///
/// # Parameters
/// - `p`: The parameters of the model; Use the `fetch_params!` macro to extract the parameters
/// - `d`: A mutable reference to the diffusion term for each state variable
/// (This vector should have the same length as the x, and dx vectors on the drift closure)
pub type Diffusion = fn;
/// This closure represents the initial state of the system.
///
/// # Parameters
/// - `p`: The parameters of the model; Use the `fetch_params!` macro to extract the parameters
/// - `t`: The time at which the initial state is evaluated; Hardcoded to 0.0
/// - `cov`: A reference to the covariates at time t; Use the `fetch_cov!` macro to extract the covariates
/// - `x`: A mutable reference to the state vector at time t
///
/// # Example
/// ```ignore
/// use pharmsol::*;
/// let init = |p, t, cov, x| {
/// fetch_params!(p, ka, ke, v);
/// fetch_cov!(cov, t, wt);
/// x[0] = 500.0;
/// x[1] = 0.0;
/// };
/// ```
pub type Init = fn;
/// This closure represents the output equation of the model.
///
/// # Parameters
/// - `x`: The state vector at time t
/// - `p`: The parameters of the model; Use the `fetch_params!` macro to extract the parameters
/// - `t`: The time at which the output equation is evaluated
/// - `cov`: A reference to the covariates at time t; Use the `fetch_cov!` macro to extract the covariates
/// - `y`: A mutable reference to the output vector at time t
///
/// # Example
/// ```ignore
/// use pharmsol::*;
/// let out = |x, p, t, cov, y| {
/// fetch_params!(p, ka, ke, v);
/// y[0] = x[1] / v;
/// };
/// ```
pub type Out = fn;
/// This closure represents the secondary equation of the model.
///
/// Secondary equations are used to update the parameter values based on the covariates.
///
/// # Parameters
/// - `p`: The parameters of the model; Use the `fetch_params!` macro to extract the parameters
/// - `t`: The time at which the secondary equation is evaluated
/// - `cov`: A reference to the covariates at time t; Use the `fetch_cov!` macro to extract the covariates
///
/// # Example
/// ```ignore
/// use pharmsol::*;
/// let sec_eq = |p, _t, cov| {
/// fetch_params!(p, ka, ke);
/// fetch_cov!(cov, t, wt);
/// ka = ka * wt;
/// };
/// ```
pub type SecEq = fn;
/// This closure represents the lag time of the model.
///
/// The lag term delays only the boluses going into a specific compartment.
///
/// # Parameters
/// - `p`: The parameters of the model; Use the `fetch_params!` macro to extract the parameters
///
/// # Returns
/// - A hashmap with the lag times for each compartment. If not present, lag is assumed to be 0.
///
/// There is a convenience macro `lag!` to create the hashmap
///
/// # Example
/// ```ignore
/// use pharmsol::*;
/// let lag = |p, _t, _cov| {
/// fetch_params!(p, tlag);
/// lag! {0=>tlag, 1=>0.3}
/// };
/// ```
/// This will lag the bolus going into the first compartment by tlag and the bolus going into the
/// second compartment by 0.3
pub type Lag = fn ;
/// This closure represents the fraction absorbed (also called bioavailability or protein binding)
/// of the model.
///
/// The fa term is used to adjust the amount of drug that is absorbed into the system.
///
/// # Parameters
/// - `p`: The parameters of the model; Use the `fetch_params!` macro to extract the parameters
///
/// # Returns
/// - A hashmap with the fraction absorbed for each compartment. If not present, it is assumed to be 1.
///
/// There is a convenience macro `fa!` to create the hashmap
///
/// # Example
/// ```ignore
/// use pharmsol::*;
/// let fa = |p, _t, _cov| {
/// fetch_params!(p, fa);
/// fa! {0=>fa, 1=>0.3}
/// };
/// ```
/// This will adjust the amount of drug absorbed into the first compartment by fa and the amount of drug
/// absorbed into the second compartment by 0.3
pub type Fa = fn ;
/// The dimensions of the model: number of states, drug inputs, and output equations.
///
/// # Fields
/// - `nstates`: Number of state variables (ODE compartments)
/// - `ndrugs`: Number of drug input channels (size of bolus[] and rateiv[])
/// - `nout`: Number of output equations
///
/// # Defaults
/// All dimensions default to 5 if not explicitly set via builder methods.
///
/// # Example
/// ```ignore
/// // Using the builder pattern on ODE/Analytical/SDE
/// let ode = equation::ODE::new(diffeq, lag, fa, init, out)
/// .with_nstates(2)
/// .with_ndrugs(1)
/// .with_nout(1);
/// ```
// Re-export cache types at the simulator level for convenience.
pub use ;