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

Improved type stability #87

Merged
merged 1 commit into from
Apr 24, 2024
Merged
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
6 changes: 4 additions & 2 deletions src/Peridynamics.jl
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,16 @@ abstract type AbstractMaterial end
abstract type AbstractSpatialSetup end
abstract type AbstractBody{T<:AbstractMaterial} <: AbstractSpatialSetup end
abstract type AbstractMultibodySetup{T<:AbstractMaterial} <: AbstractSpatialSetup end
abstract type AbstractPointParameters end
abstract type AbstractParameterSetup end
abstract type AbstractPointParameters <: AbstractParameterSetup end
abstract type AbstractParamSpec end
abstract type AbstractTimeSolver end
abstract type AbstractJob end
abstract type AbstractOptions end
abstract type AbstractSystem end
abstract type AbstractPredefinedCrack end
abstract type AbstractBodyChunk{S<:AbstractSystem,T<:AbstractMaterial} end
abstract type AbstractParameterHandler{N} end
abstract type AbstractParameterHandler <: AbstractParameterSetup end
abstract type AbstractChunkHandler end
abstract type AbstractDataHandler end
abstract type AbstractThreadsDataHandler <: AbstractDataHandler end
Expand Down
9 changes: 6 additions & 3 deletions src/core/mpi_data_handler.jl
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,10 @@ end

function MPIDataHandler(body::AbstractBody, solver::AbstractTimeSolver,
point_decomp::PointDecomposition)
@timeit_debug TO "chop chunk" chunk = chop_body_mpi(body, solver, point_decomp)
param_spec = get_param_spec(body)
@timeit_debug TO "chop chunk" begin
chunk = chop_body_mpi(body, solver, point_decomp, param_spec)
end
@timeit_debug TO "find halo exchanges" begin
halo_infos = get_halo_infos(chunk, point_decomp, body.n_points)
lth_exs, htl_exs = find_halo_exchanges(halo_infos)
Expand All @@ -46,8 +49,8 @@ function MPIDataHandler(multibody::AbstractMultibodySetup,
end

function chop_body_mpi(body::AbstractBody, solver::AbstractTimeSolver,
point_decomp::PointDecomposition)
chunk = BodyChunk(body, solver, point_decomp, mpi_chunk_id())
point_decomp::PointDecomposition, param_spec::AbstractParamSpec)
chunk = BodyChunk(body, solver, point_decomp, mpi_chunk_id(), param_spec)
apply_precracks!(chunk, body)
apply_initial_conditions!(chunk, body)
return chunk
Expand Down
46 changes: 30 additions & 16 deletions src/core/parameter_handler.jl
Original file line number Diff line number Diff line change
@@ -1,28 +1,42 @@
struct ParameterHandler{P<:AbstractPointParameters,N} <: AbstractParameterHandler{N}
struct SingleParamChunk <: AbstractParamSpec end

struct MultiParamChunk <: AbstractParamSpec end

struct ParameterHandler{P<:AbstractPointParameters} <: AbstractParameterHandler
parameters::Vector{P}
point_mapping::Vector{Int}
end

function ParameterHandler(body::AbstractBody, ch::AbstractChunkHandler)
parameters = body.point_params
point_mapping = body.params_map[ch.loc_points]
return ParameterHandler(parameters, point_mapping)
end

function init_params(body::AbstractBody, ::AbstractChunkHandler, ::SingleParamChunk)
return first(body.point_params)
end

function init_params(body::AbstractBody, ch::AbstractChunkHandler, ::MultiParamChunk)
return ParameterHandler(body, ch)
end

function ParameterHandler(body::Body{M,P}, ch::AbstractChunkHandler) where {M,P}
parameters = body.point_params
point_mapping = body.params_map[ch.loc_points]
N = length(body.point_params)
return new{P,N}(parameters, point_mapping)
end
@inline function get_params(paramhandler::ParameterHandler, point_id::Int)
return paramhandler.parameters[paramhandler.point_mapping[point_id]]
end

@inline function get_params(ph::ParameterHandler, point_id::Int)
return ph.parameters[ph.point_mapping[point_id]]
@inline function get_params(params::AbstractPointParameters, ::Int)
return params
end

@inline function get_params(ph::ParameterHandler{P,1}, ::Int) where {P}
return first(ph.parameters)
@inline function parameter_setup_type(::Body{M,P}, ::SingleParamChunk) where {M,P}
return P
end

@inline function get_params(ph::ParameterHandler{P,1}) where {P}
return first(ph.parameters)
@inline function parameter_setup_type(::Body{M,P}, ::MultiParamChunk) where {M,P}
return ParameterHandler{P}
end

@inline function parameter_handler_type(body::Body{M,P}) where {M,P}
N = length(body.point_params)
return ParameterHandler{P,N}
@inline function get_param_spec(body::AbstractBody)
return length(body.point_params) == 1 ? SingleParamChunk() : MultiParamChunk()
end
15 changes: 8 additions & 7 deletions src/core/threads_data_handler.jl
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ end

function ThreadsDataHandler(body::AbstractBody, solver::AbstractTimeSolver,
point_decomp::PointDecomposition)
chunks = chop_body_threads(body, solver, point_decomp)
param_spec = get_param_spec(body)
chunks = chop_body_threads(body, solver, point_decomp, param_spec)
n_chunks = length(chunks)
lth_exs, htl_exs = find_halo_exchanges(chunks)
return ThreadsDataHandler(n_chunks, chunks, lth_exs, htl_exs)
Expand All @@ -19,18 +20,18 @@ function ThreadsDataHandler(multibody::AbstractMultibodySetup, solver::AbstractT
end

function chop_body_threads(body::AbstractBody, solver::AbstractTimeSolver,
point_decomp::PointDecomposition)
ChunkType = body_chunk_type(body, solver)
chunks = _chop_body_threads(ChunkType, body, solver, point_decomp)
point_decomp::PointDecomposition, param_spec::AbstractParamSpec)
ChunkType = body_chunk_type(body, solver, param_spec)
chunks = _chop_body_threads(ChunkType, body, solver, point_decomp, param_spec)
return chunks
end

function _chop_body_threads(::Type{ChunkType}, body::AbstractBody,
solver::AbstractTimeSolver,
point_decomp::PointDecomposition) where {ChunkType}
solver::AbstractTimeSolver, point_decomp::PointDecomposition,
param_spec::AbstractParamSpec) where {ChunkType}
chunks = Vector{ChunkType}(undef, point_decomp.n_chunks)
@threads :static for chunk_id in eachindex(point_decomp.decomp)
chunk = BodyChunk(body, solver, point_decomp, chunk_id)
chunk = BodyChunk(body, solver, point_decomp, chunk_id, param_spec)
apply_precracks!(chunk, body)
apply_initial_conditions!(chunk, body)
chunks[chunk_id] = chunk
Expand Down
20 changes: 10 additions & 10 deletions src/discretization/body_chunk.jl
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
struct BodyChunk{System<:AbstractSystem,
Material<:AbstractMaterial,
Params<:AbstractParameterHandler,
Params<:AbstractParameterSetup,
Storage<:AbstractStorage} <: AbstractBodyChunk{System,Material}
system::System
mat::Material
paramhandler::Params
paramsetup::Params
storage::Storage
ch::ChunkHandler
psets::Dict{Symbol,Vector{Int}}
Expand All @@ -13,21 +13,21 @@ struct BodyChunk{System<:AbstractSystem,
cells::Vector{MeshCell{VTKCellType, Tuple{Int64}}}
end

function BodyChunk(body::AbstractBody, solver::AbstractTimeSolver, pd::PointDecomposition,
chunk_id::Int)
function BodyChunk(body::Body{M,P}, solver::AbstractTimeSolver, pd::PointDecomposition,
chunk_id::Int, param_spec::AbstractParamSpec) where {M,P}
mat = body.mat
system, ch = get_system(body, pd, chunk_id)
paramhandler = ParameterHandler(body, ch)
paramsetup = init_params(body, ch, param_spec)
storage = init_storage(mat, solver, system, ch)
psets = localized_point_sets(body.point_sets, ch)
sdbcs = body.single_dim_bcs
pdsdbcs = body.posdep_single_dim_bcs
cells = get_cells(ch.n_loc_points)
return BodyChunk(system, mat, paramhandler, storage, ch, psets, sdbcs, pdsdbcs, cells)
return BodyChunk(system, mat, paramsetup, storage, ch, psets, sdbcs, pdsdbcs, cells)
end

@inline function get_params(b::BodyChunk, point_id::Int)
return get_params(b.paramhandler, point_id)
return get_params(b.paramsetup, point_id)
end

@inline get_system_type(::BodyChunk{Sys,M,P,S}) where {Sys,M,P,S} = Sys
Expand All @@ -46,10 +46,10 @@ end
return get_storage_type(first(vb))
end

@inline function body_chunk_type(body::AbstractBody{Material},
solver::AbstractTimeSolver) where {Material}
@inline function body_chunk_type(body::AbstractBody{Material}, solver::AbstractTimeSolver,
param_spec::AbstractParamSpec) where {Material}
System = system_type(body.mat)
Params = parameter_handler_type(body)
Params = parameter_setup_type(body, param_spec)
Storage = storage_type(body, solver)
return BodyChunk{System,Material,Params,Storage}
end
Expand Down
13 changes: 6 additions & 7 deletions src/physics/force_density.jl
Original file line number Diff line number Diff line change
@@ -1,28 +1,27 @@
function calc_force_density!(b::AbstractBodyChunk)
_calc_force_density!(b.storage, b.system, b.mat, b.paramhandler, each_point_idx(b))
_calc_force_density!(b.storage, b.system, b.mat, b.paramsetup, each_point_idx(b))
return nothing
end

function _calc_force_density!(storage::AbstractStorage, system::AbstractSystem,
mat::AbstractMaterial, paramhandler::AbstractParameterHandler,
mat::AbstractMaterial, paramsetup::AbstractParameterHandler,
each_point_idx)
storage.b_int .= 0
storage.n_active_bonds .= 0
for point_id in each_point_idx
param = get_params(paramhandler, point_id)
force_density_point!(storage, system, mat, param, point_id)
params = get_params(paramsetup, point_id)
force_density_point!(storage, system, mat, params, point_id)
end
return nothing
end

function _calc_force_density!(storage::AbstractStorage, system::AbstractSystem,
mat::AbstractMaterial,
paramhandler::AbstractParameterHandler{1}, each_point_idx)
params::AbstractPointParameters, each_point_idx)
storage.b_int .= 0
storage.n_active_bonds .= 0
param = get_params(paramhandler)
for point_id in each_point_idx
force_density_point!(storage, system, mat, param, point_id)
force_density_point!(storage, system, mat, params, point_id)
end
return nothing
end
7 changes: 3 additions & 4 deletions src/time_solvers/velocity_verlet.jl
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ function _update_disp_and_pos!(displacement, position, velocity_half, Δt, each_
end

function update_acc_and_vel!(b::AbstractBodyChunk, Δt½::Float64)
_update_acc_and_vel!(b.storage, b.paramhandler, Δt½, each_point_idx(b))
_update_acc_and_vel!(b.storage, b.paramsetup, Δt½, each_point_idx(b))
return nothing
end

Expand All @@ -163,11 +163,10 @@ function _update_acc_and_vel!(s::AbstractStorage, paramhandler::AbstractParamete
return nothing
end

function _update_acc_and_vel!(s::AbstractStorage, paramhandler::AbstractParameterHandler{1},
function _update_acc_and_vel!(s::AbstractStorage, params::AbstractPointParameters,
Δt½::Float64, each_point)
param = get_params(paramhandler)
for point_id in each_point
_update_acc!(s.acceleration, s.b_int, s.b_ext, param.rho, point_id)
_update_acc!(s.acceleration, s.b_int, s.b_ext, params.rho, point_id)
_update_vel!(s.velocity, s.velocity_half, s.acceleration, Δt½, point_id)
end
return nothing
Expand Down
3 changes: 2 additions & 1 deletion test/conditions/test_boundary_conditions.jl
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@
forcedensity_bc!(t -> t, body, :a, :x)
ts = VelocityVerlet(steps=10)
pd = Peridynamics.PointDecomposition(body, 2)
b = Peridynamics.BodyChunk(body, ts, pd, 1)
ps = Peridynamics.get_param_spec(body)
b = Peridynamics.BodyChunk(body, ts, pd, 1, ps)
Peridynamics.apply_bcs!(b, 1.0)

@test b.storage.velocity_half ≈ [1.0 1.0; 0.0 0.0; 0.0 0.0]
Expand Down
5 changes: 3 additions & 2 deletions test/core/test_halo_exchange.jl
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,9 @@
precrack!(body, :a, :b)
ts = VelocityVerlet(steps=10)

param_spec = Peridynamics.get_param_spec(body)
point_decomp = Peridynamics.PointDecomposition(body, 2)
body_chunks = Peridynamics.chop_body_threads(body, ts, point_decomp)
body_chunks = Peridynamics.chop_body_threads(body, ts, point_decomp, param_spec)

lth_exs, htl_exs = Peridynamics.find_halo_exchanges(body_chunks)

Expand All @@ -38,7 +39,7 @@
@test htl_exs[2][1].dest_idxs == [1, 2]

point_decomp = Peridynamics.PointDecomposition(body, 4)
body_chunks = Peridynamics.chop_body_threads(body, ts, point_decomp)
body_chunks = Peridynamics.chop_body_threads(body, ts, point_decomp, param_spec)

lth_exs, htl_exs = Peridynamics.find_halo_exchanges(body_chunks)

Expand Down
17 changes: 10 additions & 7 deletions test/discretization/test_body_chunk.jl
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@
precrack!(body, :a, :b)
ts = VelocityVerlet(steps=10)
pd = Peridynamics.PointDecomposition(body, 2)
bc = Peridynamics.BodyChunk(body, ts, pd, 1)
ps = Peridynamics.get_param_spec(body)
bc = Peridynamics.BodyChunk(body, ts, pd, 1, ps)

@test bc.mat == mat
@test bc.system isa Peridynamics.BondSystem
Expand Down Expand Up @@ -59,8 +60,9 @@ end
precrack!(body, :a, :b)
ts = VelocityVerlet(steps=10)
point_decomp = Peridynamics.PointDecomposition(body, 2)
param_spec = Peridynamics.get_param_spec(body)

body_chunks = Peridynamics.chop_body_threads(body, ts, point_decomp)
body_chunks = Peridynamics.chop_body_threads(body, ts, point_decomp, param_spec)

b1 = body_chunks[1]
@test b1 isa Peridynamics.BodyChunk
Expand Down Expand Up @@ -90,7 +92,7 @@ end
@test b1.storage.bond_active == [1, 0, 0, 1, 0, 0]
@test b1.storage.n_active_bonds == [1, 1]

@test b1.paramhandler isa Peridynamics.ParameterHandler{Peridynamics.BBPointParameters,1}
@test b1.paramsetup isa Peridynamics.BBPointParameters
b1_params = Peridynamics.get_params(b1, 1)
@test b1_params.δ ≈ 2.0
@test b1_params.rho ≈ 1.0
Expand Down Expand Up @@ -152,7 +154,7 @@ end
@test b2.storage.bond_active == [0, 0, 1, 0, 0, 1]
@test b2.storage.n_active_bonds == [1, 1]

@test b2.paramhandler isa Peridynamics.ParameterHandler{Peridynamics.BBPointParameters,1}
@test b2.paramsetup isa Peridynamics.BBPointParameters
b2_params = Peridynamics.get_params(b2, 1)
@test b2_params.δ ≈ 2.0
@test b2_params.rho ≈ 1.0
Expand Down Expand Up @@ -205,8 +207,9 @@ end
precrack!(body, :a, :b)
ts = VelocityVerlet(steps=10)
point_decomp = Peridynamics.PointDecomposition(body, 2)
param_spec = Peridynamics.get_param_spec(body)

body_chunks = Peridynamics.chop_body_threads(body, ts, point_decomp)
body_chunks = Peridynamics.chop_body_threads(body, ts, point_decomp, param_spec)

b1 = body_chunks[1]
@test b1 isa Peridynamics.BodyChunk
Expand Down Expand Up @@ -236,7 +239,7 @@ end
@test b1.storage.bond_active == [1, 0, 0, 1, 0, 0]
@test b1.storage.n_active_bonds == [1, 1]

@test length(b1.paramhandler.parameters) == 2
@test length(b1.paramsetup.parameters) == 2

pp1 = Peridynamics.get_params(b1, 1)
@test pp1 isa Peridynamics.BBPointParameters
Expand Down Expand Up @@ -315,7 +318,7 @@ end
@test b2.storage.bond_active == [0, 0, 1, 0, 0, 1]
@test b2.storage.n_active_bonds == [1, 1]

@test length(b2.paramhandler.parameters) == 2
@test length(b2.paramsetup.parameters) == 2

pp3 = Peridynamics.get_params(b2, 1)
@test pp3 isa Peridynamics.BBPointParameters
Expand Down
6 changes: 4 additions & 2 deletions test/integration/predefined_cracks.jl
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,9 @@
precrack!(body, :a, :b)
ts = VelocityVerlet(steps=10)
point_decomp = Peridynamics.PointDecomposition(body, 2)
param_spec = Peridynamics.get_param_spec(body)

body_chunks = Peridynamics.chop_body_threads(body, ts, point_decomp)
body_chunks = Peridynamics.chop_body_threads(body, ts, point_decomp, param_spec)

b1 = body_chunks[1]
@test b1 isa Peridynamics.BodyChunk
Expand Down Expand Up @@ -101,8 +102,9 @@ end
precrack!(body, :a, :b; update_dmg=false)
ts = VelocityVerlet(steps=10)
point_decomp = Peridynamics.PointDecomposition(body, 2)
param_spec = Peridynamics.get_param_spec(body)

body_chunks = Peridynamics.chop_body_threads(body, ts, point_decomp)
body_chunks = Peridynamics.chop_body_threads(body, ts, point_decomp, param_spec)

b1 = body_chunks[1]
@test b1 isa Peridynamics.BodyChunk
Expand Down
Loading