Skip to content

Commit

Permalink
One-sided upwind reconstruction (#3658)
Browse files Browse the repository at this point in the history
* initiate stuff

* add right bias

* finally it works

* test it here

* bugfix

* remove the inbounds

* should work!

* correct nonuniform weno

* fix tests

* correct positivity preserving scheme

* this works

* try with metaprogramming

* maybe like this is better?

* more changes

* removed all divergence

* some bugfixes

* Stretched Upwind reconstruction

* fixing a typo

* another typo

* call it weno stencil

* just test hypothesis

* show have resolved the memory issue

* test it out

* bugfix

* try without conditioning

* test it without bounding

* we try with the complete stencil

* underlying grid

* remove some vestigial code

* try a nothrow

* remove assume effects

* this should work better

* remove js weno

* another test

* back as it were

* reduce computation

* starting the mad change

* back to how it was before

* cheecky test

* remove cheecky test

* we need to add another method?

* this should work!

* bugfix

* a little more bugfix

* fix tests

* some cleanup

* put back code

* another file restorec

* whoops forgot a function

* typo

* last typo

* back to LeftBias and RightBias

* add benchmarking

* correct benchmarks

* some documentation

* change to if condition

* fix positivity preserving upwind

* add some docs

* some more documentation

* more documentation

* correct docs

* better docs

* a couple of `map`

* more documentation

* correct indenting

* correct indenting

* typo
  • Loading branch information
simone-silvestri authored Jul 25, 2024
1 parent eb7c178 commit abb66e3
Show file tree
Hide file tree
Showing 20 changed files with 1,918 additions and 1,217 deletions.
1,951 changes: 1,263 additions & 688 deletions benchmark/Manifest.toml

Large diffs are not rendered by default.

3 changes: 2 additions & 1 deletion benchmark/Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ FFTW = "7a1cc6ca-52ef-59f5-83cd-3a7055c09341"
IncompleteLU = "40713840-3770-5561-ab4c-a76e7d0d7895"
IterativeSolvers = "42fd0dbc-a981-5370-80f2-aaf504508153"
JLD2 = "033835bb-8acc-5ee8-8aae-3f567f8a3819"
JSON3 = "0f8b85d8-7281-11e9-16c2-39a750bddbf1"
Logging = "56ddb016-857b-54e1-b83d-db4d58db5568"
MPI = "da04e1cc-30fd-572f-bb4f-1f8673147195"
Oceananigans = "9e8cae18-63c1-5223-a75c-80ca9d6e9a09"
Expand All @@ -22,4 +23,4 @@ PyPlot = "d330b81b-6aea-500a-939a-2ce795aea3ee"
SeawaterPolynomials = "d496a93d-167e-4197-9f49-d3af4ff8fe40"

[compat]
CUDA = "3.8"
CUDA = "^5"
10 changes: 6 additions & 4 deletions benchmark/benchmark_advection_schemes.jl
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,10 @@ using Benchmarks

# Benchmark function

function benchmark_advection_scheme(Arch, Scheme)
function benchmark_advection_scheme(Arch, Scheme, order)
grid = RectilinearGrid(Arch(); size=(192, 192, 192), extent=(1, 1, 1))
model = NonhydrostaticModel(grid=grid, advection=Scheme())
order = Scheme == Centered ? order + 1 : order
model = NonhydrostaticModel(grid=grid, advection=Scheme(; order))

time_step!(model, 1) # warmup

Expand All @@ -24,12 +25,13 @@ end
# Benchmark parameters

Architectures = has_cuda() ? [CPU, GPU] : [CPU]
Schemes = (CenteredSecondOrder, CenteredFourthOrder, UpwindBiasedThirdOrder, UpwindBiasedFifthOrder, WENO)
Schemes = (Centered, UpwindBiased, WENO)
orders = (1, 3, 5, 7, 9)

# Run and summarize benchmarks

print_system_info()
suite = run_benchmarks(benchmark_advection_scheme; Architectures, Schemes)
suite = run_benchmarks(benchmark_advection_scheme; Architectures, Schemes, orders)

df = benchmarks_dataframe(suite)
sort!(df, [:Architectures, :Schemes], by=string)
Expand Down
6 changes: 3 additions & 3 deletions benchmark/src/Benchmarks.jl
Original file line number Diff line number Diff line change
Expand Up @@ -88,15 +88,15 @@ end

function benchmarks_pretty_table(df; title="")
header = propertynames(df) .|> String
pretty_table(df, header, nosubheader=true, title=title, title_alignment=:c,
pretty_table(String, df; header, title=title, title_alignment=:c,
title_autowrap = true, title_same_width_as_table = true)

html_filename = replace(title, ' ' => '_') * ".html"
@info "Writing $html_filename..."
open(html_filename, "w") do io
html_table = pretty_table(String, df, header, nosubheader=true,
html_table = pretty_table(String, df; header,
title=title, title_alignment=:c,
backend=:html, tf=tf_html_simple)
backend=Val(:html), tf=tf_html_simple)
write(io, html_table)
end

Expand Down
2 changes: 1 addition & 1 deletion src/Advection/Advection.jl
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,8 @@ include("stretched_weno_smoothness.jl")
include("multi_dimensional_reconstruction.jl")
include("vector_invariant_upwinding.jl")
include("vector_invariant_advection.jl")
include("vector_invariant_cross_upwinding.jl")
include("vector_invariant_self_upwinding.jl")
include("vector_invariant_cross_upwinding.jl")

include("flat_advective_fluxes.jl")
include("topologically_conditional_interpolation.jl")
Expand Down
21 changes: 7 additions & 14 deletions src/Advection/centered_reconstruction.jl
Original file line number Diff line number Diff line change
Expand Up @@ -91,21 +91,14 @@ CenteredFourthOrder(grid=nothing, FT::DataType=Float64) = Centered(grid, FT; ord
const ACAS = AbstractCenteredAdvectionScheme

# left and right biased for Centered reconstruction are just symmetric!
@inline left_biased_interpolate_xᶠᵃᵃ(i, j, k, grid, scheme::ACAS, c, args...) = symmetric_interpolate_xᶠᵃᵃ(i, j, k, grid, scheme, c, args...)
@inline left_biased_interpolate_yᵃᶠᵃ(i, j, k, grid, scheme::ACAS, c, args...) = symmetric_interpolate_yᵃᶠᵃ(i, j, k, grid, scheme, c, args...)
@inline left_biased_interpolate_zᵃᵃᶠ(i, j, k, grid, scheme::ACAS, c, args...) = symmetric_interpolate_zᵃᵃᶠ(i, j, k, grid, scheme, c, args...)
@inline biased_interpolate_xᶠᵃᵃ(i, j, k, grid, scheme::ACAS, bias, c, args...) = symmetric_interpolate_xᶠᵃᵃ(i, j, k, grid, scheme, c, args...)
@inline biased_interpolate_yᵃᶠᵃ(i, j, k, grid, scheme::ACAS, bias, c, args...) = symmetric_interpolate_yᵃᶠᵃ(i, j, k, grid, scheme, c, args...)
@inline biased_interpolate_zᵃᵃᶠ(i, j, k, grid, scheme::ACAS, bias, c, args...) = symmetric_interpolate_zᵃᵃᶠ(i, j, k, grid, scheme, c, args...)

@inline right_biased_interpolate_xᶠᵃᵃ(i, j, k, grid, scheme::ACAS, c, args...) = symmetric_interpolate_xᶠᵃᵃ(i, j, k, grid, scheme, c, args...)
@inline right_biased_interpolate_yᵃᶠᵃ(i, j, k, grid, scheme::ACAS, c, args...) = symmetric_interpolate_yᵃᶠᵃ(i, j, k, grid, scheme, c, args...)
@inline right_biased_interpolate_zᵃᵃᶠ(i, j, k, grid, scheme::ACAS, c, args...) = symmetric_interpolate_zᵃᵃᶠ(i, j, k, grid, scheme, c, args...)

@inline left_biased_interpolate_xᶜᵃᵃ(i, j, k, grid, scheme::ACAS, u, args...) = symmetric_interpolate_xᶜᵃᵃ(i, j, k, grid, scheme, u, args...)
@inline left_biased_interpolate_yᵃᶜᵃ(i, j, k, grid, scheme::ACAS, v, args...) = symmetric_interpolate_yᵃᶜᵃ(i, j, k, grid, scheme, v, args...)
@inline left_biased_interpolate_zᵃᵃᶜ(i, j, k, grid, scheme::ACAS, w, args...) = symmetric_interpolate_zᵃᵃᶜ(i, j, k, grid, scheme, w, args...)

@inline right_biased_interpolate_xᶜᵃᵃ(i, j, k, grid, scheme::ACAS, u, args...) = symmetric_interpolate_xᶜᵃᵃ(i, j, k, grid, scheme, u, args...)
@inline right_biased_interpolate_yᵃᶜᵃ(i, j, k, grid, scheme::ACAS, v, args...) = symmetric_interpolate_yᵃᶜᵃ(i, j, k, grid, scheme, v, args...)
@inline right_biased_interpolate_zᵃᵃᶜ(i, j, k, grid, scheme::ACAS, w, args...) = symmetric_interpolate_zᵃᵃᶜ(i, j, k, grid, scheme, w, args...)
# left and right biased for Centered reconstruction are just symmetric!
@inline biased_interpolate_xᶜᵃᵃ(i, j, k, grid, scheme::ACAS, bias, c, args...) = symmetric_interpolate_xᶜᵃᵃ(i, j, k, grid, scheme, c, args...)
@inline biased_interpolate_yᵃᶜᵃ(i, j, k, grid, scheme::ACAS, bias, c, args...) = symmetric_interpolate_yᵃᶜᵃ(i, j, k, grid, scheme, c, args...)
@inline biased_interpolate_zᵃᵃᶜ(i, j, k, grid, scheme::ACAS, bias, c, args...) = symmetric_interpolate_zᵃᵃᶜ(i, j, k, grid, scheme, c, args...)

# uniform centered reconstruction
for buffer in advection_buffers
Expand Down
24 changes: 12 additions & 12 deletions src/Advection/positivity_preserving_tracer_advection_operators.jl
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,10 @@ end

cᵢⱼ = @inbounds c[i, j, k]

c₊ᴸ = _left_biased_interpolate_xᶠᵃᵃ(i+1, j, k, grid, advection, c)
c₊ᴿ = _right_biased_interpolate_xᶠᵃᵃ(i+1, j, k, grid, advection, c)
c₋ᴸ = _left_biased_interpolate_xᶠᵃᵃ(i, j, k, grid, advection, c)
c₋ᴿ = _right_biased_interpolate_xᶠᵃᵃ(i, j, k, grid, advection, c)
c₊ᴸ = _biased_interpolate_xᶠᵃᵃ(i+1, j, k, grid, advection, LeftBias(), c)
c₊ᴿ = _biased_interpolate_xᶠᵃᵃ(i+1, j, k, grid, advection, RightBias(), c)
c₋ᴸ = _biased_interpolate_xᶠᵃᵃ(i, j, k, grid, advection, LeftBias(), c)
c₋ᴿ = _biased_interpolate_xᶠᵃᵃ(i, j, k, grid, advection, RightBias(), c)

= (cᵢⱼ - ω̂₁ * c₋ᴿ - ω̂ₙ * c₊ᴸ) / (1 - 2ω̂₁)
M = max(p̃, c₊ᴸ, c₋ᴿ)
Expand All @@ -53,10 +53,10 @@ end

cᵢⱼ = @inbounds c[i, j, k]

c₊ᴸ = _left_biased_interpolate_yᵃᶠᵃ(i, j+1, k, grid, advection, c)
c₊ᴿ = _right_biased_interpolate_yᵃᶠᵃ(i, j+1, k, grid, advection, c)
c₋ᴸ = _left_biased_interpolate_yᵃᶠᵃ(i, j, k, grid, advection, c)
c₋ᴿ = _right_biased_interpolate_yᵃᶠᵃ(i, j, k, grid, advection, c)
c₊ᴸ = _biased_interpolate_yᵃᶠᵃ(i, j+1, k, grid, advection, LeftBias(), c)
c₊ᴿ = _biased_interpolate_yᵃᶠᵃ(i, j+1, k, grid, advection, RightBias(), c)
c₋ᴸ = _biased_interpolate_yᵃᶠᵃ(i, j, k, grid, advection, LeftBias(), c)
c₋ᴿ = _biased_interpolate_yᵃᶠᵃ(i, j, k, grid, advection, RightBias(), c)

= (cᵢⱼ - ω̂₁ * c₋ᴿ - ω̂ₙ * c₊ᴸ) / (1 - 2ω̂₁)
M = max(p̃, c₊ᴸ, c₋ᴿ)
Expand All @@ -77,10 +77,10 @@ end

cᵢⱼ = @inbounds c[i, j, k]

c₊ᴸ = _left_biased_interpolate_zᵃᵃᶠ(i, j, k+1, grid, advection, c)
c₊ᴿ = _right_biased_interpolate_zᵃᵃᶠ(i, j, k+1, grid, advection, c)
c₋ᴸ = _left_biased_interpolate_zᵃᵃᶠ(i, j, k, grid, advection, c)
c₋ᴿ = _right_biased_interpolate_zᵃᵃᶠ(i, j, k, grid, advection, c)
c₊ᴸ = _biased_interpolate_zᵃᵃᶠ(i, j, k+1, grid, advection, LeftBias(), c)
c₊ᴿ = _biased_interpolate_zᵃᵃᶠ(i, j, k+1, grid, advection, RightBias(), c)
c₋ᴸ = _biased_interpolate_zᵃᵃᶠ(i, j, k, grid, advection, LeftBias(), c)
c₋ᴿ = _biased_interpolate_zᵃᵃᶠ(i, j, k, grid, advection, RightBias(), c)

= (cᵢⱼ - ω̂₁ * c₋ᴿ - ω̂ₙ * c₊ᴸ) / (1 - 2ω̂₁)
M = max(p̃, c₊ᴸ, c₋ᴿ)
Expand Down
65 changes: 53 additions & 12 deletions src/Advection/reconstruction_coefficients.jl
Original file line number Diff line number Diff line change
@@ -1,28 +1,69 @@
# Generic reconstruction methods valid for all reconstruction schemes
# Unroll the functions to pass the coordinates in case of a stretched grid
"""
@inline symmetric_interpolate_xᶠᵃᵃ(i, j, k, grid, scheme, ψ, args...)
high order centered reconstruction of variable ψ in the x-direction. ψ can be a `Function`
with signature `ψ(i, j, k, grid, args...)` or an `AbstractArray`
"""
@inline symmetric_interpolate_xᶠᵃᵃ(i, j, k, grid, scheme, ψ, args...) = inner_symmetric_interpolate_xᶠᵃᵃ(i, j, k, grid, scheme, ψ, i, Face, args...)

"""
@inline symmetric_interpolate_yᵃᶠᵃ(i, j, k, grid, scheme, ψ, args...)
high order centered reconstruction of variable ψ in the y-direction. ψ can be a `Function`
with signature `ψ(i, j, k, grid, args...)` or an `AbstractArray`
"""
@inline symmetric_interpolate_yᵃᶠᵃ(i, j, k, grid, scheme, ψ, args...) = inner_symmetric_interpolate_yᵃᶠᵃ(i, j, k, grid, scheme, ψ, j, Face, args...)

"""
@inline symmetric_interpolate_zᵃᵃᶠ(i, j, k, grid, scheme, ψ, args...)
high order centered reconstruction of variable ψ in the z-direction. ψ can be a `Function`
with signature `ψ(i, j, k, grid, args...)` or an `AbstractArray`
"""
@inline symmetric_interpolate_zᵃᵃᶠ(i, j, k, grid, scheme, ψ, args...) = inner_symmetric_interpolate_zᵃᵃᶠ(i, j, k, grid, scheme, ψ, k, Face, args...)

""" same as [`symmetric_interpolate_xᶠᵃᵃ`](@ref) but on `Center`s instead of `Face`s """
@inline symmetric_interpolate_xᶜᵃᵃ(i, j, k, grid, scheme, ψ, args...) = inner_symmetric_interpolate_xᶠᵃᵃ(i+1, j, k, grid, scheme, ψ, i, Center, args...)
""" same as [`symmetric_interpolate_yᵃᶠᵃ`](@ref) but on `Center`s instead of `Face`s """
@inline symmetric_interpolate_yᵃᶜᵃ(i, j, k, grid, scheme, ψ, args...) = inner_symmetric_interpolate_yᵃᶠᵃ(i, j+1, k, grid, scheme, ψ, j, Center, args...)
""" same as [`symmetric_interpolate_zᵃᵃᶠ`](@ref) but on `Center`s instead of `Face`s """
@inline symmetric_interpolate_zᵃᵃᶜ(i, j, k, grid, scheme, ψ, args...) = inner_symmetric_interpolate_zᵃᵃᶠ(i, j, k+1, grid, scheme, ψ, k, Center, args...)

@inline left_biased_interpolate_xᶠᵃᵃ(i, j, k, grid, scheme, ψ, args...) = inner_left_biased_interpolate_xᶠᵃᵃ(i, j, k, grid, scheme, ψ, i, Face, args...)
@inline left_biased_interpolate_yᵃᶠᵃ(i, j, k, grid, scheme, ψ, args...) = inner_left_biased_interpolate_yᵃᶠᵃ(i, j, k, grid, scheme, ψ, j, Face, args...)
@inline left_biased_interpolate_zᵃᵃᶠ(i, j, k, grid, scheme, ψ, args...) = inner_left_biased_interpolate_zᵃᵃᶠ(i, j, k, grid, scheme, ψ, k, Face, args...)
"""
@inline biased_interpolate_xᶠᵃᵃ(i, j, k, grid, scheme, bias, ψ, args...)
@inline right_biased_interpolate_xᶠᵃᵃ(i, j, k, grid, scheme, ψ, args...) = inner_right_biased_interpolate_xᶠᵃᵃ(i, j, k, grid, scheme, ψ, i, Face, args...)
@inline right_biased_interpolate_yᵃᶠᵃ(i, j, k, grid, scheme, ψ, args...) = inner_right_biased_interpolate_yᵃᶠᵃ(i, j, k, grid, scheme, ψ, j, Face, args...)
@inline right_biased_interpolate_zᵃᵃᶠ(i, j, k, grid, scheme, ψ, args...) = inner_right_biased_interpolate_zᵃᵃᶠ(i, j, k, grid, scheme, ψ, k, Face, args...)
high order biased reconstruction of variable ψ in the x-direction. ψ can be a `Function`
with signature `ψ(i, j, k, grid, args...)` or an `AbstractArray`. The `bias` argument is
either `LeftBias` for a left biased reconstruction, or `RightBias` for a right biased reconstruction
"""
@inline biased_interpolate_xᶠᵃᵃ(i, j, k, grid, scheme, bias, ψ, args...) = inner_biased_interpolate_xᶠᵃᵃ(i, j, k, grid, scheme, bias, ψ, i, Face, args...)

@inline left_biased_interpolate_xᶜᵃᵃ(i, j, k, grid, scheme, ψ, args...) = inner_left_biased_interpolate_xᶠᵃᵃ(i+1, j, k, grid, scheme, ψ, i, Center, args...)
@inline left_biased_interpolate_yᵃᶜᵃ(i, j, k, grid, scheme, ψ, args...) = inner_left_biased_interpolate_yᵃᶠᵃ(i, j+1, k, grid, scheme, ψ, j, Center, args...)
@inline left_biased_interpolate_zᵃᵃᶜ(i, j, k, grid, scheme, ψ, args...) = inner_left_biased_interpolate_zᵃᵃᶠ(i, j, k+1, grid, scheme, ψ, k, Center, args...)
"""
@inline biased_interpolate_yᵃᶠᵃ(i, j, k, grid, scheme, bias, ψ, args...)
@inline right_biased_interpolate_xᶜᵃᵃ(i, j, k, grid, scheme, ψ, args...) = inner_right_biased_interpolate_xᶠᵃᵃ(i+1, j, k, grid, scheme, ψ, i, Center, args...)
@inline right_biased_interpolate_yᵃᶜᵃ(i, j, k, grid, scheme, ψ, args...) = inner_right_biased_interpolate_yᵃᶠᵃ(i, j+1, k, grid, scheme, ψ, j, Center, args...)
@inline right_biased_interpolate_zᵃᵃᶜ(i, j, k, grid, scheme, ψ, args...) = inner_right_biased_interpolate_zᵃᵃᶠ(i, j, k+1, grid, scheme, ψ, k, Center, args...)
high order biased reconstruction of variable ψ in the y-direction. ψ can be a `Function`
with signature `ψ(i, j, k, grid, args...)` or an `AbstractArray`. The `bias` argument is
either `LeftBias` for a left biased reconstruction, or `RightBias` for a right biased reconstruction
"""
@inline biased_interpolate_yᵃᶠᵃ(i, j, k, grid, scheme, bias, ψ, args...) = inner_biased_interpolate_yᵃᶠᵃ(i, j, k, grid, scheme, bias, ψ, j, Face, args...)

"""
@inline biased_interpolate_zᵃᵃᶠ(i, j, k, grid, scheme, bias, ψ, args...)
high order biased reconstruction of variable ψ in the z-direction. ψ can be a `Function`
with signature `ψ(i, j, k, grid, args...)` or an `AbstractArray`. The `bias` argument is
either `LeftBias` for a left biased reconstruction, or `RightBias` for a right biased reconstruction
"""
@inline biased_interpolate_zᵃᵃᶠ(i, j, k, grid, scheme, bias, ψ, args...) = inner_biased_interpolate_zᵃᵃᶠ(i, j, k, grid, scheme, bias, ψ, k, Face, args...)

""" same as [`biased_interpolate_xᶠᵃᵃ`](@ref) but on `Center`s instead of `Face`s """
@inline biased_interpolate_xᶜᵃᵃ(i, j, k, grid, scheme, bias, ψ, args...) = inner_biased_interpolate_xᶠᵃᵃ(i+1, j, k, grid, scheme, bias, ψ, i, Center, args...)
""" same as [`biased_interpolate_yᵃᶠᵃ`](@ref) but on `Center`s instead of `Face`s """
@inline biased_interpolate_yᵃᶜᵃ(i, j, k, grid, scheme, bias, ψ, args...) = inner_biased_interpolate_yᵃᶠᵃ(i, j+1, k, grid, scheme, bias, ψ, j, Center, args...)
""" same as [`biased_interpolate_zᵃᵃᶠ`](@ref) but on `Center`s instead of `Face`s """
@inline biased_interpolate_zᵃᵃᶜ(i, j, k, grid, scheme, bias, ψ, args...) = inner_biased_interpolate_zᵃᵃᶠ(i, j, k+1, grid, scheme, bias, ψ, k, Center, args...)

struct FirstDerivative end
struct SecondDerivative end
Expand Down
Loading

0 comments on commit abb66e3

Please sign in to comment.