/[escript]/trunk/modellib/py_src/geometry.py
ViewVC logotype

Contents of /trunk/modellib/py_src/geometry.py

Parent Directory Parent Directory | Revision Log Revision Log


Revision 993 - (show annotations)
Fri Feb 23 06:39:38 2007 UTC (13 years, 1 month ago) by gross
File MIME type: text/x-python
File size: 22226 byte(s)
problem with undefined value fixed.
1 # $Id$
2
3 __copyright__=""" Copyright (c) 2006 by ACcESS MNRF
4 http://www.access.edu.au
5 Primary Business: Queensland, Australia"""
6 __license__="""Licensed under the Open Software License version 3.0
7 http://www.opensource.org/licenses/osl-3.0.php"""
8
9
10 from esys.escript import *
11 from esys.escript.modelframe import Model,ParameterSet
12 from esys.pycad import TagMap
13 from esys import finley
14
15 class FinleyReader(ParameterSet):
16 """
17 reads finley mesh file.
18
19 @ivar source: mesh file in finley or gmsh format
20 @type source: C{DataSource}
21 @ivar intergrationOrder: integration order, default -1 (in).
22 @type intergrationOrder: C{int}
23 @ivar reducedIntegrationOrder: reduced integration order, default -1 (in).
24 @type reducedIntegrationOrder: C{int}
25 @ivar optimizeLabeling: switches on optimization of the labeling of the nodes
26 @type optimizeLabeling: C{bool}
27 """
28 def __init__(self,**kwargs):
29 """
30 initializes the object
31 """
32 super(FinleyReader,self).__init__(**kwargs)
33 self.declareParameter(source="none",
34 region_tag_map_source=None,
35 surface_tag_map_source=None,
36 optimizeLabeling=True,
37 reducedIntegrationOrder=-1,
38 integrationOrder=-1)
39 self.__domain=None
40 self.__region_tag_map=None
41 self.__surface_tag_map=None
42
43
44 def domain(self):
45 """
46 returns the domain
47
48 @return: the domain
49 @rtype: L{Domain}
50 """
51 if self.__domain == None:
52 if self.source.fileformat == "fly":
53 self.__domain=finley.ReadMesh(self.source.getLocalFileName(),self.integrationOrder)
54 elif self.source.fileformat == "gmsh":
55 self.__domain=finley.ReadGmsh(self.source.getLocalFileName(),self.integrationOrder,self.reducedIntegrationOrder, self.optimizeLabeling)
56 else:
57 raise TypeError("unknown mesh file format %s."%self.source.fileformat)
58 self.trace("mesh read from %s in %s format."%(self.source.getLocalFileName(), self.source.fileformat))
59 return self.__domain
60
61 def region_tag_map(self):
62 """
63 returns the map from regional tag names to tag integers used in the mesh
64
65 @return: the tag map
66 @rtype: L{TagMap}
67 """
68 if self.__region_tag_map == None:
69 self.__region_tag_map = TagMap()
70 if self.region_tag_map_source != None:
71 self.__region_tag_map.fillFromXML(open(self.region_tag_map_source.getLocalFileName()))
72 self.trace("region tag map read from %s in %s format."%(self.region_tag_map_source.getLocalFileName(), self.region_tag_map_source.fileformat))
73 return self.__region_tag_map
74
75 def surface_tag_map(self):
76 """
77 returns the map from surface tag names to tag integers used in the mesh
78
79 @return: the tag map
80 @rtype: L{TagMap}
81 """
82 if self.__surface_tag_map == None:
83 self.__surface_tag_map = TagMap()
84 if self.surface_tag_map_source != None:
85 self.__surface_tag_map.fillFromXML(open(self.surface_tag_map_source.getLocalFileName()))
86 self.trace("surface tag map read from %s in %s format."%(self.surface_tag_map_source.getLocalFileName(), self.surface_tag_map_source.fileformat))
87 return self.__surface_tag_map
88
89
90 class RectangularDomain(ParameterSet):
91 """
92 Generates a mesh over a rectangular domain finley.
93
94 @ivar dim: spatial dimension, default =2 (in).
95 @type dim: spatial dimension
96 @ivar l: spatial lengths, default [1.,1.,1.] (in).
97 @type l: C{list} of C{floats}s
98 @ivar n: number of elements, default [10,10,10] (in).
99 @type n: C{list} of C{int}s
100 @ivar order: element order, default 1 (in).
101 @type order: C{int}
102 @ivar periodic: flags for periodicity, default [False,False,False] (in).
103 @type periodic: C{list} of C{bool}s
104 @ivar intergrationOrder: integration order, default -1 (in).
105 @type intergrationOrder: C{int}
106 """
107 def __init__(self,**kwargs):
108 """
109 initializes the object
110 """
111 super(RectangularDomain,self).__init__(**kwargs)
112 self.declareParameter(dim=2,\
113 l=[1.,1.,1.],\
114 n=[10,10,10], \
115 order=1,\
116 periodic=[False,False,False],
117 integrationOrder=-1)
118 self.__domain=None
119
120 def domain(self):
121 """
122 returns the domain
123
124 @return: the domain
125 @rtype: L{Domain}
126 """
127 if self.__domain==None:
128 if self.dim==2:
129 self.__domain=finley.Rectangle(n0=self.n[0],\
130 n1=self.n[1],\
131 l0=self.l[0],\
132 l1=self.l[1],\
133 order=self.order, \
134 periodic0=self.periodic[0], \
135 periodic1=self.periodic[1], \
136 integrationOrder=self.integrationOrder)
137 else:
138 self.__domain=finley.Brick(n0=self.n[0],\
139 n1=self.n[1],\
140 n2=self.n[2],\
141 l0=self.l[0],\
142 l1=self.l[1],\
143 l2=self.l[2],\
144 order=self.order, \
145 periodic0=self.periodic[0], \
146 periodic1=self.periodic[1], \
147 periodic2=self.periodic[2], \
148 integrationOrder=self.integrationOrder)
149 return self.__domain
150
151 class UpdateGeometry(Model):
152 """
153 applies a displacement field to a domain
154
155 @ivar displacement: displacements applied to the original mesh coordinates (in).
156 @type displacement: L{escript.Vector}
157 @ivar domain: domain
158 @type domain: L{escript.Domain}
159 """
160 def __init__(self,**kwargs):
161 """
162 set-up the object
163 """
164 super(UpdateGeometry, self).__init__(**kwargs)
165 self.declareParameter(domain=None,\
166 displacement=None)
167
168
169 def doInitialization(self):
170 """
171 initialize model
172 """
173 self.__x=self.domain.getX()
174 self.__reset=True
175
176 def doStepPreprocessing(self,dt):
177 """
178 applies the current L{displacement} to mesh nodes if required.
179 """
180 if self.__reset:
181 self.trace("mesh nodes updated.")
182 self.domain.setX(self.__x+self.displacement)
183 self.__reset=False
184
185 def doStep(self,dt):
186 """
187 applies the current L{displacement} to mesh nodes.
188 """
189 self.trace("mesh nodes updated.")
190 self.domain.setX(self.__x+self.displacement)
191 self.__reset=True
192
193 def doStepPostprocessing(self,dt):
194 """
195 marks nodes as beeing updated.
196 """
197 self.__reset=False
198
199 class ConstrainerOverBox(Model):
200 """
201 Creates a characteristic function for the location of constraints
202 for all components of a value and selects the value from an initial value
203 ate these locations.
204
205 In the case that the spatial dimension is two, the arguments front and back are ignored.
206
207 @ivar domain: domain (in).
208 @ivar left: True to set a constraint at the left face of the domain (x[0]=min x[0]), default False (in).
209 @ivar right: True to set a constraint at the left face of the domain (x[0]=max x[0]), default False (in).
210 @ivar top: True to set a constraint at the left face of the domain (x[1]=min x[1]), default False (in).
211 @ivar bottom: True to set a constraint at the left face of the domain (x[1]=max x[1]), default False (in).
212 @ivar front: True to set a constraint at the left face of the domain (x[2]=min x[2]), default False (in).
213 @ivar back: True to set a constraint at the left face of the domain (x[2]=max x[2]), default False (in).
214 @ivar tol: absolute tolerance for "x=max x" condition, default 1.e-8 (in).
215 """
216 def __init__(self,**kwargs):
217 super(ConstrainerOverBox, self).__init__(**kwargs)
218 self.declareParameter(domain=None, \
219 value=None, \
220 left=False, \
221 right=False, \
222 top=False, \
223 bottom=False, \
224 front=False, \
225 back=False, \
226 tol=1.e-8)
227 self.__value_of_constraint = None
228 self.__location_of_constraint=None
229 def location_of_constraint(self):
230 """
231 return the values used to constrain a solution
232
233 @return: the mask marking the locations of the constraints
234 @rtype: L{escript.Scalar}
235 """
236 if self.__location_of_constraint == None: self.__setOutput()
237 return self.__location_of_constraint
238
239 def value_of_constraint(self):
240 """
241 return the values used to constrain a solution
242
243 @return: values to be used at the locations of the constraints. If
244 L{value} is not given C{None} is rerturned.
245 @rtype: L{escript.Scalar}
246 """
247 if self.__location_of_constraint == None: self.__setOutput()
248 return self.__value_of_constraint
249
250 def __setOutput(self):
251 if self.__location_of_constraint == None:
252 x=self.domain.getX()
253 val=self.value
254 if isinstance(val, int) or isinstance(val, float):
255 shape=()
256 elif isinstance(val, list) or isinstance(val, tuple) :
257 shape=(len(val),)
258 elif isinstance(val, numarray.NumArray):
259 shape=val.shape
260 elif val == None:
261 shape=()
262 else:
263 shape=val.getShape()
264 self.__location_of_constraint=Data(0,shape,x.getFunctionSpace())
265 if self.domain.getDim()==3:
266 x0,x1,x2=x[0],x[1],x[2]
267 if self.left: self.__location_of_constraint+=whereZero(x0-inf(x0),self.tol)
268 if self.right: self.__location_of_constraint+=whereZero(x0-sup(x0),self.tol)
269 if self.front: self.__location_of_constraint+=whereZero(x1-inf(x1),self.tol)
270 if self.back: self.__location_of_constraint+=whereZero(x1-sup(x1),self.tol)
271 if self.bottom: self.__location_of_constraint+=whereZero(x2-inf(x2),self.tol)
272 if self.top: self.__location_of_constraint+=whereZero(x2-sup(x2),self.tol)
273 else:
274 x0,x1=x[0],x[1]
275 if self.left: self.__location_of_constraint+=whereZero(x0-inf(x0),self.tol)
276 if self.right: self.__location_of_constraint+=whereZero(x0-sup(x0),self.tol)
277 if self.bottom: self.__location_of_constraint+=whereZero(x1-inf(x1),self.tol)
278 if self.top: self.__location_of_constraint+=whereZero(x1-sup(x1),self.tol)
279 if not self.value == None:
280 self.__value_of_constraint=self.__location_of_constraint*self.value
281 class ScalarConstrainerOverBox(Model):
282 """
283 Creates a characteristic function for the location of constraints
284 for a scalar value and selects the value from an initial value
285 ate these locations.
286
287 In the case that the spatial dimension is two, the arguments front and back are ignored.
288
289 @ivar domain: domain (in).
290 @ivar left: True to set a constraint at the left face of the domain (x[0]=min x[0]), default False (in).
291 @ivar right: True to set a constraint at the left face of the domain (x[0]=max x[0]), default False (in).
292 @ivar top: True to set a constraint at the left face of the domain (x[1]=min x[1]), default False (in).
293 @ivar bottom: True to set a constraint at the left face of the domain (x[1]=max x[1]), default False (in).
294 @ivar front: True to set a constraint at the left face of the domain (x[2]=min x[2]), default False (in).
295 @ivar back: True to set a constraint at the left face of the domain (x[2]=max x[2]), default False (in).
296 @ivar tol: absolute tolerance for "x=max x" condition, default 1.e-8 (in).
297 """
298 def __init__(self,**kwargs):
299 super(ScalarConstrainerOverBox, self).__init__(**kwargs)
300 self.declareParameter(domain=None, \
301 value=None, \
302 left=False, \
303 right=False, \
304 top=False, \
305 bottom=False, \
306 front=False, \
307 back=False, \
308 tol=1.e-8)
309 self.__value_of_constraint = None
310 self.__location_of_constraint=None
311 def location_of_constraint(self):
312 """
313 return the values used to constrain a solution
314
315 @return: the mask marking the locations of the constraints
316 @rtype: L{escript.Scalar}
317 """
318 if self.__location_of_constraint == None: self.__setOutput()
319 return self.__location_of_constraint
320
321 def value_of_constraint(self):
322 """
323 return the values used to constrain a solution
324
325 @return: values to be used at the locations of the constraints. If
326 L{value} is not given C{None} is rerturned.
327 @rtype: L{escript.Scalar}
328 """
329 if self.__location_of_constraint == None: self.__setOutput()
330 return self.__value_of_constraint
331
332 def __setOutput(self):
333 x=self.domain.getX()
334 self.__location_of_constraint=Scalar(0,x.getFunctionSpace())
335 if self.domain.getDim()==3:
336 x0,x1,x2=x[0],x[1],x[2]
337 if self.left: self.__location_of_constraint+=whereZero(x0-inf(x0),self.tol)
338 if self.right: self.__location_of_constraint+=whereZero(x0-sup(x0),self.tol)
339 if self.front: self.__location_of_constraint+=whereZero(x1-inf(x1),self.tol)
340 if self.back: self.__location_of_constraint+=whereZero(x1-sup(x1),self.tol)
341 if self.bottom: self.__location_of_constraint+=whereZero(x2-inf(x2),self.tol)
342 if self.top: self.__location_of_constraint+=whereZero(x2-sup(x2),self.tol)
343 else:
344 x0,x1=x[0],x[1]
345 if self.left: self.__location_of_constraint+=whereZero(x0-inf(x0),self.tol)
346 if self.right: self.__location_of_constraint+=whereZero(x0-sup(x0),self.tol)
347 if self.bottom: self.__location_of_constraint+=whereZero(x1-inf(x1),self.tol)
348 if self.top: self.__location_of_constraint+=whereZero(x1-sup(x1),self.tol)
349 if not self.value == None:
350 self.__value_of_constraint=self.__location_of_constraint*self.value
351
352 class VectorConstrainerOverBox(Model):
353 """
354 Creates a characteristic function for the location of constraints vector value.
355 In the case that the spatial dimension is two, the arguments front and
356 back as well as the third component of each argument is ignored.
357
358 @ivar domain: domain
359 @ivar left: list of three boolean. left[i]==True sets a constraint for the i-th component at the left face of the domain (x[0]=min x[0]),
360 default [False,False,False] (in).
361 @ivar right: list of three boolean. left[i]==True sets a constraint for the i-th component at the right face of the domain (x[0]=max x[0]),
362 default [False,False,False] (in).
363 @ivar top: list of three boolean. left[i]==True sets a constraint for the i-th component at the top face of the domain (x[1]=min x[1]),
364 default [False,False,False] (in).
365 @ivar bottom: list of three boolean. left[i]==True sets a constraint for the i-th component at the bottom face of the domain (x[1]=min x[1]),
366 default [False,False,False] (in).
367 @ivar front: list of three boolean. left[i]==True sets a constraint for the i-th component at the front face of the domain (x[2]=min x[2]),
368 default [False,False,False] (in).
369 @ivar back: list of three boolean. left[i]==True sets a constraint for the i-th component at the back face of the domain (x[2]=max x[2]),
370 default [False,False,False] (in).
371 @ivar tol: absolute tolerance for "x=max x" condition, default 1.e-8 (in).
372 """
373 def __init__(self, **kwargs):
374 super(VectorConstrainerOverBox, self).__init__(**kwargs)
375 self.declareParameter(domain=None, \
376 value=None, \
377 left=[0,0,0], \
378 right=[0,0,0], \
379 top=[0,0,0], \
380 bottom=[0,0,0], \
381 front=[0,0,0], \
382 back=[0,0,0], \
383 tol=1.e-8)
384 self.__value_of_constraint = None
385 self.__location_of_constraint=None
386
387 def location_of_constraint(self):
388 """
389 return the values used to constrain a solution
390
391 @return: the mask marking the locations of the constraints
392 @rtype: L{escript.Vector}
393 """
394 if self.__location_of_constraint == None: self.__setOutput()
395 return self.__location_of_constraint
396
397 def value_of_constraint(self):
398 """
399 return the values used to constrain a solution
400
401 @return: values to be used at the locations of the constraints. If
402 L{value} is not given C{None} is rerturned.
403 @rtype: L{escript.Vector}
404 """
405 if self.__location_of_constraint == None: self.__setOutput()
406 return self.__value_of_constraint
407
408 def __setOutput(self):
409 x=self.domain.getX()
410 self.__location_of_constraint=Vector(0,x.getFunctionSpace())
411 if self.domain.getDim()==3:
412 x0,x1,x2=x[0],x[1],x[2]
413 left_mask=whereZero(x0-inf(x0),self.tol)
414 if self.left[0]: self.__location_of_constraint+=left_mask*[1.,0.,0.]
415 if self.left[1]: self.__location_of_constraint+=left_mask*[0.,1.,0.]
416 if self.left[2]: self.__location_of_constraint+=left_mask*[0.,0.,1.]
417 right_mask=whereZero(x0-sup(x0),self.tol)
418 if self.right[0]: self.__location_of_constraint+=right_mask*[1.,0.,0.]
419 if self.right[1]: self.__location_of_constraint+=right_mask*[0.,1.,0.]
420 if self.right[2]: self.__location_of_constraint+=right_mask*[0.,0.,1.]
421 front_mask=whereZero(x1-inf(x1),self.tol)
422 if self.front[0]: self.__location_of_constraint+=front_mask*[1.,0.,0.]
423 if self.front[1]: self.__location_of_constraint+=front_mask*[0.,1.,0.]
424 if self.front[2]: self.__location_of_constraint+=front_mask*[0.,0.,1.]
425 back_mask=whereZero(x1-sup(x1),self.tol)
426 if self.back[0]: self.__location_of_constraint+=back_mask*[1.,0.,0.]
427 if self.back[1]: self.__location_of_constraint+=back_mask*[0.,1.,0.]
428 if self.back[2]: self.__location_of_constraint+=back_mask*[0.,0.,1.]
429 bottom_mask=whereZero(x2-inf(x2),self.tol)
430 if self.bottom[0]: self.__location_of_constraint+=bottom_mask*[1.,0.,0.]
431 if self.bottom[1]: self.__location_of_constraint+=bottom_mask*[0.,1.,0.]
432 if self.bottom[2]: self.__location_of_constraint+=bottom_mask*[0.,0.,1.]
433 top_mask=whereZero(x2-sup(x2),self.tol)
434 if self.top[0]: self.__location_of_constraint+=top_mask*[1.,0.,0.]
435 if self.top[1]: self.__location_of_constraint+=top_mask*[0.,1.,0.]
436 if self.top[2]: self.__location_of_constraint+=top_mask*[0.,0.,1.]
437 if not self.value == None:
438 self.__value_of_constraint=self.__location_of_constraint*self.value
439 else:
440 x0,x1=x[0],x[1]
441 left_mask=whereZero(x0-inf(x0),self.tol)
442 if self.left[0]: self.__location_of_constraint+=left_mask*[1.,0.]
443 if self.left[1]: self.__location_of_constraint+=left_mask*[0.,1.]
444 right_mask=whereZero(x0-sup(x0),self.tol)
445 if self.right[0]: self.__location_of_constraint+=right_mask*[1.,0.]
446 if self.right[1]: self.__location_of_constraint+=right_mask*[0.,1.]
447 bottom_mask=whereZero(x1-inf(x1),self.tol)
448 if self.bottom[0]: self.__location_of_constraint+=bottom_mask*[1.,0.]
449 if self.bottom[1]: self.__location_of_constraint+=bottom_mask*[0.,1.]
450 top_mask=whereZero(x1-sup(x1),self.tol)
451 if self.top[0]: self.__location_of_constraint+=top_mask*[1.,0.]
452 if self.top[1]: self.__location_of_constraint+=top_mask*[0.,1.]
453 if not self.value == None:
454 self.__value_of_constraint=self.__location_of_constraint*self.value[:2]
455
456 # vim: expandtab shiftwidth=4:

Properties

Name Value
svn:eol-style native
svn:keywords Author Date Id Revision

  ViewVC Help
Powered by ViewVC 1.1.26