- Demo_Main.R
- Working directory
- Source functions
- Read Runfile
- Model
- Model parameters
- Model scheme
- Boundary conditions
- Light and temperature
- Simulation time
- Initial conditions
- Run the model
- Postprocessing
Demo_Main.R
This R-script is located in the root of the DEMO model and contains the code to run the model. The following steps are made:
Working directory
The working directory to to run the model is set by the R function: "setwd()".
Source functions
All the functions included in the subdirectory "./Rfunctions/" are sourced in order to become available to the model.
Read Runfile
The runfile is selected and data is read from the runfile. The Runfile is a text file that contains information for the specific model run.
Model
The model is defined in the runfile. Three different models can be run. The NPZ model is the water quality model that simulates the nutrient dynamics in the Oosterschelde. The tracer model is a model with one tracer that is transported by water exchange between the compartments. The tracer can be inert or can have an exponential decay rate. Finally a salinity model can be run.
Model parameters
The parameters are read from a the text file indicated in the Runfile. Parameter files are located in the sub-directory "./Parameters/". Parameter names and values are stored in the dataframe "parms".
Model scheme
The required modelscheme (0D, 4Comp or 28Comp) is selected and information on model compartment and transport between the compartments is stored in the dataframe "Comp" and the function "Transp", respectively.
Boundary conditions
The boundary conditions at the North sea, Krammersluizen and Bergse Diepsluis are set as approxfun() functions. The boundary conditions include the concentrations of Chla, DIN and Det at the boundaries. The functions are derived from the measurements (see data on nutrients) or from and "average" seasonal function fitted through the data.
Light and temperature
Irradiation and water temperature are included as approxfun() functions based on measurements of irradiation and water temperature, respectively.
Simulation time
Simulation time is in days from the reference day (1 jan 1970). Starting and ending date are set in the runfile (StartDate and EndDate, respectively). Model output is generated at equal intervals between start and end. The number of intervals is provided by the user by "Intervals" in the runfile. Note that this is not the model timestep, but only the moments of model output.
Initial conditions
The initial conditions for all state variables at the start of the model simulation is set. When Init_manual = TRUE, the user can enter the initial conditions in a text file (Init_file). Initial conditions in all compartments are the same then. Alternatively (Init_manual = FALSE), the results from a previous modelrun (Init_model) can be used as initial conditions for the model simulation. It is advised that the end time of the previous model run that is in the same month/season than the start time of the actual model simulation.
Run the model
The model is run with the function ode(). For long simulations, it is advised to set Prog_bar = TRUE in the runfile. Then the progress of the model is shown on the screen. Input for the model are initial conditions (State_initial), simulation time (outtimes), The model (NPZ, Tracer or Salinity) is given and the model parameters. A number of solvers can be selected (Method) in ode(). Fot the model it is advised to use a solver with a variable timestep. The integrators Method = "adams" and Method = rkMethod("rk45ck") are "relatively" fast and give comparable results.
Postprocessing
At the end of the simulation postprocessing is needed to save the results. The results are then stored in the directory "./ModelOutput/" in five Rdata files (run_*,rds, var_*,rds, out_*,rds, agg_*,rds and init_*,rds). The files can be used by the visualization app.