Workflow

Install R package

# Enable repository from kwb-r
options(repos = c(
  kwbr = 'https://kwb-r.r-universe.dev',
  CRAN = 'https://cloud.r-project.org'))

# Download and install kwb.swmm in R
install.packages('kwb.swmm')

Download SWMM Executable

library(kwb.swmm)

swmm_exe <- kwb.swmm::download_swmm_executable(tdir = getwd(),
                                               swmm_version = "5.2.0")

Define Paths


paths_list <- list(
  model_dir =  kwb.swmm::extdata_file("models/lid"),
  model_name = "zone-1_bioretention-cell_lidshare-1.00",
  model_inp = "<model_dir>/<model_name>.inp",
  model_out = "<model_dir>/<model_name>.out",
  model_rpt = "<model_dir>/<model_name>.rpt",
  swmm_exe = swmm_exe
)

paths <- kwb.utils::resolve(paths_list)

Read Input

inp <- swmmr::read_inp(paths$model_inp)

## print some inputs
inp$options

inp$lid_controls

inp$lid_usage

Run SWMM

kwb.swmm::run_swmm(model_dir = paths$model_dir,
                   model_inp = paths$model_inp,
                   exe = paths$swmm_exe)

Read Output


results <- kwb.swmm::get_results(path_out = paths$model_out)
results

Calculate Rainstats

results_system <- kwb.swmm::get_results(path_out = paths$model_out,
                                        vIndex = c(1, 4)) %>%
  dplyr::rename(
    total_rainfall_mmPerHour = .data$total_rainfall,
    total_runoff_litrePerSecond = .data$total_runoff
  ) %>%
  dplyr::mutate(total_runoff_mmPerHour = kwb.swmm::lps_to_mmPerHour(.data$total_runoff_litrePerSecond)) %>%
  dplyr::select(-.data$total_runoff_litrePerSecond)

results_system

results_vrr <-  results_system %>%
  dplyr::mutate(year = lubridate::year(.data$datetime)) %>%
  dplyr::group_by(.data$year) %>%
  dplyr::summarise(vrr = 1 - (
    sum(.data$total_runoff_mmPerHour) / sum(.data$total_rainfall_mmPerHour)
  ))
results_vrr

col_eventsep <- "total_rainfall_mmPerHour"

rainevent_stats_mean <-
  kwb.swmm::calculate_rainevent_stats(results_system,
                                      col_eventsep = col_eventsep,
                                      aggregation_function = "mean") %>%
  dplyr::mutate(
    rainfall_cbm = .data$dur * .data$mean_total_rainfall_mmPerHour / 3600 /
      1000,
    runoff_cbm = .data$dur * .data$mean_total_runoff_mmPerHour /
      3600 / 1000,
    vrr = 1 - runoff_cbm / rainfall_cbm
  ) %>%
  dplyr::arrange(dplyr::desc(.data$mean_total_rainfall_mmPerHour))

head(rainevent_stats_mean)

rainevent_stats_max <-
  kwb.swmm::calculate_rainevent_stats(results_system,
                                      col_eventsep = col_eventsep,
                                      aggregation_function = "max") %>%
  dplyr::arrange(dplyr::desc(.data$max_total_rainfall_mmPerHour))

head(rainevent_stats_max)