MED fichier
UsesCase_MEDfield_18.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 18 : read a field (generic approach) in a MED file with
32  * values defined on integration points on struct elements
33  */
34 
35 
36 int main (int argc, char **argv) {
37  med_idt fid,mfid,sfid,cmfid;
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=NULL, 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=NULL;
66  med_bool coordinatechangement;
67  med_bool geotransformation;
68  int ret=-1;
69 
70 
71  /* open file */
72  fid = MEDfileOpen("UsesCase_MEDfield_17.med",MED_ACC_RDWR);
73  if (fid < 0) {
74  MESSAGE("ERROR : open file ...");
75  return -1;
76  }
77 
78  if (( mfid=MEDfileObjectsMount(fid, "UsesCase_MEDstructElement_1.med",MED_MESH_SUPPORT)) < 0 ) {
79  MESSAGE("ERROR : file mounting ...");
80  return -1;
81  }
82 
83  if (( sfid=MEDfileObjectsMount(fid, "UsesCase_MEDstructElement_1.med",MED_ELSTRUCT)) < 0 ) {
84  MESSAGE("ERROR : file mounting ...");
85  return -1;
86  }
87 
88  if (( cmfid=MEDfileObjectsMount(fid, "UsesCase_MEDstructElement_1.med",MED_MESH)) < 0 ) {
89  MESSAGE("ERROR : file mounting ...");
90  return -1;
91  }
92 
93 
94  /*
95  * generic approach : how many fields in the file and identification
96  * of each field.
97  */
98  if ((nfield = MEDnField(fid)) < 0) {
99  MESSAGE("ERROR : How many fields in the file ...");
100  return -1;
101  }
102 
103  /*
104  * read values for each field
105  */
106  for (i=0; i<nfield; i++) {
107 
108  if ((ncomponent = MEDfieldnComponent(fid,i+1)) < 0) {
109  MESSAGE("ERROR : number of field component ...");
110  return -1;
111  }
112 
113  if ((componentname = (char *) malloc(ncomponent*MED_SNAME_SIZE+1)) == NULL) {
114  MESSAGE("ERROR : memory allocation ...");
115  return -1;
116  }
117 
118  if ((componentunit = (char *) malloc(ncomponent*MED_SNAME_SIZE+1)) == NULL) {
119  MESSAGE("ERROR : memory allocation ...");
120  return -1;
121  }
122 
123  if (MEDfieldInfo(fid, i+1, fieldname, meshname, &localmesh, &fieldtype,
124  componentname, componentunit, dtunit, &nstep) < 0) {
125  MESSAGE("ERROR : Field info ...");
126  free(componentname);
127  free(componentunit);
128  return -1;
129  }
130 
131  free(componentname);
132  free(componentunit);
133 
134  /* read how many struct element models in the mesh ? */
135  if ((nmodels = MEDmeshnEntity(fid, meshname, MED_NO_DT, MED_NO_IT, MED_STRUCT_ELEMENT, MED_GEO_ALL,
136  MED_UNDEF_DATATYPE, MED_NO_CMODE,&coordinatechangement,
137  &geotransformation)) < 0) {
138  MESSAGE("ERROR : number of nodes ...");
139  return -1;
140  }
141  geotypes = (med_geometry_type *) malloc(sizeof(med_geometry_type)*nmodels);
142  nvaratt = (med_int *) malloc(sizeof(med_int)*nmodels);
143 
144  for (it=0; it<nmodels; it++) {
145  if (MEDstructElementInfo(mfid, it+1, elementname, geotypes+it, &elementdim,
146  supportmeshname, &entitype, &nnode, &ncell,
147  &geocelltype, &nconstatt, &anyprofile, nvaratt+it) < 0) {
148  MESSAGE("ERROR : struct element models information ...");
149  return -1;
150  }
151  }
152 
153 
154  /*
155  * Read field values for each computing step
156  */
157  for (csit=0; csit<nstep; csit++) {
158 
159  if (MEDfieldComputingStepInfo(fid, fieldname, csit+1, &numdt, &numit, &dt) < 0) {
160  MESSAGE("ERROR : Computing step info ...");
161  return -1;
162  }
163 
164  /*
165  * ... In our case, we suppose that the field values are only defined on struct element ...
166  */
167  for (it=0; it<nmodels; it++) {
168  geotype = *(geotypes+it);
169 
170  /*
171  * How many profile for each geometry type ?
172  */
173  if ((nprofile = MEDfieldnProfile(fid, fieldname, numdt, numit, MED_STRUCT_ELEMENT, geotype,
174  profilename, localizationname)) < 0) {
175  MESSAGE("ERROR : read number of profile ");
176  return -1;
177  }
178 
179  /*
180  * Read values for each profile
181  */
182  for (pit=0; pit<nprofile; pit++) {
183 
184  if ((nvalues = MEDfieldnValueWithProfile(fid, fieldname, numdt, numit, MED_STRUCT_ELEMENT, geotype,
185  pit+1, MED_COMPACT_STMODE, profilename, &profilesize,
186  localizationname, &nintegrationpoint)) < 0) {
187  MESSAGE("ERROR : read number of values with a profile ...");
188  return -1;
189  }
190 
191  if (nvalues) {
192  if ((values = (med_float *) malloc(sizeof(med_float)*nvalues*ncomponent*nintegrationpoint)) == NULL) {
193  MESSAGE("ERROR : memory allocation ...");
194  return -1;
195  }
196  if (MEDfieldValueWithProfileRd(fid, fieldname, numdt, numit, MED_STRUCT_ELEMENT, geotype,
197  MED_COMPACT_STMODE, profilename,
199  (unsigned char*) values) < 0) {
200  MESSAGE("ERROR : read fields values for cells ...");
201  free(values);
202  return -1;
203  }
204  free(values);
205  }
206  }
207  }
208  }
209 
210  ret=0;
211  ERROR:
212  if (nvaratt)
213  free(nvaratt);
214  if (geotypes)
215  free(geotypes);
216 
217  }
218 
219 
220 
221  if ( MEDfileObjectsUnmount(fid, mfid, MED_MESH_SUPPORT) < 0 ) {
222  MESSAGE("ERROR : file unmounting ...");
223  ret=-1;
224  }
225 
226  if ( MEDfileObjectsUnmount(fid, sfid, MED_ELSTRUCT) < 0 ) {
227  MESSAGE("ERROR : file unmounting ...");
228  ret=-1;
229  }
230 
231 
232  if ( MEDfileObjectsUnmount(fid, cmfid, MED_MESH) < 0 ) {
233  MESSAGE("ERROR : file unmounting ...");
234  ret=-1;
235  }
236 
237  /* close file */
238  if (MEDfileClose(fid) < 0) {
239  MESSAGE("ERROR : close file ...");
240  ret=-1;
241  }
242 
243  return ret;
244 }
MED_FULL_INTERLACE
Definition: med.h:96
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
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
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
MED_NO_CMODE
Definition: med.h:255
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_UNDEF_DATATYPE
Definition: med.h:152
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
MED_GEO_ALL
#define MED_GEO_ALL
Definition: med.h:236
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
MED_NO_DT
#define MED_NO_DT
Definition: med.h:320
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
main
int main(int argc, char **argv)
Definition: UsesCase_MEDfield_18.c:36
MED_MESH
Definition: med.h:188
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_NO_IT
#define MED_NO_IT
Definition: med.h:321
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