From 7cacbf8f92b58e21770dd51866ee1b2f7c43d117 Mon Sep 17 00:00:00 2001
From: David <david.dorchies@inrae.fr>
Date: Sat, 28 Dec 2024 08:42:52 +0100
Subject: [PATCH] tests(RunModel.GRiwrmOutputsModel): check simulation result

Refs #179
---
 tests/testthat/helper_1_RunModel.R            | 90 ++++++++++---------
 .../test-RunModel.GRiwrmOutputsModel.R        | 31 ++++---
 2 files changed, 64 insertions(+), 57 deletions(-)

diff --git a/tests/testthat/helper_1_RunModel.R b/tests/testthat/helper_1_RunModel.R
index 57110eb..81423de 100644
--- a/tests/testthat/helper_1_RunModel.R
+++ b/tests/testthat/helper_1_RunModel.R
@@ -18,7 +18,8 @@ setupRunModel <-
            Qinf = NULL,
            Qrelease = NULL,
            Qmin = NULL,
-           IsHyst = FALSE) {
+           IsHyst = FALSE, 
+           ParamMichel = getDefaultParamMichel()) {
 
     data(Severn)
 
@@ -52,49 +53,6 @@ setupRunModel <-
       TempMean <- NULL
     }
 
-    # Calibration parameters
-    ParamMichel <- list(
-      `54057` = c(
-        0.781180650559296,
-        74.4247133147015,
-        -1.26590474908235,
-        0.96012365697022,
-        2.51101785373787
-      ),
-      `54032` = c(
-        0.992743594649893,
-        1327.19309205366,
-        -0.505902143697436,
-        7.91553615210291,
-        2.03604525989572
-      ),
-      `54001` = c(
-        1.03,
-        24.7790862245877,
-        -1.90430150145153,
-        21.7584023961971,
-        1.37837837837838
-      ),
-      `54095` = c(
-        256.844150254651,
-        0.0650458497009288,
-        57.523675209819,
-        2.71809513102128
-      ),
-      `54002` = c(
-        419.437754485522,
-        0.12473266292168,
-        13.0379482833606,
-        2.12230907892238
-      ),
-      `54029` = c(
-        219.203385553954,
-        0.389211590110934,
-        48.4242150713452,
-        2.00300300300301
-      )
-    )
-
     # set up inputs
     if (!runInputsModel)
       return(environment())
@@ -132,3 +90,47 @@ setupRunOptions <- function(InputsModel) {
                                  IndPeriod_Run = IndPeriod_Run)
   return(environment())
 }
+
+getDefaultParamMichel <- function() {
+  list(
+    `54057` = c(
+      0.781180650559296,
+      74.4247133147015,
+      -1.26590474908235,
+      0.96012365697022,
+      2.51101785373787
+    ),
+    `54032` = c(
+      0.992743594649893,
+      1327.19309205366,
+      -0.505902143697436,
+      7.91553615210291,
+      2.03604525989572
+    ),
+    `54001` = c(
+      1.03,
+      24.7790862245877,
+      -1.90430150145153,
+      21.7584023961971,
+      1.37837837837838
+    ),
+    `54095` = c(
+      256.844150254651,
+      0.0650458497009288,
+      57.523675209819,
+      2.71809513102128
+    ),
+    `54002` = c(
+      419.437754485522,
+      0.12473266292168,
+      13.0379482833606,
+      2.12230907892238
+    ),
+    `54029` = c(
+      219.203385553954,
+      0.389211590110934,
+      48.4242150713452,
+      2.00300300300301
+    )
+  )
+}
diff --git a/tests/testthat/test-RunModel.GRiwrmOutputsModel.R b/tests/testthat/test-RunModel.GRiwrmOutputsModel.R
index d0dc612..9ee98b0 100644
--- a/tests/testthat/test-RunModel.GRiwrmOutputsModel.R
+++ b/tests/testthat/test-RunModel.GRiwrmOutputsModel.R
@@ -26,25 +26,29 @@ Qrelease <- data.frame(Dam = rep(100E3, length(DatesR)))
 Qmin <- data.frame("54095" = rep(3E6, length(DatesR)))
 names(Qmin) <- "54095"
 e <- setupRunModel(
+  runRunModel = FALSE,
   griwrm = griwrm,
   Qinf = Qinf,
   Qrelease = Qrelease,
-  Qmin = Qmin,
-  runRunOptions = FALSE
+  Qmin = Qmin
 )
 for (x in ls(e)) assign(x, get(x, e))
 
-# Set up initial conditions
-RunOptions <- CreateRunOptions(InputsModel, IndPeriod_WarmUp = 1:364, IndPeriod_Run = 365L)
-Param <- c(ParamMichel[names(ParamMichel) %in% griwrm$id], list(Dam = c(100E6, 1)))
-OM <- RunModel(InputsModel, RunOptions, Param)
-
-# Loop over periods months periods
+# Simulation periods up to 31/12/1986
 dfTS <- data.frame(
   DatesR = DatesR,
   yearmonth = format(DatesR, "%Y-%m")
 )
-dfTS <- dfTS[1:(which(dfTS$yearmonth == "1987-01")[1]), ]
+dfTS <- dfTS[1:(which(dfTS$yearmonth == "1987-01")[1] - 1), ]
+
+# Run simulation in "normal" mode
+Param <- c(ParamMichel[names(ParamMichel) %in% griwrm$id], list(Dam = c(100E6, 1)))
+ROref <- CreateRunOptions(InputsModel, IndPeriod_WarmUp = 1:364, IndPeriod_Run = 365:nrow(dfTS))
+OMref <- RunModel(InputsModel, ROref, Param)
+
+# Set up initial conditions
+ROO <- CreateRunOptions(InputsModel, IndPeriod_WarmUp = 1:364, IndPeriod_Run = 365L)
+OM <- RunModel(InputsModel, ROO, Param)
 
 test_that("RunModel.GRiwrmOutputsModel works with InputsModel", {
 
@@ -56,10 +60,10 @@ test_that("RunModel.GRiwrmOutputsModel works with InputsModel", {
     ym_Qrelease <- Qrelease[ym_IndPeriod_Run, , drop = FALSE]
 
     # 50% Restriction on reservoir withdrawals if remaining less than 90 days of water
-    nb_remain_days <- OM$Dam$StateEnd$Reservoir$V / (-ym_Qinf$`WD`[1] + ym_Qrelease$Dam[1])
-    if (nb_remain_days < 180) {
-      ym_Qinf$`WD` <- -(max(0, OM$Dam$StateEnd$Reservoir$V - sum(ym_Qrelease$Dam))) / 365
-    }
+    # nb_remain_days <- OM$Dam$StateEnd$Reservoir$V / (-ym_Qinf$`WD`[1] + ym_Qrelease$Dam[1])
+    # if (nb_remain_days < 180) {
+    #   ym_Qinf$`WD` <- -(max(0, OM$Dam$StateEnd$Reservoir$V - sum(ym_Qrelease$Dam))) / 365
+    # }
     OM <- RunModel(OM,
                    InputsModel = InputsModel,
                    RunOptions = RunOptions,
@@ -69,6 +73,7 @@ test_that("RunModel.GRiwrmOutputsModel works with InputsModel", {
 
   expect_equal(nrow(attr(OM, "Qm3s")), nrow(dfTS) - 364)
   expect_equal(length(OM[[1]]$DatesR), nrow(dfTS) - 364)
+  expect_equal(str(attr(OM, "Qm3s")), str(attr(OMref, "Qm3s")))
 })
 
 test_that("RunModel.GRiwrmOutputsModel works with Supervisor", {
-- 
GitLab