+ Show code- Hide code
library(OpasnetUtils)
#########3# Calculate all building events (constructions, demolitions, renovations)
buildings <- Ovariable("buildings",
dependencies = data.frame(Name = c(
"stockBuildings", # Building stock given as an amount at given timepoints
"heatingShares", # Heating types of current buildings
"efficiencyShares", # Energy efficiency types of current buildings
"changeBuildings", # Construction or demolition rate as floor-m2. Indices Efficiency and
# Heating should be included here, if used.
# "buildingTypes", # A dummy variable to combine two different indices: Building and Building2
"renovationShares", # Fraction of renovation type when renovation is done.
"rateBuildings", # Percentage of renovations and other relative changes per year
# "eventyear", # A dummy variable to combine time periods to numerical time axis.
"year" # A data.frame of years for which observations are calculated. This requires timepoints function.
)),
formula = function(...) {
# Current building stock
stock <- stockBuildings * heatingShares * efficiencyShares
if(!"Built" %in% colnames(stock@output)) { # Make a duplicate of Time because Built is needed later.
marginals <- colnames(stock@output)[stock@marginal]
stock@output$Built <- stock@output$Time
stock@marginal <- colnames(stock@output) %in% c(marginals, "Built")
}
#### Changes in building stock due to construction etc.
# change <- changeBuildings
change <- timepoints(changeBuildings, obsyear, sumtimecol = FALSE) # Cumulative changes at certain timepoints.
stock@output$Time <- as.numeric(as.character(stock@output$Time))
stock@output$Built <- as.numeric(as.character(stock@output$Built))
stock@output <- orbind(stock, change)
stock@output$Age <- stock@output$Time - stock@output$Built
stock@marginal <- colnames(stock@output) %in% c(
"Age",
colnames(stock@output)[stock@marginal],
colnames(changeBuildings@output)[changeBuildings@marginal]
)
stock <- unkeep(stock, prevresults = TRUE, sources = TRUE)
# nonrenovated <- oapply(stock, cols = character(), FUN = sum) # Simplify row structure
# if("Renovation" %in% colnames(nonrenovated@output)) {nonrenovated@output <- subset(
# nonrenovated@output,
# Renovation == "None",
# colnames(nonrenovated@output) != "Renovation"
# )}
reno <- nonrenovated * renovationShares
# Floor area of renovations in ten years. Combine with continuous index Age.
reno <- continuousOps(stock, renovationRate, '*') * renovationShares * 10
reno@output$Built <- reno@output$Time # Renovation is the new event.
reno <- unkeep(reno, cols = c("Age", "Time"), sources = TRUE, prevresults = TRUE)
temp2 <- orbind(temp1 * 1, reno * -1) # Equal amount stops being non-renovated.
temp2$Renovation <- "None"
temp2 <- orbind(temp2, reno) # Temp2: Construction + renovation in data.frame
temp2$Renovation <- as.factor(temp2$Renovation)
temp3 <- Ovariable(output = temp2)) # Temp3: Like Temp2 but ovariable
temp3 <- unkeep(temp3, sources = TRUE, prevresults = TRUE)
# temp3@output <- fillna(temp3@output, marginals = colnames(temp3@output)[temp3@marginal]) # WHY THIS? CANNOT FILLNA AMOUNTS!
# colnames(temp3@output)[colnames(temp3@output) == "City area"] <- "City.area"
# temp4: Drop redundant time indices
temp4 <- oapply(temp3, cols = c("Built", "Age"), FUN = "sum", na.rm = TRUE)
# temp4@output <- temp4@output[!is.na(result(temp4)) , ]
# Calculate cumulative events at timepoints defined by obsyear.
out <- timepoints(temp4, obsyear = obsyear)
return(out)
}
)
### HeatingEnergy
heatingEnergy <- Ovariable("heatingEnergy",
dependencies = data.frame(Name = c(
"energyUse", # Energy consumption per floor area
"efficiencyRatio", # Relative energy consumption compared with the efficiency group Old.
"renovationRatio", # Relative energy consumption compared with the Renovation
"buildings",
"buildingTypes"
)),
formula = function(...) {
out <- buildings * buildingTypes * energyUse * efficiencyRatio * renovationRatio
return(out)
}
)
####### Calculate emissions
emissions <- Ovariable("emissions",
dependencies = data.frame(
Name = c(
"heatingEnergy",
"fuelTypes",
"emissionFactors"
)
),
formula = function(...) {
out <- oapply(heatingEnergy, cols = c("Building", "Efficiency"), FUN = sum)
out <- out * fuelTypes * emissionFactors * 3.6 * 1E-9 # convert from kWh /a to MJ /a and mg to ton
out <- unkeep(out * emissionLocations, sources = TRUE, prevresults = TRUE)
out@output$Emission_site <- as.factor(ifelse(
out@output$Emission_site == "At site of consumption",
as.character(out@output$City_area),
as.character(out@output$Emission_site)
))
out <- oapply(
out,
cols = c("Heating", "Burner", "Fuel", "City_area"),
FUN = sum
)
}
)
#"Emission_site", "Emission_height",
exposure <- Ovariable("exposure",
# emis is in ton /a
# iF = conc (g /m3) * pop (#) * BR (m3 /s) / emis (g /s) <=> conc = emis * iF / BR / pop # conc is the exposure concentration
dependencies = data.frame(Name = c("emis2", "iF", "pop")),
formula = function(...) {
BR <- 20 # Nominal breathing rate (m^3 /d)
BR <- BR / 24 / 3600 # m^3 /s
out <- 1E+12 / 365 / 24 / 3600 # Emission scaling from ton /a to ug /s.
out <- (emis2 * out) * iF / BR / pop # the actual equation
out <- unkeep(out, prevresults = TRUE, sources = TRUE)
return(out)
}
)
objects.store(buildings, heatingEnergy, emissions, exposure)
cat("Saved ovariables buildings, heatingEnergy, emissions, exposure\n")
| |