-
Notifications
You must be signed in to change notification settings - Fork 48
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
robinhasse
wants to merge
1
commit into
pik-piam:master
Choose a base branch
from
robinhasse:rmStationary
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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))) | ||
} | ||
|
||
|
||
.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") | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not sure if this makes much of a difference, but usually
predict
takes data frames.