MED fichier
UsesCase_MEDmesh_5.c
Aller à la documentation de ce fichier.
1 /* This file is part of MED.
2  *
3  * COPYRIGHT (C) 1999 - 2017 EDF R&D, CEA/DEN
4  * MED is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU Lesser General Public License as published by
6  * the Free Software Foundation, either version 3 of the License, or
7  * (at your option) any later version.
8  *
9  * MED is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public License
15  * along with MED. If not, see <http://www.gnu.org/licenses/>.
16  */
17 
18 /*
19  * Mesh Use case 5 : read a 2D structured mesh
20  * 5x3 cartesian grid
21  *
22  */
23 
24 #include <med.h>
25 #define MESGERR 1
26 #include <med_utils.h>
27 
28 #include <string.h>
29 
30 int main (int argc, char **argv) {
31  med_idt fid;
32  const char meshname[MED_NAME_SIZE+1]="2D structured mesh";
33  med_int spacedim;
34  med_int meshdim;
35  char meshdescription[MED_COMMENT_SIZE+1];
36  char axisname[2*MED_SNAME_SIZE+1];
37  char unitname[2*MED_SNAME_SIZE+1];
38  char dtunit[MED_SNAME_SIZE+1];
39  med_mesh_type meshtype;
40  med_axis_type axistype;
41  med_grid_type gridtype;
42  med_int axis, size ;
43  med_float *cooXaxis = NULL;
44  med_float *cooYaxis = NULL;
45  med_bool coordinatechangement;
46  med_bool geotransformation;
47  med_int nstep;
48  med_sorting_type sortingtype;
49  int j;
50  int ret=-1;
51  int ncell=0;
52  char *cellsname=NULL;
53 
54  /* open MED file */
55  fid = MEDfileOpen("UsesCase_MEDmesh_4.med",MED_ACC_RDONLY);
56  if (fid < 0) {
57  MESSAGE("ERROR : open file ...");
58  goto ERROR;
59  }
60 
61  /* read mesh informations : meshname, mesh dimension, space dimension ... */
62  if (MEDmeshInfoByName(fid, meshname, &spacedim, &meshdim, &meshtype, meshdescription,
63  dtunit, &sortingtype, &nstep, &axistype, axisname, unitname) < 0) {
64  MESSAGE("ERROR : mesh info ...");
65  goto ERROR;
66  }
67 
68  /* read the grid type : MED_CARTESIAN_GRID or MED_CURVILINEAR_GRID */
69  if (MEDmeshGridTypeRd(fid, meshname, &gridtype) < 0) {
70  MESSAGE("ERROR : read grid type ...");
71  }
72 
73  /*
74  * ... we know that we the mesh is a cartesian grid,
75  * a real code would check ...
76  */
77 
78  /* read the axis coordinates (MED_CARTESIAN coordinates system */
79  /* X */
80  axis = 1;
81  if ((size = MEDmeshnEntity(fid, meshname, MED_NO_DT, MED_NO_IT,
83  &coordinatechangement, &geotransformation)) < 0) {
84  MESSAGE("ERROR : number of coordinates on X axis ...");
85  goto ERROR;
86  }
87  ncell = size-1;
88 
89  if ((cooXaxis = (med_float *) malloc(sizeof(med_float)*size)) == NULL) {
90  MESSAGE("ERROR : memory allocation ...");
91  goto ERROR;
92  }
94  axis, cooXaxis) < 0) {
95  MESSAGE("ERROR : read axis X coordinates ...");
96  free(cooXaxis);
97  goto ERROR;
98  }
99 
100  free(cooXaxis);
101 
102  /* Y */
103  axis = 2;
104  if ((size = MEDmeshnEntity(fid, meshname, MED_NO_DT, MED_NO_IT,
106  &coordinatechangement, &geotransformation)) < 0) {
107  MESSAGE("ERROR : number of coordinates on Y axis ...");
108  goto ERROR;
109  }
110  ncell = ncell * (size-1);
111 
112  if ((cooYaxis = (med_float *) malloc(sizeof(med_float)*size)) == NULL) {
113  MESSAGE("ERROR : memory allocation ...");
114  goto ERROR;
115  }
116  if (MEDmeshGridIndexCoordinateRd(fid, meshname, MED_NO_DT, MED_NO_IT,
117  axis, cooYaxis) < 0) {
118  MESSAGE("ERROR : read axis Y coordinates ...");
119  free(cooYaxis);
120  goto ERROR;
121  }
122 
123  free(cooYaxis);
124 
125  cellsname = (char *) malloc((sizeof(char))*ncell*MED_SNAME_SIZE+1);
126  if (MEDmeshEntityNameRd(fid, meshname, MED_NO_DT, MED_NO_IT,
127  MED_CELL, MED_QUAD4, cellsname) < 0) {
128  MESSAGE("ERROR : read cells name ...");
129  free(cellsname);
130  goto ERROR;
131  }
132  free(cellsname);
133 
134  ret=0;
135  ERROR:
136 
137  /* close MED file */
138  if (MEDfileClose(fid) < 0) {
139  MESSAGE("ERROR : close file ...");
140  ret = -1;
141  }
142 
143  return ret;
144 }
#define MED_QUAD4
Definition: med.h:199
MEDC_EXPORT med_err MEDfileClose(med_idt fid)
Fermeture d'un fichier MED.
Definition: MEDfileClose.c:30
Definition: med.h:139
#define MED_NO_IT
Definition: med.h:314
#define MESSAGE(chaine)
Definition: med_utils.h:316
double med_float
Definition: med.h:329
med_mesh_type
Definition: med.h:127
Definition: med.h:139
med_sorting_type
Definition: med.h:304
#define MED_NONE
Definition: med.h:226
med_axis_type
Definition: med.h:253
int med_int
Definition: med.h:335
int main(int argc, char **argv)
MEDC_EXPORT med_err MEDmeshEntityNameRd(const med_idt fid, const char *const meshname, const med_int numdt, const med_int numit, const med_entity_type entitype, const med_geometry_type geotype, char *const name)
Cette routine permet de lire les noms d'un type d'entité d'un maillage.
#define MED_COMMENT_SIZE
Definition: med.h:75
MEDC_EXPORT med_idt MEDfileOpen(const char *const filename, const med_access_mode accessmode)
Ouverture d'un fichier MED.
Definition: MEDfileOpen.c:41
hid_t med_idt
Definition: med.h:324
MEDC_EXPORT med_int MEDmeshnEntity(const med_idt fid, const char *const meshname, const med_int numdt, const med_int numit, const med_entity_type entitype, const med_geometry_type geotype, const med_data_type datatype, const med_connectivity_mode cmode, med_bool *const changement, med_bool *const transformation)
Cette routine permet de lire le nombre d'entités dans un maillage pour une séquence de calcul donnée...
#define MED_NO_DT
Definition: med.h:313
#define MED_NAME_SIZE
Definition: med.h:77
MEDC_EXPORT med_err MEDmeshGridTypeRd(const med_idt fid, const char *const meshname, med_grid_type *const gridtype)
Cette routine permet de lire le type d'un maillage structuré (MED_STRUCTURED_MESH).
med_grid_type
Definition: med.h:133
#define MED_SNAME_SIZE
Definition: med.h:78
med_bool
Definition: med.h:255
MEDC_EXPORT med_err MEDmeshGridIndexCoordinateRd(const med_idt fid, const char *const meshname, const med_int numdt, const med_int numit, const med_int axis, med_float *const gridindex)
Cette routine permet la lecture des coordonnées des noeuds d'un maillage structuré selon un axe du re...
MEDC_EXPORT med_err MEDmeshInfoByName(const med_idt fid, const char *const meshname, med_int *const spacedim, med_int *const meshdim, med_mesh_type *const meshtype, char *const description, char *const dtunit, med_sorting_type *const sortingtype, med_int *const nstep, med_axis_type *const axistype, char *const axisname, char *const axisunit)
Cette routine permet de lire les informations relatives à un maillage en précisant son nom...