forked from mxgiuliani00/hbv
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main_HBV.cpp
executable file
·82 lines (65 loc) · 2.58 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
/*
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>
using namespace std;
void evaluate(double* Qobs, double* Qsim, int nDays, double* objs){
// calibration minimizes the RMSE + maximize R2 (1-year of warmup)
vector<double> Qerr, Qerr2, Qv;
for(unsigned int i=366; i<nDays; i++){
Qerr.push_back( Qobs[i] - Qsim[i] ); // model error
Qerr2.push_back( (Qobs[i] - Qsim[i])*(Qobs[i] - Qsim[i]) ); // model squared error
Qv.push_back( Qobs[i] );
}
double MSE = pow( utils::computeMean(Qerr2), 0.5 );
double R2 = 1 - ( utils::computeVariance( Qerr ) / utils::computeVariance( Qv ) ) ;
// 2-objective calibration
objs[0] = MSE;
objs[1] = -R2;
}
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 = 2;
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;
}