RunModel_GR6J.R 6.52 KB
Newer Older
1
RunModel_GR6J <- function(InputsModel, RunOptions, Param) {
2
  
3
4
  
  ## Initialization of variables
5
  NParam <- 6
6
7
  FortranOutputs <- .FortranOutputs(GR = "GR6J")$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
48
49
50
51
  
  Param_X1X3X6_threshold <- 1e-2
  Param_X4_threshold     <- 0.5
  if (Param[1L] < Param_X1X3X6_threshold) {
    warning(sprintf("Param[1] (X1: production store capacity [mm]) < %.2f\n X1 set to %.2f", Param_X1X3X6_threshold, Param_X1X3X6_threshold))
    Param[1L] <- Param_X1X3X6_threshold
  }
  if (Param[3L] < Param_X1X3X6_threshold) {
    warning(sprintf("Param[3] (X3: routing store capacity [mm]) < %.2f\n X3 set to %.2f", Param_X1X3X6_threshold, Param_X1X3X6_threshold))
    Param[3L] <- Param_X1X3X6_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
  }
  if (Param[6L] < Param_X1X3X6_threshold) {
    warning(sprintf("Param[6] (X6: coefficient for emptying exponential store [mm]) < %.2f\n X6 set to %.2f", Param_X1X3X6_threshold, Param_X1X3X6_threshold))
    Param[6L] <- Param_X1X3X6_threshold
  }      
  
52
  ## Input data preparation
53
  if (identical(RunOptions$IndPeriod_WarmUp, 0L)) {
54
55
    RunOptions$IndPeriod_WarmUp <- NULL
  }
56
  IndPeriod1   <- c(RunOptions$IndPeriod_WarmUp, RunOptions$IndPeriod_Run)
57
  LInputSeries <- as.integer(length(IndPeriod1))
58
59
60
61
62
  if ("all" %in% RunOptions$Outputs_Sim) {
    IndOutputs <- as.integer(1:length(FortranOutputs)) 
  } else {
    IndOutputs <- which(FortranOutputs %in% RunOptions$Outputs_Sim)
  }
63
  
64
  ## Output data preparation
65
66
67
  IndPeriod2     <- (length(RunOptions$IndPeriod_WarmUp)+1):LInputSeries
  ExportDatesR   <- "DatesR"   %in% RunOptions$Outputs_Sim
  ExportStateEnd <- "StateEnd" %in% RunOptions$Outputs_Sim
68
  
69
  ## Use of IniResLevels
70
  if (!is.null(RunOptions$IniResLevels)) {
71
72
    RunOptions$IniStates[1] <- RunOptions$IniResLevels[1] * Param[1] ### production store level (mm)
    RunOptions$IniStates[2] <- RunOptions$IniResLevels[2] * Param[3] ### routing store level (mm)
73
    RunOptions$IniStates[3] <- RunOptions$IniResLevels[3]          ### exponential store level (mm)
74
75
  }
  
76
  ## Call GR model Fortan
77
  RESULTS <- .Fortran("frun_gr6j", PACKAGE = "airGR", 
78
                      ## inputs
79
80
81
82
83
84
85
86
87
                      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
88
                      ## outputs
89
90
                      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
91
  )
92
93
  RESULTS$Outputs[RESULTS$Outputs   <= -99e8] <- NA
  RESULTS$StateEnd[RESULTS$StateEnd <= -99e8] <- NA
94
95
  if (ExportStateEnd) {
    RESULTS$StateEnd[-3L] <- ifelse(RESULTS$StateEnd[-3L] < 0, 0, RESULTS$StateEnd[-3L]) ### remove negative values except for the ExpStore location
96
97
    RESULTS$StateEnd <- CreateIniStates(FUN_MOD = RunModel_GR6J, InputsModel = InputsModel, 
                                        ProdStore = RESULTS$StateEnd[1L], RoutStore = RESULTS$StateEnd[2L], ExpStore = RESULTS$StateEnd[3L], 
98
                                        UH1 = RESULTS$StateEnd[(1:20) + 7], UH2 = RESULTS$StateEnd[(1:40) + (7+20)], 
99
                                        GCemaNeigeLayers = NULL, eTGCemaNeigeLayers = NULL, 
100
101
102
                                        verbose = FALSE)
  }
  
103
104
  ## Output data preparation
  ## OutputsModel only
105
  if (!ExportDatesR & !ExportStateEnd) {
106
    OutputsModel <- lapply(seq_len(RESULTS$NOutputs), function(i) RESULTS$Outputs[IndPeriod2, i])
107
108
    names(OutputsModel) <- FortranOutputs[IndOutputs]
  }
109
  ## DatesR and OutputsModel only
110
  if (ExportDatesR & !ExportStateEnd) {
111
112
    OutputsModel <- c(list(InputsModel$DatesR[RunOptions$IndPeriod_Run]), 
                      lapply(seq_len(RESULTS$NOutputs), function(i) RESULTS$Outputs[IndPeriod2, i]))
113
114
    names(OutputsModel) <- c("DatesR", FortranOutputs[IndOutputs])
  }
115
  ## OutputsModel and SateEnd only
116
  if (!ExportDatesR & ExportStateEnd) {
117
118
    OutputsModel <- c(lapply(seq_len(RESULTS$NOutputs), function(i) RESULTS$Outputs[IndPeriod2, i]), 
                      list(RESULTS$StateEnd))
119
120
    names(OutputsModel) <- c(FortranOutputs[IndOutputs], "StateEnd")
  }
121
  ## DatesR and OutputsModel and SateEnd
122
  if ((ExportDatesR & ExportStateEnd) | "all" %in% RunOptions$Outputs_Sim) {
123
124
125
    OutputsModel <- c(list(InputsModel$DatesR[RunOptions$IndPeriod_Run]), 
                      lapply(seq_len(RESULTS$NOutputs), function(i) RESULTS$Outputs[IndPeriod2, i]), 
                      list(RESULTS$StateEnd))
126
127
    names(OutputsModel) <- c("DatesR", FortranOutputs[IndOutputs], "StateEnd")
  }
128
  
129
  ## End
130
131
132
  rm(RESULTS) 
  class(OutputsModel) <- c("OutputsModel", "daily", "GR")
  return(OutputsModel)
133
  
Delaigue Olivier's avatar
Delaigue Olivier committed
134
}