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
2 changes: 1 addition & 1 deletion src/TrixiParticles.jl
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ include("general/restart.jl")
include("visualization/recipes_plots.jl")

export Semidiscretization, semidiscretize, restart_with!
export PairsNHSHandler, SharedNHSHandler
export PairsNHSHandler, SharedNHSHandler, CombinedNHSHandler
export InitialCondition, apply_angular_velocity
export WeaklyCompressibleSPHSystem, EntropicallyDampedSPHSystem, TotalLagrangianSPHSystem,
RigidBodySystem, WallBoundarySystem, DEMSystem, BoundaryDEMSystem,
Expand Down
7 changes: 7 additions & 0 deletions src/callbacks/sorting.jl
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,13 @@ function sort_particles!(system::RequiresSortingSystem, v, u, nhs, semi)
throw(ArgumentError("`SortingCallback` can only be used with a `GridNeighborhoodSearch`"))
end

# The `CombinedNHSHandler` returns a `GridNHSPair`. Sort by the grid of the neighbor search,
# since each NHS in the `CombinedGridNeighborhoodSearch` uses the same grid.
function sort_particles!(system::RequiresSortingSystem, v, u,
nhs::PointNeighbors.GridNHSPair, semi)
sort_particles!(system, v, u, nhs.neighbor_search, semi)
end

# TODO: Sort also masses and particle spacings for variable smoothing lengths.
function sort_particles!(system::RequiresSortingSystem, v, u,
nhs::GridNeighborhoodSearch, semi)
Expand Down
5 changes: 5 additions & 0 deletions src/general/gpu.jl
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,11 @@ function adapt_neighborhood_search_handler(to, handler::SharedNHSHandler)
return SharedNHSHandler(handler.search_radii, searches)
end

function adapt_neighborhood_search_handler(to, handler::CombinedNHSHandler)
return CombinedNHSHandler(Adapt.adapt(to, handler.neighborhood_search),
handler.systems_moving)
end

# This makes `@threaded semi for ...` use `semi.parallelization_backend` for parallelization
@inline function PointNeighbors.parallel_foreach(f, iterator, semi::Semidiscretization)
PointNeighbors.parallel_foreach(f, iterator, semi.parallelization_backend)
Expand Down
11 changes: 10 additions & 1 deletion src/general/interpolation.jl
Original file line number Diff line number Diff line change
Expand Up @@ -489,6 +489,14 @@ function interpolate_points(point_coords, semi, ref_system, v_ode, u_ode;
smoothing_length, cut_off_bnd, clip_negative_pressure)
end

# Template to create a standalone neighborhood search with `copy_neighborhood_search`.
# A `GridNHSPair` (see `CombinedNHSHandler`) is only a view of two cell lists and cannot
# be copied, so we use the neighborhood search of the neighbor system as template.
@inline standalone_template(neighborhood_search) = neighborhood_search
@inline function standalone_template(neighborhood_search::PointNeighbors.GridNHSPair)
return neighborhood_search.neighbor_search
end

function process_neighborhood_searches(semi, u_ode, ref_system, smoothing_length,
point_coords)
if isapprox(smoothing_length, initial_smoothing_length(ref_system))
Expand Down Expand Up @@ -522,7 +530,8 @@ function process_neighborhood_searches(semi, u_ode, ref_system, smoothing_length
old_nhs_cpu = old_nhs
end

nhs_cpu = PointNeighbors.copy_neighborhood_search(old_nhs_cpu, search_radius,
nhs_cpu = PointNeighbors.copy_neighborhood_search(standalone_template(old_nhs_cpu),
search_radius,
nparticles(system))

PointNeighbors.initialize!(nhs_cpu, point_coords_cpu, system_coords_cpu;
Expand Down
175 changes: 173 additions & 2 deletions src/general/neighborhood_search.jl
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,29 @@
points, parallelization_backend)
end

# Return the `PeriodicBox` of a neighborhood search, or `nothing` for a non-periodic one.
# `neighborhood_search = nothing` creates a `TrivialNeighborhoodSearch` without periodicity.
extract_periodic_box(::Nothing) = nothing
extract_periodic_box(nhs) = nhs.periodic_box

# The `CombinedNHSHandler` returns a `GridNHSPair`.
# Both searches of the pair are created from the same template with the same periodic box.
function extract_periodic_box(nhs::PointNeighbors.GridNHSPair)
return extract_periodic_box(nhs.neighbor_search)
end

deactivate_out_of_bounds_particles!(system, buffer, nhs, v, u, semi) = system

function deactivate_out_of_bounds_particles!(system, buffer::SystemBuffer,
nhs::GridNeighborhoodSearch, v, u, semi)
deactivate_out_of_bounds_particles!(system, buffer, nhs, nhs.cell_list, v, u, semi)
end

function deactivate_out_of_bounds_particles!(system, buffer::SystemBuffer,
nhs::PointNeighbors.GridNHSPair, v, u, semi)
deactivate_out_of_bounds_particles!(system, buffer, nhs.neighbor_search, v, u, semi)
end

function deactivate_out_of_bounds_particles!(system, buffer, nhs, cell_list, v, u, semi)
return system
end
Expand Down Expand Up @@ -410,6 +426,89 @@ function get_neighborhood_search(handler::SharedNHSHandler, system_index, neighb
return handler.neighborhood_searches[neighbor_index][radius_index]
end

"""
CombinedNHSHandler

Neighborhood search handler that stores the particles of all systems in the same grid.

`CombinedNHSHandler` stores one cell list per system, like the [`SharedNHSHandler`](@ref),
but all cell lists use the same grid with the same search radius
(see [`PointNeighbors.CombinedGridNeighborhoodSearch`](@ref)).
The cell list of each system is updated only once per update of the neighborhood searches.
Because the particles of both systems of an interaction are stored in the same grid,
neighbor loops can traverse the particles of both systems cell by cell.

!!! warning
This handler requires a [`GridNeighborhoodSearch`](@ref) and the same search radius
for all pairs of systems that interact with each other.

# Examples
```jldoctest semi_example; output=false, setup = :(using TrixiParticles; trixi_include(@__MODULE__, joinpath(examples_dir(), "fluid", "hydrostatic_water_column_2d.jl"), sol=nothing); system1 = fluid_system; system2 = boundary_system)
semi = Semidiscretization(system1, system2;
neighborhood_search=GridNeighborhoodSearch{2}(),
neighborhood_search_handler=CombinedNHSHandler)

# output
┌──────────────────────────────────────────────────────────────────────────────────────────────────┐
│ Semidiscretization │
│ ══════════════════ │
│ #spatial dimensions: ………………………… 2 │
│ #systems: ……………………………………………………… 2 │
│ neighborhood search: ………………………… CombinedGridNeighborhoodSearch │
│ total #particles: ………………………………… 636 │
│ eltype: …………………………………………………………… Float64 │
│ coordinates eltype: …………………………… Float64 │
└──────────────────────────────────────────────────────────────────────────────────────────────────┘
```
"""
struct CombinedNHSHandler{NHS} <: AbstractNHSHandler
neighborhood_search::NHS
# Buffer for `update_nhs!` to store which cell lists have to be updated.
systems_moving::Vector{Bool}
end

function CombinedNHSHandler(neighborhood_search::GridNeighborhoodSearch, systems)
# A compact support of zero means that this neighborhood search is never used.
# TLSPH self-interaction is using a specialized neighborhood search.
search_radii = [compact_support(system, neighbor)
for system in systems, neighbor in systems
if !(system === neighbor && system isa TotalLagrangianSPHSystem)]
search_radius = maximum(search_radii)

if !all(radius -> iszero(radius) || isapprox(radius, search_radius), search_radii)
throw(ArgumentError("`CombinedNHSHandler` requires the same search radius " *
"for all pairs of systems, but got the search radii " *
"$(unique(search_radii))"))
end

combined_nhs = PointNeighbors.CombinedGridNeighborhoodSearch(neighborhood_search,
search_radius,
map(nparticles, systems))

return CombinedNHSHandler(combined_nhs, fill(false, length(systems)))
end

function CombinedNHSHandler(neighborhood_search, systems)
throw(ArgumentError("`CombinedNHSHandler` requires a `GridNeighborhoodSearch`"))
end

function neighborhood_search_name(handler::CombinedNHSHandler)
return nameof(typeof(handler.neighborhood_search))
end

@inline function get_neighborhood_search(handler::CombinedNHSHandler, system_index,
neighbor_index, search_radius)
(; neighborhood_search) = handler

# Subtract `eps` to avoid an error due to rounding errors.
@boundscheck if search_radius - eps(search_radius) >
PointNeighbors.search_radius(neighborhood_search)
throw(ArgumentError("no combined neighborhood search with radius >= $search_radius"))
end

return PointNeighbors.grid_nhs_pair(neighborhood_search, system_index, neighbor_index)
end

# === Neighborhood search lookup ===
@inline function get_neighborhood_search(system, semi)
return get_neighborhood_search(system, system, semi)
Expand Down Expand Up @@ -458,6 +557,10 @@ function initialize_neighborhood_searches!(semi, u0_ode, restart_with::Nothing)
end

function initialize_neighborhood_searches!(semi)
initialize_neighborhood_searches!(semi, semi.neighborhood_search_handler)
end

function initialize_neighborhood_searches!(semi, handler)
foreach_system(semi) do system
foreach_system(semi) do neighbor
initialize_neighborhood_search!(semi, system, neighbor)
Expand All @@ -467,6 +570,27 @@ function initialize_neighborhood_searches!(semi)
return semi
end

function initialize_neighborhood_searches!(semi, handler::CombinedNHSHandler)
return initialize_combined_neighborhood_search!(initial_coordinates, semi, handler)
end

# `coordinates(system)` returns the coordinates to initialize the cell list of `system` with.
function initialize_combined_neighborhood_search!(coordinates, semi,
handler::CombinedNHSHandler)
foreach_system(semi) do system
# TODO Initialize after adapting to the GPU.
# Currently, this cannot use `semi.parallelization_backend`
# because data is still on the CPU.
PointNeighbors.initialize!(handler.neighborhood_search,
system_indices(system, semi),
coordinates(system),
eachindex=each_active_particle(system),
parallelization_backend=PolyesterBackend())
end

return semi
end

function initialize_neighborhood_search!(semi, system, neighbor)
# TODO Initialize after adapting to the GPU.
# Currently, this cannot use `semi.parallelization_backend`
Expand Down Expand Up @@ -494,19 +618,28 @@ end

# === Neighborhood search updates (per-system) ===
function update_nhs!(semi, u_ode)
update_nhs!(semi, semi.neighborhood_search_handler, u_ode)
end

function update_nhs!(semi, handler, u_ode)
# Update NHS for each pair of systems
foreach_system(semi) do system
u_system = wrap_u(u_ode, system, semi)

foreach_system(semi) do neighbor
points_moving_ = points_moving(system, neighbor)

# Skip the update if no points are moving. This also skips pairs whose NHS
# is never updated, like the frozen TLSPH self-interaction NHS.
any(points_moving_) || return nothing

u_neighbor = wrap_u(u_ode, neighbor, semi)
neighborhood_search = get_neighborhood_search(system, neighbor, semi)
neighbor_coords = current_coordinates(u_neighbor, neighbor)

eachindex_y = each_active_particle(neighbor)
if eachindex_y != axes(neighbor_coords, 2) &&
!PointNeighbors.supports_inactive_points(neighborhood_search)

if PointNeighbors.supports_infinite_domain(neighborhood_search)
# If the neighborhood search supports an infinite domain,
# we can update it for inactive points as well, which will not interfere
Expand All @@ -521,9 +654,47 @@ function update_nhs!(semi, u_ode)
update!(neighborhood_search,
current_coordinates(u_system, system),
current_coordinates(u_neighbor, neighbor),
semi; points_moving=points_moving(system, neighbor), eachindex_y)
semi; points_moving=points_moving_, eachindex_y)
end
end
end

function update_nhs!(semi, handler::CombinedNHSHandler, u_ode)
(; neighborhood_search, systems_moving) = handler

# The per-pair `points_moving` methods define which systems have to be updated.
# Instead of updating the neighborhood search of each pair, we set a system as moving
# if it appears as moving in at least one pair.
# Note that a `GridNHSPair` requires up-to-date cell lists of both systems
# (see `PointNeighbors.requires_update`), not only of the neighbor system.
# The systems flagged as moving are then updated only once.
systems_moving .= false
foreach_system(semi) do system
system_index = system_indices(system, semi)

foreach_system(semi) do neighbor
neighbor_index = system_indices(neighbor, semi)
system_moving, neighbor_moving = points_moving(system, neighbor)

systems_moving[system_index] |= system_moving
systems_moving[neighbor_index] |= neighbor_moving
end
end

# Update the NHS of each moving system only once.
foreach_system(semi) do system
system_index = system_indices(system, semi)

if systems_moving[system_index]
u_system = wrap_u(u_ode, system, semi)
PointNeighbors.update!(neighborhood_search, system_index,
current_coordinates(u_system, system),
eachindex=each_active_particle(system),
parallelization_backend=semi.parallelization_backend)
end
end

return semi
end

# === Which points move between updates (per-pair dispatch) ===
Expand Down
12 changes: 12 additions & 0 deletions src/general/restart.jl
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,11 @@ restore_previous_state!(system, restart_file) = system

function initialize_neighborhood_searches!(semi, u0_ode,
restart_with::Tuple{Vararg{String}})
initialize_restart_neighborhood_searches!(semi, semi.neighborhood_search_handler,
u0_ode)
end

function initialize_restart_neighborhood_searches!(semi, handler, u0_ode)
foreach_system(semi) do system
foreach_system(semi) do neighbor
initialize_neighborhood_search!(semi, system, neighbor, u0_ode)
Expand All @@ -95,6 +100,13 @@ function initialize_neighborhood_searches!(semi, u0_ode,
return semi
end

function initialize_restart_neighborhood_searches!(semi, handler::CombinedNHSHandler,
u0_ode)
coordinates(system) = initial_restart_coordinates(system, u0_ode, semi)

return initialize_combined_neighborhood_search!(coordinates, semi, handler)
end

function initialize_neighborhood_search!(semi, system, neighbor, u0_ode)
# TODO Initialize after adapting to the GPU.
# Currently, this cannot use `semi.parallelization_backend`
Expand Down
2 changes: 1 addition & 1 deletion src/io/write_vtk.jl
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ function _trixi2vtk(dvdu_ode, vu_ode, semi, t; iter=nothing, overwrite=isnothing

foreach_system(semi) do system
system_index = system_indices(system, semi)
periodic_box = get_neighborhood_search(system, semi).periodic_box
periodic_box = extract_periodic_box(get_neighborhood_search(system, semi))

_trixi2vtk(system, dvdu_ode, vu_ode, semi, t, periodic_box;
system_name=filenames[system_index], output_directory, iter,
Expand Down
3 changes: 2 additions & 1 deletion src/schemes/boundary/open_boundary/boundary_zones.jl
Original file line number Diff line number Diff line change
Expand Up @@ -498,7 +498,8 @@ end
end

function update_boundary_zone_indices!(system, u, boundary_zones, semi)
periodic_box = get_neighborhood_search(system.fluid_system, system, semi).periodic_box
periodic_box = extract_periodic_box(get_neighborhood_search(system.fluid_system, system,
semi))

set_zero!(system.boundary_zone_indices)

Expand Down
10 changes: 6 additions & 4 deletions src/schemes/boundary/open_boundary/mirroring.jl
Original file line number Diff line number Diff line change
Expand Up @@ -166,9 +166,10 @@ function extrapolate_values!(system,
eltype(system)}))

# TODO: Not public API
search_radius = PointNeighbors.search_radius(nhs)
PointNeighbors.foreach_neighbor(fluid_coords, nhs, particle, ghost_node_position,
nhs.search_radius) do particle, neighbor, pos_diff,
distance
search_radius) do particle, neighbor, pos_diff,
distance
m_b = hydrodynamic_mass(fluid_system, neighbor)
rho_b = current_density(v_fluid, fluid_system, neighbor)
pressure_b = current_pressure(v_fluid, fluid_system, neighbor)
Expand Down Expand Up @@ -306,9 +307,10 @@ function extrapolate_values!(system, mirror_method::ZerothOrderMirroring,
interpolated_velocity = Ref(zero(particle_coords))

# TODO: Not public API
search_radius = PointNeighbors.search_radius(nhs)
PointNeighbors.foreach_neighbor(fluid_coords, nhs, particle, ghost_node_position,
nhs.search_radius) do particle, neighbor, pos_diff,
distance
search_radius) do particle, neighbor, pos_diff,
distance
m_b = hydrodynamic_mass(fluid_system, neighbor)
rho_b = current_density(v_fluid, fluid_system, neighbor)
volume_b = m_b / rho_b
Expand Down
2 changes: 1 addition & 1 deletion src/schemes/boundary/open_boundary/system.jl
Original file line number Diff line number Diff line change
Expand Up @@ -397,7 +397,7 @@ function check_domain!(system, v, u, v_ode, u_ode, semi)

u_fluid = wrap_u(u_ode, fluid_system, semi)
v_fluid = wrap_v(v_ode, fluid_system, semi)
periodic_box = get_neighborhood_search(fluid_system, system, semi).periodic_box
periodic_box = extract_periodic_box(get_neighborhood_search(fluid_system, system, semi))

boundary_candidates .= false

Expand Down
3 changes: 0 additions & 3 deletions src/schemes/structure/total_lagrangian_sph/system.jl
Original file line number Diff line number Diff line change
Expand Up @@ -271,9 +271,6 @@ function initialize_self_interaction_nhs(system::TotalLagrangianSPHSystem,
system.cache)
end

extract_periodic_box(::Nothing) = nothing
extract_periodic_box(nhs) = nhs.periodic_box

create_cache_tlsph(::Nothing, initial_condition) = (;)

function create_cache_tlsph(::PrescribedMotion, initial_condition)
Expand Down
2 changes: 1 addition & 1 deletion src/visualization/recipes_plots.jl
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ end

function get_system_plot_data(u_ode, system, semi, particle_spacing)
u = wrap_u(u_ode, system, semi)
periodic_box = get_neighborhood_search(system, semi).periodic_box
periodic_box = extract_periodic_box(get_neighborhood_search(system, semi))
coordinates = PointNeighbors.periodic_coords(active_coordinates(u, system),
periodic_box)

Expand Down
Loading