1 |
jgs |
127 |
# $Id$ |
2 |
jgs |
149 |
from esys.escript import * |
3 |
|
|
from esys.escript.modelframe import Model,ParameterSet |
4 |
jgs |
147 |
from math import log |
5 |
jgs |
127 |
|
6 |
jgs |
147 |
class Sequencer(Model): |
7 |
jgs |
148 |
""" |
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 |
jgs |
149 |
@param t_end: - model is terminated when t_end is passed |
13 |
|
|
(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 |
jgs |
127 |
|
20 |
jgs |
149 |
""" |
21 |
jgs |
147 |
Model.__init__(self,debug=debug) |
22 |
elspeth |
270 |
self.declareParameter(t=t, |
23 |
|
|
t_end=t_end, |
24 |
jgs |
148 |
dt_max=dt_max) |
25 |
jgs |
127 |
|
26 |
jgs |
147 |
def doInitialization(self): |
27 |
jgs |
148 |
""" |
28 |
jgs |
149 |
initialize time integration |
29 |
jgs |
148 |
""" |
30 |
|
|
self.__t_old = self.t |
31 |
jgs |
147 |
|
32 |
jgs |
148 |
def doStepPreprocessing(self, dt): |
33 |
|
|
self.t = self.__t_old+dt |
34 |
jgs |
147 |
|
35 |
jgs |
148 |
def doStepPostprocessing(self, dt): |
36 |
|
|
self.__t_old = self.t |
37 |
jgs |
147 |
|
38 |
|
|
def finalize(self): |
39 |
jgs |
148 |
""" |
40 |
|
|
true when t has reached t_end |
41 |
|
|
""" |
42 |
|
|
return self.t >= self.t_end |
43 |
jgs |
147 |
|
44 |
jgs |
148 |
def getSafeTimeStepSize(self, dt): |
45 |
|
|
""" |
46 |
|
|
returns dt_max |
47 |
|
|
""" |
48 |
|
|
return self.dt_max |
49 |
jgs |
147 |
|
50 |
jgs |
148 |
class GaussianProfile(ParameterSet): |
51 |
|
|
""" |
52 |
jgs |
149 |
Generates a Gaussian profile at center x_c, width width and height A |
53 |
jgs |
148 |
over a domain |
54 |
jgs |
127 |
|
55 |
jgs |
149 |
@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 |
jgs |
127 |
|
62 |
jgs |
148 |
In the case that the spatial dimension is two, The third component of |
63 |
|
|
x_c is dropped |
64 |
jgs |
127 |
""" |
65 |
|
|
def __init__(self,debug=False): |
66 |
jgs |
148 |
ParameterSet.__init__(self,debug=debug) |
67 |
jgs |
147 |
self.declareParameter(domain=None, |
68 |
|
|
x_c=numarray.zeros([3]), |
69 |
|
|
A=1., |
70 |
|
|
width=0.1, |
71 |
|
|
r=0) |
72 |
jgs |
127 |
|
73 |
|
|
def out(self): |
74 |
jgs |
149 |
""" |
75 |
|
|
Generate the Gaussian profile |
76 |
|
|
""" |
77 |
jgs |
148 |
x = self.domain.getX() |
78 |
|
|
dim = self.domain.getDim() |
79 |
|
|
l = length(x-self.x_c[:dim]) |
80 |
gross |
323 |
m = whereNegative(l-self.r) |
81 |
jgs |
148 |
|
82 |
jgs |
127 |
return (m+(1.-m)*exp(-log(2.)*(l/self.width)**2))*self.A |
83 |
|
|
|
84 |
jgs |
148 |
class InterpolateOverBox(ParameterSet): |
85 |
jgs |
147 |
""" |
86 |
jgs |
148 |
Returns values at each time. The values are defined through given values |
87 |
|
|
at time node. |
88 |
jgs |
147 |
|
89 |
jgs |
149 |
@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 |
jgs |
147 |
""" |
106 |
|
|
|
107 |
jgs |
148 |
def __init__(self, debug=False): |
108 |
|
|
ParameterSet.__init__(self, debug=debug) |
109 |
jgs |
147 |
self.declareParameter(domain=None, |
110 |
|
|
left_bottom_front=[0.,0.,0.], |
111 |
|
|
right_top_back=[1.,1.,1.], |
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 |
jgs |
148 |
x = self.domain.getX() |
124 |
|
|
if self.domain.getDim() == 2: |
125 |
|
|
f_right = (x[0] - self.left_bottom_front[0])/\ |
126 |
|
|
(self.right_top_back[0] - self.left_bottom_front[0]) |
127 |
|
|
f_left = 1. - f_right |
128 |
|
|
f_top = (x[1] - self.left_bottom_front[1])/\ |
129 |
|
|
(self.right_top_back[1] - self.left_bottom_front[1]) |
130 |
|
|
f_bottom = 1. - f_top |
131 |
|
|
out = self.value_left_bottom_front * f_left * f_bottom \ |
132 |
|
|
+ self.value_right_bottom_front* f_right * f_bottom \ |
133 |
|
|
+ self.value_left_top_front * f_left * f_top \ |
134 |
|
|
+ self.value_right_top_front * f_right * f_top |
135 |
jgs |
147 |
else: |
136 |
jgs |
148 |
f_right = (x[0] - self.left_bottom_front[0])/\ |
137 |
|
|
(self.right_top_back[0] - self.left_bottom_front[0]) |
138 |
|
|
f_left = 1. - f_right |
139 |
|
|
f_top = (x[1] - self.left_bottom_front[1])/\ |
140 |
|
|
(self.right_top_back[1] - self.left_bottom_front[1]) |
141 |
|
|
f_bottom = 1. - f_top |
142 |
|
|
f_back = (x[2] - self.left_bottom_front[1])/\ |
143 |
|
|
(self.right_top_back[2] - self.left_bottom_front[2]) |
144 |
|
|
f_front = 1. - f_back |
145 |
|
|
out = self.value_left_bottom_front * f_left * f_bottom * f_front \ |
146 |
|
|
+ self.value_right_bottom_front* f_right * f_bottom * f_front \ |
147 |
|
|
+ self.value_left_top_front * f_left * f_top * f_front \ |
148 |
|
|
+ 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 |
jgs |
147 |
return out |
154 |
|
|
|
155 |
|
|
|
156 |
jgs |
148 |
class InterpolatedTimeProfile(ParameterSet): |
157 |
|
|
""" |
158 |
|
|
|
159 |
|
|
Returns values at each time. The values are defined through given |
160 |
|
|
values at time node. |
161 |
jgs |
147 |
|
162 |
jgs |
148 |
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 |
jgs |
147 |
|
168 |
jgs |
149 |
@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 |
jgs |
148 |
""" |
173 |
jgs |
127 |
|
174 |
|
|
def __init__(self,debug=False): |
175 |
jgs |
148 |
ParameterSet.__init__(self,debug=debug) |
176 |
jgs |
147 |
self.declareParameter(t=0., \ |
177 |
|
|
nodes=[0.,1.],\ |
178 |
|
|
values=[1.,1.]) |
179 |
|
|
def out(self): |
180 |
jgs |
148 |
l = len(self.nodes) - 1 |
181 |
|
|
t = self.t |
182 |
|
|
if t <= self.nodes[0]: |
183 |
|
|
return self.values[0] |
184 |
jgs |
147 |
else: |
185 |
|
|
for i in range(1,l): |
186 |
jgs |
148 |
if t < self.nodes[i]: |
187 |
|
|
m = (self.values[i-1] - self.values[i])/\ |
188 |
|
|
(self.nodes[i-1] - self.nodes[i]) |
189 |
|
|
return m*(t-self.nodes[i-1]) + self.values[i-1] |
190 |
jgs |
147 |
return self.values[l] |
191 |
jgs |
127 |
|
192 |
jgs |
147 |
class LinearCombination(Model): |
193 |
jgs |
148 |
""" |
194 |
|
|
Returns a linear combination of the f0*v0+f1*v1+f2*v2+f3*v3+f4*v4 |
195 |
jgs |
147 |
|
196 |
jgs |
149 |
@ivar f0 (in): numerical object or None (default: None) |
197 |
|
|
@ivar v0 (in): numerical object or None (default: None) |
198 |
|
|
@ivar f1 (in): numerical object or None (default: None) |
199 |
|
|
@ivar v1 (in): numerical object or None (default: None) |
200 |
|
|
@ivar f2 (in): numerical object or None (default: None) |
201 |
|
|
@ivar v2 (in): numerical object or None (default: None) |
202 |
|
|
@ivar f3 (in): numerical object or None (default: None) |
203 |
|
|
@ivar v3 (in): numerical object or None (default: None) |
204 |
|
|
@ivar f4 (in): numerical object or None (default: None) |
205 |
|
|
@ivar v4 (in): numerical object or None (default: None) |
206 |
|
|
@ivar out (callable): current value |
207 |
jgs |
148 |
""" |
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 |
jgs |
147 |
|
221 |
jgs |
148 |
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 |
jgs |
147 |
|
227 |
jgs |
148 |
if not self.f1 == None and not self.v1 == None: |
228 |
|
|
fv1 = self.f1*self.v1 |
229 |
|
|
else: |
230 |
|
|
fv1 = None |
231 |
jgs |
147 |
|
232 |
jgs |
148 |
if not self.f2 == None and not self.v2 == None: |
233 |
|
|
fv2 = f2*v2 |
234 |
|
|
else: |
235 |
|
|
fv2 = None |
236 |
jgs |
147 |
|
237 |
jgs |
148 |
if not self.f3 == None and not self.v3 == None: |
238 |
|
|
fv3 = self.f3*self.v3 |
239 |
|
|
else: |
240 |
|
|
fv3 = None |
241 |
jgs |
147 |
|
242 |
jgs |
148 |
if not self.f4 == None and not self.v4 == None: |
243 |
|
|
fv4 = self.f4*self.v4 |
244 |
|
|
else: |
245 |
|
|
fv4 = None |
246 |
jgs |
147 |
|
247 |
jgs |
148 |
if fv0 == None: |
248 |
|
|
out = 0. |
249 |
|
|
else: |
250 |
|
|
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 |
jgs |
147 |
|
259 |
jgs |
148 |
# vim: expandtab shiftwidth=4: |