1  Managing multiple SWAP-WOFOST simulations

Regional studies, as well as uncertainty and sensitivity analyses, often require a large number of simulations. Manually modifying input files for each run can therefore be very time-consuming. To address this, the R package SWAPtools has been developed, enabling users to manage multiple SWAP-WOFOST simulations through Excel.

The process begins with creating a SWAP template, typically based on the input files of an existing SWAP-WOFOST simulation. Within this template, one or more SWAP variables relevant to the study are selected (step 1). Next, the corresponding values for these variables are defined in an associated Excel file (step 2). The final step is to run the simulations (step 3).

1.1 Selecting SWAP-variables

SWAPtools can handle most SWAP-WOFOST variables. Within SWAP, three types of variables are distinguished:

  • scalar variables;
  • vector variables;
  • switch variables.

A complete list of variables supported by SWAPtools is included in the package’s installation directory. If the installation procedure described in Appendix A is followed, this list can be found at C:\ProgramData\R\R-4.5.2\libraries\SWAPtools\io\swap_input.csv (see R_LIBS in Section A.3). The belonging templates can be found at C:\ProgramData\R\R-4.5.2\libraries\SWAPtools\templates.

1.1.1 Scalar variables

A scalar variable can be selected using double curly brackets, for example {{VARIABLE}}. In the example below, the SWAP variable PONDMX is selected.

Tip 1.1: Selecting scalar variable.
 PONDMX = {{PONDMX}}       ! In case of ponding, minimum thickness for runoff [0..1000 cm, R]

1.1.2 Vector variables

Variables defined as a function of an index - such as date (DATE), depth/layer (ISOILLAY) or development stage (DVS) - are represented as tables. These variables consist of a sequence of values. In the example below, the table used to specify soil hydraulic functions for each soil layer is selected, containing nine SWAP variables (ORES, OSAT, … BDENS):

Tip 1.2: Selecting vector variable.
* If SWSOPHY = 0, specify MvG parameters for each soil physical layer (maximum MAHO):
* ORES    = Residual water content [0..1 cm3/cm3, R]
* OSAT    = Saturated water content [0..1 cm3/cm3, R]
* ALFA    = Parameter alfa of main drying curve [0.0001..100 /cm, R]
* NPAR    = Parameter n [1.001..9 -, R]
* KSATFIT = Fitting parameter Ksat of hydraulic conductivity function [1.d-5..1d5 cm/d, R]
* LEXP    = Exponent in hydraulic conductivity function [-25..25 -, R]
* H_ENPR  = Air entry pressure head [-40.0..0.0 cm, R]
* KSATEXM = Measured hydraulic conductivity at saturated conditions [1.d-5..1d5 cm/d, R]
* BDENS   = Dry soil bulk density [100..1d4 mg/cm3, R]

{{#TABLE_SOILHYDRFUNC}}
  {{ORES}} {{OSAT}} {{ALFA}} {{NPAR}} {{KSATFIT}} {{LEXP}} {{H_ENPR}} {{KSATEXM}} {{BDENS}}
{{/TABLE_SOILHYDRFUNC}}

1.1.3 Switch variables

Variables that represent specific options - so-called switches - determine which input is required. Selecting a particular option may trigger additional input. For example, if a simulation is configured using option A, it is no longer necessary to provide input for option B. In the example below, the switch SWINCO is selected, which introduces additional input for options 2 and 3. If a simulation uses option 2 for SWINCO, the user must also specify GWLI, while information related to INIFIL is not required.

Tip 1.3: Selecting switch variable.
* Switch, type of initial soil moisture condition:
! 1 = pressure head as function of soil depth
! 2 = pressure head of each compartment is in hydrostatic equilibrium with initial groundwater level
! 3 = read final pressure heads from output file of previous Swap simulation
   SWINCO = {{SWINCO}}        

{{#SWITCH_SWINCO_OPTION_2}}
* If SWINCO = 2, specify initial groundwater level:
  GWLI   = {{GWLI}}       ! Initial groundwater level, [-10000..100 cm, R]
{{/SWITCH_SWINCO_OPTION_2}}
{{#SWITCH_SWINCO_OPTION_3}}
* If SWINCO = 3, specify output file with initial values for current run:
  INIFIL = {{INIFIL}}        ! name of output file *.END which contains initial values [A200]
{{/SWITCH_SWINCO_OPTION_3}}

1.2 Setting variables

Once all variables in the SWAP-template(s) have been selected, Excel can be used to define their values. There are a few restrictions to follow. The Excel file must include a worksheet names Runs containing a column run_id, which should be a sequence from 1 to \(n\). Each run_id represents a single SWAP simulation. Variable values can be linked directly to the run_id, as illustrated for PONDMX below.

Another way to associate variables with a specific run_id is by using a so-called key. This key may consist of a single column or a combination of columns, all of which must have names ending in *._id. In a separate worksheet, variable values can then be linked to the run_id via this single- or multi-column key.

Variables that depend on an index such as date (DATE), depth or soil layer (ISOILLAY), or development stage (DVS) can be defined using a key together with an additional index column. In the example below, the maximum ponding depth is varied for two soil types using three worksheets (Runs, Ponding, and Soil), resulting in a total of eight SWAP simulations.

In case of a so-called switch variables it is allowed to leave cells empty, see example below.

In Excel, the use of formulas is allowed. Comments can be added by labeling the top row with “#”.

1.3 Execute SWAP simulations

Once the SWAP templates and the corresponding Excel file are prepared, the simulations can be executed. This is done using a single R script, run_swap.R. The script requires a plain text file, a so-called control file (in this case control.inp):

Tip 1.4: Example of batch file to run SWAPtools with input file control.inp.
call set_Renv.cmd
%RSCRIPT% .\run_swap.R control.inp

The control file is used to define various options and to specify paths to input files and directories. The user is responsible for organizing the data in a logical structure (the control file supports relative paths such as ‘./’ and ‘../’). Paths and options are identified using specific keywords that must not be modified. Comments can be included in the control file by using ‘#’.

The R-script run_swap.R consist of three separate procedures: a pre-process, a main-process and a post-process, which can be executed independently (see example below). The pre-process creates a SQLite database based on the information in the Excel file; using SQLite enables parallel execution through multicore processing. The main process then runs all or a selected subset of SWAP simulations.

Tip 1.5: Example of input file control.inp.
# Controlfile: SWAP-WOFOST
# Author:      H.M. Mulder
# Model:       https://swap.wur.nl
# Company:     Wageningen Environmental Research
#________________________________________________
#

# PREPROCESS
#------------------------------------------------

PREPROCESS                          Yes                                  # Converting Excel to SQLite, options 'Yes' or 'No'

FILXLS                              .\tools\datamodel.xlsx               # Path to Excel file containing SWAP settings
FILSQL                              .\tools\datamodel.sqlite             # Path to SQLite file (to be created by the R)

# MAINPROCESS
#------------------------------------------------

MAINPROCESS                         Yes                                  # Executing SWAP simulations, options 'Yes' or 'No' 

DIRRUN                              .\temp                               # Path to directory where SWAP simulations are performed

RUNID                               ALL                                  # Selection of run_id; for example 'ALL', '1,2,10' or '1-5, 10'

PRGSWP                              ..\..\tools\SWAP\swap_4.3.0.exe      # Path to SWAP executable

FILSWP                              .\tools\template\swap.swp            # Path swp-file
FILDRA                              .\tools\template\swap.dra            # Path dra-file
DIRINI                              .\tools\template\initial             # Path to directory ini-file(s)
DIRMET                              .\tools\template\weather             # Path to directory met-file(s)
DIRCRP                              .\tools\template\crop                # Path to directory crp-file(s)

# POSTPROCESS
#------------------------------------------------

POSTPROCESS                         Yes

DIROUT                              .\output                             # Path to directory with model results
DIRFIG                              .\tools\figures                      # Path to directory with fig-template(s)

Optionally, a post-process can be used to analyse the model results. This is discussed in Chapter 2.