Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

replace stationary by ONONSPEC projection #644

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -56,21 +56,32 @@ export(toolSolarFunctionAggregate)
import(madrat)
import(magclass)
importFrom(data.table,":=")
importFrom(dplyr,"%>%")
importFrom(dplyr,.data)
importFrom(dplyr,across)
importFrom(dplyr,all_of)
importFrom(dplyr,arrange)
importFrom(dplyr,bind_rows)
importFrom(dplyr,desc)
importFrom(dplyr,distinct)
importFrom(dplyr,filter)
importFrom(dplyr,full_join)
importFrom(dplyr,group_by)
importFrom(dplyr,group_modify)
importFrom(dplyr,left_join)
importFrom(dplyr,matches)
importFrom(dplyr,mutate)
importFrom(dplyr,pull)
importFrom(dplyr,reframe)
importFrom(dplyr,rename)
importFrom(dplyr,select)
importFrom(dplyr,slice_tail)
importFrom(dplyr,summarise)
importFrom(dplyr,ungroup)
importFrom(madrat,calcOutput)
importFrom(madrat,toolGetMapping)
importFrom(magclass,"getSets<-")
importFrom(magclass,getItems)
importFrom(magrittr,"%>%")
importFrom(readxl,excel_sheets)
importFrom(readxl,read_excel)
Expand Down
112 changes: 112 additions & 0 deletions R/calcFeDemandONONSPEC.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
#' Calculate historic and projected other non-specified energy demand
#'
#' Project the IEA flow ONONSPEC into the future.
#'
#' @author Robin Hasse
#'
#' @returns list with MagPIE object
#'
#' @importFrom madrat toolGetMapping calcOutput
#' @importFrom magclass getItems getSets<-
#' @importFrom dplyr %>% .data filter group_by across all_of group_modify
#' slice_tail ungroup reframe

calcFeDemandONONSPEC <- function() {

# project yearly until
endOfProjection <- 2100

# approach constant level after ... years
yearsUntilConst <- c(15, 20, 25)

# ratio of EOH slope that qualifies as constant
constTolerance <- 0.05




# FUNCTIONS ------------------------------------------------------------------

.getHistFlows <- function() {
data <- calcOutput("IOEdgeBuildings",
subtype = "output_EDGE",
aggregate = FALSE)
getSets(data) <- c("region", "period", "flow")
# ONONSPEC have been mapped to feoth* before
itemsONONSPEC <- grep("^feoth.+$", getItems(data, 3), value = TRUE)
data[, , itemsONONSPEC]
}


.getEOHcoefs <- function(x, key) {
m <- lm(value~period, x)
periodEOH <- max(x$period)
coefs <- data.frame(periodEOH = periodEOH,
slopeEOH = coef(m)[["period"]],
valueEOH = predict(m, list(period = periodEOH)))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
valueEOH = predict(m, list(period = periodEOH)))
valueEOH = predict(m, data.frame(period = periodEOH)))

Not sure if this makes much of a difference, but usually predict takes data frames.

}


.expandScenarios <- function(x, key = NULL, .yearsUntilConst = NULL) {
scens <- data.frame(scenario = c("low", "med", "high"))
if (!is.null(.yearsUntilConst)) {
scens$.yearsUntilConst = if (x$slopeEOH > 0) .yearsUntilConst else rev(.yearsUntilConst)
}
merge(scens, x)
}


.expandPeriods <- function(x, key, endOfProjection) {
startOfProjection <- unique(x$periodEOH) + 1
periods <- data.frame(period = startOfProjection:endOfProjection)
merge(periods, x)
}


.calcAsymptotic <- function(x) {
decay <- -log(constTolerance) / x$.yearsUntilConst
x$value <- pmax(0,
x$valueEOH + x$slopeEOH / decay * (1 - exp(-decay * (x$period - x$periodEOH)))
)
x
}


.projectFlows <- function(flows, endOfProjection, nTimeStepsEOH = 5) {
flows %>%
filter(!is.na(.data$value)) %>%
group_by(across(all_of(c("region", "flow")))) %>%
arrange(.data$period) %>%
slice_tail(n = nTimeStepsEOH) %>%
group_modify(.getEOHcoefs) %>%
group_modify(.expandScenarios, yearsUntilConst) %>%
group_modify(.expandPeriods, endOfProjection) %>%
ungroup() %>%
.calcAsymptotic() %>%
select("region", "period", "scenario", "flow", "value")
}


.extrapolateFlows <- function(flowsHist, endOfProjection) {
flowsProj <- .projectFlows(flowsHist, endOfProjection)
rbind(.expandScenarios(flowsHist), flowsProj)
}



# CALCULATE ------------------------------------------------------------------

flowsHist <- as_tibble(.getHistFlows())
flows <- .extrapolateFlows(flowsHist, endOfProjection)
flows <- as.magpie(flows, spatial = "region", temporal = "period")



# OUTPUT ---------------------------------------------------------------------

list(x = flows,
weight = NULL,
min = 0,
unit = "EJ/yr",
description = "Other non-specified energy demand")
}
17 changes: 17 additions & 0 deletions man/calcFeDemandONONSPEC.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading