Debian Codegraph @

tests/usecases/python/UsesCase_MEDmesh_10.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
22 # *
23 # * Use case 10 : a 2D unstructured mesh with 15 nodes, 8 triangular cells, 4 triangular cells
24 # * and families
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 unstructured mesh"
43 spacedim = 2
44 meshdim = 2
45 # max size of axisname and unitname : 2*med.MED_SNAME_SIZE+1
46 # 12345678901234561234567890123456
47 axisname = "x y "
48 unitname = "cm cm "
49 coordinates = med.MEDFLOAT([2.,1., 7.,1., 12.,1., 17.,1., 22.,1.,
50 2.,6., 7.,6., 12.,6., 17.,6., 22.,6.,
51 2.,11., 7.,11., 12.,11., 17.,11., 22.,11. ])
52 nnodes = 15
53 triaconnectivity = med.MEDINT([ 1,7,6, 2,7,1, 3,7,2, 8,7,3,
54 13,7,8, 12,7,13, 11,7,12, 6,7,11 ])
55 ntria3 = 8
56 quadconnectivity = med.MEDINT([ 3,4,9,8, 4,5,10,9,
57 15,14,9,10, 13,8,9,14 ])
58 nquad4 = 4
59 # max size of familyname : med.MED_NAME_SIZE+1
60 familyname = "BOUNDARY_VERTICES"
61 # max size of groupename : med.MED_LNAME_SIZE+1
62 groupname = med.MEDCHAR("MESH_BOUNDARY_VERTICES")
63 familynumbers = med.MEDINT([1, 1, 1, 1, 1, 1, 0, 0, 0, 1, 1, 1, 1, 1, 1])
64
65 # MED file creation
66 try :
67 fid = med.MEDfileOpen("UsesCase_MEDmesh_10.med", med.MED_ACC_CREAT)
68 except RuntimeError as ex :
69 print("ERROR : file creation ...\n%s" % ex)
70 sys.exit(-1)
71
72 # write a comment in the file
73 try :
74 med.MEDfileCommentWr(fid, "A 2D unstructured mesh : 15 nodes, 12 cells")
75 except RuntimeError as ex :
76 print("ERROR : write file description ...\n%s" % ex)
77 doCleanup(fid, -1)
78
79 # mesh creation : a 2D unstructured mesh
80 try :
81 med.MEDmeshCr(fid, meshname, spacedim, meshdim, med.MED_UNSTRUCTURED_MESH,
82 "A 2D unstructured mesh", "", med.MED_SORT_DTIT,
83 med.MED_CARTESIAN, axisname, unitname)
84 except RuntimeError as ex :
85 print("ERROR : mesh creation ...\n%s" % ex)
86 doCleanup(fid, -1)
87
88 # nodes coordinates in a cartesian axis in full interlace mode
89 # (X1,Y1, X2,Y2, X3,Y3, ...) with no iteration and computation step
90 try :
91 med.MEDmeshNodeCoordinateWr(fid, meshname, med.MED_NO_DT, med.MED_NO_IT,
92 0.0, med.MED_FULL_INTERLACE, nnodes, coordinates)
93 except RuntimeError as ex :
94 print("ERROR : nodes coordinates ...\n%s" % ex)
95 doCleanup(fid, -1)
96
97 # cells connectiviy is defined in nodal mode
98 try :
99 med.MEDmeshElementConnectivityWr(fid, meshname, med.MED_NO_DT,med.MED_NO_IT,
100 0.0, med.MED_CELL, med.MED_TRIA3, med.MED_NODAL,
101 med.MED_FULL_INTERLACE, ntria3, triaconnectivity)
102 except RuntimeError as ex :
103 print("ERROR : triangular cells connectivity ...\n%s" % ex)
104 doCleanup(fid, -1)
105
106 try :
107 med.MEDmeshElementConnectivityWr(fid, meshname, med.MED_NO_DT,med.MED_NO_IT,
108 0.0, med.MED_CELL, med.MED_QUAD4, med.MED_NODAL,
109 med.MED_FULL_INTERLACE, nquad4, quadconnectivity)
110 except RuntimeError as ex :
111 print("ERROR : quadrangular cells connectivity ...\n%s" % ex)
112 doCleanup(fid, -1)
113
114 # create family 0 : by default, all mesh entities family number is 0
115 #TODO : Etudier la pertinence de définir MED_NO_GROUP comme un MEDCHAR('')
116 #TODO : A confronter aux types des paramètres axisname, axisunit
117 try :
118 med.MEDfamilyCr(fid, meshname, med.MED_NO_NAME, 0, 0,
119 med.MEDCHAR(med.MED_NO_GROUP))
120 except RuntimeError as ex :
121 print("ERROR : family 0 creation ...\n%s" % ex)
122 doCleanup(fid, -1)
123
124 # create a family for boundary vertices : by convention a nodes family number is > 0,
125 # and an element family number is < 0
126 try :
127 med.MEDfamilyCr(fid, meshname, familyname, 1, 1, groupname)
128 except RuntimeError as ex :
129 print("ERROR : family %s creation ...\n%s" % (familyname, ex))
130 doCleanup(fid, -1)
131
132 # write family number for nodes
133 try :
134 med.MEDmeshEntityFamilyNumberWr(fid, meshname, med.MED_NO_DT, med.MED_NO_IT,
135 med.MED_NODE, med.MED_NONE, nnodes, familynumbers)
136 except RuntimeError as ex :
137 print("ERROR : nodes family numbers ...\n%s" % ex)
138 doCleanup(fid, -1)
139
140 # close MED file
141 doCleanup(fid, 0)