-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfluid_grad_U_fwaves_Eulderink.m
378 lines (323 loc) · 9.25 KB
/
fluid_grad_U_fwaves_Eulderink.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
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%Name: Grant Johnson
%Date: 5/17/2023
%Fluid algroithm, V (divergence U)
%Notes:
%-1D
% F_wave Scheme: Leveque Dust Preprint (2003)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%Update the quanitites Ux, Uy, Uz (t -> t + dt)
function [Ux,Uy,Uz,Vx,Vy,Vz,N,grid] = fluid_grad_U_fwaves_Eulderink(Ux,Uy,Uz,N,grid)
% Build Q
U_prim = construct_prim(N, Ux, Uy, Uz);
Q = construct_cons(N, Ux, Uy, Uz);
Nx = grid.Nx;
%Iterate over the domain
% I = linspace(1,Nx-1,Nx-1); %DEFAULT
if grid.iter <= 2
grid.R = mod( linspace(1,Nx,Nx), Nx-1) + 1; %mod( linspace(1,Nx,Nx), Nx) + 1; %Good
grid.L = mod( linspace(-1,Nx-2,Nx), Nx-1) + 1; %mod( linspace(-1,Nx-2,Nx), Nx) + 1; %Good
end
R = grid.R;
L = grid.L;
%Compute u_hat at the interfaces (L: i-1/2, R: i+1/2)
%u_hat_L_vector = non_linear_solve(N,Ux,Uy,Uz,L,Nx,grid.c);
vhat = v_hat_via_eulderink_cold_limit(N,Ux,Uy,Uz,L,grid.c);
% diagnostic for v_hat
diag_vhat(vhat,Ux,Uy,Uz,grid);
%Calulcate fluxes:
FG_L = Flux_Godunov(U_prim,vhat,grid);
FG_R = FG_L(:,R);
%Calculate the corrector Fluxes
[FCorr_L,grid] = Flux_Corr(U_prim,vhat,grid);
FCorr_R = FCorr_L(:,R);
%Compute the updated U
FR = FG_R + FCorr_R;
FL = FG_L + FCorr_L;
Q_new = Q - grid.dt/(grid.dx)*(FR - FL);
% Check if the density fails the positivity test
[N_new, ~, ~, ~] = destruct_cons(grid,Q_new);
if min(N_new) < 0
% X = (N_new<0);
fprintf("Second order failed in iteration: %d\n",grid.iter)
% for i = 1:Nx
% if X(i) == 1
% FCorr_R(:,i) = zeros(4,1); %FG_R(X(i));
% FCorr_L(:,i) = zeros(4,1); %FG_L(X(i));
% FCorr_R(:,i-1) = zeros(4,1); %FG_R(X_L(i));
% FCorr_L(:,i+1) = zeros(4,1); %FG_L(X_R(i));
% end
% end
% FR = FG_R + FCorr_R;
% FL = FG_L + FCorr_L;
end
%Push with the fluxes
Q = Q - grid.dt/(grid.dx)*(FR - FL);
% Destruct Q into it's components
[N, Ux, Uy, Uz] = destruct_cons(grid,Q);
%Save the output
gamma = sqrt(1+(Ux.*Ux + Uy.*Uy + Uz.*Uz)/(grid.c*grid.c));
Vx = Ux./gamma;
Vy = Uy./gamma;
Vz = Uz./gamma;
end
function diag_vhat(vhat,Ux,Uy,Uz,grid)
if mod(grid.iter,1000) == 0
figure2 = figure();
gamma = sqrt(1 + (Ux.^2 + Uy.^2 + Uz.^2)/(grid.c^2));
v = Ux./gamma;
plot(grid.x2(grid.L), vhat, '.black')
hold on
plot(grid.x1, v, '*red')
title('v-hat diagnostic')
xlabel('x-coord.')
ylabel('v.')
legend('v-hat', 'Vx')
pause(5)
close(figure2);
end
end
function [vhat] = v_hat_via_eulderink_cold_limit(N,Ux,Uy,Uz,L,c_light)
% Nonlinear solve:
%Constants
c = c_light;
rho_max = max(N(L),N);
rhoL = N(L)./rho_max;
rhoR = N./rho_max;
% SCALE
ulx = Ux(L)/c;
urx = Ux/c;
uly = Uy(L)/c;
ury = Uy/c;
ulz = Uz(L)/c;
urz = Uz/c;
c = 1.0;
gammal = sqrt(1 + (ulx.*ulx + uly.*uly + ulz.*ulz )/(c*c));
gammar = sqrt(1 + (urx.*urx + ury.*ury + urz.*urz )/(c*c));
vlx = ulx./gammal;
vrx = urx./gammar;
%vly = uly./gammal;
%vry = ury./gammar;
%vlz = ulz./gammal;
%vrz = urz./gammar;
rhol_prim = rhoL./gammal;
rhor_prim = rhoR./gammar;
%Avg state:
%k = sqrt(rhol_prim) + sqrt(rhor_prim);
w0 = sqrt(rhol_prim).*gammal + sqrt(rhor_prim).*gammar;
w1 = sqrt(rhol_prim).*gammal.*vlx + sqrt(rhor_prim).*gammar.*vrx;
%w2 = sqrt(rhol_prim).*gammal.*vly + sqrt(rhor_prim).*gammar.*vry;
%w3 = sqrt(rhol_prim).*gammal.*vlz + sqrt(rhor_prim).*gammar.*vrz;
vhat = c_light*w1./w0;
% BC
Nx = max(size(vhat));
vhat(Nx-3:Nx) = vhat(Nx-3:Nx)*0.0;
end
%Locally defined functions for f-wave:
%Construct Q
function [Q] = construct_prim(N, Ux, Uy, Uz)
Q = [N; Ux; Uy; Uz];
end
%Destruct Q into N, Ux, Uy, Uz
function [N, Ux, Uy, Uz] = destruct_prim(Q)
N = Q(1,:);
Ux = Q(2,:);
Uy = Q(3,:);
Uz = Q(4,:);
end
%Construct Q
function [Q] = construct_cons(N, Ux, Uy, Uz)
Q = [N; (N.*Ux); (N.*Uy); (N.*Uz)];
end
%Destruct Q into N, Ux, Uy, Uz
function [N, Ux, Uy, Uz] = destruct_cons(grid,Q)
N = Q(1,:);
Ux = Q(2,:)./N;
Uy = Q(3,:)./N;
Uz = Q(4,:)./N;
% Positivity Limiter
Ux(N<=0) = 0;
Uy(N<=0) = 0;
Uz(N<=0) = 0;
N(N<=0) = 1.e10;
end
%Fluxes Godunov ( u_hat(i) | Q(i) )
function [Fl] = Flux_Godunov(Q,vhat,grid)
%Grab the elements of Q
[N, Ux, Uy, Uz] = destruct_prim(Q);
%Grab individual ui, ui-1
gamma = sqrt(1+(Ux.*Ux + Uy.*Uy + Uz.*Uz)/(grid.c*grid.c));
Vx = Ux./gamma;
L = grid.L;
%Build Flux Array
Fl = zeros(min(size(Q)),grid.Nx);
% Loop over the grid, making the fluxes
for i = 1:grid.Nx
if Ux(L(i)) < 0 && 0 < Ux(i)
Fl(:,i) = [0;0;0;0];
else
if vhat(i) > 0
% f(Q_{i-1})
F1 = N(L(i))*Vx(L(i));
F2 = N(L(i))*Vx(L(i))*Ux(L(i));
F3 = N(L(i))*Vx(L(i))*Uy(L(i));
F4 = N(L(i))*Vx(L(i))*Uz(L(i));
elseif vhat(i) == 0
% 0.5* ( f(Q_{i-1}) + f(Q_{i}) )
F1i = N(i)*Vx(i);
F2i = N(i)*Vx(i)*Ux(i);
F3i = N(i)*Vx(i)*Uy(i);
F4i = N(i)*Vx(i)*Uz(i);
F1m = N(L(i))*Vx(L(i));
F2m = N(L(i))*Vx(L(i))*Ux(L(i));
F3m = N(L(i))*Vx(L(i))*Uy(L(i));
F4m = N(L(i))*Vx(L(i))*Uz(L(i));
F1 = 0.5*(F1i + F1m);
F2 = 0.5*(F2i + F2m);
F3 = 0.5*(F3i + F3m);
F4 = 0.5*(F4i + F4m);
elseif vhat(i) < 0
% f(Q_{i})
F1 = N(i)*Vx(i);
F2 = N(i)*Vx(i)*Ux(i);
F3 = N(i)*Vx(i)*Uy(i);
F4 = N(i)*Vx(i)*Uz(i);
end
Fl(:,i) = [F1;F2;F3;F4];
end
end
end
%Fluxes Godunov ( Q(i-1) | u_hat(i) | Q(i) )
function [Fl,grid] = Flux_Corr(Q,vhat,grid)
% mw number of waves
%Grab the elements of Q
[N, Ux, Uy, Uz] = destruct_prim(Q);
%Grab individual ui, ui-1
gamma = sqrt(1+(Ux.*Ux + Uy.*Uy + Uz.*Uz)/(grid.c*grid.c));
Vx = Ux./gamma;
L = grid.L;
R = grid.R;
%Build Flux Array
Fl = zeros(min(size(Q)),grid.Nx);
Z1 = zeros(min(size(Q)),grid.Nx);
Z2 = zeros(min(size(Q)),grid.Nx);
s1 = zeros(1,grid.Nx);
s2 = zeros(1,grid.Nx);
% Loop over the grid, making the fluxes
for i = 1:grid.Nx
if Ux(L(i)) < 0 && 0 < Ux(i)
Z1_1 = - N(L(i))*Vx(L(i));
Z1_2 = - N(L(i))*Vx(L(i))*Ux(L(i));
Z1_3 = - N(L(i))*Vx(L(i))*Uy(L(i));
Z1_4 = - N(L(i))*Vx(L(i))*Uz(L(i));
s1(i) = Vx(L(i));
Z1(:,i) = [Z1_1;Z1_2;Z1_3;Z1_4];
Z2_1 = N(i)*Vx(i);
Z2_2 = N(i)*Vx(i)*Ux(i);
Z2_3 = N(i)*Vx(i)*Uy(i);
Z2_4 = N(i)*Vx(i)*Uz(i);
Z2(:,i) = [Z2_1;Z2_2;Z2_3;Z2_4];
s2(i) = Vx(i);
else
s1(i) = vhat(i);
s2(i) = vhat(i);
if vhat(i)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:4
Fl(i,:) = 0.5*sign(s1).*(1-(grid.dt/grid.dx)*abs(s1)).*Z1_tilde(i,:) +...
0.5*sign(s2).*(1-(grid.dt/grid.dx)*abs(s2)).*Z2_tilde(i,:);
end
end
%Limit the waves
function phi = limiter(wL,wI,wR,s)
%Iterate over the waves:
dotl = dot_manual(wL,wI);
dotr = dot_manual(wI,wR);
wnorm2 = dot_manual(wI,wI);
% Check for zero norm:
if wnorm2 > 0
if s > 0
r = dotl/wnorm2;
else
r = dotr/wnorm2;
end
phi = minmod(r);
else
phi = 0.0;
end
end
%Compute the manual dotproduct
function W2 = dot_manual(a,b)
sz = max(size(a));
W2 = 0.0;
for i = 1:sz
W2 = W2 + a(i)*b(i);
end
end
% Minmod used for averaging
function [phi] = minmod(r)
theta = 2.0;
phi = max(0, min([theta*r, (1+r)/2, theta]));
%theta = max(0+zeros(size(r)), min(1+zeros(size(r)), r));
end
% %Fluxes N (MUSCL)
% function [Flux_vec] = Flux(QL,QR,grid)
%
% c = grid.c;
% [NR, UxR, UyR, UzR] = destruct(QR);
% [NL, UxL, UyL, UzL] = destruct(QL);
%
% % compute c for the lax flux
% UR2 = ( UxR.^2 + UyR.^2 + UzR.^2 )/(c^2);
% UL2 = ( UxL.^2 + UyL.^2 + UzL.^2 )/(c^2);
% vRx = UxR./(sqrt(1 + UR2));
% vLx = UxL./(sqrt(1 + UL2));
% c = max( abs(vRx), abs(vLx) ); %%% FIX ABS | lamdba^p|
%
% %Rusanov Flux F = vxQ
% FR = [ NR.*vRx ; NR.*vRx.*UxR ; NR.*vRx.*UyR ; NR.*vRx.*UzR ];
% FL = [ NL.*vLx ; NL.*vLx.*UxL ; NL.*vLx.*UyL ; NL.*vLx.*UzL ];
% QR_vec = [ NR ; NR.*UxR ; NR.*UyR ; NR.*UzR ];
% QL_vec = [ NL ; NL.*UxL ; NL.*UyL ; NL.*UzL ];
% Flux_vec = (1/2) * ( FR + FL - c.*( QR_vec - QL_vec ) );
%
% end