UsesCase_MEDmesh_16.c

Aller à la documentation de ce fichier.
00001 /*  This file is part of MED.
00002  *
00003  *  COPYRIGHT (C) 1999 - 2016  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 /*
00019  *  How to create an unstructured mesh with polyhedrons
00020  *
00021  *  Use case 16 : read a 2D unstructured mesh with 1 polyhedrons
00022  */
00023 
00024 #include <med.h>
00025 #define MESGERR 1
00026 #include <med_utils.h>
00027 
00028 #include <string.h>
00029 
00030 int main (int argc, char **argv) {
00031   med_idt fid;
00032   const char meshname[MED_NAME_SIZE+1] = "3D Unstructured Mesh With 2 polyhedrons";
00033   char meshdescription[MED_COMMENT_SIZE+1];
00034   med_int meshdim;
00035   med_int spacedim;
00036   med_sorting_type sortingtype;
00037   med_int nstep;
00038   med_mesh_type meshtype;
00039   med_axis_type axistype;
00040   char axisname[3*MED_SNAME_SIZE+1];
00041   char unitname[3*MED_SNAME_SIZE+1];
00042   char dtunit[MED_SNAME_SIZE+1];
00043   med_float *coordinates = NULL;
00044   med_int nnodes = 0;
00045   med_int npoly = 0;
00046   med_int faceindexsize;
00047   med_int nodeindexsize;
00048   med_int *faceindex = NULL;
00049   med_int *nodeindex = NULL;
00050   med_int *connectivity = NULL;
00051   med_int connectivitysize;
00052   med_bool coordinatechangement;
00053   med_bool geotransformation;
00054   int i;
00055   int k,ind1,ind2;
00056   int j, jind1,jind2;
00057 
00058   /* open MED file with READ ONLY access mode */
00059   fid = MEDfileOpen("./UsesCase_MEDmesh_15.med",MED_ACC_RDONLY);
00060   if (fid < 0) {
00061     MESSAGE("ERROR : open file in READ ONLY ACCESS mode ...");
00062     return -1;
00063   }
00064 
00065   /*
00066    * ... we know that the MED file has only one mesh,
00067    * a real code would check ...
00068    */
00069 
00070   /* read mesh informations : mesh dimension, space dimension ... */
00071   if (MEDmeshInfoByName(fid, meshname, &spacedim, &meshdim, &meshtype, meshdescription,
00072       dtunit, &sortingtype, &nstep, &axistype, axisname, unitname) < 0) {
00073     MESSAGE("ERROR : mesh info ...");
00074     return -1;
00075   }
00076 
00077   /* read how many nodes are in the mesh */
00078   if ((nnodes = MEDmeshnEntity(fid, meshname, MED_NO_DT, MED_NO_IT, MED_NODE, MED_POINT1,
00079              MED_COORDINATE, MED_NO_CMODE,&coordinatechangement, &geotransformation)) < 0)
00080     { MESSAGE("ERROR : number of nodes ...");
00081       return -1;
00082     }
00083 
00084   /*
00085    * ... we know that we only have MED_POLYHEDRON cells in the mesh,
00086    * a real code would check for MED geometry cell types ...
00087    */
00088 
00089   /* We read how many polyhedrons are in the mesh (using nodal connectivity mode) */
00090   /* We get the size of the polyhedrons/face index array. 
00091    * As an index of the face index array give the location of the first face   and so the 
00092    * number of polyhedrons 
00093    */
00094   if ((faceindexsize = MEDmeshnEntity(fid,meshname,MED_NO_DT,MED_NO_IT,
00095           MED_CELL,MED_POLYHEDRON,MED_INDEX_FACE,MED_NODAL,
00096           &coordinatechangement, &geotransformation)) < 0) 
00097     { MESSAGE("ERROR : read number of polyhedrons ...");
00098       return -1;
00099     }
00100   npoly = faceindexsize-1;
00101   ISCRUTE(npoly);
00102 
00103   if ((nodeindexsize = MEDmeshnEntity(fid,meshname,MED_NO_DT,MED_NO_IT,
00104           MED_CELL,MED_POLYHEDRON,MED_INDEX_NODE,MED_NODAL,
00105           &coordinatechangement, &geotransformation)) < 0) 
00106     { MESSAGE("ERROR : read number of polyhedrons ...");
00107       return -1;
00108     }
00109   ISCRUTE(nodeindexsize);
00110 
00111   /* how many nodes for the polyhedron connectivity ? */
00112   if ((connectivitysize = MEDmeshnEntity(fid,meshname,MED_NO_DT,MED_NO_IT,
00113            MED_CELL,MED_POLYHEDRON,MED_CONNECTIVITY,MED_NODAL,
00114            &coordinatechangement, &geotransformation)) < 0) 
00115     { MESSAGE("ERROR : read connevity size ...");
00116       return -1;
00117     }
00118   ISCRUTE(connectivitysize);
00119 
00120   /* read mesh nodes coordinates */
00121   if ((coordinates = (med_float*) malloc(sizeof(med_float)*nnodes*spacedim)) == NULL) {
00122     MESSAGE("ERROR : memory allocation ...");
00123     return -1;
00124   }
00125 
00126   if (MEDmeshNodeCoordinateRd(fid, meshname, MED_NO_DT, MED_NO_IT, MED_FULL_INTERLACE,
00127             coordinates) < 0) {
00128     MESSAGE("ERROR : nodes coordinates ...");
00129     return -1;
00130   }
00131   for (i=0;i<nnodes*spacedim;i++)
00132     printf("%f - ",*(coordinates+i));
00133   printf("\n");
00134 
00135 
00136   /* read polygons connectivity */
00137   faceindex = (med_int *) malloc(sizeof(med_int)*faceindexsize);
00138   nodeindex = (med_int *) malloc(sizeof(med_int)*nodeindexsize);
00139   connectivity = (med_int *) malloc(sizeof(med_int)*connectivitysize);
00140 
00141   if (MEDmeshPolyhedronRd(fid,meshname,MED_NO_DT,MED_NO_IT,MED_CELL,MED_NODAL,
00142                           faceindex,nodeindex,connectivity) < 0) 
00143     { MESSAGE("ERROR : read polygon connectivity ...");
00144       return -1;
00145     }
00146 
00147   for (i=0;i<npoly;i++)
00148     {
00149     printf(">> MED_POLYHEDRON "IFORMAT" : \n",i+1);
00150     printf("---- Face Index         ----- : [\n");
00151     ind1 = *(faceindex+i)-1;
00152     ind2 = *(faceindex+i+1)-1;
00153     for (k=ind1;k<ind2;k++)
00154       printf(IFORMAT" ",*(nodeindex+k));
00155     printf(" ] \n");
00156     printf("---- Connectivity       ----- : [\n");
00157     for (k=ind1;k<ind2;k++)
00158       {
00159       jind1 = *(nodeindex+k)-1;
00160       jind2 = *(nodeindex+k+1)-1;
00161       for (j=jind1;j<jind2;j++)
00162         printf(IFORMAT" ",*(connectivity+j));
00163       printf(" \n");
00164       }
00165     printf(" ] \n");
00166     }
00167 
00168   /*
00169    * ... we know that the family number of nodes and elements is 0, a real code would check ...
00170    */
00171 
00172   /* close MED file */
00173   if (MEDfileClose(fid) < 0) {
00174     MESSAGE("ERROR : close file");
00175     return -1;
00176   }
00177 
00178   /* memory deallocation */
00179   if (coordinates)
00180     free(coordinates);
00181 
00182   if (faceindex)
00183     free(faceindex);
00184 
00185   if (nodeindex)
00186     free(nodeindex);
00187 
00188   if (connectivity)
00189     free(connectivity);
00190 
00191   return 0;
00192 }

Généré le Thu Jan 21 18:06:09 2016 pour MED fichier par  doxygen 1.6.1