+ 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
# #### Changes in building stock due to construction etc.
# change <- timepoints(changeBuildings, obsyear, sumtimecol = FALSE) # Cumulative changes at certain timepoints.
#### Calculate cb ie. those buildings that were built between Time-1 and Time.
#### This has two parts: the stock (stockBuildings) is treated as net construction rate
#### by assuming that after construction Time nothing is demolished.
#### Then this is added to the direct construction data (changeBuildings).
# ### This code can be used to change cumulative building stock data to construction rates of periods.
# stockBuildings@output$Time <- as.numeric(as.character(stockBuildings@output$Time))
# years <- sort(unique(stockBuildings@output$Time))
# cb <- stockBuildings
# cb@output$Time <- years[match(cb@output$Time, years) + 1] # Frame shift with time, years 2:n.
# cb@output <- subset(cb@output, !is.na(Time))
# cb <- stockBuildings - cb
# cb@output <- orbind(cb, subset(stockBuildings@output, Time = years[1])) # First year
cb <- stockBuildings * heatingShares * efficiencyShares
# colnames(cb@output)[colnames(cb@output) == "Time"] <- "Startyear"
# Add buildings from stock and buildings from construction data
cb <- orbind2(cb, changeBuildings)
########### Renovations of existing buildings. # Combine with (continuous) index Age.
reno <- cb * renovationShares
marginals <- colnames(reno@output)[reno@marginal]
reno@output$Age <- as.numeric(as.character(reno@output$Startyear)) - # Startyear is the time of renovation
as.numeric(as.character(reno@output$Time)) # Time is the time of construction
reno@marginal <- colnames(reno@output) %in% marginals
reno@output <- reno@output[result(reno) != 0 , ] # Zeros are meaningless
reno <- reno * renovationRate # continuousOps(reno, renovationRate, '*') FIX continuousOps before using
# reno@output$Startyear <- reno@output$Time # Renovation is the new event.
reno <- oapply(reno, cols = c("Age", "Time"), FUN = sum)
# reno <- timepoints(reno, obstime) # Accumulate over observation time.
out <- reno * -1 # Equal amount stops being non-renovated.
out@output$Renovation <- "None"
# out@output$Renovation <- as.factor(out@output$Renovation)
out <- orbind2(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")
| |