-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathspseirsim.cpp
More file actions
3023 lines (2458 loc) · 104 KB
/
spseirsim.cpp
File metadata and controls
3023 lines (2458 loc) · 104 KB
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 <eutils/emain.h>
#include <eutils/ernd.h>
#include <gsl/gsl_cdf.h>
#include <eutils/eparser.h>
#include <eutils/eparserinterpreter.h>
#include <eutils/etable.h>
#include <eutils/vector3.h>
#include <eutils/ethread.h>
#include <eutils/edaemon.h>
#include <eutils/esystem.h>
#include <deque>
#include "videoenc.h"
#include <geotiff.h>
#include <xtiffio.h>
#include <tiffio.h>
#include <geo_normalize.h>
#include <geo_simpletags.h>
#include <geovalues.h>
#include <shapefil.h>
#include <cairo.h>
cairo_surface_t *surface=0x00;
cairo_t *cr=0x00;
using namespace std;
const int vwidth=1024;
const int vheight=768;
struct scounts {
int allCases=0;
int allICU=0;
int allNonICU=0;
int allN=0;
int allD=0;
};
ostream& operator<<(ostream& os,const scounts& sc){
os << "allCases: " << sc.allCases << " allD: " << sc.allD << " allN: " << sc.allN;
return(os);
}
double xlonMin=180.0,xlonMax=-180.0;
double ylatMin=90.0,ylatMax=-90.0;
typedef ebasicarray<uint32_t> euintarray;
// cannot use MIN or MAX macros when using random number generator in the arguments!
inline int maxint(int a,int b) { return(a>b?a:b); }
inline int minint(int a,int b) { return(a<b?a:b); }
void multinomial(int count,const edoublearray& w,euintarray& res,ernd& r)
{
gsl_ran_multinomial(r.grng, w.size(), count, &w[0], &res[0]);
}
int binomial(int n,double p,const ernd& r)
{
if (p <= 0.0) return 0.0;
return gsl_ran_binomial(r.grng, p, n);
}
template<class T>
void permute(T& arr,int s,int l,ernd& r){
for (int i=0; i<l-1; ++i){
int ir=i+r.uniformint(l-i);
if (ir==i) continue;
swap(arr[s+i],arr[s+ir]);
}
}
edoublearray delay_gamma(double mu,double shape,double tmax,double tstep);
class rgamma
{
public:
double a;
double b;
int tmax;
double tstep;
rgamma();
rgamma(double a,double b,int tmax,double tstep);
int operator()(ernd& r);
};
rgamma::rgamma():a(1.0),b(1.0),tmax(1.0),tstep(1.0){}
rgamma::rgamma(double mu,double shape,int _tmax,double _tstep): tmax(_tmax),tstep(_tstep) {
double scale=mu/shape;
a=shape; b=scale;
}
int rgamma::operator()(ernd& r)
{
int tmpr=int(gsl_ran_gamma(r.grng,a,b)/tstep+tstep/2.0);
if (tmpr>=tmax) tmpr=tmax-1;
return(tmpr);
}
struct shousehold {
uint8_t size;
uint8_t hstind;
uint8_t E;
uint8_t Ia;
uint8_t Ip;
uint8_t Is;
uint8_t hage;
uint8_t group;
int gridpos;
int ageind;
};
struct sevent {
int hhid;
uint8_t transition;
uint8_t hi;
};
struct sindiv {
uint8_t age=0;
uint8_t state=0;
};
class evqueuelist;
struct ssimstate;
struct sthreadState;
/*
class elevel
{
eintarray *inds;
unsigned int s;
unsigned int e;
eintarray li;
public:
elevel(eintarray *_inds,unsigned int _s, unsigned int _e): inds(_inds),s(_s),e(_e) { }
unsigned int swapleft(unsigned int p,unsigned int l) {
swap(&inds[s+li[l]],&inds[s+p]); // swap element with position of first element in the "level"
p=li[l]; // update position of element
++li[l]; // increase level start to exclude the element we just move there
}
unsigned int swap(unsigned int p,unsigned int ol,unsigned int nl) {
for (;ol>nl; --ol)
p=swapleft(p,ol);
return(p);
}
};
*/
class evqueuelist
{
int ip;
earray<deque<sevent> > eventarr;
euintarray tmpres;
public:
evqueuelist();
deque<sevent>& step();
void add(ssimstate& st,sthreadState& ths,int hpos,uint8_t hgroup,uint8_t hhsize,uint8_t hhexp,euintarray& count,const edoublearray& evdist,ernd& r,int ntransition=0);
void add(ssimstate& st,sevent& ev,int newt);
void resize(int newsize);
};
const int ngroups=2;
const int maxhousesize=6+1; //
const int nlevels=maxhousesize*(maxhousesize+1)/2+maxhousesize;
struct sgrid
{
int E;
int N[ngroups];
int Ia[ngroups];
int Ip[ngroups];
int Is[ngroups];
int hhIa[ngroups][nlevels];
int hhIp[ngroups][nlevels];
int hhIs[ngroups][nlevels];
};
void addCounts(scounts& dst,scounts& src)
{
dst.allCases+=src.allCases;
dst.allICU+=src.allICU;
dst.allNonICU+=src.allNonICU;
dst.allD+=src.allD;
}
void addReset(ebasicarray<scounts>& dest,ebasicarray<scounts>& src)
{
ldieif(dest.size()!=src.size(),"uninitialized src or dest?");
for (int i=0; i<dest.size(); ++i){
addCounts(dest[i],src[i]);
src[i].allCases=0;
src[i].allICU=0;
src[i].allNonICU=0;
src[i].allD=0;
}
}
typedef void (*threadFunc_t)(ssimstate&,sthreadState&,int,int);
struct sthreadState {
ernd r;
// int tI=0;
// int tN=0;
evqueuelist evqueue;
int allE=0;
int allCases=0;
ebasicarray<scounts> shapeCounts;
ebasicarray<scounts> ageCounts;
int Ip[ngroups]={0,0};
int Is[ngroups]={0,0};
int Ia[ngroups]={0,0};
int allICU=0;
int allNonICU=0;
int allD=0;
};
struct ssimstate {
double R0=2.7; // R0day is R0 divided by number of days of infectiousness to obtain rate per day
double rSA=0.66; // ratio of Symptomatic/Asymptomatic
// const double iIa=0.5; // infectiosity of asymptomatic
// const double iIp=1.0; // infectiosity of presymptomatic
// const double iIs=1.0; // infectiosity of presymptomatic
const double tstep=0.25;
edoublearray dE; // 4 days delay
edoublearray dIp; // 1.5 days non-symptomatic
edoublearray dIs; // 3.5 days as symptomatic
edoublearray dIa; // 5 days infectious as asymptomatic
edoublearray dHtoicu; // 7 days to go toicu state
edoublearray dHtononicu; // 7 days to go tononicu state
edoublearray dHicu; // 10 days in ICU
edoublearray dHnonicu; // 8 days in nonICU
edoublearray dIpD; // deaths occuring 22 days after symptoms
rgamma rIp;
rgamma rIs;
rgamma rIa;
rgamma rToicu;
rgamma rTononicu;
rgamma rInicu;
rgamma rInnonicu;
rgamma rIpDeath;
edoublearray icu_symp;
edoublearray nonicu_symp;
edoublearray death_icu;
edoublearray death_nonicu;
// const int ngroups=2; // defined as global now
ebasicarray<SHPObject*> shapes;
ebasicarray<uint8_t> gridMask;
int spGridW=0;
int spGridH=0;
int spGridSize=0;
ebasicarray<sgrid> spGrid;
ebasicarray<sindiv> popindiv;
ebasicarray<shousehold> households;
eintarray hhLevels2;
eintarray hhLevelsBegin;
int grow=0;
int arow=0;
// earray<earray<deque<int> > > hhLevels;
// earray<eintarray> hhIa,hhIp,hhIs;
int allE=0;
int allCases=0;
int N[ngroups]={0,0};
int Ip[ngroups]={0,0};
int Is[ngroups]={0,0};
int Ia[ngroups]={0,0};
int allICU=0;
int allNonICU=0;
int allD=0;
ebasicarray<scounts> shapeCounts;
ebasicarray<scounts> ageCounts;
edoublearray localIprob;
edoublearray travelKernel;
int travelKernelSize=0;
// evqueuelist evqueue;
double soldr=1.0;
double smr=1.0;
double finter=1.0;
double fintra=1.0;
double fglobal=0.001;
double flocal=1.0;
double R0day=R0/5.0;
int householdSize=7;
earrayof<int,int> iseedArr;
// int fseed=10;
// int seedGrid=0;
emutex mutex;
econdsig stateSignal;
econdsig doneSignal;
int nthreads=4;
threadFunc_t threadFunc=0x00;
earray<sthreadState> threadStates;
int threadI=0;
int threadDone=0;
eintarray gridShuffle; // needed to load balance threads
};
void processEvents(ssimstate& st,sthreadState& ths,deque<sevent>& evs,ernd& r)
{
for (int i=0; i<evs.size(); ++i){
sevent &e(evs[i]);
shousehold &hs(st.households[e.hhid]);
sindiv &hindiv(st.popindiv[hs.ageind+e.hi]);
uint8_t hl=hs.size*int(hs.size+1)/2+(hs.size-hs.E);
sgrid &g(st.spGrid[hs.gridpos]);
switch(e.transition){
case 0:{ // end of E state
if (r.uniform()<st.rSA){
// E -> Ip
++ths.Ip[hs.group];
++g.Ip[hs.group];
++g.hhIp[hs.group][hl];
++hs.Ip;
hindiv.state=2; // presymptomatic
e.transition=2;
ths.evqueue.add(st,e,st.rIp(r));
}else{
// E -> Ia
++ths.Ia[hs.group];
++g.Ia[hs.group];
++g.hhIa[hs.group][hl];
++hs.Ia;
hindiv.state=1; // asymptomatic
e.transition=1;
ths.evqueue.add(st,e,st.rIa(r));
}
break;
}
case 1:{
// Ia ->
hindiv.state=10; // recovered
--ths.Ia[hs.group];
--g.Ia[hs.group];
--g.hhIa[hs.group][hl];
--hs.Ia;
break;
}
case 2:{
// Ip -> Is , Ip -> toicu , Ip -> tononicu , Ip -> death
--ths.Ip[hs.group];
--g.Ip[hs.group];
--g.hhIp[hs.group][hl];
--hs.Ip;
++ths.allCases;
++ths.ageCounts[hindiv.age].allCases;
++ths.shapeCounts[st.gridMask[hs.gridpos]].allCases;
++ths.Is[hs.group];
++g.Is[hs.group];
++g.hhIs[hs.group][hl];
++hs.Is;
hindiv.state=3; // symptomatic
double rf=r.uniform();
rf-=st.icu_symp[hindiv.age];
if (rf<0.0){ // going to icu
if (r.uniform()<st.death_icu[hindiv.age]){
hindiv.state=20; // will be fatal
e.transition=6; // death
ths.evqueue.add(st,e,st.rIpDeath(r));
}
e.transition=4; //toicu
ths.evqueue.add(st,e,st.rToicu(r));
}else{
rf-=st.nonicu_symp[hindiv.age];
if (rf<0.0){ // going to non icu
if (r.uniform()<st.death_nonicu[hindiv.age]){
hindiv.state=20; // will be fatal
e.transition=6; // death
ths.evqueue.add(st,e,st.rIpDeath(r));
}
e.transition=5; //tononicu
ths.evqueue.add(st,e,st.rTononicu(r));
}else{
e.transition=3; // recover from symptomatic
ths.evqueue.add(st,e,st.rIs(r));
}
}
break;
}
case 3:{
// Ip ->
hindiv.state=10; // recovered
--ths.Is[hs.group];
--g.Is[hs.group];
--g.hhIs[hs.group][hl];
--hs.Is;
break;
}
case 4:{
// TODO: remove individual from population (stop being infective) once he joins ICU or nonICU, requires keeping track of individual to make sure he is not removed twice, once from Is -> and another time from the ICU, nonICU or death cases, maybe use age value as flag when -1 means he already was removed
if (hindiv.state==30) // death occurred before NonICU
break;
// toicu -> icu
--ths.Is[hs.group];
--g.Is[hs.group];
--g.hhIs[hs.group][hl];
--hs.Is;
++ths.allICU;
++ths.ageCounts[hindiv.age].allICU;
++ths.shapeCounts[st.gridMask[hs.gridpos]].allICU;
if (hindiv.state!=20){ // if state == 20 then individual will die and will not leave ICU
e.transition=7; // recover from ICU
ths.evqueue.add(st,e,st.rInicu(r));
}
hindiv.state=7; // inicu
break;
}
case 5:{
// tononicu -> nonicu
if (hindiv.state==30) // death occurred before NonICU
break;
--ths.Is[hs.group];
--g.Is[hs.group];
--g.hhIs[hs.group][hl];
--hs.Is;
++ths.allNonICU;
++ths.ageCounts[hindiv.age].allNonICU;
++ths.shapeCounts[st.gridMask[hs.gridpos]].allNonICU;
if (hindiv.state!=20){ // if state == 20 then individual will die and will not leave ICU
e.transition=8; // recover from nonICU
ths.evqueue.add(st,e,st.rInnonicu(r));
}
hindiv.state=8; // in nonicu
break;
}
case 6:{
// death
++ths.allD;
++ths.ageCounts[hindiv.age].allD;
++ths.shapeCounts[st.gridMask[hs.gridpos]].allD;
switch (hindiv.state){
case 7:
--ths.allICU;
--ths.shapeCounts[st.gridMask[hs.gridpos]].allICU;
break;
case 8:
--ths.allNonICU;
--ths.shapeCounts[st.gridMask[hs.gridpos]].allNonICU;
break;
case 20:
// death occurred before ICU or NonICU
--ths.Is[hs.group];
--g.Is[hs.group];
--g.hhIs[hs.group][hl];
--hs.Is;
break;
default:
ldie("state not expected");
break;
}
hindiv.state=30;
break;
}
case 7:{
// icu ->
hindiv.state=10; // recovered
--ths.allICU;
--ths.shapeCounts[st.gridMask[hs.gridpos]].allICU;
break;
}
case 8:{
// nonicu ->
hindiv.state=10; // recovered
--ths.allNonICU;
--ths.shapeCounts[st.gridMask[hs.gridpos]].allNonICU;
break;
}
}
}
evs.clear();
}
evqueuelist::evqueuelist(): ip(0) {}
void evqueuelist::add(ssimstate &st,sevent& ev,int newt)
{
// tmpres.init(evdist.size(),0);
// multinomial(1,evdist,tmpres,r);
// for (int i=0; i<tmpres.size()-1; ++i){
// if (tmpres[i]){
// eventarr[(ip+i)%eventarr.size()].push_back(ev);
// break;
// }
// }
ldieif(newt>eventarr.size(),"should not happen");
eventarr[(ip+newt)%eventarr.size()].push_back(ev);
}
void evqueuelist::add(ssimstate &st,sthreadState& ths,int hpos,uint8_t hgroup,uint8_t hhsize,uint8_t hhexp,euintarray& count,const edoublearray& evdist,ernd& r,int ntransition)
{
sevent ev;
uint8_t hl=hhsize*int(hhsize+1)/2+(hhsize-hhexp);
// uint8_t hhstnewlevel=hhsize*(hhsize+1)/2+(hhsize-(hhexp+1)); // increasing exposed count in household
for (int ti=1; ti<count.size(); ++ti)
ths.allE+=count[ti]*ti;
ev.transition=ntransition; // default: 0 = end of E state
sgrid &g(st.spGrid[hpos]);
for (int ic=1; ic<count.size(); ++ic){
if (count[ic]==0) continue;
uint8_t hlnew=hl-ic; // transition level depends on number of new infections in household
tmpres.init(evdist.size(),0);
multinomial(count[ic],evdist,tmpres,r);
for (int i=0; i<tmpres.size(); ++i){
for (int j=0; j<tmpres[i]; ++j){
// ldieif(st.hhLevels[hage][hl].size()==0,"hhLevels empty!");
unsigned int hlb=st.hhLevelsBegin[hpos*st.grow + hgroup*st.arow + hl];
unsigned int hlcount=st.hhLevelsBegin[hpos*st.grow + hgroup*st.arow + hl+1]-st.hhLevelsBegin[hpos*st.grow + hgroup*st.arow + hl];
lddieif(hlcount==0,"hhLevels empty!");
// choose a random household
int ri=r.uniformint(hlcount);
unsigned int hhid=st.hhLevels2[hlb+ri];
ev.hhid=hhid;
shousehold &hst(st.households[hhid]);
if (hst.gridpos != hpos || hst.size != hhsize || hst.E != hhexp || hst.group != hgroup){
printf("wrong transition: hst.gridpos: %i hpos: %i hst.size: %hhi hhsize: %hhi hst.E: %hhi hhexp: %hhi hhstlevel: %hhi\n",hst.gridpos,hpos,hst.size,hhsize,hst.E,hhexp,hl);
exit(0);
}
if (hst.E>hst.size){
printf("household exposed larger than household size: hst.E: %hhi hst.size: %hhi hhsize: %hhi hhexp: %hhi ic: %i count.size(): %i\n",hst.E,hst.size,hhsize,hhexp,ic,count.size());
exit(0);
}
// remove Ia, Ip, and Is counts from previous level
g.hhIa[hgroup][hl]-=hst.Ia;
g.hhIp[hgroup][hl]-=hst.Ip;
g.hhIs[hgroup][hl]-=hst.Is;
// this code is implemented like this for efficiency reasons
// households indices in the different levels are all stored in a single large array grouped by levels
// the start of each subarray level is stored in the hhLevelsBegin array
// to move a household to another level, I just swap the household to the beginning of the level subarray
// and then increment the beginning of that subarray level, effectively removing it from the current level to the previous one
// each level corresponds to a different number of exposed individuals in the households
for (int l=0; l<ic; ++l){
// move household chosen to beginning of level
hlb=st.hhLevelsBegin[hpos*st.grow + hgroup*st.arow + hl-l];
if (ri>0)
swap(st.hhLevels2[hlb],st.hhLevels2[hlb+ri]);
st.households[st.hhLevels2[hlb+ri]].hstind=ri; // update position of not chosen but swapped household
++st.hhLevelsBegin[hpos*st.grow + hgroup*st.arow + hl-l];
ri=st.hhLevelsBegin[hpos*st.grow + hgroup*st.arow + hl-l]-st.hhLevelsBegin[hpos*st.grow + hgroup*st.arow + hl-l-1]-1;
hst.hstind=ri; // update position of moved household, to end of lower level
}
// add Ia, Ip, and Is counts to new level
g.hhIa[hgroup][hlnew]+=hst.Ia;
g.hhIp[hgroup][hlnew]+=hst.Ip;
g.hhIs[hgroup][hlnew]+=hst.Is;
for (int l=0; l<ic; ++l){ // queue one event per person to track age of exposed individual
ev.hi=hst.E+l;
eventarr[(ip+i)%eventarr.size()].push_back(ev);
}
hst.E+=ic;
g.E+=ic;
}
}
}
}
void evqueuelist::resize(int newsize)
{
eventarr.init(newsize);
}
deque<sevent>& evqueuelist::step()
{
deque<sevent>& tmpevarr(eventarr[ip]);
ip=(ip+1)%eventarr.size();
return(tmpevarr);
}
int samplehh(eintarray& arr,ebasicarray<shousehold>& hhs,int s,ernd& r){
int scount;
int i;
int tries=0;
do{
scount=0;
for (i=0; i<arr.size() && scount < s; ++i){
int ir=r.uniformint(arr.size()-i);
scount+=hhs[arr[ir]].size;
if (ir!=arr.size()-1-i)
swap(arr[ir],arr[arr.size()-1-i]);
}
++tries;
} while (scount!=s && tries<50);
return(i);
}
edoublearray delay_gamma(double mu,double shape,double tmax,double tstep)
{
double scale=mu/shape;
edoublearray tmp;
tmp.reserve(int(tmax/tstep));
double sumx=0.0;
for (int i=0; i*tstep<tmax; ++i){
tmp.add(gsl_cdf_gamma_P(i*tstep+tstep/2.0,shape,scale)-gsl_cdf_gamma_P(MAX(0.0,i*tstep-tstep/2.0),shape,scale));
sumx+=tmp[i];
}
for (int i=0; i<tmp.size(); ++i)
tmp[i]/=sumx;
return(tmp);
}
void colorRect(uint8_t *frameRaw,int xb,int yb,int xe,int ye,uint32_t color,int prow){
if (xb>xe) swap(xb,xe);
if (yb>ye) swap(yb,ye);
for (int ty=yb; ty<ye; ++ty){
for (int tx=xb; tx<xe; ++tx){
int p=ty*prow*4+tx*4;
frameRaw[p]=0xFF&color;
frameRaw[p+1]=0xFF&(color>>8);
frameRaw[p+2]=0xFF&(color>>16);
}
}
}
inline double clamp(double vmin,double vmax,double v){ v=(v<vmin?vmin:v); return(v>vmax?vmax:v); }
//SHPObject *psShape=0x00;
static int GTIFReportACorner( GTIF *gtif, GTIFDefn *defn, FILE * fp_out, const char * corner_name, double x, double y)
{
// FILE *fp_out=stdout;
double x_saved, y_saved;
/* Try to transform the coordinate into PCS space */
if ( !GTIFImageToPCS( gtif, &x, &y ) )
return -1;
x_saved = x;
y_saved = y;
fprintf( fp_out, "%-13s ", corner_name );
if( defn->Model == ModelTypeGeographic )
{
fprintf( fp_out, "dec (%.7f,", x );
fprintf( fp_out, "%.7f)\n", y );
fprintf( fp_out, "(%s,", GTIFDecToDMS( x, "Long", 2 ) );
fprintf( fp_out, "%s)\n", GTIFDecToDMS( y, "Lat", 2 ) );
}
else
{
fprintf( fp_out, " dec (%12.3f,%12.3f)", x, y );
if ( GTIFProj4ToLatLong( defn, 1, &x, &y ) ) {
fprintf( fp_out, " (%.7f,", x );
fprintf( fp_out, "%.7f)", y );
}
else
{
const char* pszLong = GTIFDecToDMS( x, "Long", 2 );
if ( pszLong[0] == 0 ){
fprintf( fp_out, " (invalid)" );
} else {
fprintf( fp_out, " (%s,", pszLong );
fprintf( fp_out, "%s)", GTIFDecToDMS( y, "Lat", 2 ) );
}
}
}
fprintf( fp_out, "\n" );
// if( inv_flag && GTIFPCSToImage( gtif, &x_saved, &y_saved ) ){
// fprintf( fp_out, " inverse (%11.3f,%11.3f)\n", x_saved, y_saved );
// }
return 0;
}
int validate=0;
GTIF *gtif=0x00;
float nodataval=0.0;
int rmaxpop=0.0;
int rpopsize=0;
int rwidth=0;
int rheight=0;
int imaxpop=-1;
eintarray popCounts;
inline int readPopDensR(uint32_t ix,uint32_t iy){
// return(raster[iy*rwidth + ix]==nodataval?0.0:raster[iy*rwidth + ix]); // nodata values are already zeroed
return(popCounts[iy*rwidth + ix]);
}
uint32_t v3_col(const evector3& v){ return(0xFFu&uint32_t(v.x*0xFFu) | 0xFF00u&uint32_t(v.y*0xFF00u) | 0xFF0000u&uint32_t(v.z*0xFF0000)); }
uint32_t tricolorint(const evector3& col1,const evector3& col2,const evector3& col3,double p1,double p2){
evector3 col12=col1*(1.0-p1)+col2*p1;
return(v3_col((col1*(1.0-p1)+col2*p1)*(1.0-p2)+col3*p2));
}
evector3 tricolor(const evector3& col1,const evector3& col2,const evector3& col3,double p1,double p2){
evector3 col12=col1*(1.0-p1)+col2*p1;
return((col1*(1.0-p1)+col2*p1)*(1.0-p2)+col3*p2);
}
double plonMin=0.0;
double plonMax=0.0;
double lonRef=0.0;
double proj(double lon,double lat,double reflon){
return(cos(M_PI*lat/180.0)*(lon-reflon));
}
double scale=1.0;
double xpos=0.0,ypos=0.0;
double revproj(double lon,double lat,double reflon){
return(lon/cos(M_PI*lat/180.0)+reflon + plonMin);
}
void cairoDrawShape(cairo_t *cr,SHPObject *shp,float x,float y,float s,bool projection,float height,bool inverted){
for (int j=0, iPart=1; j<shp->nVertices; ++j){
float sx=s*(shp->padfX[j]-xlonMin)+x;
if (projection)
sx=s*(proj(shp->padfX[j],shp->padfY[j],lonRef)-plonMin)+x;
float sy=s*(shp->padfY[j]-ylatMin)+y;
if (inverted)
sy=height-sy;
if (j == 0 && shp->nParts > 0 )
cairo_move_to(cr,sx,sy);
// pszPartType = SHPPartTypeName( psShape->panPartType[0] );
if (iPart < shp->nParts && shp->panPartStart[iPart]==j){
cairo_close_path(cr);
cairo_move_to(cr,sx,sy);
// pszPartType = SHPPartTypeName( psShape->panPartType[0] );
// pszPartType = SHPPartTypeName( psShape->panPartType[iPart] );
iPart++;
}else
cairo_line_to(cr,sx,sy);
}
}
/*
// multithreaded rendering
void threadRenderFrame(ssimstate& st,int i,int n){
for (int i=st.spGrid.size()*i/n; i<st.spGrid.size()*(i+1)/n && i<st.spGrid.size(); ++i){
int xi=i%st.spGridW;
int yi=i/st.spGridH;
double xlon=xi*(psShape->dfXMax-psShape->dfXMin)/st.spGridW+psShape->dfXMin;
double ylat=(st.spGridH-yi-1)*(psShape->dfYMax-psShape->dfYMin)/st.spGridH+psShape->dfYMin;
double xlonn=(xi+1)*(psShape->dfXMax-psShape->dfXMin)/st.spGridW+psShape->dfXMin;
double ylatn=(st.spGridH-yi)*(psShape->dfYMax-psShape->dfYMin)/st.spGridH+psShape->dfYMin;
double sx=scale*(proj(xlon,ylat,lonRef)-lonMin)+xpos;
double sy=height-scale*(ylat-psShape->dfYMin)-ypos;
double sxn=scale*(proj(xlonn,ylatn,lonRef)-lonMin)+xpos+1.0; // 1.0 is added to avoid tears
double syn=height-scale*(ylatn-psShape->dfYMin)-ypos-1.0;
sgrid &g(st.spGrid[i]);
double tmpI=double(g.Ia[0]+g.Ia[1]+g.Ip[0]+g.Ip[1]+g.Is[0]+g.Is[1])/(g.N[0]+g.N[1]);
double tmpE=(g.N[0]+g.N[1]==0?0.0:double(g.E)/(g.N[0]+g.N[1]));
float pdens=clamp(0.0,1.0,(log(readPopDensR(i,j)+0.5)-log(0.5))/(log(rmaxpop+0.5)-log(0.5))*0.6+0.4);
uint32_t color=tricolorint(evector3(1.0,1.0,1.0)*pdens,evector3(0.3,0.8,0.3),evector3(1.0,0.0,0.0),tmpE,clamp(0.0,1.0,(log(tmpI+0.001)-log(0.001))/(-log(0.001))));
if (st.gridMask[i]==0)
color=0xFFFFFF;
colorRect(frameRaw,sx,sy,sxn,syn,color,width);
}
}
*/
void renderFrame(char *daystr,float mitigation,int newCases,uint8_t *frameRaw,ssimstate& st,int width,int height){
// memset(frameRaw, 0, 1920*1080*4);
// videoPushFrame(frameRaw);
// return;
if ((plonMax-plonMin)/(ylatMax-ylatMin) > double(width)/height){
scale=width/(plonMax-plonMin);
ypos=(height-scale*(ylatMax-ylatMin))/2.0;
}else{
scale=height/(ylatMax-ylatMin);
xpos=(width-scale*(plonMax-plonMin))/2.0;
}
scale=0.9*scale;
ypos=(height-scale*(ylatMax-ylatMin))/2.0;
xpos=(width-scale*(plonMax-plonMin))/2.0;
double maxE=0.0;
double maxI=0.0;
for (int i=0; i<st.spGridSize; ++i){
sgrid &g(st.spGrid[i]);
double tmpI=double(g.Ia[0]+g.Ia[1]+g.Ip[0]+g.Ip[1]+g.Is[0]+g.Is[1])/(g.N[0]+g.N[1]);
double tmpE=double(g.E)/(g.N[0]+g.N[1]);
if (maxI<tmpI) maxI=tmpI;
if (maxE<tmpE) maxE=tmpE;
}
for (int i=0; i<vwidth*vheight*4; i+=4){
frameRaw[i]=0xff;
frameRaw[i+1]=0xff;
frameRaw[i+2]=0xff;
frameRaw[i+3]=0x00;
}
for (int i=0; i<st.spGridW; ++i){
for (int j=0; j<st.spGridH; ++j){
if (st.gridMask[j*rwidth+i]==0) continue;
double xlon=i*(xlonMax-xlonMin)/st.spGridW+xlonMin;
double ylat=(st.spGridH-j-1)*(ylatMax-ylatMin)/st.spGridH+ylatMin;
double xlonn=(i+1)*(xlonMax-xlonMin)/st.spGridW+xlonMin;
double ylatn=(st.spGridH-j)*(ylatMax-ylatMin)/st.spGridH+ylatMin;
double sx=scale*(proj(xlon,ylat,lonRef)-plonMin)+xpos;
double sy=height-scale*(ylat-ylatMin)-ypos;
double sxn=scale*(proj(xlonn,ylatn,lonRef)-plonMin)+xpos+1.0; // 1.0 is added to avoid tears
double syn=height-scale*(ylatn-ylatMin)-ypos-1.0;
sgrid &g(st.spGrid[j*st.spGridW+i]);
double tmpI=double(g.Ia[0]+g.Ia[1]+g.Ip[0]+g.Ip[1]+g.Is[0]+g.Is[1])/(g.N[0]+g.N[1]);
double tmpE=(g.N[0]+g.N[1]==0?0.0:double(g.E)/(g.N[0]+g.N[1]));
float pdens=clamp(0.0,1.0,(log(readPopDensR(i,j)+0.5)-log(0.5))/(log(rmaxpop+0.5)-log(0.5))*0.6+0.4);
uint32_t color=tricolorint(evector3(1.0,1.0,1.0)*pdens,evector3(0.3,0.8,0.3),evector3(1.0,0.0,0.0),tmpE,clamp(0.0,1.0,(log(tmpI+0.001)-log(0.001))/(-log(0.001))));
if (st.gridMask[j*st.spGridW+i]==0)
color=0xFFFFFF;
colorRect(frameRaw,sx,sy,sxn,syn,color,width);
}
}
/*
cairo_rectangle(cr,0.0,0.0,vwidth,vheight);
cairo_set_source_rgb(cr, 1.0,1.0,1.0);
cairo_fill(cr);
evector3 color;
for (int i=0; i<st.spGridW; ++i){
for (int j=0; j<st.spGridH; ++j){
// double sxlon=i*(psShape->dfXMax-psShape->dfXMin)/st.spGridW+psShape->dfXMin;
// double sylat=j*(psShape->dfYMax-psShape->dfYMin)/st.spGridH+psShape->dfYMin;
double xlon=i*(psShape->dfXMax-psShape->dfXMin)/st.spGridW+psShape->dfXMin;
double ylat=(st.spGridH-j-1)*(psShape->dfYMax-psShape->dfYMin)/st.spGridH+psShape->dfYMin;
double xlonn=(i+1)*(psShape->dfXMax-psShape->dfXMin)/st.spGridW+psShape->dfXMin;
double ylatn=(st.spGridH-j)*(psShape->dfYMax-psShape->dfYMin)/st.spGridH+psShape->dfYMin;
double sx=scale*(proj(xlon,ylat,lonRef)-lonMin)+xpos;
double sy=height-scale*(ylat-psShape->dfYMin)-ypos;
double sxn=scale*(proj(xlonn,ylatn,lonRef)-lonMin)+xpos+1.0; // 1.0 is added to avoid tears
double syn=height-scale*(ylatn-psShape->dfYMin)-ypos-1.0;
sgrid &g(st.spGrid[j*st.spGridW+i]);
double tmpI=double(g.Ia[0]+g.Ia[1]+g.Ip[0]+g.Ip[1]+g.Is[0]+g.Is[1])/(g.N[0]+g.N[1]);
// double tmpE=double(g.E)/(g.N[0]+g.N[1]);
double tmpE=(g.N[0]+g.N[1]==0?0.0:double(g.E)/(g.N[0]+g.N[1]));
float pdens=clamp(0.0,1.0,(log(readPopDensR(i,j)+0.5)-log(0.5))/(log(rmaxpop+0.5)-log(0.5))*0.6+0.4);
// float pdens=clamp(0.0,1.0,readPopDensR(i+rxmin,rymax-j)/maxpdens*0.6+0.4);
// float pdens=clamp(0.0,1.0,readPopDens(i*(psShape->dfXMax-psShape->dfXMin)/100.0+psShape->dfXMin,j*(psShape->dfYMax-psShape->dfYMin)/100.0+psShape->dfYMin)/maxpdens);
// uint32_t color=tricolor(evector3(1.0,1.0,1.0)*pdens,evector3(0.3,0.8,0.3),evector3(1.0,0.0,0.0),tmpE,clamp(0.0,1.0,(log(tmpI+0.001)-log(0.001))/(-log(0.001))));
// colorRect(frameRaw,sx,sy,sxn,syn,color,width);
color=tricolor(evector3(1.0,1.0,1.0)*pdens,evector3(0.3,0.8,0.3),evector3(0.0,0.0,1.0),tmpE,clamp(0.0,1.0,(log(tmpI+0.001)-log(0.001))/(-log(0.001))));
if (gridMask[j*st.spGridW+i]==0)
color=evector3(1.0,1.0,1.0);
cairo_move_to(cr,sx,sy);
cairo_line_to(cr,sxn,sy);
cairo_line_to(cr,sxn,syn);
cairo_line_to(cr,sx,syn);
cairo_close_path(cr);
cairo_set_source_rgb(cr, color.x, color.y, color.z);
cairo_fill(cr);
}
}
*/
// tell cairo we have changed the image contents
cairo_surface_mark_dirty(surface);
for (int i=0; i<st.shapes.size(); ++i)
cairoDrawShape(cr,st.shapes[i],xpos,ypos,scale,true,height,true);
cairo_set_source_rgb(cr, 0.0, 0.0, 1.0);
cairo_set_line_width(cr, 1);
cairo_stroke (cr);
cairo_set_source_rgb(cr, 0.0, 0.0, 0.0);
cairo_move_to (cr, 10.0, 50.0);
cairo_show_text (cr, daystr);
cairo_move_to (cr, 10.0, 100.0);
cairo_show_text (cr, estr().sprintf("All cases: %.0lf",double(st.allCases))._str);
cairo_move_to (cr, 10.0, 130.0);
cairo_show_text (cr, estr().sprintf("New cases: %.0lf",double(newCases))._str);
cairo_move_to (cr, 10.0, 160.0);
cairo_show_text (cr, estr().sprintf("in ICU: %.0lf",double(st.allICU))._str);
cairo_move_to (cr, 10.0, 190.0);
cairo_show_text (cr, estr().sprintf("in nonICU: %.0lf",double(st.allNonICU))._str);
cairo_move_to (cr, 10.0, 220.0);
cairo_show_text (cr, estr().sprintf("Fatalities: %.0lf",double(st.allD))._str);
cairo_move_to (cr, 10.0, vheight-32.0);
cairo_show_text (cr, estr().sprintf("Exposed: %.1lf%%",double(st.allE)*100.0/(st.N[0]+st.N[1]))._str);
if (mitigation<1.0){
cairo_move_to (cr, 10.0, vheight-64.0);
cairo_show_text (cr, estr().sprintf("Mitigation: %.2lf",mitigation)._str);
}
/*
cairo_set_source_rgb (cr, 1.0, 0.0, 1.0);
cairo_rectangle (cr, 80.0, 60.0, 120.0, 80.0);
cairo_fill (cr);
*/
// finish any cairo drawing operations
cairo_surface_flush(surface);
videoPushFrame(frameRaw);
}
void initCairo(uint8_t *frameRaw,int width,int height)
{