-
Notifications
You must be signed in to change notification settings - Fork 0
/
muscl_field_push.m
213 lines (178 loc) · 4.81 KB
/
muscl_field_push.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
function [Ex,Ey,Ez,Bx,By,Bz] = muscl_field_push(Ex,Ey,Ez,Bx,By,Bz,grid)
% Apply the muscl scheme to update the fields
% Build Q
Q = construct(Ex,Ey,Ez,Bx,By,Bz);
Nx = grid.Nx;
%Iterate over the domain (assumes periodic, needs BC call after this
%function)
R = mod( linspace(1,Nx,Nx), Nx-1) + 1;
L = mod( linspace(-1,Nx-2,Nx), Nx-1) + 1;
%Reconstruct Q (soln) within one cell via linear model
dQ = reconstruct(Q,Q(:,L),Q(:,R),grid);
%Update solution, primative variables
%Q_tilde = Q - grid.dt/(2*grid.dx)*AQ(Q,grid).*dQ;
A = AQ(grid.c);
Q_tilde = zeros(6,grid.Nx);
for i = 1:Nx
Q_tilde(:,i) = Q(:,i) - grid.dt/(2*grid.dx)*A*dQ(:,i);
end
%No need to enforce a positivity correction
[Q_plus_I, Q_minus_I] = edges_linear(Q_tilde,dQ);
[Q_plus_R, ~] = edges_linear(Q_tilde(:,R),dQ(:,R));
[~, Q_minus_L] = edges_linear(Q_tilde(:,L),dQ(:,L));
%Update the fluxes
F_R = Flux(Q_minus_I,Q_plus_R,grid);
F_L = Flux(Q_minus_L,Q_plus_I,grid);
%Compute the updated Q
Q = Q - grid.dt/(grid.dx)*(F_R - F_L);
% Destruct Q into it's components
[Ex,Ey,Ez,Bx,By,Bz] = destruct(Q);
end
%Locally defined functions for MUSCL-Handcock:
%Construct Q
function [Q] = construct(Ex,Ey,Ez,Bx,By,Bz)
Q = [Ex;Ey;Ez;Bx;By;Bz];
end
%Destruct Q into N, Ux, Uy, Uz
function [Ex,Ey,Ez,Bx,By,Bz] = destruct(Q)
Ex = Q(1,:);
Ey = Q(2,:);
Ez = Q(3,:);
Bx = Q(4,:);
By = Q(5,:);
Bz = Q(6,:);
end
%Fluxes for fields
function [Fl] = Flux(QL,QR,grid)
clight = grid.c;
[~,EyR,EzR,~,ByR,BzR] = destruct(QR);
[~,EyL,EzL,~,ByL,BzL] = destruct(QL);
% compute c for the lax flux
c = clight; % Always the maximum
Zero = zeros(size(BzL));
%Rusanov Flux
FL = [Zero;BzL*clight*clight;-ByL*clight*clight;Zero;-EzL;EyL];
FR = [Zero;BzR*clight*clight;-ByR*clight*clight;Zero;-EzR;EyR];
Fl = (1/2) * ( FR + FL - c.*( QR - QL ) );
end
%Reconstruction
function [dW] = reconstruct(Wi,Wm,Wp,grid)
%Option:
option = "eigenvector";
%Average Dw
if option == "eigenvector"
% Compute eigenvectors time differences
clight = grid.c;
L = left_eigenvector(clight);
R = right_eigenvector(clight);
%Iterate through the grid
Nx = grid.Nx;
dW = zeros(6,Nx);
for i = 1:Nx
%Compute the differences with the primative vars
% (Matrix Multiplications)
delta_i_minus_1 = L*(Wi(:,i) - Wm(:,i));
delta_i = L*(Wp(:,i) - Wi(:,i));
dW(:,i) = R*ave( delta_i_minus_1, delta_i );
end
else %Standard Option
dW = ave( Wi - Wm, Wp - Wi );
end
end
% Averaging
function [dW] = ave( Wm, Wp )
avg_type = "minmod"; %"zero"; %"minmod_more_diff"; %"minmod"; %"Supebee"; %"standard";
a = Wm; b = Wp;
ab = a.*b;
sz_a = size(a);
dW = zeros(sz_a);
% Standard Averaging
if avg_type == "standard"
dW = (Wm + Wp)/2;
elseif avg_type == "zero"
dW = zeros(sz_a);
elseif avg_type == "minmod_more_diff"
for i = 1:sz_a(1)
for j = 1:sz_a(2)
if ab(i,j) > 0
dW(i,j) = minmod( [(a(i,j) + b(i,j))/2 ,a(i,j), b(i,j)] ); %minmod( [(a(i,j) + b(i,j))/2 , a(i,j), b(i,j)] );
else
dW(i,j) = 0;
end
end
end
elseif avg_type == "minmod"
for i = 1:sz_a(1)
for j = 1:sz_a(2)
if ab(i,j) > 0
dW(i,j) = minmod( [(a(i,j) + b(i,j))/2 , 2*a(i,j), 2*b(i,j)] );
else
dW(i,j) = 0;
end
end
end
elseif avg_type == "Supebee"
for i = 1:sz_a(1)
for j = 1:sz_a(2)
if ab(i,j) > 0
max_v = maxmod( [ a(i,j) , b(i,j) ] );
min_v = minmod ( [ 2*a(i,j), 2*b(i,j)] );
dW(i,j) = minmod( [ max_v , min_v ] );
else
dW(i,j) = 0;
end
end
end
end
end
% Minmod used for averaging
function [val] = minmod(a)
if (max(a) > 0) && (min(a) > 0)
val = min(a);
elseif (max(a) < 0) && (min(a) < 0)
val = max(a);
else
val = 0;
end
end
%Maxmod used for averaging
function [val] = maxmod(a)
if (max(a) > 0) && (min(a) > 0)
val = max(a);
elseif (max(a) < 0) && (min(a) < 0)
val = min(a);
else
val = 0;
end
end
% Left eigenvectors (Matrix form)
function [L] = left_eigenvector(c)
L = [[0, 1/2, 0, 0, 0, -c/2];...
[0, 0, 1/2, 0, c/2, 0];...
[0, 1/2, 0, 0, 0, c/2];...
[0, 0, 1/2, 0, -c/2, 0];...
[1, 0, 0, 0, 0, 0];...
[0, 0, 0, 1, 0, 0]];
end
% Right eigenvectors (Matrix form)
function [R] = right_eigenvector(c)
R = [[0, 0, 0, 0, 1, 0];...
[1, 0, 1, 0, 0, 0];...
[0, 1, 0, 1, 0, 0];...
[0, 0, 0, 0, 0, 1];...
[0, 1/c, 0, -1/c, 0, 0];...
[-1/c, 0, 1/c, 0, 0, 0]];
end
function [W_plus, W_minus] = edges_linear(W_tilde,dW)
W_plus = W_tilde - dW/2;
W_minus = W_tilde + dW/2;
end
% Function A(Q)
function [A] = AQ(c)
A = [[0, 0, 0, 0, 0, 0];...
[0, 0, 0, 0, 0, c^2];...
[0, 0, 0, 0, -c^2, 0];...
[0, 0, 0, 0, 0, 0];...
[0, 0, -1, 0, 0, 0];...
[0, 1, 0, 0, 0, 0]];
end