MED fichier

UsesCase_MEDfield_16.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 #include <med.h>
25 #define MESGERR 1
26 #include <med_utils.h>
27
28 #include <string.h>
29
30 /*
31 * Field use case 16 : read a field (generic approach) in a MED file with
32 * - values defined on struct elements
33 */
34
35
36 int main (int argc, char **argv) {
37 med_idt fid,mfid,sfid;
38 med_idt nfield, i, j;
39 char meshname[MED_NAME_SIZE+1]="";
40 med_bool localmesh;
41 char fieldname[MED_NAME_SIZE+1]="";
42 med_field_type fieldtype;
43 char *componentname = NULL;
44 char *componentunit = NULL;
45 char dtunit[MED_SNAME_SIZE+1]="";
46 med_float *values = NULL;
47 med_int nstep, nvalues;
48 med_int ncomponent;
49 med_int csit, numit, numdt, it;
50 med_float dt;
51 med_geometry_type *geotypes, geotype;
52 med_int nmodels;
53 med_int nprofile, pit, profilesize;
54 char profilename[MED_NAME_SIZE+1]="";
55 med_int nintegrationpoint;
56 char localizationname[MED_NAME_SIZE+1]="";
57 int k;
58 char elementname[MED_NAME_SIZE+1];
59 char supportmeshname[MED_NAME_SIZE+1];
60 med_entity_type entitype;
61 med_int elementdim;
62 med_int nnode,ncell;
63 med_geometry_type geocelltype;
64 med_bool anyprofile=0;
65 med_int nconstatt, *nvaratt;
66 int ret=-1;
67
68 /* open file */
69 fid = MEDfileOpen("UsesCase_MEDfield_15.med",MED_ACC_RDWR);
70 if (fid < 0) {
71 MESSAGE("ERROR : open file ...");
72 goto ERROR;
73 }
74
75 if (( mfid=MEDfileObjectsMount(fid, "UsesCase_MEDstructElement_1.med",MED_MESH_SUPPORT)) < 0 ) {
76 MESSAGE("ERROR : file mounting ...");
77 goto ERROR;
78 }
79
80 if (( sfid=MEDfileObjectsMount(fid, "UsesCase_MEDstructElement_1.med",MED_ELSTRUCT)) < 0 ) {
81 MESSAGE("ERROR : file mounting ...");
82 goto ERROR;
83 }
84
85 /* How many struct element models ? */
86 if ((nmodels = MEDnStructElement(mfid)) < 0) {
87 MESSAGE("ERROR : read number of struct element models ...");
88 goto ERROR;
89 }
90 geotypes = (med_geometry_type *) malloc(sizeof(med_geometry_type)*nmodels);
91 nvaratt = (med_int *) malloc(sizeof(med_int)*nmodels);
92
93 for (it=0; it<nmodels; it++) {
94 if (MEDstructElementInfo(mfid, it+1, elementname, geotypes+it, &elementdim,
95 supportmeshname, &entitype, &nnode, &ncell,
96 &geocelltype, &nconstatt, &anyprofile, nvaratt+it) < 0) {
97 MESSAGE("ERROR : struct element models information ...");
98 free(nvaratt);
99 goto ERROR;
100 }
101 }
102 free(nvaratt);
103
104
105 /*
106 * generic approach : how many fields in the file and identification
107 * of each field.
108 */
109 if ((nfield = MEDnField(fid)) < 0) {
110 MESSAGE("ERROR : How many fields in the file ...");
111 goto ERROR;
112 }
113
114 /*
115 * read values for each field
116 */
117 for (i=0; i<nfield; i++) {
118
119 if ((ncomponent = MEDfieldnComponent(fid,i+1)) < 0) {
120 MESSAGE("ERROR : number of field component ...");
121 goto ERROR;
122 }
123
124 if ((componentname = (char *) malloc(ncomponent*MED_SNAME_SIZE+1)) == NULL) {
125 MESSAGE("ERROR : memory allocation ...");
126 goto ERROR;
127 }
128
129 if ((componentunit = (char *) malloc(ncomponent*MED_SNAME_SIZE+1)) == NULL) {
130 MESSAGE("ERROR : memory allocation ...");
131 goto ERROR;
132 }
133
134 if (MEDfieldInfo(fid, i+1, fieldname, meshname, &localmesh, &fieldtype,
135 componentname, componentunit, dtunit, &nstep) < 0) {
136 MESSAGE("ERROR : Field info ...");
137 free(componentname);
138 free(componentunit);
139 goto ERROR;
140 }
141
142 free(componentname);
143 free(componentunit);
144
145 /*
146 * Read field values for each computing step
147 */
148 for (csit=0; csit<nstep; csit++) {
149
150 if (MEDfieldComputingStepInfo(fid, fieldname, csit+1, &numdt, &numit, &dt) < 0) {
151 MESSAGE("ERROR : Computing step info ...");
152 goto ERROR;
153 }
154
155 /*
156 * ... In our case, we suppose that the field values are only defined on struct element ...
157 */
158 for (it=0; it<nmodels; it++) {
159
160 geotype = *(geotypes+it);
161
162 /*
163 * How many profile for each geometry type ?
164 */
165 if ((nprofile = MEDfieldnProfile(fid, fieldname, numdt, numit, MED_STRUCT_ELEMENT, geotype,
166 profilename, localizationname)) < 0) {
167 MESSAGE("ERROR : read number of profile ");
168 goto ERROR;
169 }
170
171 /*
172 * Read values for each profile
173 */
174 for (pit=0; pit<nprofile; pit++) {
175
176 if ((nvalues = MEDfieldnValueWithProfile(fid, fieldname, numdt, numit, MED_STRUCT_ELEMENT, geotype,
177 pit+1, MED_COMPACT_STMODE, profilename, &profilesize,
178 localizationname, &nintegrationpoint)) < 0) {
179 MESSAGE("ERROR : read number of values with a profile ...");
180 goto ERROR;
181 }
182
183 if (nvalues) {
184 if ((values = (med_float *) malloc(sizeof(med_float)*nvalues*ncomponent*nintegrationpoint)) == NULL) {
185 MESSAGE("ERROR : memory allocation ...");
186 goto ERROR;
187 }
188 if (MEDfieldValueWithProfileRd(fid, fieldname, numdt, numit, MED_STRUCT_ELEMENT, geotype,
189 MED_COMPACT_STMODE, profilename,
190 MED_FULL_INTERLACE, MED_ALL_CONSTITUENT,
191 (unsigned char*) values) < 0) {
192 MESSAGE("ERROR : read fields values for cells ...");
193 goto ERROR;
194 free(values);
195 }
196 free(values);
197 }
198 }
199 }
200 }
201 }
202
203 ret=0;
204 ERROR :
205
206 free(geotypes);
207
208 if ( MEDfileObjectsUnmount(fid, sfid, MED_ELSTRUCT) < 0 ) {
209 MESSAGE("ERROR : file unmounting ...");
210 ret=-1;
211 }
212
213 if ( MEDfileObjectsUnmount(fid, mfid, MED_MESH_SUPPORT) < 0 ) {
214 MESSAGE("ERROR : file unmounting ...");
215 ret= -1;
216 }
217
218 /* close file */
219 if (MEDfileClose(fid) < 0) {
220 MESSAGE("ERROR : close file ...");
221 ret= -1;
222 }
223
224 return ret;
225 }
MED_FULL_INTERLACE
Definition: med.h:96
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
MEDnField
MEDC_EXPORT med_int MEDnField(const med_idt fid)
Cette fonction permet de lire le nombre de champs dans un fichier.
Definition: MEDnField.c:35
med_idt
hid_t med_idt
Definition: med.h:331
main
int main(int argc, char **argv)
Definition: UsesCase_MEDfield_16.c:36
MED_STRUCT_ELEMENT
Definition: med.h:144
MEDfileObjectsUnmount
MEDC_EXPORT med_err MEDfileObjectsUnmount(const med_idt fid, const med_idt mid, const med_class medclass)
Une fois le démontage effectué, les données précédemment montées ne sont plus accessibles.
Definition: MEDfileObjectsUnmount.c:33
med_entity_type
med_entity_type
Definition: med.h:143
MEDfieldnValueWithProfile
MEDC_EXPORT med_int MEDfieldnValueWithProfile(const med_idt fid, const char *const fieldname, const med_int numdt, const med_int numit, const med_entity_type entitype, const med_geometry_type geotype, const int profileit, const med_storage_mode storagemode, char *const profilename, med_int *const profilesize, char *const localizationname, med_int *const nintegrationpoint)
Cette fonction permet de lire le nombre de valeurs à lire dans un champ pour une étape de calcul,...
Definition: MEDfieldnValueWithProfile.c:45
MESSAGE
#define MESSAGE(chaine)
Definition: med_utils.h:324
med_int
int med_int
Definition: med.h:342
med_field_type
med_field_type
Definition: med.h:165
MED_ACC_RDWR
Definition: med.h:121
med.h
med_bool
med_bool
Definition: med.h:260
MED_MESH_SUPPORT
Definition: med.h:189
med_float
double med_float
Definition: med.h:336
MEDfileObjectsMount
MEDC_EXPORT med_idt MEDfileObjectsMount(const med_idt fid, const char *const filename, const med_class medclass)
Cette routine permet de monter dans le fichier courant un type de données (exemples les maillages,...
Definition: MEDfileObjectsMount.c:40
MEDfieldnProfile
MEDC_EXPORT med_int MEDfieldnProfile(const med_idt fid, const char *const fieldname, const med_int numdt, const med_int numit, const med_entity_type enttype, const med_geometry_type geotype, char *const defaultprofilename, char *const defaultlocalizationname)
Cette fonction permet de lire le nombre de profils référencés dans un champ pour une étape de calcul,...
Definition: MEDfieldnProfile.c:40
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
MEDfieldValueWithProfileRd
MEDC_EXPORT med_err MEDfieldValueWithProfileRd(const med_idt fid, const char *const fieldname, const med_int numdt, const med_int numit, const med_entity_type entitype, const med_geometry_type geotype, const med_storage_mode storagemode, const char *const profilename, const med_switch_mode switchmode, const med_int componentselect, unsigned char *const value)
Cette fonction permet de lire les valeurs d'un champ définies sur des entités d'un maillage pour une ...
Definition: MEDfieldValueWithProfileRd.c:42
MED_NAME_SIZE
#define MED_NAME_SIZE
Definition: med.h:81
MEDfieldComputingStepInfo
MEDC_EXPORT med_err MEDfieldComputingStepInfo(const med_idt fid, const char *const fieldname, const int csit, med_int *const numdt, med_int *const numit, med_float *const dt)
Cette fonction permet de lire les informations caractérisant une étape de calcul : numéro de pas de t...
Definition: MEDfieldComputingStepInfo.c:39
MEDfileClose
MEDC_EXPORT med_err MEDfileClose(med_idt fid)
Fermeture d'un fichier MED.
Definition: MEDfileClose.c:30
MED_ALL_CONSTITUENT
#define MED_ALL_CONSTITUENT
Definition: med.h:299
MED_COMPACT_STMODE
Definition: med.h:110
MEDfieldInfo
MEDC_EXPORT med_err MEDfieldInfo(const med_idt fid, const int ind, char *const fieldname, char *const meshname, med_bool *const localmesh, med_field_type *const fieldtype, char *const componentname, char *const componentunit, char *const dtunit, med_int *const ncstp)
Cette fonction permet de lire les informations concernant le champ d'indice ind .
Definition: MEDfieldInfo.c:42
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_ELSTRUCT
Definition: med.h:189
MEDfieldnComponent
MEDC_EXPORT med_int MEDfieldnComponent(const med_idt fid, const int ind)
Cette fonction lit le nombre de composantes d'un champ.
Definition: MEDfieldnComponent.c:34
med_utils.h