/Users/huttone/Devel/sedflux-new/sedflux/trunk/ew/hydrotrend/hydrooutlet.c

Go to the documentation of this file.
00001 /*
00002  *  HydroOutlet.c
00003  *
00004  *
00005  *  Author:    A.J. Kettner (April 2003)
00006  * 
00007  *  Contains 5 functions / subroutines for multiple outlet option
00008  *      in HYDORTREND.
00009  * 
00010  *  1) HydroOutletFraction
00011  *  2) HydroSetNumberOutlet
00012  *  3) HydroQFractionShuffle
00013  *  4) HydroAllocMemOutlet
00014  *  4a)Hydroallocmemoutlet1
00015  *  5) HydroFreeMemOutlet
00016  * 
00017  */
00018 
00019 #include <stdlib.h>
00020 #include <math.h>
00021 #include "hydroclimate.h"
00022 #include "hydroinout.h"
00023 #include "hydroparams.h"
00024 #include "hydrotimeser.h"
00025 #include "hydroreadclimate.h"
00026 #include "hydroalloc_mem.h"
00027 #include "hydrofree_mem.h"
00028 #include "hydrornseeds.h"
00029 
00030 
00031 /*  1) HydroOutletFraction
00032  *
00033  * If the discharge fraction per outlet is not specified by the user
00034  * and number of outlets is >1, hydrotrend computes the fraction based
00035  * on the number of outlets.
00036  * (If number of outlets changes during the ep, hydrotrend calculates
00037  * the fraction per event).
00038  * The calculation of the discharge fraction is based on x=y^2. y 
00039  * "represents" the number of outlets, and are values between 0 and 1.
00040  * (y = 1/(nr of outlets + 1)). So y0 = y*1; y1 = y*2; y2 = y*3 etc.
00041  * x is the power of y. As the total off all x must be equal to 1 (100% 
00042  * of the discharge is leaving the outlets) each x has to be devide by
00043  * the sum of all x.
00044  * 
00045  * 
00046  * Variable             Def.Location            Type            Units   Usage
00047  * --------             ------------            ----            -----   -----
00048  * check                HydroOutletFraction     double          -               Check if total fraction is 1.0
00049  * err                  various                         int                     -               error flag, halts program
00050  * fdistance    HydroOutletFraction     double          -               
00051  * p                    HydroOutletFraction     int                     -               counter
00052  * Qtempfractot HydroOutletFraction     double          -
00053  * totalpct             HydroOutletFraction     double          -
00054  * *tempfrac    HydroOutletFraction     double          -
00055  * *Qpowtempf   HydroOutletFraction     double          -
00056  * 
00057  */
00058  
00059 /*--------------------------------
00060  *  Start of HydroOutletFraction
00061  *--------------------------------*/
00062 int hydrooutletfraction(int x)
00063 {
00064 
00065 /*-------------------
00066  *  Local Variables
00067  *-------------------*/
00068 int err,p;
00069 double *tempfrac, *Qpowtempf;
00070 double fdistance, Qtempfractot, totalpct, check;
00071 
00072 /*------------------------
00073  *  Initialize variables
00074  *------------------------*/
00075 err = 0;
00076 Qtempfractot = 0.0;
00077 totalpct = 0.0;
00078 
00079 /*---------------------------------------
00080  *  Allocate memory for multiple outlet
00081  *---------------------------------------*/
00082 tempfrac        = malloc1d( maxnoutlet, double );
00083 Qpowtempf       = malloc1d( maxnoutlet, double );
00084 
00085 /*----------------------------------
00086  *  Set outlet fraction per outlet
00087  *----------------------------------*/ 
00088         for (p=0; p<maxnoutlet; p++)
00089         tempfrac[p] = 0.0;
00090         fdistance = 1.0 / (noutlet+1);
00091         for (p=0; p<noutlet; p++){
00092                 tempfrac[p] = fdistance * (1 + p);
00093                 Qpowtempf[p] = pow(tempfrac[p],2);
00094                 Qtempfractot += Qpowtempf[p];
00095         }       
00096         for (p=0; p<noutlet; p++){
00097                 outletpct[p][ep][x] = Qpowtempf[p]/Qtempfractot;
00098                 totalpct += outletpct[p][ep][x];
00099         }
00100         check = (totalpct - 1.0);
00101         if (fabs(check) > masscheck )
00102                 err++;
00103 
00104 /*---------------
00105  *  Free memory
00106  *---------------*/
00107         freematrix1D( (void*)tempfrac );
00108         freematrix1D( (void*)Qpowtempf );
00109 
00110         return (err);
00111 } /* end of hydrooutletfraction */
00112 
00113 
00114 
00115 /*  2) hydroSetNumberOutlet
00116  *
00117  * If the number of outlets is not specified by the user hydrotrend
00118  * computes randomly the number of outlet between a given range.
00119  * It will do this per event. So with each event (flood) hydrotrend
00120  * calculates a new number of outlets.
00121  * 
00122  * 
00123  * Variable             Def.Location                    Type            Units   Usage
00124  * --------             ------------                    ----            -----   -----
00125  * dumint               HydroSetNumberOutlet    int                     -
00126  * dumflt               HydroSetNumberOutlet    flt                     -
00127  * err                  various                                 int                     -               error flag, halts program
00128  * noutletoptionHydroSetNumberOutlet    int                     -
00129  * x                    HydroSetNumberOutlet    int                     -
00130  * 
00131  */
00132 
00133 /*---------------------------------
00134  *  Start of HydroSetNumberOutlet
00135  *---------------------------------*/
00136 int hydrosetnumberoutlet(int x)
00137 {
00138 
00139 /*-------------------
00140  *  Local Variables
00141  *-------------------*/
00142 int dumint, noutletoption;
00143 double dumdbl;
00144 float hydroran4(long *idum);
00145 static int err = 0;
00146 dumdbl = 0.5;
00147 
00148 /*------------------------
00149  *  Set number of outlet
00150  *------------------------*/
00151         if ( x == minnoutlet )
00152                 rnseed4 = -INIT_RAN_NUM_SEED;
00153         dumint = minnoutlet-1;
00154         while ( dumint < minnoutlet || dumint > maxnoutlet){
00155                 dumdbl = (double)hydroran4(&rnseed4);
00156                 if( 0 > dumdbl || dumdbl > 1 ){
00157                         err++;                  
00158                         fprintf( stderr,"A function in HydroRan2 failed in HydroSetNumberOutlet (HydroOutlet) \n");
00159                         fprintf( stderr," \t dumflt = %f: \t setting value to 0.5, x = %d \n", dumdbl, x);
00160                         dumdbl=0.5;
00161                 }               
00162                 dumdbl = dumdbl*10.0;           
00163                 dumint = (int)(dumdbl);
00164                 if ( err > 1 ){
00165                         fprintf( stderr, " ERROR in HydroSetNumberOutlet (HydroOutlet).\n" );
00166                         fprintf( stderr, "\t Randomnummer generator failed twice: HydroTrend Aborted \n\n" );
00167                         fprintf( fidlog, " ERROR in HydroSetNumberOutlet (HydroOutlet).\n" );
00168                         fprintf( fidlog, "\t Randomnummer generator failed twice: HydroTrend Aborted \n\n" );
00169                         exit(1);
00170                 }                                               
00171         }
00172         noutletoption = dumint;
00173         return (noutletoption); 
00174 } /* end of hydrosetnumberoutlet */
00175 
00176 
00177 
00178 /*
00179  * 3) Hydroqfractionshuffle
00180  * 
00181  * Randomly shuffles the outletfractions.
00182  *
00183  *
00184  * Variable             Def.Location                    Type    Units   Usage
00185  * --------             ------------                    ----    -----   -----
00186  * dumflt               Hydroqfractionshuffle   float   -               temporary float
00187  * dvals[31]    Hydroqfractionshuffle   int             -               shuffled array of daily index values
00188  * err                  Hydroqfractionshuffle   int             -               error flag, halts program
00189  * ii                   Hydroqfractionshuffle   int             -               temporary loop counter
00190  * mnth                 Hydroqfractionshuffle   int             -               month of the year
00191  * yy                   Hydroqfractionshuffle   int             -               temporary integer
00192  * pp                   Hydroqfractionshuffle   int             -               temporary integer
00193  *
00194  */
00195 
00196 /*------------------------------------
00197  *  Start of Hydroqfractionshuffle.c
00198  *------------------------------------*/
00199 int hydroqfractionshuffle( int k )
00200 {
00201 
00202 float hydroran5(long * idumd);
00203 double dumdbl;
00204 double dummy_double, *dummyoutletpct;
00205 int yy, ii, err, *nvals, pp, *dummyvals, a;
00206 
00207 /*---------------------------------------
00208  *  Allocate memory for multiple outlet
00209  *---------------------------------------*/
00210 dummyoutletpct  = malloc1d( maxnoutlet, double );
00211 nvals                   = malloc1d( maxnoutlet, int);
00212 dummyvals               = malloc1d( maxnoutlet, int );
00213 
00214 /*------------------------
00215  *  Initialize variables
00216  *------------------------*/
00217 err   = 0;
00218 dumdbl = 0.5;
00219 for( ii=0; ii<maxnoutlet; ii++ ){
00220         dummyoutletpct[ii] = outletpct[ii][ep][k];
00221         nvals[ii] = ii+1;
00222 }
00223 
00224 /*-------------------------------------
00225  *  shuffle the numbers of the outlet
00226  *-------------------------------------*/
00227         if ( k == 0)
00228                 rnseed5 = -INIT_RAN_NUM_SEED/20; 
00229         for( ii=0; ii<maxnoutlet; ii++ ) {
00230                 dumdbl = (double)hydroran5(&rnseed5);                           /* get a uniform random number [0:1] */
00231                 if( 0 > dumdbl || dumdbl > 1 ){
00232                         fprintf( stderr,"A function in HydroRan2 failed in HydroShuffle.c \n");
00233                         fprintf( stderr," \t dumdbl = %f: \t setting value to 0.5, ii = %d \n", dumdbl, ii);
00234                         dumdbl=0.5;
00235                         err++;
00236                 }
00237                 if ( k > 0 ){
00238                         dummy_double = dumdbl*(float)maxnoutlet;
00239                         yy = (int)rnd(dummy_double);                    /* scale to a random day */
00240                         if( yy == 0 ) yy += 1;
00241                         nvals[ii] = nvals[yy-1];
00242                         dummyvals[ii] = nvals[ii];              
00243                         if ( ii !=0 ){
00244                                 a=1;
00245                                 for (pp=ii-1; pp>-1; pp-- )
00246                                         while ( dummyvals[ii] == dummyvals[pp] ){
00247                                                 dummyvals[ii] = a;
00248                                                 a++;
00249                                                 pp = (ii-1);
00250                                         }
00251                                 nvals[ii] = dummyvals[ii];
00252                         }
00253                 }
00254         }
00255         for( ii=0; ii<maxnoutlet; ii++ ){
00256                 nvals[ii] = nvals[ii]-1;
00257                 outletpct[ii][ep][k] = dummyoutletpct[nvals[ii]];
00258         }
00259 
00260 /*---------------
00261  *  Free memory
00262  *---------------*/
00263         freematrix1D( (void*)dummyoutletpct );
00264         freematrix1D( (void*)nvals );
00265         freematrix1D( (void*)dummyvals );
00266 
00267         return(err);
00268 } /* end of HydroqfractionShuffle */
00269 
00270 
00271 /*  4) HydroAllocmemOutlet
00272  *
00273  * Allocates memory for the outlet variables. The real allocation takes
00274  * place in Hydroalloc_mem.c
00275  *
00276  */
00277  
00278 /*--------------------------------
00279  *  Start of HydroAllocmemOutlet
00280  *--------------------------------*/
00281 void hydroallocmemoutlet(int ep)
00282 {
00283         outletpcttotevents              = malloc2d( maxnoutlet, maxepoch, double );
00284         Qbedannualoutlet                = malloc1d( maxnoutlet, double );
00285         Qpeakperoutlet                  = malloc1d( maxnoutlet, double );
00286         Qpeakperoutletall               = malloc2d( maxepoch, maxnoutlet, double );
00287         Qtotaloutletannual              = malloc1d( maxnoutlet, double );
00288         Qsgrandtotaloutlet              = malloc2d( maxepoch, maxnoutlet, double );
00289         Csgrandtotaloutlet              = malloc2d( maxepoch, maxnoutlet, double );
00290         Qsannualoutlet                  = malloc1d( maxnoutlet, double );
00291         Csannualoutlet                  = malloc1d( maxnoutlet, double );
00292         Coutlettotal                    = malloc2d( maxepoch, maxnoutlet, double );
00293         Csoutlet                                = malloc2d( daysiy, maxnoutlet, double );
00294         Qboutlet                                = malloc2d( daysiy, maxnoutlet, double );
00295         Qsoutlet                                = malloc2d( daysiy, maxnoutlet, double );
00296         Qsum                                    = malloc2d( maxday, maxnoutlet, double );
00297         Qgrandtotalperepoch     = malloc2d( maxepoch, maxnoutlet, double );
00298         Qdummy                                  = malloc2d( maxepoch, maxnoutlet, double );
00299         Qgrandtotaltotoutlet    = malloc1d( maxnoutlet, double );
00300         Qsgrandtotaltotoutlet   = malloc1d( maxnoutlet, double );
00301         Qpeakfloodtemp                  = malloc2d( nyears[ep], daysiy, double );
00302         return;
00303 } /* end of HydroAllocmemOutlet1 */
00304 
00305 
00306 /*---------------------------------------
00307  *  Start of HydroAllocmemOutlet1; TEST
00308  *---------------------------------------*/
00309 void hydroallocmemoutlet1(int ep)
00310 {
00311         numberday                               = malloc1d( eventsnr[ep], long ); //max allocatie is 365
00312         nroutlets                               = malloc1d( eventsnr[ep], int );
00313         outletpct                               = malloc3d( maxnoutlet, maxepoch, eventsnr[ep], double);
00314         Qgrandtotaloutlet               = malloc3d( maxepoche, maxnoutlet, eventsnr[ep], double );
00315         Qpeakevents                             = malloc1d( eventsnr[ep], double );
00316         Qtotaloutlet                    = malloc2d( maxnoutlet, eventsnr[ep], double );
00317         Qbar                                    = malloc3d( maxepoche, maxnoutlet, eventsnr[ep], double );
00318         Qpeakallevents                  = malloc2d( maxepoche, eventsnr[ep], double );
00319         daysievent                              = malloc1d( eventsnr[ep], long ); // max = days*years per event
00320         return;
00321 } /* end of HydroAllocmemOutlet1 */
00322 
00323 
00324 
00325 /*  5) HydroFreeMemOutlet
00326  *
00327  * Free memory for the outlet variables. To free the memory, it will use
00328  * the various subprograms in Hydrofree_mem.c
00329  *
00330  */
00331  
00332 /*--------------------------------
00333  *  Start of HydroFreeMemOutlet
00334  *--------------------------------*/
00335 void hydrofreememoutlet(int j)
00336 {
00337         freematrix2D( (void**)outletpcttotevents, maxnoutlet );
00338         freematrix1D( (void*)Qbedannualoutlet );
00339         freematrix1D( (void*)Qpeakperoutlet );
00340         freematrix2D( (void**)Qpeakperoutletall, maxepoch);
00341         freematrix1D( (void*)Qtotaloutletannual );
00342         freematrix2D( (void**)Qsgrandtotaloutlet, maxepoch );
00343         freematrix2D( (void**)Csgrandtotaloutlet, maxepoch );
00344         freematrix1D( (void*)Qsannualoutlet );
00345         freematrix1D( (void*)Csannualoutlet );  
00346         freematrix2D( (void**)Coutlettotal, maxepoch );
00347         freematrix2D( (void**)Csoutlet, daysiy );
00348         freematrix2D( (void**)Qboutlet, daysiy );
00349         freematrix2D( (void**)Qsoutlet, daysiy );       
00350         freematrix2D( (void**)Qsum, maxday );
00351         freematrix2D( (void**)Qgrandtotalperepoch, maxepoch );  
00352         freematrix2D( (void**)Qdummy, maxepoch );       
00353         freematrix1D( (void*)Qgrandtotaltotoutlet );
00354         freematrix1D( (void*)Qsgrandtotaltotoutlet );
00355         freematrix2D( (void**)Qpeakfloodtemp, j );
00356         return;
00357 } /* end of hydroFreeMemOutlet */
00358 
00359 /*-------------------------------------
00360  *  Start of HydroFreeMemOutlet1 TEST
00361  *-------------------------------------*/
00362 void hydrofreememoutlet1(int ep)
00363 {
00364         freematrix1D( (void*)numberday );
00365         freematrix1D( (void*)nroutlets );
00366         freematrix3D( (void***)outletpct, maxnoutlet, maxepoch );
00367         freematrix3D( (void***)Qgrandtotaloutlet, maxepoche, maxnoutlet );
00368         freematrix1D( (void*)Qpeakevents );
00369         freematrix2D( (void**)Qtotaloutlet, maxnoutlet );
00370         freematrix3D( (void***)Qbar, maxepoche, maxnoutlet );
00371         freematrix2D( (void**)Qpeakallevents, maxepoche );
00372         freematrix1D( (void*)daysievent );                              
00373         return; 
00374 } /* end of hydroFreeMemOutlet1 */
00375 
00376 
00377 /* end of HydroOutlet.c */

Generated on Fri Jan 4 18:04:14 2008 for sedflux by  doxygen 1.5.2