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

4
  ##Arguments_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
20
  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)) {
21
    stop("'InputsModel' should contain an 'OutputsModel' key containing the output of the runoff of the downstream subcatchment")
22
  }
23
  if (is.null(InputsModel$OutputsModel$Qsim)) {
24
    stop("'InputsModel$OutputsModel' should contain a key 'Qsim' containing the output of the runoff of the downstream subcatchment")
25
  }
26
  if (sum(!is.na(InputsModel$OutputsModel$Qsim)) != length(RunOptions$IndPeriod_Run)) {
27
    stop("'InputsModel$OutputsModel$Qim' should have the same lenght as 'RunOptions$IndPeriod_Run' and contain no NA")
28
  }
29

30
31
  OutputsModel <- InputsModel$OutputsModel
  OutputsModel$QsimDown <- OutputsModel$Qsim
32

33
34
  if (inherits(InputsModel, "hourly")) {
    TimeStep <- 60 * 60
35
36
37
38
  } else if (inherits(InputsModel, "daily")) {
    TimeStep <- 60 * 60 * 24
  } else {
    stop("'InputsModel' should be of class \"daily\" or \"hourly\"")
39
40
41
  }

  # propagation time from upstream meshes to outlet
42
  PT <- InputsModel$LengthHydro * 1e3 / 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
  IniSD <- RunOptions$IniStates[grep("SD", names(RunOptions$IniStates))]
50
51
52
53
54
55
56
57
58
59
  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
        )
      )
    }
60
61
    IniStates <- lapply(seq_len(NbUpBasins), function(x) {
      iStart <- 1
62
63
      if (x > 1) {
        iStart <- iStart + sum(floor(PT[1:x - 1]) + 1)
64
65
66
67
68
      }
      IniSD[iStart:(iStart + PT[x])]
    })
  } else {
    IniStates <- lapply(seq_len(NbUpBasins), function(x) {
69
70
      rep(0, floor(PT[x] + 1))
    })
71
  }
72
  # message("Initstates: ", paste(IniStates, collapse = ", "))
73

74
  for (upstream_basin in seq_len(NbUpBasins)) {
75
76
    Qupstream <- c(IniStates[[upstream_basin]],
                   InputsModel$Qupstream[RunOptions$IndPeriod_Run, upstream_basin])
77
    if (!is.na(InputsModel$BasinAreas[upstream_basin])) {
78
      # Upstream flow with area needs to be converted to m3 by time step
79
      Qupstream <- Qupstream * InputsModel$BasinAreas[upstream_basin] * 1e3
80
    }
81
82
    # message("Qupstream[", upstream_basin, "]: ", paste(Qupstream, collapse = ", "))
    OutputsModel$Qsim <- OutputsModel$Qsim +
83
84
      Qupstream[2:(1 + LengthTs)] * HUTRANS[1, upstream_basin] +
      Qupstream[1:LengthTs] * HUTRANS[2, upstream_basin]
85
  }
86
87
88
89
90
91
92
93
94
95
  # Warning for negative flows or NAs only in extended outputs
  if(length(RunOptions$Outputs_Sim <3)) {
    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")
    }
96
97
  }
  # Convert back Qsim to mm
98
  OutputsModel$Qsim <- OutputsModel$Qsim / sum(InputsModel$BasinAreas, na.rm = TRUE) / 1e3
99
  # message("Qsim: ", paste(OutputsModel$Qsim, collapse = ", "))
100

101
  if ("StateEnd" %in% RunOptions$Outputs_Sim) {
102
    OutputsModel$StateEnd$SD <- lapply(seq(NbUpBasins), function(x) {
103
104
      lastTS <- RunOptions$IndPeriod_Run[length(RunOptions$IndPeriod_Run)]
      InputsModel$Qupstream[(lastTS - floor(PT[x])):lastTS, x]
105
    })
106
    # message("StateEnd: ", paste(OutputsModel$StateEnd$SD, collapse = ", "))
107
108
  }

109
  return(OutputsModel)
110
}