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

Diff of /trunk/pycad/py_src/design.py

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

revision 931 by gross, Fri Jan 19 03:06:33 2007 UTC revision 932 by gross, Fri Jan 19 09:27:15 2007 UTC
# Line 1  Line 1 
1  # $Id:$  # $Id:$
2    
3  """  """
4  Geometrical Elementries  template for the Design which defines a regions and features
5    for a mesh generator.
 the concept is inspired by gmsh and very much focused on the fact that  
 the classes are used to wrk with gmsh.  
6    
7  @var __author__: name of author  @var __author__: name of author
8  @var __copyright__: copyrights  @var __copyright__: copyrights
# Line 16  the classes are used to wrk with gmsh. Line 14  the classes are used to wrk with gmsh.
14    
15    
16  __author__="Lutz Gross, l.gross@uq.edu.au"  __author__="Lutz Gross, l.gross@uq.edu.au"
17  __copyright__="""  Copyright (c) 2006 by ACcESS MNRF  __copyright__="""  Copyright (c) 2007 by ACcESS MNRF
18                      http://www.access.edu.au                      http://www.access.edu.au
19                  Primary Business: Queensland, Australia"""                  Primary Business: Queensland, Australia"""
20  __license__="""Licensed under the Open Software License version 3.0  __license__="""Licensed under the Open Software License version 3.0
# Line 25  __url__="http://www.iservo.edu.au/esys/e Line 23  __url__="http://www.iservo.edu.au/esys/e
23  __version__="$Revision:$"  __version__="$Revision:$"
24  __date__="$Date:$"  __date__="$Date:$"
25    
26  from primitives import Primitive #, PrimitiveStack  
27  from datetime import date  from primitives import Primitive, ReversePrimitive
 import tempfile  
 import os  
   
 class PrimitiveBaseStack(object):  
       def __init__(self,*items):  
         self.__prims=set()  
         for i in items:  
             self.__prims|=i.getPrimitives()  
         self.__prims=list(self.__prims)  
         self.__prims.sort()  
   
       def getGmshCommands(self,scaling_factor=1.):  
         out=""  
         for i in self.__prims:  
            out+=i.getGmshCommand(scaling_factor)+"\n"  
         return out  
28    
29  class Design(object):  class Design(object):
30      """      """
31      template for elementary geometrical object      template for a design which defines the input for a mesh generator
32      """      """
33      def __init__(self,dim=3,scale=1.,element_order=1,keep_tmp_files=False):      def __init__(self,dim=3,element_size=1.,order=1,keep_files=False):
34         """         """
35           initializes a design
36    
37           @param dim: patial dimension
38           @element_size: global element size
39           @order: element order
40           @keep_files: flag to keep work files.
41         """         """
42         self.__items=[]         self.clearItems()
43         self.setScale(scale)         self.setElementSize(element_size)
44         self.setDim(dim)         self.setDim(dim)
45         self.setElementOrder(element_order)         self.setElementOrder(order)
46         self.setKeepTmpFiles(keep_tmp_files)         if keep_files:
47      def addPrimitive(self,item):            self.setKeepFilesOn()
48          self.addPrimitives(item)         else:
49      def addPrimitives(self,*items):            self.setKeepFilesOff()
        for i in range(len(items)):  
           if not isinstance(items[i],Primitive):  
              raise TypeError("%s-th argument is not a Primitive object"%i)  
        for i in items:  
           self.__items.append(i)  
     def setKeepTmpFiles(self,flag=None):  
         if flag==None:  
            if self.__keep_tmp_files==True:  
               self.__keep_tmp_files=False  
            else:  
               self.__keep_tmp_files=True  
         else:  
               self.__keep_tmp_files=flag  
     def keepTmpFiles(self,scale=1.):  
         return self.__keep_tmp_files  
     def setScale(self,scale=1.):  
         self.__scale=scale  
     def getScale(self,scale=1.):  
         return self.__scale  
50      def setDim(self,dim=3):      def setDim(self,dim=3):
51            """
52            sets the spatial dimension
53            """
54            if not dim  in [1,2,3]:
55               raise ValueError("only dimension 1, 2, 3 are supported.")
56          self.__dim=dim          self.__dim=dim
57      def getDim(self,dim=3):      def getDim(self,dim=3):
58            """
59            returns the spatial dimension
60            """
61          return self.__dim          return self.__dim
62      def setElementOrder(self,order=1):      def setElementOrder(self,order=1):
63            """
64            sets the element order
65            """
66            if not order in [1,2]:
67               raise ValueError("only element orser 1 or 2 is supported.")
68          self.__order=order          self.__order=order
69      def getElementOrder(self,order=1):      def getElementOrder(self,order=1):
70            """
71            returns the element order
72            """
73          return self.__order          return self.__order
74      def getPrimitives(self):      def setElementSize(self,element_size=0.1):
75            """
76            set the global element size.
77            """
78            if element_size<=0.:
79               raise ValueError("element size needs to be non--negative.")
80            self.__element_size=element_size
81        def getElementSize(self,element_size=1.):
82            """
83            returns the global element size.
84            """
85            return self.__element_size
86        def setKeepFilesOn(self):
87            """
88            work files are kept at the end of the generation
89            """
90            self.__keep_files=True
91        def setKeepFilesOff(self):
92            """
93            work files are deleted at the end of the generation
94            """
95            self.__keep_files=False
96        def keepFiles(self):
97            """
98            returns True if work files are kept
99            """
100            return self.__keep_files
101        def addItems(self,*items):
102           """
103           adds items to the design
104           """
105           for i in range(len(items)):
106              if not isinstance(items[i],(Primitive, ReversePrimitive)):
107                 raise TypeError("%s-th argument is not a Primitive object"%i)
108           for i in items:
109              self.__items.append(i)
110        def getItems(self):
111            """
112            returns a list of the items used in the design
113            """
114          return self.__items          return self.__items
115      def getGmshScript(self):      def clearItems(self):
116          ps=PrimitiveStack(*tuple(self.getPrimitives()))          """
117          return "// generated by esys.pycad\nscale = %s;\n%s"%(self.getScale(),ps.getGmshCommands())          resets the items in design
118      def writeGmshScript(self,file):          """
119          file.write(self.getGmshScript())          self.__items=[]
120      def writeGmshMesh(self,filename):      def getAllPrimitives(self):
121          scriptname=tempfile.mkstemp(suffix=".geo")[1]          """
122          self.writeGmshScript(open(scriptname,"w"))          returns a list of all primitives used to create the design.
123          exe="gmsh -%s -smooth 2 -optimize -v 0 -order %s -o %s %s"%(self.getDim(),self.getElementOrder(),filename,scriptname)          each primitve appears once. The primitives are ordered by their
124          os.system(exe)          order of generation
125          if not self.keepTmpFiles(): os.unlink(scriptname)          """
126          return exe          prims=set()
127      def writeFinleyMesh(self,filename):          for i in self.getItems(): prims|=set(i.getPrimitives())
128          mshname=tempfile.mkstemp(suffix=".msh")[1]          prims=list(prims)
129          exe=self.writeGmshMesh(mshname)          prims.sort()
130          convertGmshToFinley(open(mshname,"r"),open(filename,"w"),dim=self.getDim())          return prims
131          if not self.keepTmpFiles(): os.unlink(mshname)  
132          return exe      def setOptions(self,**kwargs):
133            """
134  def convertGmshToFinley(gmsh_file,finley_file,dim=3):          sets options of the mesh generator
135          line=gmsh_file.readline().split()  
136          while len(line)>0:          @note: this method is typically overwritten by a particular Design implementation
137             print line          """
138             line=gmsh_file.readline().split()          pass
139        def getMeshHandler(self):
140            """
141            returns a handle to a mesh meshing the design
142    
143            @note: this method has to be overwritten by a particular Design implementation
144            """
145            raise NotImplementedError()
146    

Legend:
Removed from v.931  
changed lines
  Added in v.932

  ViewVC Help
Powered by ViewVC 1.1.26