1 |
# |
2 |
# $Id$ |
3 |
# |
4 |
####################################################### |
5 |
# |
6 |
# Copyright 2003-2007 by ACceSS MNRF |
7 |
# Copyright 2007 by University of Queensland |
8 |
# |
9 |
# http://esscc.uq.edu.au |
10 |
# Primary Business: Queensland, Australia |
11 |
# Licensed under the Open Software License version 3.0 |
12 |
# http://www.opensource.org/licenses/osl-3.0.php |
13 |
# |
14 |
####################################################### |
15 |
# |
16 |
|
17 |
__copyright__=""" Copyright (c) 2006 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 |
|
23 |
from esys.escript import * |
24 |
from esys.escript.modelframe import Model,ParameterSet |
25 |
from esys.escript.linearPDEs import LinearPDE |
26 |
from math import log |
27 |
|
28 |
class Sequencer(Model): |
29 |
""" |
30 |
Runs through time until t_end is reached. |
31 |
|
32 |
@ivar t_end: model is terminated when t_end is passed, default 1 (in). |
33 |
@type t_end: C{float} |
34 |
@ivar dt_max: maximum time step size, default L{Model.UNDEF_DT} (in) |
35 |
@type dt_max: C{float} |
36 |
@ivar t: current time stamp (in/out). By default it is initialized with zero. |
37 |
@type t: C{float} |
38 |
|
39 |
""" |
40 |
def __init__(self,**kwargs): |
41 |
""" |
42 |
""" |
43 |
super(Sequencer,self).__init__(**kwargs) |
44 |
self.declareParameter(t=0., |
45 |
t_end=1., |
46 |
dt_max=Model.UNDEF_DT) |
47 |
|
48 |
def doInitialization(self): |
49 |
""" |
50 |
initialize time integration |
51 |
""" |
52 |
self.__t_old = self.t |
53 |
|
54 |
def doStepPreprocessing(self, dt): |
55 |
self.t = self.__t_old+dt |
56 |
|
57 |
def doStepPostprocessing(self, dt): |
58 |
self.__t_old = self.t |
59 |
|
60 |
def finalize(self): |
61 |
""" |
62 |
returns true when L{t} has reached L{t_end} |
63 |
""" |
64 |
return self.t >= self.t_end |
65 |
|
66 |
def getSafeTimeStepSize(self, dt): |
67 |
""" |
68 |
returns L{dt_max} |
69 |
""" |
70 |
return self.dt_max |
71 |
|
72 |
class GaussianProfile(ParameterSet): |
73 |
""" |
74 |
Generates a Gaussian profile at center x_c, width width and height A |
75 |
over a domain |
76 |
|
77 |
@ivar domain: domain |
78 |
@ivar x_c: center of the Gaussian profile (default [0.,0.,0.]) |
79 |
@ivar A: (in) height of the profile. A maybe a vector. (default 1.) |
80 |
@ivar width: (in) width of the profile (default 0.1) |
81 |
@ivar r: (in) radius of the circle (default = 0) |
82 |
|
83 |
In the case that the spatial dimension is two, The third component of |
84 |
x_c is dropped. |
85 |
""" |
86 |
def __init__(self,**kwargs): |
87 |
super(GaussianProfile, self).__init__(**kwargs) |
88 |
self.declareParameter(domain=None, |
89 |
x_c=numarray.zeros([3]), |
90 |
A=1., |
91 |
width=0.1, |
92 |
r=0) |
93 |
|
94 |
def out(self): |
95 |
""" |
96 |
Generate the Gaussian profile |
97 |
|
98 |
Link against this method to get the output of this model. |
99 |
""" |
100 |
x = self.domain.getX() |
101 |
dim = self.domain.getDim() |
102 |
l = length(x-self.x_c[:dim]) |
103 |
m = whereNegative(l-self.r) |
104 |
|
105 |
return (m+(1.-m)*exp(-log(2.)*(l/self.width)**2))*self.A |
106 |
|
107 |
class InterpolateOverBox(ParameterSet): |
108 |
""" |
109 |
Returns values at each time. The values are defined through given values |
110 |
at time node. For two dimensional domains back values are ignored. |
111 |
|
112 |
@ivar domain: domain |
113 |
@ivar value_left_bottom_front: (in) value at left,bottom,front corner |
114 |
@ivar value_right_bottom_front: (in) value at right, bottom, front corner |
115 |
@ivar value_left_top_front: (in) value at left,top,front corner |
116 |
@ivar value_right_top_front: (in) value at right,top,front corner |
117 |
@ivar value_left_bottom_back: (in) value at left,bottom,back corner |
118 |
@ivar value_right_bottom_back: (in) value at right,bottom,back corner |
119 |
@ivar value_left_top_back: (in) value at left,top,back corner |
120 |
@ivar value_right_top_back: (in) value at right,top,back corner |
121 |
""" |
122 |
|
123 |
def __init__(self, **kwargs): |
124 |
super(InterpolateOverBox, self).__init__(self) |
125 |
self.declareParameter(domain=None, |
126 |
value_left_bottom_front=0., |
127 |
value_right_bottom_front=0., |
128 |
value_left_top_front=0., |
129 |
value_right_top_front=0., |
130 |
value_left_bottom_back=0., |
131 |
value_right_bottom_back=0., |
132 |
value_left_top_back=0., |
133 |
value_right_top_back=0.) |
134 |
|
135 |
|
136 |
def out(self): |
137 |
""" |
138 |
values at domain locations by bilinear interpolation of the given values. |
139 |
|
140 |
Link against this method to get the output of this model. |
141 |
""" |
142 |
x = self.domain.getX() |
143 |
if self.domain.getDim() == 2: |
144 |
x0,x1=x[0],x[1] |
145 |
left_bottom_front0,right_top_back0=inf(x0),sup(x0) |
146 |
left_bottom_front1,right_top_back1=inf(x1),sup(x1) |
147 |
f_right = (x0 - left_bottom_front0)/(right_top_back0 -left_bottom_front0) |
148 |
f_left = 1. - f_right |
149 |
f_top = (x1 - left_bottom_front1)/(right_top_back1 - left_bottom_front1) |
150 |
f_bottom = 1. - f_top |
151 |
out = f_left * f_bottom * self.value_left_bottom_front \ |
152 |
+ f_right * f_bottom * self.value_right_bottom_front \ |
153 |
+ f_left * f_top * self.value_left_top_front \ |
154 |
+ f_right * f_top * self.value_right_top_front |
155 |
else: |
156 |
x0,x1,x2=x[0],x[1],x[2] |
157 |
left_bottom_front0,right_top_back0=inf(x0),sup(x0) |
158 |
left_bottom_front1,right_top_back1=inf(x1),sup(x1) |
159 |
left_bottom_front2,right_top_back2=inf(x2),sup(x2) |
160 |
f_right = (x0 - left_bottom_front0)/(right_top_back0 - left_bottom_front0) |
161 |
f_left = 1. - f_right |
162 |
f_top = (x1 - left_bottom_front1)/(right_top_back1 - left_bottom_front1) |
163 |
f_bottom = 1. - f_top |
164 |
f_back = (x2 - left_bottom_front1)/(right_top_back2 - left_bottom_front2) |
165 |
f_front = 1. - f_back |
166 |
out = f_left * f_bottom * f_front * self.value_left_bottom_front\ |
167 |
+ f_right * f_bottom * f_front * self.value_right_bottom_front\ |
168 |
+ f_left * f_top * f_front * self.value_left_top_front\ |
169 |
+ f_right * f_top * f_front * self.value_right_top_front\ |
170 |
+ f_left * f_bottom * f_back * self.value_left_bottom_back\ |
171 |
+ f_right * f_bottom * f_back * self.value_right_bottom_back\ |
172 |
+ f_left * f_top * f_back * self.value_left_top_back\ |
173 |
+ f_right * f_top * f_back * self.value_right_top_back |
174 |
return out |
175 |
|
176 |
|
177 |
class InterpolatedTimeProfile(ParameterSet): |
178 |
""" |
179 |
|
180 |
Returns values at each time. The values are defined through given |
181 |
values at time node. |
182 |
|
183 |
value[i] defines the value at time nodes[i]. Between nodes linear |
184 |
interpolation is used. |
185 |
|
186 |
For time t<nodes[0], value[0] is used and for t>nodes[l], values[l] |
187 |
is used where l=len(nodes)-1. |
188 |
|
189 |
@ivar t: (in) current time |
190 |
@ivar node: (in) list of time nodes |
191 |
@ivar values: (in) list of values at time nodes |
192 |
""" |
193 |
|
194 |
def __init__(self,**kwargs): |
195 |
super( InterpolatedTimeProfile, self).__init__(**kwargs) |
196 |
self.declareParameter(t=0., \ |
197 |
nodes=[0.,1.],\ |
198 |
values=[1.,1.]) |
199 |
def out(self): |
200 |
""" |
201 |
current value |
202 |
|
203 |
Link against this method to get the output of this model. |
204 |
""" |
205 |
l = len(self.nodes) - 1 |
206 |
t = self.t |
207 |
if t <= self.nodes[0]: |
208 |
return self.values[0] |
209 |
else: |
210 |
for i in range(1,l): |
211 |
if t < self.nodes[i]: |
212 |
m = (self.values[i-1] - self.values[i])/\ |
213 |
(self.nodes[i-1] - self.nodes[i]) |
214 |
return m*(t-self.nodes[i-1]) + self.values[i-1] |
215 |
return self.values[l] |
216 |
|
217 |
class ScalarDistributionFromTags(ParameterSet): |
218 |
""" |
219 |
creates a scalar distribution on a domain from tags, If tag_map is given |
220 |
the tags can be given a names and tag_map is used to map it into domain tags. |
221 |
|
222 |
@ivar domain: domain |
223 |
@type domain: L{esys.escript.Domain} |
224 |
@ivar default: default value |
225 |
@ivar tag0: tag 0 |
226 |
@type tag0: C{int} |
227 |
@ivar value0: value for tag 0 |
228 |
@type value0: C{float} |
229 |
@ivar tag1: tag 1 |
230 |
@type tag1: C{int} |
231 |
@ivar value1: value for tag 1 |
232 |
@type value1: C{float} |
233 |
@ivar tag2: tag 2 |
234 |
@type tag2: C{int} |
235 |
@ivar value2: value for tag 2 |
236 |
@type value2: C{float} |
237 |
@ivar tag3: tag 3 |
238 |
@type tag3: C{int} |
239 |
@ivar value3: value for tag 3 |
240 |
@type value3: C{float} |
241 |
@ivar tag4: tag 4 |
242 |
@type tag4: C{int} |
243 |
@ivar value4: value for tag 4 |
244 |
@type value4: C{float} |
245 |
@ivar tag5: tag 5 |
246 |
@type tag5: C{int} |
247 |
@ivar value5: value for tag 5 |
248 |
@type value5: C{float} |
249 |
@ivar tag6: tag 6 |
250 |
@type tag6: C{int} |
251 |
@ivar value6: value for tag 6 |
252 |
@type value6: C{float} |
253 |
@ivar tag7: tag 7 |
254 |
@type tag7: C{int} |
255 |
@ivar value7: value for tag 7 |
256 |
@type value7: C{float} |
257 |
@ivar tag8: tag 8 |
258 |
@type tag8: C{int} |
259 |
@ivar value8: value for tag 8 |
260 |
@type value8: C{float} |
261 |
@ivar tag9: tag 9 |
262 |
@type tag9: C{int} |
263 |
@ivar value9: value for tag 9 |
264 |
@type value9: C{float} |
265 |
""" |
266 |
def __init__(self,**kwargs): |
267 |
super(ScalarDistributionFromTags, self).__init__(**kwargs) |
268 |
self.declareParameter(domain=None, |
269 |
default=0., |
270 |
tag0=None, |
271 |
value0=0., |
272 |
tag1=None, |
273 |
value1=0., |
274 |
tag2=None, |
275 |
value2=0., |
276 |
tag3=None, |
277 |
value3=0., |
278 |
tag4=None, |
279 |
value4=0., |
280 |
tag5=None, |
281 |
value5=0., |
282 |
tag6=None, |
283 |
value6=0., |
284 |
tag7=None, |
285 |
value7=0., |
286 |
tag8=None, |
287 |
value8=0., |
288 |
tag9=None, |
289 |
value9=0.) |
290 |
|
291 |
|
292 |
def out(self): |
293 |
""" |
294 |
returns a L{esys.escript.Data} object |
295 |
Link against this method to get the output of this model. |
296 |
""" |
297 |
d=Scalar(self.default,Function(self.domain)) |
298 |
if not self.tag0 == None: d.setTaggedValue(self.tag0,self.value0) |
299 |
if not self.tag1 == None: d.setTaggedValue(self.tag1,self.value1) |
300 |
if not self.tag2 == None: d.setTaggedValue(self.tag2,self.value2) |
301 |
if not self.tag3 == None: d.setTaggedValue(self.tag3,self.value3) |
302 |
if not self.tag4 == None: d.setTaggedValue(self.tag4,self.value4) |
303 |
if not self.tag5 == None: d.setTaggedValue(self.tag5,self.value5) |
304 |
if not self.tag6 == None: d.setTaggedValue(self.tag6,self.value6) |
305 |
if not self.tag7 == None: d.setTaggedValue(self.tag7,self.value7) |
306 |
if not self.tag8 == None: d.setTaggedValue(self.tag8,self.value8) |
307 |
if not self.tag9 == None: d.setTaggedValue(self.tag9,self.value9) |
308 |
return d |
309 |
|
310 |
class SmoothScalarDistributionFromTags(ParameterSet): |
311 |
""" |
312 |
creates a smooth scalar distribution on a domain from region tags |
313 |
|
314 |
@ivar domain: domain |
315 |
@type domain: L{esys.escript.Domain} |
316 |
@ivar default: default value |
317 |
@ivar tag0: tag 0 |
318 |
@type tag0: C{int} |
319 |
@ivar value0: value for tag 0 |
320 |
@type value0: C{float} |
321 |
@ivar tag1: tag 1 |
322 |
@type tag1: C{int} |
323 |
@ivar value1: value for tag 1 |
324 |
@type value1: C{float} |
325 |
@ivar tag2: tag 2 |
326 |
@type tag2: C{int} |
327 |
@ivar value2: value for tag 2 |
328 |
@type value2: C{float} |
329 |
@ivar tag3: tag 3 |
330 |
@type tag3: C{int} |
331 |
@ivar value3: value for tag 3 |
332 |
@type value3: C{float} |
333 |
@ivar tag4: tag 4 |
334 |
@type tag4: C{int} |
335 |
@ivar value4: value for tag 4 |
336 |
@type value4: C{float} |
337 |
@ivar tag5: tag 5 |
338 |
@type tag5: C{int} |
339 |
@ivar value5: value for tag 5 |
340 |
@type value5: C{float} |
341 |
@ivar tag6: tag 6 |
342 |
@type tag6: C{int} |
343 |
@ivar value6: value for tag 6 |
344 |
@type value6: C{float} |
345 |
@ivar tag7: tag 7 |
346 |
@type tag7: C{int} |
347 |
@ivar value7: value for tag 7 |
348 |
@type value7: C{float} |
349 |
@ivar tag8: tag 8 |
350 |
@type tag8: C{int} |
351 |
@ivar value8: value for tag 8 |
352 |
@type value8: C{float} |
353 |
@ivar tag9: tag 9 |
354 |
@type tag9: C{int} |
355 |
@ivar value9: value for tag 9 |
356 |
@type value9: C{float} |
357 |
""" |
358 |
def __init__(self,**kwargs): |
359 |
super(SmoothScalarDistributionFromTags, self).__init__(**kwargs) |
360 |
self.declareParameter(domain=None, |
361 |
default=0., |
362 |
tag0=None, |
363 |
value0=0., |
364 |
tag1=None, |
365 |
value1=0., |
366 |
tag2=None, |
367 |
value2=0., |
368 |
tag3=None, |
369 |
value3=0., |
370 |
tag4=None, |
371 |
value4=0., |
372 |
tag5=None, |
373 |
value5=0., |
374 |
tag6=None, |
375 |
value6=0., |
376 |
tag7=None, |
377 |
value7=0., |
378 |
tag8=None, |
379 |
value8=0., |
380 |
tag9=None, |
381 |
value9=0.) |
382 |
|
383 |
|
384 |
def __update(self,tag,tag_value,value): |
385 |
if self.__pde==None: |
386 |
self.__pde=LinearPDE(self.domain,numSolutions=1) |
387 |
mask=Scalar(0.,Function(self.domain)) |
388 |
mask.setTaggedValue(tag,1.) |
389 |
self.__pde.setValue(Y=mask) |
390 |
mask=wherePositive(abs(self.__pde.getRightHandSide())) |
391 |
value*=(1.-mask) |
392 |
value+=tag_value*mask |
393 |
return value |
394 |
|
395 |
def out(self): |
396 |
""" |
397 |
returns a L{esys.escript.Data} object |
398 |
Link against this method to get the output of this model. |
399 |
""" |
400 |
d=Scalar(self.default,Solution(self.domain)) |
401 |
self.__pde=None |
402 |
if not self.tag0 == None: d=self.__update(self.tag0,self.value0,d) |
403 |
if not self.tag1 == None: d=self.__update(self.tag1,self.value1,d) |
404 |
if not self.tag2 == None: d=self.__update(self.tag2,self.value2,d) |
405 |
if not self.tag3 == None: d=self.__update(self.tag3,self.value3,d) |
406 |
if not self.tag4 == None: d=self.__update(self.tag4,self.value4,d) |
407 |
if not self.tag5 == None: d=self.__update(self.tag5,self.value5,d) |
408 |
if not self.tag6 == None: d=self.__update(self.tag6,self.value6,d) |
409 |
if not self.tag7 == None: d=self.__update(self.tag7,self.value7,d) |
410 |
if not self.tag8 == None: d=self.__update(self.tag8,self.value8,d) |
411 |
if not self.tag9 == None: d=self.__update(self.tag9,self.value9,d) |
412 |
return d |
413 |
|
414 |
class LinearCombination(ParameterSet): |
415 |
""" |
416 |
Returns a linear combination of the f0*v0+f1*v1+f2*v2+f3*v3+f4*v4 |
417 |
|
418 |
@ivar f0: numerical object or None, default=None (in) |
419 |
@ivar v0: numerical object or None, default=None (in) |
420 |
@ivar f1: numerical object or None, default=None (in) |
421 |
@ivar v1: numerical object or None, default=None (in) |
422 |
@ivar f2: numerical object or None, default=None (in) |
423 |
@ivar v2: numerical object or None, default=None (in) |
424 |
@ivar f3: numerical object or None, default=None (in) |
425 |
@ivar v3: numerical object or None, default=None (in) |
426 |
@ivar f4: numerical object or None, default=None (in) |
427 |
@ivar v4: numerical object or None, default=None (in) |
428 |
""" |
429 |
def __init__(self,**kwargs): |
430 |
super(LinearCombination, self).__init__(**kwargs) |
431 |
self.declareParameter(f0=None, \ |
432 |
v0=None, \ |
433 |
f1=None, \ |
434 |
v1=None, \ |
435 |
f2=None, \ |
436 |
v2=None, \ |
437 |
f3=None, \ |
438 |
v3=None, \ |
439 |
f4=None, \ |
440 |
v4=None) |
441 |
|
442 |
def out(self): |
443 |
""" |
444 |
returns f0*v0+f1*v1+f2*v2+f3*v3+f4*v4. |
445 |
Link against this method to get the output of this model. |
446 |
""" |
447 |
if not self.f0 == None and not self.v0 == None: |
448 |
fv0 = self.f0*self.v0 |
449 |
else: |
450 |
fv0 = None |
451 |
|
452 |
if not self.f1 == None and not self.v1 == None: |
453 |
fv1 = self.f1*self.v1 |
454 |
else: |
455 |
fv1 = None |
456 |
|
457 |
if not self.f2 == None and not self.v2 == None: |
458 |
fv2 = f2*v2 |
459 |
else: |
460 |
fv2 = None |
461 |
|
462 |
if not self.f3 == None and not self.v3 == None: |
463 |
fv3 = self.f3*self.v3 |
464 |
else: |
465 |
fv3 = None |
466 |
|
467 |
if not self.f4 == None and not self.v4 == None: |
468 |
fv4 = self.f4*self.v4 |
469 |
else: |
470 |
fv4 = None |
471 |
|
472 |
if fv0 == None: |
473 |
out = 0. |
474 |
else: |
475 |
out = fv0 |
476 |
if not fv1 == None: |
477 |
out += fv1 |
478 |
if not fv2 == None: |
479 |
out += fv2 |
480 |
if not fv3 == None: |
481 |
out += fv3 |
482 |
return out |
483 |
|
484 |
class MergeConstraints(ParameterSet): |
485 |
""" |
486 |
Returns a linear combination of the f0*v0+f1*v1+f2*v2+f3*v3+f4*v4 |
487 |
""" |
488 |
def __init__(self,**kwargs): |
489 |
super(MergeConstraints, self).__init__(**kwargs) |
490 |
self.declareParameter(location_of_constraint0=None, \ |
491 |
value_of_constraint0=None, \ |
492 |
location_of_constraint1=None, \ |
493 |
value_of_constraint1=None, \ |
494 |
location_of_constraint2=None, \ |
495 |
value_of_constraint2=None, \ |
496 |
location_of_constraint3=None, \ |
497 |
value_of_constraint3=None, \ |
498 |
location_of_constraint4=None, \ |
499 |
value_of_constraint4=None) |
500 |
def location_of_constraint(self): |
501 |
""" |
502 |
return the values used to constrain a solution |
503 |
|
504 |
@return: the mask marking the locations of the constraints |
505 |
@rtype: L{escript.Scalar} |
506 |
""" |
507 |
out_loc=0 |
508 |
if not self.location_of_constraint0 == None: |
509 |
out_loc=wherePositive(out_loc+wherePositive(self.location_of_constraint0)) |
510 |
if not self.location_of_constraint1 == None: |
511 |
out_loc=wherePositive(out_loc+wherePositive(self.location_of_constraint1)) |
512 |
if not self.location_of_constraint2 == None: |
513 |
out_loc=wherePositive(out_loc+wherePositive(self.location_of_constraint2)) |
514 |
if not self.location_of_constraint3 == None: |
515 |
out_loc=wherePositive(out_loc+wherePositive(self.location_of_constraint3)) |
516 |
return out_loc |
517 |
|
518 |
def value_of_constraint(self): |
519 |
""" |
520 |
return the values used to constrain a solution |
521 |
|
522 |
@return: values to be used at the locations of the constraints. If |
523 |
L{value} is not given C{None} is rerturned. |
524 |
@rtype: L{escript.Scalar} |
525 |
""" |
526 |
out_loc=0 |
527 |
out=0 |
528 |
if not self.location_of_constraint0 == None: |
529 |
tmp=wherePositive(self.location_of_constraint0) |
530 |
out=out*(1.-tmp)+self.value_of_constraint0*tmp |
531 |
out_loc=wherePositive(out_loc+tmp) |
532 |
if not self.location_of_constraint1 == None: |
533 |
tmp=wherePositive(self.location_of_constraint1) |
534 |
out=out*(1.-tmp)+self.value_of_constraint1*tmp |
535 |
out_loc=wherePositive(out_loc+tmp) |
536 |
if not self.location_of_constraint2 == None: |
537 |
tmp=wherePositive(self.location_of_constraint2) |
538 |
out=out*(1.-tmp)+self.value_of_constraint2*tmp |
539 |
out_loc=wherePositive(out_loc+tmp) |
540 |
if not self.location_of_constraint3 == None: |
541 |
tmp=wherePositive(self.location_of_constraint3) |
542 |
out=out*(1.-tmp)+self.value_of_constraint3*tmp |
543 |
out_loc=wherePositive(out_loc+tmp) |
544 |
return out |
545 |
# vim: expandtab shiftwidth=4: |