+ Show code- Hide code
library(OpasnetUtils)
########## 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
"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(!"Startyear" %in% colnames(stock@output)) { # Make a duplicate of Time because Startyear is needed later.
# marginals <- colnames(stock@output)[stock@marginal]
# stock@output$Startyear <- stock@output$Time
# stock@marginal <- colnames(stock@output) %in% c(marginals, "Startyear")
# }
#### Changes in building stock due to construction etc.
# change <- changeBuildings
change <- timepoints(changeBuildings, obsyear, sumtimecol = FALSE) # Cumulative changes at certain timepoints.
nw <- stock
nw@output$Time <- as.numeric(as.character(nw@output$Time))
nw@output <- subset(nw@output, Time == max(Time))
colnames(nw@output)[colnames(nw@output) == "Time"] <- "Startyear"
changeBuildings@output$Startyear <- as.numeric(as.character(changeBuildings@output$Startyear))
nw <- unkeep(nw, prevresults = TRUE, sources = TRUE)
nw@output <- orbind(nw, unkeep(1 * changeBuildings, prevresults = TRUE, sources = TRUE))
# nw <-
# stock <- unkeep(change, cols = "Startyear")
# change@marginal <- colnames(change@output) %in% c(
# "Age",
# colnames(stock@output)[stock@marginal],
# colnames(changeBuildings@output)[changeBuildings@marginal]
# )
# change <- unkeep(change, cols = "Startyear", prevresults = TRUE, sources = TRUE)
########### Floor area of renovations. Combine with continuous index Age.
reno <- nw * renovationShares
marginals <- colnames(reno@output)[reno@marginal]
reno@output$Age <- as.numeric(as.character(reno@output$Time)) -
as.numeric(as.character(reno@output$Startyear))
reno@marginal <- colnames(reno@output) %in% marginals
reno <- reno * 0.03 # continuousOps(reno, renovationRate, '*') FIX continuousOps
reno@output$Startyear <- reno@output$Time # Renovation is the new event.
reno <- oapply(reno, cols = c("Age", "Time"), FUN = sum)
reno <- timepoints(reno, obstime)
reno@output <- reno@output[result(reno) != 0 , ]
out <- reno * -1 # Equal amount stops being non-renovated.
out@output$Renovation <- "None"
out@output$Renovation <- as.factor(out@output$Renovation)
out <- orbind(out, reno) # Temp2: Construction + renovation in data.frame
for(i in colnames(reno@output)[reno@marginal]) {
out[[i]][is.na(out[[i]])] <- "Not known"
}
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")
| |