-
Notifications
You must be signed in to change notification settings - Fork 115
/
Copy pathtest_unit.jl
2080 lines (1725 loc) · 79.5 KB
/
test_unit.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
module TestUnit
using Test
using Trixi
using LinearAlgebra: norm, dot
using DelimitedFiles: readdlm
# Use Convex and ECOS to load the extension that extends functions for testing
# PERK Single p2 Constructors
using Convex: Convex
using ECOS: Optimizer
# Use NLsolve to load the extension that extends functions for testing
# PERK Single p3 Constructors
using NLsolve: nlsolve
include("test_trixi.jl")
# Start with a clean environment: remove Trixi.jl output directory if it exists
outdir = "out"
isdir(outdir) && rm(outdir, recursive = true)
# Run various unit (= non-elixir-triggered) tests
@testset "Unit tests" begin
#! format: noindent
@timed_testset "SerialTree" begin
@testset "constructors" begin
@test_nowarn Trixi.SerialTree(Val(1), 10, 0.0, 1.0)
@test_nowarn Trixi.SerialTree{1}(10, 0.0, 1.0)
end
@testset "helper functions" begin
t = Trixi.SerialTree(Val(1), 10, 0.0, 1.0)
@test_nowarn display(t)
@test Trixi.ndims(t) == 1
@test Trixi.has_any_neighbor(t, 1, 1) == true
@test Trixi.isperiodic(t, 1) == true
@test Trixi.n_children_per_cell(t) == 2
@test Trixi.n_directions(t) == 2
end
@testset "refine!/coarsen!" begin
t = Trixi.SerialTree(Val(1), 10, 0.0, 1.0)
@test Trixi.refine!(t) == [1]
@test Trixi.coarsen!(t) == [1]
@test Trixi.refine!(t) == [1]
@test Trixi.coarsen!(t, 1) == [1]
@test Trixi.coarsen!(t) == Int[] # Coarsen twice to check degenerate case of single-cell tree
@test Trixi.refine!(t) == [1]
@test Trixi.refine!(t) == [2, 3]
@test Trixi.coarsen_box!(t, [-0.5], [0.0]) == [2]
@test Trixi.coarsen_box!(t, 0.0, 0.5) == [3]
@test isnothing(Trixi.reset_data_structures!(t))
end
end
@timed_testset "ParallelTree" begin
@testset "constructors" begin
@test_nowarn Trixi.ParallelTree(Val(1), 10, 0.0, 1.0)
@test_nowarn Trixi.ParallelTree{1}(10, 0.0, 1.0)
end
@testset "helper functions" begin
t = Trixi.ParallelTree(Val(1), 10, 0.0, 1.0)
@test isnothing(display(t))
@test isnothing(Trixi.reset_data_structures!(t))
end
end
@timed_testset "TreeMesh" begin
@testset "constructors" begin
@test TreeMesh{1, Trixi.SerialTree{1, Float64}, Float64}(1, 5.0, 2.0) isa
TreeMesh
# Invalid domain length check (TreeMesh expects a hypercube)
# 2D
@test_throws ArgumentError TreeMesh((-0.5, 0.0), (1.0, 2.0),
initial_refinement_level = 2,
n_cells_max = 10_000)
# 3D
@test_throws ArgumentError TreeMesh((-0.5, 0.0, -0.2), (1.0, 2.0, 1.5),
initial_refinement_level = 2,
n_cells_max = 10_000)
end
end
@timed_testset "ParallelTreeMesh" begin
@testset "partition!" begin
@testset "mpi_nranks() = 2" begin
Trixi.mpi_nranks() = 2
let
@test Trixi.mpi_nranks() == 2
mesh = TreeMesh{2, Trixi.ParallelTree{2, Float64}, Float64}(30,
(0.0, 0.0),
1.0)
# Refine twice
Trixi.refine!(mesh.tree)
Trixi.refine!(mesh.tree)
# allow_coarsening = true
Trixi.partition!(mesh)
# Use parent for OffsetArray
@test parent(mesh.n_cells_by_rank) == [11, 10]
@test mesh.tree.mpi_ranks[1:21] ==
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]
@test parent(mesh.first_cell_by_rank) == [1, 12]
# allow_coarsening = false
Trixi.partition!(mesh; allow_coarsening = false)
@test parent(mesh.n_cells_by_rank) == [11, 10]
@test mesh.tree.mpi_ranks[1:21] ==
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]
@test parent(mesh.first_cell_by_rank) == [1, 12]
end
Trixi.mpi_nranks() = Trixi.MPI_SIZE[] # restore the original behavior
end
@testset "mpi_nranks() = 3" begin
Trixi.mpi_nranks() = 3
let
@test Trixi.mpi_nranks() == 3
mesh = TreeMesh{2, Trixi.ParallelTree{2, Float64}, Float64}(100,
(0.0, 0.0),
1.0)
# Refine twice
Trixi.refine!(mesh.tree)
Trixi.refine!(mesh.tree)
# allow_coarsening = true
Trixi.partition!(mesh)
# Use parent for OffsetArray
@test parent(mesh.n_cells_by_rank) == [11, 5, 5]
@test mesh.tree.mpi_ranks[1:21] ==
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2]
@test parent(mesh.first_cell_by_rank) == [1, 12, 17]
# allow_coarsening = false
Trixi.partition!(mesh; allow_coarsening = false)
@test parent(mesh.n_cells_by_rank) == [9, 6, 6]
@test mesh.tree.mpi_ranks[1:21] ==
[0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2]
@test parent(mesh.first_cell_by_rank) == [1, 10, 16]
end
Trixi.mpi_nranks() = Trixi.MPI_SIZE[] # restore the original behavior
end
@testset "mpi_nranks() = 9" begin
Trixi.mpi_nranks() = 9
let
@test Trixi.mpi_nranks() == 9
mesh = TreeMesh{2, Trixi.ParallelTree{2, Float64}, Float64}(1000,
(0.0, 0.0),
1.0)
# Refine twice
Trixi.refine!(mesh.tree)
Trixi.refine!(mesh.tree)
Trixi.refine!(mesh.tree)
Trixi.refine!(mesh.tree)
# allow_coarsening = true
Trixi.partition!(mesh)
# Use parent for OffsetArray
@test parent(mesh.n_cells_by_rank) ==
[44, 37, 38, 37, 37, 37, 38, 37, 36]
@test parent(mesh.first_cell_by_rank) ==
[1, 45, 82, 120, 157, 194, 231, 269, 306]
end
Trixi.mpi_nranks() = Trixi.MPI_SIZE[] # restore the original behavior
end
@testset "mpi_nranks() = 3 non-uniform" begin
Trixi.mpi_nranks() = 3
let
@test Trixi.mpi_nranks() == 3
mesh = TreeMesh{2, Trixi.ParallelTree{2, Float64}, Float64}(100,
(0.0, 0.0),
1.0)
# Refine whole tree
Trixi.refine!(mesh.tree)
# Refine left leaf
Trixi.refine!(mesh.tree, [2])
# allow_coarsening = true
Trixi.partition!(mesh)
# Use parent for OffsetArray
@test parent(mesh.n_cells_by_rank) == [6, 1, 2]
@test mesh.tree.mpi_ranks[1:9] == [0, 0, 0, 0, 0, 0, 1, 2, 2]
@test parent(mesh.first_cell_by_rank) == [1, 7, 8]
# allow_coarsening = false
Trixi.partition!(mesh; allow_coarsening = false)
@test parent(mesh.n_cells_by_rank) == [5, 2, 2]
@test mesh.tree.mpi_ranks[1:9] == [0, 0, 0, 0, 0, 1, 1, 2, 2]
@test parent(mesh.first_cell_by_rank) == [1, 6, 8]
end
Trixi.mpi_nranks() = Trixi.MPI_SIZE[] # restore the original behavior
end
@testset "not enough ranks" begin
Trixi.mpi_nranks() = 3
let
@test Trixi.mpi_nranks() == 3
mesh = TreeMesh{2, Trixi.ParallelTree{2, Float64}, Float64}(100,
(0.0, 0.0),
1.0)
# Only one leaf
@test_throws AssertionError("Too many ranks to properly partition the mesh!") Trixi.partition!(mesh)
# Refine to 4 leaves
Trixi.refine!(mesh.tree)
# All four leaves will need to be on one rank to allow coarsening
@test_throws AssertionError("Too many ranks to properly partition the mesh!") Trixi.partition!(mesh)
@test_nowarn Trixi.partition!(mesh; allow_coarsening = false)
end
Trixi.mpi_nranks() = Trixi.MPI_SIZE[] # restore the original behavior
end
end
end
@timed_testset "curved mesh" begin
@testset "calc_jacobian_matrix" begin
@testset "identity map" begin
basis = LobattoLegendreBasis(5)
nodes = Trixi.get_nodes(basis)
jacobian_matrix = Array{Float64, 5}(undef, 2, 2, 6, 6, 1)
node_coordinates = Array{Float64, 4}(undef, 2, 6, 6, 1)
node_coordinates[1, :, :, 1] .= [nodes[i] for i in 1:6, j in 1:6]
node_coordinates[2, :, :, 1] .= [nodes[j] for i in 1:6, j in 1:6]
expected = zeros(2, 2, 6, 6, 1)
expected[1, 1, :, :, 1] .= 1
expected[2, 2, :, :, 1] .= 1
@test Trixi.calc_jacobian_matrix!(jacobian_matrix, 1, node_coordinates,
basis) ≈ expected
end
@testset "maximum exact polydeg" begin
basis = LobattoLegendreBasis(3)
nodes = Trixi.get_nodes(basis)
jacobian_matrix = Array{Float64, 5}(undef, 2, 2, 4, 4, 1)
# f(x, y) = [x^3, xy^2]
node_coordinates = Array{Float64, 4}(undef, 2, 4, 4, 1)
node_coordinates[1, :, :, 1] .= [nodes[i]^3 for i in 1:4, j in 1:4]
node_coordinates[2, :, :, 1] .= [nodes[i] * nodes[j]^2
for i in 1:4, j in 1:4]
# Df(x, y) = [3x^2 0;
# y^2 2xy]
expected = zeros(2, 2, 4, 4, 1)
expected[1, 1, :, :, 1] .= [3 * nodes[i]^2 for i in 1:4, j in 1:4]
expected[2, 1, :, :, 1] .= [nodes[j]^2 for i in 1:4, j in 1:4]
expected[2, 2, :, :, 1] .= [2 * nodes[i] * nodes[j] for i in 1:4, j in 1:4]
@test Trixi.calc_jacobian_matrix!(jacobian_matrix, 1, node_coordinates,
basis) ≈ expected
end
end
end
@timed_testset "interpolation" begin
@testset "nodes and weights" begin
@test Trixi.gauss_nodes_weights(1) == ([0.0], [2.0])
@test Trixi.gauss_nodes_weights(2)[1] ≈ [-1 / sqrt(3), 1 / sqrt(3)]
@test Trixi.gauss_nodes_weights(2)[2] == [1.0, 1.0]
@test Trixi.gauss_nodes_weights(3)[1] ≈ [-sqrt(3 / 5), 0.0, sqrt(3 / 5)]
@test Trixi.gauss_nodes_weights(3)[2] ≈ [5 / 9, 8 / 9, 5 / 9]
end
@testset "multiply_dimensionwise" begin
nodes_in = [0.0, 0.5, 1.0]
nodes_out = [0.0, 1 / 3, 2 / 3, 1.0]
matrix = Trixi.polynomial_interpolation_matrix(nodes_in, nodes_out)
data_in = [3.0 4.5 6.0]
@test isapprox(Trixi.multiply_dimensionwise(matrix, data_in), [3.0 4.0 5.0 6.0])
n_vars = 3
size_in = 2
size_out = 3
matrix = randn(size_out, size_in)
# 1D
data_in = randn(n_vars, size_in)
data_out = Trixi.multiply_dimensionwise_naive(matrix, data_in)
@test isapprox(data_out, Trixi.multiply_dimensionwise(matrix, data_in))
# 2D
data_in = randn(n_vars, size_in, size_in)
data_out = Trixi.multiply_dimensionwise_naive(matrix, data_in)
@test isapprox(data_out, Trixi.multiply_dimensionwise(matrix, data_in))
# 3D
data_in = randn(n_vars, size_in, size_in, size_in)
data_out = Trixi.multiply_dimensionwise_naive(matrix, data_in)
@test isapprox(data_out, Trixi.multiply_dimensionwise(matrix, data_in))
end
end
@timed_testset "L2 projection" begin
@testset "calc_reverse_upper for LGL" begin
@test isapprox(Trixi.calc_reverse_upper(2, Val(:gauss_lobatto)),
[[0.25, 0.25] [0.0, 0.5]])
end
@testset "calc_reverse_lower for LGL" begin
@test isapprox(Trixi.calc_reverse_lower(2, Val(:gauss_lobatto)),
[[0.5, 0.0] [0.25, 0.25]])
end
end
@testset "containers" begin
# Set up mock container
mutable struct MyContainer <: Trixi.AbstractContainer
data::Vector{Int}
capacity::Int
length::Int
dummy::Int
end
function MyContainer(data, capacity)
c = MyContainer(Vector{Int}(undef, capacity + 1), capacity, length(data),
capacity + 1)
c.data[eachindex(data)] .= data
return c
end
MyContainer(data::AbstractArray) = MyContainer(data, length(data))
Trixi.invalidate!(c::MyContainer, first, last) = (c.data[first:last] .= 0; c)
function Trixi.raw_copy!(target::MyContainer, source::MyContainer, first, last,
destination)
Trixi.copy_data!(target.data, source.data, first, last, destination)
return target
end
Trixi.move_connectivity!(c::MyContainer, first, last, destination) = c
Trixi.delete_connectivity!(c::MyContainer, first, last) = c
function Trixi.reset_data_structures!(c::MyContainer)
(c.data = Vector{Int}(undef,
c.capacity + 1);
c)
end
function Base.:(==)(c1::MyContainer, c2::MyContainer)
return (c1.capacity == c2.capacity &&
c1.length == c2.length &&
c1.dummy == c2.dummy &&
c1.data[1:(c1.length)] == c2.data[1:(c2.length)])
end
@testset "size" begin
c = MyContainer([1, 2, 3])
@test size(c) == (3,)
end
@testset "resize!" begin
c = MyContainer([1, 2, 3])
@test length(resize!(c, 2)) == 2
end
@testset "copy!" begin
c1 = MyContainer([1, 2, 3])
c2 = MyContainer([4, 5])
@test Trixi.copy!(c1, c2, 2, 1, 2) == MyContainer([1, 2, 3]) # no-op
c1 = MyContainer([1, 2, 3])
c2 = MyContainer([4, 5])
@test Trixi.copy!(c1, c2, 1, 2, 2) == MyContainer([1, 4, 5])
c1 = MyContainer([1, 2, 3])
@test Trixi.copy!(c1, c2, 1, 2) == MyContainer([1, 4, 3])
c1 = MyContainer([1, 2, 3])
@test Trixi.copy!(c1, 2, 3, 1) == MyContainer([2, 3, 3])
c1 = MyContainer([1, 2, 3])
@test Trixi.copy!(c1, 1, 3) == MyContainer([1, 2, 1])
end
@testset "move!" begin
c = MyContainer([1, 2, 3])
@test Trixi.move!(c, 1, 1) == MyContainer([1, 2, 3]) # no-op
c = MyContainer([1, 2, 3])
@test Trixi.move!(c, 1, 2) == MyContainer([0, 1, 3])
end
@testset "swap!" begin
c = MyContainer([1, 2])
@test Trixi.swap!(c, 1, 1) == MyContainer([1, 2]) # no-op
c = MyContainer([1, 2])
@test Trixi.swap!(c, 1, 2) == MyContainer([2, 1])
end
@testset "erase!" begin
c = MyContainer([1, 2])
@test Trixi.erase!(c, 2, 1) == MyContainer([1, 2]) # no-op
c = MyContainer([1, 2])
@test Trixi.erase!(c, 1) == MyContainer([0, 2])
end
@testset "remove_shift!" begin
c = MyContainer([1, 2, 3, 4])
@test Trixi.remove_shift!(c, 2, 1) == MyContainer([1, 2, 3, 4]) # no-op
c = MyContainer([1, 2, 3, 4])
@test Trixi.remove_shift!(c, 2, 2) == MyContainer([1, 3, 4], 4)
c = MyContainer([1, 2, 3, 4])
@test Trixi.remove_shift!(c, 2) == MyContainer([1, 3, 4], 4)
end
@testset "remove_fill!" begin
c = MyContainer([1, 2, 3, 4])
@test Trixi.remove_fill!(c, 2, 1) == MyContainer([1, 2, 3, 4]) # no-op
c = MyContainer([1, 2, 3, 4])
@test Trixi.remove_fill!(c, 2, 2) == MyContainer([1, 4, 3], 4)
end
@testset "reset!" begin
c = MyContainer([1, 2, 3])
@test Trixi.reset!(c, 2) == MyContainer(Int[], 2)
end
end
@timed_testset "example elixirs" begin
@test basename(examples_dir()) == "examples"
@test !isempty(get_examples())
@test endswith(default_example(), "elixir_advection_basic.jl")
end
@timed_testset "HLL flux with vanishing wave speed estimates (#502)" begin
equations = CompressibleEulerEquations1D(1.4)
u = SVector(1.0, 0.0, 0.0)
@test !any(isnan, flux_hll(u, u, 1, equations))
end
@timed_testset "DG L2 mortar container debug output" begin
c2d = Trixi.L2MortarContainer2D{Float64}(1, 1, 1)
@test isnothing(display(c2d))
c3d = Trixi.L2MortarContainer3D{Float64}(1, 1, 1)
@test isnothing(display(c3d))
end
@timed_testset "Printing indicators/controllers" begin
# OBS! Constructing indicators/controllers using the parameters below doesn't make sense. It's
# just useful to run basic tests of `show` methods.
c = ControllerThreeLevelCombined(1, 2, 3, 10.0, 11.0, 12.0, "primary", "secondary",
"cache")
@test_nowarn show(stdout, c)
indicator_hg = IndicatorHennemannGassner(1.0, 0.0, true, "variable", "cache")
@test_nowarn show(stdout, indicator_hg)
limiter_idp = SubcellLimiterIDP(true, [1], true, [1], ["variable"], 0.1,
true, [(Trixi.entropy_guermond_etal, min)], "cache",
1, (1.0, 1.0), 1.0)
@test_nowarn show(stdout, limiter_idp)
indicator_loehner = IndicatorLöhner(1.0, "variable", (; cache = nothing))
@test_nowarn show(stdout, indicator_loehner)
indicator_max = IndicatorMax("variable", (; cache = nothing))
@test_nowarn show(stdout, indicator_max)
end
@timed_testset "LBM 2D constructor" begin
# Neither Mach number nor velocity set
@test_throws ErrorException LatticeBoltzmannEquations2D(Ma = nothing, Re = 1000)
# Both Mach number and velocity set
@test_throws ErrorException LatticeBoltzmannEquations2D(Ma = 0.1, Re = 1000,
u0 = 1.0)
# Neither Reynolds number nor viscosity set
@test_throws ErrorException LatticeBoltzmannEquations2D(Ma = 0.1, Re = nothing)
# Both Reynolds number and viscosity set
@test_throws ErrorException LatticeBoltzmannEquations2D(Ma = 0.1, Re = 1000,
nu = 1.0)
# No non-dimensional values set
@test LatticeBoltzmannEquations2D(Ma = nothing, Re = nothing, u0 = 1.0,
nu = 1.0) isa
LatticeBoltzmannEquations2D
end
@timed_testset "LBM 3D constructor" begin
# Neither Mach number nor velocity set
@test_throws ErrorException LatticeBoltzmannEquations3D(Ma = nothing, Re = 1000)
# Both Mach number and velocity set
@test_throws ErrorException LatticeBoltzmannEquations3D(Ma = 0.1, Re = 1000,
u0 = 1.0)
# Neither Reynolds number nor viscosity set
@test_throws ErrorException LatticeBoltzmannEquations3D(Ma = 0.1, Re = nothing)
# Both Reynolds number and viscosity set
@test_throws ErrorException LatticeBoltzmannEquations3D(Ma = 0.1, Re = 1000,
nu = 1.0)
# No non-dimensional values set
@test LatticeBoltzmannEquations3D(Ma = nothing, Re = nothing, u0 = 1.0,
nu = 1.0) isa
LatticeBoltzmannEquations3D
end
@timed_testset "LBM 2D functions" begin
# Set up LBM struct and dummy distribution
equation = LatticeBoltzmannEquations2D(Ma = 0.1, Re = 1000)
u = Trixi.equilibrium_distribution(1, 2, 3, equation)
# Component-wise velocity
@test isapprox(Trixi.velocity(u, 1, equation), 2)
@test isapprox(Trixi.velocity(u, 2, equation), 3)
end
@timed_testset "LBM 3D functions" begin
# Set up LBM struct and dummy distribution
equation = LatticeBoltzmannEquations3D(Ma = 0.1, Re = 1000)
u = Trixi.equilibrium_distribution(1, 2, 3, 4, equation)
# Component-wise velocity
@test isapprox(velocity(u, 1, equation), 2)
@test isapprox(velocity(u, 2, equation), 3)
@test isapprox(velocity(u, 3, equation), 4)
end
@timed_testset "LBMCollisionCallback" begin
# Printing of LBM collision callback
callback = LBMCollisionCallback()
@test_nowarn show(stdout, callback)
println()
@test_nowarn show(stdout, "text/plain", callback)
println()
end
@timed_testset "Acoustic perturbation 2D varnames" begin
v_mean_global = (0.0, 0.0)
c_mean_global = 1.0
rho_mean_global = 1.0
equations = AcousticPerturbationEquations2D(v_mean_global, c_mean_global,
rho_mean_global)
@test Trixi.varnames(cons2state, equations) ==
("v1_prime", "v2_prime", "p_prime_scaled")
@test Trixi.varnames(cons2mean, equations) ==
("v1_mean", "v2_mean", "c_mean", "rho_mean")
end
@timed_testset "Euler conversion between conservative/entropy variables" begin
rho, v1, v2, v3, p = 1.0, 0.1, 0.2, 0.3, 2.0
let equations = CompressibleEulerEquations1D(1.4)
cons_vars = prim2cons(SVector(rho, v1, p), equations)
entropy_vars = cons2entropy(cons_vars, equations)
@test cons_vars ≈ entropy2cons(entropy_vars, equations)
# test tuple args
cons_vars = prim2cons((rho, v1, p), equations)
entropy_vars = cons2entropy(cons_vars, equations)
@test cons_vars ≈ entropy2cons(entropy_vars, equations)
end
let equations = CompressibleEulerEquations2D(1.4)
cons_vars = prim2cons(SVector(rho, v1, v2, p), equations)
entropy_vars = cons2entropy(cons_vars, equations)
@test cons_vars ≈ entropy2cons(entropy_vars, equations)
# test tuple args
cons_vars = prim2cons((rho, v1, v2, p), equations)
entropy_vars = cons2entropy(cons_vars, equations)
@test cons_vars ≈ entropy2cons(entropy_vars, equations)
end
let equations = CompressibleEulerEquations3D(1.4)
cons_vars = prim2cons(SVector(rho, v1, v2, v3, p), equations)
entropy_vars = cons2entropy(cons_vars, equations)
@test cons_vars ≈ entropy2cons(entropy_vars, equations)
# test tuple args
cons_vars = prim2cons((rho, v1, v2, v3, p), equations)
entropy_vars = cons2entropy(cons_vars, equations)
@test cons_vars ≈ entropy2cons(entropy_vars, equations)
end
end
@timed_testset "Shallow water conversion between conservative/entropy variables" begin
H, v1, v2, b, a = 3.5, 0.25, 0.1, 0.4, 0.3
let equations = ShallowWaterEquations1D(gravity_constant = 9.8)
cons_vars = prim2cons(SVector(H, v1, b), equations)
entropy_vars = cons2entropy(cons_vars, equations)
@test cons_vars ≈ entropy2cons(entropy_vars, equations)
total_energy = energy_total(cons_vars, equations)
@test total_energy ≈ entropy(cons_vars, equations)
# test tuple args
cons_vars = prim2cons((H, v1, b), equations)
entropy_vars = cons2entropy(cons_vars, equations)
@test cons_vars ≈ entropy2cons(entropy_vars, equations)
end
let equations = ShallowWaterEquations2D(gravity_constant = 9.8)
cons_vars = prim2cons(SVector(H, v1, v2, b), equations)
entropy_vars = cons2entropy(cons_vars, equations)
@test cons_vars ≈ entropy2cons(entropy_vars, equations)
total_energy = energy_total(cons_vars, equations)
@test total_energy ≈ entropy(cons_vars, equations)
# test tuple args
cons_vars = prim2cons((H, v1, v2, b), equations)
entropy_vars = cons2entropy(cons_vars, equations)
@test cons_vars ≈ entropy2cons(entropy_vars, equations)
end
let equations = ShallowWaterEquationsQuasi1D(gravity_constant = 9.8)
cons_vars = prim2cons(SVector(H, v1, b, a), equations)
entropy_vars = cons2entropy(cons_vars, equations)
total_energy = energy_total(cons_vars, equations)
@test entropy(cons_vars, equations) ≈ a * total_energy
end
end
@timed_testset "boundary_condition_do_nothing" begin
rho, v1, v2, p = 1.0, 0.1, 0.2, 0.3, 2.0
let equations = CompressibleEulerEquations2D(1.4)
u = prim2cons(SVector(rho, v1, v2, p), equations)
x = SVector(1.0, 2.0)
t = 0.5
surface_flux = flux_lax_friedrichs
outward_direction = SVector(0.2, -0.3)
@test flux(u, outward_direction, equations) ≈
boundary_condition_do_nothing(u, outward_direction, x, t, surface_flux,
equations)
orientation = 2
direction = 4
@test flux(u, orientation, equations) ≈
boundary_condition_do_nothing(u, orientation, direction, x, t,
surface_flux, equations)
end
end
@timed_testset "boundary_condition_do_nothing_non_conservative" begin
rho, v1, v2, v3, p, B1, B2, B3, psi = 1.0, 0.1, 0.2, 0.3, 1.0, 0.0,
40.0 / sqrt(4.0 * pi), 0.0, 0.0
let equations = IdealGlmMhdEquations2D(1.4, initial_c_h = 1.0)
u = prim2cons(SVector(rho, v1, v2, v3, p, B1, B2, B3, psi), equations)
x = SVector(1.0, 2.0)
t = 0.5
surface_fluxes = (flux_lax_friedrichs, flux_nonconservative_powell)
outward_direction = SVector(0.2, 0.3)
@test all(isapprox(x, y)
for (x, y) in zip(ntuple(i -> surface_fluxes[i](u, u,
outward_direction,
equations), 2),
boundary_condition_do_nothing(u, outward_direction,
x, t, surface_fluxes,
equations)))
orientation = 2
direction = 4
@test all(isapprox(x, y)
for (x, y) in zip(ntuple(i -> surface_fluxes[i](u, u, orientation,
equations), 2),
boundary_condition_do_nothing(u, orientation,
direction, x, t,
surface_fluxes,
equations)))
end
end
@timed_testset "TimeSeriesCallback" begin
# Test the 2D TreeMesh version of the callback and some warnings
@test_nowarn_mod trixi_include(@__MODULE__,
joinpath(examples_dir(), "tree_2d_dgsem",
"elixir_acoustics_gaussian_source.jl"),
tspan = (0, 0.05))
point_data_1 = time_series.affect!.point_data[1]
@test all(isapprox.(point_data_1[1:7],
[-2.4417734981719132e-5, -3.4296207289200194e-5,
0.0018130846385739788, -0.5, 0.25, 1.0, 1.0]))
@test_throws DimensionMismatch Trixi.get_elements_by_coordinates!([1, 2],
rand(2, 4), mesh,
solver, nothing)
@test_nowarn show(stdout, time_series)
@test_throws ArgumentError TimeSeriesCallback(semi, [(1.0, 1.0)]; interval = -1)
@test_throws ArgumentError TimeSeriesCallback(semi, [1.0 1.0 1.0; 2.0 2.0 2.0])
end
@timed_testset "Consistency check for single point flux: CEMCE" begin
equations = CompressibleEulerMulticomponentEquations2D(gammas = (1.4, 1.4),
gas_constants = (0.4, 0.4))
u = SVector(0.1, -0.5, 1.0, 1.0, 2.0)
orientations = [1, 2]
for orientation in orientations
@test flux(u, orientation, equations) ≈
flux_ranocha(u, u, orientation, equations)
end
end
@timed_testset "Consistency check for HLL flux (naive): CEE" begin
flux_hll = FluxHLL(min_max_speed_naive)
# Set up equations and dummy conservative variables state
equations = CompressibleEulerEquations1D(1.4)
u = SVector(1.1, 2.34, 5.5)
orientations = [1]
for orientation in orientations
@test flux_hll(u, u, orientation, equations) ≈ flux(u, orientation, equations)
end
equations = CompressibleEulerEquations2D(1.4)
u = SVector(1.1, -0.5, 2.34, 5.5)
orientations = [1, 2]
for orientation in orientations
@test flux_hll(u, u, orientation, equations) ≈ flux(u, orientation, equations)
end
equations = CompressibleEulerEquations3D(1.4)
u = SVector(1.1, -0.5, 2.34, 2.4, 5.5)
orientations = [1, 2, 3]
for orientation in orientations
@test flux_hll(u, u, orientation, equations) ≈ flux(u, orientation, equations)
end
end
@timed_testset "Consistency check for flux_chan_etal: CEEQ" begin
# Set up equations and dummy conservative variables state
equations = CompressibleEulerEquationsQuasi1D(1.4)
u = SVector(1.1, 2.34, 5.5, 2.73)
orientations = [1]
for orientation in orientations
@test flux_chan_etal(u, u, orientation, equations) ≈
flux(u, orientation, equations)
end
end
@timed_testset "Consistency check for HLL flux (naive): LEE" begin
flux_hll = FluxHLL(min_max_speed_naive)
equations = LinearizedEulerEquations2D(SVector(1.0, 1.0), 1.0, 1.0)
u = SVector(1.1, -0.5, 2.34, 5.5)
orientations = [1, 2]
for orientation in orientations
@test flux_hll(u, u, orientation, equations) ≈ flux(u, orientation, equations)
end
normal_directions = [SVector(1.0, 0.0),
SVector(0.0, 1.0),
SVector(0.5, -0.5),
SVector(-1.2, 0.3)]
for normal_direction in normal_directions
@test flux_hll(u, u, normal_direction, equations) ≈
flux(u, normal_direction, equations)
end
end
@timed_testset "Consistency check for HLL flux (naive): SWE" begin
flux_hll = FluxHLL(min_max_speed_naive)
equations = ShallowWaterEquations1D(gravity_constant = 9.81)
u = SVector(1, 0.5, 0.0)
@test flux_hll(u, u, 1, equations) ≈ flux(u, 1, equations)
u_ll = SVector(0.1, 1.0, 0.0)
u_rr = SVector(0.1, 1.0, 0.0)
@test flux_hll(u_ll, u_rr, 1, equations) ≈ flux(u_ll, 1, equations)
u_ll = SVector(0.1, -1.0, 0.0)
u_rr = SVector(0.1, -1.0, 0.0)
@test flux_hll(u_ll, u_rr, 1, equations) ≈ flux(u_rr, 1, equations)
equations = ShallowWaterEquations2D(gravity_constant = 9.81)
normal_directions = [SVector(1.0, 0.0),
SVector(0.0, 1.0),
SVector(0.5, -0.5),
SVector(-1.2, 0.3)]
u = SVector(1, 0.5, 0.5, 0.0)
for normal_direction in normal_directions
@test flux_hll(u, u, normal_direction, equations) ≈
flux(u, normal_direction, equations)
end
normal_direction = SVector(1.0, 0.0, 0.0)
u_ll = SVector(0.1, 1.0, 1.0, 0.0)
u_rr = SVector(0.1, 1.0, 1.0, 0.0)
@test flux_hll(u_ll, u_rr, normal_direction, equations) ≈
flux(u_ll, normal_direction, equations)
u_ll = SVector(0.1, -1.0, -1.0, 0.0)
u_rr = SVector(0.1, -1.0, -1.0, 0.0)
@test flux_hll(u_ll, u_rr, normal_direction, equations) ≈
flux(u_rr, normal_direction, equations)
end
@timed_testset "Consistency check for HLL flux (naive): MHD" begin
flux_hll = FluxHLL(min_max_speed_naive)
equations = IdealGlmMhdEquations1D(1.4)
u_values = [SVector(1.0, 0.4, -0.5, 0.1, 1.0, 0.1, -0.2, 0.1),
SVector(1.5, -0.2, 0.1, 0.2, 5.0, -0.1, 0.1, 0.2)]
for u in u_values
@test flux_hll(u, u, 1, equations) ≈ flux(u, 1, equations)
end
equations = IdealGlmMhdEquations2D(1.4, 5.0) #= c_h =#
normal_directions = [SVector(1.0, 0.0),
SVector(0.0, 1.0),
SVector(0.5, -0.5),
SVector(-1.2, 0.3)]
orientations = [1, 2]
u_values = [SVector(1.0, 0.4, -0.5, 0.1, 1.0, 0.1, -0.2, 0.1, 0.0),
SVector(1.5, -0.2, 0.1, 0.2, 5.0, -0.1, 0.1, 0.2, 0.2)]
for u in u_values, orientation in orientations
@test flux_hll(u, u, orientation, equations) ≈ flux(u, orientation, equations)
end
for u in u_values, normal_direction in normal_directions
@test flux_hll(u, u, normal_direction, equations) ≈
flux(u, normal_direction, equations)
end
equations = IdealGlmMhdEquations3D(1.4, 5.0) #= c_h =#
normal_directions = [SVector(1.0, 0.0, 0.0),
SVector(0.0, 1.0, 0.0),
SVector(0.0, 0.0, 1.0),
SVector(0.5, -0.5, 0.2),
SVector(-1.2, 0.3, 1.4)]
orientations = [1, 2, 3]
u_values = [SVector(1.0, 0.4, -0.5, 0.1, 1.0, 0.1, -0.2, 0.1, 0.0),
SVector(1.5, -0.2, 0.1, 0.2, 5.0, -0.1, 0.1, 0.2, 0.2)]
for u in u_values, orientation in orientations
@test flux_hll(u, u, orientation, equations) ≈ flux(u, orientation, equations)
end
for u in u_values, normal_direction in normal_directions
@test flux_hll(u, u, normal_direction, equations) ≈
flux(u, normal_direction, equations)
end
end
@timed_testset "Consistency check for HLL flux with Davis wave speed estimates: CEE" begin
flux_hll = FluxHLL(min_max_speed_davis)
# Set up equations and dummy conservative variables state
equations = CompressibleEulerEquations1D(1.4)
u = SVector(1.1, 2.34, 5.5)
orientations = [1]
for orientation in orientations
@test flux_hll(u, u, orientation, equations) ≈ flux(u, orientation, equations)
end
equations = CompressibleEulerEquations2D(1.4)
u = SVector(1.1, -0.5, 2.34, 5.5)
orientations = [1, 2]
for orientation in orientations
@test flux_hll(u, u, orientation, equations) ≈ flux(u, orientation, equations)
end
normal_directions = [SVector(1.0, 0.0),
SVector(0.0, 1.0),
SVector(0.5, -0.5),
SVector(-1.2, 0.3)]
for normal_direction in normal_directions
@test flux_hll(u, u, normal_direction, equations) ≈
flux(u, normal_direction, equations)
end
equations = CompressibleEulerEquations3D(1.4)
u = SVector(1.1, -0.5, 2.34, 2.4, 5.5)
orientations = [1, 2, 3]
for orientation in orientations
@test flux_hll(u, u, orientation, equations) ≈ flux(u, orientation, equations)
end
normal_directions = [SVector(1.0, 0.0, 0.0),
SVector(0.0, 1.0, 0.0),
SVector(0.0, 0.0, 1.0),
SVector(0.5, -0.5, 0.2),
SVector(-1.2, 0.3, 1.4)]
for normal_direction in normal_directions
@test flux_hll(u, u, normal_direction, equations) ≈
flux(u, normal_direction, equations)
end
end
@timed_testset "Consistency check for HLL flux with Davis wave speed estimates: Polytropic CEE" begin
flux_hll = FluxHLL(min_max_speed_davis)
gamma = 1.4
kappa = 0.5 # Scaling factor for the pressure.
equations = PolytropicEulerEquations2D(gamma, kappa)
u = SVector(1.1, -0.5, 2.34)
orientations = [1, 2]
for orientation in orientations
@test flux_hll(u, u, orientation, equations) ≈ flux(u, orientation, equations)
end
normal_directions = [SVector(1.0, 0.0),
SVector(0.0, 1.0),
SVector(0.5, -0.5),
SVector(-1.2, 0.3)]
for normal_direction in normal_directions
@test flux_hll(u, u, normal_direction, equations) ≈
flux(u, normal_direction, equations)
end
end
@timed_testset "Consistency check for Winters flux: Polytropic CEE" begin
for gamma in [1.4, 1.0, 5 / 3]
kappa = 0.5 # Scaling factor for the pressure.
equations = PolytropicEulerEquations2D(gamma, kappa)
u = SVector(1.1, -0.5, 2.34)
orientations = [1, 2]
for orientation in orientations
@test flux_winters_etal(u, u, orientation, equations) ≈
flux(u, orientation, equations)
end
normal_directions = [SVector(1.0, 0.0),
SVector(0.0, 1.0),
SVector(0.5, -0.5),
SVector(-1.2, 0.3)]
for normal_direction in normal_directions
@test flux_winters_etal(u, u, normal_direction, equations) ≈
flux(u, normal_direction, equations)
end
end
end
@timed_testset "Consistency check for Lax-Friedrich flux: Polytropic CEE" begin
for gamma in [1.4, 1.0, 5 / 3]
kappa = 0.5 # Scaling factor for the pressure.
equations = PolytropicEulerEquations2D(gamma, kappa)
u = SVector(1.1, -0.5, 2.34)
orientations = [1, 2]
for orientation in orientations
@test flux_lax_friedrichs(u, u, orientation, equations) ≈
flux(u, orientation, equations)
end
normal_directions = [SVector(1.0, 0.0),
SVector(0.0, 1.0),
SVector(0.5, -0.5),
SVector(-1.2, 0.3)]
for normal_direction in normal_directions
@test flux_lax_friedrichs(u, u, normal_direction, equations) ≈
flux(u, normal_direction, equations)
end
end
end
@timed_testset "Consistency check for HLL flux with Davis wave speed estimates: LEE" begin
flux_hll = FluxHLL(min_max_speed_davis)
equations = LinearizedEulerEquations2D(SVector(1.0, 1.0), 1.0, 1.0)
u = SVector(1.1, -0.5, 2.34, 5.5)
orientations = [1, 2]
for orientation in orientations
@test flux_hll(u, u, orientation, equations) ≈ flux(u, orientation, equations)
end
normal_directions = [SVector(1.0, 0.0),