# UsesCase_MEDmesh_3.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
    00019 /*
    00020  *  Use case 3 : read an unstructured mesh : generic approach
    00021  */
    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   char meshname[MED_NAME_SIZE+1]="";
    00033   char meshdescription[MED_COMMENT_SIZE+1]="";
    00034   med_int meshdim=0;
    00035   med_int spacedim=0;
    00036   med_sorting_type sortingtype;
    00037   med_int nstep;
    00038   med_mesh_type meshtype;
    00039   med_axis_type axistype;
    00040   char *axisname="";
    00041   char *unitname="";
    00042   char dtunit[MED_SNAME_SIZE+1]="";
    00043   med_float *coordinates = NULL;
    00044   med_int nnodes = 0;
    00045   med_int ngeo = 0;
    00046   med_int nelt=0;
    00047   med_int *connectivity = NULL;
    00048   med_bool coordinatechangement;
    00049   med_bool geotransformation;
    00050   med_int i, j, it, nmesh;
    00051   med_geometry_type geotype;
    00052   med_geometry_type *geotypes = MED_GET_CELL_GEOMETRY_TYPE;
    00053   char geotypename[MED_NAME_SIZE+1];
    00054   int ret=-1;
    00055
    00056
    00057   /* open MED file with READ ONLY access mode */
    00058   fid = MEDfileOpen("UsesCase_MEDmesh_1.med",MED_ACC_RDONLY);
    00059   if (fid < 0) {
    00060     MESSAGE("ERROR : open file in READ ONLY ACCESS mode ...");
    00061     goto ERROR;
    00062   }
    00063
    00064   /* read how many mesh in the file */
    00065   if ((nmesh = MEDnMesh(fid)) < 0) {
    00066     MESSAGE("ERROR : read how many mesh ...");
    00067     goto ERROR;
    00068   }
    00069
    00070   for (i=0;i<nmesh;i++) {
    00071
    00072     /* read computation space dimension */
    00073     if ((spacedim = MEDmeshnAxis(fid, i+1)) < 0) {
    00074       MESSAGE("ERROR : read computation space dimension ...");
    00075       goto ERROR;
    00076     }
    00077
    00078     /* memory allocation */
    00079     if ((axisname  = (char*) malloc(MED_SNAME_SIZE*spacedim+1)) == NULL) {
    00080       MESSAGE("ERROR : memory allocation ...");
    00081       goto ERROR;
    00082     }
    00083     if ((unitname  = (char*) malloc(MED_SNAME_SIZE*spacedim+1)) == NULL) {
    00084       MESSAGE("ERROR : memory allocation ...");
    00085       goto ERROR;
    00086     }
    00087
    00088     /* read mesh informations : meshname, mesh dimension, mesh type ... */
    00089     if (MEDmeshInfo(fid, i+1, meshname, &spacedim, &meshdim, &meshtype, meshdescription,
    00090                     dtunit, &sortingtype, &nstep, &axistype, axisname, unitname) < 0) {
    00091       MESSAGE("ERROR : mesh info ...");
    00092       free(axisname); free(unitname);
    00093       goto ERROR;
    00094     }
    00095
    00096     free(axisname);
    00097     free(unitname);
    00098
    00099     /* read how many nodes in the mesh */
    00100     if ((nnodes = MEDmeshnEntity(fid, meshname, MED_NO_DT, MED_NO_IT, MED_NODE, MED_NO_GEOTYPE,
    00101                                  MED_COORDINATE, MED_NO_CMODE,&coordinatechangement,
    00102                                  &geotransformation)) < 0) {
    00103       MESSAGE("ERROR : number of nodes ...");
    00104     goto ERROR;
    00105     }
    00106
    00107     /* read mesh nodes coordinates */
    00108     if ((coordinates = (med_float*) malloc(sizeof(med_float)*nnodes*spacedim)) == NULL) {
    00109       MESSAGE("ERROR : memory allocation ...");
    00110       goto ERROR;
    00111     }
    00112
    00113     if (MEDmeshNodeCoordinateRd(fid, meshname, MED_NO_DT, MED_NO_IT, MED_FULL_INTERLACE,
    00114                                 coordinates) < 0) {
    00115       MESSAGE("ERROR : nodes coordinates ...");
    00116       free(coordinates);
    00117       goto ERROR;
    00118     }
    00119
    00120     if (coordinates)
    00121       free(coordinates);
    00122
    00123     /* read number of geometrical types for cells */
    00124     if ((ngeo = MEDmeshnEntity(fid, meshname, MED_NO_DT, MED_NO_IT, MED_CELL,MED_GEO_ALL,
    00125                                MED_CONNECTIVITY, MED_NODAL,&coordinatechangement,
    00126                                &geotransformation)) < 0) {
    00127       MESSAGE("ERROR : number of cell ...");
    00128       ISCRUTE(geotype);
    00129       goto ERROR;
    00130     }
    00131
    00132     for (it=1; it<=ngeo; it++) {
    00133
    00134       /* get geometry type */
    00135       if ((nelt = MEDmeshEntityInfo(fid, meshname, MED_NO_DT, MED_NO_IT,MED_CELL,it,
    00136                                    geotypename,&geotype)) < 0) {
    00137         MESSAGE("ERROR : number of cell ...");
    00138         ISCRUTE(it);
    00139         goto ERROR;
    00140       }
    00141
    00142       /* how many cells of type geotype ? */
    00143       if ((nelt = MEDmeshnEntity(fid, meshname, MED_NO_DT, MED_NO_IT, MED_CELL,geotype,
    00144                                  MED_CONNECTIVITY, MED_NODAL,&coordinatechangement,
    00145                                    &geotransformation)) < 0) {
    00146         MESSAGE("ERROR : number of cell ...");
    00147         goto ERROR;
    00148       }
    00149
    00150       /* read cells connectivity in the mesh */
    00151       if ((connectivity = (med_int *) malloc(sizeof(med_int)*nelt*(geotype%100))) == NULL) {
    00152         MESSAGE("ERROR : memory allocation ...");
    00153         goto ERROR;
    00154       }
    00155
    00156       if (MEDmeshElementConnectivityRd(fid, meshname, MED_NO_DT, MED_NO_IT, MED_CELL,
    00157                                        geotype, MED_NODAL, MED_FULL_INTERLACE, connectivity) < 0) {
    00158         MESSAGE("ERROR : cellconnectivity ...");
    00159         ISCRUTE(geotype);
    00160         free(connectivity);
    00161         goto ERROR;
    00162       }
    00163
    00164       /* memory deallocation */
    00165       if (connectivity) {
    00166         free(connectivity);
    00167         connectivity = NULL;
    00168       }
    00169
    00170
    00171     }
    00172
    00173   }
    00174
    00175   /*
    00176    * ... we know that the family number of nodes and elements is 0, a real working would check ...
    00177    */
    00178   ret=0;
    00179  ERROR:
    00180
    00181   /* close MED file */
    00182   if (MEDfileClose(fid) < 0) {
    00183     MESSAGE("ERROR : close file");
    00184     ret=-1;
    00185   }
    00186
    00187   return ret;
    00188 }
    00189