/[escript]/trunk/pycad/py_src/gmsh.py
ViewVC logotype

Contents of /trunk/pycad/py_src/gmsh.py

Parent Directory Parent Directory | Revision Log Revision Log


Revision 2096 - (show annotations)
Tue Nov 25 10:29:27 2008 UTC (10 years, 10 months ago) by phornby
File MIME type: text/x-python
File size: 8524 byte(s)
Starting from the recent reversion:

1. Leave __del__ alone.
2. mkstemp() actually opens the file it creates, only to be opened again in getMeshHandler(), so add an os.close() straight after the mkstemp()'s
3. in getMeshHandler(), close the script file immediately when finished with it instead of doing something non-local in __del__.

Untested on 'nix's at this point. Commit is for testing purposes.

1
2 ########################################################
3 #
4 # Copyright (c) 2003-2008 by University of Queensland
5 # Earth Systems Science Computational Center (ESSCC)
6 # http://www.uq.edu.au/esscc
7 #
8 # Primary Business: Queensland, Australia
9 # Licensed under the Open Software License version 3.0
10 # http://www.opensource.org/licenses/osl-3.0.php
11 #
12 ########################################################
13
14 __copyright__="""Copyright (c) 2003-2008 by University of Queensland
15 Earth Systems Science Computational Center (ESSCC)
16 http://www.uq.edu.au/esscc
17 Primary Business: Queensland, Australia"""
18 __license__="""Licensed under the Open Software License version 3.0
19 http://www.opensource.org/licenses/osl-3.0.php"""
20 __url__="http://www.uq.edu.au/esscc/escript-finley"
21
22 """
23 mesh generation using gmsh
24
25 @var __author__: name of author
26 @var __copyright__: copyrights
27 @var __license__: licence agreement
28 @var __url__: url entry point on documentation
29 @var __version__: version
30 @var __date__: date of the version
31 """
32
33 __author__="Lutz Gross, l.gross@uq.edu.au"
34
35 import design
36 import tempfile
37 import os
38 from primitives import Point, Spline, BezierCurve, BSpline, Line, Arc, CurveLoop, RuledSurface, PlaneSurface, SurfaceLoop, Volume, PropertySet, Ellipse
39
40 class Design(design.Design):
41 """
42 design fo gmsh
43 """
44 DELAUNAY="iso"
45 NETGEN="netgen"
46 TETGEN="tetgen"
47
48 def __init__(self,dim=3,element_size=1.,order=1,keep_files=False):
49 """
50 initializes the gmsh design
51
52 @param dim: patial dimension
53 @param element_size: global element size
54 @param order: element order
55 @param keep_files: flag to keep work files.
56 """
57 design.Design.__init__(self,dim=dim,element_size=element_size,order=order,keep_files=keep_files)
58 self.setScriptFileName()
59 self.setMeshFileName()
60 self.setOptions()
61 def setScriptFileName(self,name=None):
62 """
63 set the filename for the gmsh input script. if no name is given a name with extension geo is generated.
64 """
65 if name == None:
66 tmp_f_id=tempfile.mkstemp(suffix=".geo")
67 self.__scriptname=tmp_f_id[1]
68 os.close(tmp_f_id[0])
69 else:
70 self.__scriptname=name
71 self.setKeepFilesOn()
72 def getScriptFileName(self):
73 """
74 returns the name of the file for the gmsh script
75 """
76 return self.__scriptname
77 def setMeshFileName(self, name=None):
78 """
79 sets the name for the gmsh mesh file. if no name is given a name with extension msh is generated.
80 """
81 if name == None:
82 tmp_f_id=tempfile.mkstemp(suffix=".msh")
83 self.__mshname=tmp_f_id[1]
84 os.close(tmp_f_id[0])
85 else:
86 self.__mshname=name
87 self.setKeepFilesOn()
88 def getMeshFileName(self):
89 """
90 returns the name of the file for the gmsh msh
91 """
92 return self.__mshname
93 def setOptions(self,algorithm=None,optimize_quality=True,smoothing=1, curvature_based_element_size=False):
94 """
95 sets options for the mesh generator
96 """
97 if algorithm==None: algorithm=self.DELAUNAY
98 self.__curvature_based_element_size=curvature_based_element_size
99 self.__algo=algorithm
100 self.__optimize_quality=optimize_quality
101 self.__smoothing=smoothing
102 def __del__(self):
103 """
104 clean up
105 """
106 if not self.keepFiles() :
107 os.unlink(self.getScriptFileName())
108 os.unlink(self.getMeshFileName())
109
110 def getCommandString(self):
111 """
112 returns the gmsh comand
113 """
114 if self.__optimize_quality:
115 opt="-optimize "
116 else:
117 opt=""
118 if self.__curvature_based_element_size:
119 clcurv="-clcurv "
120 else:
121 clcurv=""
122
123 exe="gmsh -%s -algo %s %s-smooth %s %s-v 0 -order %s -o %s %s"%(self.getDim(),
124 self.__algo,
125 clcurv,
126 self.__smoothing,
127 opt,
128 self.getElementOrder(),
129 self.getMeshFileName(),
130 self.getScriptFileName())
131 return exe
132 def getMeshHandler(self):
133 """
134 returns a handle to a mesh meshing the design. In the current implementation
135 a mesh file name in gmsh format is returned.
136 """
137 f = open(self.getScriptFileName(),"w")
138 f.write(self.getScriptString())
139 f.close()
140 cmd = self.getCommandString()
141 ret = os.system(cmd) / 256
142 if ret > 0:
143 raise RuntimeError, "Could not build mesh: %s"%cmd
144 else:
145 return self.getMeshFileName()
146
147 def getScriptString(self):
148 """
149 returns the gmsh script to generate the mesh
150 """
151 h=self.getElementSize()
152 out="// generated by esys.pycad\n"
153 for prim in self.getAllPrimitives():
154 p=prim.getUnderlyingPrimitive()
155 if isinstance(p, Point):
156 c=p.getCoordinates()
157 out+="Point(%s) = {%s , %s, %s , %s };\n"%(p.getID(),c[0],c[1],c[2], p.getLocalScale()*h)
158
159 elif isinstance(p, Spline):
160 out+="Spline(%s) = {%s};\n"%(p.getID(),self.__mkArgs(p.getControlPoints()))
161
162 elif isinstance(p, BezierCurve):
163 out+="Bezier(%s) = {%s};\n"%(p.getID(),self.__mkArgs(p.getControlPoints()))
164
165 elif isinstance(p, BSpline):
166 out+="BSpline(%s) = {%s};\n"%(p.getID(),self.__mkArgs(p.getControlPoints()))
167
168 elif isinstance(p, Line):
169 out+="Line(%s) = {%s, %s};\n"%(p.getID(),p.getStartPoint().getDirectedID(),p.getEndPoint().getDirectedID())
170
171 elif isinstance(p, Arc):
172 out+="Circle(%s) = {%s, %s, %s};\n"%(p.getID(),p.getStartPoint().getDirectedID(),p.getCenterPoint().getDirectedID(),p.getEndPoint().getDirectedID())
173
174 elif isinstance(p, Ellipse):
175 out+="Ellipse(%s) = {%s, %s, %s, %s};\n"%(p.getID(),p.getStartPoint().getDirectedID(),p.getCenterPoint().getDirectedID(),p.getPointOnMainAxis().getDirectedID(), p.getEndPoint().getDirectedID())
176
177 elif isinstance(p, CurveLoop):
178 out+="Line Loop(%s) = {%s};\n"%(p.getID(),self.__mkArgs(p.getCurves()))
179
180 elif isinstance(p, RuledSurface):
181 out+="Ruled Surface(%s) = {%s};\n"%(p.getID(),p.getBoundaryLoop().getDirectedID())
182
183 elif isinstance(p, PlaneSurface):
184 line=self.__mkArgs(p.getHoles())
185 if len(line)>0:
186 out+="Plane Surface(%s) = {%s, %s};\n"%(p.getID(),p.getBoundaryLoop().getDirectedID(), line)
187 else:
188 out+="Plane Surface(%s) = {%s};\n"%(p.getID(),p.getBoundaryLoop().getDirectedID())
189
190 elif isinstance(p, SurfaceLoop):
191 out+="Surface Loop(%s) = {%s};\n"%(p.getID(),self.__mkArgs(p.getSurfaces()))
192
193 elif isinstance(p, Volume):
194 line=self.__mkArgs(p.getHoles())
195 if len(line)>0:
196 out+="Volume(%s) = {%s, %s};\n"%(p.getID(),p.getSurfaceLoop().getDirectedID(), line)
197 else:
198 out+="Volume(%s) = {%s};\n"%(p.getID(),p.getSurfaceLoop().getDirectedID())
199
200 elif isinstance(p, PropertySet):
201 if p.getNumItems()>0:
202 dim=p.getDim()
203 line="Physical "
204 if dim==0:
205 line+="Point"
206 elif dim==1:
207 line+="Line"
208 elif dim==2:
209 line+="Surface"
210 else:
211 line+="Volume"
212 out+=line+"(" + str(p.getID()) + ") = {"+self.__mkArgs(p.getItems())+"};\n"
213
214 else:
215 raise TypeError("unable to pass %s object to gmsh."%str(type(p)))
216 return out
217
218
219 def __mkArgs(self,args):
220 line=""
221 for i in args:
222 if len(line)>0:
223 line+=", %s"%i.getDirectedID()
224 else:
225 line="%s"%i.getDirectedID()
226 return line

  ViewVC Help
Powered by ViewVC 1.1.26