From a70522f69ed69fc5aa4c6ca5654fcad8d31cb287 Mon Sep 17 00:00:00 2001 From: Erik Faulhaber <44124897+efaulhaber@users.noreply.github.com> Date: Fri, 21 Jun 2024 20:32:39 +0200 Subject: [PATCH] Avoid type instability in `SemidiscretizationCoupled` (#1979) * Avoid type instability in `SemidiscretizationCoupled` * Remove all allocations --------- Co-authored-by: Michael Schlottke-Lakemper Co-authored-by: Simon Candelaresi <10759273+SimonCan@users.noreply.github.com> Co-authored-by: Hendrik Ranocha --- .../semidiscretization_coupled.jl | 32 ++++++------------- 1 file changed, 10 insertions(+), 22 deletions(-) diff --git a/src/semidiscretization/semidiscretization_coupled.jl b/src/semidiscretization/semidiscretization_coupled.jl index 4843f211089..44aae912c27 100644 --- a/src/semidiscretization/semidiscretization_coupled.jl +++ b/src/semidiscretization/semidiscretization_coupled.jl @@ -429,12 +429,11 @@ BoundaryConditionCoupled(2, (:j, :i_backwards, :end), Float64, fun) !!! warning "Experimental code" This is an experimental feature and can change any time. """ -mutable struct BoundaryConditionCoupled{NDIMS, NDIMST2M1, uEltype <: Real, Indices, - CouplingConverter} +mutable struct BoundaryConditionCoupled{NDIMS, other_semi_index, NDIMST2M1, + uEltype <: Real, Indices, CouplingConverter} # NDIMST2M1 == NDIMS * 2 - 1 # Buffer for boundary values: [variable, nodes_i, nodes_j, cell_i, cell_j] u_boundary :: Array{uEltype, NDIMST2M1} # NDIMS * 2 - 1 - other_semi_index :: Int other_orientation :: Int indices :: Indices coupling_converter :: CouplingConverter @@ -452,9 +451,9 @@ mutable struct BoundaryConditionCoupled{NDIMS, NDIMST2M1, uEltype <: Real, Indic other_orientation = 3 end - new{NDIMS, NDIMS * 2 - 1, uEltype, typeof(indices), + new{NDIMS, other_semi_index, NDIMS * 2 - 1, uEltype, typeof(indices), typeof(coupling_converter)}(u_boundary, - other_semi_index, other_orientation, + other_orientation, indices, coupling_converter) end end @@ -558,28 +557,17 @@ function copy_to_coupled_boundary!(boundary_conditions::Union{Tuple, NamedTuple} boundary_conditions...) end -function mesh_equations_solver_cache(other_semi_index, i, semi_, semi_tuple...) - if i == other_semi_index - return mesh_equations_solver_cache(semi_) - else - # Walk through semidiscretizations until we find `i` - mesh_equations_solver_cache(other_semi_index, i + 1, semi_tuple...) - end -end - # In 2D -function copy_to_coupled_boundary!(boundary_condition::BoundaryConditionCoupled{2}, - u_ode, - semi_coupled, semi) +function copy_to_coupled_boundary!(boundary_condition::BoundaryConditionCoupled{2, + other_semi_index}, + u_ode, semi_coupled, semi) where {other_semi_index} @unpack u_indices = semi_coupled - @unpack other_semi_index, other_orientation, indices = boundary_condition + @unpack other_orientation, indices = boundary_condition @unpack coupling_converter, u_boundary = boundary_condition mesh_own, equations_own, solver_own, cache_own = mesh_equations_solver_cache(semi) - - mesh_other, equations_other, solver_other, cache_other = mesh_equations_solver_cache(other_semi_index, - 1, - semi_coupled.semis...) + other_semi = semi_coupled.semis[other_semi_index] + mesh_other, equations_other, solver_other, cache_other = mesh_equations_solver_cache(other_semi) node_coordinates_other = cache_other.elements.node_coordinates u_ode_other = get_system_u_ode(u_ode, other_semi_index, semi_coupled)