+ 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.
)),
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
out <- EvalOutput(Ovariable("out", data = orbind(Now, New)))
out <- unkeep(out, 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(2010, 2020, 2030, 2040, 2050),
Age = NA,
Result = 1
))
temp <- out * renovationyear # temp is the building stock repeated for every potential renovation decade.
temp@output$Age <- temp@output$Renovationyear - temp@output$Eventyear
# Floor area of renovations in ten years. Combine with continuous index Age.
renovations <- continuousOps(temp, renovation, '*') * renovationShares * 10
marginals <- colnames(renovations@output)[renovations@marginal]
out <- orbind(out * 1, renovations * -1) # Equal amount stops being non-renovated.
out$Renovation <- "None"
out <- orbind(out, renovations)
out$Renovation <- as.factor(out$Renovation)
temp2 <- EvalOutput(Ovariable("temp2", data = out))
temp2 <- unkeep(temp2, sources = TRUE, prevresults = TRUE)
temp2@output <- fillna(temp2@output, marginals = colnames(temp2@output)[temp2@marginal])
temp2 <- oapply(temp2, cols = c("Constructed", "Renovationyear", "Age"), FUN = "sum", na.rm = TRUE)
# out <- as.data.frame(as.table(tapply( # Remove indices that are no longer needed
# out$Result,
# INDEX = out[marginals[!marginals %in% c("Constructed", "Renovationyear", #"Age")]],
# FUN = "sum",
# na.rm = TRUE
# )))
out$Eventyear <- as.numeric(levels(out$Eventyear)[out$Eventyear])
# tapply (and therefore oapply) changes continuous indices to factors! Must change back by hand.
colnames(out)[colnames(out) == "City area"] <- "City.area"
colnames(out)[colnames(out) == "Freq"] <- "Result"
out <- out[!is.na(out$Result) , ]
return(out)
}
)
### HeatingEnergy
heatingEnergy <- Ovariable("heatingEnergy",
dependencies = data.frame(Name = c("energyUse", "savingPotential", "buildings")),
formula = function(...) {
out <- energyUse * savingPotential * buildings
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.
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
return(X)
}
objects.store(buildings, heatingEnergy, timepoints)
cat("Saved ovariables buildings, heatingEnergy, timepoints\n")
| |