-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTransmission_Function.cpp
More file actions
179 lines (140 loc) · 4.46 KB
/
Transmission_Function.cpp
File metadata and controls
179 lines (140 loc) · 4.46 KB
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
/*
* This script contains the Rcpp code for the nested "for" loop that controls
* interactions between agents.
*/
#include <RcppArmadillo.h>
// [[Rcpp::depends(RcppArmadillo)]]
// [[Rcpp::interfaces(r, cpp)]]
// Create a constant integer value for integer arithmetic
// int CONSTANT = 1000;
// Here is the function that propagates infection based on agent interactions.
// [[Rcpp::export]]
Rcpp::List propogate(Rcpp::List agents, Rcpp::IntegerMatrix contact_matrix, Rcpp::NumericVector initial_set)
{
// Initialize necessary variables
int len = 0, num_hcws = 0, num_pats = 0, val = 0, send_disease_stat = 0;
int receive_disease_stat = 0, num_contacts = 0, new_infects = 0;
int hcw_to_pat = 0, pat_to_hcw = 0, secondary_hcw_infected = 0, id = 0;
int secondary_patient_infected = 0, Rnot = 0, val1 = 0, val2 = 0, pat_to_pat = 0, hcw_to_hcw = 0;
double infect = 0.0, sus = 0.0, random_double_one = 0.0, random_double_two = 0.0;
// Create lists to hold agents
Rcpp::List ids, infects, suscs, types, disease_stats;
// Rcpp::Rcout << "Rcpp is running." << std::endl;
// Extract lists of different agent characteristics
ids = agents("id");
types = agents("type");
infects = agents("infect");
suscs = agents("sus");
disease_stats = agents("disease_stat");
len = ids.length();
// Rcpp::Rcout << "Number of agents is " << len << std::endl;
// For loop sees how many agents of each type there are
for (int i = 0; i < len; i++)
{
val = types[i];
if (val == 1)
{
num_pats++;
}
if (val == 2)
{
num_hcws++;
}
}
// Rcpp::Rcout << "Number of HCWs is " << num_hcws << std::endl;
// Rcpp::Rcout << "Number of patients is " << num_pats << std::endl;
// For loop goes through all the infecting agents
for (int i = 0; i < len; i++)
{
// Find infectivity of sending agent
infect = infects[i];
// Find disease status of sending agent
send_disease_stat = disease_stats[i];
if (infect == 0 || send_disease_stat != 1)
{
continue;
}
// For loop goes through all the susceptible agents
for (int j = 0; j < len; j++)
{
// Find susceptibility, disease status, and number of contacts of receiving agent
sus = suscs[j];
receive_disease_stat = disease_stats[j];
num_contacts = contact_matrix(i, j);
if (sus == 0 || receive_disease_stat != 0 || num_contacts == 0)
{
continue;
}
else
{
// Check if contacts are less than one for timestemp
if (num_contacts < 1)
{
if (R::runif(0, 1) < num_contacts)
{
num_contacts = 1;
}
else
{
continue;
}
}
// For loop draws a random number for each contact to see whether susceptible agent gets infected.
for (int k = 0; k < num_contacts; k++)
{
random_double_one = R::runif(0, 1);
random_double_two = R::runif(0, 1);
if (random_double_one <= infect && random_double_two <= sus)
{
// If infected, disease status becomes 2
disease_stats[j] = 2;
new_infects++;
// Update R0 if necessary
id = ids[i];
if (std::count(initial_set.begin(), initial_set.end(), id))
{
Rnot++;
}
// Check who is infecting whom.
val1 = types[i];
val2 = types[j];
if (val2 == 1)
{
secondary_patient_infected++;
}
else if (val2 == 2)
{
secondary_hcw_infected++;
}
if (val1 == 1 && val2 == 1)
{
pat_to_pat++;
}
else if(val1 == 2 && val2 == 1)
{
hcw_to_pat++;
}
else if (val2 == 2 && val1 == 2)
{
hcw_to_hcw++;
}
else if (val1 == 1 && val2 == 2)
{
pat_to_hcw++;
}
break;
}
}
}
}
}
disease_stats.push_back(new_infects);
disease_stats.push_back(hcw_to_pat);
disease_stats.push_back(pat_to_hcw);
disease_stats.push_back(pat_to_pat);
disease_stats.push_back(hcw_to_hcw);
disease_stats.push_back(secondary_patient_infected);
disease_stats.push_back(secondary_hcw_infected);
disease_stats.push_back(Rnot);
return disease_stats;
}