# UsesCase_MEDmesh_16.c

Aller à la documentation de ce fichier.

    00001 /*  This file is part of MED.
    00002  *
    00003  *  COPYRIGHT (C) 1999 - 2012  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 polygons
    00020  *
    00021  *  Use case 16 : read a 2D unstructured mesh with 2 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";
    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 indexsize;
    00047   med_int faceIndexSize;
    00048   med_int *index = NULL;
    00049   med_int *faceindex = 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 working 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 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,
    00080              &geotransformation)) < 0) {
    00081     MESSAGE("ERROR : number of nodes ...");
    00082     return -1;
    00083   }
    00084
    00085   /*
    00086    * ... we know that we only have MED_POLYHEDRON cells in the mesh,
    00087    * a real code working would check all MED geometry cell types ...
    00088    */
    00089
    00090   /* How many polygon in the mesh in nodal connectivity mode */
    00091   /* For the polygons, we get the size of array index */
    00092   if ((indexsize = MEDmeshnEntity(fid,meshname,MED_NO_DT,MED_NO_IT,
    00093           MED_CELL,MED_POLYHEDRON,MED_INDEX_FACE,MED_NODAL,
    00094           &coordinatechangement,
    00095           &geotransformation)) < 0) {
    00096     MESSAGE("ERROR : read number of polyedron ...");
    00097     return -1;
    00098   }
    00099   npoly = indexsize-1;
    00100   ISCRUTE(npoly);
    00101
    00102   if ((faceIndexSize = MEDmeshnEntity(fid,meshname,MED_NO_DT,MED_NO_IT,
    00103           MED_CELL,MED_POLYHEDRON,MED_INDEX_NODE,MED_NODAL,
    00104           &coordinatechangement,
    00105           &geotransformation)) < 0) {
    00106     MESSAGE("ERROR : read number of polyedron ...");
    00107     return -1;
    00108   }
    00109   ISCRUTE(faceIndexSize);
    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,
    00115            &geotransformation)) < 0) {
    00116     MESSAGE("ERROR : read connevity size ...");
    00117     return -1;
    00118     }
    00119   ISCRUTE(connectivitysize);
    00120
    00121   /* read mesh nodes coordinates */
    00122   if ((coordinates = (med_float*) malloc(sizeof(med_float)*nnodes*spacedim)) == NULL) {
    00123     MESSAGE("ERROR : memory allocation ...");
    00124     return -1;
    00125   }
    00126
    00127   if (MEDmeshNodeCoordinateRd(fid, meshname, MED_NO_DT, MED_NO_IT, MED_FULL_INTERLACE,
    00128             coordinates) < 0) {
    00129     MESSAGE("ERROR : nodes coordinates ...");
    00130     return -1;
    00131   }
    00132   for (i=0;i<nnodes*spacedim;i++)
    00133     printf("%f - ",*(coordinates+i));
    00134   printf("\n");
    00135
    00136
    00137   /* read polygons connectivity */
    00138   index = (med_int *) malloc(sizeof(med_int)*indexsize);
    00139   faceindex = (med_int *) malloc(sizeof(med_int)*faceIndexSize);
    00140   connectivity = (med_int *) malloc(sizeof(med_int)*connectivitysize);
    00141
    00142   if (MEDmeshPolyhedronRd(fid,meshname,MED_NO_DT,MED_NO_IT,MED_CELL,MED_NODAL,
    00143                           index,faceindex,connectivity) < 0) {
    00144     MESSAGE("ERROR : read polygon connectivity ...");
    00145     return -1;
    00146   }
    00147
    00148   for (i=0;i<npoly;i++)
    00149     {
    00150     printf(">> MED_POLYHEDRON "IFORMAT" : \n",i+1);
    00151     printf("---- Face Index         ----- : [ ");
    00152     ind1 = *(index+i)-1;
    00153     ind2 = *(index+i+1)-1;
    00154     for (k=ind1;k<ind2;k++)
    00155       printf(IFORMAT" ",*(faceindex+k));
    00156     printf(" ] \n");
    00157     printf("---- Connectivity       ----- : [ ");
    00158     for (k=ind1;k<ind2;k++)
    00159       {
    00160       jind1 = *(faceindex+k)-1;
    00161       jind2 = *(faceindex+k+1)-1;
    00162       for (j=jind1;j<jind2;j++)
    00163         printf(IFORMAT" ",*(connectivity+j));
    00164       printf(" \n");
    00165       }
    00166     printf(" ] \n");
    00167     }
    00168
    00169   /*
    00170    * ... we know that the family number of nodes and elements is 0, a real working would check ...
    00171    */
    00172
    00173   /* close MED file */
    00174   if (MEDfileClose(fid) < 0) {
    00175     MESSAGE("ERROR : close file");
    00176     return -1;
    00177   }
    00178
    00179   /* memory deallocation */
    00180   if (coordinates)
    00181     free(coordinates);
    00182
    00183   if (index)
    00184     free(index);
    00185
    00186   if (faceindex)
    00187     free(faceindex);
    00188
    00189   if (connectivity)
    00190     free(connectivity);
    00191
    00192   return 0;
    00193 }