forked from kettner/hydrotrend
-
Notifications
You must be signed in to change notification settings - Fork 1
/
hydrosumflow.c
342 lines (318 loc) · 13.4 KB
/
hydrosumflow.c
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
/*-------------------------------------------------------------------------------------------
* hydrosumflow.c
*
* Author: Albert Kettner, March 2006
*
* Sums the discharges from the snowmelt, icemelt and precipitation routines, calculates the
* annual peak flood.
* Stores the lagged overflow and groundwater pool size for the following year.
*
* Variable Def.Location Type Units Usage
* -------- ------------ ---- ----- -----
* err various int - error flag, halts program
* ii various int - temporary loop counter
* mtotal HydroSumFlow.c dbl % mass balance total
* p various int - temporary loop counter for events or outlets
* Qeventcounter HydroSumFlow.c int -
*
*-------------------------------------------------------------------------------------------*/
#include <math.h>
#include "hydroclimate.h"
#include "hydroparams.h"
#include "hydrotimeser.h"
#include "hydroinout.h"
/*-------------------------
* Start of HydroSumFlow
*-------------------------*/
int
hydrosumflow ()
{
/*-------------------
* Local Variables
*-------------------*/
int err, ii, p, Qeventcounter, eventsoccure, x, y;
double mtotal, Qpeaktemp;
/*------------------------
* Initialize Variables
*------------------------*/
err = 0;
Ewetannual = 0.0;
mtotal = 0.0;
Minput = 0.0;
Moutput = 0.0;
MEtotal = 0.0;
MQprevious = 0.0;
MQnext = 0.0;
Qtotal = 0.0;
Qeventcounter = 0;
eventsperyear = 0;
eventsoccure = 0;
if (yr == syear[ep])
{
Qgrandtotal[ep] = 0.0;
for (x = 0; x < eventsnr[ep]; x++)
{
if (setstartmeanQandQs > 0)
{
daysievent[x] = 0;
for (p = 0; p < maxnoutlet; p++)
Qgrandtotaloutlet[ep][p][x] = 0.0;
}
}
}
for (p = 0; p < maxnoutlet; p++)
{
Qpeakperoutlet[p] = 1.0;
Qtotaloutletannual[p] = 0.0;
if (setstartmeanQandQs > 0)
for (x = 0; x < eventsnr[ep]; x++)
Qtotaloutlet[p][x] = 0.0;
}
Qpeak = 1.0;
if (setstartmeanQandQs > 0)
for (p = 0; p < eventsnr[ep]; p++)
{
Qpeakevents[p] = 1.0;
numberday[p] = 0;
}
for (ii = 0; ii < daysiy; ii++)
{
/*------------------------------------------------------------------
* Sum the daily flow (m^3/s) and find the annual peak.
* Sum the annual total annual flow (m^3/s) and evaporation (m^3)
*------------------------------------------------------------------*/
if (setstartmeanQandQs == 0)
{
Qsumtot[ii] =
Qice[ii] + Qnival[ii] + Qrain[ii] + Qss[ii] + Qexceedgw[ii];
if (steadyoutletpctflag == 1)
if (floodvalue[ep] > 0.0 && Qsumtot[ii] > floodvalue[ep])
{
floodcounter++;
Qpeakfloodtemp[yr - syear[ep]][ii] = Qsumtot[ii];
}
}
if (setstartmeanQandQs > 0)
{
/*------------------------------------------------
* Check is baseflow isn't > than Qbartotal[ep]
*------------------------------------------------*/
if (baseflowtot[ep] > Qbartotal[ep])
{
fprintf (stderr,
"ERROR: in HydroSumFlow.c; setstartmeanQandQs=%d \n",
setstartmeanQandQs);
fprintf (stderr,
" baseflow is higher than your average discharge in year %d:\n",
yr);
fprintf (stderr, " baseflow = %e, Qbar= %e. \n",
baseflowtot[ep], Qbartotal[ep]);
fprintf (stderr, "Qgrandtotal[ep]=%e. \n", Qgrandtotal[ep]);
exit (-1);
}
Qsumtot[ii] =
((Qbartotal[ep] - baseflowtot[ep]) / Qbartotal[ep]) * (Qice[ii] +
Qnival[ii] +
Qrain[ii] +
Qss[ii] +
Qexceedgw
[ii]) +
baseflowtot[ep];
// printf("Qice=%f, Qnival=%f, Qrain=%f, Qss=%f, Qexceedgw=%f, Qsumtot=%f\n",Qice[ii], Qnival[ii], Qrain[ii], Qss[ii], Qexceedgw[ii], Qsumtot[ii]);
if (steadyoutletpctflag == 1)
{
for (p = 0; p < eventsnr[ep]; p++)
if (Qsumtot[ii] == Qpeakallevents[ep][p]
&& eventcounter < (eventsnr[ep] - 1))
{
eventcounter++;
numberday[eventsperyear] = ii;
eventsperyear++;
p = eventsnr[ep];
}
}
/*--------------------------------------
* calculate Qsum daily with baseflow
*--------------------------------------*/
if (outletmodelflag == 1)
for (p = 0; p < maxnoutlet; p++)
{
Qsum[ii][p] = Qsumtot[ii] * outletpct[p][ep][eventcounter];
}
daysievent[eventcounter]++;
} // ( end setstartmeanQandQs > 0 )
Qpeak = mx (Qpeak, Qsumtot[ii]);
/*-------------------------------------------------
* Find the largest Qpeak events of this year
* if the number of outlets is unknown or the
* Qfraction shifts per event over the outlets.
*-------------------------------------------------*/
if (steadyoutletpctflag == 1 && setstartmeanQandQs > 0)
{
for (p = 0; p < eventsnr[ep]; p++)
{
eventsoccure = 0;
if (Qpeakevents[p] < Qsumtot[ii]
&& Qeventcounter == (eventsnr[ep] - 1))
{
Qpeakevents[p] = mx (Qpeakevents[p], Qsumtot[ii]);
eventsoccure = 1;
p = eventsnr[ep];
}
if (Qpeakevents[p] < Qsumtot[ii]
&& Qeventcounter != (eventsnr[ep] - 1)
&& Qpeakevents[p] == 1.0)
{
Qpeakevents[p] = mx (Qpeakevents[p], Qsumtot[ii]);
Qeventcounter++;
eventsoccure = 1;
p = eventsnr[ep];
}
}
/*-------------------------------------
* Rearrange the events of this year
* ------------------------------------*/
if (eventsoccure == 1)
for (p = 0; p < eventsnr[ep] - 1; p++)
while (Qpeakevents[p] > Qpeakevents[p + 1])
{
Qpeaktemp = Qpeakevents[p + 1];
Qpeakevents[p + 1] = Qpeakevents[p];
Qpeakevents[p] = Qpeaktemp;
p = 0;
}
} /* end if ( steadyoutletpctflag == 1) */
/*---------------------------------------------------
* Calculate the total discharge of the year,
* per outlet and for the total river (all outlets)
*---------------------------------------------------*/
Qtotal += Qsumtot[ii];
if (outletmodelflag == 1 && setstartmeanQandQs > 0)
{
for (p = 0; p < maxnoutlet; p++)
{
Qpeakperoutlet[p] = mx (Qpeakperoutlet[p], Qsum[ii][p]);
Qtotaloutlet[p][eventcounter] += Qsum[ii][p];
}
}
Ewetannual += (Egw[ii] * totalarea[ep]) + (Ecanopy[ii] * rainarea[ii]);
} /* end for daysiy */
Qtotal *= dTOs;
Qgrandtotal[ep] += Qtotal;
if (outletmodelflag == 1 && setstartmeanQandQs > 0)
for (p = 0; p < maxnoutlet; p++)
for (y = eventcounter - eventsperyear; y < eventcounter + 1; y++)
{
Qtotaloutlet[p][y] *= dTOs;
Qgrandtotaloutlet[ep][p][y] += Qtotaloutlet[p][y];
Qtotaloutletannual[p] += Qtotaloutlet[p][y];
}
MEtotal +=
Enivalannual * totalarea[ep] + Eiceannual * glacierarea + Ewetannual;
/*-----------------------------------------------------
* Sum the carryover from/to the previous/next years
*-----------------------------------------------------*/
for (ii = 0; ii < maxday - daysiy; ii++)
MQprevious +=
(Qrainwrap[ii] + Qicewrap[ii] + Qnivalwrap[ii] + Qsswrap[ii]) * dTOs;
for (ii = daysiy; ii < maxday; ii++)
MQnext += (Qrain[ii] + Qice[ii] + Qnival[ii] + Qss[ii]) * dTOs;
/*---------------------------
* Set the minimum Qsum[i]
* This is done after the
* precipitation balance!!
*---------------------------*/
/* for (ii = 0; ii < daysiy; ii++)
if (Qsumtot[ii] < 0.1)
Qsumtot[ii] = 0.1;
//This is a very bad fix, Qsumtot should be able to produce 0m3/s flow. Zero water discharge might cause some errors in the sediment routine, which are now fixed as well.
*/
/*-----------------------------------------------
* Check the precipitation input balance (m^3)
*-----------------------------------------------
* MPglacial = Mass of precip as glacial/year
* MPnival = Mass of precip as nival/year
* MPrain = Mass of precip as rain/year
*-----------------------------------------------*/
if (setstartmeanQandQs == 0)
{
Moutput = MPnival + MPglacial + MPrain;
Minput = (Pannual) * totalarea[ep];
mtotal = (Moutput - Minput) / Moutput;
if (fabs (mtotal) > masscheck)
{
fprintf (stderr, "ERROR: in HydroSumFlow.c: \n");
fprintf (stderr,
" Precipitation Balance Error: fabs(mtotal) > masscheck \n");
fprintf (stderr, " note: masscheck set in HydroParams.h \n");
fprintf (stderr, " masscheck = %e \n", masscheck);
fprintf (stderr, " mtotal = %e (%%) \n", mtotal);
fprintf (stderr, " Minput = %e (m^3/a) \n", Minput);
fprintf (stderr, " Moutput = %e (m^3/a) \n", Moutput);
fprintf (stderr, " out-in = %e (m^3/a) \n\n", Moutput - Minput);
fprintf (stderr, " Year = %d \n", yr);
fprintf (stderr, " Epoch = %d \n\n", ep + 1);
fprintf (stderr, " Minput = (Pannual)*totalarea \n");
fprintf (stderr, " \t Minput \t = \t %e (m^3) \n", Minput);
fprintf (stderr, " \t Pannual \t = \t %e (m^3) \n",
Pannual * totalarea[ep]);
fprintf (stderr, " \t Pannual \t = \t %e (m) \n", Pannual);
fprintf (stderr, " \t Pbaseflow \t = \t %e (m) \n\n",
baseflowtot[ep] * totalarea[ep]);
fprintf (stderr, " Moutput = MPnival + MPglacial + MPrain \n");
fprintf (stderr, " \t Moutput \t = \t %e (m^3) \n", Moutput);
fprintf (stderr, " \t MPnival \t = \t %e (m^3) \n", MPnival);
fprintf (stderr, " \t MPglacial \t = \t %e (m^3) \n", MPglacial);
fprintf (stderr, " \t MPrain \t = \t %e (m^3) \n\n", MPrain);
// err = 1;
}
/*--------------------------------
* Check the mass balance (m^3)
*-----------------------------------------------
* Msnowstart = Snow in basin at start of year
* Msnowend = Snow in basin at end of year
*-----------------------------------------------*/
Minput =
Pannual * totalarea[ep] + gwlast + Gmass + Msnowstart + MQprevious;
Moutput = MEtotal + Qtotal + gwstore[daysiy - 1] + Msnowend + MQnext;
mtotal = (Minput - Moutput) / Moutput;
if (fabs (mtotal) > masscheck)
{
fprintf (stderr, "ERROR: in HydroSumFlow.c: \n");
fprintf (stderr,
" Mass Balance Error: fabs(mtotal) > masscheck \n");
fprintf (stderr, " note: masscheck set in HydroParams.h \n");
fprintf (stderr, " masscheck = %e \n", masscheck);
fprintf (stderr, " mtotal = %e (%%) \n", mtotal);
fprintf (stderr, " Minput = %e (m^3/a) \n", Minput);
fprintf (stderr, " Moutput = %e (m^3/a) \n", Moutput);
fprintf (stderr, " out-in = %e (m^3/a) \n\n", Moutput - Minput);
fprintf (stderr, " Year = %d \n", yr);
fprintf (stderr, " Epoch = %d \n", ep + 1);
fprintf (stderr, " counterQandQs = %d \n\n", setstartmeanQandQs);
fprintf (stderr,
" Minput = Pannual + gwlast + previousice + previousnival + MQprevious \n");
fprintf (stderr, " \t Minput \t = \t\t\t %e (m^3) \n", Minput);
fprintf (stderr, " \t Pannual \t = %e (m) \t %e (m^3)\n", Pannual,
Pannual * totalarea[ep]);
fprintf (stderr, " \t gwlast \t = \t\t\t %e (m^3) \n", gwlast);
fprintf (stderr, " \t Gmass \t\t = \t\t\t %e (m^3) \n", Gmass);
fprintf (stderr, " \t Msnowstart \t = \t\t\t %e (m^3) \n",
Msnowstart);
fprintf (stderr, " \t MQprevious \t = \t\t\t %e (m^3) \n\n",
MQprevious);
fprintf (stderr, " \t 0.8*MPglacial = %e\n\n", 0.8 * MPglacial);
fprintf (stderr,
" Moutput = MEtotal + Qtotal + gwstore + nextice + nextnival + MQnext \n");
fprintf (stderr, " \t Moutput \t = \t %e (m^3) \n", Moutput);
fprintf (stderr, " \t MEtotal \t = \t %e (m^3)\n", MEtotal);
fprintf (stderr, " \t Qtotal \t = \t %e (m^3) \n", Qtotal);
fprintf (stderr, " \t gwstore[daysiy-1] = \t %e (m^3) \n",
gwstore[daysiy - 1]);
fprintf (stderr, " \t Msnowend \t = \t %e (m^3) \n", Msnowend);
fprintf (stderr, " \t MQnext \t = \t %e (m^3) \n", MQnext);
err = 1;
}
}
return (err);
} /* end of HydroSumFlow.c */