MED fichier

UsesCase_MEDfield_2.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 /*
19 * Field use case 2 : read the field of use case 1
20 */
21
22
23 #include <med.h>
24 #define MESGERR 1
25 #include <med_utils.h>
26
27 #include <string.h>
28
29 int main (int argc, char **argv) {
30 med_idt fid;
31 char meshname[MED_NAME_SIZE+1]="";
32 med_bool localmesh;
33 const char fieldname[MED_NAME_SIZE+1] = "TEMPERATURE_FIELD";
34 med_field_type fieldtype;
35 char componentname[MED_SNAME_SIZE+1]="";
36 char componentunit[MED_SNAME_SIZE+1]="";
37 char dtunit[MED_SNAME_SIZE+1]="";
38 med_float *verticesvalues = NULL;
39 med_float *tria3values = NULL;
40 med_float *quad4values = NULL;
41 med_int nstep, nvalues;
42 const med_int ncomponent = 1;
43 int ret=-1;
44
45 /* open MED file with READ ONLY access mode */
46 fid = MEDfileOpen("UsesCase_MEDfield_1.med",MED_ACC_RDONLY);
47 if (fid < 0) {
48 MESSAGE("ERROR : open file ...");
49 goto ERROR;
50 }
51
52 /*
53 * ... we know that the MED file has only one field with one component ,
54 * a real code would check ...
55 */
56
57 /*
58 * if you know the field name, direct access to field informations
59 */
60 if (MEDfieldInfoByName(fid, fieldname, meshname, &localmesh, &fieldtype,
61 componentname, componentunit, dtunit, &nstep) < 0) {
62 MESSAGE("ERROR : Field info by name ...");
63 goto ERROR;
64 }
65
66 /*
67 * ... we know that the field values are defined on vertices and MED_TRIA3
68 * and MED_QUAD4 cells, a real code would check ...
69 */
70
71 /* MED_NODE */
72 if ((nvalues = MEDfieldnValue(fid, fieldname, MED_NO_DT, MED_NO_IT, MED_NODE, MED_NONE)) < 0) {
73 MESSAGE("ERROR : read number of values ...");
74 goto ERROR;
75 }
76
77 if ((verticesvalues = (med_float *) malloc(sizeof(med_float)*nvalues*ncomponent)) == NULL) {
78 MESSAGE("ERROR : memory allocation ...");
79 goto ERROR;
80 }
81 if (MEDfieldValueRd(fid, fieldname, MED_NO_DT, MED_NO_IT, MED_NODE, MED_NONE,
82 MED_FULL_INTERLACE, MED_ALL_CONSTITUENT, (unsigned char*) verticesvalues) < 0) {
83 MESSAGE("ERROR : read fields values on vertices ...");
84 free(verticesvalues);
85 goto ERROR;
86 }
87 free(verticesvalues);
88
89 /* MED_TRIA3 */
90 if ((nvalues = MEDfieldnValue(fid, fieldname, MED_NO_DT, MED_NO_IT, MED_CELL,
91 MED_TRIA3)) < 0) {
92 MESSAGE("ERROR : read number of values ...");
93 goto ERROR;
94 }
95 if ((tria3values = (med_float *) malloc(sizeof(med_float)*nvalues*ncomponent)) == NULL) {
96 MESSAGE("ERROR : memory allocation ...");
97 goto ERROR;
98 }
99 if (MEDfieldValueRd(fid, fieldname, MED_NO_DT, MED_NO_IT, MED_CELL, MED_TRIA3,
100 MED_FULL_INTERLACE, MED_ALL_CONSTITUENT, (unsigned char*) tria3values) < 0) {
101 MESSAGE("ERROR : read fields values for MED_TRIA3 cells ...");
102 free(tria3values);
103 goto ERROR;
104 }
105 free(tria3values);
106
107 /* MED_QUAD4 */
108 if ((nvalues = MEDfieldnValue(fid, fieldname, MED_NO_DT, MED_NO_IT, MED_CELL,
109 MED_QUAD4)) < 0) {
110 MESSAGE("ERROR : read number of values ...");
111 goto ERROR;
112 }
113 if ((quad4values = (med_float *) malloc(sizeof(med_float)*nvalues*ncomponent)) == NULL) {
114 MESSAGE("ERROR : memory allocation ...");
115 goto ERROR;
116 }
117 if (MEDfieldValueRd(fid, fieldname, MED_NO_DT, MED_NO_IT, MED_CELL, MED_QUAD4,
118 MED_FULL_INTERLACE, MED_ALL_CONSTITUENT, (unsigned char*) quad4values) < 0) {
119 MESSAGE("ERROR : read fields values for MED_QUAD4 cells ...");
120 free(quad4values);
121 goto ERROR;
122 }
123 free(quad4values);
124
125 ret=0;
126 ERROR:
127
128 /* close file */
129 if (MEDfileClose(fid) < 0) {
130 MESSAGE("ERROR : close file ...");
131 ret=-1;
132 }
133
134 return ret;
135 }
MED_TRIA3
#define MED_TRIA3
Definition: med.h:203
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_SNAME_SIZE
#define MED_SNAME_SIZE
Definition: med.h:82
med_idt
hid_t med_idt
Definition: med.h:331
MED_ACC_RDONLY
Definition: med.h:120
MED_CELL
Definition: med.h:143
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
MEDfieldValueRd
MEDC_EXPORT med_err MEDfieldValueRd(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_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: MEDfieldValueRd.c:44
med.h
med_bool
med_bool
Definition: med.h:260
med_float
double med_float
Definition: med.h:336
MEDfieldInfoByName
MEDC_EXPORT med_err MEDfieldInfoByName(const med_idt fid, const 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 de nom fieldname.
Definition: MEDfieldInfoByName.c:39
MED_NO_DT
#define MED_NO_DT
Definition: med.h:320
MED_NONE
#define MED_NONE
Definition: med.h:231
MED_NODE
Definition: med.h:143
main
int main(int argc, char **argv)
Definition: UsesCase_MEDfield_2.c:29
MEDfieldnValue
MEDC_EXPORT med_int MEDfieldnValue(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)
Cette fonction permet de lire le nombre de valeurs dans un champ pour une étape de calcul,...
Definition: MEDfieldnValue.c:38
MED_NAME_SIZE
#define MED_NAME_SIZE
Definition: med.h:81
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_NO_IT
#define MED_NO_IT
Definition: med.h:321
med_utils.h
MED_QUAD4
#define MED_QUAD4
Definition: med.h:204