Skip to content
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

Add ideal gas equation #607

Merged
merged 40 commits into from
Nov 20, 2024
Merged
Show file tree
Hide file tree
Changes from 26 commits
Commits
Show all changes
40 commits
Select commit Hold shift + click to select a range
ed83ea1
set test up for 1.11
svchb Aug 8, 2024
493e26b
Merge branch 'main' into prepare_for_1.11
svchb Aug 12, 2024
bad7de8
add basic struct
svchb Aug 20, 2024
aa90aae
add export
svchb Aug 20, 2024
7841461
make equation available
svchb Aug 20, 2024
1e07757
format
svchb Aug 20, 2024
98065e6
typo
svchb Aug 20, 2024
6eb6fec
fix docs
svchb Aug 20, 2024
83ebc5f
add exmaple
svchb Aug 21, 2024
aa9348c
format
svchb Aug 21, 2024
f65eb5a
fix
svchb Aug 22, 2024
31b2f75
Merge branch 'main' into prepare_for_1.11
svchb Oct 9, 2024
1d05304
Merge branch 'main' into prepare_for_1.11
svchb Oct 9, 2024
4ed0b91
Increase errors for 1.11
svchb Oct 9, 2024
1ce0460
Fix invalidations
svchb Oct 9, 2024
1774f5a
Fix tests
svchb Oct 9, 2024
4daf984
Update ci.yml
svchb Oct 9, 2024
ac2eb2c
revert
svchb Oct 9, 2024
258f2ba
Update ci.yml
svchb Oct 9, 2024
1a9f3f5
Update test/validation/validation.jl
svchb Oct 10, 2024
796b1d1
Merge branch 'main' into add_ideal_gas
svchb Oct 10, 2024
c402975
Merge remote-tracking branch 'origin/prepare_for_1.11' into add_ideal…
svchb Oct 10, 2024
83514da
Merge remote-tracking branch 'origin/add_ideal_gas' into add_ideal_gas
svchb Oct 10, 2024
f267d43
Merge branch 'main' into add_ideal_gas
svchb Oct 11, 2024
f9e2202
Merge branch 'main' into add_ideal_gas
svchb Nov 13, 2024
77493f5
format
svchb Nov 13, 2024
d4016a6
review fixes
svchb Nov 16, 2024
7afffb4
Merge branch 'add_ideal_gas' of https://github.com/svchb/TrixiParticl…
svchb Nov 16, 2024
3dfa4e4
Merge branch 'main' into add_ideal_gas
svchb Nov 16, 2024
1467107
add test
svchb Nov 16, 2024
fc4f61b
fix example
svchb Nov 16, 2024
33a704a
fix test
svchb Nov 16, 2024
eeca819
review comments
svchb Nov 18, 2024
c18b774
Merge branch 'main' into add_ideal_gas
svchb Nov 18, 2024
f5dac70
Merge branch 'main' into add_ideal_gas
svchb Nov 18, 2024
96e1cf6
fix docs
svchb Nov 19, 2024
747bf1c
Update src/schemes/fluid/weakly_compressible_sph/state_equations.jl
svchb Nov 19, 2024
4f66fa7
forgot to edit the other doc
svchb Nov 19, 2024
8faa548
Merge branch 'main' into add_ideal_gas
svchb Nov 19, 2024
1cab8eb
Merge branch 'main' into add_ideal_gas
svchb Nov 20, 2024
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
3 changes: 2 additions & 1 deletion examples/fluid/dam_break_2d.jl
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@ tank_size = (floor(5.366 * H / boundary_particle_spacing) * boundary_particle_sp
fluid_density = 1000.0
sound_speed = 20 * sqrt(gravity * H)
state_equation = StateEquationCole(; sound_speed, reference_density=fluid_density,
exponent=1, clip_negative_pressure=false)
exponent=1, clip_negative_pressure=false,
background_pressure=0.0)
svchb marked this conversation as resolved.
Show resolved Hide resolved

tank = RectangularTank(fluid_particle_spacing, initial_fluid_size, tank_size, fluid_density,
n_layers=boundary_layers, spacing_ratio=spacing_ratio,
Expand Down
95 changes: 95 additions & 0 deletions examples/fluid/dam_break_2phase_2d.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
# 2D dam break simulation with an air layer on top

using TrixiParticles
using OrdinaryDiffEq

# Size parameters
H = 0.6
W = 2 * H

gravity = 9.81
tspan = (0.0, 2.0)

# Resolution
svchb marked this conversation as resolved.
Show resolved Hide resolved
# Note: The resolution is very coarse. A better result is obtained with H/60 (which takes over 1 hour)
fluid_particle_spacing = H / 20

# Numerical settings
smoothing_length = 3.5 * fluid_particle_spacing
#sound_speed = 100
# when using the Ideal gas equation
sound_speed = 400

# physical values
nu_water = 8.9E-7
nu_air = 1.544E-5
nu_ratio = nu_water / nu_air

nu_sim_air = 0.02 * smoothing_length * sound_speed
nu_sim_water = nu_ratio * nu_sim_air

air_viscosity = ViscosityMorris(nu=nu_sim_air)
water_viscosity = ViscosityMorris(nu=nu_sim_water)

trixi_include(@__MODULE__, joinpath(examples_dir(), "fluid", "dam_break_2d.jl"),
sol=nothing, fluid_particle_spacing=fluid_particle_spacing,
viscosity=water_viscosity, smoothing_length=smoothing_length,
gravity=gravity, tspan=tspan, density_diffusion=nothing,
sound_speed=sound_speed, cfl=0.8, exponent=7, background_pressure=0.0,
tank_size=(floor(5.366 * H / fluid_particle_spacing) * fluid_particle_spacing,
2.6 * H))

# ==========================================================================================
# ==== Setup air_system layer

air_size = (tank_size[1], 1.5 * H)
air_size2 = (tank_size[1] - W, H)
air_density = 1.0

# Air above the initial water
air_system = RectangularShape(fluid_particle_spacing,
round.(Int, air_size ./ fluid_particle_spacing),
zeros(length(air_size)), density=air_density)

# Air for the rest of the empty volume
air_system2 = RectangularShape(fluid_particle_spacing,
round.(Int, air_size2 ./ fluid_particle_spacing),
(W, 0.0), density=air_density)

# move on top of the water
for i in axes(air_system.coordinates, 2)
air_system.coordinates[:, i] .+= [0.0, H]
end

air_system = union(air_system, air_system2)

# We use background_pressure here to prevent negative pressure regions in the gas phase.
air_system_system = WeaklyCompressibleSPHSystem(air_system, fluid_density_calculator,
StateEquationCole(; sound_speed,
reference_density=air_density,
exponent=1,
clip_negative_pressure=false,
background_pressure=1000),
smoothing_kernel, smoothing_length,
viscosity=air_viscosity,
acceleration=(0.0, -gravity))

# air_system_system = WeaklyCompressibleSPHSystem(air_system, fluid_density_calculator,
# StateEquationIdealGas(; gamma=1.4,
# gas_constant=287.0,
# temperature=293.15),
# smoothing_kernel, smoothing_length,
# viscosity=air_viscosity,
# acceleration=(0.0, -gravity))

# ==========================================================================================
# ==== Simulation
semi = Semidiscretization(fluid_system, air_system_system, boundary_system,
neighborhood_search=GridNeighborhoodSearch{2}(update_strategy=nothing))
ode = semidiscretize(semi, tspan, data_type=nothing)

sol = solve(ode, RDPK3SpFSAL35(),
abstol=1e-5, # Default abstol is 1e-6 (may need to be tuned to prevent boundary penetration)
reltol=1e-4, # Default reltol is 1e-3 (may need to be tuned to prevent boundary penetration)
dtmax=1e-2, # Limit stepsize to prevent crashing
save_everystep=false, callback=callbacks);
21 changes: 14 additions & 7 deletions examples/fluid/dam_break_oil_film_2d.jl
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,20 @@ fluid_particle_spacing = H / 60
smoothing_length = 3.5 * fluid_particle_spacing
sound_speed = 20 * sqrt(gravity * H)

nu = 0.02 * smoothing_length * sound_speed / 8
oil_viscosity = ViscosityMorris(nu=10 * nu)
# Physical values
nu_water = 8.9E-7
nu_oil = 6E-5
nu_ratio = nu_water / nu_oil

nu_sim_oil = max(0.01 * smoothing_length * sound_speed, nu_oil)
nu_sim_water = nu_ratio * nu_sim_oil

oil_viscosity = ViscosityMorris(nu=nu_sim_oil)

trixi_include(@__MODULE__, joinpath(examples_dir(), "fluid", "dam_break_2d.jl"),
sol=nothing, fluid_particle_spacing=fluid_particle_spacing,
viscosity=ViscosityMorris(nu=nu), smoothing_length=smoothing_length,
gravity=gravity,
density_diffusion=DensityDiffusionMolteniColagrossi(delta=0.1),
sound_speed=sound_speed)
viscosity=ViscosityMorris(nu=nu_sim_water), smoothing_length=smoothing_length,
gravity=gravity, density_diffusion=nothing, sound_speed=sound_speed)

# ==========================================================================================
# ==== Setup oil layer
Expand All @@ -42,8 +47,10 @@ for i in axes(oil.coordinates, 2)
oil.coordinates[:, i] .+= [0.0, H]
end

oil_state_equation = StateEquationCole(; sound_speed, reference_density=oil_density,
exponent=1, clip_negative_pressure=false)
oil_system = WeaklyCompressibleSPHSystem(oil, fluid_density_calculator,
state_equation, smoothing_kernel,
oil_state_equation, smoothing_kernel,
smoothing_length, viscosity=oil_viscosity,
density_diffusion=density_diffusion,
acceleration=(0.0, -gravity),
Expand Down
2 changes: 1 addition & 1 deletion src/TrixiParticles.jl
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ export PenaltyForceGanzenmueller, TransportVelocityAdami
export SchoenbergCubicSplineKernel, SchoenbergQuarticSplineKernel,
SchoenbergQuinticSplineKernel, GaussianKernel, WendlandC2Kernel, WendlandC4Kernel,
WendlandC6Kernel, SpikyKernel, Poly6Kernel
export StateEquationCole
export StateEquationCole, StateEquationIdealGas
export ArtificialViscosityMonaghan, ViscosityAdami, ViscosityMorris
export DensityDiffusion, DensityDiffusionMolteniColagrossi, DensityDiffusionFerrari,
DensityDiffusionAntuono
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -208,15 +208,15 @@ end

(; delta) = density_diffusion
(; smoothing_length, state_equation) = particle_system
(; sound_speed) = state_equation
sound_speed_ = sound_speed(state_equation)

volume_b = m_b / rho_b

psi = density_diffusion_psi(density_diffusion, rho_a, rho_b, pos_diff, distance,
particle_system, particle, neighbor)
density_diffusion_term = dot(psi, grad_kernel) * volume_b

dv[end, particle] += delta * smoothing_length * sound_speed * density_diffusion_term
dv[end, particle] += delta * smoothing_length * sound_speed_ * density_diffusion_term
end

# Density diffusion `nothing` or interaction other than fluid-fluid
Expand Down
5 changes: 3 additions & 2 deletions src/schemes/fluid/weakly_compressible_sph/rhs.jl
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ function interact!(dv, v_particle_system, u_particle_system,
particle_system::WeaklyCompressibleSPHSystem,
neighbor_system)
(; density_calculator, state_equation, correction, surface_tension) = particle_system
(; sound_speed) = state_equation

sound_speed_ = sound_speed(state_equation)

surface_tension_a = surface_tension_model(particle_system)
surface_tension_b = surface_tension_model(neighbor_system)
Expand Down Expand Up @@ -56,7 +57,7 @@ function interact!(dv, v_particle_system, u_particle_system,
dv_viscosity(particle_system, neighbor_system,
v_particle_system, v_neighbor_system,
particle, neighbor, pos_diff, distance,
sound_speed, m_a, m_b, rho_a, rho_b, grad_kernel)
sound_speed_, m_a, m_b, rho_a, rho_b, grad_kernel)

dv_surface_tension = surface_tension_correction *
surface_tension_force(surface_tension_a, surface_tension_b,
Expand Down
62 changes: 62 additions & 0 deletions src/schemes/fluid/weakly_compressible_sph/state_equations.jl
Original file line number Diff line number Diff line change
Expand Up @@ -49,3 +49,65 @@ function inverse_state_equation(state_equation::StateEquationCole, pressure)

return reference_density * tmp^(1 / exponent)
end

@inline sound_speed(eos) = eos.sound_speed

@doc raw"""
StateEquationIdealGas(; gas_constant, temperature, gamma)
Equation of state to describe the relationship between pressure and density
svchb marked this conversation as resolved.
Show resolved Hide resolved
of a gas using the Ideal Gas Law.
# Keywords
- `gas_constant`: Specific gas constant (R) for the gas, typically in J/(kg*K).
svchb marked this conversation as resolved.
Show resolved Hide resolved
- `temperature` : Absolute temperature of the gas in Kelvin.
- `gamma` : Heat-capacity ratio
This struct calculates the pressure of a gas from its density using the formula:
svchb marked this conversation as resolved.
Show resolved Hide resolved
\[ P = \rho \cdot R \cdot T \]
where \( P \) is pressure, \( \rho \) is density, \( R \) is the gas constant, and \( T \) is temperature.
Note:
For basic WCSPH this boils down to the assumption of a linear pressure-density relationship.
svchb marked this conversation as resolved.
Show resolved Hide resolved
"""
struct StateEquationIdealGas{ELTYPE}
gas_constant :: ELTYPE
temperature :: ELTYPE
gamma :: ELTYPE

function StateEquationIdealGas(; gas_constant, temperature, gamma)
new{typeof(gas_constant)}(gas_constant, temperature, gamma)
end
end

function (state_equation::StateEquationIdealGas)(density)
(; gas_constant, temperature) = state_equation
pressure = density * gas_constant * temperature
return pressure
end
svchb marked this conversation as resolved.
Show resolved Hide resolved

# This version is for simulations that include a temperature.
function (state_equation::StateEquationIdealGas)(density, internal_energy)
(; gamma) = state_equation
pressure = (gamma - 1.0) * density * internal_energy
return pressure
end
svchb marked this conversation as resolved.
Show resolved Hide resolved

function inverse_state_equation(state_equation::StateEquationIdealGas, pressure)
(; gas_constant, temperature) = state_equation
density = pressure / (gas_constant * temperature)
return density
end

# This version is for simulations that include a temperature.
function inverse_state_equation(state_equation::StateEquationIdealGas, pressure,
internal_energy)
gamma = state_equation.gamma

density = pressure / ((gamma - 1.0) * internal_energy)
return density
end

@inline sound_speed(eos::StateEquationIdealGas) = sqrt(eos.gamma * eos.gas_constant *
eos.temperature)
# This version is for simulations that include a temperature.
@inline sound_speed(eos::StateEquationIdealGas, pressure, density) = sqrt(eos.gamma *
pressure /
(density *
eos.gas_constant))
5 changes: 4 additions & 1 deletion src/schemes/fluid/weakly_compressible_sph/system.jl
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,10 @@ end
return system.pressure[particle]
end

@inline system_sound_speed(system::WeaklyCompressibleSPHSystem) = system.state_equation.sound_speed
@inline system_sound_speed(system::WeaklyCompressibleSPHSystem) = sound_speed(system.state_equation)
@inline system_sound_speed(system::WeaklyCompressibleSPHSystem, pressure, density) = sound_speed(system.state_equation,
svchb marked this conversation as resolved.
Show resolved Hide resolved
pressure,
density)

function update_quantities!(system::WeaklyCompressibleSPHSystem, v, u,
v_ode, u_ode, semi, t)
Expand Down
16 changes: 12 additions & 4 deletions src/visualization/write2vtk.jl
Original file line number Diff line number Diff line change
Expand Up @@ -261,10 +261,18 @@ function write2vtk!(vtk, v, u, t, system::FluidSystem; write_meta_data=true)
vtk["correction_rho0"] = system.correction.rho0
end
vtk["state_equation"] = type2string(system.state_equation)
vtk["state_equation_rho0"] = system.state_equation.reference_density
vtk["state_equation_pa"] = system.state_equation.background_pressure
vtk["state_equation_c"] = system.state_equation.sound_speed
vtk["state_equation_exponent"] = system.state_equation.exponent
if system.state_equation isa StateEquationCole
vtk["state_equation_rho0"] = system.state_equation.reference_density
vtk["state_equation_pa"] = system.state_equation.background_pressure
vtk["state_equation_c"] = system.state_equation.sound_speed
vtk["state_equation_exponent"] = system.state_equation.exponent
end

if system.state_equation isa StateEquationIdealGas
vtk["state_equation_gamma"] = system.state_equation.gamma
vtk["state_equation_gas_c"] = system.state_equation.gas_constant
vtk["state_equation_temperature"] = system.state_equation.temperature
end

vtk["solver"] = "WCSPH"
else
Expand Down
12 changes: 12 additions & 0 deletions test/examples/examples.jl
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,18 @@
@test count_rhs_allocations(sol, semi) == 0
end

@trixi_testset "fluid/dam_break_2phase_2d.jl" begin
@test_nowarn_mod trixi_include(@__MODULE__,
joinpath(examples_dir(), "fluid",
"dam_break_2phase_2d.jl"),
tspan=(0.0, 0.05)) [
r"┌ Info: The desired tank length in y-direction .*\n",
r"└ New tank length in y-direction.*\n"
]
@test sol.retcode == ReturnCode.Success
@test count_rhs_allocations(sol, semi) == 0
end

@trixi_testset "fluid/dam_break_3d.jl" begin
@test_nowarn_mod trixi_include(@__MODULE__,
joinpath(examples_dir(), "fluid",
Expand Down
Loading