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