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

Taal: AMR allocations #228

Merged
merged 16 commits into from
Oct 26, 2020
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
16 changes: 11 additions & 5 deletions src/callbacks/amr.jl
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,13 @@
Performs adaptive mesh refinement (AMR) every `interval` time steps
for a given semidiscretization `semi` using the chosen `controller`.
"""
struct AMRCallback{Controller, Adaptor}
struct AMRCallback{Controller, Adaptor, Cache}
controller::Controller
interval::Int
adapt_initial_conditions::Bool
adapt_initial_conditions_only_refine::Bool
adaptor::Adaptor
amr_cache::Cache
end


Expand All @@ -33,9 +34,13 @@ function AMRCallback(semi, controller, adaptor;
condition = (u, t, integrator) -> false
end

amr_callback = AMRCallback{typeof(controller), typeof(adaptor)}(
to_refine = Int[]
to_coarsen = Int[]
amr_cache = (; to_refine, to_coarsen)

amr_callback = AMRCallback{typeof(controller), typeof(adaptor), typeof(amr_cache)}(
controller, interval, adapt_initial_conditions,
adapt_initial_conditions_only_refine, adaptor)
adapt_initial_conditions_only_refine, adaptor, amr_cache)

DiscreteCallback(condition, amr_callback,
save_positions=(false,false),
Expand Down Expand Up @@ -220,8 +225,9 @@ function (amr_callback::AMRCallback)(u_ode::AbstractVector, mesh::TreeMesh,
@assert axes(lambda) == axes(leaf_cell_ids) ("Indicator (axes = $(axes(lambda))) and leaf cell (axes = $(axes(leaf_cell_ids))) arrays have different axes")
end

to_refine = Int[]
to_coarsen = Int[]
@unpack to_refine, to_coarsen = amr_callback.amr_cache
empty!(to_refine)
empty!(to_coarsen)
for element in eachelement(dg, cache)
controller_value = lambda[element]
if controller_value > 0
Expand Down
54 changes: 26 additions & 28 deletions src/callbacks/amr_dg1d.jl
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,10 @@ function refine!(u_ode::AbstractVector, adaptor, mesh::TreeMesh{1},
# Get new list of leaf cells
leaf_cell_ids = leaf_cells(mesh.tree)

# Initialize new elements container
elements = init_elements(leaf_cell_ids, mesh,
real(dg), nvariables(equations), polydeg(dg))
copy!(cache.elements, elements)
# re-initialize elements container
@unpack elements = cache
resize!(elements, length(leaf_cell_ids))
init_elements!(elements, leaf_cell_ids, mesh, dg.basis.nodes)
@assert nelements(dg, cache) > old_n_elements

resize!(u_ode, nvariables(equations) * nnodes(dg)^ndims(mesh) * nelements(dg, cache))
Expand All @@ -54,16 +54,15 @@ function refine!(u_ode::AbstractVector, adaptor, mesh::TreeMesh{1},
@assert element_id == nelements(dg, cache) + 1 || element_id == nelements(dg, cache) + 2^ndims(mesh) "element_id = $element_id, nelements(dg, cache) = $(nelements(dg, cache))"
end # GC.@preserve old_u_ode

# TODO: Taal performance, allow initializing the stuff in place, making use of resize!
# Initialize new interfaces container
interfaces = init_interfaces(leaf_cell_ids, mesh, elements,
real(dg), nvariables(equations), polydeg(dg))
copy!(cache.interfaces, interfaces)
# re-initialize interfaces container
@unpack interfaces = cache
resize!(interfaces, count_required_interfaces(mesh, leaf_cell_ids))
init_interfaces!(interfaces, elements, mesh)

# Initialize boundaries
boundaries, _ = init_boundaries(leaf_cell_ids, mesh, elements,
real(dg), nvariables(equations), polydeg(dg))
copy!(cache.boundaries, boundaries)
# re-initialize boundaries container
@unpack boundaries = cache
resize!(boundaries, count_required_boundaries(mesh, leaf_cell_ids))
init_boundaries!(boundaries, elements, mesh)

# Sanity check
if isperiodic(mesh.tree)
Expand Down Expand Up @@ -144,10 +143,10 @@ function coarsen!(u_ode::AbstractVector, adaptor, mesh::TreeMesh{1},
# Get new list of leaf cells
leaf_cell_ids = leaf_cells(mesh.tree)

# Initialize new elements container
elements = init_elements(leaf_cell_ids, mesh,
real(dg), nvariables(equations), polydeg(dg))
copy!(cache.elements, elements)
# re-initialize elements container
@unpack elements = cache
resize!(elements, length(leaf_cell_ids))
init_elements!(elements, leaf_cell_ids, mesh, dg.basis.nodes)
@assert nelements(dg, cache) < old_n_elements

resize!(u_ode, nvariables(equations) * nnodes(dg)^ndims(mesh) * nelements(dg, cache))
Expand Down Expand Up @@ -184,16 +183,15 @@ function coarsen!(u_ode::AbstractVector, adaptor, mesh::TreeMesh{1},
@assert element_id == nelements(dg, cache) + 1 "element_id = $element_id, nelements(dg, cache) = $(nelements(dg, cache))"
end # GC.@preserve old_u_ode

# TODO: Taal performance, allow initializing the stuff in place, making use of resize!
# Initialize new interfaces container
interfaces = init_interfaces(leaf_cell_ids, mesh, elements,
real(dg), nvariables(equations), polydeg(dg))
copy!(cache.interfaces, interfaces)
# re-initialize interfaces container
@unpack interfaces = cache
resize!(interfaces, count_required_interfaces(mesh, leaf_cell_ids))
init_interfaces!(interfaces, elements, mesh)

# Initialize boundaries
boundaries, _ = init_boundaries(leaf_cell_ids, mesh, elements,
real(dg), nvariables(equations), polydeg(dg))
copy!(cache.boundaries, boundaries)
# re-initialize boundaries container
@unpack boundaries = cache
resize!(boundaries, count_required_boundaries(mesh, leaf_cell_ids))
init_boundaries!(boundaries, elements, mesh)

# Sanity check
if isperiodic(mesh.tree)
Expand All @@ -205,7 +203,7 @@ end


# TODO: Taal compare performance of different implementations
# Coarsen solution data u for four elements, using L2 projection
# Coarsen solution data u for two elements, using L2 projection
function coarsen_elements!(u::AbstractArray{<:Any,3}, element_id,
old_u, old_element_id,
adaptor::LobattoLegendreAdaptorL2, equations, dg)
Expand Down Expand Up @@ -248,7 +246,7 @@ end
# this method is called when an `ControllerThreeLevel` is constructed
function create_cache(::Type{ControllerThreeLevel}, mesh::TreeMesh{1}, equations, dg::DG, cache)

controller_value = Vector{real(dg)}(undef, nelements(dg, cache))
controller_value = Vector{Int}(undef, nelements(dg, cache))
return (; controller_value)
end

68 changes: 33 additions & 35 deletions src/callbacks/amr_dg2d.jl
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,10 @@ function refine!(u_ode::AbstractVector, adaptor, mesh::TreeMesh{2},
# Get new list of leaf cells
leaf_cell_ids = leaf_cells(mesh.tree)

# Initialize new elements container
elements = init_elements(leaf_cell_ids, mesh,
real(dg), nvariables(equations), polydeg(dg))
copy!(cache.elements, elements)
# re-initialize elements container
@unpack elements = cache
resize!(elements, length(leaf_cell_ids))
init_elements!(elements, leaf_cell_ids, mesh, dg.basis.nodes)
@assert nelements(dg, cache) > old_n_elements

resize!(u_ode, nvariables(equations) * nnodes(dg)^ndims(mesh) * nelements(dg, cache))
Expand All @@ -54,21 +54,20 @@ function refine!(u_ode::AbstractVector, adaptor, mesh::TreeMesh{2},
@assert element_id == nelements(dg, cache) + 1 || element_id == nelements(dg, cache) + 2^ndims(mesh) "element_id = $element_id, nelements(dg, cache) = $(nelements(dg, cache))"
end # GC.@preserve old_u_ode

# TODO: Taal performance, allow initializing the stuff in place, making use of resize!
# Initialize new interfaces container
interfaces = init_interfaces(leaf_cell_ids, mesh, elements,
real(dg), nvariables(equations), polydeg(dg))
copy!(cache.interfaces, interfaces)
# re-initialize interfaces container
@unpack interfaces = cache
resize!(interfaces, count_required_interfaces(mesh, leaf_cell_ids))
init_interfaces!(interfaces, elements, mesh)

# Initialize boundaries
boundaries, _ = init_boundaries(leaf_cell_ids, mesh, elements,
real(dg), nvariables(equations), polydeg(dg))
copy!(cache.boundaries, boundaries)
# re-initialize boundaries container
@unpack boundaries = cache
resize!(boundaries, count_required_boundaries(mesh, leaf_cell_ids))
init_boundaries!(boundaries, elements, mesh)

# Initialize new mortar containers
mortars = init_mortars(leaf_cell_ids, mesh, elements,
real(dg), nvariables(equations), polydeg(dg), dg.mortar)
copy!(cache.mortars, mortars)
# re-initialize mortars container
@unpack mortars = cache
resize!(mortars, count_required_mortars(mesh, leaf_cell_ids))
init_mortars!(mortars, elements, mesh)

# Sanity check
if isperiodic(mesh.tree) && nmortars(mortars) == 0
Expand Down Expand Up @@ -171,10 +170,10 @@ function coarsen!(u_ode::AbstractVector, adaptor, mesh::TreeMesh{2},
# Get new list of leaf cells
leaf_cell_ids = leaf_cells(mesh.tree)

# Initialize new elements container
elements = init_elements(leaf_cell_ids, mesh,
real(dg), nvariables(equations), polydeg(dg))
copy!(cache.elements, elements)
# re-initialize elements container
@unpack elements = cache
resize!(elements, length(leaf_cell_ids))
init_elements!(elements, leaf_cell_ids, mesh, dg.basis.nodes)
@assert nelements(dg, cache) < old_n_elements

resize!(u_ode, nvariables(equations) * nnodes(dg)^ndims(mesh) * nelements(dg, cache))
Expand Down Expand Up @@ -211,21 +210,20 @@ function coarsen!(u_ode::AbstractVector, adaptor, mesh::TreeMesh{2},
@assert element_id == nelements(dg, cache) + 1 "element_id = $element_id, nelements(dg, cache) = $(nelements(dg, cache))"
end # GC.@preserve old_u_ode

# TODO: Taal performance, allow initializing the stuff in place, making use of resize!
# Initialize new interfaces container
interfaces = init_interfaces(leaf_cell_ids, mesh, elements,
real(dg), nvariables(equations), polydeg(dg))
copy!(cache.interfaces, interfaces)
# re-initialize interfaces container
@unpack interfaces = cache
resize!(interfaces, count_required_interfaces(mesh, leaf_cell_ids))
init_interfaces!(interfaces, elements, mesh)

# Initialize boundaries
boundaries, _ = init_boundaries(leaf_cell_ids, mesh, elements,
real(dg), nvariables(equations), polydeg(dg))
copy!(cache.boundaries, boundaries)
# re-initialize boundaries container
@unpack boundaries = cache
resize!(boundaries, count_required_boundaries(mesh, leaf_cell_ids))
init_boundaries!(boundaries, elements, mesh)

# Initialize new mortar containers
mortars = init_mortars(leaf_cell_ids, mesh, elements,
real(dg), nvariables(equations), polydeg(dg), dg.mortar)
copy!(cache.mortars, mortars)
# re-initialize mortars container
@unpack mortars = cache
resize!(mortars, count_required_mortars(mesh, leaf_cell_ids))
init_mortars!(mortars, elements, mesh)

# Sanity check
if isperiodic(mesh.tree) && nmortars(mortars) == 0
Expand Down Expand Up @@ -293,7 +291,7 @@ end
# this method is called when an `ControllerThreeLevel` is constructed
function create_cache(::Type{ControllerThreeLevel}, mesh::TreeMesh{2}, equations, dg::DG, cache)

controller_value = Vector{real(dg)}(undef, nelements(dg, cache))
controller_value = Vector{Int}(undef, nelements(dg, cache))
return (; controller_value)
end

Loading