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

remove variables / parameters after deletion of industry realization fixed_shares #685

Draft
wants to merge 3 commits 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
2 changes: 1 addition & 1 deletion .buildlibrary
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
ValidationKey: '233451114'
ValidationKey: '2336667836104'
AcceptedWarnings:
- .*following variables are expected in the piamInterfaces.*
- Summation checks have revealed some gaps.*
Expand Down
7 changes: 7 additions & 0 deletions .github/workflows/check.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,13 @@ jobs:
[ -f requirements.txt ] && python -m pip install --upgrade pip wheel || true
[ -f requirements.txt ] && pip install -r requirements.txt || true

- name: Run pre-commit checks
shell: bash
run: |
python -m pip install pre-commit
python -m pip freeze --local
pre-commit run --show-diff-on-failure --color=always --all-files

- name: Verify validation key
shell: Rscript {0}
run: lucode2:::validkey(stopIfInvalid = TRUE)
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@ tests/testthat/test-convGDX2mif-data
.github/workflows/lucode2-check.yaml
.Rprofile
renv
remind2.Rproj
2 changes: 1 addition & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,4 @@ repos:
- id: readme-rmd-rendered
- id: use-tidy-description
ci:
autoupdate_schedule: quarterly
autoupdate_schedule: weekly
4 changes: 2 additions & 2 deletions CITATION.cff
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ cff-version: 1.2.0
message: If you use this software, please cite it using the metadata from this file.
type: software
title: 'remind2: The REMIND R package (2nd generation)'
version: 1.162.2
date-released: '2024-12-30'
version: 1.162.2.9001
date-released: '2025-01-16'
abstract: Contains the REMIND-specific routines for data and model output manipulation.
authors:
- family-names: Rodrigues
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: remind2
Title: The REMIND R package (2nd generation)
Version: 1.162.2
Date: 2024-12-30
Version: 1.162.2.9001
Date: 2025-01-16
Authors@R: c(
person("Renato", "Rodrigues", , "[email protected]", role = c("aut", "cre")),
person("Lavinia", "Baumstark", role = "aut"),
Expand Down
14 changes: 0 additions & 14 deletions R/reportLCOE.R
Original file line number Diff line number Diff line change
Expand Up @@ -1381,7 +1381,6 @@ reportLCOE <- function(gdx, output.type = "both") {

### for buildings and industry (calculate average cost for simplicity, not marginal)

v37_costAddTeInvH2 <- readGDX(gdx, "v37_costAddTeInvH2", field = "l", restore_zeros = FALSE, react = "silent")
v36_costAddTeInvH2 <- readGDX(gdx, "v36_costAddTeInvH2", field = "l", restore_zeros = FALSE, react = "silent")

df.AddTeInvH2 <- NULL
Expand All @@ -1396,19 +1395,6 @@ reportLCOE <- function(gdx, output.type = "both") {
df.AddTeInvH2 <- rbind(df.AddTeInvH2, df.AddTeInvH2Build)
}

if (!is.null(v37_costAddTeInvH2)) {

df.AddTeInvH2Indst <- as.quitte(v37_costAddTeInvH2[, ttot_from2005, "tdh2s"] / collapseNames(dimSums(vm_demFeSector[, ttot_from2005, "seh2.feh2s.indst"], dim = 3.4, na.rm = TRUE)) / s_twa2mwh * 1e12 * s_usd2017t2015) %>%
mutate(value = ifelse(is.infinite(value), 0, value)) %>%
mutate(emi_sectors = "industry") %>%
select(region, period, all_te, emi_sectors, value) %>%
rename(AddH2TdCost = value, sector = emi_sectors, tech = all_te)

df.AddTeInvH2 <- rbind(df.AddTeInvH2, df.AddTeInvH2Indst)
}



# Join all data for marginal LCOE calculation ----

### create table with all parameters needed for LCOE calculation
Expand Down
217 changes: 95 additions & 122 deletions R/reportMacroEconomy.R
Original file line number Diff line number Diff line change
Expand Up @@ -58,34 +58,27 @@ reportMacroEconomy <- function(gdx, regionSubsetList = NULL,

# Realisation of the different modules
if (!is.null(module2realisation) && (!"CES_structure" %in% module2realisation[, 1])) {
indu_mod <- findRealModule(module2realisation, "industry")
buil_mod <- findRealModule(module2realisation, "buildings")
} else {
indu_mod <- "fixed_shares"
buil_mod <- "simple"
}

steel_process_based <- "steel" %in% readGDX(gdx, "secInd37Prc", react='silent')

# choose the CES entries names for transport
name_trsp <- c("fepet", "ueLDVt", "fedie", "ueHDVt", "feelt", "ueelTt")
name_trsp <- name_trsp[name_trsp %in% getNames(vm_cesIO)]


cons <- setNames(readGDX(gdx, name = "vm_cons", field = "l", format = "first_found")[, t2005to2150, ] * 1000,
"Consumption (billion US$2017/yr)")
"Consumption (billion US$2017/yr)")
gdp <- setNames(vm_cesIO[, , "inco"] * 1000, "GDP|MER (billion US$2017/yr)")
shPPPMER <- readGDX(gdx, c("pm_shPPPMER", "p_ratio_ppp"), format = "first_found")
gdp_ppp <- setNames(gdp / shPPPMER, "GDP|PPP (billion US$2017/yr)")
invE <- setNames(readGDX(gdx, name = c("v_costInv", "v_costin"), field = "l", format = "first_found") * 1000,
"Energy Investments (billion US$2017/yr)")
"Energy Investments (billion US$2017/yr)")
pop <- setNames(readGDX(gdx, name = c("pm_pop", "pm_datapop"), format = "first_found")[, t2005to2150, ] * 1000,
"Population (million)")
"Population (million)")


damageFactor <- setNames(readGDX(gdx, name = c("vm_damageFactor", "vm_damage"), field = "l",
format = "first_found"),
"Damage factor (1)")
"Damage factor (1)")

# Calculate net GDP using the damage factors
tintersect <- intersect(getYears(gdp), getYears(damageFactor))
Expand Down Expand Up @@ -154,11 +147,6 @@ reportMacroEconomy <- function(gdx, regionSubsetList = NULL,
# macro
ces <- mbind(ces, setNames(vm_cesIO[, , "kap"] * 1000, "CES_input|kap (billion US$2017)"))

# transport
ces <- mbind(ces, setNames(vm_cesIO[, , name_trsp[3]] * TWa_2_EJ, "CES_input|feelt (EJ/yr)"))
ces <- mbind(ces, setNames(vm_cesIO[, , name_trsp[1]] * TWa_2_EJ, "CES_input|fepet (EJ/yr)"))
ces <- mbind(ces, setNames(vm_cesIO[, , name_trsp[2]] * TWa_2_EJ, "CES_input|fedie (EJ/yr)"))

if (buil_mod == "simple") {
ces <- mbind(ces, setNames(vm_cesIO[, , "feelb"] * TWa_2_EJ, "CES_input|feelb (EJ/yr)"))
ces <- mbind(ces, setNames(vm_cesIO[, , "fegab"] * TWa_2_EJ, "CES_input|fegab (EJ/yr)"))
Expand All @@ -168,32 +156,24 @@ reportMacroEconomy <- function(gdx, regionSubsetList = NULL,
ces <- mbind(ces, setNames(vm_cesIO[, , "feh2b"] * TWa_2_EJ, "CES_input|feh2b (EJ/yr)"))
}

if (indu_mod == "fixed_shares") {
ces <- mbind(ces, setNames(vm_cesIO[, , "feeli"] * TWa_2_EJ, "CES_input|feeli (EJ/yr)"))
ces <- mbind(ces, setNames(vm_cesIO[, , "fegai"] * TWa_2_EJ, "CES_input|fegai (EJ/yr)"))
ces <- mbind(ces, setNames(vm_cesIO[, , "fehoi"] * TWa_2_EJ, "CES_input|fehoi (EJ/yr)"))
ces <- mbind(ces, setNames(vm_cesIO[, , "fesoi"] * TWa_2_EJ, "CES_input|fesoi (EJ/yr)"))
ces <- mbind(ces, setNames(vm_cesIO[, , "fehei"] * TWa_2_EJ, "CES_input|fehei (EJ/yr)"))
ces <- mbind(ces, setNames(vm_cesIO[, , "feh2i"] * TWa_2_EJ, "CES_input|feh2i (EJ/yr)"))
} else if ("subsectors" == indu_mod) {
ces <- mbind(
ces,
mbind(
lapply(ppfkap_ind,
function(x) {
setNames(vm_cesIO[, , x] * 1000,
paste0("CES_input|", x, " (billion US$2017)"))
}
)),

mbind(lapply(ppfen_ind,
function(x) {
setNames(vm_cesIO[, , x] * TWa_2_EJ,
paste0("CES_input|", x, " (EJ/yr)"))
}
))
)
}

ces <- mbind(
ces,
mbind(
lapply(ppfkap_ind,
function(x) {
setNames(vm_cesIO[, , x] * 1000,
paste0("CES_input|", x, " (billion US$2017)"))
}
)),

mbind(lapply(ppfen_ind,
function(x) {
setNames(vm_cesIO[, , x] * TWa_2_EJ,
paste0("CES_input|", x, " (EJ/yr)"))
}
))
)


#### ---- additional variables for CES function reporting ----
Expand All @@ -204,104 +184,97 @@ reportMacroEconomy <- function(gdx, regionSubsetList = NULL,
# check that CES derivatives output parameter exists in GDX
if ((length(o01_CESmrs > 0)) & (length(o01_CESderivatives > 0))) {

## 1.) CES Prices (CES Derivatives)

# remove inco from inputs as no CES derivative exists
inputs <- inputs[inputs != "inco"]
# get sets of CES inputs which are FEs and those which are no FEs
inputs.fe <- grep("fe",inputs, value=T)
inputs.nofe <- setdiff(inputs,inputs.fe)
## 1.) CES Prices (CES Derivatives)

# CES prices of inputs are derivatives of inco (GDP) w.r.t to input
CES.price <- collapseNames(mselect(o01_CESderivatives, all_in = "inco", all_in1 = inputs))
# remove inco from inputs as no CES derivative exists
inputs <- inputs[inputs != "inco"]
# get sets of CES inputs which are FEs and those which are no FEs
inputs.fe <- grep("fe",inputs, value=T)
inputs.nofe <- setdiff(inputs,inputs.fe)

# convert FE input prices to USD/GJ (like PE, SE, FE Prices)
CES.price.fe <- setNames(CES.price[,,inputs.fe] / as.numeric(sm_DpGJ_2_TDpTWa),
paste0("Internal|CES Function|CES Price|",
getNames(CES.price[,,inputs.fe]), " (US$2017/GJ)"))
# CES prices of inputs are derivatives of inco (GDP) w.r.t to input
CES.price <- collapseNames(mselect(o01_CESderivatives, all_in = "inco", all_in1 = inputs))

# leave non-FE input prices generally as they are in trUSD/CES input
CES.price.nofe <- setNames(CES.price[,,inputs.nofe],
# convert FE input prices to USD/GJ (like PE, SE, FE Prices)
CES.price.fe <- setNames(CES.price[,,inputs.fe] / as.numeric(sm_DpGJ_2_TDpTWa),
paste0("Internal|CES Function|CES Price|",
getNames(CES.price[,,inputs.nofe]), " (trUS$2017/Input)"))
getNames(CES.price[,,inputs.fe]), " (US$2017/GJ)"))

# bind FE and non-FE CES prices to one array
CES.price <- mbind(CES.price.fe, CES.price.nofe)
# leave non-FE input prices generally as they are in trUSD/CES input
CES.price.nofe <- setNames(CES.price[,,inputs.nofe],
paste0("Internal|CES Function|CES Price|",
getNames(CES.price[,,inputs.nofe]), " (trUS$2017/Input)"))

# bind FE and non-FE CES prices to one array
CES.price <- mbind(CES.price.fe, CES.price.nofe)


## 2.) Marginal rates of substitution

# equals ratio of CES prices
# corresponds to amount of second input needed to substitute the first input to generate the same economic value
# only needed for certain FE inputs to assess substitution/rebound effect from price changes
## 2.) Marginal rates of substitution

# add MRS from o01_CESmrs parameter
mrs.report <- c("feelhpb.fehob",
"feelhpb.fesob",
"feelhpb.feheb",
"feelhpb.feelrhb",
"feh2b.fegab")
# equals ratio of CES prices
# corresponds to amount of second input needed to substitute the first input to generate the same economic value
# only needed for certain FE inputs to assess substitution/rebound effect from price changes

if (indu_mod == "subsectors") {
# add MRS from o01_CESmrs parameter
mrs.report <- c("feelhpb.fehob",
"feelhpb.fesob",
"feelhpb.feheb",
"feelhpb.feelrhb",
"feh2b.fegab",

"feelhth_otherInd.fega_otherInd",
"feelhth_otherInd.feli_otherInd",
"feelhth_otherInd.feso_otherInd",
"feelhth_chemicals.fega_chemicals",
"feelhth_chemicals.feli_chemicals",

mrs.report <- c(mrs.report,
"feh2_otherInd.fega_otherInd",
"feh2_otherInd.feli_otherInd",
"feh2_otherInd.feso_otherInd",
"feh2_chemicals.fega_chemicals",
"feh2_chemicals.feli_chemicals",
"feh2_cement.fega_cement",
"feh2_cement.feso_cement",
"feh2_cement.feli_cement")

if (!steel_process_based) {
mrs.report <- append(mrs.report,
"feh2_steel.feso_steel"
)
}


"feelhth_otherInd.fega_otherInd",
"feelhth_otherInd.feli_otherInd",
"feelhth_otherInd.feso_otherInd",
"feelhth_chemicals.fega_chemicals",
"feelhth_chemicals.feli_chemicals",

"feh2_otherInd.fega_otherInd",
"feh2_otherInd.feli_otherInd",
"feh2_otherInd.feso_otherInd",
"feh2_chemicals.fega_chemicals",
"feh2_chemicals.feli_chemicals",
"feh2_cement.fega_cement",
"feh2_cement.feso_cement",
"feh2_cement.feli_cement")
CES.mrs <- collapseDim(setNames(o01_CESmrs[,,mrs.report],
paste0("Internal|CES Function|MRS|",gsub("\\.","\\|",mrs.report), " (ratio)")))

if (!steel_process_based) {
mrs.report <- append(mrs.report,
"feh2_steel.feso_steel"
)
}
# add further MRS by calculating price ratio for inputs which are not in one nest

}
CES.mrs <- mbind( CES.mrs,

CES.mrs <- collapseDim(setNames(o01_CESmrs[,,mrs.report],
paste0("Internal|CES Function|MRS|",gsub("\\.","\\|",mrs.report), " (ratio)")))
setNames(CES.price[,,"Internal|CES Function|CES Price|feelhpb (US$2017/GJ)"] /
CES.price[,,"Internal|CES Function|CES Price|fegab (US$2017/GJ)"],
"Internal|CES Function|MRS|feelhpb|fegab (ratio)"),

# add further MRS by calculating price ratio for inputs which are not in one nest
if (indu_mod == "subsectors") {
CES.mrs <- mbind( CES.mrs,
setNames(CES.price[,,"Internal|CES Function|CES Price|feelhpb (US$2017/GJ)"] /
CES.price[,,"Internal|CES Function|CES Price|feh2b (US$2017/GJ)"],
"Internal|CES Function|MRS|feelhpb|feh2b (ratio)"))

setNames(CES.price[,,"Internal|CES Function|CES Price|feelhpb (US$2017/GJ)"] /
CES.price[,,"Internal|CES Function|CES Price|fegab (US$2017/GJ)"],
"Internal|CES Function|MRS|feelhpb|fegab (ratio)"),
if (!steel_process_based) {
CES.mrs <- mbind( CES.mrs,
setNames(CES.price[,,"Internal|CES Function|CES Price|feel_steel_secondary (US$2017/GJ)"] /
CES.price[,,"Internal|CES Function|CES Price|feso_steel (US$2017/GJ)"],
"Internal|CES Function|MRS|feel_steel_secondary|feso_steel (ratio)"))
}

setNames(CES.price[,,"Internal|CES Function|CES Price|feelhpb (US$2017/GJ)"] /
CES.price[,,"Internal|CES Function|CES Price|feh2b (US$2017/GJ)"],
"Internal|CES Function|MRS|feelhpb|feh2b (ratio)"))

if (!steel_process_based) {
CES.mrs <- mbind( CES.mrs,
setNames(CES.price[,,"Internal|CES Function|CES Price|feel_steel_secondary (US$2017/GJ)"] /
CES.price[,,"Internal|CES Function|CES Price|feso_steel (US$2017/GJ)"],
"Internal|CES Function|MRS|feel_steel_secondary|feso_steel (ratio)"))
}
}

## 3.) Value generated by CES Inputs (price * quantity)
CES.value <- setNames( mselect(o01_CESderivatives, all_in = "inco", all_in1 = inputs) * vm_cesIO[,getYears(o01_CESderivatives),inputs] * 1000,
paste0("Internal|CES Function|Value|",inputs, " (billion US$2017)"))
## 3.) Value generated by CES Inputs (price * quantity)
CES.value <- setNames( mselect(o01_CESderivatives, all_in = "inco", all_in1 = inputs) * vm_cesIO[,getYears(o01_CESderivatives),inputs] * 1000,
paste0("Internal|CES Function|Value|",inputs, " (billion US$2017)"))

# bind all CES variables together in one array
ces <- mbind(ces,CES.price,CES.mrs,CES.value)
# bind all CES variables together in one array
ces <- mbind(ces,CES.price,CES.mrs,CES.value)

}
#### end CES function reporting
Expand Down Expand Up @@ -347,17 +320,17 @@ reportMacroEconomy <- function(gdx, regionSubsetList = NULL,
for (t in getYears(out[, which(getYears(out, as.integer = TRUE) > 2005 &
getYears(out, as.integer = TRUE) < max(getYears(out, as.integer = TRUE))), ])) {
inteRate[, t, "Interest Rate (t+1)/(t-1)|Real (unitless)"] <-
(1
- ((setYears(pm_pvp[, (which(getYears(pm_pvp) == t) + 1), ], t) /
setYears(pm_pvp[, (which(getYears(pm_pvp) == t) - 1), ], t))
^ (1 / (getYears(pm_pvp[, (which(getYears(pm_pvp) == t) + 1), ], as.integer = TRUE) -
getYears(pm_pvp[, (which(getYears(pm_pvp) == t) - 1), ], as.integer = TRUE)))
)
(1
- ((setYears(pm_pvp[, (which(getYears(pm_pvp) == t) + 1), ], t) /
setYears(pm_pvp[, (which(getYears(pm_pvp) == t) - 1), ], t))
^ (1 / (getYears(pm_pvp[, (which(getYears(pm_pvp) == t) + 1), ], as.integer = TRUE) -
getYears(pm_pvp[, (which(getYears(pm_pvp) == t) - 1), ], as.integer = TRUE)))
)
)
inteRate[, t, "Interest Rate t/(t-1)|Real (unitless)"] <-
(1
- ((pm_pvp[, t, ] / setYears(pm_pvp[, (which(getYears(pm_pvp) == t) - 1), ], t))
^ (1 / (getYears(pm_pvp[, t, ], as.integer = TRUE) - getYears(pm_pvp[, (which(getYears(pm_pvp) == t) - 1), ],
^ (1 / (getYears(pm_pvp[, t, ], as.integer = TRUE) - getYears(pm_pvp[, (which(getYears(pm_pvp) == t) - 1), ],
as.integer = TRUE)))
)
)
Expand Down
Loading
Loading