| 1 |
% function [MAT,NODES,DN]=laplacianfun(NPTS,[BCS]) |
| 2 |
% |
| 3 |
% Generates a discretized Laplacian operator in any arbitrarily |
| 4 |
% large number of dimensions. |
| 5 |
% Input: |
| 6 |
% NPTS - Vector containing the number of points per dimension of |
| 7 |
% the discretization |
| 8 |
% [BCS] - Boundary conditions for each variable. 0 [default] |
| 9 |
% gives dirchlet (to a point not included in our grid). 1 |
| 10 |
% gives periodic bc's in that variable. |
| 11 |
% Output: |
| 12 |
% MAT - Discretized Laplacian |
| 13 |
% NODES - Location of the nodes |
| 14 |
% DN - Number of dirchlet neighbors per node (does not work |
| 15 |
% with Neuman bc's). |
| 16 |
% |
| 17 |
% by: Chris Siefert <[email protected]> |
| 18 |
% Last Update: 07/05/06 <siefert> |
| 19 |
function [MAT,NODES,DN]=laplacianfun(NPTS,varargin) |
| 20 |
|
| 21 |
% Check for BC's |
| 22 |
OPTS=nargin-1; |
| 23 |
if (OPTS >= 1) BCS=varargin{1}; |
| 24 |
else BCS=0*NPTS;end |
| 25 |
if (OPTS == 2) SCALE=varargin{2}; |
| 26 |
else SCALE=false;end |
| 27 |
|
| 28 |
% Pull Constants |
| 29 |
NDIM=length(NPTS); |
| 30 |
N=prod(NPTS); |
| 31 |
|
| 32 |
% Sanity check |
| 33 |
if (length(NPTS) ~= length(BCS)) fprintf('Error: Size mismatch between NPTS and BCS\n');return;end |
| 34 |
|
| 35 |
% Compute jumps (normal) |
| 36 |
JUMP=cumprod(NPTS);JUMP=[1,JUMP(1:NDIM)]; |
| 37 |
|
| 38 |
% Compute jumps (periodic) |
| 39 |
JP=JUMP(2:NDIM+1)-JUMP(1:NDIM); |
| 40 |
|
| 41 |
% Diagonal |
| 42 |
MAT=2*NDIM*speye(N,N); |
| 43 |
|
| 44 |
% Assembly |
| 45 |
for I=1:NDIM, |
| 46 |
VEC=repmat([ones(JUMP(I)*(NPTS(I)-1),1);zeros(JUMP(I),1)],N/JUMP(I+1),1); |
| 47 |
VEC=VEC(1:N-JUMP(I)); |
| 48 |
MAT=MAT-spdiags([zeros(JUMP(I),1);VEC],JUMP(I),N,N) - spdiags([VEC;zeros(JUMP(I),1)],-JUMP(I),N,N); |
| 49 |
if(BCS(I)==1) |
| 50 |
VEC=repmat([ones(JUMP(I),1);zeros(JUMP(I)*(NPTS(I)-1),1)],N/JUMP(I+1),1); |
| 51 |
VEC=VEC(1:N-JP(I)); |
| 52 |
MAT=MAT-spdiags([zeros(JP(I),1);VEC],JP(I),N,N) - spdiags([VEC;zeros(JP(I),1)],-JP(I),N,N); |
| 53 |
end |
| 54 |
end |
| 55 |
|
| 56 |
% Nodal Location |
| 57 |
NODES=[1:NPTS(1)]'; |
| 58 |
for I=2:NDIM, |
| 59 |
SZ=size(NODES,1); |
| 60 |
NODES=[repmat(NODES,NPTS(I),1),reshape(repmat(1:NPTS(I),SZ,1),SZ*NPTS(I),1)]; |
| 61 |
end |
| 62 |
|
| 63 |
% Dirchlet Neighbors |
| 64 |
DEG=sum(abs(MAT)>0,2); |
| 65 |
DN=full(max(DEG)-DEG); |