| 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_RegionAMG_def.hpp |
| 12 |
* |
| 13 |
* Created on: August 17, 2017 |
| 14 |
* Author: Massimiliano Lupo Pasini ([email protected]) |
| 15 |
*/ |
| 16 |
#ifndef XPETRA_REGIONAMG_DEF_HPP |
| 17 |
#define XPETRA_REGIONAMG_DEF_HPP |
| 18 |
|
| 19 |
#include "Xpetra_RegionAMG_decl.hpp" |
| 20 |
|
| 21 |
namespace Xpetra { |
| 22 |
|
| 23 |
template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node> |
| 24 |
RegionAMG<Scalar, LocalOrdinal, GlobalOrdinal, Node>::RegionAMG( |
| 25 |
Teuchos::RCP<tpetra_splitting> matrixSplitting, |
| 26 |
Teuchos::RCP<Xpetra::RegionHandler<Scalar, LocalOrdinal, GlobalOrdinal, Node> > regionHandler, |
| 27 |
RCP<const Teuchos::Comm<int> > comm, Teuchos::ParameterList muelu, |
| 28 |
GlobalOrdinal num_levels, GlobalOrdinal coarsening_factor) |
| 29 |
: num_levels_(num_levels) |
| 30 |
, coarsening_factor_(coarsening_factor) |
| 31 |
, comm_(comm) |
| 32 |
, muelu_( |
| 33 |
muelu) { |
| 34 |
TEUCHOS_TEST_FOR_EXCEPT(matrixSplitting.is_null()); |
| 35 |
matrixSplitting_ = matrixSplitting; |
| 36 |
|
| 37 |
// The maps defined here below are used to interface with outer applications (e.g. Belos-type solvers) |
| 38 |
// and guarantee that DomainMap and RangeMap partition input multivectors and output multivectors in a proper way. |
| 39 |
// the maps stored in doaminMap and rangeMap coincide with the maps of the composite matrix |
| 40 |
domainMap_ = matrixSplitting_->getDomainMap(); |
| 41 |
rangeMap_ = matrixSplitting_->getRangeMap(); |
| 42 |
num_regions_ = matrixSplitting_->getNumRegions(); |
| 43 |
|
| 44 |
SetUpHierarchy(); |
| 45 |
} |
| 46 |
|
| 47 |
template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node> |
| 48 |
void RegionAMG<Scalar, LocalOrdinal, GlobalOrdinal, Node>::SetUpHierarchy() { |
| 49 |
TEUCHOS_TEST_FOR_EXCEPTION(num_levels_ <= 0, Exceptions::RuntimeError, "Number of levels must be a positive integer number \n"); |
| 50 |
TEUCHOS_TEST_FOR_EXCEPTION(num_regions_ <= 0, Exceptions::RuntimeError, "No existing regions \n"); |
| 51 |
|
| 52 |
// Creation of the MueLu list for the region multigrid preconditioner |
| 53 |
// It would be ideal to have this parameter list read by an .xml file |
| 54 |
// at the final stage of the implementation. |
| 55 |
// As for now, we hard code it insice of SetUpHierarchy because we want standard setups for almost everything |
| 56 |
RCP<ParameterList> list = rcp(new Teuchos::ParameterList()); |
| 57 |
list->setName("MueLu"); |
| 58 |
|
| 59 |
list->set("verbosity", "low"); |
| 60 |
list->set("number of equations", 1); |
| 61 |
list->set("max levels", num_levels_); |
| 62 |
list->set("multigrid algorithm", "unsmoothed"); |
| 63 |
list->set("smoother: pre or post", "none"); |
| 64 |
list->set("coarse: type", "RELAXATION"); |
| 65 |
list->set("coarse: max size", 16); |
| 66 |
|
| 67 |
GlobalOrdinal num_regions = matrixSplitting_->getNumRegions(); |
| 68 |
regionHierarchies_.resize(num_regions); |
| 69 |
|
| 70 |
// Instantiation of MueLu Hierarchies for each region of the domain |
| 71 |
for (GlobalOrdinal region_idx = 0; region_idx < num_regions; ++region_idx) { |
| 72 |
// The parameter n is the total number of mesh nodes in a region |
| 73 |
GlobalOrdinal n = matrixSplitting_->getRegionHandler()->GetNumRegionNodes(region_idx); |
| 74 |
|
| 75 |
// N.B. The following instructions is based on the assumption that each region is a square. If not, then another instruciton |
| 76 |
// must be used to specify the size of the region mesh along teh x-direction |
| 77 |
GlobalOrdinal nx = std::sqrt(n); |
| 78 |
|
| 79 |
RCP<const map_type> map = matrixSplitting_->getRegionMatrix(region_idx)->getRowMap(); |
| 80 |
size_t NumMyElements = map->getLocalNumElements(); |
| 81 |
Teuchos::ArrayView<const GlobalOrdinal> MyGlobalElements = map->getLocalElementList(); |
| 82 |
|
| 83 |
RCP<multivector_type> coords = mv_factory_type::Build(map, 2); |
| 84 |
|
| 85 |
// Coordinates are created to pass them as additional input the MueLu::Hierarchy constructors |
| 86 |
// The only thing we care aboput is to create a unique mapping between coordinates and degrees of freedom associated with mesh nodes |
| 87 |
// Therefore we base the creation of coordinates upon a fake region mesh, which does not coincide with its real gemetric shape |
| 88 |
// The fake region we use for coordinates is a [0,(number_nodes_x-1)]X[0,(number_nodes_y-1)] rectangle |
| 89 |
Teuchos::ArrayRCP<Teuchos::ArrayRCP<Scalar> > Coord(2); |
| 90 |
Coord[0] = coords->getDataNonConst(0); |
| 91 |
Coord[1] = coords->getDataNonConst(1); |
| 92 |
|
| 93 |
for (size_t i = 0; i < NumMyElements; ++i) { |
| 94 |
GlobalOrdinal ix = MyGlobalElements[i] % nx; |
| 95 |
GlobalOrdinal iy = (MyGlobalElements[i] - ix) / nx; |
| 96 |
|
| 97 |
Coord[0][i] = Teuchos::as<Scalar>(ix); |
| 98 |
Coord[1][i] = Teuchos::as<Scalar>(iy); |
| 99 |
} |
| 100 |
|
| 101 |
// We create as many MueLu::Hierarchy objects as the nubmer of region that partition the composite domain |
| 102 |
regionHierarchies_[region_idx] = MueLu::CreateXpetraPreconditioner<Scalar, LocalOrdinal, GlobalOrdinal, Node>(matrixSplitting_->getRegionMatrix(region_idx), *list, coords); |
| 103 |
|
| 104 |
// // The following commented line here below should replace the line right above if the MueLu parameter list is passed through an .xml file |
| 105 |
// regionHierarchies_[region_idx] = MueLu::CreateXpetraPreconditioner<Scalar,LocalOrdinal,GlobalOrdinal,Node>( matrixSplitting_->getRegionMatrix( region_idx), muelu_, coords ); |
| 106 |
|
| 107 |
// Different regions may have meshes with different size, or the number of levels the user wants to create may be too big |
| 108 |
// with respect to the number of levels MueLu allows (i.e. the minimum coarse size may be reached for a smaller number of levels) |
| 109 |
// In this case, the value of num_levels_ is readjusted to the minimum number of levels instantied across all the regions of the domain |
| 110 |
num_levels_ = std::min(num_levels_, regionHierarchies_[region_idx]->GetNumLevels()); |
| 111 |
} |
| 112 |
|
| 113 |
DefineLevels(); |
| 114 |
} |
| 115 |
|
| 116 |
template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node> |
| 117 |
void RegionAMG<Scalar, LocalOrdinal, GlobalOrdinal, Node>::DefineLevels() { |
| 118 |
levels_.clear(); |
| 119 |
Array<RCP<matrix_type> > P; |
| 120 |
P.clear(); |
| 121 |
Array<RCP<matrix_type> > R; |
| 122 |
R.clear(); |
| 123 |
Array<RCP<matrix_type> > A; |
| 124 |
A.clear(); |
| 125 |
|
| 126 |
TEUCHOS_TEST_FOR_EXCEPTION(levels_.size() != 0, Exceptions::RuntimeError, "Levels structure is already initialized \n"); |
| 127 |
for (int i = 0; i < num_levels_; ++i) { |
| 128 |
P.clear(); |
| 129 |
R.clear(); |
| 130 |
A.clear(); |
| 131 |
|
| 132 |
RCP<level> new_level = rcp(new level(i, num_regions_)); |
| 133 |
for (int region_idx = 0; region_idx < num_regions_; ++region_idx) { |
| 134 |
TEUCHOS_TEST_FOR_EXCEPTION(!regionHierarchies_[region_idx]->GetLevel(i)->IsAvailable("A"), Exceptions::RuntimeError, "No existing operator at level " << i << " of region " << region_idx << "\n"); |
| 135 |
A.push_back(regionHierarchies_[region_idx]->GetLevel(i)->template Get<RCP<matrix_type> >("A")); |
| 136 |
|
| 137 |
// MueLu::Hierarchy objects store prolongator and restriction operators only in levels that are the result of a coarsening (they do not exist at the fine level) |
| 138 |
if (i > 0) { |
| 139 |
TEUCHOS_TEST_FOR_EXCEPTION(!regionHierarchies_[region_idx]->GetLevel(i)->IsAvailable("P"), Exceptions::RuntimeError, "No existing prolongator at level " << i << " of region " << region_idx << "\n"); |
| 140 |
TEUCHOS_TEST_FOR_EXCEPTION(!regionHierarchies_[region_idx]->GetLevel(i)->IsAvailable("R"), Exceptions::RuntimeError, "No existing restriction at level " << i << " of region " << region_idx << "\n"); |
| 141 |
P.push_back(regionHierarchies_[region_idx]->GetLevel(i)->template Get<RCP<matrix_type> >("P")); |
| 142 |
R.push_back(regionHierarchies_[region_idx]->GetLevel(i)->template Get<RCP<matrix_type> >("R")); |
| 143 |
} |
| 144 |
} |
| 145 |
// Following the same policy adopted in MueLu::Hierarchy, prolongator and restriction operators are stored at levels produced with a coarsening |
| 146 |
if (i > 0) { |
| 147 |
new_level->SetP(P); |
| 148 |
new_level->SetR(R); |
| 149 |
} |
| 150 |
|
| 151 |
// The Galerkin operator is define dat every level (regardless of coarsening) |
| 152 |
new_level->SetA(A); |
| 153 |
|
| 154 |
// The structure regionToAll is passed to the fine level as it is |
| 155 |
if (0 == i) |
| 156 |
new_level->SetRegionToAll(matrixSplitting_->getRegionHandler()->GetRegionToAll()); |
| 157 |
else { |
| 158 |
// Coarsening level own a regionToAll structure whcih is the result of an injected coarsened regionToAll from the upper level of the hierarchy |
| 159 |
Array<Array<std::tuple<GlobalOrdinal, GlobalOrdinal> > > coarse_regionToAll; |
| 160 |
regionToAllCoarsen(*(levels_[i - 1]), *new_level); |
| 161 |
} |
| 162 |
|
| 163 |
new_level->checkConsistency(); |
| 164 |
|
| 165 |
// // The following routine should be dedicated to the construction of the region smoother (where it could be possible to define |
| 166 |
// the region smoother as a local visualization of the composite smoother) |
| 167 |
// FIXME ComputeRegionJacobi AS FOR NOW LOCAL JACOBI IS THE ACTUAL REGIONAL SMOOTHER |
| 168 |
// (THIS HAS TO BE CHANGED SO THAT A PORTION OF THE COMPOSITE SMOOTHER IS STORED INSTEAD) |
| 169 |
new_level->ComputeRegionJacobi(); |
| 170 |
|
| 171 |
levels_.push_back(new_level); |
| 172 |
} |
| 173 |
} |
| 174 |
|
| 175 |
template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node> |
| 176 |
void RegionAMG<Scalar, LocalOrdinal, GlobalOrdinal, Node>::regionToAllCoarsen(const level& fine, level& coarse) { |
| 177 |
TEUCHOS_TEST_FOR_EXCEPTION(fine.GetNumRegions() != coarse.GetNumRegions(), Exceptions::RuntimeError, "Level " << fine.GetLevelID() << "has " << fine.GetNumRegions() << " regions instantiated whereas level " << coarse.GetLevelID() << "has " << coarse.GetNumRegions() << " regions instantiated \n"); |
| 178 |
|
| 179 |
Array<Array<std::tuple<GlobalOrdinal, GlobalOrdinal> > > fine_regionToAll = fine.GetRegionToAll(); |
| 180 |
Array<Array<std::tuple<GlobalOrdinal, GlobalOrdinal> > > coarse_regionToAll(num_regions_); |
| 181 |
|
| 182 |
for (GlobalOrdinal region_idx = 0; region_idx < num_regions_; ++region_idx) { |
| 183 |
// Coordinates needed to apply the coarsening |
| 184 |
GlobalOrdinal n = fine.GetNumRegionNodes(region_idx); |
| 185 |
GlobalOrdinal nx = std::sqrt(n); |
| 186 |
GlobalOrdinal ny = nx; |
| 187 |
|
| 188 |
TEUCHOS_TEST_FOR_EXCEPTION((nx - 1) % coarsening_factor_ != 0 || (ny - 1) % coarsening_factor_ != 0, Exceptions::RuntimeError, "Region: " << region_idx << " cannot be coarsened by factor equal to " << coarsening_factor_ << "n: " << n << " - nx: " << nx << " - ny: " << ny << " \n"); |
| 189 |
|
| 190 |
// here below we exploit the fact that the regionToAll has been sorted in ascending order for the region node index |
| 191 |
GlobalOrdinal count = 1; |
| 192 |
|
| 193 |
while (count <= fine_regionToAll[region_idx].size()) { |
| 194 |
coarse_regionToAll[region_idx].push_back(fine_regionToAll[region_idx][count]); |
| 195 |
if (count % ny != 0) |
| 196 |
count = count + coarsening_factor_; |
| 197 |
else |
| 198 |
count = count + (coarsening_factor_ - 1) * ny + 1; |
| 199 |
} |
| 200 |
|
| 201 |
coarse.SetRegionToAll(coarse_regionToAll); |
| 202 |
} |
| 203 |
} |
| 204 |
|
| 205 |
template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node> |
| 206 |
void RegionAMG<Scalar, LocalOrdinal, GlobalOrdinal, Node>::computeRegionX(const multivector_type& X, Array<RCP<multivector_type> > regionX) const { |
| 207 |
// Array to store extended region Maps (needed to copy composite entries of the input vector into region partitioning of it) |
| 208 |
Teuchos::Array<Teuchos::Array<GlobalOrdinal> > overlapping_composite_array; |
| 209 |
|
| 210 |
// Create Overlapping composite maps |
| 211 |
for (int region_idx = 0; region_idx < num_regions_; ++region_idx) { |
| 212 |
Teuchos::Array<GlobalOrdinal> overlapped_composite; |
| 213 |
LocalOrdinal num_elements = regionX[region_idx]->getMap()->getLocalNumElements(); |
| 214 |
for (LocalOrdinal local_region_index = 0; local_region_index < num_elements; ++local_region_index) { |
| 215 |
GlobalOrdinal composite_region_index = regionX[region_idx]->getMap()->getGlobalElement(local_region_index); |
| 216 |
GlobalOrdinal composite_composite_index = levels_[0]->GetCompositeIndex(region_idx, composite_region_index + 1); |
| 217 |
overlapped_composite.push_back(composite_composite_index - 1); |
| 218 |
} |
| 219 |
overlapping_composite_array.push_back(overlapped_composite); |
| 220 |
} |
| 221 |
|
| 222 |
// We split at first the input and output multivectors into region ones |
| 223 |
Array<RCP<multivector_type> > composite_overlapping_X; |
| 224 |
composite_overlapping_X.resize(num_regions_); |
| 225 |
for (int region_idx = 0; region_idx < num_regions_; ++region_idx) { |
| 226 |
// The new Map associated with a process must contain entries currently owned plus new ones previously owned only by neighbouring processes |
| 227 |
Teuchos::Array<GlobalOrdinal> aux; |
| 228 |
if (overlapping_composite_array[region_idx].size() > 0) { |
| 229 |
Teuchos::Array<GlobalOrdinal> aux1 = overlapping_composite_array[region_idx]; |
| 230 |
Teuchos::Array<GlobalOrdinal> aux2 = X.getMap()->getLocalElementList(); |
| 231 |
std::set_union(aux1.begin(), aux1.end(), aux2.begin(), aux2.end(), std::back_inserter(aux)); |
| 232 |
} else { |
| 233 |
aux = X.getMap()->getLocalElementList(); |
| 234 |
} |
| 235 |
RCP<map_type> overlapping_composite_map = MapFactory<LocalOrdinal, GlobalOrdinal, Node>::Build(Xpetra::UseTpetra, X.getGlobalLength(), aux, 0, comm_); |
| 236 |
composite_overlapping_X[region_idx] = MultiVectorFactory<Scalar, LocalOrdinal, GlobalOrdinal, Node>::Build(overlapping_composite_map, X.getNumVectors()); |
| 237 |
RCP<Import<LocalOrdinal, GlobalOrdinal, Node> > Import1 = ImportFactory<LocalOrdinal, GlobalOrdinal, Node>::Build(X.getMap(), overlapping_composite_map); |
| 238 |
TEUCHOS_TEST_FOR_EXCEPTION(X.getMap()->getMinAllGlobalIndex() != composite_overlapping_X[region_idx]->getMap()->getMinAllGlobalIndex(), Exceptions::RuntimeError, "Minimal index in old an new maps do not coincide \n"); |
| 239 |
TEUCHOS_TEST_FOR_EXCEPTION(X.getMap()->getMaxAllGlobalIndex() != composite_overlapping_X[region_idx]->getMap()->getMaxAllGlobalIndex(), Exceptions::RuntimeError, "Maximal index in old an new maps do not coincide \n"); |
| 240 |
composite_overlapping_X[region_idx]->doImport(X, *Import1, Xpetra::INSERT); |
| 241 |
} |
| 242 |
|
| 243 |
// Copy values from composite input multivector X into region multivectors regionX |
| 244 |
for (int i = 0; i < X.getNumVectors(); ++i) { |
| 245 |
for (int region_idx = 0; region_idx < num_regions_; ++region_idx) { |
| 246 |
ArrayRCP<const Scalar> composite_column = composite_overlapping_X[region_idx]->getData(i); |
| 247 |
LocalOrdinal num_elements = regionX[region_idx]->getMap()->getLocalNumElements(); |
| 248 |
ArrayRCP<Scalar> region_column = regionX[region_idx]->getDataNonConst(i); |
| 249 |
for (LocalOrdinal local_region_index = 0; local_region_index < num_elements; ++local_region_index) { |
| 250 |
GlobalOrdinal composite_region_index = regionX[region_idx]->getMap()->getGlobalElement(local_region_index); |
| 251 |
GlobalOrdinal composite_composite_index = levels_[0]->GetCompositeIndex(region_idx, composite_region_index + 1); |
| 252 |
LocalOrdinal local_composite_index = composite_overlapping_X[region_idx]->getMap()->getLocalElement(composite_composite_index - 1); |
| 253 |
region_column[local_region_index] = composite_column[local_composite_index]; |
| 254 |
} |
| 255 |
} |
| 256 |
} |
| 257 |
} |
| 258 |
|
| 259 |
template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node> |
| 260 |
void RegionAMG<Scalar, LocalOrdinal, GlobalOrdinal, Node>::computeCompositeY(Array<RCP<const multivector_type> > regionY, multivector_type& Y) const { |
| 261 |
// Array to store extended region Maps (needed to copy composite entries of the input vector into region partitioning of it) |
| 262 |
Teuchos::Array<GlobalOrdinal> overlapping_composite; |
| 263 |
|
| 264 |
// Create Overlapping composite maps |
| 265 |
for (int region_idx = 0; region_idx < num_regions_; ++region_idx) { |
| 266 |
LocalOrdinal num_elements = regionY[region_idx]->getMap()->getLocalNumElements(); |
| 267 |
for (LocalOrdinal local_region_index = 0; local_region_index < num_elements; ++local_region_index) { |
| 268 |
GlobalOrdinal composite_region_index = regionY[region_idx]->getMap()->getGlobalElement(local_region_index); |
| 269 |
GlobalOrdinal composite_composite_index = levels_[0]->GetCompositeIndex(region_idx, composite_region_index + 1); |
| 270 |
overlapping_composite.push_back(composite_composite_index - 1); |
| 271 |
} |
| 272 |
} |
| 273 |
|
| 274 |
typename Teuchos::Array<GlobalOrdinal>::iterator last; |
| 275 |
std::sort(overlapping_composite.begin(), overlapping_composite.end()); |
| 276 |
last = std::unique(overlapping_composite.begin(), overlapping_composite.end()); |
| 277 |
overlapping_composite.erase(last, overlapping_composite.end()); |
| 278 |
|
| 279 |
// We split at first the input and output multivectors into region ones |
| 280 |
RCP<multivector_type> composite_overlapping_Y; |
| 281 |
RCP<map_type> overlapping_composite_map = MapFactory<LocalOrdinal, GlobalOrdinal, Node>::Build(Xpetra::UseTpetra, Y.getGlobalLength(), overlapping_composite, 0, comm_); |
| 282 |
composite_overlapping_Y = MultiVectorFactory<Scalar, LocalOrdinal, GlobalOrdinal, Node>::Build(overlapping_composite_map, Y.getNumVectors()); |
| 283 |
|
| 284 |
// Safety checks to guarantee the legitimacy of non-unique source composite map and unique target composite map |
| 285 |
// The unique target composite map must be a subset of the non-unique source composite map |
| 286 |
// Before running the std::includes algorithm it is very important to sort the ElementList in ascending order |
| 287 |
Teuchos::Array<GlobalOrdinal> original_map = Y.getMap()->getLocalElementList(); |
| 288 |
Teuchos::Array<GlobalOrdinal> overlapping_map = composite_overlapping_Y->getMap()->getLocalElementList(); |
| 289 |
std::sort(original_map.begin(), original_map.end()); |
| 290 |
std::sort(overlapping_map.begin(), overlapping_map.end()); |
| 291 |
TEUCHOS_TEST_FOR_EXCEPTION(!(std::includes(overlapping_map.begin(), overlapping_map.end(), original_map.begin(), original_map.end())), Exceptions::RuntimeError, "Overlapping (non-unique) composite map does NOT include original (unique) composite map \n"); |
| 292 |
|
| 293 |
// Copy values from output region multivectors regionY into output composite multivector Y |
| 294 |
for (int i = 0; i < Y.getNumVectors(); ++i) { |
| 295 |
for (int region_idx = 0; region_idx < num_regions_; ++region_idx) { |
| 296 |
ArrayRCP<Scalar> composite_column = composite_overlapping_Y->getDataNonConst(i); |
| 297 |
LocalOrdinal num_elements = regionY[region_idx]->getMap()->getLocalNumElements(); |
| 298 |
ArrayRCP<const Scalar> region_column = regionY[region_idx]->getData(i); |
| 299 |
for (LocalOrdinal local_region_index = 0; local_region_index < num_elements; ++local_region_index) { |
| 300 |
GlobalOrdinal composite_region_index = regionY[region_idx]->getMap()->getGlobalElement(local_region_index); |
| 301 |
GlobalOrdinal composite_composite_index = levels_[0]->GetCompositeIndex(region_idx, composite_region_index + 1); |
| 302 |
LocalOrdinal local_composite_index = composite_overlapping_Y->getMap()->getLocalElement(composite_composite_index - 1); |
| 303 |
composite_column[local_composite_index] += region_column[local_region_index]; |
| 304 |
} |
| 305 |
} |
| 306 |
} |
| 307 |
|
| 308 |
// Import object needed to transfer output composite multivector from non-unique map to unique map |
| 309 |
RCP<Import<LocalOrdinal, GlobalOrdinal, Node> > Import1 = ImportFactory<LocalOrdinal, GlobalOrdinal, Node>::Build(composite_overlapping_Y->getMap(), Y.getMap()); |
| 310 |
|
| 311 |
// IMPORTANT: ALWAYS USE IMPORT-EXPORT OBJECTS IN FORWARD MODE (i.e. doImport methods must use Import objects and doExport methods must use Export objects) |
| 312 |
// Reverse mode (i.e. crossed use of doImport/doExport methods with Export/Import objects makes the code possibly crash due to undefined behavior) |
| 313 |
Y.doImport(*composite_overlapping_Y, *Import1, Xpetra::ADD); |
| 314 |
} |
| 315 |
|
| 316 |
template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node> |
| 317 |
void RegionAMG<Scalar, LocalOrdinal, GlobalOrdinal, Node>::rescaleInterfaceEntries(Array<RCP<multivector_type> > regionY) const { |
| 318 |
Array<Array<std::tuple<GlobalOrdinal, GlobalOrdinal> > > regionToAll = levels_[0]->GetRegionToAll(); |
| 319 |
Array<std::tuple<GlobalOrdinal, Array<GlobalOrdinal> > > interfaceNodes = matrixSplitting_->getRegionHandler()->GetInterfaceNodes(); |
| 320 |
|
| 321 |
TEUCHOS_TEST_FOR_EXCEPTION(num_regions_ != regionToAll.size(), Exceptions::RuntimeError, "Regions stored in Level 0 do not match with total number of regions in RegionAMG class \n"); |
| 322 |
|
| 323 |
for (int region_idx = 0; region_idx < num_regions_; ++region_idx) { |
| 324 |
LocalOrdinal num_elements = regionY[region_idx]->getMap()->getLocalNumElements(); |
| 325 |
for (LocalOrdinal local_region_index = 0; local_region_index < num_elements; ++local_region_index) { |
| 326 |
GlobalOrdinal composite_region_index = regionY[region_idx]->getMap()->getGlobalElement(local_region_index); |
| 327 |
GlobalOrdinal composite_composite_index = levels_[0]->GetCompositeIndex(region_idx, composite_region_index + 1); |
| 328 |
checkerNodesToRegion<GlobalOrdinal> unaryPredicateNode(composite_composite_index); |
| 329 |
typename Array<std::tuple<GlobalOrdinal, Array<GlobalOrdinal> > >::iterator nodes_to_region_iterator; |
| 330 |
nodes_to_region_iterator = std::find_if<typename Array<std::tuple<GlobalOrdinal, Array<GlobalOrdinal> > >::iterator, checkerNodesToRegion<GlobalOrdinal> >(interfaceNodes.begin(), interfaceNodes.end(), unaryPredicateNode); |
| 331 |
// The rescaling must be applied only to entries associated with mesh on the interface |
| 332 |
if (nodes_to_region_iterator != interfaceNodes.end()) { |
| 333 |
Array<GlobalOrdinal> nodal_regions = std::get<1>(*nodes_to_region_iterator); |
| 334 |
for (int i = 0; i < regionY[region_idx]->getNumVectors(); ++i) { |
| 335 |
ArrayRCP<Scalar> region_column = regionY[region_idx]->getDataNonConst(i); |
| 336 |
if (nodal_regions.size() > 1) |
| 337 |
region_column[local_region_index] = region_column[local_region_index] / (nodal_regions.size()); |
| 338 |
} |
| 339 |
} |
| 340 |
} |
| 341 |
} |
| 342 |
} |
| 343 |
|
| 344 |
template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node> |
| 345 |
void RegionAMG<Scalar, LocalOrdinal, GlobalOrdinal, Node>::apply(const multivector_type& X, multivector_type& Y, Teuchos::ETransp mode, Scalar alpha, Scalar beta) const { |
| 346 |
// N.B.: currently Scalar quantities alpha and beta are passed as input parameters to have the apply method signature match with the apply signature of an Xpetra::Operator |
| 347 |
// however we are not currently using them (for us alpha=0 and beta=1) |
| 348 |
|
| 349 |
// At first we check that input and output vector have matching maps with the composite matrix |
| 350 |
// The Map of X must coincide with the Domain map of compositeA |
| 351 |
// The Map of Y must coincide with the Range map of compositeA |
| 352 |
TEUCHOS_TEST_FOR_EXCEPTION(!(X.getMap()->isSameAs(*(matrixSplitting_->getMatrix()->getDomainMap()))), Exceptions::RuntimeError, "Map of composite input multivector X does not coincide with Domain Map of composite matrix \n"); |
| 353 |
TEUCHOS_TEST_FOR_EXCEPTION(!(Y.getMap()->isSameAs(*(matrixSplitting_->getMatrix()->getRangeMap()))), Exceptions::RuntimeError, "Map of composite input multivector X does not coincide with Range Map of composite matrix \n"); |
| 354 |
TEUCHOS_TEST_FOR_EXCEPTION(X.getNumVectors() != Y.getNumVectors(), Exceptions::RuntimeError, "Number of vectors in input numltivector X does NOT match number of vectors in output multivector Y \n"); |
| 355 |
|
| 356 |
// We split at first the input and output multivectors into region ones |
| 357 |
Array<RCP<multivector_type> > regionX; |
| 358 |
Array<RCP<multivector_type> > regionY; |
| 359 |
regionX.resize(num_regions_); |
| 360 |
regionY.resize(num_regions_); |
| 361 |
|
| 362 |
// Associate Maps to region input (regionX) and output vectors (regionY) |
| 363 |
for (int region_idx = 0; region_idx < num_regions_; ++region_idx) { |
| 364 |
regionX[region_idx] = MultiVectorFactory<Scalar, LocalOrdinal, GlobalOrdinal, Node>::Build(levels_[0]->GetRegionMatrix(region_idx)->getDomainMap(), X.getNumVectors()); |
| 365 |
regionY[region_idx] = MultiVectorFactory<Scalar, LocalOrdinal, GlobalOrdinal, Node>::Build(levels_[0]->GetRegionMatrix(region_idx)->getRangeMap(), Y.getNumVectors()); |
| 366 |
} |
| 367 |
|
| 368 |
// Split the composite input multivector X into region multivector regionX |
| 369 |
computeRegionX(X, regionX); |
| 370 |
|
| 371 |
// This is the portion where the V-cycle is executed (for now we only have region V-cycles) |
| 372 |
for (int region_idx = 0; region_idx < num_regions_; ++region_idx) { |
| 373 |
regionHierarchies_[region_idx]->Iterate(*regionX[region_idx], *regionY[region_idx]); |
| 374 |
} |
| 375 |
|
| 376 |
// We rescale entries of region multivector regionY that are associated with mesh nodes on an interface |
| 377 |
rescaleInterfaceEntries(regionY); |
| 378 |
|
| 379 |
// We create const view of the region multivector regionY because we want to guarantee that the composite output multivector Y |
| 380 |
// is constructed without modifying any information given from each region |
| 381 |
Array<RCP<const multivector_type> > regionYconst; |
| 382 |
for (int region_idx = 0; region_idx < num_regions_; ++region_idx) { |
| 383 |
regionYconst.push_back(regionY[region_idx].getConst()); |
| 384 |
} |
| 385 |
|
| 386 |
// Assemble composite output multivector Y from region multivector regionY |
| 387 |
computeCompositeY(regionYconst, Y); |
| 388 |
} |
| 389 |
|
| 390 |
} // namespace Xpetra |
| 391 |
#endif |