UsesCase_MEDfield_16.c

Aller à la documentation de ce fichier.
00001 /*  This file is part of MED.
00002  *
00003  *  COPYRIGHT (C) 1999 - 2015  EDF R&D, CEA/DEN
00004  *  MED is free software: you can redistribute it and/or modify
00005  *  it under the terms of the GNU Lesser General Public License as published by
00006  *  the Free Software Foundation, either version 3 of the License, or
00007  *  (at your option) any later version.
00008  *
00009  *  MED is distributed in the hope that it will be useful,
00010  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00011  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012  *  GNU Lesser General Public License for more details.
00013  *
00014  *  You should have received a copy of the GNU Lesser General Public License
00015  *  along with MED.  If not, see <http://www.gnu.org/licenses/>.
00016  */
00017 
00018 #include <med.h>
00019 #define MESGERR 1
00020 #include <med_utils.h>
00021 
00022 #include <string.h>
00023 
00024 #include <med.h>
00025 #define MESGERR 1
00026 #include <med_utils.h>
00027 
00028 #include <string.h>
00029 
00030 /* 
00031  * Field use case 16 : read a field (generic approach) in a MED file with 
00032  * - values defined on struct elements
00033  */
00034 
00035 
00036 int main (int argc, char **argv) {
00037   med_idt fid,mfid,sfid;
00038   med_idt nfield, i, j;
00039   char meshname[MED_NAME_SIZE+1]="";
00040   med_bool localmesh;
00041   char fieldname[MED_NAME_SIZE+1]="";
00042   med_field_type fieldtype;
00043   char *componentname = NULL;
00044   char *componentunit = NULL;
00045   char dtunit[MED_SNAME_SIZE+1]="";
00046   med_float *values = NULL;
00047   med_int nstep, nvalues;
00048   med_int ncomponent;
00049   med_int csit, numit, numdt, it;
00050   med_float dt;
00051   med_geometry_type *geotypes, geotype;
00052   med_int nmodels;
00053   med_int nprofile, pit, profilesize;
00054   char profilename[MED_NAME_SIZE+1]="";
00055   med_int nintegrationpoint;
00056   char localizationname[MED_NAME_SIZE+1]="";
00057   int k;
00058   char elementname[MED_NAME_SIZE+1];
00059   char supportmeshname[MED_NAME_SIZE+1];
00060   med_entity_type entitype;
00061   med_int elementdim;
00062   med_int nnode,ncell;
00063   med_geometry_type geocelltype;
00064   med_bool anyprofile=0;
00065   med_int nconstatt, *nvaratt;
00066   int ret=-1;
00067 
00068   /* open file */
00069   fid = MEDfileOpen("UsesCase_MEDfield_15.med",MED_ACC_RDWR);
00070   if (fid < 0) {
00071     MESSAGE("ERROR : open file ...");
00072     goto ERROR;
00073   }
00074 
00075   if (( mfid=MEDfileObjectsMount(fid,  "UsesCase_MEDstructElement_1.med",MED_MESH_SUPPORT)) < 0 ) {
00076     MESSAGE("ERROR : file mounting ...");
00077     goto ERROR;
00078   }
00079 
00080   if (( sfid=MEDfileObjectsMount(fid,  "UsesCase_MEDstructElement_1.med",MED_ELSTRUCT)) < 0 ) {
00081     MESSAGE("ERROR : file mounting ...");
00082     goto ERROR;
00083   }
00084 
00085   /* How many struct element models ? */
00086   if ((nmodels =  MEDnStructElement(mfid)) < 0) {
00087     MESSAGE("ERROR : read number of struct element models ...");
00088     goto ERROR;
00089   }
00090   geotypes = (med_geometry_type *) malloc(sizeof(med_geometry_type)*nmodels);
00091   nvaratt = (med_int *) malloc(sizeof(med_int)*nmodels);
00092 
00093   for (it=0; it<nmodels; it++) {
00094     if (MEDstructElementInfo(mfid, it+1, elementname, geotypes+it, &elementdim,
00095                              supportmeshname, &entitype, &nnode, &ncell,
00096                              &geocelltype, &nconstatt, &anyprofile, nvaratt+it) < 0) {
00097       MESSAGE("ERROR : struct element models information ...");
00098       free(nvaratt);
00099       goto ERROR;
00100     }
00101   }
00102   free(nvaratt);
00103 
00104 
00105   /*
00106    * generic approach : how many fields in the file and identification
00107    * of each field.
00108    */
00109   if ((nfield = MEDnField(fid)) < 0) {
00110     MESSAGE("ERROR : How many fields in the file ...");
00111     goto ERROR;
00112   }
00113 
00114   /*
00115    * read values for each field
00116    */
00117   for (i=0; i<nfield; i++) {
00118 
00119     if ((ncomponent = MEDfieldnComponent(fid,i+1)) < 0) {
00120       MESSAGE("ERROR : number of field component ...");
00121       goto ERROR;
00122     }
00123     
00124     if ((componentname = (char *) malloc(ncomponent*MED_SNAME_SIZE+1)) == NULL) {
00125       MESSAGE("ERROR : memory allocation ...");
00126       goto ERROR;
00127     }
00128 
00129     if ((componentunit = (char *) malloc(ncomponent*MED_SNAME_SIZE+1)) == NULL) {
00130       MESSAGE("ERROR : memory allocation ...");
00131       goto ERROR;
00132     }
00133 
00134     if (MEDfieldInfo(fid, i+1, fieldname, meshname, &localmesh, &fieldtype,
00135                      componentname, componentunit, dtunit, &nstep) < 0) {
00136       MESSAGE("ERROR : Field info ...");
00137       free(componentname);
00138       free(componentunit);
00139       goto ERROR;
00140     }
00141 
00142     free(componentname);
00143     free(componentunit);
00144 
00145     /*
00146      * Read field values for each computing step 
00147      */ 
00148     for (csit=0; csit<nstep; csit++) {
00149 
00150       if (MEDfieldComputingStepInfo(fid, fieldname, csit+1, &numdt, &numit, &dt) < 0) {
00151         MESSAGE("ERROR : Computing step info ...");
00152         goto ERROR;
00153       }
00154 
00155       /* 
00156        * ... In our case, we suppose that the field values are only defined on struct element ... 
00157        */
00158       for (it=0; it<nmodels; it++) {
00159 
00160         geotype = *(geotypes+it);
00161      
00162         /*
00163          * How many profile for each geometry type ? 
00164          */
00165         if ((nprofile = MEDfieldnProfile(fid, fieldname, numdt, numit, MED_STRUCT_ELEMENT, geotype,
00166                                          profilename, localizationname)) < 0) {
00167           MESSAGE("ERROR : read number of profile ");
00168           goto ERROR;
00169         }
00170         
00171         /* 
00172          * Read values for each profile
00173          */
00174         for (pit=0; pit<nprofile; pit++) {
00175           
00176           if ((nvalues = MEDfieldnValueWithProfile(fid, fieldname, numdt, numit, MED_STRUCT_ELEMENT, geotype,
00177                                                    pit+1, MED_COMPACT_PFLMODE, profilename, &profilesize,
00178                                                    localizationname, &nintegrationpoint)) < 0) {
00179             MESSAGE("ERROR : read number of values with a profile ...");
00180             goto ERROR;
00181           } 
00182           
00183           if (nvalues) {
00184             if ((values = (med_float *) malloc(sizeof(med_float)*nvalues*ncomponent*nintegrationpoint)) == NULL) {
00185               MESSAGE("ERROR : memory allocation ...");
00186               goto ERROR;
00187             }
00188             if (MEDfieldValueWithProfileRd(fid, fieldname, numdt, numit, MED_STRUCT_ELEMENT, geotype,
00189                                            MED_COMPACT_PFLMODE, profilename,
00190                                            MED_FULL_INTERLACE, MED_ALL_CONSTITUENT, 
00191                                            (unsigned char*) values) < 0) {
00192               MESSAGE("ERROR : read fields values for cells ..."); 
00193               goto ERROR; 
00194               free(values);
00195             }  
00196             free(values);
00197           }
00198         }
00199       }
00200     }
00201   }
00202   
00203   ret=0;
00204  ERROR :
00205 
00206   free(geotypes);
00207 
00208   if ( MEDfileObjectsUnmount(fid, sfid, MED_ELSTRUCT) < 0 ) {
00209     MESSAGE("ERROR : file unmounting ...");
00210     ret=-1;
00211   }
00212 
00213   if ( MEDfileObjectsUnmount(fid, mfid, MED_MESH_SUPPORT) < 0 ) {
00214     MESSAGE("ERROR : file unmounting ...");
00215     ret= -1;
00216   }
00217 
00218   /* close file */
00219   if (MEDfileClose(fid) < 0) {
00220     MESSAGE("ERROR : close file ...");             
00221     ret= -1; 
00222   } 
00223   
00224   return ret;
00225 }

Généré le Thu Oct 8 14:26:17 2015 pour MED fichier par  doxygen 1.6.1