/[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 1809 - (show annotations)
Thu Sep 25 06:43:44 2008 UTC (11 years ago) by ksteube
File MIME type: text/x-python
File size: 10324 byte(s)
Copyright updated in all python files

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 a 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 tm=TagMap({5 : x })
44 tm.setMap(a=1,x=4)
45 assert tm.getTags("a") == [ 1 ]
46 assert tm.getTags("x") == [ 5, 4 ]
47 assert tm.map(x=10., a=20.) == { 5 : 10, 4: 10, 1 : 20 }
48
49 """
50 def __init__(self, mapping={}):
51 """
52 initizlizes the mapping. map defines an initial mapping from tag to a name.
53 """
54 self.__mapping={}
55 for tag, name in mapping.items():
56 if not isinstance(tag, int):
57 raise TypeError("tag needs to be int")
58 if not isinstance(name, str):
59 raise TypeError("name needs to be a str.")
60 self.__mapping[tag]=name
61 def setMap(self,**kwargs):
62 """
63 set a new map where <name>=<tag> assigns the tag <tag> to name <name>. <tag> has to be integer.
64 If <tag> has been assigned to a name before the mapping will be overwritten. Otherwise a new
65 mapping <tag> -> <name> is set. Notice that a single name can be assigned to different tags.
66 """
67 for name, tag in kwargs.items():
68 if not isinstance(tag, int):
69 raise TypeError("tag needs to be int")
70 self.__mapping[tag]=name
71 def getTags(self,name=None):
72 """
73 returns a list of the tags assigned to name. If name is not present a list of tags is returned.
74 """
75 if name == None:
76 out=self.__mapping.keys()
77 else:
78 out=[]
79 for tag, arg in self.__mapping.items():
80 if arg == name: out.append(tag)
81 return out
82 def getName(self,tag=None):
83 """
84 returns the name of a tag. If tag is not present a list of names is returned.
85 """
86 if tag == None:
87 return list(set(self.__mapping.values()))
88 else:
89 return self.__mapping[tag]
90
91 def getMapping(self):
92 """
93 returns a dictionary where the tags define the keys and the values the corresposnding names.
94 """
95 return self.__mapping
96
97 def map(self,default=0,**kwargs):
98 """
99 returns a dictionary where the tags define the keys and the values give the values assigned to the tag via name
100 and kwargs:
101
102 tm=TagMap(x=5)
103 tm.setMap(a=1,x=4)
104 print tm.map(x=10., a=20.)
105 { 5 : 10, 4: 10, 1 : 20 }
106
107 the default is used for tags which map onto name with unspecified values
108 """
109 out={}
110 for tag in self.__mapping:
111 if kwargs.has_key(self.__mapping[tag]):
112 out[tag]=kwargs[self.__mapping[tag]]
113 else:
114 out[tag]=default
115 return out
116
117 def insert(self,data,default=0,**kwargs):
118 """
119 inserts values into the L{esys.escript.Data} object according to the given values assigned to the keywords.
120 the default is used for tags which map onto name with unspecified values
121 """
122 d=self.map(default=default,**kwargs)
123 for t,v in d.items():
124 data.setTaggedValue(t,v)
125 def passToDomain(self,domain):
126 """
127 passes the tag map to L{esys.escript.Domain} domain.
128 """
129 for tag, name in self.__mapping.items():
130 domain.setTagMap(name,tag)
131
132 def toDOM(self,dom):
133 """
134 adds object to dom
135 """
136 tm=dom.createElement("TagMap")
137 dom.appendChild(tm)
138 for tag,name in self.getMapping().items():
139 item_dom=dom.createElement("map")
140 tag_dom=dom.createElement("tag")
141 name_dom=dom.createElement("name")
142 tag_dom.appendChild(dom.createTextNode(str(tag)))
143 name_dom.appendChild(dom.createTextNode(str(name)))
144 item_dom.appendChild(tag_dom)
145 item_dom.appendChild(name_dom)
146 tm.appendChild(item_dom)
147 return tm
148 def fromDom(self,node):
149 """
150 fills from dom node
151 """
152 for node in node.childNodes:
153 if isinstance(node, minidom.Element):
154 if node.tagName == 'map':
155 tag=int(node.getElementsByTagName("tag")[0].firstChild.nodeValue.strip())
156 name=str(node.getElementsByTagName("name")[0].firstChild.nodeValue.strip())
157 self.setMap(**{ name : tag })
158 return
159
160 def fillFromXML(self,iostream):
161 """
162 uses the xml file or string to set the mapping
163 """
164 if isinstance(iostream,str):
165 dom=minidom.parseString(iostream)
166 else:
167 dom=minidom.parse(iostream)
168 root=dom.getElementsByTagName('ESys')[0]
169 for node in root.childNodes:
170 if isinstance(node, minidom.Element):
171 if node.tagName == 'TagMap':
172 self.fromDom(node)
173 return
174
175
176
177 def writeXML(self,iostream=None):
178 """
179 writes XML serialization into the iostream or if not present returns the XML as string
180 """
181 dom=minidom.Document()
182 esys=dom.createElement('ESys')
183 esys.appendChild(self.toDOM(dom))
184 dom.appendChild(esys)
185 if iostream == None:
186 return dom.toprettyxml()
187 else:
188 iostream.write(dom.toprettyxml())
189
190 class Design(object):
191 """
192 template for a design which defines the input for a mesh generator
193 """
194 def __init__(self,dim=3,element_size=1.,order=1,keep_files=False):
195 """
196 initializes a design
197
198 @param dim: patial dimension
199 @param element_size: global element size
200 @param order: element order
201 @param keep_files: flag to keep work files.
202 """
203 self.clearItems()
204 self.setElementSize(element_size)
205 self.setDim(dim)
206 self.setElementOrder(order)
207 if keep_files:
208 self.setKeepFilesOn()
209 else:
210 self.setKeepFilesOff()
211 def setDim(self,dim=3):
212 """
213 sets the spatial dimension
214 """
215 if not dim in [1,2,3]:
216 raise ValueError("only dimension 1, 2, 3 are supported.")
217 self.__dim=dim
218 def getDim(self,dim=3):
219 """
220 returns the spatial dimension
221 """
222 return self.__dim
223 def setElementOrder(self,order=1):
224 """
225 sets the element order
226 """
227 if not order in [1,2]:
228 raise ValueError("only element orser 1 or 2 is supported.")
229 self.__order=order
230
231 def getElementOrder(self):
232 """
233 returns the element order
234 """
235 return self.__order
236
237 def setElementSize(self,element_size=1.):
238 """
239 set the global element size.
240 """
241 if element_size<=0.:
242 raise ValueError("element size needs to be positive.")
243 self.__element_size=element_size
244
245 def getElementSize(self):
246 """
247 returns the global element size.
248 """
249 return self.__element_size
250
251 def setKeepFilesOn(self):
252 """
253 work files are kept at the end of the generation
254 """
255 self.__keep_files=True
256 def setKeepFilesOff(self):
257 """
258 work files are deleted at the end of the generation
259 """
260 self.__keep_files=False
261 def keepFiles(self):
262 """
263 returns True if work files are kept
264 """
265 return self.__keep_files
266 def addItems(self,*items):
267 """
268 adds items to the design
269 """
270 for i in range(len(items)):
271 if not isinstance(items[i],(Primitive, ReversePrimitive)):
272 raise TypeError("%s-th argument is not a Primitive object"%i)
273 for i in items:
274 self.__items.append(i)
275 def getItems(self):
276 """
277 returns a list of the items used in the design
278 """
279 return self.__items
280 def clearItems(self):
281 """
282 resets the items in design
283 """
284 self.__items=[]
285 def getAllPrimitives(self):
286 """
287 returns a list of all primitives used to create the design.
288 each primitve appears once. The primitives are ordered by their
289 order of generation
290 """
291 prims=[]
292 for i in self.getItems():
293 for p in i.getPrimitives():
294 if not p in prims: prims.append(p)
295 prims.sort()
296 return prims
297
298 def setOptions(self,**kwargs):
299 """
300 sets options of the mesh generator
301
302 @note: this method is typically overwritten by a particular Design implementation
303 """
304 pass
305 def getMeshHandler(self):
306 """
307 returns a handle to a mesh meshing the design
308
309 @note: this method has to be overwritten by a particular Design implementation
310 """
311 raise NotImplementedError()
312
313 def getTagMap(self):
314 """
315 returns a L{TagMap} to map the names of L{PropertySet}s to tags
316 """
317 m={}
318 for p in self.getAllPrimitives():
319 if isinstance(p, PropertySet): m[ p.getTag() ] = p.getName()
320 return TagMap(m)

  ViewVC Help
Powered by ViewVC 1.1.26