+ 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.
"renovationShares", # Fraction of renovation type when renovation is done.
"renovationRate", # Rate of renovation (fraction per time unit)
"obstime" # one-column data.frame about the years to be used in output. Time column name must be the same
# as in RenovationShares.
)),
formula = function(...) {
#### 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
# 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 <- reno * renovationRate # continuousOps(reno, renovationRate, '*') FIX continuousOps before using
reno <- oapply(reno, cols = c("Age", "Time"), FUN = sum)
out <- reno * -1 # Equal amount stops being non-renovated.
out@output$Renovation <- "None"
out <- orbind2(out, reno) # Renovated buildings
colnames(cb@output)[colnames(cb@output) == "Time"] <- "Startyear"
out <- orbind2(
out,
cb * Ovariable( # Add non-renovated buildings
output = data.frame(Renovation = "None", Result = 1),
marginal = c(TRUE, FALSE)
)
)
######## NA in indices will ruin timepoints. Therefore must do fillna but with warning.
b <- character()
for(i in colnames(out@output)[out@marginal]) {if(any(is.na(out@output[[i]]))) b <- c(b, i)}
if(length(b) > 0) {
out@output <- fillna(out@output, b)
warning("Missing values had to be filled by function fillna in indices: ", b, "\n")
}
out <- timepoints(out, obstime) # Accumulate over observation time.
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")
| |