Skip to content

Commit

Permalink
Make TreeMesh type general
Browse files Browse the repository at this point in the history
  • Loading branch information
DanielDoehring committed Oct 23, 2024
1 parent 4a81e00 commit cba1d58
Show file tree
Hide file tree
Showing 4 changed files with 97 additions and 84 deletions.
46 changes: 24 additions & 22 deletions src/auxiliary/precompile.jl
Original file line number Diff line number Diff line change
Expand Up @@ -244,10 +244,12 @@ function _precompile_manual_()
end
for TreeType in (SerialTree, ParallelTree), NDIMS in 1:3
@assert Base.precompile(Tuple{typeof(Trixi.initialize!),
TreeMesh{NDIMS, TreeType{NDIMS}}, Int, Tuple{},
TreeMesh{NDIMS, TreeType{NDIMS}, Float64}, Int,
Tuple{},
Tuple{}})
@assert Base.precompile(Tuple{typeof(Trixi.save_mesh_file),
TreeMesh{NDIMS, TreeType{NDIMS}}, String, Int})
TreeMesh{NDIMS, TreeType{NDIMS}, Float64}, String,
Int})
end

# Constructors: linear advection
Expand Down Expand Up @@ -396,58 +398,58 @@ function _precompile_manual_()

# 1D, serial
@assert Base.precompile(Tuple{typeof(Trixi.init_boundaries), Array{Int, 1},
TreeMesh{1, Trixi.SerialTree{1}},
TreeMesh{1, Trixi.SerialTree{1}, RealT},
Trixi.ElementContainer1D{RealT, uEltype}})
@assert Base.precompile(Tuple{typeof(Trixi.init_interfaces), Array{Int, 1},
TreeMesh{1, Trixi.SerialTree{1}},
TreeMesh{1, Trixi.SerialTree{1}, RealT},
Trixi.ElementContainer1D{RealT, uEltype}})
@assert Base.precompile(Tuple{typeof(Trixi.save_mesh_file),
TreeMesh{1, Trixi.SerialTree{1}}, String})
TreeMesh{1, Trixi.SerialTree{1}, RealT}, String})

# 2D, serial
@assert Base.precompile(Tuple{typeof(Trixi.init_boundaries), Array{Int, 1},
TreeMesh{2, Trixi.SerialTree{2}},
TreeMesh{2, Trixi.SerialTree{2}, RealT},
Trixi.ElementContainer2D{RealT, uEltype}})
@assert Base.precompile(Tuple{typeof(Trixi.init_interfaces), Array{Int, 1},
TreeMesh{2, Trixi.SerialTree{2}},
TreeMesh{2, Trixi.SerialTree{2}, RealT},
Trixi.ElementContainer2D{RealT, uEltype}})
@assert Base.precompile(Tuple{typeof(Trixi.init_mortars), Array{Int, 1},
TreeMesh{2, Trixi.SerialTree{2}},
TreeMesh{2, Trixi.SerialTree{2}, RealT},
Trixi.ElementContainer2D{RealT, uEltype},
mortar_type})
@assert Base.precompile(Tuple{typeof(Trixi.save_mesh_file),
TreeMesh{2, Trixi.SerialTree{2}}, String})
TreeMesh{2, Trixi.SerialTree{2}, RealT}, String})

# 2D, parallel
@assert Base.precompile(Tuple{typeof(Trixi.init_boundaries), Array{Int, 1},
TreeMesh{2, Trixi.ParallelTree{2}},
TreeMesh{2, Trixi.ParallelTree{2}, RealT},
Trixi.ElementContainer2D{RealT, uEltype}})
@assert Base.precompile(Tuple{typeof(Trixi.init_interfaces), Array{Int, 1},
TreeMesh{2, Trixi.ParallelTree{2}},
TreeMesh{2, Trixi.ParallelTree{2}, RealT},
Trixi.ElementContainer2D{RealT, uEltype}})
@assert Base.precompile(Tuple{typeof(Trixi.init_mortars), Array{Int, 1},
TreeMesh{2, Trixi.ParallelTree{2}},
TreeMesh{2, Trixi.ParallelTree{2}, RealT},
Trixi.ElementContainer2D{RealT, uEltype},
mortar_type})
@assert Base.precompile(Tuple{typeof(Trixi.init_mpi_interfaces), Array{Int, 1},
TreeMesh{2, Trixi.ParallelTree{2}},
TreeMesh{2, Trixi.ParallelTree{2}, RealT},
Trixi.ElementContainer2D{RealT, uEltype}})
@assert Base.precompile(Tuple{typeof(Trixi.save_mesh_file),
TreeMesh{2, Trixi.ParallelTree{2}}, String})
TreeMesh{2, Trixi.ParallelTree{2}, RealT}, String})

# 3D, serial
@assert Base.precompile(Tuple{typeof(Trixi.init_boundaries), Array{Int, 1},
TreeMesh{3, Trixi.SerialTree{3}},
TreeMesh{3, Trixi.SerialTree{3}, RealT},
Trixi.ElementContainer3D{RealT, uEltype}})
@assert Base.precompile(Tuple{typeof(Trixi.init_interfaces), Array{Int, 1},
TreeMesh{3, Trixi.SerialTree{3}},
TreeMesh{3, Trixi.SerialTree{3}, RealT},
Trixi.ElementContainer3D{RealT, uEltype}})
@assert Base.precompile(Tuple{typeof(Trixi.init_mortars), Array{Int, 1},
TreeMesh{3, Trixi.SerialTree{3}},
TreeMesh{3, Trixi.SerialTree{3}, RealT},
Trixi.ElementContainer3D{RealT, uEltype},
mortar_type})
@assert Base.precompile(Tuple{typeof(Trixi.save_mesh_file),
TreeMesh{3, Trixi.SerialTree{3}}, String})
TreeMesh{3, Trixi.SerialTree{3}, RealT}, String})
end

# various `show` methods
Expand All @@ -456,16 +458,16 @@ function _precompile_manual_()
for NDIMS in 1:3
# serial
@assert Base.precompile(Tuple{typeof(show), Base.TTY,
TreeMesh{NDIMS, Trixi.SerialTree{NDIMS}}})
TreeMesh{NDIMS, Trixi.SerialTree{NDIMS}, RealT}})
@assert Base.precompile(Tuple{typeof(show), IOContext{Base.TTY},
MIME"text/plain",
TreeMesh{NDIMS, Trixi.SerialTree{NDIMS}}})
TreeMesh{NDIMS, Trixi.SerialTree{NDIMS}, RealT}})
# parallel
@assert Base.precompile(Tuple{typeof(show), Base.TTY,
TreeMesh{NDIMS, Trixi.ParallelTree{NDIMS}}})
TreeMesh{NDIMS, Trixi.ParallelTree{NDIMS}, RealT}})
@assert Base.precompile(Tuple{typeof(show), IOContext{Base.TTY},
MIME"text/plain",
TreeMesh{NDIMS, Trixi.ParallelTree{NDIMS}}})
TreeMesh{NDIMS, Trixi.ParallelTree{NDIMS}, RealT}})
end

# equations
Expand Down
33 changes: 18 additions & 15 deletions src/meshes/parallel_tree.jl
Original file line number Diff line number Diff line change
Expand Up @@ -25,24 +25,24 @@
# function, which is required for implementing level-wise refinement in a sane
# way. Also, depth-first ordering *might* not be guaranteed during
# refinement/coarsening operations.
mutable struct ParallelTree{NDIMS} <: AbstractTree{NDIMS}
mutable struct ParallelTree{NDIMS, RealT <: Real} <: AbstractTree{NDIMS}
parent_ids::Vector{Int}
child_ids::Matrix{Int}
neighbor_ids::Matrix{Int}
levels::Vector{Int}
coordinates::Matrix{Float64}
coordinates::Matrix{RealT}
original_cell_ids::Vector{Int}
mpi_ranks::Vector{Int}

capacity::Int
length::Int
dummy::Int

center_level_0::SVector{NDIMS, Float64}
length_level_0::Float64
center_level_0::SVector{NDIMS, RealT}
length_level_0::RealT
periodicity::NTuple{NDIMS, Bool}

function ParallelTree{NDIMS}(capacity::Integer) where {NDIMS}
function ParallelTree{NDIMS, RealT}(capacity::Integer) where {NDIMS, RealT <: Real}
# Verify that NDIMS is an integer
@assert NDIMS isa Integer

Expand Down Expand Up @@ -71,13 +71,14 @@ mutable struct ParallelTree{NDIMS} <: AbstractTree{NDIMS}
end

# Constructor for passing the dimension as an argument
ParallelTree(::Val{NDIMS}, args...) where {NDIMS} = ParallelTree{NDIMS}(args...)
ParallelTree(::Val{NDIMS}, args...) where {NDIMS, RealT <: Real} = ParallelTree{NDIMS}(args...)

# Create and initialize tree
function ParallelTree{NDIMS}(capacity::Int, center::AbstractArray{Float64},
length::Real, periodicity = true) where {NDIMS}
function ParallelTree{NDIMS}(capacity::Int, center::AbstractArray{RealT},
length::Real,
periodicity = true) where {NDIMS, RealT <: Real}
# Create instance
t = ParallelTree{NDIMS}(capacity)
t = ParallelTree{NDIMS, RealT}(capacity)

# Initialize root cell
init!(t, center, length, periodicity)
Expand All @@ -86,13 +87,14 @@ function ParallelTree{NDIMS}(capacity::Int, center::AbstractArray{Float64},
end

# Constructor accepting a single number as center (as opposed to an array) for 1D
function ParallelTree{1}(cap::Int, center::Real, len::Real, periodicity = true)
ParallelTree{1}(cap, [convert(Float64, center)], len, periodicity)
function ParallelTree{1}(cap::Int, center::RealT, len::RealT,
periodicity = true) where {RealT <: Real}
ParallelTree{1}(cap, [center], len, periodicity)
end

# Clear tree with deleting data structures, store center and length, and create root cell
function init!(t::ParallelTree, center::AbstractArray{Float64}, length::Real,
periodicity = true)
function init!(t::ParallelTree, center::AbstractArray{RealT}, length::RealT,
periodicity = true) where {RealT}
clear!(t)

# Set domain information
Expand Down Expand Up @@ -222,12 +224,13 @@ function raw_copy!(target::ParallelTree, source::ParallelTree, first::Int, last:
end

# Reset data structures by recreating all internal storage containers and invalidating all elements
function reset_data_structures!(t::ParallelTree{NDIMS}) where {NDIMS}
function reset_data_structures!(t::ParallelTree{NDIMS, RealT}) where {NDIMS,
RealT <: Real}
t.parent_ids = Vector{Int}(undef, t.capacity + 1)
t.child_ids = Matrix{Int}(undef, 2^NDIMS, t.capacity + 1)
t.neighbor_ids = Matrix{Int}(undef, 2 * NDIMS, t.capacity + 1)
t.levels = Vector{Int}(undef, t.capacity + 1)
t.coordinates = Matrix{Float64}(undef, NDIMS, t.capacity + 1)
t.coordinates = Matrix{RealT}(undef, NDIMS, t.capacity + 1)
t.original_cell_ids = Vector{Int}(undef, t.capacity + 1)
t.mpi_ranks = Vector{Int}(undef, t.capacity + 1)

Expand Down
34 changes: 19 additions & 15 deletions src/meshes/serial_tree.jl
Original file line number Diff line number Diff line change
Expand Up @@ -25,23 +25,23 @@
# function, which is required for implementing level-wise refinement in a sane
# way. Also, depth-first ordering *might* not by guaranteed during
# refinement/coarsening operations.
mutable struct SerialTree{NDIMS} <: AbstractTree{NDIMS}
mutable struct SerialTree{NDIMS, RealT <: Real} <: AbstractTree{NDIMS}
parent_ids::Vector{Int}
child_ids::Matrix{Int}
neighbor_ids::Matrix{Int}
levels::Vector{Int}
coordinates::Matrix{Float64}
coordinates::Matrix{RealT}
original_cell_ids::Vector{Int}

capacity::Int
length::Int
dummy::Int

center_level_0::SVector{NDIMS, Float64}
length_level_0::Float64
center_level_0::SVector{NDIMS, RealT}
length_level_0::RealT
periodicity::NTuple{NDIMS, Bool}

function SerialTree{NDIMS}(capacity::Integer) where {NDIMS}
function SerialTree{NDIMS, RealT}(capacity::Integer) where {NDIMS, RealT <: Real}
# Verify that NDIMS is an integer
@assert NDIMS isa Integer

Expand Down Expand Up @@ -69,13 +69,15 @@ mutable struct SerialTree{NDIMS} <: AbstractTree{NDIMS}
end

# Constructor for passing the dimension as an argument
SerialTree(::Val{NDIMS}, args...) where {NDIMS} = SerialTree{NDIMS}(args...)
SerialTree(::Val{NDIMS}, args...) where {NDIMS, RealT <: Real} = SerialTree{NDIMS,
RealT}(args...)

# Create and initialize tree
function SerialTree{NDIMS}(capacity::Int, center::AbstractArray{Float64},
length::Real, periodicity = true) where {NDIMS}
function SerialTree{NDIMS}(capacity::Int, center::AbstractArray{RealT},
length::RealT,
periodicity = true) where {NDIMS, RealT <: Real}
# Create instance
t = SerialTree{NDIMS}(capacity)
t = SerialTree{NDIMS, RealT}(capacity)

# Initialize root cell
init!(t, center, length, periodicity)
Expand All @@ -84,13 +86,14 @@ function SerialTree{NDIMS}(capacity::Int, center::AbstractArray{Float64},
end

# Constructor accepting a single number as center (as opposed to an array) for 1D
function SerialTree{1}(cap::Int, center::Real, len::Real, periodicity = true)
SerialTree{1}(cap, [convert(Float64, center)], len, periodicity)
function SerialTree{1}(cap::Int, center::RealT, len::RealT,
periodicity = true) where {RealT <: Real}
SerialTree{1, RealT}(cap, [center], len, periodicity)
end

# Clear tree with deleting data structures, store center and length, and create root cell
function init!(t::SerialTree, center::AbstractArray{Float64}, length::Real,
periodicity = true)
function init!(t::SerialTree, center::AbstractArray{RealT}, length::RealT,
periodicity = true) where {RealT}
clear!(t)

# Set domain information
Expand Down Expand Up @@ -203,12 +206,13 @@ function raw_copy!(target::SerialTree, source::SerialTree, first::Int, last::Int
end

# Reset data structures by recreating all internal storage containers and invalidating all elements
function reset_data_structures!(t::SerialTree{NDIMS}) where {NDIMS}
function reset_data_structures!(t::SerialTree{NDIMS, RealT}) where {NDIMS, RealT <:
Real}
t.parent_ids = Vector{Int}(undef, t.capacity + 1)
t.child_ids = Matrix{Int}(undef, 2^NDIMS, t.capacity + 1)
t.neighbor_ids = Matrix{Int}(undef, 2 * NDIMS, t.capacity + 1)
t.levels = Vector{Int}(undef, t.capacity + 1)
t.coordinates = Matrix{Float64}(undef, NDIMS, t.capacity + 1)
t.coordinates = Matrix{RealT}(undef, NDIMS, t.capacity + 1)
t.original_cell_ids = Vector{Int}(undef, t.capacity + 1)

invalidate!(t, 1, capacity(t) + 1)
Expand Down
Loading

0 comments on commit cba1d58

Please sign in to comment.