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

1 ksteube 1809
2     ########################################################
3 ksteube 1312 #
4 ksteube 1809 # Copyright (c) 2003-2008 by University of Queensland
5     # Earth Systems Science Computational Center (ESSCC)
6     # http://www.uq.edu.au/esscc
7 ksteube 1312 #
8 ksteube 1809 # 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 ksteube 1312 #
12 ksteube 1809 ########################################################
13 gross 899
14 ksteube 1809 __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 gross 899 """
23 gross 932 template for the Design which defines a regions and features
24     for a mesh generator.
25 gross 899
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 gross 944 from primitives import Primitive, ReversePrimitive, PropertySet, Point, Manifold1D, Manifold2D, Manifold3D
37     from xml.dom import minidom
38 gross 931
39 gross 944 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 gross 1052 def __init__(self, mapping={}):
51 gross 944 """
52     initizlizes the mapping. map defines an initial mapping from tag to a name.
53     """
54 gross 1052 self.__mapping={}
55     for tag, name in mapping.items():
56 gross 944 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 gross 1052 self.__mapping[tag]=name
61 gross 944 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 gross 1052 self.__mapping[tag]=name
71 gross 944 def getTags(self,name=None):
72     """
73 gross 999 returns a list of the tags assigned to name. If name is not present a list of tags is returned.
74 gross 944 """
75     if name == None:
76 gross 1052 out=self.__mapping.keys()
77 gross 944 else:
78     out=[]
79 gross 1052 for tag, arg in self.__mapping.items():
80 gross 944 if arg == name: out.append(tag)
81     return out
82 gross 999 def getName(self,tag=None):
83 gross 944 """
84 gross 999 returns the name of a tag. If tag is not present a list of names is returned.
85 gross 944 """
86 gross 999 if tag == None:
87 gross 1052 return list(set(self.__mapping.values()))
88 gross 999 else:
89 gross 1052 return self.__mapping[tag]
90 gross 997
91 gross 944 def getMapping(self):
92     """
93     returns a dictionary where the tags define the keys and the values the corresposnding names.
94     """
95 gross 1052 return self.__mapping
96 gross 997
97 gross 944 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 gross 1052 for tag in self.__mapping:
111     if kwargs.has_key(self.__mapping[tag]):
112     out[tag]=kwargs[self.__mapping[tag]]
113 gross 944 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 gross 1044 def passToDomain(self,domain):
126     """
127     passes the tag map to L{esys.escript.Domain} domain.
128     """
129 gross 1052 for tag, name in self.__mapping.items():
130 gross 1044 domain.setTagMap(name,tag)
131    
132 gross 944 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 gross 999 def fillFromXML(self,iostream):
161 gross 944 """
162     uses the xml file or string to set the mapping
163     """
164 gross 999 if isinstance(iostream,str):
165     dom=minidom.parseString(iostream)
166 gross 944 else:
167 gross 999 dom=minidom.parse(iostream)
168 gross 944 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 gross 899 class Design(object):
191     """
192 gross 932 template for a design which defines the input for a mesh generator
193 gross 899 """
194 gross 932 def __init__(self,dim=3,element_size=1.,order=1,keep_files=False):
195 gross 899 """
196 gross 932 initializes a design
197    
198     @param dim: patial dimension
199 ksteube 990 @param element_size: global element size
200     @param order: element order
201     @param keep_files: flag to keep work files.
202 gross 899 """
203 gross 932 self.clearItems()
204     self.setElementSize(element_size)
205 gross 899 self.setDim(dim)
206 gross 932 self.setElementOrder(order)
207     if keep_files:
208     self.setKeepFilesOn()
209     else:
210     self.setKeepFilesOff()
211 gross 899 def setDim(self,dim=3):
212 gross 932 """
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 gross 899 self.__dim=dim
218     def getDim(self,dim=3):
219 gross 932 """
220     returns the spatial dimension
221     """
222 gross 899 return self.__dim
223     def setElementOrder(self,order=1):
224 gross 932 """
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 gross 899 self.__order=order
230 gross 999
231     def getElementOrder(self):
232 gross 932 """
233     returns the element order
234     """
235 gross 899 return self.__order
236 gross 999
237     def setElementSize(self,element_size=1.):
238 gross 932 """
239     set the global element size.
240     """
241     if element_size<=0.:
242 gross 999 raise ValueError("element size needs to be positive.")
243 gross 932 self.__element_size=element_size
244 gross 999
245     def getElementSize(self):
246 gross 932 """
247     returns the global element size.
248     """
249     return self.__element_size
250 gross 999
251 gross 932 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 gross 899 return self.__items
280 gross 932 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 gross 1021 prims=[]
292     for i in self.getItems():
293     for p in i.getPrimitives():
294     if not p in prims: prims.append(p)
295 gross 932 prims.sort()
296     return prims
297 gross 901
298 gross 932 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 gross 997 def getTagMap(self):
314 gross 944 """
315 gross 999 returns a L{TagMap} to map the names of L{PropertySet}s to tags
316 gross 944 """
317     m={}
318     for p in self.getAllPrimitives():
319 gross 997 if isinstance(p, PropertySet): m[ p.getTag() ] = p.getName()
320 gross 944 return TagMap(m)

  ViewVC Help
Powered by ViewVC 1.1.26