mrgsolve
Loading...
Searching...
No Matches
mrgx.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
21
22#ifndef MRGX_H
23#define MRGX_H
24
25#include "mrgsolv.h"
26
34
35namespace mrgx {
36
47Rcpp::Environment get_envir(const databox& self) {
48 return (*(reinterpret_cast<Rcpp::Environment*>(self.envir)));
49}
50
71double rnorm(const double mean, const double sd, const double lower,
72 const double upper) {
73 double x = 0;
74 for(int i=0; i < 50; ++i) {
75 x = R::rnorm(mean,sd);
76 if(x >= lower && x <= upper) return(x);
77 }
78 Rcpp::stop("Could not simulate normal variate within specified bounds");
79}
80
98double rlognorm(const double mean, const double sd, const double lower,
99 const double upper) {
100 double x = 0;
101 for(int i=0; i < 50; ++i) {
102 x = exp(R::rnorm(mean,sd));
103 if(x >= lower && x <= upper) {
104 return(x);
105 }
106 }
107 Rcpp::stop("Could not simulate log normal variate within specified bounds");
108}
109
135template<typename _T___>
136_T___ get(const std::string name, const databox& self) {
137 Rcpp::Environment env = get_envir(self);
138 return env[name];
139}
140
166template<typename _T___>
167void assign(const std::string x, const _T___& value, const databox& self) {
168 Rcpp::Environment env = get_envir(self);
169 env.assign(x, value);
170 return;
171}
172
186template<typename _T___>
187_T___ get(const std::string name) {
188 Rcpp::Environment env = Rcpp::Environment::global_env();
189 return env[name];
190}
191
218template<typename _T___>
219_T___ get(const std::string package, const std::string name) {
220 Rcpp::Environment env = Rcpp::Environment::namespace_env(package);
221 _T___ ans = env[name];
222 return ans;
223}
224
249template<typename _T___>
250_T___ readRDS(const std::string filename) {
251 Rcpp::Function readRDS = get<Rcpp::Function>("base", "readRDS");
252 return readRDS(filename);
253}
254
284Rcpp::Function mt_fun() {
285 // See R/Aaaa.R
286 Rcpp::Environment env = Rcpp::Environment::namespace_env("mrgsolve");
287 Rcpp::Function ans = env["mrgx_mt_fun"];
288 return ans;
289}
290
291}
292
293#endif
member functions mevent and tad come in via housemodel; see inst/base/databox.cpp
Definition mrgsolv.h:96
void * envir
model environment
Definition mrgsolv.h:115
double rnorm(const double mean, const double sd, const double lower, const double upper)
Definition mrgx.h:71
void assign(const std::string x, const _T___ &value, const databox &self)
Definition mrgx.h:167
_T___ readRDS(const std::string filename)
Definition mrgx.h:250
double rlognorm(const double mean, const double sd, const double lower, const double upper)
Definition mrgx.h:98
Rcpp::Function mt_fun()
Definition mrgx.h:284
Rcpp::Environment get_envir(const databox &self)
Return the model environment.
Definition mrgx.h:47
_T___ get(const std::string name, const databox &self)
Definition mrgx.h:136
Definition mrgx.h:35