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 import finley |
13 |
|
14 |
class FinleyReader(ParameterSet): |
15 |
""" |
16 |
reads finley mesh file. |
17 |
|
18 |
@ivar source: file name of the finley input file |
19 |
@type source: C{str} |
20 |
@ivar intergrationOrder: integration order, default -1 (in). |
21 |
@type intergrationOrder: C{int} |
22 |
""" |
23 |
def __init__(self,debug=False): |
24 |
""" |
25 |
initializes the object |
26 |
""" |
27 |
super(FinleyReader,self).__init__(debug=debug) |
28 |
self.declareParameter(source="none", |
29 |
integrationOrder=-1) |
30 |
self.__domain=None |
31 |
|
32 |
def domain(self): |
33 |
""" |
34 |
returns the domain |
35 |
|
36 |
@return: the domain |
37 |
@rtype: L{Domain} |
38 |
""" |
39 |
if not self.__domain: |
40 |
self.__domain=finley.ReadMesh(self.source.getLocalFileName(),self.integrationOrder) |
41 |
self.trace("mesh read from %s"%self.source) |
42 |
return self.__domain |
43 |
|
44 |
|
45 |
class RectangularDomain(ParameterSet): |
46 |
""" |
47 |
Generates a mesh over a rectangular domain finley. |
48 |
|
49 |
@ivar dim: spatial dimension, default =2 (in). |
50 |
@type dim: spatial dimension |
51 |
@ivar l: spatial lengths, default [1.,1.,1.] (in). |
52 |
@type l: C{list} of C{floats}s |
53 |
@ivar n: number of elements, default [10,10,10] (in). |
54 |
@type n: C{list} of C{int}s |
55 |
@ivar order: element order, default 1 (in). |
56 |
@type order: C{int} |
57 |
@ivar periodic: flags for periodicity, default [False,False,False] (in). |
58 |
@type periodic: C{list} of C{bool}s |
59 |
@ivar intergrationOrder: integration order, default -1 (in). |
60 |
@type intergrationOrder: C{int} |
61 |
""" |
62 |
def __init__(self,debug=False): |
63 |
""" |
64 |
initializes the object |
65 |
""" |
66 |
super(RectangularDomain,self).__init__(debug=debug) |
67 |
self.declareParameter(dim=2,\ |
68 |
l=[1.,1.,1.],\ |
69 |
n=[10,10,10], \ |
70 |
order=1,\ |
71 |
periodic=[False,False,False], |
72 |
integrationOrder=-1) |
73 |
self.__domain=None |
74 |
|
75 |
def domain(self): |
76 |
""" |
77 |
returns the domain |
78 |
|
79 |
@return: the domain |
80 |
@rtype: L{Domain} |
81 |
""" |
82 |
if not self.__domain: |
83 |
if self.dim==2: |
84 |
self.domain=finley.Rectangle(n0=self.n[0],\ |
85 |
n1=self.n[1],\ |
86 |
l0=self.l[0],\ |
87 |
l1=self.l[1],\ |
88 |
order=self.order, \ |
89 |
periodic0=self.periodic[0], \ |
90 |
periodic1=self.periodic[1], \ |
91 |
integrationOrder=self.integrationOrder) |
92 |
else: |
93 |
self.__domain=finley.Brick(n0=self.n[0],\ |
94 |
n1=self.n[1],\ |
95 |
n2=self.n[2],\ |
96 |
l0=self.l[0],\ |
97 |
l1=self.l[1],\ |
98 |
l2=self.l[2],\ |
99 |
order=self.order, \ |
100 |
periodic0=self.periodic[0], \ |
101 |
periodic1=self.periodic[1], \ |
102 |
periodic2=self.periodic[2], \ |
103 |
integrationOrder=self.integrationOrder) |
104 |
return self.__domain |
105 |
|
106 |
class UpdateGeometry(Model): |
107 |
""" |
108 |
applies a displacement field to a domain |
109 |
|
110 |
@ivar displacement: displacements applied to the original mesh coordinates (in). |
111 |
@type displacement: L{escript.Vector} |
112 |
@ivar domain: domain |
113 |
@type domain: L{escript.Domain} |
114 |
""" |
115 |
def __init__(self,debug=False): |
116 |
""" |
117 |
set-up the object |
118 |
""" |
119 |
super(UpdateGeometry, self).__init__(debug=debug) |
120 |
self.declareParameter(domain=None,\ |
121 |
displacement=None) |
122 |
|
123 |
|
124 |
def doInitialization(self): |
125 |
""" |
126 |
initialize model |
127 |
""" |
128 |
self.__x=self.domain.getX() |
129 |
self.__reset=True |
130 |
|
131 |
def doStepPreprocessing(self,dt): |
132 |
""" |
133 |
applies the current L{displacement} to mesh nodes if required. |
134 |
""" |
135 |
if self.__reset: |
136 |
self.trace("mesh nodes updated.") |
137 |
self.domain.setX(self.__x+self.displacement) |
138 |
self.__reset=False |
139 |
|
140 |
def doStep(self,dt): |
141 |
""" |
142 |
applies the current L{displacement} to mesh nodes. |
143 |
""" |
144 |
self.trace("mesh nodes updated.") |
145 |
self.domain.setX(self.__x+self.displacement) |
146 |
self.__reset=True |
147 |
|
148 |
def doStepPostprocessing(self,dt): |
149 |
""" |
150 |
marks nodes as beeing updated. |
151 |
""" |
152 |
self.__reset=False |
153 |
|
154 |
class ScalarConstrainer(Model): |
155 |
""" |
156 |
Creates a characteristic function for the location of constraints |
157 |
for a scalar value and selects the value from an initial value |
158 |
ate these locations. |
159 |
|
160 |
In the case that the spatial dimension is two, the arguments front and back are ignored. |
161 |
|
162 |
@ivar domain: domain (in). |
163 |
@ivar left: True to set a constraint at the left face of the domain (x[0]=min x[0]), default False (in). |
164 |
@ivar right: True to set a constraint at the left face of the domain (x[0]=max x[0]), default False (in). |
165 |
@ivar top: True to set a constraint at the left face of the domain (x[1]=min x[1]), default False (in). |
166 |
@ivar bottom: True to set a constraint at the left face of the domain (x[1]=max x[1]), default False (in). |
167 |
@ivar front: True to set a constraint at the left face of the domain (x[2]=min x[2]), default False (in). |
168 |
@ivar back: True to set a constraint at the left face of the domain (x[2]=max x[2]), default False (in). |
169 |
@ivar tol: absolute tolerance for "x=max x" condition, default 1.e-8 (in). |
170 |
""" |
171 |
def __init__(self,debug=False): |
172 |
ParameterSet.__init__(self,debug=debug) |
173 |
self.declareParameter(domain=None, \ |
174 |
value=None, \ |
175 |
left=False, \ |
176 |
right=False, \ |
177 |
top=False, \ |
178 |
bottom=False, \ |
179 |
front=False, \ |
180 |
back=False, \ |
181 |
tol=1.e-8) |
182 |
self.__value_of_constraint = None |
183 |
self.__location_of_constraint=None |
184 |
def location_of_constraint(self): |
185 |
""" |
186 |
return the values used to constrain a solution |
187 |
|
188 |
@return: the mask marking the locations of the constraints |
189 |
@rtype: L{escript.Scalar} |
190 |
""" |
191 |
if not self.__location_of_constraint: self.__setOutput() |
192 |
return self.__location_of_constraint |
193 |
|
194 |
def value_of_constraint(self): |
195 |
""" |
196 |
return the values used to constrain a solution |
197 |
|
198 |
@return: values to be used at the locations of the constraints. If |
199 |
L{value} is not given C{None} is rerturned. |
200 |
@rtype: L{escript.Scalar} |
201 |
""" |
202 |
if not self.__location_of_constraint: self.__setOutput() |
203 |
return self.__value_of_constraint |
204 |
|
205 |
def __setOutput(self): |
206 |
x=self.domain.getX() |
207 |
self.__location_of_constraint=Scalar(0,x.getFunctionSpace()) |
208 |
if self.domain.getDim()==3: |
209 |
x0,x1,x2=x[0],x[1],x[2] |
210 |
if self.left: self.__location_of_constraint+=whereZero(x0-inf(x0),self.tol) |
211 |
if self.right: self.__location_of_constraint+=whereZero(x0-sup(x0),self.tol) |
212 |
if self.front: self.__location_of_constraint+=whereZero(x1-inf(x1),self.tol) |
213 |
if self.back: self.__location_of_constraint+=whereZero(x1-sup(x1),self.tol) |
214 |
if self.bottom: self.__location_of_constraint+=whereZero(x2-inf(x2),self.tol) |
215 |
if self.top: self.__location_of_constraint+=whereZero(x2-sup(x2),self.tol) |
216 |
else: |
217 |
x0,x1=x[0],x[1] |
218 |
if self.left: self.__location_of_constraint+=whereZero(x0-inf(x0),self.tol) |
219 |
if self.right: self.__location_of_constraint+=whereZero(x0-sup(x0),self.tol) |
220 |
if self.bottom: self.__location_of_constraint+=whereZero(x1-inf(x1),self.tol) |
221 |
if self.top: self.__location_of_constraint+=whereZero(x1-sup(x1),self.tol) |
222 |
if self.value: |
223 |
self.__value_of_constraint=self.__location_of_constraint*self.value |
224 |
|
225 |
class VectorConstrainer(Model): |
226 |
""" |
227 |
Creates a characteristic function for the location of constraints vector value. |
228 |
In the case that the spatial dimension is two, the arguments front and |
229 |
back as well as the third component of each argument is ignored. |
230 |
|
231 |
@ivar domain: domain |
232 |
@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]), |
233 |
default [False,False,False] (in). |
234 |
@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]), |
235 |
default [False,False,False] (in). |
236 |
@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]), |
237 |
default [False,False,False] (in). |
238 |
@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]), |
239 |
default [False,False,False] (in). |
240 |
@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]), |
241 |
default [False,False,False] (in). |
242 |
@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]), |
243 |
default [False,False,False] (in). |
244 |
@ivar tol: absolute tolerance for "x=max x" condition, default 1.e-8 (in). |
245 |
""" |
246 |
def __init__(self,debug=False): |
247 |
ParameterSet.__init__(self,debug=debug) |
248 |
self.declareParameter(domain=None, \ |
249 |
value=None, \ |
250 |
left=[0,0,0], \ |
251 |
right=[0,0,0], \ |
252 |
top=[0,0,0], \ |
253 |
bottom=[0,0,0], \ |
254 |
front=[0,0,0], \ |
255 |
back=[0,0,0], \ |
256 |
tol=1.e-8) |
257 |
self.__value_of_constraint = None |
258 |
self.__location_of_constraint=None |
259 |
|
260 |
def location_of_constraint(self): |
261 |
""" |
262 |
return the values used to constrain a solution |
263 |
|
264 |
@return: the mask marking the locations of the constraints |
265 |
@rtype: L{escript.Vector} |
266 |
""" |
267 |
if not self.__location_of_constraint: self.__setOutput() |
268 |
return self.__location_of_constraint |
269 |
|
270 |
def value_of_constraint(self): |
271 |
""" |
272 |
return the values used to constrain a solution |
273 |
|
274 |
@return: values to be used at the locations of the constraints. If |
275 |
L{value} is not given C{None} is rerturned. |
276 |
@rtype: L{escript.Vector} |
277 |
""" |
278 |
if not self.__location_of_constraint: self.__setOutput() |
279 |
return self.__value_of_constraint |
280 |
|
281 |
def __setOutput(self): |
282 |
x=self.domain.getX() |
283 |
self.__location_of_constraint=Vector(0,x.getFunctionSpace()) |
284 |
if self.domain.getDim()==3: |
285 |
x0,x1,x2=x[0],x[1],x[2] |
286 |
left_mask=whereZero(x0-inf(x0),self.tol) |
287 |
if self.left[0]: self.__location_of_constraint+=left_mask*[1.,0.,0.] |
288 |
if self.left[1]: self.__location_of_constraint+=left_mask*[0.,1.,0.] |
289 |
if self.left[2]: self.__location_of_constraint+=left_mask*[0.,0.,1.] |
290 |
right_mask=whereZero(x0-sup(x0),self.tol) |
291 |
if self.right[0]: self.__location_of_constraint+=right_mask*[1.,0.,0.] |
292 |
if self.right[1]: self.__location_of_constraint+=right_mask*[0.,1.,0.] |
293 |
if self.right[2]: self.__location_of_constraint+=right_mask*[0.,0.,1.] |
294 |
front_mask=whereZero(x1-inf(x1),self.tol) |
295 |
if self.front[0]: self.__location_of_constraint+=front_mask*[1.,0.,0.] |
296 |
if self.front[1]: self.__location_of_constraint+=front_mask*[0.,1.,0.] |
297 |
if self.front[2]: self.__location_of_constraint+=front_mask*[0.,0.,1.] |
298 |
back_mask=whereZero(x1-sup(x1),self.tol) |
299 |
if self.back[0]: self.__location_of_constraint+=back_mask*[1.,0.,0.] |
300 |
if self.back[1]: self.__location_of_constraint+=back_mask*[0.,1.,0.] |
301 |
if self.back[2]: self.__location_of_constraint+=back_mask*[0.,0.,1.] |
302 |
bottom_mask=whereZero(x2-inf(x2),self.tol) |
303 |
if self.bottom[0]: self.__location_of_constraint+=bottom_mask*[1.,0.,0.] |
304 |
if self.bottom[1]: self.__location_of_constraint+=bottom_mask*[0.,1.,0.] |
305 |
if self.bottom[2]: self.__location_of_constraint+=bottom_mask*[0.,0.,1.] |
306 |
top_mask=whereZero(x2-sup(x2),self.tol) |
307 |
if self.top[0]: self.__location_of_constraint+=top_mask*[1.,0.,0.] |
308 |
if self.top[1]: self.__location_of_constraint+=top_mask*[0.,1.,0.] |
309 |
if self.top[2]: self.__location_of_constraint+=top_mask*[0.,0.,1.] |
310 |
if self.value: |
311 |
self.__value_of_constraint=self.__location_of_constraint*self.value |
312 |
else: |
313 |
x0,x1=x[0],x[1] |
314 |
left_mask=whereZero(x0-inf(x0),self.tol) |
315 |
if self.left[0]: self.__location_of_constraint+=left_mask*[1.,0.] |
316 |
if self.left[1]: self.__location_of_constraint+=left_mask*[0.,1.] |
317 |
right_mask=whereZero(x0-sup(x0),self.tol) |
318 |
if self.right[0]: self.__location_of_constraint+=right_mask*[1.,0.] |
319 |
if self.right[1]: self.__location_of_constraint+=right_mask*[0.,1.] |
320 |
bottom_mask=whereZero(x1-inf(x1),self.tol) |
321 |
if self.bottom[0]: self.__location_of_constraint+=bottom_mask*[1.,0.] |
322 |
if self.bottom[1]: self.__location_of_constraint+=bottom_mask*[0.,1.] |
323 |
top_mask=whereZero(x1-sup(x1),self.tol) |
324 |
if self.top[0]: self.__location_of_constraint+=top_mask*[1.,0.] |
325 |
if self.top[1]: self.__location_of_constraint+=top_mask*[0.,1.] |
326 |
if self.value: |
327 |
self.__value_of_constraint=self.__location_of_constraint*self.value[:2] |
328 |
|
329 |
# vim: expandtab shiftwidth=4: |