-
Notifications
You must be signed in to change notification settings - Fork 10
/
hbv_model.cpp
executable file
·555 lines (440 loc) · 16.2 KB
/
hbv_model.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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
/*
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/>.
*/
#include "hbv_model.h"
using namespace std;
#define PI 3.141592
hbv_model::hbv_model() {
// TODO Auto-generated constructor stub
}
hbv_model::~hbv_model() {
// TODO Auto-generated destructor stub
}
hbv_model::hbv_model(string dataFile)
{
//Read input data and allocate internal arrays
readData(dataFile);
//Calculate the Hamon Potential Evaporation for the time series
calculateHamonPE(startingIndex, data.nDays, dayStartIndex);
}
void hbv_model::hbv_allocate(int nDays)
{
states.stw1 = new double [nDays];
states.stw2 = new double [nDays];
states.sowat = new double [nDays];
states.sdep = new double [nDays];
tst = 24*3600; // daily timestep
// (these will be reset after MaxBas is read in)
fluxes.Qrouting = new double [1];
//Allocate the array used to store the modelled Q, and other things
fluxes.Qsim = new double [nDays];
fluxes.actualET = new double [nDays];
return;
}
double hbv_model::snow(int modelDay)
{
double smelt = 0.0;
double eff_precip = 0.0; //effective precip initialized to zero
// Read in temperature and precip data for this time step
double avg_temp = data.avgTemp[startingIndex + modelDay];
double precip = data.precip[startingIndex + modelDay];
// starting point: equal to yesterday
states.sdep[modelDay] = states.sdep[modelDay-1];
// Snow/Rain
if (avg_temp < params.ttlim)
states.sdep[modelDay] += precip; // if temperature is lower than threshold (ttlim) --> precip is all snow
else
eff_precip += precip; // otherwise --> add precip to effective precip
// Snow melt if temperature > threshold (degw)
if (avg_temp > params.degw)
{
//If there is actually snow to melt in the snow store...
if (states.sdep[modelDay] > 0.0)
{
//Calculate snow melt using degree-day factor (degd)
smelt = (avg_temp - params.degw)*params.degd;
//If snow melt that wants to occur is more than what is actually stored...
if (smelt > states.sdep[modelDay])
{
eff_precip += states.sdep[modelDay]; //add full snow depth to effective precip
states.sdep[modelDay] = 0.0; //All of the snow has melted
}
else //Otherwise, we melt a portion of the snow store
{
eff_precip += smelt; //effective precip is precip together with what acutally melted
states.sdep[modelDay] -= smelt; //Remove the amount that melted from the snow store
}
}
}
return eff_precip;
}
void hbv_model::soil(double eff_precip, int modelDay)
{
double hsw, AET, runoff_depth;
double fcap = params.fcap;
double lp = params.lp;
double beta = params.beta;
double PET = evap.PE[modelDay];
// starting point: equal to yesterday's storage
states.sowat[modelDay] = states.sowat[modelDay-1];
//If the soil moisture storage is already at capacity, runoff = all precip + excess
if (states.sowat[modelDay] >= fcap) {
runoff_depth = eff_precip + (states.sowat[modelDay] - fcap);
states.sowat[modelDay] = fcap;
}
else
{
//This is the portion of the effective precip that goes into storage
hsw = eff_precip * (1.0 - pow((states.sowat[modelDay]/fcap), beta));
states.sowat[modelDay] += hsw;
runoff_depth = eff_precip - hsw;
//If the amount going into the soil moisture storage will result in exceeding the capacity of the store...
if (states.sowat[modelDay] > fcap)
{
runoff_depth += (states.sowat[modelDay] - fcap);
states.sowat[modelDay] = fcap; //We are at capacity
}
}
AET = PET*min(states.sowat[modelDay-1]/(fcap*lp), 1.0); // actual ET, after adjusting for saturation in soil layer
if (AET < 0.0) AET = 0.0;
//If there is enough in the soil moisture store to supply the AET, subtract it
if (states.sowat[modelDay] > AET) {
fluxes.actualET[modelDay] = AET;
states.sowat[modelDay] -= AET;
}
else {
fluxes.actualET[modelDay] = states.sowat[modelDay];
states.sowat[modelDay] = 0.0; // all of it evaporates
}
states.stw1[modelDay] += states.stw1[modelDay-1] + runoff_depth;
return;
}
double hbv_model::discharge(int modelDay)
{
double Q0, Q1, Q2, Qall;
//If the upper reservoir water level is above the threshold for near surface flow
if (states.stw1[modelDay] > params.hl1)
{
//Calculate it, and remove it from the reservoir
Q0 = (states.stw1[modelDay] - params.hl1)*params.ck0;
states.stw1[modelDay] -= Q0;
}
else Q0 = 0.0;
//If there is still water left in the upper reservoir
if (states.stw1[modelDay] > 0.0)
{
//Calculate what now goes into interflow, and remove it
Q1 = states.stw1[modelDay] * params.ck1;
states.stw1[modelDay] -= Q1;
}
else Q1 = 0.0;
//If there is still anough water in the upper reservois to completely supply percolation...
if (states.stw1[modelDay] > params.perc)
{
// Move the amount from the upper to the lower reservoir
states.stw1[modelDay] -= params.perc;
states.stw2[modelDay] += params.perc;
}
else
{
//We just put what we can from the upper into the lower
states.stw2[modelDay] += states.stw1[modelDay];
states.stw1[modelDay] = 0.0;
}
//If there is water in the lower reservoir...
if (states.stw2[modelDay] > 0.0)
{
//Calculate base flow, and remove it
Q2 = states.stw2[modelDay] * params.ck2;
states.stw2[modelDay] -= Q2;
}
else Q2 = 0.0;
Qall = (Q0 + Q1 + Q2); // total dischargearge - mm per timestep
return Qall;
}
void hbv_model::routing(double Qall, int modelDay)
{
///////////////////////////////////////////////////////////
//Parameter in code | parameter in manual/lit | description
///////////////////////////////////////////////////////////
//Qall | Q0+Q1+Q2 | Total dischargearge from both reservoirs
int m2;
double wsum;
double *wei = new double [params.maxbas];
///////////////////////////////////////////////////////////
//Variable in code | variable in manual/lit | description
///////////////////////////////////////////////////////////
//wei | g(t,MAXBAS) | transformation function consisting os a triangular weighting function and one free parameter
//Qrouting | NA | This is the flow from the single Qall spread out over time according to the transformation function
//Qsim | NA | The final flow output by the model
m2 = (params.maxbas / 2)-1;
wsum = 0.0;
//Calculate the values of the transformation function according to maxbas
for (int i=0; i< params.maxbas; i++)
{
if (i <= m2) wei[i] = double(i+1);
else wei[i] = double(params.maxbas - (i+1)) + 1.0;
wsum += wei[i];
}
//Now, spread the flow Qall out over Qind according to the transformation function
for (int i=0; i < params.maxbas; i++)
{
wei[i] /= wsum;
//Qind is constantly added to by the transformed Qall. In other words, when Qall is transformed (spread out over time)
//it is then added to whatever currently exists in Qind for those time steps. In other words, a previous transformation of
//Qall for the previous time step placed flows in Qind in times that overlapped with the currently transformed flow times.
fluxes.Qrouting[i] += Qall * wei[i];
}
fluxes.Qsim[modelDay] = fluxes.Qrouting[0];
delete[] wei;
return;
}
void hbv_model::backflow()
{
int klen = 2*params.maxbas-1;
for (int k = 0; k < klen; k++)
{
fluxes.Qrouting[k] = fluxes.Qrouting[k+1];
}
fluxes.Qrouting[klen] = 0.0;
return;
}
void hbv_model::reinitForMaxBas()
{
delete[] fluxes.Qrouting;
fluxes.Qrouting = new double [2*params.maxbas];
for (int i = 0; i < 2*params.maxbas; i++)
{
fluxes.Qrouting[i] = 0.0;
}
return;
}
void hbv_model::hbv_delete(int nDays)
{
delete[] states.stw1;
delete[] states.stw2;
delete[] states.sowat;
delete[] states.sdep;
delete[] fluxes.Qrouting;
delete[] fluxes.Qsim;
delete[] fluxes.actualET;
for (int i = 0; i < nDays; i++) delete[] data.date[i];
delete[] data.date;
delete[] data.precip;
delete[] data.evap;
delete[] data.flow;
delete[] evap.PE;
if(data.tempData>1){
delete[] data.maxTemp;
delete[] data.minTemp;
}
delete[] data.avgTemp;
return;
}
void hbv_model::setParameters(double* parameters){
// assign parameters to HBV structure
// Rate constants K0, K1, K2: entered with units of 1/day, but converted to unitless
params.ck2 = 1.0 / parameters[0] * tst / (3600.0 * 24.0);
params.ck1 = 1.0 / parameters[1] * tst / (3600.0 * 24.0);
params.ck0 = 1.0 / parameters[2] * tst / (3600.0 * 24.0);
params.maxbas = ROUNDINT(parameters[3] / 24); // Number of days for hydrograph routing
params.degd = parameters[4] * tst / (3600.0 * 24.0); // Degree-day factor [mm/(degC-d)]
params.degw = parameters[5]; // Snowmelt threshold [degC]
params.ttlim = parameters[6]; // Temp to start snowing [degC]
params.perc = parameters[7]; // Percolation [mm/d]
params.beta = parameters[8]; // Beta (soil moisture exponent, unitless)
params.lp = parameters[9]; // Unitless evaporation constant
params.fcap = parameters[10]; // Max storage of soil layer [mm]
params.hl1 = parameters[11]; // Max storage of shallow layer [mm]
}
void hbv_model::reinitStateFluxes(){
// set states and fluxes to zero
for(int k=0; k<data.nDays; k++){
states.sdep[k] = 0.0;
states.sowat[k] = 0.0;
states.stw1[k] = 0.0;
states.stw2[k] = 0.0;
fluxes.actualET[k] = 0.0;
fluxes.Qsim[k] = 0.0;
}
}
void hbv_model::calc_HBV(double* parameters)
{
// set parameters and reinitialize HBV
setParameters(parameters);
reinitStateFluxes();
reinitForMaxBas();
// Now run the components of the model
double Qall, eff_precip;
// Run over daily timesteps (starting at 1)
for (int day = 1; day < data.nDays; day++)
//for (int day = 1; day < 10; day++)
{
//Degree-day snow module (sets eff_precip value)
eff_precip = snow(day);
//Soil/ET module (sets runoff_depth value)
soil(eff_precip, day);
// Calculate the resulting dischargearge Qall
Qall = discharge(day);
// Route Qall using MaxBas routing
routing(Qall, day);
// Shift the routing arrays to the next timestep
backflow();
}
return;
}
void hbv_model::readData(string filename){
ifstream in;
string sJunk = "";
int ijunk;
double dTemp;
in.open(filename.c_str(), ios_base::in);
if(!in)
{
cout << "The input file specified: " << filename << " could not be found!" << endl;
exit(1);
}
//Look for the <WATERSHED_NAME> key
while (sJunk != "<WATERSHED_NAME>")
{
in >> sJunk;
}
in >> data.ID;
//Return to the beginning of the file
in.seekg(0, ios::beg);
//Look for the <GAGE_LATITUDE> key
while (sJunk != "<GAGE_LATITUDE>")
{
in >> sJunk;
}
in >> data.gageLat;
//Return to the beginning of the file
in.seekg(0, ios::beg);
//Look for the <GAGE_LONGITUDE> key
while (sJunk != "<GAGE_LONGITUDE>")
{
in >> sJunk;
}
in >> data.gageLong;
//Return to the beginning of the file
in.seekg(0, ios::beg);
//Look for the <DRAINAGE_AREA> key
while (sJunk != "<DRAINAGE_AREA>")
{
in >> sJunk;
}
in >> data.DA;
//Return to the beginning of the file
in.seekg(0, ios::beg);
//Look for the <TIME_STEPS> key
while (sJunk != "<TIME_STEPS>")
{
in >> sJunk;
}
in >> data.nDays;
//Return to the beginning of the file
in.seekg(0, ios::beg);
//Look for the <INDEX_INIT> key
while (sJunk != "<INDEX_INIT>")
{
in >> sJunk;
}
in >> startingIndex;
//Return to the beginning of the file
in.seekg(0, ios::beg);
//Look for the <DOY_INIT> key
while (sJunk != "<DOY_INIT>")
{
in >> sJunk;
}
in >> dayStartIndex;
//Return to the beginning of the file
in.seekg(0, ios::beg);
//Look for the <TEMP_DATA> key
while (sJunk != "<TEMP_DATA>")
{
in >> sJunk;
}
in >> data.tempData;
//Return to the beginning of the file
in.seekg(0, ios::beg);
//Allocate the arrays
hbv_allocate(data.nDays);
data.date = new int* [data.nDays];
for (int i=0; i<data.nDays; i++) data.date[i] = new int[3];
data.precip = new double[data.nDays];
data.evap = new double[data.nDays];
data.flow = new double[data.nDays];
if(data.tempData>1){
data.maxTemp = new double[data.nDays];
data.minTemp = new double[data.nDays];
}
data.avgTemp = new double[data.nDays];
//Look for the <DATA_START> key
while (sJunk != "<DATA_START>")
{
in >> sJunk;
}
//Once we found the key, ignore the rest of the line and move to the data
in.ignore(1000,'\n');
//Loop through all of the input data and read in this order:
for (int i=0; i<data.nDays; i++)
{
in >> dTemp;
data.date[i][0] = int(dTemp);
in >> dTemp;
data.date[i][1] = int(dTemp);
in >> dTemp;
data.date[i][2] = int(dTemp);
if(data.tempData > 1){ // max and min temperatures
in >> data.precip[i] >> data.flow[i] >> data.maxTemp[i] >> data.minTemp[i];
data.avgTemp[i] = (data.maxTemp[i] + data.minTemp[i])/2.0;
}else{
in >> data.precip[i] >> data.flow[i] >> data.avgTemp[i] ;
}
in.ignore(1000,'\n');
}
//Close the input file
in.close();
return;
}
void hbv_model::calculateHamonPE(int dataIndex, int nDays, int startDay){
int oldYear;
int counter;
//Allocate
evap.PE = new double [nDays];
//Initialize the starting year
oldYear = data.date[dataIndex][0];
counter = startDay-1;
//Fill out each of the arrays
for (int i=0; i<nDays; i++)
{
//If the years hasn't changed, increment counter
if (data.date[dataIndex+i][0] == oldYear) counter++;
//If it has changed, reset counter - this handles leap years
else counter = 1;
evap.day = counter;
evap.P = asin(0.39795*cos(0.2163108 + 2.0 * atan(0.9671396*tan(0.00860*double(evap.day-186)))));
evap.dayLength = 24.0 - (24.0/PI)*(acos((sin(0.8333*PI/180.0)+sin(data.gageLat*PI/180.0)*sin(evap.P))/(cos(data.gageLat*PI/180.0)*cos(evap.P))));
evap.eStar = 0.6108*exp((17.27*data.avgTemp[dataIndex+i])/(237.3+data.avgTemp[dataIndex+i]));
evap.PE[i] = (715.5*evap.dayLength*evap.eStar/24.0)/(data.avgTemp[dataIndex+i] + 273.2);
oldYear = data.date[dataIndex+i][0];
}
return;
}
MyData hbv_model::getData(){
return data;
}
hbv_fluxes hbv_model::getFluxes(){
return fluxes;
}