-
Notifications
You must be signed in to change notification settings - Fork 10
/
main_HBV.cpp
executable file
·91 lines (73 loc) · 2.96 KB
/
main_HBV.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
/*
Copyright (C) 2010-2015 Matteo Giuliani, Josh Kollat, Jon Herman, and others.
HBV is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
HBV is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with HBV. If not, see <http://www.gnu.org/licenses/>.
*/
/****************************************************************************
C/C++ Version of HBV: Lumped model, one catchment. Uses Hamon ET and MOPEX forcing data.
*****************************************************************************/
#include "hbv_model.h"
#include "moeaframework.h"
#include "utils.h"
#include <math.h>
#include <vector>
using namespace std;
void evaluate(double* Qobs, double* Qsim, int nDays, double* objs){
//convert observations and simulations from array to vector removing first year which is used as warm-up
vector<double> Vobs(nDays, -99);
vector<double> Vsim(nDays, -99);
for(int i=366; i<Vobs.size(); i++){
Vobs[i] = Qobs[i];
Vsim[i] = Qsim[i];
}
// calibration using NSE decomposition from Gupta et al., 2009
// (see http://www.meteo.mcgill.ca/~huardda/articles/gupta09.pdf):
// obj 1) minimize relative variability (alpha)
// obj 2) minimize absolute value of relative bias (beta)
// obj 3) maximize correlation coefficient (r)
double alpha = utils::computeStDev(Vsim) / utils::computeStDev(Vobs);
double beta = fabs( utils::computeMean(Vsim) - utils::computeMean(Vobs) ) / utils::computeStDev(Vobs);
double r = utils::computeCorr(Vsim, Vobs);
// 3-objective calibration
objs[0] = alpha;
objs[1] = beta;
objs[2] = -r;
}
int main(int argc, char **argv)
{
// read user input: single input for calibration, two inputs for simulation
string input_file = argv[1];
string output_file;
if(argc>2){
output_file = argv[2];
}
// hbv model
hbv_model myHBV(input_file);
// calibration settings
int nobjs = 3;
int nvars = 12;
double objs[nobjs];
double vars[nvars];
MOEA_Init(nobjs, 0);
while (MOEA_Next_solution() == MOEA_SUCCESS) {
MOEA_Read_doubles(nvars, vars);
myHBV.calc_HBV(vars);
evaluate(myHBV.getData().flow, myHBV.getFluxes().Qsim, myHBV.getData().nDays, objs);
MOEA_Write(objs, NULL);
}
// save simulation results
if(argc>2){
utils::logArray(myHBV.getFluxes().Qsim, myHBV.getData().nDays, output_file);
}
// clear HBV
myHBV.hbv_delete(myHBV.getData().nDays);
return 0;
}