Main Page   Compound List   File List   Compound Members   File Members  

FuelModel.c

Go to the documentation of this file.
00001 
00025 #include "FuelModel.h"
00026 
00027 /*
00028  *********************************************************
00029  * NON PUBLIC FUNCTIONS
00030  *********************************************************
00031  */
00032 
00033 int FuelModelInitRothFuelModel(FuelModel * fm, char * fmd_fname);
00034 
00035 int FuelModelInitRothFuelModelUnBurnable(FuelModel * fm);
00036 
00037 int FuelModelInitPhysFuelModel(FuelModel * fm, char * fmd_fname);
00038 
00039 int FuelModelInitPhysFuelModelUnBurnable(FuelModel * fm);
00040     
00041 FuelModel * InitFuelModelEmpty(int fm_num, char * fm_name, char * fm_desc)  {
00042     FuelModel * fm = NULL;
00043     
00044     if ( (fm = (FuelModel *) malloc(sizeof(FuelModel))) == NULL )   {
00045         ERR_ERROR_CONTINUE("Unable to allocate memory for FuelModel. \n", ERR_ENOMEM);
00046         return fm;
00047         }
00048 
00049     /* assign model number */
00050     fm->model_num = fm_num;
00051 
00052     /* assign model name */
00053     if ( fm_name != NULL )  {
00054         if ( (fm->model_name = (char *) malloc(sizeof(char) * (strlen(fm_name) + 1))) != NULL ) {
00055             strcpy(fm->model_name, fm_name);
00056             }
00057         else    {
00058             ERR_ERROR_CONTINUE("Unable to allocate memory for FuelModel name. \n", ERR_ENOMEM);
00059             fm->model_name = NULL;      
00060             }
00061         }
00062     else    {
00063         if ( (fm->model_name = (char *) malloc(sizeof(char) * (strlen(FUELMODEL_UNNAMED_MODEL) + 1))) != NULL ) {
00064             strcpy(fm->model_name, FUELMODEL_UNNAMED_MODEL);
00065             }
00066         else    {
00067             ERR_ERROR_CONTINUE("Unable to allocate memory for FuelModel name. \n", ERR_ENOMEM);
00068             fm->model_name = NULL;      
00069             }
00070         }
00071 
00072     /* assign model description */
00073     if ( fm_desc != NULL )  {
00074         if ( (fm->model_desc = (char *) malloc(sizeof(char) * (strlen(fm_desc) + 1))) != NULL ) {
00075             strcpy(fm->model_desc, fm_desc);
00076             }
00077         else    {
00078             ERR_ERROR_CONTINUE("Unable to allocate memory for FuelModel description. \n", ERR_ENOMEM);
00079             fm->model_desc = NULL;
00080             }
00081         }
00082     else    {
00083         fm->model_desc = NULL;
00084         }
00085     
00086     /* assign unknown fuel model type */
00087     fm->type = EnumUnknownFuelModelType;
00088     
00089     /* NULL any other members */
00090     fm->rfm = NULL;
00091     fm->pfm = NULL;
00092     
00093     return fm;
00094     }
00095     
00096 FuelModel * InitFuelModelFMDFile(int fm_num, char * fm_name, char * fm_desc, char * fmd_fname, FuelModelType type)  {
00097     FuelModel * fm = NULL;
00098     if ( (fm = InitFuelModelEmpty(fm_num, fm_name, fm_desc)) != NULL )  {
00099         switch(type)    {
00100             case EnumRoth:  
00101                 if ( FuelModelInitRothFuelModel(fm, fmd_fname) )    {
00102                     ERR_ERROR_CONTINUE("Unable to initialize RothFuelModel from FMD File. \n", ERR_EFAILED);
00103                     FreeFuelModel(fm);
00104                     fm = NULL;
00105                     }
00106                 break;
00107             case EnumPhys:
00108                 if ( FuelModelInitPhysFuelModel(fm, fmd_fname) )    {
00109                     ERR_ERROR_CONTINUE("Unable to initialize PhysFuelModel from FMD File. \n", ERR_EFAILED);
00110                     FreeFuelModel(fm);
00111                     fm = NULL;
00112                     }           
00113                 break;
00114             default:
00115                 ERR_ERROR_CONTINUE("Must specify FuelModelType to initialize FuelModel. \n", ERR_EFAILED);
00116                 FreeFuelModel(fm);
00117                 fm = NULL;          
00118                 break;
00119             }
00120         }
00121     return fm;
00122     }
00123 
00124 FuelModel * InitFuelModelUnBurnable(int fm_num, char * fm_name, char * fm_desc, FuelModelType type) {
00125     FuelModel * fm = NULL;
00126     if ( (fm = InitFuelModelEmpty(fm_num, fm_name, fm_desc)) != NULL )  {
00127         switch(type)    {
00128             case EnumRoth:
00129                 if ( FuelModelInitRothFuelModelUnBurnable(fm) ) {
00130                     ERR_ERROR_CONTINUE("Unable to initialize RothFuelModel as unburnable. \n", ERR_EFAILED);
00131                     FreeFuelModel(fm);
00132                     fm = NULL;          
00133                     }
00134                 break;
00135             case EnumPhys:
00136                 if ( FuelModelInitPhysFuelModelUnBurnable(fm) ) {
00137                     ERR_ERROR_CONTINUE("Unable to initialize PhysFuelModel as unburnable. \n", ERR_EFAILED);
00138                     FreeFuelModel(fm);
00139                     fm = NULL;          
00140                     }           
00141                 break;
00142             default:
00143                 ERR_ERROR_CONTINUE("Must specify FuelModelType to initialize FuelModel. \n", ERR_EFAILED);
00144                 FreeFuelModel(fm);
00145                 fm = NULL;              
00146                 break;
00147             }
00148         }
00149     return fm;
00150     }
00151 
00152 /*
00153  * Description:
00154  * Initializes the RothFuelModel member of the FuelModel structure with attributes from an .FMD file.
00155  *
00156  * Arguments:
00157  * fm- ptr to initialized FuelModel
00158  *
00159  * Returns:
00160  * ERR_SUCCESS (0) if operation successful, an error code otherwise.
00161  * Best use of this facility is as follows...
00162  * int error_status = CallFunctionXXX();
00163  * if ( error_status)  something bad happened
00164  */ 
00165 int FuelModelInitRothFuelModel(FuelModel * fm, char * fmd_fname)    {
00166     FILE * fstream;
00167     
00168     /* check args */
00169     if ( fm == NULL )   {
00170         ERR_ERROR("FuelModel must be initialized before RothFuelModel attributes. \n", ERR_EINVAL);
00171         }
00172     if ( (fmd_fname != NULL ) && ((fstream = fopen(fmd_fname, "r")) != NULL ) ) {
00173         if ( (fm->rfm = InitRothFuelModelFMDFile(fstream, fm->model_num)) == NULL )     {
00174             fclose(fstream);
00175             ERR_ERROR("Unable to intialize RothFuelModel attributes in FuelModel. \n", ERR_EBADFUNC);
00176             }
00177         }
00178     else    {
00179         ERR_ERROR("Unable to open stream to fmd file for RothFuelModel attributes. \n", ERR_EINVAL);
00180         }
00181     
00182     fclose(fstream);
00183     fm->type = EnumRoth;
00184     
00185     return ERR_SUCCESS;
00186     }
00187 
00188 /*
00189  * Description:
00190  * Initializes the RothFuelModel member of the FuelModel structure as unburnable fuel.
00191  *
00192  * Arguments:
00193  * fm- ptr to initialized FuelModel
00194  *
00195  * Returns:
00196  * ERR_SUCCESS (0) if operation successful, an error code otherwise.
00197  * Best use of this facility is as follows...
00198  * int error_status = CallFunctionXXX();
00199  * if ( error_status)  something bad happened
00200  */ 
00201 int FuelModelInitRothFuelModelUnBurnable(FuelModel * fm)    {
00202     /* check args */
00203     if ( fm == NULL )   {
00204         ERR_ERROR("FuelModel must be initialized before RothFuelModel attributes. \n", ERR_EINVAL);
00205         }
00206         
00207     fm->rfm = InitRothFuelModelUnBurnable();
00208     fm->type = EnumRoth;
00209 
00210     return ERR_SUCCESS;
00211     }
00212 
00213 /*
00214  * Description:
00215  * Initializes the PhysFuelModel member of the FuelModel structure with attributes from an .FMD file.
00216  *
00217  * Arguments:
00218  * fm- ptr to initialized FuelModel
00219  *
00220  * Returns:
00221  * ERR_SUCCESS (0) if operation successful, an error code otherwise.
00222  * Best use of this facility is as follows...
00223  * int error_status = CallFunctionXXX();
00224  * if ( error_status)  something bad happened
00225  */ 
00226 int FuelModelInitPhysFuelModel(FuelModel * fm, char * fmd_fname)    {
00227     FILE * fstream;
00228     
00229     /* check args */
00230     if ( fm == NULL )   {
00231         ERR_ERROR("FuelModel must be initialized before PhysFuelModel attributes. \n", ERR_EINVAL);
00232         }
00233     if ( (fmd_fname != NULL ) && ((fstream = fopen(fmd_fname, "r")) != NULL ) ) {
00234         if ( (fm->pfm = InitPhysFuelModelFMDFile(fstream, fm->model_num)) == NULL )     {
00235             fclose(fstream);
00236             ERR_ERROR("Unable to intialize PhysFuelModel attributes in FuelModel. \n", ERR_EBADFUNC);
00237             }
00238         }
00239     else    {
00240         ERR_ERROR("Unable to open stream to fmd file for PhysFuelModel attributes. \n", ERR_EINVAL);
00241         }
00242     
00243     fclose(fstream);
00244     fm->type = EnumPhys;
00245     
00246     return ERR_SUCCESS;
00247     }
00248 
00249 /*
00250  * Description:
00251  * Initializes the PhysFuelModel member of the FuelModel structure as unburnable fuel.
00252  *
00253  * Arguments:
00254  * fm- ptr to initialized FuelModel
00255  *
00256  * Returns:
00257  * ERR_SUCCESS (0) if operation successful, an error code otherwise.
00258  * Best use of this facility is as follows...
00259  * int error_status = CallFunctionXXX();
00260  * if ( error_status)  something bad happened
00261  */ 
00262 int FuelModelInitPhysFuelModelUnBurnable(FuelModel * fm)    {
00263     /* check args */
00264     if ( fm == NULL )   {
00265         ERR_ERROR("FuelModel must be initialized before PhysFuelModel attributes. \n", ERR_EINVAL);
00266         }
00267         
00268     fm->pfm = InitPhysFuelModelUnBurnable();
00269     fm->type = EnumPhys;
00270 
00271     return ERR_SUCCESS;
00272     }
00273 
00274 int CmpNumToFuelModelNum(const void * fm_num, const void * fm)  {
00275     return (*(const int *)fm_num == ((FuelModel *)fm)->model_num);
00276     }
00277     
00278 void FuelModelDumpToStream(FuelModel * fm, FILE * fstream)  {
00279     if ( fm == NULL || fstream == NULL )    {
00280         ERR_ERROR_CONTINUE("Unable to dump FuelModel. \n", ERR_EINVAL);
00281         return;
00282         }
00283 
00284     fprintf(fstream,    "\n...Fuel Model Dump...\n");
00285     fprintf(fstream,    "MODEL NUM: %d \n", fm->model_num);
00286     if ( fm->model_name != NULL )
00287         fprintf(fstream,"MODEL NAME: %s \n", fm->model_name);
00288     else
00289         fprintf(fstream,"MODEL NAME: <NOT INITIALIZED> \n");
00290     if ( fm->model_desc != NULL )
00291         fprintf(fstream,"MODEL DESCRIPTION: %s \n", fm->model_desc);
00292     else
00293         fprintf(fstream,"MODEL DESCRIPTION: <NOT INITIALIZED> \n");
00294     switch(fm->type)    {
00295         case EnumRoth:          
00296             if ( fm->rfm != NULL )
00297                 RothFuelModelDumpToStream(fm->rfm, fstream);
00298             break;
00299         case EnumPhys:
00300             if ( fm->pfm != NULL )
00301                 PhysFuelModelDumpToStream(fm->pfm, fstream);
00302             break;
00303         default:
00304             fprintf(fstream, "FUEL MODEL ATTRIBUTES NOT INITIALIZED \n");
00305             break;
00306         }
00307     fprintf(fstream,    "\n");
00308 
00309     return;
00310     }
00311 
00312  void FreeFuelModel(void * vptr)    {
00313     FuelModel * fm = NULL;
00314     if ( vptr != NULL ) {
00315         fm = (FuelModel *) vptr;
00316         if ( fm->model_name != NULL )   {
00317             free(fm->model_name);
00318             }           
00319         if ( fm->model_desc != NULL )   {
00320             free(fm->model_desc);
00321             } 
00322         if ( fm->rfm != NULL )  {
00323             FreeRothFuelModel(fm->rfm);
00324             } 
00325         if ( fm->pfm != NULL )  {
00326             FreePhysFuelModel(fm->pfm);
00327             }
00328         free(fm);
00329         }   
00330     fm = NULL;
00331     return; 
00332     }
00333 
00334 /* end of FuelModel.c */

Generated at Fri Jun 22 00:46:51 2001 for HFire by doxygen1.2.3 written by Dimitri van Heesch, © 1997-2000