-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlandscapeScaleSimulation.c
1676 lines (1595 loc) · 49.2 KB
/
landscapeScaleSimulation.c
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
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <time.h>
/*
Random number library function
http://www.math.sci.hiroshima-u.ac.jp/~m-mat/MT/MT2002/emt19937ar.html
*/
#include "mt19937ar.h"
/*
Stop Visual C++ from warning about thread safety when asked to compile idiomatic ANSI
*/
#ifdef _MSC_VER
#pragma warning(disable : 4996)
#endif
/*
Constants used throughout program
*/
#define _EMPTY_CELL -1
#define _UNDEF_TIME -1.0
#define _MAX_STATIC_BUFF_LEN 1024
#define _MAX_DYNAMIC_BUFF_LEN (1024*1024)
#define _GIS_HEADER_LENGTH 6
#define _GIS_WHITESPACE ",\t "
#define _GIS_NEWLINES "\r\n"
#define _GIS_NODATA "-9999"
#define _LANDSCAPE_BLOCK_SIZE 128
#define _PI 3.1415926535897932384626433
#define _PRI_INF_TYPE 1
#define _SEC_INF_TYPE 2
#define _SETUP_DISPERSAL_PRINT_DOT 10000
#define _VERY_LONG_TIME 10000000.0
#ifdef _MSC_VER
#define C_DIR_DELIMITER '\\'
#include <direct.h>
#include <process.h>
#else
#define C_DIR_DELIMITER '/'
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#endif
/*
Information on a single cell
*/
typedef struct
{
int xPos; /* column in gis raster */
int yPos; /* row in gis raster */
double propFull; /* proportion of cell with host */
double relInf; /* relative infectivity */
double relSus; /* relative susceptibility */
double relPri; /* relative force of primary infection */
double tInf; /* time of first infection of this cell */
double tNext; /* time of next possible secondary infection caused by this cell */
int infType; /* whether this cell became infected via primary or secondary infection */
int infBy; /* which host infected (=_EMPTY_CELL for primary) */
} t_Cell;
/*
Cells are collated in a landscape
*/
typedef struct
{
int numRows;
int numCols;
t_Cell *aCells;
int numCells;
int numCellsSpace;
int *aCellLookup;
double totalFull; /* this stores the total number of cells that are full, accounting for fractions */
} t_Landscape;
/*
Store the rate of primary infection on each cell
Keep track of the cumulative pressure to make it easy to find which cell is going to be (primary) infected next
*/
typedef struct
{
double *aCumPressure;
double totalPressure;
double ratePri;
double nextT;
} t_PriInf;
/*
Progress of a single epidemic is stored by keeping track of which cells have become infected
And a priority queue for the next infection each generates
*/
typedef struct
{
int *aQueueCells;
int queueLen;
int *aInfCells;
int totalInf;
} t_Epidemic;
/*
Parameters which control behaviour of simulation
*/
typedef struct
{
double cellThresh; /* minimum proportion covered for cell to be included */
char filePropFull[_MAX_STATIC_BUFF_LEN];
char fileRelInf[_MAX_STATIC_BUFF_LEN];
char fileRelSus[_MAX_STATIC_BUFF_LEN];
char fileRelPri[_MAX_STATIC_BUFF_LEN];
int numIts; /* number of iterations of the simulation to run */
char outStub[_MAX_STATIC_BUFF_LEN];
double ratePriInf; /* this is the max rate at which expect primary infections over entire landscape */
double rateSecInf; /* this is the secondary infection rate */
double dispScale; /* average dispersal scale (measured in cells) */
double reportTime; /* how frequently to report to the screen */
double maxTime; /* maximum time to run epidemics up to (only used if don't set incidence) */
double maxIncidence; /* this gives an alternate stopping condition */
double withinCellBulkUp; /* logistic rate of increase of within-cell infection */
double withinCellMin; /* minimum fraction of cell that can be infected (note if trueMinFlag=0 this is relative to carrying capacity of cell) */
int trueMinFlag; /* whether to make withinCellMin relative to amount of hosts in cell (trueMinFlag=0) or a raw proportion (trueMinFlag=1) */
} t_Params;
/*
This structure stores the probabilities of dispersing to cells in a single quadrant, from (0,0) -> (nCols, nRows) in a flattened list
note that when doing dispersal, program decides whether to multiply both x and y by -1
*/
typedef struct
{
int numProbs; /* number of probabilities that are stored */
double *aProbs; /* array of dispersal probabilities */
double inCell; /* probability of dispersing back to original cell (=aProbs[0]) */
double onLandscape; /* probability of dispersing on the landscape */
} t_Dispersal;
/*
Keep track of how many of each type of event were attempted
*/
typedef struct
{
long numSecondaryAttempts;
long numNonEmpty;
long numNonInfected;
long numSuccessful;
long numFindNextSecondary;
} t_RunStats;
/*
Utility functions for the priority queue
*/
int getLeftChildIndex(int nodeIndex)
{
return 2 * nodeIndex + 1;
}
int getRightChildIndex(int nodeIndex)
{
return 2 * nodeIndex + 2;
}
int getParentIndex(int nodeIndex)
{
return (nodeIndex - 1) / 2;
}
void siftUp(int nodeIndex, t_Epidemic *pEpidemic, t_Landscape *pLandscape)
{
int parentIndex, tmp;
if (nodeIndex != 0)
{
parentIndex = getParentIndex(nodeIndex);
if (pLandscape->aCells[pEpidemic->aQueueCells[parentIndex]].tNext > pLandscape->aCells[pEpidemic->aQueueCells[nodeIndex]].tNext)
{
tmp = pEpidemic->aQueueCells[parentIndex];
pEpidemic->aQueueCells[parentIndex] = pEpidemic->aQueueCells[nodeIndex];
pEpidemic->aQueueCells[nodeIndex] = tmp;
siftUp(parentIndex, pEpidemic, pLandscape);
}
}
}
void siftDown(int nodeIndex, t_Epidemic *pEpidemic, t_Landscape *pLandscape)
{
int leftChildIndex, rightChildIndex, minIndex, tmp;
leftChildIndex = getLeftChildIndex(nodeIndex);
rightChildIndex = getRightChildIndex(nodeIndex);
if (rightChildIndex >= pEpidemic->queueLen)
{
if (leftChildIndex >= pEpidemic->queueLen)
{
return;
}
else
{
minIndex = leftChildIndex;
}
}
else
{
if (pLandscape->aCells[pEpidemic->aQueueCells[leftChildIndex]].tNext <= pLandscape->aCells[pEpidemic->aQueueCells[rightChildIndex]].tNext)
{
minIndex = leftChildIndex;
}
else
{
minIndex = rightChildIndex;
}
}
if (pLandscape->aCells[pEpidemic->aQueueCells[nodeIndex]].tNext > pLandscape->aCells[pEpidemic->aQueueCells[minIndex]].tNext)
{
tmp = pEpidemic->aQueueCells[minIndex];
pEpidemic->aQueueCells[minIndex] = pEpidemic->aQueueCells[nodeIndex];
pEpidemic->aQueueCells[nodeIndex] = tmp;
siftDown(minIndex, pEpidemic, pLandscape);
}
}
double removeMinElement(t_Epidemic *pEpidemic, t_Landscape *pLandscape)
{
double min;
min = -1;
if (pEpidemic->queueLen == 0)
{
fprintf(stderr, "Heap is empty");
}
else
{
min = pLandscape->aCells[pEpidemic->aQueueCells[0]].tNext;
pEpidemic->aQueueCells[0] = pEpidemic->aQueueCells[pEpidemic->queueLen - 1];
pEpidemic->queueLen--;
if (pEpidemic->queueLen > 0)
{
siftDown(0, pEpidemic, pLandscape);
}
}
return min;
}
double removeArbitaryElement(int index, t_Epidemic *pEpidemic, t_Landscape *pLandscape)
{
double thisElement;
thisElement = -1.0;
if(index >= pEpidemic->queueLen)
{
fprintf(stderr, "Trying to remove element that doesn't exist");
}
else
{
thisElement = pLandscape->aCells[pEpidemic->aQueueCells[index]].tNext;
pEpidemic->aQueueCells[index] = pEpidemic->aQueueCells[pEpidemic->queueLen - 1];
pEpidemic->queueLen--;
if(pEpidemic->queueLen > 0)
{
siftDown(index, pEpidemic, pLandscape);
siftUp(index, pEpidemic, pLandscape);
}
}
return thisElement;
}
void insertElement(int cellIndex, t_Epidemic *pEpidemic, t_Landscape *pLandscape)
{
if (pEpidemic->queueLen == pLandscape->numCells)
{
fprintf(stderr, "Heap's storage has overflowed");
}
else
{
pEpidemic->queueLen++;
pEpidemic->aQueueCells[pEpidemic->queueLen - 1] = cellIndex;
siftUp(pEpidemic->queueLen - 1, pEpidemic, pLandscape);
}
}
void checkHeap(FILE *heapOut, t_Epidemic *pEpidemic, t_Landscape *pLandscape)
{
int i,c;
/*
In a heap, a node's value should be smaller than (or equal to) its children
*/
for(i=0;i<pEpidemic->queueLen;i++)
{
fprintf(heapOut, "%d -> %d -> %.5f\n", i, pEpidemic->aQueueCells[i], pLandscape->aCells[pEpidemic->aQueueCells[i]].tNext);
c = getRightChildIndex(i);
if(c >= pEpidemic->queueLen)
{
fprintf(heapOut, "\tright child: empty\n");
}
else
{
fprintf(heapOut, "\tright child: (%d %d %.5f) ", c, pEpidemic->aQueueCells[c], pLandscape->aCells[pEpidemic->aQueueCells[c]].tNext);
if(pLandscape->aCells[pEpidemic->aQueueCells[c]].tNext >= pLandscape->aCells[pEpidemic->aQueueCells[i]].tNext)
{
fprintf(heapOut, "ok\n");
}
else
{
fprintf(heapOut, "failed\n");
}
}
c = getLeftChildIndex(i);
if(c >= pEpidemic->queueLen)
{
fprintf(heapOut, "\tleft child: empty\n");
}
else
{
fprintf(heapOut, "\tleft child: (%d %d %.5f) ", c, pEpidemic->aQueueCells[c], pLandscape->aCells[pEpidemic->aQueueCells[c]].tNext);
if(pLandscape->aCells[pEpidemic->aQueueCells[c]].tNext >= pLandscape->aCells[pEpidemic->aQueueCells[i]].tNext)
{
fprintf(heapOut, "ok\n");
}
else
{
fprintf(heapOut, "failed\n");
}
}
}
}
/*
Utility functions for reading configuration options
*/
int getCfgFileName(char *szProgName, char *szCfgFile)
{
/*
Work out configuration file name from that of the executable
and check whether it exists by attempting to read it
*/
char *pPtr;
FILE *fp;
szCfgFile[0] = '\0';
{
if((pPtr = strrchr(szProgName,C_DIR_DELIMITER))!=NULL)
{
strcpy(szCfgFile,pPtr+1);
}
else
{
strcpy(szCfgFile,szProgName);
}
if((pPtr = strstr(szCfgFile,".exe"))!=NULL)
{
*pPtr = '\0';
}
strcat(szCfgFile,".cfg");
}
/* check file exists */
fp = fopen(szCfgFile, "rb");
if(fp)
{
fclose(fp);
return 1;
}
return 0;
}
/*
Following set of routines find values of parameters from the command line options,
or, failing that, from the cfg file
*/
int findKey(int argc, char **argv, char*szCfgFile, char *szKey, char *szValue)
{
char *pVal;
int bRet,i;
FILE *fp;
char *pThisPair;
char *szArgvCopy;
bRet = 0;
i=0;
/* try to find the relevant key on the command line */
while(bRet == 0 && i<argc)
{
szArgvCopy = strdup(argv[i]);
if(szArgvCopy)
{
pThisPair = strtok(szArgvCopy, " \t");
while(pThisPair)
{
if(strncmp(pThisPair,szKey,strlen(szKey))==0)
{
pVal = strchr(pThisPair,'=');
if(pVal)
{
/* make sure isn't just start of string matching the key */
if(pThisPair[strlen(szKey)] == '=')
{
strcpy(szValue,pVal+1);
fprintf(stdout, "extracted %s->%s from command line\n", szKey, szValue);
bRet = 1;
}
}
}
pThisPair = strtok(NULL, " \t");
}
free(szArgvCopy);
}
i++;
}
/* otherwise, look in the cfg file */
if(bRet == 0)
{
fp = fopen(szCfgFile,"rb");
if(fp)
{
char szLine[_MAX_STATIC_BUFF_LEN];
while(!bRet && fgets(szLine,_MAX_STATIC_BUFF_LEN,fp))
{
char *pPtr;
if((pPtr = strchr(szLine,'='))!=NULL)
{
*pPtr = '\0';
if(strcmp(szKey,szLine)==0)
{
strcpy(szValue,pPtr+1);
/* strip off newline (if any) */
if((pPtr = strpbrk(szValue,"\r\n"))!=NULL)
*pPtr = '\0';
bRet = 1;
}
}
}
fclose(fp);
}
}
return bRet;
}
int readStringFromCfg(int argc, char **argv, char *szCfgFile, char *szKey, char *szValue)
{
return(findKey(argc, argv, szCfgFile,szKey,szValue));
}
int readDoubleFromCfg(int argc, char **argv, char *szCfgFile, char *szKey, double *pdValue)
{
char szValue[_MAX_STATIC_BUFF_LEN];
if(findKey(argc, argv, szCfgFile,szKey,szValue))
{
*pdValue = atof(szValue);
return 1;
}
return 0;
}
int readIntFromCfg(int argc, char **argv, char *szCfgFile, char *szKey, int *pnValue)
{
char szValue[_MAX_STATIC_BUFF_LEN];
if(findKey(argc, argv, szCfgFile,szKey,szValue))
{
*pnValue = atoi(szValue);
return 1;
}
return 0;
}
/*
Internally the two dimensional landscape is stored in a flattened structure
These routines convert between the two
*/
int gridToPos(int x, int y, int numCols)
{
return x + y*numCols;
}
void posToGrid(int pos, int numCols, int *pX, int *pY)
{
*pY = pos/numCols;
*pX = pos%numCols;
}
/*
Return uniform random number between 0 and 1
Encapsulated to allow easy replacement if necessary
*/
double uniformRandom()
{
#if 0
int nRet;
nRet = RAND_MAX;
while(nRet == RAND_MAX || nRet == 0)
{
nRet = rand();
}
return ((double)nRet/(double)(RAND_MAX));
#else
return genrand_real3();
#endif
}
/*
Seed random number generator
Encapsulated to allow easy replacement if necessary
*/
void seedRandom()
{
unsigned long ulnSeed;
unsigned long myPID;
ulnSeed =(unsigned long) time(NULL);
/* Make sure that multiple different processes started at same time have different seeds */
#ifndef _MSC_VER
myPID = (unsigned long) getpid();
#else
myPID = (unsigned long) _getpid();
#endif
ulnSeed += myPID;
#if 0
srand((unsigned int)ulnSeed);
#else
init_genrand(ulnSeed);
#endif
}
/*
Read all parameters from the configuration file
(and dump to a new file so can tell which values program used)
*/
int readParams(t_Params *pParams, int argc, char **argv)
{
char szCfgFile[_MAX_STATIC_BUFF_LEN];
fprintf(stdout, "readParams()\n");
memset(pParams,0,sizeof(t_Params));
if(!getCfgFileName(argv[0], szCfgFile))
{
fprintf(stderr, "Couldn't find cfg file for program name '%s'\n", argv[0]);
return 0;
}
if(!readDoubleFromCfg(argc, argv, szCfgFile, "cellThresh", &pParams->cellThresh))
{
fprintf(stdout, "Couldn't read cellThresh\n");
return 0;
}
if(!readIntFromCfg(argc, argv, szCfgFile, "numIts", &pParams->numIts))
{
fprintf(stdout, "Couldn't read numIts\n");
return 0;
}
if(!readStringFromCfg(argc, argv, szCfgFile, "filePropFull", pParams->filePropFull))
{
fprintf(stdout, "Couldn't read filePropFull\n");
return 0;
}
if(!readStringFromCfg(argc, argv, szCfgFile, "fileRelInf", pParams->fileRelInf))
{
fprintf(stdout, "Couldn't read fileRelInf\n");
return 0;
}
if(!readStringFromCfg(argc, argv, szCfgFile, "fileRelSus", pParams->fileRelSus))
{
fprintf(stdout, "Couldn't read fileRelSus\n");
return 0;
}
if(!readStringFromCfg(argc, argv, szCfgFile, "fileRelPri", pParams->fileRelPri))
{
fprintf(stdout, "Couldn't read fileRelPri\n");
return 0;
}
if(!readStringFromCfg(argc, argv, szCfgFile, "outStub", pParams->outStub))
{
fprintf(stdout, "Couldn't read outStub\n");
return 0;
}
#ifdef _MSC_VER
if(!mkdir(pParams->outStub))
#else
if(!mkdir(pParams->outStub, 0777))
#endif
{
fprintf(stdout, "\tcreated directory %s for output\n",pParams->outStub);
}
else
{
fprintf(stdout, "\tdirectory %s already exists\n",pParams->outStub);
}
if(!readDoubleFromCfg(argc, argv, szCfgFile, "maxTime", &pParams->maxTime))
{
fprintf(stdout, "Couldn't read maxTime\n");
return 0;
}
if(!readDoubleFromCfg(argc, argv, szCfgFile, "ratePriInf", &pParams->ratePriInf))
{
fprintf(stdout, "Couldn't read ratePriInf\n");
return 0;
}
if(!readDoubleFromCfg(argc, argv, szCfgFile, "rateSecInf", &pParams->rateSecInf))
{
fprintf(stdout, "Couldn't read rateSecInf\n");
return 0;
}
if(!readDoubleFromCfg(argc, argv, szCfgFile, "dispScale", &pParams->dispScale))
{
fprintf(stdout, "Couldn't read dispScale\n");
return 0;
}
if(!readDoubleFromCfg(argc, argv, szCfgFile, "reportTime", &pParams->reportTime))
{
fprintf(stdout, "Couldn't read reportTime\n");
return 0;
}
if(!readDoubleFromCfg(argc, argv, szCfgFile, "maxIncidence", &pParams->maxIncidence))
{
fprintf(stdout, "Couldn't read maxIncidence\n");
return 0;
}
if(!readDoubleFromCfg(argc, argv, szCfgFile, "withinCellBulkUp", &pParams->withinCellBulkUp))
{
fprintf(stdout, "Couldn't read withinCellBulkUp\n");
return 0;
}
if(!readDoubleFromCfg(argc, argv, szCfgFile, "withinCellMin", &pParams->withinCellMin))
{
fprintf(stdout, "Couldn't read withinCellMin\n");
return 0;
}
if (!readIntFromCfg(argc, argv, szCfgFile, "trueMinFlag", &pParams->trueMinFlag))
{
fprintf(stdout, "Couldn't read trueMinFlag\n");
return 0;
}
/* Following code dumps out parameters as read in */
{
FILE *paramsOut;
char outFile[_MAX_STATIC_BUFF_LEN];
sprintf(outFile, "%s%cparamsOut.txt", pParams->outStub, C_DIR_DELIMITER);
fprintf(stdout, "\twriting copy of parameters to %s\n", outFile);
paramsOut = fopen(outFile, "wb");
if(paramsOut)
{
fprintf(paramsOut, "pParams->cellThresh=%.6f\n", pParams->cellThresh);
fprintf(paramsOut, "pParams->numIts=%d\n", pParams->numIts);
fprintf(paramsOut, "pParams->filePropFull=%s\n", pParams->filePropFull);
fprintf(paramsOut, "pParams->fileRelInf=%s\n", pParams->fileRelInf);
fprintf(paramsOut, "pParams->fileRelSus=%s\n", pParams->fileRelSus);
fprintf(paramsOut, "pParams->fileRelPri=%s\n", pParams->fileRelPri);
fprintf(paramsOut, "pParams->outStub=%s\n", pParams->outStub);
fprintf(paramsOut, "pParams->maxTime=%.6f\n", pParams->maxTime);
fprintf(paramsOut, "pParams->ratePriInf=%.6f\n", pParams->ratePriInf);
fprintf(paramsOut, "pParams->rateSecInf=%.6f\n", pParams->rateSecInf);
fprintf(paramsOut, "pParams->dispScale=%.6f\n", pParams->dispScale);
fprintf(paramsOut, "pParams->reportTime=%f\n", pParams->reportTime);
fprintf(paramsOut, "pParams->maxIncidence=%.6f\n", pParams->maxIncidence);
fprintf(paramsOut, "pParams->withinCellMin=%.6f\n", pParams->withinCellMin);
fprintf(paramsOut, "pParams->withinCellBulkUp=%.6f\n", pParams->withinCellBulkUp);
fprintf(paramsOut, "pParams->trueMinFlag=%d\n", pParams->trueMinFlag);
fclose(paramsOut);
}
else
{
fprintf(stderr, "couldn't dump parameters...exiting\n");
return 0;
}
}
return 1;
}
/*
Rather unwieldy parsing routine which reads in all data from GIS format
*/
int readLandscape(t_Landscape *pLandscape, char *filePropFull, char *fileRelInf, char *fileRelPri, char *filRelSus, double cellThresh, char *outStub)
{
FILE *fIn;
char *fileName,*inBuff,*pPtr;
int noData,retVal,thisX,thisY,i,headerCount;
double thisVal;
char outFile[_MAX_STATIC_BUFF_LEN];
t_Cell* pTmp;
fprintf(stdout, "readLandscape()\n");
retVal = 0;
memset(pLandscape,0,sizeof(t_Landscape));
inBuff = malloc(_MAX_DYNAMIC_BUFF_LEN*sizeof(char));
if(inBuff)
{
retVal = 1;
for(i=0;i<4 && retVal;i++)
{
retVal = 0;
fileName = NULL;
switch(i)
{
case 0:
fileName = filePropFull;
break;
case 1:
fileName = fileRelInf;
break;
case 2:
fileName = fileRelPri;
break;
case 3:
fileName = filRelSus;
break;
default:
fprintf(stderr, "readLandscape(): shouldn't get here...\n");
break;
}
fprintf(stdout, "\t%s...", fileName);
fIn = fopen(fileName,"rb");
if(fIn)
{
/* strip GIS header */
retVal = 1;
headerCount = 0;
while(retVal && headerCount < _GIS_HEADER_LENGTH && fgets(inBuff, _MAX_DYNAMIC_BUFF_LEN, fIn))
{
if(i == 0)
{
if(strncmp(inBuff, "ncols", strlen("ncols"))==0)
{
pPtr = strpbrk(inBuff,_GIS_WHITESPACE);
if(pPtr)
{
pLandscape->numCols = atoi(pPtr);
}
else
{
retVal = 0;
}
}
if(strncmp(inBuff, "nrows", strlen("nrows"))==0)
{
pPtr = strpbrk(inBuff,_GIS_WHITESPACE);
if(pPtr)
{
pLandscape->numRows = atoi(pPtr);
}
else
{
retVal = 0;
}
}
}
headerCount++;
}
if(retVal && pLandscape->numCols > 0 && pLandscape->numRows > 0)
{
if(i == 0)
{
pLandscape->aCellLookup = malloc(sizeof(int)*pLandscape->numCols*pLandscape->numRows);
if(!pLandscape->aCellLookup)
{
retVal = 0;
}
}
thisY = 0;
while(retVal && fgets(inBuff, _MAX_DYNAMIC_BUFF_LEN, fIn)) /* will fail on very long lines */
{
pPtr = strpbrk(inBuff, _GIS_NEWLINES);
if(pPtr)
{
pPtr[0] = '\0';
}
else
{
fprintf(stderr, "readLandscape(): line too long\n");
retVal=0; /* no CR or NL means line not read in fully */
}
thisX = 0;
pPtr = strtok(inBuff, _GIS_WHITESPACE);
while(retVal && pPtr)
{
noData = 0;
if(strcmp(pPtr,_GIS_NODATA)==0)
{
noData = 1;
}
else
{
thisVal = atof(pPtr);
}
if(i==0) /* in the first pass through need to add this cell to the landscape */
{
if(noData || thisVal < cellThresh)
{
pLandscape->aCellLookup[gridToPos(thisX,thisY,pLandscape->numCols)] = _EMPTY_CELL;
}
else
{
if(pLandscape->numCells == pLandscape->numCellsSpace)
{
pLandscape->numCellsSpace += _LANDSCAPE_BLOCK_SIZE;
/* have to rewrite perfectly idiomatic C to get around visual studio warnings re. memory leaks */
pTmp = realloc(pLandscape->aCells, sizeof(t_Cell) * pLandscape->numCellsSpace);
if (pTmp)
{
pLandscape->aCells = pTmp;
}
else
{
retVal = 0;
}
}
pLandscape->aCellLookup[gridToPos(thisX,thisY,pLandscape->numCols)] = pLandscape->numCells;
if (pLandscape->aCells)
{
pLandscape->aCells[pLandscape->numCells].xPos = thisX;
pLandscape->aCells[pLandscape->numCells].yPos = thisY;
pLandscape->aCells[pLandscape->numCells].propFull = thisVal;
pLandscape->aCells[pLandscape->numCells].tInf = _UNDEF_TIME;
pLandscape->aCells[pLandscape->numCells].tNext = _UNDEF_TIME;
pLandscape->aCells[pLandscape->numCells].infBy = _EMPTY_CELL;
pLandscape->aCells[pLandscape->numCells].infType = _EMPTY_CELL;
pLandscape->totalFull += pLandscape->aCells[pLandscape->numCells].propFull;
pLandscape->numCells++;
}
}
}
else
{
if(pLandscape->aCellLookup[gridToPos(thisX,thisY,pLandscape->numCols)] != _EMPTY_CELL)
{
if(!noData)
{
switch(i)
{
case 1:
pLandscape->aCells[pLandscape->aCellLookup[gridToPos(thisX,thisY,pLandscape->numCols)]].relInf = thisVal;
break;
case 2:
pLandscape->aCells[pLandscape->aCellLookup[gridToPos(thisX,thisY,pLandscape->numCols)]].relPri = thisVal;
break;
case 3:
pLandscape->aCells[pLandscape->aCellLookup[gridToPos(thisX,thisY,pLandscape->numCols)]].relSus = thisVal;
break;
default:
fprintf(stderr, "readLandscape(): shouldn't get here...\n");
break;
}
}
else
{
fprintf(stderr, "readLandscape(): NODATA when expecting value...\n");
retVal = 0;
}
}
}
thisX++;
pPtr = strtok(NULL, _GIS_WHITESPACE);
}
if(thisX != pLandscape->numCols)
{
fprintf(stderr, "%s, line %d: bad number of columns (%d)\n", fileName, thisY, thisX);
retVal = 0;
}
thisY++;
}
if(thisY != pLandscape->numRows)
{
fprintf(stderr, "%s: bad number of rows (%d)\n", fileName, thisY);
retVal = 0;
}
}
else
{
fprintf(stderr, "readLandscape(): failed to parse gis header\n");
}
if(retVal)
{
fprintf(stdout, "successfully\n");
}
fclose(fIn);
}
else
{
fprintf(stderr, "couldn't open %s\n", fileName);
retVal = 0;
break;
}
}
free(inBuff);
}
else
{
fprintf(stderr, "out of memory\n");
retVal = 0;
}
/*
Write out information on all cells that are active in the simulation (i.e. >= cellThresh)
*/
if(retVal)
{
FILE *fpOut;
int i;
sprintf(outFile, "%s%cactiveLandscape.txt", outStub, C_DIR_DELIMITER);
fpOut = fopen(outFile, "wb");
if(fpOut)
{
for(i=0;i<pLandscape->numCells;i++)
{
fprintf(fpOut, "%d %d %f %d\n", pLandscape->aCells[i].xPos, pLandscape->aCells[i].yPos, pLandscape->aCells[i].propFull, i);
}
fclose(fpOut);
}
fprintf(stdout, "\t%d valid cells (%d rows; %d cols)\n", pLandscape->numCells, pLandscape->numRows, pLandscape->numCols);
fprintf(stdout, "\ttotalFull=%f\n", pLandscape->totalFull);
}
return retVal;
}
/*
Primary rate of infection is stored in cumulative form to make it easier to find out which cell is infected next
*/
int setupPrimary(t_PriInf *pPriInf, t_Landscape *pLandscape, double ratePri)
{
int i,retVal;
double thisVal,cumVal;
fprintf(stdout, "setupPrimary()\n");
retVal = 0;
memset(pPriInf,0,sizeof(t_PriInf));
pPriInf->aCumPressure = malloc(sizeof(double)*pLandscape->numCells);
if(pPriInf->aCumPressure)
{
cumVal = 0;
for(i=0;i<pLandscape->numCells;i++)
{
/*
Values in the GIS file are multipled by proportion of cell occupied, as well as relative susceptibility
*/
thisVal = pLandscape->aCells[i].propFull * pLandscape->aCells[i].relPri * pLandscape->aCells[i].relSus;
cumVal += thisVal;
pPriInf->aCumPressure[i] = cumVal;
}
pPriInf->totalPressure = cumVal;
pPriInf->nextT = _UNDEF_TIME;
pPriInf->ratePri = ratePri;
retVal = 1;
fprintf(stdout, "\ttotalPressure=%f\n", pPriInf->totalPressure);
}
return retVal;
}
/*
Allocate memory for epidemic
*/
int setupEpidemic(t_Epidemic *pEpidemic, t_Landscape *pLandscape, double dispScale)
{
int retVal;
fprintf(stdout, "setupEpidemic()\n");
retVal = 0;
memset(pEpidemic,0,sizeof(t_Epidemic));
pEpidemic->queueLen = 0;
pEpidemic->aQueueCells = malloc(sizeof(int) * pLandscape->numCells);
pEpidemic->totalInf = 0;
pEpidemic->aInfCells = malloc(sizeof(int) * pLandscape->numCells);
if(pEpidemic->aQueueCells && pEpidemic->aInfCells)
{
retVal = 1;
fprintf(stdout, "\t%d cells\n", pLandscape->numCells);
}
return retVal;
}
/*
Find the time of the next primary infection across the landscape
given that the total rate of entry on an entirely susceptible landscape is pPriInf->ratePri
per unit of time [that some will hit already infected cells is handled later]
*/
void setNextPossPriTime(t_PriInf *pPriInf, double thisTime)
{
double randDbl;
randDbl = uniformRandom();
if(pPriInf->ratePri > 0.0)
{
pPriInf->nextT = thisTime - log(randDbl)/pPriInf->ratePri;
}
else
{
pPriInf->nextT=_VERY_LONG_TIME;
}
}
double getNextPossPriTime(t_PriInf *pPriInf)
{
return pPriInf->nextT;
}