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

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

Parent Directory Parent Directory | Revision Log Revision Log


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