Debian Codegraph @

tests/usecases/python/UsesCase_MEDmesh_4.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 structured mesh
22 # *
23 # * Use case 4 : write a 2D structured mesh (5x3 cartesian grid)
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 = "2D structured 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 cooXaxis = med.MEDFLOAT([1.,2.,3.,4.,5.])
50 cooYaxis = med.MEDFLOAT([1.,2.,3.])
51 nquad4 = 8
52 # max size of cellsname : 8*med.MED_SNAME_SIZE+1
53 # 12345678901234561234567890123456123456789012345612345678901234561234567890123456123456789012345612345678901234561234567890123456
54 cellsnames = med.MEDCHAR("CELL_1 CELL_2 CELL_3 CELL_4 CELL_5 CELL_6 CELL_7 CELL_8 ")
55 # max size of familyname : med.MED_NAME_SIZE+1
56 familyname = "CART_GRID_QUAD_FAMILY"
57 # max size of groupname : med.MED_LNAME_SIZE+1
58 groupname = med.MEDCHAR("CART_GRID_GROUP")
59 familynumbers = med.MEDINT([-1,-1,-1,-1,-1,-1,-1,-1])
60
61 # MED file creation
62 try :
63 fid = med.MEDfileOpen("UsesCase_MEDmesh_4.med", med.MED_ACC_CREAT)
64 except RuntimeError as ex :
65 print("ERROR : file creation ...\n%s" % ex)
66 sys.exit(-1)
67
68 # create the structured mesh in the MED file
69 try :
70 med.MEDmeshCr(fid, meshname, spacedim, meshdim, med.MED_STRUCTURED_MESH,
71 "A 2D structured mesh", "", med.MED_SORT_DTIT,
72 med.MED_CARTESIAN, axisname, unitname)
73 except RuntimeError as ex :
74 print("ERROR : mesh creation ...\n%s" % ex)
75 doCleanup(fid, -1)
76
77 # specify the grid type : MED_CARTESIAN_GRID
78 try :
79 med.MEDmeshGridTypeWr(fid, meshname, med.MED_CARTESIAN_GRID)
80 except RuntimeError as ex :
81 print("ERROR : write grid type ...\n%s" % ex)
82 doCleanup(fid, -1)
83
84 # write axis "X" and "Y" coordinates
85 axis = 1
86 size = 5
87 try :
88 med.MEDmeshGridIndexCoordinateWr(fid, meshname, med.MED_NO_DT,med.MED_NO_IT,
89 0.0, axis, size, cooXaxis)
90 except RuntimeError as ex :
91 print("ERROR : write of axis X coordinates ...\n%s" % ex)
92 doCleanup(fid, -1)
93
94 axis = 2
95 size = 3
96 try :
97 med.MEDmeshGridIndexCoordinateWr(fid, meshname, med.MED_NO_DT,med.MED_NO_IT,
98 0.0, axis, size, cooYaxis)
99 except RuntimeError as ex :
100 print("ERROR : write of axis Y coordinates ...\n%s" % ex)
101 doCleanup(fid, -1)
102
103 # optionnal : names for nodes or elements
104 # In this case, a name is given to the cells of the mesh
105 try :
106 med.MEDmeshEntityNameWr(fid, meshname, med.MED_NO_DT, med.MED_NO_IT,
107 med.MED_CELL, med.MED_QUAD4, nquad4, cellsnames)
108 except RuntimeError as ex :
109 print("ERROR : cells names ...\n%s" % ex)
110 doCleanup(fid, -1)
111
112 # We have to create family 0 : by default, all mesh entities family number is 0
113 #TODO : Etudier la pertinence de définir MED_NO_GROUP comme un MEDCHAR('')
114 #TODO : A confronter aux types des paramètres axisname, axisunit
115 try :
116 med.MEDfamilyCr(fid, meshname, med.MED_NO_NAME, 0, 0,
117 med.MEDCHAR(med.MED_NO_GROUP))
118 except RuntimeError as ex :
119 print("ERROR : family 0 creation ...\n%s" % ex)
120 doCleanup(fid, -1)
121
122 # We decide to create a family for boundary quad4 :
123 # by convention a nodes family number is > 0 and an element family number is < 0
124 try :
125 med.MEDfamilyCr(fid, meshname, familyname, 1, -1, groupname)
126 except RuntimeError as ex :
127 print("ERROR : family creation ...\n%s" % ex)
128 doCleanup(fid, -1)
129
130 # Then we write family number for quad4
131 try :
132 med.MEDmeshEntityFamilyNumberWr(fid, meshname, med.MED_NO_DT, med.MED_NO_IT,
133 med.MED_CELL, med.MED_QUAD4, nquad4, familynumbers)
134 except RuntimeError as ex :
135 print("ERROR : nodes family numbers ...\n%s" % ex)
136 doCleanup(fid, -1)
137
138 # close MED file
139 doCleanup(fid, 0)