Skip to content

Commit

Permalink
Change nomenclature in model from M_m to Q_t to align with nomenclatu…
Browse files Browse the repository at this point in the history
…re from Palm paper.
  • Loading branch information
jvendries committed Jan 4, 2024
1 parent d7474cb commit 2706f29
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 20 deletions.
4 changes: 2 additions & 2 deletions R/BuildModel.R
Original file line number Diff line number Diff line change
Expand Up @@ -137,9 +137,9 @@ constructEEIOMatrices <- function(model, configpaths = NULL) {
}
logging::loginfo("Calculating N_d matrix (total environmental impacts per dollar from domestic activity)...")
model$N_d <- model$C %*% model$M_d
if(!is.null(model$M_m)) {
if(!is.null(model$Q_t)) {
logging::loginfo("Calculating N_m matrix (total environmental impacts per dollar from imported activity)...")
model$N_m <- model$C %*% model$M_m # I don't think this is correct. See ImportFactorsTests.rmd, "Calculate the N matrix for the standard model using coupled model approach" chunk.
model$N_m <- model$C %*% model$Q_t # I don't think this is correct. See ImportFactorsTests.rmd, "Calculate the N matrix for the standard model using coupled model approach" chunk.
}
}

Expand Down
6 changes: 3 additions & 3 deletions R/CalculationFunctions.R
Original file line number Diff line number Diff line change
Expand Up @@ -117,15 +117,15 @@ calculateResultsWithExternalFactors <- function(model, demand = "Consumption", u
if (use_domestic_requirements) {
result$LCI_f <- (model$B %*% model$L_d %*% y_d)
} else {
result$LCI_f <- (model$B %*% model$L_d %*% y_d) + (model$M_m %*% model$A_m %*% model$L_d %*% y_d + model$M_m %*% y_m)
result$LCI_f <- (model$B %*% model$L_d %*% y_d) + (model$Q_t %*% model$A_m %*% model$L_d %*% y_d + model$Q_t %*% y_m)
}
result$LCIA_f <- model$C %*% result$LCI_f

result$LCI_f <- t(result$LCI_f)
result$LCIA_f <- t(result$LCIA_f)

colnames(result$LCI_f) <- rownames(model$M_m)
rownames(result$LCI_f) <- colnames(model$M_m)
colnames(result$LCI_f) <- rownames(model$Q_t)
rownames(result$LCI_f) <- colnames(model$Q_t)

colnames(result$LCIA_f) <- rownames(model$N_m)
rownames(result$LCIA_f) <- colnames(model$N_m)
Expand Down
12 changes: 6 additions & 6 deletions R/IOFunctions.R
Original file line number Diff line number Diff line change
Expand Up @@ -277,14 +277,14 @@ buildModelwithImportFactors <- function(model, configpaths = NULL) {
logging::loginfo("Calculating M_d matrix (total emissions and resource use per dollar from domestic activity)...")
model$M_d <- model$B %*% model$L_d

logging::loginfo("Calculating M_m matrix (total emissions and resource use per dollar from imported activity)...")
M_m <- loadExternalImportFactors(model, configpaths)
logging::loginfo("Calculating Q_t matrix (total emissions and resource use per dollar from imported activity)...")
Q_t <- loadExternalImportFactors(model, configpaths)

# Fill in flows for M_m not found in Import Factors but that exist in model and align order
M_m <- rbind(M_m, model$M_d[setdiff(rownames(model$M_d), rownames(M_m)),])
M_m <- M_m[rownames(model$M_d), ]
# Fill in flows for Q_t not found in Import Factors but that exist in model and align order
Q_t <- rbind(Q_t, model$M_d[setdiff(rownames(model$M_d), rownames(Q_t)),])
Q_t <- Q_t[rownames(model$M_d), ]

model$M_m <- M_m
model$Q_t <- Q_t

return(model)
}
Expand Down
12 changes: 6 additions & 6 deletions R/LoadExternalImportFactors.R
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
#' @param model An EEIO form USEEIO model object with model specs loaded
#' @param configpaths str vector, paths (including file name) of model configuration file
#' and optional agg/disagg configuration file(s). If NULL, built-in config files are used.
#' @return M_m, matrix of import coefficients (flow x sector).
#' @return Q_t, matrix of import coefficients (flow x sector).
loadExternalImportFactors <- function(model, configpaths = NULL) {

# Read in file with Import factors
Expand Down Expand Up @@ -50,12 +50,12 @@ loadExternalImportFactors <- function(model, configpaths = NULL) {
IFTable['Location'] <- "US"
}

M_m <- standardizeandcastSatelliteTable(IFTable, model)
Q_t <- standardizeandcastSatelliteTable(IFTable, model)
# standardizeandcast prepares df for industries, convert to commodities
M_m[, setdiff(model$Commodities$Code_Loc, colnames(M_m))] <- 0
Q_t[, setdiff(model$Commodities$Code_Loc, colnames(Q_t))] <- 0
# Adjust column order to be the same with V_n rownames
M_m <- M_m[, model$Commodities$Code_Loc]
M_m <- as.matrix(M_m)
Q_t <- Q_t[, model$Commodities$Code_Loc]
Q_t <- as.matrix(Q_t)

return(M_m)
return(Q_t)
}
6 changes: 3 additions & 3 deletions R/ValidateModel.R
Original file line number Diff line number Diff line change
Expand Up @@ -564,12 +564,12 @@ validateImportFactorsApproach <- function(model, demand = "Consumption"){
# Calculate LCI using coupled model approach
# Revised equation from RW email (2023-11-01):
# LCI <- (s_d * L_d * Y_d) + (s*L*A_m*L_d*Y_d + s*L*Y_m). I.e., s in RW email is analogous to model$B
# For validation, model$M = model$M_m, whereas in normally we'd be using model$M_m instead of model$M
# For validation, model$M = model$Q_t, whereas in normally we'd be using model$Q_t instead of model$M

LCI_dm <- (model$M_d %*% y_d) + (M %*% model$A_m %*% model$L_d %*% y_d + M %*% y_m)

cat("\nTesting that LCI results are equivalent between standard and coupled model approaches (i.e., LCI = LCI_dm) when\n")
cat("assuming model$M = model$M_m.\n")
cat("assuming model$M = model$Q_t.\n")
print(all.equal(LCI, LCI_dm))

# Calculate LCIA using standard approach
Expand All @@ -588,7 +588,7 @@ validateImportFactorsApproach <- function(model, demand = "Consumption"){
rownames(LCIA_dm) <- colnames(model$N_m)

cat("\nTesting that LCIA results are equivalent between standard and coupled model approaches (i.e., LCIA = LCIA_dm) when\n")
cat("assuming model$M = model$M_m.\n")
cat("assuming model$M = model$Q_t.\n")
all.equal(LCIA_dm, LCIA)

}

0 comments on commit 2706f29

Please sign in to comment.