UsesCase_MEDstructElement_2.c

Aller à la documentation de ce fichier.
00001 /*  This file is part of MED.
00002  *
00003  *  COPYRIGHT (C) 1999 - 2011  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 /* 
00025  * StructElement use case 2 : read struct element models in a file 
00026  * Classical iteration approach
00027  * STEP 1 : read suppport mesh
00028  * STEP 2 : read struct element model
00029  * STEP 3 : read in a computation mesh
00030  * A access from the computation mesh is defined in StructElement use case 3.
00031  */
00032 
00033 int main (int argc, char **argv) {
00034   med_idt fid;
00035   med_int nmodels, nsmesh;
00036   int i,j,k;
00037   char elementname[MED_NAME_SIZE+1]="";
00038   char supportmeshname[MED_NAME_SIZE+1]="";
00039   const char computmeshname[MED_NAME_SIZE+1]="COMPUT_MESH";
00040   med_geometry_type *geotype;
00041   med_entity_type entitype;
00042   med_int elementdim;
00043   med_int nnode,ncell;
00044   med_geometry_type geocelltype;
00045   med_bool anyprofile=0;
00046   med_int nconstatt, *nvaratt;
00047   char attname[MED_NAME_SIZE+1]="";
00048   char profilename[MED_NAME_SIZE+1]="";
00049   med_attribute_type atttype;
00050   med_int nattcomp;
00051   med_entity_type attentitype;
00052   med_int profilesize;
00053   unsigned char *value;
00054   med_int size=0;
00055   med_int meshdim, spacedim;
00056   char description[MED_COMMENT_SIZE+1]="";
00057   char axisname[3*MED_SNAME_SIZE+1]="";
00058   char axisunit[3*MED_SNAME_SIZE+1]="";
00059   med_axis_type axistype;
00060   med_float *coordinates;
00061   med_bool coordinatechangement;
00062   med_bool geotransformation;
00063   med_int nseg2;
00064   med_int *seg2connectivity;
00065   med_int nentities=0;
00066   med_sorting_type sortingtype;
00067   med_mesh_type meshtype;
00068   med_int nstep;
00069   char dtunit[MED_SNAME_SIZE+1]="";
00070   char unitname[2*MED_SNAME_SIZE+1]="";
00071   char tmp[MED_NAME_SIZE+1]="";
00072   int ret=-1;
00073 
00074   /* open file */
00075   fid = MEDfileOpen("UsesCase_MEDstructElement_1.med",MED_ACC_RDONLY);
00076   if (fid < 0) {
00077     MESSAGE("ERROR : file creation ...");
00078     goto ERROR;
00079   }
00080 
00081   /* STEP 1 */
00082 
00083   /* how many support mesh in the file ? */
00084   if ((nsmesh = MEDnSupportMesh(fid)) < 0 ) {
00085     MESSAGE("ERROR : read number of support mesh ...");
00086     goto ERROR;
00087   }
00088   
00089   /* read each support mesh */
00090   for (i=0; i<nsmesh; i++) {
00091     if ( MEDsupportMeshInfo(fid, i+1, supportmeshname, &spacedim, &meshdim, description,
00092                             &axistype, axisname, axisunit) < 0 ) {
00093       MESSAGE("ERROR : read information about mesh support ...");
00094       goto ERROR;
00095     }
00096     
00097     /* read how many nodes in the mesh */
00098     if ((nnode = MEDmeshnEntity(fid, supportmeshname, MED_NO_DT, MED_NO_IT, MED_NODE, MED_NONE,
00099                                 MED_COORDINATE, MED_NO_CMODE,&coordinatechangement,
00100                                 &geotransformation)) < 0) {
00101       MESSAGE("ERROR : read number of nodes ...");
00102       goto ERROR;
00103     }
00104   
00105     /* read mesh nodes coordinates */
00106     coordinates = (med_float*) malloc(sizeof(med_float)*nnode*spacedim);
00107     
00108     if (MEDmeshNodeCoordinateRd(fid, supportmeshname, MED_NO_DT, MED_NO_IT, MED_FULL_INTERLACE,
00109                                 coordinates) < 0) {
00110       MESSAGE("ERROR : read nodes coordinates ...");
00111       free(coordinates);
00112       goto ERROR;
00113     }
00114   
00115     /* free memory */
00116     free(coordinates);
00117   
00118     /* ... In this case, we suppose that we have only MED_SEG2
00119      *     as cell elements in our support meshes 
00120      *     a real code working would check ...  */
00121     if ((nseg2 = MEDmeshnEntity(fid, supportmeshname, MED_NO_DT, MED_NO_IT, MED_CELL,MED_SEG2,
00122                               MED_CONNECTIVITY, MED_NODAL, &coordinatechangement,
00123                                 &geotransformation)) < 0) {
00124       MESSAGE("ERROR : number of MED_SEG2 ...");
00125       goto ERROR;
00126     }
00127   
00128     /* read MED_SEG2 connectivity if necessary */
00129     if (nseg2 > 0) {
00130       seg2connectivity = (med_int *) malloc(sizeof(med_int)*nseg2*2);
00131       
00132       if (MEDmeshElementConnectivityRd(fid, supportmeshname, MED_NO_DT, MED_NO_IT, MED_CELL,
00133                                        MED_SEG2, MED_NODAL, MED_FULL_INTERLACE, seg2connectivity) < 0) {
00134         MESSAGE("ERROR : MED_SEG2 connectivity ...");
00135         free(seg2connectivity);
00136         goto ERROR;  
00137       }
00138       
00139       free(seg2connectivity);
00140     }
00141 
00142   }
00143 
00144   /* STEP 2 */
00145 
00146   /* how many struct element models ? */
00147   if ((nmodels =  MEDnStructElement(fid)) < 0) {
00148     MESSAGE("ERROR : read number of struct element models ...");
00149     goto ERROR;
00150   }
00151 
00152   geotype = (med_geometry_type *) malloc(sizeof(med_geometry_type)*nmodels);
00153   nvaratt = (med_int *) malloc(sizeof(med_int)*nmodels);
00154   
00155 
00156   /* read each model */
00157   for (i=0; i<nmodels; i++) {
00158         if (MEDstructElementInfo(fid, i+1, elementname, geotype+i, &elementdim,
00159                                  supportmeshname, &entitype, &nnode, &ncell,
00160                                  &geocelltype, &nconstatt, &anyprofile, nvaratt+i) < 0) {
00161           MESSAGE("ERROR : struct element models information ...");
00162           goto ERROR;
00163         }
00164 
00165         /* read constant attribute(s) */
00166         for (j=0; j<nconstatt; j++) {
00167           if ( MEDstructElementConstAttInfo(fid, elementname, j+1, 
00168                                             attname, &atttype, &nattcomp, &attentitype,
00169                                             profilename, &profilesize) < 0) {
00170             MESSAGE("ERROR : const attribute information ...");
00171             goto ERROR;
00172           }
00173 
00174           /* memory allocation */
00175           if (profilesize != 0)
00176             size = profilesize*nattcomp*MEDstructElementAttSizeof(atttype);
00177           else
00178             if (entitype == MED_NODE)
00179               size = nnode*nattcomp*MEDstructElementAttSizeof(atttype);
00180             else
00181               size = ncell*nattcomp*MEDstructElementAttSizeof(atttype);
00182           if ( atttype == MED_ATT_NAME) ++size;
00183           value = (unsigned char *) malloc(size);
00184 
00185           /* read attribute(s) value(s) */
00186           if ( MEDstructElementConstAttRd(fid, elementname, attname, (unsigned char *)value ) < 0 ) {
00187             MESSAGE("ERROR : const attribute value ...");
00188             free(value);
00189             goto ERROR;     
00190           } 
00191 
00192           free(value);
00193 
00194         }
00195 
00196         /* read variable attribute(s)                              */
00197         /* values must be read in a computation mesh => see STEP 3 */
00198   }
00199 
00200   /* STEP 3 */
00201 
00202   /* 
00203    * ... In this case, we know that the MED file has only one mesh, 
00204    * a real code working would check ... 
00205    */
00206   /* read mesh informations : mesh dimension, space dimension ... */
00207   if (MEDmeshInfoByName(fid, computmeshname, &spacedim, &meshdim, &meshtype, description, 
00208                         dtunit, &sortingtype, &nstep, &axistype, axisname, unitname) < 0) {
00209     MESSAGE("ERROR : mesh info ...");
00210     goto ERROR;
00211   }
00212 
00213   /* read how many nodes in the mesh */
00214   if ((nnode = MEDmeshnEntity(fid, computmeshname, MED_NO_DT, MED_NO_IT, MED_NODE, MED_NONE,
00215                               MED_COORDINATE, MED_NO_CMODE,&coordinatechangement,
00216                               &geotransformation)) < 0) {
00217     MESSAGE("ERROR : number of nodes ...");
00218     goto ERROR;
00219   }
00220 
00221 
00222   /* Get dynamically struct element name for each struct element model,
00223      then for each type read the connectivity if a support mesh exist and
00224      finaly the variable(s) attribute(s) */
00225   for (i=0;i<nmodels;i++) {
00226 
00227     /* read how many MED_STRUCT_ELEMENT of type *(geotype+i) there is in the mesh */
00228     if ((nentities = MEDmeshnEntity(fid, computmeshname, MED_NO_DT, MED_NO_IT, MED_STRUCT_ELEMENT,*(geotype+i),
00229                                     MED_CONNECTIVITY, MED_NODAL, &coordinatechangement,
00230                                     &geotransformation)) < 0) {
00231       MESSAGE("ERROR : number of MED_STRUCT_ELEMENT ...");
00232       goto ERROR;
00233     }
00234 
00235     if (MEDstructElementName(fid,*(geotype+i),elementname) < 0) {
00236           MESSAGE("ERROR : get element name ...");
00237           goto ERROR;
00238     }
00239 
00240     for (j=0; j<*(nvaratt+i); j++) {
00241 
00242       /* read informations about each attribute */
00243       if ( MEDstructElementVarAttInfo(fid, elementname, j+1, 
00244                                       attname, &atttype, &nattcomp) < 0) {
00245             MESSAGE("ERROR : var attribute information ...");
00246             goto ERROR;
00247       }
00248 
00249       /* memory allocation */
00250       if (entitype == MED_NODE)
00251         size = nnode*nattcomp*MEDstructElementAttSizeof(atttype);
00252       else
00253         size = ncell*nattcomp*MEDstructElementAttSizeof(atttype);
00254       if ( atttype == MED_ATT_NAME) ++size;
00255       value = (unsigned char *) malloc(size);
00256 
00257       /* read attribute values */
00258       if (MEDmeshStructElementVarAttRd(fid, computmeshname, MED_NO_DT, MED_NO_IT,
00259                                        *(geotype+i), attname, value ) < 0) {
00260         MESSAGE("ERROR : read variable attributes values ...");
00261         free(value);
00262         goto ERROR;
00263       }
00264 
00265       free(value);
00266 
00267     }
00268   }
00269 
00270   ret=0;
00271  ERROR:
00272   
00273   free(geotype);
00274   free(nvaratt);
00275 
00276   /* close file */
00277   if (MEDfileClose(fid) < 0) {
00278     MESSAGE("ERROR : file closing ...");
00279     ret=-1;
00280   }
00281 
00282   return ret;
00283 }
00284 

Généré le Mon May 16 17:10:24 2011 pour MED fichier par  doxygen 1.6.1