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 math import log |
12 |
|
13 |
class Sequencer(Model): |
14 |
""" |
15 |
Runs through time until t_end is reached. |
16 |
|
17 |
@ivar t_end: model is terminated when t_end is passed, default 1 (in). |
18 |
@type t_end: C{float} |
19 |
@ivar dt_max: maximum time step size, default L{Model.UNDEF_DT} (in) |
20 |
@type dt_max: C{float} |
21 |
@ivar t: current time stamp (in/out). By default it is initialized with zero. |
22 |
@type t: C{float} |
23 |
|
24 |
""" |
25 |
def __init__(self,debug=False): |
26 |
""" |
27 |
""" |
28 |
super(Sequencer,self).__init__(debug=debug) |
29 |
self.declareParameter(t=0., |
30 |
t_end=1., |
31 |
dt_max=Model.UNDEF_DT) |
32 |
|
33 |
def doInitialization(self): |
34 |
""" |
35 |
initialize time integration |
36 |
""" |
37 |
self.__t_old = self.t |
38 |
|
39 |
def doStepPreprocessing(self, dt): |
40 |
self.t = self.__t_old+dt |
41 |
|
42 |
def doStepPostprocessing(self, dt): |
43 |
self.__t_old = self.t |
44 |
|
45 |
def finalize(self): |
46 |
""" |
47 |
returns true when L{t} has reached L{t_end} |
48 |
""" |
49 |
return self.t >= self.t_end |
50 |
|
51 |
def getSafeTimeStepSize(self, dt): |
52 |
""" |
53 |
returns L{dt_max} |
54 |
""" |
55 |
return self.dt_max |
56 |
|
57 |
class GaussianProfile(ParameterSet): |
58 |
""" |
59 |
Generates a Gaussian profile at center x_c, width width and height A |
60 |
over a domain |
61 |
|
62 |
@ivar domain: domain |
63 |
@ivar x_c: center of the Gaussian profile (default [0.,0.,0.]) |
64 |
@ivar A: (in) height of the profile. A maybe a vector. (default 1.) |
65 |
@ivar width: (in) width of the profile (default 0.1) |
66 |
@ivar r: (in) radius of the circle (default = 0) |
67 |
|
68 |
In the case that the spatial dimension is two, The third component of |
69 |
x_c is dropped. |
70 |
""" |
71 |
def __init__(self,debug=False): |
72 |
ParameterSet.__init__(self,debug=debug) |
73 |
self.declareParameter(domain=None, |
74 |
x_c=numarray.zeros([3]), |
75 |
A=1., |
76 |
width=0.1, |
77 |
r=0) |
78 |
|
79 |
def out(self): |
80 |
""" |
81 |
Generate the Gaussian profile |
82 |
|
83 |
Link against this method to get the output of this model. |
84 |
""" |
85 |
x = self.domain.getX() |
86 |
dim = self.domain.getDim() |
87 |
l = length(x-self.x_c[:dim]) |
88 |
m = whereNegative(l-self.r) |
89 |
|
90 |
return (m+(1.-m)*exp(-log(2.)*(l/self.width)**2))*self.A |
91 |
|
92 |
class InterpolateOverBox(ParameterSet): |
93 |
""" |
94 |
Returns values at each time. The values are defined through given values |
95 |
at time node. For two dimensional domains back values are ignored. |
96 |
|
97 |
@ivar domain: domain |
98 |
@ivar value_left_bottom_front: (in) value at left,bottom,front corner |
99 |
@ivar value_right_bottom_front: (in) value at right, bottom, front corner |
100 |
@ivar value_left_top_front: (in) value at left,top,front corner |
101 |
@ivar value_right_top_front: (in) value at right,top,front corner |
102 |
@ivar value_left_bottom_back: (in) value at left,bottom,back corner |
103 |
@ivar value_right_bottom_back: (in) value at right,bottom,back corner |
104 |
@ivar value_left_top_back: (in) value at left,top,back corner |
105 |
@ivar value_right_top_back: (in) value at right,top,back corner |
106 |
""" |
107 |
|
108 |
def __init__(self, debug=False): |
109 |
ParameterSet.__init__(self, debug=debug) |
110 |
self.declareParameter(domain=None, |
111 |
value_left_bottom_front=0., |
112 |
value_right_bottom_front=0., |
113 |
value_left_top_front=0., |
114 |
value_right_top_front=0., |
115 |
value_left_bottom_back=0., |
116 |
value_right_bottom_back=0., |
117 |
value_left_top_back=0., |
118 |
value_right_top_back=0.) |
119 |
|
120 |
|
121 |
def out(self): |
122 |
""" |
123 |
values at domain locations by bilinear interpolation of the given values. |
124 |
|
125 |
Link against this method to get the output of this model. |
126 |
""" |
127 |
x = self.domain.getX() |
128 |
if self.domain.getDim() == 2: |
129 |
x0,x1=x[0],x[1] |
130 |
left_bottom_front0,right_top_back0=inf(x0),sup(x0) |
131 |
left_bottom_front1,right_top_back1=inf(x1),sup(x1) |
132 |
f_right = (x0 - left_bottom_front0)/(right_top_back0 -left_bottom_front0) |
133 |
f_left = 1. - f_right |
134 |
f_top = (x1 - left_bottom_front1)/(right_top_back1 - left_bottom_front1) |
135 |
f_bottom = 1. - f_top |
136 |
out = f_left * f_bottom * self.value_left_bottom_front \ |
137 |
+ f_right * f_bottom * self.value_right_bottom_front \ |
138 |
+ f_left * f_top * self.value_left_top_front \ |
139 |
+ f_right * f_top * self.value_right_top_front |
140 |
else: |
141 |
x0,x1,x2=x[0],x[1],x[2] |
142 |
left_bottom_front0,right_top_back0=inf(x0),sup(x0) |
143 |
left_bottom_front1,right_top_back1=inf(x1),sup(x1) |
144 |
left_bottom_front2,right_top_back2=inf(x2),sup(x2) |
145 |
f_right = (x0 - left_bottom_front0)/(right_top_back0 - left_bottom_front0) |
146 |
f_left = 1. - f_right |
147 |
f_top = (x1 - left_bottom_front1)/(right_top_back1 - left_bottom_front1) |
148 |
f_bottom = 1. - f_top |
149 |
f_back = (x2 - left_bottom_front1)/(right_top_back2 - left_bottom_front2) |
150 |
f_front = 1. - f_back |
151 |
out = f_left * f_bottom * f_front * self.value_left_bottom_front\ |
152 |
+ f_right * f_bottom * f_front * self.value_right_bottom_front\ |
153 |
+ f_left * f_top * f_front * self.value_left_top_front\ |
154 |
+ f_right * f_top * f_front * self.value_right_top_front\ |
155 |
+ f_left * f_bottom * f_back * self.value_left_bottom_back\ |
156 |
+ f_right * f_bottom * f_back * self.value_right_bottom_back\ |
157 |
+ f_left * f_top * f_back * self.value_left_top_back\ |
158 |
+ f_right * f_top * f_back * self.value_right_top_back |
159 |
return out |
160 |
|
161 |
|
162 |
class InterpolatedTimeProfile(ParameterSet): |
163 |
""" |
164 |
|
165 |
Returns values at each time. The values are defined through given |
166 |
values at time node. |
167 |
|
168 |
value[i] defines the value at time nodes[i]. Between nodes linear |
169 |
interpolation is used. |
170 |
|
171 |
For time t<nodes[0], value[0] is used and for t>nodes[l], values[l] |
172 |
is used where l=len(nodes)-1. |
173 |
|
174 |
@ivar t: (in) current time |
175 |
@ivar node: (in) list of time nodes |
176 |
@ivar values: (in) list of values at time nodes |
177 |
""" |
178 |
|
179 |
def __init__(self,debug=False): |
180 |
ParameterSet.__init__(self,debug=debug) |
181 |
self.declareParameter(t=0., \ |
182 |
nodes=[0.,1.],\ |
183 |
values=[1.,1.]) |
184 |
def out(self): |
185 |
""" |
186 |
current value |
187 |
|
188 |
Link against this method to get the output of this model. |
189 |
""" |
190 |
l = len(self.nodes) - 1 |
191 |
t = self.t |
192 |
if t <= self.nodes[0]: |
193 |
return self.values[0] |
194 |
else: |
195 |
for i in range(1,l): |
196 |
if t < self.nodes[i]: |
197 |
m = (self.values[i-1] - self.values[i])/\ |
198 |
(self.nodes[i-1] - self.nodes[i]) |
199 |
return m*(t-self.nodes[i-1]) + self.values[i-1] |
200 |
return self.values[l] |
201 |
|
202 |
class LinearCombination(Model): |
203 |
""" |
204 |
Returns a linear combination of the f0*v0+f1*v1+f2*v2+f3*v3+f4*v4 |
205 |
|
206 |
@ivar f0: numerical object or None, default=None (in) |
207 |
@ivar v0: numerical object or None, default=None (in) |
208 |
@ivar f1: numerical object or None, default=None (in) |
209 |
@ivar v1: numerical object or None, default=None (in) |
210 |
@ivar f2: numerical object or None, default=None (in) |
211 |
@ivar v2: numerical object or None, default=None (in) |
212 |
@ivar f3: numerical object or None, default=None (in) |
213 |
@ivar v3: numerical object or None, default=None (in) |
214 |
@ivar f4: numerical object or None, default=None (in) |
215 |
@ivar v4: numerical object or None, default=None (in) |
216 |
""" |
217 |
def __init__(self,debug=False): |
218 |
Model.__init__(self,debug=debug) |
219 |
self.declareParameter(f0=None, \ |
220 |
v0=None, \ |
221 |
f1=None, \ |
222 |
v1=None, \ |
223 |
f2=None, \ |
224 |
v2=None, \ |
225 |
f3=None, \ |
226 |
v3=None, \ |
227 |
f4=None, \ |
228 |
v4=None) |
229 |
|
230 |
def out(self): |
231 |
""" |
232 |
returns f0*v0+f1*v1+f2*v2+f3*v3+f4*v4. |
233 |
Link against this method to get the output of this model. |
234 |
""" |
235 |
if not self.f0 == None and not self.v0 == None: |
236 |
fv0 = self.f0*self.v0 |
237 |
else: |
238 |
fv0 = None |
239 |
|
240 |
if not self.f1 == None and not self.v1 == None: |
241 |
fv1 = self.f1*self.v1 |
242 |
else: |
243 |
fv1 = None |
244 |
|
245 |
if not self.f2 == None and not self.v2 == None: |
246 |
fv2 = f2*v2 |
247 |
else: |
248 |
fv2 = None |
249 |
|
250 |
if not self.f3 == None and not self.v3 == None: |
251 |
fv3 = self.f3*self.v3 |
252 |
else: |
253 |
fv3 = None |
254 |
|
255 |
if not self.f4 == None and not self.v4 == None: |
256 |
fv4 = self.f4*self.v4 |
257 |
else: |
258 |
fv4 = None |
259 |
|
260 |
if fv0 == None: |
261 |
out = 0. |
262 |
else: |
263 |
out = fv0 |
264 |
if not fv1 == None: |
265 |
out += fv1 |
266 |
if not fv2 == None: |
267 |
out += fv2 |
268 |
if not fv3 == None: |
269 |
out += fv3 |
270 |
return out |
271 |
|
272 |
# vim: expandtab shiftwidth=4: |