-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path3_sensitivity_analysis.R
More file actions
1584 lines (1349 loc) · 80.1 KB
/
3_sensitivity_analysis.R
File metadata and controls
1584 lines (1349 loc) · 80.1 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
# Using smart thermostats to reduce indoor exposure to wildfire fine particulate matter (PM2.5)
# Cite: https://doi.org/10.1016/j.indenv.2025.100088
# ----
# TASK: Use a Monte Carlo simulation to find the model parameters that are influencing indoor PM2.5 concentration/exposure
# Code Authors: Federico Dallo, Thomas Parkinson, Carlos Duarte, Chai Yoon Um, Paul Raftery
# ----
#### SETUP ####
# use pacman for package management
library(pacman)
# load packages
pacman::p_load(tidyverse, here, lubridate, # essential
scales, patchwork, # plots
randtoolbox, # sobol
zoo, # timeseries
sf, # spatial data
pbapply, # progress bars
furrr, future.apply # parallel calculation
) # animate map
# Use "here" package to set working directory
here::i_am("healthRiskADAPT.Rproj")
#### LOAD SIMULATION DATA ####
# cleanup
rm(list = ls())
set.seed(42) # Ensure reproducibility
# get list of EPA stations
if(!file.exists(here("data", "sens_analysis", "grid_data.csv"))) {
# load tract dataset
df_tract <- read_rds(here("data", "US", "CA", "df_tract.rds")) %>%
select(!geometry)
# Define the number of simulations
n_sim <- 25
# Define distributions for house parameters (example)
# Assume normal distributions for parameters or based on your data
##### Floor Area #####
# ggplot(df_tract, aes(x = "", y = house_area)) +
# geom_violin(fill = "lightblue", color = "black") +
# labs(title = "Violin Plot of House Area",
# x = "",
# y = "House Area (sq. ft.)") +
# theme_minimal()
# ggplot(df_tract, aes(x = "", y = house_area)) +
# geom_boxplot(fill = "lightblue", color = "black") +
# labs(title = "Violin Plot of House Area",
# x = "",
# y = "House Area (sq. ft.)") +
# theme_minimal()
# ggplot(data.frame(df_tract), aes(x = house_area)) +
# geom_histogram(bins = 50, fill = "lightblue", color = "black", alpha = 0.7) +
# labs(title = "Monte Carlo Sampled Distribution of House Area",
# x = "House Area (sq. ft.)",
# y = "Frequency") +
# theme_minimal()
###### MC house area ######
# Generate Monte Carlo samples (e.g., 1,000)
# mc_house_area_samples <- sample(df_tract$house_area, size = n_sim, replace = TRUE)
# Plot the Monte Carlo Sample Distribution
# ggplot(data.frame(mc_house_area_samples), aes(x = mc_house_area_samples)) +
# geom_histogram(bins = 50, fill = "lightblue", color = "black", alpha = 0.7) +
# labs(title = "Monte Carlo Sampled Distribution of House Area",
# x = "House Area (sq. ft.)",
# y = "Frequency") +
# theme_minimal()
# Combine original and MC sample data into a dataframe
# df_plot <- data.frame(
# value = c(df_tract$house_area, mc_house_area_samples),
# type = c(rep("Original Data", length(df_tract$house_area)),
# rep("Monte Carlo Samples", length(mc_house_area_samples)))
# )
# Create the histogram
# ggplot(df_plot, aes(x = value, y = ..density.., fill = type)) +
# geom_histogram(position = "identity", alpha = 0.5, bins = 50, color = "black") +
# scale_fill_manual(values = c("Original Data" = "blue", "Monte Carlo Samples" = "red")) +
# labs(title = "Comparison of House Area: Original vs Monte Carlo Samples",
# x = "House Area (sq. ft.)",
# #y = "Frequency",
# y = "Density", # if y = ..density..,, otherwise "Frequency"
# fill = "Dataset") +
# theme_minimal()
###### Sobol House Area ######
# Generate Sobol sequence (between 0 and 1)
sobol_samples <- sobol(n = n_sim, dim = 1)
# Scale Sobol samples to match `house_area` range
house_min <- min(df_tract$house_area, na.rm = TRUE)
house_max <- max(df_tract$house_area, na.rm = TRUE)
# Convert Sobol samples to closest real `house_area` values using quantiles
sobol_house_area_samples <- quantile(df_tract$house_area, sobol_samples, type = 8)
# Combine original and Sobol sampled data
# df_plot <- data.frame(
# value = c(df_tract$house_area, sobol_house_area_samples),
# type = c(rep("Original Data", length(df_tract$house_area)),
# rep("Sobol Samples", length(sobol_house_area_samples)))
# )
# Plot Histogram for Comparison
# ggplot(df_plot, aes(x = value, y = ..density.., fill = type)) +
# geom_histogram(position = "identity", alpha = 0.5, bins = 50, color = "black") +
# scale_fill_manual(values = c("Original Data" = "blue", "Sobol Samples" = "green")) +
# labs(title = "Comparison of House Area: Original vs Sobol Samples",
# x = "House Area (sq. m)",
# #y = "Frequency",
# y = "Density", # if y = ..density..,, otherwise "Frequency"
# fill = "Dataset") +
# theme_minimal()
rm(house_max, house_min, sobol_samples)
##### Effective Leakage #####
# Generate Sobol sequence (between 0 and 1)
sobol_samples <- sobol(n = n_sim, dim = 1)
# Scale Sobol samples to match `house_height` range
house_min <- min(df_tract$effect_leakage, na.rm = TRUE)
house_max <- max(df_tract$effect_leakage, na.rm = TRUE)
# Convert Sobol samples to closest real `house_height` values using quantiles
sobol_effect_leakage_samples <- quantile(df_tract$effect_leakage[!is.na(df_tract$effect_leakage)], sobol_samples, type = 8)
# Combine original and Sobol sampled data
#df_plot <- data.frame(
# value = c(df_tract$effect_leakage, sobol_effect_leakage_samples),
# type = c(rep("Original Data", length(df_tract$effect_leakage)),
# rep("Sobol Samples", length(sobol_effect_leakage_samples)))
#)
# Plot Histogram for Comparison
#ggplot(df_plot, aes(x = value, y = ..density.., fill = type)) +
# geom_histogram(position = "identity", alpha = 0.5, bins = 50, color = "black") +
# scale_fill_manual(values = c("Original Data" = "blue", "Sobol Samples" = "green")) +
# labs(title = "Comparison of House Effective Leakage: Original vs Sobol Samples",
# x = "Square m",
# #y = "Frequency",
# y = "Density", # if y = ..density..,, otherwise "Frequency"
# fill = "Dataset") +
# theme_minimal()
rm(house_max, house_min, sobol_samples)
##### MERV FILTERS #####
"
We can use MERV 8, MERV 10, MERV 13, MERV 16
"
##### geoid RANDOM SAMPLE n_sim tracts #####
mc_geoid_samples <- sample(df_tract$geoid, size = n_sim)
##### I/O ratio as first guess #####
"
We can use 0.25, 0.5, 0.75
"
##### Combinations #####
grid_data <- expand_grid(c(0.25, 0.5, 0.75), c(8,10,13,16), mc_geoid_samples, sobol_house_area_samples, sobol_effect_leakage_samples)
grid_data <- grid_data %>%
mutate(test = row_number())
names(grid_data) <- c("start_IO", "MERV", "geoid", "house_area", "effect_leakage", "test")
grid_data <- grid_data %>%
select(test, geoid, start_IO, MERV, house_area, effect_leakage)
write_csv(grid_data, file = here("data", "sens_analysis", "grid_data.csv"))
} else {
grid_data <- read_csv(file = here("data", "sens_analysis", "grid_data.csv"))
}
#### simulations from 7 to 13 sept 2020 ####
#### DATA PREP ####
## FILTER ##
# build table of MERV filters
df_filter <- tibble(filter_merv = c(0, 8, 9, 10, 11, 12, 13, 14, 15, 16),
filter_eff = c(0, 0.2, 0.35, 0.5, 0.65, 0.8, 0.85, 0.9, 0.9, 0.95),
filter_pres = c(0, 0.056, 0.056, 0.056, 0.088, 0.088, 0.088, 0.120, 0.120, 0.120)) # pressure loss
## TRACTS ##
# load tract data
df_tract <- read_rds(here("data", "US", "CA", "df_tract.rds"))
df_tract <- df_tract %>% st_drop_geometry()
# heating load for air flow rate##
df_tract <- df_tract %>%
mutate(heat_load = case_when(climate_ashrae == "2B" ~ 40,
climate_ashrae == "3B" ~ 45,
climate_ashrae == "3C" ~ 45,
climate_ashrae == "4B" ~ 50,
climate_ashrae == "5B" ~ 60,
climate_ashrae == "6B" ~ 65))
# NOTE: (county_new) We approximate the indoor temperature for the 8 counties without Ecobee users
# using the indoor temperature of homes from a close county.
# The selection was manual, considering geographical similarities (flatland, mountain range, etc.)
# when multiple choices were possible.
# The new variable for county is overwritten
df_tract <- df_tract %>%
mutate(county = case_when(county == "colusa" | county == "glenn" | county == "tehama" ~ "sutter",
county == "del norte" | county == "mendocino" | county == "trinity" ~ "humboldt",
county == "mariposa" ~ "tuolumne",
county == "modoc" ~ "lassen",
TRUE ~ county))
#### Summary stats for the original df_tract ####
# house area
df_tract %>%
summarise(
q25 = round(quantile(house_area, 0.25, na.rm = TRUE)),
q50 = round(quantile(house_area, 0.50, na.rm = TRUE)),
q75 = round(quantile(house_area, 0.75, na.rm = TRUE))
)
# house leakage
df_tract %>%
summarise(
q25 = round(quantile(effect_leakage, 0.25, na.rm = TRUE), digits = 4),
q50 = round(quantile(effect_leakage, 0.50, na.rm = TRUE), digits = 4),
q75 = round(quantile(effect_leakage, 0.75, na.rm = TRUE), digits = 4)
)
#### grid_data to create synthetic sample ####
df_tract <- grid_data %>%
left_join(df_tract %>% select(geoid,
tract,
county,
#geometry,
lon,
lat,
climate_ca,
climate_ashrae,
isd_station,
epa_station,
population,
median_income,
house_value,
poverty_perc,
house_total,
house_detached,
house_storey,
house_height,
poverty_status,
heat_load
), by = "geoid") %>% glimpse()
df_tract %>% select(house_height) %>% group_by(house_height) %>% summarise(n())
# "
# The house that are 3 mt height are 95%, let's not consider the 5.5 mt
# "
# df_tract <- df_tract %>%
# filter(house_height == 3)
# calculate norm_leakage, effect_leakage for tracts
df_tract <- df_tract %>%
mutate(house_volume = house_area * house_height, # redo house volume for tract with missing house area
floor_area_ft2 = house_area * 10.764, # convert from m2 to ft2
pred_heat_load_btu_hr = round((floor_area_ft2*heat_load) / 3000) * 3000,
pred_air_flow_rate_cfm_hi = round((pred_heat_load_btu_hr / (60 * ((0.06965 + 0.07935)/2) * ((0.2404 + 0.2401)/2) * 70)) / 50) * 50,
design_furnace = pred_air_flow_rate_cfm_hi * (0.3048^3)) # convfrt from cfm to cmm
# calculate norm_leakage, effect_leakage for tracts
df_tract <- df_tract %>%
mutate(house_volume = house_area * house_height, # redo house volume for tract with missing house area
floor_area_ft2 = house_area * 10.764, # convert from m2 to ft2
pred_heat_load_btu_hr = round((floor_area_ft2*heat_load) / 3000) * 3000,
pred_air_flow_rate_cfm_hi = round((pred_heat_load_btu_hr / (60 * ((0.06965 + 0.07935)/2) * ((0.2404 + 0.2401)/2) * 70)) / 50) * 50,
design_furnace = pred_air_flow_rate_cfm_hi * (0.3048^3)) # convfrt from cfm to cmm
#### only keep columns needed for pm2.5 calculation ####
df_tract <- df_tract %>%
select(test, start_IO, MERV, geoid, county, epa_station, isd_station, house_height, house_volume, house_area, effect_leakage, design_furnace)
#### ISD ####
# load isd dataset
df_isd <- read_rds(here("data", "US", "df_isd.rds"))
## expand to 10 min timesteps and interpolate pm2.5
df_isd <- df_isd %>%
group_by(isd_station) %>%
arrange(datetime, .by_group = TRUE) %>%
complete(datetime = seq(min(datetime), max(datetime), by = "10 min")) %>% #NOTE: find the median on ecobee dataset for 2020 wildfire season
mutate(wind_speed = na.approx(wind_speed, rule = 2),
t_out = na.approx(t_out, rule = 2)) %>%
ungroup()
#### EPA PM2.5 ####
# load epa data
df_epa <- read_rds(here("data", "US", "df_epa.rds"))
# replace NaNs with interpolated values
df_epa <- df_epa %>%
mutate(pm_25_out = ifelse(is.nan(pm_25_out), NA, pm_25_out),
pm_25_out = na.approx(pm_25_out, rule = 2))
## expand to 10 min timesteps and interpolate pm2.5
df_epa <- df_epa %>%
group_by(epa_station) %>%
arrange(datetime, .by_group = TRUE) %>%
complete(datetime = seq(min(datetime), max(datetime), by = "10 min")) %>%
mutate(pm_25_out = na.approx(pm_25_out, rule = 2)) %>%
ungroup()
#### ECOBEE ####
# load ecobee data for entire CA
df_ecobee <- read_rds(here("data", "US", "df_ecobee.rds"))
# make averages for date_time and county
df_ecobee <- df_ecobee %>%
drop_na(county) %>% # remove row with county == NA
group_by(date_time, county) %>%
summarise(t_in = mean(t_ctrl, na.rm = TRUE)) %>%
ungroup()
# expand to 10 mins and interpolate temperatures
df_ecobee <- df_ecobee %>%
group_by(county) %>%
arrange(date_time, .by_group = TRUE) %>%
complete(date_time = seq(min(date_time), max(date_time), by = "10 min")) %>%
mutate(t_in = na.approx(t_in, rule = 2)) %>%
ungroup()
# fan runtime for the baseline scenario
df_counties_baseline_run <- read_rds(here("data", "US", "df_counties_baseline_run.rds"))
#### DYNAMIC PM2.5 MODEL ####
# define list of model constants for stack and wind effects
#NOTE# values pulled from https://doi.org/10.1038/jes.2016.49 and ** from supporting information
model_const <- lst(c_g = 9.8, # gravitational acceleration (m2/s)
c_r = 0.5, # fraction of the total leackage area (unitless)**
c_t0 = 298, # reference temperature (K)
c_x = 0.25, # difference between the fraction of the total leakage area that is in the ceiling and the floor (unitless)**
c_c = 0.19, # shielding parameter, referring to the wind shielding resulting from obstructions immediately around the house (unitless)**
c_factor = 10, # factor (m)
c_a = 0.67, # related to the “terrain class”, which characterizes the general neighborhood where the home is situated (unitless)**
c_b = 0.25) # related to the “terrain class”, which characterizes the general neighborhood where the home is situated (unitless)**
# make data directory for the sens_analysis simulations
if(!dir.exists(here("data/sens_analysis/sa_simulations/"))) {
dir.create(here("data/sens_analysis/sa_simulations/"))
}
# check with geoid are already present in the directory to don't redo the simulations
# get list of simulation files
# list_sim_file <- list.files(path = here("data/sens_analysis/sa_simulations/"), pattern = "[0-9].rds", full.names = FALSE) %>%
# str_replace(".rds", "")
#
# df_tract_reduced <- df_tract %>%
# filter(!test %in% list_sim_file)
# focus on specific time
start_time_sens_analysis <- as.POSIXct("2020-09-07 00:00:00")
end_time_sens_analysis <- as.POSIXct("2020-09-11 00:00:00")
# parallel
plan(multisession, workers = parallel::detectCores() - 1) # Use available cores
# run loop for each census tract
#for(i in seq_len(nrow(df_tract))){
#for(i in seq_len(5)){
#for(i in seq_len(nrow(df_tract_reduced))){
process_tract <- function(i) { # for parallelization
print(i)
# get tract info
sim_house <- df_tract %>%
slice(i)
# print
print(sim_house$test)
# use pm2.5 data to build time series
sim_house <- df_epa %>%
left_join(sim_house, ., by = "epa_station")
# add indoor temperatures from ecobee dataset
# NOTE: see note at: (goto "county_new")
sim_house <- df_ecobee %>%
left_join(sim_house, ., by = c("county", "datetime" = "date_time")) %>%
drop_na(t_in)
# add outdoor temperature and windspeed from ISD dataset
sim_house <- df_isd %>%
left_join(sim_house, ., by = c("isd_station", "datetime")) %>%
drop_na(t_out)
# calculate specific infiltration (m/s), aer
sim_house <- sim_house %>%
mutate(d_t = as.numeric(difftime(datetime, lag(datetime, n = 1))), # timesteps
d_t = replace_na(d_t, median(d_t, na.rm = TRUE)),
stack_effect = ((1 + (model_const$c_r/2) / 3) * (1 - (model_const$c_x^2)/(2 - model_const$c_r)^2)^1.5 * ((model_const$c_g * house_height) / model_const$c_t0)^0.5),
wind_effect = (model_const$c_c * (1 - model_const$c_r)^0.333) * (model_const$c_a * (house_height / 10)^model_const$c_b),
s_inf = sqrt(stack_effect^2 * abs(t_out - t_in) + wind_effect^2 * wind_speed^2), # specific infiltration (https://doi.org/10.1038/jes.2016.49, equation nr.7) #FEDE# abs()???
q_f = effect_leakage * s_inf * 3600, # airflow due to infiltration through small unintentional openings (m3/h) (https://doi.org/10.1038/jes.2016.49, equation nr.3)
q_nat = 0, # set natural ventilation to 0 (assume closed windows)
q_tot = sqrt(q_f^2 + q_nat^2),
aer = q_tot / house_volume, # 1/hr air changes per hour
v_infil = aer * house_volume / 60 * d_t, # m3, 60 for having aer/min
house_aspect_ratio = 1.2) # [-] aspect ratio of the house (l/w)
# only keep columns needed for pm2.5 calculations
sim_house <- sim_house %>%
select(test, start_IO, MERV, county, datetime, d_t, house_volume, house_area, v_infil, design_furnace, pm_25_out)
# Focus on specific interval
sim_house <- sim_house %>%
filter(datetime >= start_time_sens_analysis & datetime < end_time_sens_analysis)
# add the ecobee county fan runtime
sim_house <- sim_house %>%
left_join(., df_counties_baseline_run, by = c("county", "datetime" = "date_time"))
# define model variables
sim_house <- sim_house %>%
mutate(pm25_pene = 0.75, # penetration factor of PM 2.5 particles
pm25_dep = 0.5, # [- per hour] deposition per hour
filter_merv = MERV, # merv rating # THIS IS NEW FOR THE SENS ANALYSIS
n_lr = 0.15, # leakage as a percentage of v_fan at the return side duct
n_ls = 0.15, # leakage as a percentage of v_fan at the supply side duct
#pac_cadr = 156, # cfm median CADR for portable air cleaners (source: df_pac.csv)
pac_cadr = 156 * (0.3048^3), # m3/min
pac_filt_eff = 0.9997, # FEDE # check this number for EPA filter
pac_sqft = 242, # median room size sqft raccomandation (source: df_pac.csv)
pac_mq = round(pac_sqft * 0.092903, digits = 1), # 0.092903 is the conversion factor from sqft to m2
pac_norm = pac_mq / house_area # normalized area
) %>%
left_join(., df_filter, by = "filter_merv")
# prepare other values for calculation
sim_house <- sim_house %>%
arrange(datetime) %>%
mutate(
# global
v_de = house_volume * pm25_dep/60 * d_t, #m3
# baseline
c_in_baseline = if_else(row_number() == 1, pm_25_out*start_IO, NA_real_), # estimate initial indoor pm2.5 concentration (#FEDE 0.5 instead of 0.35 - be conservative) ### SENSITIVITY ANALYSIS WE CAN USE start_IO
v_fan_baseline = ifelse(minute(datetime) < 0, yes = 1, no = 0) * design_furnace * d_t, # m3
v_da_baseline = v_fan_baseline * ((1 - filter_pres) - n_ls),
v_ra_baseline = (v_infil + v_da_baseline) * as.integer(v_fan_baseline > 0), # m3
c_bf_baseline = 0, # (v_ra_baseline*c_in_baseline + v_fan_baseline*n_lr*pm_25_out) / (v_ra_baseline + v_fan_baseline*n_lr),
c_af_baseline = c_bf_baseline*(1 - filter_eff),
# 10-min hvac
c_in_hvac = if_else(row_number() == 1, pm_25_out*start_IO, NA_real_), # ug/m3 estimate initial indoor pm2.5 concentration
v_fan_hvac = ifelse(minute(datetime) < 10, yes = 1, no = 0) * design_furnace * d_t, # m3 ##FEDE remove hard coded 10..
v_da_hvac = v_fan_hvac * ((1 - filter_pres) - n_ls), # m3
v_ra_hvac = (v_infil + v_da_hvac) * as.integer(v_fan_hvac > 0), # m3, second part evaluating that is non-zero only when HVAC is ON
c_bf_hvac = case_when( (v_ra_hvac + v_fan_hvac*n_lr) > 0 ~ (v_ra_hvac*c_in_hvac + v_fan_hvac*n_lr*pm_25_out)/(v_ra_hvac + v_fan_hvac*n_lr), # ug/m3
(v_ra_hvac + v_fan_hvac*n_lr) == 0 ~ 0,
TRUE ~ 0
),
c_af_hvac = c_bf_hvac*(1 - filter_eff), # ug/m3
# baseline county hvac NEW
c_in_base_hvac = if_else(row_number() == 1, pm_25_out*start_IO, NA_real_), # ug/m3 estimate initial indoor pm2.5 concentration
v_fan_base_hvac = fan_baseline_logic * design_furnace * d_t, # m3
v_da_base_hvac = v_fan_base_hvac * ((1 - filter_pres) - n_ls), # m3
v_ra_base_hvac = (v_infil + v_da_base_hvac) * as.integer(v_fan_base_hvac > 0), # m3, second part evaluating that is non-zero only when HVAC is ON
c_bf_base_hvac = case_when( (v_ra_base_hvac + v_fan_base_hvac*n_lr) > 0 ~ (v_ra_base_hvac*c_in_base_hvac + v_fan_base_hvac*n_lr*pm_25_out)/(v_ra_base_hvac + v_fan_base_hvac*n_lr), # ug/m3
(v_ra_base_hvac + v_fan_base_hvac*n_lr) == 0 ~ 0,
TRUE ~ 0
),
c_af_base_hvac = c_bf_base_hvac*(1 - filter_eff), # ug/m3
# control logic hvac with BASELINE and outdoor PM2.5 >= 35
c_in_base_out_logic = if_else(row_number() == 1, pm_25_out*start_IO, NA_real_), # ug/m3 estimate initial indoor pm2.5 concentration
#v_fan_logic = ifelse(pm_25_out > 35, yes = 1, no = 0) * design_furnace * d_t, # m3
v_fan_base_out_logic = case_when(fan_baseline_logic == 1 ~ 1, # when the HVAC is running for heating/cooling
pm_25_out >= 35 ~ 1,
pm_25_out < 35 ~ 0,
TRUE ~ 0) * design_furnace * d_t, # m3
v_da_base_out_logic = v_fan_base_out_logic * ((1 - filter_pres) - n_ls), # m3
v_ra_base_out_logic = (v_infil + v_da_base_out_logic) * as.integer(v_fan_base_out_logic > 0), # m3
#c_bf_logic = (v_ra_logic*c_in_logic + v_fan_logic*n_lr*pm_25_out)/(v_ra_logic + v_fan_logic*n_lr), # ug/m3 # we have zero division
c_bf_base_out_logic = case_when((v_ra_base_out_logic + v_fan_base_out_logic*n_lr) > 0 ~ (v_ra_base_out_logic*c_in_base_out_logic + v_fan_base_out_logic*n_lr*pm_25_out)/(v_ra_base_out_logic + v_fan_base_out_logic*n_lr),
(v_ra_base_out_logic + v_fan_base_out_logic*n_lr) == 0 ~ 0,
TRUE ~ 0
),
c_af_base_out_logic = c_bf_base_out_logic*(1 - filter_eff), # ug/m3
# control logic hvac with BASELINE and indoor PM2.5 >= 5
c_in_base_in_logic = if_else(row_number() == 1, pm_25_out*start_IO, NA_real_), # ug/m3 estimate initial indoor pm2.5 concentration
#v_fan_logic = ifelse(pm_25_out > 35, yes = 1, no = 0) * design_furnace * d_t, # m3
v_fan_base_in_logic = case_when(fan_baseline_logic == 1 ~ 1, # when the HVAC is running for heating/cooling
c_in_base_in_logic >= 5 ~ 1,
c_in_base_in_logic < 5 ~ 0,
TRUE ~ 0) * design_furnace * d_t, # m3
v_da_base_in_logic = v_fan_base_in_logic * ((1 - filter_pres) - n_ls), # m3
v_ra_base_in_logic = (v_infil + v_da_base_in_logic) * as.integer(v_fan_base_in_logic > 0), # m3
#c_bf_logic = (v_ra_logic*c_in_logic + v_fan_logic*n_lr*pm_25_out)/(v_ra_logic + v_fan_logic*n_lr), # ug/m3 # we have zero division
c_bf_base_in_logic = case_when((v_ra_base_in_logic + v_fan_base_in_logic*n_lr) > 0 ~ (v_ra_base_in_logic*c_in_base_in_logic + v_fan_base_in_logic*n_lr*pm_25_out)/(v_ra_base_in_logic + v_fan_base_in_logic*n_lr),
(v_ra_base_in_logic + v_fan_base_in_logic*n_lr) == 0 ~ 0,
TRUE ~ 0
),
c_af_base_in_logic = c_bf_base_in_logic*(1 - filter_eff), # ug/m3
# control logic hvac NO BASELINE FAN RUNTIME - ONLY AQ MODE
c_in_logic = if_else(row_number() == 1, pm_25_out*start_IO, NA_real_), # ug/m3 estimate initial indoor pm2.5 concentration
#v_fan_logic = ifelse(pm_25_out > 35, yes = 1, no = 0) * design_furnace * d_t, # m3
v_fan_logic = case_when(pm_25_out >= 35 & c_in_logic >= 5 ~ 1,
pm_25_out >= 35 & c_in_logic < 5 ~ 0,
pm_25_out < 35 & c_in_logic >= 5 ~ 1,
pm_25_out < 35 & c_in_logic < 5 ~ 0,
TRUE ~ 0
) * design_furnace * d_t, # m3
v_da_logic = v_fan_logic * ((1 - filter_pres) - n_ls), # m3
v_ra_logic = (v_infil + v_da_logic) * as.integer(v_fan_logic > 0), # m3
#c_bf_logic = (v_ra_logic*c_in_logic + v_fan_logic*n_lr*pm_25_out)/(v_ra_logic + v_fan_logic*n_lr), # ug/m3 # we have zero division
c_bf_logic = case_when((v_ra_logic + v_fan_logic*n_lr) > 0 ~ (v_ra_logic*c_in_logic + v_fan_logic*n_lr*pm_25_out)/(v_ra_logic + v_fan_logic*n_lr),
(v_ra_logic + v_fan_logic*n_lr) == 0 ~ 0,
TRUE ~ 0
),
c_af_logic = c_bf_logic*(1 - filter_eff), # ug/m3
# Portable Air Purifier
v_fan_pac = ifelse(minute(datetime) <= 60 , yes = 1, no = 0) * ( pac_cadr / pac_filt_eff * pac_norm) * d_t, # m3
v_da_pac = v_fan_pac, # m3
v_ra_pac = v_fan_pac, # m3
c_in_pac = if_else(row_number() == 1, pm_25_out*start_IO, NA_real_), # estimate initial indoor pm2.5 concentration
)
# run simulation loop
{
tmp_sim_startime <- Sys.time()
for (i in seq_len(nrow(sim_house)-1)) {
# baseline, no HVAC
sim_house[i+1,"c_bf_baseline"] <- (sim_house$v_ra_baseline[i]*sim_house$c_in_baseline[i] + sim_house$v_fan_baseline[i]*sim_house$n_lr[i]*sim_house$pm_25_out[i]) /
(sim_house$v_ra_baseline[i] + sim_house$v_fan_baseline[i]*sim_house$n_lr[i])
sim_house[i+1,"c_bf_baseline"] <- ifelse(is.na(sim_house$c_bf_baseline[i]), 0, sim_house$c_bf_baseline[i])
sim_house[i+1,"c_af_baseline"] <- sim_house$c_bf_baseline[i]*( 1 - (sim_house$filter_eff[i]) )
sim_house[i+1,"c_in_baseline"] <- (
sim_house$c_in_baseline[i]*sim_house$house_volume[i] - sim_house$v_ra_baseline[i]*(sim_house$c_in_baseline[i]) +
sim_house$v_da_baseline[i]*sim_house$c_af_baseline[i] + sim_house$v_infil[i]*sim_house$pm_25_out[i]*sim_house$pm25_pene[i] - # NOTE 75% particle go trough the walls
sim_house$v_infil[i]*(sim_house$c_in_baseline[i]) - sim_house$v_de[i]*(sim_house$c_in_baseline[i])
) / sim_house$house_volume[i]
sim_house[i+1,"c_in_baseline"] <- ifelse(sim_house[i+1,"c_in_baseline"] < 0, 0, sim_house[i+1,"c_in_baseline"])
# HVAC running for fixed mins/hr
### 1- Forecast the indoor concentration at the next step
sim_house[i+1,"c_in_hvac"] <- (
sim_house$c_in_hvac[i]*sim_house$house_volume[i] - sim_house$v_ra_hvac[i]*(sim_house$c_in_hvac[i]) +
sim_house$v_da_hvac[i]*sim_house$c_af_hvac[i] + sim_house$v_infil[i]*sim_house$pm_25_out[i]*sim_house$pm25_pene[i] - # NOTE 75% particle go trough the walls
sim_house$v_infil[i]*(sim_house$c_in_hvac[i]) - sim_house$v_de[i]*(sim_house$c_in_hvac[i])
) / sim_house$house_volume[i] # ug/m3
sim_house[i+1,"c_in_hvac"] <- ifelse(sim_house[i+1,"c_in_hvac"] < 0, 0, sim_house[i+1,"c_in_hvac"]) # NOTE.. is the deposition making the value of concentration going below zero?
### 2- Calculate concentration on ducts *Fan runtime is known and fixed as well as V_da and V_ra
sim_house[i+1,"c_bf_hvac"] <- case_when( (sim_house$v_ra_hvac[i+1] + sim_house$v_fan_hvac[i+1]*sim_house$n_lr[i+1]) > 0 ~ (sim_house$v_ra_hvac[i+1]*sim_house$c_in_hvac[i+1] + sim_house$v_fan_hvac[i+1]*sim_house$n_lr[i+1]*sim_house$pm_25_out[i+1]) /
(sim_house$v_ra_hvac[i+1] + sim_house$v_fan_hvac[i+1]*sim_house$n_lr[i+1]),
(sim_house$v_ra_hvac[i+1] + sim_house$v_fan_hvac[i+1]*sim_house$n_lr[i+1]) == 0 ~ 0,
TRUE ~ 0
)
sim_house[i+1,"c_af_hvac"] <- sim_house$c_bf_hvac[i+1]*( 1 - (sim_house$filter_eff[i+1]) )
# HVAC running using the average run time of the County
### 1- Forecast the indoor concentration at the next step
sim_house[i+1,"c_in_base_hvac"] <- (
sim_house$c_in_base_hvac[i]*sim_house$house_volume[i] - sim_house$v_ra_base_hvac[i]*(sim_house$c_in_base_hvac[i]) +
sim_house$v_da_base_hvac[i]*sim_house$c_af_base_hvac[i] + sim_house$v_infil[i]*sim_house$pm_25_out[i]*sim_house$pm25_pene[i] - # NOTE 75% particle go trough the walls
sim_house$v_infil[i]*(sim_house$c_in_base_hvac[i]) - sim_house$v_de[i]*(sim_house$c_in_base_hvac[i])
) / sim_house$house_volume[i] # ug/m3
sim_house[i+1,"c_in_base_hvac"] <- ifelse(sim_house[i+1,"c_in_base_hvac"] < 0, 0, sim_house[i+1,"c_in_base_hvac"]) # Correct if the deposition is making the value of concentration going below zero?
### 2- Calculate concentration on ducts *Fan runtime is known and fixed as well as V_da and V_ra
sim_house[i+1,"c_bf_base_hvac"] <- case_when( (sim_house$v_ra_base_hvac[i+1] + sim_house$v_fan_base_hvac[i+1]*sim_house$n_lr[i+1]) > 0 ~ (sim_house$v_ra_base_hvac[i+1]*sim_house$c_in_base_hvac[i+1] + sim_house$v_fan_base_hvac[i+1]*sim_house$n_lr[i+1]*sim_house$pm_25_out[i+1]) /
(sim_house$v_ra_base_hvac[i+1] + sim_house$v_fan_base_hvac[i+1]*sim_house$n_lr[i+1]),
(sim_house$v_ra_base_hvac[i+1] + sim_house$v_fan_base_hvac[i+1]*sim_house$n_lr[i+1]) == 0 ~ 0,
TRUE ~ 0
)
sim_house[i+1,"c_af_base_hvac"] <- sim_house$c_bf_base_hvac[i+1]*( 1 - (sim_house$filter_eff[i+1]) )
# HVAC control logic over baseline and when outdoor PM2.5 >= 35 ug/m3
### 1- Forecast the indoor concentration at the next step
sim_house[i+1,"c_in_base_out_logic"] <- (
sim_house$c_in_base_out_logic[i]*sim_house$house_volume[i] - sim_house$v_ra_base_out_logic[i]*(sim_house$c_in_base_out_logic[i]) +
sim_house$v_da_base_out_logic[i]*sim_house$c_af_base_out_logic[i] + sim_house$v_infil[i]*sim_house$pm_25_out[i]*sim_house$pm25_pene[i] - # NOTE 75% particle go trough the walls
sim_house$v_infil[i]*(sim_house$c_in_base_out_logic[i]) - sim_house$v_de[i]*(sim_house$c_in_base_out_logic[i])
) / sim_house$house_volume[i] # ug/m3
sim_house[i+1,"c_in_base_out_logic"] <- ifelse(sim_house[i+1,"c_in_base_out_logic"] < 0, 0, sim_house[i+1,"c_in_base_out_logic"])
### 2- Decide the logic to run based on indoor thermal comfort outdoor PM2.5 concentration
sim_house[i+1, "v_fan_base_out_logic"] <- case_when(sim_house$v_fan_base_out_logic[i+1] > 0 ~ 1, # here there is an optimization possible as we are overwriting an already present calculation
sim_house$pm_25_out[i+1] >= 35 ~ 1,
sim_house$pm_25_out[i+1] < 35 ~ 0,
TRUE ~ 0) * sim_house$design_furnace[i+1] * sim_house$d_t[i+1] # m3
### 3- Calculate flows and concentrations
sim_house[i+1, "v_da_base_out_logic"] <- sim_house$v_fan_base_out_logic[i+1] * ((1 - sim_house$filter_pres[i+1]) - sim_house$n_ls[i+1])
sim_house[i+1, "v_ra_base_out_logic"] <- (sim_house$v_infil[i+1] + sim_house$v_da_base_out_logic[i+1]) * as.integer(sim_house$v_fan_base_out_logic[i+1] > 0)
sim_house[i+1,"c_bf_base_out_logic"] <- case_when( (sim_house$v_ra_base_out_logic[i+1] + sim_house$v_fan_base_out_logic[i+1]*sim_house$n_lr[i+1]) > 0 ~ (sim_house$v_ra_base_out_logic[i+1]*sim_house$c_in_base_out_logic[i+1] + sim_house$v_fan_base_out_logic[i+1]*sim_house$n_lr[i+1]*sim_house$pm_25_out[i+1]) /
(sim_house$v_ra_base_out_logic[i+1] + sim_house$v_fan_base_out_logic[i+1]*sim_house$n_lr[i+1]),
(sim_house$v_ra_base_out_logic[i+1] + sim_house$v_fan_base_out_logic[i+1]*sim_house$n_lr[i+1]) == 0 ~ 0,
TRUE ~ 0
)
sim_house[i+1,"c_af_base_out_logic"] <- sim_house$c_bf_base_out_logic[i+1]*( 1 - (sim_house$filter_eff[i+1]) )
# HVAC control logic over baseline and when indoor PM2.5 >= 5 ug/m3
### 1- Forecast the indoor concentration at the next step
sim_house[i+1,"c_in_base_in_logic"] <- (
sim_house$c_in_base_in_logic[i]*sim_house$house_volume[i] - sim_house$v_ra_base_in_logic[i]*(sim_house$c_in_base_in_logic[i]) +
sim_house$v_da_base_in_logic[i]*sim_house$c_af_base_in_logic[i] + sim_house$v_infil[i]*sim_house$pm_25_out[i]*sim_house$pm25_pene[i] - # NOTE 75% particle go trough the walls
sim_house$v_infil[i]*(sim_house$c_in_base_in_logic[i]) - sim_house$v_de[i]*(sim_house$c_in_base_in_logic[i])
) / sim_house$house_volume[i] # ug/m3
sim_house[i+1,"c_in_base_in_logic"] <- ifelse(sim_house[i+1,"c_in_base_in_logic"] < 0, 0, sim_house[i+1,"c_in_base_in_logic"])
### 2- Decide the logic to run based on indoor thermal comfort indoor PM2.5 concentration
sim_house[i+1, "v_fan_base_in_logic"] <- case_when(sim_house$v_fan_base_in_logic[i+1] > 0 ~ 1, # here there is an optimization possible as we are overwriting an already present calculation
sim_house$c_in_base_in_logic[i+1] >= 5 ~ 1,
sim_house$c_in_base_in_logic[i+1] < 5 ~ 0,
TRUE ~ 0) * sim_house$design_furnace[i+1] * sim_house$d_t[i+1] # m3
### 3- Calculate flows and concentrations
sim_house[i+1, "v_da_base_in_logic"] <- sim_house$v_fan_base_in_logic[i+1] * ((1 - sim_house$filter_pres[i+1]) - sim_house$n_ls[i+1])
sim_house[i+1, "v_ra_base_in_logic"] <- (sim_house$v_infil[i+1] + sim_house$v_da_base_in_logic[i+1]) * as.integer(sim_house$v_fan_base_in_logic[i+1] > 0)
sim_house[i+1,"c_bf_base_in_logic"] <- case_when( (sim_house$v_ra_base_in_logic[i+1] + sim_house$v_fan_base_in_logic[i+1]*sim_house$n_lr[i+1]) > 0 ~ (sim_house$v_ra_base_in_logic[i+1]*sim_house$c_in_base_in_logic[i+1] + sim_house$v_fan_base_in_logic[i+1]*sim_house$n_lr[i+1]*sim_house$pm_25_out[i+1]) /
(sim_house$v_ra_base_in_logic[i+1] + sim_house$v_fan_base_in_logic[i+1]*sim_house$n_lr[i+1]),
(sim_house$v_ra_base_in_logic[i+1] + sim_house$v_fan_base_in_logic[i+1]*sim_house$n_lr[i+1]) == 0 ~ 0,
TRUE ~ 0
)
sim_house[i+1,"c_af_base_in_logic"] <- sim_house$c_bf_base_in_logic[i+1]*( 1 - (sim_house$filter_eff[i+1]) )
# HVAC control logic - AQ mode, no HVAC baseline
### 1- Forecast the indoor concentration at the next step
sim_house[i+1,"c_in_logic"] <- (
sim_house$c_in_logic[i]*sim_house$house_volume[i] - sim_house$v_ra_logic[i]*(sim_house$c_in_logic[i]) +
sim_house$v_da_logic[i]*sim_house$c_af_logic[i] + sim_house$v_infil[i]*sim_house$pm_25_out[i]*sim_house$pm25_pene[i] - # NOTE 75% particle go trough the walls
sim_house$v_infil[i]*(sim_house$c_in_logic[i]) - sim_house$v_de[i]*(sim_house$c_in_logic[i])
) / sim_house$house_volume[i] # ug/m3
sim_house[i+1,"c_in_logic"] <- ifelse(sim_house[i+1,"c_in_logic"] < 0, 0, sim_house[i+1,"c_in_logic"])
### 2- Decide the logic to run based on outdoor and indoor concentration
sim_house[i+1, "v_fan_logic"] <- case_when(sim_house$pm_25_out[i+1] >= 35 & sim_house$c_in_logic[i+1] >= 5 ~ 1,
sim_house$pm_25_out[i+1] >= 35 & sim_house$c_in_logic[i+1] < 5 ~ 0,
sim_house$pm_25_out[i+1] < 35 & sim_house$c_in_logic[i+1] >= 5 ~ 1,
sim_house$pm_25_out[i+1] < 35 & sim_house$c_in_logic[i+1] < 5 ~ 0,
TRUE ~ 0) * sim_house$design_furnace[i+1] * sim_house$d_t[i+1] # m3
### 3- Calculate flows and concentrations
sim_house[i+1, "v_da_logic"] <- sim_house$v_fan_logic[i+1] * ((1 - sim_house$filter_pres[i+1]) - sim_house$n_ls[i+1])
sim_house[i+1, "v_ra_logic"] <- (sim_house$v_infil[i+1] + sim_house$v_da_logic[i+1]) * as.integer(sim_house$v_fan_logic[i+1] > 0)
sim_house[i+1,"c_bf_logic"] <- case_when( (sim_house$v_ra_logic[i+1] + sim_house$v_fan_logic[i+1]*sim_house$n_lr[i+1]) > 0 ~ (sim_house$v_ra_logic[i+1]*sim_house$c_in_logic[i+1] + sim_house$v_fan_logic[i+1]*sim_house$n_lr[i+1]*sim_house$pm_25_out[i+1]) /
(sim_house$v_ra_logic[i+1] + sim_house$v_fan_logic[i+1]*sim_house$n_lr[i+1]),
(sim_house$v_ra_logic[i+1] + sim_house$v_fan_logic[i+1]*sim_house$n_lr[i+1]) == 0 ~ 0,
TRUE ~ 0
)
sim_house[i+1,"c_af_logic"] <- sim_house$c_bf_logic[i+1]*( 1 - (sim_house$filter_eff[i+1]) )
# PAC running all the time
sim_house[i+1,"c_in_pac"] <- (
sim_house$c_in_pac[i]*sim_house$house_volume[i] - # ug, tot numb particle house
sim_house$v_fan_pac[i]*(sim_house$c_in_pac[i]) * (sim_house$pac_filt_eff[i]) + # ug, total number particles removed by the filter
sim_house$v_infil[i]*sim_house$pm_25_out[i]*sim_house$pm25_pene[i] - # ug, particle infiltration, # NOTE 75% particle go trough the walls
sim_house$v_infil[i]*(sim_house$c_in_pac[i]) - # ug, particle outfiltration
sim_house$v_de[i]*(sim_house$c_in_pac[i]) # ug, particle deposition
) / sim_house$house_volume[i]
sim_house[i+1,"c_in_pac"] <- ifelse(sim_house[i+1,"c_in_pac"] < 0, 0, sim_house[i+1,"c_in_pac"])
}
tmp_sim_endtime <- Sys.time()
print(tmp_sim_endtime - tmp_sim_startime)
}
# [DEBUG] plot the results
# sim_house %>%
# filter(datetime > "2020-09-05 00:00:00" & datetime < "2020-09-07 00:00:00" ) %>%
# ggplot(data = ., aes(x = datetime)) +
# geom_hline(yintercept = 5, lty=2, alpha = 0.6) +
# geom_hline(yintercept = 35, lty=2, alpha = 0.6) +
# geom_line(aes(y = pm_25_out), alpha = 0.9) +
# geom_line(aes(y=c_in_baseline), color="blue", alpha=0.5) +
# geom_line(aes(y=c_in_hvac), color="firebrick", alpha=0.5) +
# geom_line(aes(y=c_in_logic), color="forestgreen") +
# geom_line(aes(y=c_in_pac), color="purple", alpha=0.5) +
# ggtitle(paste0("Outdoor/indoor scenario PM2.5 concentration\nGeoid: ", sim_house$geoid[1], ", ", round(sim_house$house_volume[1], digits = 1), " m3, ",
# round(sim_house$house_area[1], digits = 1), " m2, ",
# sim_house$house_volume[1] / sim_house$house_area[1], " h\n",
# sim_house$datetime[1], " - ",
# sim_house$datetime[nrow(sim_house)]),
# subtitle = "Black: outdoor PM2.5, blue: no HVAC, red: HVAC 12 min/hr, green: HVAC control logic, purple: PAC 60 min/hr
# Indoor PM2.5 threshold is 5 ug/m3
# Outdoor PM2.5 threshold is 35 ug/m3") +
# theme(axis.title.x = element_blank(),
# axis.title.y = element_blank())
# select columns to keep -> rename to be consistent with the new names 2023-05-27
sim_house <- sim_house %>%
mutate(v_fan_hvac = case_when(v_fan_hvac > 0 ~ 1, TRUE ~ 0),
v_fan_base_hvac = case_when(v_fan_base_hvac > 0 ~ 1, TRUE ~ 0),
v_fan_base_out_logic = case_when(v_fan_base_out_logic > 0 ~ 1, TRUE ~ 0),
v_fan_base_in_logic = case_when(v_fan_base_in_logic > 0 ~ 1, TRUE ~ 0),
v_fan_logic = case_when(v_fan_logic > 0 ~ 1, TRUE ~ 0),
v_fan_pac = case_when(v_fan_pac > 0 ~ 1, TRUE ~ 0),
) %>%
select(test, datetime, pm_25_out,
# save infiltration data
#fan_baseline = v_fan_baseline,
pm_25_infiltration = c_in_baseline,
# save 10-min fixed HVAC runtime
fan_10min_hvac = v_fan_hvac,
pm_25_10min_hvac = c_in_hvac,
# save baseline HVAC runtime
fan_baseline_hvac = v_fan_base_hvac,
pm_25_baseline_hvac = c_in_base_hvac,
# save baseline and outdoor PM2.5 logic
fan_baseline_out = v_fan_base_out_logic,
pm_25_baseline_out = c_in_base_out_logic,
# save baseline and indoor PM2.5 logic
fan_baseline_in = v_fan_base_in_logic,
pm_25_baseline_in = c_in_base_in_logic,
# save AQ mode and PAC
fan_AQ_logic = v_fan_logic,
pm_25_AQ_logic = c_in_logic,
fan_pac = v_fan_pac,
pm_25_pac = c_in_pac
)
# save result
write_rds(sim_house, file = here("data", "sens_analysis", "sa_simulations", paste0(sim_house[1,1],'.rds')))
# summarise exposure and write to file # FEDE check runtime
sim_house %>%
summarise(test = first(test),
sample = n(),
# save infiltration PM2.5
pm_25_infiltration = sum(pm_25_infiltration, na.rm = TRUE),
# save total runtime and indoor PM2.5 for the 10-min logic
fan_10min_hvac = sum(fan_10min_hvac, na.rm = TRUE) * 10, # to have the total min
pm_25_10min_hvac = sum(pm_25_10min_hvac, na.rm = TRUE),
# save total runtime and indoor PM2.5 for the heating and air conditioning baseline
fan_baseline_hvac = sum(fan_baseline_hvac, na.rm = TRUE) * 10,
pm_25_baseline_hvac = sum(pm_25_baseline_hvac, na.rm = TRUE),
# save total runtime and indoor PM2.5 for the baseline scenario and the outdoor PM2.5 control logic
fan_baseline_out = sum(fan_baseline_out, na.rm = TRUE) * 10,
pm_25_baseline_out = sum(pm_25_baseline_out, na.rm = TRUE),
# save total runtime and indoor PM2.5 for the baseline scenario and the indoor PM2.5 control logic
fan_baseline_in = sum(fan_baseline_in, na.rm = TRUE) * 10,
pm_25_baseline_in = sum(pm_25_baseline_in, na.rm = TRUE),
# save total runtime and indoor PM2.5 for the AQ scenario considering only the indoor PM2.5 logic
fan_AQ_logic = sum(fan_AQ_logic, na.rm = TRUE) * 10,
pm_25_AQ_logic = sum(pm_25_AQ_logic, na.rm = TRUE),
# save total runtime and indoor PM2.5 for the PAC
fan_pac = sum(fan_pac, na.rm = TRUE) * 10,
pm_25_pac = sum(pm_25_pac, na.rm = TRUE),
# save the total outdoor PM2.5
pm_25_out = sum(pm_25_out, na.rm = TRUE)
) %>%
write_delim(., here("data", "sens_analysis", "sa_simulations", "_total_exposure.csv"), delim = ",", append = TRUE)
return(sim_house)
}
# try running first 5 tests
#results <- future_map(seq_len(5), process_tract, .progress = TRUE)
# try running random 100 test
#random_numbers <- sample(1:180000, 100, replace = FALSE)
# START SIMULATIONS *** CAREFUL ***
results <- future_map(seq_len(nrow(grid_data)), process_tract, .progress = TRUE)
stop("Finishing simulations. Exit source.")
#### MANUAL ####
#### IMPORT DATA ####
# get list of simulation files
list_file <- list.files(path = here("data", "sens_analysis", "sa_simulations"), pattern = "[0-9].rds", full.names = TRUE)
# function to import and summarise data files
import_sim <- function(sim_file) {
# read rds and average over time interval (day, hour etc.)
df_sim_load <- read_rds(file = sim_file) %>%
group_by(datetime = floor_date(datetime, unit = "hour")) %>%
summarise(test = first(test),
# infiltration PM2.5
pm_25_infiltration = sum(pm_25_infiltration, na.rm = TRUE),
# total runtime and indoor PM2.5 for the 10-min logic
fan_10min_hvac = sum(fan_10min_hvac, na.rm = TRUE) * 10, # to have the total min
pm_25_10min_hvac = sum(pm_25_10min_hvac, na.rm = TRUE),
# total runtime and indoor PM2.5 for the heating and air conditioning baseline
fan_baseline_hvac = sum(fan_baseline_hvac, na.rm = TRUE) * 10,
pm_25_baseline_hvac = sum(pm_25_baseline_hvac, na.rm = TRUE),
# total runtime and indoor PM2.5 for the baseline scenario and the outdoor PM2.5 control logic
fan_baseline_out = sum(fan_baseline_out, na.rm = TRUE) * 10,
pm_25_baseline_out = sum(pm_25_baseline_out, na.rm = TRUE),
# total runtime and indoor PM2.5 for the baseline scenario and the indoor PM2.5 control logic
fan_baseline_in = sum(fan_baseline_in, na.rm = TRUE) * 10,
pm_25_baseline_in = sum(pm_25_baseline_in, na.rm = TRUE),
# total runtime and indoor PM2.5 for the AQ scenario considering only the indoor PM2.5 logic
fan_AQ_logic = sum(fan_AQ_logic, na.rm = TRUE) * 10,
pm_25_AQ_logic = sum(pm_25_AQ_logic, na.rm = TRUE),
# total runtime and indoor PM2.5 for the PAC
fan_pac = sum(fan_pac, na.rm = TRUE) * 10,
pm_25_pac = sum(pm_25_pac, na.rm = TRUE),
# total outdoor PM2.5
pm_25_out = sum(pm_25_out, na.rm = TRUE)
)
# keep dataframe
return(df_sim_load)
}
# run on list of files
list_sim <- pblapply(list_file, import_sim)
# combine data files
df_sim <- bind_rows(list_sim)
# save dataset
write_rds(df_sim, here("data", "sens_analysis", "sa_simulations", paste0("df_hourly_", format(Sys.time(), "%Y%m%d_%H%M%S"), ".rds")), compress = "gz")
# clean up
rm(list_sim, list_file, import_sim)
#### PLOTS FOR SENSITIVITY ANALYSIS ####
grid_data <- read_csv(file = here("data", "sens_analysis", "grid_data.csv"))
df_sim_plt <- df_sim %>%
left_join(y = grid_data, by = "test")
##### SUMMARY STATS #####
small_house <- 90
medium_house <- 120
large_house <- 152
airtight_house <- 0.0273
med.leaky_house <- 0.0359
leaky_house <- 0.0454
df_sim_plt %>% nrow()
# house area
df_sim_plt %>%
group_by(test) %>%
summarise(start_IO = first(start_IO),
MERV = first(MERV),
house_area = first(house_area),
effect_leakage = first(effect_leakage)) %>%
select(house_area) %>%
mutate(house_size = case_when(house_area <= small_house ~ "small",
house_area > small_house & house_area <= large_house ~ "medium",
house_area > large_house ~ "large")) %>%
select(house_size) %>% group_by(house_size) %>% summarise(n())
# leakage
df_sim_plt %>%
group_by(test) %>%
summarise(start_IO = first(start_IO),
MERV = first(MERV),
house_area = first(house_area),
effect_leakage = first(effect_leakage)) %>%
select(effect_leakage) %>%
mutate(house_leak = case_when(effect_leakage <= airtight_house ~ "airtight",
effect_leakage > airtight_house & effect_leakage <= leaky_house ~ "med. leaky",
effect_leakage > leaky_house ~ "very leaky"
)) %>% select(house_leak) %>% group_by(house_leak) %>% summarise(n())
# I/O
df_sim_plt %>%
group_by(test) %>%
summarise(start_IO = first(start_IO),
MERV = first(MERV),
house_area = first(house_area),
effect_leakage = first(effect_leakage)) %>%
select(start_IO) %>%
group_by(start_IO) %>%
summarise(n())
##### PLOTS #####
###### Upgrading Filter and using wildfire mode ######
# add variables "house_size" and "house_leak" to dataset
df_sim_round <- df_sim_plt %>%
mutate(house_size = case_when(house_area <= small_house ~ "small",
house_area > small_house & house_area <= large_house ~ "medium",
house_area > large_house ~ "large")) %>%
mutate(house_leak = case_when(effect_leakage <= airtight_house ~ "airtight",
effect_leakage > airtight_house & effect_leakage <= leaky_house ~ "med.leaky",
effect_leakage > leaky_house ~ "very.leaky"
))
# calculate the reduction of indoor exposure by upgrading filter and using wildfire mode - against outdoor exposure
df_sim_round %>%
group_by(MERV) %>%
summarise(exp_outdoor = sum(pm_25_out),
exp_sheltering = sum(pm_25_infiltration),
exp_wildfire_mode = sum(pm_25_baseline_out),
exp_diff_shelter = 100 - exp_sheltering/exp_outdoor * 100,
exp_diff_wildfire_mode = 100 - exp_wildfire_mode/exp_outdoor * 100,
exp_diff = (exp_diff_wildfire_mode - exp_diff_shelter)
)
# calculate the reduction of indoor exposure by house size
df_sim_round %>%
#filter(MERV > 8) %>%
group_by(house_size) %>%
summarise(exp_outdoor = sum(pm_25_out),
exp_sheltering = sum(pm_25_infiltration),
exp_diff_shelter = 100 - exp_sheltering/exp_outdoor * 100
)
# calculate the reduction of indoor exposure by house leakage
df_sim_round %>%
filter(MERV > 8) %>%
group_by(house_leak) %>%
summarise(exp_outdoor = sum(pm_25_out),
exp_sheltering = sum(pm_25_infiltration),
exp_diff_shelter = 100 - exp_sheltering/exp_outdoor * 100
)
###### IS I/O IMPORTANT ? ######
# prepare dataset to be used for plots
prep_plot_io <- df_sim_plt %>%
group_by(test) %>%
mutate(house_size = case_when(house_area <= small_house ~ "small",
house_area > small_house & house_area <= large_house ~ "medium",
house_area > large_house ~ "large")) %>%
mutate(house_leak = case_when(effect_leakage <= airtight_house ~ "airtight",
effect_leakage > airtight_house & effect_leakage <= leaky_house ~ "med.leaky",
effect_leakage > leaky_house ~ "very.leaky"
)) %>%
summarise(pm_25_infiltration = sum(pm_25_infiltration),
fan_10min_hvac = sum(fan_10min_hvac),
pm_25_10min_hvac = sum(pm_25_10min_hvac),
fan_baseline_hvac = sum(fan_baseline_hvac),
pm_25_baseline_hvac = sum(pm_25_baseline_hvac),
fan_baseline_out = sum(fan_baseline_out),
pm_25_baseline_out = sum(pm_25_baseline_out),
fan_baseline_in = sum(fan_baseline_in),
pm_25_baseline_in = sum(pm_25_baseline_in),
fan_AQ_logic = sum(fan_AQ_logic),
pm_25_AQ_logic = sum(pm_25_AQ_logic),
fan_pac = sum(fan_pac),