Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions src/io/io.jl
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,7 @@ function add_system_data!(system_data, state_equation::StateEquationCole)
system_data["state_equation"]["reference_density"] = state_equation.reference_density
system_data["state_equation"]["background_pressure"] = state_equation.background_pressure
system_data["state_equation"]["exponent"] = state_equation.exponent
add_minimum_pressure_data!(system_data["state_equation"], state_equation)
end

function add_system_data!(system_data, state_equation::StateEquationAdaptiveCole)
Expand All @@ -251,6 +252,15 @@ function add_system_data!(system_data, state_equation::StateEquationAdaptiveCole
system_data["state_equation"]["mach_number_target"] = state_equation.mach_number_target
system_data["state_equation"]["min_sound_speed"] = state_equation.min_sound_speed
system_data["state_equation"]["max_sound_speed"] = state_equation.max_sound_speed
add_minimum_pressure_data!(system_data["state_equation"], state_equation)
end

function add_minimum_pressure_data!(state_equation_data, state_equation)
has_minimum_pressure(state_equation) || return state_equation_data

state_equation_data["minimum_pressure"] = state_equation.minimum_pressure
state_equation_data["minimum_pressure_transition_width"] = state_equation.minimum_pressure_transition_width
return state_equation_data
end

function add_system_data!(system_data, state_equation::StateEquationIdealGas)
Expand Down
207 changes: 171 additions & 36 deletions src/schemes/fluid/weakly_compressible_sph/state_equations.jl
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
@doc raw"""
StateEquationAdaptiveCole(; mach_number_target=0.1f0, min_sound_speed=10.0f0,
reference_density, max_sound_speed=100.0f0, exponent,
background_pressure=0.0f0, clip_negative_pressure=false)
background_pressure=0.0f0, clip_negative_pressure=false,
minimum_pressure=nothing,
minimum_pressure_transition_width=nothing)

This variant of [`StateEquationCole`](@ref) adapts the speed of sound during simulation to
balance accuracy and performance.
Expand Down Expand Up @@ -33,50 +35,77 @@ The speed of sound is initialized as `min_sound_speed`.
The `BoundaryModelDummyParticles` itself also provides an option to clip negative
pressure of boundary particles, which is a more targeted way to prevent sticking artifacts
without affecting the fluid behavior in the rest of the domain.
- `minimum_pressure=nothing`: Optional lower pressure bound, for example the gauge vapor
pressure when modeling cavitation. This cannot be combined with
`clip_negative_pressure=true`.
- `minimum_pressure_transition_width=nothing`: Half-width of the continuously
differentiable transition around `minimum_pressure`. Both pressure-floor keywords must
be specified together. The unmodified Cole pressure is used above
`minimum_pressure + minimum_pressure_transition_width`.
"""
struct StateEquationAdaptiveCole{ELTYPE, CLIP, SR} # Boolean to clip negative pressure
sound_speed_ref :: SR
mach_number_target :: ELTYPE
min_sound_speed :: ELTYPE
max_sound_speed :: ELTYPE
exponent :: ELTYPE
reference_density :: ELTYPE
background_pressure :: ELTYPE
struct StateEquationAdaptiveCole{ELTYPE, CLIP, SR, MIN_PRESSURE}
sound_speed_ref::SR
mach_number_target::ELTYPE
min_sound_speed::ELTYPE
max_sound_speed::ELTYPE
exponent::ELTYPE
reference_density::ELTYPE
background_pressure::ELTYPE
minimum_pressure::ELTYPE
minimum_pressure_transition_width::ELTYPE
end

function StateEquationAdaptiveCole(; mach_number_target=0.1f0, min_sound_speed=10.0f0,
reference_density, max_sound_speed=100.0f0, exponent,
background_pressure=0.0f0,
clip_negative_pressure=false)
clip_negative_pressure=false, minimum_pressure=nothing,
minimum_pressure_transition_width=nothing)
sound_speed = min_sound_speed
return StateEquationAdaptiveCole{typeof(mach_number_target), clip_negative_pressure,
typeof(Ref(sound_speed))}(Ref(sound_speed),
mach_number_target,
min_sound_speed,
max_sound_speed,
exponent,
reference_density,
background_pressure)
ELTYPE = typeof(mach_number_target)
minimum_pressure_, transition_width_,
has_minimum_pressure = minimum_pressure_parameters(ELTYPE, background_pressure,
clip_negative_pressure,
minimum_pressure,
minimum_pressure_transition_width)

return StateEquationAdaptiveCole{ELTYPE, clip_negative_pressure,
typeof(Ref(sound_speed)),
has_minimum_pressure}(Ref(sound_speed),
mach_number_target,
min_sound_speed,
max_sound_speed,
exponent,
reference_density,
background_pressure,
minimum_pressure_,
transition_width_)
end

function Adapt.adapt_structure(to,
se::StateEquationAdaptiveCole{ELTYPE, CLIP}) where {ELTYPE,
CLIP}
se::StateEquationAdaptiveCole{ELTYPE, CLIP, SR,
MIN_PRESSURE}) where {ELTYPE,
CLIP, SR,
MIN_PRESSURE}
sound_speed_ref = Adapt.adapt_structure(to, se.sound_speed_ref)
mach_number_target = Adapt.adapt_structure(to, se.mach_number_target)
min_sound_speed = Adapt.adapt_structure(to, se.min_sound_speed)
max_sound_speed = Adapt.adapt_structure(to, se.max_sound_speed)
exponent = Adapt.adapt_structure(to, se.exponent)
reference_density = Adapt.adapt_structure(to, se.reference_density)
background_pressure = Adapt.adapt_structure(to, se.background_pressure)
minimum_pressure = Adapt.adapt_structure(to, se.minimum_pressure)
transition_width = Adapt.adapt_structure(to, se.minimum_pressure_transition_width)

return StateEquationAdaptiveCole{ELTYPE, CLIP, typeof(sound_speed_ref)}(sound_speed_ref,
mach_number_target,
min_sound_speed,
max_sound_speed,
exponent,
reference_density,
background_pressure)
return StateEquationAdaptiveCole{ELTYPE, CLIP, typeof(sound_speed_ref),
MIN_PRESSURE}(sound_speed_ref,
mach_number_target,
min_sound_speed,
max_sound_speed,
exponent,
reference_density,
background_pressure,
minimum_pressure,
transition_width)
end

@inline function sound_speed(state_equation::StateEquationAdaptiveCole)
Expand All @@ -85,7 +114,9 @@ end

@doc raw"""
StateEquationCole(; sound_speed, reference_density, exponent,
background_pressure=0.0, clip_negative_pressure=false)
background_pressure=0.0, clip_negative_pressure=false,
minimum_pressure=nothing,
minimum_pressure_transition_width=nothing)

Equation of state to describe the relationship between pressure and density
of water up to high pressures.
Expand All @@ -96,29 +127,132 @@ of water up to high pressures.
- `exponent`: A value of `7` is usually used for most simulations.
- `background_pressure=0.0`: Background pressure.
- `clip_negative_pressure=false`: Negative pressure values are clipped to 0, which prevents spurious surface tension with `SummationDensity` but allows unphysical rarefaction of the fluid.
- `minimum_pressure=nothing`: Optional lower pressure bound, for example the gauge vapor
pressure when modeling cavitation. This cannot be combined with
`clip_negative_pressure=true`.
- `minimum_pressure_transition_width=nothing`: Half-width of the continuously
differentiable transition around `minimum_pressure`. Both pressure-floor keywords must
be specified together. The unmodified Cole pressure is used above
`minimum_pressure + minimum_pressure_transition_width`.
"""
struct StateEquationCole{ELTYPE, CLIP} # Boolean to clip negative pressure
sound_speed :: ELTYPE
exponent :: ELTYPE
reference_density :: ELTYPE
background_pressure :: ELTYPE
struct StateEquationCole{ELTYPE, CLIP, MIN_PRESSURE}
sound_speed::ELTYPE
exponent::ELTYPE
reference_density::ELTYPE
background_pressure::ELTYPE
minimum_pressure::ELTYPE
minimum_pressure_transition_width::ELTYPE

function StateEquationCole(; sound_speed, reference_density, exponent,
background_pressure=0.0, clip_negative_pressure=false)
background_pressure=0.0, clip_negative_pressure=false,
minimum_pressure=nothing,
minimum_pressure_transition_width=nothing)
ELTYPE = typeof(sound_speed)
minimum_pressure_, transition_width_,
has_minimum_pressure = minimum_pressure_parameters(ELTYPE, background_pressure,
clip_negative_pressure,
minimum_pressure,
minimum_pressure_transition_width)

new{typeof(sound_speed),
clip_negative_pressure}(sound_speed, exponent, reference_density,
background_pressure)
clip_negative_pressure, has_minimum_pressure}(sound_speed, exponent,
reference_density,
background_pressure,
minimum_pressure_,
transition_width_)
end
end

clip_negative_pressure(::StateEquationCole{<:Any, CLIP}) where {CLIP} = CLIP
clip_negative_pressure(::StateEquationAdaptiveCole{<:Any, CLIP}) where {CLIP} = CLIP

@inline function has_minimum_pressure(::StateEquationCole{<:Any, CLIP,
MIN_PRESSURE}) where {CLIP,
MIN_PRESSURE}
return MIN_PRESSURE
end

@inline function has_minimum_pressure(::StateEquationAdaptiveCole{<:Any, CLIP, SR,
MIN_PRESSURE}) where {CLIP,
SR,
MIN_PRESSURE}
return MIN_PRESSURE
end

function minimum_pressure_parameters(::Type{ELTYPE}, background_pressure,
clip_negative_pressure, minimum_pressure,
transition_width) where {ELTYPE}
if isnothing(minimum_pressure) != isnothing(transition_width)
throw(ArgumentError("`minimum_pressure` and " *
"`minimum_pressure_transition_width` must be specified together"))
end

if isnothing(minimum_pressure)
return zero(ELTYPE), zero(ELTYPE), false
end

clip_negative_pressure &&
throw(ArgumentError("`minimum_pressure` cannot be combined with " *
"`clip_negative_pressure=true`"))

background_pressure_ = convert(ELTYPE, background_pressure)
minimum_pressure_ = convert(ELTYPE, minimum_pressure)
transition_width_ = convert(ELTYPE, transition_width)

isfinite(minimum_pressure_) ||
throw(ArgumentError("`minimum_pressure` must be finite"))
isfinite(transition_width_) ||
throw(ArgumentError("`minimum_pressure_transition_width` must be finite"))
transition_width_ > zero(ELTYPE) ||
throw(ArgumentError("`minimum_pressure_transition_width` must be positive"))
minimum_pressure_ < background_pressure_ ||
throw(ArgumentError("`minimum_pressure` must be below `background_pressure`"))
transition_width_ <= background_pressure_ - minimum_pressure_ ||
throw(ArgumentError("the pressure-floor transition must end at or below " *
"`background_pressure`"))

return minimum_pressure_, transition_width_, true
end

@inline function apply_minimum_pressure(state_equation, pressure)
# This branch is compile-time constant through the state-equation type parameter.
has_minimum_pressure(state_equation) || return pressure

minimum_pressure = state_equation.minimum_pressure
transition_width = state_equation.minimum_pressure_transition_width
lower_transition_pressure = minimum_pressure - transition_width
upper_transition_pressure = minimum_pressure + transition_width

pressure <= lower_transition_pressure && return minimum_pressure
pressure >= upper_transition_pressure && return pressure

normalized_pressure = (pressure - lower_transition_pressure) / (2 * transition_width)
return minimum_pressure + transition_width * normalized_pressure^2
end

@inline function remove_minimum_pressure(state_equation, pressure)
has_minimum_pressure(state_equation) || return pressure

minimum_pressure = state_equation.minimum_pressure
transition_width = state_equation.minimum_pressure_transition_width
lower_transition_pressure = minimum_pressure - transition_width
upper_transition_pressure = minimum_pressure + transition_width

# The constant part of the floor is not uniquely invertible. Use its highest raw
# pressure, which gives the least rarefied density compatible with the floor.
pressure <= minimum_pressure && return lower_transition_pressure
pressure >= upper_transition_pressure && return pressure

normalized_pressure = sqrt((pressure - minimum_pressure) / transition_width)
return lower_transition_pressure + 2 * transition_width * normalized_pressure
end

function (state_equation::Union{StateEquationCole, StateEquationAdaptiveCole})(density)
(; exponent, reference_density, background_pressure) = state_equation

B = reference_density * sound_speed(state_equation)^2 / exponent
pressure = B * ((density / reference_density)^exponent - 1) + background_pressure
pressure = apply_minimum_pressure(state_equation, pressure)

# This is determined statically and has therefore no overhead
if clip_negative_pressure(state_equation)
Expand All @@ -133,6 +267,7 @@ function inverse_state_equation(state_equation::Union{StateEquationCole,
(; exponent, reference_density, background_pressure) = state_equation

B = reference_density * sound_speed(state_equation)^2 / exponent
pressure = remove_minimum_pressure(state_equation, pressure)
tmp = (pressure - background_pressure) / B + 1

return reference_density * tmp^(1 / exponent)
Expand Down
Loading
Loading