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

Go to the documentation of this file.
00001 /*
00002  *  HydroRan2.c
00003  *  This program contance a couple of functions, hydroran2 till hydroran5.
00004  *  The functions are the same but made to generate exactly the
00005  *  same numbers for every epoch when it goes 4 times through the
00006  *  loop in hydrotrend.c. (Once for calculating Qbar, second time
00007  *  to calculate Qs e.d.
00008  *
00009  *  Generates uniformly distributed numbers between 0.0 and 1.0.
00010  *  ran2.c From: "Numerical Recipes in C", p282, 2nd ed.
00011  *
00012  *  For the first deviate, use make sure the seed is negative;
00013  *  this initializes ran2 appropriately.
00014  *  For subsequent years, use the generated seed; idum should not
00015  *  be altered between successive deviates in a sequence.
00016  *
00017  */
00018 
00019 #include <math.h>
00020 #include "hydroparams.h"
00021 
00022 #define IM1     2147483563
00023 #define IM2     2147483399
00024 #define AM      (1.0/IM1)
00025 #define IMM1    (IM1-1)
00026 #define IA1     40014
00027 #define IA2     40692
00028 #define IQ1     53668
00029 #define IQ2     52774
00030 #define IR1     12211
00031 #define IR2     3791
00032 #define NTAB    32
00033 #define NDIV    (1+IMM1/NTAB)
00034 #define EPS     1.2e-7
00035 #define RNMX    (1.0-EPS)
00036 
00037 #define BIM1    2147483563
00038 #define BIM2    2147483399
00039 #define BAM     (1.0/BIM1)
00040 #define BIMM1   (BIM1-1)
00041 #define BIA1    40014
00042 #define BIA2    40692
00043 #define BIQ1    53668
00044 #define BIQ2    52774
00045 #define BIR1    12211
00046 #define BIR2    3791
00047 #define BNTAB   32
00048 #define BNDIV   (1+BIMM1/BNTAB)
00049 #define BEPS    1.2e-7
00050 #define BRNMX   (1.0-BEPS)
00051 
00052 #define CIM1    2147483563
00053 #define CIM2    2147483399
00054 #define CAM     (1.0/CIM1)
00055 #define CIMM1   (CIM1-1)
00056 #define CIA1    40014
00057 #define CIA2    40692
00058 #define CIQ1    53668
00059 #define CIQ2    52774
00060 #define CIR1    12211
00061 #define CIR2    3791
00062 #define CNTAB   32
00063 #define CNDIV   (1+CIMM1/CNTAB)
00064 #define CEPS    1.2e-7
00065 #define CRNMX   (1.0-CEPS)
00066 
00067 #define DIM1    2147483563
00068 #define DIM2    2147483399
00069 #define DAM     (1.0/DIM1)
00070 #define DIMM1   (DIM1-1)
00071 #define DIA1    40014
00072 #define DIA2    40692
00073 #define DIQ1    53668
00074 #define DIQ2    52774
00075 #define DIR1    12211
00076 #define DIR2    3791
00077 #define DNTAB   32
00078 #define DNDIV   (1+DIMM1/DNTAB)
00079 #define DEPS    1.2e-7
00080 #define DRNMX   (1.0-DEPS)
00081 
00082 #include <stdio.h>
00083 
00084 /*------------------------
00085  *  Start of HydroRan2.c
00086  *------------------------*/
00087 float hydroran2(long *idum)
00088 {
00089 int jj;
00090 long kk;
00091 static long idum2=123456789;
00092 static long iy=0;
00093 static long iv[NTAB];
00094 float temp;
00095 
00096 /*----------------------------
00097  *  Initialize the generator
00098  *----------------------------*/
00099 if (*idum <= 0) {
00100     if( -(*idum) < 1 ) *idum = 1;         /* prevent idum = 0 */
00101     else *idum = -(*idum);
00102     idum2=(*idum);
00103     for (jj=NTAB+7; jj>=0; jj--) {        /* load the shuffle table */
00104          kk=(*idum)/IQ1;
00105          *idum=IA1*(*idum-kk*IQ1)-kk*IR1;
00106          if( *idum < 0 ) *idum += IM1;
00107          if( jj < NTAB ){
00108          iv[jj] = *idum;
00109          }
00110     }
00111     iy = iv[0];
00112 }
00113 
00114 /*-----------------------
00115  *  Start the generator
00116  *-----------------------*/
00117 kk = (*idum)/IQ1;
00118 *idum = IA1*(*idum-kk*IQ1)-kk*IR1;
00119 if (*idum < 0) *idum += IM1;
00120 kk = idum2/IQ2;
00121 idum2 = IA2*(idum2-kk*IQ2)-kk*IR2;
00122 if (idum2 < 0) idum2 += IM2;
00123 jj=iy/NDIV;
00124 iy = iv[jj]-idum2;
00125 iv[jj] = *idum;
00126 if (iy < 1) iy += IMM1;
00127 if ((temp=AM*iy) > RNMX) return RNMX;
00128 else return temp;
00129 
00130 }  /* end of HydroRan2.c */
00131 
00132 /*------------------------
00133  *  Start of HydroRan3.c
00134  *------------------------*/
00135 float hydroran3(long *idumb)
00136 {
00137 int jjj;
00138 long kkk;
00139 static long idum22=123456789;
00140 static long iy2=0;
00141 static long iv2[BNTAB];
00142 float temp2;
00143 
00144 /*----------------------------
00145  *  Initialize the generator
00146  *----------------------------*/
00147 if (*idumb <= 0) {
00148     if( -(*idumb) < 1 ) *idumb = 1;       /* prevent idum = 0 */
00149     else *idumb = -(*idumb);
00150     idum22=(*idumb);
00151     for (jjj=BNTAB+7; jjj>=0; jjj--) {    /* load the shuffle table */
00152          kkk=(*idumb)/IQ1;
00153          *idumb=IA1*(*idumb-kkk*IQ1)-kkk*IR1;
00154          if( *idumb < 0 ) *idumb += BIM1;
00155          if( jjj < BNTAB ){
00156          iv2[jjj] = *idumb;
00157          }
00158     }
00159     iy2 = iv2[0];
00160 }
00161 
00162 /*-----------------------
00163  *  Start the generator
00164  *-----------------------*/
00165 kkk = (*idumb)/BIQ1;
00166 *idumb = BIA1*(*idumb-kkk*BIQ1)-kkk*BIR1;
00167 if (*idumb < 0) *idumb += BIM1;
00168 kkk = idum22/IQ2;
00169 idum22 = BIA2*(idum22-kkk*BIQ2)-kkk*BIR2;
00170 if (idum22 < 0) idum22 += BIM2;
00171 jjj=iy2/BNDIV;
00172 iy2 = iv2[jjj]-idum22;
00173 iv2[jjj] = *idumb;
00174 if (iy2 < 1) iy2 += BIMM1;
00175 if ((temp2=BAM*iy2) > BRNMX) return BRNMX;
00176 else return temp2;
00177 
00178 }  /* end of HydroRan3.c */
00179 
00180 
00181 /*------------------------
00182  *  Start of HydroRan4.c
00183  *------------------------*/
00184 float hydroran4(long *idumc)
00185 {
00186 int j;
00187 long k;
00188 static long idum23=123456789;
00189 static long iy3=0;
00190 static long iv3[CNTAB];
00191 float temp3;
00192 
00193 /*----------------------------
00194  *  Initialize the generator
00195  *----------------------------*/
00196 if (*idumc <= 0) {
00197     if( -(*idumc) < 1 ) *idumc = 1;       /* prevent idum = 0 */
00198     else *idumc = -(*idumc);
00199     idum23=(*idumc);
00200     for (j=CNTAB+7; j>=0; j--) {          /* load the shuffle table */
00201          k=(*idumc)/CIQ1;
00202          *idumc=CIA1*(*idumc-k*CIQ1)-k*CIR1;
00203          if( *idumc < 0 ) *idumc += CIM1;
00204          if( j < CNTAB ){
00205          iv3[j] = *idumc;
00206          }
00207     }
00208     iy3 = iv3[0];
00209 }
00210 
00211 /*-----------------------
00212  *  Start the generator
00213  *-----------------------*/
00214 k = (*idumc)/CIQ1;
00215 *idumc = CIA1*(*idumc-k*CIQ1)-k*CIR1;
00216 if (*idumc < 0) *idumc += CIM1;
00217 k = idum23/CIQ2;
00218 idum23 = CIA2*(idum23-k*CIQ2)-k*CIR2;
00219 if (idum23 < 0) idum23 += CIM2;
00220 j=iy3/CNDIV;
00221 iy3 = iv3[j]-idum23;
00222 iv3[j] = *idumc;
00223 if (iy3 < 1) iy3 += CIMM1;
00224 if ((temp3=CAM*iy3) > CRNMX) return CRNMX;
00225 else return temp3;
00226 
00227 }  /* end of HydroRan4.c */
00228 
00229 
00230 /*------------------------
00231  *  Start of HydroRan5.c
00232  *------------------------*/
00233 float hydroran5(long *idumd)
00234 {
00235 int j;
00236 long k;
00237 static long idum5=123456789;
00238 static long iy5=0;
00239 static long iv5[DNTAB];
00240 float temp5;
00241 
00242 /*----------------------------
00243  *  Initialize the generator
00244  *----------------------------*/
00245 if (*idumd <= 0) {
00246     if( -(*idumd) < 1 ) *idumd = 1;       /* prevent idum = 0 */
00247     else *idumd = -(*idumd);
00248     idum5=(*idumd);
00249     for (j=DNTAB+7; j>=0; j--) {          /* load the shuffle table */
00250          k=(*idumd)/DIQ1;
00251          *idumd=DIA1*(*idumd-k*DIQ1)-k*DIR1;
00252          if( *idumd < 0 ) *idumd += DIM1;
00253          if( j < DNTAB ){
00254                  iv5[j] = *idumd;
00255          }
00256     }
00257     iy5 = iv5[0];
00258 }
00259 
00260 /*-----------------------
00261  *  Start the generator
00262  *-----------------------*/
00263 k = (*idumd)/DIQ1;
00264 *idumd = DIA1*(*idumd-k*DIQ1)-k*DIR1;
00265 if (*idumd < 0) *idumd += DIM1;
00266 k = idum5/DIQ2;
00267 idum5 = DIA2*(idum5-k*DIQ2)-k*DIR2;
00268 if (idum5 < 0) idum5 += DIM2;
00269 j=iy5/DNDIV;
00270 iy5 = iv5[j]-idum5;
00271 iv5[j] = *idumd;
00272 if (iy5 < 1) iy5 += DIMM1;
00273 if ((temp5=DAM*iy5) > DRNMX) return DRNMX;
00274 else return temp5;
00275 
00276 }  /* end of HydroRan5.c */
00277 

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