/[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 2180 - (show annotations)
Thu Dec 18 00:30:25 2008 UTC (11 years, 6 months ago) by caltinay
File MIME type: text/x-python
File size: 8077 byte(s)
Assorted spelling, grammar, whitespace and copy/paste error fixes (Part 3).
With boost 1.37 all epydoc warnings are gone now.
This commit should be a no-op.

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

  ViewVC Help
Powered by ViewVC 1.1.26