MED fichier
UsesCase_MEDfield_12.c
Aller à la documentation de ce fichier.
1/* This file is part of MED.
2 *
3 * COPYRIGHT (C) 1999 - 2020 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 12 : read a field (generic approach) in a MED file with computing steps,
32 * profiles, integration points and interpolation families
33 */
34
35
36int main (int argc, char **argv) {
37 med_idt fid;
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 geotype;
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 med_int ninterp;
59 char interpname[MED_NAME_SIZE+1];
60 int ret=-1;
61
62
63 /* open file */
64 fid = MEDfileOpen("UsesCase_MEDfield_10.med",MED_ACC_RDONLY);
65 if (fid < 0) {
66 MESSAGE("ERROR : open file ...");
67 return -1;
68 }
69
70 /*
71 * generic approach : how many fields in the file and identification
72 * of each field.
73 */
74 if ((nfield = MEDnField(fid)) < 0) {
75 MESSAGE("ERROR : How many fields in the file ...");
76 return -1;
77 }
78
79 /*
80 * read values for each field
81 */
82 for (i=0; i<nfield; i++) {
83
84 if ((ncomponent = MEDfieldnComponent(fid,i+1)) < 0) {
85 MESSAGE("ERROR : number of field component ...");
86 return -1;
87 }
88
89 if ((componentname = (char *) malloc(ncomponent*MED_SNAME_SIZE+1)) == NULL) {
90 MESSAGE("ERROR : memory allocation ...");
91 return -1;
92 }
93
94 if ((componentunit = (char *) malloc(ncomponent*MED_SNAME_SIZE+1)) == NULL) {
95 MESSAGE("ERROR : memory allocation ...");
96 return -1;
97 }
98
99 if (MEDfieldInfo(fid, i+1, fieldname, meshname, &localmesh, &fieldtype,
100 componentname, componentunit, dtunit, &nstep) < 0) {
101 MESSAGE("ERROR : Field info ...");
102 free(componentname);
103 free(componentunit);
104 return -1;
105 }
106
107 free(componentname);
108 free(componentunit);
109
110 /*
111 * Read how many interpolation family name in the field ?
112 */
113 if ((ninterp = MEDfieldnInterp(fid, fieldname)) < 0) {
114 MESSAGE("ERROR : Read how many interpolation functions for the field ...");
115 return -1;
116 }
117 /* - Read each interlolation family name
118 * - The way to read an interploation family and it's basis functions
119 * is described in UsesCase_MEDinterp_2 and UsesCase_MEDinterp_3 uses case
120 */
121 for (it=0; it<ninterp; it++) {
122 if (MEDfieldInterpInfo(fid,fieldname,it+1,interpname) < 0) {
123 MESSAGE("ERROR : read interpolation family name ...");
124 return -1;
125 }
126 }
127
128 /*
129 * Read field values for each computing step
130 */
131 for (csit=0; csit<nstep; csit++) {
132
133 if (MEDfieldComputingStepInfo(fid, fieldname, csit+1, &numdt, &numit, &dt) < 0) {
134 MESSAGE("ERROR : Computing step info ...");
135 return -1;
136 }
137
138 /*
139 * ... In our case, we suppose that the field values are only defined on cells ...
140 */
141 for (it=1; it<=MED_N_CELL_FIXED_GEO; it++) {
142
143 geotype = geotypes[it];
144
145 /*
146 * How many profile for each geometry type ?
147 */
148 if ((nprofile = MEDfieldnProfile(fid, fieldname, numdt, numit, MED_CELL, geotype,
149 profilename, localizationname)) < 0) {
150 MESSAGE("ERROR : read number of profile ");
151 return -1;
152 }
153
154 /*
155 * Read values for each profile
156 */
157 for (pit=0; pit<nprofile; pit++) {
158
159 if ((nvalues = MEDfieldnValueWithProfile(fid, fieldname, numdt, numit, MED_CELL, geotype,
160 pit+1, MED_COMPACT_STMODE, profilename, &profilesize,
161 localizationname, &nintegrationpoint)) < 0) {
162 MESSAGE("ERROR : read number of values with a profile ...");
163 return -1;
164 }
165
166 if (nvalues) {
167 if ((values = (med_float *) malloc(sizeof(med_float)*nvalues*ncomponent*nintegrationpoint)) == NULL) {
168 MESSAGE("ERROR : memory allocation ...");
169 return -1;
170 }
171 if (MEDfieldValueWithProfileRd(fid, fieldname, numdt, numit, MED_CELL, geotype,
172 MED_COMPACT_STMODE, profilename,
174 (unsigned char*) values) < 0) {
175 MESSAGE("ERROR : read fields values for cells ...");
176 free(values);
177 return -1;
178 }
179 free(values);
180 }
181 }
182 }
183 }
184 }
185
186 ret=0;
187 ERROR:
188
189 /* close file */
190 if (MEDfileClose(fid) < 0) {
191 MESSAGE("ERROR : close file ...");
192 ret=-1;
193 }
194
195 return ret;
196}
int main(int argc, char **argv)
#define MED_NAME_SIZE
#define MED_SNAME_SIZE
#define MED_ALL_CONSTITUENT
#define MED_N_CELL_FIXED_GEO
MEDC_EXPORT med_geometry_type MED_GET_CELL_GEOMETRY_TYPE[MED_N_CELL_FIXED_GEO+2]
#define MESSAGE(chaine)
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
MEDC_EXPORT med_err MEDfieldInterpInfo(const med_idt fid, const char *const fieldname, const int interpit, char *const interpname)
Cette routine indique le nom interpname de la interpit ème fonction d'interpolation associées au cham...
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...
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 ...
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,...
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,...
MEDC_EXPORT med_int MEDfieldnComponent(const med_idt fid, const int ind)
Cette fonction lit le nombre de composantes d'un champ.
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 .
MEDC_EXPORT med_int MEDfieldnInterp(const med_idt fid, const char *const fieldname)
Cette routine renvoie le nombre de fonctions d'interpolation associées au champ résultat fieldname.
MEDC_EXPORT med_err MEDfileClose(med_idt fid)
Fermeture d'un fichier MED.
MEDC_EXPORT med_idt MEDfileOpen(const char *const filename, const med_access_mode accessmode)
Ouverture d'un fichier MED.
Definition MEDfileOpen.c:42