/[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 1904 - (show annotations)
Wed Oct 22 08:27:56 2008 UTC (12 years, 11 months ago) by phornby
File MIME type: text/x-python
File size: 8647 byte(s)
This now passes on windows. The main issue was that __del__ was being called when there was no open script or mesh file??? Could not fathom why, but added an extra test in __del__ to make sure the files were actually open before closing & unlinking.  It may be better to deal with the opening, writing, closing, os.system call, and final unlinking of the script file in getMeshHandler(). That way all manipulations are local, and only the mesh file need be handled in a remote piece of code like __del__.

Not tested on Altix or linux yet.... get to that tonight.
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 __script_file = None
49
50 def __init__(self,dim=3,element_size=1.,order=1,keep_files=False):
51 """
52 initializes the gmsh design
53
54 @param dim: patial dimension
55 @param element_size: global element size
56 @param order: element order
57 @param keep_files: flag to keep work files.
58 """
59 design.Design.__init__(self,dim=dim,element_size=element_size,order=order,keep_files=keep_files)
60 self.setScriptFileName()
61 self.setMeshFileName()
62 self.setOptions()
63 def setScriptFileName(self,name=None):
64 """
65 set the filename for the gmsh input script. if no name is given a name with extension geo is generated.
66 """
67 if name == None:
68 self.__scriptname=tempfile.mkstemp(suffix=".geo")[1]
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 self.__mshname=tempfile.mkstemp(suffix=".msh")[1]
83 else:
84 self.__mshname=name
85 self.setKeepFilesOn()
86 def getMeshFileName(self):
87 """
88 returns the name of the file for the gmsh msh
89 """
90 return self.__mshname
91 def setOptions(self,algorithm=None,optimize_quality=True,smoothing=1, curvature_based_element_size=False):
92 """
93 sets options for the mesh generator
94 """
95 if algorithm==None: algorithm=self.DELAUNAY
96 self.__curvature_based_element_size=curvature_based_element_size
97 self.__algo=algorithm
98 self.__optimize_quality=optimize_quality
99 self.__smoothing=smoothing
100 def __del__(self):
101 """
102 clean up
103 """
104 if not self.keepFiles() and (self.scriptFile() != None) :
105 self.scriptFile().close()
106 os.unlink(self.getScriptFileName())
107 os.unlink(self.getMeshFileName())
108
109 def getCommandString(self):
110 """
111 returns the gmsh comand
112 """
113 if self.__optimize_quality:
114 opt="-optimize "
115 else:
116 opt=""
117 if self.__curvature_based_element_size:
118 clcurv="-clcurv "
119 else:
120 clcurv=""
121
122 exe="gmsh -%s -algo %s %s-smooth %s %s-v 0 -order %s -o %s %s"%(self.getDim(),
123 self.__algo,
124 clcurv,
125 self.__smoothing,
126 opt,
127 self.getElementOrder(),
128 self.getMeshFileName(),
129 self.getScriptFileName())
130 return exe
131 def getMeshHandler(self):
132 """
133 returns a handle to a mesh meshing the design. In the current implementation
134 a mesh file name in gmsh format is returned.
135 """
136 f = open(self.getScriptFileName(),"w")
137 setScriptFile(f)
138 f.write(self.getScriptString())
139 cmd = self.getCommandString()
140 ret = os.system(cmd) / 256
141 if ret > 0:
142 raise RuntimeError, "Could not build mesh: %s"%cmd
143 else:
144 return self.getMeshFileName()
145
146 def setScriptFile(self,f=None):
147 self.__script_file=f
148 return
149
150 def scriptFile(self):
151 return self.__script_file
152
153
154
155 def getScriptString(self):
156 """
157 returns the gmsh script to generate the mesh
158 """
159 h=self.getElementSize()
160 out="// generated by esys.pycad\n"
161 for prim in self.getAllPrimitives():
162 p=prim.getUnderlyingPrimitive()
163 if isinstance(p, Point):
164 c=p.getCoordinates()
165 out+="Point(%s) = {%s , %s, %s , %s };\n"%(p.getID(),c[0],c[1],c[2], p.getLocalScale()*h)
166
167 elif isinstance(p, Spline):
168 out+="Spline(%s) = {%s};\n"%(p.getID(),self.__mkArgs(p.getControlPoints()))
169
170 elif isinstance(p, BezierCurve):
171 out+="Bezier(%s) = {%s};\n"%(p.getID(),self.__mkArgs(p.getControlPoints()))
172
173 elif isinstance(p, BSpline):
174 out+="BSpline(%s) = {%s};\n"%(p.getID(),self.__mkArgs(p.getControlPoints()))
175
176 elif isinstance(p, Line):
177 out+="Line(%s) = {%s, %s};\n"%(p.getID(),p.getStartPoint().getDirectedID(),p.getEndPoint().getDirectedID())
178
179 elif isinstance(p, Arc):
180 out+="Circle(%s) = {%s, %s, %s};\n"%(p.getID(),p.getStartPoint().getDirectedID(),p.getCenterPoint().getDirectedID(),p.getEndPoint().getDirectedID())
181
182 elif isinstance(p, Ellipse):
183 out+="Ellipse(%s) = {%s, %s, %s, %s};\n"%(p.getID(),p.getStartPoint().getDirectedID(),p.getCenterPoint().getDirectedID(),p.getPointOnMainAxis().getDirectedID(), p.getEndPoint().getDirectedID())
184
185 elif isinstance(p, CurveLoop):
186 out+="Line Loop(%s) = {%s};\n"%(p.getID(),self.__mkArgs(p.getCurves()))
187
188 elif isinstance(p, RuledSurface):
189 out+="Ruled Surface(%s) = {%s};\n"%(p.getID(),p.getBoundaryLoop().getDirectedID())
190
191 elif isinstance(p, PlaneSurface):
192 line=self.__mkArgs(p.getHoles())
193 if len(line)>0:
194 out+="Plane Surface(%s) = {%s, %s};\n"%(p.getID(),p.getBoundaryLoop().getDirectedID(), line)
195 else:
196 out+="Plane Surface(%s) = {%s};\n"%(p.getID(),p.getBoundaryLoop().getDirectedID())
197
198 elif isinstance(p, SurfaceLoop):
199 out+="Surface Loop(%s) = {%s};\n"%(p.getID(),self.__mkArgs(p.getSurfaces()))
200
201 elif isinstance(p, Volume):
202 line=self.__mkArgs(p.getHoles())
203 if len(line)>0:
204 out+="Volume(%s) = {%s, %s};\n"%(p.getID(),p.getSurfaceLoop().getDirectedID(), line)
205 else:
206 out+="Volume(%s) = {%s};\n"%(p.getID(),p.getSurfaceLoop().getDirectedID())
207
208 elif isinstance(p, PropertySet):
209 if p.getNumItems()>0:
210 dim=p.getDim()
211 line="Physical "
212 if dim==0:
213 line+="Point"
214 elif dim==1:
215 line+="Line"
216 elif dim==2:
217 line+="Surface"
218 else:
219 line+="Volume"
220 out+=line+"(" + str(p.getID()) + ") = {"+self.__mkArgs(p.getItems())+"};\n"
221
222 else:
223 raise TypeError("unable to pass %s object to gmsh."%str(type(p)))
224 return out
225
226
227 def __mkArgs(self,args):
228 line=""
229 for i in args:
230 if len(line)>0:
231 line+=", %s"%i.getDirectedID()
232 else:
233 line="%s"%i.getDirectedID()
234 return line

  ViewVC Help
Powered by ViewVC 1.1.26