-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
1496 lines (1235 loc) · 33.5 KB
/
main.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 <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <time.h>
#include <assert.h>
#include <math.h>
#include "IBMheader.h"
#include "list.h"
int grid[CELLS][CELLS] = {{0}};
/* Initial values */
int num_healthy = 100488;
int num_infected = 4500;
int num_sick = 3;
int num_treatment = 11;
int num_treated = 121;
int n_new_sick;
int n_new_infected;
int n_heal_infected;
int n_dead_infected;
int n_new_treatment;
int n_new_treated;
int n_recovered;
/* Defines age_groups considered: 0-5, 6-15, ...*/
int age_groups[11] = {-1,5,15,25,35,45,55,65,75,85,90};
/* Probability of a generic infected/sickened to be on a given age group */
float p_infected_age[10] = {0.0220,0.0558,0.1201,0.2521,0.2775,0.1827,0.0575,0.0186,0.0118,0.0019};
float p_sickened_age[10] = {0.0270,0.0315,0.1393,0.3000,0.2225,0.1169,0.0719,0.0382,0.0393,0.0134};
/* Probabilities of gender and origin */
float p_infected_male = 0.6802;
float p_infected_foreign = 0.7357;
float p_sickened_male = 0.6978;
float p_sickened_foreign = 0.7157;
/* Characteristic times in days */
int t_infected_max = 7*365;
int t_treatment_min = 15;
int t_treatment_max = 180;
int t_to_healthy = 720; // Time after diagnose to consider healthy.
/* Probabilities of risk factors */
float p_HIV = 0.0042;
float p_diabetes = 0.241;
float p_smoking = 0.056;
/* Probability of smear positive case */
float p_smear = 0.22;
/* Diagnose delay mean and std.
* Mean has two components [autochton,foreign]
*/
int diagnose_mean[2];
int diagnose_std = 4;
/* Probability to abandon treatment before finishing it */
float p_abandon;
float p_relapse_min = 0.01;
/* Probability to infect an close individual */
float p_infect;
/* Parameters related to age class for new infected.
* youngster (0-20), adult (20-60), elder (60-) */
/* Boundaries in days between age classes */
int age_class_boundaries[2] = {20*365, 60*365};
/* Probability to belong to a certain age class and gender
* according to characteristics of the infecting case.
* [youngster] [male adult] [female adult] [elder] (Case)
* [youngster]
* [adult]
* [elder]
* (Contact)
*/
float p_age_class[3][4] = {{0.30, 0.05, 0.12, 0.05},
{0.65, 0.94, 0.85, 0.45},
{0.05, 0.01, 0.03, 0.50}};
/* Age groups in each age class and probability of each one */
int young_age_groups[5] = {-1,5,10,15,20};
float p_young_age[4] = {0.1970, 0.2121, 0.2879, 0.3030};
int adult_age_groups[5] = {19, 30, 40, 50, 60};
float p_adult_age[4] = {0.2581, 0.3557, 0.2419, 0.1443};
int elder_age_groups[4] = {59, 70, 80, 90};
float p_elder_age[3] = {0.6667, 0.2121, 0.1212};
/* Probability of origin and gender of new infected depending
* of origin and gender of the infector
* [Native male] [Native female] [Foreign male] [Foreign female] (Case)
* [Native male]
* [Native female]
* [Foreign male]
* [Foreign female]
* (Contact)
*/
float p_origin_gender[4][4] = {{0.40, 0.35, 0.11, 0.08},
{0.28, 0.33, 0.03, 0.06},
{0.19, 0.16, 0.65, 0.49},
{0.13, 0.16, 0.21, 0.37}};
/* Probability to become sick depending on time infected */
float p_sicken_all;
float p_sicken[7];
int f_sicken;
/* Factors that, if present, multiply p_sicken */
float f_sicken_child; // [0,5) years
float f_sicken_young; // [5,15) years
float f_sicken_HIV;
float f_sicken_diabetes;
float f_sicken_smoking;
/* Superior value of age group and probability to die for each group */
int p_die_age[2] = {10*365, 65*365};
double p_die[3] = {6.877e-7, 4.65e-6, 1.2299e-4};
double p_die_sick = {2.192e-4};
void main(){
/* Initialize the random seed to generate different simulations */
clock_t begin = clock();
srand(time(NULL));
int ii;
FILE *inputfile;
inputfile = fopen("data/input.txt","r");
fscanf(inputfile,"%f",&f_sicken_HIV);
fscanf(inputfile,"%f",&f_sicken_diabetes);
fscanf(inputfile,"%f",&f_sicken_smoking);
float diag0;
fscanf(inputfile,"%f",&diag0);
diagnose_mean[0] = (int)diag0;
float diag1;
fscanf(inputfile,"%f",&diag1);
diagnose_mean[1] = (int)diag1;
fscanf(inputfile,"%f",&p_abandon);
fscanf(inputfile,"%f",&p_infect);
fscanf(inputfile,"%f",&p_sicken_all);
fscanf(inputfile,"%d",&f_sicken);
fscanf(inputfile,"%f",&f_sicken_child);
fscanf(inputfile,"%f",&f_sicken_young);
fclose(inputfile);
switch(f_sicken) {
case 1:
p_sicken[0] = 0.0280 * p_sicken_all;
p_sicken[1] = 0.0220 * p_sicken_all;
p_sicken[2] = 0.0177 * p_sicken_all;
p_sicken[3] = 0.0134 * p_sicken_all;
p_sicken[4] = 0.0093 * p_sicken_all;
p_sicken[5] = 0.0060 * p_sicken_all;
p_sicken[6] = 0.0036 * p_sicken_all;
case 2:
p_sicken[0] = 0.0250 * p_sicken_all;
p_sicken[1] = 0.0250 * p_sicken_all;
p_sicken[2] = 0.0100 * p_sicken_all;
p_sicken[3] = 0.0100 * p_sicken_all;
p_sicken[4] = 0.0100 * p_sicken_all;
p_sicken[5] = 0.0100 * p_sicken_all;
p_sicken[6] = 0.0100 * p_sicken_all;
case 3:
p_sicken[0] = 0.0285 * p_sicken_all;
p_sicken[1] = 0.0215 * p_sicken_all;
p_sicken[2] = 0.0163 * p_sicken_all;
p_sicken[3] = 0.0123 * p_sicken_all;
p_sicken[4] = 0.0093 * p_sicken_all;
p_sicken[5] = 0.0070 * p_sicken_all;
p_sicken[6] = 0.0053 * p_sicken_all;
case 4:
p_sicken[0] = 0.0250 * p_sicken_all;
p_sicken[1] = 0.0250 * p_sicken_all;
p_sicken[2] = 0.0177 * p_sicken_all;
p_sicken[3] = 0.0126 * p_sicken_all;
p_sicken[4] = 0.0089 * p_sicken_all;
p_sicken[5] = 0.0045 * p_sicken_all;
p_sicken[6] = 0.0034 * p_sicken_all;
case 5:
p_sicken[0] = 0.0270 * p_sicken_all;
p_sicken[1] = 0.0230 * p_sicken_all;
p_sicken[2] = 0.0188 * p_sicken_all;
p_sicken[3] = 0.0145 * p_sicken_all;
p_sicken[4] = 0.0101 * p_sicken_all;
p_sicken[5] = 0.0056* p_sicken_all;
p_sicken[6] = 0.0010 * p_sicken_all;
}
FILE *outputfile;
outputfile = fopen("data/output.txt","w");
int i, t, t_max = 365;
int yearmax = 10;
int j,k,count = 0;
/* List for each state */
List *Elist = malloc(sizeof(List));
List *Ilist = malloc(sizeof(List));
List *Tlist = malloc(sizeof(List));
List *Rlist = malloc(sizeof(List));
/* Nodes to read the list */
ListNode *node, *temp;
initialize_simulation(Elist, Ilist, Tlist, Rlist);
/* Initialize grid */
for(j = 0; j < CELLS; j++){
for(k = 0; k < CELLS; k++){
count += grid[j][k];
}
}
/*
printf("Tot: %d, ",count+Elist->logicalLength+Ilist->logicalLength+Tlist->logicalLength+Rlist->logicalLength);
printf("S: %d, ",count);
printf("E: %d, ", Elist->logicalLength);
printf("I: %d, ", Ilist->logicalLength);
printf("T: %d, ", Tlist->logicalLength);
printf("R: %d\n", Rlist->logicalLength);
*/
for(i = 0; i < yearmax; i++){
/*Years*/
/* Restart counters each year */
n_new_sick = 0;
n_new_infected = 0;
n_heal_infected = 0;
n_dead_infected = 0;
n_new_treatment = 0;
n_new_treated = 0;
n_recovered = 0;
for(t = 0; t < t_max; t++){
/* Days */
/* Sick people dynamics */
node = Ilist->head;
while(node != NULL) {
temp = node->next;
sick_update(Ilist,node,Elist,Tlist);
node = temp;
}
/* Infected people dynamics */
node = Elist->head;
while(node != NULL) {
temp = node->next;
infected_update(Elist,node,Ilist);
node = temp;
}
/* Treatment people dynamics */
node = Tlist->head;
while(node != NULL) {
temp = node->next;
treatment_update(Tlist,node,Rlist);
node = temp;
}
/* Treated people dynamics */
node = Rlist->head;
while(node != NULL) {
temp = node->next;
treated_update(Rlist,node,Ilist);
node = temp;
}
/* Healthy people dynamics */
count = 0;
for(j = 0; j < CELLS; j++){
for(k = 0; k < CELLS; k++){
count += grid[j][k];
}
}
move_healthy();
}
fprintf(outputfile,"%d %d %d %d %d %d %d\n", n_new_sick, n_new_infected, n_heal_infected, n_dead_infected, n_new_treatment, n_new_treated, n_recovered);
/*printf("Infected: %d, ",Elist->logicalLength);
printf("new sick: %d\n",n_new_sick);
*/
}
fclose(outputfile);
clock_t end = clock();
double time_spent = (double)(end - begin) /CLOCKS_PER_SEC;
printf("Elapsed: %f seconds \n",time_spent);
}
void print_num_states(List *Elist, List *Ilist, List *Tlist, List *Rlist, int count)
{
printf("Tot: %d, ",count+Elist->logicalLength+Ilist->logicalLength+Tlist->logicalLength+Rlist->logicalLength);
printf("S: %d, ",count);
printf("E: %d, ", Elist->logicalLength);
printf("I: %d, ", Ilist->logicalLength);
printf("T: %d, ", Tlist->logicalLength);
printf("R: %d\n", Rlist->logicalLength);
}
void print_yearly_parameters(void)
{
printf("new sick: %d, ",n_new_sick);
printf("new infected: %d, ", n_new_infected);
printf("heal infected: %d, ", n_heal_infected);
printf("new treatment %d, ", n_new_treatment);
printf("new treated: %d, ", n_new_treated);
printf("recovered: %d, ", n_recovered);
printf("dead infected: %d\n", n_dead_infected);
}
void list_new(List *list, int elementSize, freeFunction freeFn)
{
assert(elementSize > 0);
list->logicalLength = 0;
list->elementSize = elementSize;
list->head = list->tail = NULL;
list->freeFn = freeFn;
}
void list_destroy(List *list)
{
ListNode *current;
while(list->head != NULL) {
current = list->head;
list->head = current->next;
if(list->freeFn) {
list->freeFn(current->data);
}
free(current->data);
free(current);
}
}
void list_prepend(List *list, void *element)
{
ListNode *node = malloc(sizeof(ListNode));
node->data = malloc(list->elementSize);
node->prev = NULL;
memcpy(node->data, element, list->elementSize);
if(list->logicalLength == 0) {
list->head = list->tail = node;
} else {
node->next = list->head;
list->head->prev = node;
list->head = node;
}
list->logicalLength++;
}
void list_append(List *list, void *element)
{
ListNode *node = malloc(sizeof(ListNode));
node->data = malloc(list->elementSize);
node->next = NULL;
memcpy(node->data, element, list->elementSize);
if(list->logicalLength == 0) {
list->head = list->tail = node;
} else {
node->prev = list->tail;
list->tail->next = node;
list->tail = node;
}
list->logicalLength++;
}
void list_for_each(List *list, ListIterator iterator)
{
assert(iterator != NULL);
ListNode *node = list->head;
bool result = TRUE;
while(node != NULL && result) {
result = iterator(node->data);
node = node->next;
}
}
void list_del_node(List *list, ListNode *node)
{
assert(list->logicalLength > 0);
if(node == list->head && node == list->tail) {
list->head = NULL;
list->tail = NULL;
} else if(node == list->head) {
list->head = node->next;
list->head->prev = NULL;
} else if(node == list->tail) {
list->tail = node->prev;
list->tail->next = NULL;
} else {
node->prev->next = node->next;
node->next->prev = node->prev;
}
list->logicalLength--;
free(node->data);
free(node);
}
void list_head(List *list, void *element, bool removeFromList)
{
assert(list->head != NULL);
ListNode *node = list->head;
memcpy(element, node->data, list->elementSize);
if(removeFromList) {
list->head = node->next;
list->logicalLength--;
free(node->data);
free(node);
}
}
void list_tail(List *list, void *element)
{
assert(list->tail != NULL);
ListNode *node = list->tail;
memcpy(element, node->data, list->elementSize);
}
int list_size(List *list)
{
return list->logicalLength;
}
void move(Position *pos){
double r1, p_lin, p_tot;
int r2;
int lin_disp_x[4] = {0,1,0,-1};
int lin_disp_y[4] = {1,0,-1,0};
int diag_disp_x[4] = {1,1,-1,-1};
int diag_disp_y[4] = {1,-1,1,-1};
int i;
p_tot = 4 + 4/sqrt(2);
p_tot = 4 + 4;
p_lin = 4/p_tot;
r1 = randdb(0,1);
r2 = randint(0,4);
if (r1 < p_lin){
pos->x += lin_disp_x[r2];
pos->y += lin_disp_y[r2];
} else{
pos->x += diag_disp_x[r2];
pos->y += diag_disp_y[r2];
}
boundary_condition(pos);
}
void move_healthy()
{
int i,j,k,n;
Position pos;
for(i = 0; i < CELLS; i++){
for(j = 0; j < CELLS; j++){
n = grid[i][j];
for(k = 0; k < n; k++){
pos.x = i;
pos.y = j;
grid[i][j] -= 1;
move(&pos);
grid[pos.x][pos.y] += 1;
}
}
}
}
void boundary_condition(Position *pos)
{
/* If a position is out of the grid, returns a new one
* according to periodic boundary condition.
*/
if (pos->x > (CELLS-1)){
pos->x = 0;
}
else if (pos->x < 0){
pos->x = CELLS - 1;
}
if (pos->y > (CELLS-1)){
pos->y = 0;
}
else if (pos->y < 0){
pos->y = CELLS - 1;
}
}
double define_p_die(int age)
{
/* Define the probability to die according to age for all
* individuals but undiagnosed sick.
*/
double prob;
if(age < p_die_age[0]) {
prob = p_die[0];
} else if(age < p_die_age[1]) {
prob = p_die[1];
} else {
prob = p_die[2];
}
return prob;
}
/*************************************************************
**** INFECTED DYNAMICS FUNCTIONS ****
*************************************************************/
void infected_update(List *Elist, ListNode *node, List *Ilist)
{
/* Contains everything that has to be considered per infected node
* each day.
*/
Enode *infected = node->data;
/* Check if it dies. If so, end function */
if (infected_die(Elist, node)){
return;
}
/* If not, grow and move */
infected->age++;
move(&(infected->pos));
/* Check if it heals. If so, end function */
if(infected_heal(Elist, node)) {
return;
}
/* Check if it gets sick. If so, end function */
if(infected_sicken(Elist, node, Ilist)) {
return;
}
/* If it reaches here, advance by one the time infected */
infected->t_infected++;
}
int infected_die(List *Elist, ListNode *node)
{
/* Check if an infected individual dies. If so, delete it, generate
* a new healthy individual in a random position and return 1.
* If it survives, return 0;
*/
Enode *infected = node->data;
double r = randdb(0,1);
double prob = define_p_die(infected->age);
if(r < prob) {
Position pos = generate_position();
new_healthy(pos);
list_del_node(Elist,node);
n_dead_infected++;
return 1;
} else {
return 0;
}
}
int infected_heal(List *Elist, ListNode *node)
{
/* Determine if an infected individual can be considered healthy
* again. If so, generate a healthy in its position and return 1.
* If not, return 0.
*/
Enode *infected = node->data;
if(infected->t_infected > t_infected_max) {
new_healthy(infected->pos);
list_del_node(Elist,node);
n_heal_infected++;
return 1;
} else {
return 0;
}
}
int infected_sicken(List *Elist, ListNode *node, List *Ilist)
{
/* Given an infected individual determine if it gets sick.
* If it does, delete infected node and generate sick node with
* same characteristics.
* Also determine if it can be considered healthy.
*/
double prob, r;
int i;
float f; //factor to fit the desired behaviour
r = randdb(0,1);
Enode *infected = node->data;
for(i = 0; i*365 < infected->t_infected; i++){
;
}
prob = p_sicken[i-1]/365;
if(infected->age < 5*365) {
prob *= f_sicken_child;
} else if(infected->age < 15*365) {
prob *= f_sicken_young;
}
if(infected->HIV) {
prob *= f_sicken_HIV;
}
if(infected->diabetes) {
prob *= f_sicken_diabetes;
}
if(infected->smoking) {
prob *= f_sicken_smoking;
}
f = 0.9;
prob *= f;
if(r < prob) {
n_new_sick++;
new_sick(Ilist, infected);
list_del_node(Elist, node);
return 1;
} else {
return 0;
}
}
void new_sick(List *Ilist, Enode *infected)
{
/* Generate a sick node based on the characteristics
* of a previously infected node.
*/
Inode *new = malloc(sizeof(Inode));
new->age = infected->age;
new->foreign = infected->foreign;
new->gender = infected->gender;
new->smear = define_smear();
new->t_sick = 0;
define_diagnose(new);
new->pos = infected->pos;
list_append(Ilist, new);
}
/*************************************************************
**** SICK DYNAMICS FUNCTIONS ****
*************************************************************/
void sick_update(List *Ilist, ListNode *node, List *Elist, List *Tlist)
{
/* Contains everything that has to be considered per sick node
* each day.
*/
Inode *sick = node->data;
/* Check if it dies. If so, end function */
if(sick_die(Ilist, node)) {
return;
}
/* If not, grow and move */
sick->age++;
move(&(sick->pos));
/* Check if it starts treatment. If so, end function */
if(start_treatment(Ilist, node, Tlist)) {
return;
}
/* If not, advance sick days and infect others */
sick->t_sick++;
infect(sick,Elist);
}
int sick_die(List *Ilist, ListNode *node)
{
/* Check if sick individual dies. If so, delete it, generate
* a new healthy individual in a random position and return 1.
* If it survives, return 0;
*/
double r = randdb(0,1);
Enode *infected = node->data;
if(r < p_die_sick) {
Position pos = generate_position();
new_healthy(pos);
list_del_node(Ilist,node);
return 1;
} else {
return 0;
}
}
int start_treatment(List *Ilist, ListNode *node, List *Tlist)
{
/* If the time sick of a sick individual has reached the
* diagnose delay time, move the node from sick to node under
* treatment.
*/
Inode *sick = node->data;
if(sick->t_sick == sick->diagnose) {
new_treatment(Tlist, sick);
list_del_node(Ilist, node);
n_new_treatment++;
return 1;
} else {
return 0;
}
}
void new_treatment(List *Tlist, Inode *sick)
{
/* Generate an individual under treatment from a sick one.
* The sick evolves to treatment.
*/
Tnode *new = malloc(sizeof(Tnode));
new->age = sick->age;
new->foreign = sick->foreign;
new->gender = sick->gender;
new->smear = sick->smear;
new->t_treatment = 0;
new->pos = sick->pos;
list_append(Tlist, new);
}
void infect(Inode *sick, List *Elist)
{
/* Determine if there are healthy individuals susceptible
* to be infected (in contact) and infect them with a given
* probability.
*/
Position look;
double r, prob;
int i, j, susceptibles;
int square[4][2] = {{1,0},
//{1,1},
{0,1},
//{-1,1},
{-1,0},
//{-1,-1},
{0,-1}};
//{1,-1}};
prob = (1 + sick->smear)*p_infect;
for(i = 0; i < 9; i++) {
look = sick->pos;
look.x += square[i][0];
look.y += square[i][1];
boundary_condition(&look);
susceptibles = grid[look.x][look.y];
for(j = 0; j < susceptibles; j++) {
r = randdb(0,1);
if (r < prob){
new_infected(sick, Elist, look);
grid[look.x][look.y] -= 1;
n_new_infected++;
}
}
}
}
void new_infected(Inode *sick, List *Elist, Position pos)
{
/* Generate a new infected from a healthy individual according to
* characteristics determined by the case that infects.
*/
Enode *new = malloc(sizeof(Enode));
infected_age(new, sick);
infected_gender_origin(new, sick);
define_risk_factors(new);
new->t_infected = 0;
new->pos = pos;
list_append(Elist, new);
}
void infected_age(Enode *node, Inode *infector)
{
/* Define age of a new infected node according
* to the age of its infector.
*/
int i = 0, j = 0;
int age = infector->age;
char gender = infector->gender;
double r, prob;
prob = 0;
r = randdb(0,1);
/* Determine the age class. i-1 determines it */
if (age < age_class_boundaries[0]){
do {
prob += p_age_class[i++][0];
} while(prob < r);
} else if (age < age_class_boundaries[1] && gender == 0) {
do {
prob += p_age_class[i++][1];
} while(prob < r);
} else if (age < age_class_boundaries[1] && gender == 1) {
do {
prob += p_age_class[i++][2];
} while(prob < r);
} else {
do {
prob += p_age_class[i++][3];
} while(prob < r);
}
/* Determine age group and age inside a class */
prob = 0;
r = randdb(0,1);
switch(i-1){
case 0:
do {
prob += p_young_age[j++];
} while(prob < r);
node->age = randint((young_age_groups[j-1]+1)*365, young_age_groups[j]*365);
break;
case 1:
do {
prob += p_adult_age[j++];
} while(prob < r);
node->age = randint((adult_age_groups[j-1]+1)*365, adult_age_groups[j]*365);
break;
case 2:
do {
prob += p_elder_age[j++];
} while(prob < r);
node->age = randint((elder_age_groups[j-1]+1)*365, elder_age_groups[j]*365);
break;
}
}
void infected_gender_origin(Enode *node, Inode *infector)
{
/* Define origin and gender of a new infected according to the
* origin and gender of the infector.
*/
int i = 0, aux;
double r, prob;
aux = infector->gender + 2*infector->foreign;
// aux. 0: native male, 1: native female, 2: foreign male, 3: foreign female
r = randdb(0.0,1.0);
prob = 0;
do {
prob += p_origin_gender[i++][aux];
} while(prob < r);
switch(i-1){
case 0:
node->foreign = 0;
node->gender = 0;
break;
case 1:
node->foreign = 0;
node->gender = 1;
break;
case 2:
node->foreign = 1;
node->gender = 0;
break;
case 3:
node->foreign = 1;
node->gender = 1;
break;
}
}
/*************************************************************
**** TREATMENT DYNAMICS FUNCTIONS ****
*************************************************************/
void treatment_update(List *Tlist, ListNode *node, List *Rlist)
{
/* Contains everything that has to be considered per node under
* treatment each day.
*/
Tnode *treatment = node->data;
/* Check if it dies. If so, end function */
if (treatment_die(Tlist, node)){
return;
}
/* If not, grow and move */
treatment->age++;
move(&(treatment->pos));
/* Check if it finishes treatment. If so, end function. */
if(finish_treatment(Tlist, node, Rlist)) {
return;
}
treatment->t_treatment++;
}
int treatment_die(List *Tlist, ListNode *node)
{
/* Check if an individual under treatment dies. If so, delete it, generate
* a new healthy individual in a random position and return 1.
* If it survives, return 0;
*/
Tnode *treatment = node->data;
double r = randdb(0,1);
double prob = define_p_die(treatment->age);
if(r < prob) {
Position pos = generate_position();
new_healthy(pos);
list_del_node(Tlist,node);
return 1;
} else {
return 0;
}
}
int finish_treatment(List *Tlist, ListNode *node, List *Rlist)
{
/* Check if a given individual under treatment finishes or abandons
* treatment. If so, evolve it to treated.
*/
double r, prob;
Tnode *treatment = node->data;
r = randdb(0,1);
prob = p_abandon/t_treatment_max; // Daily prob to abandon.
if(r < prob || treatment->t_treatment == t_treatment_max) {
new_treated(Rlist, treatment);
list_del_node(Tlist, node);
n_new_treated++;
return 1;
} else {
return 0;
}
}
void new_treated(List *Rlist, Tnode *treatment)
{
/* Move a node under treatment to treated.