Microsimulation API

/home/marcle/src/R/microsimulation/src/microsimulation.cc
Go to the documentation of this file.
1
2 //#include <stdio.h>
3 //#include <stdlib.h>
4 //#include <string.h>
5
6 #include <microsimulation.h>
7
8 namespace ssim {
9
10 double rweibullHR(double shape, double scale, double hr){
11 return R::rweibull(shape, scale*pow(hr,1.0/shape));
12 }
13
14 Time now() {
15 return Sim::clock();
16 }
17
18 Time simTime() {
19 return Sim::clock();
20 }
21
22
23 static Rng * default_stream, * current_stream;
24 static double rn = 0.0;
25
26 Rng::~Rng() {
27 if (current_stream->id == this->id)
28 current_stream = default_stream;
29 }
30
31 void Rng::set() {
32 current_stream = this;
33 }
34
35 extern "C" {
36
37 void r_create_current_stream()
38 {
39 default_stream = new Rng();
40 current_stream = default_stream;
41 }
42
43 void r_remove_current_stream()
44 {
45 delete default_stream;
46 }
47
48 void r_set_user_random_seed(double * inseed) {
49 double seed[6];
50 for(int i=0; i<6; i++) {
51 seed[i] = inseed[i];
52 }
53 Rng::SetPackageSeed(seed);
54 default_stream->SetSeed(seed);
55 }
56
57 void r_get_user_random_seed(double * outseed) {
58 double seed[6];
59 default_stream->GetState(seed);
60 for(int i=0; i<6; i++) {
61 outseed[i] = (double)seed[i];
62 }
63 }
64
65 void r_next_rng_substream() {
66 default_stream->ResetNextSubstream();
67 }
68
69 void r_rng_advance_substream(double * inoutseed, int * n) {
70 RngStream r;
71 double seed[6];
72 for (int i=0; i<6; i++)
73 seed[i]=inoutseed[i];
74 r.SetSeed(seed);
75 r.AdvanceSubstream(0, *n);
76 r.GetState(seed);
77 for (int i=0; i<6; i++)
78 inoutseed[i]= seed[i];
79 }
80
81 double *user_unif_rand ()
82 {
83 if (!current_stream) {
84 REprintf("user_unif_rand(): No stream created yet!");
85 return NULL;
86 }
87 rn = current_stream->RandU01();
88 return &rn;
89 }
90
91 void test_rstream2(double * x) {
92 Rng * s1 = new Rng();
93 Rng * s2 = new Rng();
94 x[0]=WithRNG(s1,R::rexp(1.0));
95 x[1]=WithRNG(s2,R::rexp(1.0));
96 s1->ResetNextSubstream();
97 x[2]=R::rexp(1.0);
98 delete s1;
99 delete s2;
100 }
101
102 } // extern "C"
103
104 } // namespace ssim
105
106 namespace R {
107 double rnormPos(double mean, double sd) {
108 double x;
109 while ((x=R::rnorm(mean,sd))<0.0) { }
110 return x;
111 }
112
113 double rllogis(double shape, double scale) {
114 double u = R::runif(0.0,1.0);
115 return scale*exp(-log(1.0/u-1.0)/shape);
116 }
117
118 double rllogis_trunc(double shape, double scale, double left) {
119 double S0 = 1.0/(1.0+exp(log(left/scale)*shape));
120 double u = R::runif(0.0,1.0);
121 return scale*exp(log(1.0/(u*S0)-1.0)/shape);
122 }
123
124 }
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:180
ssim::user_unif_rand
double * user_unif_rand()
Definition: microsimulation.cc:81
R
Definition: microsimulation.cc:106
ssim::Rng
Definition: microsimulation.h:393
ssim::RngStream::GetState
void GetState(double seed[6]) const
Definition: RngStream.cpp:496
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:37
ssim::simTime
Time simTime()
simTime() function for OMNET++ API compatibility
Definition: microsimulation.cc:18
ssim::RngStream
Definition: RngStream.h:30
ssim::now
Time now()
now() function for compatibility with C++SIM
Definition: microsimulation.cc:14
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:107
ssim::rn
static double rn
Definition: microsimulation.cc:24
ssim::RngStream::RandU01
double RandU01()
Definition: RngStream.cpp:566
ssim::default_stream
static Rng * default_stream
Definition: microsimulation.cc:23
ssim::current_stream
static Rng * current_stream
Definition: microsimulation.cc:23
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:43
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:48
ssim::Sim::clock
static Time clock()
returns the current virtual time for the current process
Definition: ssim.cc:266
microsimulation.h
ssim::Time
double Time
virtual time type
Definition: ssim.h:79
ssim::Rng::id
int id
Definition: microsimulation.h:406
ssim::RngStream::SetSeed
bool SetSeed(const double seed[6])
Definition: RngStream.cpp:402
ssim::RngStream::AdvanceSubstream
void AdvanceSubstream(int32_t e, int32_t c)
Definition: RngStream.cpp:454
ssim::Rng::set
void set()
Definition: microsimulation.cc:31
ssim::rweibullHR
double rweibullHR(double shape, double scale, double hr)
Random Weibull distribution for a given shape, scale and hazard ratio.
Definition: microsimulation.cc:10
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:69
ssim::RngStream::ResetNextSubstream
void ResetNextSubstream()
Definition: RngStream.cpp:381
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:113
ssim::Rng::~Rng
virtual ~Rng()
Definition: microsimulation.cc:26
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:65
ssim::test_rstream2
void test_rstream2(double *x)
Simple test of the random streams (with a stupid name)
Definition: microsimulation.cc:91
ssim
name space for the Siena simulator.
Definition: microsimulation.cc:8
ssim::RngStream::SetPackageSeed
static bool SetPackageSeed(const double seed[6])
Definition: RngStream.cpp:391
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:57
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:118