Skip to content

Commit

Permalink
prim2phys
Browse files Browse the repository at this point in the history
  • Loading branch information
ranocha committed Aug 18, 2024
1 parent 5679c0c commit 40dc766
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 3 deletions.
2 changes: 1 addition & 1 deletion src/DispersiveShallowWater.jl
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ export AbstractShallowWaterEquations,
SvärdKalischEquations1D, SvaerdKalischEquations1D,
SerreGreenNaghdiEquations1D, HyperbolicSerreGreenNaghdiEquations1D

export prim2prim, prim2cons, cons2prim,
export prim2prim, prim2cons, cons2prim, prim2phys,
waterheight_total, waterheight,
velocity, momentum, discharge,
gravity_constant,
Expand Down
27 changes: 25 additions & 2 deletions src/equations/equations.jl
Original file line number Diff line number Diff line change
Expand Up @@ -349,7 +349,7 @@ of another set of equations and `Val{false}()` otherwise (default).
For example, the [`HyperbolicSerreGreenNaghdiEquations1D`](@ref) are
a hyperbolic approximation of the [`SerreGreenNaghdiEquations1D`](@ref).
See also [`hyperbolic_approximation_limit`](@ref).
See also [`hyperbolic_approximation_limit`](@ref) and [`prim2phys`](@ref).
!!! note "Implementation details"
This function is mostly used for some internal dispatch. For example,
Expand All @@ -364,7 +364,7 @@ is_hyperbolic_appproximation(::AbstractEquations) = Val{false}()
If the equations are a hyperbolic approximation of another set of equations,
return the equations of the limit system. Otherwise, return the input equations.
See also [`is_hyperbolic_appproximation`](@ref).
See also [`is_hyperbolic_appproximation`](@ref) and [`prim2phys`](@ref).
!!! note "Implementation details"
This function is mostly used for some internal dispatch. For example,
Expand All @@ -373,6 +373,29 @@ See also [`is_hyperbolic_appproximation`](@ref).
"""
hyperbolic_approximation_limit(equations::AbstractEquations) = equations

"""
prim2phys(q, equations)
Convert the primitive variables `q` to the physically meaningful variables
for a given set of `equations`. By default, this is the same as
[`prim2prim`](@ref) for most equations. However, some equations like the
[`HyperbolicSerreGreenNaghdiEquations1D`](@ref) return a reduced set of
variables since they are a hyperbolic approximation of another set of
equations (in this case the [`SerreGreenNaghdiEquations1D`](@ref)).
See also [`is_hyperbolic_appproximation`](@ref) and
[`hyperbolic_approximation_limit`](@ref).
`q` is a vector type of the correct length `nvariables(equations)`.
Notice the function doesn't include any error checks for the purpose of
efficiency, so please make sure your input is correct.
"""
prim2phys(q, equations::AbstractEquations) = prim2prim(q, equations)

function varnames(::typeof(prim2phys), equations::AbstractEquations)
return varnames(prim2prim, equations)
end

abstract type AbstractBathymetry end

struct BathymetryFlat <: AbstractBathymetry end
Expand Down
14 changes: 14 additions & 0 deletions src/equations/hyperbolic_serre_green_naghdi_1d.jl
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,20 @@ function varnames(::typeof(prim2cons), ::HyperbolicSerreGreenNaghdiEquations1D)
return ("h", "hv", "b", "hw", "hH")
end

"""
prim2phys(q, equations::HyperbolicSerreGreenNaghdiEquations1D)
Return the physical variables ``\\eta, v, D`` used also by the
[`SerreGreenNaghdiEquations1D`](@ref) from the main variables
`q` for the [`HyperbolicSerreGreenNaghdiEquations1D`](@ref).
"""
function prim2phys(q, ::HyperbolicSerreGreenNaghdiEquations1D)
eta, v, D = q
return SVector(eta, v, D)
end

varnames(::typeof(prim2phys), ::HyperbolicSerreGreenNaghdiEquations1D) = ("η", "v", "D")

is_hyperbolic_appproximation(::HyperbolicSerreGreenNaghdiEquations1D) = Val{true}()

function hyperbolic_approximation_limit(equations::HyperbolicSerreGreenNaghdiEquations1D)
Expand Down
10 changes: 10 additions & 0 deletions test/test_unit.jl
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ using SparseArrays: sparse, SparseMatrixCSC
energy_total,
prim2cons,
prim2prim,
prim2phys,
]
for conversion in conversion_functions
@test DispersiveShallowWater.varnames(conversion, equations) isa Tuple
Expand All @@ -128,6 +129,7 @@ using SparseArrays: sparse, SparseMatrixCSC
@test @inferred(still_water_surface(q, equations)) == 0.0
@test isapprox(@inferred(energy_total(q, equations)), 8740.42)
@test @inferred(energy_total(q, equations)) == @inferred(entropy(q, equations))
@test @inferred(prim2phys(q, equations)) == @inferred(prim2prim(q, equations))

@testset "default implementation of energy_total_modified" begin
initial_condition = initial_condition_convergence_test
Expand Down Expand Up @@ -162,6 +164,7 @@ using SparseArrays: sparse, SparseMatrixCSC
energy_total,
prim2cons,
prim2prim,
prim2phys,
]
for conversion in conversion_functions
@test DispersiveShallowWater.varnames(conversion, equations) isa Tuple
Expand All @@ -177,6 +180,7 @@ using SparseArrays: sparse, SparseMatrixCSC
@test @inferred(still_water_surface(q, equations)) == 0.0
@test isapprox(@inferred(energy_total(q, equations)), 8740.42)
@test @inferred(energy_total(q, equations)) == @inferred(entropy(q, equations))
@test @inferred(prim2phys(q, equations)) == @inferred(prim2prim(q, equations))

@testset "default implementation of energy_total_modified" begin
initial_condition = initial_condition_convergence_test
Expand Down Expand Up @@ -214,6 +218,7 @@ using SparseArrays: sparse, SparseMatrixCSC
energy_total,
prim2cons,
prim2prim,
prim2phys,
energy_total_modified,
entropy_modified,
]
Expand All @@ -230,6 +235,7 @@ using SparseArrays: sparse, SparseMatrixCSC
@test @inferred(discharge(q, equations)) == 88.0
@test @inferred(still_water_surface(q, equations)) == 0.0
@test isapprox(@inferred(energy_total(q, equations)), 8740.42)
@test @inferred(prim2phys(q, equations)) == @inferred(prim2prim(q, equations))
end

@testset "SerreGreenNaghdiEquations1D" begin
Expand All @@ -246,6 +252,7 @@ using SparseArrays: sparse, SparseMatrixCSC
energy_total,
prim2cons,
prim2prim,
prim2phys,
]
for conversion in conversion_functions
@test DispersiveShallowWater.varnames(conversion, equations) isa Tuple
Expand All @@ -259,6 +266,7 @@ using SparseArrays: sparse, SparseMatrixCSC
@test @inferred(momentum(q, equations)) == 84.0
@test @inferred(discharge(q, equations)) == 84.0
@test @inferred(still_water_surface(q, equations)) == 0.0
@test @inferred(prim2phys(q, equations)) == @inferred(prim2prim(q, equations))
end

@testset "HyperbolicSerreGreenNaghdiEquations1D" begin
Expand All @@ -276,6 +284,7 @@ using SparseArrays: sparse, SparseMatrixCSC
energy_total,
prim2cons,
prim2prim,
prim2phys,
]
for conversion in conversion_functions
@test DispersiveShallowWater.varnames(conversion, equations) isa Tuple
Expand All @@ -289,6 +298,7 @@ using SparseArrays: sparse, SparseMatrixCSC
@test @inferred(momentum(q, equations)) == 84.0
@test @inferred(discharge(q, equations)) == 84.0
@test @inferred(still_water_surface(q, equations)) == 0.0
@test @inferred(prim2phys(q, equations)) == [42.0, 2.0, 0.0]
end

@testset "AnalysisCallback" begin
Expand Down

0 comments on commit 40dc766

Please sign in to comment.