MED fichier

UsesCase_MEDstructElement_2.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 #include <med.h>
19 #define MESGERR 1
20 #include <med_utils.h>
21
22 #include <string.h>
23
24 /*
25 * StructElement use case 2 : read struct element models in a file
26 * Classical iteration approach
27 * STEP 1 : read suppport mesh
28 * STEP 2 : read struct element model
29 * STEP 3 : read in a computation mesh
30 * A access from the computation mesh is defined in StructElement use case 3.
31 */
32
33 int main (int argc, char **argv) {
34 med_idt fid;
35 med_int nmodels, nsmesh;
36 int i,j,k;
37 char elementname [MED_NAME_SIZE+1]="";
38 char supportmeshname[MED_NAME_SIZE+1]="";
39 const char computmeshname [MED_NAME_SIZE+1]="COMPUT_MESH";
40 med_geometry_type *geotype;
41 med_geometry_type geocelltype;
42
43 med_entity_type entitype;
44 med_int elementdim,nnode,ncell;
45 med_bool anyprofile=0;
46 med_int nconstatt, *nvaratt;
47 char attname [MED_NAME_SIZE+1]="";
48 char profilename[MED_NAME_SIZE+1]="";
49 med_attribute_type atttype;
50 med_int nattcomp;
51 med_entity_type attentitype;
52 med_int profilesize;
53 unsigned char *value;
54 med_int size=0;
55 med_int meshdim, spacedim;
56 char description[MED_COMMENT_SIZE+1]="";
57 char axisname [3*MED_SNAME_SIZE+1]="";
58 char axisunit [3*MED_SNAME_SIZE+1]="";
59 med_axis_type axistype;
60 med_float *coordinates;
61 med_bool coordinatechangement, geotransformation;
62 med_int nseg2, *seg2connectivity;
63 med_int nentities=0;
64 med_sorting_type sortingtype;
65 med_mesh_type meshtype;
66 med_int nstep;
67 char dtunit [MED_SNAME_SIZE+1]="";
68 char unitname[2*MED_SNAME_SIZE+1]="";
69 char tmp [MED_NAME_SIZE+1]="";
70 int ret=-1;
71
72 /* open file */
73 fid = MEDfileOpen("UsesCase_MEDstructElement_1.med",MED_ACC_RDONLY);
74 if (fid < 0) {
75 MESSAGE("ERROR : file creation ...");
76 goto ERROR;
77 }
78
79 /* STEP 1 */
80
81 /* how many support mesh in the file ? */
82 if ((nsmesh = MEDnSupportMesh(fid)) < 0 ) {
83 MESSAGE("ERROR : read number of support mesh ...");
84 goto ERROR;
85 }
86
87 /* read each support mesh */
88 for (i=0; i<nsmesh; i++) {
89 if ( MEDsupportMeshInfo(fid, i+1, supportmeshname, &spacedim, &meshdim, description,
90 &axistype, axisname, axisunit) < 0 ) {
91 MESSAGE("ERROR : read information about mesh support ...");
92 goto ERROR;
93 }
94
95 /* read how many nodes in the mesh */
96 if ((nnode = MEDmeshnEntity(fid, supportmeshname, MED_NO_DT, MED_NO_IT, MED_NODE, MED_NONE,
97 MED_COORDINATE, MED_NO_CMODE, &coordinatechangement,
98 &geotransformation)) < 0) {
99 MESSAGE("ERROR : read number of nodes ...");
100 goto ERROR;
101 }
102
103 /* read mesh nodes coordinates */
104 coordinates = (med_float*) malloc(sizeof(med_float)*nnode*spacedim);
105
106 if (MEDmeshNodeCoordinateRd(fid, supportmeshname, MED_NO_DT, MED_NO_IT, MED_FULL_INTERLACE,
107 coordinates) < 0) {
108 MESSAGE("ERROR : read nodes coordinates ...");
109 free(coordinates);
110 goto ERROR;
111 }
112
113 /* free memory */
114 free(coordinates);
115
116 /* ... In this case, we suppose that we have only MED_SEG2
117 * as cell elements in our support meshes
118 * a real code would check ... */
119 if ((nseg2 = MEDmeshnEntity(fid, supportmeshname, MED_NO_DT, MED_NO_IT, MED_CELL,MED_SEG2,
120 MED_CONNECTIVITY, MED_NODAL, &coordinatechangement,
121 &geotransformation)) < 0) {
122 MESSAGE("ERROR : number of MED_SEG2 ...");
123 goto ERROR;
124 }
125
126 /* read MED_SEG2 connectivity if necessary */
127 if (nseg2 > 0) {
128 seg2connectivity = (med_int *) malloc(sizeof(med_int)*nseg2*2);
129
130 if (MEDmeshElementConnectivityRd(fid, supportmeshname, MED_NO_DT, MED_NO_IT, MED_CELL,
131 MED_SEG2, MED_NODAL, MED_FULL_INTERLACE, seg2connectivity) < 0) {
132 MESSAGE("ERROR : MED_SEG2 connectivity ...");
133 free(seg2connectivity);
134 goto ERROR;
135 }
136
137 free(seg2connectivity);
138 }
139
140 }
141 /* STEP 2 */
142 /* how many struct element models ? */
143 if ((nmodels = MEDnStructElement(fid)) < 0) {
144 MESSAGE("ERROR : read number of struct element models ...");
145 goto ERROR;
146 }
147
148 geotype = (med_geometry_type *) malloc(sizeof(med_geometry_type)*nmodels);
149 nvaratt = (med_int *) malloc(sizeof(med_int)*nmodels);
150
151
152 /* read each model */
153 for (i=0; i<nmodels; i++) {
154 if (MEDstructElementInfo(fid, i+1, elementname, geotype+i, &elementdim,
155 supportmeshname, &entitype, &nnode, &ncell,
156 &geocelltype, &nconstatt, &anyprofile, nvaratt+i) < 0) {
157 MESSAGE("ERROR : struct element models information ...");
158 goto ERROR;
159 }
160
161 /* read constant attribute(s) */
162 for (j=0; j<nconstatt; j++) {
163 if ( MEDstructElementConstAttInfo(fid, elementname, j+1,
164 attname, &atttype, &nattcomp, &attentitype,
165 profilename, &profilesize) < 0) {
166 MESSAGE("ERROR : const attribute information ...");
167 goto ERROR;
168 }
169
170 /* memory allocation */
171 if (profilesize != 0)
172 size = profilesize*nattcomp*MEDstructElementAttSizeof(atttype);
173 else
174 if (entitype == MED_NODE)
175 size = nnode*nattcomp*MEDstructElementAttSizeof(atttype);
176 else
177 size = ncell*nattcomp*MEDstructElementAttSizeof(atttype);
178 if ( atttype == MED_ATT_NAME) ++size;
179 value = (unsigned char *) malloc(size);
180
181 /* read attribute(s) value(s) */
182 if ( MEDstructElementConstAttRd(fid, elementname, attname, (unsigned char *)value ) < 0 ) {
183 MESSAGE("ERROR : const attribute value ...");
184 free(value);
185 goto ERROR;
186 }
187
188 free(value);
189
190 }
191
192 /* read variable attribute(s) */
193 /* values must be read in a computation mesh => see STEP 3 */
194 }
195
196 /* STEP 3 */
197
198 /*
199 * ... In this case, we know that the MED file has only one mesh,
200 * a real code would check ...
201 */
202 /* read mesh informations : mesh dimension, space dimension ... */
203 if (MEDmeshInfoByName(fid, computmeshname, &spacedim, &meshdim, &meshtype, description,
204 dtunit, &sortingtype, &nstep, &axistype, axisname, unitname) < 0) {
205 MESSAGE("ERROR : mesh info ...");
206 goto ERROR;
207 }
208
209
210 /* Get dynamically struct element name for each struct element model,
211 then for each type read the connectivity if a support mesh exist and
212 finaly the variable(s) attribute(s) */
213 for (i=0;i<nmodels;i++) {
214
215 /* read how many MED_STRUCT_ELEMENT of type *(geotype+i) there is in the mesh */
216 if ((nentities = MEDmeshnEntity(fid, computmeshname, MED_NO_DT, MED_NO_IT, MED_STRUCT_ELEMENT,*(geotype+i),
217 MED_CONNECTIVITY, MED_NODAL, &coordinatechangement,
218 &geotransformation)) < 0) {
219 MESSAGE("ERROR : number of MED_STRUCT_ELEMENT ...");
220 goto ERROR;
221 }
222
223 if (MEDstructElementName(fid,*(geotype+i),elementname) < 0) {
224 MESSAGE("ERROR : get element name ...");
225 goto ERROR;
226 }
227
228 for (j=0; j<*(nvaratt+i); j++) {
229
230 /* read informations about each attribute */
231 if ( MEDstructElementVarAttInfo(fid, elementname, j+1,
232 attname, &atttype, &nattcomp) < 0) {
233 MESSAGE("ERROR : var attribute information ...");
234 goto ERROR;
235 }
236
237 /* memory allocation */
238 if (entitype == MED_NODE)
239 size = nattcomp*nentities*MEDstructElementAttSizeof(atttype);
240 else
241 size = nattcomp*nentities*MEDstructElementAttSizeof(atttype);
242 if ( atttype == MED_ATT_NAME) ++size;
243 value = (unsigned char *) malloc(size);
244
245 /* read attribute values */
246 if (MEDmeshStructElementVarAttRd(fid, computmeshname, MED_NO_DT, MED_NO_IT,
247 *(geotype+i), attname, value ) < 0) {
248 MESSAGE("ERROR : read variable attributes values ...");
249 free(value);
250 goto ERROR;
251 }
252
253 /*TODO : Lire les connectivit�s des �l�ments de structures */
254
255 free(value);
256
257 }
258 }
259
260 ret=0;
261 ERROR:
262
263 free(geotype);
264 free(nvaratt);
265
266 /* close file */
267 if (MEDfileClose(fid) < 0) {
268 MESSAGE("ERROR : file closing ...");
269 ret=-1;
270 }
271
272 return ret;
273 }
274
MED_FULL_INTERLACE
Definition: med.h:96
MED_COMMENT_SIZE
#define MED_COMMENT_SIZE
Definition: med.h:79
MEDsupportMeshInfo
MEDC_EXPORT med_err MEDsupportMeshInfo(const med_idt fid, const int meshit, char *const supportmeshname, med_int *const spacedim, med_int *const meshdim, char *const description, med_axis_type *const axistype, char *const axisname, char *const axisunit)
Cette routine permet de lire les informations relatives à un maillage support dans un fichier.
Definition: MEDsupportMeshInfo.c:39
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_geometry_type
int med_geometry_type
Definition: med.h:194
MED_SNAME_SIZE
#define MED_SNAME_SIZE
Definition: med.h:82
MED_NODAL
Definition: med.h:255
main
int main(int argc, char **argv)
Definition: UsesCase_MEDstructElement_2.c:33
MEDstructElementName
MEDC_EXPORT med_err MEDstructElementName(const med_idt fid, const med_geometry_type mgeotype, char *const modelname)
Cette routine renvoie le nom du modèle d'éléments de structure associé au type mgeotype.
Definition: MEDstructElementName.c:37
med_idt
hid_t med_idt
Definition: med.h:331
MED_SEG2
#define MED_SEG2
Definition: med.h:200
MED_ACC_RDONLY
Definition: med.h:120
MED_STRUCT_ELEMENT
Definition: med.h:144
med_sorting_type
med_sorting_type
Definition: med.h:309
med_entity_type
med_entity_type
Definition: med.h:143
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_attribute_type
med_attribute_type
Definition: med.h:173
med.h
med_bool
med_bool
Definition: med.h:260
med_float
double med_float
Definition: med.h:336
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
MEDstructElementConstAttRd
MEDC_EXPORT med_err MEDstructElementConstAttRd(const med_idt fid, const char *const modelname, const char *const constattname, void *const value)
Cette routine lit la valeur d'un attribut caractéristique constant d'un modèle d'éléments de structur...
Definition: MEDstructElementConstAttRd.c:42
MEDnStructElement
MEDC_EXPORT med_int MEDnStructElement(const med_idt fid)
Cette routine renvoie le nombre de modèles d'éléments de structure.
Definition: MEDnStructElement.c:35
MED_ATT_NAME
Definition: med.h:175
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_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
MEDnSupportMesh
MEDC_EXPORT med_int MEDnSupportMesh(const med_idt fid)
Cette routine permet de lire le nombre de maillages support dans un fichier.
Definition: MEDnSupportMesh.c:34
MEDstructElementConstAttInfo
MEDC_EXPORT med_err MEDstructElementConstAttInfo(const med_idt fid, const char *const modelname, const int attit, char *const constattname, med_attribute_type *const constatttype, med_int *const ncomponent, med_entity_type *const sentitytype, char *const profilename, med_int *const profilesize)
Cette routine décrit les caractéristiques d'un attribut constant de modèle d'élément de structure par...
Definition: MEDstructElementConstAttInfo.c:45
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
MEDstructElementInfo
MEDC_EXPORT med_err MEDstructElementInfo(const med_idt fid, const int mit, char *const modelname, med_geometry_type *const mgeotype, med_int *const modeldim, char *const supportmeshname, med_entity_type *const sentitytype, med_int *const snnode, med_int *const sncell, med_geometry_type *const sgeotype, med_int *const nconstantattribute, med_bool *const anyprofile, med_int *const nvariableattribute)
Cette routine décrit les caractéristiques d'un modèle d'élément de structure par itération.
Definition: MEDstructElementInfo.c:50
MED_NO_IT
#define MED_NO_IT
Definition: med.h:321
MED_COORDINATE
Definition: med.h:149
MEDstructElementAttSizeof
MEDC_EXPORT int MEDstructElementAttSizeof(const med_attribute_type atttype)
Cette routine renvoie la taille en octets du type élémentaire atttype.
Definition: MEDstructElementAttSizeof.c:36
MEDstructElementVarAttInfo
MEDC_EXPORT med_err MEDstructElementVarAttInfo(const med_idt fid, const char *const modelname, const int attit, char *const varattname, med_attribute_type *const varatttype, med_int *const ncomponent)
Cette routine décrit les caractéristiques d'un attribut variable de modèle d'élément de structure par...
Definition: MEDstructElementVarAttInfo.c:41
med_utils.h
MEDmeshStructElementVarAttRd
MEDC_EXPORT med_err MEDmeshStructElementVarAttRd(const med_idt fid, const char *const meshname, const med_int numdt, const med_int numit, const med_geometry_type mgeotype, const char *const varattname, void *const value)
Cette routine lit les valeurs d'un attribut caractéristique variable sur les éléments de structure d'...
Definition: MEDmeshStructElementVarAttRd.c:43