| 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 |
#include <Teuchos_UnitTestHarness.hpp> |
| 11 |
|
| 12 |
#include <MueLu_TestHelpers.hpp> |
| 13 |
#include <MueLu_TestHelpersSmoothers.hpp> |
| 14 |
|
| 15 |
#include <MueLu_HierarchyUtils.hpp> |
| 16 |
#include <MueLu_SubBlockAFactory.hpp> |
| 17 |
#include <MueLu_ReorderBlockAFactory.hpp> |
| 18 |
#include <MueLu_SmootherFactory.hpp> |
| 19 |
#include <MueLu_SchurComplementFactory.hpp> |
| 20 |
#include <MueLu_InverseApproximationFactory.hpp> |
| 21 |
#include <MueLu_SimpleSmoother.hpp> |
| 22 |
#include <MueLu_BlockedDirectSolver.hpp> |
| 23 |
#include <MueLu_Utilities.hpp> |
| 24 |
|
| 25 |
namespace MueLuTests { |
| 26 |
|
| 27 |
using namespace TestHelpers::Smoothers; |
| 28 |
|
| 29 |
// Xpetra version of CreateMap |
| 30 |
template <class LocalOrdinal, class GlobalOrdinal, class Node, class MapType> |
| 31 |
Teuchos::RCP<Xpetra::Map<LocalOrdinal, GlobalOrdinal, Node> > CreateMap(const std::set<GlobalOrdinal>& gids, const Teuchos::Comm<int>& comm) { |
| 32 |
Teuchos::Array<GlobalOrdinal> mapvec; |
| 33 |
mapvec.reserve(gids.size()); |
| 34 |
mapvec.assign(gids.begin(), gids.end()); |
| 35 |
GlobalOrdinal count = Teuchos::as<GlobalOrdinal>(mapvec.size()); |
| 36 |
GlobalOrdinal gcount; |
| 37 |
Teuchos::reduceAll(comm, Teuchos::REDUCE_SUM, count, Teuchos::outArg(gcount)); |
| 38 |
|
| 39 |
Teuchos::RCP<Xpetra::Map<LocalOrdinal, GlobalOrdinal, Node> > map = |
| 40 |
Teuchos::rcp(new MapType(gcount, |
| 41 |
mapvec(), |
| 42 |
0, |
| 43 |
Teuchos::rcpFromRef(comm))); |
| 44 |
mapvec.clear(); |
| 45 |
return map; |
| 46 |
} |
| 47 |
|
| 48 |
// Xpetra version of SplitMap |
| 49 |
template <class LocalOrdinal, class GlobalOrdinal, class Node, class MapType> |
| 50 |
Teuchos::RCP<Xpetra::Map<LocalOrdinal, GlobalOrdinal, Node> > SplitMap(const Xpetra::Map<LocalOrdinal, GlobalOrdinal, Node>& Amap, const Xpetra::Map<LocalOrdinal, GlobalOrdinal, Node>& Agiven) { |
| 51 |
Teuchos::RCP<const Teuchos::Comm<int> > comm = Amap.getComm(); |
| 52 |
|
| 53 |
GlobalOrdinal count = 0; |
| 54 |
Teuchos::Array<GlobalOrdinal> myaugids(Amap.getLocalNumElements()); |
| 55 |
for (size_t i = 0; i < Amap.getLocalNumElements(); ++i) { |
| 56 |
const GlobalOrdinal gid = Amap.getGlobalElement(i); |
| 57 |
if (Agiven.isNodeGlobalElement(gid)) continue; |
| 58 |
myaugids[Teuchos::as<GlobalOrdinal>(count)] = gid; |
| 59 |
++count; |
| 60 |
} |
| 61 |
myaugids.resize(count); |
| 62 |
GlobalOrdinal gcount; |
| 63 |
Teuchos::reduceAll(*comm, Teuchos::REDUCE_SUM, 1, &count, &gcount); |
| 64 |
return Teuchos::rcp(new MapType(gcount, myaugids(), 0, comm)); |
| 65 |
} |
| 66 |
|
| 67 |
template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node, class MapType> |
| 68 |
Teuchos::RCP<Xpetra::BlockedCrsMatrix<Scalar, LocalOrdinal, GlobalOrdinal, Node> > CreateBlockDiagonalExampleMatrix(int noBlocks, const Teuchos::Comm<int>& comm) { |
| 69 |
typedef Xpetra::Map<LocalOrdinal, GlobalOrdinal, Node> Map; |
| 70 |
typedef Xpetra::CrsMatrix<Scalar, LocalOrdinal, GlobalOrdinal, Node> CrsMatrix; |
| 71 |
typedef Xpetra::CrsMatrixFactory<Scalar, LocalOrdinal, GlobalOrdinal, Node> CrsMatrixFactory; |
| 72 |
typedef Xpetra::MapExtractor<Scalar, LocalOrdinal, GlobalOrdinal, Node> MapExtractor; |
| 73 |
typedef Xpetra::BlockedCrsMatrix<Scalar, LocalOrdinal, GlobalOrdinal, Node> BlockedCrsMatrix; |
| 74 |
typedef Xpetra::CrsMatrixWrap<Scalar, LocalOrdinal, GlobalOrdinal, Node> CrsMatrixWrap; |
| 75 |
|
| 76 |
GlobalOrdinal nOverallDOFGidsPerProc = Teuchos::as<GlobalOrdinal>(Teuchos::ScalarTraits<GlobalOrdinal>::pow(2, noBlocks - 2)) * 10; |
| 77 |
|
| 78 |
GlobalOrdinal procOffset = comm.getRank() * nOverallDOFGidsPerProc; |
| 79 |
|
| 80 |
std::set<GlobalOrdinal> myDOFGids; |
| 81 |
for (GlobalOrdinal i = 0; i < nOverallDOFGidsPerProc; i++) |
| 82 |
myDOFGids.insert(i + procOffset); |
| 83 |
|
| 84 |
Teuchos::RCP<Map> fullmap = CreateMap<LocalOrdinal, GlobalOrdinal, Node, MapType>(myDOFGids, comm); |
| 85 |
|
| 86 |
std::vector<Teuchos::RCP<const Map> > maps(noBlocks, Teuchos::null); |
| 87 |
GlobalOrdinal nPartGIDs = nOverallDOFGidsPerProc; |
| 88 |
Teuchos::RCP<Map> remainingpartmap = fullmap; |
| 89 |
for (int it = 0; it < noBlocks; it++) { |
| 90 |
if (it == noBlocks - 1) { |
| 91 |
maps[0] = remainingpartmap; |
| 92 |
break; |
| 93 |
} |
| 94 |
// collect first half of GIDs |
| 95 |
nPartGIDs = nPartGIDs / 2; |
| 96 |
std::set<GlobalOrdinal> myHalfGIDs; |
| 97 |
for (GlobalOrdinal j = 0; j < nPartGIDs; j++) |
| 98 |
myHalfGIDs.insert(j + procOffset); |
| 99 |
|
| 100 |
Teuchos::RCP<Map> halfmap = CreateMap<LocalOrdinal, GlobalOrdinal, Node, MapType>(myHalfGIDs, comm); |
| 101 |
|
| 102 |
Teuchos::RCP<Map> secondmap = SplitMap<LocalOrdinal, GlobalOrdinal, Node, MapType>(*remainingpartmap, *halfmap); |
| 103 |
remainingpartmap = halfmap; |
| 104 |
|
| 105 |
maps[noBlocks - 1 - it] = secondmap; |
| 106 |
} |
| 107 |
|
| 108 |
// create diagonal blocks |
| 109 |
std::vector<Teuchos::RCP<CrsMatrix> > blocks(noBlocks, Teuchos::null); |
| 110 |
for (int it = 0; it < noBlocks; it++) { |
| 111 |
// std::cout << it << " " << maps[it]->getMinAllGlobalIndex() << " - " << maps[it]->getMaxAllGlobalIndex() << std::endl; |
| 112 |
blocks[it] = CrsMatrixFactory::Build(maps[it], 1); |
| 113 |
|
| 114 |
LocalOrdinal NumMyElements = maps[it]->getLocalNumElements(); |
| 115 |
Teuchos::ArrayView<const GlobalOrdinal> MyGlobalElements = maps[it]->getLocalElementList(); |
| 116 |
|
| 117 |
for (LocalOrdinal i = 0; i < NumMyElements; i++) |
| 118 |
blocks[it]->insertGlobalValues(MyGlobalElements[i], |
| 119 |
Teuchos::tuple<GlobalOrdinal>(MyGlobalElements[i]), |
| 120 |
Teuchos::tuple<Scalar>(it + 1)); |
| 121 |
blocks[it]->fillComplete(); |
| 122 |
} |
| 123 |
|
| 124 |
// create map extractor |
| 125 |
Teuchos::RCP<const MapExtractor> rgMapExtractor = Teuchos::rcp(new MapExtractor(fullmap, maps, false)); |
| 126 |
Teuchos::RCP<const MapExtractor> doMapExtractor = Teuchos::rcp(new MapExtractor(fullmap, maps, false)); |
| 127 |
|
| 128 |
// build blocked operator |
| 129 |
Teuchos::RCP<BlockedCrsMatrix> bop = Teuchos::rcp(new BlockedCrsMatrix(rgMapExtractor, doMapExtractor, 1)); |
| 130 |
|
| 131 |
for (int it = 0; it < noBlocks; it++) { |
| 132 |
Teuchos::RCP<CrsMatrixWrap> csrwrap = |
| 133 |
Teuchos::rcp(new CrsMatrixWrap(blocks[it])); |
| 134 |
bop->setMatrix(Teuchos::as<size_t>(it), Teuchos::as<size_t>(it), csrwrap); |
| 135 |
} |
| 136 |
bop->fillComplete(); |
| 137 |
return bop; |
| 138 |
} |
| 139 |
|
| 140 |
template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node, class MapType> |
| 141 |
Teuchos::RCP<Xpetra::BlockedCrsMatrix<Scalar, LocalOrdinal, GlobalOrdinal, Node> > CreateBlockDiagonalExampleMatrixThyra(int noBlocks, const Teuchos::Comm<int>& comm) { |
| 142 |
typedef Xpetra::Map<LocalOrdinal, GlobalOrdinal, Node> Map; |
| 143 |
typedef Xpetra::MapFactory<LocalOrdinal, GlobalOrdinal, Node> MapFactory; |
| 144 |
typedef Xpetra::CrsMatrix<Scalar, LocalOrdinal, GlobalOrdinal, Node> CrsMatrix; |
| 145 |
typedef Xpetra::CrsMatrixFactory<Scalar, LocalOrdinal, GlobalOrdinal, Node> CrsMatrixFactory; |
| 146 |
typedef Xpetra::MapExtractor<Scalar, LocalOrdinal, GlobalOrdinal, Node> MapExtractor; |
| 147 |
typedef Xpetra::BlockedCrsMatrix<Scalar, LocalOrdinal, GlobalOrdinal, Node> BlockedCrsMatrix; |
| 148 |
typedef Xpetra::CrsMatrixWrap<Scalar, LocalOrdinal, GlobalOrdinal, Node> CrsMatrixWrap; |
| 149 |
|
| 150 |
std::vector<Teuchos::RCP<const Map> > maps(noBlocks, Teuchos::null); |
| 151 |
|
| 152 |
MapType tm(1, 0, Teuchos::rcpFromRef(comm)); |
| 153 |
Xpetra::UnderlyingLib lib = tm.lib(); |
| 154 |
|
| 155 |
maps[0] = MapFactory::Build(lib, comm.getSize() * 5, 5, 0, Teuchos::rcpFromRef(comm)); |
| 156 |
for (int it = 1; it < noBlocks; it++) { |
| 157 |
GlobalOrdinal localDofs = Teuchos::as<GlobalOrdinal>(Teuchos::ScalarTraits<GlobalOrdinal>::pow(2, it - 1) * 5); |
| 158 |
maps[it] = MapFactory::Build(lib, comm.getSize() * localDofs, localDofs, 0, Teuchos::rcpFromRef(comm)); |
| 159 |
} |
| 160 |
|
| 161 |
// create diagonal blocks |
| 162 |
std::vector<Teuchos::RCP<CrsMatrix> > blocks(noBlocks, Teuchos::null); |
| 163 |
for (int it = 0; it < noBlocks; it++) { |
| 164 |
// std::cout << it << " " << maps[it]->getMinAllGlobalIndex() << " - " << maps[it]->getMaxAllGlobalIndex() << std::endl; |
| 165 |
blocks[it] = CrsMatrixFactory::Build(maps[it], 1); |
| 166 |
|
| 167 |
LocalOrdinal NumMyElements = maps[it]->getLocalNumElements(); |
| 168 |
Teuchos::ArrayView<const GlobalOrdinal> MyGlobalElements = maps[it]->getLocalElementList(); |
| 169 |
|
| 170 |
for (LocalOrdinal i = 0; i < NumMyElements; i++) |
| 171 |
blocks[it]->insertGlobalValues(MyGlobalElements[i], |
| 172 |
Teuchos::tuple<GlobalOrdinal>(MyGlobalElements[i]), |
| 173 |
Teuchos::tuple<Scalar>(it + 1)); |
| 174 |
blocks[it]->fillComplete(); |
| 175 |
} |
| 176 |
|
| 177 |
// create map extractor |
| 178 |
// To generate the Thyra style map extractor we do not need a full map but only the |
| 179 |
// information about the Map details (i.e. lib and indexBase). We can extract this |
| 180 |
// information from maps[0] |
| 181 |
Teuchos::RCP<const MapExtractor> rgMapExtractor = |
| 182 |
Teuchos::rcp(new MapExtractor(maps[0], maps, true)); |
| 183 |
Teuchos::RCP<const MapExtractor> doMapExtractor = |
| 184 |
Teuchos::rcp(new MapExtractor(maps[0], maps, true)); |
| 185 |
// build blocked operator |
| 186 |
Teuchos::RCP<BlockedCrsMatrix> bop = Teuchos::rcp(new BlockedCrsMatrix(rgMapExtractor, doMapExtractor, 1)); |
| 187 |
|
| 188 |
for (int it = 0; it < noBlocks; it++) { |
| 189 |
Teuchos::RCP<CrsMatrixWrap> csrwrap = Teuchos::rcp(new CrsMatrixWrap(blocks[it])); |
| 190 |
bop->setMatrix(Teuchos::as<size_t>(it), Teuchos::as<size_t>(it), csrwrap); |
| 191 |
} |
| 192 |
bop->fillComplete(); |
| 193 |
return bop; |
| 194 |
} |
| 195 |
|
| 196 |
template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node> |
| 197 |
Teuchos::RCP<Xpetra::BlockedCrsMatrix<Scalar, LocalOrdinal, GlobalOrdinal, Node> > CreateBlockedMatrixThyra(const Teuchos::Comm<int>& comm, Xpetra::UnderlyingLib lib) { |
| 198 |
typedef Xpetra::Map<LocalOrdinal, GlobalOrdinal, Node> Map; |
| 199 |
typedef Xpetra::Matrix<Scalar, LocalOrdinal, GlobalOrdinal, Node> Matrix; |
| 200 |
typedef Xpetra::MapExtractor<Scalar, LocalOrdinal, GlobalOrdinal, Node> MapExtractor; |
| 201 |
typedef Xpetra::BlockedCrsMatrix<Scalar, LocalOrdinal, GlobalOrdinal, Node> BlockedCrsMatrix; |
| 202 |
|
| 203 |
std::vector<RCP<const Map> > maps = std::vector<RCP<const Map> >(3, Teuchos::null); |
| 204 |
maps[0] = TestHelpers::TestFactory<Scalar, LocalOrdinal, GlobalOrdinal, Node>::BuildMap(100); |
| 205 |
maps[1] = TestHelpers::TestFactory<Scalar, LocalOrdinal, GlobalOrdinal, Node>::BuildMap(100); |
| 206 |
maps[2] = TestHelpers::TestFactory<Scalar, LocalOrdinal, GlobalOrdinal, Node>::BuildMap(100); |
| 207 |
RCP<Matrix> A00 = TestHelpers::TestFactory<Scalar, LocalOrdinal, GlobalOrdinal, Node>::BuildTridiag(maps[0], 4.0, -1.0, -1.0, lib); |
| 208 |
RCP<Matrix> A01 = TestHelpers::TestFactory<Scalar, LocalOrdinal, GlobalOrdinal, Node>::BuildTridiag(maps[0], -1.0, 0.0, 0.0, lib); |
| 209 |
RCP<Matrix> A10 = TestHelpers::TestFactory<Scalar, LocalOrdinal, GlobalOrdinal, Node>::BuildTridiag(maps[1], -1.0, 0.0, 0.0, lib); |
| 210 |
RCP<Matrix> A11 = TestHelpers::TestFactory<Scalar, LocalOrdinal, GlobalOrdinal, Node>::BuildTridiag(maps[1], 4.0, -1.0, -1.0, lib); |
| 211 |
RCP<Matrix> A12 = TestHelpers::TestFactory<Scalar, LocalOrdinal, GlobalOrdinal, Node>::BuildTridiag(maps[1], -1.0, 0.0, 0.0, lib); |
| 212 |
RCP<Matrix> A21 = TestHelpers::TestFactory<Scalar, LocalOrdinal, GlobalOrdinal, Node>::BuildTridiag(maps[2], -1.0, 0.0, 0.0, lib); |
| 213 |
RCP<Matrix> A22 = TestHelpers::TestFactory<Scalar, LocalOrdinal, GlobalOrdinal, Node>::BuildTridiag(maps[2], 4.0, -1.0, -1.0, lib); |
| 214 |
|
| 215 |
// create map extractor |
| 216 |
// To generate the Thyra style map extractor we do not need a full map but only the |
| 217 |
// information about the Map details (i.e. lib and indexBase). We can extract this |
| 218 |
// information from maps[0] |
| 219 |
Teuchos::RCP<const MapExtractor> rgMapExtractor = |
| 220 |
Teuchos::rcp(new MapExtractor(maps[0], maps, true)); |
| 221 |
Teuchos::RCP<const MapExtractor> doMapExtractor = |
| 222 |
Teuchos::rcp(new MapExtractor(maps[0], maps, true)); |
| 223 |
// build blocked operator |
| 224 |
Teuchos::RCP<BlockedCrsMatrix> bop = Teuchos::rcp(new BlockedCrsMatrix(rgMapExtractor, doMapExtractor, 5)); |
| 225 |
bop->setMatrix(Teuchos::as<size_t>(0), Teuchos::as<size_t>(0), A00); |
| 226 |
bop->setMatrix(Teuchos::as<size_t>(0), Teuchos::as<size_t>(1), A01); |
| 227 |
bop->setMatrix(Teuchos::as<size_t>(1), Teuchos::as<size_t>(0), A10); |
| 228 |
bop->setMatrix(Teuchos::as<size_t>(1), Teuchos::as<size_t>(1), A11); |
| 229 |
bop->setMatrix(Teuchos::as<size_t>(1), Teuchos::as<size_t>(2), A12); |
| 230 |
bop->setMatrix(Teuchos::as<size_t>(2), Teuchos::as<size_t>(1), A21); |
| 231 |
bop->setMatrix(Teuchos::as<size_t>(2), Teuchos::as<size_t>(2), A22); |
| 232 |
bop->fillComplete(); |
| 233 |
return bop; |
| 234 |
} |
| 235 |
|
| 236 |
TEUCHOS_UNIT_TEST_TEMPLATE_4_DECL(BlockedDirectSolver, BlockedDirectSolver_Setup_Apply, Scalar, LocalOrdinal, GlobalOrdinal, Node) { |
| 237 |
#include <MueLu_UseShortNames.hpp> |
| 238 |
MUELU_TESTING_SET_OSTREAM; |
| 239 |
MUELU_TESTING_LIMIT_SCOPE(Scalar, GlobalOrdinal, Node); |
| 240 |
|
| 241 |
std::array<std::string, 3> solver_types{"", "Klu", "Superlu"}; |
| 242 |
|
| 243 |
for (const auto& solver_type : solver_types) { |
| 244 |
RCP<const Teuchos::Comm<int> > comm = Parameters::getDefaultComm(); |
| 245 |
Xpetra::UnderlyingLib lib = MueLuTests::TestHelpers::Parameters::getLib(); |
| 246 |
|
| 247 |
Teuchos::RCP<const BlockedCrsMatrix> bop = TestHelpers::TestFactory<Scalar, LocalOrdinal, GlobalOrdinal, Node>::CreateBlockDiagonalExampleMatrix(lib, 5, comm); |
| 248 |
Teuchos::RCP<const Matrix> Aconst = Teuchos::rcp_dynamic_cast<const Matrix>(bop); |
| 249 |
Teuchos::RCP<Matrix> A = Teuchos::rcp_const_cast<Matrix>(Aconst); |
| 250 |
|
| 251 |
Level level; |
| 252 |
TestHelpers::TestFactory<Scalar, LocalOrdinal, GlobalOrdinal, Node>::createSingleLevelHierarchy(level); |
| 253 |
level.Set("A", A); |
| 254 |
level.setlib(lib); |
| 255 |
|
| 256 |
RCP<MultiVector> X = MultiVectorFactory::Build(A->getDomainMap(), 1); |
| 257 |
RCP<MultiVector> RHS = MultiVectorFactory::Build(A->getRangeMap(), 1); |
| 258 |
|
| 259 |
// Random X |
| 260 |
X->setSeed(846930886); |
| 261 |
X->randomize(); |
| 262 |
|
| 263 |
typedef typename Teuchos::ScalarTraits<Scalar>::magnitudeType magnitude_type; |
| 264 |
|
| 265 |
// Normalize X |
| 266 |
Array<magnitude_type> norms(1); |
| 267 |
X->norm2(norms); |
| 268 |
X->scale(1 / norms[0]); |
| 269 |
|
| 270 |
// Smoothers |
| 271 |
RCP<BlockedDirectSolver> smootherPrototype = rcp(new BlockedDirectSolver(solver_type)); |
| 272 |
RCP<SmootherFactory> smootherFact = rcp(new SmootherFactory(smootherPrototype)); |
| 273 |
|
| 274 |
// main factory manager |
| 275 |
FactoryManager M; |
| 276 |
M.SetFactory("Smoother", smootherFact); |
| 277 |
MueLu::SetFactoryManager SFM(Teuchos::rcpFromRef(level), Teuchos::rcpFromRef(M)); |
| 278 |
|
| 279 |
// request smoother (and all dependencies) on level |
| 280 |
level.Request("Smoother", smootherFact.get()); |
| 281 |
level.Request("PreSmoother", smootherFact.get()); |
| 282 |
level.Request("PostSmoother", smootherFact.get()); |
| 283 |
|
| 284 |
smootherFact->Build(level); |
| 285 |
|
| 286 |
RCP<SmootherBase> solver = level.Get<RCP<SmootherBase> >("PreSmoother", smootherFact.get()); |
| 287 |
|
| 288 |
// Compute RHS corresponding to X |
| 289 |
A->apply(*X, *RHS, Teuchos::NO_TRANS, (SC)1.0, (SC)0.0); |
| 290 |
|
| 291 |
// Reset X to 0 |
| 292 |
X->putScalar((SC)0.0); |
| 293 |
|
| 294 |
RHS->norm2(norms); |
| 295 |
out << "||RHS|| = " << std::setiosflags(std::ios::fixed) << std::setprecision(10) << norms[0] << std::endl; |
| 296 |
|
| 297 |
out << "solve with zero initial guess" << std::endl; |
| 298 |
Teuchos::Array<magnitude_type> initialNorms(1); |
| 299 |
X->norm2(initialNorms); |
| 300 |
out << " ||X_initial|| = " << std::setiosflags(std::ios::fixed) << std::setprecision(10) << initialNorms[0] << std::endl; |
| 301 |
|
| 302 |
solver->Apply(*X, *RHS, true); // zero initial guess |
| 303 |
|
| 304 |
Teuchos::Array<magnitude_type> finalNorms(1); |
| 305 |
X->norm2(finalNorms); |
| 306 |
Teuchos::Array<magnitude_type> residualNorm1 = Utilities::ResidualNorm(*A, *X, *RHS); |
| 307 |
out << " ||Residual_final|| = " << std::setiosflags(std::ios::fixed) << std::setprecision(20) << residualNorm1[0] << std::endl; |
| 308 |
out << " ||X_final|| = " << std::setiosflags(std::ios::fixed) << std::setprecision(10) << finalNorms[0] << std::endl; |
| 309 |
|
| 310 |
TEUCHOS_TEST_COMPARE(residualNorm1[0], <, 5e-15, out, success); |
| 311 |
TEUCHOS_TEST_COMPARE(finalNorms[0] - Teuchos::ScalarTraits<Scalar>::magnitude(Teuchos::ScalarTraits<Scalar>::one()), <, |
| 312 |
5e-15, out, success); |
| 313 |
|
| 314 |
out << "solve with random initial guess" << std::endl; |
| 315 |
X->randomize(); |
| 316 |
X->norm2(initialNorms); |
| 317 |
out << " ||X_initial|| = " << std::setiosflags(std::ios::fixed) << std::setprecision(10) << initialNorms[0] << std::endl; |
| 318 |
|
| 319 |
solver->Apply(*X, *RHS, false); // nonzero initial guess |
| 320 |
|
| 321 |
X->norm2(finalNorms); |
| 322 |
Teuchos::Array<magnitude_type> residualNorm2 = Utilities::ResidualNorm(*A, *X, *RHS); |
| 323 |
out << " ||Residual_final|| = " << std::setiosflags(std::ios::fixed) << std::setprecision(20) << residualNorm2[0] << std::endl; |
| 324 |
out << " ||X_final|| = " << std::setiosflags(std::ios::fixed) << std::setprecision(10) << finalNorms[0] << std::endl; |
| 325 |
|
| 326 |
TEUCHOS_TEST_COMPARE(residualNorm2[0], <, 5e-15, out, success); |
| 327 |
TEUCHOS_TEST_COMPARE(finalNorms[0] - Teuchos::ScalarTraits<Scalar>::magnitude(Teuchos::ScalarTraits<Scalar>::one()), <, |
| 328 |
5e-15, out, success); |
| 329 |
|
| 330 |
if (comm->getSize() == 1) { |
| 331 |
TEST_EQUALITY(residualNorm1[0] == residualNorm2[0], true); |
| 332 |
} else { |
| 333 |
out << "Pass/Fail is only checked in serial." << std::endl; |
| 334 |
} |
| 335 |
} |
| 336 |
} |
| 337 |
|
| 338 |
TEUCHOS_UNIT_TEST_TEMPLATE_4_DECL(BlockedDirectSolver, NestedI53I42II01II_BlockedDirectSolver_Setup_Apply, Scalar, LocalOrdinal, GlobalOrdinal, Node) { |
| 339 |
#include <MueLu_UseShortNames.hpp> |
| 340 |
MUELU_TESTING_SET_OSTREAM; |
| 341 |
MUELU_TESTING_LIMIT_SCOPE(Scalar, GlobalOrdinal, Node); |
| 342 |
|
| 343 |
std::array<std::string, 3> solver_types{"", "Klu", "Superlu"}; |
| 344 |
|
| 345 |
for (const auto& solver_type : solver_types) { |
| 346 |
RCP<const Teuchos::Comm<int> > comm = Parameters::getDefaultComm(); |
| 347 |
Xpetra::UnderlyingLib lib = MueLuTests::TestHelpers::Parameters::getLib(); |
| 348 |
|
| 349 |
Teuchos::RCP<const BlockedCrsMatrix> bop = TestHelpers::TestFactory<Scalar, LocalOrdinal, GlobalOrdinal, Node>::CreateBlockDiagonalExampleMatrix(lib, 6, comm); |
| 350 |
Teuchos::RCP<const Matrix> Aconst = Teuchos::rcp_dynamic_cast<const Matrix>(bop); |
| 351 |
Teuchos::RCP<Matrix> A = Teuchos::rcp_const_cast<Matrix>(Aconst); |
| 352 |
|
| 353 |
Level level; |
| 354 |
TestHelpers::TestFactory<Scalar, LocalOrdinal, GlobalOrdinal, Node>::createSingleLevelHierarchy(level); |
| 355 |
level.Set("A", A); |
| 356 |
level.setlib(lib); |
| 357 |
|
| 358 |
// Test ReorderBlockAFactory |
| 359 |
Teuchos::RCP<ReorderBlockAFactory> rAFact = Teuchos::rcp(new ReorderBlockAFactory()); |
| 360 |
rAFact->SetFactory("A", MueLu::NoFactory::getRCP()); |
| 361 |
rAFact->SetParameter(std::string("Reorder Type"), Teuchos::ParameterEntry(std::string("[5 3 [4 2] [0 1]]"))); |
| 362 |
|
| 363 |
// Smoothers |
| 364 |
RCP<BlockedDirectSolver> smootherPrototype = rcp(new BlockedDirectSolver(solver_type)); |
| 365 |
RCP<SmootherFactory> smootherFact = rcp(new SmootherFactory(smootherPrototype)); |
| 366 |
|
| 367 |
// main factory manager |
| 368 |
FactoryManager M; |
| 369 |
M.SetFactory("Smoother", smootherFact); |
| 370 |
M.SetFactory("A", rAFact); |
| 371 |
|
| 372 |
MueLu::SetFactoryManager SFM(Teuchos::rcpFromRef(level), Teuchos::rcpFromRef(M)); |
| 373 |
|
| 374 |
// request block smoother (and all dependencies) on level |
| 375 |
level.Request("A", rAFact.get()); |
| 376 |
level.Request("Smoother", smootherFact.get()); |
| 377 |
level.Request("PreSmoother", smootherFact.get()); |
| 378 |
level.Request("PostSmoother", smootherFact.get()); |
| 379 |
|
| 380 |
smootherFact->Build(level); |
| 381 |
|
| 382 |
level.print(std::cout, Teuchos::VERB_EXTREME); |
| 383 |
|
| 384 |
RCP<SmootherBase> solver = level.Get<RCP<SmootherBase> >("PreSmoother", smootherFact.get()); |
| 385 |
|
| 386 |
RCP<Matrix> reorderedA = level.Get<RCP<Matrix> >("A", rAFact.get()); |
| 387 |
RCP<BlockedCrsMatrix> reorderedbA = Teuchos::rcp_dynamic_cast<BlockedCrsMatrix>(reorderedA); |
| 388 |
|
| 389 |
TEST_EQUALITY(reorderedbA->Rows(), 4); |
| 390 |
TEST_EQUALITY(reorderedbA->Cols(), 4); |
| 391 |
|
| 392 |
RCP<MultiVector> X = MultiVectorFactory::Build(A->getDomainMap(), 1); |
| 393 |
RCP<MultiVector> RHS = MultiVectorFactory::Build(A->getRangeMap(), 1); |
| 394 |
|
| 395 |
// apply simple smoother |
| 396 |
RHS->putScalar((SC)1.0); |
| 397 |
X->putScalar((SC)0.0); |
| 398 |
|
| 399 |
// solve system |
| 400 |
solver->Apply(*X, *RHS, true); // zero initial guess |
| 401 |
Teuchos::RCP<BlockedMultiVector> bX = Teuchos::rcp_dynamic_cast<BlockedMultiVector>(X); |
| 402 |
Teuchos::RCP<MultiVector> XX = bX->Merge(); |
| 403 |
Teuchos::ArrayRCP<const Scalar> xdata = XX->getData(0); |
| 404 |
bool bCheck = true; |
| 405 |
for (size_t i = 0; i < XX->getLocalLength(); i++) { |
| 406 |
if (i < 5) { |
| 407 |
if (xdata[i] != (SC)1.0) bCheck = false; |
| 408 |
} |
| 409 |
if (i >= 5 && i < 10) { |
| 410 |
if (xdata[i] != (SC)(1.0 / 2.0)) bCheck = false; |
| 411 |
} |
| 412 |
if (i >= 10 && i < 20) { |
| 413 |
if (xdata[i] != (SC)(1.0 / 3.0)) bCheck = false; |
| 414 |
} |
| 415 |
if (i >= 20 && i < 40) { |
| 416 |
if (xdata[i] != (SC)(1.0 / 4.0)) bCheck = false; |
| 417 |
} |
| 418 |
if (i >= 40 && i < 80) { |
| 419 |
if (xdata[i] != (SC)(1.0 / 5.0)) bCheck = false; |
| 420 |
} |
| 421 |
if (i >= 80 && i < 160) { |
| 422 |
if (xdata[i] != (SC)(1.0 / 6.0)) bCheck = false; |
| 423 |
} |
| 424 |
} |
| 425 |
TEST_EQUALITY(bCheck, true); |
| 426 |
|
| 427 |
// Random X |
| 428 |
X->setSeed(846930886); |
| 429 |
X->randomize(); |
| 430 |
|
| 431 |
typedef typename Teuchos::ScalarTraits<Scalar>::magnitudeType magnitude_type; |
| 432 |
|
| 433 |
// Normalize X |
| 434 |
Array<magnitude_type> norms(1); |
| 435 |
X->norm2(norms); |
| 436 |
X->scale(1 / norms[0]); |
| 437 |
|
| 438 |
// Compute RHS corresponding to X |
| 439 |
A->apply(*X, *RHS, Teuchos::NO_TRANS, (SC)1.0, (SC)0.0); |
| 440 |
|
| 441 |
// Reset X to 0 |
| 442 |
X->putScalar((SC)0.0); |
| 443 |
|
| 444 |
RHS->norm2(norms); |
| 445 |
out << "||RHS|| = " << std::setiosflags(std::ios::fixed) << std::setprecision(10) << norms[0] << std::endl; |
| 446 |
|
| 447 |
out << "solve with zero initial guess" << std::endl; |
| 448 |
Teuchos::Array<magnitude_type> initialNorms(1); |
| 449 |
X->norm2(initialNorms); |
| 450 |
out << " ||X_initial|| = " << std::setiosflags(std::ios::fixed) << std::setprecision(10) << initialNorms[0] << std::endl; |
| 451 |
|
| 452 |
solver->Apply(*X, *RHS, true); // zero initial guess |
| 453 |
|
| 454 |
Teuchos::Array<magnitude_type> finalNorms(1); |
| 455 |
X->norm2(finalNorms); |
| 456 |
Teuchos::Array<magnitude_type> residualNorm1 = Utilities::ResidualNorm(*A, *X, *RHS); |
| 457 |
out << " ||Residual_final|| = " << std::setiosflags(std::ios::fixed) << std::setprecision(20) << residualNorm1[0] << std::endl; |
| 458 |
out << " ||X_final|| = " << std::setiosflags(std::ios::fixed) << std::setprecision(10) << finalNorms[0] << std::endl; |
| 459 |
|
| 460 |
TEUCHOS_TEST_COMPARE(residualNorm1[0], <, 5e-15, out, success); |
| 461 |
TEUCHOS_TEST_COMPARE(finalNorms[0] - Teuchos::ScalarTraits<Scalar>::magnitude(Teuchos::ScalarTraits<Scalar>::one()), <, 5e-15, |
| 462 |
out, success); |
| 463 |
} |
| 464 |
} |
| 465 |
|
| 466 |
TEUCHOS_UNIT_TEST_TEMPLATE_4_DECL(BlockedDirectSolver, NestedI53I42II01II_Thyra_BlockedDirectSolver_Setup_Apply, Scalar, LocalOrdinal, GlobalOrdinal, Node) { |
| 467 |
#include <MueLu_UseShortNames.hpp> |
| 468 |
MUELU_TESTING_SET_OSTREAM; |
| 469 |
MUELU_TESTING_LIMIT_SCOPE(Scalar, GlobalOrdinal, Node); |
| 470 |
|
| 471 |
std::array<std::string, 3> solver_types{"", "Klu", "Superlu"}; |
| 472 |
|
| 473 |
for (const auto& solve_type : solver_types) { |
| 474 |
RCP<const Teuchos::Comm<int> > comm = Parameters::getDefaultComm(); |
| 475 |
Xpetra::UnderlyingLib lib = MueLuTests::TestHelpers::Parameters::getLib(); |
| 476 |
|
| 477 |
Teuchos::RCP<const BlockedCrsMatrix> bop = TestHelpers::TestFactory<Scalar, LocalOrdinal, GlobalOrdinal, Node>::CreateBlockDiagonalExampleMatrixThyra(lib, 6, comm); |
| 478 |
Teuchos::RCP<const Matrix> Aconst = Teuchos::rcp_dynamic_cast<const Matrix>(bop); |
| 479 |
Teuchos::RCP<Matrix> A = Teuchos::rcp_const_cast<Matrix>(Aconst); |
| 480 |
|
| 481 |
Level level; |
| 482 |
TestHelpers::TestFactory<Scalar, LocalOrdinal, GlobalOrdinal, Node>::createSingleLevelHierarchy(level); |
| 483 |
level.Set("A", A); |
| 484 |
level.setlib(lib); |
| 485 |
|
| 486 |
// Test ReorderBlockAFactory |
| 487 |
Teuchos::RCP<ReorderBlockAFactory> rAFact = Teuchos::rcp(new ReorderBlockAFactory()); |
| 488 |
rAFact->SetFactory("A", MueLu::NoFactory::getRCP()); |
| 489 |
rAFact->SetParameter(std::string("Reorder Type"), Teuchos::ParameterEntry(std::string("[5 3 [4 2] [0 1]]"))); |
| 490 |
|
| 491 |
// Smoothers |
| 492 |
RCP<BlockedDirectSolver> smootherPrototype = rcp(new BlockedDirectSolver(solve_type)); |
| 493 |
RCP<SmootherFactory> smootherFact = rcp(new SmootherFactory(smootherPrototype)); |
| 494 |
|
| 495 |
// main factory manager |
| 496 |
FactoryManager M; |
| 497 |
M.SetFactory("Smoother", smootherFact); |
| 498 |
M.SetFactory("A", rAFact); |
| 499 |
|
| 500 |
MueLu::SetFactoryManager SFM(Teuchos::rcpFromRef(level), Teuchos::rcpFromRef(M)); |
| 501 |
|
| 502 |
// request block smoother (and all dependencies) on level |
| 503 |
level.Request("A", rAFact.get()); |
| 504 |
level.Request("Smoother", smootherFact.get()); |
| 505 |
level.Request("PreSmoother", smootherFact.get()); |
| 506 |
level.Request("PostSmoother", smootherFact.get()); |
| 507 |
|
| 508 |
smootherFact->Build(level); |
| 509 |
|
| 510 |
level.print(std::cout, Teuchos::VERB_EXTREME); |
| 511 |
|
| 512 |
RCP<SmootherBase> solver = level.Get<RCP<SmootherBase> >("PreSmoother", smootherFact.get()); |
| 513 |
|
| 514 |
RCP<Matrix> reorderedA = level.Get<RCP<Matrix> >("A", rAFact.get()); |
| 515 |
RCP<BlockedCrsMatrix> reorderedbA = Teuchos::rcp_dynamic_cast<BlockedCrsMatrix>(reorderedA); |
| 516 |
|
| 517 |
TEST_EQUALITY(reorderedbA->Rows(), 4); |
| 518 |
TEST_EQUALITY(reorderedbA->Cols(), 4); |
| 519 |
|
| 520 |
RCP<MultiVector> X = MultiVectorFactory::Build(A->getDomainMap(), 1); |
| 521 |
RCP<MultiVector> RHS = MultiVectorFactory::Build(A->getRangeMap(), 1); |
| 522 |
|
| 523 |
// apply simple smoother |
| 524 |
RHS->putScalar((SC)1.0); |
| 525 |
X->putScalar((SC)0.0); |
| 526 |
|
| 527 |
// solve system |
| 528 |
solver->Apply(*X, *RHS, true); // zero initial guess |
| 529 |
Teuchos::RCP<BlockedMultiVector> bX = Teuchos::rcp_dynamic_cast<BlockedMultiVector>(X); |
| 530 |
Teuchos::RCP<MultiVector> XX = bX->Merge(); |
| 531 |
Teuchos::ArrayRCP<const Scalar> xdata = XX->getData(0); |
| 532 |
bool bCheck = true; |
| 533 |
for (size_t i = 0; i < XX->getLocalLength(); i++) { |
| 534 |
if (i < 5) { |
| 535 |
if (xdata[i] != (SC)1.0) bCheck = false; |
| 536 |
} |
| 537 |
if (i >= 5 && i < 10) { |
| 538 |
if (xdata[i] != (SC)(1.0 / 2.0)) bCheck = false; |
| 539 |
} |
| 540 |
if (i >= 10 && i < 20) { |
| 541 |
if (xdata[i] != (SC)(1.0 / 3.0)) bCheck = false; |
| 542 |
} |
| 543 |
if (i >= 20 && i < 40) { |
| 544 |
if (xdata[i] != (SC)(1.0 / 4.0)) bCheck = false; |
| 545 |
} |
| 546 |
if (i >= 40 && i < 80) { |
| 547 |
if (xdata[i] != (SC)(1.0 / 5.0)) bCheck = false; |
| 548 |
} |
| 549 |
if (i >= 80 && i < 160) { |
| 550 |
if (xdata[i] != (SC)(1.0 / 6.0)) bCheck = false; |
| 551 |
} |
| 552 |
} |
| 553 |
TEST_EQUALITY(bCheck, true); |
| 554 |
|
| 555 |
// Random X |
| 556 |
X->setSeed(846930886); |
| 557 |
X->randomize(); |
| 558 |
|
| 559 |
typedef typename Teuchos::ScalarTraits<Scalar>::magnitudeType magnitude_type; |
| 560 |
|
| 561 |
// Normalize X |
| 562 |
Array<magnitude_type> norms(1); |
| 563 |
X->norm2(norms); |
| 564 |
X->scale(1 / norms[0]); |
| 565 |
|
| 566 |
// Compute RHS corresponding to X |
| 567 |
A->apply(*X, *RHS, Teuchos::NO_TRANS, (SC)1.0, (SC)0.0); |
| 568 |
|
| 569 |
// Reset X to 0 |
| 570 |
X->putScalar((SC)0.0); |
| 571 |
|
| 572 |
RHS->norm2(norms); |
| 573 |
out << "||RHS|| = " << std::setiosflags(std::ios::fixed) << std::setprecision(10) << norms[0] << std::endl; |
| 574 |
|
| 575 |
out << "solve with zero initial guess" << std::endl; |
| 576 |
Teuchos::Array<magnitude_type> initialNorms(1); |
| 577 |
X->norm2(initialNorms); |
| 578 |
out << " ||X_initial|| = " << std::setiosflags(std::ios::fixed) << std::setprecision(10) << initialNorms[0] << std::endl; |
| 579 |
|
| 580 |
solver->Apply(*X, *RHS, true); // zero initial guess |
| 581 |
|
| 582 |
Teuchos::Array<magnitude_type> finalNorms(1); |
| 583 |
X->norm2(finalNorms); |
| 584 |
Teuchos::Array<magnitude_type> residualNorm1 = Utilities::ResidualNorm(*A, *X, *RHS); |
| 585 |
out << " ||Residual_final|| = " << std::setiosflags(std::ios::fixed) << std::setprecision(20) << residualNorm1[0] << std::endl; |
| 586 |
out << " ||X_final|| = " << std::setiosflags(std::ios::fixed) << std::setprecision(10) << finalNorms[0] << std::endl; |
| 587 |
|
| 588 |
TEUCHOS_TEST_COMPARE(residualNorm1[0], <, 5e-15, out, success); |
| 589 |
TEUCHOS_TEST_COMPARE(finalNorms[0] - Teuchos::ScalarTraits<Scalar>::magnitude(Teuchos::ScalarTraits<Scalar>::one()), <, 5e-15, |
| 590 |
out, success); |
| 591 |
} |
| 592 |
} |
| 593 |
|
| 594 |
TEUCHOS_UNIT_TEST_TEMPLATE_4_DECL(BlockedDirectSolver, NestedII20I1I_BlockedDirectSolver_Setup_Apply, Scalar, LocalOrdinal, GlobalOrdinal, Node) { |
| 595 |
#include <MueLu_UseShortNames.hpp> |
| 596 |
MUELU_TESTING_SET_OSTREAM; |
| 597 |
MUELU_TESTING_LIMIT_SCOPE(Scalar, GlobalOrdinal, Node); |
| 598 |
|
| 599 |
std::array<std::string, 3> solver_types{"", "Klu", "Superlu"}; |
| 600 |
|
| 601 |
RCP<const Teuchos::Comm<int> > comm = Parameters::getDefaultComm(); |
| 602 |
Xpetra::UnderlyingLib lib = MueLuTests::TestHelpers::Parameters::getLib(); |
| 603 |
|
| 604 |
if (comm->getSize() > 1) { |
| 605 |
out << "Skipping test for " << comm->getSize() |
| 606 |
<< " processors as Amesos2 cannot deal with non-standard maps in the non-serial case." << std::endl; |
| 607 |
return; |
| 608 |
} |
| 609 |
|
| 610 |
for (const auto& solver_type : solver_types) { |
| 611 |
Teuchos::RCP<const BlockedCrsMatrix> bop = TestHelpers::TestFactory<Scalar, LocalOrdinal, GlobalOrdinal, Node>::CreateBlockDiagonalExampleMatrix(lib, 3, comm); |
| 612 |
Teuchos::RCP<const Matrix> Aconst = Teuchos::rcp_dynamic_cast<const Matrix>(bop); |
| 613 |
Teuchos::RCP<Matrix> A = Teuchos::rcp_const_cast<Matrix>(Aconst); |
| 614 |
|
| 615 |
Level level; |
| 616 |
TestHelpers::TestFactory<Scalar, LocalOrdinal, GlobalOrdinal, Node>::createSingleLevelHierarchy(level); |
| 617 |
level.Set("A", A); |
| 618 |
level.setlib(lib); |
| 619 |
|
| 620 |
// Test ReorderBlockAFactory |
| 621 |
Teuchos::RCP<ReorderBlockAFactory> rAFact = Teuchos::rcp(new ReorderBlockAFactory()); |
| 622 |
rAFact->SetFactory("A", MueLu::NoFactory::getRCP()); |
| 623 |
rAFact->SetParameter(std::string("Reorder Type"), Teuchos::ParameterEntry(std::string("[ [2 0] 1]"))); |
| 624 |
|
| 625 |
// Smoothers |
| 626 |
RCP<SimpleSmoother> smootherPrototype = rcp(new SimpleSmoother()); |
| 627 |
smootherPrototype->SetFactory("A", rAFact); |
| 628 |
smootherPrototype->SetParameter("Sweeps", Teuchos::ParameterEntry(Teuchos::as<LocalOrdinal>(1))); |
| 629 |
smootherPrototype->SetParameter("Damping factor", Teuchos::ParameterEntry(Teuchos::as<Scalar>(1.0))); |
| 630 |
smootherPrototype->SetParameter("UseSIMPLEC", Teuchos::ParameterEntry(false)); |
| 631 |
|
| 632 |
std::vector<RCP<SubBlockAFactory> > sA(1, Teuchos::null); |
| 633 |
std::vector<RCP<SmootherFactory> > sF(2, Teuchos::null); |
| 634 |
std::vector<RCP<FactoryManager> > sM(2, Teuchos::null); |
| 635 |
|
| 636 |
// prediction |
| 637 |
std::string strInfo = std::string("{ 1 }"); |
| 638 |
sA[0] = rcp(new SubBlockAFactory()); |
| 639 |
sA[0]->SetFactory("A", rAFact); |
| 640 |
sA[0]->SetParameter("block row", Teuchos::ParameterEntry(0)); |
| 641 |
sA[0]->SetParameter("block col", Teuchos::ParameterEntry(0)); |
| 642 |
sA[0]->SetParameter("Range map: Striding info", Teuchos::ParameterEntry(strInfo)); |
| 643 |
sA[0]->SetParameter("Domain map: Striding info", Teuchos::ParameterEntry(strInfo)); |
| 644 |
|
| 645 |
// create a 2x2 SIMPLE for the prediction eq. |
| 646 |
RCP<BlockedDirectSolver> smoProtoPredict = Teuchos::rcp(new BlockedDirectSolver(solver_type)); |
| 647 |
smoProtoPredict->SetFactory("A", sA[0]); |
| 648 |
sF[0] = rcp(new SmootherFactory(smoProtoPredict)); |
| 649 |
sM[0] = rcp(new FactoryManager()); |
| 650 |
sM[0]->SetFactory("A", sA[0]); |
| 651 |
sM[0]->SetFactory("Smoother", sF[0]); |
| 652 |
sM[0]->SetIgnoreUserData(true); |
| 653 |
|
| 654 |
smootherPrototype->SetVelocityPredictionFactoryManager(sM[0]); |
| 655 |
|
| 656 |
RCP<InverseApproximationFactory> AinvFact = Teuchos::rcp(new InverseApproximationFactory()); |
| 657 |
AinvFact->SetFactory("A", rAFact); |
| 658 |
|
| 659 |
RCP<SchurComplementFactory> SFact = Teuchos::rcp(new SchurComplementFactory()); |
| 660 |
SFact->SetParameter("omega", Teuchos::ParameterEntry(Teuchos::as<Scalar>(1.0))); |
| 661 |
SFact->SetFactory("A", rAFact); |
| 662 |
SFact->SetFactory("Ainv", AinvFact); |
| 663 |
|
| 664 |
RCP<SmootherPrototype> smoProtoCorrect = rcp(new TrilinosSmoother(std::string("RELAXATION"), Teuchos::ParameterList(), 0)); |
| 665 |
smoProtoCorrect->SetFactory("A", SFact); |
| 666 |
sF[1] = rcp(new SmootherFactory(smoProtoCorrect)); |
| 667 |
|
| 668 |
sM[1] = rcp(new FactoryManager()); |
| 669 |
sM[1]->SetFactory("A", SFact); |
| 670 |
sM[1]->SetFactory("Smoother", sF[1]); |
| 671 |
sM[1]->SetIgnoreUserData(true); |
| 672 |
|
| 673 |
smootherPrototype->SetSchurCompFactoryManager(sM[1]); |
| 674 |
RCP<SmootherFactory> smootherFact = rcp(new SmootherFactory(smootherPrototype)); |
| 675 |
|
| 676 |
// main factory manager |
| 677 |
FactoryManager M; |
| 678 |
M.SetFactory("Smoother", smootherFact); |
| 679 |
M.SetFactory("A", rAFact); |
| 680 |
|
| 681 |
MueLu::SetFactoryManager SFM(Teuchos::rcpFromRef(level), Teuchos::rcpFromRef(M)); |
| 682 |
|
| 683 |
// request smoother (and all dependencies) on level |
| 684 |
level.Request("A", rAFact.get()); |
| 685 |
level.Request("Smoother", smootherFact.get()); |
| 686 |
level.Request("PreSmoother", smootherFact.get()); |
| 687 |
level.Request("PostSmoother", smootherFact.get()); |
| 688 |
|
| 689 |
smootherFact->Build(level); |
| 690 |
|
| 691 |
level.print(std::cout, Teuchos::VERB_EXTREME); |
| 692 |
|
| 693 |
RCP<SmootherBase> simpleSmoother = level.Get<RCP<SmootherBase> >("PreSmoother", smootherFact.get()); |
| 694 |
|
| 695 |
RCP<Matrix> reorderedA = level.Get<RCP<Matrix> >("A", rAFact.get()); |
| 696 |
RCP<BlockedCrsMatrix> reorderedbA = Teuchos::rcp_dynamic_cast<BlockedCrsMatrix>(reorderedA); |
| 697 |
|
| 698 |
TEST_EQUALITY(reorderedbA->Rows(), 2); |
| 699 |
TEST_EQUALITY(reorderedbA->Cols(), 2); |
| 700 |
|
| 701 |
RCP<MultiVector> X = MultiVectorFactory::Build(reorderedA->getDomainMap(), 1); |
| 702 |
RCP<MultiVector> RHS = MultiVectorFactory::Build(reorderedA->getRangeMap(), 1); |
| 703 |
|
| 704 |
// apply simple smoother |
| 705 |
RHS->putScalar((SC)1.0); |
| 706 |
X->putScalar((SC)0.0); |
| 707 |
|
| 708 |
// solve system |
| 709 |
simpleSmoother->Apply(*X, *RHS, true); // zero initial guess |
| 710 |
Teuchos::RCP<BlockedMultiVector> bX = Teuchos::rcp_dynamic_cast<BlockedMultiVector>(X); |
| 711 |
Teuchos::RCP<MultiVector> XX = bX->Merge(); |
| 712 |
Teuchos::ArrayRCP<const Scalar> xdata = XX->getData(0); |
| 713 |
bool bCheck = true; |
| 714 |
for (size_t i = 0; i < XX->getLocalLength(); i++) { |
| 715 |
if (i < 10) { |
| 716 |
if (xdata[i] != (SC)(1.0 / 3.0)) bCheck = false; |
| 717 |
} |
| 718 |
if (i >= 10 && i < 15) { |
| 719 |
if (xdata[i] != (SC)1.0) bCheck = false; |
| 720 |
} |
| 721 |
if (i >= 15 && i < 20) { |
| 722 |
if (xdata[i] != (SC)0.5) bCheck = false; |
| 723 |
} |
| 724 |
} |
| 725 |
TEST_EQUALITY(bCheck, true); |
| 726 |
|
| 727 |
// Random X |
| 728 |
X->setSeed(846930886); |
| 729 |
X->randomize(); |
| 730 |
|
| 731 |
typedef typename Teuchos::ScalarTraits<Scalar>::magnitudeType magnitude_type; |
| 732 |
|
| 733 |
// Normalize X |
| 734 |
Array<magnitude_type> norms(1); |
| 735 |
X->norm2(norms); |
| 736 |
X->scale(1 / norms[0]); |
| 737 |
|
| 738 |
// Compute RHS corresponding to X |
| 739 |
reorderedA->apply(*X, *RHS, Teuchos::NO_TRANS, (SC)1.0, (SC)0.0); |
| 740 |
|
| 741 |
// Reset X to 0 |
| 742 |
X->putScalar((SC)0.0); |
| 743 |
|
| 744 |
RHS->norm2(norms); |
| 745 |
out << "||RHS|| = " << std::setiosflags(std::ios::fixed) << std::setprecision(10) << norms[0] << std::endl; |
| 746 |
|
| 747 |
out << "solve with zero initial guess" << std::endl; |
| 748 |
Teuchos::Array<magnitude_type> initialNorms(1); |
| 749 |
X->norm2(initialNorms); |
| 750 |
out << " ||X_initial|| = " << std::setiosflags(std::ios::fixed) << std::setprecision(10) << initialNorms[0] << std::endl; |
| 751 |
|
| 752 |
simpleSmoother->Apply(*X, *RHS, true); // zero initial guess |
| 753 |
|
| 754 |
Teuchos::Array<magnitude_type> finalNorms(1); |
| 755 |
X->norm2(finalNorms); |
| 756 |
Teuchos::Array<magnitude_type> residualNorm1 = Utilities::ResidualNorm(*reorderedA, *X, *RHS); |
| 757 |
out << " ||Residual_final|| = " << std::setiosflags(std::ios::fixed) << std::setprecision(20) << residualNorm1[0] << std::endl; |
| 758 |
out << " ||X_final|| = " << std::setiosflags(std::ios::fixed) << std::setprecision(10) << finalNorms[0] << std::endl; |
| 759 |
|
| 760 |
TEUCHOS_TEST_COMPARE(residualNorm1[0], <, 5e-15, out, success); |
| 761 |
TEUCHOS_TEST_COMPARE(finalNorms[0] - Teuchos::ScalarTraits<Scalar>::magnitude(Teuchos::ScalarTraits<Scalar>::one()), <, |
| 762 |
5e-15, out, success); |
| 763 |
} |
| 764 |
} |
| 765 |
|
| 766 |
TEUCHOS_UNIT_TEST_TEMPLATE_4_DECL(BlockedDirectSolver, NestedII20I1I_Thyra_BlockedDirectSolver_Setup_Apply, Scalar, LocalOrdinal, GlobalOrdinal, Node) { |
| 767 |
#include <MueLu_UseShortNames.hpp> |
| 768 |
MUELU_TESTING_SET_OSTREAM; |
| 769 |
MUELU_TESTING_LIMIT_SCOPE(Scalar, GlobalOrdinal, Node); |
| 770 |
|
| 771 |
std::array<std::string, 3> solver_types{"", "Klu", "Superlu"}; |
| 772 |
|
| 773 |
RCP<const Teuchos::Comm<int> > comm = Parameters::getDefaultComm(); |
| 774 |
Xpetra::UnderlyingLib lib = MueLuTests::TestHelpers::Parameters::getLib(); |
| 775 |
|
| 776 |
if (comm->getSize() > 1) { |
| 777 |
out << "Skipping test for " << comm->getSize() |
| 778 |
<< " processors as Amesos2 cannot deal with non-standard maps in the non-serial case." << std::endl; |
| 779 |
return; |
| 780 |
} |
| 781 |
|
| 782 |
for (const auto& solver_type : solver_types) { |
| 783 |
Teuchos::RCP<const BlockedCrsMatrix> bop = TestHelpers::TestFactory<Scalar, LocalOrdinal, GlobalOrdinal, Node>::CreateBlockDiagonalExampleMatrixThyra(lib, 3, comm); |
| 784 |
Teuchos::RCP<const Matrix> Aconst = Teuchos::rcp_dynamic_cast<const Matrix>(bop); |
| 785 |
Teuchos::RCP<Matrix> A = Teuchos::rcp_const_cast<Matrix>(Aconst); |
| 786 |
|
| 787 |
Level level; |
| 788 |
TestHelpers::TestFactory<Scalar, LocalOrdinal, GlobalOrdinal, Node>::createSingleLevelHierarchy(level); |
| 789 |
level.Set("A", A); |
| 790 |
level.setlib(lib); |
| 791 |
|
| 792 |
// Test ReorderBlockAFactory |
| 793 |
Teuchos::RCP<ReorderBlockAFactory> rAFact = Teuchos::rcp(new ReorderBlockAFactory()); |
| 794 |
rAFact->SetFactory("A", MueLu::NoFactory::getRCP()); |
| 795 |
rAFact->SetParameter(std::string("Reorder Type"), Teuchos::ParameterEntry(std::string("[ [2 0] 1]"))); |
| 796 |
|
| 797 |
// Smoothers |
| 798 |
RCP<SimpleSmoother> smootherPrototype = rcp(new SimpleSmoother()); |
| 799 |
smootherPrototype->SetFactory("A", rAFact); |
| 800 |
smootherPrototype->SetParameter("Sweeps", Teuchos::ParameterEntry(Teuchos::as<LocalOrdinal>(1))); |
| 801 |
smootherPrototype->SetParameter("Damping factor", Teuchos::ParameterEntry(Teuchos::as<Scalar>(1.0))); |
| 802 |
smootherPrototype->SetParameter("UseSIMPLEC", Teuchos::ParameterEntry(false)); |
| 803 |
|
| 804 |
std::vector<RCP<SubBlockAFactory> > sA(1, Teuchos::null); |
| 805 |
std::vector<RCP<SmootherFactory> > sF(2, Teuchos::null); |
| 806 |
std::vector<RCP<FactoryManager> > sM(2, Teuchos::null); |
| 807 |
|
| 808 |
// prediction |
| 809 |
std::string strInfo = std::string("{ 1 }"); |
| 810 |
sA[0] = rcp(new SubBlockAFactory()); |
| 811 |
sA[0]->SetFactory("A", rAFact); |
| 812 |
sA[0]->SetParameter("block row", Teuchos::ParameterEntry(0)); |
| 813 |
sA[0]->SetParameter("block col", Teuchos::ParameterEntry(0)); |
| 814 |
sA[0]->SetParameter("Range map: Striding info", Teuchos::ParameterEntry(strInfo)); |
| 815 |
sA[0]->SetParameter("Domain map: Striding info", Teuchos::ParameterEntry(strInfo)); |
| 816 |
|
| 817 |
// create a 2x2 SIMPLE for the prediction eq. |
| 818 |
RCP<BlockedDirectSolver> smoProtoPredict = Teuchos::rcp(new BlockedDirectSolver(solver_type)); |
| 819 |
smoProtoPredict->SetFactory("A", sA[0]); |
| 820 |
sF[0] = rcp(new SmootherFactory(smoProtoPredict)); |
| 821 |
sM[0] = rcp(new FactoryManager()); |
| 822 |
sM[0]->SetFactory("A", sA[0]); |
| 823 |
sM[0]->SetFactory("Smoother", sF[0]); |
| 824 |
sM[0]->SetIgnoreUserData(true); |
| 825 |
|
| 826 |
smootherPrototype->SetVelocityPredictionFactoryManager(sM[0]); |
| 827 |
|
| 828 |
RCP<InverseApproximationFactory> AinvFact = Teuchos::rcp(new InverseApproximationFactory()); |
| 829 |
AinvFact->SetFactory("A", rAFact); |
| 830 |
|
| 831 |
RCP<SchurComplementFactory> SFact = Teuchos::rcp(new SchurComplementFactory()); |
| 832 |
SFact->SetParameter("omega", Teuchos::ParameterEntry(Teuchos::as<Scalar>(1.0))); |
| 833 |
SFact->SetFactory("A", rAFact); |
| 834 |
SFact->SetFactory("Ainv", AinvFact); |
| 835 |
|
| 836 |
RCP<SmootherPrototype> smoProtoCorrect = rcp(new TrilinosSmoother(std::string("RELAXATION"), Teuchos::ParameterList(), 0)); |
| 837 |
smoProtoCorrect->SetFactory("A", SFact); |
| 838 |
sF[1] = rcp(new SmootherFactory(smoProtoCorrect)); |
| 839 |
|
| 840 |
sM[1] = rcp(new FactoryManager()); |
| 841 |
sM[1]->SetFactory("A", SFact); |
| 842 |
sM[1]->SetFactory("Smoother", sF[1]); |
| 843 |
sM[1]->SetIgnoreUserData(true); |
| 844 |
|
| 845 |
smootherPrototype->SetSchurCompFactoryManager(sM[1]); |
| 846 |
RCP<SmootherFactory> smootherFact = rcp(new SmootherFactory(smootherPrototype)); |
| 847 |
|
| 848 |
// main factory manager |
| 849 |
FactoryManager M; |
| 850 |
M.SetFactory("Smoother", smootherFact); |
| 851 |
M.SetFactory("A", rAFact); |
| 852 |
|
| 853 |
MueLu::SetFactoryManager SFM(Teuchos::rcpFromRef(level), Teuchos::rcpFromRef(M)); |
| 854 |
|
| 855 |
// request BGS smoother (and all dependencies) on level |
| 856 |
level.Request("A", rAFact.get()); |
| 857 |
level.Request("Smoother", smootherFact.get()); |
| 858 |
level.Request("PreSmoother", smootherFact.get()); |
| 859 |
level.Request("PostSmoother", smootherFact.get()); |
| 860 |
|
| 861 |
smootherFact->Build(level); |
| 862 |
|
| 863 |
level.print(std::cout, Teuchos::VERB_EXTREME); |
| 864 |
|
| 865 |
RCP<SmootherBase> simpleSmoother = level.Get<RCP<SmootherBase> >("PreSmoother", smootherFact.get()); |
| 866 |
|
| 867 |
RCP<Matrix> reorderedA = level.Get<RCP<Matrix> >("A", rAFact.get()); |
| 868 |
RCP<BlockedCrsMatrix> reorderedbA = Teuchos::rcp_dynamic_cast<BlockedCrsMatrix>(reorderedA); |
| 869 |
|
| 870 |
TEST_EQUALITY(reorderedbA->Rows(), 2); |
| 871 |
TEST_EQUALITY(reorderedbA->Cols(), 2); |
| 872 |
|
| 873 |
RCP<MultiVector> X = MultiVectorFactory::Build(reorderedA->getDomainMap(), 1); |
| 874 |
RCP<MultiVector> RHS = MultiVectorFactory::Build(reorderedA->getRangeMap(), 1); |
| 875 |
|
| 876 |
// apply simple smoother |
| 877 |
RHS->putScalar((SC)1.0); |
| 878 |
X->putScalar((SC)0.0); |
| 879 |
|
| 880 |
// solve system |
| 881 |
simpleSmoother->Apply(*X, *RHS, true); // zero initial guess |
| 882 |
Teuchos::RCP<BlockedMultiVector> bX = Teuchos::rcp_dynamic_cast<BlockedMultiVector>(X); |
| 883 |
Teuchos::RCP<MultiVector> XX = bX->Merge(); |
| 884 |
Teuchos::ArrayRCP<const Scalar> xdata = XX->getData(0); |
| 885 |
bool bCheck = true; |
| 886 |
for (size_t i = 0; i < XX->getLocalLength(); i++) { |
| 887 |
if (i < 10) { |
| 888 |
if (xdata[i] != (SC)(1.0 / 3.0)) bCheck = false; |
| 889 |
} |
| 890 |
if (i >= 10 && i < 15) { |
| 891 |
if (xdata[i] != (SC)1.0) bCheck = false; |
| 892 |
} |
| 893 |
if (i >= 15 && i < 20) { |
| 894 |
if (xdata[i] != (SC)0.5) bCheck = false; |
| 895 |
} |
| 896 |
} |
| 897 |
TEST_EQUALITY(bCheck, true); |
| 898 |
|
| 899 |
// Random X |
| 900 |
X->setSeed(846930886); |
| 901 |
X->randomize(); |
| 902 |
|
| 903 |
typedef typename Teuchos::ScalarTraits<Scalar>::magnitudeType magnitude_type; |
| 904 |
|
| 905 |
// Normalize X |
| 906 |
Array<magnitude_type> norms(1); |
| 907 |
X->norm2(norms); |
| 908 |
X->scale(1 / norms[0]); |
| 909 |
|
| 910 |
// Compute RHS corresponding to X |
| 911 |
reorderedA->apply(*X, *RHS, Teuchos::NO_TRANS, (SC)1.0, (SC)0.0); |
| 912 |
|
| 913 |
// Reset X to 0 |
| 914 |
X->putScalar((SC)0.0); |
| 915 |
|
| 916 |
RHS->norm2(norms); |
| 917 |
out << "||RHS|| = " << std::setiosflags(std::ios::fixed) << std::setprecision(10) << norms[0] << std::endl; |
| 918 |
|
| 919 |
out << "solve with zero initial guess" << std::endl; |
| 920 |
Teuchos::Array<magnitude_type> initialNorms(1); |
| 921 |
X->norm2(initialNorms); |
| 922 |
out << " ||X_initial|| = " << std::setiosflags(std::ios::fixed) << std::setprecision(10) << initialNorms[0] << std::endl; |
| 923 |
|
| 924 |
simpleSmoother->Apply(*X, *RHS, true); // zero initial guess |
| 925 |
|
| 926 |
Teuchos::Array<magnitude_type> finalNorms(1); |
| 927 |
X->norm2(finalNorms); |
| 928 |
Teuchos::Array<magnitude_type> residualNorm1 = Utilities::ResidualNorm(*reorderedA, *X, *RHS); |
| 929 |
out << " ||Residual_final|| = " << std::setiosflags(std::ios::fixed) << std::setprecision(20) << residualNorm1[0] << std::endl; |
| 930 |
out << " ||X_final|| = " << std::setiosflags(std::ios::fixed) << std::setprecision(10) << finalNorms[0] << std::endl; |
| 931 |
|
| 932 |
TEUCHOS_TEST_COMPARE(residualNorm1[0], <, 5e-15, out, success); |
| 933 |
TEUCHOS_TEST_COMPARE(finalNorms[0] - Teuchos::ScalarTraits<Scalar>::magnitude(Teuchos::ScalarTraits<Scalar>::one()), <, |
| 934 |
5e-15, out, success); |
| 935 |
} |
| 936 |
} |
| 937 |
|
| 938 |
#define MUELU_ETI_GROUP(SC, LO, GO, NO) \ |
| 939 |
TEUCHOS_UNIT_TEST_TEMPLATE_4_INSTANT(BlockedDirectSolver, BlockedDirectSolver_Setup_Apply, SC, LO, GO, NO) \ |
| 940 |
TEUCHOS_UNIT_TEST_TEMPLATE_4_INSTANT(BlockedDirectSolver, NestedI53I42II01II_BlockedDirectSolver_Setup_Apply, SC, LO, GO, NO) \ |
| 941 |
TEUCHOS_UNIT_TEST_TEMPLATE_4_INSTANT(BlockedDirectSolver, NestedI53I42II01II_Thyra_BlockedDirectSolver_Setup_Apply, SC, LO, GO, NO) \ |
| 942 |
TEUCHOS_UNIT_TEST_TEMPLATE_4_INSTANT(BlockedDirectSolver, NestedII20I1I_BlockedDirectSolver_Setup_Apply, SC, LO, GO, NO) \ |
| 943 |
TEUCHOS_UNIT_TEST_TEMPLATE_4_INSTANT(BlockedDirectSolver, NestedII20I1I_Thyra_BlockedDirectSolver_Setup_Apply, SC, LO, GO, NO) |
| 944 |
|
| 945 |
#include <MueLu_ETI_4arg.hpp> |
| 946 |
} // namespace MueLuTests |