MED fichier: Fichier source de UsesCase_MEDmesh_11.c

MED fichier

- Documentation MED
- Guides d'utilisation
- Guides de référence

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
112 if (MEDmeshNodeCoordinateRd(fid, meshname, MED_NO_DT, MED_NO_IT, MED_FULL_INTERLACE,
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 }
126 if (MEDmeshElementConnectivityRd(fid, meshname, MED_NO_DT, MED_NO_IT, MED_CELL,
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 }
138 if (MEDmeshElementConnectivityRd(fid, meshname, MED_NO_DT, MED_NO_IT, MED_CELL,
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 }
MED_QUAD4
#define MED_QUAD4
Definition: med.h:199
MEDfileClose
MEDC_EXPORT med_err MEDfileClose(med_idt fid)
Fermeture d'un fichier MED.
Definition: MEDfileClose.c:30
MED_TRIA3
#define MED_TRIA3
Definition: med.h:198
MED_NODE
Definition: med.h:139
MED_FULL_INTERLACE
Definition: med.h:92
MED_NO_IT
#define MED_NO_IT
Definition: med.h:314
MED_COORDINATE
Definition: med.h:145
MESSAGE
#define MESSAGE(chaine)
Definition: med_utils.h:316
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_float
double med_float
Definition: med.h:329
med_mesh_type
med_mesh_type
Definition: med.h:127
MED_ACC_RDONLY
Definition: med.h:116
MED_CELL
Definition: med.h:139
med_sorting_type
med_sorting_type
Definition: med.h:304
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
med.h
MED_NO_CMODE
Definition: med.h:250
MED_NODAL
Definition: med.h:250
MED_LNAME_SIZE
#define MED_LNAME_SIZE
Definition: med.h:79
MED_NONE
#define MED_NONE
Definition: med.h:226
med_axis_type
med_axis_type
Definition: med.h:253
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
med_int
int med_int
Definition: med.h:335
main
int main(int argc, char **argv)
Definition: UsesCase_MEDmesh_11.c:29
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_GEOTYPE
#define MED_NO_GEOTYPE
Definition: med.h:227
MED_COMMENT_SIZE
#define MED_COMMENT_SIZE
Definition: med.h:75
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_CONNECTIVITY
Definition: med.h:145
MEDfileOpen
MEDC_EXPORT med_idt MEDfileOpen(const char *const filename, const med_access_mode accessmode)
Ouverture d'un fichier MED.
Definition: MEDfileOpen.c:41
med_idt
hid_t med_idt
Definition: med.h:324
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 séquence de calcul donnée...
Definition: MEDmeshnEntity.c:44
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, selon une séquence de calcul donnée.
Definition: MEDmeshNodeCoordinateRd.c:37
MED_NO_DT
#define MED_NO_DT
Definition: med.h:313
MED_NAME_SIZE
#define MED_NAME_SIZE
Definition: med.h:77
med_utils.h
MED_SNAME_SIZE
#define MED_SNAME_SIZE
Definition: med.h:78
med_bool
med_bool
Definition: med.h:255
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