/
SimPars.cpp
executable file
·374 lines (336 loc) · 8.89 KB
/
SimPars.cpp
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
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
/*
*
* SimPars.cpp
* Pneumo-ABM - S. Cobey
*
*/
#include <cstdlib>
using namespace std;
#include "SimPars.h"
#include "Parameters.h"
#define HFLU_INDEX (INIT_NUM_STYPES-1)
SimPars::SimPars( int tID, int sID ) {
treatmentID = tID;
simID = sID;
initializeDemInput();
initializeEpidInput();
}
SimPars::~SimPars() {
}
// PUBLIC MEMBER FUNCTIONS
const age_index& SimPars::get_demPMF_row( int row ) const {
return demPMFs[ row ];
}
const double SimPars::get_serotypePar_ij( int i, int j ) const {
return serotypePars[i][j];
}
const double SimPars::get_XI_ij( int i, int j) const {
return XI[i][j];
}
const double SimPars::get_waifw_ij( int i, int j ) const {
return normWAIFW[i][j];
}
const kids_index& SimPars::get_parity( void ) const {
return parity_pmf;
}
const double SimPars::get_neighbor( int i, int j ) const {
return neighborhoods[i][j];
}
const double SimPars::get_normalized_neighbor( int i, int j ) const {
return NNN[i][j];
}
const double SimPars::get_Hflu_prob( int s ) const {
return Hflu_probs[s];
}
const double SimPars::get_reductions( int s ) const {
return reductions[s];
}
// PRIVATE MEMBER FUNCTIONS
void SimPars::initializeDemInput() {
// First initialize all matrices to zero
for ( int i = 0; i < NUM_SOCIODEM_FILES; i++ ) {
for ( int j = 0; j < INIT_NUM_AGE_CATS; j++ ) {
demPMFs[i][j] = 0.0;
}
}
for ( int i = 0; i < INIT_NUM_AGE_CATS; i++ ) {
for ( int j = 0; j < INIT_NUM_AGE_CATS; j++ ) {
WAIFW[i][j] = 0.0;
normWAIFW[i][j] = 0.0;
}
}
for ( int i = 0; i < PARITY_BUFFER; i++ ) {
parity_pmf[i] = 0.0;
}
for ( int n1 = 0; n1 < NUM_NEIGHBORHOODS; n1++ ) {
for ( int n2 = 0; n2 < NUM_NEIGHBORHOODS; n2++ ) {
neighborhoods[n1][n2] = 0;
NNN[n1][n2] = 0;
}
}
// Sociodemographic data
for ( int i = 0; i < NUM_SOCIODEM_FILES; i++ ) {
std::ifstream thisFile;
thisFile.open( SOCIODEM_FILENAMES[ i ], ios::in );
if ( !thisFile ) {
cerr << "Error reading " << SOCIODEM_FILENAMES[ i ] << " file." << endl;
exit(1);
}
double currentMax = 0.0;
int maxIndex = 0;
double cumulativeSum = 0.0;
double thisVal;
int ii = 0;
while ( !thisFile.eof() ) {
thisFile >> thisVal;
if ( ii < INIT_NUM_AGE_CATS ) {
demPMFs[ i ][ ii ] = thisVal;
cumulativeSum += thisVal;
if ( thisVal > currentMax ) {
currentMax = thisVal;
maxIndex = ii;
}
ii++;
}
}
if ( cumulativeSum != 1.0 ) {
cout << "Cumulative sum of " << SOCIODEM_FILENAMES[ i ] << " is " << cumulativeSum << ", not 1. ";
double diff = 1.0-cumulativeSum;
if ( abs(diff) < ERR_EPSILON ) {
if ( diff > 0 ) {
cout << "Adding difference (" << diff << ") to highest rate." << endl;
demPMFs[ i ][ maxIndex ] += diff;
} else {
cout << "Adding difference (" << diff << ") to highest rate." << endl;
demPMFs[ i ][ maxIndex ] += diff;
}
} else {
cout << "Difference (" << diff << ") is too great (max acceptable is " << ERR_EPSILON << "). Fix the input file." << endl;
exit(1);
}
}
}
// WAIFW
std::ifstream thisFile3;
thisFile3.open( "WAIFW.txt", ios::in );
if ( !thisFile3 ) {
cerr << "Error reading " << WAIFW_FILENAME << "." << endl;
exit(1);
}
double thisVal;
int ii = 0;
int i = 0;
int j = 0;
while ( !thisFile3.eof() ) {
thisFile3 >> thisVal;
if ( ii < INIT_NUM_AGE_CATS * INIT_NUM_AGE_CATS ) {
if ( thisVal == 0 ) {
thisVal = WAIFW_NONZERO;
}
WAIFW[i][j] = thisVal;
j++;
if ( j == INIT_NUM_AGE_CATS ) {
j = 0;
i++;
}
ii++;
}
}
// Normalize WAIFW
for ( int j = 0; j < INIT_NUM_AGE_CATS; j++ ) { // for each column (FROM)
double colSum = 0.0;
for ( int i = 0; i < INIT_NUM_AGE_CATS; i++ ) {
colSum += WAIFW[i][j];
}
for ( int i = 0; i < INIT_NUM_AGE_CATS; i++ ) {
normWAIFW[ i ][ j ] = WAIFW[i][j]/colSum;
}
}
// PARITY_PMF
std::ifstream thisFile2;
thisFile2.open( PARITY_FILENAME, ios::in );
if ( !thisFile2 ) {
cerr << "Error reading " << PARITY_FILENAME << "." << endl;
exit(1);
}
thisVal = 0;
i = 0;
double currentMax = 0.0;
int maxIndex = 0;
double cumulativeSum = 0.0;
while ( !thisFile2.eof() ) {
thisFile2 >> thisVal;
if ( i < PARITY_BUFFER ) {
parity_pmf[ i ] = thisVal;
cumulativeSum += thisVal;
if ( thisVal > currentMax ) {
currentMax = thisVal;
maxIndex = i;
}
i++;
}
}
double diff = 1.0-cumulativeSum;
if ( abs(diff) >= 0 && abs(diff) < ERR_EPSILON ) {
cout << "Cumulative sum of " << PARITY_FILENAME << " is " << cumulativeSum << ", not 1." << endl;
if ( diff < 0 ) {
cout << "Adding difference (" << diff << ") to highest rate." << endl;
parity_pmf[ maxIndex ] += diff;
} else {
cout << "Adding difference (" << diff << ") to highest rate." << endl;
parity_pmf[ maxIndex ] += diff;
}
} else {
cerr << "Difference (" << diff << ") exceeds acceptable threshold (" << ERR_EPSILON << ")." << endl;
exit(1);
}
// NEIGHBORHOODS
std::ifstream thisFile4;
thisFile4.open( NEIGHBORHOOD_FILENAME, ios::in );
if ( !thisFile4 ) {
cerr << "Error reading " << NEIGHBORHOOD_FILENAME << "." << endl;
exit(1);
}
double thisVal2;
int ii2 = 0;
int i2 = 0;
int j2 = 0;
while ( !thisFile4.eof() && ii2 < NUM_NEIGHBORHOODS * NUM_NEIGHBORHOODS ) {
thisFile4 >> thisVal2;
if ( ii2 < NUM_NEIGHBORHOODS * NUM_NEIGHBORHOODS ) {
neighborhoods[i2][j2] = thisVal2;
j2++;
if ( j2 == NUM_NEIGHBORHOODS ) {
j2 = 0;
i2++;
}
ii2++;
}
}
// Normalize neighborhoods
for ( int j = 0; j < NUM_NEIGHBORHOODS; j++ ) { // for each column (FROM)
double colSum = 0.0;
for ( int i = 0; i < NUM_NEIGHBORHOODS; i++ ) {
colSum += neighborhoods[i][j];
}
for ( int i = 0; i < NUM_NEIGHBORHOODS; i++ ) {
NNN[ i ][ j ] = neighborhoods[i][j]/colSum;
}
}
}
void SimPars::initializeEpidInput() {
// Epidemiological data
// Read in files
for ( int f = 0; f < NUM_EPID_FILES; f++ ) {
std::ifstream thisFile;
thisFile.open( EPID_FILENAMES[ f ],ios::in);
if ( !thisFile ) {
cerr << "Error reading " << EPID_FILENAMES[ f ] << " file." << endl;
exit(1);
}
double thisVal;
int ii = 0;
while ( !thisFile.eof() ) {
thisFile >> thisVal;
if ( ii < INIT_NUM_STYPES ) {
serotypePars[ f ][ ii ] = thisVal;
ii++;
}
}
}
// ADD BETA
std::ifstream betaStream;
string betaFilename = makeName( treatmentID, simID, "BETA");
betaStream.open( betaFilename.c_str(),ios::in);
if ( !betaStream ) {
cerr << "Error reading " << betaFilename << "." << endl;
exit(1);
}
double betaVal = 0.0;
int ib = 0;
while ( !betaStream.eof() ) {
betaStream >> betaVal;
if ( ib < INIT_NUM_STYPES ) {
serotypePars[ NUM_EPID_FILES ][ ib ] = betaVal;
ib++;
}
}
cout << "Reading out contents of serotypePars: " << endl;
for ( int i = 0; i < NUM_EPID_FILES+1; i++ ) {
if ( i < NUM_EPID_FILES ) {
cout << EPID_FILENAMES[ i ] << "\t" ;
} else {
cout << "BETA " << "\t";
}
for ( int j = 0; j < INIT_NUM_STYPES; j++ ) {
cout << serotypePars[i][j] << "\t" ;
}
cout << endl;
}
cout << endl;
// XI
std::ifstream thisFile;
string XIFile = makeName( treatmentID, simID, "XI" );
thisFile.open( XIFile.c_str(),ios::in );
if ( !thisFile ) {
cerr << "Error reading " << XIFile << "." << endl;
exit(1);
}
double thisVal;
int ii = 0;
int i = 0;
int j = 0;
while ( !thisFile.eof() ) {
thisFile >> thisVal;
if ( ii < INIT_NUM_STYPES * INIT_NUM_STYPES ) {
XI[i][j] = thisVal;
j++;
if ( j == INIT_NUM_STYPES ) {
j = 0;
i++;
}
ii++;
}
}
// HFLU_PROBS
std::ifstream hfStream;
hfStream.open( HFPROB_FILENAME, ios::in );
if ( !hfStream ) {
cerr << "Error reading " << HFPROB_FILENAME << "." << endl;
exit(1);
}
i = 0;
while ( !hfStream.eof() && ( i < INIT_NUM_STYPES - 1 ) ) {
hfStream >> thisVal;
Hflu_probs[ i ] = thisVal;
i++;
}
// Set up competition terms
double maxDuration = serotypePars[ MEAN_DURATION_INDEX ][ 0 ];
double minDuration = BASE_DURATION;
double rangeDuration = maxDuration - minDuration;
double thisDuration;
for ( int s = 0; s < INIT_NUM_STYPES; s++ ) {
if ( MAX_REDUCTION > 0 ) {
if ( s < HFLU_INDEX ) {
thisDuration = serotypePars[ MEAN_DURATION_INDEX ][ s ];
reductions[ s ] = MAX_REDUCTION*( thisDuration - minDuration )/rangeDuration;
} else {
reductions[ s ] = 0.0;
}
} else {
reductions[ s ] = 0.0;
}
}
}
string SimPars::d2str( double d ) {
std::stringstream t;
t << d;
return t.str();
}
string SimPars::makeName( int treatmentIdx, int simIdx, string suffix ) {
string thisCtr = d2str( simIdx );
string thisTr = d2str( treatmentIdx );
string thisName = "tr_" + thisTr + "_sim_" + thisCtr + "_" + suffix;
return thisName;
}