-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathse2d_vecdiff_matrices.m
250 lines (206 loc) · 8.23 KB
/
se2d_vecdiff_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
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
function [DDall_uu, DDall_uv, DDall_vu, DDall_vv, VDall_uu, VDall_uv, VDall_vu, VDall_vv] = se2d_vecdiff_matrices(npts)
% se2d_deriv_matrices - Generate matrices for the spectral element
% divergence damping and vorticity damping operators.
%
% Syntax: [DDall_uu, DDall_uv, DDall_vu, DDall_vv, VDall_uu, VDall_uv, VDall_vu, VDall_vv] = se2d_vecdiff_matrices(npts)
%
% Inputs:
% npts - Order of the spectral element method
%
% Outputs:
% DDall_uu - Matrix of coefficients for the divergence damping operator
% affecting the u field in terms of the u field for all
% neigbhoring spectral elements (npts-1 x npts-1 x 3 x 3)
% DDall_uv - Matrix of coefficients for the divergence damping operator
% affecting the u field in terms of the v field for all
% neigbhoring spectral elements (npts-1 x npts-1 x 3 x 3)
% DDall_vu - Matrix of coefficients for the divergence damping operator
% affecting the v field in terms of the u field for all
% neigbhoring spectral elements (npts-1 x npts-1 x 3 x 3)
% DDall_vv - Matrix of coefficients for the divergence damping operator
% affecting the v field in terms of the v field for all
% neigbhoring spectral elements (npts-1 x npts-1 x 3 x 3)
% VDall_uu - Matrix of coefficients for the vorticity damping operator
% affecting the u field in terms of the u field for all
% neigbhoring spectral elements (npts-1 x npts-1 x 3 x 3)
% VDall_uv - Matrix of coefficients for the vorticity damping operator
% affecting the u field in terms of the v field for all
% neigbhoring spectral elements (npts-1 x npts-1 x 3 x 3)
% VDall_vu - Matrix of coefficients for the vorticity damping operator
% affecting the v field in terms of the u field for all
% neigbhoring spectral elements (npts-1 x npts-1 x 3 x 3)
% VDall_vv - Matrix of coefficients for the vorticity damping operator
% affecting the v field in terms of the v field for all
% neigbhoring 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:
% >> [DDall_uu, DDall_uv, DDall_vu, DDall_vv, VDall_uu, VDall_uv,
% VDall_vu, VDall_vv] = se2d_vecdiff_matrices(4);
%
% Author: Paul Ullrich
% University of California, Davis
% Email address: [email protected]
% Last revision: 02-Feb-2018
%------------- BEGIN CODE --------------
if (~isscalar(npts))
error('npts argument must be a scalar');
end
if (~exist('xonly'))
xonly = 0;
end
if ((xonly ~= 0) && (xonly ~= 1))
error('invalid value for agument xonly; value should be 0 or 1');
end
% Compute Gauss-Legendre-Lobatto points on [0,1] interval
[myroots,myweights] = lglnodes(npts,0,1);
% Find coefficients of characteristic polynomials
[~,difflegpoly] = characteristic_polyfit(myroots);
% Obtain mass matrix
M = zeros(npts*npts,1);
for n = 1:npts
for m = 1:npts
nbasis = (m-1)*npts+n;
M(nbasis) = myweights(n) * myweights(m);
if ((n == 1) || (n == npts))
M(nbasis) = M(nbasis) * 2;
end
if ((m == 1) || (m == npts))
M(nbasis) = M(nbasis) * 2;
end
end
end
% Construct open curl and divergence operator
Curl = zeros(npts*npts,2*npts*npts);
Div = zeros(npts*npts,2*npts*npts);
for n = 1:npts
for m = 1:npts
nbasis = (m-1)*npts+n;
for s = 1:npts
mbasis = (m-1)*npts+s;
% DxU
Div(nbasis,mbasis) = polyval(difflegpoly(s,:), myroots(n));
% - DxU
Curl(nbasis,npts*npts+mbasis) = polyval(difflegpoly(s,:), myroots(n));
mbasis = (s-1)*npts+n;
% DyV
Div(nbasis,npts*npts+mbasis) = polyval(difflegpoly(s,:), myroots(m));
% DyU
Curl(nbasis,mbasis) = - polyval(difflegpoly(s,:), myroots(m));
end
end
end
% Construct closed gradient operator
Grad = zeros(2*npts*npts,npts*npts);
GradT = zeros(2*npts*npts,npts*npts);
for n = 1:npts
for m = 1:npts
nbasis = (m-1)*npts+n;
for s = 1:npts
% Dx component of grad
mbasis = (m-1)*npts+s;
Grad(nbasis,mbasis) = - polyval(difflegpoly(n,:), myroots(s)) * myweights(s) / myweights(n);
% Dy component of grad
mbasis = (s-1)*npts+n;
Grad(npts*npts+nbasis,mbasis) = - polyval(difflegpoly(m,:), myroots(s)) * myweights(s) / myweights(m);
end
end
end
GradT(1:npts*npts,:) = - Grad(npts*npts+1:2*npts*npts,:);
GradT(npts*npts+1:2*npts*npts,:) = Grad(1:npts*npts,:);
% Construct divergence damping operator
DDlocal = Grad * Div;
% Construct vorticity damping operator
VDlocal = GradT * Curl;
% Apply DSS
DD = apply_DSS_vector(npts, DDlocal);
VD = apply_DSS_vector(npts, VDlocal);
% Extract submatrices
npx = npts*npts;
DDall_uu = extract_submatrices(npts, DD(1:npx,1:npx));
DDall_uv = extract_submatrices(npts, DD(1:npx,npx+1:2*npx));
DDall_vu = extract_submatrices(npts, DD(npx+1:2*npx,1:npx));
DDall_vv = extract_submatrices(npts, DD(npx+1:2*npx,npx+1:2*npx));
VDall_uu = extract_submatrices(npts, VD(1:npx,1:npx));
VDall_uv = extract_submatrices(npts, VD(1:npx,npx+1:2*npx));
VDall_vu = extract_submatrices(npts, VD(npx+1:2*npx,1:npx));
VDall_vv = extract_submatrices(npts, VD(npx+1:2*npx,npx+1:2*npx));
myweights;
end
% Apply a virtual DSS operation to the vector operator DD
function DD = apply_DSS_vector(npts, DD)
npx = npts*npts;
DD(1:npx,1:npx) = apply_DSS(npts, DD(1:npx,1:npx));
DD(1:npx,npx+1:2*npx) = apply_DSS(npts, DD(1:npx,npx+1:2*npx));
DD(npx+1:2*npx,1:npx) = apply_DSS(npts, DD(npx+1:2*npx,1:npx));
DD(npx+1:2*npx,npx+1:2*npx) = apply_DSS(npts, DD(npx+1:2*npx,npx+1:2*npx));
end
% Apply a virtual DSS operation to the operator D
function D = apply_DSS(npts, D)
Dast = D;
% Mirror integrals along edges of constant y
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);
% Reweight
D(1:npts,1:npts*npts) = D(1:npts,1:npts*npts) / 2;
D((npts-1)*npts+1:npts*npts,1:npts*npts) = D((npts-1)*npts+1:npts*npts,1:npts*npts) / 2;
D(1:npts:npts*npts,1:npts*npts) = D(1:npts:npts*npts,1:npts*npts) / 2;
D(npts:npts:npts*npts,1:npts*npts) = D(npts:npts:npts*npts,1:npts*npts) / 2;
end
% Extract the submatrices of DD needed for linear analysis
function MDall = extract_submatrices(npts, MD)
% 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(:,1:(npts-1):(npts-1)*(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(1:(npts-1):(npts-1)*(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