RunModel_GR5J.R 6.13 KB
Newer Older
1
RunModel_GR5J <- function(InputsModel, RunOptions, Param) {
2
  
3
4
  
  ## Initialization of variables
5
  NParam <- 5
6
7
  FortranOutputs <- .FortranOutputs(GR = "GR5J")$GR
  
8
  
9
  ## Arguments check
10
  if (!inherits(InputsModel, "InputsModel")) {
11
12
    stop("'InputsModel' must be of class 'InputsModel'")
  }  
13
  if (!inherits(InputsModel, "daily"      )) {
14
15
    stop("'InputsModel' must be of class 'daily'      ")
  }  
16
  if (!inherits(InputsModel, "GR"         )) {
17
18
    stop("'InputsModel' must be of class 'GR'         ")
  }  
19
  if (!inherits(RunOptions, "RunOptions"  )) {
20
21
    stop("'RunOptions' must be of class 'RunOptions'  ")
  }  
22
  if (!inherits(RunOptions, "GR"          )) {
23
24
25
26
27
28
29
30
    stop("'RunOptions' must be of class 'GR'          ")
  }  
  if (!is.vector(Param) | !is.numeric(Param)) {
    stop("'Param' must be a numeric vector")
  }
  if (sum(!is.na(Param)) != NParam) {
    stop(paste("'Param' must be a vector of length ", NParam, " and contain no NA", sep = ""))
  }
31
  Param <- as.double(Param)
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
  
  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
  }      
  
48
  ## Input data preparation
49
  if (identical(RunOptions$IndPeriod_WarmUp, 0L)) {
50
51
    RunOptions$IndPeriod_WarmUp <- NULL
  }
52
  IndPeriod1   <- c(RunOptions$IndPeriod_WarmUp, RunOptions$IndPeriod_Run)
53
  LInputSeries <- as.integer(length(IndPeriod1))
54
55
56
57
58
  if ("all" %in% RunOptions$Outputs_Sim) {
    IndOutputs <- as.integer(1:length(FortranOutputs)) 
  } else {
    IndOutputs <- which(FortranOutputs %in% RunOptions$Outputs_Sim)
  }
59
  
60
  ## Output data preparation
61
62
63
  IndPeriod2     <- (length(RunOptions$IndPeriod_WarmUp)+1):LInputSeries
  ExportDatesR   <- "DatesR"   %in% RunOptions$Outputs_Sim
  ExportStateEnd <- "StateEnd" %in% RunOptions$Outputs_Sim
64
  
65
  ## Use of IniResLevels
66
  if (!is.null(RunOptions$IniResLevels)) {
67
68
    RunOptions$IniStates[1] <- RunOptions$IniResLevels[1] * Param[1] ### production store level (mm)
    RunOptions$IniStates[2] <- RunOptions$IniResLevels[2] * Param[3] ### routing store level (mm)
69
70
  }
  
71
  ## Call GR model Fortan
72
  RESULTS <- .Fortran("frun_gr5j", PACKAGE = "airGR", 
73
                      ## inputs
74
75
76
77
78
79
80
81
82
                      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
83
                      ## outputs
84
85
                      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
86
  )
87
88
  RESULTS$Outputs[RESULTS$Outputs   <= -99e8] <- NA
  RESULTS$StateEnd[RESULTS$StateEnd <= -99e8] <- NA
89
90
  if (ExportStateEnd) {
    RESULTS$StateEnd[-3L] <- ifelse(RESULTS$StateEnd[-3L] < 0, 0, RESULTS$StateEnd[-3L]) ### remove negative values except for the ExpStore location
91
92
    RESULTS$StateEnd <- CreateIniStates(FUN_MOD = RunModel_GR5J, InputsModel = InputsModel, 
                                        ProdStore = RESULTS$StateEnd[1L], RoutStore = RESULTS$StateEnd[2L], ExpStore = NULL, 
93
                                        UH1 = NULL, UH2 = RESULTS$StateEnd[(1:40) + (7+20)], 
94
                                        GCemaNeigeLayers = NULL, eTGCemaNeigeLayers = NULL, 
95
96
97
                                        verbose = FALSE)
  }
  
98
99
  ## Output data preparation
  ## OutputsModel only
100
  if (!ExportDatesR & !ExportStateEnd) {
101
    OutputsModel <- lapply(seq_len(RESULTS$NOutputs), function(i) RESULTS$Outputs[IndPeriod2, i])
102
103
    names(OutputsModel) <- FortranOutputs[IndOutputs]
  }
104
  ## DatesR and OutputsModel only
105
  if (ExportDatesR & !ExportStateEnd) {
106
107
    OutputsModel <- c(list(InputsModel$DatesR[RunOptions$IndPeriod_Run]), 
                      lapply(seq_len(RESULTS$NOutputs), function(i) RESULTS$Outputs[IndPeriod2, i]))
108
109
    names(OutputsModel) <- c("DatesR", FortranOutputs[IndOutputs])
  }
110
  ## OutputsModel and SateEnd only
111
  if (!ExportDatesR & ExportStateEnd) {
112
113
    OutputsModel <- c(lapply(seq_len(RESULTS$NOutputs), function(i) RESULTS$Outputs[IndPeriod2, i]), 
                      list(RESULTS$StateEnd))
114
115
    names(OutputsModel) <- c(FortranOutputs[IndOutputs], "StateEnd")
  }
116
  ## DatesR and OutputsModel and SateEnd
117
  if ((ExportDatesR & ExportStateEnd) | "all" %in% RunOptions$Outputs_Sim) {
118
119
120
    OutputsModel <- c(list(InputsModel$DatesR[RunOptions$IndPeriod_Run]), 
                      lapply(seq_len(RESULTS$NOutputs), function(i) RESULTS$Outputs[IndPeriod2, i]), 
                      list(RESULTS$StateEnd))
121
122
    names(OutputsModel) <- c("DatesR", FortranOutputs[IndOutputs], "StateEnd")
  }
123
  
124
  ## End
125
126
127
  rm(RESULTS) 
  class(OutputsModel) <- c("OutputsModel", "daily", "GR")
  return(OutputsModel)
128
  
Delaigue Olivier's avatar
Delaigue Olivier committed
129
}