/home/marcle/src/R/microsimulation/src/microsimulation.cc
Go to the documentation of this file.
#include <microsimulation.h>
namespace ssim {
double rweibullHR(double shape, double scale, double hr){
return R::rweibull(shape, scale*pow(hr,1.0/shape));
}
Time now() {
return Sim::clock();
}
Time simTime() {
return Sim::clock();
}
static Rng * default_stream, * current_stream;
static double rn = 0.0;
Rng::~Rng() {
if (current_stream->id == this->id)
current_stream = default_stream;
}
void Rng::set() {
current_stream = this;
}
extern "C" {
void r_create_current_stream()
{
default_stream = new Rng();
current_stream = default_stream;
}
void r_remove_current_stream()
{
delete default_stream;
}
void r_set_user_random_seed(double * inseed) {
double seed[6];
for(int i=0; i<6; i++) {
seed[i] = inseed[i];
}
Rng::SetPackageSeed(seed);
default_stream->SetSeed(seed);
}
void r_get_user_random_seed(double * outseed) {
double seed[6];
default_stream->GetState(seed);
for(int i=0; i<6; i++) {
outseed[i] = (double)seed[i];
}
}
void r_next_rng_substream() {
default_stream->ResetNextSubstream();
}
void r_rng_advance_substream(double * inoutseed, int * n) {
RngStream r;
double seed[6];
for (int i=0; i<6; i++)
seed[i]=inoutseed[i];
r.SetSeed(seed);
r.AdvanceSubstream(0, *n);
r.GetState(seed);
for (int i=0; i<6; i++)
inoutseed[i]= seed[i];
}
double *user_unif_rand ()
{
if (!current_stream) {
REprintf("user_unif_rand(): No stream created yet!");
return NULL;
}
rn = current_stream->RandU01();
return &rn;
}
void test_rstream2(double * x) {
Rng * s1 = new Rng();
Rng * s2 = new Rng();
x[0]=WithRNG(s1,R::rexp(1.0));
x[1]=WithRNG(s2,R::rexp(1.0));
s1->ResetNextSubstream();
x[2]=R::rexp(1.0);
delete s1;
delete s2;
}
} // extern "C"
} // namespace ssim
namespace R {
double rnormPos(double mean, double sd) {
double x;
while ((x=R::rnorm(mean,sd))<0.0) { }
return x;
}
double rllogis(double shape, double scale) {
double u = R::runif(0.0,1.0);
return scale*exp(-log(1.0/u-1.0)/shape);
}
double rllogis_trunc(double shape, double scale, double left) {
double S0 = 1.0/(1.0+exp(log(left/scale)*shape));
double u = R::runif(0.0,1.0);
return scale*exp(log(1.0/(u*S0)-1.0)/shape);
}
double rgompertz(double shape, double rate) {
double u = 1.0 - R::runif(0.0, 1.0);
return (shape < 0.0 && u<exp(rate/shape)) ? R_PosInf :
log(1.0 - shape*log(u)/rate)/shape;
}
}
ssim::r_create_current_stream
void r_create_current_stream()
A utility function to create the current_stream. Used when initialising the microsimulation package i...
Definition: microsimulation.cc:32
ssim::Rng::~Rng
virtual ~Rng()
Definition: microsimulation.cc:21
ssim::rn
static double rn
Definition: microsimulation.cc:19
ssim::RngStream::SetPackageSeed
static bool SetPackageSeed(const double seed[6])
Definition: RngStream.cpp:391
R::rgompertz
double rgompertz(double shape, double rate)
Random draw from a Gompertz distribution.
Definition: microsimulation.cc:119
ssim::simTime
Time simTime()
simTime() function for OMNET++ API compatibility
Definition: microsimulation.cc:13
ssim::default_stream
static Rng * default_stream
Definition: microsimulation.cc:18
ssim::RngStream::AdvanceSubstream
void AdvanceSubstream(int32_t e, int32_t c)
Definition: RngStream.cpp:454
R::rllogis_trunc
double rllogis_trunc(double shape, double scale, double left)
rllogis_trunc function for a random covariate from a log-logistic distribution with shape and scale w...
Definition: microsimulation.cc:113
WithRNG
#define WithRNG(rng, expr)
WithRNG is a macro for using the random number generator rng and then evaluating the expression expr.
Definition: microsimulation.h:200
ssim::r_set_user_random_seed
void r_set_user_random_seed(double *inseed)
A utility function to set the user random seed for the simulation.
Definition: microsimulation.cc:43
ssim::now
Time now()
now() function for compatibility with C++SIM
Definition: microsimulation.cc:9
ssim::current_stream
static Rng * current_stream
Definition: microsimulation.cc:18
ssim::RngStream::SetSeed
bool SetSeed(const double seed[6])
Definition: RngStream.cpp:402
R::rllogis
double rllogis(double shape, double scale)
rllogis function for a random covariate from a log-logistic distribution with shape and scale....
Definition: microsimulation.cc:108
ssim::r_remove_current_stream
void r_remove_current_stream()
A utility function to remove the current_stream. Used when finalising the microsimulation package in ...
Definition: microsimulation.cc:38
ssim::RngStream::ResetNextSubstream
void ResetNextSubstream()
Definition: RngStream.cpp:381
ssim::RngStream::GetState
void GetState(double seed[6]) const
Definition: RngStream.cpp:496
R
Definition: microsimulation.cc:101
ssim::Sim::clock
static Time clock()
returns the current virtual time for the current process
Definition: ssim.cc:264
ssim::r_rng_advance_substream
void r_rng_advance_substream(double *inoutseed, int *n)
A utility function to advance the random sub-stream n steps for a specified seed.
Definition: microsimulation.cc:64
ssim::Time
double Time
virtual time type
Definition: ssim.h:75
ssim
name space for the Siena simulator.
Definition: microsimulation.cc:3
ssim::rweibullHR
double rweibullHR(double shape, double scale, double hr)
Random Weibull distribution for a given shape, scale and hazard ratio.
Definition: microsimulation.cc:5
ssim::RngStream::RandU01
double RandU01()
Definition: RngStream.cpp:566
ssim::r_get_user_random_seed
void r_get_user_random_seed(double *outseed)
A utility function to set the user random seed for the simulation.
Definition: microsimulation.cc:52
ssim::Rng::id
int id
Definition: microsimulation.h:561
ssim::r_next_rng_substream
void r_next_rng_substream()
A utility function to move to the next user random stream for the simulation.
Definition: microsimulation.cc:60
ssim::test_rstream2
void test_rstream2(double *x)
Simple test of the random streams (with a stupid name)
Definition: microsimulation.cc:86
R::rnormPos
double rnormPos(double mean, double sd)
rnorm function constrained to be positive. This uses brute-force re-sampling rather than conditioning...
Definition: microsimulation.cc:102
ssim::Rng
Definition: microsimulation.h:548
microsimulation.h
ssim::Rng::set
void set()
Definition: microsimulation.cc:26
ssim::user_unif_rand
double * user_unif_rand()
Definition: microsimulation.cc:76
ssim::RngStream
Definition: RngStream.h:30