|
| 1 | +@testset "struct tests" begin |
| 2 | + @testset "1D Shu test" begin |
| 3 | + |
| 4 | + # Number of grid points |
| 5 | + nx = 200 |
| 6 | + |
| 7 | + x_min = -1.0 |
| 8 | + x_max = 1.0 |
| 9 | + Lx = x_max - x_min |
| 10 | + |
| 11 | + x = range(x_min, stop=x_max, length=nx) |
| 12 | + |
| 13 | + # Courant number |
| 14 | + CFL = 0.4 |
| 15 | + period = 2 |
| 16 | + |
| 17 | + # Parameters for Shu test |
| 18 | + z = -0.7 |
| 19 | + δ = 0.005 |
| 20 | + β = log(2)/(36*δ^2) |
| 21 | + a = 0.5 |
| 22 | + α = 10 |
| 23 | + |
| 24 | + # Functions |
| 25 | + G(x, β, z) = exp.(-β .* (x .- z).^2) |
| 26 | + F(x, α, a) = sqrt.(max.(1 .- α^2 .* (x .- a).^2, 0.0)) |
| 27 | + |
| 28 | + # Grid x assumed defined |
| 29 | + u0_vec = zeros(length(x)) |
| 30 | + |
| 31 | + # Gaussian-like smooth bump at x in [-0.8, -0.6] |
| 32 | + idx = (x .>= -0.8) .& (x .<= -0.6) |
| 33 | + u0_vec[idx] .= (1/6) .* (G(x[idx], β, z - δ) .+ 4 .* G(x[idx], β, z) .+ G(x[idx], β, z + δ)) |
| 34 | + |
| 35 | + # Heaviside step at x in [-0.4, -0.2] |
| 36 | + idx = (x .>= -0.4) .& (x .<= -0.2) |
| 37 | + u0_vec[idx] .= 1.0 |
| 38 | + |
| 39 | + # Piecewise linear ramp at x in [0, 0.2] |
| 40 | + # Triangular spike at x=0.1, base width 0.2 |
| 41 | + idx = abs.(x .- 0.1) .<= 0.1 |
| 42 | + u0_vec[idx] .= 1 .- 10 .* abs.(x[idx] .- 0.1) |
| 43 | + |
| 44 | + # Elliptic/smooth bell at x in [0.4, 0.6] |
| 45 | + idx = (x .>= 0.4) .& (x .<= 0.6) |
| 46 | + u0_vec[idx] .= (1/6) .* (F(x[idx], α, a - δ) .+ 4 .* F(x[idx], α, a) .+ F(x[idx], α, a + δ)) |
| 47 | + |
| 48 | + |
| 49 | + u = copy(u0_vec) |
| 50 | + weno = WENOScheme(u; boundary=(2, 2), stag=true) |
| 51 | + |
| 52 | + # advection velocity |
| 53 | + a = ones(nx+1) .* 1 |
| 54 | + |
| 55 | + # grid size |
| 56 | + Δx = x[2] - x[1] |
| 57 | + Δt = CFL*Δx^(5/3) |
| 58 | + |
| 59 | + tmax = period * (Lx + Δx) / maximum(abs.(a)) |
| 60 | + |
| 61 | + t = 0 |
| 62 | + |
| 63 | + while t < tmax |
| 64 | + WENO_step!(u, a, weno, Δt, Δx) |
| 65 | + |
| 66 | + t += Δt |
| 67 | + |
| 68 | + if t + Δt > tmax |
| 69 | + Δt = tmax - t |
| 70 | + end |
| 71 | + end |
| 72 | + |
| 73 | + @test sum(u) ≈ 51.92724276042664 |
| 74 | + @test maximum(u) ≈ 0.9991824828036449 |
| 75 | + end |
| 76 | + |
| 77 | + @testset "1D Shu test Chmy CPU" begin# |
| 78 | + |
| 79 | + backend=CPU() |
| 80 | + nx=200 |
| 81 | + |
| 82 | + arch = Arch(backend) |
| 83 | + |
| 84 | + x_min = -1.0 |
| 85 | + x_max = 1.0 |
| 86 | + Lx = x_max - x_min |
| 87 | + |
| 88 | + x = range(x_min, stop=x_max, length=nx) |
| 89 | + |
| 90 | + grid = UniformGrid(arch; origin=(x_min,), extent=(Lx,), dims=(nx,)) |
| 91 | + |
| 92 | + # Courant number |
| 93 | + CFL = 0.7 |
| 94 | + period = 4 |
| 95 | + |
| 96 | + # Parameters |
| 97 | + z = -0.7 |
| 98 | + δ = 0.005 |
| 99 | + β = log(2)/(36*δ^2) |
| 100 | + a = 0.5 |
| 101 | + α = 10 |
| 102 | + |
| 103 | + # Functions |
| 104 | + G(x, β, z) = exp.(-β .* (x .- z).^2) |
| 105 | + F(x, α, a) = sqrt.(max.(1 .- α^2 .* (x .- a).^2, 0.0)) |
| 106 | + |
| 107 | + # Grid x assumed defined |
| 108 | + u0_vec = zeros(length(x)) |
| 109 | + |
| 110 | + # Gaussian-like smooth bump at x in [-0.8, -0.6] |
| 111 | + idx = (x .>= -0.8) .& (x .<= -0.6) |
| 112 | + u0_vec[idx] .= (1/6) .* (G(x[idx], β, z - δ) .+ 4 .* G(x[idx], β, z) .+ G(x[idx], β, z + δ)) |
| 113 | + |
| 114 | + # Heaviside step at x in [-0.4, -0.2] |
| 115 | + idx = (x .>= -0.4) .& (x .<= -0.2) |
| 116 | + u0_vec[idx] .= 1.0 |
| 117 | + |
| 118 | + # Piecewise linear ramp at x in [0, 0.2] |
| 119 | + # Triangular spike at x=0.1, base width 0.2 |
| 120 | + idx = abs.(x .- 0.1) .<= 0.1 |
| 121 | + u0_vec[idx] .= 1 .- 10 .* abs.(x[idx] .- 0.1) |
| 122 | + |
| 123 | + # Elliptic/smooth bell at x in [0.4, 0.6] |
| 124 | + idx = (x .>= 0.4) .& (x .<= 0.6) |
| 125 | + u0_vec[idx] .= (1/6) .* (F(x[idx], α, a - δ) .+ 4 .* F(x[idx], α, a) .+ F(x[idx], α, a + δ)) |
| 126 | + |
| 127 | + |
| 128 | + u = Field(backend, grid, Center()) |
| 129 | + set!(u, u0_vec) |
| 130 | + weno = WENOScheme(u, grid; boundary=(2, 2), stag=true) |
| 131 | + |
| 132 | + # advection velocity |
| 133 | + a_vec = ones(nx+1) .* -1 |
| 134 | + a = VectorField(backend, grid) |
| 135 | + set!(a, a_vec) |
| 136 | + |
| 137 | + # grid size |
| 138 | + Δx = x[2] - x[1] |
| 139 | + Δt = CFL * Δx^(5/3) |
| 140 | + |
| 141 | + tmax = period * (Lx+Δx) / maximum(abs.(a.x)) |
| 142 | + |
| 143 | + t = 0 |
| 144 | + |
| 145 | + while t < tmax |
| 146 | + WENO_step!(u, a, weno, Δt, Δx, grid, arch) |
| 147 | + |
| 148 | + t += Δt |
| 149 | + |
| 150 | + if t + Δt > tmax |
| 151 | + Δt = tmax - t |
| 152 | + end |
| 153 | + end |
| 154 | + |
| 155 | + @test sum(u) ≈ 51.92724276042664 |
| 156 | + @test maximum(u) ≈ 0.9991824828036449 |
| 157 | + end |
| 158 | +end |
0 commit comments