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