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


4
  ## Initialization of variables
5
  NParam <- 5
6
7


8
  ## Arguments check
9
  if (!inherits(InputsModel, "InputsModel")) {
10
    stop("'InputsModel' must be of class 'InputsModel'")
11
  }
12
13
  if (!inherits(InputsModel, "daily")) {
    stop("'InputsModel' must be of class 'daily'")
14
  }
15
16
  if (!inherits(InputsModel, "GR")) {
    stop("'InputsModel' must be of class 'GR'")
17
  }
18
19
  if (!inherits(RunOptions, "RunOptions")) {
    stop("'RunOptions' must be of class 'RunOptions'")
20
  }
21
22
  if (!inherits(RunOptions, "GR")) {
    stop("'RunOptions' must be of class 'GR'")
23
  }
24
25
26
27
  if (!is.vector(Param) | !is.numeric(Param)) {
    stop("'Param' must be a numeric vector")
  }
  if (sum(!is.na(Param)) != NParam) {
28
    stop(paste("'Param' must be a vector of length", NParam, "and contain no NA"))
29
  }
30
  Param <- as.double(Param)
31

32
33
34
35
36
37
38
39
40
41
42
43
44
  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
45
46
  }

47
  ## Input data preparation
48
  if (identical(RunOptions$IndPeriod_WarmUp, 0L)) {
49
50
    RunOptions$IndPeriod_WarmUp <- NULL
  }
51
  IndPeriod1   <- c(RunOptions$IndPeriod_WarmUp, RunOptions$IndPeriod_Run)
52
  LInputSeries <- as.integer(length(IndPeriod1))
53
  if ("all" %in% RunOptions$Outputs_Sim) {
54
    IndOutputs <- as.integer(1:length(RunOptions$FortranOutputs$GR))
55
  } else {
56
    IndOutputs <- which(RunOptions$FortranOutputs$GR %in% RunOptions$Outputs_Sim)
57
  }
58

59
  ## Output data preparation
60
61
62
  IndPeriod2     <- (length(RunOptions$IndPeriod_WarmUp)+1):LInputSeries
  ExportDatesR   <- "DatesR"   %in% RunOptions$Outputs_Sim
  ExportStateEnd <- "StateEnd" %in% RunOptions$Outputs_Sim
63

64
  ## Use of IniResLevels
65
  if (!is.null(RunOptions$IniResLevels)) {
66
67
    RunOptions$IniStates[1] <- RunOptions$IniResLevels[1] * Param[1] ### production store level (mm)
    RunOptions$IniStates[2] <- RunOptions$IniResLevels[2] * Param[3] ### routing store level (mm)
68
  }
69

70
  ## Call GR model Fortan
71
  RESULTS <- .Fortran("frun_gr5j", PACKAGE = "airGR",
72
                      ## inputs
73
74
75
76
77
78
79
80
81
                      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
82
                      ## outputs
83
84
                      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
85
  )
86
87
  RESULTS$Outputs[RESULTS$Outputs   <= -99e8] <- NA
  RESULTS$StateEnd[RESULTS$StateEnd <= -99e8] <- NA
88
89
  if (ExportStateEnd) {
    RESULTS$StateEnd[-3L] <- ifelse(RESULTS$StateEnd[-3L] < 0, 0, RESULTS$StateEnd[-3L]) ### remove negative values except for the ExpStore location
90
91
    RESULTS$StateEnd <- CreateIniStates(FUN_MOD = RunModel_GR5J, InputsModel = InputsModel,
                                        ProdStore = RESULTS$StateEnd[1L], RoutStore = RESULTS$StateEnd[2L], ExpStore = NULL,
92
                                        UH1 = NULL,
93
94
                                        UH2 = RESULTS$StateEnd[(1:40) + (7+20)],
                                        GCemaNeigeLayers = NULL, eTGCemaNeigeLayers = NULL,
95
96
                                        verbose = FALSE)
  }
97
98
99
100
101
102

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