-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathS2D.R
2302 lines (2046 loc) · 107 KB
/
S2D.R
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
# Source-to-dose module (S2D) for the Human Exposure Mofdel (HEM)
# Written for EPA by Graham Glen at ICF, Feb - May 2017
# Last modified on June 11, 2018
wd <- "C:/HEMforICF/QA"
setwd(wd)
s2d = function(control.file="control_file.txt", number.of.houses= NULL) {
library(data.table)
library(stringr)
library(plyr)
library(dplyr)
library(dtplyr)
library(ggplot2)
library(bit64)
library(foreach)
library(doParallel)
library(reshape2)
###################################################################
############# Start of function definitions #######################
###################################################################
# Distrib returns samples from distributions - Written by WGG for SHEDS-HT in 2012
distrib = function(shape="",par1=NA,par2=NA,par3=NA,par4=NA,lt=NA,
ut=NA, resamp="y",n=1,q=NA,p=c(1),v="" ) {
# Distrib generates samples from a distribution. If a vector of quantiles
# q is given, the corresponding values are returned. Otherwise n determines
# the number of samples, but then quantiles are randomly generated first.
# Written by Graham Glen, July 2012.
m <- ""
n <- round(n)
if (n<=0) m <- paste("# samples requested = ",n)
if (length(par1)>1|length(par2)>1|length(par3)>1|length(par4)>1) {
m <- "Non-scalar distribution parameters"
}
if (is.na(q[1])) q <- runif(n)
s <- strtrim(tolower(shape),4)
r <- strtrim(tolower(resamp),1)
if (is.na(lt)&is.na(ut)) r <- "n"
if (!is.na(ut)&!is.na(lt)&ut<lt) m <- paste("Truncation limits",lt,"and",ut)
if (!is.numeric(q)) m <- "Quantiles are not numeric"
if (min(q)<0 || max(q)>1) m <- "Quantiles not all between 0 and 1"
if (m!="") {
} else if (s=="bern" || s=="bino") {
if (is.na(par1)) par1 <- 0.5
lt <- NA
ut <- NA
if (par1<0 | par1>1) { m <- paste("Invalid binomial parameter",par1)
} else {
x <- q
x[q<=1-par1] <- 0
x[q> 1-par1] <- 1
}
} else if (s=="beta") {
if (is.na(par1)) par1 <- 1
if (is.na(par2)) par2 <- 1
if (is.na(par3)&&is.na(par4)) { par3 <- 0; par4 <- 1
} else if (is.na(par4)) { par4 <- par3+1
} else if (is.na(par3)) par3 <- par4-1
if (!is.na(lt) && lt>par4) m <- "Lower beta truncation above par4"
if (!is.na(ut) && ut<par3) m <- "Upper beta truncation below par3"
if (!is.na(lt)&&!is.na(ut)&<>ut) m <- "Lower truncation above upper"
w <- par4-par3
if (par1<=0 | par2<=0 | w<0) {
m <- paste("Invalid beta parameters",par1,par2,par3,par4)
} else if (r=="y") {
if (!is.na(lt)) qlo <- pbeta((lt-par3)/w,par1,par2) else qlo <- 0
if (!is.na(ut)) qhi <- pbeta((ut-par3)/w,par1,par2) else qhi <- 1
q <- qlo+q*(qhi-qlo)
}
if (m=="") x <- par3 + w*qbeta(q,par1,par2)
} else if (s=="disc" || s=="prob") {
if (mode(p)=="character") p <- scan(text=p,quiet=TRUE)
if (mode(v)!="numeric") {
if (mode(try(scan(text=v,quiet=TRUE),silent=TRUE))=="numeric") {
v <- c(scan(text=v,quiet=TRUE),recursive=TRUE)
} else {
v <- c(scan(what=list(""),text=v,quiet=TRUE),recursive=TRUE)
}
}
if (length(v)==0) v <- 0
if (s=="prob") v <- 1:length(p)
if (s=="disc" && length(p)==1 && length(v)>1) p <- rep(1,length(v))
if (s=="disc" && length(v)==1 && length(p)>1) v <- 1:length(p)
if (!is.na(lt)) lt <- min(v[which(v>=lt)])
if (!is.na(ut)) ut <- max(v[which(v<=ut)])
if (length(p)!=length(v)) {
m <- paste("Unequal vectors, v=",length(v),", p=",length(p))
} else if (min(p)<0) { m <- "Negative probabilities found"
} else if (r=="y") {
if (!is.na(lt)) p[v<lt] <- 0
if (!is.na(ut)) p[v>ut] <- 0
}
if (length(p[p>0])==0) m <- "All probabilities are zero"
if (m=="") {
t <- cumsum(p/sum(p))
t[t>0.99999999] <- 1.00000001
x <- v[mapply(function(q,t) {which.max(cummax(q<t))},q,MoreArgs=list(t))]
}
} else if (s=="empi") {
if (mode(v)!="numeric") {
if (mode(try(scan(text=v,quiet=TRUE),silent=TRUE))=="numeric") {
v <- c(scan(text=v,quiet=TRUE),recursive=TRUE)
} else {
v <- c(scan(what=list(""),text=v,quiet=TRUE),recursive=TRUE)
}
}
if (length(v)==0) v <- 0
if (!is.na(lt)) lt <- min(v[which(v>=lt)])
if (!is.na(ut)) ut <- max(v[which(v<=ut)])
if (r=="y") {
if (!is.na(lt)) v <- v[v>=lt]
if (!is.na(ut)) v <- v[v<=ut]
}
if (length(v)==0) m <- "No empirical values"
if (m=="") x <- v[round(0.5+length(v)*q)]
} else if (s=="expo") {
if (is.na(par1)) par1 <- 1
if (is.na(par2)) par2 <- 0
if (!is.na(lt) && lt<par2) lt <- par2
if (!is.na(ut) && ut<par2) m <- "Upper expo. truncation below par2"
if (!is.na(lt)&&!is.na(ut)&<>ut) m <- "Lower truncation above upper"
if (par1<=0) { m <- paste("Invalid exponential parameter",par1)
} else if (r=='y') {
if (!is.na(lt)) qlo <- pexp(lt-par2,par1) else qlo <- 0
if (!is.na(ut)) qhi <- pexp(ut-par2,par1) else qhi <- 1
q <- qlo+q*(qhi-qlo)
}
if (m=="") x <- par2 + qexp(q,par1)
} else if (s=="gamm") {
if (is.na(par1)) par1 <- 1
if (is.na(par2)) par2 <- 1
if (is.na(par3)) par3 <- 0
if (!is.na(lt) && lt<par3) lt <- par3
if (!is.na(ut) && ut<par3) m <- "Upper gamma truncation below par3"
if (!is.na(lt)&&!is.na(ut)&<>ut) m <- "Upper truncation above lower"
if (par1<=0 | par2<=0) {
m <- paste("Invalid gamma parameters",par1,par2,par3)
} else if (r=='y') {
if (!is.na(lt)) qlo <- pgamma(lt-par3,par1,1/par2,par2) else qlo <- 0
if (!is.na(ut)) qhi <- pgamma(ut-par3,par1,1/par2,par2) else qhi <- 1
q <- qlo+q*(qhi-qlo)
}
if (m=="") x <- par3 + qgamma(q,par1,1/par2,par2)
} else if (s=="logn") {
if (is.na(par1)) par1 <- 1
if (is.na(par2)) par2 <- exp(1)
if (is.na(par3)) par3 <- 0
if (!is.na(lt) && lt<par3) lt <- par3
if (!is.na(ut) && ut<par3) m <- "Upper lognormal truncation below par3"
if (!is.na(lt)&&!is.na(ut)&<>ut) m <- "Upper truncation above lower"
if (par1<=0 | par2<1) {
m <- paste("Invalid lognormal parameters",par1,par2,par3)
} else if (r=='y' && par2>1) {
if (!is.na(lt)) qlo <- plnorm(lt-par3,log(par1),log(par2)) else qlo <- 0
if (!is.na(ut)) qhi <- plnorm(ut-par3,log(par1),log(par2)) else qhi <- 1
q <- qlo+q*(qhi-qlo)
}
if (m=="") x <- par3 + qlnorm(q,log(par1),log(par2))
} else if (s=="norm") {
if (is.na(par1)) par1 <- 0
if (is.na(par2)) par2 <- 1
if (par2<0) {m <- paste("Invalid normal parameters",par1,par2)
if (!is.na(lt)&&!is.na(ut)&<>ut) m <- "Lower truncation above upper"
} else if (r=='y' && par2>0) {
if (!is.na(lt)) qlo <- pnorm(lt,par1,par2) else qlo <- 0
if (!is.na(ut)) qhi <- pnorm(ut,par1,par2) else qhi <- 1
q <- qlo+q*(qhi-qlo)
}
if (m=="") x <- qnorm(q,par1,par2)
} else if (s=="poin") {
if (is.na(par1)) par1 <- 0
lt <- NA
ut <- NA
x <- rep(par1,length(q))
} else if (s=="tria") {
if (is.na(par1)&&is.na(par2)) { par1 <- 0; par2 <- 1
} else if (is.na(par2)) { par2 <- par1+1
} else if (is.na(par1)) par1 <- par2-1
if (is.na(par3)) par3 <- (par1+par2)/2
if (par3>par2) { t<-par2; par2<-par3; par3<-t }
if (!is.na(lt) && lt>par2) m <- "Lower triangle truncation above par2"
if (!is.na(ut) && ut<par1) m <- "Upper triangle truncation below par1"
if (!is.na(lt)&&!is.na(ut)&<>ut) m <- "Upper truncation above lower"
if (par1>par2 | par3<par1 | par3>par2 ) {
m <- paste("Invalid triangle parameters",par1,par2,par3)
}
if (par1==par2) return(rep(par1,length(q)))
p <- (par3-par1)/(par2-par1)
if (r=='y') {
if (!is.na(lt) && lt>par1 && lt<=par2) {
if (lt==par3) { qlo <- p
} else if (lt<par3) { qlo <- (lt-par1)^2/((par2-par1)*(par3-par1))
} else if (lt>par3) { qlo <- 1-(par2-lt)^2/((par2-par1)*(par2-par3))
}
} else qlo <- 0
if (!is.na(ut) && ut>=par1 && ut<par2) {
if (ut==par3) {qhi <- p
} else if (ut<par3) { qhi <- (ut-par1)^2/((par2-par1)*(par3-par1))
} else if (ut>par3) { qhi <- 1-(par2-ut)^2/((par2-par1)*(par2-par3))
}
} else qhi <- 1
q <- qlo+q*(qhi-qlo)
}
if (m=="") {
x <- par1 + sqrt( q *(par2-par1)*(par3-par1))
x2 <- par2 - sqrt((1-q)*(par2-par1)*(par2-par3))
x[x2>par3] <- x2[x2>par3]
}
} else if (s=="unif") {
if (is.na(par1)) par1 <- 0
if (is.na(par2)) par2 <- 1
if (par2<par1) m <- paste("Invalid uniform parameters", par1,par2)
if (!is.na(lt) && lt>par2) m <- "Lower uniform truncation above par2"
if (!is.na(ut) && ut<par1) m <- "Upper uniform truncation below par1"
if (!is.na(lt)&&!is.na(ut)&<>ut) m <- "Upper truncation above lower"
if (r=='y' && par2>par1) {
if (!is.na(lt)) qlo <- punif(lt,par1,par2) else qlo <- 0
if (!is.na(ut)) qhi <- punif(ut,par1,par2) else qhi <- 1
q <- qlo+q*(qhi-qlo)
}
if (m=="") x <- qunif(q,par1,par2)
} else if (s=="weib") {
if (is.na(par1)) par1 <- 1
if (is.na(par2)) par2 <- 1
if (is.na(par3)) par3 <- 0
if (!is.na(lt) && lt<par3) lt <- par3
if (!is.na(ut) && ut<par3) m <- "Upper Weibull truncation below par3"
if (!is.na(lt)&&!is.na(ut)&<>ut) m <- "Upper truncation above lower"
if (par1<=0 | par2<=0) {
m <- paste("Invalid Weibull parameters",par1,par2,par3)
}
if (r=='y') {
if (!is.na(lt)) qlo <- pweibull(lt-par3,par1,par2) else qlo <- 0
if (!is.na(ut)) qhi <- pweibull(ut-par3,par1,par2) else qhi <- 1
q <- qlo+q*(qhi-qlo)
}
if (m=="") x <- par3+qweibull(q,par1,par2)
} else m <- paste("Unknown Distrib shape ",s)
if (m != "") {cat("\n Error in distrib: ",m,"\n"); return(NULL)
} else {
if (!is.na(lt)) x <- mapply(max,lt,x)
if (!is.na(ut)) x <- mapply(min,ut,x)
return(x)
}
}
# eval.brand.list evaluates the products (brands) and formulations to be retained
eval.brand.list = function(chem.list,puc.list,chem.fracs) {
keep.puc <- rep(TRUE,length(puc.list))
for (i in 1:length(puc.list)) {
pchem <- unique(chem.fracs$dtxsid[chem.fracs$source.id==puc.list[i]])
if (length(pchem %in% chem.list)==0) {
cat ("\n PUC ",puc.list[i]," contains no modeled chemicals")
keep.puc[i] <- FALSE
}
}
puc <- puc.list[keep.puc]
brands <- vector("list",length(puc))
for (i in 1:length(puc)) {
brands[[i]] <- c(unique(chem.fracs$product_id[chem.fracs$source.id==puc[i]]))
}
brand.list <- data.table(puc,brands)
setorder(brand.list,puc)
return(brand.list)
}
# eval.chem.list evaluates the chemicals to be retained
eval.chem.list = function(chem.list,puc.list,chem.fracs) {
keep.chem <- rep(TRUE,length(chem.list))
chems <- unique(chem.fracs$dtxsid)
for (i in 1:length(chem.list)) {
chem <- chem.list[i]
if (!chem %in% chems) {
cat("\n Chem ",chem," not in any modeled PUC")
keep.chem[i] <- FALSE
} else {
y <- chem.fracs[chem.fracs$dtxsid==chem]
if (max(y$weight_fraction)==0) {
cat ("\n Chem ",chem," always zero")
keep.chem[i] <- FALSE
}
}
}
chem.list <- unique(chem.list[keep.chem])
return(chem.list)
}
# eval.chem.props returns chemical properties sampled from distributions
eval.chem.props = function(fug.cvars,chem.list,ran.vars,q) {
nc <- length(chem.list)
molwt <- vector("numeric",nc)
vapor <- vector("numeric",nc)
solub <- vector("numeric",nc)
kow <- vector("numeric",nc)
decay.air <- vector("numeric",nc)
decay.sur <- vector("numeric",nc)
diffus.air <- vector("numeric",nc)
qf <- as.data.frame(q)[ran.vars[[3]]]
qx <- data.table(matrix(qf,nrow=nc,ncol=6,byrow=TRUE))
setnames(qx,unique(unlist(lapply(names(qf),strip.n))))
if(g$save.r.objects=="y") write.csv(unlist(qx),paste0(g$out,"/Temp/qx_",run.name,".csv"))
for (c in 1:nc) {
cvars <- fug.cvars[fug.cvars$dtxsid==chem.list[c]]
if (nrow(cvars)>0) {
qc <- lapply(qx[c],as.numeric)
molwt[c] <- cvars$molwt
vaporGM <- cvars$vapor
if(is.null(vaporGM)) vaporGM <- 1
vapor[c] <- distrib("logn",par1=vaporGM,par2=2,lt=vaporGM/100,ut=min(vaporGM*100,1E5),q=qc$vapor)
solubGM <- cvars$solub
if(is.null(solubGM)) solubGM <- 1
solub[c] <- distrib("logn",par1=solubGM,par2=1.3,lt=solubGM/5,ut=solubGM*5,q=qc$solub)
kowGM <- cvars$kow
if(is.null(kowGM)) kowGM <- 10
kow[c] <- distrib("logn",par1=kowGM,par2=1.5,lt=kowGM/10,ut=kowGM*10,q=qc$kow)
dc.airGM <- cvars$decay.a
if(is.null(dc.airGM)) dc.airGM <- 0.01
decay.air[c] <- distrib("logn",par1=dc.airGM,par2=1.5,lt=dc.airGM/10,ut=dc.airGM*10,q=qc$decay.air)
dc.surGM <- cvars$decay.s
if(is.null(dc.surGM)) dc.surGM <- 0.001
decay.sur[c] <- distrib("logn",par1=dc.surGM,par2=1.5,lt=dc.surGM/10,ut=dc.surGM*10,q=qc$decay.sur)
diffusGM <- (2.05*(1/29+1/molwt[c])^0.5)/molwt[c]^0.33 * 86400 / 10000
diffus.air[c] <- distrib("logn",par1=diffusGM,par2=1.3,lt=diffusGM/5,ut=diffusGM*5,q=qc$diffus.air)
} else {
molwt[c] <- 100
vapor[c] <- 1
solub[c] <- 1
kow[c] <- 10
decay.air[c] <- 0.01
decay.sur[c] <- 0.001
diffus.air[c] <- 0.01
}
}
props <- data.table(chem.list,molwt,vapor,solub,kow,decay.air,decay.sur,diffus.air)
setnames(props,names(props),c("dtxsid","molwt","vapor","solub","kow","decay.air","decay.sur","diffus.air"))
if(g$prog=="y") cat("\n Evaluating chem props complete")
return(props)
}
# eval.chem.release produces an hourly calendar with chemical releases inside house
eval.chem.release = function(pucs,use.data,use.chem,compart.list,diary,nc,calendar.hours) {
np <- nrow(pucs)
air <- matrix(use.data[,3,compart.list=="fia",],nrow=np,ncol=nc)
sur <- matrix(use.data[,3,compart.list=="fis",],nrow=np,ncol=nc)
tot <- as.data.table(use.chem)
setnames(tot,str_c(rep("tot",nc),rep(1:nc)))
puc.data <- data.table(pucs$source.id,air,sur,tot)
setnames(puc.data,1:(1+2*nc),c("source.id",str_c(rep("air",nc),rep(1:nc)),str_c(rep("sur",nc),rep(1:nc))))
cair <- as.data.table(matrix(0,nrow=8736,ncol=nc))
csur <- as.data.table(matrix(0,nrow=8736,ncol=nc))
ctot <- as.data.table(matrix(0,nrow=8736,ncol=nc))
rel.ind <- rep(0,8736)
rel.tot <- rep(0,8736)
setnames(cair,str_c(rep("air",nc),rep(1:nc)))
setnames(csur,str_c(rep("sur",nc),rep(1:nc)))
setnames(ctot,str_c(rep("tot",nc),rep(1:nc)))
d <- select(diary[diary$mass!=0],source.id,row,person,daynum,hour,mass)
d[!d$source.id %in% pucs$source.id]$source.id <- "none"
d$hournum <- as.integer(24*(d$daynum-1)+d$hour)
setkey(d,source.id)#sorting by source.id, in ascending order
setkey(puc.data,source.id)
dpuc <- as.data.frame(inner_join(puc.data,d,by="source.id"))
hournum <- dpuc$hournum
yair <- select_vars(names(puc.data),starts_with("air"))
ysur <- select_vars(names(puc.data),starts_with("sur"))
ytot <- select_vars(names(puc.data),starts_with("tot"))
pair <- as.data.table(dpuc[yair])
psur <- as.data.table(dpuc[ysur])
ptot <- as.data.table(dpuc[ytot])
hours <- unique(hournum)
for (i in 1:length(hours)) {
hr <- hours[i]
set(cair,i=hr,1:nc,lapply(pair[hournum==hr],sum))
set(csur,i=hr,1:nc,lapply(psur[hournum==hr],sum))
set(ctot,i=hr,1:nc,lapply(ptot[hournum==hr],sum))
rel.ind[hr] <- sum(cair[hr],csur[hr])
rel.tot[hr] <- sum(ctot[hr])
}
chem.release <- as.data.table(data.frame(calendar.hours,rel.ind,rel.tot,cair,csur,ctot))
if (g$prog=="y") cat("\n Evaluating chemical release complete")
return(chem.release)
}
# eval.chem.totals sets the annual household usage of each chemical in mg
eval.chem.totals = function(chem.release,chem.list,house.num) {
x <- select(as.data.frame(chem.release),starts_with("tot"))
y <- as.data.table(matrix(colSums(x),nrow=1,ncol=length(chem.list)))
setnames(y,chem.list)
write.csv(y,paste0(g$out,"/Chem/House_",house.num,".csv"),row.names=TRUE)
return(y)
}
# eval.dermal.rates determines the rate constants for removal processes from skin
eval.dermal.rates = function(fug.cvars,hp,prime,pucs.areas,prod.chem,nc,house.num) {
np <- nrow(pucs.areas) # last one is for indirect
kaw <- 1/(fug.cvars$solub/fug.cvars$vapor*8.314*hp$temp) # inverse of SHEDS convention
phi.a <- 8 * kaw
phi.w <- 20.6/fug.cvars$molwt^0.4757
kp <- fug.cvars$kp /100 # convert cm/hr to m/hr
kh <- prime$hand.mouth
kr <- 0 # other removal is zero for now
# hw is number per day, all others are rates per hour
hw <- prime$hand.wash
# ka and ks require product layer thickness on skin
# first calulate affected skin area by PUC (in cm2 from pophouse file)
#if (prime$age<18) prod.area <- pucs.areas$f.child * prime$skin.area
#if (prime$age>=18) prod.area <- pucs.areas$f.adult * prime$skin.area
#prod.area[is.na(prod.area)] <- 1
#prod.area <- pmax(prod.area, 1) # set minimum of 1 cm2
if (prime$age<18) frac.area <- pmax(pucs.areas$f.child,0.01) # set minimum to 1% of total
if (prime$age>=18) frac.area <- pmax(pucs.areas$f.adult,0.01)
frac.area[is.na(frac.area)] <- 0.01
prod.area <- frac.area * prime$skin.area
# volume in cm3 = product mass in grams * fraction applied to skin (assume density=1 g/cm3)
prod.vol <- pucs.areas$mass * c(prod.chem$fsk,1)
# thickness is volume / affected skin area (cm3/cm2)
prod.thick <- prod.vol / prod.area
h <- prod.thick / 100 # convert cm to meters
h[length(h)] <- pucs.areas$h[length(h)]
h <- pmax(1E-5,h) # minimum of 10 microns
source.id <- rep(pucs.areas$source.id,nc)
dtxsid <- unlist(lapply(fug.cvars$dtxsid,rep,np))
rates <- data.table(source.id,dtxsid,matrix(0,length(dtxsid),ncol=6))
setnames(rates,c("source.id","dtxsid","hw","kh","kr","ka","ks","h"))
rates$hw <- hw
rates$kh <- kh
rates$kr <- kr
for (c in 1:length(kaw)) {
for (p in 1:np) {
rates$ka[p+(c-1)*np] <- 1/(h[[p]]/phi.a[[c]] + h[[p]]/phi.w[[c]]) # rate into air
rates$ks[p+(c-1)*np] <- 1/(h[[p]]/kp[[c]] + h[[p]]/phi.w[[c]]) # rate into skin
}
}
rates$h <- h
rates[is.na(rates)] <- 0
if (g$prog=="y") cat("\n Evaluating dermal rates complete")
if (g$save.r.objects=="y") write.csv(rates,paste0(g$out,"/Temp/dermal_rates_",house.num,"_",run.name,".csv"))
return(rates)
}
# eval.direct produces a daily summary of direct exposure variables
eval.direct = function(d,use.data,use.chem,pucs,dermal.rates,compart.list,prime,puc.wipe.rinse,chem.list,fug.cvars,chem.totals) {
chems <- as.data.frame(chem.totals)
# First, evaluate the exposure variables for each combination of product and chemical
skin1 <- use.data[,1,compart.list=="fsk",]
skin2 <- use.data[,2,compart.list=="fsk",]
skin3 <- use.data[,3,compart.list=="fsk",]
inair1 <- use.data[,1,compart.list=="fia",]
inair2 <- use.data[,2,compart.list=="fia",]
gut <- use.data[,3,compart.list=="fgi",]
nc <- length(chem.list)
np <- nrow(pucs)
use.avg.derm <- as.data.frame(matrix((skin1+skin2)/2,nrow=np,ncol=nc))
use.avg.hand <- use.avg.derm * puc.wipe.rinse$fhands
use.avg.body <- use.avg.derm * puc.wipe.rinse$fbody
use.avg.air <- as.data.frame(matrix((inair1+inair2)/2,nrow=np,ncol=nc))
use.gut <- as.data.frame(matrix(gut,nrow=np,ncol=nc))
post.derm <- as.data.frame(matrix(skin3,nrow=np,ncol=nc))
post.hand <- post.derm * puc.wipe.rinse$fhands
post.body <- post.derm * puc.wipe.rinse$fbody
vol.cloud <- 2 # personal cloud size
aer.cloud <- 10 # cloud exchanges air 10 times per hour
dur.cloud <- pucs$hand.dur/60 # handling time in hours
rates <- as.data.table(dermal.rates)[!dermal.rates$source.id=="Indirect"]
f.hands <- puc.wipe.rinse$fhands
f.hands[is.na(f.hands)] <- 0.05
hand.area <- 0.05*prime$skin.area
body.area <- 0.95*prime$skin.area
vars <- c("f.use.lost","use.avg.air","use.avg.derm","use.hands.exp","use.body.exp","use.hands.air",
"use.body.air","use.body.abs","use.hands.load","use.body.load","use.dermal.abs","use.air.mass",
"use.air.conc","use.inhal.exp","use.inhal.mass","use.hands.abs",
"use.inhal.abs","use.inges.exp","use.inges.abs",
"f.hands.lost","f.body.lost","post.dermal.exp","post.hands.exp","post.body.exp","post.hands.abs",
"post.hands.air","post.inges.hand","post.body.air","post.body.abs",
"post.hands.load","post.body.load","post.dermal.abs",
"post.air.rate","post.air.conc","post.inhal.exp","post.inhal.mass",
"post.inhal.abs","post.inges.abs")
zero <- data.table(matrix(0,nrow=nrow(pucs),ncol=length(vars)))
setnames(zero,vars)
dp <- data.table(pucs$source.id,f.hands,pucs$hand.dur,pucs$hand.dur/60,vol.cloud,aer.cloud)
setnames(dp,c("source.id","f.hands","use.dur.min","use.dur.hr","vol.cloud","aer.cloud"))
if (g$prog=="y") cat("\n direct initiation complete...")
for (c in 1:nc) {
dp <- cbind(dp,zero)
if (chems[c]>0) {
if (g$prog=="y") cat(paste0("\n Chemical ",c," ",chem.list[c]))
ka <- pmax(0.00001,rates$ka[rates$dtxsid==chem.list[c]])
ks <- pmax(0.00001,rates$ks[rates$dtxsid==chem.list[c]])
kh <- pmax(0.00001,rates$kh[rates$dtxsid==chem.list[c]])
kr <- pmax(0.00001,rates$kr[rates$dtxsid==chem.list[c]])
hw <- pmax(0.00001,rates$hw[rates$dtxsid==chem.list[c]])
dp$f.use.lost <- 1-exp(-(ka+ks)*dp$use.dur.hr) # fraction of hand loading lost during product usage time
dp$use.avg.air <- use.avg.air[c]
dp$use.avg.derm <- use.avg.derm[c]
dp$use.hands.exp <- use.avg.derm[c] * dp$f.hands
dp$use.body.exp <- use.avg.derm[c] * (1-dp$f.hands)
dp$use.hands.air <- dp$use.hands.exp * ka/(ka+ks)* dp$f.use.lost
dp$use.hands.abs <- dp$use.hands.exp * ks/(ka+ks)* dp$f.use.lost
dp$use.body.air <- dp$use.body.exp * ka/(ka+ks)* dp$f.use.lost
dp$use.body.abs <- dp$use.body.exp * ks/(ka+ks)* dp$f.use.lost
dp$use.hands.load <- dp$use.hands.exp / hand.area
dp$use.body.load <- dp$use.body.exp / body.area
dp$use.dermal.abs <- dp$use.hands.abs + dp$use.body.abs
dp$use.air.mass <- dp$use.avg.air + dp$use.hands.air + dp$use.body.air
dp$use.air.conc <- dp$use.air.mass/(vol.cloud*aer.cloud*dur.cloud)
dp$use.inhal.exp <- dp$use.air.conc * dp$use.dur.hr/24 # converted to daily avg
dp$use.inhal.mass <- dp$use.inhal.exp * prime$basal.vent * pucs$met
dp$use.inges.exp <- use.gut[c] # amount in fgi compartment
# for spray products, reassign 75% of inhaled mass to ingestion
dp$use.inges.exp[pucs$spray] <- 0.75*dp$use.inhal.mass[pucs$spray] + dp$use.inges.exp[pucs$spray]
dp$use.inhal.mass[pucs$spray] <- 0.25*dp$use.inhal.mass[pucs$spray]
dp$use.inges.abs <- dp$use.inges.exp * fug.cvars$fabs[c]
dp$use.inhal.abs <- dp$use.inhal.mass * 0.16 # assume 16% absorption
dp$f.hands.lost <- 1-exp(-(ka+ks+kh+kr)*8/hw) # dur on hands is (8/handwashes) hrs
dp$f.body.lost <- 1-exp(-(ka+ks+kr)*8) # dur on body is 8 hrs
dp$post.dermal.exp <- post.derm[c]
dp$post.hands.exp <- post.derm[c] * dp$f.hands
dp$post.body.exp <- post.derm[c] * (1-dp$f.hands)
dp$post.hands.abs <- dp$post.hands.exp * ks/(ka+ks+kh+kr) * dp$f.hands.lost
dp$post.hands.air <- dp$post.hands.exp * ka/(ka+ks+kh+kr) * dp$f.hands.lost
dp$post.inges.hand <- dp$post.hands.exp * kh/(ka+ks+kh+kr) * dp$f.hands.lost
dp$post.body.air <- dp$post.body.exp * ka/(ka+ks+kr) * dp$f.body.lost
dp$post.body.abs <- dp$post.body.exp * ks/(ka+ks+kr) * dp$f.body.lost
dp$post.hands.load <- dp$post.hands.exp / hand.area
dp$post.body.load <- dp$post.body.exp / body.area
dp$post.dermal.abs <- dp$post.hands.abs+dp$post.body.abs
dp$post.air.rate <- (dp$post.hands.air + dp$post.body.air)/8 # hourly emission rate into cloud
dp$post.air.conc <- dp$post.air.rate/(vol.cloud*aer.cloud) # inflow in 1 hour / ouflow in 1 hour
dp$post.inhal.exp <- dp$post.air.conc * 8/24 # convert to daily avg
dp$post.inhal.mass <- dp$post.inhal.exp * prime$basal.vent * 2.2 # assume post-use mean MET is 2.2
dp$post.inhal.abs <- dp$post.inhal.mass * 0.16 # assume 16% absorption
dp$post.inges.abs <- dp$post.inges.hand * fug.cvars$fabs[c]
}
setnames(dp,vars,str_c(vars,c))
}
dchem <- as.data.table(data.frame(dp,use.chem))
setkey(dchem,source.id)
# Now loop over the diary events, applying the above results as products are used
d <- d[d$source.id %in% pucs$source.id]
setkey(d,source.id)
ddata <- left_join(d,dchem,by="source.id")
derm.exp <- matrix(0,nrow=364,ncol=nc)
derm.max <- matrix(0,nrow=364,ncol=nc)
derm.abs <- matrix(0,nrow=364,ncol=nc)
inhal.exp <- matrix(0,nrow=364,ncol=nc)
inhal.mass<- matrix(0,nrow=364,ncol=nc)
inhal.max <- matrix(0,nrow=364,ncol=nc)
inhal.abs <- matrix(0,nrow=364,ncol=nc)
inges.exp <- matrix(0,nrow=364,ncol=nc)
inges.abs <- matrix(0,nrow=364,ncol=nc)
release <- matrix(0,nrow=364,ncol=nc)
nam.de <- str_c(rep("dir.derm.exp",nc),1:nc)
nam.dm <- str_c(rep("dir.derm.max",nc),1:nc)
nam.da <- str_c(rep("dir.derm.abs",nc),1:nc)
nam.ihe <- str_c(rep("dir.inhal.exp",nc),1:nc)
nam.ihmas <- str_c(rep("dir.inhal.mass",nc),1:nc)
nam.ihm <- str_c(rep("dir.inhal.max",nc),1:nc)
nam.iha <- str_c(rep("dir.inhal.abs",nc),1:nc)
nam.ige <- str_c(rep("dir.ingest.exp",nc),1:nc)
nam.iga <- str_c(rep("dir.ingest.abs",nc),1:nc)
nam.rel <- str_c(rep("dir.release",nc),1:nc)
uhe <- select_vars(names(ddata),starts_with("use.hands.exp"))
ube <- select_vars(names(ddata),starts_with("use.body.exp"))
phe <- select_vars(names(ddata),starts_with("post.hands.exp"))
pbe <- select_vars(names(ddata),starts_with("post.body.exp"))
uha <- select_vars(names(ddata),starts_with("use.hands.abs"))
uba <- select_vars(names(ddata),starts_with("use.body.abs"))
pha <- select_vars(names(ddata),starts_with("post.hands.abs"))
pba <- select_vars(names(ddata),starts_with("post.body.abs"))
uhl <- select_vars(names(ddata),starts_with("use.hands.load"))
ubl <- select_vars(names(ddata),starts_with("use.body.load"))
phl <- select_vars(names(ddata),starts_with("post.hands.load"))
pbl <- select_vars(names(ddata),starts_with("post.body.load"))
uie <- select_vars(names(ddata),starts_with("use.inhal.exp"))
pie <- select_vars(names(ddata),starts_with("post.inhal.exp"))
uim <- select_vars(names(ddata),starts_with("use.inhal.mass"))
pim <- select_vars(names(ddata),starts_with("post.inhal.mass"))
uac <- select_vars(names(ddata),starts_with("use.air.conc"))
pac <- select_vars(names(ddata),starts_with("post.air.conc"))
uia <- select_vars(names(ddata),starts_with("use.inhal.abs"))
pia <- select_vars(names(ddata),starts_with("post.inhal.abs"))
pgh <- select_vars(names(ddata),starts_with("post.inges.hand"))
ugm <- select_vars(names(ddata),starts_with("use.inges.mass"))
uga <- select_vars(names(ddata),starts_with("use.inges.abs"))
pga <- select_vars(names(ddata),starts_with("post.inges.abs"))
tot <- select_vars(names(ddata),starts_with("tot"))
if (g$prog=="y") cat("\n direct starting loop over chemicals and days..")
ndaysused <- nrow(ddata)
if (ndaysused>0) {
for (i in 1:ndaysused) {
dd <- as.data.frame(ddata[i])
day <- d$daynum[i]
for (c in 1:nc) {
if (chems[c]>0) {
derm.exp[day,c] <- sum(dd[uhe][c],dd[ube][c],dd[phe][c],dd[pbe][c])
derm.max[day,c] <- max(0,unlist(dd[uhl][c]),unlist(dd[ubl][c]),unlist(dd[phl][c]),unlist(dd[pbl][c]))
derm.abs[day,c] <- sum(dd[uha][c],dd[uba][c],dd[pha][c],dd[pba][c])
inhal.exp[day,c] <- sum(dd[uie][c],dd[pie][c])
inhal.mass[day,c] <- sum(dd[uim][c],dd[pim][c])
inhal.max[day,c] <- max(0,unlist(dd[uac][c]),unlist(dd[pac][c]),unlist(dd[uie][c]),unlist(dd[pie][c]))
inhal.abs[day,c] <- sum(dd[uia][c],dd[pia][c])
inges.exp[day,c] <- sum(dd[pgh][c])
inges.abs[day,c] <- sum(dd[uga][c],dd[pga][c])
release[day,c] <- sum(dd[tot][c])
}
}
}
}
direct <- as.data.table(data.frame(1:364,derm.exp,derm.max,derm.abs,inhal.exp,inhal.mass,inhal.max,inhal.abs,inges.exp,inges.abs,release))
setnames(direct,c("daynum",nam.de,nam.dm,nam.da,nam.ihe,nam.ihmas,nam.ihm,nam.iha,nam.ige,nam.iga,nam.rel))
if (g$prog=="y") cat("\n Evaluating direct exposures complete")
return(direct)
}
# eval.env.impact produces a daily summary of emissions from the house
eval.env.impact = function(diary,use.data,pucs,fug.day,compart.list,chem.totals,nc) {
chems <- as.data.frame(chem.totals)
d <- diary[diary$source.id %in% pucs$source.id]
np <- nrow(pucs)
out.sur <- matrix(use.data[,3,compart.list=="fos",],nrow=np,ncol=nc)
out.air <- matrix(use.data[,3,compart.list=="foa",],nrow=np,ncol=nc)
drain <- matrix(use.data[,3,compart.list=="fdr",],nrow=np,ncol=nc)
waste <- matrix(use.data[,3,compart.list=="fws",],nrow=np,ncol=nc)
rels <- data.table(pucs$source.id,drain,waste,out.sur,out.air)
nam.os <- str_c(rep("out.sur",nc),1:nc)
nam.oa <- str_c(rep("out.air",nc),1:nc)
nam.dr <- str_c(rep("drain",nc),1:nc)
nam.ws <- str_c(rep("waste",nc),1:nc)
setnames(rels,c("source.id",nam.dr,nam.ws,nam.os,nam.oa))
setkey(d,source.id)
setkey(rels,source.id)
drel <- left_join(d,rels,by="source.id")
dir.os <- matrix(0,nrow=364,ncol=nc)
dir.oa <- matrix(0,nrow=364,ncol=nc)
dir.dr <- matrix(0,nrow=364,ncol=nc)
dir.ws <- matrix(0,nrow=364,ncol=nc)
os <- select_vars(names(drel),starts_with("out.sur"))
oa <- select_vars(names(drel),starts_with("out.air"))
dr <- select_vars(names(drel),starts_with("drain"))
ws <- select_vars(names(drel),starts_with("waste"))
fwn <- select_vars(names(fug.day),starts_with("win"))
fws <- select_vars(names(fug.day),starts_with("was"))
for (c in 1:nc) {
if(chems[c]>0) {
for (i in 1:364) {
dd <- as.data.frame(drel[drel$daynum==i])
if (nrow(dd)>0) {
fd <- as.data.frame(fug.day[fug.day$daynum==i])
dir.os[i,c] <- sum(dd[os][c])
dir.oa[i,c] <- sum(dd[oa][c],fd[fwn][c])
dir.dr[i,c] <- sum(dd[dr][c])
dir.ws[i,c] <- sum(dd[ws][c],fd[fws][c])
}
}
}
}
env.impact <- data.table(1:364,dir.os,dir.oa,dir.dr,dir.ws)
setnames(env.impact,c("daynum",os,oa,dr,ws))
if (g$prog=="y") cat("\n Evaluating environmental impacts complete")
return(env.impact)
}
# eval.flows detetmines the flow rate constants between house compartments
eval.flows = function(hp,cp) {
cp$vol.air <- hp$area.sur * hp$height # vol.air [m3]
cp$aer.out <- hp$aer.out # aer.out [1/day]
cp$sm.mass.air <- cp$vol.air * hp$sm.load.air # sm.mass.air [ug]
cp$lg.mass.air <- cp$vol.air * hp$lg.load.air # lg.mass.air [ug]
cp$sm.mass.sur <- hp$area.sur * hp$sm.load.sur # sm.mass.sur [ug]
cp$lg.mass.sur <- hp$area.sur * hp$lg.load.sur # lg.mass.sur [ug]
cp$sm.clean.sur <- max(hp$sm.clean.sur,hp$sm.depos*hp$sm.load.air/hp$sm.load.sur-hp$sm.resus)
cp$lg.clean.sur <- max(hp$lg.clean.sur,hp$lg.depos*hp$lg.load.air/hp$lg.load.sur-hp$lg.resus)
# clean.sur [1/day], depos [m/day], load.air [ug/m3], load.sur [ug/m2], resus [1/day]
cp$sm.clean.air <- pmax(hp$sm.clean.air,(hp$sm.resus*cp$sm.mass.sur-hp$sm.depos*hp$sm.load.air*hp$area.sur)/cp$sm.mass.air)
cp$lg.clean.air <- pmax(hp$lg.clean.air,(hp$lg.resus*cp$lg.mass.sur-hp$lg.depos*hp$lg.load.air*hp$area.sur)/cp$lg.mass.air)
# clean.air [1/day], resus [1/day], mass.sur [ug], depos [m/day], load.air [ug/m3], area.sur [m2], mass.air [ug]
cp$ug.mol <- 1E6*cp$molwt # ug.mol [ug/mol]
cp$z.air <- 1/(8.314*hp$temp) # z.air [mol/(Pa m3)]
cp$zvb.air <- cp$z.air * cp$vol.air * cp$ug.mol # zvb.air [ug/Pa]
cp$z.sur <- cp$z.air * 82500 / (cp$vapor^0.65) # z.sur [mol/(Pa m3)]
cp$zvb.sur <- cp$z.sur * hp$area.sur * hp$thick.sur * cp$ug.mol # zvb.sur [ug/Pa]
cp$sm.kp <- 1.662E-12 * cp$kow * hp$sm.carb.f * cp$solub / (cp$vapor * cp$z.air)
cp$lg.kp <- 1.662E-12 * cp$kow * hp$lg.carb.f * cp$solub / (cp$vapor * cp$z.air)
# kp [m3/ug], 1.662E-12 [m3/ug], kow [-], carb.f [-], solub [mol/m3], vapor [Pa], z.air [mol/(Pa m3)]
cp$sm.zv.air <- cp$zvb.air * cp$sm.kp * hp$sm.load.air # sm.zv.air [ug/Pa]
cp$lg.zv.air <- cp$zvb.air * cp$lg.kp * hp$lg.load.air # lg.zv.air [ug/Pa]
cp$sm.cap <- cp$z.air * cp$sm.kp * cp$ug.mol # sm.cap [1/Pa]
cp$lg.cap <- cp$z.air * cp$lg.kp * cp$ug.mol # lg.cap [1/Pa]
cp$sm.zv.sur <- cp$sm.cap * cp$sm.mass.sur # sm.zv.sur [ug/Pa]
cp$lg.zv.sur <- cp$lg.cap * cp$lg.mass.sur # lg.zv.sur [ug/Pa]
cp$zv.air <- cp$zvb.air + cp$sm.zv.air + cp$lg.zv.air # zv.air [ug/Pa]
cp$zv.sur <- cp$zvb.sur + cp$sm.zv.sur + cp$lg.zv.sur # zv.sur [ug/Pa]
cp$izv.air <- pmin(1E100,1/cp$zv.air) # izv.air [Pa/ug]
cp$izv.sur <- pmin(1E100,1/cp$zv.sur) # izv.sur [Pa/ug]
cp$yaf <- pmin(cp$diffus.air*cp$z.air/hp$thick.bou,0.0135/(cp$vapor^0.32)) # yaf [mol/(m2-Pa-day)]
cp$cln.air <- cp$izv.air*(cp$sm.zv.air*cp$sm.clean.air+cp$lg.zv.air*cp$lg.clean.air) # cln.air [1/day]
cp$cln.sur <- cp$izv.sur*(cp$sm.zv.sur*cp$sm.clean.sur+cp$lg.zv.sur*cp$lg.clean.sur) # cln.sur [1/day]
cp$sm.dep <- cp$izv.air * hp$area.sur * hp$sm.load.air * hp$sm.depos * cp$sm.cap # sm.dep [1/day]
cp$lg.dep <- cp$izv.air * hp$area.sur * hp$lg.load.air * hp$lg.depos * cp$lg.cap # lg.dep [1/day]
cp$dep <- cp$sm.dep + cp$lg.dep # dep [1/day]
cp$res <- cp$izv.sur*(cp$sm.mass.sur*hp$sm.resus+cp$lg.mass.sur*hp$lg.resus) # res [1/day]
cp$diff.air <- cp$izv.air * cp$ug.mol * hp$area.sur * cp$yaf # diff.air [1/day]
cp$diff.sur <- cp$izv.sur * cp$ug.mol * hp$area.sur * cp$yaf # diff.sur [1/day]
cp$a <- hp$aer.out + cp$decay.air + cp$cln.air + cp$dep + cp$diff.air # a [1/day]
cp$b <- cp$res + cp$diff.sur # b [1/day]
cp$c <- cp$dep + cp$diff.air # c [1/day]
cp$d <- cp$decay.sur + cp$cln.sur + cp$res + cp$diff.sur # d [1/day]
if (g$prog=="y") cat("\n Evaluating flows complete")
return(cp)
}
# eval.fug.concs.an solves the fugacity equations analytically
eval.fug.concs.an = function(chem.release,flows,indoor.hrs,indoor.gaps) {
if (g$prog=="y") cat("\n Starting fugacity calculations")
nc <- nrow(flows)
air.mass <- as.data.table(matrix(0,nrow=8736,ncol=nc))
sur.mass <- as.data.table(matrix(0,nrow=8736,ncol=nc))
waste.mass <- as.data.table(matrix(0,nrow=364,ncol=nc))
window.mass <- as.data.table(matrix(0,nrow=364,ncol=nc))
if(length(indoor.hrs)>0) {
yair <- select_vars(names(chem.release),starts_with("air"))
ysur <- select_vars(names(chem.release),starts_with("sur"))
df <- as.data.frame(chem.release)
new.air <- as.data.table(df[yair]) # chemical mass in mg
new.sur <- as.data.table(df[ysur]) # chemical mass in mg
air <- rep(0,nc)
sur <- rep(0,nc)
was <- rep(0,nc)
win <- rep(0,nc)
fac <- 24
fromair <- flows$a/fac # get hourly flow rates
toair <- flows$b/fac
tosur <- flows$c/fac
fromsur <- flows$d/fac
rterm <- sqrt(fromair^2+4*toair*tosur-2*fromair*fromsur+fromsur^2)
lam1 <- (fromair+fromsur+rterm)/2 # eigenvalues of J
lam2 <- (fromair+fromsur-rterm)/2 # eigenvalues of J
maxlag <- max(indoor.gaps)+1
hrvec <- 1:maxlag
elt1 <- as.data.frame(matrix(0,nrow=maxlag,ncol=nc))
elt2 <- as.data.frame(matrix(0,nrow=maxlag,ncol=nc))
for (c in 1:nc) {
elt1[c] <- as.data.frame.vector(lapply(-lam1[c]*hrvec,exp))
elt2[c] <- as.data.frame.vector(lapply(-lam2[c]*hrvec,exp))
}
towin <- flows$aer.out/fac
cl.air <- flows$cln.air/fac
cl.sur <- flows$cln.sur/fac
for (hrnum in 1:length(indoor.hrs)) {
hr <- as.integer(indoor.hrs[hrnum])
hr1 <- as.integer(max(1,hr-1))
ma0 <- air.mass[hr1] + new.air[hr]
ms0 <- sur.mass[hr1] + new.sur[hr]
k1a <- (2*tosur*ma0+(fromair-fromsur-rterm)*ms0)*(rterm+fromair-fromsur)/(4*tosur*rterm)
k2a <- (2*tosur*ma0+(fromair-fromsur+rterm)*ms0)*(rterm-fromair+fromsur)/(4*tosur*rterm)
k1s <- -(2*tosur*ma0+(fromair-fromsur-rterm)*ms0)/(2*rterm)
k2s <- (2*tosur*ma0+(fromair-fromsur+rterm)*ms0)/(2*rterm)
n <- indoor.gaps[hrnum]+1
if (n>0) {
for (lag in 1:n){
t1 <- as.numeric(unlist(elt1[lag,]))
t2 <- as.numeric(unlist(elt2[lag,]))
row <- as.integer(hr+lag-1)
set(air.mass,row,1:nc,k1a*t1+k2a*t2)
set(sur.mass,row,1:nc,k1s*t1+k2s*t2)
}
}
}
}
day.air <- colMeans(array(as.matrix(air.mass),c(24,364,nc)),dim=1)
day.sur <- colMeans(array(as.matrix(sur.mass),c(24,364,nc)),dim=1)
day.win <- day.air*flows[1]$aer.out
day.was <- day.air*flows[1]$cln.air + day.sur*flows[1]$cln.sur
fug.day <- as.data.table(cbind(1:364,rep(0,364),day.air,day.sur,day.win,day.was))
setnames(fug.day,c("daynum","hour",str_c(c(rep("air",nc),rep("sur",nc),rep("win",nc),rep("was",nc)),rep(1:nc,4))))
padding <- matrix(0,nrow=8736,ncol=2*nc)
fug.hour <- as.data.table(cbind(unlist(lapply(1:364,rep,24)),rep(1:24,364),air.mass,sur.mass,padding))
setnames(fug.hour,c("daynum","hour",str_c(c(rep("air",nc),rep("sur",nc),rep("win",nc),rep("was",nc)),rep(1:nc,4))))
fugs <- rbind(fug.day,fug.hour)
if (g$prog=="y") cat("\n Evaluating fugacity concentrations complete")
return(fugs)
}
# eval.fug.concs.st solves the fugacity equations using a finite time step
eval.fug.concs.st = function(chem.release,flows,days,steps) {
nc <- nrow(flows)
air.mass <- as.data.table(matrix(0,nrow=8736,ncol=nc))
sur.mass <- as.data.table(matrix(0,nrow=8736,ncol=nc))
waste.mass <- as.data.table(matrix(0,nrow=364,ncol=nc))
window.mass <- as.data.table(matrix(0,nrow=364,ncol=nc))
yair <- select_vars(names(chem.release),starts_with("air"))
ysur <- select_vars(names(chem.release),starts_with("sur"))
df <- as.data.frame(chem.release)
new.air <- as.data.table(df[yair]) # chemical mass in mg
new.sur <- as.data.table(df[ysur]) # chemical mass in mg
air <- rep(0,nc)
sur <- rep(0,nc)
was <- rep(0,nc)
win <- rep(0,nc)
fac <- 24*steps
fromair <- 1-exp(-flows$a/fac)
toair <- 1-exp(-flows$b/fac)
tosur <- 1-exp(-flows$c/fac)
fromsur <- 1-exp(-flows$d/fac)
towin <- 1-exp(-flows$aer.out/fac)
cl.air <- 1-exp(-flows$cln.air/fac)
cl.sur <- 1-exp(-flows$cln.sur/fac)
for (day in 1:days) {
win <- win*0
was <- was*0
doff <- 24*(day-1)
for (hr in 1:24) {
i <- as.integer(doff+hr)
air <- air + new.air[i]
sur <- sur + new.sur[i]
for (j in 1:steps) {
air <- air - air*fromair + sur*toair
sur <- sur - sur*fromsur + air*tosur
win <- win + air*towin
was <- was + air*cl.air + sur*cl.sur
}
set(air.mass,i,1:nc,air)
set(sur.mass,i,1:nc,sur)
}
set(window.mass,day,1:nc,win)
set(waste.mass,day,1:nc,was)
}
return(list(air.mass,sur.mass.waste.mass,window.mass))
}
# eval.hm rate sets the hand-to-mouth rate constant for the primary person
# based on results from SHEDS soil-dust model runs (up to age 20)
# top age group extended to all adults
eval.hm.rate = function(prime,q) {
age <- prime$age
if (age==0) {
gm <- 0.20
gsd <- 1.8
} else if (age==1) {
gm <- 0.23
gsd <- 1.7
} else if (age==2) {
gm <- 0.19
gsd <- 1.9
} else if (age>=3 & age<=5) {
gm <- 0.15
gsd <- 1.9
} else if (age>=6 & age<=10) {
gm <- 0.09
gsd <- 2.0
} else if (age>=11 & age<=15) {
gm <- 0.05
gsd <- 2.4
} else if (age>=16) {
gm <- 0.02
gsd <- 3.1
}
gm <- gm/2 # convert from fraction to rate, assuming 2 hrs average before hand washing
kh <- distrib("logn",gm,gsd,q=q)
return(kh)
}
# eval.house.props samples distributions for house characteristics relating to fugacity calculations
eval.house.props <- function(fug.hvars,person.data,q.house) {
if (g$prog=="y") cat("\n Evaluating house props...")
area.sur <- person.data$unitsf/10.7639 # convert from ft2 to m2
f <- fug.hvars[fug.hvars$varname=="aer.out"]
aer.out <- distrib(f$form,f$par1,f$par2,f$par3,f$par4,f$lower.trun,f$upper.trun,f$resamp,q=q.house$aer.out)
f <- fug.hvars[fug.hvars$varname=="height"]
height <- distrib(f$form,f$par1,f$par2,f$par3,f$par4,f$lower.trun,f$upper.trun,f$resamp,q=q.house$height)
f <- fug.hvars[fug.hvars$varname=="lg.carb.f"]
lg.carb.f <- distrib(f$form,f$par1,f$par2,f$par3,f$par4,f$lower.trun,f$upper.trun,f$resamp,q=q.house$lg.carb.f)
f <- fug.hvars[fug.hvars$varname=="lg.clean.air"]
lg.clean.air <- distrib(f$form,f$par1,f$par2,f$par3,f$par4,f$lower.trun,f$upper.trun,f$resamp,q=q.house$lg.clean.air)
f <- fug.hvars[fug.hvars$varname=="lg.clean.sur"]
lg.clean.sur <- distrib(f$form,f$par1,f$par2,f$par3,f$par4,f$lower.trun,f$upper.trun,f$resamp,q=q.house$lg.clean.sur)
f <- fug.hvars[fug.hvars$varname=="lg.depos"]
lg.depos <- distrib(f$form,f$par1,f$par2,f$par3,f$par4,f$lower.trun,f$upper.trun,f$resamp,q=q.house$lg.depos)
f <- fug.hvars[fug.hvars$varname=="lg.load.air"]
lg.load.air <- distrib(f$form,f$par1,f$par2,f$par3,f$par4,f$lower.trun,f$upper.trun,f$resamp,q=q.house$lg.load.air)
f <- fug.hvars[fug.hvars$varname=="lg.load.sur"]
lg.load.sur <- distrib(f$form,f$par1,f$par2,f$par3,f$par4,f$lower.trun,f$upper.trun,f$resamp,q=q.house$lg.load.sur)
lg.load.sur <- 1E4*lg.load.sur # convert from ug/cm2 to ug/m2
f <- fug.hvars[fug.hvars$varname=="lg.resus"]
lg.resus <- distrib(f$form,f$par1,f$par2,f$par3,f$par4,f$lower.trun,f$upper.trun,f$resamp,q=q.house$lg.resus)
f <- fug.hvars[fug.hvars$varname=="sm.carb.f"]
sm.carb.f <- distrib(f$form,f$par1,f$par2,f$par3,f$par4,f$lower.trun,f$upper.trun,f$resamp,q=q.house$sm.carb.f)
f <- fug.hvars[fug.hvars$varname=="sm.clean.air"]
sm.clean.air <- distrib(f$form,f$par1,f$par2,f$par3,f$par4,f$lower.trun,f$upper.trun,f$resamp,q=q.house$sm.clean.air)
f <- fug.hvars[fug.hvars$varname=="sm.clean.sur"]
sm.clean.sur <- distrib(f$form,f$par1,f$par2,f$par3,f$par4,f$lower.trun,f$upper.trun,f$resamp,q=q.house$sm.clean.sur)
f <- fug.hvars[fug.hvars$varname=="sm.depos"]
sm.depos <- distrib(f$form,f$par1,f$par2,f$par3,f$par4,f$lower.trun,f$upper.trun,f$resamp,q=q.house$sm.depos)
f <- fug.hvars[fug.hvars$varname=="sm.load.air"]
sm.load.air <- distrib(f$form,f$par1,f$par2,f$par3,f$par4,f$lower.trun,f$upper.trun,f$resamp,q=q.house$sm.load.air)
f <- fug.hvars[fug.hvars$varname=="sm.load.sur"]
sm.load.sur <- distrib(f$form,f$par1,f$par2,f$par3,f$par4,f$lower.trun,f$upper.trun,f$resamp,q=q.house$sm.load.sur)
sm.load.sur <- 1E4*sm.load.sur # convert from ug/cm2 to ug/m2
f <- fug.hvars[fug.hvars$varname=="sm.resus"]
sm.resus <- distrib(f$form,f$par1,f$par2,f$par3,f$par4,f$lower.trun,f$upper.trun,f$resamp,q=q.house$sm.resus)
f <- fug.hvars[fug.hvars$varname=="temp"]
temp <- distrib(f$form,f$par1,f$par2,f$par3,f$par4,f$lower.trun,f$upper.trun,f$resamp,q=q.house$temp)
f <- fug.hvars[fug.hvars$varname=="thick.bou"]
thick.bou <- distrib(f$form,f$par1,f$par2,f$par3,f$par4,f$lower.trun,f$upper.trun,f$resamp,q=q.house$thick.bou)
f <- fug.hvars[fug.hvars$varname=="thick.sur"]
thick.sur <- distrib(f$form,f$par1,f$par2,f$par3,f$par4,f$lower.trun,f$upper.trun,f$resamp,q=q.house$thick.sur)
house <- person.data$house
house.props <- as.data.table(cbind(area.sur,aer.out,height,lg.carb.f,lg.clean.air,lg.clean.sur,
lg.depos,lg.load.air,lg.load.sur,lg.resus,sm.carb.f,sm.clean.air,sm.clean.sur,
sm.depos,sm.load.air,sm.load.sur,sm.resus,temp,thick.bou,thick.sur,house))
setnames(house.props,c("area.sur","aer.out","height","lg.carb.f","lg.clean.air","lg.clean.sur",
"lg.depos","lg.load.air","lg.load.sur","lg.resus","sm.carb.f","sm.clean.air","sm.clean.sur",
"sm.depos","sm.load.air","sm.load.sur","sm.resus","temp","thick.bou","thick.sur","house"))
return(house.props)
}
# eval.indirect calculates the indirect exposures on a daily basis
eval.indirect = function(d,fug.hour,dermal.rates,hp,nc,prime,fug.cvars,chem.list) {
# first, find the status for the primary person at each hour
# 3 options: sleep, awake, out
# each hour of yearF has # minutes in each: min.sleep, min.awake, min.out
# start.min and end.min refer to status, not to product use
df <- as.data.frame(d)
df$dayofweek <- 1 + (df$daynum-1) %% 7
df$weekend <- 0
df$weekend[df$dayofweek>5] <- 1
df$start.min <- 60*(df$hournum-1)+round(60*(1+df$start-df$hour))
df$end.min <- lead(df$start.min)-1
df$end.min[is.na(df$end.min)]<- 8736*60
# activites: -1=idle, -2=PUC, 1,2=commute, 3,4,5=eat B,D,Lunch, 6=sleep, 7=work
# status : 1=sleep, 2=awake, 3=away from home
df$status <- 2
df$status[df$activity_code==6] <- 1
df$status[df$activity_code==7] <- 3
df$status[df$activity_code==1] <- 3
df$status[df$activity_code==2] <- 3
df$status[df$activity_code==5] <- 3 # assume lunch is away from home, other meals at home
status.min <- rep(1,8736*60) # status.min has value for each minute of year
for (i in 1:nrow(df)) {
status.min[df$start.min[i]:df$end.min[i]] <- df$status[i]
}
fh <- as.data.frame(fug.hour)
fh$min.sleep <- rep(0,8736)