Skip to content

Commit 2614a5f

Browse files
Refactor Tree constructors (#2661)
Co-authored-by: Hendrik Ranocha <[email protected]>
1 parent f77e504 commit 2614a5f

File tree

3 files changed

+58
-74
lines changed

3 files changed

+58
-74
lines changed

src/meshes/parallel_tree.jl

Lines changed: 22 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -26,47 +26,42 @@
2626
# way. Also, depth-first ordering *might* not be guaranteed during
2727
# refinement/coarsening operations.
2828
mutable struct ParallelTree{NDIMS, RealT <: Real} <: AbstractTree{NDIMS}
29+
const capacity::Int
30+
length::Int
31+
2932
parent_ids::Vector{Int}
3033
child_ids::Matrix{Int}
3134
neighbor_ids::Matrix{Int}
3235
levels::Vector{Int}
3336
coordinates::Matrix{RealT}
3437
original_cell_ids::Vector{Int}
35-
mpi_ranks::Vector{Int}
36-
37-
capacity::Int
38-
length::Int
39-
dummy::Int
4038

4139
center_level_0::SVector{NDIMS, RealT}
4240
length_level_0::RealT
4341
periodicity::NTuple{NDIMS, Bool}
4442

43+
mpi_ranks::Vector{Int} # Addition compared to `SerialTree`
44+
4545
function ParallelTree{NDIMS, RealT}(capacity::Integer) where {NDIMS, RealT <: Real}
46-
# Verify that NDIMS is an integer
4746
@assert NDIMS isa Integer
4847

49-
# Create instance
50-
t = new()
51-
52-
# Initialize fields with defaults
53-
# Note: length as capacity + 1 is to use `capacity + 1` as temporary storage for swap operations
54-
t.parent_ids = fill(typemin(Int), capacity + 1)
55-
t.child_ids = fill(typemin(Int), 2^NDIMS, capacity + 1)
56-
t.neighbor_ids = fill(typemin(Int), 2 * NDIMS, capacity + 1)
57-
t.levels = fill(typemin(Int), capacity + 1)
58-
t.coordinates = fill(convert(RealT, NaN), NDIMS, capacity + 1) # `NaN` is of type Float64
59-
t.original_cell_ids = fill(typemin(Int), capacity + 1)
60-
t.mpi_ranks = fill(typemin(Int), capacity + 1)
61-
62-
t.capacity = capacity
63-
t.length = 0
64-
t.dummy = capacity + 1
65-
66-
t.center_level_0 = SVector(ntuple(_ -> convert(RealT, NaN), NDIMS))
67-
t.length_level_0 = convert(RealT, NaN)
68-
69-
return t
48+
parent_ids = fill(typemin(Int), capacity + 1)
49+
child_ids = fill(typemin(Int), 2^NDIMS, capacity + 1)
50+
neighbor_ids = fill(typemin(Int), 2 * NDIMS, capacity + 1)
51+
levels = fill(typemin(Int), capacity + 1)
52+
coordinates = fill(convert(RealT, NaN), NDIMS, capacity + 1)
53+
original_cell_ids = fill(typemin(Int), capacity + 1)
54+
mpi_ranks = fill(typemin(Int), capacity + 1)
55+
56+
center_level_0 = SVector(ntuple(_ -> convert(RealT, NaN), NDIMS))
57+
length_level_0 = convert(RealT, NaN)
58+
periodicity = ntuple(_ -> false, NDIMS)
59+
60+
return new(capacity, 0, # length
61+
parent_ids, child_ids, neighbor_ids,
62+
levels, coordinates, original_cell_ids,
63+
center_level_0, length_level_0,
64+
periodicity, mpi_ranks)
7065
end
7166
end
7267

@@ -156,7 +151,6 @@ function Base.show(io::IO, ::MIME"text/plain", t::ParallelTree)
156151
println(io, "t.mpi_ranks[1:l] = $(t.mpi_ranks[1:l])")
157152
println(io, "t.capacity = $(t.capacity)")
158153
println(io, "t.length = $(t.length)")
159-
println(io, "t.dummy = $(t.dummy)")
160154
println(io, "t.center_level_0 = $(t.center_level_0)")
161155
println(io, "t.length_level_0 = $(t.length_level_0)")
162156
println(io, '*'^20)

src/meshes/serial_tree.jl

Lines changed: 19 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -26,45 +26,39 @@
2626
# way. Also, depth-first ordering *might* not by guaranteed during
2727
# refinement/coarsening operations.
2828
mutable struct SerialTree{NDIMS, RealT <: Real} <: AbstractTree{NDIMS}
29+
const capacity::Int
30+
length::Int
31+
2932
parent_ids::Vector{Int}
3033
child_ids::Matrix{Int}
3134
neighbor_ids::Matrix{Int}
3235
levels::Vector{Int}
3336
coordinates::Matrix{RealT}
3437
original_cell_ids::Vector{Int}
3538

36-
capacity::Int
37-
length::Int
38-
dummy::Int
39-
4039
center_level_0::SVector{NDIMS, RealT}
4140
length_level_0::RealT
4241
periodicity::NTuple{NDIMS, Bool}
4342

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

48-
# Create instance
49-
t = new()
50-
51-
# Initialize fields with defaults
52-
# Note: length as capacity + 1 is to use `capacity + 1` as temporary storage for swap operations
53-
t.parent_ids = fill(typemin(Int), capacity + 1)
54-
t.child_ids = fill(typemin(Int), 2^NDIMS, capacity + 1)
55-
t.neighbor_ids = fill(typemin(Int), 2 * NDIMS, capacity + 1)
56-
t.levels = fill(typemin(Int), capacity + 1)
57-
t.coordinates = fill(convert(RealT, NaN), NDIMS, capacity + 1) # `NaN` is of type Float64
58-
t.original_cell_ids = fill(typemin(Int), capacity + 1)
59-
60-
t.capacity = capacity
61-
t.length = 0
62-
t.dummy = capacity + 1
63-
64-
t.center_level_0 = SVector(ntuple(_ -> convert(RealT, NaN), NDIMS))
65-
t.length_level_0 = convert(RealT, NaN)
66-
67-
return t
46+
parent_ids = fill(typemin(Int), capacity + 1)
47+
child_ids = fill(typemin(Int), 2^NDIMS, capacity + 1)
48+
neighbor_ids = fill(typemin(Int), 2 * NDIMS, capacity + 1)
49+
levels = fill(typemin(Int), capacity + 1)
50+
coordinates = fill(convert(RealT, NaN), NDIMS, capacity + 1)
51+
original_cell_ids = fill(typemin(Int), capacity + 1)
52+
53+
center_level_0 = SVector(ntuple(_ -> convert(RealT, NaN), NDIMS))
54+
length_level_0 = convert(RealT, NaN)
55+
periodicity = ntuple(_ -> false, NDIMS)
56+
57+
return new(capacity, 0, # length
58+
parent_ids, child_ids, neighbor_ids,
59+
levels, coordinates, original_cell_ids,
60+
center_level_0, length_level_0,
61+
periodicity)
6862
end
6963
end
7064

@@ -152,7 +146,6 @@ function Base.show(io::IO, ::MIME"text/plain", t::SerialTree)
152146
println(io, "t.original_cell_ids[1:l] = $(t.original_cell_ids[1:l])")
153147
println(io, "t.capacity = $(t.capacity)")
154148
println(io, "t.length = $(t.length)")
155-
println(io, "t.dummy = $(t.dummy)")
156149
println(io, "t.center_level_0 = $(t.center_level_0)")
157150
println(io, "t.length_level_0 = $(t.length_level_0)")
158151
println(io, '*'^20)

src/meshes/tree_mesh.jl

Lines changed: 17 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ mutable struct TreeMesh{NDIMS, TreeType <: AbstractTree{NDIMS}, RealT <: Real} <
2929
tree::TreeType
3030
current_filename::String
3131
unsaved_changes::Bool
32+
# These are needed for distributed memory (i.e., MPI) parallelization
3233
first_cell_by_rank::OffsetVector{Int, Vector{Int}}
3334
n_cells_by_rank::OffsetVector{Int, Vector{Int}}
3435

@@ -37,15 +38,14 @@ mutable struct TreeMesh{NDIMS, TreeType <: AbstractTree{NDIMS}, RealT <: Real} <
3738
AbstractTree{NDIMS},
3839
RealT <:
3940
Real}
40-
# Create mesh
41-
m = new()
42-
m.tree = TreeType(n_cells_max)
43-
m.current_filename = ""
44-
m.unsaved_changes = true
45-
m.first_cell_by_rank = OffsetVector(Int[], 0)
46-
m.n_cells_by_rank = OffsetVector(Int[], 0)
47-
48-
return m
41+
tree = TreeType(n_cells_max)
42+
current_filename = ""
43+
unsaved_changes = true
44+
first_cell_by_rank = OffsetVector(Int[], 0)
45+
n_cells_by_rank = OffsetVector(Int[], 0)
46+
47+
return new(tree, current_filename, unsaved_changes,
48+
first_cell_by_rank, n_cells_by_rank)
4949
end
5050

5151
# TODO: Taal refactor, order of important arguments, use of n_cells_max?
@@ -56,17 +56,14 @@ mutable struct TreeMesh{NDIMS, TreeType <: AbstractTree{NDIMS}, RealT <: Real} <
5656
TreeType <:
5757
AbstractTree{NDIMS},
5858
RealT <: Real}
59-
@assert NDIMS isa Integer && NDIMS > 0
60-
61-
# Create mesh
62-
m = new()
63-
m.tree = TreeType(n_cells_max, domain_center, domain_length, periodicity)
64-
m.current_filename = ""
65-
m.unsaved_changes = true
66-
m.first_cell_by_rank = OffsetVector(Int[], 0)
67-
m.n_cells_by_rank = OffsetVector(Int[], 0)
68-
69-
return m
59+
tree = TreeType(n_cells_max, domain_center, domain_length, periodicity)
60+
current_filename = ""
61+
unsaved_changes = true
62+
first_cell_by_rank = OffsetVector(Int[], 0)
63+
n_cells_by_rank = OffsetVector(Int[], 0)
64+
65+
return new(tree, current_filename, unsaved_changes,
66+
first_cell_by_rank, n_cells_by_rank)
7067
end
7168
end
7269

0 commit comments

Comments
 (0)