From 4810a47563e3a03ac8d6366afe8075bbc4fdc307 Mon Sep 17 00:00:00 2001 From: ahagen Date: Wed, 30 Oct 2024 10:51:41 +0100 Subject: [PATCH] Scripts for PR test routine/ChangeLog --- tests/PRtestEDGET.R | 122 +++++++++++++++++++++++++++ tests/compScenPR.R | 95 +++++++++++++++++++++ tests/renvfromsource.R | 9 ++ tests/submit_compScenPR.sh | 17 ++++ tests/test-standard-runs | 165 +++++++++++++++++++++++++++++++++++++ 5 files changed, 408 insertions(+) create mode 100644 tests/PRtestEDGET.R create mode 100644 tests/compScenPR.R create mode 100644 tests/renvfromsource.R create mode 100644 tests/submit_compScenPR.sh create mode 100755 tests/test-standard-runs diff --git a/tests/PRtestEDGET.R b/tests/PRtestEDGET.R new file mode 100644 index 0000000..bc3ab7e --- /dev/null +++ b/tests/PRtestEDGET.R @@ -0,0 +1,122 @@ +#!/usr/bin/env Rscript + +args <- commandArgs(trailingOnly = TRUE) + +folderName <- args[1] +defScen <- args[2] +refFolder <- NULL +if (length(args) == 3) { + refFolder <- args[3] +} + +# maybe a check for possible argument errors here? only needed if script is used individually + +renv::status() +renv::restore() + +library(madrat) +library(mrdrivers) +library(mrremind) +library(dplyr) +library(tidyr) +library(edgeTransport) +library(reporttransport) +library(piamPlotComparison) + +testStandardScenarios <- function(folderNameD, defScenN){ + + allScens <- tribble( + ~SSPscen, ~transportPolScen, ~isICEban, ~demScen, + 'SSP2', 'Mix1', FALSE, 'default', + 'SSP2', 'Mix2', TRUE, 'default', + 'SSP2', 'Mix3', TRUE, 'default', + 'SSP2', 'Mix4', TRUE, 'default' + ) + + # generate list from data frame rows + allScens <- split(allScens, seq(nrow(allScens))) + defScen <- allScens[[defScenN]] + + EdgeTransportSAdata <- calcEdgeTransportSA(SSPscen = defScen[["SSPscen"]], + transportPolScen = defScen[["transportPolScen"]], + isICEban = defScen[["isICEban"]], + demScen = defScen[["demScen"]], + isTransportReported = TRUE, + isTransportExtendedReported = TRUE, + isREMINDinputReported = TRUE, + isAnalyticsReported = TRUE, + isStored = TRUE, + outputFolder = folderNameD) + + renv::snapshot(type = "all") +} + +plotStandardScenarios <- function(folderNameD, defScenN, refFolderD = NULL){ + + n <- as.numeric(defScenN) + refFolder <- refFolderD + current_path <- getwd() + PRchangeLogPath <- dirname(current_path) + + testDirs <- sort(list.dirs(path = PRchangeLogPath, recursive = FALSE)) + #Only use folders that comply with naming convention + testDirs <- testDirs[grepl(".*\\d{8}_.*", testDirs)] + + if (is.character(refFolder)) { + testDirs <- testDirs[c(grep(refFolderD, testDirs), grep(current_path, testDirs))] + } else { + testDirs <- testDirs[c(grep(current_path, testDirs)-1, grep(current_path, testDirs))] + } + + mifHist <- "/p/projects/edget/PRchangeLog/historical21.mif" + + allScens <- tribble( + ~transportPolScen, ~scenName1, ~scenName2, ~filename, + 'Mix1', 'Mix1before', 'Mix1after', "ChangeLogMix1", + 'Mix2', 'Mix2before', 'Mix2after', "ChangeLogMix2", + 'Mix3', 'Mix3before', 'Mix3after', "ChangeLogMix3", + 'Mix4', 'Mix4before', 'Mix4after', "ChangeLogMix4" + ) + + mifs <- list.files(testDirs, "Transport.mif", full.names = TRUE, recursive = TRUE) + + mifs <- mifs[grep(allScens$transportPolScen[n], mifs)] + scenNames <- c(allScens$scenName1[n], allScens$scenName2[n]) + filename <- allScens$filename[n] + outDir <- file.path(folderNameD, filename) + dir.create(outDir, showWarnings = FALSE) + + sec <- c("00_info", "01_energy_demand","02_energy_services","03_energy_intensity", "04_stock_and_sales", "05_emissions", "06_input_parameters", "08_transportRemindInputfiles") + + #EU21 + piamPlotComparison::compareScenarios( + projectLibrary = "reporttransport", + mifs, + mifHist, + outDir, + outputFile = paste0(format(Sys.time(), "%Y-%m-%d_%H.%M.%S"), "_", filename, "EUR.pdf"), + outputFormat = "pdf", + mifScenNames = scenNames, + sections = sec, + reg = c("ENC","EWN","ECS","ESC","ECE","FRA","DEU","UKI","ESW","EUR"), + mainReg = "EUR" + ) + + #H12 + piamPlotComparison::compareScenarios( + projectLibrary = "reporttransport", + mifs, + mifHist, + outDir, + outputFile = paste0(format(Sys.time(), "%Y-%m-%d_%H.%M.%S"), "_", filename, "H12.pdf"), + outputFormat = "pdf", + mifScenNames = scenNames, + sections = sec, + reg = c("OAS","MEA","SSA","LAM","REF","CAZ","CHA","IND","JPN","USA","NEU","EUR","World"), + mainReg = "World" + ) +} + + +testStandardScenarios(folderName, defScen) +plotStandardScenarios(folderName, defScen, refFolder) diff --git a/tests/compScenPR.R b/tests/compScenPR.R new file mode 100644 index 0000000..2c3ed5c --- /dev/null +++ b/tests/compScenPR.R @@ -0,0 +1,95 @@ +#!/usr/bin/env Rscript + +args <- commandArgs(trailingOnly = TRUE) + +refFolder <- args[1] +defScen <- args[2] + +help <- " +Usage: Rscript compScenPR.R + +Compares a standard scenario run of two different edgeT versions via compScen's. A Transport.mif from the working directory is compared with the corresponding scenario run of . + + : optional, one of 1,2,3,4 corresponding to the standard transport scenarios, Mix1, Mix2ICEban, Mix3ICEban, Mix4ICEban + + -h, --help show this text and exit +" + +if (2 != length(args)) { + cat("ERROR: Wrong number of Arguments!") + cat(help) + quit(save = 'no', status = 1) +} + +library(dplyr) +library(tidyr) +library(reporttransport) +library(piamPlotComparison) + +plotStandardScenarios <- function(refFolderD, defScenN = 2){ + + n <- as.numeric(defScenN) + refFolder <- refFolderD + current_path <- getwd() + PRchangeLogPath <- dirname(current_path) + + testDirs <- sort(list.dirs(path = PRchangeLogPath, recursive = FALSE)) + #Only use folders that comply with naming convention + testDirs <- testDirs[grepl(".*\\d{8}_.*", testDirs)] + + if (is.character(refFolder)) { + testDirs <- testDirs[c(grep(refFolderD, testDirs), grep(current_path, testDirs))] + } else { + testDirs <- testDirs[c(grep(current_path, testDirs)-1, grep(current_path, testDirs))] + } + + mifHist <- "/p/projects/edget/PRchangeLog/historical21.mif" + + allScens <- tribble( + ~transportPolScen, ~scenName1, ~scenName2, ~filename, + 'Mix1', 'Mix1before', 'Mix1after', "ChangeLogMix1", + 'Mix2', 'Mix2before', 'Mix2after', "ChangeLogMix2", + 'Mix3', 'Mix3before', 'Mix3after', "ChangeLogMix3", + 'Mix4', 'Mix4before', 'Mix4after', "ChangeLogMix4" + ) + + mifs <- list.files(testDirs, "Transport.mif", full.names = TRUE, recursive = TRUE) + + mifs <- mifs[grep(allScens$transportPolScen[n], mifs)] + scenNames <- c(allScens$scenName1[n], allScens$scenName2[n]) + filename <- allScens$filename[n] + outDir <- file.path(current_path, filename) + dir.create(outDir, showWarnings = FALSE) + + sec <- c("00_info", "01_energy_demand","02_energy_services","03_energy_intensity", "04_stock_and_sales", "05_emissions", "06_input_parameters", "08_transportRemindInputfiles") + + #EU21 + piamPlotComparison::compareScenarios( + projectLibrary = "reporttransport", + mifs, + mifHist, + outDir, + outputFile = paste0(format(Sys.time(), "%Y-%m-%d_%H.%M.%S"), "_", filename, "EUR.pdf"), + outputFormat = "pdf", + mifScenNames = scenNames, + sections = sec, + reg = c("ENC","EWN","ECS","ESC","ECE","FRA","DEU","UKI","ESW","EUR"), + mainReg = "EUR" + ) + + #H12 + piamPlotComparison::compareScenarios( + projectLibrary = "reporttransport", + mifs, + mifHist, + outDir, + outputFile = paste0(format(Sys.time(), "%Y-%m-%d_%H.%M.%S"), "_", filename, "H12.pdf"), + outputFormat = "pdf", + mifScenNames = scenNames, + sections = sec, + reg = c("OAS","MEA","SSA","LAM","REF","CAZ","CHA","IND","JPN","USA","NEU","EUR","World"), + mainReg = "World" + ) +} + +plotStandardScenarios(refFolder, defScen) diff --git a/tests/renvfromsource.R b/tests/renvfromsource.R new file mode 100644 index 0000000..02a7c4d --- /dev/null +++ b/tests/renvfromsource.R @@ -0,0 +1,9 @@ +#!/usr/bin/env Rscript + +sourceRepos <- commandArgs(trailingOnly = TRUE) + +renv::load() + +renv::install(sourceRepos, type = source) + +renv::snapshot(type = "all") diff --git a/tests/submit_compScenPR.sh b/tests/submit_compScenPR.sh new file mode 100644 index 0000000..266a855 --- /dev/null +++ b/tests/submit_compScenPR.sh @@ -0,0 +1,17 @@ +#!/bin/bash + +#SBATCH --qos=short +#SBATCH --job-name=compScen_edgetPR +#SBATCH --output=%x-%j.out +#SBATCH --ntasks=4 +#SBATCH --nodes=4 +#SBATCH --ntasks-per-node=1 +#SBATCH --mem=32000 +#SBATCH --time=02:00:00 + +srun --ntasks=1 --nodes=1 Rscript compScenPR.R $1 "1" & +srun --ntasks=1 --nodes=1 Rscript compScenPR.R $1 "2" & +srun --ntasks=1 --nodes=1 Rscript compScenPR.R $1 "3" & +srun --ntasks=1 --nodes=1 Rscript compScenPR.R $1 "4" & + +wait diff --git a/tests/test-standard-runs b/tests/test-standard-runs new file mode 100755 index 0000000..df444aa --- /dev/null +++ b/tests/test-standard-runs @@ -0,0 +1,165 @@ +#!/bin/bash + +help=' +Usage: ./test-standard-runs + +Creates a new directory in and initializes renv there. +Starts edgeTransport runs for all four standard policy scenarios and compares data with a previous version located in via compareScenarios. + + : name of the new folder to be generated, ideally relating to a PR + : optional, any unique substring of the folder name as identifier, + runs from this folder are chosen for compScens, shares path with + : optional, path to and , should end with "/" + + -h, --help show this text and exit +' + +# handle missing input parameters +if [ $# -lt 1 -o $# -gt 3 ] +then + echo -e "\e[0;31mERROR: wrong number of arguments\e[31m" + echo "$help" + exit 1 +fi + +# handle help +if [ "-h" = $1 -o "--help" = $1 ] +then + echo "$help" + exit 0 +fi + +# get path and folders +current_date=$(date +"%Y%m%d") +folder_name="${current_date}_${1}" + +if [ $# -eq 1 ] +then + working_path="$PWD/" +fi + +if [ $# -eq 2 ] +then + if [[ $2 =~ ['/\'] ]]; then + working_path=${2} + else + ref_folder=${2} + working_path="$PWD/" + fi +fi + +if [ $# -eq 3 ] +then + ref_folder=${2} + working_path=${3} +fi + +echo -e "\e[0;32mOutput folder name: ${folder_name}\e[0m" +echo -e "\e[0;32mReference folder name for plots: ${ref_folder}\e[0m" +echo -e "\e[0;32mPath that will be used: ${working_path}\e[0m" + +mkdir ${working_path}${folder_name} + +# copy scripts for renv set-up and test run of scenarios +cp /p/projects/edget/PRchangeLog/.helperTestStd/renvfromsource.R ${working_path}${folder_name}/ +cp /p/projects/edget/PRchangeLog/.helperTestStd/PRtestEDGET.R ${working_path}${folder_name}/ + +# copy scripts for additional compScen's if needed +cp /p/projects/edget/PRchangeLog/.helperTestStd/*compScenPR* ${working_path}${folder_name}/ + +cd ${working_path}${folder_name} + +# interactive set-up of renv +Rscript -e 'renv::init()' +Rscript -e 'renv::install("renv@1.0.7")' +Rscript -e ' +renv::record("renv@1.0.7") +renv::install("madrat") +renv::install("mrdrivers") +renv::install("mrremind") +renv::install("mrtransport") +renv::install("dplyr") +renv::install("tidyr") +renv::install("tidyverse") +renv::install("edgeTransport") +renv::install("reporttransport") +renv::install("data.table") +renv::snapshot(type = "all")' + +while true; do + read -p "Do you want to install (additional) repos from source?" yn + case $yn in + [Yy]* ) echo -e "\e[0;32mPlease provide your local repository without quotes.\e[0m"; echo -e "\e[0;32mAn absolute local path /p/projects/edget/some/repo or a branch on GitHub username/repo@branch is possible.\e[0m"; read repos; + Rscript renvfromsource.R ${repos};; + [Nn]* ) break;; + * ) echo -e "\e[0;31mPlease answer Y or N.\e[0m";; + esac +done + +echo -e "\e[0;32mset up of renv finished\e[0m" + +# generate temporary sbatch scripts to sent jobs to slurm +# Mix1 +cat < tmp_submit_test1.sbatch +#!/bin/bash +#SBATCH --qos=short +#SBATCH --job-name=testScen1_edgetPR +#SBATCH --output=%x-%j.out +#SBATCH --nodes=1 +#SBATCH --ntasks-per-node=1 +#SBATCH --mem=32000 +#SBATCH --time=05:00:00 + +srun Rscript PRtestEDGET.R ${working_path}${folder_name} "1" ${ref_folder} +EOL + +# Mix2 ICEban +cat < tmp_submit_test2.sbatch +#!/bin/bash +#SBATCH --qos=short +#SBATCH --job-name=testScen2_edgetPR +#SBATCH --output=%x-%j.out +#SBATCH --nodes=1 +#SBATCH --ntasks-per-node=1 +#SBATCH --mem=32000 +#SBATCH --time=05:00:00 + +srun Rscript PRtestEDGET.R ${working_path}${folder_name} "2" ${ref_folder} +EOL + +# Mix3 ICEban +cat < tmp_submit_test3.sbatch +#!/bin/bash +#SBATCH --qos=short +#SBATCH --job-name=testScen3_edgetPR +#SBATCH --output=%x-%j.out +#SBATCH --nodes=1 +#SBATCH --ntasks-per-node=1 +#SBATCH --mem=32000 +#SBATCH --time=05:00:00 + +srun Rscript PRtestEDGET.R ${working_path}${folder_name} "3" ${ref_folder} +EOL + +# Mix4 ICEban +cat < tmp_submit_test4.sbatch +#!/bin/bash +#SBATCH --qos=short +#SBATCH --job-name=testScen4_edgetPR +#SBATCH --output=%x-%j.out +#SBATCH --nodes=1 +#SBATCH --ntasks-per-node=1 +#SBATCH --mem=32000 +#SBATCH --time=05:00:00 + +srun Rscript PRtestEDGET.R ${working_path}${folder_name} "4" ${ref_folder} +EOL + +sbatch tmp_submit_test1.sbatch +sbatch tmp_submit_test2.sbatch +sbatch tmp_submit_test3.sbatch +sbatch tmp_submit_test4.sbatch + +#cleanup +rm tmp_submit_test*.sbatch +rm renvfromsource.R