+ Show code- Hide code
library(OpasnetUtils)
#########3# Calculate all building events (constructions, demolitions, renovations)
buildings <- Ovariable("buildings",
dependencies = data.frame(Name = c(
"buildingStock", # Current building stock
"heatingShares", # Heating types of current buildings
"efficiencies", # Energy efficiencies of current buildings
"construction", # Construction rate in the future
"efficienciesNew", # Energy efficiencies in the future
"buildingTypes", # A dummy variable to combine two different indices: Building and Building2
"heatingSharesNew", # Heating types of the buildings in the future
"renovationShares", # Fraction of renovation type when renovation is done.
"renovation", # Percentage of renovations per year
"eventyear", # A dummy variable to combine time periods to numerical time axis.
"obsyear" # Years for which observations are calculated. This requires timepoints function.
)),
formula = function(...) {
# Current building stock
Now <- buildingStock * heatingShares * efficiencies * eventyear
# Construction in ten years multiplied by fraction of energy classes and heating types.
New <- construction * 10 * efficienciesNew * buildingTypes * heatingSharesNew * eventyear
temp1 <- EvalOutput(Ovariable("temp1", data = orbind(Now, New))) # All construction
temp1 <- unkeep(temp1, cols = "Building2", sources = TRUE, prevresults = TRUE)
# All source indices can be unkept, because each has only one location.
renovationyear <- Ovariable("renovationyear", data = data.frame(
Renovationyear = c(2015, 2025, 2035, 2045),
Age = NA,
Result = 1
))
reno <- temp1 * renovationyear # reno is the building stock repeated for every potential renovation decade.
reno@output$Age <- reno@output$Renovationyear - reno@output$Eventyear
# Floor area of renovations in ten years. Combine with continuous index Age.
reno <- continuousOps(reno, renovation, '*') * renovationShares * 10
reno@output$Eventyear <- reno@output$Renovationyear # Renovation is the new event.
reno <- unkeep(reno, cols = c("Age", "Renovationyear"), 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 <- EvalOutput(Ovariable("buildings", data = temp2)) # Temp3: Like Temp2 but ovariable
temp3 <- unkeep(temp3, sources = TRUE, prevresults = TRUE)
temp3@output <- fillna(temp3@output, marginals = colnames(temp3@output)[temp3@marginal])
colnames(temp3@output)[colnames(temp3@output) == "City area"] <- "City.area"
# temp4: Drop redundant time indices
temp4 <- oapply(temp3, cols = c("Constructed", "Renovationyear", "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",
"savingPotential",
"buildings",
"buildingTypes"
)),
formula = function(...) {
out <- buildings * buildingTypes * energyUse * savingPotential
return(out)
}
)
# Calculate the cumulative impact of the events on building stock to given years
timepoints <- function(X, obsyear) {
# Function timepoints takes an event list and turns that into existing crosscutting situations at
# timepoints defined by years.
# X must be an ovariable with index Eventyear.
# obsyear must be a vector of years.
if(is.factor(X@output$Eventyear))
X@output$Eventyear <- as.numeric(levels(X@output$Eventyear)[X@output$Eventyear])
# tapply (and therefore oapply) changes continuous indices to factors! Must change back by hand.
out <- data.frame()
for(i in obsyear) {
out <- rbind(out, data.frame(
Year = i,
X@output[X@output$Eventyear <= i , ]
))
}
X@output <- out
X@marginal <- c(TRUE, X@marginal) # Add Year to marginal
X <- oapply(X, cols = "Eventyear", FUN = "sum", na.rm = TRUE)
X@output$Year <- as.numeric(levels(X@output$Year)[X@output$Year])
X@output <- X@output[!is.na(result(X)) , ]
return(X)
}
objects.store(buildings, heatingEnergy, timepoints)
cat("Saved ovariables buildings, heatingEnergy, timepoints\n")
| |