% Copyright 2019-2020 LIST (Luxembourg Institute of Science and Technology), all right reserved.
%
% Author: Nicolas Rodriguez (nicolas.bjorn.rodriguez@gmail.com)
%
% This is the main script calling all the other scripts in order:
% init_virtual initializes the model: parameters, constants, and loads hydrological and tracer data
% model_run_virtual governs the model runs, calling the calculation script model_calc, and evaluating model performance
% The last part of the script is the Monte Carlo for parameter calibration
% Customizations should be mainly specified in this script
% Clear and close
clc;
clearvariables;
closeall;
% Start clock
t1=tic;
% Display options (text display about calculation status in the command window)
options.display=1;% 0 for no display, 1 for all display
options.plots.display=0;% 0 for no plots, 1 for all plots
% Solver options (numerical solution to water mass balance equation)
options.solver.type='EE';% Explicit Euler, 'IE' for implicit Euler
options.solver.checks=0.25;% How often to check calculations? (as a percentage of completion)
ifstrcmp(options.solver.type,'IE')% Implicit Euler, which is more or less useless in this context
% see file mysolver.m for the parameters below
options.solver.tol=eps;% approximation of zero for function evaluations
options.solver.prec=eps;% convergence criteria on the difference between function evaluations
options.solver.maxiter=1000;% max number of iterations for the solver
options.solver.display='on';% 'off' for no solver messages
options.solver.maxdisp=100;% max nb of msgs by solver
end
% Model parameter options
% SASQ_p1 to SASQ_pn: n parameters for the streamflow SAS function
% same for ET SAS function
% other parameters (do not need to be changed):
% m: smoothing parameter for AET calculations from PET (see supplement info and model_calc.m)
% S_ET: storage value below which AET drops from PET towards 0, using the smoothing parameter m
% n: smoothing parameter for storage calculated from WB (see model_calc.m)
% Psi: optional multiplicative constant for ET
% C_i: initial tracer concentration in storage (per mil)
% S_i: initial storage (mm)
% T0: mean age in years for the initial residence time distribution (exponential)
% Calibration parameters (if needed, more parameters can be taken from the fixed parameter list and put here, for instance, one can cut 'SASQ_p3' and paste here, and adjust options.param.fix.nb to 22 and options.param.cal.nb to 3
options.param.cal.nb=2;% number of calibration parameters (here only two are needed if the unimodal gamma SAS function is used, they will correspond to SASQ_p1 = k and SASQ_p2 = theta)
options.param.cal.name={'SASQ_p1';'SASQ_p2'};
% p calibration parameters are placed in a vector param(1,p)
% Fixed parameters (their values can be modified only in init_virtual.m)
options.param.fix.nb=23;% number of fixed parameters (not calibrated)
% all other parameters are placed in the 'options' stucture, and fixed during initialisation
% Age calculation options
options.ages.dT=24*2;% age resolution for the calculations, in hours
options.ages.lookup=1;% use a table lookup for the gamma distributions (instead of the built-in function gampdf)? This can make calculations faster
options.ages.dS=1;% resolution of age-ranked storage used to lookup the gamma pdfs
options.ages.short=1;% shorten the TTDs by neglecting numbers smaller than eps in the tails? This can make calculations faster
options.ages.norm=0;% normalize distributions to avoid numerical inaccruacies? (basically make sure the weights applied in the convolution sum to 1)
options.ages.save_warmup=0;% save age results of the warmup? (uses a lot of memory!)
options.ages.save_interval=24*40;% interval of time (in the simulation time) to save age calculations, in hours (needed to reduce memory and RAM constraints)
options.ages.init_RTD=[];% initial residence time distribution in the reservoir that can be loaded instead of the default exponential distribution with mean T0
options.ages.age_prctiles=[0.100.250.500.750.90];% percentiles of TTDs and RTDs to be calculated
options.ages.T_group=24*[27315073100120150240360365550100011003000];% groups to classify water younger than the given ages hours (needs to be round number)
options.ages.SAStype_Q='gam';% type of SAS function used for Q, see evalSAS.m (here gamma distribution)
options.ages.SAStype_ET='RS';% type of SAS function used for ET, see evalSAS.m (here Random Sampling)
% Tracer options
options.tracer=1;% 1 for 2H (only available choice here)
% Initialization scripts
options.dt=24;% desired data time step in hours
options.y0=[];% no user-loaded initial values for state variables
options.nbyrs_warmup=0;% number of years for the warmup period (here there is no warmup needed, we directly match the simulated values to observations that already exclude the 12.5 first years as warmup)
options.movavg=1;% apply moving average to results? (necessary to remove artificial numerical oscillations due to the convolution equation and the different resolutions dt and dT)
options.periods.eval=options.periods.run;
param=[624/87,87];% k=mu/theta and theta of the SAS of Q gamma distribution (best set from the monte carlo)
[Eff_C,model_output,model_output_warmup,options]=model_run_virtual(param,model_input,meas,options);% script that runs the model
% model_output contains all simulation results, including all age results (see model_calc.m)
%% Monte Carlo script
% Note: this was run on a high performance computer (using a parfor loop). A standard office PC would take about 15 days too complete the Monte Carlo simulation!
% The results can be found in the file "Monte_Carlo_results.mat". Columns 1 and 2 respectively correspond to parameters k=mu/theta and theta, column 3 corresponds to the NSE