MED fichier: Concepts et fonctionnalité de base pour créer et lire un champ de résultat

MED fichier

- Documentation MED
- Guides d'utilisation
- Guides de référence

Concepts et fonctionnalité de base pour créer et lire un champ de résultat

Créer un champ de résultat

Lire un champ de résultats

Créer des séquences de calcul dans un champ de résultats

Lire un champ de résultats avec des séquences de calcul

### Créer un champ de résultat

La routine MEDfieldCr / mfdcre permet de créer un champ de résultat dans un fichier MED. Un champ est identifié par son nom qui est une chaîne de MED_NAME_SIZE caractères. Un champ de résultat est constitué d'une ou plusieurs composantes scalaires. A un champ est associé un type et les valeurs correspondant aux différentes composantes sont toutes du même type qui peut être MED_FLOAT64 (réel), MED_INT (entier). Chaque composante se voit attribuer un nom et une unité. Les valeurs du champ sont associée aux entités d'un seul maillage.

La routine MEDfieldValueWr / mfdrvw mfdivw permet d'écrire dans un champ des valeurs correspondant à un résultat portant sur un type d'entité géométrique d'un maillage donné. Si le champ porte sur plusieurs types d'entité, il faut donc appeler cette routine autant de fois qu'il y a de types différents. Les valeurs du champ peuvent porter sur les noeuds, les éléments, les points de Gauss des éléments, les noeuds par élément. Nous ne traitons dans un premier temps que les champs aux noeuds et aux éléments.

Le cas d'utilisation suivant permet de créer un champ Température à une composante. Les résultats de ce champ portent sur les noeuds et les éléments du maillage. A noter que le maillage se trouvant dans un fichier différent de celui du champ, il est possible d'indiquer le nom et le chemin d'accès au fichier contenant le maillage en créant un lien avec la routine MEDlinkWr / mlnliw. Le lien porte alors le nom du maillage.

/* This file is part of MED.
*
* COPYRIGHT (C) 1999 - 2019 EDF R&D, CEA/DEN
* MED is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* MED is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with MED. If not, see <http://www.gnu.org/licenses/>.
*/
/*
* Field use case 1 : write a field on mesh vertices and elements
*/
#include <med.h>
#define MESGERR 1
#include <med_utils.h>
#include <string.h>
int main (int argc, char **argv) {
med_idt fid;
const char meshname[MED_NAME_SIZE+1] = "2D unstructured mesh";
const char fieldname[MED_NAME_SIZE+1] = "TEMPERATURE_FIELD";
const med_int ncomponent = 1;
const char componentname[MED_SNAME_SIZE+1] = "TEMPERATURE";
const char componentunit[MED_SNAME_SIZE+1] = "C";
const med_float verticesvalues[15] = { 0., 100., 200., 300., 400.,
500., 600., 700., 800., 900,
1000., 1100, 1200., 1300., 1500. };
const med_int nnodes = 15;
const med_float tria3values[8] = {1000., 2000., 3000., 4000.,
5000., 6000., 7000., 8000.};
const med_int ntria3 = 8;
const med_float quad4values[4] = {10000., 20000., 30000., 4000.};
const med_int nquad4 = 4;
int ret=-1;
/* file creation */
fid = MEDfileOpen("UsesCase_MEDfield_1.med",MED_ACC_CREAT);
if (fid < 0) {
MESSAGE("ERROR : file creation ...");
goto ERROR;
}
/* field creation : temperature field : 1 component in celsius degree
* the mesh is the 2D unstructured mesh of UsescaseMEDmesh_1.c
*/
/* create mesh link */
if (MEDlinkWr(fid,meshname,"./UsesCase_MEDmesh_1.med") < 0) {
MESSAGE("ERROR : create mesh link ...");
goto ERROR;
}
if (MEDfieldCr(fid, fieldname, MED_FLOAT64,
ncomponent, componentname, componentunit,"",
meshname) < 0) {
MESSAGE("ERROR : create field");
goto ERROR;
}
/* write field values at vertices */
if (MEDfieldValueWr(fid, fieldname, MED_NO_DT, MED_NO_IT, 0.0, MED_NODE,
MED_NONE, MED_FULL_INTERLACE, MED_ALL_CONSTITUENT,
nnodes,(unsigned char*) verticesvalues) < 0) {
MESSAGE("ERROR : write field values on vertices");
goto ERROR;
}
/* write values at cell centers : 8 MED_TRIA3 and 4 MED_QUAD4 */
/* MED_TRIA3 */
if (MEDfieldValueWr(fid, fieldname, MED_NO_DT, MED_NO_IT, 0.0, MED_CELL,
MED_TRIA3, MED_FULL_INTERLACE, MED_ALL_CONSTITUENT,
ntria3, (unsigned char*) tria3values) < 0) {
MESSAGE("ERROR : write field values on MED_TRIA3");
goto ERROR;
}
/* MED_QUAD4 */
if (MEDfieldValueWr(fid, fieldname, MED_NO_DT, MED_NO_IT, 0.0, MED_CELL,
MED_QUAD4, MED_FULL_INTERLACE, MED_ALL_CONSTITUENT,
nquad4, (unsigned char*) quad4values) < 0) {
MESSAGE("ERROR : write field values on MED_QUAD4 ");
goto ERROR;
}
ret=0;
ERROR :
/* close file */
if (MEDfileClose(fid) < 0) {
MESSAGE("ERROR : close file ...");
ret = -1;
}
return ret;
}

### Lire un champ de résultats

Comme pour les maillages, l'accès aux champs stockés dans un fichier se fait via deux approches possibles : accès par le nom ou via un itérateur. La routine MEDfieldInfoByName / mfdfin permet de lire les informations relatives à un champ dont on connaît le nom. Les informations lues correspondent à celles écrites par la routine MEDfieldCr / mfdcre.

Il est ensuite nécessaire pour chaque type d'entité présent dans le maillage de connaître le nombre de valeurs à lire avec la routine MEDfieldnValue / mfdnva. Le nombre de valeurs ainsi obtenu, permet de calculer la zône mémoire à allouer en vue de lire ces données (à savoir le nombre de valeurs * nombre de composantes du champ). Enfin la routine MEDfieldValueRd / mfdrvr mfdivr permet de lire les valeurs associées à un type d'entité donné.

Le cas d'utilisation suivant montre comment lire les données du champ écrites dans le cas d'utilisation précédent.

/* This file is part of MED.
*
* COPYRIGHT (C) 1999 - 2019 EDF R&D, CEA/DEN
* MED is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* MED is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with MED. If not, see <http://www.gnu.org/licenses/>.
*/
/*
* Field use case 2 : read the field of use case 1
*/
#include <med.h>
#define MESGERR 1
#include <med_utils.h>
#include <string.h>
int main (int argc, char **argv) {
med_idt fid;
char meshname[MED_NAME_SIZE+1]="";
med_bool localmesh;
const char fieldname[MED_NAME_SIZE+1] = "TEMPERATURE_FIELD";
med_field_type fieldtype;
char componentname[MED_SNAME_SIZE+1]="";
char componentunit[MED_SNAME_SIZE+1]="";
char dtunit[MED_SNAME_SIZE+1]="";
med_float *verticesvalues = NULL;
med_float *tria3values = NULL;
med_float *quad4values = NULL;
med_int nstep, nvalues;
const med_int ncomponent = 1;
int ret=-1;
/* open MED file with READ ONLY access mode */
fid = MEDfileOpen("UsesCase_MEDfield_1.med",MED_ACC_RDONLY);
if (fid < 0) {
MESSAGE("ERROR : open file ...");
goto ERROR;
}
/*
* ... we know that the MED file has only one field with one component ,
* a real code would check ...
*/
/*
* if you know the field name, direct access to field informations
*/
if (MEDfieldInfoByName(fid, fieldname, meshname, &localmesh, &fieldtype,
componentname, componentunit, dtunit, &nstep) < 0) {
MESSAGE("ERROR : Field info by name ...");
goto ERROR;
}
/*
* ... we know that the field values are defined on vertices and MED_TRIA3
* and MED_QUAD4 cells, a real code would check ...
*/
/* MED_NODE */
if ((nvalues = MEDfieldnValue(fid, fieldname, MED_NO_DT, MED_NO_IT, MED_NODE, MED_NONE)) < 0) {
MESSAGE("ERROR : read number of values ...");
goto ERROR;
}
if ((verticesvalues = (med_float *) malloc(sizeof(med_float)*nvalues*ncomponent)) == NULL) {
MESSAGE("ERROR : memory allocation ...");
goto ERROR;
}
if (MEDfieldValueRd(fid, fieldname, MED_NO_DT, MED_NO_IT, MED_NODE, MED_NONE,
MED_FULL_INTERLACE, MED_ALL_CONSTITUENT, (unsigned char*) verticesvalues) < 0) {
MESSAGE("ERROR : read fields values on vertices ...");
free(verticesvalues);
goto ERROR;
}
free(verticesvalues);
/* MED_TRIA3 */
if ((nvalues = MEDfieldnValue(fid, fieldname, MED_NO_DT, MED_NO_IT, MED_CELL,
MED_TRIA3)) < 0) {
MESSAGE("ERROR : read number of values ...");
goto ERROR;
}
if ((tria3values = (med_float *) malloc(sizeof(med_float)*nvalues*ncomponent)) == NULL) {
MESSAGE("ERROR : memory allocation ...");
goto ERROR;
}
if (MEDfieldValueRd(fid, fieldname, MED_NO_DT, MED_NO_IT, MED_CELL, MED_TRIA3,
MED_FULL_INTERLACE, MED_ALL_CONSTITUENT, (unsigned char*) tria3values) < 0) {
MESSAGE("ERROR : read fields values for MED_TRIA3 cells ...");
free(tria3values);
goto ERROR;
}
free(tria3values);
/* MED_QUAD4 */
if ((nvalues = MEDfieldnValue(fid, fieldname, MED_NO_DT, MED_NO_IT, MED_CELL,
MED_QUAD4)) < 0) {
MESSAGE("ERROR : read number of values ...");
goto ERROR;
}
if ((quad4values = (med_float *) malloc(sizeof(med_float)*nvalues*ncomponent)) == NULL) {
MESSAGE("ERROR : memory allocation ...");
goto ERROR;
}
if (MEDfieldValueRd(fid, fieldname, MED_NO_DT, MED_NO_IT, MED_CELL, MED_QUAD4,
MED_FULL_INTERLACE, MED_ALL_CONSTITUENT, (unsigned char*) quad4values) < 0) {
MESSAGE("ERROR : read fields values for MED_QUAD4 cells ...");
free(quad4values);
goto ERROR;
}
free(quad4values);
ret=0;
ERROR:
/* close file */
if (MEDfileClose(fid) < 0) {
MESSAGE("ERROR : close file ...");
ret=-1;
}
return ret;
}

Une approche plus générique dans la lecture d'un champ est donnée dans l'exemple suivant. La routine MEDnField / mfdnfd va lire le nombre de champ dans le fichier. Il s'agit ensuite d'itérer sur ces champs. La routine MEDfieldInfo / mfdfdi permet de lire les informations relatives à un champ à partir d'un itérateur. La routine MEDfieldnComponent / mfdnfc permet de récupérer le nombre de composante du champ en vue de dimensionner la taille des paramètres utiles à la routine MEDfieldInfo.

Une fois les informations générales du champ connues, il est nécessaire pour chaque type d'entité présent dans le maillage de connaître le nombre de valeurs à lire avec la routine MEDfieldnValue / mfdnva. Le nombre de valeurs ainsi obtenu, permet de calculer la zône mémoire à allouer en vue de lire ces données (à savoir le nombre de valeurs * nombre de composantes du champ). Enfin la routine MEDfieldValueRd / mfdrvr mfdivr permet de lire les valeurs associées à un type d'entité donné. Pour les mailles et le cas échéant les faces et les arêtes, il est possible d'itérer sur le nombre de type géométrique pré-défini dans le modèle. Par exemple pour les mailles, cette valeur est fixée par le paramètre constant MED_N_CELL_FIXED_GEO.

/* This file is part of MED.
*
* COPYRIGHT (C) 1999 - 2019 EDF R&D, CEA/DEN
* MED is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* MED is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with MED. If not, see <http://www.gnu.org/licenses/>.
*/
#include <med.h>
#define MESGERR 1
#include <med_utils.h>
#include <string.h>
#include <med.h>
#define MESGERR 1
#include <med_utils.h>
#include <string.h>
/*
* Field use case 3 : read a field (generic approach)
*/
int main (int argc, char **argv) {
med_idt fid;
med_idt nfield, i, j;
char meshname[MED_NAME_SIZE+1]="";
med_bool localmesh;
char fieldname[MED_NAME_SIZE+1]="";
med_field_type fieldtype;
char *componentname = NULL;
char *componentunit = NULL;
char dtunit[MED_SNAME_SIZE+1]="";
med_float *values = NULL;
med_int nstep, nvalues;
med_int ncomponent;
med_geometry_type geotype;
med_geometry_type *geotypes = MED_GET_CELL_GEOMETRY_TYPE;
int ret=-1;
/* file creation */
fid = MEDfileOpen("UsesCase_MEDfield_1.med",MED_ACC_RDONLY);
if (fid < 0) {
MESSAGE("ERROR : open file ...");
goto ERROR;
}
/*
* generic approach : how many fields in the file and identification
* of each field.
*/
if ((nfield = MEDnField(fid)) < 0) {
MESSAGE("ERROR : How many fields in the file ...");
goto ERROR;
}
for (i=0; i<nfield; i++) {
/* field information
* ... we know that the field has no computation step
* and that the field values type is MED_FLOAT64, a real code would check ...
*/
if ((ncomponent = MEDfieldnComponent(fid,i+1)) < 0) {
MESSAGE("ERROR : number of field component ...");
goto ERROR;
}
if ((componentname = (char *) malloc(ncomponent*MED_SNAME_SIZE+1)) == NULL) {
MESSAGE("ERROR : memory allocation ...");
goto ERROR;
}
if ((componentunit = (char *) malloc(ncomponent*MED_SNAME_SIZE+1)) == NULL) {
MESSAGE("ERROR : memory allocation ...");
goto ERROR;
}
if (MEDfieldInfo(fid, i+1, fieldname, meshname, &localmesh, &fieldtype,
componentname, componentunit, dtunit, &nstep) < 0) {
MESSAGE("ERROR : Field info ...");
free(componentname); free(componentunit);
goto ERROR;
}
free(componentname); free(componentunit);
/* read field values for nodes and cells */
/* MED_NODE */
if ((nvalues = MEDfieldnValue(fid, fieldname, MED_NO_DT, MED_NO_IT, MED_NODE, MED_NONE)) < 0) {
MESSAGE("ERROR : read number of values ...");
goto ERROR;
}
if (nvalues) {
if ((values = (med_float *) malloc(sizeof(med_float)*nvalues*ncomponent)) == NULL) {
MESSAGE("ERROR : memory allocation ...");
goto ERROR;
}
if (MEDfieldValueRd(fid, fieldname, MED_NO_DT, MED_NO_IT, MED_NODE, MED_NONE,
MED_FULL_INTERLACE, MED_ALL_CONSTITUENT, (unsigned char*) values) < 0) {
MESSAGE("ERROR : read fields values defined on vertices ...");
free(values);
goto ERROR;
}
free(values);
}
/* MED_CELL */
for (j=1; j<=MED_N_CELL_FIXED_GEO; j++) {
geotype = geotypes[j];
if ((nvalues = MEDfieldnValue(fid, fieldname, MED_NO_DT, MED_NO_IT, MED_CELL,
geotype)) < 0) {
MESSAGE("ERROR : read number of values ...");
goto ERROR;
}
if (nvalues) {
if ((values = (med_float *) malloc(sizeof(med_float)*nvalues*ncomponent)) == NULL) {
MESSAGE("ERROR : memory allocation ...");
goto ERROR;
}
if (MEDfieldValueRd(fid, fieldname, MED_NO_DT, MED_NO_IT, MED_CELL, geotype,
MED_FULL_INTERLACE, MED_ALL_CONSTITUENT, (unsigned char*) values) < 0) {
MESSAGE("ERROR : read fields values for cells ...");
free(values);
goto ERROR;
}
free(values);
}
}
}
ret=0;
ERROR:
/* close file */
if (MEDfileClose(fid) < 0) {
MESSAGE("ERROR : close file ...");
ret=-1;
}
return ret;
}

### Créer des séquences de calcul dans un champ de résultats

Une séquence de calcul correspond à une date (pas de temps) et/ou une étape itérative (numéro d'ordre). Les valeurs d'un champ peuvent porter sur une séquence de calcul autre que la séquence initiale (MED_NO_DT, MED_NO_IT) utilisée par défaut. La définition de la séquence de calcul se fait lors de la l'écriture des valeurs du champ avec la routine MEDfieldValueWr / mfdrvw mfdivw qui prend en paramètres le pas de temps et le numéro d'ordre.

Il est possible de référencer l'étape d'évolution d'un maillage à laquelle l'étape de calcul correspond le cas échéant avec la routine MEDfieldComputingStepMeshWr / mfdcmw. Par défaut les valeurs du champ se rapportent au maillage initial (MED_NO_DT, MED_NO_IT).

/* This file is part of MED.
*
* COPYRIGHT (C) 1999 - 2019 EDF R&D, CEA/DEN
* MED is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* MED is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with MED. If not, see <http://www.gnu.org/licenses/>.
*/
/*
* Field use case 4 : write a field with computing steps
*/
#include <med.h>
#define MESGERR 1
#include <med_utils.h>
#include <string.h>
int main (int argc, char **argv) {
med_idt fid;
const char meshname[MED_NAME_SIZE+1] = "2D unstructured mesh";
const char fieldname[MED_NAME_SIZE+1] = "TEMPERATURE_FIELD";
const med_int ncomponent = 1;
const char componentname[MED_SNAME_SIZE+1] = "TEMPERATURE";
const char componentunit[MED_SNAME_SIZE+1] = "C";
const med_int ntria3 = 8;
const med_int nquad4 = 4;
const med_float tria3values_step1[8] = {1000., 2000., 3000., 4000.,
5000., 6000., 7000., 8000.};
const med_float quad4values_step1[4] = {10000., 20000., 30000., 4000.};
const med_float tria3values_step2[8] = {1500., 2500., 3500., 4500.,
5500., 6500., 7500., 8500.};
const med_float quad4values_step2[4] = {15000., 25000., 35000., 45000.};
int ret=-1;
/* file creation */
fid = MEDfileOpen("UsesCase_MEDfield_4.med",MED_ACC_CREAT);
if (fid < 0) {
MESSAGE("ERROR : file creation ...");
goto ERROR;
}
/* create mesh link */
if (MEDlinkWr(fid,meshname,"./UsesCase_MEDmesh_1.med") < 0) {
MESSAGE("ERROR : create mesh link ...");
goto ERROR;
}
/* field creation : temperature field : 1 component in celsius degree
* the mesh is the 2D unstructured mesh of UsescaseMEDmesh_1.c
* use case. Computation step unit in 'ms'
*/
if (MEDfieldCr(fid, fieldname, MED_FLOAT64,
ncomponent, componentname, componentunit,"ms", meshname) < 0) {
MESSAGE("ERROR : create field");
goto ERROR;
}
/* two computation steps :
* - first on meshname MED_NO_DT,MED_NO_IT mesh computation step
* - second on meshname 1,3 mesh computation step */
/* write values at cell centers : 8 MED_TRIA3 and 4 MED_QUAD4 */
/* STEP 1 : dt1 = 5.5, it = 1*/
/* MED_TRIA3 */
if (MEDfieldValueWr(fid, fieldname, 1, 1, 5.5, MED_CELL, MED_TRIA3, MED_FULL_INTERLACE,
MED_ALL_CONSTITUENT, ntria3, (unsigned char*) tria3values_step1) < 0) {
MESSAGE("ERROR : write field values on MED_TRIA3");
goto ERROR;
}
/* MED_QUAD4 */
if (MEDfieldValueWr(fid, fieldname, 1, 1, 5.5, MED_CELL,MED_QUAD4, MED_FULL_INTERLACE,
MED_ALL_CONSTITUENT, nquad4, (unsigned char*) quad4values_step1) < 0) {
MESSAGE("ERROR : write field values on MED_QUAD4 ");
goto ERROR;
}
/* STEP 2 : dt2 = 8.9, it = 1*/
/* MED_TRIA3 */
if (MEDfieldValueWr(fid, fieldname, 2 , 1 , 8.9 , MED_CELL, MED_TRIA3, MED_FULL_INTERLACE,
MED_ALL_CONSTITUENT, ntria3, (unsigned char*)tria3values_step2) < 0) {
MESSAGE("ERROR : write field values on MED_TRIA3");
goto ERROR;
}
/* MED_QUAD4 */
if (MEDfieldValueWr(fid, fieldname, 2, 1, 8.9, MED_CELL, MED_QUAD4, MED_FULL_INTERLACE,
MED_ALL_CONSTITUENT, nquad4, (unsigned char*)quad4values_step2) < 0) {
MESSAGE("ERROR : write field values on MED_QUAD4 ");
goto ERROR;
}
/* Write associated mesh computation step */
if ( MEDfieldComputingStepMeshWr(fid, fieldname, 2, 1,
1, 3 ) < 0 ) {
MESSAGE("ERROR : write field mesh computation step error ");
goto ERROR;
}
ret=0;
ERROR:
/* close file */
if (MEDfileClose(fid) < 0) {
MESSAGE("ERROR : close file ...");
ret=-1;
}
return ret;
}

### Lire un champ de résultats avec des séquences de calcul

A la lecture, le nombre de séquence de calcul du champ peut est récupéré par les routines MEDfieldInfoByName / mfdfin et MEDfieldInfo / mfdfdi. Il s'agit ensuite d'itérer sur ce nombre de séquence afin de lire les valeurs portant sur les entités du maillage. La routine MEDfieldComputingStepMeshInfo / mfdcmi permet de récupérer les informations relatives à la séquence de calcul (pas de temps et numéro d'ordre), ainsi que l'étape d'évolution du maillage correspondante. La routine MEDfieldComputingStepInfo / mfdcsi permet de récupérer les informations relatives à la séquence de calcul sans se préoccuper du maillage dans le cas d'un maillage non évolutif.

/* This file is part of MED.
*
* COPYRIGHT (C) 1999 - 2019 EDF R&D, CEA/DEN
* MED is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* MED is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with MED. If not, see <http://www.gnu.org/licenses/>.
*/
/*
* Field use case 5 : read a field with following with computing steps
*/
#include <med.h>
#define MESGERR 1
#include <med_utils.h>
#include <string.h>
int main (int argc, char **argv) {
med_idt fid;
char meshname[MED_NAME_SIZE+1]="";
med_bool localmesh;
const char fieldname[MED_NAME_SIZE+1] = "TEMPERATURE_FIELD";
med_field_type fieldtype;
char componentname[MED_SNAME_SIZE+1]="";
char componentunit[MED_SNAME_SIZE+1]="";
char dtunit[MED_SNAME_SIZE+1]="";
med_float *values = NULL;
med_int nstep, nvalues;
med_int ncomponent = 1;
med_int csit, numit, numdt, meshnumit, meshnumdt, it;
med_float dt;
med_geometry_type geotype;
med_geometry_type *geotypes = MED_GET_CELL_GEOMETRY_TYPE;
int ret=-1;
/* open file */
fid = MEDfileOpen("UsesCase_MEDfield_4.med",MED_ACC_RDONLY);
if (fid < 0) {
MESSAGE("ERROR : open file ...");
goto ERROR;
}
/*
* ... we know that the MED file has only one field with one component ,
* a real code would check ...
*/
/*
* if you know the field name, direct access to field informations
*/
if (MEDfieldInfoByName(fid, fieldname, meshname, &localmesh, &fieldtype,
componentname, componentunit, dtunit, &nstep) < 0) {
MESSAGE("ERROR : Field info by name ...");
goto ERROR;
}
/*
* Read field values for each computing step
*/
for (csit=0; csit<nstep; csit++) {
if (MEDfieldComputingStepMeshInfo(fid, fieldname, csit+1, &numdt, &numit, &dt,
&meshnumdt, &meshnumit) < 0) {
MESSAGE("ERROR : Computing step info ...");
goto ERROR;
}
/*
* ... In our case, we suppose that the field values are only defined on cells ...
*/
for (it=1; it<=MED_N_CELL_FIXED_GEO; it++) {
geotype = geotypes[it];
if ((nvalues = MEDfieldnValue(fid, fieldname, numdt, numit, MED_CELL, geotype)) < 0) {
MESSAGE("ERROR : read number of values ...");
goto ERROR;
}
if (nvalues) {
if ((values = (med_float *) malloc(sizeof(med_float)*nvalues*ncomponent)) == NULL) {
MESSAGE("ERROR : memory allocation ...");
goto ERROR;
}
if (MEDfieldValueRd(fid, fieldname, numdt, numit, MED_CELL, geotype,
MED_FULL_INTERLACE, MED_ALL_CONSTITUENT, (unsigned char*) values) < 0) {
MESSAGE("ERROR : read fields values for cells ...");
free(values);
goto ERROR;
}
free(values);
}
}
}
ret=0;
ERROR:
/* close file */
if (MEDfileClose(fid) < 0) {
MESSAGE("ERROR : close file ...");
ret=-1;
}
return ret;
}

Le cas d'utilisation suivant fournit une approche générique pour lire les valeurs portant sur les mailles du maillage pour toutes les séquences de calcul de tous les champs d'un fichier MED.

/* This file is part of MED.
*
* COPYRIGHT (C) 1999 - 2019 EDF R&D, CEA/DEN
* MED is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* MED is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with MED. If not, see <http://www.gnu.org/licenses/>.
*/
/*
* Field use case 6 : read a field (generic approach) with computing steps
*/
#include <med.h>
#define MESGERR 1
#include <med_utils.h>
#include <string.h>
#include <med.h>
#define MESGERR 1
#include <med_utils.h>
#include <string.h>
int main (int argc, char **argv) {
med_idt fid;
med_idt nfield, i, j;
char meshname[MED_NAME_SIZE+1]="";
med_bool localmesh;
char fieldname[MED_NAME_SIZE+1]="";
med_field_type fieldtype;
char *componentname = NULL;
char *componentunit = NULL;
char dtunit[MED_SNAME_SIZE+1]="";
med_float *values = NULL;
med_int nstep, nvalues;
med_int ncomponent;
med_int csit, numit, numdt, meshnumit, meshnumdt, it;
med_float dt;
med_geometry_type geotype;
med_geometry_type *geotypes = MED_GET_CELL_GEOMETRY_TYPE;
int k;
int ret=-1;
/* open file */
fid = MEDfileOpen("UsesCase_MEDfield_4.med",MED_ACC_RDONLY);
if (fid < 0) {
MESSAGE("ERROR : open file ...");
goto ERROR;
}
/*
* generic approach : how many fields in the file and identification
* of each field.
*/
if ((nfield = MEDnField(fid)) < 0) {
MESSAGE("ERROR : How many fields in the file ...");
goto ERROR;
}
/*
* read values for each field
*/
for (i=0; i<nfield; i++) {
if ((ncomponent = MEDfieldnComponent(fid,i+1)) < 0) {
MESSAGE("ERROR : number of field component ...");
goto ERROR;
}
if ((componentname = (char *) malloc(ncomponent*MED_SNAME_SIZE+1)) == NULL) {
MESSAGE("ERROR : memory allocation ...");
goto ERROR;
}
if ((componentunit = (char *) malloc(ncomponent*MED_SNAME_SIZE+1)) == NULL) {
MESSAGE("ERROR : memory allocation ...");
goto ERROR;
}
if (MEDfieldInfo(fid, i+1, fieldname, meshname, &localmesh, &fieldtype,
componentname, componentunit, dtunit, &nstep) < 0) {
MESSAGE("ERROR : Field info ...");
free(componentname);
free(componentunit);
goto ERROR;
}
free(componentname);
free(componentunit);
/*
* Read field values for each computing step
*/
for (csit=0; csit<nstep; csit++) {
if (MEDfieldComputingStepMeshInfo(fid, fieldname, csit+1, &numdt, &numit, &dt,
&meshnumdt, &meshnumit) < 0) {
MESSAGE("ERROR : Computing step info ...");
goto ERROR;
}
/*
* ... In our case, we suppose that the field values are only defined on cells ...
*/
for (it=1; it<=MED_N_CELL_FIXED_GEO; it++) {
geotype = geotypes[it];
if ((nvalues = MEDfieldnValue(fid, fieldname, numdt, numit, MED_CELL, geotype)) < 0) {
MESSAGE("ERROR : read number of values ...");
goto ERROR;
}
if (nvalues) {
if ((values = (med_float *) malloc(sizeof(med_float)*nvalues*ncomponent)) == NULL) {
MESSAGE("ERROR : memory allocation ...");
goto ERROR;
}
if (MEDfieldValueRd(fid, fieldname, numdt, numit, MED_CELL, geotype,
MED_FULL_INTERLACE, MED_ALL_CONSTITUENT, (unsigned char*) values) < 0) {
MESSAGE("ERROR : read fields values for cells ...");
free(values);
goto ERROR;
}
free(values);
}
}
}
}
ret=0;
ERROR:
/* close file */
if (MEDfileClose(fid) < 0) {
MESSAGE("ERROR : close file ...");
ret=-1;
}
return ret;
}