-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrobust_ranocha.jl
1594 lines (1366 loc) · 61.6 KB
/
robust_ranocha.jl
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
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# Load dependencies
using LinearAlgebra
using Statistics
using DelimitedFiles: DelimitedFiles, writedlm, readdlm
using Trixi
using OrdinaryDiffEq
using DiffEqCallbacks
using SummationByPartsOperators
using TrixiAtmo
import PolyesterWeave, ThreadingUtilities
using LaTeXStrings
using Plots: Plots, plot, plot!, scatter, scatter!, savefig
using Trixi2Vtk: trixi2vtk
using PrettyTables: PrettyTables, pretty_table, ft_printf
const figdir = joinpath(dirname(@__DIR__), "figures")
################################################################################
# Spectra
function plot_spectra()
# Common axis formatting
xlims = (-320, 0)
ylims = (-250, 250)
# Show spectra for comparable upwind SBP FD and DGSEM schemes in a single plot
let accuracy_order = 4, polydeg = 2
fig = plot(xguide = "Real part", yguide = "Imaginary part", xlims=xlims, ylims=ylims)
let
nnodes = 48
initial_refinement_level = 2
λ = compute_spectrum_1d(; initial_refinement_level, nnodes, accuracy_order)
@show extrema(real, λ)
@show maximum(abs, λ)
λ = sort_spectrum(λ)
label = "$(2^initial_refinement_level) upwind SBP elements with $(nnodes) nodes"
plot!(fig, real.(λ), imag.(λ); label, plot_kwargs()..., linestyle = :solid)
end
let
nnodes = 24
initial_refinement_level = 3
λ = compute_spectrum_1d(; initial_refinement_level, nnodes, accuracy_order)
@show extrema(real, λ)
@show maximum(abs, λ)
λ = sort_spectrum(λ)
label = "$(2^initial_refinement_level) upwind SBP elements with $(nnodes) nodes"
plot!(fig, real.(λ), imag.(λ); label, plot_kwargs()..., linestyle = :dash)
end
let
nnodes = 12
initial_refinement_level = 4
λ = compute_spectrum_1d(; initial_refinement_level, nnodes, accuracy_order)
@show extrema(real, λ)
@show maximum(abs, λ)
λ = sort_spectrum(λ)
label = "$(2^initial_refinement_level) upwind SBP elements with $(nnodes) nodes"
plot!(fig, real.(λ), imag.(λ); label, plot_kwargs()..., linestyle = :dot)
end
let
initial_refinement_level = 6
λ = compute_spectrum_1d_dgsem(; initial_refinement_level, polydeg)
@show extrema(real, λ)
@show maximum(abs, λ)
λ = sort_spectrum(λ)
label = "$(2^initial_refinement_level) DG elements with polynomial degree $polydeg"
plot!(fig, real.(λ), imag.(λ); label, plot_kwargs()..., linestyle = :dashdotdot,
color = :black)
end
plot!(fig, legend = :outertop)
savefig(fig, joinpath(figdir,
"spectra_linear_advection_1d_order$(accuracy_order)_polydeg$(polydeg).pdf"))
end
let accuracy_order = 6, polydeg = 3
fig = plot(xguide = "Real part", yguide = "Imaginary part", xlims=xlims, ylims=ylims)
let
nnodes = 64
initial_refinement_level = 2
λ = compute_spectrum_1d(; initial_refinement_level, nnodes, accuracy_order)
@show extrema(real, λ)
@show maximum(abs, λ)
λ = sort_spectrum(λ)
label = "$(2^initial_refinement_level) upwind SBP elements with $(nnodes) nodes"
plot!(fig, real.(λ), imag.(λ); label, plot_kwargs()..., linestyle = :solid)
end
let
nnodes = 32
initial_refinement_level = 3
λ = compute_spectrum_1d(; initial_refinement_level, nnodes, accuracy_order)
@show extrema(real, λ)
@show maximum(abs, λ)
λ = sort_spectrum(λ)
label = "$(2^initial_refinement_level) upwind SBP elements with $(nnodes) nodes"
plot!(fig, real.(λ), imag.(λ); label, plot_kwargs()..., linestyle = :dash)
end
let
nnodes = 16
initial_refinement_level = 4
λ = compute_spectrum_1d(; initial_refinement_level, nnodes, accuracy_order)
@show extrema(real, λ)
@show maximum(abs, λ)
λ = sort_spectrum(λ)
label = "$(2^initial_refinement_level) upwind SBP elements with $(nnodes) nodes"
plot!(fig, real.(λ), imag.(λ); label, plot_kwargs()..., linestyle = :dot)
end
let
initial_refinement_level = 6
λ = compute_spectrum_1d_dgsem(; initial_refinement_level, polydeg)
@show extrema(real, λ)
@show maximum(abs, λ)
λ = sort_spectrum(λ)
label = "$(2^initial_refinement_level) DG elements with polynomial degree $polydeg"
plot!(fig, real.(λ), imag.(λ); label, plot_kwargs()..., linestyle = :dashdotdot,
color = :black)
end
plot!(fig, legend = :outertop)
savefig(fig, joinpath(figdir,
"spectra_linear_advection_1d_order$(accuracy_order)_polydeg$(polydeg).pdf"))
end
@info "1D spectra saved in the directory `figdir`" figdir
return nothing
end
function plot_kwargs()
fontsizes = (
xtickfontsize = 14, ytickfontsize = 14,
xguidefontsize = 16, yguidefontsize = 16,
legendfontsize = 14)
(; linewidth = 3, gridlinewidth = 2,
markersize = 8, markerstrokewidth = 4,
fontsizes..., size=(600, 500))
end
function sort_spectrum(λ)
idx_pos = imag.(λ) .> 0
pos = λ[idx_pos]
neg = λ[.!(idx_pos)]
sort!(pos; lt = !isless, by = real)
sort!(neg; lt = isless, by = real)
return vcat(pos, neg)
end
function compute_spectrum_1d(; initial_refinement_level, nnodes,
accuracy_order)
equations = LinearScalarAdvectionEquation1D(1.0)
function initial_condition(x, t, equations::LinearScalarAdvectionEquation1D)
return SVector(sinpi(x[1] - equations.advection_velocity[1] * t))
end
D_upw = upwind_operators(
SummationByPartsOperators.Mattsson2017;
derivative_order = 1,
accuracy_order,
xmin = -1.0, xmax = 1.0,
N = nnodes)
flux_splitting = splitting_lax_friedrichs
solver = FDSBP(D_upw,
surface_integral = SurfaceIntegralUpwind(flux_splitting),
volume_integral = VolumeIntegralUpwind(flux_splitting))
coordinates_min = -1.0
coordinates_max = 1.0
mesh = TreeMesh(coordinates_min, coordinates_max;
initial_refinement_level,
n_cells_max=10_000)
semi = SemidiscretizationHyperbolic(mesh, equations, initial_condition, solver)
J = jacobian_ad_forward(semi)
λ = eigvals(J)
return λ
end
function compute_spectrum_1d_dgsem(; initial_refinement_level, polydeg)
equations = LinearScalarAdvectionEquation1D(1.0)
function initial_condition(x, t, equations::LinearScalarAdvectionEquation1D)
return SVector(sinpi(x[1] - equations.advection_velocity[1] * t))
end
solver = DGSEM(polydeg = polydeg, surface_flux = flux_godunov)
coordinates_min = -1.0
coordinates_max = 1.0
mesh = TreeMesh(coordinates_min, coordinates_max;
initial_refinement_level,
n_cells_max=10_000)
semi = SemidiscretizationHyperbolic(mesh, equations, initial_condition, solver)
J = jacobian_ad_forward(semi)
λ = eigvals(J)
return λ
end
################################################################################
# Local linear/energy experiments
function local_linear_stability(; latex = false)
@info "Full upwind discretization (only D_−)"
accuracy_orders = Int[]
num_elements = Int[]
num_nodes = Int[]
max_real_part = Float64[]
for accuracy_order in 2:7
for nelements in 1:2
for nnodes in 13:14
λ = compute_spectrum_burgers_upwind_full(; accuracy_order,
nnodes,
nelements)
push!(accuracy_orders, accuracy_order)
push!(num_elements, nelements)
push!(num_nodes, nnodes)
push!(max_real_part, maximum(real, λ))
end
end
end
# print results
data = hcat(accuracy_orders, num_elements, num_nodes, max_real_part)
header = ["order", "#elements", "#nodes", "max. real part"]
kwargs = (; header, formatters=(ft_printf("%2d", [1, 2, 3]),
ft_printf("%9.2e", [4])))
pretty_table(data; kwargs...)
if latex
pretty_table(data; kwargs..., backend=Val(:latex))
end
return nothing
end
function compute_spectrum_burgers_upwind_full(; accuracy_order,
nnodes,
nelements)
D_local = derivative_operator(Mattsson2017(:minus);
derivative_order = 1,
xmin = -1.0, xmax = 1.0,
accuracy_order, N = nnodes)
mesh = UniformPeriodicMesh1D(xmin = -1.0, xmax = 1.0, Nx = nelements)
D = couple_discontinuously(D_local, mesh, Val(:minus))
u0 = rand(size(D, 2))
J = Trixi.ForwardDiff.jacobian(u0) do u
-D * (u.^2 ./ 2)
end
return eigvals(J)
end
################################################################################
# Convergence experiments
function convergence_tests_1d_advection(; latex = false)
@info "1D linear advection"
for accuracy_order in 2:5
@show accuracy_order
refinement_levels = 0:7
num_nodes = fill(20, size(refinement_levels))
_convergence_tests_1d_advection(; refinement_levels, num_nodes,
accuracy_order, latex)
num_nodes = 10 .* 2 .^ (0:7)
refinement_levels = fill(2, size(num_nodes))
_convergence_tests_1d_advection(; refinement_levels, num_nodes,
accuracy_order, latex)
end
return nothing
end
function _convergence_tests_1d_advection(; refinement_levels, num_nodes, accuracy_order,
latex = false)
num_elements = Vector{Int}()
errors = Vector{Float64}()
for (initial_refinement_level, nnodes) in zip(refinement_levels, num_nodes)
nelements = 2^initial_refinement_level
tol = 1.0e-12
res = compute_errors_1d_advection(; initial_refinement_level, nnodes,
accuracy_order, tol)
push!(num_elements, nelements)
push!(errors, first(res.l2))
end
if length(unique(num_elements)) == 1
eoc = compute_eoc(num_nodes, errors)
elseif length(unique(num_nodes)) == 1
eoc = compute_eoc(num_elements, errors)
end
# print results
data = hcat(num_elements, num_nodes, errors, eoc)
header = ["#elements", "#nodes", "L2 error", "L2 EOC"]
kwargs = (; header, formatters=(ft_printf("%3d", [1, 2]),
ft_printf("%.2e", [3]),
ft_printf("%.2f", [4])))
pretty_table(data; kwargs...)
if latex
pretty_table(data; kwargs..., backend=Val(:latex))
end
return nothing
end
function compute_errors_1d_advection(; initial_refinement_level, nnodes,
accuracy_order, tol)
equations = LinearScalarAdvectionEquation1D(1.0)
function initial_condition(x, t, equations::LinearScalarAdvectionEquation1D)
return SVector(sinpi(x[1] - equations.advection_velocity[1] * t))
end
D_upw = upwind_operators(
SummationByPartsOperators.Mattsson2017;
derivative_order = 1,
accuracy_order,
xmin = -1.0, xmax = 1.0,
N = nnodes)
flux_splitting = splitting_lax_friedrichs
solver = FDSBP(D_upw,
surface_integral = SurfaceIntegralUpwind(flux_splitting),
volume_integral = VolumeIntegralUpwind(flux_splitting))
coordinates_min = -1.0
coordinates_max = 1.0
mesh = TreeMesh(coordinates_min, coordinates_max;
initial_refinement_level,
n_cells_max=10_000)
semi = SemidiscretizationHyperbolic(mesh, equations, initial_condition, solver)
ode = semidiscretize(semi, (0.0, 5.0))
sol = solve(ode, RDPK3SpFSAL49(); ode_default_options()...,
abstol = tol, reltol = tol)
analysis_callback = AnalysisCallback(semi)
return analysis_callback(sol)
end
function compute_eoc(Ns, errors)
eoc = similar(errors)
eoc[begin] = NaN # no EOC defined for the first grid
for idx in Iterators.drop(eachindex(errors, Ns, eoc), 1)
eoc[idx] = -( log(errors[idx] / errors[idx - 1]) / log(Ns[idx] / Ns[idx - 1]) )
end
return eoc
end
function convergence_tests_1d_euler(; latex = false)
for accuracy_order in 2:5
refinement_levels = 0:7
num_nodes = fill(20, size(refinement_levels))
splitting = splitting_vanleer_haenel
@info "1D compressible Euler equations" accuracy_order splitting
_convergence_tests_1d_euler(; refinement_levels, num_nodes,
accuracy_order, splitting, latex)
refinement_levels = 0:7
num_nodes = fill(20, size(refinement_levels))
splitting = splitting_steger_warming
@info "1D compressible Euler equations" accuracy_order splitting
_convergence_tests_1d_euler(; refinement_levels, num_nodes,
accuracy_order, splitting, latex)
num_nodes = 10 .* 2 .^ (0:7)
refinement_levels = fill(2, size(num_nodes))
splitting = splitting_steger_warming
@info "1D compressible Euler equations" accuracy_order splitting
_convergence_tests_1d_euler(; refinement_levels, num_nodes,
accuracy_order, splitting, latex)
end
return nothing
end
function _convergence_tests_1d_euler(; refinement_levels, num_nodes, accuracy_order,
splitting, latex = false)
num_elements = Vector{Int}()
errors = Vector{Float64}()
for (initial_refinement_level, nnodes) in zip(refinement_levels, num_nodes)
nelements = 2^initial_refinement_level
tol = 1.0e-13
res = compute_errors_1d_euler(; initial_refinement_level, nnodes,
accuracy_order, tol, splitting)
push!(num_elements, nelements)
push!(errors, first(res.l2))
end
if length(unique(num_elements)) == 1
eoc = compute_eoc(num_nodes, errors)
elseif length(unique(num_nodes)) == 1
eoc = compute_eoc(num_elements, errors)
end
# print results
data = hcat(num_elements, num_nodes, errors, eoc)
header = ["#elements", "#nodes", "L2 error", "L2 EOC"]
kwargs = (; header, formatters=(ft_printf("%3d", [1, 2]),
ft_printf("%.2e", [3]),
ft_printf("%.2f", [4])))
pretty_table(data; kwargs...)
if latex
pretty_table(data; kwargs..., backend=Val(:latex))
end
return nothing
end
function compute_errors_1d_euler(; initial_refinement_level, nnodes,
accuracy_order, tol, splitting)
equations = CompressibleEulerEquations1D(1.4)
initial_condition = initial_condition_convergence_test
source_terms = source_terms_convergence_test
D_upw = upwind_operators(
SummationByPartsOperators.Mattsson2017;
derivative_order = 1,
accuracy_order,
xmin = -1.0, xmax = 1.0,
N = nnodes)
solver = FDSBP(D_upw,
surface_integral = SurfaceIntegralUpwind(splitting),
volume_integral = VolumeIntegralUpwind(splitting))
coordinates_min = 0.0
coordinates_max = 2.0
mesh = TreeMesh(coordinates_min, coordinates_max;
initial_refinement_level,
n_cells_max = 10_000)
semi = SemidiscretizationHyperbolic(mesh, equations, initial_condition,
solver; source_terms)
ode = semidiscretize(semi, (0.0, 2.0))
sol = solve(ode, RDPK3SpFSAL49(); ode_default_options()...,
abstol = tol, reltol = tol)
analysis_callback = AnalysisCallback(semi)
return analysis_callback(sol)
end
################################################################################
# Isentropic vortex
function experiments_isentropic_vortex()
isentropic_vortex_generate_data()
isentropic_vortex_plot_results(version = "blocks")
isentropic_vortex_plot_results(version = "periodic")
end
function isentropic_vortex_generate_data()
for accuracy_order in 2:6
@info "Generate data" accuracy_order
t, error_density = compute_error_isentropic_vortex(; accuracy_order,
initial_refinement_level = 2,
nnodes = 16,
source_of_coefficients = Mattsson2017
)
open(joinpath(figdir, "isentropic_vortex_order_$(accuracy_order)_blocks.dat"), "w") do io
println(io, "# t\tL2_error_density")
writedlm(io, hcat(t, error_density))
end
t, error_density = compute_error_isentropic_vortex(; accuracy_order,
initial_refinement_level = 0,
nnodes = 64,
source_of_coefficients = periodic_derivative_operator
)
open(joinpath(figdir, "isentropic_vortex_order_$(accuracy_order)_periodic.dat"), "w") do io
println(io, "# t\tL2_error_density")
writedlm(io, hcat(t, error_density))
end
end
end
function isentropic_vortex_plot_results(; version = "blocks")
fig = plot(xguide = L"Time $t$", yguide = L"$L^2$ error of the density";
xscale = :log10, yscale = :log10,
plot_kwargs()...)
linestyles = [:solid, :dash, :dashdot, :dot, :solid]
for(accuracy_order, linestyle) in zip(2:6, linestyles)
data = readdlm(joinpath(figdir, "isentropic_vortex_order_$(accuracy_order)_$(version).dat"), comments = true)
plot!(fig, data[:, 1], data[:, 2];
label = "Order $(accuracy_order)", linestyle,
plot_kwargs()...)
end
plot!(fig, legend = :bottomright)
savefig(fig, joinpath(figdir, "isentropic_vortex_error_$(version).pdf"))
@info "Error plot saved in the directory `figdir`" figdir
return nothing
end
function compute_error_isentropic_vortex(; accuracy_order = 4, nnodes = 16,
initial_refinement_level = 2,
flux_splitting = splitting_steger_warming,
source_of_coefficients = Mattsson2017,
polydeg = nothing,
volume_flux = flux_ranocha_turbo,
surface_flux = flux_lax_friedrichs,
tspan = (0.0, 1000.0),
tol = 1.0e-6)
equations = CompressibleEulerEquations2D(1.4)
"""
initial_condition_isentropic_vortex(x, t, equations)
The classical isentropic vortex test case of
- Chi-Wang Shu (1997)
Essentially Non-Oscillatory and Weighted Essentially Non-Oscillatory
Schemes for Hyperbolic Conservation Laws.
[NASA/CR-97-206253](https://ntrs.nasa.gov/citations/19980007543)
"""
function initial_condition(x, t, equations::CompressibleEulerEquations2D)
ϱ0 = 1.0 # background density
v0 = SVector(1.0, 1.0) # background velocity
p0 = 10.0 # background pressure
ε = 10.0 # vortex strength
L = 10.0 # size of the domain per coordinate direction
T0 = p0 / ϱ0 # background temperature
γ = equations.gamma # ideal gas constant
vortex_center(x, L) = mod(x + L/2, L) - L/2
x0 = v0 * t # current center of the vortex
dx = vortex_center.(x - x0, L)
r2 = sum(abs2, dx)
# perturbed primitive variables
T = T0 - (γ - 1) * ε^2 / (8 * γ * π^2) * exp(1 - r2)
v = v0 + ε / (2 * π) * exp(0.5 * (1 - r2)) * SVector(-dx[2], dx[1])
ϱ = ϱ0 * (T / T0)^(1 / (γ - 1))
p = ϱ * T
return prim2cons(SVector(ϱ, v..., p), equations)
end
if polydeg === nothing
# Use upwind SBP discretization
D_upw = upwind_operators(source_of_coefficients;
derivative_order = 1,
accuracy_order,
xmin = -1.0, xmax = 1.0,
N = nnodes)
solver = FDSBP(D_upw,
surface_integral = SurfaceIntegralUpwind(flux_splitting),
volume_integral = VolumeIntegralUpwind(flux_splitting))
else
# Use DGSEM
volume_integral = VolumeIntegralFluxDifferencing(volume_flux)
solver = DGSEM(; polydeg, surface_flux, volume_integral)
end
coordinates_min = (-5.0, -5.0)
coordinates_max = ( 5.0, 5.0)
mesh = TreeMesh(coordinates_min, coordinates_max;
initial_refinement_level,
n_cells_max = 100_000,
periodicity = true)
semi = SemidiscretizationHyperbolic(mesh, equations, initial_condition, solver)
ode = semidiscretize(semi, tspan)
saveat = range(tspan..., step = 20)[2:end]
saved_values = SavedValues(Float64, Float64)
save_func = let cb = AnalysisCallback(semi)
function save_func(u_ode, t, integrator)
semi = integrator.p
analysis_callback = cb.affect!
(; analyzer) = analysis_callback
cache_analysis = analysis_callback.cache
l2_error, linf_error = Trixi.calc_error_norms(u_ode, t,
analyzer, semi,
cache_analysis)
return first(l2_error)
end
end
saving = SavingCallback(save_func, saved_values; saveat)
sol = solve(ode, RDPK3SpFSAL49();
abstol = tol, reltol = tol,
ode_default_options()..., callback = saving, tstops = saveat)
return (; t = saved_values.t, error_density = saved_values.saveval)
end
################################################################################
# Kelvin-Helmholtz instability
function experiments_kelvin_helmholtz_instability(; latex = false)
# Upwind SBP operators
@info "Upwind SBP operators"
nnodes = 16
initial_refinement_levels = 0:4
accuracy_orders = 2:7
for flux_splitting in (splitting_vanleer_haenel, splitting_steger_warming)
@info flux_splitting
final_times = Matrix{Float64}(undef, length(initial_refinement_levels),
length(accuracy_orders))
for (j, accuracy_order) in enumerate(accuracy_orders)
for (i, initial_refinement_level) in enumerate(initial_refinement_levels)
t = blowup_kelvin_helmholtz(; accuracy_order, nnodes,
initial_refinement_level,
flux_splitting)
final_times[i, j] = t
end
end
# print results
data = hcat(4 .^ initial_refinement_levels, final_times)
header = vcat(0, accuracy_orders)
kwargs = (; header, title = string(flux_splitting) * ", constant #nodes",
formatters=(ft_printf("%3d", [1]),
ft_printf("%.2f", 2:size(data,2))))
pretty_table(data; kwargs...)
if latex
pretty_table(data; kwargs..., backend=Val(:latex))
end
println()
end
# Upwind SBP operators with constant number of degrees of freedom
@info "Upwind SBP operators with constant number of degrees of freedom"
initial_refinement_levels = 0:4
accuracy_orders = 2:7
for flux_splitting in (splitting_vanleer_haenel, splitting_steger_warming)
@info flux_splitting
final_times = Matrix{Float64}(undef, length(initial_refinement_levels),
length(accuracy_orders))
for (j, accuracy_order) in enumerate(accuracy_orders)
for (i, initial_refinement_level) in enumerate(initial_refinement_levels)
nnodes = 256 ÷ 2^initial_refinement_level
t = blowup_kelvin_helmholtz(; accuracy_order, nnodes,
initial_refinement_level,
flux_splitting)
final_times[i, j] = t
end
end
# print results
data = hcat(4 .^ initial_refinement_levels, final_times)
header = vcat(0, accuracy_orders)
kwargs = (; header, title = string(flux_splitting) * ", constant #DOFs",
formatters=(ft_printf("%3d", [1]),
ft_printf("%.2f", 2:size(data,2))))
pretty_table(data; kwargs...)
if latex
pretty_table(data; kwargs..., backend=Val(:latex))
end
println()
end
# DGSEM
@info "DGSEM"
initial_refinement_levels = 2:5
polydegs = 2:7
for volume_flux in (flux_ranocha, flux_shima_etal)
@info volume_flux
final_times = Matrix{Float64}(undef, length(initial_refinement_levels),
length(polydegs))
for (j, polydeg) in enumerate(polydegs)
for (i, initial_refinement_level) in enumerate(initial_refinement_levels)
t = blowup_kelvin_helmholtz(; polydeg,
initial_refinement_level,
volume_flux)
final_times[i, j] = t
end
end
# print results
data = hcat(4 .^ initial_refinement_levels, final_times)
header = vcat(0, polydegs)
kwargs = (; header, title = string(volume_flux),
formatters=(ft_printf("%3d", [1]),
ft_printf("%.2f", 2:size(data,2))))
pretty_table(data; kwargs...)
if latex
pretty_table(data; kwargs..., backend=Val(:latex))
end
println()
end
# Periodic upwind SBP operators
@info "Periodic upwind SBP operators"
numbers_of_nodes = 16 .* 2 .^ (0:4)
initial_refinement_level = 0
accuracy_orders = 2:7
source_of_coefficients = periodic_derivative_operator
for flux_splitting in (splitting_vanleer_haenel, splitting_steger_warming)
@info flux_splitting source_of_coefficients
final_times = Matrix{Float64}(undef, length(numbers_of_nodes),
length(accuracy_orders))
for (j, accuracy_order) in enumerate(accuracy_orders)
for (i, nnodes) in enumerate(numbers_of_nodes)
t = blowup_kelvin_helmholtz(; accuracy_order, nnodes,
initial_refinement_level,
source_of_coefficients,
flux_splitting)
final_times[i, j] = t
end
end
# print results
data = hcat(numbers_of_nodes .^ 2, final_times)
header = vcat(0, accuracy_orders)
kwargs = (; header, title = string(flux_splitting) * ", periodic upwind SBP",
formatters=(ft_printf("%3d", [1]),
ft_printf("%.2f", 2:size(data,2))))
pretty_table(data; kwargs...)
if latex
pretty_table(data; kwargs..., backend=Val(:latex))
end
println()
end
return nothing
end
function blowup_kelvin_helmholtz(; accuracy_order = 4, nnodes = 16,
initial_refinement_level = 2,
flux_splitting = splitting_vanleer_haenel,
source_of_coefficients = Mattsson2017,
polydeg = nothing,
volume_flux = flux_ranocha,
tol = 1.0e-6)
equations = CompressibleEulerEquations2D(1.4)
function initial_condition(x, t, equations::CompressibleEulerEquations2D)
# change discontinuity to tanh
# typical resolution 128^2, 256^2
# domain size is [-1,+1]^2
slope = 15
B = tanh(slope * x[2] + 7.5) - tanh(slope * x[2] - 7.5)
rho = 0.5 + 0.75 * B
v1 = 0.5 * (B - 1)
v2 = 0.1 * sin(2 * pi * x[1])
p = 1.0
return prim2cons(SVector(rho, v1, v2, p), equations)
end
if polydeg === nothing
# Use upwind SBP discretization
D_upw = upwind_operators(source_of_coefficients;
derivative_order = 1,
accuracy_order,
xmin = -1.0, xmax = 1.0,
N = nnodes)
solver = FDSBP(D_upw,
surface_integral = SurfaceIntegralUpwind(flux_splitting),
volume_integral = VolumeIntegralUpwind(flux_splitting))
@info "Kelvin-Helmholtz instability" accuracy_order nnodes initial_refinement_level flux_splitting
else
# Use DGSEM
volume_integral = VolumeIntegralFluxDifferencing(volume_flux)
solver = DGSEM(; polydeg, surface_flux = flux_lax_friedrichs, volume_integral)
@info "Kelvin-Helmholtz instability" polydeg initial_refinement_level volume_flux
end
coordinates_min = (-1.0, -1.0)
coordinates_max = ( 1.0, 1.0)
mesh = TreeMesh(coordinates_min, coordinates_max;
initial_refinement_level,
n_cells_max = 100_000)
semi = SemidiscretizationHyperbolic(mesh, equations, initial_condition, solver)
@show Trixi.ndofs(semi)
tspan = (0.0, 15.0)
ode = semidiscretize(semi, tspan)
integrator = init(ode, SSPRK43(); controller = PIDController(0.55, -0.27, 0.05),
abstol = tol, reltol = tol,
ode_default_options()...)
try
solve!(integrator)
catch error
@info "Blow-up" integrator.t
reset_threads!()
end
return integrator.t
end
# https://github.com/JuliaSIMD/Polyester.jl/issues/30
function reset_threads!()
PolyesterWeave.reset_workers!()
for i in 1:(Threads.nthreads() - 1)
ThreadingUtilities.initialize_task(i)
end
return nothing
end
function run_kelvin_helmholtz(; accuracy_order = 4, nnodes = 16,
initial_refinement_level = 2,
flux_splitting = splitting_vanleer_haenel,
source_of_coefficients = Mattsson2017,
polydeg = nothing,
volume_flux = flux_ranocha_turbo,
tol = 1.0e-6)
equations = CompressibleEulerEquations2D(1.4)
function initial_condition(x, t, equations::CompressibleEulerEquations2D)
# change discontinuity to tanh
# typical resolution 128^2, 256^2
# domain size is [-1,+1]^2
slope = 15
B = tanh(slope * x[2] + 7.5) - tanh(slope * x[2] - 7.5)
rho = 0.5 + 0.75 * B
v1 = 0.5 * (B - 1)
v2 = 0.1 * sin(2 * pi * x[1])
p = 1.0
return prim2cons(SVector(rho, v1, v2, p), equations)
end
if polydeg === nothing
# Use upwind SBP discretization
D_upw = upwind_operators(source_of_coefficients;
derivative_order = 1,
accuracy_order,
xmin = -1.0, xmax = 1.0,
N = nnodes)
solver = FDSBP(D_upw,
surface_integral=SurfaceIntegralUpwind(flux_splitting),
# surface_integral=SurfaceIntegralStrongForm(flux_lax_friedrichs),
volume_integral=VolumeIntegralUpwind(flux_splitting))
else
# Use DGSEM
volume_integral = VolumeIntegralFluxDifferencing(volume_flux)
solver = DGSEM(; polydeg, surface_flux = flux_hllc, volume_integral)
end
coordinates_min = (-1.0, -1.0)
coordinates_max = ( 1.0, 1.0)
mesh = TreeMesh(coordinates_min, coordinates_max;
initial_refinement_level,
n_cells_max = 100_000)
semi = SemidiscretizationHyperbolic(mesh, equations, initial_condition, solver)
tspan = (0.0, 15.0)
ode = semidiscretize(semi, tspan)
summary_callback = SummaryCallback()
analysis_interval = 1000
analysis_callback = AnalysisCallback(semi, interval=analysis_interval)
alive_callback = AliveCallback(analysis_interval=analysis_interval)
saving_callback = SaveSolutionCallback(; interval = 100,
save_final_solution = true,
output_directory = joinpath(@__DIR__, "out_dev"),
# solution_variables = cons2cons)
solution_variables = cons2prim)
callbacks = CallbackSet(summary_callback,
analysis_callback,
alive_callback,
saving_callback)
integrator = init(ode, SSPRK43(); controller = PIDController(0.55, -0.27, 0.05),
abstol = tol, reltol = tol,
ode_default_options()..., callback=callbacks)
try
solve!(integrator)
catch err
@warn "Crashed at time" integrator.t
saving_callback.affect!(integrator)
reset_threads!()
end
summary_callback() # print the timer summary
return nothing
end
# Kelvin-Helmholtz instability
function experiments_kelvin_helmholtz_instability_theta(; latex = false)
# Upwind SBP operators
@info "Upwind SBP operators"
nnodes = 16
initial_refinement_levels = 0:4
accuracy_orders = 2:7
# for flux_splitting in (TrixiAtmo.splitting_vanleer_haenel,)
# @info flux_splitting
# final_times = Matrix{Float64}(undef, length(initial_refinement_levels),
# length(accuracy_orders))
# for (j, accuracy_order) in enumerate(accuracy_orders)
# for (i, initial_refinement_level) in enumerate(initial_refinement_levels)
# t = blowup_kelvin_helmholtz_theta(; accuracy_order, nnodes,
# initial_refinement_level,
# flux_splitting)
# final_times[i, j] = t
# end
# end
# # print results
# data = hcat(4 .^ initial_refinement_levels, final_times)
# header = vcat(0, accuracy_orders)
# kwargs = (; header, title = string(flux_splitting) * ", constant #nodes",
# formatters=(ft_printf("%3d", [1]),
# ft_printf("%.2f", 2:size(data,2))))
# pretty_table(data; kwargs...)
# if latex
# pretty_table(data; kwargs..., backend=Val(:latex))
# end
# println()
# end
# # Upwind SBP operators with constant number of degrees of freedom
# @info "Upwind SBP operators with constant number of degrees of freedom"
# initial_refinement_levels = 0:4
# accuracy_orders = 2:7
# for flux_splitting in (TrixiAtmo.splitting_vanleer_haenel,)
# @info flux_splitting
# final_times = Matrix{Float64}(undef, length(initial_refinement_levels),
# length(accuracy_orders))
# for (j, accuracy_order) in enumerate(accuracy_orders)
# for (i, initial_refinement_level) in enumerate(initial_refinement_levels)
# nnodes = 256 ÷ 2^initial_refinement_level
# t = blowup_kelvin_helmholtz_theta(; accuracy_order, nnodes,
# initial_refinement_level,
# flux_splitting)
# final_times[i, j] = t
# end
# end
# # print results
# data = hcat(4 .^ initial_refinement_levels, final_times)
# header = vcat(0, accuracy_orders)
# kwargs = (; header, title = string(flux_splitting) * ", constant #DOFs",
# formatters=(ft_printf("%3d", [1]),
# ft_printf("%.2f", 2:size(data,2))))
# pretty_table(data; kwargs...)
# if latex
# pretty_table(data; kwargs..., backend=Val(:latex))
# end
# println()
# end
# DGSEM
@info "DGSEM"
initial_refinement_levels = 2:5
polydegs = 2:7
for volume_flux in (flux_theta, flux_theta_global, flux_theta_rhos)
@info volume_flux
final_times = Matrix{Float64}(undef, length(initial_refinement_levels),
length(polydegs))
for (j, polydeg) in enumerate(polydegs)
for (i, initial_refinement_level) in enumerate(initial_refinement_levels)
t = blowup_kelvin_helmholtz_theta(; polydeg,
initial_refinement_level,
volume_flux)
final_times[i, j] = t
end
end
# print results
data = hcat(4 .^ initial_refinement_levels, final_times)
header = vcat(0, polydegs)
kwargs = (; header, title = string(volume_flux),
formatters=(ft_printf("%3d", [1]),
ft_printf("%.2f", 2:size(data,2))))
pretty_table(data; kwargs...)
if latex
pretty_table(data; kwargs..., backend=Val(:latex))
end
println()
end
# Periodic upwind SBP operators
@info "Periodic upwind SBP operators"