When you are calling hbv_pso
, chances are you will not do so from the commandline but at the end of an R script in which you first set up all the different options and your input time series. If you need to do multiple optimization runs, for example when comparing different objective functions or precipitation products, this setup quickly becomes unwieldy.
The hbv_pso_run
function provides a simple framework for managing (and running) many different hbv_pso
configurations with a minimal amount of boiler plate, while still allowing for flexibility where required. The core ideas are:
source
to import base configurations and selectively overwrite parametersDownload the example data and extract it to your local system. In the remainder of this document it will be assumed that you assigned the path to the extracted data folder to a variable with the name hpi
(for hbv_pso_imperial). Also make sure you have the hbvPSO
package loaded:
The only required argument for hbv_pso_run
is configpath
. In the simplest case, this represents the path to a configuration file which, when sourced, provides the arguments for hbv_pso
as variables. It will then execute hbv_pso
with the provided arguments and return the results. As an example, run the following file for the example data from TUWmodel
(this will take a couple of minutes):
While the optimization is running, have a look at the file tuwmodel_example/run_default.R
. The first line sources a seperate configuration file located in the parent directory:
By using source
to include other files you can define a set of “default” options which are used in the majority of runs in a single place, which minimizes repeating yourself and allows you to easily change an argument for all runs.
WARNING: When sourcing other configuration files, you must always use local=TRUE
! Internally, all parameters are sourced into a seperate environment, so with the default options to source
they would end up in the global environment and not be utilized. The chdir
argument is useful because it allows you to use relative paths inside the configuration files.
Open the file global_config.R
to see what default arguments have been set:
library(parallel)
library(hydroGOF)
# a model run is identified by "foldername-gof.name-suffix"
# suffix is optional, but can be used to add additional information
# according to specific settings e.g. imperial_chirps-lnNSE-singlezone
suffix = "nozones"
# gof.name <- "NSE"
# start of calibration
# from <- "1998-01-01"
from <- "2000-01-01"
# end of calibration
to <- "2010-12-31"
# end of warmup (as date, date string, or number of days)
warmup <- "2001-01-01"
# warmup <- "1999-12-31"
# start of validation (optional, set to NULL to disable)
from_validation <- "2011-01-01"
# end of validation (optional, set to NULL to disable)
to_validation <- "2015-12-31"
# forces the model to run with a single zone even if elevation zones
# are defined in hbv-light
area <- 1
elev_zones <- c(0)
FUN_gof <- hydroGOF::NSE
gof.name <- "lnNSE"
# to use tcalt, set telev and add the tcalt range to the parameters, e.g.:
# param["tcalt",] <- c(0,1.2)
telev = NULL
# to use tcalt, set pelev and add the pcalt range to the parameters, e.g.:
# param["pcalt",] <- c(0,0.4)
pelev = NULL
plotting = TRUE
control = list(
digits=2,
normalise = TRUE,
npart = 50,
maxit = 200,
reltol = 1E-30,
Xini.type = "lhs",
Vini.type = "lhs2011",
c1 = 2.05,
c2 = 2.05,
use.IW = FALSE,
use.CF = TRUE,
topology = "l",
K = 3,
boundary.wall = "absorbing2011",
verbose = TRUE,
par.nnodes=detectCores(),
parallel = "parallel"
# parallel = "none"
)
hydroPSO_args = list(control=control)
# control = list(
# normalise = TRUE,
# do.plots=TRUE,
# verbose = TRUE,
# par.nnodes=detectCores(),
# # parallel = "parallel"
# parallel = "none"
# )
–> –>