Skip to content

Commit 7b422e9

Browse files
added error warning to KdV (#232)
* added error warning to KdV * define check_solver * clean up * format * Update src/semidiscretization.jl Co-authored-by: Collin Wittenstein <126870995+cwittens@users.noreply.github.com> --------- Co-authored-by: Joshua Lampert <joshua.lampert@uni-hamburg.de> Co-authored-by: Joshua Lampert <51029046+JoshuaLampert@users.noreply.github.com>
1 parent 1e607d9 commit 7b422e9

File tree

6 files changed

+75
-13
lines changed

6 files changed

+75
-13
lines changed

src/equations/bbm_1d.jl

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,12 @@ function BBMEquation1D(; gravity, D = 1.0, eta0 = 0.0, split_form = true)
5353
BBMEquation1D(gravity, D, eta0, split_form)
5454
end
5555

56+
function check_solver(::BBMEquation1D, solver, boundary_conditions)
57+
if isnothing(solver.D2)
58+
throw(ArgumentError("The BBM equation requires a second-derivative operator. Explicitly set `D2`."))
59+
end
60+
end
61+
5662
"""
5763
initial_condition_convergence_test(x, t, equations::BBMEquation1D, mesh)
5864

src/equations/bbm_bbm_1d.jl

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,12 @@ function BBMBBMEquations1D(; bathymetry_type = bathymetry_variable,
6464
BBMBBMEquations1D(bathymetry_type, gravity, eta0)
6565
end
6666

67+
function check_solver(::BBMBBMEquations1D, solver, boundary_conditions)
68+
if isnothing(solver.D2)
69+
throw(ArgumentError("The BBM-BBM equations require a second-derivative operator. Explicitly set `D2`."))
70+
end
71+
end
72+
6773
"""
6874
initial_condition_convergence_test(x, t, equations::BBMBBMEquations1D, mesh)
6975

src/equations/kdv_1d.jl

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,13 @@ function KdVEquation1D(; gravity, D = 1.0, eta0 = 0.0)
5353
KdVEquation1D(gravity, D, eta0)
5454
end
5555

56+
function check_solver(::KdVEquation1D, solver, boundary_conditions)
57+
if !(solver.D1 isa PeriodicUpwindOperators && isnothing(solver.D3)) &&
58+
isnothing(solver.D3)
59+
throw(ArgumentError("The KdV equation requires a third-derivative operator. Either explicitly set `D3` or set `D1` as an upwind operator."))
60+
end
61+
end
62+
5663
"""
5764
initial_condition_soliton(x, t, equations::KdVEquation1D, mesh)
5865
@@ -133,14 +140,14 @@ where `D` is the still-water depth.
133140
134141
!!! warning "Parameter constraints"
135142
This conversion is only valid for equations with specific parameter values:
136-
- `gravity = 4/27`
137-
- `D = 3.0`
138-
139-
These values ensure the dimensional KdV equation matches the standard
143+
- `gravity = 4/27`
144+
- `D = 3.0`
145+
146+
These values ensure the dimensional KdV equation matches the standard
140147
non-dimensional form `u_t + u u_x + u_{xxx} = 0`.
141148
142-
This function allows converting solutions from the standard non-dimensional
143-
KdV form commonly found in literature to the dimensional form implemented
149+
This function allows converting solutions from the standard non-dimensional
150+
KdV form commonly found in literature to the dimensional form implemented
144151
in DispersiveShallowWater.jl.
145152
146153
See also [`prim2nondim`](@ref).
@@ -153,7 +160,7 @@ end
153160
"""
154161
prim2nondim(eta, equations::KdVEquation1D)
155162
156-
Convert the primitive/physical variable `eta` (total water height) to the
163+
Convert the primitive/physical variable `eta` (total water height) to the
157164
non-dimensional variable `u` for the [`KdVEquation1D`](@ref).
158165
159166
The transformation is given by:
@@ -164,15 +171,15 @@ where `D` is the still-water depth.
164171
165172
!!! warning "Parameter constraints"
166173
This conversion is only valid for equations with specific parameter values:
167-
- `gravity = 4/27`
174+
- `gravity = 4/27`
168175
- `D = 3.0`
169-
170-
These values ensure the dimensional KdV equation matches the standard
176+
177+
These values ensure the dimensional KdV equation matches the standard
171178
non-dimensional form `u_t + u u_x + u_{xxx} = 0`.
172179
173-
This function allows converting solutions from the dimensional form implemented
174-
in DispersiveShallowWater.jl to the standard non-dimensional KdV form
175-
commonly found in literature, enabling comparison with theoretical results
180+
This function allows converting solutions from the dimensional form implemented
181+
in DispersiveShallowWater.jl to the standard non-dimensional KdV form
182+
commonly found in literature, enabling comparison with theoretical results
176183
and other implementations.
177184
178185
See also [`nondim2prim`](@ref).

src/equations/svaerd_kalisch_1d.jl

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,12 @@ function SvaerdKalischEquations1D(; bathymetry_type = bathymetry_variable, gravi
6868
SvaerdKalischEquations1D(bathymetry_type, gravity, eta0, alpha, beta, gamma)
6969
end
7070

71+
function check_solver(::SvaerdKalischEquations1D, solver, ::BoundaryConditionPeriodic)
72+
if isnothing(solver.D2)
73+
throw(ArgumentError("The Svärd-Kalisch equations with periodic boundary conditions require a second-derivative operator. Explicitly set `D2`."))
74+
end
75+
end
76+
7177
"""
7278
initial_condition_manufactured(x, t, equations::SvaerdKalischEquations1D, mesh)
7379

src/semidiscretization.jl

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ function Semidiscretization(mesh, equations, initial_condition, solver;
7676
throw(ArgumentError("Non-periodic derivative operators in `solver` are incompatible with periodic boundary conditions."))
7777
end
7878

79+
check_solver(equations, solver, boundary_conditions)
7980
cache = (;
8081
create_cache(mesh, equations, solver, initial_condition, boundary_conditions,
8182
RealT, uEltype)...,
@@ -88,6 +89,19 @@ function Semidiscretization(mesh, equations, initial_condition, solver;
8889
solver, cache)
8990
end
9091

92+
"""
93+
check_solver(equations, solver, boundary_conditions)
94+
95+
Check that the `solver` is compatible with the given `equations` and
96+
`boundary_conditions`. The default implementation performs no checks.
97+
Specific equation types can override this method to validate that
98+
required derivative operators are present (e.g., some equations
99+
require `D2` or `D3` to be non-`nothing`).
100+
101+
Throws an `ArgumentError` if the solver is incompatible.
102+
"""
103+
check_solver(equations, solver, boundary_conditions) = nothing
104+
91105
function Base.show(io::IO, semi::Semidiscretization)
92106
@nospecialize semi # reduce precompilation time
93107

test/test_unit.jl

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,29 @@ end
9292
@test_throws ArgumentError semidiscretize(semi_flat, (0.0, 1.0))
9393
end
9494

95+
@testitem "Solver consistency" setup=[Setup, AdditionalImports] begin
96+
mesh = Mesh1D(-1.0, 1.0, 10)
97+
initial_condition = initial_condition_convergence_test
98+
D1 = periodic_derivative_operator(1, 4, mesh.xmin, mesh.xmax, mesh.N)
99+
solver = Solver(D1)
100+
101+
equations = KdVEquation1D(gravity = 1.0)
102+
@test_throws ArgumentError Semidiscretization(mesh, equations, initial_condition,
103+
solver)
104+
105+
equations = BBMEquation1D(gravity = 1.0)
106+
@test_throws ArgumentError Semidiscretization(mesh, equations, initial_condition,
107+
solver)
108+
109+
equations = BBMBBMEquations1D(gravity = 1.0)
110+
@test_throws ArgumentError Semidiscretization(mesh, equations, initial_condition,
111+
solver)
112+
113+
equations = SvaerdKalischEquations1D(gravity = 1.0)
114+
@test_throws ArgumentError Semidiscretization(mesh, equations, initial_condition,
115+
solver)
116+
end
117+
95118
@testitem "Boundary conditions" setup=[Setup] begin
96119
boundary_conditions = boundary_condition_periodic
97120
@test_nowarn print(boundary_conditions)

0 commit comments

Comments
 (0)