/[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 2090 - (show annotations)
Mon Nov 24 06:41:08 2008 UTC (10 years, 10 months ago) by ksteube
File MIME type: text/x-python
File size: 8371 byte(s)
Reverted pycad/py_src/gmsh.py to before revision 1904.
A change had been made to the way the geo file was opened.

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

  ViewVC Help
Powered by ViewVC 1.1.26