-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpmx_solve_twocpt_rk45.hpp
344 lines (325 loc) · 15.2 KB
/
pmx_solve_twocpt_rk45.hpp
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
#ifndef STAN_MATH_TORSTEN_TWOCPT_RK45_HPP
#define STAN_MATH_TORSTEN_TWOCPT_RK45_HPP
#include <Eigen/Dense>
#include <stan/math/torsten/meta.hpp>
#include <stan/math/torsten/ev_manager.hpp>
#include <stan/math/torsten/ev_solver.hpp>
#include <stan/math/torsten/pmx_coupled_model.hpp>
#include <stan/math/torsten/pmx_twocpt_model.hpp>
#include <stan/math/torsten/pmx_ode_model.hpp>
#include <stan/math/torsten/dsolve/pmx_odeint_integrator.hpp>
#include <boost/math/tools/promotion.hpp>
#include <vector>
namespace torsten {
/**
* Compute the predicted amounts in each compartment at each event
* of an ODE model. The model contains a base 2 Compartment PK
* component which gets solved analytically, while the other ODEs
* are solved numerically using stan::math::integrate_ode_rk45. This
* amounts to using the mix solver method.
*
* <b>Warning:</b> This prototype does not handle steady state events.
*
* @tparam T0 type of scalar for time of events.
* @tparam T1 type of scalar for amount at each event.
* @tparam T2 type of scalar for rate at each event.
* @tparam T3 type of scalar for inter-dose inteveral at each event.
* @tparam T4 type of scalars for the model parameters.
* @tparam T5 type of scalars for the bio-variability parameters.
* @tparam T6 type of scalars for the model tlag parameters.
* @tparam F type of ODE system function.
* @param[in] f functor for base ordinary differential equation
* which gets solved numerically.
* @param[in] nOde number of ODEs we solve numerically.
* @param[in] time times of events
* @param[in] amt amount at each event
* @param[in] rate rate at each event
* @param[in] ii inter-dose interval at each event
* @param[in] evid event identity:
* (0) observation
* (1) dosing
* (2) other
* (3) reset
* (4) reset AND dosing
* @param[in] cmt compartment number at each event
* @param[in] addl additional dosing at each event
* @param[in] ss steady state approximation at each event (0: no, 1: yes)
* @param[in] theta vector of ODE parameters
* @param[in] biovar bio-availability in each compartment
* @param[in] tlag lag time in each compartment
* @param[in] rel_tol relative tolerance for the Boost ode solver
* @param[in] abs_tol absolute tolerance for the Boost ode solver
* @param[in] max_num_steps maximal number of steps to take within
* the Boost ode solver
* @return a matrix with predicted amount in each compartment
* at each event.
*
* FIX ME: msg should be passed on to functor (allows use of
* print statement inside ODE system).
*/
template <typename T0, typename T1, typename T2, typename T3, typename T4,
typename T5, typename T6, typename F>
stan::matrix_return_t<T0, T1, T2, T3, T4, T5, T6>
pmx_solve_twocpt_rk45(const F& f,
const int nOde,
const std::vector<T0>& time,
const std::vector<T1>& amt,
const std::vector<T2>& rate,
const std::vector<T3>& ii,
const std::vector<int>& evid,
const std::vector<int>& cmt,
const std::vector<int>& addl,
const std::vector<int>& ss,
const std::vector<std::vector<T4> >& theta,
const std::vector<std::vector<T5> >& biovar,
const std::vector<std::vector<T6> >& tlag,
double rel_tol,
double abs_tol,
long int max_num_steps,
double as_rel_tol,
double as_abs_tol,
long int as_max_num_steps,
std::ostream* msgs = 0) {
using std::vector;
using Eigen::Dynamic;
using Eigen::Matrix;
using boost::math::tools::promote_args;
// check arguments
static const char* function("pmx_solve_twocpt_rk45");
torsten::pmx_check(time, amt, rate, ii, evid, cmt, addl, ss, theta, function);
// Construct dummy array of matrix for last argument of pred
Matrix<T4, Dynamic, Dynamic> dummy_system;
vector<Matrix<T4, Dynamic, Dynamic> >
dummy_systems(1, dummy_system);
const int &nPK = torsten::PMXTwoCptModel<double>::Ncmt;
dsolve::PMXOdeIntegrator<dsolve::PMXVariadicOdeSystem, dsolve::PMXOdeintIntegrator<dsolve::odeint_scheme_rk45>>
integrator(rel_tol, abs_tol, max_num_steps, as_rel_tol, as_abs_tol, as_max_num_steps, msgs);
const int nCmt = nPK + nOde;
using ER = NONMENEventsRecord<T0, T1, T2, T3>;
using EM = EventsManager<ER, NonEventParameters<T0, T4, std::vector, std::tuple<T5, T6> >>;
const ER events_rec(nCmt, time, amt, rate, ii, evid, cmt, addl, ss);
Matrix<typename EM::T_scalar, -1, -1> pred(events_rec.num_event_times(), EM::nCmt(events_rec));
pred.setZero();
using model_type = torsten::PkTwoCptOdeModel<typename EM::T_par, F>;
EventSolver<model_type, EM> pr;
pr.pred(0, events_rec, pred, integrator, theta, biovar, tlag, nOde, f);
return pred;
}
/*
* overload with default ode & algebra solver controls
*/
template <typename T0, typename T1, typename T2, typename T3, typename T4,
typename T5, typename T6, typename F>
stan::matrix_return_t<T0, T1, T2, T3, T4, T5, T6>
pmx_solve_twocpt_rk45(const F& f,
const int nOde,
const std::vector<T0>& time,
const std::vector<T1>& amt,
const std::vector<T2>& rate,
const std::vector<T3>& ii,
const std::vector<int>& evid,
const std::vector<int>& cmt,
const std::vector<int>& addl,
const std::vector<int>& ss,
const std::vector<std::vector<T4> >& theta,
const std::vector<std::vector<T5> >& biovar,
const std::vector<std::vector<T6> >& tlag,
std::ostream* msgs = 0) {
return pmx_solve_twocpt_rk45(f, nOde,
time, amt, rate, ii, evid, cmt, addl, ss,
theta, biovar, tlag,
1.e-6, 1.e-6, 1e6,
1.e-6, 1.e-6, 1e2,
msgs);
}
/*
* overload with default algebra solver controls
*/
template <typename T0, typename T1, typename T2, typename T3, typename T4,
typename T5, typename T6, typename F>
stan::matrix_return_t<T0, T1, T2, T3, T4, T5, T6>
pmx_solve_twocpt_rk45(const F& f,
const int nOde,
const std::vector<T0>& time,
const std::vector<T1>& amt,
const std::vector<T2>& rate,
const std::vector<T3>& ii,
const std::vector<int>& evid,
const std::vector<int>& cmt,
const std::vector<int>& addl,
const std::vector<int>& ss,
const std::vector<std::vector<T4> >& theta,
const std::vector<std::vector<T5> >& biovar,
const std::vector<std::vector<T6> >& tlag,
double rel_tol,
double abs_tol,
long int max_num_steps,
std::ostream* msgs = 0) {
return pmx_solve_twocpt_rk45(f, nOde,
time, amt, rate, ii, evid, cmt, addl, ss,
theta, biovar, tlag,
rel_tol, abs_tol, max_num_steps,
1.e-6, 1.e-6, 1e2,
msgs);
}
/**
* Overload function to allow user to pass an std::vector for
* pMatrix/bioavailability/tlag
*/
template <typename T0, typename T1, typename T2, typename T3,
typename T_par, typename T_biovar, typename T_tlag,
typename F,
typename = require_any_not_std_vector_t<T_par, T_biovar, T_tlag> >
stan::matrix_return_t<T0, T1, T2, T3, T_par, T_biovar, T_tlag>
pmx_solve_twocpt_rk45(const F& f,
const int nOde,
const std::vector<T0>& time,
const std::vector<T1>& amt,
const std::vector<T2>& rate,
const std::vector<T3>& ii,
const std::vector<int>& evid,
const std::vector<int>& cmt,
const std::vector<int>& addl,
const std::vector<int>& ss,
const std::vector<T_par>& pMatrix,
const std::vector<T_biovar>& biovar,
const std::vector<T_tlag>& tlag,
double rel_tol,
double abs_tol,
long int max_num_steps,
double as_rel_tol,
double as_abs_tol,
long int as_max_num_steps,
std::ostream* msgs = 0) {
auto param_ = torsten::to_array_2d(pMatrix);
auto biovar_ = torsten::to_array_2d(biovar);
auto tlag_ = torsten::to_array_2d(tlag);
return pmx_solve_twocpt_rk45(f, nOde,
time, amt, rate, ii, evid, cmt, addl, ss,
param_, biovar_, tlag_,
rel_tol, abs_tol, max_num_steps,
as_rel_tol, as_abs_tol, as_max_num_steps);
}
/**
* Overload function to allow user to pass an std::vector for
* pMatrix/bioavailability/tlag, with defualt ode &
* algebra solver controls
*/
template <typename T0, typename T1, typename T2, typename T3,
typename T_par, typename T_biovar, typename T_tlag,
typename F,
typename = require_any_not_std_vector_t<T_par, T_biovar, T_tlag> >
stan::matrix_return_t<T0, T1, T2, T3, T_par, T_biovar, T_tlag>
pmx_solve_twocpt_rk45(const F& f,
const int nOde,
const std::vector<T0>& time,
const std::vector<T1>& amt,
const std::vector<T2>& rate,
const std::vector<T3>& ii,
const std::vector<int>& evid,
const std::vector<int>& cmt,
const std::vector<int>& addl,
const std::vector<int>& ss,
const std::vector<T_par>& pMatrix,
const std::vector<T_biovar>& biovar,
const std::vector<T_tlag>& tlag,
std::ostream* msgs = 0) {
return pmx_solve_twocpt_rk45(f, nOde,
time, amt, rate, ii, evid, cmt, addl, ss,
pMatrix, biovar, tlag,
1.e-6, 1.e-6, 1e6,
1.e-6, 1.e-6, 1e2,
msgs);
}
/**
* Overload function to allow user to pass an std::vector for
* pMatrix/bioavailability/tlag, with defualt algebra solver controls
*/
template <typename T0, typename T1, typename T2, typename T3,
typename T_par, typename T_biovar, typename T_tlag,
typename F,
typename = require_any_not_std_vector_t<T_par, T_biovar, T_tlag> >
stan::matrix_return_t<T0, T1, T2, T3, T_par, T_biovar, T_tlag>
pmx_solve_twocpt_rk45(const F& f,
const int nOde,
const std::vector<T0>& time,
const std::vector<T1>& amt,
const std::vector<T2>& rate,
const std::vector<T3>& ii,
const std::vector<int>& evid,
const std::vector<int>& cmt,
const std::vector<int>& addl,
const std::vector<int>& ss,
const std::vector<T_par>& pMatrix,
const std::vector<T_biovar>& biovar,
const std::vector<T_tlag>& tlag,
double rel_tol,
double abs_tol,
long int max_num_steps,
std::ostream* msgs = 0) {
return pmx_solve_twocpt_rk45(f, nOde,
time, amt, rate, ii, evid, cmt, addl, ss,
pMatrix, biovar, tlag,
rel_tol, abs_tol, max_num_steps,
1.e-6, 1.e-6, 1e2,
msgs);
}
// old version
template <typename T0, typename T1, typename T2, typename T3, typename T4,
typename T5, typename T6, typename F>
stan::matrix_return_t<T0, T1, T2, T3, T4, T5, T6>
mixOde2CptModel_rk45(const F& f,
const int nOde,
const std::vector<T0>& time,
const std::vector<T1>& amt,
const std::vector<T2>& rate,
const std::vector<T3>& ii,
const std::vector<int>& evid,
const std::vector<int>& cmt,
const std::vector<int>& addl,
const std::vector<int>& ss,
const std::vector<std::vector<T4> >& theta,
const std::vector<std::vector<T5> >& biovar,
const std::vector<std::vector<T6> >& tlag,
double rel_tol = 1e-6,
double abs_tol = 1e-6,
long int max_num_steps = 1e6,
std::ostream* msgs = 0) {
auto x = pmx_solve_twocpt_rk45(f, nOde,
time, amt, rate, ii, evid, cmt, addl, ss,
theta, biovar, tlag,
rel_tol, abs_tol, max_num_steps,
msgs);
return x.transpose();
}
template <typename T0, typename T1, typename T2, typename T3,
typename T_par, typename T_biovar, typename T_tlag,
typename F,
typename = require_any_not_std_vector_t<T_par, T_biovar, T_tlag> >
stan::matrix_return_t<T0, T1, T2, T3, T_par, T_biovar, T_tlag>
mixOde2CptModel_rk45(const F& f,
const int nOde,
const std::vector<T0>& time,
const std::vector<T1>& amt,
const std::vector<T2>& rate,
const std::vector<T3>& ii,
const std::vector<int>& evid,
const std::vector<int>& cmt,
const std::vector<int>& addl,
const std::vector<int>& ss,
const std::vector<T_par>& pMatrix,
const std::vector<T_biovar>& biovar,
const std::vector<T_tlag>& tlag,
double rel_tol = 1e-6,
double abs_tol = 1e-6,
long int max_num_steps = 1e6,
std::ostream* msgs = 0) {
auto x = pmx_solve_twocpt_rk45(f, nOde,
time, amt, rate, ii, evid, cmt, addl, ss,
pMatrix, biovar, tlag,
rel_tol, abs_tol, max_num_steps,
msgs);
return x.transpose();
}
}
#endif