RunModel_GR5J.R 4.11 KB
Newer Older
1
RunModel_GR5J <- function(InputsModel, RunOptions, Param) {
2

3
  .ArgumentsCheckGR(InputsModel, RunOptions, Param)
4

5
  Param <- as.double(Param)
6

7
8
9
10
11
12
13
14
15
16
17
18
19
  Param_X1X3_threshold <- 1e-2
  Param_X4_threshold   <- 0.5
  if (Param[1L] < Param_X1X3_threshold) {
    warning(sprintf("Param[1] (X1: production store capacity [mm]) < %.2f\n X1 set to %.2f", Param_X1X3_threshold, Param_X1X3_threshold))
    Param[1L] <- Param_X1X3_threshold
  }
  if (Param[3L] < Param_X1X3_threshold) {
    warning(sprintf("Param[3] (X3: routing store capacity [mm]) < %.2f\n X3 set to %.2f", Param_X1X3_threshold, Param_X1X3_threshold))
    Param[3L] <- Param_X1X3_threshold
  }
  if (Param[4L] < Param_X4_threshold) {
    warning(sprintf("Param[4] (X4: unit hydrograph time constant [d]) < %.2f\n X4 set to %.2f", Param_X4_threshold, Param_X4_threshold))
    Param[4L] <- Param_X4_threshold
20
21
  }

22
  ## Input data preparation
23
  if (identical(RunOptions$IndPeriod_WarmUp, 0L)) {
24
25
    RunOptions$IndPeriod_WarmUp <- NULL
  }
26
  IndPeriod1   <- c(RunOptions$IndPeriod_WarmUp, RunOptions$IndPeriod_Run)
27
  LInputSeries <- as.integer(length(IndPeriod1))
28
  if ("all" %in% RunOptions$Outputs_Sim) {
29
    IndOutputs <- as.integer(1:length(RunOptions$FortranOutputs$GR))
30
  } else {
31
    IndOutputs <- which(RunOptions$FortranOutputs$GR %in% RunOptions$Outputs_Sim)
32
  }
33

34
  ## Output data preparation
35
36
37
  IndPeriod2     <- (length(RunOptions$IndPeriod_WarmUp)+1):LInputSeries
  ExportDatesR   <- "DatesR"   %in% RunOptions$Outputs_Sim
  ExportStateEnd <- "StateEnd" %in% RunOptions$Outputs_Sim
38

39
  ## Use of IniResLevels
40
  if (!is.null(RunOptions$IniResLevels)) {
41
42
    RunOptions$IniStates[1] <- RunOptions$IniResLevels[1] * Param[1] ### production store level (mm)
    RunOptions$IniStates[2] <- RunOptions$IniResLevels[2] * Param[3] ### routing store level (mm)
43
  }
44

45
  ## Call GR model Fortan
46
  RESULTS <- .Fortran("frun_gr5j", PACKAGE = "airGR",
47
                      ## inputs
48
49
50
51
52
53
54
55
56
                      LInputs = LInputSeries,                             ### length of input and output series
                      InputsPrecip = InputsModel$Precip[IndPeriod1],      ### input series of total precipitation [mm/d]
                      InputsPE = InputsModel$PotEvap[IndPeriod1],         ### input series potential evapotranspiration [mm/d]
                      NParam = as.integer(length(Param)),                 ### number of model parameter
                      Param = Param,                                      ### parameter set
                      NStates = as.integer(length(RunOptions$IniStates)), ### number of state variables used for model initialising
                      StateStart = RunOptions$IniStates,                  ### state variables used when the model run starts
                      NOutputs = as.integer(length(IndOutputs)),          ### number of output series
                      IndOutputs = IndOutputs,                            ### indices of output series
57
                      ## outputs
58
59
                      Outputs = matrix(as.double(-99e9), nrow = LInputSeries, ncol = length(IndOutputs)), ### output series [mm or mm/d]
                      StateEnd = rep(as.double(-99e9), length(RunOptions$IniStates))                      ### state variables at the end of the model run
60
  )
61
62
  RESULTS$Outputs[RESULTS$Outputs   <= -99e8] <- NA
  RESULTS$StateEnd[RESULTS$StateEnd <= -99e8] <- NA
63
64
  if (ExportStateEnd) {
    RESULTS$StateEnd[-3L] <- ifelse(RESULTS$StateEnd[-3L] < 0, 0, RESULTS$StateEnd[-3L]) ### remove negative values except for the ExpStore location
65
66
    RESULTS$StateEnd <- CreateIniStates(FUN_MOD = RunModel_GR5J, InputsModel = InputsModel,
                                        ProdStore = RESULTS$StateEnd[1L], RoutStore = RESULTS$StateEnd[2L], ExpStore = NULL,
67
                                        UH1 = NULL,
68
69
                                        UH2 = RESULTS$StateEnd[(1:40) + (7+20)],
                                        GCemaNeigeLayers = NULL, eTGCemaNeigeLayers = NULL,
70
71
                                        verbose = FALSE)
  }
72
73
74
75
76
77

  ## OutputsModel generation
  .GetOutputsModel(InputsModel,
                   RunOptions,
                   RESULTS,
                   LInputSeries)
Delaigue Olivier's avatar
Delaigue Olivier committed
78
}