RunModel_Lag.R 6.15 KB
Newer Older
1
RunModel_Lag <- function(InputsModel, RunOptions, Param, QcontribDown) {
2
  NParam <- 1
3

4
  ## argument check
5
6
  if (!inherits(InputsModel, "InputsModel")) {
    stop("'InputsModel' must be of class 'InputsModel'")
7
  }
8
9
  if (!inherits(InputsModel, "SD")) {
    stop("'InputsModel' must be of class 'SD'")
10
  }
11
12
13
14
15
16
17
18
19
  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"))
  }
20
21
22
23
  if (inherits(QcontribDown, "OutputsModel")) {
    if (is.null(QcontribDown$Qsim)) {
      stop("'QcontribDown' should contain a key 'Qsim' containing the output of the runoff of the downstream subcatchment")
    }
24
25
26
27
28
29
30
31
    if (length(QcontribDown$Qsim) != length(RunOptions$IndPeriod_Run)) {
      stop("Time series Qsim in 'QcontribDown' should have the same lenght as 'RunOptions$IndPeriod_Run'")
    }
    if (!is.null(QcontribDown$WarmUpQsim) &&
        length(QcontribDown$WarmUpQsim) != length(RunOptions$IndPeriod_WarmUp) &&
        RunOptions$IndPeriod_WarmUp != 0L) {
      stop("Time series WarmUpQsim in 'QcontribDown' should have the same lenght as 'RunOptions$IndPeriod_WarmUp'")
    }
32
  } else if (is.vector(QcontribDown) && is.numeric(QcontribDown)) {
33
34
35
    if (length(QcontribDown) != length(RunOptions$IndPeriod_Run)) {
      stop("'QcontribDown' should have the same lenght as 'RunOptions$IndPeriod_Run'")
    }
36
37
  } else {
    stop("'QcontribDown' must be a numeric vector or a 'OutputsModel' object")
38
  }
39
40
41
42
43

  # data set up
  NbUpBasins <- length(InputsModel$LengthHydro)
  if (identical(RunOptions$IndPeriod_WarmUp, 0L)) {
    RunOptions$IndPeriod_WarmUp <- NULL
44
  }
45
46
47
  IndPeriod1   <- c(RunOptions$IndPeriod_WarmUp, RunOptions$IndPeriod_Run)
  LInputSeries <- as.integer(length(IndPeriod1))
  IndPeriod2 <- (length(RunOptions$IndPeriod_WarmUp)+1):LInputSeries
48

49
50
51
52
53
54
55
56
57
58
59
  if (inherits(QcontribDown, "OutputsModel")) {
    OutputsModel <- QcontribDown
    if (is.null(OutputsModel$WarmUpQsim)) {
      OutputsModel$WarmUpQsim <- rep(NA, length(RunOptions$IndPeriod_WarmUp))
    }
    QsimDown <- c(OutputsModel$WarmUpQsim, OutputsModel$Qsim)
  } else if (is.vector(QcontribDown) && is.numeric(QcontribDown)) {
    OutputsModel <- list()
    class(OutputsModel) <- c("OutputsModel", class(RunOptions)[-1])
    QsimDown <- c(rep(NA, length(RunOptions$IndPeriod_WarmUp)),
                  QcontribDown)
60
61
  }

62
63
  ## propagation time from upstream meshes to outlet
  PT <- InputsModel$LengthHydro * 1e3 / Param[1L] / RunOptions$FeatFUN_MOD$TimeStep
64
65
  HUTRANS <- rbind(1 - (PT - floor(PT)), PT - floor(PT))

66
  ## set up initial states
67
  IniSD <- RunOptions$IniStates[grep("SD", names(RunOptions$IniStates))]
68
69
70
71
72
73
74
75
76
77
  if (length(IniSD) > 0) {
    if (sum(floor(PT)) + NbUpBasins != length(IniSD)) {
      stop(
        sprintf(
          "SD initial states has a length of %i and a length of %i is required",
          length(IniSD),
          sum(floor(PT)) + NbUpBasins
        )
      )
    }
78
79
    IniStates <- lapply(seq_len(NbUpBasins), function(x) {
      iStart <- 1
80
81
      if (x > 1) {
        iStart <- iStart + sum(floor(PT[1:x - 1]) + 1)
82
      }
83
      as.vector(IniSD[iStart:(iStart + PT[x])])
84
85
    })
  } else {
86
87
88
89
    IniStates <- lapply(
      seq_len(NbUpBasins),
      function(iUpBasins) {
        iWarmUp <- seq.int(
90
91
          from = max(1, IndPeriod1[1] - floor(PT[iUpBasins]) - 1),
          to   = max(1, IndPeriod1[1] - 1)
92
93
        )
        ini <- InputsModel$Qupstream[iWarmUp, iUpBasins]
94
        if (length(ini) != floor(PT[iUpBasins] + 1)) {
95
96
97
          # If warm-up period is not enough long complete beginning with first value
          ini <- c(rep(ini[1], floor(PT[iUpBasins] + 1) - length(ini)), ini)
        }
98
        return(as.vector(ini))
99
100
      }
    )
101
  }
102
103
104
105
106
  # message("IniStates: ", paste(IniStates, collapse = ", "))

  ## Lag model computation
  Qsim_m3 <- QsimDown *
    InputsModel$BasinAreas[length(InputsModel$BasinAreas)] * 1e3
107

108
  for (upstream_basin in seq_len(NbUpBasins)) {
109
    Qupstream <- c(IniStates[[upstream_basin]],
110
                   InputsModel$Qupstream[IndPeriod1, upstream_basin])
111
    # message("Qupstream[", upstream_basin, "]: ", paste(Qupstream, collapse = ", "))
112
113
114
115
116
117
118
    Qsim_m3 <- Qsim_m3 +
      Qupstream[2:(1 + LInputSeries)] * HUTRANS[1, upstream_basin] +
      Qupstream[1:LInputSeries] * HUTRANS[2, upstream_basin]
  }

  ## OutputsModel

119
120
121
  if ("Qsim_m3" %in% RunOptions$Outputs_Sim) {
    OutputsModel$Qsim_m3 <- Qsim_m3[IndPeriod2]
  }
122
123
124

  if ("Qsim" %in% RunOptions$Outputs_Sim) {
    # Convert back Qsim to mm
125
    OutputsModel$Qsim <- Qsim_m3[IndPeriod2] / sum(InputsModel$BasinAreas, na.rm = TRUE) / 1e3
126
    # message("Qsim: ", paste(OutputsModel$Qsim, collapse = ", "))
127
  }
128

129
130
131
132
133
  if ("QsimDown" %in% RunOptions$Outputs_Sim) {
    # Convert back Qsim to mm
    OutputsModel$QsimDown <- QsimDown[IndPeriod2]
  }

134
  # Warning for negative flows or NAs only in extended outputs
135
  if (length(RunOptions$Outputs_Sim) > 2) {
136
137
138
139
140
141
142
143
    if (any(OutputsModel$Qsim[!is.na(OutputsModel$Qsim)] < 0)) {
      warning(length(which(OutputsModel$Qsim < 0)), " time steps with negative flow, set to zero.")
      OutputsModel$Qsim[OutputsModel$Qsim < 0] <- 0
    }
    # Warning for NAs
    if (any(is.na(OutputsModel$Qsim))) {
      warning(length(which(is.na(OutputsModel$Qsim))), " time steps with NA values")
    }
144
  }
145

146
  if ("StateEnd" %in% RunOptions$Outputs_Sim) {
147
    SD <- lapply(seq(NbUpBasins), function(x) {
148
149
      lastTS <- RunOptions$IndPeriod_Run[length(RunOptions$IndPeriod_Run)]
      InputsModel$Qupstream[(lastTS - floor(PT[x])):lastTS, x]
150
    })
151
152
153
154
155
    if(is.null(OutputsModel$StateEnd)) {
      OutputsModel$StateEnd <- CreateIniStates(RunModel_Lag, InputsModel, SD = SD)
    } else {
      OutputsModel$StateEnd$SD <- SD
    }
156
    # message("StateEnd: ", paste(OutputsModel$StateEnd$SD, collapse = ", "))
157
  }
158
  if ("WarmUpQsim" %in% RunOptions$Outputs_Sim) {
159
    OutputsModel$WarmUpQsim <- Qsim_m3[seq_len(length(RunOptions$IndPeriod_WarmUp))] / sum(InputsModel$BasinAreas, na.rm = TRUE) / 1e3
160
  }
161

162
163
164
165
  if ("Param" %in% RunOptions$Outputs_Sim) {
    OutputsModel$Param <- c(Param, OutputsModel$Param)
  }

166
  return(OutputsModel)
167
}