-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathswn.c
executable file
·2412 lines (2161 loc) · 62 KB
/
swn.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 <time.h>
#include <math.h>
#include <string.h>
#include <pthread.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
#include "inet.h"
#define ver "2.0" /* Network Influenza Model (NIM) Version # */
#define first_host 0
int main( int argc, char **argv);
void runProg(char **argv);
void freeNetwork();
void acceptFromOtherCity();
void connectToCity();
void rewire();
int isANeighbor(int host, int randHost);
void delNeighbor(int host, int delHost);
void initInfect();
struct strain_list *new_strain_list();
struct flu *new_flu_strain();
void infect(int host, struct flu *f, int d);
void initialize(char **argv);
void vaccinate_hosts();
void readvars();
void vacc_high_clust();
void siftDownHighClust(int root, int bottom);
void vacc_clust();
void vacc_cross_cuts();
void vacc_hubs();
void cross_cut();
void degreedist();
void siftDownDegree(int root, int bottom);
void sort_cross_cuts();
void siftDownCrossCuts(int root, int bottom);
void only_one_across(int hostID);
void vacc_low_clust();
void siftDownLowClust(int root, int bottom);
void vaccinate_node(int IDnum);
void spread_flu_from_host(int host);
void spreadflu(void);
struct infectious *get_infectious_host_list(void);//changed struct to int
int is_infectious(int h);
float strains_matchN(int h);
float N_prop_match(int h);
struct infectious *new_infected_host(void);
struct flu *mutate(struct flu *f);
float percent_match(int a, int b, int nbits);
int hamming_distance(int a, int b, int nbits);
void make_pajek(char *argv[]);
void make_degree_distribution(char *argv[]);
void make_tree_file(char *argv[]);
void make_strains_file(char *argv[]);
void clustercoeff(void);
void recovery(void);
void statistics(void);
void summarystats(void);
void suminfected (int d);
void average(void);
void free_strain_list(void);
void print_tree();
struct people
{
int *neighbor;
int myNumNeighbors;
int cross;
struct flu *flu_strains_infected; /* current strains in host */
struct flu *flu_strains_recovered; /* past strains of flu */
double clustco;
};
struct point
{
struct people *id;
} *level;
struct infectious
{
int h;
struct infectious *next;
struct infectious *prev;
};
struct flu
{
int H, N; /* H and N proteins */
int strain; /*strain, variable length*/
int old_strain;
int infect_day; /* day flu infected */
int vaccine;
struct flu *next;
};
struct strain_list
{
int first_day;
int last_day_inf;
int strain;
int old_strain;
int instance;
int hamming_distance;
struct strain_list *next;
};
int unit, nodes;
struct strain_list *first_strain;
struct flu *first_flu;
int neighbors;
char printinfo, vaccinate;
int day,vacc_day,net_type,num_days,num_runs,numbits_Hsubtypes,numbits_Nsubtypes, num_net_types, strat_comb, pc_comb;
int half_pop_inf,num_days_vacc, num_vacc_strat, vacc_strats[36], vacc_strat, print_average, tree, count_strains, currentvacc;
int strategy,percentage,original_number_neighbors,net_types[2], duration, peak_day,countrun = 0;
int vacc_days[100],num_pc_vacc,num_pop_size,pop_sizes[100],num_NCR,num_mutate,print_strains,tot_count_strains;
int num_days_infectious,latent_period,num_reps;
int print_hosts,makepajek,makedegreedist,num_infect,infect_H[20],infect_N[20];
int vaccine_subtype_H[3],vaccine_subtype_N[3],IDs[1000000], cross_cuts[1000000];
int num_vaccinate,num_flu_vaccine,originalstrain[20],vacstrain[20],num_p_values;
long numbits_strain;
float NCRs[100],p_values[100],mutation_rates[100],mut_rate,swnP,NCR,num_infected,peak_num = 0;
float totalinf[10000],averageinf[10000],totalrec[10000],averagerec[10000],numstrain[10000],averagetotstrain[10000];
float totalstrain[10000],averagestrain[10000],per_vaccinate,pc_vacc[100];
float totalinfections[10000],averageinfections[10000];
FILE *input, *average_flu_days, *host_output, *flu_sum_output, *tree_files, *strain_files;
int CITY_NUMBER;
/* declarations for sockets */
int sockfd,newsockfd,clilen,childpid, n;
struct sockaddr_in cli_addr, serv_addr, serv_addr2;
char recvline[MAXLINE];
struct timeval tv;
fd_set readfds;
int main(int argc, char **argv)
{
char s[120];
pthread_t doWork, hear, speak;
if (argc != 3)
{
printf ("Try: argv[0] RandNumberSeed CityNumber\n");
exit(0);
}
srand(25 * atoi(argv[1]));
CITY_NUMBER = atoi(argv[2]);
//srand((unsigned)time(NULL));
strcpy(s,"sum_file");
strcat(s,argv[1]);
strcat(s," ");
strcat(s,argv[2]);
strcat(s,".csv");
if ((flu_sum_output = fopen(s, "w")) == NULL)
{
printf("failed to open flu summary output file: be sure it's closed in Excel\n");
exit(0);
}
pthread_create(&doWork, NULL, runProg, argv);
if(CITY_NUMBER == 1)
pthread_create(&hear, NULL, acceptFromOtherCity, NULL);
else if (CITY_NUMBER == 2)
pthread_create(&speak, NULL, connectToCity, NULL);
pthread_join(doWork, NULL);
pthread_join(hear,NULL);
pthread_join(speak,NULL);
}
void acceptFromOtherCity()
{
int host,numbytes;
tv.tv_sec = 2;
tv.tv_usec = 500000;
struct flu *firstFlu = (struct flu *) malloc(sizeof(struct flu));
struct flu *f = (struct flu *) malloc(sizeof(struct flu));
FD_ZERO(&readfds);
/*
* open a TCP socket - Internet stream socket
*/
if ( (sockfd = socket(AF_INET, SOCK_STREAM,0)) < 0 )
{
perror("server: can't open stream socket");
exit(1);
}
/*
* bind our local address so that the client can send us
*/
memset((char *) &serv_addr, 0, sizeof(serv_addr));
serv_addr.sin_family = AF_INET;
serv_addr.sin_addr.s_addr = htonl(INADDR_ANY);
serv_addr.sin_port = htons(SERV_TCP_PORT);
if ( bind(sockfd, (struct sockaddr *)&serv_addr,sizeof(serv_addr)) < 0)
{
perror("server: can't bind local address");
exit(1);
}
printf("waiting for City to connect.....\n");
listen(sockfd,5);
while(1)
{
/*
* wait for a connection from a client process
* - concurrent server -
*/
clilen = sizeof(cli_addr);
newsockfd = accept(sockfd, (struct sockaddr *) &cli_addr, &clilen);
if (newsockfd < 0 )
{
if (errno == EINTR)
continue; /*try again*/
perror("server: accept error");
exit(1);
}
FD_SET(newsockfd, &readfds);
printf("city connected\n");
// close(sockfd);
while(day < num_days)
{
recv(newsockfd, &host, sizeof(host), 0);
if(host < nodes)
printf("received %d host, gonna infect on %d\n",host,day);
recv(newsockfd, &firstFlu->H, sizeof(int), 0);
recv(newsockfd, &firstFlu->N, sizeof(int), 0);
recv(newsockfd, &firstFlu->strain, sizeof(int), 0);
recv(newsockfd, &firstFlu->old_strain, sizeof(int), 0);
recv(newsockfd, &firstFlu->vaccine, sizeof(int), 0);
if(host < nodes)
infect(host,firstFlu,day);
host = nodes+1;
}
//}
}//while 1
close(newsockfd);
}//acceptfromothercity
void connectToCity()
{
int hostToInfect = 670, i;
struct flu *f = first_flu;
memset( (char *) &serv_addr, 0, sizeof(serv_addr));
serv_addr.sin_family = AF_INET;
serv_addr.sin_addr.s_addr = inet_addr(SERV_HOST_ADDR);
serv_addr.sin_port = htons(SERV_TCP_PORT);
/*
* open a TCP socket - internet stream socket
*/
if ( (sockfd = socket(AF_INET, SOCK_STREAM,0)) < 0 )
{
perror("client: can't open stream socket");
exit(1);
}
/*
* connect to the server
*/
if (connect(sockfd, (struct sockaddr *) &serv_addr, sizeof(serv_addr) ) < 0)
{
perror("client: can't connect to server");
exit(2);
}
printf("CONNECTED\n");
while(day < num_days)// && hostToInfect < nodes)
{
/* if( send(sockfd, &hostToInfect, sizeof(int), 0) == -1 )
{
printf("SEND ERROR!!\n");
exit(1);
}
send(sockfd, &f->H, sizeof(int), 0);
send(sockfd, &f->N, sizeof(int), 0);
send(sockfd, &f->strain, sizeof(int), 0);
send(sockfd, &f->old_strain, sizeof(int), 0);
send(sockfd, &f->vaccine, sizeof(int), 0);
printf("host %d sent\n",hostToInfect);
hostToInfect++;
// usleep(500000);
*/
}
close(sockfd);
}//connectToCity
void runProg(char **argv)
{
int a, b, c, f, l, k, j, g, y;
char average_flu_day[120],s[120];
int si;
int numinfhosts, numrechosts, numinfections;
struct flu *recovereds;
int toSend = 512;
readvars();
fprintf(flu_sum_output,"\nNet_type,swnP,vacc_strats,per_vaccs,popsize,NCR,mut_rate,num_inf,tot_inf,duration,peak_inf,peak_day,half_pop_inf_day,# strains\n");
if(!vaccinate) {
printf("WARNING: Not vaccinating but summary output will report vacc_strat!\n");
printf("\tAlthough slower, consider vaccinating but setting proportion \n\tof pop to vacc = 0.0%\n\n");
}
swnP = p_values[0];
nodes = pop_sizes[0];
printf("Population: %d\n",nodes);
strategy = 0;
vacc_strat = vacc_strats[strategy];
percentage = 0;
per_vaccinate = pc_vacc[percentage];
NCR = NCRs[0];
mut_rate = mutation_rates[0];
// vaccinate = 1;
for (y = 0; y < num_days; y++)
{
totalinf[y]=0;
totalrec[y]=0;
totalinfections[y]=0;
totalstrain[y]=0;
numstrain[y]=0;
averagestrain[y]=0;
averagetotstrain[y]=0;
}
for (g = 1; g<=num_runs; g++)
{
initialize(argv); /* set up model and do statistics */
printf("done setting up network and rewiring\n");
duration = 0;
peak_num = 0;
peak_day = 0;
currentvacc = 0;
vacc_day = vacc_days[currentvacc++];
for (day = 0; day < num_days; day++)
{
//usleep(20000);
if(day <= 120)
sleep(1);
if(day == 10)
{
if(CITY_NUMBER == 2)
{
if( send(sockfd, &toSend, sizeof(int), 0) == -1 )
{
printf("SEND ERROR!!\n");
exit(1);
}
}
}
printf("%d\n",day);
//printf("vaccinate %d\n",vaccinate);
if(vaccinate==1 && day==vacc_day)
{
// printf("vaccinating on day %d\n",day);
vaccinate_hosts(); /* vaccinates if its a vacc_day */
vacc_day = vacc_days[currentvacc];
}
recovery(); /* recovery check of all hosts */
spreadflu();
statistics();
numinfhosts = 0;
numrechosts = 0;
numinfections = 0;
for (si=0; si < nodes; si++)
{
if (level[si/unit].id[si%unit].flu_strains_infected)
{
numinfhosts++; /* count up infected hosts */
}
if (level[si/unit].id[si%unit].flu_strains_recovered)
{
if (level[si/unit].id[si%unit].flu_strains_recovered->vaccine == 0)
{
numrechosts++; /* count up recovered hosts, but not vaccinated hosts */
}
recovereds = level[si/unit].id[si%unit].flu_strains_recovered;
while(recovereds)
{
if (recovereds->vaccine == 0)
{
numinfections++; /* counts all previous infections, but not vaccinations */
}
recovereds = recovereds->next;
}
}
}
totalinf[day]= numinfhosts; /* add infections from current run to total */
totalrec[day]= numrechosts; /* add recovereds from current run to total */
totalinfections[day]= numinfections; /* add count all previous infections from current run to total */
numstrain[day]= count_strains; /* add count of strains/day from current run to total */
totalstrain[day]= tot_count_strains; /* add cummulative count of strains from current run to total */
}
free_strain_list();
freeNetwork();
}
average();
summarystats();
/******** Print Summary File ******/
if (net_type == 0)
fprintf(flu_sum_output,"SWN,");
else
fprintf(flu_sum_output,"SFN,");
fprintf(flu_sum_output,"%4.2f,",swnP);
for(j=0;j<num_days_vacc;j++)
{
fprintf(flu_sum_output," %d",vacc_strats[j]);
}
fprintf(flu_sum_output,",");
for(j=0;j<num_days_vacc;j++)
{
fprintf(flu_sum_output," %4.2f",pc_vacc[j]);
}
fprintf(flu_sum_output,",%d,%4.2f,%4.4f,%4.2f,%4.2f,%d,%4.2f,%d,%d,%d\n",nodes,NCR,mut_rate,num_infected,averageinfections[num_days-1],duration,peak_num,peak_day,half_pop_inf,count_strains);
fflush(flu_sum_output);
if(print_average)
{
fclose(average_flu_days);
}
printf("NORMAL EXECUTION\n");
exit(0);
}
void freeNetwork()
{
int i, j;
struct flu *temp;
for(i = 0; i < nodes/unit; i ++)
{
for(j = 0; j < unit; j ++)
{
free(level[i].id[j].neighbor);
temp = level[i].id[j].flu_strains_infected;
while(temp)
{
temp = temp->next;
free(level[i].id[j].flu_strains_infected);
level[i].id[j].flu_strains_infected = temp;
}
temp = level[i].id[j].flu_strains_recovered;
while(temp)
{
temp = temp->next;
free(level[i].id[j].flu_strains_recovered);
level[i].id[j].flu_strains_recovered= temp;
}
free(temp);
}
free(level[i].id);
}
free(level);
}
void initialize(char **argv)// initialize the network
{
int max, offset;
int i, j, k;
int additional = 1;
neighbors = original_number_neighbors;
unit = nodes / 10; // unit size set to tenth of total nodes
for(i = 0; i < nodes; i ++)
IDs[i] = i;
if ((unit / 2) > 2999)
unit = unit / 2;
if ((nodes % unit) != 0)
max = (nodes / unit) + 1;
else
max = (nodes / unit);
level = (struct point *) malloc (sizeof(struct point) * max);
for(i = 0; i < max; i++)\
{
level[i].id = (struct people *) malloc (sizeof(struct people) * unit);
for(j = 0; j < unit; j++)
{
level[i].id[j].neighbor = (int *) malloc (sizeof(int) * original_number_neighbors*6);
level[i].id[j].myNumNeighbors = 0;
level[i].id[j].flu_strains_infected = NULL;
level[i].id[j].flu_strains_recovered = NULL;
level[i].id[j].clustco = 0;
}
}
max = nodes;
for(i = 0; i < nodes; i++) //intital wiring loop
{
k = level[i/unit].id[i%unit].myNumNeighbors;
for(j = 1; j <= (neighbors); j++) //set right neighbors
{
offset = i + j;
if(offset >= max)
{
offset = (offset - (max-1)) - 1;
}
level[i/unit].id[i%unit].neighbor[k++] = offset;
level[i/unit].id[i%unit].myNumNeighbors++;
}
for(j = (neighbors); j > 0; j--) //set left neighbors
{
offset = i - j;
if(offset < 0)
{
offset += max;
}
level[i/unit].id[i%unit].neighbor[k++] = offset;
level[i/unit].id[i%unit].myNumNeighbors++;
}
}
rewire();
clustercoeff();
if (makepajek)
make_pajek(argv);
if (makedegreedist)
make_degree_distribution(argv);
if (tree)
make_tree_file(argv);
if (print_strains)
make_strains_file(argv);
initInfect();
if (print_hosts)
fprintf(host_output,"Day, hostID,infected_day,H,N,strain\n");
}//initialize
void rewire()
{
int i,j,k,l,m, temp;
float randnumb;
int randHost;
for(j = 0; j < nodes; j++)
{
for(k = 0; k < level[j/unit].id[j%unit].myNumNeighbors; k++)
{
randnumb = (rand()/(float)RAND_MAX); //random number generation
if((randnumb < swnP) && ((level[j/unit].id[j%unit].neighbor[k] - j > 0) || (j >= nodes - neighbors)) && level[j/unit].id[j%unit].neighbor[k]>0)
{
// printf("node %d 's %d neighbor must be rewired\n", j, level[j/unit].id[j%unit].neighbor[k]);
randHost = nodes*(rand()/(float)RAND_MAX); //get a random host
while(randHost == j || isANeighbor(j,randHost)) //if it is us or one of our neighbors....
randHost = nodes*(rand()/(float)RAND_MAX); // then get get a different random host
// printf("it will be rewired to %d\n",randHost);
delNeighbor(level[j/unit].id[j%unit].neighbor[k],j); //delete the host from the original neighbors array
level[j/unit].id[j%unit].neighbor[k]=0-randHost; //set the neighbor to the negative value of the random host, negative so that we know it has been rewired already
//add the host to the end of the neighbor hosts array
level[randHost/unit].id[randHost%unit].neighbor[level[randHost/unit].id[randHost%unit].myNumNeighbors++]=0-j;
}
}
}
}//rewire
int isANeighbor(int host, int randHost)
{
int i;
for(i = 0; i < level[host/unit].id[host%unit].myNumNeighbors; i++)
{
if(abs(level[host/unit].id[host%unit].neighbor[i]) == randHost)
return 1;
}
return 0;
}//isANeighbor
void delNeighbor(int host, int delHost)
{
int i,j;
for(i = 0; i < level[host/unit].id[host%unit].myNumNeighbors; i++)
{
if(abs(level[host/unit].id[host%unit].neighbor[i]) == delHost)
{
for(j = i; j < level[host/unit].id[host%unit].myNumNeighbors-1 ; j++)
level[host/unit].id[host%unit].neighbor[j] = level[host/unit].id[host%unit].neighbor[j+1];
break;
}
}
level[host/unit].id[host%unit].myNumNeighbors--;
}//delNeighbor
void initInfect()
{
struct flu *f;
struct strain_list *s;
int i, j, host;
// begin strain list, add all strains to initially infect with
first_strain = new_strain_list();
first_strain->strain = originalstrain[0];
first_strain->first_day = 0;
first_strain->last_day_inf = 0;
first_strain->old_strain = originalstrain[0];
s = first_strain;
for(j = 1; j < num_infect; j++)
{
s->next = new_strain_list();
s = s->next;
s->strain = originalstrain[j];
s->first_day = 0;
s->last_day_inf = 0;
s->old_strain = originalstrain[j];
}
for (i = 0; i < num_infect; i++)
{
host = nodes*(rand()/(float)RAND_MAX); // get random host
// get uninfected host and one that hasn't already been vaccinated
while (level[host/unit].id[host%unit].flu_strains_infected || level[host/unit].id[host%unit].flu_strains_recovered)
host= nodes*(rand()/(float)RAND_MAX);
f = new_flu_strain();
f->H = infect_H[i];
f->N = infect_N[i];
f->strain = originalstrain[i];
first_flu = f;
infect(host,f,0); //day = 0
}
}//initInfect
struct strain_list *new_strain_list()
{
struct strain_list *l;
l = (struct strain_list *) malloc(sizeof(struct strain_list));
if (!l)
{
printf("malloc of l in new_strain_list failed!\n");
exit(0);
}
l->first_day = -1;
l->last_day_inf = -1;
l->strain = -1;
l->old_strain = -1;
l->instance = 0;
l->hamming_distance = -1;
l->next = NULL;
return (l);
}//new_strain_list
struct flu *new_flu_strain()
{
struct flu *f;
f = (struct flu *) malloc(sizeof(struct flu));
if (!f)
{
printf("malloc of f in new_flu_strain failed!\n");
exit(0);
}
f->H = -1; // make -1 = unassigned
f->N = -1; // make -1 = unassigned
f->strain = -1; // make -1 = unassigned
f->old_strain = 0;
f->vaccine = 0;
f->infect_day = -1;
f->next = NULL;
return (f);
}// new_flu_strain
void infect(int host, struct flu *f, int d) // infects host "h" with flu "f" on day "d"
{
if (level[host/unit].id[host%unit].flu_strains_infected) // infect if not already infected
{
printf("host isn't supposed to be infected\n");
//exit(0);
}
else
{
// printf("host:%d is getting infected\n",host);
level[host/unit].id[host%unit].flu_strains_infected = f;
level[host/unit].id[host%unit].flu_strains_infected->infect_day = d;
}
}//infect
void vaccinate_hosts()
{
struct flu *f_new, *f;
int i, j, host;
num_vaccinate = (int)(per_vaccinate*nodes);
switch (vacc_strat)
{
case 0:
// printf("vaccinating randomly\n");
// randomly choose unvaccinated host to vaccinate
for (i = 0; i < num_vaccinate;i++)
{
host = nodes*(rand()/(float)RAND_MAX); // get random host
while(level[host/unit].id[host%unit].flu_strains_recovered || level[host/unit].id[host%unit].flu_strains_infected) // already recovered or infected, get another
{
host = nodes*(rand()/(float)RAND_MAX); // get random host
printf("in here %d \n",host);
}
for (j = 0; j < num_flu_vaccine; j++) // vaccinate with all strains
{
f_new = new_flu_strain(); // flu strain from vaccine to give host
f_new->H = vaccine_subtype_H[j];
f_new->N = vaccine_subtype_N[j];
f_new->strain = vacstrain[j];
f_new->vaccine = 1;
f = level[host/unit].id[host%unit].flu_strains_recovered; // point to recovered list
if (!f) // first vaccine flu strain in recovered list
{
level[host/unit].id[host%unit].flu_strains_recovered = f_new;
}
else
{
while (f->next)
{
f = f->next;
}
f->next = f_new;
}
}
}
// printf("done vaccinating randomly\n");
break;
case 1 :
// printf("vaccinating hubs\n");
// randomly choose most connected inds to vaccinate
vacc_hubs();
// printf("done vaccinating hubs\n");
break;
case 2 :
// printf("vaccinating low clustering\n");
// choose nodes with lowest clustering coefficient
vacc_low_clust();
// printf("done vaccinating low clust\n");
break;
case 3 :
// printf("high clust\n");
// choose nodes with highest clustering coefficient
vacc_high_clust();
// printf("done with high clust\n");
break;
case 4 :
// printf("cross cuts\n");
// choose nodes with cross-cut edges
vacc_cross_cuts();
// printf("done with cross cuts\n");
break;
default :
printf("problem in vaccinate_hosts()\n");
exit(0);
}
strategy = strategy+1;
percentage = percentage+1;
if(vacc_days[currentvacc+1] && (strategy < num_vacc_strat))
{
currentvacc = currentvacc+1;
vacc_day = vacc_days[currentvacc];
vacc_strat = vacc_strats[strategy];
per_vaccinate = pc_vacc[percentage];
}
}//vaccinate_hosts
void readvars()
{
int i,j;
char c;
if (printinfo)
printf("in readvars\n");
if ((input = fopen("fluvars.in", "r")) == NULL)
{
printf("failed to open input file");
exit(0);
}
fscanf(input, "%d %*s", &printinfo);
// printf("printinfo:%d\n",printinfo);
fscanf(input, "%d %*s", &num_days);
// printf("num_days:%d\n",num_days);
fscanf(input, "%d %*s", &num_runs);
// printf("num_runs:%d\n",num_runs);
fscanf(input, "%d %*s", &num_reps);
// printf("num_reps:%d\n",num_reps);
fscanf(input, "%d %*s", &num_net_types);
// printf("num_net_types:%d\n",num_net_types);
if (num_net_types < 0 || num_net_types > 2)
{
printf("only 1 or 2 net types allowed\n");
exit(0);
}
for(i=0;i<num_net_types;i++)
{
fscanf(input, "%d", &net_types[i]);
// printf("net_types[%d]:%d\n",i,net_types[i]);
}
fscanf(input, "%d %*s", &num_p_values);
//printf("num_p_values:%d\n",num_p_values);
for(i=0;i < num_p_values;i++)
{
fscanf(input, "%f", &p_values[i]);
// printf("p_values[%d]:%f\n",i,p_values[i]);
if (p_values[i] < 0 || p_values[i] > 1.)
{
printf("swnP = %f is not allowed\n",p_values[i]);
exit(0);
}
}
fscanf(input, "%s %*s", &c);
vaccinate = atoi(&c);
// printf("vaccinate:%d\n",vaccinate);
fscanf(input, "%d %*s", &num_days_vacc);
//printf("num_days_vacc:%d\n",num_days_vacc);
for(i=0;i<num_days_vacc;i++)
{
fscanf(input, "%d", &vacc_days[i]);
//printf("vacc_days[%d]:%d\n",i,vacc_days[i]);
if(vacc_days[i] < 0 || vacc_days[i] > num_days)
{
// printf("vacc_day = %f is not allowed\n", vacc_days[i]);
exit(0);
}
}
fscanf(input, "%d %*s", &num_vacc_strat);
// printf("num_vacc_strat:%d\n",num_vacc_strat);
if (num_vacc_strat < 0)
{
printf("cannot have a vacc_strat less than 0\n");
exit(0);
}
for(i=0;i<num_vacc_strat;i++)
{
for(j=0;j<num_days_vacc;j++)
{
fscanf(input, "%d", &vacc_strats[i*num_days_vacc+j]);
// printf("vacc_strats[%d]:%d\n",i*num_days_vacc+j,vacc_strats[i*num_days_vacc+j]);
}
}
fscanf(input, "%d %*s", &num_pc_vacc);
// printf("num_pc_vacc:%d\n",num_pc_vacc);
for(i=0;i<num_pc_vacc;i++)
{
for(j=0;j<num_days_vacc;j++)
{
fscanf(input, "%f", &pc_vacc[i*num_days_vacc+j]);
// printf("pc_vacc[%d]:%f\n",i*num_days_vacc+j, pc_vacc[i*num_days_vacc+j]);
if (pc_vacc[i*num_days_vacc+j] < 0. || pc_vacc[i*num_days_vacc+j] > 1.)
{
printf("pc_vacc = %f is not allowed\n",pc_vacc[i]);
exit(0);
}
}
}
fscanf(input, "%d %*s", &num_pop_size);
// printf("num_pop_size:%d\n",num_pop_size);
for(i=0;i<num_pop_size;i++)
{
fscanf(input, "%d", &pop_sizes[i]);
// printf("pop_sizes[%d]:%d\n",i,pop_sizes[i]);
}
fscanf(input, "%d %*s", &num_NCR);
// printf("num_NCR:%d\n",num_NCR);
for(i=0;i<num_NCR;i++)
{
fscanf(input, "%f", &NCRs[i]);
// printf("NCRs[%d]:%f\n",i,NCRs[i]);
if (NCRs[i] < 0. || NCRs[i] > 1.) {
// printf("NCR = %f is not allowed\n",NCRs[i]);
exit(0);
}
}
fscanf(input, "%d %*s", &num_mutate);
// printf("num_mutate:%d\n",num_mutate);
for(i=0;i<num_mutate;i++)
{
fscanf(input, "%f", &mutation_rates[i]);
// printf("mutation_rates[%d]:%f\n",i,mutation_rates[i]);
if (mutation_rates[i] < 0. || mutation_rates[i] > 1.)
{
printf("mutation_rate = %f is not allowed\n",mutation_rates[i]);
exit(0);
}
}
fscanf(input, "%d %*s", &original_number_neighbors);
// printf("original_number_neighbors:%d\n",original_number_neighbors);
fscanf(input, "%d %*s", &print_hosts);
// printf("print_hosts:%d\n",print_hosts);
fscanf(input, "%d %*s", &tree);
// printf("tree:%d\n",tree);
fscanf(input, "%d %*s", &print_strains);
// printf("print_strains:%d\n",print_strains);
fscanf(input, "%d %*s", &makepajek);
// printf("makepajek:%d\n",makepajek);
if (makepajek && nodes > 1000)
printf("WARNING: Making Pajek file with %d hosts\n",nodes);
fscanf(input, "%d %*s", &print_average);
// printf("print_average:%d\n",print_average);
fscanf(input, "%d %*s", &makedegreedist);
// printf("makedegreedist:%d\n",makedegreedist);
fscanf(input, "%d %*s", &numbits_Hsubtypes);
// printf("numbits_Hsubtypes:%d\n",numbits_Hsubtypes);
fscanf(input, "%d %*s", &numbits_Nsubtypes);
// printf("numbits_Nsubtypes:%d\n",numbits_Nsubtypes);
fscanf(input, "%d %*s", &numbits_strain);
// printf("numbits_strain:%d\n",numbits_strain);
if (numbits_Hsubtypes > 32 || numbits_Nsubtypes > 32 || numbits_strain > 32)
{
printf("Currently not supporting > 32-bit subtypes or strains\n");
exit(0);
}
fscanf(input, "%d %*s", &latent_period);
// printf("latent_period:%d\n",latent_period);
fscanf(input, "%d %*s", &num_days_infectious);
// printf("num_days_infectious:%d\n", num_days_infectious);
fscanf(input, "%d %*s", &num_flu_vaccine);
// printf("num_flu_vaccine:%d\n",num_flu_vaccine);
if (num_flu_vaccine > 3)
{
printf("only allowing up to 3 strains in vaccine\n");
exit(0);
}
for (i=0;i<num_flu_vaccine;i++)
{
fscanf(input, "%d %d %d %*s", &vaccine_subtype_H[i],&vaccine_subtype_N[i],&vacstrain[i]);
// printf("vaccine_subtype_H[%d]:%d\tvaccine_subtype_N[%d]:%d\tvacstrain[%d]:%d\n",i,vaccine_subtype_H[i],i,vaccine_subtype_N[i],i,vacstrain[i]);
}
fscanf(input, "%d %*s", &num_infect);
// printf("num_infect:%d\n",num_infect);
if (num_infect > 20)
{
printf("only allowing up to 20 strains to infect\n");
exit(0);
}
for (i=0;i<num_infect;i++)