Debian Codegraph @

tests/usecases/python/UsesCase_MEDmesh_15.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 # * How to create an unstructured mesh with polyhedrons
22 # *
23 # * Use case 15 : a 3D unstructured mesh with 2 polyhedrons
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 = "3D Unstructured Mesh With 2 polyhedrons"
43 spacedim = 3
44 meshdim = 3
45 # max size of axisname and unitname : 3*med.MED_SNAME_SIZE+1
46 # 123456789012345612345678901234561234567890123456
47 axisname = "x y z "
48 unitname = "cm cm cm "
49 coordinates = med.MEDFLOAT([
50 -10.0, -10.0, +0.0, # [ 1]
51 -10.0, -10.0, +10., # [ 2]
52 -10.0, +10.0, +10., # [ 3]
53 -10.0, +10.0, +0.0, # [ 4]
54 +10.0, -10.0, +0.0, # [ 5]
55 +10.0, -10.0, +10., # [ 6]
56 +10.0, +10.0, +10., # [ 7]
57 +10.0, +10.0, +0.0, # [ 8]
58 -10.0, +0.0 , +10.0, # [ 9]
59 -10.0, +0.0 , +0.0 , # [10]
60 +0.0 , -10.0 , +10.0, # [11]
61 +0.0 , -10.0 , +0.0 , # [12]
62 +0.0 , +10.0 , +10.0, # [13]
63 +10.0, +0.0 , +10.0, # [14]
64 +0.0 , +10.0 , +0.0 , # [15]
65 +10.0, +0.0 , +0.0 # [16]
66 ])
67 nnodes = 16
68 # 58 node numbers in the connectivity array
69 connectivity = med.MEDINT([
70 # - Poly 1
71 1, 2 , 9 , 3 , 10, # - Face 1
72 1, 12 , 5 , 6 , 11, 2, # - Face 2
73 2, 11 , 6 , 3 , 9 , # - Face 3
74 3, 6 , 5 , # - Face 4
75 3, 5 , 10 , # - Face 5
76 1, 10 , 5 , 12, # - Face 6
77 # - Poly 2
78 3, 13 , 7 , 8 , 15, 4, # - Face 1
79 3, 4 , 10 , # - Face 2
80 4, 15 , 8 , 16 , 5, 10,# - Face 3
81 3, 6 , 14 , 7 , 13, # - Face 4
82 5, 16 , 8 , 7 , 14, 6, # - Face 5
83 3, 10 , 5 , # - Face 6
84 3, 5 , 6 # - Face 7
85 ])
86
87 # there are two polyhedrons, the first has 6 faces, the second 7 faces
88 # face 7 of Poly 2 uses 59-56=3 nodes
89 nodeindex = med.MEDINT([1, 6, 12, 17, 20, 23,
90 27, 33, 36, 42, 47, 53, 56, 59])
91 # there is a total of 13 faces
92 # nodeindexsize == nbr of faces + 1
93 nodeindexSize = 6+7+1
94
95 # there are two polyhedrons, the first has 6 faces, the second 7 faces
96 # Pn+1 == FaceIndex[n+1]== NodeIndexSize== nbr of faces + 1
97 faceindex = med.MEDINT([1, 7, 14])
98 faceindexSize = 3
99
100 # MED file creation
101 try :
102 fid = med.MEDfileOpen("UsesCase_MEDmesh_15.med", med.MED_ACC_CREAT)
103 except RuntimeError as ex :
104 print("ERROR : file creation ...\n%s" % ex)
105 sys.exit(-1)
106
107 # write a comment in the file
108 try :
109 med.MEDfileCommentWr(fid, "A 3D unstructured mesh : 2 polyhedrons")
110 except RuntimeError as ex :
111 print("ERROR : write file description ...\n%s" % ex)
112 doCleanup(fid, -1)
113
114 # mesh creation : a 3D unstructured mesh
115 try :
116 med.MEDmeshCr(fid, meshname, spacedim, meshdim, med.MED_UNSTRUCTURED_MESH,
117 "A 3D mesh with 2 polyhedron", "", med.MED_SORT_DTIT,
118 med.MED_CARTESIAN, axisname, unitname)
119 except RuntimeError as ex :
120 print("ERROR : mesh creation ...\n%s" % ex)
121 doCleanup(fid, -1)
122
123 # nodes coordinates in a cartesian axis in full interlace mode
124 # (X1,Y1, X2,Y2, X3,Y3, ...) with no iteration and computation step
125 try :
126 med.MEDmeshNodeCoordinateWr(fid, meshname, med.MED_NO_DT, med.MED_NO_IT,
127 med.MED_UNDEF_DT, med.MED_FULL_INTERLACE,
128 nnodes, coordinates)
129 except RuntimeError as ex :
130 print("ERROR : nodes coordinates ...\n%s" % ex)
131 doCleanup(fid, -1)
132
133 # cells connectiviy is defined in nodal mode with no iteration and computation step
134 try :
135 med.MEDmeshPolyhedronWr(fid, meshname, med.MED_NO_DT, med.MED_NO_IT,
136 med.MED_UNDEF_DT, med.MED_CELL, med.MED_NODAL, faceindexSize,
137 faceindex, nodeindexSize, nodeindex, connectivity)
138 except RuntimeError as ex :
139 print("ERROR : polyhedron connectivity ...\n%s" % ex)
140 doCleanup(fid, -1)
141
142 # create family 0 : by default, all mesh entities family number is 0
143 #TODO : Etudier la pertinence de définir MED_NO_GROUP comme un MEDCHAR('')
144 #TODO : A confronter aux types des paramètres axisname, axisunit
145 try :
146 med.MEDfamilyCr(fid, meshname, "", 0, 0, med.MEDCHAR(""))
147 except RuntimeError as ex :
148 print("ERROR : family 0 creation ...\n%s" % ex)
149 doCleanup(fid, -1)
150
151 # close MED file
152 doCleanup(fid, 0)