|
a |
|
b/Cobiveco/functions/solveLaplace.m |
|
|
1 |
function u = solveLaplace(L, boundaryIds, boundaryVal, tol, maxit) |
|
|
2 |
% Computes the solution u of the Laplace equation L*u = 0 |
|
|
3 |
% with boundary conditions u(boundaryIds) = boundaryVal. |
|
|
4 |
% |
|
|
5 |
% u = solveLaplace(L, boundaryIds, boundaryVal, tol, maxit) |
|
|
6 |
% |
|
|
7 |
% Inputs: |
|
|
8 |
% L: Laplacian matrix computed with cotmatrix() of gptoolbox [numPoints x numPoints] |
|
|
9 |
% boundaryIds: point IDs for Dirichlet boundary conditions [numBoundaryPoints x 1] |
|
|
10 |
% boundaryVal: values for Dirichlet boundary conditions [numBoundaryPoints x 1] |
|
|
11 |
% tol: tolerance for bicgstab |
|
|
12 |
% maxit: maximum number of iterations for bicgstab |
|
|
13 |
% |
|
|
14 |
% Outputs: |
|
|
15 |
% u: Laplace solution [numPoints x 1] |
|
|
16 |
% |
|
|
17 |
% Written by Steffen Schuler, Institute of Biomedical Engineering, KIT |
|
|
18 |
|
|
|
19 |
if nargin < 5 |
|
|
20 |
maxit = 500; |
|
|
21 |
end |
|
|
22 |
if nargin < 4 |
|
|
23 |
tol = []; |
|
|
24 |
end |
|
|
25 |
|
|
|
26 |
if numel(boundaryIds) ~= numel(unique(boundaryIds)) |
|
|
27 |
error('Duplicate entries found in boundaryIds.'); |
|
|
28 |
end |
|
|
29 |
|
|
|
30 |
N = length(L); |
|
|
31 |
K = numel(boundaryIds); |
|
|
32 |
boundaryIds = double(boundaryIds); |
|
|
33 |
|
|
|
34 |
% set up right-hand side |
|
|
35 |
b = -L(:,boundaryIds)*boundaryVal(:); |
|
|
36 |
b(boundaryIds) = boundaryVal; |
|
|
37 |
|
|
|
38 |
% add boundary conditions to coefficient matrix |
|
|
39 |
L(boundaryIds,:) = sparse(1:K, boundaryIds, ones(K,1), K, N); |
|
|
40 |
L(:,boundaryIds) = sparse(boundaryIds, 1:K, ones(K,1), N, K); |
|
|
41 |
|
|
|
42 |
% apply reverse Cuthill-McKee reordering |
|
|
43 |
p = symrcm(L); |
|
|
44 |
L = L(p,p); |
|
|
45 |
b = b(p); |
|
|
46 |
|
|
|
47 |
% compute incomplete LU factorization |
|
|
48 |
[lowTri, upTri] = ilu(L); |
|
|
49 |
|
|
|
50 |
% solve linear system |
|
|
51 |
[x, flag, relres, iter] = bicgstab(L, b, tol, maxit, lowTri, upTri); |
|
|
52 |
if flag |
|
|
53 |
warning('bicgstab failed at iteration %i with flag %i and relative residual %.1e.', iter, flag, relres); |
|
|
54 |
end |
|
|
55 |
u = NaN(N,1); |
|
|
56 |
u(p) = x; |
|
|
57 |
|
|
|
58 |
end |