MED fichier
UsesCase_MEDmesh_11.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  * Use case 11 : read a 2D unstructured mesh with 15 nodes, 8 triangular cells, 4 quadragular cells with
20  * nodes families
21  */
22 
23 #include <med.h>
24 #define MESGERR 1
25 #include <med_utils.h>
26 
27 #include <string.h>
28 
29 int main (int argc, char **argv) {
30  med_idt fid;
31  const char meshname[MED_NAME_SIZE+1] = "2D unstructured mesh";
32  char meshdescription[MED_COMMENT_SIZE+1]="";
33  med_int meshdim;
34  med_int spacedim;
35  med_sorting_type sortingtype;
36  med_int nstep;
37  med_mesh_type meshtype;
38  med_axis_type axistype;
39  char axisname[2*MED_SNAME_SIZE+1]="";
40  char unitname[2*MED_SNAME_SIZE+1]="";
41  char dtunit[MED_SNAME_SIZE+1];
42  med_float *coordinates = NULL;
43  med_int nnodes = 0;
44  med_int *triaconnectivity = NULL;
45  med_int ntria3 = 0;
46  med_int *quadconnectivity = NULL;
47  med_int nquad4 = 0;
48  med_bool coordinatechangement;
49  med_bool geotransformation;
50  int i;
51  med_int nfamily, ngroup;
52  med_int familynumber;
53  char *groupname=NULL;
54  char familyname[MED_NAME_SIZE+1]="";
55  med_int *familynumbers = NULL;
56  med_int nfamilynumber = 0;
57  int ret=-1;
58 
59  /* open MED file with READ ONLY access mode */
60  fid = MEDfileOpen("UsesCase_MEDmesh_10.med",MED_ACC_RDONLY);
61  if (fid < 0) {
62  MESSAGE("ERROR : open file in READ ONLY ACCESS mode ...");
63  goto ERROR;
64  }
65 
66  /*
67  * ... we know that the MED file has only one mesh,
68  * a real code would check ...
69  */
70 
71  /* read mesh informations : mesh dimension, space dimension ... */
72  if (MEDmeshInfoByName(fid, meshname, &spacedim, &meshdim, &meshtype, meshdescription,
73  dtunit, &sortingtype, &nstep, &axistype, axisname, unitname) < 0) {
74  MESSAGE("ERROR : mesh info ...");
75  goto ERROR;
76  }
77 
78  /* read how many nodes in the mesh */
79  if ((nnodes = MEDmeshnEntity(fid, meshname, MED_NO_DT, MED_NO_IT, MED_NODE, MED_NO_GEOTYPE,
80  MED_COORDINATE, MED_NO_CMODE,&coordinatechangement,
81  &geotransformation)) < 0) {
82  MESSAGE("ERROR : number of nodes ...");
83  goto ERROR;
84  }
85 
86  /*
87  * ... we know that we only have MED_TRIA3 and MED_QUAD4 in the mesh,
88  * a real code would check all MED geometry cell types ...
89  */
90 
91  /* read how many triangular cells in the mesh */
92  if ((ntria3 = MEDmeshnEntity(fid, meshname, MED_NO_DT, MED_NO_IT, MED_CELL,MED_TRIA3,
93  MED_CONNECTIVITY, MED_NODAL,&coordinatechangement,
94  &geotransformation)) < 0) {
95  MESSAGE("ERROR : number of MED_TRIA3 ...");
96  goto ERROR;
97  }
98 
99  /* read how many quadrangular cells in the mesh */
100  if ((nquad4 = MEDmeshnEntity(fid, meshname, MED_NO_DT, MED_NO_IT, MED_CELL,MED_QUAD4,
101  MED_CONNECTIVITY, MED_NODAL, &coordinatechangement,
102  &geotransformation)) < 0) {
103  MESSAGE("ERROR : number of MED_QUAD4 ...");
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  free(coordinates);
121 
122  /* read cells connectivity in the mesh */
123  if ((triaconnectivity = (med_int *) malloc(sizeof(med_int)*ntria3*3)) == NULL) {
124  MESSAGE("ERROR : memory allocation ...");
125  goto ERROR;
126  }
128  MED_TRIA3, MED_NODAL, MED_FULL_INTERLACE, triaconnectivity) < 0) {
129  MESSAGE("ERROR : MED_TRIA3 connectivity ...");
130  free(triaconnectivity);
131  goto ERROR;
132  }
133  free(triaconnectivity);
134 
135  if ((quadconnectivity = (med_int *) malloc(sizeof(med_int)*nquad4*4)) == NULL) {
136  MESSAGE("ERROR : memory allocation ...");
137  goto ERROR;
138  }
140  MED_QUAD4, MED_NODAL, MED_FULL_INTERLACE, quadconnectivity) < 0) {
141  MESSAGE("ERROR : MED_QUAD4 connectivity ...");
142  free(quadconnectivity);
143  goto ERROR;
144  }
145  free(quadconnectivity);
146 
147  /*
148  * read families of entities...
149  */
150  if ((nfamily = MEDnFamily(fid,meshname)) < 0) {
151  MESSAGE("ERROR : read number of family ...");
152  goto ERROR;
153  }
154 
155  for (i=0; i<nfamily ; i++) {
156 
157  if ((ngroup = MEDnFamilyGroup(fid, meshname, i+1)) < 0) {
158  MESSAGE("ERROR : read number of group in a family ...");
159  goto ERROR;
160  }
161 
162  if (ngroup > 0) {
163  if ((groupname = (char*) malloc(sizeof(char)*MED_LNAME_SIZE*ngroup+1)) == NULL) {
164  MESSAGE("ERROR : memory allocation ...");
165  goto ERROR;
166  }
167 
168  if (MEDfamilyInfo(fid, meshname, i+1, familyname, &familynumber, groupname) < 0) {
169  MESSAGE("ERROR : family info ...");
170  free(groupname);
171  goto ERROR;
172  }
173  free(groupname);
174  }
175 
176  }
177 
178  /* check for family numbers */
179  /* By convention, if there is no numbers in the file, it means that 0 is the family
180  number of all nodes */
181  if ((nfamilynumber = MEDmeshnEntity(fid, meshname, MED_NO_DT, MED_NO_IT,
183  &coordinatechangement, &geotransformation)) < 0) {
184  MESSAGE("ERROR : check family numbers nodes ...");
185  goto ERROR;
186  }
187 
188  if ((familynumbers = (med_int *) malloc(sizeof(med_int)*nnodes)) == NULL) {
189  MESSAGE("ERROR : memory allocation ...");
190  goto ERROR;
191  }
192 
193  if (nfamilynumber > 0) {
194  /* read family numbers for nodes */
195  if (MEDmeshEntityFamilyNumberRd(fid, meshname, MED_NO_DT, MED_NO_IT,
196  MED_NODE, MED_NONE, familynumbers) < 0) {
197  MESSAGE("ERROR : read family numbers nodes ...");
198  goto ERROR;
199  }
200  } else
201  for (i=0; i<nnodes; i++) *(familynumbers+i) = 0;
202 
203  for (i=0; i<nnodes; i++) {
204  printf(IFORMAT, *(familynumbers+i));
205  if (i+1 != nnodes)
206  printf(" - ");
207  else
208  printf("\n");
209  }
210 
211  if (familynumbers)
212  free(familynumbers);
213 
214  /* read family numbers for cells */
215 
216  if ((nfamilynumber = MEDmeshnEntity(fid, meshname, MED_NO_DT, MED_NO_IT,
218  &coordinatechangement, &geotransformation)) < 0) {
219  MESSAGE("ERROR : check family number tria3 ...")
220  goto ERROR;
221  }
222 
223  if ((familynumbers = (med_int *) malloc(sizeof(med_int)*ntria3)) == NULL) {
224  MESSAGE("ERROR : memory allocation ...");
225  goto ERROR;
226  }
227 
228  if (nfamilynumber > 0) {
229  if (MEDmeshEntityFamilyNumberRd(fid, meshname, MED_NO_DT, MED_NO_IT,
230  MED_CELL, MED_TRIA3, familynumbers) < 0) {
231  MESSAGE("ERROR : read family numbers tria3 ...");
232  }
233  } else
234  for (i=0; i<ntria3; i++) *(familynumbers+i) = 0;
235 
236  free (familynumbers);
237 
238  if ((nfamilynumber = MEDmeshnEntity(fid, meshname, MED_NO_DT, MED_NO_IT,
240  &coordinatechangement, &geotransformation)) < 0) {
241  MESSAGE("ERROR : check family number quad4 ...");
242  goto ERROR;
243  }
244 
245  if ((familynumbers = (med_int *) malloc(sizeof(med_int)*nquad4)) == NULL) {
246  MESSAGE("ERROR : memory allocation ...");
247  goto ERROR;
248  }
249 
250  if (nfamilynumber > 0) {
251  if (MEDmeshEntityFamilyNumberRd(fid, meshname, MED_NO_DT, MED_NO_IT,
252  MED_CELL, MED_QUAD4, familynumbers) < 0) {
253  MESSAGE("ERROR : read family numbers quad4 ...");
254  goto ERROR;
255  }
256  } else
257  for (i=0; i<nquad4; i++) *(familynumbers+i) = 0;
258 
259  free (familynumbers);
260 
261  ret=0;
262  ERROR:
263 
264  /* close MED file */
265  if (MEDfileClose(fid) < 0) {
266  MESSAGE("ERROR : close file");
267  ret= -1;
268  }
269 
270  return ret;
271 }
MED_TRIA3
#define MED_TRIA3
Definition: med.h:203
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_SNAME_SIZE
#define MED_SNAME_SIZE
Definition: med.h:82
MED_NODAL
Definition: med.h:255
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
med_int
int med_int
Definition: med.h:342
med.h
med_bool
med_bool
Definition: med.h:260
MED_FAMILY_NUMBER
Definition: med.h:149
med_float
double med_float
Definition: med.h:336
MEDnFamilyGroup
MEDC_EXPORT med_int MEDnFamilyGroup(const med_idt fid, const char *const meshname, const int famit)
Cette routine permet de lire le nombre de groupe dans une famille.
Definition: MEDnFamilyGroup.c:36
IFORMAT
#define IFORMAT
Definition: med_utils.h:145
MED_NO_DT
#define MED_NO_DT
Definition: med.h:320
MED_NONE
#define MED_NONE
Definition: med.h:231
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
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_LNAME_SIZE
#define MED_LNAME_SIZE
Definition: med.h:83
MED_NAME_SIZE
#define MED_NAME_SIZE
Definition: med.h:81
MEDfileClose
MEDC_EXPORT med_err MEDfileClose(med_idt fid)
Fermeture d'un fichier MED.
Definition: MEDfileClose.c:30
MEDnFamily
MEDC_EXPORT med_int MEDnFamily(const med_idt fid, const char *const meshname)
Cette routine permet de lire le nombre de famille dans un maillage.
Definition: MEDnFamily.c:35
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_axis_type
med_axis_type
Definition: med.h:258
MEDfamilyInfo
MEDC_EXPORT med_err MEDfamilyInfo(const med_idt fid, const char *const meshname, const int famit, char *const familyname, med_int *const familynumber, char *const groupname)
Cette routine permet de lire les informations relatives à une famille d'un maillage.
Definition: MEDfamilyInfo.c:39
MED_NO_IT
#define MED_NO_IT
Definition: med.h:321
main
int main(int argc, char **argv)
Definition: UsesCase_MEDmesh_11.c:29
MED_COORDINATE
Definition: med.h:149
MEDmeshEntityFamilyNumberRd
MEDC_EXPORT med_err MEDmeshEntityFamilyNumberRd(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, med_int *const number)
Cette routine permet la lecture des numéros de famille d'un type d'entité d'un maillage.
Definition: MEDmeshEntityFamilyNumberRd.c:39
med_utils.h
MED_QUAD4
#define MED_QUAD4
Definition: med.h:204