-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathproblem.py
419 lines (353 loc) · 14.1 KB
/
problem.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
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import numpy as np
from blockops.utils.params import ParamClass, setParams, \
PositiveInteger, ScalarNumber, VectorNumbers, MultipleChoices
from blockops.schemes import SCHEMES
from blockops.block import BlockOperator
from blockops.iteration import ALGORITHMS, BlockIteration
class ProblemError(Exception):
"""Specific exception for the BlockProblem class"""
pass
@setParams(
lam=VectorNumbers(latexName=r'\lambda'),
tEnd=ScalarNumber(positive=True, latexName=r'T_{end}'),
nBlocks=PositiveInteger(latexName='N'),
scheme=MultipleChoices(*SCHEMES.keys()),
u0=ScalarNumber(latexName=r'u_0'),
)
class BlockProblem(ParamClass):
r"""
Class instantiating a block problem for the Dahlquist problem
.. math::
\frac{du}{dt} = \lambda u, \quad
\lambda \in \mathbb{C}, \quad
t \in [0, T], \quad
u(0) = u_0
for one or several values of :math:`\lambda`.
The block problem has the form :
.. math::
\begin{pmatrix}
\phi & & &\\
-\chi & \phi & & \\
& \ddots & \ddots & \\
& & -\chi & \phi
\end{pmatrix}
\begin{bmatrix}
{\bf u}_1\\{\bf u}_2\\\vdots\\{\bf u}_N
\end{bmatrix}
=
\begin{bmatrix}
\chi(u_0{\bf e})\\0\\\vdots\\0
\end{bmatrix}
With :math:`\phi` and :math:`\chi` the block operator of size :math:`(M,M)`
such that
.. math::
\phi {\bf u}_{n+1} = \chi {\bf u}_n
Parameters
----------
lam : float, complex or np.1darray
Lambda values.
tEnd : float
End of simulation interval :math:`T`.
nBlocks : int
Number of blocks :math:`N`.
scheme : str
Time discretization scheme used for the block operators.
u0 : scalar, optional
The initial solution :math:`u_0`. The default is 1.
**schemeArgs :
Additional keyword arguments used for the time-discretization scheme.
"""
def __init__(self, lam, tEnd, nBlocks, scheme, u0=1, **schemeArgs):
# Initialize parameters
self.initialize(locals())
# Block sizes and problem settings
self.nBlocks = nBlocks
self.dt = tEnd/nBlocks
self.lam = np.asarray(lam)
# Set up bock operators and propagator of the sequential problem
self.scheme = SCHEMES[scheme](**schemeArgs)
self.phi, self.chi = self.scheme.getBlockOperators(
self.lam*self.dt, r'\phi', r'\chi')
# Storage for approximate operator and parameters
self.schemeApprox = None
self.phiApprox= None
self.paramsApprox = None
# Storage for coarse operators
self.schemeCoarse = None
self.phiCoarse = None
self.chiCoarse = None
self.TFtoC = None
self.TCtoF = None
self.deltaChi = None
# Storage for coarse approximate operator and parameters
self.schemeCoarseApprox = None
self.phiCoarseApprox = None
# Additional storage for propagators
self.prop = self.phi**(-1) * self.chi
self.propApprox = None
self.propCoarse = None
self.propCoarseApprox = None
# Problem parameters
self.u0 = np.ones_like(u0*lam, shape=(1, self.nPoints))
if np.size(lam) == 1:
self.u0 = self.u0.squeeze(axis=0)
@property
def points(self):
return self.scheme.points
@property
def nLam(self):
"""Number of lambda values for the block problem"""
return np.size(self.lam)
@property
def nPoints(self):
"""Number of time points in each blocks"""
return np.size(self.points)
@property
def times(self):
"""2D vector with time values (nBlocks, nPoints)"""
return np.array([[(i+tau)*self.dt for tau in self.points]
for i in range(self.nBlocks)])
@property
def uShape(self):
"""Shape of the global u vector for each block"""
if self.nLam == 1:
return (self.nBlocks, self.nPoints)
else:
return (self.nBlocks, self.nLam, self.nPoints)
# -------------------------------------------------------------------------
# Method for approximate operator
# -------------------------------------------------------------------------
def setApprox(self, scheme, **schemeArgs):
r"""
Set the approximate block operator :math:`\tilde{\phi}`
on the fine level.
Parameters
----------
scheme : str
Time discretization scheme for the approximate block operator.
**schemeArgs :
Additional keyword arguments used for the time-discretization scheme.
"""
# Force the same formulation and number of points as the fine scheme
schemeArgs['form'] = self.scheme.PARAMS['form'].value
schemeArgs['nPoints'] = self.nPoints
# Set up approximate block operators
if not scheme in SCHEMES:
raise ValueError(f'{scheme} scheme is not implemented')
self.schemeApprox = SCHEMES[scheme](**schemeArgs)
self.phiApprox, _ = self.schemeApprox.getBlockOperators(
self.lam*self.dt, r'\tilde{\phi}', r'\tilde{\chi}')
self.propApprox = self.phiApprox**(-1) * self.chi
# Eventually set the coarse approximate block operators
try:
self.setCoarseApprox()
except ProblemError:
pass
@property
def approxIsSet(self):
"""Wether or not the approximate operator is defined on fine level"""
for attr in ['schemeApprox', 'phiApprox', 'propApprox']:
if getattr(self, attr) is None:
return False
return True
# -------------------------------------------------------------------------
# Method for coarse level operators
# -------------------------------------------------------------------------
def setCoarseLevel(self, nPoints, **schemeArgs):
# Retrieve parameters and BlockScheme class from fine level
params = self.scheme.getParamsValue()
params.update(schemeArgs)
params['nPoints'] = nPoints
BlockScheme = self.scheme.__class__
# Build coarse block operators
self.schemeCoarse = BlockScheme(**params)
self.phiCoarse, self.chiCoarse = self.schemeCoarse.getBlockOperators(
self.lam*self.dt, r'\phi_C', r'\chi_C')
# Build transfer operators
TFtoC, TCtoF = self.scheme.getTransferMatrices(
self.pointsCoarse, vectorized=self.nLam > 1)
self.TFtoC = BlockOperator('T_F^C', matrix=TFtoC, cost=0)
self.TCtoF = BlockOperator('T_C^F', matrix=TCtoF, cost=0)
# Additional block operators
self.deltaChi = self.TFtoC * self.chi - self.chiCoarse * self.TFtoC
self.propCoarse = self.TCtoF * self.phiCoarse**(-1) * self.chiCoarse * self.TFtoC
# Eventually set the coarse approximate operator
try:
self.setCoarseApprox()
except ProblemError:
pass
@property
def coarseIsSet(self):
"""Wether or not coarse level and its operators are defined"""
for attr in ['schemeCoarse', 'phiCoarse', 'chiCoarse',
'TFtoC', 'TCtoF', 'deltaChi',
'propCoarse']:
if getattr(self, attr) is None:
return False
return True
@property
def noDeltaChi(self):
r"""Wether or not :math:`\Delta_\chi = T_F^C\chi - \chi^C T_C^F` is null"""
if self.coarseIsSet:
return np.linalg.norm(self.deltaChi.matrix, ord=np.inf) < 1e-14
@property
def pointsCoarse(self):
if self.schemeCoarse is not None:
return self.schemeCoarse.points
@property
def nPointsCoarse(self):
"""Number of points per block for the coarse level"""
if self.schemeCoarse is not None:
return np.size(self.pointsCoarse)
@property
def invariantCoarseProlong(self):
"""Wether or not :math:`T_F^C T_C^F = I`"""
if self.coarseIsSet:
transfer = (self.TFtoC * self.TCtoF).matrix
return np.allclose(transfer, np.eye(self.nPointsCoarse))
@property
def timesCoarse(self):
"""2D vector with coarse time values (nBlocks, nPointsCoarse)"""
return np.array([[(i+tau)*self.dt for tau in self.pointsCoarse]
for i in range(self.nBlocks)])
def setCoarseApprox(self, **schemeArgs):
if not (self.coarseIsSet and self.approxIsSet):
raise ProblemError(
'cannot set coarse approximate operators before both coarse'
' level and approximation are set')
# Retrieve parameters and BlockScheme class from fine level
params = self.schemeApprox.getParamsValue()
params.update(schemeArgs)
params['nPoints'] = self.nPointsCoarse
BlockScheme = self.schemeApprox.__class__
# Build coarse approximate block operators
self.schemeCoarseApprox = BlockScheme(**params)
self.phiCoarseApprox, _ = self.schemeCoarse.getBlockOperators(
self.lam*self.dt, r'\tilde{\phi}_C', r'\tilde{\chi}_C')
self.propCoarseApprox = self.TCtoF * self.phiCoarseApprox**(-1) * self.chiCoarse * self.TFtoC
# -------------------------------------------------------------------------
# Method for solutions and errors
# -------------------------------------------------------------------------
def getSolution(self, sType='fine', initSol=False) -> np.ndarray:
"""
Get a specific solution from the block problem.
Parameters
----------
sType : str, optional
Solution type. The default is 'fine'.
initSol : bool, optional
Wether or not include the :math:`u_0` term.
The default is False.
Returns
-------
sol : np.ndarray
The solution in array form (shape self.uShape).
"""
u = [self.u0]
# Exact solution using exponential
if sType == 'exact':
lamT = self.lam*self.times if self.nLam == 1 else \
self.lam[None, :, None] * self.times[:, None, :]
uTh = np.exp(lamT)*self.u0[0]
if initSol:
return np.array(u + uTh.tolist())
else:
return uTh
# Build the propagator depending on the selected solution type
if sType == 'fine':
prop = self.prop
elif sType == 'approx':
prop = self.propApprox
elif sType == 'coarse':
prop = self.propCoarse
elif sType == 'coarseApprox':
prop = self.propCoarseApprox
else:
raise NotImplementedError(f'sType={sType}')
# Propagate solution (serial)
for i in range(self.nBlocks):
u.append(prop(u[-1]))
if initSol:
return np.array(u)
else:
return np.array(u[1:])
def getError(self, uNum='fine', uRef='exact') -> np.ndarray:
"""
Compute the (absolute) error between two solutions.
Parameters
----------
uNum : str or np.ndarray, optional
Solution from which to compute the error.
Either a solution type returned by getSolution,
or some solution values in array form.
The default is 'fine'.
uRef : str or np.ndarray, optional
Reference solution for the error computation.
Either a solution type returned by getSolution,
or some solution values in array form. The default is 'fine'.
Returns
-------
err : np.ndarray
The error in array form (shape self.uShape).
"""
if isinstance(uRef, str):
uRef = self.getSolution(uRef)
if isinstance(uNum, str):
uNum = self.getSolution(uNum)
return np.abs(uNum - uRef)
# -------------------------------------------------------------------------
# Method for block iterations
# -------------------------------------------------------------------------
def getBlockIteration(self, algo: str) -> BlockIteration:
"""
Generate a block iteration object associated to the block problem.
Parameters
----------
algo : str
Type of algorithm to use (Parareal, ABGS, TMG, PFASST, ...)
Returns
-------
BlockIteration
The block iteration object.
"""
try:
BlockIter = ALGORITHMS[algo]
if BlockIter.needApprox and not self.approxIsSet:
raise ValueError(f'{algo} need an approximate block operator')
if BlockIter.needCoarse and not self.coarseIsSet:
raise ValueError(f'{algo} need a coarse block operator')
blockIter = BlockIter(
phi=self.phi, phiApprox=self.phiApprox, chi=self.chi,
phiCoarse=self.phiCoarse, chiCoarse=self.chiCoarse,
TFtoC=self.TFtoC, TCtoF=self.TCtoF,
phiCoarseApprox=self.phiCoarseApprox)
blockIter.prob = self
return blockIter
except KeyError:
raise NotImplementedError(
f'block iteration for {algo} not implemented')
if __name__ == '__main__':
# Quick module testing
# Create block problem with its fine level operator
prob = BlockProblem(
1j, 12*np.pi, nBlocks=6, scheme='RungeKutta', nPoints=11,
rkScheme='SDIRK54')
# Set up coarse level :
prob.setCoarseLevel(nPoints=5)
# Set up approximate operator (on coarse and fine level) :
prob.setApprox(scheme='RungeKutta', rkScheme='SDIRK2')
import matplotlib.pyplot as plt
# Get time vector and add t=0
times = prob.times.ravel()
times = np.concatenate(([0], times))
# Compute and plot exact solution
u = prob.getSolution('exact').ravel()
u = np.concatenate(([1], u))
plt.plot(times, u.real, 'o--', label='exact')
for sType in ['fine', 'approx', 'coarse', 'coarseApprox']:
u = prob.getSolution(sType).ravel()
u = np.concatenate(([1], u))
plt.plot(times, u.real, label=sType)
plt.legend()