MED fichier: Fichier source de UsesCase_MEDstructElement_3.c

MED fichier

UsesCase_MEDstructElement_3.c
Aller à la documentation de ce fichier.
1 /* This file is part of MED.
2 *
3 * COPYRIGHT (C) 1999 - 2023 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 3 : read struct element models in a file from a computation mesh
26 */
27
28 int main (int argc, char **argv) {
29 med_idt fid;
30 med_int nmodels, nsmesh;
31 int i,j;
32 char elementname[MED_NAME_SIZE+1]="";
33 char supportmeshname[MED_NAME_SIZE+1]="";
34 const char computmeshname[MED_NAME_SIZE+1]="COMPUT_MESH";
35 med_geometry_type *geotype;
36 med_entity_type entitype;
37 med_int elementdim;
38 med_int nnode,ncell;
39 med_geometry_type geocelltype;
40 med_bool anyprofile=0;
41 med_int nconstatt, *nvaratt=NULL;
42 char attname[MED_NAME_SIZE+1]="";
43 char profilename[MED_NAME_SIZE+1]="";
44 med_attribute_type atttype;
45 med_int nattcomp;
46 med_entity_type attentitype;
47 med_int profilesize;
48 med_float *value=NULL;
49 med_int size=0;
50 med_int meshdim, spacedim;
51 char description[MED_COMMENT_SIZE+1]="";
52 char axisname[3*MED_SNAME_SIZE+1]="";
53 char axisunit[3*MED_SNAME_SIZE+1]="";
54 med_axis_type axistype;
55 med_float *coordinates=NULL;
56 med_bool coordinatechangement;
57 med_bool geotransformation;
58 med_int nseg2;
59 med_int *seg2connectivity=NULL;
60 med_int nentities=0;
61 med_sorting_type sortingtype;
62 med_mesh_type meshtype;
63 med_int nstep;
64 char dtunit[MED_SNAME_SIZE+1]="";
65 char unitname[2*MED_SNAME_SIZE+1]="";
66 int ret=-1;
67
68 /* open file */
69 fid = MEDfileOpen("UsesCase_MEDstructElement_1.med",MED_ACC_RDONLY);
70 if (fid < 0) {
71 MESSAGE("ERROR : file creation ...");
72 goto ERROR;
73 }
74
75 /*
76 * ... In this case, we know that the MED file has only one mesh,
77 * a real code would check ...
78 */
79 /* read mesh informations : mesh dimension, space dimension ... */
80 if (MEDmeshInfoByName(fid, computmeshname, &spacedim, &meshdim, &meshtype, description,
81 dtunit, &sortingtype, &nstep, &axistype, axisname, unitname) < 0) {
82 MESSAGE("ERROR : mesh info ...");
83 goto ERROR;
84 }
85
86 /* read how many struct element models in the mesh ? */
87 if ((nmodels = MEDmeshnEntity(fid, computmeshname, MED_NO_DT, MED_NO_IT, MED_STRUCT_ELEMENT, MED_GEO_ALL,
88 MED_UNDEF_DATATYPE, MED_NO_CMODE,&coordinatechangement,
89 &geotransformation)) < 0) {
90 MESSAGE("ERROR : number of nodes ...");
91 goto ERROR;
92 }
93
94 geotype = (med_geometry_type *) malloc(sizeof(med_geometry_type)*nmodels);
95 nvaratt = (med_int *) malloc(sizeof(med_int)*nmodels);
96
97 /* Get the name and the geotype of each struct element model used in the computation mesh.
98 For each struct element geotype read the connectivity and the attribute(s)
99 The mesh support may be read if needed.
100 */
101 for (i=0;i<nmodels;i++) {
102
103 /* read the geotype */
104 if (MEDmeshEntityInfo(fid, computmeshname, MED_NO_DT, MED_NO_IT, MED_STRUCT_ELEMENT,i+1,elementname,(geotype+i)) < 0) {
105 MESSAGE("ERROR : name and type of MED_STRUCT_ELEMENT ...");
106 goto ERROR;
107 }
108
109 /* read how many MED_STRUCT_ELEMENT of type *(geotype+i) there is in the mesh */
110 if ((nentities = MEDmeshnEntity(fid, computmeshname, MED_NO_DT, MED_NO_IT, MED_STRUCT_ELEMENT,*(geotype+i),
111 MED_CONNECTIVITY, MED_NODAL, &coordinatechangement,
112 &geotransformation)) < 0) {
113 MESSAGE("ERROR : number of MED_STRUCT_ELEMENT ...");
114 goto ERROR;
115 }
116
117 /* read informations about the model */
118 if (MEDstructElementInfoByName(fid, elementname, geotype+i, &elementdim,
119 supportmeshname, &entitype, &nnode, &ncell,
120 &geocelltype, &nconstatt, &anyprofile, nvaratt+i) < 0) {
121 MESSAGE("ERROR : struct element models information ...");
122 goto ERROR;
123 }
124
125 /* read support mesh */
126 /* TODO : Ceci est optionnel dans ce usescase */
127 if (strcmp(supportmeshname,"")) {
128 if ( MEDsupportMeshInfoByName(fid, supportmeshname, &spacedim, &meshdim, description,
129 &axistype, axisname, axisunit) < 0 ) {
130 MESSAGE("ERROR : read information about mesh support ...");
131 goto ERROR;
132 }
133
134 ISCRUTE(nnode);
135 /* read how many nodes in the support mesh */
136 /* Ceci est optionnel dans ce usescase */
137 if ((nnode = MEDmeshnEntity(fid, supportmeshname, MED_NO_DT, MED_NO_IT, MED_NODE, MED_NONE,
138 MED_COORDINATE, MED_NO_CMODE,&coordinatechangement,
139 &geotransformation)) < 0) {
140 MESSAGE("ERROR : read number of nodes ...");
141 goto ERROR;
142 }
143 ISCRUTE(nnode);
144
145 /* read support mesh nodes coordinates */
146 /* Ceci est optionnel dans ce usescase */
147 coordinates = (med_float*) malloc(sizeof(med_float)*nnode*spacedim);
148
149 if (MEDmeshNodeCoordinateRd(fid, supportmeshname, MED_NO_DT, MED_NO_IT, MED_FULL_INTERLACE,
150 coordinates) < 0) {
151 MESSAGE("ERROR : read nodes coordinates ...");
152 free(coordinates);
153 goto ERROR;
154 }
155
156 /* free memory */
157 free(coordinates);
158
159 /* read how many MED_SEG2 cells in the support mesh */
160 /* Ceci est optionnel dans ce usescase */
161 if ((nseg2 = MEDmeshnEntity(fid, supportmeshname, MED_NO_DT, MED_NO_IT, MED_CELL,geocelltype,
162 MED_CONNECTIVITY, MED_NODAL, &coordinatechangement,
163 &geotransformation)) < 0) {
164 MESSAGE("ERROR : number of MED_SEG2 ...");
165 goto ERROR;
166 }
167 ISCRUTE(nseg2);
168 ISCRUTE(ncell);
169 /* read MED_SEG2 connectivity in the support mesh */
170 if (nseg2 > 0) {
171 seg2connectivity = (med_int *) malloc(sizeof(med_int)*nseg2*2);
172
173 if (MEDmeshElementConnectivityRd(fid, supportmeshname, MED_NO_DT, MED_NO_IT, MED_CELL,
174 geocelltype, MED_NODAL, MED_FULL_INTERLACE, seg2connectivity) < 0) {
175 MESSAGE("ERROR : MED_SEG2 connectivity ...");
176 free(seg2connectivity);
177 goto ERROR;
178 }
179 free(seg2connectivity);
180 }
181 }
182
183 /* read constant attribute(s) */
184 /* Optionnel pour ce usescase (cf. usescase lecture modèle) */
185 for (j=0; j<nconstatt; j++) {
186 if ( MEDstructElementConstAttInfo(fid, elementname, j+1,
187 attname, &atttype, &nattcomp, &attentitype,
188 profilename, &profilesize) < 0) {
189 MESSAGE("ERROR : const attribute information ...");
190 goto ERROR;
191 }
192
193 /* if there is a profile => read the profile, see UsesCase_MEDprofile_2 */
194
195 /* memory allocation */
196 if (profilesize != 0)
197 size = profilesize*nattcomp*MEDstructElementAttSizeof(atttype);
198 else
199 if (attentitype== MED_NODE)
200 size = nnode*nattcomp*MEDstructElementAttSizeof(atttype);
201 else
202 size = ncell*nattcomp*MEDstructElementAttSizeof(atttype);
203 if ( atttype == MED_ATT_NAME) ++size;
204 ISCRUTE(size);
205 value = (med_float *) malloc(size);
206
207
208 /* read attribute(s) value(s) */
209 if ( MEDstructElementConstAttRd(fid, elementname, attname, (unsigned char *)value ) < 0 ) {
210 MESSAGE("ERROR : const attribute value ...");
211 free(value);
212 goto ERROR;
213 }
214
215 /* memory deallocation */
216 free(value);
217 }
218
219 /* read variable attribute(s) */
220 for (j=0; j<*(nvaratt+i); j++) {
221
222 /* read informations about the attribute */
223 if ( MEDstructElementVarAttInfo(fid, elementname, j+1,
224 attname, &atttype, &nattcomp) < 0) {
225 MESSAGE("ERROR : var attribute information ...");
226 goto ERROR;
227 }
228
229 /* memory allocation */
230 size = nentities*nattcomp*MEDstructElementAttSizeof(atttype);
231 if ( atttype == MED_ATT_NAME) ++size;
232 ISCRUTE(size);
233 value = (med_float *) malloc((size)*sizeof(char));
234
235 /* read attribute values */
236 if (MEDmeshStructElementVarAttRd(fid, computmeshname, MED_NO_DT, MED_NO_IT,
237 *(geotype+i), attname, value ) < 0) {
238 MESSAGE("ERROR : read variable attributes values ...");
239 free(value);
240 goto ERROR;
241 }
242
243 /* free memory */
244 free(value);
245
246 }
247
248 }
249
250 ret=0;
251 ERROR:
252
253 /* free memory */
254 free(geotype);
255 free(nvaratt);
256
257 /* close file */
258 if (MEDfileClose(fid) < 0) {
259 MESSAGE("ERROR : file closing ...");
260 ret=-1;
261 }
262
263 return ret;
264 }
265
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
MED_ACC_RDONLY
Definition: med.h:122
MED_COMMENT_SIZE
#define MED_COMMENT_SIZE
Definition: med.h:81
med_geometry_type
int med_geometry_type
Definition: med.h:196
MED_ATT_NAME
Definition: med.h:177
MED_SNAME_SIZE
#define MED_SNAME_SIZE
Definition: med.h:84
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_idt
hid_t med_idt
Definition: med.h:333
MED_UNDEF_DATATYPE
Definition: med.h:154
MEDmeshEntityInfo
MEDC_EXPORT med_err MEDmeshEntityInfo(const med_idt fid, const char *const meshname, const med_int numdt, const med_int numit, const med_entity_type entitype, const int geotypeit, char *const geotypename, med_geometry_type *const geotype)
Cette routine indique de façon itérative les types géométriques disponibles dans un maillage.
Definition: MEDmeshEntityInfo.c:41
main
int main(int argc, char **argv)
Definition: UsesCase_MEDstructElement_3.c:28
med_sorting_type
med_sorting_type
Definition: med.h:311
med_entity_type
med_entity_type
Definition: med.h:145
MESSAGE
#define MESSAGE(chaine)
Definition: med_utils.h:324
MED_FULL_INTERLACE
Definition: med.h:98
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_int
int med_int
Definition: med.h:344
MED_STRUCT_ELEMENT
Definition: med.h:146
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
ISCRUTE
#define ISCRUTE(entier)
Definition: med_utils.h:313
med_attribute_type
med_attribute_type
Definition: med.h:175
med_bool
med_bool
Definition: med.h:262
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
med_float
double med_float
Definition: med.h:338
MED_COORDINATE
Definition: med.h:151
MED_NO_CMODE
Definition: med.h:257
MED_GEO_ALL
#define MED_GEO_ALL
Definition: med.h:238
MED_NO_DT
#define MED_NO_DT
Definition: med.h:322
MED_NONE
#define MED_NONE
Definition: med.h:233
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:27
MEDfileClose
MEDC_EXPORT med_err MEDfileClose(med_idt fid)
Fermeture d'un fichier MED.
Definition: MEDfileClose.c:30
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
med_mesh_type
med_mesh_type
Definition: med.h:133
MED_CELL
Definition: med.h:145
MED_NAME_SIZE
#define MED_NAME_SIZE
Definition: med.h:83
MEDstructElementInfoByName
MEDC_EXPORT med_err MEDstructElementInfoByName(const med_idt fid, const 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 nconstantatribute, med_bool *const anyprofile, med_int *const nvariableattribute)
Cette routine décrit les caractéristiques d'un modèle d'élément de structure à partir de son nom.
Definition: MEDstructElementInfoByName.c:49
med_utils.h
MED_NODE
Definition: med.h:145
med_axis_type
med_axis_type
Definition: med.h:260
med.h
MED_CONNECTIVITY
Definition: med.h:151
MED_NO_IT
#define MED_NO_IT
Definition: med.h:323
MEDfileOpen
MEDC_EXPORT med_idt MEDfileOpen(const char *const filename, const med_access_mode accessmode)
Ouverture d'un fichier MED.
Definition: MEDfileOpen.c:42
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
MEDsupportMeshInfoByName
MEDC_EXPORT med_err MEDsupportMeshInfoByName(const med_idt fid, const 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 en précisant son nom.
Definition: MEDsupportMeshInfoByName.c:39
MED_NODAL
Definition: med.h:257
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