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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1044 - (hide annotations)
Mon Mar 19 07:29:31 2007 UTC (12 years, 11 months ago) by gross
File MIME type: text/x-python
File size: 9865 byte(s)
clear name tagging is supported now.
1 gross 899 # $Id:$
2    
3     """
4 gross 932 template for the Design which defines a regions and features
5     for a mesh generator.
6 gross 899
7     @var __author__: name of author
8     @var __copyright__: copyrights
9     @var __license__: licence agreement
10     @var __url__: url entry point on documentation
11     @var __version__: version
12     @var __date__: date of the version
13     """
14    
15    
16     __author__="Lutz Gross, l.gross@uq.edu.au"
17 gross 932 __copyright__=""" Copyright (c) 2007 by ACcESS MNRF
18 gross 899 http://www.access.edu.au
19     Primary Business: Queensland, Australia"""
20     __license__="""Licensed under the Open Software License version 3.0
21     http://www.opensource.org/licenses/osl-3.0.php"""
22     __url__="http://www.iservo.edu.au/esys/escript"
23     __version__="$Revision:$"
24     __date__="$Date:$"
25    
26    
27 gross 944 from primitives import Primitive, ReversePrimitive, PropertySet, Point, Manifold1D, Manifold2D, Manifold3D
28     from xml.dom import minidom
29 gross 931
30 gross 944 class TagMap(object):
31     """
32     a class that allows to map tags to names
33    
34     tm=TagMap({5 : x })
35     tm.setMap(a=1,x=4)
36     assert tm.getTags("a") == [ 1 ]
37     assert tm.getTags("x") == [ 5, 4 ]
38     assert tm.map(x=10., a=20.) == { 5 : 10, 4: 10, 1 : 20 }
39    
40     """
41     def __init__(self, map={}):
42     """
43     initizlizes the mapping. map defines an initial mapping from tag to a name.
44     """
45     for tag, name in map.items():
46     if not isinstance(tag, int):
47     raise TypeError("tag needs to be int")
48     if not isinstance(name, str):
49     raise TypeError("name needs to be a str.")
50     self.__map=map
51     def setMap(self,**kwargs):
52     """
53     set a new map where <name>=<tag> assigns the tag <tag> to name <name>. <tag> has to be integer.
54     If <tag> has been assigned to a name before the mapping will be overwritten. Otherwise a new
55     mapping <tag> -> <name> is set. Notice that a single name can be assigned to different tags.
56     """
57     for name, tag in kwargs.items():
58     if not isinstance(tag, int):
59     raise TypeError("tag needs to be int")
60     self.__map[tag]=name
61     def getTags(self,name=None):
62     """
63 gross 999 returns a list of the tags assigned to name. If name is not present a list of tags is returned.
64 gross 944 """
65     if name == None:
66     out=self.__map.keys()
67     else:
68     out=[]
69     for tag, arg in self.__map.items():
70     if arg == name: out.append(tag)
71     return out
72 gross 999 def getName(self,tag=None):
73 gross 944 """
74 gross 999 returns the name of a tag. If tag is not present a list of names is returned.
75 gross 944 """
76 gross 999 if tag == None:
77     return list(set(self.__map.values()))
78     else:
79     return self.__map[tag]
80 gross 997
81 gross 944 def getMapping(self):
82     """
83     returns a dictionary where the tags define the keys and the values the corresposnding names.
84     """
85     return self.__map
86 gross 997
87 gross 944 def map(self,default=0,**kwargs):
88     """
89     returns a dictionary where the tags define the keys and the values give the values assigned to the tag via name
90     and kwargs:
91    
92     tm=TagMap(x=5)
93     tm.setMap(a=1,x=4)
94     print tm.map(x=10., a=20.)
95     { 5 : 10, 4: 10, 1 : 20 }
96    
97     the default is used for tags which map onto name with unspecified values
98     """
99     out={}
100     for tag in self.__map:
101     if kwargs.has_key(self.__map[tag]):
102     out[tag]=kwargs[self.__map[tag]]
103     else:
104     out[tag]=default
105     return out
106    
107     def insert(self,data,default=0,**kwargs):
108     """
109     inserts values into the L{esys.escript.Data} object according to the given values assigned to the keywords.
110     the default is used for tags which map onto name with unspecified values
111     """
112     d=self.map(default=default,**kwargs)
113     for t,v in d.items():
114     data.setTaggedValue(t,v)
115 gross 1044 def passToDomain(self,domain):
116     """
117     passes the tag map to L{esys.escript.Domain} domain.
118     """
119     for tag, name in self.__map.items():
120     domain.setTagMap(name,tag)
121    
122 gross 944 def toDOM(self,dom):
123     """
124     adds object to dom
125     """
126     tm=dom.createElement("TagMap")
127     dom.appendChild(tm)
128     for tag,name in self.getMapping().items():
129     item_dom=dom.createElement("map")
130     tag_dom=dom.createElement("tag")
131     name_dom=dom.createElement("name")
132     tag_dom.appendChild(dom.createTextNode(str(tag)))
133     name_dom.appendChild(dom.createTextNode(str(name)))
134     item_dom.appendChild(tag_dom)
135     item_dom.appendChild(name_dom)
136     tm.appendChild(item_dom)
137     return tm
138     def fromDom(self,node):
139     """
140     fills from dom node
141     """
142     for node in node.childNodes:
143     if isinstance(node, minidom.Element):
144     if node.tagName == 'map':
145     tag=int(node.getElementsByTagName("tag")[0].firstChild.nodeValue.strip())
146     name=str(node.getElementsByTagName("name")[0].firstChild.nodeValue.strip())
147     self.setMap(**{ name : tag })
148     return
149    
150 gross 999 def fillFromXML(self,iostream):
151 gross 944 """
152     uses the xml file or string to set the mapping
153     """
154 gross 999 if isinstance(iostream,str):
155     dom=minidom.parseString(iostream)
156 gross 944 else:
157 gross 999 dom=minidom.parse(iostream)
158 gross 944 root=dom.getElementsByTagName('ESys')[0]
159     for node in root.childNodes:
160     if isinstance(node, minidom.Element):
161     if node.tagName == 'TagMap':
162     self.fromDom(node)
163     return
164    
165    
166    
167     def writeXML(self,iostream=None):
168     """
169     writes XML serialization into the iostream or if not present returns the XML as string
170     """
171     dom=minidom.Document()
172     esys=dom.createElement('ESys')
173     esys.appendChild(self.toDOM(dom))
174     dom.appendChild(esys)
175     if iostream == None:
176     return dom.toprettyxml()
177     else:
178     iostream.write(dom.toprettyxml())
179    
180 gross 899 class Design(object):
181     """
182 gross 932 template for a design which defines the input for a mesh generator
183 gross 899 """
184 gross 932 def __init__(self,dim=3,element_size=1.,order=1,keep_files=False):
185 gross 899 """
186 gross 932 initializes a design
187    
188     @param dim: patial dimension
189 ksteube 990 @param element_size: global element size
190     @param order: element order
191     @param keep_files: flag to keep work files.
192 gross 899 """
193 gross 932 self.clearItems()
194     self.setElementSize(element_size)
195 gross 899 self.setDim(dim)
196 gross 932 self.setElementOrder(order)
197     if keep_files:
198     self.setKeepFilesOn()
199     else:
200     self.setKeepFilesOff()
201 gross 899 def setDim(self,dim=3):
202 gross 932 """
203     sets the spatial dimension
204     """
205     if not dim in [1,2,3]:
206     raise ValueError("only dimension 1, 2, 3 are supported.")
207 gross 899 self.__dim=dim
208     def getDim(self,dim=3):
209 gross 932 """
210     returns the spatial dimension
211     """
212 gross 899 return self.__dim
213     def setElementOrder(self,order=1):
214 gross 932 """
215     sets the element order
216     """
217     if not order in [1,2]:
218     raise ValueError("only element orser 1 or 2 is supported.")
219 gross 899 self.__order=order
220 gross 999
221     def getElementOrder(self):
222 gross 932 """
223     returns the element order
224     """
225 gross 899 return self.__order
226 gross 999
227     def setElementSize(self,element_size=1.):
228 gross 932 """
229     set the global element size.
230     """
231     if element_size<=0.:
232 gross 999 raise ValueError("element size needs to be positive.")
233 gross 932 self.__element_size=element_size
234 gross 999
235     def getElementSize(self):
236 gross 932 """
237     returns the global element size.
238     """
239     return self.__element_size
240 gross 999
241 gross 932 def setKeepFilesOn(self):
242     """
243     work files are kept at the end of the generation
244     """
245     self.__keep_files=True
246     def setKeepFilesOff(self):
247     """
248     work files are deleted at the end of the generation
249     """
250     self.__keep_files=False
251     def keepFiles(self):
252     """
253     returns True if work files are kept
254     """
255     return self.__keep_files
256     def addItems(self,*items):
257     """
258     adds items to the design
259     """
260     for i in range(len(items)):
261     if not isinstance(items[i],(Primitive, ReversePrimitive)):
262     raise TypeError("%s-th argument is not a Primitive object"%i)
263     for i in items:
264     self.__items.append(i)
265     def getItems(self):
266     """
267     returns a list of the items used in the design
268     """
269 gross 899 return self.__items
270 gross 932 def clearItems(self):
271     """
272     resets the items in design
273     """
274     self.__items=[]
275     def getAllPrimitives(self):
276     """
277     returns a list of all primitives used to create the design.
278     each primitve appears once. The primitives are ordered by their
279     order of generation
280     """
281 gross 1021 prims=[]
282     for i in self.getItems():
283     for p in i.getPrimitives():
284     if not p in prims: prims.append(p)
285 gross 932 prims.sort()
286     return prims
287 gross 901
288 gross 932 def setOptions(self,**kwargs):
289     """
290     sets options of the mesh generator
291    
292     @note: this method is typically overwritten by a particular Design implementation
293     """
294     pass
295     def getMeshHandler(self):
296     """
297     returns a handle to a mesh meshing the design
298    
299     @note: this method has to be overwritten by a particular Design implementation
300     """
301     raise NotImplementedError()
302    
303 gross 997 def getTagMap(self):
304 gross 944 """
305 gross 999 returns a L{TagMap} to map the names of L{PropertySet}s to tags
306 gross 944 """
307     m={}
308     for p in self.getAllPrimitives():
309 gross 997 if isinstance(p, PropertySet): m[ p.getTag() ] = p.getName()
310 gross 944 return TagMap(m)

  ViewVC Help
Powered by ViewVC 1.1.26