forked from tjhladish/dengue-abm
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPerson.cpp
More file actions
350 lines (289 loc) · 11.1 KB
/
Person.cpp
File metadata and controls
350 lines (289 loc) · 11.1 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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
// Person.cpp
#include <cstdlib>
#include <cstring>
#include <climits>
#include <iostream>
#include <string>
#include <math.h>
#include <assert.h>
#include <bitset>
#include <gsl/gsl_rng.h>
#include <gsl/gsl_randist.h>
#include "Person.h"
#include "Community.h"
#include "Parameters.h"
using namespace dengue::standard;
int Person::_nNextID = 1;
const Parameters* Person::_par;
Person::Person() {
_nID = _nNextID++;
_nAge = -1;
_nLifespan = -1;
_nHomeID = -1;
_nWorkID = -1;
for(int i=0; i<STEPS_PER_DAY; i++) _pLocation[i] = NULL;
_bDead = false;
_bVaccinated = false;
_bNaiveVaccineProtection = false;
_bCase = false;
}
Person::~Person() {
clearInfectionHistory();
}
void Person::clearInfectionHistory() {
for (unsigned int i = 0; i < infectionHistory.size(); i++) {
delete infectionHistory[i];
}
infectionHistory.clear();
}
Infection& Person::initializeNewInfection(Serotype serotype) {
setImmunity(serotype);
Infection* infection = new Infection(serotype);
infectionHistory.push_back(infection);
return *infection;
}
// copyImmunity - copy immune status from person* p
void Person::copyImmunity(const Person* p) {
assert(p!=NULL);
_nImmunity = p->_nImmunity;
_bVaccinated = p->_bVaccinated;
vaccineHistory.clear();
vaccineHistory.assign(p->vaccineHistory.begin(), p->vaccineHistory.end());
clearInfectionHistory();
for (int i=0; i < p->getNumInfections(); i++) {
infectionHistory.push_back( new Infection(p->infectionHistory[i]) );
}
}
// resetImmunity - reset immune status (infants)
void Person::resetImmunity() {
_nImmunity.reset();
clearInfectionHistory();
_bVaccinated = false;
vaccineHistory.clear();
_bNaiveVaccineProtection = false;
_bCase = false;
_bDead = false;
}
bool Person::naturalDeath(int t) {
if (_nLifespan<=_nAge+(t/365.0)) {
_bDead = true;
return true;
}
return false;
}
void Person::kill(int time) {
_bDead = true;
}
bool Person::isInfectable(Serotype serotype, int time) const {
bool infectable = true;
const int maxinfectionparity = _par->nMaxInfectionParity;
if (!isSusceptible(serotype)) {
// immune to this serotype via previous infection OR non-leaky vaccine
infectable = false;
} else if (getNumInfections() > 0 and infectionHistory.back()->infectedTime + _par->nDaysImmune > time) {
// cross-serotype protection from last infection
infectable = false;
} else if (getInfectionParity() >= maxinfectionparity) {
// already infected max number of times
infectable = false;
} else if (isVaccinated() && _par->bVaccineLeaky==true) {
// potentially protected by leaky vaccine
// (all-or-none vaccines are handled via isSusceptible conditional)
double r = gsl_rng_uniform(RNG);
double protectionProbability = vaccineProtection(serotype, time);
// Which level of protection does this person have?
if ( r < protectionProbability ) {
infectable = false;
}
} else {
// Apparently person is infectable
}
return infectable;
}
double Person::remainingEfficacy(const int time) const {
double remainingFraction = 1.0;
if (_par->linearlyWaningVaccine) {
// reduce by fraction of immunity duration that has waned
int time_since_vac = daysSinceVaccination(time);
remainingFraction -= ((double) time_since_vac / _par->vaccineImmunityDuration);
}
return remainingFraction;
}
double Person::vaccineProtection(const Serotype serotype, const int time) const {
double ves;
if (not isVaccinated()) {
ves = 0.0;
} else {
int time_since_vac = daysSinceVaccination(time);
if (time_since_vac > _par->vaccineImmunityDuration) {
ves = 0.0;
} else {
if (_bNaiveVaccineProtection == true) {
ves = _par->fVESs_NAIVE[serotype];
} else {
ves = _par->fVESs[serotype];
}
ves *= remainingEfficacy(time);
}
}
return ves;
}
// infect - infect this individual
// primary symptomatic is a scaling factor for pathogenicity of primary infections.
// secondary_scaling * primarysymptomatic is the scaling factor for pathogenicity of secondary infections.
// returns true if infection occurs
bool Person::infect(int sourceid, Serotype serotype, int time, int sourceloc) {
// Bail now if this person can not become infected
// TODO - clarify this. why would a person not be infectable in this scope?
if (not isInfectable(serotype, time)) {
return false;
}
Infection& infection = initializeNewInfection(serotype); // Create a new infection record
infection.infectedByID = sourceid; // TODO - What kind of ID is this?
infection.infectedTime = time;
infection.infectedPlace = sourceloc;
// When do they become infectious?
infection.infectiousTime = 0;
double r = gsl_rng_uniform(RNG);
while (infection.infectiousTime < MAX_INCUBATION && INCUBATION_DISTRIBUTION[infection.infectiousTime] < r) {
infection.infectiousTime++;
}
infection.infectiousTime += time;
if (_nImmunity.any()) {
infection.recoveryTime = infection.infectiousTime+INFECTIOUS_PERIOD_SEC; // TODO - draw from distribution
} else {
infection.recoveryTime = infection.infectiousTime+INFECTIOUS_PERIOD_PRI; // TODO - draw from distribution
}
// Determine if this person withdraws (stops going to work/school)
const double primary_symptomatic = _par->fPrimaryPathogenicity[(int) serotype];
const double secondary_symptomatic = _nImmunity.any() ? _par->fSecondaryScaling[(int) serotype] : 1.0;
const double effective_VEP = isVaccinated() ? _par->fVEP : 0.0; // reduced symptoms due to vaccine
const double symptomatic_probability = primary_symptomatic * SYMPTOMATIC_BY_AGE[_nAge] * (1.0 - effective_VEP) * secondary_symptomatic;
if (gsl_rng_uniform(RNG) < symptomatic_probability) {
// Person develops symptoms == this is a case
// Is this a severe case (for estimating hospitalizations)
if (gsl_rng_uniform(RNG) < _par->hospitalizedFraction) { // potentially a severe case . . .
if (not isVaccinated() or gsl_rng_uniform(RNG) > _par->fVEH*remainingEfficacy(time)) { // is this person unvaccinated or unlucky?
infection.severeDisease = true;
}
}
// TODO - the 1 in the below line is a parameter and should be moved out
infection.symptomTime = infection.infectiousTime + 1; // symptomatic one day after infectious
double r = gsl_rng_uniform(RNG);
const int symptomatic_duration = infection.recoveryTime - infection.symptomTime;
for (int i=0; i<symptomatic_duration; i++) {
if (r < 1-pow(0.5,1+i) ) { // TODO - refactor to sample from geometric
infection.withdrawnTime = infection.symptomTime + i; // withdraws
break;
}
}
_bCase = true;
}
// TODO - clarify what's going on with d; sometimes this is an old infection, sometimes a current one
for (int d=infection.infectiousTime; d<infection.recoveryTime; d++) {
if (d < 0) continue; // we don't need to worry about flagging locations for past infections
for (int t=0; t<STEPS_PER_DAY; t++) {
Community::flagInfectedLocation(_pLocation[t], d);
}
}
if (_par->bRetroactiveMatureVaccine) {
// if the best vaccine-induced immunity can be acquired retroactively,
// upgrade this person from naive to mature
_bNaiveVaccineProtection = false;
}
return true;
}
bool Person::isNewlyInfected(int time) const {
if (infectionHistory.size() > 0) {
Infection* infection = infectionHistory.back();
if (time == infection->infectedTime) {
return true;
}
}
return false;
}
bool Person::isInfected(int time) const {
if (infectionHistory.size() > 0) {
Infection* infection = infectionHistory.back();
if (time >= infection->infectedTime and time < infection->recoveryTime) {
return true;
}
}
return false;
}
bool Person::isViremic(int time) const {
if (infectionHistory.size() > 0) {
Infection* infection = infectionHistory.back();
if (time >= infection->infectiousTime and time < infection->recoveryTime and not _bDead) {
return true;
}
}
return false;
}
bool Person::isSymptomatic(int time) const {
if (infectionHistory.size() > 0) {
Infection* infection = infectionHistory.back();
if (time >= infection->symptomTime and time < infection->recoveryTime and not _bDead) {
return true;
}
}
return false;
}
bool Person::hasSevereDisease(int time) const {
if (infectionHistory.size() > 0) {
Infection* infection = infectionHistory.back();
if (infection->severeDisease and time >= infection->symptomTime and time < infection->recoveryTime and not _bDead) {
return true;
}
}
return false;
}
bool Person::isWithdrawn(int time) const {
if (infectionHistory.size() > 0) {
Infection* infection = infectionHistory.back();
if (time >= infection->withdrawnTime and time < infection->recoveryTime and not _bDead) {
return true;
}
}
return false;
}
bool Person::isSusceptible(Serotype serotype) const {
return !_bDead && !(_nImmunity[serotype] == 1); //1<<0=1 1<<1=2 1<<2=4 1<<3=8 1<<4=16
}
// getInfectionParity - number of serotypes with immunity (including vaccination)
int Person::getInfectionParity() const {
return _nImmunity.count(); // count number of 1's in bit string
}
bool Person::fullySusceptible() const {
bool susceptible = true;
for (int s = 0; s<(int) NUM_OF_SEROTYPES; ++s) {
if ( not isSusceptible((Serotype) s) ) { susceptible = false; }
}
return susceptible;
}
bool Person::vaccinate(int time) {
if (!_bDead) {
//vector<double> _fVES = _par->fVESs;
_bVaccinated = true;
vaccineHistory.push_back(time);
if ( fullySusceptible() ) {
_bNaiveVaccineProtection = true;
} else {
_bNaiveVaccineProtection = false;
}
if ( _par->bVaccineLeaky == false ) { // all-or-none VE_S protection
if ( fullySusceptible() ) { // naive against all serotypes
for (int i=0; i<NUM_OF_SEROTYPES; i++) {
if (gsl_rng_uniform(RNG)<_par->fVESs_NAIVE[i]) _nImmunity[i] = 1; // protect against serotype i
}
} else {
for (int i=0; i<NUM_OF_SEROTYPES; i++) {
if (gsl_rng_uniform(RNG)<_par->fVESs[i]) _nImmunity[i] = 1; // protect against serotype i
}
}
}
return true;
} else {
return false;
}
}