Debian Codegraph @

tests/usecases/python/UsesCase_MEDmesh_13.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 13 : a 2D unstructured mesh with 10 nodes and 2 polygons
23 # * poly1 : 1,4,7,9,6,3
24 # * poly2 : 2,5,8,10,7,4
25 # * 9 10
26 # *
27 # * 6 7 8
28 # *
29 # * 3 4 5
30 # *
31 # * 1 2
32 # *
33 # *****************************************************************************/
34
35 import sys
36
37 import med
38
39 def doCleanup(fid, status) :
40 try :
41 med.MEDfileClose(fid)
42 except RuntimeError as ex :
43 print("ERROR : close file ...\n%s" % ex)
44 status = -1
45 finally :
46 if status != 0 :
47 sys.exit(status)
48
49 # max size of meshname : med.MED_NAME_SIZE+1
50 meshname = "2D unstructured mesh"
51 spacedim = 2
52 meshdim = 2
53 # max size of axisname and unitname : 2*med.MED_SNAME_SIZE+1
54 # 12345678901234561234567890123456
55 axisname = "x y "
56 unitname = "cm cm "
57 # ten nodes with two of them shared by the two polygons
58 coordinates = med.MEDFLOAT([0.5, 0.,
59 1.5, 0.,
60 0., 0.5,
61 1., 0.5,
62 2., 0.5,
63 0., 1.,
64 1., 1.,
65 2., 1.,
66 0.5, 2.,
67 1.5, 2. ])
68 nnodes = 10
69 indexsize = 3
70 index = med.MEDINT([1, 7, 13])
71 # connectivity : 2 hexagons
72 connectivity = med.MEDINT([ 1, 4, 7, 9, 6, 3,
73 2, 5, 8, 10, 7, 4])
74
75 # MED file creation
76 try :
77 fid = med.MEDfileOpen("UsesCase_MEDmesh_13.med", med.MED_ACC_CREAT)
78 except RuntimeError as ex :
79 print("ERROR : file creation ...\n%s" % ex)
80 sys.exit(-1)
81
82 # write a comment in the file
83 try :
84 med.MEDfileCommentWr(fid, "A 2D unstructured mesh : 12, 12 polygons")
85 except RuntimeError as ex :
86 print("ERROR : write file description ...\n%s" % ex)
87 doCleanup(fid, -1)
88
89 # mesh creation : a 2D unstructured mesh
90 try :
91 med.MEDmeshCr(fid, meshname, spacedim, meshdim, med.MED_UNSTRUCTURED_MESH,
92 "A 2D unstructured mesh", "", med.MED_SORT_DTIT,
93 med.MED_CARTESIAN, axisname, unitname)
94 except RuntimeError as ex :
95 print("ERROR : mesh creation ...\n%s" % ex)
96 doCleanup(fid, -1)
97
98 # nodes coordinates in a cartesian axis in full interlace mode
99 # (X1,Y1, X2,Y2, X3,Y3, ...) with no iteration and computation step
100 try :
101 med.MEDmeshNodeCoordinateWr(fid, meshname, med.MED_NO_DT, med.MED_NO_IT,
102 med.MED_UNDEF_DT, med.MED_FULL_INTERLACE,
103 nnodes, coordinates)
104 except RuntimeError as ex :
105 print("ERROR : nodes coordinates ...\n%s" % ex)
106 doCleanup(fid, -1)
107
108 # cells connectiviy is defined in nodal mode
109 # 2 polygons
110 try :
111 med.MEDmeshPolygonWr(fid, meshname, med.MED_NO_DT, med.MED_NO_IT,
112 med.MED_UNDEF_DT, med.MED_CELL, med.MED_NODAL,
113 indexsize, index, connectivity)
114 except RuntimeError as ex :
115 print("ERROR : polygon connectivity ...\n%s" % ex)
116 doCleanup(fid, -1)
117
118 # create family 0 : by default, all mesh entities family number is 0
119 #TODO : Etudier la pertinence de définir MED_NO_GROUP comme un MEDCHAR('')
120 #TODO : A confronter aux types des paramètres axisname, axisunit
121 try :
122 med.MEDfamilyCr(fid, meshname, med.MED_NO_NAME, 0, 0,
123 med.MEDCHAR(med.MED_NO_GROUP))
124 except RuntimeError as ex :
125 print("ERROR : family 0 creation ...\n%s" % ex)
126 doCleanup(fid, -1)
127
128 # close MED file
129 doCleanup(fid, 0)