00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062 #include <math.h>
00063 #include <stdlib.h>
00064 #include "hydroclimate.h"
00065 #include "hydroinout.h"
00066 #include "hydroparams.h"
00067 #include "hydrotimeser.h"
00068 #include "hydroreadclimate.h"
00069 #include "hydroalloc_mem.h"
00070 #include "hydrofree_mem.h"
00071
00072 #ifdef DBG
00073 #include "hydroinout.h"
00074 #endif
00075
00076
00077
00078
00079 int hydrosedload ( gw_rainfall_etc* gw_rain )
00080 {
00081
00082
00083
00084
00085 #ifdef DBG
00086 FILE *fid;
00087 #endif
00088
00089 int err, i,p,kk,y;
00090 long j;
00091 double A, H, RQbar[maxepoch];
00092 double Tbar, Tmean,Tend, Tdummy;
00093 double Psi[daysiy], mu, sigma;
00094 double cbar, s;
00095 double ratio;
00096 double unit_normal, normal;
00097 double unit_normal2, normal2;
00098 double trnfac;
00099 double test;
00100 double *Coutletannual, *cbaroutlet, *soutlet, **Coutlet, *sigmaoutlet;
00101
00102
00103
00104
00105
00106 err = 0;
00107 y = 0;
00108 A = (totalarea[ep] / 1e6);
00109 H = maxalt[ep];
00110
00111
00112
00113
00114 sigmaoutlet = malloc1d( maxnoutlet, double );
00115 soutlet = malloc1d( maxnoutlet, double );
00116 Coutlet = malloc2d( daysiy, maxnoutlet, double );
00117 cbaroutlet = malloc1d( maxnoutlet, double );
00118 Coutletannual = malloc1d( maxnoutlet, double );
00119 Qsbartotoutlet = malloc2d( maxepoch, maxnoutlet, double );
00120
00121
00122
00123
00124 if ( raindatafile == 1 ){
00125 Tdummy=0.0;
00126 for (i=0; i<nyears[ep]; i++)
00127 Tdummy +=gw_rain->Tperyear[i];
00128 Tmean=Tdummy/nyears[ep];
00129 }
00130 else{
00131 Tend = Tstart[ep] + (Tchange[ep]*nyears[ep]);
00132 Tmean=(Tstart[ep] + Tend)/2;
00133 }
00134 Tbar = Tmean - ( (lapserate[ep] * maxalt[ep]) / 3.0 );
00135
00136
00137
00138
00139 if (setstartmeanQandQs == 3 && yr == syear[ep]){
00140 if ( Rvol[ep] != 0.0 ){
00141 if ( Rarea[ep] == 0.0 )
00142 for (kk=0; kk<nhypts[ep];kk++)
00143 if ( hypselev[ep][kk] == Ralt[ep] || hypselev[ep][kk] > Ralt[ep] ){
00144 Rarea[ep] = A - ( hypsarea[ep][kk] / 1e6 );
00145 kk = nhypts[ep];
00146 }
00147
00148 if (Rvol[ep] < 0.5){
00149
00150
00151
00152 TEsubbasin[ep] = ( 1.0 - (1.0 / (1 + 0.00021 * ((Rvol[ep] * 1e9) / Rarea[ep]))));
00153 TE[ep] = (Rarea[ep] / A) * TEsubbasin[ep];
00154 }
00155
00156 else if (Rvol[ep] >= 0.5) {
00157
00158
00159
00160 RQbar[ep] = Qbartotal[ep] * (Rarea[ep] / A);
00161 RQbar[ep] *=0.031536;
00162 TEsubbasin[ep] = 1.0 - (0.05 / pow(((Rvol[ep]/RQbar[ep])),0.5));
00163 TE[ep] = (Rarea[ep] / A) * TEsubbasin[ep];
00164 }
00165 }
00166 else if (Rvol[ep] == 0.0)
00167 TE[ep] = 0.0;
00168 }
00169
00170
00171
00172
00173 if (Qsbarformulaflag[ep] == 1)
00174 if (setstartmeanQandQs == 3 && yr == syear[ep]){
00175 Qsbartot[ep] = alpha3 * (1.0 - TE[ep]) * pow(A,alpha4) * pow(H,alpha5) * exp(k1*Tbar);
00176 }
00177 if (Qsbarformulaflag[ep] == 0){
00178 if (setstartmeanQandQs == 3 && yr == syear[ep])
00179 Qsbartot[ep] = (alpha6 *(1.0 - TE[ep]) * pow(Qbartotal[ep],alpha7) * pow(H,alpha8) * exp(k2*Tbar));
00180 }
00181 if (setstartmeanQandQs == 2)
00182 Qsbartot[ep] = 1;
00183
00184
00185
00186 if (outletmodelflag == 1 && setstartmeanQandQs > 2){
00187 Qsoutletdummy = 0.0;
00188 for (p=0; p<maxnoutlet; p++){
00189 Qsbartotoutlet[ep][p]= pow(Qbar[ep][p][eventcounter-eventsperyear],alpha7) ;
00190 Qsoutletdummy += Qsbartotoutlet[ep][p];
00191 }
00192 }
00193
00194
00195
00196
00197 trnfac = ( rhowater[ep] * rhosed[ep] * trneff ) / \
00198 ( (rhosed[ep] - rhowater[ep]) * tan( anglerep*degTOr ));
00199
00200
00201
00202
00203
00204 mu = 0.0;
00205 s = 0.17 + (0.0000183 * Qbartotal[ep]);
00206 sigma = 0.763 * pow(0.99995, Qbartotal[ep]);
00207 cbar = 1.4 - (0.025 * Tbar) + (0.00013 * H) + (0.145 *log(Qsbartot[ep]));
00208
00209
00210
00211
00212
00213
00214 Qsannual = 0.0;
00215 Csannual = 0.0;
00216 Qbedannual = 0.0;
00217 if (outletmodelflag == 1 && setstartmeanQandQs > 2)
00218 for (p=0; p<maxnoutlet; p++){
00219 Qsannualoutlet[p] = 0.0;
00220 Csannualoutlet[p] = 0.0;
00221 Qbedannualoutlet[p] = 0.0;
00222 Coutletannual[p] = 0.0;
00223 }
00224 if ( yr == syear[ep] ){
00225 Qsgrandtotal[ep] = 0.0;
00226 Csgrandtotal[ep] = 0.0;
00227 if ( outletmodelflag == 1 && setstartmeanQandQs > 2 )
00228 for (p=0; p<maxnoutlet; p++){
00229 Qsgrandtotaloutlet[ep][p] = 0.0;
00230 Csgrandtotaloutlet[ep][p] = 0.0;
00231 }
00232 }
00233
00234
00235
00236
00237
00238
00239 for (i=0; i < daysiy; i++) {
00240
00241
00242
00243 unit_normal = ranarray[i];
00244 normal = (sigma * unit_normal) + mu;
00245 Psi[i] = exp(normal);
00246 }
00247
00248
00249
00250
00251 if (setstartmeanQandQs == 2 && yr == syear[ep]){
00252 C = malloc1d( nyears[ep], double );
00253 for (j=0; j < nyears[ep]; j++) {
00254 unit_normal2 = ((rand()/327680000.9));
00255
00256 normal2 = (s * unit_normal2) + cbar;
00257 C[j] = normal2;
00258
00259 }
00260
00261 }
00262 for (i=0; i < daysiy; i++) {
00263
00264
00265
00266
00267
00268
00269 ratio = Qsumtot[i]/Qbartotal[ep];
00270 if (setstartmeanQandQs < 4){
00271 Qs[i] = Psi[i] * pow(ratio, C[yr-syear[ep]]);
00272 }
00273 if (setstartmeanQandQs == 4){
00274 Qs[i] = Psi[i] * Qsbarnew[ep] * pow(ratio, C[yr-syear[ep]]);
00275
00276
00277
00278
00279 if (isnan(Qs[i])) {
00280 fprintf( stderr,"\nHydroSedload ERROR: Qs = nan \n");
00281 fprintf( stderr," yr = %d, day = %d \n", yr, i );
00282 fprintf( stderr," Qnival[i] = %e \n", Qnival[i] );
00283 fprintf( stderr," Qrain[i] = %e \n", Qrain[i] );
00284 fprintf( stderr," Qexceedgw[i] = %e \n", Qexceedgw[i] );
00285 fprintf( stderr," Qice[i] = %e \n", Qice[i] );
00286 fprintf( stderr," Qss[i] = %e \n", Qss[i] );
00287 fprintf( stderr," baseflow[ep] = %e \n", baseflowtot[ep] );
00288 fprintf( stderr," ratio = %e \n", ratio );
00289 fprintf( stderr," Qsum[i] = %e \n", Qsumtot[i] );
00290 fprintf( stderr," Qbar[ep] = %e \n", Qbartotal[ep] );
00291 fprintf( stderr," Psi[i] = %e \n", Psi[i] );
00292 fprintf( stderr," Qsbarnew[ep] = %e \n", Qsbarnew[ep] );
00293 fprintf( stderr," C[i] = %e \n", C[yr-syear[ep]] );
00294 fprintf( stderr," ep = %d \n", ep );
00295 fprintf( stderr," cbar = %e \n", cbar );
00296 err = 1;
00297 }
00298 }
00299
00300
00301
00302
00303 if (Qs[i] > 0.0) {
00304 Cs[i] = Qs[i] / Qsumtot[i];
00305 Qsannual += Qs[i] * dTOs;
00306 } else
00307 Cs[i] = 0.0;
00308 Csannual += Cs[i]*dTOs;
00309
00310
00311
00312
00313
00314
00315
00316
00317
00318
00319 Qb[i] = trnfac * rslope[ep] * pow( Qsumtot[i], alphabed );
00320 Qbedannual += Qb[i] * dTOs;
00321
00322
00323
00324
00325
00326 if (outletmodelflag == 1 && (setstartmeanQandQs > 2)){
00327
00328
00329
00330
00331
00332
00333 if ( numberday[y] == i && eventsperyear > 0){
00334 y++;
00335 eventsperyear--;
00336 }
00337
00338
00339
00340
00341
00342
00343 if (i == 0)
00344 for (j=0; j < daysiy; j++) {
00345 for (p=0; p<maxnoutlet; p++){
00346 if (outletpct[p][ep][eventcounter-eventsperyear] != 0.0){
00347 test = (Qsbartotoutlet[ep][p]/Qsoutletdummy)*Qsbartot[ep];
00348 if (test == 0.000 ){
00349 printf("test=%f",test);
00350 exit(-1);
00351 }
00352 cbaroutlet[p] = (1.4 - (0.025 * Tbar) + (0.00013 * H) + (0.145 *log (test)));
00353 soutlet[p] = (0.17 + (0.0000183 * Qbar[ep][p][eventcounter-eventsperyear]));
00354 unit_normal2 = ranarray[i + daysiy];
00355 normal2 = (soutlet[p] * unit_normal2) + cbaroutlet[p];
00356 Coutlet[j][p] = normal2;
00357 Coutletannual[p] +=Coutlet[j][p];
00358 }
00359 }
00360 }
00361 for (p=0; p<maxnoutlet; p++){
00362 if (outletpct[p][ep][eventcounter-eventsperyear] != 0.0){
00363
00364
00365
00366 ratio = Qsum[i][p]/Qbar[ep][p][eventcounter-eventsperyear];
00367 if (setstartmeanQandQs == 3){
00368 Qsoutlet[i][p] = Psi[i] * pow((Qsbartotoutlet[ep][p]/Qsoutletdummy), Coutlet[yr-syear[ep]][p]) * pow(ratio, Coutlet[yr-syear[ep]][p]);
00369 }
00370 if (setstartmeanQandQs == 4){
00371 Qsoutlet[i][p] = Psi[i] * pow((Qsbartotoutlet[ep][p]/Qsoutletdummy), Coutlet[yr-syear[ep]][p]) * Qsbarnew2[ep] * pow(ratio, Coutlet[yr-syear[ep]][p]);
00372
00373
00374
00375
00376 if (isnan(Qsoutlet[i][p])) {
00377 fprintf( stderr,"\nHydroSedload ERROR: Qsoutlet[day][nr] = nan \n");
00378 fprintf( stderr," yr = %d, day = %d, outlet = %d \n", yr, i, p );
00379 fprintf( stderr," Qnival[i] = %e \n", Qnival[i] );
00380 fprintf( stderr," Qrain[i] = %e \n", Qrain[i] );
00381 fprintf( stderr," Qexceedgw[i] = %e \n", Qexceedgw[i] );
00382 fprintf( stderr," Qice[i] = %e \n", Qice[i] );
00383 fprintf( stderr," Qss[i] = %e \n", Qss[i] );
00384 fprintf( stderr," baseflow[ep] = %e \n", baseflowtot[ep] );
00385 fprintf( stderr," ratio = %e \n", ratio );
00386 fprintf( stderr," Qsumoutlet = %e \n", Qsum[i][p] );
00387 fprintf( stderr," Qbar[ep] = %e \n", Qbartotal[ep] );
00388 fprintf( stderr," Psi[i] = %e \n", Psi[i] );
00389 fprintf( stderr," Qsbarnew2[ep]= %e \n", Qsbarnew2[ep] );
00390 fprintf( stderr," Coutlet[i][p]= %e \n", Coutlet[i][p] );
00391 fprintf( stderr," cbaroutlet[p]= %e \n", cbaroutlet[p] );
00392 fprintf( stderr," soutlet[p] = %e \n", soutlet[p] );
00393 err = 1;
00394 }
00395 }
00396 Qsannualoutlet[p] += Qsoutlet[i][p] * dTOs;
00397
00398
00399
00400
00401 if (Qsoutlet[i][p] > 0.0)
00402 Csoutlet[i][p] = Qsoutlet[i][p] / Qsum[i][p];
00403 else Csoutlet[i][p] = 0.0;
00404 Csannualoutlet[p] += Csoutlet[i][p] * dTOs;
00405
00406
00407
00408
00409 Qboutlet[i][p] = trnfac * rslope[ep] * pow( Qsum[i][p], alphabed );
00410 Qbedannualoutlet[p] += Qboutlet[i][p] * dTOs;
00411 }
00412 else Qsoutlet[i][p] = 0.0;
00413 }
00414 }
00415 }
00416
00417
00418 Qsgrandtotal[ep] += Qsannual;
00419 Csgrandtotal[ep] += Csannual;
00420 if ( outletmodelflag == 1 && setstartmeanQandQs > 2 )
00421 for (p=0; p<maxnoutlet; p++){
00422 Qsgrandtotaloutlet[ep][p] += Qsannualoutlet[p];
00423 Csgrandtotaloutlet[ep][p] += Csannualoutlet[p];
00424 Coutlettotal[ep][p] +=Coutletannual[p];
00425 }
00426
00427
00428
00429
00430 freematrix1D( (void*)sigmaoutlet);
00431 freematrix1D( (void*)soutlet);
00432 freematrix1D( (void*)cbaroutlet);
00433 freematrix1D( (void*)Coutletannual);
00434 freematrix2D( (void**)Coutlet, daysiy);
00435 freematrix2D( (void**)Qsbartotoutlet, maxepoch);
00436
00437 return(err);
00438 }
00439