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

3
  NParam <- 1
4

5
  ##Arguments_check
6
7
  if (!inherits(InputsModel, "InputsModel")) {
    stop("'InputsModel' must be of class 'InputsModel'")
8
  }
9
10
  if (!inherits(InputsModel, "SD")) {
    stop("'InputsModel' must be of class 'SD'")
11
  }
12
13
14
15
16
17
18
19
20
21
  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
51
52
53
54
55
56
57
58
59
60
61
62
  
  IniSD <- RunOptions$IniStates[grep("SD", names(RunOptions$IniStates))]
  if(length(IniSD) > 0) {
    IniStates <- lapply(seq_len(NbUpBasins), function(x) {
      iStart <- 1
      if(x > 1) {
        iStart <- iStart + sum(floor(PT[1:x-1]) + 1)
      }
      IniSD[iStart:(iStart + PT[x])]
    })
  } else {
    IniStates <- lapply(seq_len(NbUpBasins), function(x) {
      rep(0, floor(PT[x] + 1))})
  }
  
63
64
  for (upstream_basin in seq_len(NbUpBasins)) {
    Qupstream <- InputsModel$Qupstream[RunOptions$IndPeriod_Run, upstream_basin]
65
    if (!is.na(InputsModel$BasinAreas[upstream_basin])) {
66
      # Upstream flow with area needs to be converted to m3 by time step
67
      Qupstream <- Qupstream * InputsModel$BasinAreas[upstream_basin] * 1e3
68
69
    }
    OutputsModel$Qsim <- OutputsModel$Qsim +
70
      c(IniStates[[upstream_basin]][-length(IniStates[[upstream_basin]])],
71
72
        Qupstream[1:(LengthTs - floor(PT[upstream_basin]))]) *
      HUTRANS[1, upstream_basin] +
73
      c(IniStates[[upstream_basin]],
74
75
76
77
        Qupstream[1:(LengthTs - floor(PT[upstream_basin]) - 1)]) *
      HUTRANS[2, upstream_basin]
  }
  # Warning for negative flows
78
  if (any(OutputsModel$Qsim < 0)) {
79
80
81
82
    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
83
  OutputsModel$Qsim <- OutputsModel$Qsim / sum(InputsModel$BasinAreas, na.rm = TRUE) / 1e3
84

85
  if ("StateEnd" %in% RunOptions$Outputs_Sim) {
86
    OutputsModel$StateEnd$SD <- lapply(seq(NbUpBasins), function(x) {
87
88
      Qupstream[(LengthTs - floor(PT[x])):LengthTs]
    })
89
90
  }

91
92
  return(OutputsModel)
}