MED fichier
UsesCase_MEDmesh_16.c
Aller à la documentation de ce fichier.
1 /* This file is part of MED.
2  *
3  * COPYRIGHT (C) 1999 - 2023 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  * How to create an unstructured mesh with polyhedrons
20  *
21  * Use case 16 : read a 3D unstructured mesh with 2 polyhedrons
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] = "3D Unstructured Mesh With 2 polyhedrons";
33  char meshdescription[MED_COMMENT_SIZE+1];
34  med_int meshdim;
35  med_int spacedim;
36  med_sorting_type sortingtype;
37  med_int nstep;
38  med_mesh_type meshtype;
39  med_axis_type axistype;
40  char axisname[3*MED_SNAME_SIZE+1];
41  char unitname[3*MED_SNAME_SIZE+1];
42  char dtunit[MED_SNAME_SIZE+1];
43  med_float *coordinates = NULL;
44  med_int nnodes = 0;
45  med_int npoly = 0;
46  med_int faceindexsize;
47  med_int nodeindexsize;
48  med_int *faceindex = NULL;
49  med_int *nodeindex = NULL;
50  med_int *connectivity = NULL;
51  med_int connectivitysize;
52  med_bool coordinatechangement;
53  med_bool geotransformation;
54  int i;
55  int k,ind1,ind2;
56  int j, jind1,jind2;
57 
58  /* open MED file with READ ONLY access mode */
59  fid = MEDfileOpen("./UsesCase_MEDmesh_15.med",MED_ACC_RDONLY);
60  if (fid < 0) {
61  MESSAGE("ERROR : open file in READ ONLY ACCESS mode ...");
62  return -1;
63  }
64 
65  /*
66  * ... we know that the MED file has only one mesh,
67  * a real code would check ...
68  */
69 
70  /* read mesh informations : mesh dimension, space dimension ... */
71  if (MEDmeshInfoByName(fid, meshname, &spacedim, &meshdim, &meshtype, meshdescription,
72  dtunit, &sortingtype, &nstep, &axistype, axisname, unitname) < 0) {
73  MESSAGE("ERROR : mesh info ...");
74  return -1;
75  }
76 
77  /* read how many nodes are in the mesh */
78  if ((nnodes = MEDmeshnEntity(fid, meshname, MED_NO_DT, MED_NO_IT, MED_NODE, MED_POINT1,
79  MED_COORDINATE, MED_NO_CMODE,&coordinatechangement, &geotransformation)) < 0)
80  { MESSAGE("ERROR : number of nodes ...");
81  return -1;
82  }
83 
84  /*
85  * ... we know that we only have MED_POLYHEDRON cells in the mesh,
86  * a real code would check for MED geometry cell types ...
87  */
88 
89  /* We read how many polyhedrons are in the mesh (using nodal connectivity mode) */
90  /* We get the size of the polyhedrons/face index array.
91  * As an index of the face index array give the location of the first face and so the
92  * number of polyhedrons
93  */
94  if ((faceindexsize = MEDmeshnEntity(fid,meshname,MED_NO_DT,MED_NO_IT,
96  &coordinatechangement, &geotransformation)) < 0)
97  { MESSAGE("ERROR : read number of polyhedrons ...");
98  return -1;
99  }
100  npoly = faceindexsize-1;
101  ISCRUTE(npoly);
102 
103  if ((nodeindexsize = MEDmeshnEntity(fid,meshname,MED_NO_DT,MED_NO_IT,
105  &coordinatechangement, &geotransformation)) < 0)
106  { MESSAGE("ERROR : read number of polyhedrons ...");
107  return -1;
108  }
109  ISCRUTE(nodeindexsize);
110 
111  /* how many nodes for the polyhedron connectivity ? */
112  if ((connectivitysize = MEDmeshnEntity(fid,meshname,MED_NO_DT,MED_NO_IT,
114  &coordinatechangement, &geotransformation)) < 0)
115  { MESSAGE("ERROR : read connectivity size ...");
116  return -1;
117  }
118  ISCRUTE(connectivitysize);
119 
120  /* read mesh nodes coordinates */
121  if ((coordinates = (med_float*) malloc(sizeof(med_float)*nnodes*spacedim)) == NULL) {
122  MESSAGE("ERROR : memory allocation ...");
123  return -1;
124  }
125 
127  coordinates) < 0) {
128  MESSAGE("ERROR : nodes coordinates ...");
129  return -1;
130  }
131  for (i=0;i<nnodes*spacedim;i++)
132  printf("%f - ",*(coordinates+i));
133  printf("\n");
134 
135 
136  /* read polyhedron connectivity */
137  faceindex = (med_int *) malloc(sizeof(med_int)*faceindexsize);
138  nodeindex = (med_int *) malloc(sizeof(med_int)*nodeindexsize);
139  connectivity = (med_int *) malloc(sizeof(med_int)*connectivitysize);
140 
142  faceindex,nodeindex,connectivity) < 0)
143  { MESSAGE("ERROR : read polyhedron connectivity ...");
144  return -1;
145  }
146 
147  for (i=0;i<npoly;i++)
148  {
149  printf(">> MED_POLYHEDRON %d : \n",i+1);
150  printf("---- Face Index ----- : [\n");
151  ind1 = *(faceindex+i)-1;
152  ind2 = *(faceindex+i+1)-1;
153  for (k=ind1;k<ind2;k++)
154  printf(IFORMAT" ",*(nodeindex+k));
155  printf(" ] \n");
156  printf("---- Connectivity ----- : [\n");
157  for (k=ind1;k<ind2;k++)
158  {
159  jind1 = *(nodeindex+k)-1;
160  jind2 = *(nodeindex+k+1)-1;
161  for (j=jind1;j<jind2;j++)
162  printf(IFORMAT" ",*(connectivity+j));
163  printf(" \n");
164  }
165  printf(" ] \n");
166  }
167 
168  /*
169  * ... we know that the family number of nodes and elements is 0, a real code would check ...
170  */
171 
172  /* close MED file */
173  if (MEDfileClose(fid) < 0) {
174  MESSAGE("ERROR : close file");
175  return -1;
176  }
177 
178  /* memory deallocation */
179  if (coordinates)
180  free(coordinates);
181 
182  if (faceindex)
183  free(faceindex);
184 
185  if (nodeindex)
186  free(nodeindex);
187 
188  if (connectivity)
189  free(connectivity);
190 
191  return 0;
192 }
MED_ACC_RDONLY
Definition: med.h:122
MED_COMMENT_SIZE
#define MED_COMMENT_SIZE
Definition: med.h:81
MED_INDEX_FACE
Definition: med.h:153
MED_SNAME_SIZE
#define MED_SNAME_SIZE
Definition: med.h:84
med_idt
hid_t med_idt
Definition: med.h:333
med_sorting_type
med_sorting_type
Definition: med.h:311
MED_INDEX_NODE
Definition: med.h:153
MESSAGE
#define MESSAGE(chaine)
Definition: med_utils.h:324
MED_FULL_INTERLACE
Definition: med.h:98
MEDmeshInfoByName
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.
Definition: MEDmeshInfoByName.c:42
med_int
int med_int
Definition: med.h:344
ISCRUTE
#define ISCRUTE(entier)
Definition: med_utils.h:313
med_bool
med_bool
Definition: med.h:262
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
med_float
double med_float
Definition: med.h:338
MED_COORDINATE
Definition: med.h:151
MED_NO_CMODE
Definition: med.h:257
IFORMAT
#define IFORMAT
Definition: med_utils.h:145
MED_POINT1
#define MED_POINT1
Definition: med.h:200
MED_NO_DT
#define MED_NO_DT
Definition: med.h:322
MEDfileClose
MEDC_EXPORT med_err MEDfileClose(med_idt fid)
Fermeture d'un fichier MED.
Definition: MEDfileClose.c:30
med_mesh_type
med_mesh_type
Definition: med.h:133
MED_CELL
Definition: med.h:145
MED_NAME_SIZE
#define MED_NAME_SIZE
Definition: med.h:83
med_utils.h
MED_NODE
Definition: med.h:145
med_axis_type
med_axis_type
Definition: med.h:260
med.h
MEDmeshPolyhedronRd
MEDC_EXPORT med_err MEDmeshPolyhedronRd(const med_idt fid, const char *const meshname, const med_int numdt, const med_int numit, const med_entity_type entitype, const med_connectivity_mode cmode, med_int *const faceindex, med_int *const nodeindex, med_int *const connectivity)
Cette routine permet la lecture dans un maillage des connectivités de polyèdres.
Definition: MEDmeshPolyhedronRd.c:45
MED_CONNECTIVITY
Definition: med.h:151
MED_NO_IT
#define MED_NO_IT
Definition: med.h:323
MEDfileOpen
MEDC_EXPORT med_idt MEDfileOpen(const char *const filename, const med_access_mode accessmode)
Ouverture d'un fichier MED.
Definition: MEDfileOpen.c:42
main
int main(int argc, char **argv)
Definition: UsesCase_MEDmesh_16.c:30
MED_POLYHEDRON
#define MED_POLYHEDRON
Definition: med.h:227
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_NODAL
Definition: med.h:257