MED fichier
UsesCase_MEDmesh_3.c
Aller à la documentation de ce fichier.
1 /* This file is part of MED.
2  *
3  * COPYRIGHT (C) 1999 - 2021 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 /*
20  * Use case 3 : read an unstructured mesh : generic approach
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  char meshname[MED_NAME_SIZE+1]="";
33  char meshdescription[MED_COMMENT_SIZE+1]="";
34  med_int meshdim=0;
35  med_int spacedim=0;
36  med_sorting_type sortingtype;
37  med_int nstep;
38  med_mesh_type meshtype;
39  med_axis_type axistype;
40  char *axisname="";
41  char *unitname="";
42  char dtunit[MED_SNAME_SIZE+1]="";
43  med_float *coordinates = NULL;
44  med_int nnodes = 0;
45  med_int ngeo = 0;
46  med_int nelt=0;
47  med_int *connectivity = NULL;
48  med_bool coordinatechangement;
49  med_bool geotransformation;
50  med_int i, j, it, nmesh;
51  med_geometry_type geotype;
53  char geotypename[MED_NAME_SIZE+1];
54  int ret=-1;
55 
56 
57  /* open MED file with READ ONLY access mode */
58  fid = MEDfileOpen("UsesCase_MEDmesh_1.med",MED_ACC_RDONLY);
59  if (fid < 0) {
60  MESSAGE("ERROR : open file in READ ONLY ACCESS mode ...");
61  goto ERROR;
62  }
63 
64  /* read how many mesh in the file */
65  if ((nmesh = MEDnMesh(fid)) < 0) {
66  MESSAGE("ERROR : read how many mesh ...");
67  goto ERROR;
68  }
69 
70  for (i=0;i<nmesh;i++) {
71 
72  /* read computation space dimension */
73  if ((spacedim = MEDmeshnAxis(fid, i+1)) < 0) {
74  MESSAGE("ERROR : read computation space dimension ...");
75  goto ERROR;
76  }
77 
78  /* memory allocation */
79  if ((axisname = (char*) malloc(MED_SNAME_SIZE*spacedim+1)) == NULL) {
80  MESSAGE("ERROR : memory allocation ...");
81  goto ERROR;
82  }
83  if ((unitname = (char*) malloc(MED_SNAME_SIZE*spacedim+1)) == NULL) {
84  MESSAGE("ERROR : memory allocation ...");
85  goto ERROR;
86  }
87 
88  /* read mesh informations : meshname, mesh dimension, mesh type ... */
89  if (MEDmeshInfo(fid, i+1, meshname, &spacedim, &meshdim, &meshtype, meshdescription,
90  dtunit, &sortingtype, &nstep, &axistype, axisname, unitname) < 0) {
91  MESSAGE("ERROR : mesh info ...");
92  free(axisname); free(unitname);
93  goto ERROR;
94  }
95 
96  free(axisname);
97  free(unitname);
98 
99  /* read how many nodes in the mesh */
100  if ((nnodes = MEDmeshnEntity(fid, meshname, MED_NO_DT, MED_NO_IT, MED_NODE, MED_NO_GEOTYPE,
101  MED_COORDINATE, MED_NO_CMODE,&coordinatechangement,
102  &geotransformation)) < 0) {
103  MESSAGE("ERROR : number of nodes ...");
104  goto ERROR;
105  }
106 
107  /* read mesh nodes coordinates */
108  if ((coordinates = (med_float*) malloc(sizeof(med_float)*nnodes*spacedim)) == NULL) {
109  MESSAGE("ERROR : memory allocation ...");
110  goto ERROR;
111  }
112 
114  coordinates) < 0) {
115  MESSAGE("ERROR : nodes coordinates ...");
116  free(coordinates);
117  goto ERROR;
118  }
119 
120  if (coordinates)
121  free(coordinates);
122 
123  /* read number of geometrical types for cells */
124  if ((ngeo = MEDmeshnEntity(fid, meshname, MED_NO_DT, MED_NO_IT, MED_CELL,MED_GEO_ALL,
125  MED_CONNECTIVITY, MED_NODAL,&coordinatechangement,
126  &geotransformation)) < 0) {
127  MESSAGE("ERROR : number of geo type ...");
128  ISCRUTE_int(geotype);
129  goto ERROR;
130  }
131 
132  for (it=1; it<=ngeo; it++) {
133 
134  /* get geometry type */
135  if (MEDmeshEntityInfo(fid, meshname, MED_NO_DT, MED_NO_IT,MED_CELL,it,
136  geotypename,&geotype) < 0) {
137  MESSAGE("ERROR : get geo type ...");
138  ISCRUTE(it);
139  goto ERROR;
140  }
141 
142  /* how many cells of type geotype ? */
143  if ((nelt = MEDmeshnEntity(fid, meshname, MED_NO_DT, MED_NO_IT, MED_CELL,geotype,
144  MED_CONNECTIVITY, MED_NODAL,&coordinatechangement,
145  &geotransformation)) < 0) {
146  MESSAGE("ERROR : number of cell ...");
147  goto ERROR;
148  }
149 
150  /* read cells connectivity in the mesh */
151  if ((connectivity = (med_int *) malloc(sizeof(med_int)*nelt*(geotype%100))) == NULL) {
152  MESSAGE("ERROR : memory allocation ...");
153  goto ERROR;
154  }
155 
157  geotype, MED_NODAL, MED_FULL_INTERLACE, connectivity) < 0) {
158  MESSAGE("ERROR : cell connectivity ...");
159  ISCRUTE_int(geotype);
160  free(connectivity);
161  goto ERROR;
162  }
163 
164  /* memory deallocation */
165  if (connectivity) {
166  free(connectivity);
167  connectivity = NULL;
168  }
169 
170 
171  }
172 
173  }
174 
175  /*
176  * ... we know that the family number of nodes and elements is 0, a real code would check ...
177  */
178  ret=0;
179  ERROR:
180 
181  /* close MED file */
182  if (MEDfileClose(fid) < 0) {
183  MESSAGE("ERROR : close file");
184  ret=-1;
185  }
186 
187  return ret;
188 }
189 
main
int main(int argc, char **argv)
Definition: UsesCase_MEDmesh_3.c:30
MED_FULL_INTERLACE
Definition: med.h:96
MED_COMMENT_SIZE
#define MED_COMMENT_SIZE
Definition: med.h:79
MEDmeshnEntity
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 étape de calcul donnée.
Definition: MEDmeshnEntity.c:44
MEDfileOpen
MEDC_EXPORT med_idt MEDfileOpen(const char *const filename, const med_access_mode accessmode)
Ouverture d'un fichier MED.
Definition: MEDfileOpen.c:42
med_geometry_type
int med_geometry_type
Definition: med.h:194
MED_SNAME_SIZE
#define MED_SNAME_SIZE
Definition: med.h:82
MED_NODAL
Definition: med.h:255
ISCRUTE_int
#define ISCRUTE_int(entier)
Definition: med_utils.h:314
MED_NO_GEOTYPE
#define MED_NO_GEOTYPE
Definition: med.h:232
med_idt
hid_t med_idt
Definition: med.h:331
MED_ACC_RDONLY
Definition: med.h:120
med_sorting_type
med_sorting_type
Definition: med.h:309
MED_NO_CMODE
Definition: med.h:255
MED_CELL
Definition: med.h:143
MESSAGE
#define MESSAGE(chaine)
Definition: med_utils.h:324
MEDmeshEntityInfo
MEDC_EXPORT med_err MEDmeshEntityInfo(const med_idt fid, const char *const meshname, const med_int numdt, const med_int numit, const med_entity_type entitype, const int geotypeit, char *const geotypename, med_geometry_type *const geotype)
Cette routine indique de façon itérative les types géométriques disponibles dans un maillage.
Definition: MEDmeshEntityInfo.c:41
med_int
int med_int
Definition: med.h:342
MED_GET_CELL_GEOMETRY_TYPE
med_geometry_type MED_GET_CELL_GEOMETRY_TYPE[MED_N_CELL_FIXED_GEO+2]
Definition: MEDiterators.c:55
med.h
ISCRUTE
#define ISCRUTE(entier)
Definition: med_utils.h:313
med_bool
med_bool
Definition: med.h:260
med_float
double med_float
Definition: med.h:336
MED_GEO_ALL
#define MED_GEO_ALL
Definition: med.h:236
MED_NO_DT
#define MED_NO_DT
Definition: med.h:320
MED_CONNECTIVITY
Definition: med.h:149
MEDmeshElementConnectivityRd
MEDC_EXPORT med_err MEDmeshElementConnectivityRd(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_connectivity_mode cmode, const med_switch_mode switchmode, med_int *const connectivity)
Cette routine permet de lire dans un maillage le tableau des connectivités pour un type géométrique d...
Definition: MEDmeshElementConnectivityRd.c:40
MED_NODE
Definition: med.h:143
MEDmeshnAxis
MEDC_EXPORT med_int MEDmeshnAxis(const med_idt fid, const int meshit)
Cette routine permet de lire dans un maillage le nombre d'axes du repère des coordonnées des noeuds.
Definition: MEDmeshnAxis.c:35
med_mesh_type
med_mesh_type
Definition: med.h:131
MEDmeshNodeCoordinateRd
MEDC_EXPORT med_err MEDmeshNodeCoordinateRd(const med_idt fid, const char *const meshname, const med_int numdt, const med_int numit, const med_switch_mode switchmode, med_float *const coordinates)
Cette routine permet de lire dans un maillage le tableau des coordonnées des noeuds,...
Definition: MEDmeshNodeCoordinateRd.c:37
MED_NAME_SIZE
#define MED_NAME_SIZE
Definition: med.h:81
MEDnMesh
MEDC_EXPORT med_int MEDnMesh(const med_idt fid)
Cette routine permet de lire le nombre de maillages dans un fichier.
Definition: MEDnMesh.c:34
MEDfileClose
MEDC_EXPORT med_err MEDfileClose(med_idt fid)
Fermeture d'un fichier MED.
Definition: MEDfileClose.c:30
med_axis_type
med_axis_type
Definition: med.h:258
MEDmeshInfo
MEDC_EXPORT med_err MEDmeshInfo(const med_idt fid, const int meshit, 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 dans un fichier.
Definition: MEDmeshInfo.c:43
MED_NO_IT
#define MED_NO_IT
Definition: med.h:321
MED_COORDINATE
Definition: med.h:149
med_utils.h