Debian Codegraph @

tests/usecases/python/UsesCase_MEDmesh_5.py

raw
1 #! /usr/bin/env python
2 # -*- coding:utf-8 -*-
3 # /* This file is part of MED.
4 # *
5 # * COPYRIGHT (C) 1999 - 2020 EDF R&D, CEA/DEN
6 # * MED is free software: you can redistribute it and/or modify
7 # * it under the terms of the GNU Lesser General Public License as published by
8 # * the Free Software Foundation, either version 3 of the License, or
9 # * (at your option) any later version.
10 # *
11 # * MED is distributed in the hope that it will be useful,
12 # * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # * GNU Lesser General Public License for more details.
15 # *
16 # * You should have received a copy of the GNU Lesser General Public License
17 # * along with MED. If not, see <http://www.gnu.org/licenses/>.
18 # */
19
20 # /******************************************************************************
21 # *
22 # * Mesh Use case 5 : read a 2D structured mesh
23 # * 5x3 cartesian grid
24 # *
25 # *****************************************************************************/
26
27 import sys
28
29 import med
30
31 def doCleanup(fid, status) :
32 try :
33 med.MEDfileClose(fid)
34 except RuntimeError as ex :
35 print("ERROR : close file ...\n%s" % ex)
36 status = -1
37 finally :
38 if status != 0 :
39 sys.exit(status)
40
41 # max size of meshname : med.MED_NAME_SIZE+1
42 meshname = "2D structured mesh"
43
44 # open MED file
45 try :
46 fid = med.MEDfileOpen("UsesCase_MEDmesh_4.med", med.MED_ACC_RDONLY)
47 except RuntimeError as ex :
48 print("ERROR : open file in READ ONLY ACCESS mode ...\n%s" % ex)
49 sys.exit(-1)
50
51 # read mesh informations : mesh dimension, space dimension ...
52 try :
53 meshinfo = med.MEDmeshInfoByName(fid, meshname)
54 except RuntimeError as ex :
55 print("ERROR : mesh info ...\n%s" % ex)
56 doCleanup(fid, -1)
57
58 if (not meshinfo) or (len(meshinfo) != 10) :
59 print("ERROR : no mesh info created from MEDmeshInfoByName")
60 doCleanup(fid, -1)
61 else :
62 spacedim,meshdim,meshtype,description,dtunit,sortingtype,nstep,axistype,axisname,axisunit=meshinfo
63
64 # read the grid type : MED_CARTESIAN_GRID or MED_CURVILINEAR_GRID
65 try :
66 gridtype = med.MEDmeshGridTypeRd(fid, meshname)
67 except RuntimeError as ex :
68 print("ERROR : read grid type ...\n%s" % ex)
69 doCleanup(fid, -1)
70
71 # ... we know that the mesh is a cartesian grid,
72 # a real code would check ...
73
74 # read the axis coordinates (MED_CARTESIAN coordinates system)
75 # X
76 axis = 1
77 try :
78 size, chgt, trsf = med.MEDmeshnEntity(fid, meshname, med.MED_NO_DT,
79 med.MED_NO_IT, med.MED_NODE, med.MED_NONE,
80 med.MED_COORDINATE_AXIS1, med.MED_NO_CMODE)
81 except RuntimeError as ex :
82 print("ERROR : number of coordinates on X axis ...\n%s" % ex)
83 doCleanup(fid, -1)
84 ncell = size - 1
85 cooXaxis = med.MEDFLOAT([0.] * size)
86
87 try :
88 med.MEDmeshGridIndexCoordinateRd(fid, meshname,
89 med.MED_NO_DT, med.MED_NO_IT, axis, cooXaxis)
90 except RuntimeError as ex :
91 print("ERROR : read axis X coordinates ...\n%s" % ex)
92 doCleanup(fid, -1)
93
94 # Y
95 axis = 2
96 try :
97 size2, chgt2, trsf2 = med.MEDmeshnEntity(fid, meshname, med.MED_NO_DT,
98 med.MED_NO_IT, med.MED_NODE, med.MED_NONE,
99 med.MED_COORDINATE_AXIS2, med.MED_NO_CMODE)
100 except RuntimeError as ex :
101 print("ERROR : number of coordinates on Y axis ...\n%s" % ex)
102 doCleanup(fid, -1)
103 ncell = ncell * (size2 - 1)
104 cooYaxis = med.MEDFLOAT([0.] * size2)
105
106 try :
107 med.MEDmeshGridIndexCoordinateRd(fid, meshname,
108 med.MED_NO_DT, med.MED_NO_IT, axis, cooYaxis)
109 except RuntimeError as ex :
110 print("ERROR : read axis Y coordinates ...\n%s" % ex)
111 doCleanup(fid, -1)
112
113 # read cells name
114
115 cellsname = med.MEDCHAR(["\0"] * (ncell * med.MED_SNAME_SIZE + 1))
116 try :
117 med.MEDmeshEntityNameRd(fid, meshname, med.MED_NO_DT, med.MED_NO_IT,
118 med.MED_CELL, med.MED_QUAD4, cellsname)
119 except RuntimeError as ex :
120 print("ERROR : read cells name ...\n%s" % ex)
121 doCleanup(fid, -1)
122
123 # close MED file
124 doCleanup(fid, 0)