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