| 1 |
// @HEADER |
| 2 |
// ***************************************************************************** |
| 3 |
// MueLu: A package for multigrid based preconditioning |
| 4 |
// |
| 5 |
// Copyright 2012 NTESS and the MueLu contributors. |
| 6 |
// SPDX-License-Identifier: BSD-3-Clause |
| 7 |
// ***************************************************************************** |
| 8 |
// @HEADER |
| 9 |
|
| 10 |
/* |
| 11 |
* Xpetra_Level_def.hpp |
| 12 |
* |
| 13 |
* Created on: August 17, 2017 |
| 14 |
* Author: Massimiliano Lupo Pasini ([email protected]) |
| 15 |
*/ |
| 16 |
#ifndef XPETRA_LEVEL_DEF_HPP |
| 17 |
#define XPETRA_LEVEL_DEF_HPP |
| 18 |
|
| 19 |
#include "Xpetra_Level_decl.hpp" |
| 20 |
|
| 21 |
namespace Xpetra { |
| 22 |
|
| 23 |
template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node> |
| 24 |
Level<Scalar, LocalOrdinal, GlobalOrdinal, Node>::Level(int levelID, int num_regions) |
| 25 |
: levelID_(levelID) |
| 26 |
, num_regions_(num_regions) {} |
| 27 |
|
| 28 |
template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node> |
| 29 |
void Level<Scalar, LocalOrdinal, GlobalOrdinal, Node>::SetP(Array<RCP<matrix_type> >& P) { |
| 30 |
TEUCHOS_TEST_FOR_EXCEPTION(regionP_.size() != 0, Exceptions::RuntimeError, "Current level already has prolongators \n"); |
| 31 |
TEUCHOS_TEST_FOR_EXCEPTION(P.size() != num_regions_, Exceptions::RuntimeError, "Number of region prolongators is " << P.size() << "and does not math the number of regions which is " << num_regions_ << " \n"); |
| 32 |
regionP_ = P; |
| 33 |
} |
| 34 |
|
| 35 |
template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node> |
| 36 |
void Level<Scalar, LocalOrdinal, GlobalOrdinal, Node>::SetR(Array<RCP<matrix_type> >& R) { |
| 37 |
TEUCHOS_TEST_FOR_EXCEPTION(regionR_.size() != 0, Exceptions::RuntimeError, "Current level already has restrictions \n"); |
| 38 |
TEUCHOS_TEST_FOR_EXCEPTION(R.size() != num_regions_, Exceptions::RuntimeError, "Number of region restrictions is " << R.size() << "and does not math the number of regions which is " << num_regions_ << " \n"); |
| 39 |
regionR_ = R; |
| 40 |
} |
| 41 |
|
| 42 |
template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node> |
| 43 |
void Level<Scalar, LocalOrdinal, GlobalOrdinal, Node>::SetA(Array<RCP<matrix_type> >& A) { |
| 44 |
TEUCHOS_TEST_FOR_EXCEPTION(regionA_.size() != 0, Exceptions::RuntimeError, "Current level already has operators \n"); |
| 45 |
TEUCHOS_TEST_FOR_EXCEPTION(A.size() != num_regions_, Exceptions::RuntimeError, "Number of region operators is " << A.size() << "and does not math the number of regions which is " << num_regions_ << " \n"); |
| 46 |
regionA_ = A; |
| 47 |
} |
| 48 |
|
| 49 |
template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node> |
| 50 |
void Level<Scalar, LocalOrdinal, GlobalOrdinal, Node>::SetSmoother(Array<RCP<multivector_type> >& S) { |
| 51 |
TEUCHOS_TEST_FOR_EXCEPTION(regionSmoother_.size() != 0, Exceptions::RuntimeError, "Current level already has smoothers \n"); |
| 52 |
TEUCHOS_TEST_FOR_EXCEPTION(S.size() != num_regions_, Exceptions::RuntimeError, "Number of region smoothers is " << S.size() << "and does not math the number of regions which is " << num_regions_ << " \n"); |
| 53 |
regionSmoother_ = S; |
| 54 |
} |
| 55 |
|
| 56 |
template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node> |
| 57 |
void Level<Scalar, LocalOrdinal, GlobalOrdinal, Node>::SetRegionToAll(Array<Array<std::tuple<GlobalOrdinal, GlobalOrdinal> > > regionToAll) { |
| 58 |
TEUCHOS_TEST_FOR_EXCEPTION(regionToAll.size() != num_regions_, Exceptions::RuntimeError, "Passed regionToAll has number of regions equal to " << regionToAll.size() << "which does not match the number of regions in level ID: <<" << levelID_ << " with number of regions equal to " << num_regions_ << "\n"); |
| 59 |
level_regionToAll_ = regionToAll; |
| 60 |
} |
| 61 |
|
| 62 |
template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node> |
| 63 |
int Level<Scalar, LocalOrdinal, GlobalOrdinal, Node>::GetNumRegions() const { |
| 64 |
TEUCHOS_TEST_FOR_EXCEPTION(num_regions_ == 0, Exceptions::RuntimeError, "level ID: <<" << levelID_ << " does NOT have defined regions \n"); |
| 65 |
return num_regions_; |
| 66 |
} |
| 67 |
|
| 68 |
template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node> |
| 69 |
Array<Array<std::tuple<GlobalOrdinal, GlobalOrdinal> > > Level<Scalar, LocalOrdinal, GlobalOrdinal, Node>::GetRegionToAll() const { |
| 70 |
TEUCHOS_TEST_FOR_EXCEPTION(level_regionToAll_.size() == 0, Exceptions::RuntimeError, "level ID: " << levelID_ << " does NOT have level_regionToAll_ initialized yet \n"); |
| 71 |
return level_regionToAll_; |
| 72 |
} |
| 73 |
|
| 74 |
template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node> |
| 75 |
GlobalOrdinal Level<Scalar, LocalOrdinal, GlobalOrdinal, Node>::GetCompositeIndex(int region_idx, GlobalOrdinal region_node_idx) const { |
| 76 |
TEUCHOS_TEST_FOR_EXCEPTION(level_regionToAll_.size() == 0, Exceptions::RuntimeError, "level ID: " << levelID_ << " does NOT have level_regionToAll_ initialized yet \n"); |
| 77 |
TEUCHOS_TEST_FOR_EXCEPTION(level_regionToAll_.size() != num_regions_, Exceptions::RuntimeError, "level ID: " << levelID_ << " has information stored for a number of regions that does NOT match with the declared number of regions \n"); |
| 78 |
TEUCHOS_TEST_FOR_EXCEPTION(region_idx >= num_regions_, Exceptions::RuntimeError, "level ID: " << levelID_ << " Invalid region index \n"); |
| 79 |
|
| 80 |
GlobalOrdinal composite_index = -1; |
| 81 |
|
| 82 |
checkerRegionToAll<GlobalOrdinal> unaryPredicate(region_node_idx); |
| 83 |
typename Array<std::tuple<GlobalOrdinal, GlobalOrdinal> >::iterator global_iterator; |
| 84 |
Array<std::tuple<GlobalOrdinal, GlobalOrdinal> > regionToAll = level_regionToAll_[region_idx]; |
| 85 |
global_iterator = std::find_if<typename Array<std::tuple<GlobalOrdinal, GlobalOrdinal> >::iterator, checkerRegionToAll<GlobalOrdinal> >(regionToAll.begin(), regionToAll.end(), unaryPredicate); |
| 86 |
TEUCHOS_TEST_FOR_EXCEPTION(global_iterator == level_regionToAll_[region_idx].end(), Exceptions::RuntimeError, " - Region: " << region_idx << " - " |
| 87 |
<< " node with region index: " << region_idx << " is not in regionToAll[" << region_idx << "]" |
| 88 |
<< "\n"); |
| 89 |
composite_index = std::get<1>(*global_iterator); |
| 90 |
|
| 91 |
return composite_index; |
| 92 |
} |
| 93 |
|
| 94 |
template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node> |
| 95 |
void Level<Scalar, LocalOrdinal, GlobalOrdinal, Node>::checkConsistency() const { |
| 96 |
TEUCHOS_TEST_FOR_EXCEPTION(num_regions_ <= 0, Exceptions::RuntimeError, "level ID: " << levelID_ << " does not have any regions \n"); |
| 97 |
TEUCHOS_TEST_FOR_EXCEPTION(num_regions_ != regionA_.size(), Exceptions::RuntimeError, "level ID: " << levelID_ << " - number of region matrices " << regionA_.size() << " does NOT match the number of regions " << num_regions_ << "\n"); |
| 98 |
TEUCHOS_TEST_FOR_EXCEPTION(num_regions_ != level_regionToAll_.size(), Exceptions::RuntimeError, "level ID: " << levelID_ << " - size of level_regionToAll_ " << level_regionToAll_.size() << " does NOT match the number of regions " << num_regions_ << "\n"); |
| 99 |
|
| 100 |
if (levelID_ > 0) { |
| 101 |
TEUCHOS_TEST_FOR_EXCEPTION(num_regions_ != regionP_.size(), Exceptions::RuntimeError, "level ID: " << levelID_ << " - number of prolongators " << regionP_.size() << " does NOT match the number of regions " << num_regions_ << "\n"); |
| 102 |
TEUCHOS_TEST_FOR_EXCEPTION(num_regions_ != regionR_.size(), Exceptions::RuntimeError, "level ID: " << levelID_ << " - number of restrictions " << regionR_.size() << " does NOT match the number of regions " << num_regions_ << "\n"); |
| 103 |
} |
| 104 |
|
| 105 |
for (int region_idx = 0; region_idx < num_regions_; ++region_idx) { |
| 106 |
if (levelID_ > 0) { |
| 107 |
TEUCHOS_TEST_FOR_EXCEPTION(regionP_[region_idx]->getGlobalNumCols() != regionA_[region_idx]->getGlobalNumCols(), Exceptions::RuntimeError, "level ID: " << levelID_ << " - Region: " << region_idx << " , numCols(P) = " << regionP_[region_idx]->getGlobalNumCols() << " BUT numCols(A) = " << regionA_[region_idx]->getGlobalNumCols() << "\n"); |
| 108 |
TEUCHOS_TEST_FOR_EXCEPTION(regionR_[region_idx]->getGlobalNumRows() != regionA_[region_idx]->getGlobalNumCols(), Exceptions::RuntimeError, "level ID: " << levelID_ << " - Region: " << region_idx << " , numRows(R) = " << regionR_[region_idx]->getGlobalNumRows() << " BUT numCols(A) = " << regionA_[region_idx]->getGlobalNumCols() << "\n"); |
| 109 |
} |
| 110 |
|
| 111 |
TEUCHOS_TEST_FOR_EXCEPTION(regionA_[region_idx]->getRowMap()->getLocalNumElements() > 0 && level_regionToAll_[region_idx].size() != regionA_[region_idx]->getGlobalNumRows(), Exceptions::RuntimeError, "Process ID: " << regionA_[region_idx]->getRowMap()->getComm()->getRank() << " - level ID: " << levelID_ << " - Region: " << region_idx << " , size(regionToAll) = " << level_regionToAll_[region_idx].size() << " BUT numCols(A) = " << regionA_[region_idx]->getGlobalNumCols() << "\n"); |
| 112 |
} |
| 113 |
} |
| 114 |
|
| 115 |
template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node> |
| 116 |
void Level<Scalar, LocalOrdinal, GlobalOrdinal, Node>::ComputeRegionJacobi() { |
| 117 |
regionSmoother_.resize(num_regions_); |
| 118 |
|
| 119 |
for (int i = 0; i < num_regions_; ++i) { |
| 120 |
regionSmoother_[i] = VectorFactory<Scalar, LocalOrdinal, GlobalOrdinal, Node>::Build(regionA_[i]->getRowMap()); |
| 121 |
regionA_[i]->getLocalDiagCopy(*(regionSmoother_[i])); |
| 122 |
} |
| 123 |
} |
| 124 |
|
| 125 |
} // namespace Xpetra |
| 126 |
#endif |