-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathstencil_FirstOrder.m
88 lines (78 loc) · 2 KB
/
stencil_FirstOrder.m
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
function [idx, vals] = stencil_FirstOrder(a, mode, j, d)
% Collection of stencils for first order approximation
% INPUT: a - (a)lgorithmic structure, necessary components are spatial differences and grid sizes
% mode - option to choose between 'Forward', 'Backward', 'Central'
% j - vector of global indices where to apply difference operator
% d - (d)imension controls the dimension in which the stencil works,
% i.e. d = 1 means d_xx, d = 2 means d_yy, etc.
% OUTPUT: idx - offset for assembling of differential operator
% vals - coefficients of size = (ndofs - 1) x 3
if nargin < 4 || isempty(d)
d = 1;
end % if nargin < 4
if nargin < 3 || isempty(j)
switch mode
case 'Forward'
switch d
case 1
j = setdiff(a.iall, a.ixup);
case 2
j = setdiff(a.iall, a.iyup);
case 3
j = setdiff(a.iall, a.izup);
end % switch d
case 'Backward'
switch d
case 1
j = setdiff(a.iall, a.ixlow);
case 2
j = setdiff(a.iall, a.iylow);
case 3
j = setdiff(a.iall, a.izlow);
end % switch d
case 'Central'
switch d
case 1
j = setdiff(a.iall, unique([a.ixlow; a.ixup]));
case 2
j = setdiff(a.iall, unique([a.iylow; a.iyup]));
case 3
j = setdiff(a.iall, unique([a.izlow; a.izup]));
end % switch d
end % switch mode
end % if nargin < 3 || isempty(j)
if size(a.XY,2)==1
h = a.Dx;
fak = 1;
else
switch d
case 1
h = a.Dx;
fak = (a.ny+1);
case 2
h = a.Dy;
fak = 1;
case 3
h = a.Dz;
fak = (a.nx+1) * (a.ny+1);
end % switch d
end % if size(a.XY,2)==1
% Prepare indices
jb = j - fak;
jf = j + fak;
% Forward first order stencil which is of order 1
if strcmp(mode,'Forward')
idx = [j, jf];
vals = 1./h(j) * [-1, 1];
end
% Backward first order stencil which is of order 1
if strcmp(mode,'Backward')
idx = [jb, j];
vals = 1./h(jb) * [-1, 1];
end
% Central first order stencil which is of order 2
if strcmp(mode,'Central')
idx = [jb, jf];
vals = 1./( h(jb) + h(j) ) * [-1, 1];
end
end % function