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