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

3
4
5
  NParam <- 1
  
  ##Arguments_check
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
  if (!inherits(InputsModel, "InputsModel")) {
    stop("'InputsModel' must be of class 'InputsModel'")
  }  
  if (!inherits(InputsModel, "SD")) {
    stop("'InputsModel' must be of class 'SD'")
  }  
  if (!inherits(RunOptions, "RunOptions")) {
    stop("'RunOptions' must be of class 'RunOptions'")
  }
  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"))
  }
  if (is.null(InputsModel$OutputsModel)) {
22
23
    stop("'InputsModel' should contain an 'OutputsModel' key containing the output of the runoff of the downstream subcatchment")
  }
24
  if (is.null(InputsModel$OutputsModel$Qsim)) {
25
26
    stop("'InputsModel$OutputsModel' should contain a key 'Qsim' containing the output of the runoff of the downstream subcatchment")
  }
27
  if (sum(!is.na(InputsModel$OutputsModel$Qsim)) != length(RunOptions$IndPeriod_Run)) {
28
29
30
    stop("'InputsModel$OutputsModel$Qim' should have the same lenght as 'RunOptions$IndPeriod_Run' and contain no NA")
  }
  
31
32
  OutputsModel <- InputsModel$OutputsModel
  OutputsModel$QsimDown <- OutputsModel$Qsim
33
  
34
35
36
37
38
39
40
41
  if (inherits(InputsModel, "daily")) {
    TimeStep <- 60 * 60 * 24
  }
  if (inherits(InputsModel, "hourly")) {
    TimeStep <- 60 * 60
  }

  # propagation time from upstream meshes to outlet
42
  PT <- InputsModel$LengthHydro / Param[1L] / TimeStep
43
44
45
46
  HUTRANS <- rbind(1 - (PT - floor(PT)), PT - floor(PT))

  NbUpBasins <- length(InputsModel$LengthHydro)
  LengthTs <- length(OutputsModel$QsimDown)
47
  OutputsModel$Qsim <- OutputsModel$QsimDown * InputsModel$BasinAreas[length(InputsModel$BasinAreas)] * 1e3
48
49
50

  for (upstream_basin in seq_len(NbUpBasins)) {
    Qupstream <- InputsModel$Qupstream[RunOptions$IndPeriod_Run, upstream_basin]
51
    if (!is.na(InputsModel$BasinAreas[upstream_basin])) {
52
      # Upstream flow with area needs to be converted to m3 by time step
53
      Qupstream <- Qupstream * InputsModel$BasinAreas[upstream_basin] * 1e3
54
55
56
57
58
59
60
61
62
63
    }
    OutputsModel$Qsim <- OutputsModel$Qsim +
      c(rep(0, floor(PT[upstream_basin])),
        Qupstream[1:(LengthTs - floor(PT[upstream_basin]))]) *
      HUTRANS[1, upstream_basin] +
      c(rep(0, floor(PT[upstream_basin] + 1)),
        Qupstream[1:(LengthTs - floor(PT[upstream_basin]) - 1)]) *
      HUTRANS[2, upstream_basin]
  }
  # Warning for negative flows
64
  if (any(OutputsModel$Qsim < 0)) {
65
66
67
68
    warning(length(which(OutputsModel$Qsim < 0)), " time steps with negative flow, set to zero.")
    OutputsModel$Qsim[OutputsModel$Qsim < 0] <- 0
  }
  # Convert back Qsim to mm
69
  OutputsModel$Qsim <- OutputsModel$Qsim / sum(InputsModel$BasinAreas, na.rm = TRUE) / 1e3
70
71
  return(OutputsModel)
}