1 /* This file is part of MED.
2 *
3 * COPYRIGHT (C) 1999 - 2019 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
main(int argc,char ** argv)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
275