UsesCase_MEDfield_18.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 18 : read a field (generic approach) in a MED file with
00032  *                     values defined on integration points on struct elements
00033  */
00034 
00035 
00036 int main (int argc, char **argv) {
00037   med_idt fid,mfid,sfid,cmfid;
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=NULL, 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=NULL;
00066   med_bool coordinatechangement;
00067   med_bool geotransformation;
00068   int ret=-1;
00069 
00070 
00071   /* open file */
00072   fid = MEDfileOpen("UsesCase_MEDfield_17.med",MED_ACC_RDWR);
00073   if (fid < 0) {
00074     MESSAGE("ERROR : open file ...");
00075     return -1;
00076   }
00077 
00078   if (( mfid=MEDfileObjectsMount(fid,  "UsesCase_MEDstructElement_1.med",MED_MESH_SUPPORT)) < 0 ) {
00079     MESSAGE("ERROR : file mounting ...");
00080     return -1;
00081   }
00082 
00083   if (( sfid=MEDfileObjectsMount(fid,  "UsesCase_MEDstructElement_1.med",MED_ELSTRUCT)) < 0 ) {
00084     MESSAGE("ERROR : file mounting ...");
00085     return -1;
00086   }
00087 
00088   if (( cmfid=MEDfileObjectsMount(fid,  "UsesCase_MEDstructElement_1.med",MED_MESH)) < 0 ) {
00089     MESSAGE("ERROR : file mounting ...");
00090     return -1;
00091   }
00092 
00093 
00094   /*
00095    * generic approach : how many fields in the file and identification
00096    * of each field.
00097    */
00098   if ((nfield = MEDnField(fid)) < 0) {
00099     MESSAGE("ERROR : How many fields in the file ...");
00100     return -1;
00101   }
00102 
00103   /*
00104    * read values for each field
00105    */
00106   for (i=0; i<nfield; i++) {
00107 
00108     if ((ncomponent = MEDfieldnComponent(fid,i+1)) < 0) {
00109       MESSAGE("ERROR : number of field component ...");
00110       return -1;
00111     }
00112     
00113     if ((componentname = (char *) malloc(ncomponent*MED_SNAME_SIZE+1)) == NULL) {
00114       MESSAGE("ERROR : memory allocation ...");
00115       return -1;
00116     }
00117 
00118     if ((componentunit = (char *) malloc(ncomponent*MED_SNAME_SIZE+1)) == NULL) {
00119       MESSAGE("ERROR : memory allocation ...");
00120       return -1;
00121     }
00122 
00123     if (MEDfieldInfo(fid, i+1, fieldname, meshname, &localmesh, &fieldtype,
00124                      componentname, componentunit, dtunit, &nstep) < 0) {
00125       MESSAGE("ERROR : Field info ...");
00126       free(componentname);
00127       free(componentunit);
00128       return -1;
00129     }
00130 
00131     free(componentname);
00132     free(componentunit);
00133 
00134     /* read how many struct element models in the mesh ? */
00135     if ((nmodels = MEDmeshnEntity(fid, meshname, MED_NO_DT, MED_NO_IT, MED_STRUCT_ELEMENT, MED_GEO_ALL,
00136                                   MED_UNDEF_DATATYPE, MED_NO_CMODE,&coordinatechangement,
00137                                   &geotransformation)) < 0) {
00138       MESSAGE("ERROR : number of nodes ...");
00139       return -1;
00140     }
00141     geotypes = (med_geometry_type *) malloc(sizeof(med_geometry_type)*nmodels);
00142     nvaratt = (med_int *) malloc(sizeof(med_int)*nmodels);
00143     
00144     for (it=0; it<nmodels; it++) {
00145       if (MEDstructElementInfo(mfid, it+1, elementname, geotypes+it, &elementdim,
00146                                supportmeshname, &entitype, &nnode, &ncell,
00147                                &geocelltype, &nconstatt, &anyprofile, nvaratt+it) < 0) {
00148         MESSAGE("ERROR : struct element models information ...");
00149         return -1;
00150       }
00151     }
00152 
00153 
00154     /*
00155      * Read field values for each computing step 
00156      */ 
00157     for (csit=0; csit<nstep; csit++) {
00158 
00159       if (MEDfieldComputingStepInfo(fid, fieldname, csit+1, &numdt, &numit, &dt) < 0) {
00160         MESSAGE("ERROR : Computing step info ...");
00161         return -1;
00162       }
00163 
00164       /* 
00165        * ... In our case, we suppose that the field values are only defined on struct element ... 
00166        */
00167       for (it=0; it<nmodels; it++) {
00168         geotype = *(geotypes+it);
00169         
00170         /*
00171          * How many profile for each geometry type ? 
00172          */
00173         if ((nprofile = MEDfieldnProfile(fid, fieldname, numdt, numit, MED_STRUCT_ELEMENT, geotype,
00174                                          profilename, localizationname)) < 0) {
00175           MESSAGE("ERROR : read number of profile ");
00176           return -1;
00177         }
00178 
00179         /* 
00180          * Read values for each profile
00181          */
00182         for (pit=0; pit<nprofile; pit++) {
00183           
00184           if ((nvalues = MEDfieldnValueWithProfile(fid, fieldname, numdt, numit, MED_STRUCT_ELEMENT, geotype,
00185                                                    pit+1, MED_COMPACT_PFLMODE, profilename, &profilesize,
00186                                                    localizationname, &nintegrationpoint)) < 0) {
00187             MESSAGE("ERROR : read number of values with a profile ...");
00188             return -1;
00189           } 
00190           
00191           if (nvalues) {
00192             if ((values = (med_float *) malloc(sizeof(med_float)*nvalues*ncomponent*nintegrationpoint)) == NULL) {
00193               MESSAGE("ERROR : memory allocation ...");
00194               return -1;
00195             }
00196             if (MEDfieldValueWithProfileRd(fid, fieldname, numdt, numit, MED_STRUCT_ELEMENT, geotype,
00197                                            MED_COMPACT_PFLMODE, profilename,
00198                                            MED_FULL_INTERLACE, MED_ALL_CONSTITUENT, 
00199                                            (unsigned char*) values) < 0) {
00200               MESSAGE("ERROR : read fields values for cells ..."); 
00201               free(values);
00202               return -1; 
00203             }  
00204             free(values);
00205           }
00206         }
00207       }
00208     }
00209 
00210     ret=0;
00211   ERROR:
00212     if (nvaratt)
00213       free(nvaratt);
00214     if (geotypes)
00215       free(geotypes);
00216 
00217   }
00218 
00219 
00220 
00221   if ( MEDfileObjectsUnmount(fid, mfid, MED_MESH_SUPPORT) < 0 ) {
00222     MESSAGE("ERROR : file unmounting ...");
00223     ret=-1;
00224   }
00225 
00226   if ( MEDfileObjectsUnmount(fid, sfid, MED_ELSTRUCT) < 0 ) {
00227     MESSAGE("ERROR : file unmounting ...");
00228     ret=-1;
00229   }
00230 
00231 
00232   if ( MEDfileObjectsUnmount(fid, cmfid, MED_MESH) < 0 ) {
00233     MESSAGE("ERROR : file unmounting ...");
00234     ret=-1;
00235   }
00236 
00237   /* close file */
00238   if (MEDfileClose(fid) < 0) {
00239     MESSAGE("ERROR : close file ...");             
00240     ret=-1; 
00241   } 
00242   
00243   return ret;
00244 }

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