-
Notifications
You must be signed in to change notification settings - Fork 82
/
utils.py
672 lines (534 loc) · 19.3 KB
/
utils.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
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
"""This module contains utility functions such as convenient access to
SciPy linear solvers."""
import logging
from warnings import warn
from typing import Optional, Union, Tuple, Callable, Dict
import numpy as np
import scipy.sparse as sp
import scipy.sparse.csgraph as spg
import scipy.sparse.linalg as spl
from numpy import ndarray
from scipy.sparse import spmatrix
from skfem.assembly import asm, BilinearForm, LinearForm, DofsView
from skfem.assembly.basis import AbstractBasis
from skfem.element import ElementVector
logger = logging.getLogger(__name__)
# custom types for describing input and output values
Solution = Union[ndarray, Tuple[ndarray, ndarray]]
LinearSolver = Callable[..., ndarray]
EigenSolver = Callable[..., Tuple[ndarray, ndarray]]
LinearSystem = Union[spmatrix,
Tuple[spmatrix, ndarray],
Tuple[spmatrix, spmatrix]]
CondensedSystem = Union[LinearSystem,
Tuple[spmatrix, ndarray, ndarray],
Tuple[spmatrix, ndarray, ndarray, ndarray],
Tuple[spmatrix, spmatrix, ndarray, ndarray]]
DofsCollection = Union[ndarray, DofsView, Dict[str, DofsView]]
# preconditioners, e.g. for :func:`skfem.utils.solver_iter_krylov`
def build_pc_ilu(A: spmatrix,
drop_tol: Optional[float] = 1e-4,
fill_factor: Optional[float] = 20) -> spl.LinearOperator:
"""Incomplete LU preconditioner."""
P = spl.spilu(A.tocsc(), drop_tol=drop_tol, fill_factor=fill_factor)
M = spl.LinearOperator(A.shape, matvec=P.solve)
return M
def build_pc_diag(A: spmatrix) -> spmatrix:
"""Diagonal preconditioner."""
return sp.spdiags(1.0/A.diagonal(), 0, A.shape[0], A.shape[0])
# solvers for :func:`skfem.utils.solve`
def solver_eigen_scipy(**kwargs) -> EigenSolver:
"""Solve generalized eigenproblem using SciPy (ARPACK).
Returns
-------
EigenSolver
A solver function that can be passed to :func:`solve`.
"""
params = {
'sigma': 10,
'k': 5,
}
params.update(kwargs)
def solver(K, M, **solve_time_kwargs):
params.update(solve_time_kwargs)
from scipy.sparse.linalg import eigs
return eigs(K, M=M, **params)
return solver
def solver_eigen_scipy_sym(**kwargs) -> EigenSolver:
"""Solve symmetric generalized eigenproblem using SciPy (ARPACK).
Returns
-------
EigenSolver
A solver function that can be passed to :func:`solve`.
"""
params = {
'sigma': 10,
'k': 5,
'mode': 'normal',
}
params.update(kwargs)
def solver(K, M, **solve_time_kwargs):
params.update(solve_time_kwargs)
from scipy.sparse.linalg import eigsh
return eigsh(K, M=M, **params)
return solver
def solver_direct_scipy(**kwargs) -> LinearSolver:
"""The default linear solver of SciPy."""
def solver(A, b, **solve_time_kwargs):
kwargs.update(solve_time_kwargs)
return spl.spsolve(A, b, **kwargs)
return solver
def solver_iter_krylov(krylov: Optional[LinearSolver] = spl.cg,
verbose: Optional[bool] = False,
**kwargs) -> LinearSolver:
"""Krylov-subspace iterative linear solver.
Parameters
----------
krylov
A Krylov iterative linear solver, like, and by default,
:func:`scipy.sparse.linalg.cg`
verbose
If True, print the norm of the iterate.
Any remaining keyword arguments are passed on to the solver, in particular
tol and atol, the tolerances, maxiter, and M, the preconditioner. If the
last is omitted, a diagonal preconditioner is supplied using
:func:`skfem.utils.build_pc_diag`.
Returns
-------
LinearSolver
A solver function that can be passed to :func:`solve`.
"""
def callback(x):
if verbose:
print(np.linalg.norm(x))
def solver(A, b, **solve_time_kwargs):
kwargs.update(solve_time_kwargs)
if 'M' not in kwargs:
kwargs['M'] = build_pc_diag(A)
sol, info = krylov(A, b, **{'callback': callback, **kwargs})
if info > 0:
logger.warning("Iterative solver did not converge.")
elif info == 0 and verbose:
print(f"{krylov.__name__} converged to "
+ f"tol={kwargs.get('tol', 'default')} and "
+ f"atol={kwargs.get('atol', 'default')}")
return sol
return solver
def solver_iter_pcg(**kwargs) -> LinearSolver:
"""Conjugate gradient solver, specialized from solver_iter_krylov"""
return solver_iter_krylov(**kwargs)
# solve and condense
def solve_eigen(A: spmatrix,
M: spmatrix,
x: Optional[ndarray] = None,
I: Optional[ndarray] = None,
solver: Optional[Union[EigenSolver]] = None,
**kwargs) -> Tuple[ndarray, ndarray]:
if solver is None:
solver = solver_eigen_scipy(**kwargs)
if x is not None and I is not None:
L, X = solver(A, M, **kwargs)
y = np.tile(x.copy()[:, None], (1, X.shape[1]))
y[I] = X
return L, y
return solver(A, M, **kwargs)
def solve_linear(A: spmatrix,
b: ndarray,
x: Optional[ndarray] = None,
I: Optional[ndarray] = None,
solver: Optional[Union[LinearSolver]] = None,
**kwargs) -> ndarray:
if solver is None:
solver = solver_direct_scipy(**kwargs)
if x is not None and I is not None:
y = x.copy()
y[I] = solver(A, b, **kwargs)
return y
return solver(A, b, **kwargs)
def solve(A: spmatrix,
b: Union[ndarray, spmatrix],
x: Optional[ndarray] = None,
I: Optional[ndarray] = None,
solver: Optional[Union[LinearSolver, EigenSolver]] = None,
**kwargs) -> Solution:
"""Solve a linear system or a generalized eigenvalue problem.
The remaining keyword arguments are passed to the solver.
Parameters
----------
A
The system matrix
b
The right hand side vector or the mass matrix of a generalized
eigenvalue problem.
solver
Choose one of the following solvers:
:func:`skfem.utils.solver_direct_scipy` (default),
:func:`skfem.utils.solver_eigen_scipy` (default),
:func:`skfem.utils.solver_iter_pcg`,
:func:`skfem.utils.solver_iter_krylov`.
"""
logger.info("Solving linear system, shape={}.".format(A.shape))
if isinstance(b, spmatrix):
out = solve_eigen(A, b, x, I, solver, **kwargs) # type: ignore
elif isinstance(b, ndarray):
out = solve_linear(A, b, x, I, solver, **kwargs) # type: ignore
else:
raise NotImplementedError("Provided argument types not supported")
logger.info("Solving done.")
return out
def _flatten_dofs(S: Optional[DofsCollection]) -> Optional[ndarray]:
if S is None:
return None
if isinstance(S, ndarray):
return S
elif isinstance(S, DofsView):
return S.flatten()
elif isinstance(S, dict):
def _flatten_helper(S, key):
if key in S and isinstance(S[key], DofsView):
return S[key].flatten()
raise NotImplementedError
return np.unique(
np.concatenate([_flatten_helper(S, key) for key in S])
)
raise NotImplementedError("Unable to flatten the given set of DOFs.")
def _init_bc(A: spmatrix,
b: Optional[Union[ndarray, spmatrix]] = None,
x: Optional[ndarray] = None,
I: Optional[DofsCollection] = None,
D: Optional[DofsCollection] = None) -> Tuple[Optional[ndarray],
ndarray,
ndarray,
ndarray]:
D = _flatten_dofs(D)
I = _flatten_dofs(I)
if I is None and D is None:
raise Exception("Either I or D must be given!")
elif I is None and D is not None:
I = np.setdiff1d(np.arange(A.shape[0]), D)
elif D is None and I is not None:
D = np.setdiff1d(np.arange(A.shape[0]), I)
else:
raise Exception("Give only I or only D!")
assert isinstance(I, ndarray)
assert isinstance(D, ndarray)
if x is None:
x = np.zeros(A.shape[0])
elif b is None:
b = np.zeros_like(x)
return b, x, I, D
def enforce(A: spmatrix,
b: Optional[Union[ndarray, spmatrix]] = None,
x: Optional[ndarray] = None,
I: Optional[DofsCollection] = None,
D: Optional[DofsCollection] = None,
diag: float = 1.,
overwrite: bool = False) -> LinearSystem:
r"""Enforce degrees-of-freedom of a linear system.
An alternative to :func:`~skfem.utils.condense` which sets the matrix
diagonals to one and right-hand side vector to the enforced
degree-of-freedom value.
.. note::
The original system is both returned
(for compatibility with :func:`skfem.utils.solve`) and optionally (if
`overwrite`) modified (for performance).
Parameters
----------
A
The system matrix
b
Optionally, the right hand side vector.
x
The values of the enforced degrees-of-freedom. If not given, assumed
to be zero.
I
Specify either this or ``D``: The set of degree-of-freedom indices to
solve for.
D
Specify either this or ``I``: The set of degree-of-freedom indices to
enforce (rows/diagonal set to zero/one).
overwrite
Optionally, the original system is both modified (for performance) and
returned (for compatibility with :func:`skfem.utils.solve`). By
default, ``False``.
Returns
-------
LinearSystem
A linear system with the enforced rows/diagonals set to zero/one.
"""
b, x, I, D = _init_bc(A, b, x, I, D)
Aout = A if overwrite else A.copy()
# set rows on lhs to zero
start = Aout.indptr[D]
stop = Aout.indptr[D + 1]
count = stop - start
idx = np.ones(count.sum(), dtype=np.int64)
idx[np.cumsum(count)[:-1]] -= count[:-1]
idx = np.repeat(start, count) + np.cumsum(idx) - 1
Aout.data[idx] = 0.
# set diagonal value
d = Aout.diagonal()
d[D] = diag
Aout.setdiag(d)
if b is not None:
if isinstance(b, spmatrix):
# mass matrix (eigen- or initial value problem)
bout = enforce(b, D=D, diag=0., overwrite=overwrite)
else:
# set rhs to the given value
bout = b if overwrite else b.copy()
bout[D] = x[D]
return Aout, bout
return Aout
def penalize(A: spmatrix,
b: Optional[Union[ndarray, spmatrix]] = None,
x: Optional[ndarray] = None,
I: Optional[DofsCollection] = None,
D: Optional[DofsCollection] = None,
epsilon: Optional[float] = None,
overwrite: bool = False) -> LinearSystem:
r"""Penalize degrees-of-freedom of a linear system.
Parameters
----------
A
The system matrix
b
Optionally, the right hand side vector.
x
The values of the penalized degrees-of-freedom. If not given, assumed
to be zero.
I
Specify either this or ``D``: The set of degree-of-freedom indices to
solve for.
D
Specify either this or ``I``: The set of degree-of-freedom indices to
enforce (rows/diagonal set to zero/one).
epsilon
Very small value, the reciprocal of which penalizes deviations from
the Dirichlet condition
overwrite
Optionally, the original system is both modified (for performance) and
returned (for compatibility with :func:`skfem.utils.solve`). By
default, ``False``.
Returns
-------
LinearSystem
A linear system with the penalized diagonal and RHS entries set to
very large values, 1/epsilon and x/epsilon, respectively.
"""
b, x, I, D = _init_bc(A, b, x, I, D)
Aout = A if overwrite else A.copy()
d = Aout.diagonal()
if epsilon is None:
epsilon = 1e-10 / np.linalg.norm(d[D], np.inf)
d[D] = 1/epsilon
Aout.setdiag(d)
if b is None:
return Aout
bout = b if overwrite else b.copy()
# Nothing needs doing for mass matrix, but RHS vector needs penalty factor
if not isinstance(b, spmatrix):
bout[D] = x[D] / epsilon
return Aout, bout
def condense(A: spmatrix,
b: Optional[Union[ndarray, spmatrix]] = None,
x: Optional[ndarray] = None,
I: Optional[DofsCollection] = None,
D: Optional[DofsCollection] = None,
expand: bool = True) -> CondensedSystem:
r"""Eliminate degrees-of-freedom from a linear system.
The user should provide the linear system ``A`` and ``b``
and either the set of DOFs to eliminate (``D``) or the set
of DOFs to keep (``I``). Optionally, nonzero values for
the eliminated DOFs can be supplied via ``x``.
.. note::
Supports also generalized eigenvalue problems
where ``b`` is a matrix.
Example
-------
Suppose that the solution vector :math:`x` can be
split as
.. math::
x = \begin{bmatrix}
x_I\\
x_D
\end{bmatrix}
where :math:`x_D` are known and :math:`x_I` are unknown. This allows
splitting the linear system as
.. math::
\begin{bmatrix}
A_{II} & A_{ID}\\
A_{DI} & A_{DD}
\end{bmatrix}
\begin{bmatrix}
x_I\\
x_D
\end{bmatrix}
=
\begin{bmatrix}
b_I\\
b_D
\end{bmatrix}
which leads to the condensed system
.. math::
A_{II} x_I = b_I - A_{ID} x_D.
As an example, let us assemble the matrix :math:`A` and the vector
:math:`b` corresponding to the Poisson equation :math:`-\Delta u = 1`.
.. doctest::
>>> import skfem as fem
>>> from skfem.models.poisson import laplace, unit_load
>>> m = fem.MeshTri().refined(2)
>>> basis = fem.CellBasis(m, fem.ElementTriP1())
>>> A = laplace.assemble(basis)
>>> b = unit_load.assemble(basis)
The condensed system is obtained with :func:`skfem.utils.condense`. Below
we provide the DOFs to eliminate via the keyword argument
``D``.
.. doctest::
>>> AII, bI, xI, I = fem.condense(A, b, D=m.boundary_nodes())
>>> AII.toarray()
array([[ 4., 0., 0., 0., -1., -1., -1., -1., 0.],
[ 0., 4., 0., 0., -1., 0., -1., 0., 0.],
[ 0., 0., 4., 0., 0., -1., 0., -1., 0.],
[ 0., 0., 0., 4., -1., -1., 0., 0., 0.],
[-1., -1., 0., -1., 4., 0., 0., 0., 0.],
[-1., 0., -1., -1., 0., 4., 0., 0., 0.],
[-1., -1., 0., 0., 0., 0., 4., 0., -1.],
[-1., 0., -1., 0., 0., 0., 0., 4., -1.],
[ 0., 0., 0., 0., 0., 0., -1., -1., 4.]])
>>> bI
array([0.0625, 0.0625, 0.0625, 0.0625, 0.0625, 0.0625, 0.0625, 0.0625,
0.0625])
By default, the eliminated DOFs are set to zero.
Different values can be provided through the keyword argument ``x``;
see :ref:`ex14`.
Parameters
----------
A
The system matrix
b
The right hand side vector, or zero if x is given, or the mass matrix
for generalized eigenvalue problems.
x
The values of the condensed degrees-of-freedom. If not given, assumed
to be zero.
I
The set of degree-of-freedom indices to include.
D
The set of degree-of-freedom indices to dismiss.
expand
If ``True`` (default), returns also `x` and `I`. As a consequence,
:func:`skfem.utils.solve` will expand the solution vector
automatically.
Returns
-------
CondensedSystem
The condensed linear system and (optionally) information about
the boundary values.
"""
b, x, I, D = _init_bc(A, b, x, I, D)
ret_value: CondensedSystem = (None,)
if b is None:
ret_value = (A[I].T[I].T,)
else:
if isinstance(b, spmatrix):
# generalized eigenvalue problem: don't modify rhs
Aout = A[I].T[I].T
bout = b[I].T[I].T
elif isinstance(b, ndarray):
Aout = A[I].T[I].T
bout = b[I] - A[I].T[D].T @ x[D]
else:
raise Exception("Type of second arg not supported.")
ret_value = (Aout, bout)
if expand:
ret_value += (x, I)
return ret_value if len(ret_value) > 1 else ret_value[0]
# additional utilities
def rcm(A: spmatrix,
b: ndarray) -> Tuple[spmatrix, ndarray, ndarray]:
"""Reverse Cuthill-McKee ordering."""
p = spg.reverse_cuthill_mckee(A, symmetric_mode=False)
return A[p].T[p].T, b[p], p
def adaptive_theta(est, theta=0.5, max=None):
"""For choosing which elements to refine in an adaptive strategy."""
if max is None:
return np.nonzero(theta * np.max(est) < est)[0]
else:
return np.nonzero(theta * max < est)[0]
def projection(fun,
basis_to: Optional[AbstractBasis] = None,
basis_from: Optional[AbstractBasis] = None,
diff: Optional[int] = None,
I: Optional[ndarray] = None,
expand: bool = False) -> ndarray:
"""Perform projections onto a finite element basis.
Parameters
----------
fun
A solution vector or a function handle.
basis_to
The finite element basis to project to.
basis_from
The finite element basis to project from.
diff
Differentiate with respect to the given dimension.
I
Index set for limiting the projection to a subset.
expand
Passed to :func:`skfem.utils.condense`.
Returns
-------
ndarray
The projected solution vector.
"""
@BilinearForm
def mass(u, v, w):
from skfem.helpers import dot, ddot
p = 0
if len(u.value.shape) == 2:
p = u * v
elif len(u.value.shape) == 3:
p = dot(u, v)
elif len(u.value.shape) == 4:
p = ddot(u, v)
return p
if isinstance(fun, LinearForm):
funv = fun
else:
@LinearForm
def funv(v, w):
p = fun(w.x) * v
return sum(p) if isinstance(basis_to.elem, ElementVector) else p
@BilinearForm
def deriv(u, v, w):
from skfem.helpers import grad
du = grad(u)
return du[diff] * v
M = asm(mass, basis_to)
if not isinstance(fun, ndarray):
f = asm(funv, basis_to)
else:
if diff is not None:
f = asm(deriv, basis_from, basis_to) @ fun
else:
f = asm(mass, basis_from, basis_to) @ fun
if I is not None:
return solve_linear(*condense(M, f, I=I, expand=expand))
return solve_linear(M, f)
def project(fun,
basis_from: Optional[AbstractBasis] = None,
basis_to: Optional[AbstractBasis] = None,
diff: Optional[int] = None,
I: Optional[ndarray] = None,
expand: bool = False) -> ndarray:
warn("project is deprecated in favor of projection.",
DeprecationWarning)
return projection(
fun,
basis_to=basis_to,
basis_from=basis_from,
diff=diff,
I=I,
expand=expand,
)