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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1137 - (hide annotations)
Thu May 10 08:11:31 2007 UTC (12 years, 4 months ago) by gross
File MIME type: text/x-python
File size: 7804 byte(s)
This version passes the tests on windows except for 

   * vtk
   * netCDF

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

  ViewVC Help
Powered by ViewVC 1.1.26