ReGen.cpp

00001 /*
00002  *  ReGen.cpp
00003  *  ReGen 1.0.1
00004  *
00005  *  Created by Martin Koechy on Thu Feb 06 2003.
00006  *
00007  */
00008 #include "ReGen.h"
00009 #include <math.h>
00010 
00012 RAINPARAMETERS::RAINPARAMETERS (int MAP, float dDMR)
00013 {
00014   calcParameters (MAP, dDMR);
00015 }
00016 
00018 RAINPARAMETERS::RAINPARAMETERS (float A, float L, float W, float a, float l, float w, float dDMR)
00019 {
00020   lA=A; lL=L; lW=W; vA=a; vL=l; vW=w; d=dDMR;
00021 }
00022 
00024 void RAINPARAMETERS::calcParameters (float MAP, float d) 
00025 { 
00026   // Parameter values based on monthly units
00027   
00028   // rain occurance:
00029   lA = 0.130034 + 0.000408 * MAP;
00030   lA = lA*(1-d +1.33*d*d-(0.61+1.57*lA)*d*d*d);
00031   lL = 5.735; // 5.235 + 0.5 months
00032   lW = 1.704746 + 0.000242 * MAP;
00033   
00034   // rain volume:
00035   vA = -13.7178 + 4.0508301 * log(MAP);
00036   vA = vA * (1+d);
00037   vL = 5.586; // 5.086 + 0.5 months
00038   vW = 11.336;
00039 }
00040 
00041 
00042 //------------------------------------------------------------------
00043 ReGen::ReGen (RAINPARAMETERS* p_RainP)
00044 {   
00045   pP = p_RainP;
00046   theAnnualRain = 0.0;
00047   
00048   float days[days_in_year];
00049   for (int d = 0; d < days_in_year; days[d] = d++);
00050   valarray <float> localDays (days, days_in_year);
00051   theDays.resize(days_in_year);
00052   theDays = localDays;
00053   
00054   rainProb.resize(days_in_year);
00055 
00056   meanRainVolume.resize(days_in_year);
00057   meanRainVolume = calcGauss(pP->vA, pP->vL, pP->vW, 4);
00058 
00059   rainyDay.resize(days_in_year);
00060   
00061   rainVolume.resize(days_in_year);
00062 }
00063 
00064 //------------------------------------------------------------------
00065 void ReGen::drawYear (void)
00066 {
00067   calcRain(); 
00068   theAnnualRain = rainVolume.sum(); // calculate right away, reuse later
00069 }
00070 
00071 //------------------------------------------------------------------
00072 valarray <float> ReGen::calcGauss (float A, float L, float W, int s)
00073 {
00074   // A: amplitude, L: location, W: width, s: shape
00075   valarray <float> G (days_in_year);
00076   G = theDays;
00077   
00078   G *= (12.0F/365.0F);
00079   G += (6.0F - L);
00080   for (int i = 0; i < days_in_year; i++) //  .apply does not work
00081         G[i] = fmod(G[i],12.0F);
00082   G += (L - 6.0F);
00083   
00084   G = A * exp(-pow(G - L, float(s))/float(2.0*W*W));
00085 
00086   return G;
00087 }
00088 
00089 //------------------------------------------------------------------
00090 void ReGen::calcRain (void)
00091 {   
00092   // calculate rainy days
00093   rainProb = calcGauss(pP->lA, pP->lL, pP->lW);
00094 
00095   rainyDay=0.0;
00096   for (int i = 0; i < days_in_year; i++)
00097         if (Zzg.rand_halfclosed01() < rainProb[i] && rainProb[i] > 0.05)
00098           rainyDay[i] = 1.0;
00099  
00100   
00101   // calculate rain volume on a specific day
00102   for (int i = 0; i < days_in_year; i++)
00103         rainVolume[i] = Zzg.rand_halfclosed01();
00104   rainVolume *= -1.0;
00105   rainVolume += 1.0;
00106   rainVolume = -log(rainVolume);
00107   
00108   rainVolume *= meanRainVolume;
00109   rainVolume *= rainyDay;
00110 }
00111 
00112 //------------------------------------------------------------------
00113 float ReGen::getRain (int day) const
00114 {
00115   return rainVolume[day];
00116 }
00117 
00118 //------------------------------------------------------------------
00119 float ReGen::getAnnualRain (void) const
00120 {
00121   return theAnnualRain;
00122 }
00123 
00124 //------------------------------------------------------------------
00125 
00126 float ReGen::getPeriodRain (int start, int end) const
00127 {
00128   float sum = 0.0;
00129   for (int day = start; day <= end; day++)
00130         sum+=rainVolume[day];
00131   return sum;
00132 }

Generated on Tue May 1 23:39:16 2007 for ReGen by  doxygen 1.5.1