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

Introduce detailed calibration implementation #76

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
2 changes: 1 addition & 1 deletion .buildlibrary
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
ValidationKey: '10275188'
ValidationKey: '1207140'
AutocreateReadme: yes
AcceptedWarnings:
- 'Warning: package ''.*'' was built under R version'
Expand Down
4 changes: 2 additions & 2 deletions CITATION.cff
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ message: If you use this software, please cite it using the metadata from this f
type: software
title: 'brick: Building sector model with heterogeneous renovation and construction
of the stock'
version: 0.5.11
date-released: '2025-01-20'
version: 0.6.0
date-released: '2025-01-31'
abstract: This building stock model represents residential and commercial buildings
at customisable regional and temporal resolution. The building stock is quantified
in floor area and distinguished by building type (SFH/MFH) and location (rural/urban).
Expand Down
4 changes: 2 additions & 2 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
Type: Package
Package: brick
Title: Building sector model with heterogeneous renovation and construction of the stock
Version: 0.5.11
Date: 2025-01-20
Version: 0.6.0
Date: 2025-01-31
Authors@R: c(
person("Robin", "Hasse", , "robin.hasse@pik-potsdam.de",
role = c("aut", "cre"),
Expand Down
5 changes: 5 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,11 @@ importFrom(dplyr,"%>%")
importFrom(dplyr,.data)
importFrom(dplyr,across)
importFrom(dplyr,all_of)
importFrom(dplyr,anti_join)
importFrom(dplyr,any_of)
importFrom(dplyr,arrange)
importFrom(dplyr,bind_rows)
importFrom(dplyr,case_match)
importFrom(dplyr,case_when)
importFrom(dplyr,cross_join)
importFrom(dplyr,everything)
Expand All @@ -32,6 +34,7 @@ importFrom(dplyr,mutate)
importFrom(dplyr,n)
importFrom(dplyr,reframe)
importFrom(dplyr,rename)
importFrom(dplyr,rename_with)
importFrom(dplyr,right_join)
importFrom(dplyr,row_number)
importFrom(dplyr,select)
Expand Down Expand Up @@ -86,13 +89,15 @@ importFrom(stats,median)
importFrom(stats,pweibull)
importFrom(tidyr,complete)
importFrom(tidyr,matches)
importFrom(tidyr,pivot_longer)
importFrom(tidyr,pivot_wider)
importFrom(tidyr,replace_na)
importFrom(tidyr,unite)
importFrom(utils,head)
importFrom(utils,read.csv)
importFrom(utils,read.csv2)
importFrom(utils,tail)
importFrom(utils,write.csv)
importFrom(utils,write.csv2)
importFrom(withr,with_dir)
importFrom(yaml,read_yaml)
Expand Down
11 changes: 9 additions & 2 deletions R/checkGamsSuccess.R
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,14 @@ checkGamsSuccess <- function(path) {
} else if (file.exists(file.path(path, "abort.gdx"))) {
stop("Gams aborted with \"abort.gdx\" due to errors in at least one variable.")
} else {
stop("Gams did not finish successfully.\n",
"Check the files main.log and main.lst for more information.")
allCalibrationFiles <- list.files(path, pattern = "calibration_\\d+\\.gdx$")
if (length(allCalibrationFiles) > 0) {
maxIteration <- max(as.numeric(sub("calibration_(\\d+)\\.gdx", "\\1", allCalibrationFiles)))
message("Gams successfully completed ", maxIteration, " calibration iterations: ",
"\"calibration_", maxIteration, ".gdx\" exists.")
} else {
stop("Gams did not finish successfully.\n",
"Check the files main.log and main.lst for more information.")
}
}
}
29 changes: 28 additions & 1 deletion R/createParameters.R
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,8 @@ createParameters <- function(m, config, inputDir) {
c("ttot", "reg", "bs", "hs", "bsr", "hsr",
"typ", "vin"),
inputDir) %>%
toModelResolution(m)
toModelResolution(m) %>%
.explicitZero()
p_specCostRen <- expandSets("cost", "bs", "hs", "bsr", "hsr", "vin", "reg",
"loc", "typ", "inc", "ttot", .m = m)
p_specCostRenTang <- p_specCostRen %>%
Expand Down Expand Up @@ -486,6 +487,32 @@ createParameters <- function(m, config, inputDir) {
)



# Price sensitivity ---------------------------------------------------

priceSensBS <- unlist(config[["priceSens"]][["bs"]])
priceSensBS <- expandSets("var", "reg", "loc", "typ", "inc", .m = m) %>%
filter(.data[["var"]] %in% names(priceSensBS)) %>%
mutate(value = priceSensBS[.data[["var"]]])
priceSensBS <- m$addParameter(
name = "priceSensBS",
domain = c("var", "reg", "loc", "typ", "inc"),
records = priceSensBS,
description = "price sensitivity of building shell choice"
)

priceSensHS <- unlist(config[["priceSens"]][["hs"]])
priceSensHS <- expandSets("var", "reg", "loc", "typ", "inc", .m = m) %>%
filter(.data[["var"]] %in% names(priceSensHS)) %>%
mutate(value = priceSensHS[.data[["var"]]])
priceSensHS <- m$addParameter(
name = "priceSensHS",
domain = c("var", "reg", "loc", "typ", "inc"),
records = priceSensHS,
description = "price sensitivity of heating system choice"
)


return(m)
}

Expand Down
9 changes: 8 additions & 1 deletion R/createSets.R
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ createSets <- function(m, config) {
))



# Temporal -------------------------------------------------------------------

ttotNum <- periodFromConfig(config, "ttot")
Expand Down Expand Up @@ -66,6 +65,14 @@ createSets <- function(m, config) {
description = "historic time steps"
))

if (grepl("calibration", config[["switches"]][["RUNTYPE"]], fixed = TRUE)) {
invisible(m$addSet(
"tcalib",
records = periodFromConfig(config, "tcalib"),
description = "time steps considered by the calibration when minimising deviation from target trajectories"
))
}


# Vintages -------------------------------------------------------------------

Expand Down
7 changes: 6 additions & 1 deletion R/initModel.R
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,12 @@ initModel <- function(config = NULL,

cfg <- readConfig(config = config,
configFolder = configFolder)
title <- cfg[["title"]]
title <- paste(cfg[["title"]], sep = "-")

if (cfg[["switches"]][["RUNTYPE"]] %in% c("calibrationOptimization", "calibrationLogit")) {
title <- paste0(title, cfg[["parameters"]][["iteration"]], "Iter",
cfg[["parameters"]][["stepSize"]], "A")
}

if (is.null(path)) {
stamp <- format(Sys.time(), "_%Y-%m-%d_%H.%M.%S")
Expand Down
8 changes: 8 additions & 0 deletions R/periodFromConfig.R
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,13 @@ periodFromConfig <- function(config, periodType) {

t <- ttot[which(ttot >= startyear)]

if (periodType == "tcalib") {
calibperiods <- config[["calibperiods"]]
#TODO: Add check that calibration periods are consistent with t
} else {
calibperiods <- min(t)
}

switch(periodType,
ttot = ttot,
tall = min(ttot):max(ttot),
Expand All @@ -28,6 +35,7 @@ periodFromConfig <- function(config, periodType) {
thist = setdiff(ttot, t),
tinit = min(ttot),
t0 = min(t),
tcalib = calibperiods,
stop("unknown type of period: ", periodType)
)
}
Loading