-
Notifications
You must be signed in to change notification settings - Fork 12
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Implement simple CFL condition #359
Merged
Merged
Changes from 13 commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
0d402a0
[skip ci] Implement simple CFL condition
efaulhaber 83da641
[skip ci] Reformat
efaulhaber 2e54335
Add docs
efaulhaber 3d84709
Undo change in `rectangular_tank_2d.jl`
efaulhaber 605c232
Merge branch 'main' into cfl-condition
efaulhaber 7c2e05d
Use Carpenter-Kennedy for dam break example
efaulhaber d593237
Merge branch 'main' into cfl-condition
efaulhaber 84a1ba3
Fix corrections example tests
efaulhaber ca3db76
Fix show and add tests
efaulhaber baeb382
Fix symbol
efaulhaber c819b4a
Merge branch 'main' into cfl-condition
efaulhaber 65c5c56
Extract kinematic viscosity
efaulhaber ab4f42f
Merge branch 'main' into cfl-condition
efaulhaber 8c87067
Fix tests
efaulhaber 29fdfa3
Merge branch 'main' into cfl-condition
efaulhaber 14b0d44
Merge branch 'main' into cfl-condition
efaulhaber File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,3 +8,4 @@ end | |
include("info.jl") | ||
include("solution_saving.jl") | ||
include("density_reinit.jl") | ||
include("stepsize.jl") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
struct StepsizeCallback{ISCONSTANT, ELTYPE} | ||
cfl_number::ELTYPE | ||
end | ||
|
||
@inline is_constant(::StepsizeCallback{ISCONSTANT}) where {ISCONSTANT} = ISCONSTANT | ||
|
||
@doc raw""" | ||
StepsizeCallback(; cfl::Real) | ||
|
||
Set the time step size according to a CFL condition if the time integration method isn't | ||
adaptive itself. | ||
|
||
The current implementation is using the simplest form of CFL condition, which chooses a | ||
time step size that is constant during the simulation. | ||
The step size is therefore only applied once at the beginning of the simulation. | ||
|
||
The step size ``\Delta t`` is chosen as the minimum | ||
```math | ||
\Delta t = \min(\Delta t_\eta, \Delta t_a, \Delta t_c), | ||
``` | ||
where | ||
```math | ||
\Delta t_\eta = 0.125 \, h^2 / \eta, \quad \Delta t_a = 0.25 \sqrt{h / \lVert g \rVert}, | ||
\quad \Delta t_c = \text{CFL} \, h / c, | ||
``` | ||
with ``\nu = \alpha h c / (2n + 4)``, where ``\alpha`` is the parameter of the viscosity | ||
and ``n`` is the number of dimensions. | ||
|
||
!!! warning "Experimental implementation" | ||
This is an experimental feature and may change in future releases. | ||
|
||
## References | ||
- M. Antuono, A. Colagrossi, S. Marrone. | ||
"Numerical Diffusive Terms in Weakly-Compressible SPH Schemes." | ||
In: Computer Physics Communications 183, no. 12 (2012), pages 2570--80. | ||
[doi: 10.1016/j.cpc.2012.07.006](https://doi.org/10.1016/j.cpc.2012.07.006) | ||
- S. Adami, X. Y. Hu, N. A. Adams. | ||
"A generalized wall boundary condition for smoothed particle hydrodynamics". | ||
In: Journal of Computational Physics 231, 21 (2012), pages 7057--7075. | ||
[doi: 10.1016/J.JCP.2012.05.005](https://doi.org/10.1016/J.JCP.2012.05.005) | ||
- P. N. Sun, A. Colagrossi, S. Marrone, A. M. Zhang. | ||
"The δplus-SPH Model: Simple Procedures for a Further Improvement of the SPH Scheme." | ||
In: Computer Methods in Applied Mechanics and Engineering 315 (2017), pages 25--49. | ||
[doi: 10.1016/j.cma.2016.10.028](https://doi.org/10.1016/j.cma.2016.10.028) | ||
- M. Antuono, S. Marrone, A. Colagrossi, B. Bouscasse. | ||
"Energy Balance in the δ-SPH Scheme." | ||
In: Computer Methods in Applied Mechanics and Engineering 289 (2015), pages 209--26. | ||
[doi: 10.1016/j.cma.2015.02.004](https://doi.org/10.1016/j.cma.2015.02.004) | ||
""" | ||
function StepsizeCallback(; cfl::Real) | ||
# TODO adapt for non-constant CFL conditions | ||
is_constant = true | ||
stepsize_callback = StepsizeCallback{is_constant, typeof(cfl)}(cfl) | ||
|
||
# The first one is the `condition`, the second the `affect!` | ||
return DiscreteCallback(stepsize_callback, stepsize_callback, | ||
save_positions=(false, false), | ||
initialize=initialize_stepsize_callback) | ||
end | ||
|
||
function initialize_stepsize_callback(discrete_callback, u, t, integrator) | ||
stepsize_callback = discrete_callback.affect! | ||
|
||
stepsize_callback(integrator) | ||
end | ||
|
||
# `condition` | ||
function (stepsize_callback::StepsizeCallback)(u, t, integrator) | ||
# Only apply the callback when the stepsize is not constant and the time integrator | ||
# is not adaptive. | ||
return !is_constant(stepsize_callback) && !integrator.opts.adaptive | ||
end | ||
|
||
# `affect!` | ||
function (stepsize_callback::StepsizeCallback)(integrator) | ||
(; cfl_number) = stepsize_callback | ||
|
||
v_ode, u_ode = integrator.u.x | ||
semi = integrator.p | ||
|
||
dt = @trixi_timeit timer() "calculate dt" calculate_dt(v_ode, u_ode, cfl_number, semi) | ||
|
||
set_proposed_dt!(integrator, dt) | ||
integrator.opts.dtmax = dt | ||
integrator.dtcache = dt | ||
|
||
# Tell OrdinaryDiffEq that `u` has not been modified | ||
u_modified!(integrator, false) | ||
|
||
return stepsize_callback | ||
end | ||
|
||
function Base.show(io::IO, cb::DiscreteCallback{<:Any, <:StepsizeCallback}) | ||
@nospecialize cb # reduce precompilation time | ||
|
||
stepsize_callback = cb.affect! | ||
print(io, "StepsizeCallback(is_constant=", is_constant(stepsize_callback), | ||
", cfl_number=", stepsize_callback.cfl_number, ")") | ||
end | ||
|
||
function Base.show(io::IO, ::MIME"text/plain", | ||
cb::DiscreteCallback{<:Any, <:StepsizeCallback}) | ||
@nospecialize cb # reduce precompilation time | ||
|
||
if get(io, :compact, false) | ||
show(io, cb) | ||
else | ||
stepsize_callback = cb.affect! | ||
|
||
setup = [ | ||
"is constant" => string(is_constant(stepsize_callback)), | ||
"CFL number" => stepsize_callback.cfl_number, | ||
] | ||
summary_box(io, "StepsizeCallback", setup) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
@testset verbose=true "Callbacks" begin | ||
include("info.jl") | ||
include("stepsize.jl") | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
@testset verbose=true "StepsizeCallback" begin | ||
@testset verbose=true "show" begin | ||
callback = StepsizeCallback(cfl=1.2) | ||
|
||
show_compact = "StepsizeCallback(is_constant=true, cfl_number=1.2)" | ||
@test repr(callback) == show_compact | ||
|
||
show_box = """ | ||
┌──────────────────────────────────────────────────────────────────────────────────────────────────┐ | ||
│ StepsizeCallback │ | ||
│ ════════════════ │ | ||
│ is constant: ……………………………………………… true │ | ||
│ CFL number: ………………………………………………… 1.2 │ | ||
└──────────────────────────────────────────────────────────────────────────────────────────────────┘""" | ||
@test repr("text/plain", callback) == show_box | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This runs in 17s vs 27s with
RDPK3SpFSAL35
.