Debian Codegraph @

tests/usecases/python/UsesCase_MEDmesh_12.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 # * Use case 12 : read a 2D unstructured mesh with moving grid (generic approach)
23 # *
24 # *****************************************************************************/
25
26 import sys
27
28 import med
29
30 def doCleanup(fid, status) :
31 try :
32 med.MEDfileClose(fid)
33 except RuntimeError as ex :
34 print("ERROR : close file ...\n%s" % ex)
35 status = -1
36 finally :
37 if status != 0 :
38 sys.exit(status)
39
40 geotypes = med.MED_GET_CELL_GEOMETRY_TYPE
41 matrix = med.MEDFLOAT([0.] * 7)
42
43 # open MED file with READ ONLY access mode
44 try :
45 fid = med.MEDfileOpen("UsesCase_MEDmesh_9.med", med.MED_ACC_RDONLY)
46 except RuntimeError as ex :
47 print("ERROR : open file in READ ONLY ACCESS mode ...\n%s" % ex)
48 sys.exit(-1)
49
50 # read how many mesh in the file
51 try :
52 nmesh = med.MEDnMesh(fid)
53 except RuntimeError as ex :
54 print("ERROR : read how many mesh ...\n%s" % ex)
55 doCleanup(fid, -1)
56
57 for i in range(0, nmesh) :
58
59 # read computation space dimension
60 try :
61 spacedim = med.MEDmeshnAxis(fid, i+1)
62 except RuntimeError as ex :
63 print("ERROR : read computation space dimension ...\n%s" % ex)
64 doCleanup(fid, -1)
65
66 # read mesh informations : meshname, mesh dimension, mesh type ...
67 try :
68 meshinfo = med.MEDmeshInfo(fid, i+1)
69 except RuntimeError as ex :
70 print("ERROR : mesh info ...\n%s" % ex)
71 doCleanup(fid, -1)
72
73 if (not meshinfo) or (len(meshinfo) != 11) :
74 print("ERROR : no mesh info created from MEDmeshInfo")
75 doCleanup(fid, -1)
76 else :
77 meshname,spacedim,meshdim,meshtype,description,dtunit,sortingtype,nstep,axistype,axisname,axisunit=meshinfo
78
79 # read how many nodes in the mesh
80 try :
81 nnodes, chgt, trsf = med.MEDmeshnEntity(fid, meshname, med.MED_NO_DT,
82 med.MED_NO_IT, med.MED_NODE, med.MED_NONE,
83 med.MED_COORDINATE, med.MED_NO_CMODE)
84 except RuntimeError as ex :
85 print("ERROR : number of nodes initial ...\n%s" % ex)
86 doCleanup(fid, -1)
87
88 # read mesh nodes coordinates
89 coordinates = med.MEDFLOAT([0.] * nnodes * spacedim)
90 try :
91 med.MEDmeshNodeCoordinateRd(fid, meshname, med.MED_NO_DT, med.MED_NO_IT,
92 med.MED_FULL_INTERLACE, coordinates)
93 except RuntimeError as ex :
94 print("ERROR : nodes coordinates ...\n%s" % ex)
95 doCleanup(fid, -1)
96
97 # read all MED geometry cell types
98 for it in range(1, med.MED_N_CELL_FIXED_GEO + 1) :
99
100 geotype = geotypes[it - 1][0]
101
102 # how many cells
103 try :
104 ngeo, chgt, trsf = med.MEDmeshnEntity(fid, meshname, med.MED_NO_DT,
105 med.MED_NO_IT, med.MED_CELL, geotype,
106 med.MED_CONNECTIVITY, med.MED_NODAL)
107 except RuntimeError as ex :
108 print("ERROR : number of cell ...\n%s" % ex)
109 doCleanup(fid, -1)
110
111 if ngeo :
112 # read cells connectivity in the mesh
113 connectivity = med.MEDINT([0] * ngeo * (geotype % 100))
114 try :
115 med.MEDmeshElementConnectivityRd(fid, meshname, med.MED_NO_DT,
116 med.MED_NO_IT, med.MED_CELL, geotype,
117 med.MED_NODAL, med.MED_FULL_INTERLACE, connectivity)
118 except RuntimeError as ex :
119 print("ERROR : cell connectivity ...\n%s" % ex)
120 doCleanup(fid, -1)
121
122 # read nodes coordinates changements step by step
123 for it in range(1, nstep) :
124
125 try :
126 numdt, numit, dt = med.MEDmeshComputationStepInfo(fid, meshname, it+1)
127 except RuntimeError as ex :
128 print("ERROR : Computing step info ...\n%s" % ex)
129 doCleanup(fid, -1)
130
131 # test changement : for nodes coordinates
132 try :
133 entityinfo = med.MEDmeshnEntityWithProfile(fid, meshname, numdt,
134 numit, med.MED_NODE, med.MED_NONE, med.MED_COORDINATE,
135 med.MED_NO_CMODE, med.MED_GLOBAL_STMODE)
136 except RuntimeError as ex :
137 print("ERROR : nodes coordinates test changement ...\n%s" % ex)
138 doCleanup(fid, -1)
139
140 if (not entityinfo) or (len(entityinfo) != 5) :
141 print("ERROR : nodes coordinates test changement return ...")
142 doCleanup(fid, -1)
143 else :
144 nnodes,profilename,profilesize,chgt,trsf = entityinfo
145
146 # if only coordinates have changed, then read the new coordinates
147 if chgt and trsf :
148 try :
149 med.MEDmeshNodeCoordinateWithProfileRd(fid, meshname, numdt, numit,
150 med.MED_GLOBAL_STMODE, profilename, med.MED_FULL_INTERLACE,
151 med.MED_ALL_CONSTITUENT, coordinates)
152 except RuntimeError as ex :
153 print("ERROR : nodes coordinates changement ...\n%s" % ex)
154 doCleanup(fid, -1)
155
156 if chgt and not trsf :
157 try :
158 matrixsize, chgt, trsf = med.MEDmeshnEntity(fid,meshname,numdt,
159 numit, med.MED_NODE, med.MED_NONE,
160 med.MED_COORDINATE_TRSF, med.MED_NODAL)
161 except RuntimeError as ex :
162 print("ERROR : read matrix size ...\n%s" % ex)
163 doCleanup(fid, -1)
164 if matrixsize < 0 :
165 print("ERROR : matrix transformation ...")
166 doCleanup(fid, -1)
167
168 if trsf :
169 try :
170 med.MEDmeshNodeCoordinateTrsfRd(fid, meshname, numdt, numit, matrix)
171 except RuntimeError as ex :
172 print("ERROR : read transformation matrix ...\n%s" % ex)
173 doCleanup(fid, -1)
174
175 # close MED file
176 doCleanup(fid, 0)