MED fichier
usecases/c/UsesCase_MEDmesh_11.c
/* This file is part of MED.
*
* COPYRIGHT (C) 1999 - 2023 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/>.
*/
/*
* Use case 11 : read a 2D unstructured mesh with 15 nodes, 8 triangular cells, 4 quadragular cells with
* nodes families
*/
#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";
char meshdescription[MED_COMMENT_SIZE+1]="";
med_int meshdim;
med_int spacedim;
med_sorting_type sortingtype;
med_int nstep;
med_mesh_type meshtype;
med_axis_type axistype;
char axisname[2*MED_SNAME_SIZE+1]="";
char unitname[2*MED_SNAME_SIZE+1]="";
char dtunit[MED_SNAME_SIZE+1];
med_float *coordinates = NULL;
med_int nnodes = 0;
med_int *triaconnectivity = NULL;
med_int ntria3 = 0;
med_int *quadconnectivity = NULL;
med_int nquad4 = 0;
med_bool coordinatechangement;
med_bool geotransformation;
int i;
med_int nfamily, ngroup;
med_int familynumber;
char *groupname=NULL;
char familyname[MED_NAME_SIZE+1]="";
med_int *familynumbers = NULL;
med_int nfamilynumber = 0;
int ret=-1;
/* open MED file with READ ONLY access mode */
fid = MEDfileOpen("UsesCase_MEDmesh_10.med",MED_ACC_RDONLY);
if (fid < 0) {
MESSAGE("ERROR : open file in READ ONLY ACCESS mode ...");
goto ERROR;
}
/*
* ... we know that the MED file has only one mesh,
* a real code would check ...
*/
/* read mesh informations : mesh dimension, space dimension ... */
if (MEDmeshInfoByName(fid, meshname, &spacedim, &meshdim, &meshtype, meshdescription,
dtunit, &sortingtype, &nstep, &axistype, axisname, unitname) < 0) {
MESSAGE("ERROR : mesh info ...");
goto ERROR;
}
/* read how many nodes in the mesh */
if ((nnodes = MEDmeshnEntity(fid, meshname, MED_NO_DT, MED_NO_IT, MED_NODE, MED_NO_GEOTYPE,
MED_COORDINATE, MED_NO_CMODE,&coordinatechangement,
&geotransformation)) < 0) {
MESSAGE("ERROR : number of nodes ...");
goto ERROR;
}
/*
* ... we know that we only have MED_TRIA3 and MED_QUAD4 in the mesh,
* a real code would check all MED geometry cell types ...
*/
/* read how many triangular cells in the mesh */
if ((ntria3 = MEDmeshnEntity(fid, meshname, MED_NO_DT, MED_NO_IT, MED_CELL,MED_TRIA3,
MED_CONNECTIVITY, MED_NODAL,&coordinatechangement,
&geotransformation)) < 0) {
MESSAGE("ERROR : number of MED_TRIA3 ...");
goto ERROR;
}
/* read how many quadrangular cells in the mesh */
if ((nquad4 = MEDmeshnEntity(fid, meshname, MED_NO_DT, MED_NO_IT, MED_CELL,MED_QUAD4,
MED_CONNECTIVITY, MED_NODAL, &coordinatechangement,
&geotransformation)) < 0) {
MESSAGE("ERROR : number of MED_QUAD4 ...");
goto ERROR;
}
/* read mesh nodes coordinates */
if ((coordinates = (med_float*) malloc(sizeof(med_float)*nnodes*spacedim)) == NULL) {
MESSAGE("ERROR : memory allocation ...");
goto ERROR;
}
coordinates) < 0) {
MESSAGE("ERROR : nodes coordinates ...");
free(coordinates);
goto ERROR;
}
free(coordinates);
/* read cells connectivity in the mesh */
if ((triaconnectivity = (med_int *) malloc(sizeof(med_int)*ntria3*3)) == NULL) {
MESSAGE("ERROR : memory allocation ...");
goto ERROR;
}
MED_TRIA3, MED_NODAL, MED_FULL_INTERLACE, triaconnectivity) < 0) {
MESSAGE("ERROR : MED_TRIA3 connectivity ...");
free(triaconnectivity);
goto ERROR;
}
free(triaconnectivity);
if ((quadconnectivity = (med_int *) malloc(sizeof(med_int)*nquad4*4)) == NULL) {
MESSAGE("ERROR : memory allocation ...");
goto ERROR;
}
MED_QUAD4, MED_NODAL, MED_FULL_INTERLACE, quadconnectivity) < 0) {
MESSAGE("ERROR : MED_QUAD4 connectivity ...");
free(quadconnectivity);
goto ERROR;
}
free(quadconnectivity);
/*
* read families of entities...
*/
if ((nfamily = MEDnFamily(fid,meshname)) < 0) {
MESSAGE("ERROR : read number of family ...");
goto ERROR;
}
for (i=0; i<nfamily ; i++) {
if ((ngroup = MEDnFamilyGroup(fid, meshname, i+1)) < 0) {
MESSAGE("ERROR : read number of group in a family ...");
goto ERROR;
}
if (ngroup > 0) {
if ((groupname = (char*) malloc(sizeof(char)*MED_LNAME_SIZE*ngroup+1)) == NULL) {
MESSAGE("ERROR : memory allocation ...");
goto ERROR;
}
if (MEDfamilyInfo(fid, meshname, i+1, familyname, &familynumber, groupname) < 0) {
MESSAGE("ERROR : family info ...");
free(groupname);
goto ERROR;
}
free(groupname);
}
}
/* check for family numbers */
/* By convention, if there is no numbers in the file, it means that 0 is the family
number of all nodes */
if ((nfamilynumber = MEDmeshnEntity(fid, meshname, MED_NO_DT, MED_NO_IT,
&coordinatechangement, &geotransformation)) < 0) {
MESSAGE("ERROR : check family numbers nodes ...");
goto ERROR;
}
if ((familynumbers = (med_int *) malloc(sizeof(med_int)*nnodes)) == NULL) {
MESSAGE("ERROR : memory allocation ...");
goto ERROR;
}
if (nfamilynumber > 0) {
/* read family numbers for nodes */
MED_NODE, MED_NONE, familynumbers) < 0) {
MESSAGE("ERROR : read family numbers nodes ...");
goto ERROR;
}
} else
for (i=0; i<nnodes; i++) *(familynumbers+i) = 0;
for (i=0; i<nnodes; i++) {
printf(IFORMAT, *(familynumbers+i));
if (i+1 != nnodes)
printf(" - ");
else
printf("\n");
}
if (familynumbers)
free(familynumbers);
/* read family numbers for cells */
if ((nfamilynumber = MEDmeshnEntity(fid, meshname, MED_NO_DT, MED_NO_IT,
&coordinatechangement, &geotransformation)) < 0) {
MESSAGE("ERROR : check family number tria3 ...")
goto ERROR;
}
if ((familynumbers = (med_int *) malloc(sizeof(med_int)*ntria3)) == NULL) {
MESSAGE("ERROR : memory allocation ...");
goto ERROR;
}
if (nfamilynumber > 0) {
MED_CELL, MED_TRIA3, familynumbers) < 0) {
MESSAGE("ERROR : read family numbers tria3 ...");
}
} else
for (i=0; i<ntria3; i++) *(familynumbers+i) = 0;
free (familynumbers);
if ((nfamilynumber = MEDmeshnEntity(fid, meshname, MED_NO_DT, MED_NO_IT,
&coordinatechangement, &geotransformation)) < 0) {
MESSAGE("ERROR : check family number quad4 ...");
goto ERROR;
}
if ((familynumbers = (med_int *) malloc(sizeof(med_int)*nquad4)) == NULL) {
MESSAGE("ERROR : memory allocation ...");
goto ERROR;
}
if (nfamilynumber > 0) {
MED_CELL, MED_QUAD4, familynumbers) < 0) {
MESSAGE("ERROR : read family numbers quad4 ...");
goto ERROR;
}
} else
for (i=0; i<nquad4; i++) *(familynumbers+i) = 0;
free (familynumbers);
ret=0;
ERROR:
/* close MED file */
if (MEDfileClose(fid) < 0) {
MESSAGE("ERROR : close file");
ret= -1;
}
return ret;
}
MED_TRIA3
#define MED_TRIA3
Definition: med.h:205
MED_ACC_RDONLY
Definition: med.h:122
MED_COMMENT_SIZE
#define MED_COMMENT_SIZE
Definition: med.h:81
MED_SNAME_SIZE
#define MED_SNAME_SIZE
Definition: med.h:84
med_idt
hid_t med_idt
Definition: med.h:333
MED_NO_GEOTYPE
#define MED_NO_GEOTYPE
Definition: med.h:234
med_sorting_type
med_sorting_type
Definition: med.h:311
MED_FAMILY_NUMBER
Definition: med.h:151
MESSAGE
#define MESSAGE(chaine)
Definition: med_utils.h:324
MED_FULL_INTERLACE
Definition: med.h:98
MEDmeshInfoByName
MEDC_EXPORT med_err MEDmeshInfoByName(const med_idt fid, const char *const meshname, med_int *const spacedim, med_int *const meshdim, med_mesh_type *const meshtype, char *const description, char *const dtunit, med_sorting_type *const sortingtype, med_int *const nstep, med_axis_type *const axistype, char *const axisname, char *const axisunit)
Cette routine permet de lire les informations relatives à un maillage en précisant son nom.
Definition: MEDmeshInfoByName.c:42
med_int
int med_int
Definition: med.h:344
med_bool
med_bool
Definition: med.h:262
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
med_float
double med_float
Definition: med.h:338
MED_COORDINATE
Definition: med.h:151
MED_NO_CMODE
Definition: med.h:257
IFORMAT
#define IFORMAT
Definition: med_utils.h:145
MED_NO_DT
#define MED_NO_DT
Definition: med.h:322
MED_NONE
#define MED_NONE
Definition: med.h:233
MEDfileClose
MEDC_EXPORT med_err MEDfileClose(med_idt fid)
Fermeture d'un fichier MED.
Definition: MEDfileClose.c:30
MEDfamilyInfo
MEDC_EXPORT med_err MEDfamilyInfo(const med_idt fid, const char *const meshname, const int famit, char *const familyname, med_int *const familynumber, char *const groupname)
Cette routine permet de lire les informations relatives à une famille d'un maillage.
Definition: MEDfamilyInfo.c:39
med_mesh_type
med_mesh_type
Definition: med.h:133
MED_CELL
Definition: med.h:145
MED_LNAME_SIZE
#define MED_LNAME_SIZE
Definition: med.h:85
MEDmeshEntityFamilyNumberRd
MEDC_EXPORT med_err MEDmeshEntityFamilyNumberRd(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, med_int *const number)
Cette routine permet la lecture des numéros de famille d'un type d'entité d'un maillage.
Definition: MEDmeshEntityFamilyNumberRd.c:39
MED_NAME_SIZE
#define MED_NAME_SIZE
Definition: med.h:83
MEDnFamilyGroup
MEDC_EXPORT med_int MEDnFamilyGroup(const med_idt fid, const char *const meshname, const int famit)
Cette routine permet de lire le nombre de groupe dans une famille.
Definition: MEDnFamilyGroup.c:36
med_utils.h
MED_NODE
Definition: med.h:145
med_axis_type
med_axis_type
Definition: med.h:260
med.h
MED_CONNECTIVITY
Definition: med.h:151
MED_NO_IT
#define MED_NO_IT
Definition: med.h:323
MEDfileOpen
MEDC_EXPORT med_idt MEDfileOpen(const char *const filename, const med_access_mode accessmode)
Ouverture d'un fichier MED.
Definition: MEDfileOpen.c:42
MEDnFamily
MEDC_EXPORT med_int MEDnFamily(const med_idt fid, const char *const meshname)
Cette routine permet de lire le nombre de famille dans un maillage.
Definition: MEDnFamily.c:35
MEDmeshNodeCoordinateRd
MEDC_EXPORT med_err MEDmeshNodeCoordinateRd(const med_idt fid, const char *const meshname, const med_int numdt, const med_int numit, const med_switch_mode switchmode, med_float *const coordinates)
Cette routine permet de lire dans un maillage le tableau des coordonnées des noeuds,...
Definition: MEDmeshNodeCoordinateRd.c:37
main
int main(int argc, char **argv)
Definition: 3.0.8/test10.c:50
MED_QUAD4
#define MED_QUAD4
Definition: med.h:206
MED_NODAL
Definition: med.h:257
MEDmeshElementConnectivityRd
MEDC_EXPORT med_err MEDmeshElementConnectivityRd(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_connectivity_mode cmode, const med_switch_mode switchmode, med_int *const connectivity)
Cette routine permet de lire dans un maillage le tableau des connectivités pour un type géométrique d...
Definition: MEDmeshElementConnectivityRd.c:40