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

  ViewVC Help
Powered by ViewVC 1.1.26