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

  ViewVC Help
Powered by ViewVC 1.1.26