mrgsolve
Loading...
Searching...
No Matches
odeproblem.h
Go to the documentation of this file.
1// Copyright (C) 2013 - 2026 Metrum Research Group
2//
3// This file is part of mrgsolve.
4//
5// mrgsolve is free software: you can redistribute it and/or modify it
6// under the terms of the GNU General Public License as published by
7// the Free Software Foundation, either version 2 of the License, or
8// (at your option) any later version.
9//
10// mrgsolve is distributed in the hope that it will be useful, but
11// WITHOUT ANY WARRANTY; without even the implied warranty of
12// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13// GNU General Public License for more details.
14//
15// You should have received a copy of the GNU General Public License
16// along with mrgsolve. If not, see <http://www.gnu.org/licenses/>.
17
22
23#ifndef ODEPROBLEM_H
24#define ODEPROBLEM_H
25#include <math.h>
26#include <vector>
27#include <deque>
28#include "RcppInclude.h"
29#include "mrgsolv.h"
30#include "datarecord.h"
31
32//
33// resim functor comes from mrgsolv.h
34// so it can get defined in the model
35//
36
37class odeproblem;
38
40typedef std::deque<rec_ptr> reclist;
41
43typedef std::vector<reclist> recstack;
44
47
50
53
56
59
61//typedef void main_deriv_func(int* neq, double* t,double* y,double* ydot, void* prob);
62typedef void (*LSODA_ODE_SYSTEM_TYPE)(double t, double *y, double *dydt,
63 odeproblem* _data);
64typedef void main_deriv_func(int* neq, double* t,double* y,double* ydot);
65//typedef void (*ODE_FUNC)(double t, double *y, double *dydt, double*);
66typedef void (*MRGSOLVE_ODE_FUNC)(int neq, double* t, double* y, double* ydot, std::vector<double>& param);
67
68
69#define MRGSOLVE_GET_PRED_CL (pred[0])
70#define MRGSOLVE_GET_PRED_VC (pred[1])
71#define MRGSOLVE_GET_PRED_KA (pred[2])
72#define MRGSOLVE_GET_PRED_Q (pred[3])
73#define MRGSOLVE_GET_PRED_VP (pred[4])
74#define MRGSOLVE_GET_PRED_Q2 (pred[5])
75#define MRGSOLVE_GET_PRED_VP2 (pred[6])
76#define MRGSOLVE_GET_PRED_K10 (pred[0]/pred[1])
77#define MRGSOLVE_GET_PRED_K12 (pred[3]/pred[1])
78#define MRGSOLVE_GET_PRED_K21 (pred[3]/pred[4])
79#define MRGSOLVE_GET_PRED_K13 (pred[5]/pred[1])
80#define MRGSOLVE_GET_PRED_K31 (pred[5]/pred[6])
81
82extern "C"{DL_FUNC tofunptr(SEXP a);}
83
86
87template<typename T,typename type2> void tofunptr(T b, type2 a) {
88 b = reinterpret_cast<T>(R_ExternalPtrAddr(a));
89}
90
91void dosimeta(void*);
92void dosimeps(void*);
93
94
101
102public:
103 odeproblem(Rcpp::List param,Rcpp::NumericVector init,
104 Rcpp::List funs,
105 int n_capture_);
106
108
109 void do_init_calc(bool answer) {Do_Init_Calc = answer;}
110 void advance(double tfrom, double tto, LSODA& solver);
111 void call_derivs(double *t, double *y, double *ydot);
112 void init(int pos, double value){Init_value[pos] = value;}
113 double init(int pos){return Init_value[pos];}
114
115 void init_call(const double& time);
116 void init_call_record(const double& time);
117 void init_derivs(double time);
118
119 void y_init(int pos, double value);
120 void y_init(Rcpp::NumericVector init);
121 void y_add(const unsigned int pos, const double& value);
122
123 void table_call();
125 void event_call();
126 void config_call();
127
128 void set_d(rec_ptr this_rec);
129
130 void omega(const Rcpp::S4& mod);
131 void sigma(const Rcpp::S4& mod);
133
134 arma::mat mv_omega(int n);
135 arma::mat mv_sigma(int n);
136
137 void pass_envir(Rcpp::Environment* x){d.envir=reinterpret_cast<void*>(x);};
138
139 bool CFONSTOP(){return d.CFONSTOP;}
140
141 const std::vector<double>& param() {return Param;}
142 void param(int pos, double value) {Param[pos] = value;}
143
144 void check_data_rate(rec_ptr rec, int cmtn);
145 void check_modeled_dur(rec_ptr rec);
146 void check_modeled_rate(rec_ptr rec);
147
148 void rate(unsigned int pos, double value) {R[pos] = value;}
149 double rate(unsigned int pos);
150 void rate0(unsigned int pos, double value) {R0[pos] = value;}
151 double rate0(unsigned int pos);
152
153 int rate_count(unsigned int pos){return infusion_count[pos];}
154 void rate_add(unsigned int pos, const double& value);
155 void rate_rm(unsigned int pos, const double& value);
156 void rate_bump(const unsigned int pos, const double& value);
157 void rate_main(rec_ptr rec, int cmtn);
158 void rate_reset();
159
160 void dur(unsigned int pos, double value) {D[pos] = value;}
161 double dur(unsigned int pos);
162
163 void reset_newid(const double id_);
164
165 void eta(int pos, double value) {d.ETA[pos] = value;}
166 void eps(int pos, double value) {d.EPS[pos] = value;}
167 unsigned short int systemoff(){return d.SYSTEMOFF;}
168
169 void on(unsigned short int cmt);
170 void off(unsigned short int cmt);
171
172 int is_on(unsigned int eq_n){return On[eq_n];}
173
174 void fbio(unsigned int pos, double value) {F.at(pos) = value;}
175 double fbio(unsigned int pos);
176 double alag(int cmt);
177
178 void time(double time_){d.time = time_;}
179 void newind(unsigned int newind_){d.newind = newind_;}
180 unsigned int newind(){return d.newind;}
181
182 void advan(int x);
183 int advan(){return Advan;}
184 void advan2(const double& tfrom, const double& tto);
185 void advan4(const double& tfrom, const double& tto);
186 void advan11(const double& tfrom, const double& tto);
187
188 void set_eta();
189 void set_eps();
190 int neta(){return Omega.n_rows;}
191 int neps(){return Sigma.n_rows;}
192
193 void nid(int n) {d.nid=n;}
194 void idn(int n) {d.idn=n;}
195
196 void nrow(int n) {d.nrow=n;}
197 void rown(int n) {d.rown=n;}
198
199 void inrow(int n) {d.inrow=n;}
200 void irown(int n) {d.irown=n;}
201
203 double capture(int i) {return Capture[i];}
204
206 void copy_parin(const Rcpp::List& parin, const Rcpp::S4& mod);
207 void copy_funs(const Rcpp::List& funs);
208
209 bool any_mtime() {return d.mevector.size() > 0;}
210 std::vector<mrgsolve::evdata> mtimes(){return d.mevector;}
211 void clear_mtime(){d.mevector.clear();}
212 void y(const int pos, const double value){Y[pos] = value;}
213 double y(const int pos){return Y[pos];}
214 int istate(){return Istate;}
215 void istate(int value){Istate = value;}
218 int npar() {return Npar;}
220 int neq() {return Neq;}
222 void tol(double atol, double rtol);
223
224
225 std::vector<double> Y;
226 std::vector<double> Ydot;
227 std::vector<double> Yout;
228 std::vector<double> Param;
229 std::vector<double> Capture;
230 double Atol;
231 double Rtol;
232 double ssAtol;
233 double ssRtol; //< relative tolerance when finding steady state
234 int Npar;
235 int Neq;
236 int Istate;
237 bool ss_fixed;
238 int ss_n;
239 bool ss_flag;
241 std::vector<int> Ss_cmt;
242
244 std::vector<double> R0;
245 std::vector<unsigned int> infusion_count;
246 std::vector<double> R;
247 std::vector<double> D;
248 std::vector<double> Init_value;
249 std::vector<double> Init_dummy;
250 std::vector<double> F;
251 std::vector<double> Alag;
252
253 std::vector<int> On;
255
256 int Advan;
257 std::vector<double> a;
258 std::vector<double> alpha;
259
262
263 arma::mat Omega;
264 arma::mat Sigma;
265
266 std::vector<double> pred;
267
273
276};
277
278
279double PolyExp(const double& x,
280 const double& dose,
281 const double& rate,
282 const double& xinf,
283 const double& tau,
284 const bool ss,
285 const std::vector<double>& a,
286 const std::vector<double>& alpha,
287 const int n);
288
289#endif
void(* MRGSOLVE_ODE_FUNC)(int neq, double *t, double *y, double *ydot, std::vector< double > &param)
Definition LSODA.h:40
void(* LSODA_ODE_SYSTEM_TYPE)(double t, double *y, double *dydt, dtype _data)
Definition LSODA.h:36
void main_deriv_func(int *neq, double *t, double *y, double *ydot)
Definition LSODA.h:38
Definition LSODA.h:44
member functions mevent and tad come in via housemodel; see inst/base/databox.cpp
Definition mrgsolv.h:96
Definition odeproblem.h:100
~odeproblem()
Definition odeproblem.h:107
void check_modeled_rate(rec_ptr rec)
Definition odeproblem.cpp:299
void rate0(unsigned int pos, double value)
Definition odeproblem.h:150
void inrow(int n)
sets number of output rows for current ID
Definition odeproblem.h:199
int Advan
simulation mode: 1/2/3/4 (PK models) or 13 (odes)
Definition odeproblem.h:256
double Rtol
relative tolerance used by ODE solver
Definition odeproblem.h:231
int advan()
Definition odeproblem.h:183
int Neq
number of equations
Definition odeproblem.h:235
int npar()
returns the number of parameters
Definition odeproblem.h:218
void off(unsigned short int cmt)
Definition odeproblem.cpp:373
std::vector< double > Alag
dosing lag time
Definition odeproblem.h:251
bool check_modeled_infusions
Definition odeproblem.h:243
void advan2(const double &tfrom, const double &tto)
Definition odeproblem.cpp:415
double init(int pos)
Definition odeproblem.h:113
std::vector< double > R0
acutal current infusion rate
Definition odeproblem.h:244
dvec & get_capture()
Definition odeproblem.h:202
std::vector< int > On
compartment on/off indicator
Definition odeproblem.h:253
void eps(int pos, double value)
Definition odeproblem.h:166
void config_call()
Call $PREAMBLE function.
Definition odeproblem.cpp:256
void y_init(int pos, double value)
Definition odeproblem.cpp:162
arma::mat mv_omega(int n)
Definition odeproblem.cpp:1048
void on(unsigned short int cmt)
Definition odeproblem.cpp:369
void init(int pos, double value)
Definition odeproblem.h:112
mrgsolve::resim simeps
functor for resimulating epsilons
Definition odeproblem.h:261
void rate_main(rec_ptr rec, int cmtn)
Definition odeproblem.cpp:268
void irown(int n)
sets the output row number for current ID
Definition odeproblem.h:200
unsigned int newind()
Definition odeproblem.h:180
void newind(unsigned int newind_)
Definition odeproblem.h:179
bool ss_fixed
If true, then no warning is issued if SS not reached in ss_n doses.
Definition odeproblem.h:237
arma::mat Omega
variance/covariance matrix for between-subject variability
Definition odeproblem.h:263
std::vector< double > Yout
used to hold Y values during solving
Definition odeproblem.h:227
void set_eps()
set number of EPSs in the model
Definition odeproblem.cpp:145
void set_d(rec_ptr this_rec)
Definition odeproblem.cpp:204
void init_call(const double &time)
Definition odeproblem.cpp:216
std::vector< double > Y
compartment amounts
Definition odeproblem.h:225
void copy_funs(const Rcpp::List &funs)
Definition odeproblem.cpp:941
void do_init_calc(bool answer)
Definition odeproblem.h:109
double alag(int cmt)
Get the lag time from $MAIN.
Definition odeproblem.cpp:120
void set_eta()
set number of ETAs in the model
Definition odeproblem.cpp:140
int ss_n
Max number of doses during SS advance before warning is issued.
Definition odeproblem.h:238
std::vector< double > Ydot
dxdt values
Definition odeproblem.h:226
std::vector< unsigned int > infusion_count
number of active infusions
Definition odeproblem.h:245
int Istate
istate value
Definition odeproblem.h:236
void eta(int pos, double value)
Definition odeproblem.h:165
double ssRtol
Definition odeproblem.h:233
void table_init_call()
void lsoda_init()
Definition odeproblem.h:216
void time(double time_)
Definition odeproblem.h:178
deriv_func Derivs
$ODE function
Definition odeproblem.h:268
mrgsolve::resim simeta
functor for resimulating etas
Definition odeproblem.h:260
void table_call()
Call $TABLE function.
Definition odeproblem.cpp:246
int neps()
Definition odeproblem.h:191
const std::vector< double > & param()
Definition odeproblem.h:141
void advan11(const double &tfrom, const double &tto)
Definition odeproblem.cpp:603
void pass_envir(Rcpp::Environment *x)
Definition odeproblem.h:137
void dur(unsigned int pos, double value)
Definition odeproblem.h:160
event_func Event
$EVENT function
Definition odeproblem.h:271
void rate_rm(unsigned int pos, const double &value)
Definition odeproblem.cpp:357
void call_derivs(double *t, double *y, double *ydot)
Definition odeproblem.cpp:193
void rate_add(unsigned int pos, const double &value)
Definition odeproblem.cpp:348
int rate_count(unsigned int pos)
Definition odeproblem.h:153
std::vector< mrgsolve::evdata > mtimes()
Definition odeproblem.h:210
std::vector< double > pred
brings clearances, volumes, and & for advan 1/2/3/4
Definition odeproblem.h:266
void y(const int pos, const double value)
Definition odeproblem.h:212
bool Do_Init_Calc
Flag regulating whether or not initials are taken from $MAIN.
Definition odeproblem.h:274
bool system_at_ss
flag indicating the system was advanced to SS
Definition odeproblem.h:240
int Npar
number of parameters
Definition odeproblem.h:234
double Atol
absolute tolerance used by ODE solver
Definition odeproblem.h:230
void rate_reset()
Reset all infusion rates.
Definition odeproblem.cpp:261
unsigned short int systemoff()
Definition odeproblem.h:167
void check_modeled_dur(rec_ptr rec)
Definition odeproblem.cpp:313
std::vector< double > a
used for advan 1/2/3/4 calculations
Definition odeproblem.h:257
databox d
various data passed to model functions
Definition odeproblem.h:254
bool CFONSTOP()
Definition odeproblem.h:139
void param(int pos, double value)
Definition odeproblem.h:142
void clear_mtime()
Definition odeproblem.h:211
std::vector< double > Init_dummy
initial conditions for user input
Definition odeproblem.h:249
std::vector< double > Capture
captured data items
Definition odeproblem.h:229
double capture(int i)
Definition odeproblem.h:203
int neq()
returns the number of state variables
Definition odeproblem.h:220
void advan4(const double &tfrom, const double &tto)
Definition odeproblem.cpp:494
double y(const int pos)
Definition odeproblem.h:213
std::vector< double > D
receive user input for infusion duration
Definition odeproblem.h:247
bool any_mtime()
Definition odeproblem.h:209
double ssAtol
absolute tolerance when finding steady state
Definition odeproblem.h:232
arma::mat mv_sigma(int n)
Definition odeproblem.cpp:1052
void check_data_rate(rec_ptr rec, int cmtn)
Definition odeproblem.cpp:278
std::vector< double > F
bioavability
Definition odeproblem.h:250
void event_call()
Call $EVENT function.
Definition odeproblem.cpp:251
std::vector< double > R
receive user input for infusion rate
Definition odeproblem.h:246
int neta()
Definition odeproblem.h:190
table_func Table
$TABLE function
Definition odeproblem.h:270
void sigma(const Rcpp::S4 &mod)
Definition odeproblem.cpp:1034
int interrupt
Check User Interrupt interval (number of simulation records).
Definition odeproblem.h:275
void tol(double atol, double rtol)
sets the absolute and relative tolerances
Definition odeproblem.cpp:149
void rown(int n)
sets the current data set row number
Definition odeproblem.h:197
void nid(int n)
sets the number of IDs
Definition odeproblem.h:193
int istate()
Definition odeproblem.h:214
arma::mat Sigma
variance/covariance matrix for within-subject variability
Definition odeproblem.h:264
void idn(int n)
sets the current ID number
Definition odeproblem.h:194
void copy_sigma_diagonals()
Definition odeproblem.cpp:1039
void init_call_record(const double &time)
Definition odeproblem.cpp:240
void copy_parin(const Rcpp::List &parin, const Rcpp::S4 &mod)
copies items passed in through parin into the odeproblem object
Definition odeproblem.cpp:924
void istate(int value)
Definition odeproblem.h:215
void nrow(int n)
sets the number of data set rows
Definition odeproblem.h:196
void reset_newid(const double id_)
Reset odeproblem object for new individual.
Definition odeproblem.cpp:328
odeproblem(Rcpp::List param, Rcpp::NumericVector init, Rcpp::List funs, int n_capture_)
Definition odeproblem.cpp:49
void fbio(unsigned int pos, double value)
Definition odeproblem.h:174
bool ss_flag
flag indicating when the system is advancing to SS
Definition odeproblem.h:239
std::vector< double > Init_value
initial conditions
Definition odeproblem.h:248
void y_add(const unsigned int pos, const double &value)
add value to compartment pos
Definition odeproblem.cpp:177
void rate_bump(const unsigned int pos, const double &value)
Definition odeproblem.cpp:353
void init_derivs(double time)
Definition odeproblem.cpp:200
void advance(double tfrom, double tto, LSODA &solver)
Definition odeproblem.cpp:378
init_func Inits
$MAIN function
Definition odeproblem.h:269
std::vector< double > Param
parameter vector
Definition odeproblem.h:228
void rate(unsigned int pos, double value)
Definition odeproblem.h:148
int is_on(unsigned int eq_n)
Definition odeproblem.h:172
std::vector< double > alpha
used for advan 1/2/3/4 calculation
Definition odeproblem.h:258
void omega(const Rcpp::S4 &mod)
Definition odeproblem.cpp:1029
std::vector< int > Ss_cmt
vector of compartments to consider for SS
Definition odeproblem.h:241
config_func Config
$PREAMBLE function
Definition odeproblem.h:272
std::shared_ptr< datarecord > rec_ptr
Definition datarecord.h:31
std::deque< rec_ptr > reclist
Definition datarecord.h:32
#define MRGSOLVE_TABLE_SIGNATURE
signature for $TABLE
Definition mrgsolv.h:144
#define MRGSOLVE_ODE_SIGNATURE
signature for $ODE
Definition mrgsolv.h:164
#define MRGSOLVE_EVENT_SIGNATURE
signature for $EVENT same as what we use for $TABLE
Definition mrgsolv.h:160
#define MRGSOLVE_INIT_SIGNATURE
signature for $MAIN
Definition mrgsolv.h:130
std::vector< double > dvec
vector of doubles
Definition mrgsolv.h:127
#define MRGSOLVE_CONFIG_SIGNATURE
signature for $PREAMBLE
Definition mrgsolv.h:174
std::vector< reclist > recstack
vector of reclist vectors comprising a data set
Definition odeproblem.h:43
void dosimeta(void *)
Definition odeproblem.cpp:33
void(* config_func)(MRGSOLVE_CONFIG_SIGNATURE)
$PREAMBLE function
Definition odeproblem.h:58
main_deriv_func main_derivs
Definition odeproblem.h:84
void dosimeps(void *)
Definition odeproblem.cpp:41
void neg_istate(int istate)
double PolyExp(const double &x, const double &dose, const double &rate, const double &xinf, const double &tau, const bool ss, const std::vector< double > &a, const std::vector< double > &alpha, const int n)
Definition odeproblem.cpp:818
void(* table_func)(MRGSOLVE_TABLE_SIGNATURE)
$TABLE function
Definition odeproblem.h:49
void(* event_func)(MRGSOLVE_EVENT_SIGNATURE)
$EVENT function
Definition odeproblem.h:52
void(* init_func)(MRGSOLVE_INIT_SIGNATURE)
$MAIN function
Definition odeproblem.h:46
DL_FUNC tofunptr(SEXP a)
void(* deriv_func)(MRGSOLVE_ODE_SIGNATURE)
$ODE function
Definition odeproblem.h:55
Resim functor.
Definition mrgsolv.h:38