-
Notifications
You must be signed in to change notification settings - Fork 1
/
EvoNetC_Aim3.c
4467 lines (3972 loc) · 197 KB
/
EvoNetC_Aim3.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
/************************************************************************/
/********************* HIV NETWORK SIMULATOR **********************/
/************************************************************************/
/* */
/* Goal: Simulate effect of targeted treatment on HIV prevalence */
/* */
/* Dependencies: Variables declared in a header file listed below */
/* */
/* Input: Reads parameters from a file called "parameters.txt" */
/* */
/* Outputs: To console & ~13 "__.txt" files defined in the header file */
/* */
/************************************************************************/
// Standard C functions
#include <string.h>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include "HeaderEvoNetC_Aim3.h" // Includes names for variables and subroutines
#include "UserParams.h" // Single function that updates parameter values. *** May be generated by the R calling function. ***
int main() {
InitializationRoutines();
for (repl=1;repl<=replicates;repl++) {
//
InitializeVariables();
do {
StartOfDayPrinting();
CollectStatistics();
if (gradual_tx_incr == 1) InitiateTreatmentGradual(); else InitiateTreatment();
HIVTesting();
TreatmentDropout();
AdherenceModule();
UpdateCD4CountsStochastic();
UpdateViralLoadsAim3();
SimulateTransmission();
Births(); //ConstantInputNewSusceptibles();
Aging();
NaturalDeath();
DeathOfAIDSPatients();
UpdateDALYs();
RemoveLinksToDeadPeople();
SimulatePartnerShipDissolution();
SimulateNewPartnershipFormation();
SimulateVaccineDecay();
CheckEdgeListForImpossibleValues(); // Does some elementary error checking
time = time + 1;
EndOfDayPrinting();
} while ( (time <= tfinal) && (StopEarly == 0)); // End time loop
EndOfSimulationPrinting();
} // replicates
printf("DrugDose1 = %lf\n",DrugDose1);
} // Main
void InitializationRoutines() {
OpenFiles();
init_genrand(random_number_seed);
}
void InitializeVariables() {
time = 0;
TherapyStarted = 0;
orig_prob_sex = prob_sex;
H = Heritability;
h = sqrt(Heritability);
r0 = log(V_peak/V0)/t_peak;
Dead = 0; DiedAIDS = 0, TotalDiedAIDSAfterTasP = 0; DiedNat = 0; Time_All_Treated = 3*tfinal, Last_Time_Not_All_Treated = 0;
new_infections=0; new_infections_u50 = 0; new_infections_u25=0; new_infections_Mid = 0; new_infections_o50 = 0;
susc_days = 0; susc_days_u50 = 0; susc_days_u25 = 0; susc_days_Mid = 0; susc_days_o50 = 0;
tx_person_days = 0, cd4_cat1_person_days = 0, cd4_cat2_person_days = 0, cd4_cat3_person_days = 0, cd4_cat4_person_days = 0, cd4_cat5_person_days = 0;;
pill_count = 0; NonTargetedTxYear1 = 0; TotalTreatedYear1 = 0; NonTargetedTx = 0; TotalTreated = 0;
GetParameters();
UserParams();
BaselineAdherence1 = Adherence1; BaselineAdherence2 = Adherence2; BaselineAdherence3 = Adherence3; BaselineAdherence4 = Adherence4; BaselineAdherence5 = Adherence5;
BirthRate = BirthRate*N0/2000;
init_alleles(Num_Loci);
if (repl == 1) PrintHeaders();
//init_genrand(old_random_number_seed); // All strategies start with the same random number seed
cd4InitProbs();
init_alleles(Num_Loci);
initialize_values();
init_effect_array();
drug_effects();
CreateCD4Table();
InitialAgeDistribution();
InitializePopulation();
CalculateLifeExpectancies();
DefineSexualContactNetwork();
AddInfecteds();
UpdateViralLoadsAim3();
//PrintEdgeList(); PrintFirstAndLastContacts();
//PrintVLdistribution();
//PrintAgeDistributions();
//PrintAgeMatches();
//PrintStats(); PrintTrackedAgents();
printf("\nStarting new simulation: Percent treated = %lf, yearly_incr_tx = %lf, Under25 = %ld, Under30 = %ld, Under35 = %ld, cd4_low = %ld, cd4_aids = %ld, vl_high = %ld, vl_very_high = %ld, vl_super_high = %ld, Replicate = %ld\n",
perc_tx_start,yearly_incr_tx,under25,under30,under35,cd4_low,cd4_super_low,vl_high,vl_very_high,vl_super_high,repl);
printf("\nRepl\tTime\tN\tLinks\tUnInf\tInf\tDiag\tTx\tNoTx\tDead\tdAIDS\tdNat\tAge\tLogV\tSPVL\tMD\tUnLink\tNewInfs\tDisc\tV[1]\t\tI[1]\t\tM[1]\t\tL[1]\t\tCD4[1]\n");
}
void StartOfDayPrinting() {
if (VL_print_count >= VL_print_time) {
PrintVLdistribution();
VL_print_count = 0;
}
if (time == Start_TasP_Campaign) PrintPartnershipDistributions();
if ((time == 365) || (time == 10*365) || (time == 20*365) || (time == 30*365) || (time==40*365)) {
PrintAgeDistributions();
PrintAgeMatches();
}
VL_print_count++;
}
void EndOfDayPrinting() {
if ( time % print_frequency == 0){
PrintStats();
PrintHeritabilityStats();
PrintTrackedAgents();
}
if (time == Start_Condom_Campaign) prob_sex = orig_prob_sex * (1.0-Percent_using_condums_after_condom_campaign);
fflush(stdout);
}
void EndOfSimulationPrinting() {
if (Start_TasP_Campaign > tfinal) PrintPartnershipDistributions();
// RecordStatusOfLivingHIVPatientsForRegistryFile();
// PrintFirstAndLastContacts();
if (StopEarly == 1) {
PrintStats();
printf("Note: Program halted early -- most likely because of difficulties in finding partners using current search algorithm. Consider increasing MaxLinks.\n");
}
if (num_cases_no_link_found > 0) if (plt_warn==1) fprintf(WarningsFile,"Warning at time %ld: Number of times no link was found = %ld\n",time, num_cases_no_link_found);
if (TotalTreatedYear1 > 0) ActualNonTargetedYear1 = ((double) NonTargetedTxYear1) / ((double) TotalTreatedYear1);
else ActualNonTargetedYear1 = 0.0;
if (plt_GS==1) fprintf(GrandSummaryFile,"%d\t%ld\t%4.3lf\t%4.3lf\t%4.3lf\t%ld\t%ld\t%ld\t%ld\t%ld\t%ld\t%lf\t%ld\t%ld\t%ld\t%ld\t%ld\t%ld\t%ld\t%ld\t%ld\t%lf\t%lf\t%lf\t%lf\t%lf\t%lf\t%lf\t%lf\t%lf\t%lf\n",
TasP_Strategy,under25,perc_tx_start,ActTxStartTasP,yearly_incr_tx,repl,TotalDiedAIDSAfterTasP,Infected,Time_All_Treated,Last_Time_Not_All_Treated,
Infected - InfectedTreated,ActualNonTargetedYear1,pill_count,susc_days_last_5_years,new_infections_last_5_years,
cd4_cat1_person_days, cd4_cat2_person_days, cd4_cat3_person_days, cd4_cat4_person_days, cd4_cat5_person_days, tx_person_days,
DALY00, DALY01, DALY02, DALY03, DALY05, DALY07, DALY08, DALY10, DALY15, DALY20);
}
void OpenFiles()
{
if ( (ParFile = fopen("DefaultParameters.txt","r")) == NULL) { printf("Cannot open file 'Parameters.txt'. Hit ctrl-C to quit : "); scanf("%s",junk); }
if ( (ParOut = fopen("ParameterReguritation.txt","w")) == NULL) { printf("Cannot open file 'ParameterReguritation.txt'. Hit ctrl-C to quit : "); scanf("%s",junk); }
if ( (OutputFile = fopen("ViralLoadOutput.txt","w")) == NULL) { printf("Cannot open file 'ViralLoadOutput.txt'. Hit ctrl-C to quit : "); scanf("%s",junk); }
if ( (NumPartnersFile = fopen("NumPartners.txt","w")) == NULL) { printf("Cannot open file 'NumPartners.txt'. Hit ctrl-C to quit : "); scanf("%s",junk); }
if ( (HeritOutputFile = fopen("HeritabilityOutput.txt","w")) == NULL) { printf("Cannot open file 'HeritabilityOutput.txt'. Hit ctrl-C to quit : "); scanf("%s",junk); }
if ( (PatientRegistryOutputFile = fopen("PatientRegistryOutput.txt","w")) == NULL) { printf("Cannot open file 'PatientRegistryOutput.txt'. Hit ctrl-C to quit : "); scanf("%s",junk); }
if ( (VLOutputFile = fopen("VLHistogram.txt","w")) == NULL) { printf("Cannot open file 'VLOutput.txt'. Hit ctrl-C to quit : "); scanf("%s",junk); }
if ( (spVLOutputFile = fopen("spVLHistogram.txt","w")) == NULL) { printf("Cannot open file 'spVLOutput.txt'. Hit ctrl-C to quit : "); scanf("%s",junk); }
if ( (NetworkStatsOutputFile = fopen("NetworkStats.txt","w")) == NULL) { printf("Cannot open file 'NetworkStats.txt'. Hit ctrl-C to quit : "); scanf("%s",junk); }
if ( (AgeDistFile = fopen("AgeDistribution.txt","w")) == NULL) { printf("Cannot open file 'AgeDistribution.txt'. Hit ctrl-C to quit : "); scanf("%s",junk); }
if ( (RelLengthFile = fopen("RelationshipLengths.txt","w")) == NULL) { printf("Cannot open file 'RelationshipLenghts.txt'. Hit ctrl-C to quit : "); scanf("%s",junk); }
if ( (AgeMatchFile = fopen("AgeMatches.txt","w")) == NULL) { printf("Cannot open file 'AgeMatches.txt'. Hit ctrl-C to quit : "); scanf("%s",junk); }
if ( (RelationshipRegistryFile = fopen("RelationshipRegistry.txt","w")) == NULL) { printf("Cannot open file 'RelationshipRegistry.txt'. Hit ctrl-C to quit : "); scanf("%s",junk); }
if ( (GrandSummaryFile = fopen("GrandSummary.txt","w")) == NULL) { printf("Cannot open file 'GrandSummary.txt'. Hit ctrl-C to quit : "); scanf("%s",junk); }
if ( (AgentHistoryFile = fopen("AgentHistory.txt","w")) == NULL) { printf("Cannot open file 'AgentHistory.txt'. Hit ctrl-C to quit : "); scanf("%s",junk); }
if ( (WarningsFile = fopen("Warnings.txt","w")) == NULL) { printf("Cannot open file 'Warnings.txt'. Hit ctrl-C to quit : "); scanf("%s",junk); }
if ( (CheckSSCMaxFile = fopen("CheckSSCMax.txt", "w"))==NULL) { printf("Cannot open file 'CheckSSCMax.txt'. Hit ctrl-C to quit : "); scanf("%s",junk); }
}
void PrintTrackedAgents()
{
long i;
if (plt_AH == 1) {
for (i=1; i<=N; i++) {
if (Status[i] == 1) {
if ((i >= TrackedAgentsGroup1start && i <= TrackedAgentsGroup1end) || (i >= TrackedAgentsGroup2start && i <= TrackedAgentsGroup2end)) {
fprintf(AgentHistoryFile,"%ld\t%ld\t%ld\t%lf\t%d\t%lf\t%lf\t%ld\t%ld\t%ld\t%3.2lf\t%3.2lf\t%3.2e\t%d\t%2.1lf\t%2.1lf\t%2.1lf\t%2.1lf\t%ld\t%ld\t%ld\t%ld\t%ld\t%ld\t%ld\t%ld\t%ld",
repl,time,i, SetPoint[i], Status[i], Time_Inf[i], s[i], Treated[i], tx_days[i], tx_days_aids[i], Age[i], ProbDropout[i], V[i],
CD4[i],Drug[i][1],Drug[i][2],Drug[i][3],Drug[i][4], metab_type[i], Donors_Index[i], TransDR[i], AcqDR[i],
mut_locus1[i], mut_locus2[i], mut_locus3[i], mut_locus4[i], mut_locus5[i]);
for (i1=0;i1<=alleles[1];i1++) {
for (i2=0;i2<=alleles[2];i2++) {
for (i3=0;i3<=alleles[3];i3++) {
for (i4=0;i4<=alleles[4];i4++) {
for (i5=0;i5<=alleles[5];i5++) {
fprintf(AgentHistoryFile,"\t%2.1e", V_vec[i][i1][i2][i3][i4][i5]);
}}}}}
fprintf(AgentHistoryFile,"\n");
}
}
}
}
}
void PrintAgeDistributions()
{
long i, age, approx_age;
long AgeDist[102];
printf("Entering PrintAgeDistributions\n");
for (age=1; age<=100; age++) AgeDist[age] = 0;
for (i=1; i<=N; i++) {
if (Status[i] >= 0) {
approx_age = (long) Age[i];
if ((approx_age < 0) || (approx_age > 100)) {
printf("Problem in PrintAgeDistributions: approx_age = %ld, i = %ld\n",approx_age,i);
}
AgeDist[approx_age] = AgeDist[approx_age] + 1;
}
}
if (time <= 1) {
for (age=16; age<=100; age++) if (plt_AD==1) fprintf(AgeDistFile,"\t%ld",age);
if (plt_AD==1) fprintf(AgeDistFile,"\n");
}
if (plt_AD==1) fprintf(AgeDistFile,"%ld",time/365);
for (age=16; age<=100; age++) if (plt_AD==1) fprintf(AgeDistFile,"\t%ld",AgeDist[age]);
if (plt_AD==1) {
fprintf(AgeDistFile,"\n");
printf(" Printed some AgeDistributions\n");
}
}
void PrintPartnershipDistributions()
{
long i, j, nLinks;
long num_partners[21];
long num_partners_women[21];
long num_partners_men[21];
long num_partners_concurrent_women[21];
long num_partners_concurrent_men[21];
for (i = 0; i<= 20; i++) {
num_partners[i] = 0;
num_partners_women[i] = 0;
num_partners_men[i] = 0;
num_partners_concurrent_women[i] = 0;
num_partners_concurrent_men[i] = 0;
}
for (i = 1; i<= N; i++) {
if (Status[i] >= 0) {
nLinks = NumLinks[i];
if (nLinks > 20) nLinks = 20;
num_partners[nLinks] = num_partners[nLinks] + 1;
if (Sex[i] == 1) num_partners_women[nLinks] = num_partners_women[nLinks] + 1;
if (Sex[i] == 2) num_partners_men[nLinks] = num_partners_men[nLinks] + 1;
if (Sex[i] == 1 && Concurrent[i] == 1) num_partners_concurrent_women[nLinks] = num_partners_concurrent_women[nLinks] + 1;
if (Sex[i] == 2 && Concurrent[i] == 1) num_partners_concurrent_men[nLinks] = num_partners_concurrent_men[nLinks] + 1;
}
}
for (i = 0; i<=20; i++) fprintf(NumPartnersFile,"\t%ld",num_partners[i]); fprintf(NumPartnersFile,"\n");
for (i = 0; i<=20; i++) fprintf(NumPartnersFile,"\t%ld",num_partners_women[i]); fprintf(NumPartnersFile,"\n");
for (i = 0; i<=20; i++) fprintf(NumPartnersFile,"\t%ld",num_partners_men[i]); fprintf(NumPartnersFile,"\n");
for (i = 0; i<=20; i++) fprintf(NumPartnersFile,"\t%ld",num_partners_concurrent_women[i]); fprintf(NumPartnersFile,"\n");
for (i = 0; i<=20; i++) fprintf(NumPartnersFile,"\t%ld",num_partners_concurrent_men[i]); fprintf(NumPartnersFile,"\n");
}
void PrintAgeMatches()
{
long i, j;
for (i=1; i<=N; i++) {
if (Status[i] >= 0) {
for (j=1; j<= NumLinks[i]; j++) {
if (Status[i] >= 0 && Status[Links[i][j]] >= 0) {
if (Sex[i] == 1) {
if (plt_AM==1) fprintf(AgeMatchFile,"%ld\t%lf\t%lf\t%ld\t%ld\n",time/365,Age[i],Age[Links[i][j]],Sex[i],Sex[Links[i][j]]);
if (Sex[Links[i][j]] == 1) if (plt_warn==1) fprintf(WarningsFile,"Warning at time %ld: Sex[%ld] = %ld linked to Sex[%ld] = %ld\n",time,i,Sex[i],Links[i][j],Sex[Links[i][j]]);
} else {
if (plt_AM==1) fprintf(AgeMatchFile,"%ld\t%lf\t%lf\t%ld\t%ld\n",time/365,Age[Links[i][j]],Age[i],Sex[Links[i][j]],Sex[i]);
if (Sex[Links[i][j]] == 2) if (plt_warn==1) fprintf(WarningsFile,"Warning at time %ld: Sex[%ld] = %ld linked to Sex[%ld] = %ld\n",time,i,Sex[i],Links[i][j],Sex[Links[i][j]]);
}
}
}
}
}
//printf("Exiting PrintAgeMatches\n");
}
void PrintVLdistribution()
{
double current_VL;
long i;
long Print_Array_VL[1000];
long Print_Array_spVL[1000];
long bin_count = 0;
/* Print header giving the midpoint VL (on log scale) for each element in the histogram at time 0 */
if (time == 0) {
if (plt_VL==1) fprintf(VLOutputFile,"VL\t");
if (plt_spVL==1) fprintf(spVLOutputFile,"spVL\t");
for (current_VL = VL_print_lower; current_VL <= VL_print_upper; current_VL = current_VL + VL_print_interval) {
if (plt_VL==1) fprintf(VLOutputFile,"%3.2lf\t", current_VL+VL_print_interval/2.0);
if (plt_spVL==1) fprintf(spVLOutputFile,"%3.2lf\t", current_VL+VL_print_interval/2.0);
}
if (plt_VL==1) fprintf(VLOutputFile,"\n");
if (plt_spVL==1) fprintf(spVLOutputFile,"\n");
}
/* Zero out elements of the histogram */
bin_count = 0;
for (current_VL = VL_print_lower; current_VL <= VL_print_upper; current_VL = current_VL + VL_print_interval) {
bin_count++;
Print_Array_VL[bin_count] = 0;
Print_Array_spVL[bin_count] = 0;
}
/* For each infected person, increment histogram cell spanning that person's viral load */
for (i=1; i<=N; i++) {
if (Status[i] == 1) {
bin_count = 0;
for (current_VL = VL_print_lower; current_VL <= VL_print_upper; current_VL = current_VL + VL_print_interval) {
bin_count++;
if ( (log10(V[i]) >= current_VL) && (log10(V[i]) < current_VL+VL_print_interval)) {
//if (time - Time_Inf[i] > 50.0) {
Print_Array_VL[bin_count] = Print_Array_VL[bin_count] + 1;
//}
}
if ( (LogSetPoint[i] >= current_VL) && (LogSetPoint[i] < current_VL+VL_print_interval)) {
Print_Array_spVL[bin_count] = Print_Array_spVL[bin_count] + 1;
}
}
}
}
/* Print histogram to a file */
bin_count = 0;
if (plt_VL==1) fprintf(VLOutputFile,"%ld\t", time);
if (plt_spVL==1) fprintf(spVLOutputFile,"%ld\t", time);
for (current_VL = VL_print_lower; current_VL <= VL_print_upper; current_VL = current_VL + VL_print_interval) {
bin_count++;
if (plt_VL==1) fprintf(VLOutputFile,"%ld\t", Print_Array_VL[bin_count]);
if (plt_spVL==1) fprintf(spVLOutputFile,"%ld\t", Print_Array_spVL[bin_count]);
}
if (plt_VL==1) fprintf(VLOutputFile,"\n");
if (plt_spVL==1) fprintf(spVLOutputFile,"\n");
}
void HIVTesting() {
long i;
for (i = 1; i <=N; i++) {
if (Status[i] == 1 && (time-Time_Inf[i] > delay_test_antibodies) && Diagnosed[i] == 0) { // Assume ~35 days until sufficient antibodies for testing
if (genrand_real2() < daily_prob_diagnosis) {
Diagnosed[i] = 1;
TimeDiag[i] = time;
}
}
}
}
void VaccinatePopulation() {
long i;
for (i = 1; i <=N; i++) {
if ((Status[i] == 0) && (Vaccinated[i]==0)) { // Questions about second condition (not vaccinating those already vaccinated)
// Could get weird if vaccine compaigns occure before vaccine wears off.
// Current algorithm will work if vaccine campaigns are longer than vaccine duration
if (genrand_real2() < PercentVaccinated) {
Vaccinated[i] = 1;
LengthVaccinated[i] = 0;
}
}
}
}
void SimulateVaccineDecay() {
long i;
for (i = 1; i <=N; i++) {
if (Vaccinated[i]==1) {
LengthVaccinated[i] = LengthVaccinated[i] + 1;
if (LengthVaccinated[i] > VaccineDuration) {
Vaccinated[i] = 0;
}
}
}
}
void InitializePopulation() {
/* Initalizes the s[], Status[], Numrecepients[], Duration[], UnderCare[], Sex[], Age[], and Treated[] arrays to their default values */
long i;
long i1, i2, i3, i4, i5;
N = N0;
for (i=1;i<=5000;i++) {
NumLinks[i] = 0;
s[i] = prog_rate; // Same for all patients in this version. Could be made to vary from patient to patient in future studies
K[i] = jmpow(10.0,AverageLogSP0);
Status[i] = 0;
CD4[i] = 0;
CD4_nadir[i] = 0;
V[i] = 0.0; I[i] = 0.0; M[i] = 0.0; L[i] = 0.0;
for (i1=0;i1<=1;i1++) {
for (i2=0;i2<=1;i2++) {
for (i3=0;i3<=1;i3++) {
for (i4=0;i4<=1;i4++) {
for (i5=0;i5<=1;i5++) {
V_vec[i][i1][i2][i3][i4][i5] = 0.0;
I_vec[i][i1][i2][i3][i4][i5] = 0.0;
M_vec[i][i1][i2][i3][i4][i5] = 0.0;
L_vec[i][i1][i2][i3][i4][i5] = 0.0;
}}}}}
CD4_TimeToAIDS[i]= 0;
NumRecipients[i] = 0;
Diagnosed[i] = 0;
Vaccinated[i] = 0;
Circumcised[i] = 0;
NumPartners[i] = 0;
NumPartnersLastTime[i] = 0;
ResistantToVaccine[i] = 0;
LengthVaccinated[i] = 0;
VL_Start_AIDS[i] = 0.0;
Time_Start_AIDS[i] = 100000;
Time_AIDS_Death[i] = 0;
TimeLastBreakUp[i] = -100000;
TimeInAIDS[i] = 0;
AgeAIDS[i] = 0;
NumLinks[i] = 0;
DALY00 = 0.0, DALY01 = 0.0, DALY02 = 0.0, DALY03 = 0.0, DALY04 = 0.0, DALY05 = 0.0, DALY06 = 0.0, DALY07 = 0.0, DALY08 = 0.0, DALY10 = 0.0, DALY15 = 0.0, DALY20 = 0.0;
for (j=1;j<=MaxLinks;j++) {
Links[i][j] = 0;
TimeLinked[i][j] = 0;
}
}
//Call Randomize Metabolizer Classification and Proportion function
//This function assigns the metab_type or "Met" values for each agent
RandomizeMetabRate();
//Call the Usual Initial Values that assigns the remaining agent attributes
for (i=1;i<=N;i++) {
UsualInitialValues(i); // This does not set ages.:
}
}
void printArray(int arr[], int start, int stop)
{
for (int i = start; i < stop; i++)
printf("%d\n", arr[i]);
}
void RandomizeMetabRate(){
//Create an array to assign all agents into metabolizer groups
int n=5000;
int MAX_SIZE = n+1; //total population size, adding 1 to adjust array for indexing
int arr[MAX_SIZE]; //maximum size of array based on total population size
//Create an array of numbers to represent agent ids from 1 to total population
for(int arr_i=0; arr_i<=MAX_SIZE; arr_i++){
arr[arr_i]=arr_i;
}
//Calling functions to randomize array of agent ids
int num_agent= sizeof(arr)/sizeof(arr[1]);
//Randomize the agent ids
for (int i = num_agent-1; i>0; i--)
{
//Pick a random index from 1 to i
int j =(int) ((genrand_real2()*i))+1;
//Swap the original positions with the array position of j
int temp=arr[i];
arr[i]=arr[j];
arr[j]=temp;
}
//Print the array to ensure that the randomization is working
//printArray(arr, 1, num_agent);
//printf("\n");
int slow_num=(int) num_agent*slow_metab_prop;
int inter_num=(int) num_agent*inter_metab_prop;
int fast_num= (int) num_agent*fast_metab_prop;
// Re-calculate the number of agents using round() and +/-
//fast_num=num_agent-(slow_num+inter_num)
int slow_arr[slow_num];
int inter_arr[inter_num];
int fast_arr[fast_num];
//Get the arrays for the slow, intermediate and fast metabolizers
//Slow Metabolizer agent IDs
for(int slow_i=1; slow_i<slow_num+1; slow_i++)
{
slow_arr[slow_i]=arr[slow_i];
}
//Intermediate Metabolizer agent IDs
int sum_si=slow_num+inter_num;
for(int inter_i=slow_num; inter_i<sum_si+1; inter_i++)
{
inter_arr[inter_i-slow_num]=arr[inter_i];
}
//Fast Metabolizer agent IDs
int total_num=slow_num+inter_num+fast_num;
for(int fast_i=slow_num+inter_num; fast_i<total_num+1; fast_i++)
{
fast_arr[fast_i-(slow_num+inter_num)]=arr[fast_i];
}
//Create if-then statements or conditions for looping through
//agent ids by slow_arr, inter_arr, and fast_arr
//and assign a agent attribute and category
int i;
int j;
//assigning the fast metabolizers
for (j = 1; j<=fast_num; j++){
for (i = 1; i<num_agent; i++){
if (fast_arr[j]==i){
metab_type[i]=1;
}
}
}
//assigning the slow metabolizers
for (j = 1; j<=slow_num; j++){
for (i = 1; i<num_agent; i++){
if (slow_arr[j]==i){
metab_type[i]=2;
}
}
}
//assigning the intermediate metabolizers
for (j = 1; j<=inter_num; j++){
for (i = 1; i<num_agent; i++){
if (inter_arr[j]==i){
metab_type[i]=3;
}
}
}
}
//UsualInitialValues() function is assigning more agent attributes
//such as Sex, Concurrency, etc.
//double increased_duration_per_year = 50.0; // In days. If 50, duration will increase 500 days in 10 years, 2500 days (=6 years) in 50 years
void UsualInitialValues (long i) {
double Time_Concur_halves;
if (rate_stops_being_concurrent > 0) Time_Concur_halves = 1.0/rate_stops_being_concurrent;
else Time_Concur_halves = 1000000.0;
if (genrand_real2() < 0.5) {
Sex[i] = 1; // Female
} else {
Sex[i] = 2; // Male
}
if (genrand_real2() < percent_high_risk) HighRisk[i] = 1;
else HighRisk[i] = 0;
if (Sex[i] == 1) {
if (genrand_real2() < female_concurrency) Concurrent[i] = 1;
else Concurrent[i] = 0;
}
if (Sex[i] == 2) {
if (genrand_real2() < circum_prob) Circumcised[i] = 1;
if (genrand_real2() < male_concurrency) Concurrent[i] = 1;
else Concurrent[i] = 0;
}
if (Concurrent[i] == 1 && Age[i] > 45.0) {
if (genrand_real2() < ((Age[i] - 45.0)*365.0)/((Age[i] - 45.0)*365 + Time_Concur_halves)) Concurrent[i] = 0;
}
ProbDropout[i] = 2.0*genrand_real2()*prob_dropout/365.0;
NumLinks[i] = 0;
Status[i] = 0;
Diagnosed[i] = 0;
TimeDiag[i] = 10000000;
Duration[i] = genrand_real2()*2.0*MinDuration + (Age[i]-16.0)*increased_duration_per_year; // Distribution between 0 and 2*Min
if (Duration[i] < 1.0) Duration[i] = 1.0;
UnderCare[i] = 0; // Set to zero to start. This will be updated in AddInfecteds() below
Treated[i] = 0; // Assume no treatment for initial population
Time_Treated[i] = 0; // Assume no treatment for initial population
tx_days[i] = 0; // Assume no treatment for initial population
tx_days_aids[i] = 0; // Assume no treatment for initial population
ever_aids[i] = 0; // Assume no treatment for initial population
TargetedTreated[i] = 0; // Assume no treatment for initial population
}
void Births()
{
double per_day_birth_rate;
long nBirths;
double NewSusceptiblesPerDayDouble;
long NewSusceptiblesPerDay;
long i;
newbirths = 0;
if (MaxNReached ==0) {
per_day_birth_rate = BirthRate*exp(PopGrowth*time/365);
nBirths = rpois(per_day_birth_rate);
NewSusceptiblesPerDay = nBirths;
for (i=1;i<=NewSusceptiblesPerDay;i++) {
if (N < MaxN) {
N++;
newbirths++;
Age[N] = 16.0;
UsualInitialValues(N);
} else {
if (MaxNReached ==0) {
if (plt_warn==1) fprintf(WarningsFile,"Warning at time %ld: MaxN reached. No more births or reincarnation events allowed\n",time);
MaxNReached = 1;
tMaxNReached = time;
} // MaxNReached
} // else part of N < MaxN
} // for i = 1 to N
} // MaxNReached == 0
}
void Aging()
{
long i;
for (i=1; i<=N; i++) {
if (Status[i] >= 0) {
Age[i] = Age[i] + 1.0/365.0;
Duration[i] = Duration[i] + increased_duration_per_year * (1/365.0);
if (Duration[i] > MaxDuration) Duration[i] = MaxDuration;
if (Concurrent[i] == 1) {
if (genrand_real2() < rate_stops_being_concurrent) {
Concurrent[i] = 0;
}
}
}
if (Age[i] > 100) Age[i] = 100;
}
}
void CalculateLifeExpectancies()
{
long i, cur_age;
double prob_live_to[101][101];
for (cur_age = 15; cur_age <= 100; cur_age++) {
prob_live_to[cur_age][cur_age] = (1-0.5*asmr[cur_age]*365.0);
// printf("%ld : ",cur_age);
for (i=cur_age+1; i <= 99; i++) {
prob_live_to[cur_age][i] = prob_live_to[cur_age][i-1]*(1-asmr[i]*365.0);
// printf("%3.2lf ",prob_live_to[cur_age][i]);
}
prob_live_to[cur_age][100] = 0.0;
//printf("\n");
}
for (cur_age = 15; cur_age <= 100; cur_age++) {
LifeExpectancy[cur_age] = cur_age + 0.5; // Assuming prob_live_to[cur_age][cur_age+1] == 0, agent would live on average 0.5 years, hence the 0.5 term
for (i= cur_age+1; i<= 100; i++) {
LifeExpectancy[cur_age] = LifeExpectancy[cur_age] + prob_live_to[cur_age][i];
}
//printf("Life Expectancy for someone age %ld is %lf\n",cur_age,LifeExpectancy[cur_age]);
}
}
void UpdateDALYs()
{
long i;
double relevant_cost;
for (i=1; i<=N; i++) {
if (time > Start_TasP_Campaign) {
if (Status[i] == 1) {
relevant_cost =0.0;
if (Treated[i]==1) relevant_cost = cost_hiv_treated/365.0;
if (CD4[i] == 2 && Treated[i]==0) relevant_cost = cost_cd4_gt_350/365.0;
if (CD4[i] == 3 && Treated[i]==0) relevant_cost = cost_cd4_200_350/365.0;
if (CD4[i] == 4 && Treated[i]==0) relevant_cost = cost_cd4_lt_200/365.0;
DALY00 = DALY00 + relevant_cost;
DALY01 = DALY01 + relevant_cost * pow(0.99, (time-Start_TasP_Campaign)/365.0);
DALY02 = DALY02 + relevant_cost * pow(0.98, (time-Start_TasP_Campaign)/365.0);
DALY03 = DALY03 + relevant_cost * pow(0.97, (time-Start_TasP_Campaign)/365.0);
DALY05 = DALY05 + relevant_cost * pow(0.95, (time-Start_TasP_Campaign)/365.0);
DALY07 = DALY07 + relevant_cost * pow(0.93, (time-Start_TasP_Campaign)/365.0);
DALY08 = DALY08 + relevant_cost * pow(0.92, (time-Start_TasP_Campaign)/365.0);
DALY10 = DALY10 + relevant_cost * pow(0.90, (time-Start_TasP_Campaign)/365.0);
DALY15 = DALY15 + relevant_cost * pow(0.85, (time-Start_TasP_Campaign)/365.0);
DALY20 = DALY20 + relevant_cost * pow(0.80, (time-Start_TasP_Campaign)/365.0);
}
if (Status[i] == -2 & Time_AIDS_Death[i] == time) {
relevant_cost = cost_died_AIDS * (LifeExpectancy[(long) Age[i]] - Age[i]);
DALY00 = DALY00 + relevant_cost;
DALY01 = DALY01 + relevant_cost * pow(0.99, (time-Start_TasP_Campaign)/365.0);
DALY02 = DALY02 + relevant_cost * pow(0.98, (time-Start_TasP_Campaign)/365.0);
DALY03 = DALY03 + relevant_cost * pow(0.97, (time-Start_TasP_Campaign)/365.0);
DALY05 = DALY05 + relevant_cost * pow(0.95, (time-Start_TasP_Campaign)/365.0);
DALY07 = DALY07 + relevant_cost * pow(0.93, (time-Start_TasP_Campaign)/365.0);
DALY08 = DALY08 + relevant_cost * pow(0.92, (time-Start_TasP_Campaign)/365.0);
DALY10 = DALY10 + relevant_cost * pow(0.90, (time-Start_TasP_Campaign)/365.0);
DALY15 = DALY15 + relevant_cost * pow(0.85, (time-Start_TasP_Campaign)/365.0);
DALY20 = DALY20 + relevant_cost * pow(0.80, (time-Start_TasP_Campaign)/365.0);
}
}
}
}
long rpois(double lambda)
{
double L, p, u;
long k;
L = exp(-lambda);
k = 0;
p = 1.0;
do {
k = k + 1.0;
u = genrand_real2();
p = p * u;
} while (p > L);
return(k-1);
}
void InitialAgeDistribution() {
long jj, low_limit, high_limit, found;
double a, b, r, sum_age_vec;
double age_vec[101], rel_age_dist[101], cum_age_dist[101];
double randnum;
b = BirthRate; // Initial birth rate
a = 1.0/365.0; //Per day aging rate
r = PopGrowth/365.0; // pop_growth_rate_timestep;
// From WHO http://apps.who.int/gho/data/?theme=main&vid=61540 (first number is male. Downloaded: 7/2/18)
for (i=15; i<=19;i++) asmr[i] = 0.5 * (0.002 + 0.001) / 365.0;
for (i=20; i<=24;i++) asmr[i] = 0.5 * (0.002 + 0.002) / 365.0;
for (i=25; i<=29;i++) asmr[i] = 0.5 * (0.004 + 0.003) / 365.0;
for (i=30; i<=34;i++) asmr[i] = 0.5 * (0.006 + 0.005) / 365.0;
for (i=35; i<=39;i++) asmr[i] = 0.5 * (0.009 + 0.008) / 365.0;
for (i=40; i<=44;i++) asmr[i] = 0.5 * (0.011 + 0.007) / 365.0;
for (i=45; i<=49;i++) asmr[i] = 0.5 * (0.013 + 0.008) / 365.0;
for (i=50; i<=54;i++) asmr[i] = 0.5 * (0.018 + 0.009) / 365.0;
for (i=55; i<=59;i++) asmr[i] = 0.5 * (0.024 + 0.013) / 365.0;
for (i=60; i<=64;i++) asmr[i] = 0.5 * (0.037 + 0.019) / 365.0;
for (i=65; i<=69;i++) asmr[i] = 0.5 * (0.054 + 0.028) / 365.0;
for (i=70; i<=74;i++) asmr[i] = 0.5 * (0.077 + 0.042) / 365.0;
for (i=75; i<=80;i++) asmr[i] = 0.5 * (0.108 + 0.062) / 365.0;
for (i=80; i<=85;i++) asmr[i] = 0.5 * (0.150 + 0.101) / 365.0;
for (i=85; i<=99;i++) asmr[i] = 0.5 * (0.237 + 0.184) / 365.0;
asmr[100] = 1.0;
/* female + male
for (i=16; i<=19;i++) asmr[i] = 0.5 * (0.0013 + 0.0018) / 365.0;
for (i=20; i<=24;i++) asmr[i] = 0.5 * (0.0035 + 0.0039) / 365.0;
for (i=25; i<=29;i++) asmr[i] = 0.5 * (0.0072 + 0.0071) / 365.0;
for (i=30; i<=34;i++) asmr[i] = 0.5 * (0.0113 + 0.0117) / 365.0;
for (i=35; i<=39;i++) asmr[i] = 0.5 * (0.0130 + 0.0157) / 365.0;
for (i=40; i<=44;i++) asmr[i] = 0.5 * (0.0129 + 0.0185) / 365.0;
for (i=45; i<=49;i++) asmr[i] = 0.5 * (0.0127 + 0.0197) / 365.0;
for (i=50; i<=54;i++) asmr[i] = 0.5 * (0.0125 + 0.0197) / 365.0;
for (i=55; i<=59;i++) asmr[i] = 0.5 * (0.0127 + 0.0219) / 365.0;
for (i=60; i<=64;i++) asmr[i] = 0.5 * (0.0194 + 0.0325) / 365.0;
for (i=65; i<=69;i++) asmr[i] = 0.5 * (0.0269 + 0.0441) / 365.0;
for (i=70; i<=74;i++) asmr[i] = 0.5 * (0.0379 + 0.0582) / 365.0;
for (i=75; i<=80;i++) asmr[i] = 0.5 * (0.0563 + 0.0815) / 365.0;
for (i=80; i<=99;i++) asmr[i] = 0.5 * (0.1403 + 0.1629) / 365.0;
asmr[100] = 1.0;
*/
/* From Evonet
asmr =c(0.0013, 0.0013, 0.0013, 0.0013, 16-19 c
0.0035, 0.0035, 0.0035, 0.0035, 0.0035, 20-24 c
0.0072, 0.0072, 0.0072, 0.0072, 0.0072, 25-29 c
0.0113, 0.0113, 0.0113, 0.0113, 0.0113, 30-34 c
0.0130, 0.0130, 0.0130, 0.0130, 0.0130, 35-39 c
0.0129, 0.0129, 0.0129, 0.0129, 0.0129, 40-44 c
0.0127, 0.0127, 0.0127, 0.0127, 0.0127, 45-49 c
0.0125, 0.0125, 0.0125, 0.0125, 0.0125, 50-54 c
0.0127, 0.0127, 0.0127, 0.0127, 0.0127, 55-59 c
0.0194, 0.0194, 0.0194, 0.0194, 0.0194, 60-64 c
0.0269, 0.0269, 0.0269, 0.0269, 0.0269, 65-69 c
0.0379, 0.0379, 0.0379, 0.0379, 0.0379, 70-74 c
0.0563, 0.0563, 0.0563, 0.0563, 0.0563, 75-79 c
0.1403, 0.1403, 0.1403, 0.1403, 0.1403, 80-84 c
0.1403, 0.1403, 0.1403, 0.1403, 0.1403, 85-89 c
0.1403, 0.1403, 0.1403, 0.1403, 0.1403, 90-94 c
0.1403, 0.1403, 0.1403, 0.1403, 0.1403, 95-99 c
0.1403 100
asmr =c(0.0018, 0.0018, 0.0018, 0.0018,
0.0039, 0.0039, 0.0039, 0.0039, 0.0039,
0.0071, 0.0071, 0.0071, 0.0071, 0.0071,
0.0117, 0.0117, 0.0117, 0.0117, 0.0117,
0.0157, 0.0157, 0.0157, 0.0157, 0.0157,
0.0185, 0.0185, 0.0185, 0.0185, 0.0185,
0.0197, 0.0197, 0.0197, 0.0197, 0.0197,
0.0197, 0.0197, 0.0197, 0.0197, 0.0197,
0.0219, 0.0219, 0.0219, 0.0219, 0.0219,
0.0325, 0.0325, 0.0325, 0.0325, 0.0325,
0.0441, 0.0441, 0.0441, 0.0441, 0.0441,
0.0582, 0.0582, 0.0582, 0.0582, 0.0582,
0.0815, 0.0815, 0.0815, 0.0815, 0.0815,
0.1629, 0.1629, 0.1629, 0.1629, 0.1629,
0.1629, 0.1629, 0.1629, 0.1629, 0.1629,
0.1629, 0.1629, 0.1629, 0.1629, 0.1629,
0.1629, 0.1629, 0.1629, 0.1629, 0.1629,
0.1629),age_range=c(16,100))
*/
for (jj = 1; jj <= 15; jj++) {
age_vec[jj] = 0.0;
}
age_vec[16] = b/(a + r + asmr[16]);
sum_age_vec = age_vec[16];
age_vec[100] = 0.0; // Hard code 100% death at age 100
for (jj = 17; jj<= 99; jj++) {
age_vec[jj] = age_vec[jj-1] * a/(a + r + asmr[jj]);
sum_age_vec = sum_age_vec + age_vec[jj];
}
for (jj = 1; jj<= 100; jj++) {
rel_age_dist[jj] = age_vec[jj]/sum_age_vec;
}
//printf("age \t asmr \t age_vec \t Rel_prob \t CumProb \t(sum_age_vec = %lf, a = %lf, r = %lf)\n",sum_age_vec,a,r);
cum_age_dist[1] = 0.0;
for (jj = 2; jj<= 100; jj++) {
cum_age_dist[jj] = rel_age_dist[jj] + cum_age_dist[jj-1];
//printf("%ld \t %lf \t %lf \t %lf \t %lf (sum_age_vec = %lf)\n",jj,asmr[jj],age_vec[jj],rel_age_dist[jj],cum_age_dist[jj],sum_age_vec);
}
for (i=1;i<=N0;i++) {
randnum = genrand_real2();
found = 0;
//printf("Determining age for agent %ld (of %ld), randnum = %lf\n",i,N0,randnum);
for (jj=16; jj<= 100;jj++) {
if ( (randnum <= cum_age_dist[jj]) && (found==0)) {
found = 1;
Age[i] = ((double) jj) + genrand_real2();
//printf(" agent %ld assigned to age %lf\n",i,Age[i]);
}
}
if (found == 0) {
printf("Problem in InitialAgeDistribution: No age assigned to agent %ld\n.",i);
printf("Type ctrl-c to stop: ");
scanf("%s",junk);
}
}
}
/*
Code that worked in stand-alone script
# b = (a + r + d[1])*N[1]
# for (age in 2: 83) {
# N[age] <- N[age-1]*a/(a+r+d[age])
# }
# where N1 = lowest age class (=18 in most of our models)
# a = aging rate
# di = death rate of persons of age i
# r = rate of growth in the birth rate
# b = initial birth rate (rate of entry of persons of "age 1")
# Equations
# dN1/dt = b*exp(r*t) - a*N1 - d1*N1
# dN2/dt = a*N1 - a*N2 - d2*N2
# dNi/dt = a*Ni-1 - a*Ni - di*Ni
# Equation only works when population is at steady state, meaning that b = (a+r+d[1])*N[1]
# In other words "baseline_input_exp_growth" (=b) needs to be tuned so that rate of initial input into the youngest
# age class equals the initial death and ageing rates of the youngest age class
b <- baseline_input_exp_growth # Initial birth rate
a <- 1/365 # Per day aging rate
r <- pop_growth_rate_timestep
d_f <- mort_per_timestep_female # Per day death rate (assume females and males the same)
age_vec <- c(min_age:(max_age-1)) # Set up the vector (arbitrary numbers for now)
age_vec[1] <- b/(a + r + d_f[min_age])
low_limit <- min_age +1
upper_limit <- max_age -1
for (age in low_limit:upper_limit) {
age_index <- age - min_age +1 # Youngest age class (e.g., 16) is represented as 1 in our model
age_vec[age_index] <- age_vec[age_index-1]*a/(a +r + d_f[age_index])
}
age_vec[1] <- age_vec[1]/2 # Reduce youngest class by 50% to reflect fact that the average age of newly entering agents is min_age + 0.5.
final_age_dist <- age_vec/sum(age_vec)
*/
void AddInfecteds()
{
long i,ii, Reject_i, escape_counter = 0;
double rand_num;
Infected = Infected0;
for (ii=1;ii<=Infected0;ii++) {
Reject_i = 0; escape_counter = 0;
do {
Reject_i = 0; // Assume to be okay until shown otherwise
i = N * genrand_real2() + 1;
if (Status[i] != 0) Reject_i = 1;
rand_num = genrand_real2();
escape_counter++;
if (escape_counter > 100) {
printf("Trouble in AddInfecteds: unable to seed population\n");
printf("ii = %ld, i = %ld, N = %ld, Status[%ld] = %d, Age[%ld] = %lf\n",ii,i,N,i,Status[i],i,Age[i]);
printf("Reject_i = %ld, escape_counter = %ld, rand_num=%lf\n",Reject_i,escape_counter,rand_num);
printf("Type ctrl-C to stop:"); scanf("%s",junk);
}
} while (Reject_i == 1);
if (genrand_real2() < PercentResistantToVaccine) {
ResistantToVaccine[i] = 1;
} else {
ResistantToVaccine[i] = 0;
}
if (ii==1) i = 1; // Ensure that the first infected person is agent 1
ViralContribToLogSP0[i] = norm_rand(AverageLogSP0, sqrt(H*VarianceLogSP0));
EnvirContribToLogSP0[i] = norm_rand(0, sqrt((1-H)*VarianceLogSP0));
LogSetPoint[i] = ViralContribToLogSP0[i] + EnvirContribToLogSP0[i];
SpvlCat[i]=SPVL_Category(LogSetPoint[i] );
CD4[i]=initialCD4(SpvlCat[i]);
CD4_initial_value[i]=CD4[i];
//adding exponential distribution for time in each cd4 category
//can tighten this up once it gets working....
CD4_TimeToAIDS_exp[i][1]=log(genrand_real2()) / (-1.0/CD4_lookup[SpvlCat[i]][1]) ;
CD4_TimeToAIDS_exp[i][2]=log(genrand_real2()) / (-1.0/CD4_lookup[SpvlCat[i]][2]) ;
CD4_TimeToAIDS_exp[i][3]=log(genrand_real2()) / (-1.0/CD4_lookup[SpvlCat[i]][3]) ;
CD4_TimeToAIDS_exp[i][4]=log(genrand_real2()) / (-1.0/CD4_lookup[SpvlCat[i]][4]) ;
if(CD4_Exp_Flag==1) {
double ll=0.0;
int kk;
for(kk=CD4[i];kk<=3;kk++) {
ll = ll + CD4_TimeToAIDS_exp[i][kk];
}
CD4_TimeToAIDS[i]=ll;
}
//printf("%d----%f\n",i,CD4_TimeToAIDS_exp[i][1]);
//printf("%d -- %d -- %d\n",i,SpvlCat[i], CD4[i]);
//printf("-- %d --", CD4[i]);
SetPoint[i] = pow(10.0,LogSetPoint[i]);
d_acute[i] = 0.4; // Placeholder until exact value is calculated in UpdateViralLoads()
Status[i] = 1;
Generation[i] = 1;
printf("Agent %ld assigned to status %d at time %ld\n",i,Status[i],time);
if (genrand_real2() < percent_under_care) {
UnderCare[i] = 1;
} else {
UnderCare[i] = 0;
}
Time_Inf[i] = time;
CD4time[i]=-Time_Inf[i] ; // What does this mean???
TimeInAIDS[i]= 0;
V[i] = V0;
I[i] = V0; // Simplistic assumption values of p (100) and c (5) have been HARD CODED
M[i] = 0.0;
L[i] = 0.0;
V_vec[i][0][0][0][0][0] = V[i];
I_vec[i][0][0][0][0][0] = I[i];
M_vec[i][0][0][0][0][0] = 0.0;
L_vec[i][0][0][0][0][0] = 0.0;
//assign agents to have viral mutations at the beginning of the simulation
if(genrand_real2() < prop_mut_locus1){ //locus position 1 is K65R
mut_locus1[i] = 1;
V_vec[i][0][0][0][0][0] = 0;
V_vec[i][1][0][0][0][0]= V0;
I_vec[i][1][0][0][0][0]= V0;
I_vec[i][0][0][0][0][0]= 0;
}
if(genrand_real2() < prop_mut_locus2){ //locus position 2 is M184V
mut_locus2[i] = 1;
V_vec[i][0][0][0][0][0] = 0;
V_vec[i][0][1][0][0][0]= V0;
I_vec[i][0][1][0][0][0]=V0;
I_vec[i][0][0][0][0][0]= 0;
}
if(genrand_real2() < prop_mut_locus3){ //locus position 3 is K103N
mut_locus3[i] = 1;
V_vec[i][0][0][0][0][0] = 0;
V_vec[i][0][0][1][0][0]= V0;
I_vec[i][0][0][1][0][0]=V0;
I_vec[i][0][0][0][0][0]= 0;
}
if(genrand_real2() < prop_mut_locus4){ //locus position 4 is GenericTDF
mut_locus4[i] = 1;
V_vec[i][0][0][0][0][0] = 0;
V_vec[i][1][0][0][1][0]= V0;