From d2dcaa7145853a4361a3615e8a104237affd25d6 Mon Sep 17 00:00:00 2001 From: Simon Praetorius Date: Mon, 9 Sep 2019 23:21:26 +0200 Subject: [PATCH 1/2] Add range over node indices --- src/amdis/functions/NodeIndices.hpp | 28 ++++++++++++++++++++++++++++ src/amdis/functions/Nodes.hpp | 12 ++++++++++++ 2 files changed, 40 insertions(+) create mode 100644 src/amdis/functions/NodeIndices.hpp diff --git a/src/amdis/functions/NodeIndices.hpp b/src/amdis/functions/NodeIndices.hpp new file mode 100644 index 00000000..2e4710f7 --- /dev/null +++ b/src/amdis/functions/NodeIndices.hpp @@ -0,0 +1,28 @@ +#pragma once + +#include + +#include +#include + +namespace AMDiS +{ + /// Returns a range over the DOF indices on a node, given by the localView + template + auto nodeIndices(LocalView const& localView, Node const& node) + { + return mappedRangeView(Dune::range(node.size()), [&](std::size_t j) { + return flatMultiIndex(localView.index(node.localIndex(j))); + }); + } + + /// Returns a range over the DOF indices on the basis tree, given by the localView + template + auto nodeIndices(LocalView const& localView) + { + return mappedRangeView(Dune::range(localView.size()), [&](std::size_t i) { + return flatMultiIndex(localView.index(i)); + }); + } + +} // end namespace AMDiS diff --git a/src/amdis/functions/Nodes.hpp b/src/amdis/functions/Nodes.hpp index 5a247083..79cea5e7 100644 --- a/src/amdis/functions/Nodes.hpp +++ b/src/amdis/functions/Nodes.hpp @@ -34,4 +34,16 @@ namespace AMDiS #endif } + // dune version independent creation of node from preBasis + template + auto makeNodeIndexSet(PB const& preBasis, TP const& treePath) + { +#if DUNE_VERSION_LT(DUNE_FUNCTIONS,2,7) + return preBasis.indexSet(treePath); +#else + DUNE_UNUSED_PARAMETER(treePath); + return preBasis.makeIndexSet(); +#endif + } + } // end namespace AMDiS -- GitLab From 6f44dd6d4dcabd6941efbde53b0429cb56c49b34 Mon Sep 17 00:00:00 2001 From: Simon Praetorius Date: Mon, 23 Sep 2019 13:23:02 +0200 Subject: [PATCH 2/2] add nodeIndexCount and test of nodeIndex range --- src/amdis/functions/NodeIndices.hpp | 31 ++++++++++-- test/CMakeLists.txt | 3 ++ test/NodeIndicesTest.cpp | 73 +++++++++++++++++++++++++++++ 3 files changed, 103 insertions(+), 4 deletions(-) create mode 100644 test/NodeIndicesTest.cpp diff --git a/src/amdis/functions/NodeIndices.hpp b/src/amdis/functions/NodeIndices.hpp index 2e4710f7..98fd9463 100644 --- a/src/amdis/functions/NodeIndices.hpp +++ b/src/amdis/functions/NodeIndices.hpp @@ -1,28 +1,51 @@ #pragma once #include +#include #include #include namespace AMDiS { - /// Returns a range over the DOF indices on a node, given by the localView + /// Returns a range over (flat) DOF indices on a node, given by the localView template auto nodeIndices(LocalView const& localView, Node const& node) { - return mappedRangeView(Dune::range(node.size()), [&](std::size_t j) { + using namespace Dune::Functions; + static_assert(Dune::models, LocalView>(), ""); + static_assert(Dune::models, Node>(), ""); + + return mappedRangeView(Dune::range(node.size()), [&](std::size_t j) -> std::size_t { return flatMultiIndex(localView.index(node.localIndex(j))); }); } - /// Returns a range over the DOF indices on the basis tree, given by the localView + /// Returns a range over (flat) DOF indices on the basis tree, given by the localView template auto nodeIndices(LocalView const& localView) { - return mappedRangeView(Dune::range(localView.size()), [&](std::size_t i) { + using namespace Dune::Functions; + static_assert(Dune::models, LocalView>(), ""); + + return mappedRangeView(Dune::range(localView.size()), [&](std::size_t i) -> std::size_t { return flatMultiIndex(localView.index(i)); }); } + + /// Returns the number of DOF indices on a node, given by the localView + template + std::size_t nodeIndexCount(LocalView const& /*localView*/, Node const& node) + { + return node.size(); + } + + /// Returns the number of DOF indices on the basis tree, given by the localView + template + std::size_t nodeIndexCount(LocalView const& localView) + { + return localView.size(); + } + } // end namespace AMDiS diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index d5e76ab8..190de9c1 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -84,6 +84,9 @@ dune_add_test(SOURCES MultiTypeVectorTest.cpp dune_add_test(SOURCES MultiTypeMatrixTest.cpp LINK_LIBRARIES amdis) +dune_add_test(SOURCES NodeIndicesTest.cpp + LINK_LIBRARIES amdis) + dune_add_test(SOURCES OperationsTest.cpp LINK_LIBRARIES amdis) diff --git a/test/NodeIndicesTest.cpp b/test/NodeIndicesTest.cpp new file mode 100644 index 00000000..b180d941 --- /dev/null +++ b/test/NodeIndicesTest.cpp @@ -0,0 +1,73 @@ +#include +#include +#include +#include + +#include +#include "Tests.hpp" + +int main() +{ + using namespace AMDiS; + using namespace Dune::Functions::BasisFactory; + + // create grid + Dune::YaspGrid<2> grid({1.0, 1.0}, {1, 1}); + auto gridView = grid.leafGridView(); + + // create basis + auto taylorHoodBasis = makeBasis( + gridView, + composite( + power<2>(lagrange<2>(), flatInterleaved()), + lagrange<1>(), + flatLexicographic() + )); + + auto localView = taylorHoodBasis.localView(); + + for (auto const& e : elements(gridView)) { + localView.bind(e); + + std::size_t numDofs = 2*(3*3) + (2*2); + std::size_t numNodeIndices = nodeIndexCount(localView); + AMDIS_TEST_EQ(numNodeIndices, numDofs); + + std::size_t num = 0; + for (std::size_t dof : nodeIndices(localView)) { + DUNE_UNUSED_PARAMETER(dof); + num++; + } + AMDIS_TEST_EQ(num, numDofs); + + auto node = localView.tree(); + numNodeIndices = nodeIndexCount(localView, node); + AMDIS_TEST_EQ(numNodeIndices, numDofs); + + // count the number of velocity dofs + std::size_t numVelDofs = 2*(3*3); + auto v_node = Dune::TypeTree::child(node, Dune::Indices::_0); + std::size_t numVelNodeIndices = nodeIndexCount(localView, v_node); + AMDIS_TEST_EQ(numVelNodeIndices, numVelDofs); + num = 0; + for (std::size_t dof : nodeIndices(localView, v_node)) { + DUNE_UNUSED_PARAMETER(dof); + num++; + } + AMDIS_TEST_EQ(num, numVelDofs); + + // count the number of pressure dofs + std::size_t numPDofs = 2*2; + auto p_node = Dune::TypeTree::child(node, Dune::Indices::_1); + std::size_t numPNodeIndices = nodeIndexCount(localView, p_node); + AMDIS_TEST_EQ(numPNodeIndices, numPDofs); + num = 0; + for (std::size_t dof : nodeIndices(localView, p_node)) { + DUNE_UNUSED_PARAMETER(dof); + num++; + } + AMDIS_TEST_EQ(num, numPDofs); + } + + return report_errors(); +} -- GitLab