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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 2180 - (show annotations)
Thu Dec 18 00:30:25 2008 UTC (11 years, 6 months ago) by caltinay
File MIME type: text/x-python
File size: 10497 byte(s)
Assorted spelling, grammar, whitespace and copy/paste error fixes (Part 3).
With boost 1.37 all epydoc warnings are gone now.
This commit should be a no-op.

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

  ViewVC Help
Powered by ViewVC 1.1.26