/[escript]/trunk/doc/user/pycad.tex
ViewVC logotype

Diff of /trunk/doc/user/pycad.tex

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 999 by gross, Tue Feb 27 08:12:37 2007 UTC revision 1811 by ksteube, Thu Sep 25 23:11:13 2008 UTC
# Line 1  Line 1 
 \chapter{The module \pycad}  
 \label{PYCAD CHAP}  
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    
15    \chapter{The Module \pycad} \label{PYCAD CHAP}
16    
17    
18  \section{Introduction}  \section{Introduction}
19    
20    \pycad provides a simple way to build a mesh for your finite element
21    simulation.  You begin by building what we call a {\it Design} using
22    primitive geometric objects, and then to go on to build a mesh from
23    the {\it Design}.  The final step of generating the mesh from a {\it
24    Design} uses freely available mesh generation software, such as \gmshextern.
25    
26    A {\it Design} is built by defining points, which are used to specify
27    the corners of geometric objects and the vertices of curves.  Using
28    points you construct more interesting objects such as lines,
29    rectangles, and arcs.  By adding many of these objects into what we
30    call a {\it Design}, you can build meshes for arbitrarily complex 2-D
31    and 3-D structures.
32    
33    The example included below shows how to use {\it pycad} to create a 2-D mesh
34    in the shape of a trapezoid with a cutout area.
35    
36    \begin{python}
37        from esys.pycad import *
38        from esys.pycad.gmsh import Design
39        from esys.finley import MakeDomain
40    
41        # A trapezoid
42        p0=Point(0.0, 0.0, 0.0)
43        p1=Point(1.0, 0.0, 0.0)
44        p2=Point(1.0, 0.5, 0.0)
45        p3=Point(0.0, 1.0, 0.0)
46        l01=Line(p0, p1)
47        l12=Line(p1, p2)
48        l23=Line(p2, p3)
49        l30=Line(p3, p0)
50        c=CurveLoop(l01, l12, l23, l30)
51    
52        # A small triangular cutout
53        x0=Point(0.1, 0.1, 0.0)
54        x1=Point(0.5, 0.1, 0.0)
55        x2=Point(0.5, 0.2, 0.0)
56        x01=Line(x0, x1)
57        x12=Line(x1, x2)
58        x20=Line(x2, x0)
59        cutout=CurveLoop(x01, x12, x20)
60    
61        # Create the surface with cutout
62        s=PlaneSurface(c, holes=[cutout])
63    
64        # Create a Design which can make the mesh
65        d=Design(dim=2, element_size=0.05)
66    
67        # Add the trapezoid with cutout
68        d.addItems(s)
69    
70        # Create the geometry, mesh and Escript domain
71        d.setScriptFileName("trapezoid.geo")
72        d.setMeshFileName("trapezoid.msh")
73        domain=MakeDomain(d, integrationOrder=-1, reducedIntegrationOrder=-1, optimizeLabeling=True)
74    
75        # Create a file that can be read back in to python with mesh=ReadMesh(fileName)
76        domain.write("trapezoid.fly")
77    \end{python}
78    
79    This example is included with the software in
80    \code{pycad/examples/trapezoid.py}.  If you have gmsh installed you can
81    run the example and view the geometry and mesh with:
82    
83    \begin{python}
84        python trapezoid.py
85        gmsh trapezoid.geo
86        gmsh trapezoid.msh
87    \end{python}
88    
89    A \code{CurveLoop} is used to connect several lines into a single curve.
90    It is used in the example above to create the trapezoidal outline for the grid
91    and also for the triangular cutout area.
92    You can use any number of lines when creating a \code{CurveLoop}, but
93    the end of one line must be identical to the start of the next.
94    
95    Sometimes you might see us write \code{-c} where \code{c} is a
96    \code{CurveLoop}.  This is the reverse curve of the curve \code{c}.
97    It is identical to the original except that its points are traversed
98    in the opposite order.  This may make it easier to connect two curves
99    in a \code{CurveLoop}.
100    
101    The example python script above calls both
102    \code{d.setScriptFileName()} and \code{d.setMeshFileName()}.  You need
103    only call these if you wish to save the gmsh geometry and mesh files.
104    
105    Note that the underlying mesh generation software will not accept all
106    the geometries you can create with {\it pycad}.  For example, {\it
107    pycad} will happily allow you to create a 2-D {\it Design} that is a
108    closed loop with some additional points or lines lying outside of the
109    enclosed area, but gmsh will fail to create a mesh for it.
110    
111    
112    
113    
114    
115    
116  \section{\pycad Classes}  \section{\pycad Classes}
117  \declaremodule{extension}{esys.pycad}  \declaremodule{extension}{esys.pycad}
# Line 12  Line 119 
119    
120  \subsection{Primitives}  \subsection{Primitives}
121    
122  \begin{classdesc}{Point}{}  Some of the most commonly-used objects in {\it pycad} are listed here. For a more complete
123    list see the full API documentation.
124    
125    \begin{classdesc}{Point}{x1, x2, x3}
126    Create a point with from coordinates.
127    \end{classdesc}
128    
129    \begin{classdesc}{Line}{point1, point2}
130    Create a line with between starting and ending points.
131    \end{classdesc}
132    
133    \begin{classdesc}{Curve}{point1, point2, ...}
134    Create a \code{Curve}, which is simply a list of points.
135  \end{classdesc}  \end{classdesc}
136    
137  \begin{classdesc}{Manifold1D}{}  \begin{classdesc}{Spline}{curve}
138    Interpret a \code{Curve} using a spline.
139    \end{classdesc}
140    
141    \begin{classdesc}{BSpline}{curve}
142    Interpret a \code{Curve} using a b-spline.
143  \end{classdesc}  \end{classdesc}
144    
145  \begin{classdesc}{Manifold2D}{}  \begin{classdesc}{BezierCurve}{curve}
146    Interpret a \code{Curve} using a Bezier curve.
147    \end{classdesc}
148    
149    \begin{classdesc}{CurveLoop}{list}
150    Create a closed \code{Curve} connecting the lines and/or points given in the \code{list}.
151  \end{classdesc}  \end{classdesc}
152    
153  \begin{classdesc}{Manifold3D}{}  \begin{classdesc}{Arc}{center_point, start_point, end_point}
154    Create an arc by specifying a center for a circle and start and end points. An arc may subtend an angle of at most $\pi$ radians.
155    \end{classdesc}
156    
157    \begin{classdesc}{PlaneSurface}{loop, \optional{holes=[list]}}
158    Create a surface for a 2-D mesh, which may have one or more holes.
159  \end{classdesc}  \end{classdesc}
160    
161  %============================================================================================================  \begin{classdesc}{RuledSurface}{list}
162  \subsection{Transformations}  Create a surface that can be interpolated using transfinite interpolation.
163    \end{classdesc}
164    
165  Transformations are used to move geometrical objects in the 3-dimensional space:  \begin{classdesc}{SurfaceLoop}{list}
166    Create a loop of 2D primitives, which defines the shell of a volume.
167    \end{classdesc}
168    
169  \begin{datadesc}{DEG}  \begin{classdesc}{Volume}{loop, \optional{holes=[list]}}
170  The unit of degree. For instance use \code{90*DEG} for $90$ degrees.  Create a volume for a 3-D mesh given a SurfaceLoop, which may have one or more holes.
171  \end{datadesc}  \end{classdesc}
172    
173  \begin{datadesc}{RAD}  \begin{classdesc}{PropertySet}{list}
174  The unit of radiant. For instance use \code{math.pi*RAD} for $180$ degrees.  Create a PropertySet given a list of 1-D, 2-D or 3-D items. See the section on Properties below for more information.
175  \end{datadesc}  \end{classdesc}
176    
177    %============================================================================================================
178    \subsection{Transformations}
179    
180    Sometimes it's convenient to create an object and then make copies at
181    different orientations and in different sizes.  Transformations are
182    used to move geometrical objects in the 3-dimensional space and to
183    resize them.
184    
185  \begin{classdesc}{Translation}{\optional{b=[0,0,0]}}  \begin{classdesc}{Translation}{\optional{b=[0,0,0]}}
186  defines a translation $x \to x+b$. \var{b} can be any object that can be converted  defines a translation $x \to x+b$. \var{b} can be any object that can be converted
# Line 70  into a \numarray object of shape $(3,)$. Line 211  into a \numarray object of shape $(3,)$.
211  \var{normal} does not have to be normalized but must have positive length.  \var{normal} does not have to be normalized but must have positive length.
212  \end{classdesc}  \end{classdesc}
213    
214    \begin{datadesc}{DEG}
215    A constant to convert from degrees to an internal angle representation in radians. For instance use \code{90*DEG} for $90$ degrees.
216    \end{datadesc}
217    
218  \subsection{Properties}  \subsection{Properties}
219    
220  Property sets are used to bundle a set of geometrical objects in a group. The group  If you are building a larger geometry you may find it convenient to
221  is identified by a name. Typically a property set is used to mark  create it in smaller pieces and then assemble them into the whole.
222  subregions with share the same material properties or to mark portions of the boundary.  Property sets make this easy, and they allow you to name the smaller
223  For efficiency, the \Design class object assigns a integer to each of its property sets,  pieces for convenience.
224  a so-called tag \index{tag}. The appropriate tag is attached to the elements at generation time.  
225  The \TagMap generated by the \Design allows mapping the a name onto the corresponding tag.  Property sets are used to bundle a set of geometrical objects in a
226  In order to avoid ambiguity it is recommended to have unique names of property sets within a \Design.  group.  The group is identified by a name.  Typically a property set
227    is used to mark subregions with share the same material properties or
228    to mark portions of the boundary.  For efficiency, the \Design class
229    object assigns a integer to each of its property sets, a so-called tag
230    \index{tag}.  The appropriate tag is attached to the elements at
231    generation time.
232    
233    See the file \code{pycad/examples/quad.py} for an example using a {\it PropertySet}.
234    
235    
236  \begin{classdesc}{PropertySet}{name,*items}  \begin{classdesc}{PropertySet}{name,*items}
237  defines a group geometrical objects which can be accessed through a \var{name}  defines a group geometrical objects which can be accessed through a \var{name}
# Line 121  clears the list of items Line 273  clears the list of items
273  returns the tag used for this property set  returns the tag used for this property set
274  \end{methoddesc}  \end{methoddesc}
275    
276  \subsection{Accessing \PropertySet Names}  \section{Interface to the mesh generation software}
 During mesh generation the \PropertySet objects are not identified by their name but an integer tag (mainly to provide  
 a quicker indexing mechanism). The \TagMap which is generated by a \Design class object at mesh generation time  
 provides an mechanism to map the property set names onto tags and vice versa.  
 The following example illustrates the mechanis: In this case, the \TagMap \var{tm}  
 maps the names \var{x}, \var{a} onto the tags \var{5} and \var{4} and the tag \var{4}, respectively:    
 \begin{python}  
 tm=TagMap({5 : "x" })  
 tm.setMap(a=1,x=4)  
 print tm.getTags("a"), tm.getTags("x")  
 \end{python}  
 Th output is  
 \begin{python}  
    [ 1 ], [ 5, 4 ]  
 \end{python}  
   
 \begin{python}  
 d=Design()  
 d.add(PropertySet(name="a"))  
 print d.getTagMap().getTags("a")  
 \end{python}  
   
 \begin{python}  
 d=Design()  
 d.add(PropertySet(name="a"))  
 domain=esys.finley.  
 print d.getTagMap().getTags("a")  
 \end{python}  
   
 \begin{classdesc}{TagMap}{\optional{map = \{\} }}  
 defines a mapping between names (str) and tags (int).  
 The dictionary \var{map} sets an initial mapping from tag to name.  
 \end{classdesc}  
   
 \begin{methoddesc}[TagMap]{setMap}{**kwargs}  
 adds a map from names to tags using keyword arguments. For instance  
 \var{top=1234} assigns the tag \var{123} to name \var{top}. The tag has to be integer.  
 If a tag has been assigned to a name before the mapping will be overwritten.  
 Notice that a single name can be assigned to different tags.  
 \end{methoddesc}  
   
 \begin{methoddesc}[TagMap]{getTags}{\optional{name=None}}  
 returns a list of the tags assigned to \var{name}. If \var{name} is not present  
 a list of tags is returned.  
 \end{methoddesc}  
   
  \begin{methoddesc}[TagMap]{getName}{\optional{tag=None}}  
 returns a the name assigned to \var{name}. If \var{tag} is not present  
 a list of all names is returned.  
 \end{methoddesc}  
   
   
 \begin{methoddesc}[TagMap]{getMapping}{}  
 returns a dictionary where the tags define the keys and the values the corresponding names.  
 \end{methoddesc}  
   
 \begin{methoddesc}[TagMap]{map}{\optional{default=0}, \optional{**kwargs}}  
 returns a dictionary where the keys are the tags and the values are the corresponding values assigned  
 to the tag via the keyword arguments \var{**kwargs}. The value of \var{default} is used for tags  
 which map onto name with unspecified values.  
   
 The following example demonstrate the usage:  
 \begin{python}  
 tm=TagMap(x=5)  
 tm.setMap(a=1,x=4,z=10)  
 print tm.map(default = "unknown", x="john",  a="peter")  
 \end{python}    
 The output is  
 \begin{python}  
 { 5 : "john", 4: "john", 1 : "peter", 10 : "unknown" }  
 \end{python}    
 \end{methoddesc}  
   
 \begin{methoddesc}[TagMap]{insert}{data,\optional{default=0, \optional{**kwargs}}}  
 inserts the values assigned to name via the keyword arguments \var{**kwargs}  
 into the \Data object \var{Data}. The value \var{default} is used for names with no given value.  
 \end{methoddesc}  
   
 \begin{methoddesc}[TagMap]{writeXML}{\optional{iostream=None}}  
 writes an XML serialization into the \var{iostream} or if not present returns the XML representation  
 as a string.  
 \end{methoddesc}  
   
 \begin{methoddesc}[TagMap]{fillFromXML}{iostream}  
 uses XML data \var{iostream} defining an iostream or string. This method is the  
 inverse method of \var{writeXML}.  
   
 The following example demonstrates the usage:  
 \begin{python}  
 tm=TagMap(x=5)  
 tm.setMap(a=1,x=4,z=10)  
 tm.writeXML(open("tag_map.xml", "w"))  
 tm2=TagMap()  
 tm2.fillFromXML(open("tag_map.xml", "r"))  
 \end{python}    
 \end{methoddesc}          
   
   
   
 \section{Interface to \gmshextern}  
277  \declaremodule{extension}{esys.pycad.gmsh}  \declaremodule{extension}{esys.pycad.gmsh}
278  \modulesynopsis{Python geometry description and meshing interface}  \modulesynopsis{Python geometry description and meshing interface}
279    
280    The class and methods described here provide an interface to the mesh
281    generation software, which is currently gmsh.  This interface could be
282    adopted to triangle or another mesh generation package if this is
283    deemed to be desirable in the future.
284    
285  \begin{classdesc}{Design}{  \begin{classdesc}{Design}{
286  \optional{dim=3, \optional{element_size=1., \optional{order=1, \optional{keep_files=False}}}}}  \optional{dim=3, \optional{element_size=1., \optional{order=1, \optional{keep_files=False}}}}}
287  The \class{Design} describes the geometry defined by primitives to be meshed.  The \class{Design} describes the geometry defined by primitives to be meshed.
# Line 253  returns the element order. Line 311  returns the element order.
311    
312  \begin{methoddesc}[Design]{setElementSize}{\optional{element_size=1}}  \begin{methoddesc}[Design]{setElementSize}{\optional{element_size=1}}
313  set the global element size. The local element size at a point is defined as  set the global element size. The local element size at a point is defined as
314  the global element size multipied by the local scale. The element size must be positive.  the global element size multiplied by the local scale. The element size must be positive.
315  \end{methoddesc}  \end{methoddesc}
316    
317    
# Line 262  returns the global element size. Line 320  returns the global element size.
320  \end{methoddesc}  \end{methoddesc}
321    
322  \begin{memberdesc}[Design]{DELAUNAY}  \begin{memberdesc}[Design]{DELAUNAY}
323  the \gmshextern Delauny triangulator.  the gmsh Delauny triangulator.
324  \end{memberdesc}  \end{memberdesc}
325    
326  \begin{memberdesc}[Design]{TETGEN}  \begin{memberdesc}[Design]{TETGEN}
# Line 286  returns \True if work files are kept. Ot Line 344  returns \True if work files are kept. Ot
344  \end{methoddesc}  \end{methoddesc}
345    
346  \begin{methoddesc}[Design]{setScriptFileName}{\optional{name=None}}  \begin{methoddesc}[Design]{setScriptFileName}{\optional{name=None}}
347  set the filename for the \gmshextern input script. if no name is given a name with extension "geo" is generated.  set the filename for the gmsh input script. if no name is given a name with extension "geo" is generated.
348  \end{methoddesc}  \end{methoddesc}
349    
350  \begin{methoddesc}[Design]{getScriptFileName}{}  \begin{methoddesc}[Design]{getScriptFileName}{}
351  returns the name of the file for the \gmshextern script.  returns the name of the file for the gmsh script.
352  \end{methoddesc}  \end{methoddesc}
353    
354    
355  \begin{methoddesc}[Design]{setMeshFileName}{\optional{name=None}}  \begin{methoddesc}[Design]{setMeshFileName}{\optional{name=None}}
356  sets the name for the \gmshextern  mesh file. if no name is given a name with extension "msh" is generated.  sets the name for the gmsh  mesh file. if no name is given a name with extension "msh" is generated.
357  \end{methoddesc}  \end{methoddesc}
358    
359  \begin{methoddesc}[Design]{getMeshFileName}{}  \begin{methoddesc}[Design]{getMeshFileName}{}
# Line 321  resets the items in design Line 379  resets the items in design
379    
380  \begin{methoddesc}[Design]{getMeshHandler}{}  \begin{methoddesc}[Design]{getMeshHandler}{}
381  returns a handle to the mesh. The call of this method generates the mesh from the geometry and  returns a handle to the mesh. The call of this method generates the mesh from the geometry and
382  returns a mechnism to access the mesh data. In the current implementation this  returns a mechanism to access the mesh data. In the current implementation this
383  is this method returns a file name for a \gmshextern file containing the mesh data but this may change in  method returns a file name for a gmsh file containing the mesh data.
 later versions.  
384  \end{methoddesc}  \end{methoddesc}
385    
386  \begin{methoddesc}[Design]{getScriptString}{}  \begin{methoddesc}[Design]{getScriptString}{}
387  returns the \gmshextern script to generate the mesh as string.  returns the gmsh script to generate the mesh as a string.
388  \end{methoddesc}  \end{methoddesc}
389    
390  \begin{methoddesc}[Design]{getCommandString}{}  \begin{methoddesc}[Design]{getCommandString}{}
391  returns the \gmshextern command used to generate the mesh as string..  returns the gmsh command used to generate the mesh as string.
392  \end{methoddesc}  \end{methoddesc}
393    
394  \begin{methoddesc}[Design]{setOptions}{\optional{algorithm=None, \optional{ optimize_quality=True,\optional{ smoothing=1}}}}  \begin{methoddesc}[Design]{setOptions}{\optional{algorithm=None, \optional{ optimize_quality=True,\optional{ smoothing=1}}}}
# Line 342  or \var{Design.NETGEN}. By default \var{ Line 399  or \var{Design.NETGEN}. By default \var{
399  \end{methoddesc}  \end{methoddesc}
400    
401  \begin{methoddesc}[Design]{getTagMap}{}  \begin{methoddesc}[Design]{getTagMap}{}
402  returns a \class{TagMap} to map the name \class{PropertySet} in the class to tag numbers generated by \gmshextern.  returns a \class{TagMap} to map the name \class{PropertySet} in the class to tag numbers generated by gmsh.
403  \end{methoddesc}  \end{methoddesc}

Legend:
Removed from v.999  
changed lines
  Added in v.1811

  ViewVC Help
Powered by ViewVC 1.1.26