1 // Copyright (C) 2010-2016 CEA/DEN, EDF R&D
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 // Lesser General Public License for more details.
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 * How to create an unstructured mesh with polygons
23 * Use case 16 : read a 2D unstructured mesh with 2 polyhedrons
28 #include <med_utils.h>
32 int main (int argc, char **argv) {
34 const char meshname[MED_NAME_SIZE+1] = "3D unstructured mesh";
35 char meshdescription[MED_COMMENT_SIZE+1];
38 med_sorting_type sortingtype;
40 med_mesh_type meshtype;
41 med_axis_type axistype;
42 char axisname[3*MED_SNAME_SIZE+1];
43 char unitname[3*MED_SNAME_SIZE+1];
44 char dtunit[MED_SNAME_SIZE+1];
45 med_float *coordinates = NULL;
49 med_int faceIndexSize;
50 med_int *index = NULL;
51 med_int *faceindex = NULL;
52 med_int *connectivity = NULL;
53 med_int connectivitysize;
54 med_bool coordinatechangement;
55 med_bool geotransformation;
60 /* open MED file with READ ONLY access mode */
61 fid = MEDfileOpen("./UsesCase_MEDmesh_15.med",MED_ACC_RDONLY);
63 MESSAGE("ERROR : open file in READ ONLY ACCESS mode ...");
68 * ... we know that the MED file has only one mesh,
69 * a real code working would check ...
72 /* read mesh informations : mesh dimension, space dimension ... */
73 if (MEDmeshInfoByName(fid, meshname, &spacedim, &meshdim, &meshtype, meshdescription,
74 dtunit, &sortingtype, &nstep, &axistype, axisname, unitname) < 0) {
75 MESSAGE("ERROR : mesh info ...");
79 /* read how many nodes in the mesh */
80 if ((nnodes = MEDmeshnEntity(fid, meshname, MED_NO_DT, MED_NO_IT, MED_NODE, MED_POINT1,
81 MED_COORDINATE, MED_NO_CMODE,&coordinatechangement,
82 &geotransformation)) < 0) {
83 MESSAGE("ERROR : number of nodes ...");
88 * ... we know that we only have MED_POLYHEDRON cells in the mesh,
89 * a real code working would check all MED geometry cell types ...
92 /* How many polygon in the mesh in nodal connectivity mode */
93 /* For the polygons, we get the size of array index */
94 if ((indexsize = MEDmeshnEntity(fid,meshname,MED_NO_DT,MED_NO_IT,
95 MED_CELL,MED_POLYHEDRON,MED_INDEX_FACE,MED_NODAL,
96 &coordinatechangement,
97 &geotransformation)) < 0) {
98 MESSAGE("ERROR : read number of polyedron ...");
104 if ((faceIndexSize = MEDmeshnEntity(fid,meshname,MED_NO_DT,MED_NO_IT,
105 MED_CELL,MED_POLYHEDRON,MED_INDEX_NODE,MED_NODAL,
106 &coordinatechangement,
107 &geotransformation)) < 0) {
108 MESSAGE("ERROR : read number of polyedron ...");
111 ISCRUTE(faceIndexSize);
113 /* how many nodes for the polyhedron connectivity ? */
114 if ((connectivitysize = MEDmeshnEntity(fid,meshname,MED_NO_DT,MED_NO_IT,
115 MED_CELL,MED_POLYHEDRON,MED_CONNECTIVITY,MED_NODAL,
116 &coordinatechangement,
117 &geotransformation)) < 0) {
118 MESSAGE("ERROR : read connevity size ...");
121 ISCRUTE(connectivitysize);
123 /* read mesh nodes coordinates */
124 if ((coordinates = (med_float*) malloc(sizeof(med_float)*nnodes*spacedim)) == NULL) {
125 MESSAGE("ERROR : memory allocation ...");
129 if (MEDmeshNodeCoordinateRd(fid, meshname, MED_NO_DT, MED_NO_IT, MED_FULL_INTERLACE,
131 MESSAGE("ERROR : nodes coordinates ...");
134 for (i=0;i<nnodes*spacedim;i++)
135 printf("%f - ",*(coordinates+i));
138 /* read polygons connectivity */
139 index = (med_int *) malloc(sizeof(med_int)*indexsize);
140 faceindex = (med_int *) malloc(sizeof(med_int)*faceIndexSize);
141 connectivity = (med_int *) malloc(sizeof(med_int)*connectivitysize);
143 if (MEDmeshPolyhedronRd(fid,meshname,MED_NO_DT,MED_NO_IT,MED_CELL,MED_NODAL,
144 index,faceindex,connectivity) < 0) {
145 MESSAGE("ERROR : read polygon connectivity ...");
149 for (i=0;i<npoly;i++)
151 printf(">> MED_POLYHEDRON "IFORMAT" : \n",i+1);
152 printf("---- Face Index ----- : [ ");
154 ind2 = *(index+i+1)-1;
155 for (k=ind1;k<ind2;k++)
156 printf(IFORMAT" ",*(faceindex+k));
158 printf("---- Connectivity ----- : [ ");
159 for (k=ind1;k<ind2;k++)
161 jind1 = *(faceindex+k)-1;
162 jind2 = *(faceindex+k+1)-1;
163 for (j=jind1;j<jind2;j++)
164 printf(IFORMAT" ",*(connectivity+j));
171 * ... we know that the family number of nodes and elements is 0, a real working would check ...
175 if (MEDfileClose(fid) < 0) {
176 MESSAGE("ERROR : close file");
180 /* memory deallocation */