Debian Codegraph @

tests/usecases/c/UsesCase_MEDmesh_5.c

raw symbols SCIP
1 /* This file is part of MED.
2 *
3 * COPYRIGHT (C) 1999 - 2020 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 * Mesh Use case 5 : read a 2D structured mesh
20 * 5x3 cartesian grid
21 *
22 */
23
24 #include <med.h>
25 #define MESGERR 1
26 #include <med_utils.h>
27
28 #include <string.h>
29
30 int main (int argc, char **argv) {
31 med_idt fid;
32 const char meshname[MED_NAME_SIZE+1]="2D structured mesh";
33 med_int spacedim;
34 med_int meshdim;
35 char meshdescription[MED_COMMENT_SIZE+1];
36 char axisname[2*MED_SNAME_SIZE+1];
37 char unitname[2*MED_SNAME_SIZE+1];
38 char dtunit[MED_SNAME_SIZE+1];
39 med_mesh_type meshtype;
40 med_axis_type axistype;
41 med_grid_type gridtype;
42 med_int axis, size ;
43 med_float *cooXaxis = NULL;
44 med_float *cooYaxis = NULL;
45 med_bool coordinatechangement;
46 med_bool geotransformation;
47 med_int nstep;
48 med_sorting_type sortingtype;
49 int j;
50 int ret=-1;
51 int ncell=0;
52 char *cellsname=NULL;
53
54 /* open MED file */
55 fid = MEDfileOpen("UsesCase_MEDmesh_4.med",MED_ACC_RDONLY);
56 if (fid < 0) {
57 MESSAGE("ERROR : open file ...");
58 goto ERROR;
59 }
60
61 /* read mesh informations : meshname, mesh dimension, space dimension ... */
62 if (MEDmeshInfoByName(fid, meshname, &spacedim, &meshdim, &meshtype, meshdescription,
63 dtunit, &sortingtype, &nstep, &axistype, axisname, unitname) < 0) {
64 MESSAGE("ERROR : mesh info ...");
65 goto ERROR;
66 }
67
68 /* read the grid type : MED_CARTESIAN_GRID or MED_CURVILINEAR_GRID */
69 if (MEDmeshGridTypeRd(fid, meshname, &gridtype) < 0) {
70 MESSAGE("ERROR : read grid type ...");
71 }
72
73 /*
74 * ... we know that the mesh is a cartesian grid,
75 * a real code would check ...
76 */
77
78 /* read the axis coordinates (MED_CARTESIAN coordinates system */
79 /* X */
80 axis = 1;
81 if ((size = MEDmeshnEntity(fid, meshname, MED_NO_DT, MED_NO_IT,
82 MED_NODE, MED_NONE, MED_COORDINATE_AXIS1, MED_NO_CMODE,
83 &coordinatechangement, &geotransformation)) < 0) {
84 MESSAGE("ERROR : number of coordinates on X axis ...");
85 goto ERROR;
86 }
87 ncell = size-1;
88
89 if ((cooXaxis = (med_float *) malloc(sizeof(med_float)*size)) == NULL) {
90 MESSAGE("ERROR : memory allocation ...");
91 goto ERROR;
92 }
93 if (MEDmeshGridIndexCoordinateRd(fid, meshname, MED_NO_DT, MED_NO_IT,
94 axis, cooXaxis) < 0) {
95 MESSAGE("ERROR : read axis X coordinates ...");
96 free(cooXaxis);
97 goto ERROR;
98 }
99
100 free(cooXaxis);
101
102 /* Y */
103 axis = 2;
104 if ((size = MEDmeshnEntity(fid, meshname, MED_NO_DT, MED_NO_IT,
105 MED_NODE, MED_NONE, MED_COORDINATE_AXIS2, MED_NO_CMODE,
106 &coordinatechangement, &geotransformation)) < 0) {
107 MESSAGE("ERROR : number of coordinates on Y axis ...");
108 goto ERROR;
109 }
110 ncell = ncell * (size-1);
111
112 if ((cooYaxis = (med_float *) malloc(sizeof(med_float)*size)) == NULL) {
113 MESSAGE("ERROR : memory allocation ...");
114 goto ERROR;
115 }
116 if (MEDmeshGridIndexCoordinateRd(fid, meshname, MED_NO_DT, MED_NO_IT,
117 axis, cooYaxis) < 0) {
118 MESSAGE("ERROR : read axis Y coordinates ...");
119 free(cooYaxis);
120 goto ERROR;
121 }
122
123 free(cooYaxis);
124
125 /* read cells name */
126
127 cellsname = (char *) malloc((sizeof(char))*ncell*MED_SNAME_SIZE+1);
128 if (MEDmeshEntityNameRd(fid, meshname, MED_NO_DT, MED_NO_IT,
129 MED_CELL, MED_QUAD4, cellsname) < 0) {
130 MESSAGE("ERROR : read cells name ...");
131 free(cellsname);
132 goto ERROR;
133 }
134 free(cellsname);
135
136 ret=0;
137 ERROR:
138
139 /* close MED file */
140 if (MEDfileClose(fid) < 0) {
141 MESSAGE("ERROR : close file ...");
142 ret = -1;
143 }
144
145 return ret;
146 }