1#! /usr/bin/env python
2# -*- coding:utf-8 -*-
3# /*  This file is part of MED.
4#  *
5#  *  COPYRIGHT (C) 1999 - 2019  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 6 : a 2D unstructured mesh with the following features
24#  *  computin steps, profiles and nodes coordinates evolution.
25#  *****************************************************************************/
26
27import sys
28
29import med
30
31def 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
42meshname = "2D unstructured mesh"
43spacedim = 2
44meshdim = 2
45#  max size of axisname and unitname : 2*med.MED_SNAME_SIZE+1
46#           12345678901234561234567890123456
47axisname = "x               y               "
48unitname = "cm              cm              "
49initial_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. ])
52nnodes = 15
53triaconnectivity = 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 ])
55ntria3 = 8
56quadconnectivity = med.MEDINT([ 3,4,9,8,    4,5,10,9,
57								15,14,9,10, 13,8,9,14 ])
58nquad4 = 4
59#  new coordinates (step 1) for nodes 13, 14 and 15
60new_coordinates_step1 = med.MEDFLOAT([12.,15., 17.,15., 22.,15.])
61#  max size of profile1name : med.MED_NAME_SIZE+1
62profile1name = "UPPER_QUAD4_PROFILE"
63profile1 = med.MEDINT([13, 14, 15])
64profile1size = 3
65#  new coordinates (step 2) for nodes 8, 9 and 10
66new_coordinates_step2 = med.MEDFLOAT([12.,10., 17.,10., 22.,10.])
67#  max size of profile2name : med.MED_NAME_SIZE+1
68profile2name = "MIDDLE_QUAD4_PROFILE"
69profile2 = med.MEDINT([8, 9, 10])
70profile2size = 3
71
72#  MED file creation
73try :
74	fid = med.MEDfileOpen("UsesCase_MEDmesh_6.med", med.MED_ACC_CREAT)
75except RuntimeError as ex :
76	print("ERROR : file creation ...\n%s" % ex)
77	sys.exit(-1)
78
79#  write a comment in the file
80try :
81	med.MEDfileCommentWr(fid, "A 2D unstructured mesh : 15 nodes, 12 cells")
82except RuntimeError as ex :
83	print("ERROR : write file description ...\n%s" % ex)
84	doCleanup(fid, -1)
85
86#  create the profiles in the file
87try :
88	med.MEDprofileWr(fid, profile1name, profile1size, profile1)
89except RuntimeError as ex :
90	print("ERROR : create profile 1 ...")
91	doCleanup(fid, -1)
92
93try :
94	med.MEDprofileWr(fid, profile2name, profile2size, profile2)
95except RuntimeError as ex :
96	print("ERROR : create profile 2 ...")
97	doCleanup(fid, -1)
98
99#  mesh creation : a 2D unstructured mesh
100try :
101	med.MEDmeshCr(fid, meshname, spacedim, meshdim, med.MED_UNSTRUCTURED_MESH,
102				"A 2D unstructured mesh", "", med.MED_SORT_DTIT,
103				med.MED_CARTESIAN, axisname, unitname)
104except RuntimeError as ex :
105	print("ERROR : mesh creation ...\n%s" % ex)
106	doCleanup(fid, -1)
107
108#  initial nodes coordinates in a cartesian axis in full interlace mode
109#  (X1,Y1, X2,Y2, X3,Y3, ...)
110try :
111	med.MEDmeshNodeCoordinateWithProfileWr(fid, meshname, med.MED_NO_DT,
112						med.MED_NO_IT, 0.0, med.MED_COMPACT_STMODE,
113						med.MED_NO_PROFILE, med.MED_FULL_INTERLACE,
114						med.MED_ALL_CONSTITUENT, nnodes, initial_coordinates)
115except RuntimeError as ex :
116	print("ERROR : nodes coordinates ...\n%s" % ex)
117	doCleanup(fid, -1)
118
119#  cells connectiviy is defined in nodal mode
120try :
121	med.MEDmeshElementConnectivityWithProfileWr(fid, meshname, med.MED_NO_DT,
122						med.MED_NO_IT, 0.0, med.MED_CELL, med.MED_TRIA3,
123						med.MED_NODAL, med.MED_COMPACT_STMODE,
124						med.MED_NO_PROFILE, med.MED_FULL_INTERLACE,
125						med.MED_ALL_CONSTITUENT, ntria3, triaconnectivity)
126except RuntimeError as ex :
127	print("ERROR : triangular cells connectivity ...\n%s" % ex)
128	doCleanup(fid, -1)
129
130try :
131	med.MEDmeshElementConnectivityWithProfileWr(fid, meshname, med.MED_NO_DT,
132						med.MED_NO_IT, 0.0, med.MED_CELL, med.MED_QUAD4,
133						med.MED_NODAL, med.MED_COMPACT_STMODE,
134						med.MED_NO_PROFILE, med.MED_FULL_INTERLACE,
135						med.MED_ALL_CONSTITUENT, nquad4, quadconnectivity)
136except RuntimeError as ex :
137	print("ERROR : quadrangular cells connectivity ...\n%s" % ex)
138	doCleanup(fid, -1)
139
140#  Mesh deformation (nodes coordinates) in 2 steps
141#  The nodes modified are identified by a profile
142
143#  STEP 1 : dt1 = 5.5, it = 1
144try :
145	med.MEDmeshNodeCoordinateWithProfileWr(fid, meshname, 1, 1, 5.5,
146					med.MED_COMPACT_STMODE, profile1name,
147					med.MED_FULL_INTERLACE, med.MED_ALL_CONSTITUENT,
148					nnodes, new_coordinates_step1)
149except RuntimeError as ex :
150	print("ERROR : nodes coordinates 1 ...")
151	doCleanup(fid, -1)
152
153#  STEP 2 : dt2 = 8.9, it = 1
154try :
155	med.MEDmeshNodeCoordinateWithProfileWr(fid, meshname, 2, 1, 8.9,
156					med.MED_COMPACT_STMODE, profile2name,
157					med.MED_FULL_INTERLACE, med.MED_ALL_CONSTITUENT,
158					nnodes, new_coordinates_step2)
159except RuntimeError as ex :
160	print("ERROR : nodes coordinates 2 ...")
161	doCleanup(fid, -1)
162
163#  create family 0 : by default, all mesh entities family number is 0
164#TODO : Etudier la pertinence de définir MED_NO_GROUP comme un MEDCHAR('')
165#TODO : A confronter aux types des paramètres axisname, axisunit
166try :
167	med.MEDfamilyCr(fid, meshname, med.MED_NO_NAME, 0, 0,
168				med.MEDCHAR(med.MED_NO_GROUP))
169except RuntimeError as ex :
170	print("ERROR : family 0 creation ...\n%s" % ex)
171	doCleanup(fid, -1)
172
173#  close MED file
174doCleanup(fid, 0)