Debian Codegraph @

tests/usecases/python/UsesCase_MEDmesh_8.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 8 : read a 2D unstructured mesh with coordinates nodes
23 # * modifications (generic approach)
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
42 # open MED file with READ ONLY access mode
43 try :
44 fid = med.MEDfileOpen("UsesCase_MEDmesh_6.med", med.MED_ACC_RDONLY)
45 except RuntimeError as ex :
46 print("ERROR : open file in READ ONLY ACCESS mode ...\n%s" % ex)
47 sys.exit(-1)
48
49 # read how many mesh in the file
50 try :
51 nmesh = med.MEDnMesh(fid)
52 except RuntimeError as ex :
53 print("ERROR : read how many mesh ...\n%s" % ex)
54 doCleanup(fid, -1)
55
56 for i in range(0, nmesh) :
57
58 # read computation space dimension
59 try :
60 spacedim = med.MEDmeshnAxis(fid, i+1)
61 except RuntimeError as ex :
62 print("ERROR : read computation space dimension ...\n%s" % ex)
63 doCleanup(fid, -1)
64
65 # read mesh informations : meshname, mesh dimension, mesh type ...
66 try :
67 meshinfo = med.MEDmeshInfo(fid, i+1)
68 except RuntimeError as ex :
69 print("ERROR : mesh info ...\n%s" % ex)
70 doCleanup(fid, -1)
71
72 if (not meshinfo) or (len(meshinfo) != 11) :
73 print("ERROR : no mesh info created from MEDmeshInfo")
74 doCleanup(fid, -1)
75 else :
76 meshname,spacedim,meshdim,meshtype,description,dtunit,sortingtype,nstep,axistype,axisname,axisunit=meshinfo
77
78 # read how many nodes in the mesh
79 try :
80 nnodes, chgt, trsf = med.MEDmeshnEntity(fid, meshname, med.MED_NO_DT,
81 med.MED_NO_IT, med.MED_NODE, med.MED_NONE,
82 med.MED_COORDINATE, med.MED_NO_CMODE)
83 except RuntimeError as ex :
84 print("ERROR : number of nodes initial ...\n%s" % ex)
85 doCleanup(fid, -1)
86
87 # read mesh nodes coordinates
88 coordinates = med.MEDFLOAT([0.] * nnodes * spacedim)
89 try :
90 med.MEDmeshNodeCoordinateRd(fid, meshname, med.MED_NO_DT, med.MED_NO_IT,
91 med.MED_FULL_INTERLACE, coordinates)
92 except RuntimeError as ex :
93 print("ERROR : nodes coordinates ...\n%s" % ex)
94 doCleanup(fid, -1)
95
96 # read all MED geometry cell types
97 for it in range(1, med.MED_N_CELL_FIXED_GEO + 1) :
98
99 geotype = geotypes[it - 1][0]
100
101 # how many cells
102 try :
103 ngeo, chgt, trsf = med.MEDmeshnEntity(fid, meshname, med.MED_NO_DT,
104 med.MED_NO_IT, med.MED_CELL, geotype,
105 med.MED_CONNECTIVITY, med.MED_NODAL)
106 except RuntimeError as ex :
107 print("ERROR : number of cell ...\n%s" % ex)
108 doCleanup(fid, -1)
109
110 if ngeo :
111 # read cells connectivity in the mesh
112 connectivity = med.MEDINT([0] * ngeo * (geotype % 100))
113 try :
114 med.MEDmeshElementConnectivityRd(fid, meshname, med.MED_NO_DT,
115 med.MED_NO_IT, med.MED_CELL, geotype,
116 med.MED_NODAL, med.MED_FULL_INTERLACE, connectivity)
117 except RuntimeError as ex :
118 print("ERROR : cell connectivity ...\n%s" % ex)
119 doCleanup(fid, -1)
120
121 # read nodes coordinates changements step by step
122 for it in range(1, nstep) :
123
124 try :
125 numdt, numit, dt = med.MEDmeshComputationStepInfo(fid, meshname, it+1)
126 except RuntimeError as ex :
127 print("ERROR : Computing step info ...\n%s" % ex)
128 doCleanup(fid, -1)
129
130 # test changement : for nodes coordinates
131 try :
132 entityinfo = med.MEDmeshnEntityWithProfile(fid, meshname, numdt,
133 numit, med.MED_NODE, med.MED_NONE, med.MED_COORDINATE,
134 med.MED_NO_CMODE, med.MED_GLOBAL_STMODE)
135 except RuntimeError as ex :
136 print("ERROR : nodes coordinates test changement ...\n%s" % ex)
137 doCleanup(fid, -1)
138
139 if (not entityinfo) or (len(entityinfo) != 5) :
140 print("ERROR : nodes coordinates test changement return ...")
141 doCleanup(fid, -1)
142 else :
143 nnodes,profilename,profilesize,chgt,trsf = entityinfo
144
145 # if only coordinates have changed, then read the new coordinates
146 # to verify if there is a matrix transformation => UsesCase_MEDmesh12.
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 # close MED file
157 doCleanup(fid, 0)