pihm/read_alloc.cpp

Go to the documentation of this file.
00001 /*******************************************************************************
00002  * File        : read_alloc.c                                                  *
00003  * Function    : read in and allocate memorey for IHM 1.0                      *
00004  * Programmer  : Yizhong Qu @ Pennsylvania State Univeristy                    *
00005  * Version     : May, 2004 (1.0)                                               * 
00006  *-----------------------------------------------------------------------------*
00007  *                                                                             *
00008  * In this file, 7 input files are readed in and dynamaticlly allocate memory  *
00009  * based on control parameters in the input files. Please refer to format.pdf  *
00010  * for details about the format of all 7 input files. All files has the same   *
00011  * file prefix provided in the command line when starting IHM1.0. Otherwise,   *
00012  * the default file prefix is "leaf". For example, "IHM example" will incur    *
00013  * reading in example.mesh, example.soil, etc.                                 *
00014  *                                                                             *
00015  * This code is free for users with research purpose only, if appropriate      *
00016  * citation is refered. However, there is no warranty in any format for this   *
00017  * product.                                                                    *
00018  *                                                                             *
00019  * For questions or comments, please contact the authors of the reference.     *
00020  * One who want to use it for other consideration may also contact Dr.Duffy    *
00021  * at cxd11@psu.edu.                                                           *    
00022  *******************************************************************************/
00023 
00024 #include <stdio.h>
00025 #include <stdlib.h>
00026 #include <math.h>
00027 #include <string.h>
00028 
00029 #include "sundials_types.h"
00030 #include "ihm.h"  
00031 #include "calib.h"
00032 
00033 //#define roughEle_CALIB 1.0
00034 //#define roughRiv_CALIB 1.0
00035 //#define rivCoeff_CALIB 2.0
00036 //#define rivDepth_CALIB 1.0
00037 //#define alpha_CALIB 1.0
00038 //#define set_MP        1.0
00039 //#define lai_CALIB     0.75
00040 //#define vegfrac_CALIB 0.75
00041 //#define albedo_CALIB  1.25
00042 
00043 realtype roughEle_CALIB;
00044 realtype roughRiv_CALIB;
00045 realtype rivCoeff_CALIB;
00046 realtype rivDepth_CALIB;
00047 realtype alpha_CALIB;
00048 realtype set_MP;
00049 realtype lai_CALIB;
00050 realtype vegfrac_CALIB;
00051 realtype albedo_CALIB;
00052 
00053 
00054 void read_alloc(char *filename, Model_Data DS, Control_Data *CS)
00055 {
00056   int i, j;
00057   int tempindex;
00058   
00059   int NumTout;
00060   char *fn[8];
00061   char tempchar[5];
00062   
00063   FILE *mesh_file;            /* Pointer to .mesh file */
00064   FILE *att_file;
00065   FILE *forc_file;
00066   FILE *ibc_file;
00067   FILE *soil_file;
00068   FILE *lc_file;
00069   FILE *para_file;
00070   FILE *riv_file;
00071 
00072   roughEle_CALIB=setroughEle_CALIB();
00073   roughRiv_CALIB=setroughRiv_CALIB();
00074   rivCoeff_CALIB=setrivCoeff_CALIB();
00075   rivDepth_CALIB=setrivDepth_CALIB();
00076   alpha_CALIB=setalpha_CALIB();
00077   set_MP=setset_MP();
00078   lai_CALIB=setlai_CALIB();
00079   vegfrac_CALIB=setvegfrac_CALIB();
00080   albedo_CALIB=setalbedo_CALIB();
00081   
00082 
00083   printf("\nStart reading in input files ... \n");
00084   
00085   /*========== open *.mesh file ==========*/
00086   printf("\n  1) reading %s.mesh ... ", filename);
00087   fn[0] = (char *)malloc((strlen(filename)+5)*sizeof(char));
00088   strcpy(fn[0], filename);
00089   mesh_file = fopen(strcat(fn[0], ".mesh"), "r");
00090 
00091   if(mesh_file == NULL)
00092   {
00093     printf("\n  Fatal Error: %s.mesh is in use or does not exist!\n", filename);
00094     exit(1);
00095   }
00096     
00097   /* start reading mesh_file */ 
00098   fscanf(mesh_file,"%d %d", &DS->NumEle, &DS->NumNode);
00099   
00100   DS->Ele = (element *)malloc(DS->NumEle*sizeof(element));
00101   DS->Node = (nodes *)malloc(DS->NumNode*sizeof(nodes));
00102   
00103   /* read in elements information */ 
00104   for (i=0; i<DS->NumEle; i++)
00105   {
00106     fscanf(mesh_file, "%d", &(DS->Ele[i].index));
00107     fscanf(mesh_file, "%d %d %d", &(DS->Ele[i].node[0]), &(DS->Ele[i].node[1]), &(DS->Ele[i].node[2]));
00108     fscanf(mesh_file, "%d %d %d", &(DS->Ele[i].nabr[0]), &(DS->Ele[i].nabr[1]), &(DS->Ele[i].nabr[2]));
00109   }
00110   
00111   /* read in nodes information */   
00112   for (i=0; i<DS->NumNode; i++)
00113   {
00114     fscanf(mesh_file, "%d", &(DS->Node[i].index));
00115     fscanf(mesh_file, "%lf %lf", &(DS->Node[i].x), &(DS->Node[i].y));
00116     fscanf(mesh_file, "%lf %lf", &(DS->Node[i].zmin),&(DS->Node[i].zmax));
00117   }  
00118   
00119   printf("done.\n");
00120   
00121   /* finish reading mesh_files */  
00122   fclose(mesh_file);
00123   
00124   /*========== open *.att file ==========*/
00125   printf("\n  2) reading %s.att  ... ", filename);
00126   fn[1] = (char *)malloc((strlen(filename)+4)*sizeof(char));
00127   strcpy(fn[1], filename);
00128   att_file = fopen(strcat(fn[1], ".att"), "r");
00129 
00130   if(att_file == NULL)
00131   {
00132     printf("\n  Fatal Error: %s.att is in use or does not exist!\n", filename);
00133     exit(1);
00134   }
00135     
00136   /* start reading att_file */ 
00137   DS->Ele_IC = (element_IC *)malloc(DS->NumEle*sizeof(element_IC));
00138   for (i=0; i<DS->NumEle; i++)
00139   {
00140     fscanf(att_file, "%d", &(tempindex));
00141     fscanf(att_file, "%d %d", &(DS->Ele[i].soil), &(DS->Ele[i].LC));
00142     fscanf(att_file, "%lf %lf %lf %lf %lf",&(DS->Ele_IC[i].interception),&(DS->Ele_IC[i].snow),&(DS->Ele_IC[i].surf),&(DS->Ele_IC[i].unsat),&(DS->Ele_IC[i].sat));
00143     fscanf(att_file, "%d", &(DS->Ele[i].BC));
00144     //DS->Ele[i].BC=0;
00145     fscanf(att_file, "%d %d", &(DS->Ele[i].prep), &(DS->Ele[i].temp));
00146     fscanf(att_file, "%d %d", &(DS->Ele[i].humidity), &(DS->Ele[i].WindVel));
00147     fscanf(att_file, "%d %d", &(DS->Ele[i].Rn), &(DS->Ele[i].G));
00148     fscanf(att_file, "%d %d", &(DS->Ele[i].pressure), &(DS->Ele[i].source));
00149  }
00150   
00151   printf("done.\n");
00152   
00153   /* finish reading mesh_files */  
00154   fclose(att_file);
00155     
00156   /*========== open *.soil file ==========*/  
00157   printf("\n  3) reading %s.soil ... ", filename);
00158   fn[2] = (char *)malloc((strlen(filename)+5)*sizeof(char));
00159   strcpy(fn[2], filename);
00160   soil_file = fopen(strcat(fn[2], ".soil"), "r");
00161   
00162   if(soil_file == NULL)
00163   {
00164     printf("\n  Fatal Error: %s.soil is in use or does not exist!\n", filename);
00165     exit(1);
00166   }
00167   
00168   /* start reading soil_file */  
00169   fscanf(soil_file, "%d", &DS->NumSoil);
00170   
00171   DS->Soil = (soils *)malloc(DS->NumSoil*sizeof(soils));
00172   
00173   for (i=0; i<DS->NumSoil; i++)
00174   {
00175     fscanf(soil_file, "%d", &(DS->Soil[i].index));
00176     fscanf(soil_file, "%lf", &(DS->Soil[i].Ksat));
00177     fscanf(soil_file, "%lf %lf", &(DS->Soil[i].SitaS), &(DS->Soil[i].SitaR));
00178     fscanf(soil_file, "%lf %lf", &(DS->Soil[i].Alpha), &(DS->Soil[i].Beta));
00179     fscanf(soil_file, "%d %lf %lf", &(DS->Soil[i].Macropore), &(DS->Soil[i].base), &(DS->Soil[i].gama));
00180     fscanf(soil_file, "%lf", &(DS->Soil[i].Sf));
00181     fscanf(soil_file, "%lf", &(DS->Soil[i].RzD));
00182     fscanf(soil_file, "%d", &(DS->Soil[i].Inf));
00183     DS->Soil[i].Alpha=alpha_CALIB*DS->Soil[i].Alpha;
00184     DS->Soil[i].Macropore=set_MP;
00185   } 
00186  
00187   fscanf(soil_file, "%d", &DS->NumInc);
00188   
00189   DS->TSD_Inc = (TSD *)malloc(DS->NumInc*sizeof(TSD));
00190   
00191   for(i=0; i<DS->NumInc; i++)
00192   {
00193     fscanf(soil_file, "%s %d %d", DS->TSD_Inc[i].name, &DS->TSD_Inc[i].index, 
00194                                   &DS->TSD_Inc[i].length);
00195  
00196     DS->TSD_Inc[i].TS = (realtype **)malloc(DS->TSD_Inc[i].length*sizeof(realtype));
00197     for(j=0; j<DS->TSD_Inc[i].length; j++)
00198     {
00199       DS->TSD_Inc[i].TS[j] = (realtype *)malloc(2*sizeof(realtype));
00200     }
00201     
00202     for(j=0; j<DS->TSD_Inc[i].length; j++)
00203     {
00204       fscanf(soil_file, "%lf %lf", &DS->TSD_Inc[i].TS[j][0], 
00205                                    &DS->TSD_Inc[i].TS[j][1]);
00206     }
00207   }
00208     
00209   fclose(soil_file);
00210   printf("done.\n");
00211       
00212 
00213   /*========== open *.lc file ==========*/  
00214   printf("\n  3) reading %s.lc ... ", filename);
00215   fn[3] = (char *)malloc((strlen(filename)+5)*sizeof(char));
00216   strcpy(fn[3], filename);
00217   lc_file = fopen(strcat(fn[3], ".lc"), "r");
00218   
00219   if(lc_file == NULL)
00220   {
00221     printf("\n  Fatal Error: %s.land cover is in use or does not exist!\n", filename);
00222     exit(1);
00223   }
00224   
00225   /* start reading land cover file */  
00226   fscanf(lc_file, "%d", &DS->NumLC);
00227   
00228   DS->LandC = (LC *)malloc(DS->NumLC*sizeof(LC));
00229   
00230   for (i=0; i<DS->NumLC; i++)
00231   {
00232     fscanf(lc_file, "%d", &(DS->LandC[i].index));
00233     fscanf(lc_file, "%lf", &(DS->LandC[i].LAImax));
00234     fscanf(lc_file, "%lf %lf", &(DS->LandC[i].Rmin), &(DS->LandC[i].Rs_ref));
00235     fscanf(lc_file, "%lf %lf", &(DS->LandC[i].Albedo), &(DS->LandC[i].VegFrac));
00236     DS->LandC[i].VegFrac=vegfrac_CALIB*DS->LandC[i].VegFrac;
00237     DS->LandC[i].Albedo=albedo_CALIB*DS->LandC[i].Albedo>1.0?1.0:albedo_CALIB*DS->LandC[i].Albedo;
00238     fscanf(lc_file, "%lf", &(DS->LandC[i].Rough));
00239     DS->LandC[i].Rough=roughEle_CALIB*DS->LandC[i].Rough;
00240   } 
00241  
00242   fclose(lc_file);
00243   printf("done.\n");
00244 
00245   /*========== open *.riv file ==========*/ 
00246   printf("\n  4) reading %s.riv  ... ", filename);
00247   fn[4] = (char *)malloc((strlen(filename)+4)*sizeof(char));
00248   strcpy(fn[4], filename);
00249   riv_file =  fopen(strcat(fn[4], ".riv"), "r");
00250   
00251   if(riv_file == NULL)
00252   {
00253     printf("\n  Fatal Error: %s.riv is in use or does not exist!\n", filename);
00254     exit(1);
00255   }
00256   
00257   /* start reading riv_file */ 
00258   fscanf(riv_file, "%d", &DS->NumRiv);
00259   
00260   DS->Riv = (river_segment *)malloc(DS->NumRiv*sizeof(river_segment));
00261   DS->Riv_IC = (river_IC *)malloc(DS->NumRiv*sizeof(river_IC));
00262   
00263   for (i=0; i<DS->NumRiv; i++)
00264   {
00265     fscanf(riv_file, "%d", &(DS->Riv[i].index));
00266     fscanf(riv_file, "%d %d", &(DS->Riv[i].FromNode), &(DS->Riv[i].ToNode));
00267     fscanf(riv_file, "%d", &(DS->Riv[i].down));
00268     fscanf(riv_file, "%d %d", &(DS->Riv[i].LeftEle), &(DS->Riv[i].RightEle));
00269     fscanf(riv_file, "%d %d", &(DS->Riv[i].shape), &(DS->Riv[i].material));
00270     fscanf(riv_file, "%d %d", &(DS->Riv[i].IC), &(DS->Riv[i].BC));  
00271     fscanf(riv_file, "%d", &(DS->Riv[i].reservoir));                        
00272   } 
00273   
00274   fscanf(riv_file, "%s %d", tempchar, &DS->NumRivShape);
00275   DS->Riv_Shape = (river_shape *)malloc(DS->NumRivShape*sizeof(river_shape));
00276   
00277   for (i=0; i<DS->NumRivShape; i++)
00278   {
00279     fscanf(riv_file, "%d %lf", &DS->Riv_Shape[i].index, &DS->Riv_Shape[i].width);
00280     fscanf(riv_file, "%lf %lf", &DS->Riv_Shape[i].depth, &DS->Riv_Shape[i].bed);
00281     fscanf(riv_file, "%d %lf",&DS->Riv_Shape[i].interpOrd,&DS->Riv_Shape[i].coeff);
00282         DS->Riv_Shape[i].depth = rivDepth_CALIB*DS->Riv_Shape[i].depth;
00283         DS->Riv_Shape[i].coeff = rivCoeff_CALIB*DS->Riv_Shape[i].coeff;
00284   }
00285   
00286   fscanf(riv_file, "%s %d", tempchar, &DS->NumRivMaterial);
00287   DS->Riv_Mat = (river_material *)malloc(DS->NumRivMaterial*sizeof(river_material));
00288   
00289   for (i=0; i<DS->NumRivMaterial; i++)
00290   {
00291     fscanf(riv_file, "%d %lf %lf %lf", &DS->Riv_Mat[i].index, &DS->Riv_Mat[i].Rough, &DS->Riv_Mat[i].Cwr, &DS->Riv_Mat[i].Sf);
00292         DS->Riv_Mat[i].Rough=roughRiv_CALIB*DS->Riv_Mat[i].Rough;
00293   }
00294   
00295   fscanf(riv_file, "%s %d", tempchar, &DS->NumRivIC);
00296   DS->Riv_IC = (river_IC *)malloc(DS->NumRivIC*sizeof(river_IC));
00297   
00298   for (i=0; i<DS->NumRivIC; i++)
00299   {
00300     fscanf(riv_file, "%d %lf", &DS->Riv_IC[i].index, &DS->Riv_IC[i].value);
00301   }
00302 
00303   fscanf(riv_file, "%s %d", tempchar, &DS->NumRivBC);
00304   DS->TSD_Riv = (TSD *)malloc(DS->NumRivBC*sizeof(TSD));
00305   
00306   for(i=0; i<DS->NumRivBC; i++)
00307   {
00308     fscanf(riv_file, "%s %d %d", DS->TSD_Riv[i].name, &DS->TSD_Riv[i].index, &DS->TSD_Riv[i].length);
00309     
00310     DS->TSD_Riv[i].TS = (realtype **)malloc((DS->TSD_Riv[i].length)*sizeof(realtype));
00311     for(j=0; j<DS->TSD_Riv[i].length; j++)
00312     {
00313       DS->TSD_Riv[i].TS[j] = (realtype *)malloc(2*sizeof(realtype));
00314     }
00315     
00316     for(j=0; j<DS->TSD_Riv[i].length; j++)
00317     {
00318       fscanf(riv_file, "%lf %lf", &DS->TSD_Riv[i].TS[j][0], &DS->TSD_Riv[i].TS[j][1]);
00319     }
00320   }  
00321   
00322   // read in reservoir information
00323   fscanf(riv_file, "%s %d", tempchar, &DS->NumRes);
00324   if(DS->NumRes > 0)
00325   {
00326     /* read in reservoir information */
00327     
00328   }
00329   
00330   fclose(riv_file);
00331   printf("done.\n");
00332   
00333   /*========== open *.forc file ==========*/  
00334   printf("\n  5) reading %s.forc ... ", filename);
00335   fn[5] = (char *)malloc((strlen(filename)+5)*sizeof(char));
00336   strcpy(fn[5], filename);
00337   forc_file = fopen(strcat(fn[5], ".forc"), "r");
00338   
00339   if(forc_file == NULL)
00340   {
00341     printf("\n  Fatal Error: %s.forc is in use or does not exist!\n", filename);
00342     exit(1);
00343   }
00344   
00345   /* start reading forc_file */
00346   fscanf(forc_file, "%d %d", &DS->NumPrep, &DS->NumTemp);
00347   fscanf(forc_file, "%d %d", &DS->NumHumidity, &DS->NumWindVel);
00348   fscanf(forc_file, "%d %d", &DS->NumRn, &DS->NumG);
00349   fscanf(forc_file, "%d %d", &DS->NumP, &DS->NumLC);
00350   fscanf(forc_file, "%d", &DS->NumMeltF);
00351   fscanf(forc_file, "%d", &DS->NumSource);
00352   
00353   DS->TSD_Prep = (TSD *)malloc(DS->NumPrep*sizeof(TSD));
00354   DS->TSD_Temp = (TSD *)malloc(DS->NumTemp*sizeof(TSD));
00355   DS->TSD_Humidity = (TSD *)malloc(DS->NumHumidity*sizeof(TSD));
00356   DS->TSD_WindVel = (TSD *)malloc(DS->NumWindVel*sizeof(TSD));
00357   DS->TSD_Rn = (TSD *)malloc(DS->NumRn*sizeof(TSD));
00358   DS->TSD_G = (TSD *)malloc(DS->NumG*sizeof(TSD));
00359   DS->TSD_Pressure = (TSD *)malloc(DS->NumP*sizeof(TSD));
00360   DS->TSD_LAI = (TSD *)malloc(DS->NumLC*sizeof(TSD));
00361   DS->TSD_DH = (TSD *)malloc(DS->NumLC*sizeof(TSD));  
00362   DS->TSD_MeltF = (TSD *)malloc(DS->NumMeltF*sizeof(TSD));
00363   DS->TSD_Source = (TSD *)malloc(DS->NumSource*sizeof(TSD));
00364   
00365   DS->SIFactor = (realtype *)malloc(DS->NumLC*sizeof(realtype));
00366      
00367   for(i=0; i<DS->NumPrep; i++)
00368   {
00369     fscanf(forc_file, "%s %d %d", DS->TSD_Prep[i].name, &DS->TSD_Prep[i].index, &DS->TSD_Prep[i].length);
00370     
00371     DS->TSD_Prep[i].TS = (realtype **)malloc((DS->TSD_Prep[i].length)*sizeof(realtype));
00372     
00373     for(j=0; j<DS->TSD_Prep[i].length; j++)
00374     {
00375       DS->TSD_Prep[i].TS[j] = (realtype *)malloc(2*sizeof(realtype));
00376     }
00377     
00378     for(j=0; j<DS->TSD_Prep[i].length; j++)
00379     {
00380       fscanf(forc_file, "%lf %lf", &DS->TSD_Prep[i].TS[j][0], &DS->TSD_Prep[i].TS[j][1]);
00381     }
00382         DS->TSD_Prep[i].iCounter=0;
00383   }  
00384   
00385   for(i=0; i<DS->NumTemp; i++)
00386   {
00387     fscanf(forc_file, "%s %d %d", DS->TSD_Temp[i].name, &DS->TSD_Temp[i].index, &DS->TSD_Temp[i].length);
00388     
00389     DS->TSD_Temp[i].TS = (realtype **)malloc((DS->TSD_Temp[i].length)*sizeof(realtype));
00390     
00391     for(j=0; j<DS->TSD_Temp[i].length; j++)
00392     {
00393       DS->TSD_Temp[i].TS[j] = (realtype *)malloc(2*sizeof(realtype));
00394     }
00395     
00396     for(j=0; j<DS->TSD_Temp[i].length; j++)
00397     {
00398       fscanf(forc_file, "%lf %lf", &DS->TSD_Temp[i].TS[j][0], &DS->TSD_Temp[i].TS[j][1]);
00399     }
00400         DS->TSD_Temp[i].iCounter=0;
00401   } 
00402   
00403   for(i=0; i<DS->NumHumidity; i++)
00404   {
00405     fscanf(forc_file, "%s %d %d", DS->TSD_Humidity[i].name, &DS->TSD_Humidity[i].index, &DS->TSD_Humidity[i].length);
00406     
00407     DS->TSD_Humidity[i].TS = (realtype **)malloc((DS->TSD_Humidity[i].length)*sizeof(realtype));
00408     
00409     for(j=0; j<DS->TSD_Humidity[i].length; j++)
00410     {
00411       DS->TSD_Humidity[i].TS[j] = (realtype *)malloc(2*sizeof(realtype));
00412     }
00413     
00414     for(j=0; j<DS->TSD_Humidity[i].length; j++)
00415     {
00416       fscanf(forc_file, "%lf %lf", &DS->TSD_Humidity[i].TS[j][0], &DS->TSD_Humidity[i].TS[j][1]);
00417         DS->TSD_Humidity[i].TS[j][1] = (DS->TSD_Humidity[i].TS[j][1] > 1.0?1.0:DS->TSD_Humidity[i].TS[j][1]);
00418     }
00419         DS->TSD_Humidity[i].iCounter=0;
00420   } 
00421   
00422   for(i=0; i<DS->NumWindVel; i++)
00423   {
00424     fscanf(forc_file, "%s %d %d", DS->TSD_WindVel[i].name, &DS->TSD_WindVel[i].index, &DS->TSD_WindVel[i].length);
00425     
00426     DS->TSD_WindVel[i].TS = (realtype **)malloc((DS->TSD_WindVel[i].length)*sizeof(realtype));
00427     
00428     for(j=0; j<DS->TSD_WindVel[i].length; j++)
00429     {
00430       DS->TSD_WindVel[i].TS[j] = (realtype *)malloc(2*sizeof(realtype));
00431     }
00432     
00433     for(j=0; j<DS->TSD_WindVel[i].length; j++)
00434     {
00435       fscanf(forc_file, "%lf %lf", &DS->TSD_WindVel[i].TS[j][0], &DS->TSD_WindVel[i].TS[j][1]);
00436     }
00437         DS->TSD_WindVel[i].iCounter=0;
00438   } 
00439 
00440   for(i=0; i<DS->NumRn; i++)
00441   {
00442     fscanf(forc_file, "%s %d %d", DS->TSD_Rn[i].name, &DS->TSD_Rn[i].index, &DS->TSD_Rn[i].length);
00443     
00444     DS->TSD_Rn[i].TS = (realtype **)malloc((DS->TSD_Rn[i].length)*sizeof(realtype));
00445     
00446     for(j=0; j<DS->TSD_Rn[i].length; j++)
00447     {
00448       DS->TSD_Rn[i].TS[j] = (realtype *)malloc(2*sizeof(realtype));
00449     }
00450     
00451     for(j=0; j<DS->TSD_Rn[i].length; j++)
00452     {
00453       fscanf(forc_file, "%lf %lf", &DS->TSD_Rn[i].TS[j][0], &DS->TSD_Rn[i].TS[j][1]);
00454     }
00455         DS->TSD_Rn[i].iCounter=0;
00456   } 
00457 
00458   for(i=0; i<DS->NumG; i++)
00459   {
00460     fscanf(forc_file, "%s %d %d", DS->TSD_G[i].name, &DS->TSD_G[i].index, &DS->TSD_G[i].length);
00461     
00462     DS->TSD_G[i].TS = (realtype **)malloc((DS->TSD_G[i].length)*sizeof(realtype));
00463     
00464     for(j=0; j<DS->TSD_G[i].length; j++)
00465     {
00466       DS->TSD_G[i].TS[j] = (realtype *)malloc(2*sizeof(realtype));
00467     }
00468     
00469     for(j=0; j<DS->TSD_G[i].length; j++)
00470     {
00471       fscanf(forc_file, "%lf %lf", &DS->TSD_G[i].TS[j][0], &DS->TSD_G[i].TS[j][1]);
00472     }
00473         DS->TSD_G[i].iCounter=0;
00474   } 
00475 
00476   for(i=0; i<DS->NumP; i++)
00477   {
00478     fscanf(forc_file, "%s %d %d", DS->TSD_Pressure[i].name, &DS->TSD_Pressure[i].index, &DS->TSD_Pressure[i].length);
00479     
00480     DS->TSD_Pressure[i].TS = (realtype **)malloc((DS->TSD_Pressure[i].length)*sizeof(realtype));
00481     
00482     for(j=0; j<DS->TSD_Pressure[i].length; j++)
00483     {
00484       DS->TSD_Pressure[i].TS[j] = (realtype *)malloc(2*sizeof(realtype));
00485     }
00486     
00487     for(j=0; j<DS->TSD_Pressure[i].length; j++)
00488     {
00489       fscanf(forc_file, "%lf %lf", &DS->TSD_Pressure[i].TS[j][0], &DS->TSD_Pressure[i].TS[j][1]);
00490     }
00491         DS->TSD_Pressure[i].iCounter=0;
00492   } 
00493 
00494   for(i=0; i<DS->NumLC; i++)
00495   {
00496     fscanf(forc_file, "%s %d %d %lf", DS->TSD_LAI[i].name, &DS->TSD_LAI[i].index, &DS->TSD_LAI[i].length, &DS->SIFactor[i]);
00497     
00498     DS->TSD_LAI[i].TS = (realtype **)malloc((DS->TSD_LAI[i].length)*sizeof(realtype));
00499     
00500     for(j=0; j<DS->TSD_LAI[i].length; j++)
00501     {
00502       DS->TSD_LAI[i].TS[j] = (realtype *)malloc(2*sizeof(realtype));
00503     }
00504     
00505     for(j=0; j<DS->TSD_LAI[i].length; j++)
00506     {
00507       fscanf(forc_file, "%lf %lf", &DS->TSD_LAI[i].TS[j][0], &DS->TSD_LAI[i].TS[j][1]);
00508       DS->TSD_LAI[i].TS[j][1]=lai_CALIB*DS->TSD_LAI[i].TS[j][1];
00509     }
00510         DS->TSD_LAI[i].iCounter=0;
00511   } 
00512 
00513   for(i=0; i<DS->NumLC; i++)
00514   {
00515     fscanf(forc_file, "%s %d %d", DS->TSD_DH[i].name, &DS->TSD_DH[i].index, &DS->TSD_DH[i].length);
00516     
00517     DS->TSD_DH[i].TS = (realtype **)malloc((DS->TSD_DH[i].length)*sizeof(realtype));
00518     
00519     for(j=0; j<DS->TSD_DH[i].length; j++)
00520     {
00521       DS->TSD_DH[i].TS[j] = (realtype *)malloc(2*sizeof(realtype));
00522     }
00523     
00524     for(j=0; j<DS->TSD_DH[i].length; j++)
00525     {
00526       fscanf(forc_file, "%lf %lf", &DS->TSD_DH[i].TS[j][0], &DS->TSD_DH[i].TS[j][1]);
00527     }
00528         DS->TSD_DH[i].iCounter=0;
00529   } 
00530   
00531   for(i=0; i<DS->NumMeltF; i++)
00532   {
00533     fscanf(forc_file, "%s %d %d", DS->TSD_MeltF[i].name, &DS->TSD_MeltF[i].index, &DS->TSD_MeltF[i].length);
00534     
00535     DS->TSD_MeltF[i].TS = (realtype **)malloc((DS->TSD_MeltF[i].length)*sizeof(realtype));
00536     
00537     for(j=0; j<DS->TSD_MeltF[i].length; j++)
00538     {
00539       DS->TSD_MeltF[i].TS[j] = (realtype *)malloc(2*sizeof(realtype));
00540     }
00541     
00542     for(j=0; j<DS->TSD_MeltF[i].length; j++)
00543     {
00544       fscanf(forc_file, "%lf %lf", &DS->TSD_MeltF[i].TS[j][0], &DS->TSD_MeltF[i].TS[j][1]);
00545     }
00546         DS->TSD_MeltF[i].iCounter=0;
00547   } 
00548   
00549   for(i=0; i<DS->NumSource; i++)
00550   {
00551     fscanf(forc_file, "%s %d %d", DS->TSD_Source[i].name, &DS->TSD_Source[i].index, &DS->TSD_Source[i].length);
00552     
00553     DS->TSD_Source[i].TS = (realtype **)malloc((DS->TSD_Source[i].length)*sizeof(realtype));
00554     
00555     for(j=0; j<DS->TSD_Source[i].length; j++)
00556     {
00557       DS->TSD_Source[i].TS[j] = (realtype *)malloc(2*sizeof(realtype));
00558     }
00559     
00560     for(j=0; j<DS->TSD_Source[i].length; j++)
00561     {
00562       fscanf(forc_file, "%lf %lf", &DS->TSD_Source[i].TS[j][0], &DS->TSD_Source[i].TS[j][1]);
00563     }
00564         DS->TSD_Source[i].iCounter=0;
00565   } 
00566 
00567   fclose(forc_file);
00568   printf("done.\n");
00569       
00570   /*========== open *.ibc file ==========*/     
00571   printf("\n  6) reading %s.ibc  ... ", filename);  
00572   fn[6] = (char *)malloc((strlen(filename)+4)*sizeof(char));
00573   strcpy(fn[6], filename);
00574   ibc_file =  fopen(strcat(fn[6], ".ibc"), "r");
00575   
00576   if(ibc_file == NULL)
00577   {
00578     printf("\n  Fatal Error: %s.ibc is in use or does not exist!\n", filename);
00579     exit(1);
00580   }
00581   
00582   /* start reading ibc_file */
00583   fscanf(ibc_file, "%d %d", &DS->Num1BC, &DS->Num2BC);
00584   
00585   if(DS->Num1BC+DS->Num2BC > 0)
00586   {
00587     DS->TSD_EleBC = (TSD *)malloc((DS->Num1BC+DS->Num2BC)*sizeof(TSD));
00588   }
00589   
00590   if(DS->Num1BC>0)
00591   {
00592     /* For elements with Dirichilet Boundary Conditions */
00593     /* This part of code has not be tested ! */
00594     for(i=0; i<DS->Num1BC; i++)
00595     {
00596       fscanf(ibc_file, "%s %d %d", DS->TSD_EleBC[i].name, &DS->TSD_EleBC[i].index, 
00597                                    &DS->TSD_EleBC[i].length);
00598       
00599       DS->TSD_EleBC[i].TS = (realtype **)malloc((DS->TSD_EleBC[i].length)*sizeof(realtype));
00600       
00601       for(j=0; j<DS->TSD_EleBC[i].length; j++)
00602       {
00603         DS->TSD_EleBC[i].TS[j] = (realtype *)malloc(2*sizeof(realtype));
00604       }
00605       
00606       for(j=0; j<DS->TSD_EleBC[i].length; j++)
00607       {
00608         fscanf(forc_file, "%lf %lf", &DS->TSD_EleBC[i].TS[j][0], 
00609                                    &DS->TSD_EleBC[i].TS[j][1]);
00610       }
00611     }    
00612   }
00613   
00614   if(DS->Num2BC>0)
00615   {
00616     /* For elements with Nueman (non-natural) Boundary Conditions */
00617     /* This part of code has not be tested ! */  
00618     for(i=DS->Num1BC; i<DS->Num1BC+DS->Num2BC; i++)
00619     {
00620       fscanf(ibc_file, "%s %d %d", DS->TSD_EleBC[i].name, &DS->TSD_EleBC[i].index, 
00621                                    &DS->TSD_EleBC[i].length);
00622       
00623       DS->TSD_EleBC[i].TS = (realtype **)malloc((DS->TSD_EleBC[i].length)*sizeof(realtype));
00624       
00625       for(j=0; j<DS->TSD_EleBC[i].length; j++)
00626       {
00627         DS->TSD_EleBC[i].TS[j] = (realtype *)malloc(2*sizeof(realtype));
00628       }
00629       
00630       for(j=0; j<DS->TSD_EleBC[i].length; j++)
00631       {
00632         fscanf(forc_file, "%lf %lf", &DS->TSD_EleBC[i].TS[j][0], 
00633                                    &DS->TSD_EleBC[i].TS[j][1]);
00634       }
00635     }     
00636   }
00637   
00638   fscanf(ibc_file, "%d", &DS->NumEleIC);
00639 /*  DS->Ele_IC = (element_IC *)malloc(DS->NumEleIC*sizeof(element_IC));
00640   
00641   for(i=0; i<DS->NumEleIC; i++)
00642   {
00643     fscanf(ibc_file, "%d", &DS->Ele_IC[i].index);
00644     fscanf(ibc_file, "%lf", &DS->Ele_IC[i].interception);
00645     fscanf(ibc_file, "%lf", &DS->Ele_IC[i].snow);    
00646     fscanf(ibc_file, "%lf", &DS->Ele_IC[i].surf);
00647     fscanf(ibc_file, "%lf", &DS->Ele_IC[i].unsat);
00648     fscanf(ibc_file, "%lf", &DS->Ele_IC[i].sat);
00649   } 
00650 */  
00651   fclose(ibc_file);
00652   printf("done.\n");
00653   
00654   /*========== open *.para file ==========*/ 
00655   printf("\n  7) reading %s.para ... ", filename); 
00656   fn[7] = (char *)malloc((strlen(filename)+5)*sizeof(char));
00657   strcpy(fn[7], filename);
00658   para_file = fopen(strcat(fn[7], ".para"), "r");  
00659   
00660   if(para_file == NULL)
00661   {
00662     printf("\n  Fatal Error: %s.para is in use or does not exist!\n", filename);
00663     exit(1);
00664   }
00665   
00666   /* start reading para_file */
00667   fscanf(para_file, "%d %d", &CS->Verbose, &CS->Debug);
00668   fscanf(para_file, "%d", &CS->int_type);
00669   fscanf(para_file, "%d %d %d %d", &CS->res_out, &CS->flux_out, &CS->q_out, &CS->etis_out);
00670   fscanf(para_file, "%d %d %d", &DS->UnsatMode, &DS->SurfMode, &DS->RivMode);
00671   fscanf(para_file, "%d", &CS->Solver);
00672   if(CS->Solver == 2)
00673   {
00674     fscanf(para_file, "%d %d %lf", &CS->GSType, &CS->MaxK, &CS->delt);
00675   }
00676   fscanf(para_file, "%lf %lf", &CS->abstol, &CS->reltol);
00677   fscanf(para_file, "%lf %lf %lf", &CS->InitStep, &CS->MaxStep, &CS->ETStep);
00678   fscanf(para_file, "%lf %lf %d", &CS->StartTime, &CS->EndTime, &CS->outtype);
00679   if(CS->outtype == 0)
00680   {
00681     fscanf(para_file, "%lf %lf", &CS->a, &CS->b);
00682   }
00683   
00684   if(CS->a != 1.0)
00685   {
00686     NumTout = (int)(log(1 - (CS->EndTime - CS->StartTime)*(1 -  CS->a)/CS->b)/log(CS->a));
00687   }
00688   else
00689   {
00690     if((CS->EndTime - CS->StartTime)/CS->b - 
00691        ((int) (CS->EndTime - CS->StartTime)/CS->b) > 0)
00692     {
00693       NumTout = (int) ((CS->EndTime - CS->StartTime)/CS->b);
00694     }
00695     else
00696     {
00697       NumTout = (int) ((CS->EndTime - CS->StartTime)/CS->b - 1);
00698     }  
00699   }
00700   
00701   CS->NumSteps = NumTout + 1;
00702   
00703   CS->Tout = (realtype *)malloc((CS->NumSteps + 1)*sizeof(realtype));
00704     
00705   for(i=0; i<CS->NumSteps+1; i++)
00706   {
00707     if(i == 0)
00708     {
00709       CS->Tout[i] = CS->StartTime;
00710     }
00711     else
00712     {
00713       CS->Tout[i] = CS->Tout[i-1] + pow(CS->a, i)*CS->b;
00714     }  
00715   }
00716   
00717   if(CS->Tout[CS->NumSteps] < CS->EndTime)
00718   {
00719     CS->Tout[CS->NumSteps] = CS->EndTime;
00720   }
00721   
00722   fclose(para_file); 
00723   printf("done.\n"); 
00724 }
00725 

Generated on Sun Aug 5 17:33:59 2007 for PIHMgis by  doxygen 1.5.2