-
Notifications
You must be signed in to change notification settings - Fork 1
/
time_integration.py
338 lines (256 loc) · 12 KB
/
time_integration.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
from __future__ import division
import scipy.sparse as sp
import scipy.sparse.linalg as lin
import abc
import numpy as np
from numpy.polynomial.polynomial import Polynomial
def scale_to_new_window(p, s):
"""
@:type p Polynomial
returns a new Polynomial with a window scaled to s*p.windows.
EXAMPLE:
If p has the window [0,1] and we use s=2, the window of the returned polynomial is [0,2]
:param p:
:param s:
:return:
"""
c_scaled = np.array(p.coef)
# first compute coefficients of scaled polynomial. The scaled polynomial has len(domain) == len(window)
for i in range(c_scaled.shape[0]):
c_scaled[i] *= (1.0/s)**i
return Polynomial(c_scaled, window=s*np.array(p.window), domain=p.domain)
class TimeIntegrationScheme(object):
__metaclass__ = abc.ABCMeta
name = "Time Integration Scheme"
evaluation_times = [] # t values where numerical scheme evaluates the rhs f(t,x)
def __init__(self, n):
"""
initialize buffer for rhs values.
:param n: number of different rhs functions (usually evaluations at different points in time, different BC...)
"""
# type: () -> object
self._rhs = n * [None]
@abc.abstractmethod
def do_step(self, u0, tau):
"""
Performs one step with steplength tau of the time integration scheme
:param u0: initial condition u0 = u(t0)
:param tau:
:return:
"""
return
def set_rhs(self, A, b, tau=0):
"""
set rhs of timestepping scheme.
:param A: Linear part of f(u)
:param b: Constant part of f(u)
:param t: optional parameter, if time stepping scheme requires more than one evaluation point
:return:
"""
self._rhs[tau] = (A, b)
def set_all_rhs(self, A, b):
"""
sets all rhs
:param A: Linear part of f(u)
:param b: Constant part of f(u)
:return:
"""
for i in range(self._rhs.__len__()):
self.set_rhs(A, b, i)
def get_evaluation_point(self, i):
"""
returns the evaluation point consisting of time and u value, corresponding to the i-th right hand side.
:param i:
:return:
"""
## todo duplicate of get_sampling_times
return (self.evaluation_times[i], None)
def number_of_function_evaluations(self):
return self._rhs.__len__()
class ContinuousRepresentationScheme(TimeIntegrationScheme):
__metaclass__ = abc.ABCMeta
def __init__(self, n, sampling_times):
self._sampling_times = sampling_times
super(ContinuousRepresentationScheme, self).__init__(self.stages) # initialize four empty spots for substepts k_1,2,3,4: https://de.wikipedia.org/wiki/Klassisches_Runge-Kutta-Verfahren
@abc.abstractmethod
def get_continuous_representation_for_component(self, component_index, t0, u0, u1, tau):
return
def get_sampling_times(self, t0, tau):
## todo duplicate of get_evaluation_point
return t0 + self._sampling_times * tau
class ExplicitEuler(TimeIntegrationScheme):
name = "Explicit Euler"
evaluation_times = np.array([0])
stages = 1
def __init__(self):
super(ExplicitEuler, self).__init__(self.stages) # only initialize one empty spot for f(t0,u0)
def do_step(self, u0, tau):
"""
perform one explicit euler step with stepwidth tau and initial condition u0. Solving the ODE
du/dx = f(u), where f(u)=Au+b is a linear function of u.
:param u0: initial condition u0 = u(t0)
:param tau: stepwidth
:param A: Linear part of f(u)
:param b: COnstant part of f(u)
:return: u1 = u(t0+tau)
"""
A,b = self._rhs[0] # rhs at t0
f = lambda u: A*u+b
u1 = u0 + tau * f(u0)
return u1
class ImplicitEuler(TimeIntegrationScheme):
name = "Implicit Euler"
evaluation_times = np.array([1.0])
stages = 1
def __init__(self):
super(ImplicitEuler, self).__init__(self.stages) # only initialize one empty spot for f(t1,u1)
def do_step(self, u0, tau):
"""
perform one implicit euler step with stepwidth tau and initial condition u0. Solving the ODE
du/dx = f(u), where f(u)=Au+b is a linear function of u.
:param u0: initial condition u0 = u(t0)
:param tau: stepwidth
:param A: Linear part of f(u)
:param b: COnstant part of f(u)
:return: u1 = u(t0+tau)
"""
A,b = self._rhs[0] # rhs at t0
u1 = lin.spsolve(sp.eye(A.shape[0]) - tau*A, tau * b + u0)
return u1
class RungeKutta4(ContinuousRepresentationScheme):
name = "Runge Kutta 4"
evaluation_times = np.array([0, .5, .5, 1])
stages = 4
def __init__(self):
# initialize four empty spots for substeps k_1,2,3,4: https://de.wikipedia.org/wiki/Klassisches_Runge-Kutta-Verfahren
super(RungeKutta4, self).__init__(self.stages, self.evaluation_times)
self.k = self.stages*[None]
def do_step(self, u0, tau):
"""
perform one explicit rk4 step with stepwidth tau and initial condition u0. Solving the ODE
du/dx = f(u), where f(u)=Au+b is a linear function of u.
adapted implementation from https://rosettacode.org/wiki/Runge-Kutta_method#Python
:param u0: initial condition u0 = u(t0)
:param tau: stepwidth
:param A: Linear part of f(u)
:param b: Constant part of f(u)
:return: u1 = u(t0+tau)
"""
def f1(u):
A1, b1 = self._rhs[0]
return A1*u+b1
def f2(u):
A2, b2 = self._rhs[1]
return A2*u+b2
def f3(u):
A3, b3 = self._rhs[2]
return A3*u+b3
def f4(u):
A4, b4 = self._rhs[3]
return A4*u+b4
self.k[0] = f1(u0) # fully explicity c0 -> c1 = u0 + k1
self.k[1] = f2(u0 + 0.5 * tau * self.k[0]) # interpolated (c0 + c1)*.5 -> c2 = u0 + k2
self.k[2] = f3(u0 + 0.5 * tau * self.k[1]) # interpolated (c0 + c2)*.5 -> c3 = u0 + k3
self.k[3] = f4(u0 + tau * self.k[2]) # extrapolated c3
u1 = u0 + tau * (self.k[0] + 2*self.k[1] + 2*self.k[2] + self.k[3]) / 6.0
return u1
def get_continuous_representation_for_component(self, component_index, t0, u0, u1, tau):
b = [Polynomial([0, 1.0, -3.0/2.0, +2.0/3.0]), # theta - 3.0 * theta**2 / 2.0 + 2 * theta**3 / 3.0,
Polynomial([0, 0, 1.0, -2.0/3.0]), # theta**2 - 2 * theta**3 / 3.0,
Polynomial([0, 0, 1.0, -2.0/3.0]), # theta**2 - 2 * theta**3 / 3.0,
Polynomial([0, 0, -1.0/2.0, 2.0/3.0])] # - theta**2 / 2.0 + 2 * theta**3 / 3.0]
u1_cont = u0[component_index] + tau * np.sum([b[s] * self.k[s][component_index] for s in range(self.stages)])
# map from [0,1] to [t0, t0+tau]
u1_cont.window = [0, 1] # polynomial coefficients are defined on [0, 1]
u1_cont.domain = [t0, t0+tau] # polynomial is evaluated at time [t0, t0+tau]
scaling_factor = (u1_cont.domain[1]-u1_cont.domain[0])/(u1_cont.window[1]-u1_cont.window[0]) # scaling factor
return scale_to_new_window(u1_cont, scaling_factor)
class RungeKutta2(ContinuousRepresentationScheme):
name = "Runge Kutta 2"
evaluation_times = np.array([0, .5])
stages = 2
def __init__(self):
super(RungeKutta2, self).__init__(self.stages, self.evaluation_times) # initialize two empty spots for substepts k_1,2
self.up = None
def do_step(self, u0, tau):
def f1(u):
A1, b1 = self._rhs[0]
return A1*u+b1
def f2(u):
A2, b2 = self._rhs[1]
return A2*u+b2
k1 = tau * f1(u0)
self.up = u0 + .5 * k1 # predicted u
k2 = tau * f2(self.up)
u1 = u0 + k2
return u1
def get_continuous_representation_for_component(self, component_index, t0, u0, u1, tau):
u1_cont = Polynomial([u0[component_index], u1[component_index]-u0[component_index]]) # linear interpolation Polynomial
# map from [0,1] to [t0, t0+tau]
u1_cont.window = [0, 1] # polynomial coefficients are defined on [0, 1]
u1_cont.domain = [t0, t0+tau] # polynomial is evaluated at time [t0, t0+tau]
scaling_factor = (u1_cont.domain[1]-u1_cont.domain[0])/(u1_cont.window[1]-u1_cont.window[0]) # scaling factor
return scale_to_new_window(u1_cont, scaling_factor)
class ExplicitHeun(ContinuousRepresentationScheme):
name = "Explicit Heun"
evaluation_times = np.array([0, 1])
stages = 2
def __init__(self):
super(ExplicitHeun, self).__init__(self.stages, self.evaluation_times) # initialize two empty spots for values at t0 and t1
self._euler_method = ExplicitEuler()
self.up = None
def do_step(self, u0, tau):
"""
perform one explicit heun step with stepwidth tau and initial condition u0. Solving the ODE
du/dx = f(u), where f(u)=Au+b is a linear function of u.
:param u0: initial condition u0 = u(t0)
:param tau: stepwidth
:param A: Linear part of f(u)
:param b: COnstant part of f(u)
:return: u1 = u(t0+tau)
"""
A0, b0 = self._rhs[0]
A1, b1 = self._rhs[1]
# perform predictor step with explicit euler
self._euler_method.set_rhs(A0, b0)
self.up = self._euler_method.do_step(u0, tau) # predicted u
def f0(u):
return A0*u+b0
def f1(u):
return A1*u+b1
# perform corrector step with Heun
u1 = u0 + .5 * tau * (f0(u0) + f1(self.up))
return u1
def get_continuous_representation_for_component(self, component_index, t0, u0, u1, tau):
u1_cont = Polynomial([u0[component_index], u1[component_index]-u0[component_index]]) # linear interpolation Polynomial
# map from [0,1] to [t0, t0+tau]
u1_cont.window = [0, 1] # polynomial coefficients are defined on [0, 1]
u1_cont.domain = [t0, t0+tau] # polynomial is evaluated at time [t0, t0+tau]
scaling_factor = (u1_cont.domain[1]-u1_cont.domain[0])/(u1_cont.window[1]-u1_cont.window[0]) # scaling factor
return scale_to_new_window(u1_cont, scaling_factor)
class ImplicitTrapezoidalRule(ContinuousRepresentationScheme):
name = "Implicit Trapezoidal Rule"
evaluation_times = np.array([0, 1])
stages = 2
def __init__(self):
super(ImplicitTrapezoidalRule, self).__init__(self.stages, self.evaluation_times) # initialize two empty spots for values at t0 and t1
def do_step(self, u0, tau):
"""
perform one implicit trapezoidal rule step with stepwidth tau and initial condition u0. Solving the ODE
du/dx = f(u), where f(u)=Au+b is a linear function of u.
:param u0: initial condition u0 = u(t0)
:param tau: stepwidth
:return: u1 = u(t0+tau)
"""
A0, b0 = self._rhs[0]
A1, b1 = self._rhs[1]
# u1 = u0 + tau/2 * (f(u0,t0) + f(u1,t1))
u1 = lin.spsolve(sp.eye(A1.shape[0]) - .5 * tau * A1, u0 + .5 * tau * (b1 + b0 + A0 * u0))
return u1
def get_continuous_representation_for_component(self, component_index, t0, u0, u1, tau):
u1_cont = Polynomial([u0[component_index], u1[component_index]-u0[component_index]]) # linear interpolation Polynomial
# map from [0,1] to [t0, t0+tau]
u1_cont.window = [0, 1] # polynomial coefficients are defined on [0, 1]
u1_cont.domain = [t0, t0+tau] # polynomial is evaluated at time [t0, t0+tau]
scaling_factor = (u1_cont.domain[1]-u1_cont.domain[0])/(u1_cont.window[1]-u1_cont.window[0]) # scaling factor
return scale_to_new_window(u1_cont, scaling_factor)