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