# UsesCase_MEDmesh_5.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  * Mesh Use case 5 : read a 2D structured mesh
    00020  *                   5x3 cartesian grid
    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   const char meshname[MED_NAME_SIZE+1]="2D structured mesh";
    00033   med_int spacedim;
    00034   med_int meshdim;
    00035   char meshdescription[MED_COMMENT_SIZE+1];
    00036   char axisname[2*MED_SNAME_SIZE+1];
    00037   char unitname[2*MED_SNAME_SIZE+1];
    00038   char dtunit[MED_SNAME_SIZE+1];
    00039   med_mesh_type meshtype;
    00040   med_axis_type axistype;
    00041   med_grid_type gridtype;
    00042   med_int axis, size ;
    00043   med_float *cooXaxis = NULL;
    00044   med_float *cooYaxis = NULL;
    00045   med_bool coordinatechangement;
    00046   med_bool geotransformation;
    00047   med_int nstep;
    00048   med_sorting_type sortingtype;
    00049   int j;
    00050   int ret=-1;
    00051   int ncell=0;
    00052   char *cellsname=NULL;
    00053
    00054   /* open MED file */
    00055   fid = MEDfileOpen("UsesCase_MEDmesh_4.med",MED_ACC_RDONLY);
    00056   if (fid < 0) {
    00057     MESSAGE("ERROR : open file ...");
    00058     goto ERROR;
    00059   }
    00060
    00061   /* read mesh informations : meshname, mesh dimension, space dimension ... */
    00062   if (MEDmeshInfoByName(fid, meshname, &spacedim, &meshdim, &meshtype, meshdescription,
    00063                         dtunit, &sortingtype, &nstep, &axistype, axisname, unitname) < 0) {
    00064     MESSAGE("ERROR : mesh info ...");
    00065     goto ERROR;
    00066   }
    00067
    00068   /* read the grid type : MED_CARTESIAN_GRID or MED_CURVILINEAR_GRID */
    00069   if (MEDmeshGridTypeRd(fid, meshname, &gridtype) < 0) {
    00070     MESSAGE("ERROR : read grid type ...");
    00071   }
    00072
    00073   /*
    00074    * ... we know that we the mesh is a cartesian grid,
    00075    * a real code working would check  ...
    00076    */
    00077
    00078   /* read the axis coordinates (MED_CARTESIAN coordinates system */
    00079   /* X */
    00080   axis = 1;
    00081   if ((size = MEDmeshnEntity(fid, meshname, MED_NO_DT, MED_NO_IT,
    00082                              MED_NODE, MED_NONE, MED_COORDINATE_AXIS1, MED_NO_CMODE,
    00083                              &coordinatechangement, &geotransformation)) < 0) {
    00084     MESSAGE("ERROR : number of coordinates on X axis ...");
    00085     goto ERROR;
    00086   }
    00087   ncell = size-1;
    00088
    00089   if ((cooXaxis = (med_float *) malloc(sizeof(med_float)*size)) == NULL) {
    00090     MESSAGE("ERROR : memory allocation ...");
    00091     goto ERROR;
    00092   }
    00093   if (MEDmeshGridIndexCoordinateRd(fid, meshname, MED_NO_DT, MED_NO_IT,
    00094                                    axis, cooXaxis) < 0) {
    00095     MESSAGE("ERROR : read axis X coordinates ...");
    00096     free(cooXaxis);
    00097     goto ERROR;
    00098   }
    00099
    00100   free(cooXaxis);
    00101
    00102   /* Y */
    00103   axis = 2;
    00104   if ((size = MEDmeshnEntity(fid, meshname, MED_NO_DT, MED_NO_IT,
    00105                              MED_NODE, MED_NONE, MED_COORDINATE_AXIS2, MED_NO_CMODE,
    00106                              &coordinatechangement, &geotransformation)) < 0) {
    00107     MESSAGE("ERROR : number of coordinates on Y axis ...");
    00108     goto ERROR;
    00109   }
    00110   ncell = ncell * (size-1);
    00111
    00112   if ((cooYaxis = (med_float *) malloc(sizeof(med_float)*size)) == NULL) {
    00113     MESSAGE("ERROR : memory allocation ...");
    00114     goto ERROR;
    00115   }
    00116   if (MEDmeshGridIndexCoordinateRd(fid, meshname, MED_NO_DT, MED_NO_IT,
    00117                                    axis, cooYaxis) < 0) {
    00118     MESSAGE("ERROR : read axis Y coordinates ...");
    00119     free(cooYaxis);
    00120     goto ERROR;
    00121   }
    00122
    00123   free(cooYaxis);
    00124
    00125   cellsname = (char *) malloc((sizeof(char))*ncell*MED_SNAME_SIZE+1);
    00126   if (MEDmeshEntityNameRd(fid, meshname, MED_NO_DT, MED_NO_IT,
    00127                           MED_CELL, MED_QUAD4, cellsname) < 0)  {
    00128     MESSAGE("ERROR : read cells name ...");
    00129     free(cellsname);
    00130     goto ERROR;
    00131   }
    00132   free(cellsname);
    00133
    00134   ret=0;
    00135  ERROR:
    00136
    00137   /* close MED file */
    00138   if (MEDfileClose(fid) < 0) {
    00139     MESSAGE("ERROR : close file ...");
    00140     ret = -1;
    00141   }
    00142
    00143   return ret;
    00144 }