// SPDX-FileCopyrightText: Copyright © DUNE Project contributors, see file LICENSE.md in module root // SPDX-License-Identifier: LicenseRef-GPL-2.0-only-with-DUNE-exception // -*- tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- // vi: set et ts=4 sw=2 sts=2: #ifndef DUNE_ISTL_BLOCKLEVEL_HH #define DUNE_ISTL_BLOCKLEVEL_HH #include #include #include #include #include /*! * \file * \brief Helper functions for determining the vector/matrix block level */ // forward declaration namespace Dune { template class MultiTypeBlockVector; template class MultiTypeBlockMatrix; } // end namespace Dune namespace Dune { namespace Impl { // forward declaration template struct MaxBlockLevel; template struct MinBlockLevel; //! recursively determine block level of a MultiTypeBlockMatrix template typename BlockLevel, typename Op> constexpr std::size_t blockLevelMultiTypeBlockMatrix(const Op& op) { // inialize with zeroth diagonal block using namespace Dune::Indices; using Block00 = typename std::decay_t()[_0][_0])>; std::size_t blockLevel = BlockLevel::value() + 1; // iterate over all blocks to determine min/max block level using namespace Dune::Hybrid; forEach(integralRange(index_constant()), [&](auto&& i) { using namespace Dune::Hybrid; // needed for icc, see issue #31 forEach(integralRange(index_constant()), [&](auto&& j) { using Block = typename std::decay_t()[i][j])>; blockLevel = op(blockLevel, BlockLevel::value() + 1); }); }); return blockLevel; } //! recursively determine block level of a MultiTypeBlockVector template typename BlockLevel, typename Op> constexpr std::size_t blockLevelMultiTypeBlockVector(const Op& op) { // inialize with zeroth block using namespace Dune::Indices; using Block0 = typename std::decay_t()[_0])>; std::size_t blockLevel = BlockLevel::value() + 1; // iterate over all blocks to determine min/max block level using namespace Dune::Hybrid; forEach(integralRange(index_constant()), [&](auto&& i) { using Block = typename std::decay_t()[i])>; blockLevel = op(blockLevel, BlockLevel::value() + 1); }); return blockLevel; } template struct MaxBlockLevel { static constexpr std::size_t value(){ if constexpr (IsNumber::value) return 0; else return MaxBlockLevel::value() + 1; } }; template struct MinBlockLevel { // the default implementation assumes minBlockLevel == maxBlockLevel static constexpr std::size_t value() { return MaxBlockLevel::value(); } }; // max block level for MultiTypeBlockMatrix template struct MaxBlockLevel> { static constexpr std::size_t value() { using M = MultiTypeBlockMatrix; constexpr auto max = [](const auto& a, const auto& b){ return std::max(a,b); }; return blockLevelMultiTypeBlockMatrix(max); } }; // min block level for MultiTypeBlockMatrix template struct MinBlockLevel> { static constexpr std::size_t value() { using M = MultiTypeBlockMatrix; constexpr auto min = [](const auto& a, const auto& b){ return std::min(a,b); }; return blockLevelMultiTypeBlockMatrix(min); } }; // max block level for MultiTypeBlockVector template struct MaxBlockLevel> { static constexpr std::size_t value() { using V = MultiTypeBlockVector; constexpr auto max = [](const auto& a, const auto& b){ return std::max(a,b); }; return blockLevelMultiTypeBlockVector(max); } }; // min block level for MultiTypeBlockVector template struct MinBlockLevel> { static constexpr std::size_t value() { using V = MultiTypeBlockVector; constexpr auto min = [](const auto& a, const auto& b){ return std::min(a,b); }; return blockLevelMultiTypeBlockVector(min); } }; // special case: empty MultiTypeBlockVector template<> struct MaxBlockLevel> { static constexpr std::size_t value() { return 0; }; }; // special case: empty MultiTypeBlockVector template<> struct MinBlockLevel> { static constexpr std::size_t value() { return 0; }; }; }} // end namespace Dune::Impl namespace Dune { //! Determine the maximum block level of a possibly nested vector/matrix type template constexpr std::size_t maxBlockLevel() { return Impl::MaxBlockLevel::value(); } //! Determine the minimum block level of a possibly nested vector/matrix type template constexpr std::size_t minBlockLevel() { return Impl::MinBlockLevel::value(); } //! Determine if a vector/matrix has a uniquely determinable block level template constexpr bool hasUniqueBlockLevel() { return maxBlockLevel() == minBlockLevel(); } //! Determine the block level of a possibly nested vector/matrix type template constexpr std::size_t blockLevel() { static_assert(hasUniqueBlockLevel(), "Block level cannot be uniquely determined!"); return Impl::MaxBlockLevel::value(); } } // end namespace Dune #endif