diff --git a/R/BuildModel.R b/R/BuildModel.R index 6d35683d..7eaa958e 100644 --- a/R/BuildModel.R +++ b/R/BuildModel.R @@ -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. } } diff --git a/R/CalculationFunctions.R b/R/CalculationFunctions.R index 00e2599c..ed76b1e6 100644 --- a/R/CalculationFunctions.R +++ b/R/CalculationFunctions.R @@ -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) diff --git a/R/IOFunctions.R b/R/IOFunctions.R index 65a46ec2..bce6c454 100644 --- a/R/IOFunctions.R +++ b/R/IOFunctions.R @@ -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) } diff --git a/R/LoadExternalImportFactors.R b/R/LoadExternalImportFactors.R index ae8e6685..22294dd5 100644 --- a/R/LoadExternalImportFactors.R +++ b/R/LoadExternalImportFactors.R @@ -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 @@ -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) } diff --git a/R/ValidateModel.R b/R/ValidateModel.R index 9540e151..9512a1bc 100644 --- a/R/ValidateModel.R +++ b/R/ValidateModel.R @@ -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 @@ -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) }