-
Notifications
You must be signed in to change notification settings - Fork 1
/
se2d_deriv_matrices.m
170 lines (142 loc) · 4.73 KB
/
se2d_deriv_matrices.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
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
function [MDx,MDy] = se2d_deriv_matrices(npts)
% se2d_deriv_matrices - Generate matrices for the spectral element
% first derivative operator in the x and y coordinate directions.
%
% Syntax: [MDx,MDy] = se2d_deriv_matrices(npts)
%
% Inputs:
% npts - Order of the spectral element method
%
% Outputs:
% MDx - Matrix of coefficients for the x derivative operator for all
% neighboring spectral elements (npts-1 x npts-1 x 3 x 3)
% MDy - Matrix of coefficients for the y derivative operator for all
% neighboring spectral elements (npts-1 x npts-1 x 3 x 3)
%
% Remarks:
% Degrees of freedom within the 2D spectral element are first given in
% the x direction, then in the y direction.
%
% Example usage:
% >> [MDx,MDy] = se2d_deriv_matrices(4);
%
% Author: Paul Ullrich
% University of California, Davis
% Email address: paullrich@ucdavis.edu
% Last revision: 02-Feb-2018
%------------- BEGIN CODE --------------
if (~isscalar(npts))
error('npts argument must be a scalar');
end
% Compute Gauss-Legendre-Lobatto points on [0,1] interval
[myroots,myweights] = lglnodes(npts,0,1);
% Find coefficients of characteristic polynomials
[legpoly,difflegpoly] = characteristic_polyfit(myroots);
% Obtain mass matrix
M = zeros(npts*npts,1);
Mast = zeros(npts*npts,1);
for n = 1:npts
for m = 1:npts
nbasis = (m-1)*npts+n;
M(nbasis) = myweights(n) * myweights(m);
Mast(nbasis) = M(nbasis);
if ((n == 1) || (n == npts))
M(nbasis) = M(nbasis) * 2;
end
if ((m == 1) || (m == npts))
M(nbasis) = M(nbasis) * 2;
end
end
end
% Obtain differentiation matrix
Dx = zeros(npts*npts,npts*npts);
Dy = zeros(npts*npts,npts*npts);
for n = 1:npts
for m = 1:npts
nbasis = (m-1)*npts+n;
for s = 1:npts
for t = 1:npts
mbasis = (t-1)*npts+s;
if (m == t)
for p = 1:npts
Dx(nbasis,mbasis) = Dx(nbasis,mbasis) ...
+ myweights(p) * myweights(m) ...
* polyval(difflegpoly(n,:), myroots(p)) ...
* polyval(legpoly(s,:), myroots(p));
end
end
if (n == s)
for q = 1:npts
Dy(nbasis,mbasis) = Dy(nbasis,mbasis) ...
+ myweights(n) * myweights(q) ...
* polyval(difflegpoly(m,:), myroots(q)) ...
* polyval(legpoly(t,:), myroots(q));
end
end
end
end
end
end
MDx = se_extract_submatrices(M, Dx, npts);
MDy = se_extract_submatrices(M, Dy, npts);
myweights;
end
% Extract the submatrices
function MDall = se_extract_submatrices(M, D, npts)
Dast = D;
D(1:npts,1:npts) = D(1:npts,1:npts) + Dast((npts-1)*npts+1:npts*npts,(npts-1)*npts+1:npts*npts);
D((npts-1)*npts+1:npts*npts,(npts-1)*npts+1:npts*npts) = D((npts-1)*npts+1:npts*npts,(npts-1)*npts+1:npts*npts) + Dast(1:npts,1:npts);
% Mirror integrals along edges of constant x
D(1:npts:npts*npts,1:npts:npts*npts) = D(1:npts:npts*npts,1:npts:npts*npts) + Dast(npts:npts:npts*npts,npts:npts:npts*npts);
D(npts:npts:npts*npts,npts:npts:npts*npts) = D(npts:npts:npts*npts,npts:npts:npts*npts) + Dast(1:npts:npts*npts,1:npts:npts*npts);
% Mirror integrals diagonally
D(1,1) = D(1,1) + Dast(npts*npts,npts*npts);
D(npts*npts,npts*npts) = D(npts*npts,npts*npts) + Dast(1,1);
D(npts,npts) = D(npts,npts) + Dast((npts-1)*npts+1,(npts-1)*npts+1);
D((npts-1)*npts+1,(npts-1)*npts+1) = D((npts-1)*npts+1,(npts-1)*npts+1) + Dast(npts,npts);
% Obtain product
MD = inv(diag(M)) * D;
% Extract unidirectional (in Y) submatrices
MD1 = MD(1:(npts-1)*npts,1:(npts-1)*npts);
MD0 = zeros(size(MD1));
MD0(1:npts,:) = MD((npts-1)*npts+1:npts*npts,1:(npts-1)*npts);
MD2 = zeros(size(MD1));
MD2(:,1:npts) = MD(1:(npts-1)*npts,(npts-1)*npts+1:npts*npts);
% Extract all submatrices
MD00 = zeros((npts-1)*(npts-1));
MD01 = zeros((npts-1)*(npts-1));
MD02 = zeros((npts-1)*(npts-1));
MD10 = zeros((npts-1)*(npts-1));
MD11 = zeros((npts-1)*(npts-1));
MD12 = zeros((npts-1)*(npts-1));
MD20 = zeros((npts-1)*(npts-1));
MD21 = zeros((npts-1)*(npts-1));
MD22 = zeros((npts-1)*(npts-1));
ix1 = [];
for n = 1:npts-1
ix1 = [ix1 (n-1)*npts+1:n*npts-1];
end
ix0 = [];
for n = 1:npts-1
ix0 = [ix0 n*npts];
end
MD00(ix0-npts+1,:) = MD0(ix0,ix1);
MD01 = MD0(ix1,ix1);
MD02(:,ix0-npts+1) = MD0(ix1,ix0);
MD10(1:(npts-1):(npts-1)*(npts-1),:) = MD1(ix0,ix1);
MD11 = MD1(ix1,ix1);
MD12(:,1:(npts-1):(npts-1)*(npts-1)) = MD1(ix1,ix0);
MD20(ix0-npts+1,:) = MD2(ix0,ix1);
MD21 = MD2(ix1,ix1);
MD22(:,ix0-npts+1) = MD2(ix1,ix0);
MDall = zeros((npts-1)*(npts-1),(npts-1)*(npts-1),3,3);
MDall(:,:,1,1) = MD00;
MDall(:,:,1,2) = MD01;
MDall(:,:,1,3) = MD02;
MDall(:,:,2,1) = MD10;
MDall(:,:,2,2) = MD11;
MDall(:,:,2,3) = MD12;
MDall(:,:,3,1) = MD20;
MDall(:,:,3,2) = MD21;
MDall(:,:,3,3) = MD22;
end