MED fichier

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 - 2021 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 - 2021 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 - 2021 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 - 2021 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 - 2021 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 - 2021 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;
}

MEDfieldCr
MEDC_EXPORT med_err MEDfieldCr(const med_idt fid, const char *const fieldname, const med_field_type fieldtype, const med_int ncomponent, const char *const componentname, const char *const componentunit, const char *const dtunit, const char *const meshname)
Cette fonction crée un champ dans un fichier.
Definition: MEDfieldCr.c:44
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_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_ACC_RDONLY
Definition: med.h:120
MED_CELL
Definition: med.h:143
MESSAGE
#define MESSAGE(chaine)
Definition: med_utils.h:324
MEDlinkWr
MEDC_EXPORT med_err MEDlinkWr(const med_idt fid, const char *const meshname, const char *const link)
Cette routine permet d'écrire un lien dans un fichier MED.
Definition: MEDlinkWr.c:36
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_GET_CELL_GEOMETRY_TYPE
med_geometry_type MED_GET_CELL_GEOMETRY_TYPE[MED_N_CELL_FIXED_GEO+2]
Definition: MEDiterators.c:55
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
MEDfieldComputingStepMeshInfo
MEDC_EXPORT med_err MEDfieldComputingStepMeshInfo(const med_idt fid, const char *const fieldname, const int csit, med_int *const numdt, med_int *const numit, med_float *const dt, med_int *const meshnumdt, med_int *const meshnumit)
Cette fonction permet de lire les informations caractérisant une étape de calcul : numéro de pas de t...
Definition: MEDfieldComputingStepMeshInfo.c:39
MED_N_CELL_FIXED_GEO
#define MED_N_CELL_FIXED_GEO
Definition: med.h:239
MEDfieldComputingStepMeshWr
MEDC_EXPORT med_err MEDfieldComputingStepMeshWr(const med_idt fid, const char *const fieldname, const med_int numdt, const med_int numit, const med_int meshnumdt, const med_int meshnumit)
Cette fonction permet de définir l'étape de calcul ( meshnumdit , meshnumit ) à utiliser pour le mail...
Definition: MEDfieldComputingStepMeshWr.c:36
MED_ACC_CREAT
Definition: med.h:123
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
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
MED_FLOAT64
Definition: med.h:166
MED_NO_IT
#define MED_NO_IT
Definition: med.h:321
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
main
int main(int argc, char **argv)
Definition: 3.0.8/test10.c:50
med_utils.h
MEDfieldValueWr
MEDC_EXPORT med_err MEDfieldValueWr(const med_idt fid, const char *const fieldname, const med_int numdt, const med_int numit, const med_float dt, const med_entity_type entitype, const med_geometry_type geotype, const med_switch_mode switchmode, const med_int componentselect, const med_int nentity, const unsigned char *const value)
Cette fonction permet d'écrire les valeurs d'un champ définies sur des entités d'un maillage pour une...
Definition: MEDfieldValueWr.c:44
MED_QUAD4
#define MED_QUAD4
Definition: med.h:204