From e232ca115ee63a513b644950a04f533542888407 Mon Sep 17 00:00:00 2001 From: Ben Young Date: Thu, 12 Sep 2024 21:10:13 -0400 Subject: [PATCH] move M matrix test to validation and print any failures --- R/ExternalImportFactors.R | 9 --------- R/ValidateModel.R | 13 +++++++++++++ 2 files changed, 13 insertions(+), 9 deletions(-) diff --git a/R/ExternalImportFactors.R b/R/ExternalImportFactors.R index 552b44b1..6aedcb77 100644 --- a/R/ExternalImportFactors.R +++ b/R/ExternalImportFactors.R @@ -149,14 +149,5 @@ deriveMMatrix <- function(model) { M <- model$M_d %*% diag(as.vector(dr)) + model$M_m %*% diag(as.vector(mr)) colnames(M) <- colnames(model$M_d) - # Validate that M is between M_m and M_d - a <- signif(model$M_m, 6) - b <- signif(M, 6) - c <- signif(model$M_d, 6) - z <- ((b > a) & (b > c)) | ((b < a) & (b < c)) - if(sum(z) > 0) { - stop("Error in deriving M matrix") - } - return(M) } diff --git a/R/ValidateModel.R b/R/ValidateModel.R index 4b41c142..2c95753c 100644 --- a/R/ValidateModel.R +++ b/R/ValidateModel.R @@ -474,6 +474,19 @@ validateImportFactorsApproach <- function(model, demand = "Consumption"){ cat("assuming model$M = model$M_m.\n") print(all.equal(LCIA_dm, LCIA)) + cat("\nValidating that the derived M matrix has all values between M_d and M_m\n") + + # Validate that M is between M_m and M_d + a <- signif(model$M_m, 6) + b <- signif(model$M, 6) + c <- signif(model$M_d, 6) + z <- ((b > a) & (b > c)) | ((b < a) & (b < c)) + if(sum(z) == 0) { + print(TRUE) + } else { + comm <- colSums(z) > 0 + print(paste0("Failures: ", names(comm)[comm == TRUE])) + } } #' Validate the calculation of household_emissions