MED fichier
UsesCase_MEDmesh_2.c
Aller à la documentation de ce fichier.
1/* This file is part of MED.
2 *
3 * COPYRIGHT (C) 1999 - 2020 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 2 : read a 2D unstructured mesh with 15 nodes, 8 triangular cells, 4 triangular cells
20 */
21
22#include <med.h>
23#define MESGERR 1
24#include <med_utils.h>
25
26#include <string.h>
27
28int main (int argc, char **argv) {
29 med_idt fid;
30 const char meshname[MED_NAME_SIZE+1] = "2D unstructured mesh";
31 char meshdescription[MED_COMMENT_SIZE+1];
32 med_int meshdim;
33 med_int spacedim;
34 med_sorting_type sortingtype;
35 med_int nstep;
36 med_mesh_type meshtype;
37 med_axis_type axistype;
38 char axisname[2*MED_SNAME_SIZE+1];
39 char unitname[2*MED_SNAME_SIZE+1];
40 char dtunit[MED_SNAME_SIZE+1];
41 med_float *coordinates = NULL;
42 med_int nnodes = 0;
43 med_int *triaconnectivity = NULL;
44 med_int ntria3 = 0;
45 med_int *quadconnectivity = NULL;
46 med_int nquad4 = 0;
47 med_bool coordinatechangement;
48 med_bool geotransformation;
49 int i;
50 int ret = -1;
51
52 /* open MED file with READ ONLY access mode */
53 fid = MEDfileOpen("UsesCase_MEDmesh_1.med",MED_ACC_RDONLY);
54 if (fid < 0) {
55 MESSAGE("ERROR : open file in READ ONLY ACCESS mode ...");
56 goto ERROR;
57 }
58
59 /*
60 * ... we know that the MED file has only one mesh,
61 * a real code would check ...
62 */
63
64 /* read mesh informations : mesh dimension, space dimension ... */
65 if (MEDmeshInfoByName(fid, meshname, &spacedim, &meshdim, &meshtype, meshdescription,
66 dtunit, &sortingtype, &nstep, &axistype, axisname, unitname) < 0) {
67 MESSAGE("ERROR : mesh info ...");
68 goto ERROR;
69 }
70
71 /* read how many nodes in the mesh */
72 if ((nnodes = MEDmeshnEntity(fid, meshname, MED_NO_DT, MED_NO_IT, MED_NODE, MED_NO_GEOTYPE,
73 MED_COORDINATE, MED_NO_CMODE,&coordinatechangement,
74 &geotransformation)) < 0) {
75 MESSAGE("ERROR : number of nodes ...");
76 goto ERROR;
77 }
78
79 /*
80 * ... we know that we only have MED_TRIA3 and MED_QUAD4 in the mesh,
81 * a real code would check all MED geometry cell types ...
82 */
83
84 /* read how many triangular cells in the mesh */
85 if ((ntria3 = MEDmeshnEntity(fid, meshname, MED_NO_DT, MED_NO_IT, MED_CELL,MED_TRIA3,
86 MED_CONNECTIVITY, MED_NODAL,&coordinatechangement,
87 &geotransformation)) < 0) {
88 MESSAGE("ERROR : number of MED_TRIA3 ...");
89 goto ERROR;
90 }
91
92 /* read how many quadrangular cells in the mesh */
93 if ((nquad4 = MEDmeshnEntity(fid, meshname, MED_NO_DT, MED_NO_IT, MED_CELL,MED_QUAD4,
94 MED_CONNECTIVITY, MED_NODAL, &coordinatechangement,
95 &geotransformation)) < 0) {
96 MESSAGE("ERROR : number of MED_QUAD4 ...");
97 goto ERROR;
98 }
99
100
101 /* read mesh nodes coordinates */
102 if ((coordinates = (med_float*) malloc(sizeof(med_float)*nnodes*spacedim)) == NULL) {
103 MESSAGE("ERROR : memory allocation ...");
104 goto ERROR;
105 }
106
108 coordinates) < 0) {
109 MESSAGE("ERROR : nodes coordinates ...");
110 goto ERROR;
111 }
112
113 /* read cells connectivity in the mesh */
114 if ((triaconnectivity = (med_int *) malloc(sizeof(med_int)*ntria3*3)) == NULL) {
115 MESSAGE("ERROR : memory allocation ...");
116 goto ERROR;
117 }
119 MED_TRIA3, MED_NODAL, MED_FULL_INTERLACE, triaconnectivity) < 0) {
120 MESSAGE("ERROR : MED_TRIA3 connectivity ...");
121 goto ERROR;
122 }
123
124 if ((quadconnectivity = (med_int *) malloc(sizeof(med_int)*nquad4*4)) == NULL) {
125 MESSAGE("ERROR : memory allocation ...");
126 goto ERROR;
127 }
129 MED_QUAD4, MED_NODAL, MED_FULL_INTERLACE, quadconnectivity) < 0) {
130 MESSAGE("ERROR : MED_QUAD4 connectivity ...");
131 goto ERROR;
132 }
133
134 /*
135 * ... we know that the family number of nodes and elements is 0, a real code would check ...
136 */
137 ret = 0;
138 ERROR :
139
140 /* memory desallocation */
141 if (coordinates)
142 free(coordinates);
143
144 if (triaconnectivity)
145 free(triaconnectivity);
146
147 if (quadconnectivity)
148 free(quadconnectivity);
149
150 /* close MED file */
151 if (MEDfileClose(fid) < 0) {
152 MESSAGE("ERROR : close file");
153 ret = -1;
154 }
155
156 return ret;
157}
158
int main(int argc, char **argv)
MEDC_EXPORT med_err MEDfileClose(med_idt fid)
Fermeture d'un fichier MED.
MEDC_EXPORT med_idt MEDfileOpen(const char *const filename, const med_access_mode accessmode)
Ouverture d'un fichier MED.
Definition MEDfileOpen.c:42
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...
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,...
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.
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.
#define MED_NAME_SIZE
Definition med.h:81
@ MED_FULL_INTERLACE
Definition med.h:96
#define MED_SNAME_SIZE
Definition med.h:82
med_bool
Definition med.h:260
#define MED_QUAD4
Definition med.h:204
@ MED_CONNECTIVITY
Definition med.h:149
@ MED_COORDINATE
Definition med.h:149
#define MED_TRIA3
Definition med.h:203
med_axis_type
Definition med.h:258
med_sorting_type
Definition med.h:300
med_mesh_type
Definition med.h:131
#define MED_NO_GEOTYPE
Definition med.h:232
int med_int
Definition med.h:333
#define MED_NO_DT
Definition med.h:311
#define MED_NO_IT
Definition med.h:312
@ MED_NODE
Definition med.h:143
@ MED_CELL
Definition med.h:143
double med_float
Definition med.h:327
#define MED_COMMENT_SIZE
Definition med.h:79
@ MED_ACC_RDONLY
Definition med.h:120
hid_t med_idt
Definition med.h:322
@ MED_NO_CMODE
Definition med.h:255
@ MED_NODAL
Definition med.h:255
#define MESSAGE(chaine)
Definition med_utils.h:324