MED fichier
UsesCase_MEDmesh_11.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  * 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  int ret=-1;
57 
58  /* open MED file with READ ONLY access mode */
59  fid = MEDfileOpen("UsesCase_MEDmesh_10.med",MED_ACC_RDONLY);
60  if (fid < 0) {
61  MESSAGE("ERROR : open file in READ ONLY ACCESS mode ...");
62  goto ERROR;
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  goto ERROR;
75  }
76 
77  /* read how many nodes in the mesh */
78  if ((nnodes = MEDmeshnEntity(fid, meshname, MED_NO_DT, MED_NO_IT, MED_NODE, MED_NO_GEOTYPE,
79  MED_COORDINATE, MED_NO_CMODE,&coordinatechangement,
80  &geotransformation)) < 0) {
81  MESSAGE("ERROR : number of nodes ...");
82  goto ERROR;
83  }
84 
85  /*
86  * ... we know that we only have MED_TRIA3 and MED_QUAD4 in the mesh,
87  * a real code would check all MED geometry cell types ...
88  */
89 
90  /* read how many triangular cells in the mesh */
91  if ((ntria3 = MEDmeshnEntity(fid, meshname, MED_NO_DT, MED_NO_IT, MED_CELL,MED_TRIA3,
92  MED_CONNECTIVITY, MED_NODAL,&coordinatechangement,
93  &geotransformation)) < 0) {
94  MESSAGE("ERROR : number of MED_TRIA3 ...");
95  goto ERROR;
96  }
97 
98  /* read how many quadrangular cells in the mesh */
99  if ((nquad4 = MEDmeshnEntity(fid, meshname, MED_NO_DT, MED_NO_IT, MED_CELL,MED_QUAD4,
100  MED_CONNECTIVITY, MED_NODAL, &coordinatechangement,
101  &geotransformation)) < 0) {
102  MESSAGE("ERROR : number of MED_QUAD4 ...");
103  goto ERROR;
104  }
105 
106  /* read mesh nodes coordinates */
107  if ((coordinates = (med_float*) malloc(sizeof(med_float)*nnodes*spacedim)) == NULL) {
108  MESSAGE("ERROR : memory allocation ...");
109  goto ERROR;
110  }
111 
113  coordinates) < 0) {
114  MESSAGE("ERROR : nodes coordinates ...");
115  free(coordinates);
116  goto ERROR;
117  }
118 
119  free(coordinates);
120 
121  /* read cells connectivity in the mesh */
122  if ((triaconnectivity = (med_int *) malloc(sizeof(med_int)*ntria3*3)) == NULL) {
123  MESSAGE("ERROR : memory allocation ...");
124  goto ERROR;
125  }
127  MED_TRIA3, MED_NODAL, MED_FULL_INTERLACE, triaconnectivity) < 0) {
128  MESSAGE("ERROR : MED_TRIA3 connectivity ...");
129  free(triaconnectivity);
130  goto ERROR;
131  }
132  free(triaconnectivity);
133 
134  if ((quadconnectivity = (med_int *) malloc(sizeof(med_int)*nquad4*4)) == NULL) {
135  MESSAGE("ERROR : memory allocation ...");
136  goto ERROR;
137  }
139  MED_QUAD4, MED_NODAL, MED_FULL_INTERLACE, quadconnectivity) < 0) {
140  MESSAGE("ERROR : MED_TRIA3 connectivity ...");
141  free(quadconnectivity);
142  goto ERROR;
143  }
144  free(quadconnectivity);
145 
146  /*
147  * read families of entities...
148  */
149  if ((nfamily = MEDnFamily(fid,meshname)) < 0) {
150  MESSAGE("ERROR : read number of family ...");
151  goto ERROR;
152  }
153 
154  for (i=0; i<nfamily ; i++) {
155 
156  if ((ngroup = MEDnFamilyGroup(fid, meshname, i+1)) < 0) {
157  MESSAGE("ERROR : read number of group in a family ...");
158  goto ERROR;
159  }
160 
161  if (ngroup > 0) {
162  if ((groupname = (char*) malloc(sizeof(char)*MED_LNAME_SIZE*ngroup+1)) == NULL) {
163  MESSAGE("ERROR : memory allocation ...");
164  goto ERROR;
165  }
166 
167  if (MEDfamilyInfo(fid, meshname, i+1, familyname, &familynumber, groupname) < 0) {
168  MESSAGE("ERROR : family info ...");
169  free(groupname);
170  goto ERROR;
171  }
172  free(groupname);
173  }
174 
175  }
176 
177  /* read family numbers for nodes */
178  /* By convention, if there is no numbers in the file, it means that 0 is the family
179  number of all nodes */
180  if ((familynumbers = (med_int *) malloc(sizeof(med_int)*nnodes)) == NULL) {
181  MESSAGE("ERROR : memory allocation ...");
182  goto ERROR;
183  }
184  if (MEDmeshEntityFamilyNumberRd(fid, meshname, MED_NO_DT, MED_NO_IT,
185  MED_NODE,MED_NONE,familynumbers ) < 0)
186  for (i=0; i<nnodes; i++) *(familynumbers+i) = 0;
187 
188  for (i=0; i<nnodes; i++) printf("%d - ", *(familynumbers+i));
189 
190  if (familynumbers)
191  free (familynumbers);
192 
193  /* read family numbers for cells */
194  if ((familynumbers = (med_int *) malloc(sizeof(med_int)*ntria3)) == NULL) {
195  MESSAGE("ERROR : memory allocation ...");
196  goto ERROR;
197  }
198  if (MEDmeshEntityFamilyNumberRd(fid, meshname, MED_NO_DT, MED_NO_IT,
199  MED_CELL,MED_TRIA3,familynumbers ) < 0)
200  for (i=0; i<ntria3; i++) *(familynumbers+i) = 0;
201 
202  free (familynumbers);
203 
204 
205  if ((familynumbers = (med_int *) malloc(sizeof(med_int)*nquad4)) == NULL) {
206  MESSAGE("ERROR : memory allocation ...");
207  goto ERROR;
208  }
209  if (MEDmeshEntityFamilyNumberRd(fid, meshname, MED_NO_DT, MED_NO_IT,
210  MED_CELL,MED_QUAD4,familynumbers ) < 0)
211  for (i=0; i<nquad4; i++) *(familynumbers+i) = 0;
212 
213  free (familynumbers);
214 
215  ret=0;
216  ERROR:
217 
218  /* close MED file */
219  if (MEDfileClose(fid) < 0) {
220  MESSAGE("ERROR : close file");
221  ret= -1;
222  }
223 
224  return ret;
225 }
int med_int
Definition: med.h:335
#define MED_NO_GEOTYPE
Definition: med.h:227
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&#39;un type d&#39;entité d&#39;un maillage.
#define MED_LNAME_SIZE
Definition: med.h:79
#define MED_QUAD4
Definition: med.h:199
int main(int argc, char **argv)
#define MED_SNAME_SIZE
Definition: med.h:78
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.
hid_t med_idt
Definition: med.h:324
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&#39;un maillage.
Definition: MEDfamilyInfo.c:39
#define MED_NO_IT
Definition: med.h:314
Definition: med.h:250
#define MED_NAME_SIZE
Definition: med.h:77
#define MED_NONE
Definition: med.h:226
med_sorting_type
Definition: med.h:304
#define MED_COMMENT_SIZE
Definition: med.h:75
#define MED_TRIA3
Definition: med.h:198
double med_float
Definition: med.h:329
med_axis_type
Definition: med.h:253
med_mesh_type
Definition: med.h:127
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&#39;entités dans un maillage pour une séquence de calcul donnée...
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
#define MED_NO_DT
Definition: med.h:313
MEDC_EXPORT med_idt MEDfileOpen(const char *const filename, const med_access_mode accessmode)
Ouverture d&#39;un fichier MED.
Definition: MEDfileOpen.c:41
Definition: med.h:139
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...
med_bool
Definition: med.h:255
MEDC_EXPORT med_err MEDfileClose(med_idt fid)
Fermeture d&#39;un fichier MED.
Definition: MEDfileClose.c:30
#define MESSAGE(chaine)
Definition: med_utils.h:316
Definition: med.h:139
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, selon une séquence de calcul donnée.
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...