Debian Codegraph @

packages/muelu/test/unit_tests/Smoothers/BlockedSmoother.cpp

raw
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_BlockedGaussSeidelSmoother.hpp>
20 #include <MueLu_BlockedJacobiSmoother.hpp>
21 #include <MueLu_BraessSarazinSmoother.hpp>
22 #include <MueLu_SchurComplementFactory.hpp>
23 #include <MueLu_SimpleSmoother.hpp>
24 #include <MueLu_UzawaSmoother.hpp>
25 #include <MueLu_IndefBlockedDiagonalSmoother.hpp>
26 #include <MueLu_InverseApproximationFactory.hpp>
27 #include <MueLu_Utilities.hpp>
28
29 #include <Xpetra_BlockReorderManager.hpp>
30 #include <Xpetra_ReorderedBlockedCrsMatrix.hpp>
31 #include <Xpetra_ReorderedBlockedMultiVector.hpp>
32
33 namespace MueLuTests {
34
35 // this namespace already has: #include "MueLu_UseShortNames.hpp"
36 using namespace TestHelpers::Smoothers;
37
38 // Xpetra version of CreateMap
39 template <class LocalOrdinal, class GlobalOrdinal, class Node, class MapType>
40 Teuchos::RCP<Xpetra::Map<LocalOrdinal, GlobalOrdinal, Node> > CreateMap(const std::set<GlobalOrdinal>& gids, const Teuchos::Comm<int>& comm) {
41 Teuchos::Array<GlobalOrdinal> mapvec;
42 mapvec.reserve(gids.size());
43 mapvec.assign(gids.begin(), gids.end());
44 GlobalOrdinal count = Teuchos::as<GlobalOrdinal>(mapvec.size());
45 GlobalOrdinal gcount;
46 Teuchos::reduceAll(comm, Teuchos::REDUCE_SUM, count, Teuchos::outArg(gcount));
47
48 Teuchos::RCP<Xpetra::Map<LocalOrdinal, GlobalOrdinal, Node> > map =
49 Teuchos::rcp(new MapType(gcount,
50 mapvec(),
51 0,
52 Teuchos::rcpFromRef(comm)));
53 mapvec.clear();
54 return map;
55 }
56
57 // Xpetra version of SplitMap
58 template <class LocalOrdinal, class GlobalOrdinal, class Node, class MapType>
59 Teuchos::RCP<Xpetra::Map<LocalOrdinal, GlobalOrdinal, Node> > SplitMap(const Xpetra::Map<LocalOrdinal, GlobalOrdinal, Node>& Amap, const Xpetra::Map<LocalOrdinal, GlobalOrdinal, Node>& Agiven) {
60 Teuchos::RCP<const Teuchos::Comm<int> > comm = Amap.getComm();
61
62 GlobalOrdinal count = 0;
63 Teuchos::Array<GlobalOrdinal> myaugids(Amap.getLocalNumElements());
64 for (size_t i = 0; i < Amap.getLocalNumElements(); ++i) {
65 const GlobalOrdinal gid = Amap.getGlobalElement(i);
66 if (Agiven.isNodeGlobalElement(gid)) continue;
67 myaugids[Teuchos::as<GlobalOrdinal>(count)] = gid;
68 ++count;
69 }
70 myaugids.resize(count);
71 GlobalOrdinal gcount;
72 Teuchos::reduceAll(*comm, Teuchos::REDUCE_SUM, 1, &count, &gcount);
73 return Teuchos::rcp(new MapType(gcount, myaugids(), 0, comm));
74 }
75
76 template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node, class MapType>
77 Teuchos::RCP<Xpetra::BlockedCrsMatrix<Scalar, LocalOrdinal, GlobalOrdinal, Node> > CreateBlockDiagonalExampleMatrix(int noBlocks, const Teuchos::Comm<int>& comm) {
78 typedef Xpetra::Map<LocalOrdinal, GlobalOrdinal, Node> Map;
79 typedef Xpetra::CrsMatrix<Scalar, LocalOrdinal, GlobalOrdinal, Node> CrsMatrix;
80 typedef Xpetra::CrsMatrixFactory<Scalar, LocalOrdinal, GlobalOrdinal, Node> CrsMatrixFactory;
81 typedef Xpetra::MapExtractor<Scalar, LocalOrdinal, GlobalOrdinal, Node> MapExtractor;
82 typedef Xpetra::BlockedCrsMatrix<Scalar, LocalOrdinal, GlobalOrdinal, Node> BlockedCrsMatrix;
83 typedef Xpetra::CrsMatrixWrap<Scalar, LocalOrdinal, GlobalOrdinal, Node> CrsMatrixWrap;
84
85 GlobalOrdinal nOverallDOFGidsPerProc = Teuchos::as<GlobalOrdinal>(Teuchos::ScalarTraits<GlobalOrdinal>::pow(2, noBlocks - 2)) * 10;
86
87 GlobalOrdinal procOffset = comm.getRank() * nOverallDOFGidsPerProc;
88
89 std::set<GlobalOrdinal> myDOFGids;
90 for (GlobalOrdinal i = 0; i < nOverallDOFGidsPerProc; i++)
91 myDOFGids.insert(i + procOffset);
92
93 Teuchos::RCP<Map> fullmap = CreateMap<LocalOrdinal, GlobalOrdinal, Node, MapType>(myDOFGids, comm);
94
95 std::vector<Teuchos::RCP<const Map> > maps(noBlocks, Teuchos::null);
96 GlobalOrdinal nPartGIDs = nOverallDOFGidsPerProc;
97 Teuchos::RCP<Map> remainingpartmap = fullmap;
98 for (int it = 0; it < noBlocks; it++) {
99 if (it == noBlocks - 1) {
100 maps[0] = remainingpartmap;
101 break;
102 }
103 // collect first half of GIDs
104 nPartGIDs = nPartGIDs / 2;
105 std::set<GlobalOrdinal> myHalfGIDs;
106 for (GlobalOrdinal j = 0; j < nPartGIDs; j++)
107 myHalfGIDs.insert(j + procOffset);
108
109 Teuchos::RCP<Map> halfmap = CreateMap<LocalOrdinal, GlobalOrdinal, Node, MapType>(myHalfGIDs, comm);
110
111 Teuchos::RCP<Map> secondmap = SplitMap<LocalOrdinal, GlobalOrdinal, Node, MapType>(*remainingpartmap, *halfmap);
112 remainingpartmap = halfmap;
113
114 maps[noBlocks - 1 - it] = secondmap;
115 }
116
117 // create diagonal blocks
118 std::vector<Teuchos::RCP<CrsMatrix> > blocks(noBlocks, Teuchos::null);
119 for (int it = 0; it < noBlocks; it++) {
120 // std::cout << it << " " << maps[it]->getMinAllGlobalIndex() << " - " << maps[it]->getMaxAllGlobalIndex() << std::endl;
121 blocks[it] = CrsMatrixFactory::Build(maps[it], 1);
122
123 LocalOrdinal NumMyElements = maps[it]->getLocalNumElements();
124 Teuchos::ArrayView<const GlobalOrdinal> MyGlobalElements = maps[it]->getLocalElementList();
125
126 for (LocalOrdinal i = 0; i < NumMyElements; i++)
127 blocks[it]->insertGlobalValues(MyGlobalElements[i],
128 Teuchos::tuple<GlobalOrdinal>(MyGlobalElements[i]),
129 Teuchos::tuple<Scalar>(it + 1));
130 blocks[it]->fillComplete();
131 }
132
133 // create map extractor
134 Teuchos::RCP<const MapExtractor> rgMapExtractor = Teuchos::rcp(new MapExtractor(fullmap, maps, false));
135 Teuchos::RCP<const MapExtractor> doMapExtractor = Teuchos::rcp(new MapExtractor(fullmap, maps, false));
136
137 // build blocked operator
138 Teuchos::RCP<BlockedCrsMatrix> bop = Teuchos::rcp(new BlockedCrsMatrix(rgMapExtractor, doMapExtractor, 1));
139
140 for (int it = 0; it < noBlocks; it++) {
141 Teuchos::RCP<CrsMatrixWrap> csrwrap =
142 Teuchos::rcp(new CrsMatrixWrap(blocks[it]));
143 bop->setMatrix(Teuchos::as<size_t>(it), Teuchos::as<size_t>(it), csrwrap);
144 }
145 bop->fillComplete();
146 return bop;
147 }
148
149 template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node, class MapType>
150 Teuchos::RCP<Xpetra::BlockedCrsMatrix<Scalar, LocalOrdinal, GlobalOrdinal, Node> > CreateBlockDiagonalExampleMatrixThyra(int noBlocks, const Teuchos::Comm<int>& comm) {
151 typedef Xpetra::Map<LocalOrdinal, GlobalOrdinal, Node> Map;
152 typedef Xpetra::MapFactory<LocalOrdinal, GlobalOrdinal, Node> MapFactory;
153 typedef Xpetra::CrsMatrix<Scalar, LocalOrdinal, GlobalOrdinal, Node> CrsMatrix;
154 typedef Xpetra::CrsMatrixFactory<Scalar, LocalOrdinal, GlobalOrdinal, Node> CrsMatrixFactory;
155 typedef Xpetra::MapExtractor<Scalar, LocalOrdinal, GlobalOrdinal, Node> MapExtractor;
156 typedef Xpetra::BlockedCrsMatrix<Scalar, LocalOrdinal, GlobalOrdinal, Node> BlockedCrsMatrix;
157 typedef Xpetra::CrsMatrixWrap<Scalar, LocalOrdinal, GlobalOrdinal, Node> CrsMatrixWrap;
158
159 std::vector<Teuchos::RCP<const Map> > maps(noBlocks, Teuchos::null);
160
161 MapType tm(1, 0, Teuchos::rcpFromRef(comm));
162 Xpetra::UnderlyingLib lib = tm.lib();
163
164 maps[0] = MapFactory::Build(lib, comm.getSize() * 5, 5, 0, Teuchos::rcpFromRef(comm));
165 for (int it = 1; it < noBlocks; it++) {
166 GlobalOrdinal localDofs = Teuchos::as<GlobalOrdinal>(Teuchos::ScalarTraits<GlobalOrdinal>::pow(2, it - 1) * 5);
167 maps[it] = MapFactory::Build(lib, comm.getSize() * localDofs, localDofs, 0, Teuchos::rcpFromRef(comm));
168 }
169
170 // create diagonal blocks
171 std::vector<Teuchos::RCP<CrsMatrix> > blocks(noBlocks, Teuchos::null);
172 for (int it = 0; it < noBlocks; it++) {
173 // std::cout << it << " " << maps[it]->getMinAllGlobalIndex() << " - " << maps[it]->getMaxAllGlobalIndex() << std::endl;
174 blocks[it] = CrsMatrixFactory::Build(maps[it], 1);
175
176 LocalOrdinal NumMyElements = maps[it]->getLocalNumElements();
177 Teuchos::ArrayView<const GlobalOrdinal> MyGlobalElements = maps[it]->getLocalElementList();
178
179 for (LocalOrdinal i = 0; i < NumMyElements; i++)
180 blocks[it]->insertGlobalValues(MyGlobalElements[i],
181 Teuchos::tuple<GlobalOrdinal>(MyGlobalElements[i]),
182 Teuchos::tuple<Scalar>(it + 1));
183 blocks[it]->fillComplete();
184 }
185
186 // create map extractor
187 // To generate the Thyra style map extractor we do not need a full map but only the
188 // information about the Map details (i.e. lib and indexBase). We can extract this
189 // information from maps[0]
190 Teuchos::RCP<const MapExtractor> rgMapExtractor =
191 Teuchos::rcp(new MapExtractor(maps[0], maps, true));
192 Teuchos::RCP<const MapExtractor> doMapExtractor =
193 Teuchos::rcp(new MapExtractor(maps[0], maps, true));
194 // build blocked operator
195 Teuchos::RCP<BlockedCrsMatrix> bop = Teuchos::rcp(new BlockedCrsMatrix(rgMapExtractor, doMapExtractor, 1));
196
197 for (int it = 0; it < noBlocks; it++) {
198 Teuchos::RCP<CrsMatrixWrap> csrwrap = Teuchos::rcp(new CrsMatrixWrap(blocks[it]));
199 bop->setMatrix(Teuchos::as<size_t>(it), Teuchos::as<size_t>(it), csrwrap);
200 }
201 bop->fillComplete();
202 return bop;
203 }
204
205 template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
206 Teuchos::RCP<Xpetra::BlockedCrsMatrix<Scalar, LocalOrdinal, GlobalOrdinal, Node> > CreateBlockedMatrixThyra(const Teuchos::Comm<int>& comm, Xpetra::UnderlyingLib lib) {
207 typedef Xpetra::Map<LocalOrdinal, GlobalOrdinal, Node> Map;
208 typedef Xpetra::Matrix<Scalar, LocalOrdinal, GlobalOrdinal, Node> Matrix;
209 typedef Xpetra::MapExtractor<Scalar, LocalOrdinal, GlobalOrdinal, Node> MapExtractor;
210 typedef Xpetra::BlockedCrsMatrix<Scalar, LocalOrdinal, GlobalOrdinal, Node> BlockedCrsMatrix;
211
212 std::vector<RCP<const Map> > maps = std::vector<RCP<const Map> >(3, Teuchos::null);
213 maps[0] = TestHelpers::TestFactory<Scalar, LocalOrdinal, GlobalOrdinal, Node>::BuildMap(100);
214 maps[1] = TestHelpers::TestFactory<Scalar, LocalOrdinal, GlobalOrdinal, Node>::BuildMap(100);
215 maps[2] = TestHelpers::TestFactory<Scalar, LocalOrdinal, GlobalOrdinal, Node>::BuildMap(100);
216 RCP<Matrix> A00 = TestHelpers::TestFactory<Scalar, LocalOrdinal, GlobalOrdinal, Node>::BuildTridiag(maps[0], 4.0, -1.0, -1.0, lib);
217 RCP<Matrix> A01 = TestHelpers::TestFactory<Scalar, LocalOrdinal, GlobalOrdinal, Node>::BuildTridiag(maps[0], -1.0, 0.0, 0.0, lib);
218 RCP<Matrix> A10 = TestHelpers::TestFactory<Scalar, LocalOrdinal, GlobalOrdinal, Node>::BuildTridiag(maps[1], -1.0, 0.0, 0.0, lib);
219 RCP<Matrix> A11 = TestHelpers::TestFactory<Scalar, LocalOrdinal, GlobalOrdinal, Node>::BuildTridiag(maps[1], 4.0, -1.0, -1.0, lib);
220 RCP<Matrix> A12 = TestHelpers::TestFactory<Scalar, LocalOrdinal, GlobalOrdinal, Node>::BuildTridiag(maps[1], -1.0, 0.0, 0.0, lib);
221 RCP<Matrix> A21 = TestHelpers::TestFactory<Scalar, LocalOrdinal, GlobalOrdinal, Node>::BuildTridiag(maps[2], -1.0, 0.0, 0.0, lib);
222 RCP<Matrix> A22 = TestHelpers::TestFactory<Scalar, LocalOrdinal, GlobalOrdinal, Node>::BuildTridiag(maps[2], 4.0, -1.0, -1.0, lib);
223
224 // create map extractor
225 // To generate the Thyra style map extractor we do not need a full map but only the
226 // information about the Map details (i.e. lib and indexBase). We can extract this
227 // information from maps[0]
228 Teuchos::RCP<const MapExtractor> rgMapExtractor =
229 Teuchos::rcp(new MapExtractor(maps[0], maps, true));
230 Teuchos::RCP<const MapExtractor> doMapExtractor =
231 Teuchos::rcp(new MapExtractor(maps[0], maps, true));
232 // build blocked operator
233 Teuchos::RCP<BlockedCrsMatrix> bop = Teuchos::rcp(new BlockedCrsMatrix(rgMapExtractor, doMapExtractor, 5));
234 bop->setMatrix(Teuchos::as<size_t>(0), Teuchos::as<size_t>(0), A00);
235 bop->setMatrix(Teuchos::as<size_t>(0), Teuchos::as<size_t>(1), A01);
236 bop->setMatrix(Teuchos::as<size_t>(1), Teuchos::as<size_t>(0), A10);
237 bop->setMatrix(Teuchos::as<size_t>(1), Teuchos::as<size_t>(1), A11);
238 bop->setMatrix(Teuchos::as<size_t>(1), Teuchos::as<size_t>(2), A12);
239 bop->setMatrix(Teuchos::as<size_t>(2), Teuchos::as<size_t>(1), A21);
240 bop->setMatrix(Teuchos::as<size_t>(2), Teuchos::as<size_t>(2), A22);
241 bop->fillComplete();
242 return bop;
243 }
244
245 TEUCHOS_UNIT_TEST_TEMPLATE_4_DECL(BlockedSmoother, Jacobi_Setup_Apply, Scalar, LocalOrdinal, GlobalOrdinal, Node) {
246 #include <MueLu_UseShortNames.hpp>
247 MUELU_TESTING_SET_OSTREAM;
248 MUELU_TESTING_LIMIT_SCOPE(Scalar, GlobalOrdinal, Node);
249 MUELU_TEST_ONLY_FOR(Xpetra::UseTpetra) {
250 RCP<const Teuchos::Comm<int> > comm = Parameters::getDefaultComm();
251
252 int noBlocks = 4;
253 Teuchos::RCP<const BlockedCrsMatrix> bop = CreateBlockDiagonalExampleMatrix<Scalar, LocalOrdinal, GlobalOrdinal, Node, TpetraMap>(noBlocks, *comm);
254 Teuchos::RCP<const Matrix> Aconst = Teuchos::rcp_dynamic_cast<const Matrix>(bop);
255 Teuchos::RCP<Matrix> A = Teuchos::rcp_const_cast<Matrix>(Aconst);
256
257 // I don't use the testApply infrastructure because it has no provision for an initial guess.
258 Level level;
259 TestHelpers::TestFactory<Scalar, LocalOrdinal, GlobalOrdinal, Node>::createSingleLevelHierarchy(level);
260 level.Set("A", A);
261
262 //////////////////////////////////////////////////////////////////////
263 // Smoothers
264 RCP<BlockedJacobiSmoother> smootherPrototype = rcp(new BlockedJacobiSmoother());
265 smootherPrototype->SetParameter("Sweeps", Teuchos::ParameterEntry(1));
266
267 std::vector<RCP<SubBlockAFactory> > sA(noBlocks, Teuchos::null);
268 std::vector<RCP<SmootherFactory> > sF(noBlocks, Teuchos::null);
269 std::vector<RCP<FactoryManager> > sM(noBlocks, Teuchos::null);
270 for (int k = 0; k < noBlocks; k++) {
271 std::string strInfo = std::string("{ 1 }");
272 sA[k] = rcp(new SubBlockAFactory());
273 sA[k]->SetFactory("A", MueLu::NoFactory::getRCP());
274 sA[k]->SetParameter("block row", Teuchos::ParameterEntry(k));
275 sA[k]->SetParameter("block col", Teuchos::ParameterEntry(k));
276 sA[k]->SetParameter("Range map: Striding info", Teuchos::ParameterEntry(strInfo));
277 sA[k]->SetParameter("Domain map: Striding info", Teuchos::ParameterEntry(strInfo));
278
279 RCP<SmootherPrototype> smoProto = rcp(new Ifpack2Smoother(std::string("RELAXATION"), Teuchos::ParameterList(), 0));
280 smoProto->SetFactory("A", sA[k]);
281 sF[k] = rcp(new SmootherFactory(smoProto));
282
283 sM[k] = rcp(new FactoryManager());
284 sM[k]->SetFactory("A", sA[k]);
285 sM[k]->SetFactory("Smoother", sF[k]);
286 sM[k]->SetIgnoreUserData(true);
287
288 smootherPrototype->AddFactoryManager(sM[k], k);
289 }
290
291 RCP<SmootherFactory> smootherFact = rcp(new SmootherFactory(smootherPrototype));
292
293 // main factory manager
294 FactoryManager M;
295 M.SetFactory("Smoother", smootherFact);
296
297 MueLu::SetFactoryManager SFM(Teuchos::rcpFromRef(level), Teuchos::rcpFromRef(M));
298
299 // request Jacobi smoother (and all dependencies) on level
300 level.Request("Smoother", smootherFact.get());
301 level.Request("PreSmoother", smootherFact.get());
302 level.Request("PostSmoother", smootherFact.get());
303
304 // smootherFact->DeclareInput(level);
305 smootherFact->Build(level);
306
307 level.print(std::cout, Teuchos::VERB_EXTREME);
308
309 RCP<SmootherBase> jacSmoother = level.Get<RCP<SmootherBase> >("PreSmoother", smootherFact.get());
310
311 RCP<MultiVector> X = MultiVectorFactory::Build(A->getDomainMap(), 1);
312 RCP<MultiVector> RHS = MultiVectorFactory::Build(A->getRangeMap(), 1);
313
314 // Random X
315 X->setSeed(846930886);
316 X->randomize();
317
318 typedef typename Teuchos::ScalarTraits<Scalar>::magnitudeType magnitude_type;
319
320 // Normalize X
321 Array<magnitude_type> norms(1);
322 X->norm2(norms);
323 X->scale(1 / norms[0]);
324
325 // Compute RHS corresponding to X
326 A->apply(*X, *RHS, Teuchos::NO_TRANS, (SC)1.0, (SC)0.0);
327
328 // Reset X to 0
329 X->putScalar((SC)0.0);
330
331 RHS->norm2(norms);
332 out << "||RHS|| = " << std::setiosflags(std::ios::fixed) << std::setprecision(10) << norms[0] << std::endl;
333
334 out << "solve with zero initial guess" << std::endl;
335 Teuchos::Array<magnitude_type> initialNorms(1);
336 X->norm2(initialNorms);
337 out << " ||X_initial|| = " << std::setiosflags(std::ios::fixed) << std::setprecision(10) << initialNorms[0] << std::endl;
338
339 jacSmoother->Apply(*X, *RHS, true); // zero initial guess
340
341 Teuchos::Array<magnitude_type> finalNorms(1);
342 X->norm2(finalNorms);
343 Teuchos::Array<magnitude_type> residualNorm1 = Utilities::ResidualNorm(*A, *X, *RHS);
344 out << " ||Residual_final|| = " << std::setiosflags(std::ios::fixed) << std::setprecision(20) << residualNorm1[0] << std::endl;
345 out << " ||X_final|| = " << std::setiosflags(std::ios::fixed) << std::setprecision(10) << finalNorms[0] << std::endl;
346
347 magnitude_type tol = 50. * Teuchos::ScalarTraits<Scalar>::eps();
348 TEUCHOS_TEST_COMPARE(residualNorm1[0], <, tol, out, success);
349 TEUCHOS_TEST_COMPARE(finalNorms[0] - Teuchos::ScalarTraits<Scalar>::magnitude(Teuchos::ScalarTraits<Scalar>::one()), <, tol, out, success);
350
351 out << "solve with zero initial guess, and unreliable nonzeroed vector X" << std::endl;
352 X->randomize();
353 X->norm2(initialNorms);
354 out << " ||X_initial|| = " << std::setiosflags(std::ios::fixed) << std::setprecision(10) << initialNorms[0] << std::endl;
355
356 jacSmoother->Apply(*X, *RHS, true); // zero initial guess with nonzero X
357
358 X->norm2(finalNorms);
359 Teuchos::Array<magnitude_type> residualNorm2 = Utilities::ResidualNorm(*A, *X, *RHS);
360 out << " ||Residual_final|| = " << std::setiosflags(std::ios::fixed) << std::setprecision(20) << residualNorm2[0] << std::endl;
361 out << " ||X_final|| = " << std::setiosflags(std::ios::fixed) << std::setprecision(10) << finalNorms[0] << std::endl;
362
363 TEUCHOS_TEST_COMPARE(residualNorm2[0], <, tol, out, success);
364 TEUCHOS_TEST_COMPARE(finalNorms[0] - Teuchos::ScalarTraits<Scalar>::magnitude(Teuchos::ScalarTraits<Scalar>::one()), <, tol, out, success);
365
366 out << "solve with random initial guess" << std::endl;
367 X->randomize();
368 X->norm2(initialNorms);
369 out << " ||X_initial|| = " << std::setiosflags(std::ios::fixed) << std::setprecision(10) << initialNorms[0] << std::endl;
370
371 jacSmoother->Apply(*X, *RHS, false); // nonzero initial guess
372
373 X->norm2(finalNorms);
374 Teuchos::Array<magnitude_type> residualNorm3 = Utilities::ResidualNorm(*A, *X, *RHS);
375 out << " ||Residual_final|| = " << std::setiosflags(std::ios::fixed) << std::setprecision(20) << residualNorm3[0] << std::endl;
376 out << " ||X_final|| = " << std::setiosflags(std::ios::fixed) << std::setprecision(10) << finalNorms[0] << std::endl;
377
378 TEUCHOS_TEST_COMPARE(residualNorm3[0], <, tol, out, success);
379 TEUCHOS_TEST_COMPARE(finalNorms[0] - Teuchos::ScalarTraits<Scalar>::magnitude(Teuchos::ScalarTraits<Scalar>::one()), <, tol, out, success);
380
381 if (comm->getSize() == 1) {
382 TEST_EQUALITY(residualNorm1[0] == residualNorm2[0], true);
383 TEST_EQUALITY(residualNorm1[0] != residualNorm3[0], true);
384 } else {
385 out << "Pass/Fail is only checked in serial." << std::endl;
386 }
387 } // end UseTpetra
388 }
389
390 TEUCHOS_UNIT_TEST_TEMPLATE_4_DECL(BlockedSmoother, BGS_Setup_Apply, Scalar, LocalOrdinal, GlobalOrdinal, Node) {
391 #include <MueLu_UseShortNames.hpp>
392 MUELU_TESTING_SET_OSTREAM;
393 MUELU_TESTING_LIMIT_SCOPE(Scalar, GlobalOrdinal, Node);
394 MUELU_TEST_ONLY_FOR(Xpetra::UseTpetra) {
395 RCP<const Teuchos::Comm<int> > comm = Parameters::getDefaultComm();
396
397 int noBlocks = 4;
398 Teuchos::RCP<const BlockedCrsMatrix> bop = CreateBlockDiagonalExampleMatrix<Scalar, LocalOrdinal, GlobalOrdinal, Node, TpetraMap>(noBlocks, *comm);
399 Teuchos::RCP<const Matrix> Aconst = Teuchos::rcp_dynamic_cast<const Matrix>(bop);
400 Teuchos::RCP<Matrix> A = Teuchos::rcp_const_cast<Matrix>(Aconst);
401
402 // I don't use the testApply infrastructure because it has no provision for an initial guess.
403 Level level;
404 TestHelpers::TestFactory<Scalar, LocalOrdinal, GlobalOrdinal, Node>::createSingleLevelHierarchy(level);
405 level.Set("A", A);
406
407 //////////////////////////////////////////////////////////////////////
408 // Smoothers
409 RCP<BlockedGaussSeidelSmoother> smootherPrototype = rcp(new BlockedGaussSeidelSmoother());
410 smootherPrototype->SetParameter("Sweeps", Teuchos::ParameterEntry(1));
411
412 std::vector<RCP<SubBlockAFactory> > sA(noBlocks, Teuchos::null);
413 std::vector<RCP<SmootherFactory> > sF(noBlocks, Teuchos::null);
414 std::vector<RCP<FactoryManager> > sM(noBlocks, Teuchos::null);
415 for (int k = 0; k < noBlocks; k++) {
416 std::string strInfo = std::string("{ 1 }");
417 sA[k] = rcp(new SubBlockAFactory());
418 sA[k]->SetFactory("A", MueLu::NoFactory::getRCP());
419 sA[k]->SetParameter("block row", Teuchos::ParameterEntry(k));
420 sA[k]->SetParameter("block col", Teuchos::ParameterEntry(k));
421 sA[k]->SetParameter("Range map: Striding info", Teuchos::ParameterEntry(strInfo));
422 sA[k]->SetParameter("Domain map: Striding info", Teuchos::ParameterEntry(strInfo));
423
424 RCP<SmootherPrototype> smoProto = rcp(new Ifpack2Smoother(std::string("RELAXATION"), Teuchos::ParameterList(), 0));
425 smoProto->SetFactory("A", sA[k]);
426 sF[k] = rcp(new SmootherFactory(smoProto));
427
428 sM[k] = rcp(new FactoryManager());
429 sM[k]->SetFactory("A", sA[k]);
430 sM[k]->SetFactory("Smoother", sF[k]);
431 sM[k]->SetIgnoreUserData(true);
432
433 smootherPrototype->AddFactoryManager(sM[k], k);
434 }
435
436 RCP<SmootherFactory> smootherFact = rcp(new SmootherFactory(smootherPrototype));
437
438 // main factory manager
439 FactoryManager M;
440 M.SetFactory("Smoother", smootherFact);
441
442 MueLu::SetFactoryManager SFM(Teuchos::rcpFromRef(level), Teuchos::rcpFromRef(M));
443
444 // request BGS smoother (and all dependencies) on level
445 level.Request("Smoother", smootherFact.get());
446 level.Request("PreSmoother", smootherFact.get());
447 level.Request("PostSmoother", smootherFact.get());
448
449 // smootherFact->DeclareInput(level);
450 smootherFact->Build(level);
451
452 level.print(std::cout, Teuchos::VERB_EXTREME);
453
454 RCP<SmootherBase> bgsSmoother = level.Get<RCP<SmootherBase> >("PreSmoother", smootherFact.get());
455
456 RCP<MultiVector> X = MultiVectorFactory::Build(A->getDomainMap(), 1);
457 RCP<MultiVector> RHS = MultiVectorFactory::Build(A->getRangeMap(), 1);
458
459 // Random X
460 X->setSeed(846930886);
461 X->randomize();
462
463 typedef typename Teuchos::ScalarTraits<Scalar>::magnitudeType magnitude_type;
464
465 // Normalize X
466 Array<magnitude_type> norms(1);
467 X->norm2(norms);
468 X->scale(1 / norms[0]);
469
470 // Compute RHS corresponding to X
471 A->apply(*X, *RHS, Teuchos::NO_TRANS, (SC)1.0, (SC)0.0);
472
473 // Reset X to 0
474 X->putScalar((SC)0.0);
475
476 RHS->norm2(norms);
477 out << "||RHS|| = " << std::setiosflags(std::ios::fixed) << std::setprecision(10) << norms[0] << std::endl;
478
479 out << "solve with zero initial guess" << std::endl;
480 Teuchos::Array<magnitude_type> initialNorms(1);
481 X->norm2(initialNorms);
482 out << " ||X_initial|| = " << std::setiosflags(std::ios::fixed) << std::setprecision(10) << initialNorms[0] << std::endl;
483
484 bgsSmoother->Apply(*X, *RHS, true); // zero initial guess
485
486 Teuchos::Array<magnitude_type> finalNorms(1);
487 X->norm2(finalNorms);
488 Teuchos::Array<magnitude_type> residualNorm1 = Utilities::ResidualNorm(*A, *X, *RHS);
489 out << " ||Residual_final|| = " << std::setiosflags(std::ios::fixed) << std::setprecision(20) << residualNorm1[0] << std::endl;
490 out << " ||X_final|| = " << std::setiosflags(std::ios::fixed) << std::setprecision(10) << finalNorms[0] << std::endl;
491
492 magnitude_type tol = 50. * Teuchos::ScalarTraits<Scalar>::eps();
493
494 TEUCHOS_TEST_COMPARE(residualNorm1[0], <, tol, out, success);
495 TEUCHOS_TEST_COMPARE(finalNorms[0] - Teuchos::ScalarTraits<Scalar>::magnitude(Teuchos::ScalarTraits<Scalar>::one()), <, tol, out, success);
496
497 out << "solve with zero initial guess, and unreliable nonzeroed vector X" << std::endl;
498 X->randomize();
499 X->norm2(initialNorms);
500 out << " ||X_initial|| = " << std::setiosflags(std::ios::fixed) << std::setprecision(10) << initialNorms[0] << std::endl;
501
502 bgsSmoother->Apply(*X, *RHS, true); // zero initial guess with nonzero X
503
504 X->norm2(finalNorms);
505 Teuchos::Array<magnitude_type> residualNorm2 = Utilities::ResidualNorm(*A, *X, *RHS);
506 out << " ||Residual_final|| = " << std::setiosflags(std::ios::fixed) << std::setprecision(20) << residualNorm2[0] << std::endl;
507 out << " ||X_final|| = " << std::setiosflags(std::ios::fixed) << std::setprecision(10) << finalNorms[0] << std::endl;
508
509 TEUCHOS_TEST_COMPARE(residualNorm2[0], <, tol, out, success);
510 TEUCHOS_TEST_COMPARE(finalNorms[0] - Teuchos::ScalarTraits<Scalar>::magnitude(Teuchos::ScalarTraits<Scalar>::one()), <, tol, out, success);
511
512 out << "solve with random initial guess" << std::endl;
513 X->randomize();
514 X->norm2(initialNorms);
515 out << " ||X_initial|| = " << std::setiosflags(std::ios::fixed) << std::setprecision(10) << initialNorms[0] << std::endl;
516
517 bgsSmoother->Apply(*X, *RHS, false); // nonzero initial guess
518
519 X->norm2(finalNorms);
520 Teuchos::Array<magnitude_type> residualNorm3 = Utilities::ResidualNorm(*A, *X, *RHS);
521 out << " ||Residual_final|| = " << std::setiosflags(std::ios::fixed) << std::setprecision(20) << residualNorm3[0] << std::endl;
522 out << " ||X_final|| = " << std::setiosflags(std::ios::fixed) << std::setprecision(10) << finalNorms[0] << std::endl;
523
524 TEUCHOS_TEST_COMPARE(residualNorm3[0], <, tol, out, success);
525 TEUCHOS_TEST_COMPARE(finalNorms[0] - Teuchos::ScalarTraits<Scalar>::magnitude(Teuchos::ScalarTraits<Scalar>::one()), <, tol, out, success);
526
527 if (comm->getSize() == 1) {
528 TEST_EQUALITY(residualNorm1[0] == residualNorm2[0], true);
529 TEST_EQUALITY(residualNorm1[0] != residualNorm3[0], true);
530 } else {
531 out << "Pass/Fail is only checked in serial." << std::endl;
532 }
533 } // end UseTpetra
534 }
535
536 TEUCHOS_UNIT_TEST_TEMPLATE_4_DECL(BlockedSmoother, Reordered_BGS_Setup_Apply, Scalar, LocalOrdinal, GlobalOrdinal, Node) {
537 #include <MueLu_UseShortNames.hpp>
538 MUELU_TESTING_SET_OSTREAM;
539 MUELU_TESTING_LIMIT_SCOPE(Scalar, GlobalOrdinal, Node);
540 MUELU_TEST_ONLY_FOR(Xpetra::UseTpetra) {
541 RCP<const Teuchos::Comm<int> > comm = Parameters::getDefaultComm();
542
543 int noBlocks = 4;
544 Teuchos::RCP<const BlockedCrsMatrix> bop = CreateBlockDiagonalExampleMatrix<Scalar, LocalOrdinal, GlobalOrdinal, Node, TpetraMap>(noBlocks, *comm);
545 Teuchos::RCP<const Matrix> Aconst = Teuchos::rcp_dynamic_cast<const Matrix>(bop);
546 Teuchos::RCP<Matrix> A = Teuchos::rcp_const_cast<Matrix>(Aconst);
547
548 // I don't use the testApply infrastructure because it has no provision for an initial guess.
549 Level level;
550 TestHelpers::TestFactory<Scalar, LocalOrdinal, GlobalOrdinal, Node>::createSingleLevelHierarchy(level);
551 level.Set("A", A);
552
553 // Test ReorderBlockAFactory
554 Teuchos::RCP<ReorderBlockAFactory> rAFact = Teuchos::rcp(new ReorderBlockAFactory());
555 rAFact->SetFactory("A", MueLu::NoFactory::getRCP());
556 rAFact->SetParameter(std::string("Reorder Type"), Teuchos::ParameterEntry(std::string("[ 3 1 2 ]")));
557
558 int noBlocks2 = 3;
559
560 //////////////////////////////////////////////////////////////////////
561 // Smoothers
562 RCP<BlockedGaussSeidelSmoother> smootherPrototype = rcp(new BlockedGaussSeidelSmoother());
563 smootherPrototype->SetParameter("Sweeps", Teuchos::ParameterEntry(1));
564 smootherPrototype->SetFactory("A", rAFact);
565
566 std::vector<RCP<SubBlockAFactory> > sA(noBlocks2, Teuchos::null);
567 std::vector<RCP<SmootherFactory> > sF(noBlocks2, Teuchos::null);
568 std::vector<RCP<FactoryManager> > sM(noBlocks2, Teuchos::null);
569 for (int k = 0; k < noBlocks2; k++) {
570 std::string strInfo = std::string("{ 1 }");
571 sA[k] = rcp(new SubBlockAFactory());
572 sA[k]->SetFactory("A", rAFact);
573 sA[k]->SetParameter("block row", Teuchos::ParameterEntry(k));
574 sA[k]->SetParameter("block col", Teuchos::ParameterEntry(k));
575 sA[k]->SetParameter("Range map: Striding info", Teuchos::ParameterEntry(strInfo));
576 sA[k]->SetParameter("Domain map: Striding info", Teuchos::ParameterEntry(strInfo));
577
578 RCP<SmootherPrototype> smoProto = rcp(new Ifpack2Smoother(std::string("RELAXATION"), Teuchos::ParameterList(), 0));
579 smoProto->SetFactory("A", sA[k]);
580 sF[k] = rcp(new SmootherFactory(smoProto));
581
582 sM[k] = rcp(new FactoryManager());
583 sM[k]->SetFactory("A", sA[k]);
584 sM[k]->SetFactory("Smoother", sF[k]);
585 sM[k]->SetIgnoreUserData(true);
586
587 smootherPrototype->AddFactoryManager(sM[k], k);
588 }
589
590 RCP<SmootherFactory> smootherFact = rcp(new SmootherFactory(smootherPrototype));
591
592 // main factory manager
593 FactoryManager M;
594 M.SetFactory("A", rAFact);
595 M.SetFactory("Smoother", smootherFact);
596
597 MueLu::SetFactoryManager SFM(Teuchos::rcpFromRef(level), Teuchos::rcpFromRef(M));
598
599 // request BGS smoother (and all dependencies) on level
600 level.Request("A", rAFact.get());
601 level.Request("Smoother", smootherFact.get());
602 level.Request("PreSmoother", smootherFact.get());
603 level.Request("PostSmoother", smootherFact.get());
604
605 smootherFact->Build(level);
606
607 level.print(std::cout, Teuchos::VERB_EXTREME);
608
609 RCP<SmootherBase> bgsSmoother = level.Get<RCP<SmootherBase> >("PreSmoother", smootherFact.get());
610
611 RCP<Matrix> reorderedA = level.Get<RCP<Matrix> >("A", rAFact.get());
612 RCP<BlockedCrsMatrix> reorderedbA = Teuchos::rcp_dynamic_cast<BlockedCrsMatrix>(reorderedA);
613
614 RCP<MultiVector> X = MultiVectorFactory::Build(reorderedA->getDomainMap(), 1);
615 RCP<MultiVector> RHS = MultiVectorFactory::Build(reorderedA->getRangeMap(), 1);
616
617 // Random X
618 X->putScalar(0.0);
619 RHS->putScalar(1.0);
620
621 bgsSmoother->Apply(*X, *RHS, true); // zero initial guess
622
623 typedef typename Teuchos::ScalarTraits<Scalar>::magnitudeType magnitude_type;
624 Teuchos::Array<magnitude_type> finalNorms(1);
625 X->norm2(finalNorms);
626 Teuchos::Array<magnitude_type> residualNorm1 = Utilities::ResidualNorm(*reorderedA, *X, *RHS);
627 out << " ||Residual_final|| = " << std::setiosflags(std::ios::fixed) << std::setprecision(20) << residualNorm1[0] << std::endl;
628 out << " ||X_final|| = " << std::setiosflags(std::ios::fixed) << std::setprecision(10) << finalNorms[0] << std::endl;
629
630 magnitude_type tol = 50. * Teuchos::ScalarTraits<Scalar>::eps();
631
632 TEUCHOS_TEST_COMPARE(residualNorm1[0], <, tol, out, success);
633
634 Teuchos::RCP<const MapExtractor> doMapExtractor = reorderedbA->getDomainMapExtractor();
635
636 Teuchos::RCP<MultiVector> v0 = doMapExtractor->ExtractVector(X, 0);
637 Teuchos::RCP<MultiVector> v1 = doMapExtractor->ExtractVector(X, 1);
638 Teuchos::RCP<MultiVector> v2 = doMapExtractor->ExtractVector(X, 2);
639
640 TEST_EQUALITY((v0->getData(0))[0], Teuchos::as<Scalar>(0.25));
641 TEST_EQUALITY((v1->getData(0))[0], Teuchos::as<Scalar>(0.5));
642 TEST_EQUALITY((v2->getData(0))[0], Teuchos::as<Scalar>(1.0 / 3.0));
643
644 Teuchos::Array<magnitude_type> n0(1);
645 v0->norm1(n0);
646 Teuchos::Array<magnitude_type> n1(1);
647 v1->norm1(n1);
648 Teuchos::Array<magnitude_type> n2(1);
649 v2->norm1(n2);
650
651 TEST_EQUALITY(n0[0], Teuchos::as<Scalar>(v0->getGlobalLength() * 0.25));
652 TEST_EQUALITY(n1[0], Teuchos::as<Scalar>(v1->getGlobalLength() * 0.5));
653
654 TEST_EQUALITY(X->getMap()->getLocalElement(v0->getMap()->getMinGlobalIndex()), 0);
655 TEST_EQUALITY(X->getMap()->getLocalElement(v0->getMap()->getMaxGlobalIndex()), 19);
656 TEST_EQUALITY(v0->getMap()->getMinLocalIndex(), 0);
657 TEST_EQUALITY(v0->getMap()->getMaxLocalIndex(), 19);
658 TEST_EQUALITY(v0->getMap()->getMinAllGlobalIndex(), 20);
659 TEST_EQUALITY(v0->getMap()->getMinGlobalIndex(), comm->getRank() * 40 + 20);
660 TEST_EQUALITY(v0->getMap()->getMaxGlobalIndex(), comm->getRank() * 40 + 39);
661 TEST_EQUALITY(v0->getMap()->getMaxAllGlobalIndex(), comm->getSize() * 40 - 1);
662
663 TEST_EQUALITY(X->getMap()->getLocalElement(v1->getMap()->getMinGlobalIndex()), 20);
664 TEST_EQUALITY(X->getMap()->getLocalElement(v1->getMap()->getMaxGlobalIndex()), 24);
665 TEST_EQUALITY(v1->getMap()->getMinLocalIndex(), 0);
666 TEST_EQUALITY(v1->getMap()->getMaxLocalIndex(), 4);
667 TEST_EQUALITY(v1->getMap()->getMinAllGlobalIndex(), 5);
668 TEST_EQUALITY(v1->getMap()->getMinGlobalIndex(), comm->getRank() * 40 + 5);
669 TEST_EQUALITY(v1->getMap()->getMaxGlobalIndex(), comm->getRank() * 40 + 9);
670 TEST_EQUALITY(v1->getMap()->getMaxAllGlobalIndex(), comm->getSize() * 40 - 31);
671
672 TEST_EQUALITY(X->getMap()->getLocalElement(v2->getMap()->getMinGlobalIndex()), 25);
673 TEST_EQUALITY(X->getMap()->getLocalElement(v2->getMap()->getMaxGlobalIndex()), 34);
674 TEST_EQUALITY(v2->getMap()->getMinLocalIndex(), 0);
675 TEST_EQUALITY(v2->getMap()->getMaxLocalIndex(), 9);
676 TEST_EQUALITY(v2->getMap()->getMinAllGlobalIndex(), 10);
677 TEST_EQUALITY(v2->getMap()->getMinGlobalIndex(), comm->getRank() * 40 + 10);
678 TEST_EQUALITY(v2->getMap()->getMaxGlobalIndex(), comm->getRank() * 40 + 19);
679 TEST_EQUALITY(v2->getMap()->getMaxAllGlobalIndex(), comm->getSize() * 40 - 21);
680
681 } // end UseTpetra
682 }
683
684 TEUCHOS_UNIT_TEST_TEMPLATE_4_DECL(BlockedSmoother, NestedII30II12II_BGS_Setup_Apply, Scalar, LocalOrdinal, GlobalOrdinal, Node) {
685 #include <MueLu_UseShortNames.hpp>
686 MUELU_TESTING_SET_OSTREAM;
687 MUELU_TESTING_LIMIT_SCOPE(Scalar, GlobalOrdinal, Node);
688 MUELU_TEST_ONLY_FOR(Xpetra::UseTpetra) {
689 RCP<const Teuchos::Comm<int> > comm = Parameters::getDefaultComm();
690
691 int noBlocks = 4;
692 Teuchos::RCP<const BlockedCrsMatrix> bop = CreateBlockDiagonalExampleMatrix<Scalar, LocalOrdinal, GlobalOrdinal, Node, TpetraMap>(noBlocks, *comm);
693 Teuchos::RCP<const Matrix> Aconst = Teuchos::rcp_dynamic_cast<const Matrix>(bop);
694 Teuchos::RCP<Matrix> A = Teuchos::rcp_const_cast<Matrix>(Aconst);
695
696 // I don't use the testApply infrastructure because it has no provision for an initial guess.
697 Level level;
698 TestHelpers::TestFactory<Scalar, LocalOrdinal, GlobalOrdinal, Node>::createSingleLevelHierarchy(level);
699 level.Set("A", A);
700
701 // Test ReorderBlockAFactory
702 Teuchos::RCP<ReorderBlockAFactory> rAFact = Teuchos::rcp(new ReorderBlockAFactory());
703 rAFact->SetFactory("A", MueLu::NoFactory::getRCP());
704 rAFact->SetParameter(std::string("Reorder Type"), Teuchos::ParameterEntry(std::string("[ [ 3 0 ] [1 2] ]")));
705
706 //////////////////////////////////////////////////////////////////////
707 // global level
708 RCP<BlockedGaussSeidelSmoother> smootherPrototype = rcp(new BlockedGaussSeidelSmoother());
709 smootherPrototype->SetParameter("Sweeps", Teuchos::ParameterEntry(1));
710 smootherPrototype->SetFactory("A", rAFact);
711
712 for (int k = 0; k < 2; k++) {
713 Teuchos::RCP<SubBlockAFactory> sA = Teuchos::rcp(new SubBlockAFactory());
714 sA->SetFactory("A", rAFact);
715 sA->SetParameter("block row", Teuchos::ParameterEntry(k)); // local block indices relative to size of blocked operator
716 sA->SetParameter("block col", Teuchos::ParameterEntry(k));
717
718 Teuchos::RCP<BlockedGaussSeidelSmoother> sP = Teuchos::rcp(new BlockedGaussSeidelSmoother());
719 sP->SetParameter("Sweeps", Teuchos::ParameterEntry(1));
720 sP->SetFactory("A", sA);
721
722 for (int l = 0; l < 2; l++) {
723 std::string strInfo = std::string("{ 1 }");
724 Teuchos::RCP<SubBlockAFactory> ssA = rcp(new SubBlockAFactory());
725 ssA->SetFactory("A", sA);
726 ssA->SetParameter("block row", Teuchos::ParameterEntry(l)); // local block indices relative to size of blocked operator
727 ssA->SetParameter("block col", Teuchos::ParameterEntry(l));
728 ssA->SetParameter("Range map: Striding info", Teuchos::ParameterEntry(strInfo));
729 ssA->SetParameter("Domain map: Striding info", Teuchos::ParameterEntry(strInfo));
730 RCP<SmootherPrototype> ssP = rcp(new Ifpack2Smoother(std::string("RELAXATION"), Teuchos::ParameterList(), 0));
731 ssP->SetFactory("A", ssA);
732 Teuchos::RCP<SmootherFactory> ssF = Teuchos::rcp(new SmootherFactory(ssP));
733 Teuchos::RCP<FactoryManager> ssM = Teuchos::rcp(new FactoryManager());
734 ssM->SetFactory("A", ssA);
735 ssM->SetFactory("Smoother", ssF);
736 ssM->SetIgnoreUserData(true);
737 sP->AddFactoryManager(ssM, l);
738 }
739
740 Teuchos::RCP<SmootherFactory> sF = Teuchos::rcp(new SmootherFactory(sP));
741 Teuchos::RCP<FactoryManager> sM = Teuchos::rcp(new FactoryManager());
742 sM->SetFactory("A", sA);
743 sM->SetFactory("Smoother", sF);
744 sM->SetIgnoreUserData(true);
745 smootherPrototype->AddFactoryManager(sM, k);
746 }
747
748 // create master smoother factory
749 RCP<SmootherFactory> smootherFact = rcp(new SmootherFactory(smootherPrototype));
750
751 // main factory manager
752 FactoryManager M;
753 M.SetFactory("A", rAFact);
754 M.SetFactory("Smoother", smootherFact);
755
756 MueLu::SetFactoryManager SFM(Teuchos::rcpFromRef(level), Teuchos::rcpFromRef(M));
757
758 // request BGS smoother (and all dependencies) on level
759 level.Request("A", rAFact.get());
760 level.Request("Smoother", smootherFact.get());
761 level.Request("PreSmoother", smootherFact.get());
762 level.Request("PostSmoother", smootherFact.get());
763
764 smootherFact->Build(level);
765
766 RCP<SmootherBase> bgsSmoother = level.Get<RCP<SmootherBase> >("PreSmoother", smootherFact.get());
767
768 RCP<Matrix> reorderedA = level.Get<RCP<Matrix> >("A", rAFact.get());
769 RCP<BlockedCrsMatrix> reorderedbA = Teuchos::rcp_dynamic_cast<BlockedCrsMatrix>(reorderedA);
770
771 TEST_EQUALITY(reorderedbA->Rows(), 2);
772 TEST_EQUALITY(reorderedbA->Cols(), 2);
773
774 RCP<MultiVector> X = MultiVectorFactory::Build(reorderedA->getDomainMap(), 1);
775 RCP<MultiVector> RHS = MultiVectorFactory::Build(reorderedA->getRangeMap(), 1);
776
777 // Random X
778 X->putScalar(0.0);
779 RHS->putScalar(1.0);
780
781 bgsSmoother->Apply(*X, *RHS, true); // zero initial guess
782
783 typedef typename Teuchos::ScalarTraits<Scalar>::magnitudeType magnitude_type;
784 Teuchos::Array<magnitude_type> finalNorms(1);
785 X->norm2(finalNorms);
786 Teuchos::Array<magnitude_type> residualNorm1 = Utilities::ResidualNorm(*reorderedA, *X, *RHS);
787 out << " ||Residual_final|| = " << std::setiosflags(std::ios::fixed) << std::setprecision(20) << residualNorm1[0] << std::endl;
788 out << " ||X_final|| = " << std::setiosflags(std::ios::fixed) << std::setprecision(10) << finalNorms[0] << std::endl;
789
790 magnitude_type tol = 50. * Teuchos::ScalarTraits<Scalar>::eps();
791
792 TEUCHOS_TEST_COMPARE(residualNorm1[0], <, tol, out, success);
793
794 Teuchos::RCP<const MapExtractor> doMapExtractor = reorderedbA->getDomainMapExtractor();
795
796 Teuchos::RCP<MultiVector> v0 = doMapExtractor->ExtractVector(X, 0);
797 Teuchos::RCP<MultiVector> v1 = doMapExtractor->ExtractVector(X, 1);
798
799 Teuchos::RCP<BlockedMultiVector> bv0 = Teuchos::rcp_dynamic_cast<BlockedMultiVector>(v0);
800 Teuchos::RCP<BlockedMultiVector> bv1 = Teuchos::rcp_dynamic_cast<BlockedMultiVector>(v1);
801 TEST_EQUALITY(bv0.is_null(), false);
802 TEST_EQUALITY(bv1.is_null(), false);
803
804 Teuchos::RCP<MultiVector> bv00 = bv0->getMultiVector(0, false);
805 Teuchos::RCP<MultiVector> bv01 = bv0->getMultiVector(1, false);
806 Teuchos::RCP<MultiVector> bv10 = bv1->getMultiVector(0, false);
807 Teuchos::RCP<MultiVector> bv11 = bv1->getMultiVector(1, false);
808
809 TEST_EQUALITY((bv00->getData(0))[0], Teuchos::as<Scalar>(0.25));
810 TEST_EQUALITY((bv01->getData(0))[0], Teuchos::as<Scalar>(1.0));
811 TEST_EQUALITY((bv10->getData(0))[0], Teuchos::as<Scalar>(0.5));
812
813 Teuchos::Array<magnitude_type> n0(1);
814 v0->norm1(n0);
815 Teuchos::Array<magnitude_type> n1(1);
816 v1->norm1(n1);
817
818 TEST_EQUALITY(n0[0], Teuchos::as<Scalar>(comm->getSize() * 10.0));
819 TEUCHOS_TEST_COMPARE(n1[0], <, comm->getSize() * 5.84, out, success);
820 TEUCHOS_TEST_COMPARE(n1[0], >, comm->getSize() * 5.83, out, success);
821
822 TEST_EQUALITY(v0->getMap()->getMinLocalIndex(), 0);
823 TEST_EQUALITY(v0->getMap()->getMaxLocalIndex(), 24);
824 TEST_EQUALITY(v0->getMap()->getMinAllGlobalIndex(), 0);
825 TEST_EQUALITY(v0->getMap()->getMinGlobalIndex(), comm->getRank() * 40 + 0);
826 TEST_EQUALITY(v0->getMap()->getMaxGlobalIndex(), comm->getRank() * 40 + 39);
827 TEST_EQUALITY(v0->getMap()->getMaxAllGlobalIndex(), comm->getSize() * 40 - 1);
828
829 TEST_EQUALITY(v1->getMap()->getMinLocalIndex(), 0);
830 TEST_EQUALITY(v1->getMap()->getMaxLocalIndex(), 14);
831 TEST_EQUALITY(v1->getMap()->getMinAllGlobalIndex(), 5);
832 TEST_EQUALITY(v1->getMap()->getMinGlobalIndex(), comm->getRank() * 40 + 5);
833 TEST_EQUALITY(v1->getMap()->getMaxGlobalIndex(), comm->getRank() * 40 + 19);
834 TEST_EQUALITY(v1->getMap()->getMaxAllGlobalIndex(), comm->getSize() * 40 - 21);
835
836 Teuchos::RCP<BlockedCrsMatrix> b00 = Teuchos::rcp_dynamic_cast<BlockedCrsMatrix>(reorderedbA->getMatrix(0, 0));
837 TEST_EQUALITY(b00->Rows(), 2);
838 TEST_EQUALITY(b00->Cols(), 2);
839
840 Teuchos::RCP<const MapExtractor> me00 = b00->getDomainMapExtractor();
841 Teuchos::RCP<MultiVector> v00 = me00->ExtractVector(v0, 0);
842 Teuchos::RCP<MultiVector> v01 = me00->ExtractVector(v0, 1);
843 TEST_EQUALITY((v00->getData(0))[0], Teuchos::as<Scalar>(0.25));
844 TEST_EQUALITY((v01->getData(0))[0], Teuchos::as<Scalar>(1.0));
845 TEST_EQUALITY(v00->getLocalLength(), 20);
846 TEST_EQUALITY(v01->getLocalLength(), 5);
847 TEST_EQUALITY(v00->getGlobalLength(), Teuchos::as<size_t>(comm->getSize() * 20));
848 TEST_EQUALITY(v01->getGlobalLength(), Teuchos::as<size_t>(comm->getSize() * 5));
849
850 TEST_EQUALITY(v00->getMap()->getMinLocalIndex(), 0);
851 TEST_EQUALITY(v00->getMap()->getMaxLocalIndex(), 19);
852 TEST_EQUALITY(v00->getMap()->getMinAllGlobalIndex(), 20);
853 TEST_EQUALITY(v00->getMap()->getMinGlobalIndex(), comm->getRank() * 40 + 20);
854 TEST_EQUALITY(v00->getMap()->getMaxGlobalIndex(), comm->getRank() * 40 + 39);
855 TEST_EQUALITY(v00->getMap()->getMaxAllGlobalIndex(), comm->getSize() * 40 - 1);
856
857 TEST_EQUALITY(v01->getMap()->getMinLocalIndex(), 0);
858 TEST_EQUALITY(v01->getMap()->getMaxLocalIndex(), 4);
859 TEST_EQUALITY(v01->getMap()->getMinAllGlobalIndex(), 0);
860 TEST_EQUALITY(v01->getMap()->getMinGlobalIndex(), comm->getRank() * 40 + 0);
861 TEST_EQUALITY(v01->getMap()->getMaxGlobalIndex(), comm->getRank() * 40 + 4);
862 TEST_EQUALITY(v01->getMap()->getMaxAllGlobalIndex(), comm->getSize() * 40 - 36);
863
864 Teuchos::RCP<BlockedCrsMatrix> b11 = Teuchos::rcp_dynamic_cast<BlockedCrsMatrix>(reorderedbA->getMatrix(1, 1));
865 TEST_EQUALITY(b11->Rows(), 2);
866 TEST_EQUALITY(b11->Cols(), 2);
867
868 Teuchos::RCP<const MapExtractor> me11 = b11->getDomainMapExtractor();
869 Teuchos::RCP<MultiVector> v10 = me11->ExtractVector(v1, 0);
870 Teuchos::RCP<MultiVector> v11 = me11->ExtractVector(v1, 1);
871 TEST_EQUALITY((v10->getData(0))[0], Teuchos::as<Scalar>(0.5));
872 TEST_EQUALITY(v10->getLocalLength(), 5);
873 TEST_EQUALITY(v11->getLocalLength(), 10);
874 TEST_EQUALITY(v10->getGlobalLength(), Teuchos::as<size_t>(comm->getSize() * 5));
875 TEST_EQUALITY(v11->getGlobalLength(), Teuchos::as<size_t>(comm->getSize() * 10));
876
877 TEST_EQUALITY(v10->getMap()->getMinLocalIndex(), 0);
878 TEST_EQUALITY(v10->getMap()->getMaxLocalIndex(), 4);
879 TEST_EQUALITY(v10->getMap()->getMinAllGlobalIndex(), 5);
880 TEST_EQUALITY(v10->getMap()->getMinGlobalIndex(), comm->getRank() * 40 + 5);
881 TEST_EQUALITY(v10->getMap()->getMaxGlobalIndex(), comm->getRank() * 40 + 9);
882 TEST_EQUALITY(v10->getMap()->getMaxAllGlobalIndex(), comm->getSize() * 40 - 31);
883
884 TEST_EQUALITY(v11->getMap()->getMinLocalIndex(), 0);
885 TEST_EQUALITY(v11->getMap()->getMaxLocalIndex(), 9);
886 TEST_EQUALITY(v11->getMap()->getMinAllGlobalIndex(), 10);
887 TEST_EQUALITY(v11->getMap()->getMinGlobalIndex(), comm->getRank() * 40 + 10);
888 TEST_EQUALITY(v11->getMap()->getMaxGlobalIndex(), comm->getRank() * 40 + 19);
889 TEST_EQUALITY(v11->getMap()->getMaxAllGlobalIndex(), comm->getSize() * 40 - 21);
890
891 Teuchos::Array<magnitude_type> n00(1);
892 v00->norm1(n00);
893 Teuchos::Array<magnitude_type> n11(1);
894 v01->norm1(n11);
895 Teuchos::Array<magnitude_type> n22(1);
896 v10->norm1(n22);
897 Teuchos::Array<magnitude_type> n33(1);
898 v11->norm1(n33);
899
900 TEST_EQUALITY(n00[0], Teuchos::as<Scalar>(v00->getGlobalLength() * 0.25));
901 TEST_EQUALITY(n11[0], Teuchos::as<Scalar>(v01->getGlobalLength() * 1.0));
902 TEST_EQUALITY(n22[0], Teuchos::as<Scalar>(v10->getGlobalLength() * 0.5));
903 TEST_FLOATING_EQUALITY(n33[0], v11->getGlobalLength() / 3.0, Teuchos::ScalarTraits<magnitude_type>::eps());
904
905 } // end UseTpetra
906 }
907
908 TEUCHOS_UNIT_TEST_TEMPLATE_4_DECL(BlockedSmoother, NestedII30II12II_BGS_Setup_Apply2, Scalar, LocalOrdinal, GlobalOrdinal, Node) {
909 #include <MueLu_UseShortNames.hpp>
910 MUELU_TESTING_SET_OSTREAM;
911 MUELU_TESTING_LIMIT_SCOPE(Scalar, GlobalOrdinal, Node);
912 MUELU_TEST_ONLY_FOR(Xpetra::UseTpetra) {
913 RCP<const Teuchos::Comm<int> > comm = Parameters::getDefaultComm();
914
915 int noBlocks = 4;
916 Teuchos::RCP<const BlockedCrsMatrix> bop = CreateBlockDiagonalExampleMatrix<Scalar, LocalOrdinal, GlobalOrdinal, Node, TpetraMap>(noBlocks, *comm);
917 Teuchos::RCP<const Matrix> Aconst = Teuchos::rcp_dynamic_cast<const Matrix>(bop);
918 Teuchos::RCP<Matrix> A = Teuchos::rcp_const_cast<Matrix>(Aconst);
919
920 // I don't use the testApply infrastructure because it has no provision for an initial guess.
921 Level level;
922 TestHelpers::TestFactory<Scalar, LocalOrdinal, GlobalOrdinal, Node>::createSingleLevelHierarchy(level);
923 level.Set("A", A);
924
925 // Test ReorderBlockAFactory
926 Teuchos::RCP<ReorderBlockAFactory> rAFact = Teuchos::rcp(new ReorderBlockAFactory());
927 rAFact->SetFactory("A", MueLu::NoFactory::getRCP());
928 rAFact->SetParameter(std::string("Reorder Type"), Teuchos::ParameterEntry(std::string("[ [ 3 0 ] [1 2] ]")));
929
930 //////////////////////////////////////////////////////////////////////
931 // global level
932 RCP<BlockedGaussSeidelSmoother> smootherPrototype = rcp(new BlockedGaussSeidelSmoother());
933 smootherPrototype->SetParameter("Sweeps", Teuchos::ParameterEntry(1));
934 smootherPrototype->SetFactory("A", rAFact);
935
936 for (int k = 0; k < 2; k++) {
937 Teuchos::RCP<SubBlockAFactory> sA = Teuchos::rcp(new SubBlockAFactory());
938 sA->SetFactory("A", rAFact);
939 sA->SetParameter("block row", Teuchos::ParameterEntry(k)); // local block indices relative to size of blocked operator
940 sA->SetParameter("block col", Teuchos::ParameterEntry(k));
941
942 Teuchos::RCP<BlockedGaussSeidelSmoother> sP = Teuchos::rcp(new BlockedGaussSeidelSmoother());
943 sP->SetParameter("Sweeps", Teuchos::ParameterEntry(1));
944 sP->SetFactory("A", sA);
945
946 for (int l = 0; l < 2; l++) {
947 std::string strInfo = std::string("{ 1 }");
948 Teuchos::RCP<SubBlockAFactory> ssA = rcp(new SubBlockAFactory());
949 ssA->SetFactory("A", sA);
950 ssA->SetParameter("block row", Teuchos::ParameterEntry(l)); // local block indices relative to size of blocked operator
951 ssA->SetParameter("block col", Teuchos::ParameterEntry(l));
952 ssA->SetParameter("Range map: Striding info", Teuchos::ParameterEntry(strInfo));
953 ssA->SetParameter("Domain map: Striding info", Teuchos::ParameterEntry(strInfo));
954 RCP<SmootherPrototype> ssP = rcp(new Ifpack2Smoother(std::string("RELAXATION"), Teuchos::ParameterList(), 0));
955 ssP->SetFactory("A", ssA);
956 Teuchos::RCP<SmootherFactory> ssF = Teuchos::rcp(new SmootherFactory(ssP));
957 Teuchos::RCP<FactoryManager> ssM = Teuchos::rcp(new FactoryManager());
958 ssM->SetFactory("A", ssA);
959 ssM->SetFactory("Smoother", ssF);
960 ssM->SetIgnoreUserData(true);
961 sP->AddFactoryManager(ssM, l);
962 }
963
964 Teuchos::RCP<SmootherFactory> sF = Teuchos::rcp(new SmootherFactory(sP));
965 Teuchos::RCP<FactoryManager> sM = Teuchos::rcp(new FactoryManager());
966 sM->SetFactory("A", sA);
967 sM->SetFactory("Smoother", sF);
968 sM->SetIgnoreUserData(true);
969 smootherPrototype->AddFactoryManager(sM, k);
970 }
971
972 // create master smoother factory
973 RCP<SmootherFactory> smootherFact = rcp(new SmootherFactory(smootherPrototype));
974
975 // main factory manager
976 FactoryManager M;
977 M.SetFactory("A", rAFact);
978 M.SetFactory("Smoother", smootherFact);
979
980 MueLu::SetFactoryManager SFM(Teuchos::rcpFromRef(level), Teuchos::rcpFromRef(M));
981
982 // request BGS smoother (and all dependencies) on level
983 level.Request("A", rAFact.get());
984 level.Request("Smoother", smootherFact.get());
985 level.Request("PreSmoother", smootherFact.get());
986 level.Request("PostSmoother", smootherFact.get());
987
988 smootherFact->Build(level);
989
990 RCP<SmootherBase> bgsSmoother = level.Get<RCP<SmootherBase> >("PreSmoother", smootherFact.get());
991
992 RCP<Matrix> reorderedA = level.Get<RCP<Matrix> >("A", rAFact.get());
993 RCP<ReorderedBlockedCrsMatrix> reorderedbA = Teuchos::rcp_dynamic_cast<ReorderedBlockedCrsMatrix>(reorderedA);
994
995 TEST_EQUALITY(reorderedbA->Rows(), 2);
996 TEST_EQUALITY(reorderedbA->Cols(), 2);
997
998 RCP<BlockedMultiVector> bX = Teuchos::rcp(new BlockedMultiVector(bop->getBlockedDomainMap(), 1, true));
999 RCP<BlockedMultiVector> bRHS = Teuchos::rcp(new BlockedMultiVector(bop->getBlockedRangeMap(), 1, true));
1000
1001 RCP<MultiVector> X = bX->Merge();
1002 RCP<MultiVector> RHS = bRHS->Merge();
1003
1004 // Random X
1005 X->putScalar(0.0);
1006 RHS->putScalar(1.0);
1007
1008 bgsSmoother->Apply(*X, *RHS, true); // zero initial guess
1009
1010 typedef typename Teuchos::ScalarTraits<Scalar>::magnitudeType magnitude_type;
1011 Teuchos::Array<magnitude_type> finalNorms(1);
1012 X->norm2(finalNorms);
1013 Teuchos::Array<magnitude_type> residualNorm1 = Utilities::ResidualNorm(*reorderedA, *X, *RHS);
1014 out << " ||Residual_final|| = " << std::setiosflags(std::ios::fixed) << std::setprecision(20) << residualNorm1[0] << std::endl;
1015 out << " ||X_final|| = " << std::setiosflags(std::ios::fixed) << std::setprecision(10) << finalNorms[0] << std::endl;
1016
1017 magnitude_type tol = 50. * Teuchos::ScalarTraits<Scalar>::eps();
1018
1019 TEUCHOS_TEST_COMPARE(residualNorm1[0], <, tol, out, success);
1020
1021 Teuchos::RCP<const MapExtractor> doMapExtractor = reorderedbA->getDomainMapExtractor();
1022
1023 {
1024 RCP<BlockedMultiVector> test = Teuchos::rcp(new BlockedMultiVector(bop->getBlockedDomainMap(), X));
1025 bX.swap(test);
1026
1027 Teuchos::RCP<const Xpetra::BlockReorderManager> brm = Xpetra::blockedReorderFromString("[ [0 1] [2 3] ]");
1028 test = Teuchos::rcp_dynamic_cast<BlockedMultiVector>(buildReorderedBlockedMultiVector(brm, bX));
1029 bX.swap(test);
1030 }
1031
1032 Teuchos::RCP<MultiVector> v0 = doMapExtractor->ExtractVector(bX, 0);
1033 Teuchos::RCP<MultiVector> v1 = doMapExtractor->ExtractVector(bX, 1);
1034
1035 Teuchos::RCP<BlockedMultiVector> bv0 = Teuchos::rcp_dynamic_cast<BlockedMultiVector>(v0);
1036 Teuchos::RCP<BlockedMultiVector> bv1 = Teuchos::rcp_dynamic_cast<BlockedMultiVector>(v1);
1037 TEST_EQUALITY(bv0.is_null(), false);
1038 TEST_EQUALITY(bv1.is_null(), false);
1039
1040 Teuchos::RCP<MultiVector> bv00 = bv0->getMultiVector(0, false);
1041 Teuchos::RCP<MultiVector> bv01 = bv0->getMultiVector(1, false);
1042 Teuchos::RCP<MultiVector> bv10 = bv1->getMultiVector(0, false);
1043 Teuchos::RCP<MultiVector> bv11 = bv1->getMultiVector(1, false);
1044
1045 TEST_EQUALITY((bv00->getData(0))[0], Teuchos::as<Scalar>(0.25));
1046 TEST_EQUALITY((bv01->getData(0))[0], Teuchos::as<Scalar>(0.25));
1047 TEST_EQUALITY((bv10->getData(0))[0], Teuchos::as<Scalar>(0.25));
1048
1049 Teuchos::Array<magnitude_type> n0(1);
1050 v0->norm1(n0);
1051 Teuchos::Array<magnitude_type> n1(1);
1052 v1->norm1(n1);
1053
1054 TEST_EQUALITY(n0[0], Teuchos::as<Scalar>(comm->getSize() * 2.5));
1055 TEUCHOS_TEST_COMPARE(n1[0], <, comm->getSize() * 13.34, out, success);
1056 TEUCHOS_TEST_COMPARE(n1[0], >, comm->getSize() * 13.33, out, success);
1057
1058 TEST_EQUALITY(v0->getMap()->getMinLocalIndex(), 0);
1059 TEST_EQUALITY(v0->getMap()->getMaxLocalIndex(), 9);
1060 TEST_EQUALITY(v0->getMap()->getMinAllGlobalIndex(), 0);
1061 TEST_EQUALITY(v0->getMap()->getMinGlobalIndex(), comm->getRank() * 40);
1062 TEST_EQUALITY(v0->getMap()->getMaxGlobalIndex(), comm->getRank() * 40 + 9);
1063 TEST_EQUALITY(v0->getMap()->getMaxAllGlobalIndex(), comm->getSize() * 40 - 31);
1064
1065 TEST_EQUALITY(v1->getMap()->getMinLocalIndex(), 0);
1066 TEST_EQUALITY(v1->getMap()->getMaxLocalIndex(), 29);
1067 TEST_EQUALITY(v1->getMap()->getMinAllGlobalIndex(), 10);
1068 TEST_EQUALITY(v1->getMap()->getMinGlobalIndex(), comm->getRank() * 40 + 10);
1069 TEST_EQUALITY(v1->getMap()->getMaxGlobalIndex(), comm->getRank() * 40 + 39);
1070 TEST_EQUALITY(v1->getMap()->getMaxAllGlobalIndex(), comm->getSize() * 40 - 1);
1071
1072 Teuchos::RCP<BlockedCrsMatrix> b00 = Teuchos::rcp_dynamic_cast<BlockedCrsMatrix>(reorderedbA->getMatrix(0, 0));
1073 TEST_EQUALITY(b00->Rows(), 2);
1074 TEST_EQUALITY(b00->Cols(), 2);
1075
1076 Teuchos::RCP<const MapExtractor> me00 = b00->getDomainMapExtractor();
1077 Teuchos::RCP<MultiVector> v00 = me00->ExtractVector(v0, 0);
1078 Teuchos::RCP<MultiVector> v01 = me00->ExtractVector(v0, 1);
1079 TEST_EQUALITY((v00->getData(0))[0], Teuchos::as<Scalar>(0.25));
1080 TEST_EQUALITY((v01->getData(0))[0], Teuchos::as<Scalar>(0.25));
1081 TEST_EQUALITY(v00->getLocalLength(), 5);
1082 TEST_EQUALITY(v01->getLocalLength(), 5);
1083 TEST_EQUALITY(v00->getGlobalLength(), Teuchos::as<size_t>(comm->getSize() * 5));
1084 TEST_EQUALITY(v01->getGlobalLength(), Teuchos::as<size_t>(comm->getSize() * 5));
1085
1086 TEST_EQUALITY(v00->getMap()->getMinLocalIndex(), 0);
1087 TEST_EQUALITY(v00->getMap()->getMaxLocalIndex(), 4);
1088 TEST_EQUALITY(v00->getMap()->getMinAllGlobalIndex(), 0);
1089 TEST_EQUALITY(v00->getMap()->getMinGlobalIndex(), comm->getRank() * 40);
1090 TEST_EQUALITY(v00->getMap()->getMaxGlobalIndex(), comm->getRank() * 40 + 4);
1091 TEST_EQUALITY(v00->getMap()->getMaxAllGlobalIndex(), comm->getSize() * 40 - 36);
1092
1093 TEST_EQUALITY(v01->getMap()->getMinLocalIndex(), 0);
1094 TEST_EQUALITY(v01->getMap()->getMaxLocalIndex(), 4);
1095 TEST_EQUALITY(v01->getMap()->getMinAllGlobalIndex(), 5);
1096 TEST_EQUALITY(v01->getMap()->getMinGlobalIndex(), comm->getRank() * 40 + 5);
1097 TEST_EQUALITY(v01->getMap()->getMaxGlobalIndex(), comm->getRank() * 40 + 9);
1098 TEST_EQUALITY(v01->getMap()->getMaxAllGlobalIndex(), comm->getSize() * 40 - 31);
1099
1100 Teuchos::RCP<BlockedCrsMatrix> b11 = Teuchos::rcp_dynamic_cast<BlockedCrsMatrix>(reorderedbA->getMatrix(1, 1));
1101 TEST_EQUALITY(b11->Rows(), 2);
1102 TEST_EQUALITY(b11->Cols(), 2);
1103
1104 Teuchos::RCP<const MapExtractor> me11 = b11->getDomainMapExtractor();
1105 Teuchos::RCP<MultiVector> v10 = me11->ExtractVector(v1, 0);
1106 Teuchos::RCP<MultiVector> v11 = me11->ExtractVector(v1, 1);
1107 TEST_EQUALITY((v10->getData(0))[0], Teuchos::as<Scalar>(0.25));
1108 TEST_EQUALITY(v10->getLocalLength(), 10);
1109 TEST_EQUALITY(v11->getLocalLength(), 20);
1110 TEST_EQUALITY(v10->getGlobalLength(), Teuchos::as<size_t>(comm->getSize() * 10));
1111 TEST_EQUALITY(v11->getGlobalLength(), Teuchos::as<size_t>(comm->getSize() * 20));
1112
1113 TEST_EQUALITY(v10->getMap()->getMinLocalIndex(), 0);
1114 TEST_EQUALITY(v10->getMap()->getMaxLocalIndex(), 9);
1115 TEST_EQUALITY(v10->getMap()->getMinAllGlobalIndex(), 10);
1116 TEST_EQUALITY(v10->getMap()->getMinGlobalIndex(), comm->getRank() * 40 + 10);
1117 TEST_EQUALITY(v10->getMap()->getMaxGlobalIndex(), comm->getRank() * 40 + 19);
1118 TEST_EQUALITY(v10->getMap()->getMaxAllGlobalIndex(), comm->getSize() * 40 - 21);
1119
1120 TEST_EQUALITY(v11->getMap()->getMinLocalIndex(), 0);
1121 TEST_EQUALITY(v11->getMap()->getMaxLocalIndex(), 19);
1122 TEST_EQUALITY(v11->getMap()->getMinAllGlobalIndex(), 20);
1123 TEST_EQUALITY(v11->getMap()->getMinGlobalIndex(), comm->getRank() * 40 + 20);
1124 TEST_EQUALITY(v11->getMap()->getMaxGlobalIndex(), comm->getRank() * 40 + 39);
1125 TEST_EQUALITY(v11->getMap()->getMaxAllGlobalIndex(), comm->getSize() * 40 - 1);
1126
1127 Teuchos::Array<magnitude_type> n00(1);
1128 v00->norm1(n00);
1129 Teuchos::Array<magnitude_type> n11(1);
1130 v01->norm1(n11);
1131 Teuchos::Array<magnitude_type> n22(1);
1132 v10->norm1(n22);
1133 Teuchos::Array<magnitude_type> n33(1);
1134 v11->norm1(n33);
1135
1136 TEST_EQUALITY(n00[0], Teuchos::as<Scalar>(v00->getGlobalLength() * 0.25));
1137 TEST_EQUALITY(n11[0], Teuchos::as<Scalar>(v01->getGlobalLength() * 0.25));
1138 TEST_EQUALITY(n22[0], Teuchos::as<Scalar>(v10->getGlobalLength() * 0.25));
1139 TEUCHOS_TEST_COMPARE(n33[0], <, v11->getGlobalLength() * 0.54167, out, success);
1140 TEUCHOS_TEST_COMPARE(n33[0], >, v11->getGlobalLength() * 0.54166, out, success);
1141
1142 } // end UseTpetra
1143 }
1144
1145 TEUCHOS_UNIT_TEST_TEMPLATE_4_DECL(BlockedSmoother, Thyra_BGS_Setup_Apply, Scalar, LocalOrdinal, GlobalOrdinal, Node) {
1146 #include <MueLu_UseShortNames.hpp>
1147 MUELU_TESTING_SET_OSTREAM;
1148 MUELU_TESTING_LIMIT_SCOPE(Scalar, GlobalOrdinal, Node);
1149 MUELU_TEST_ONLY_FOR(Xpetra::UseTpetra) {
1150 RCP<const Teuchos::Comm<int> > comm = Parameters::getDefaultComm();
1151
1152 RCP<BlockedCrsMatrix> A = CreateBlockedMatrixThyra<Scalar, LocalOrdinal, GlobalOrdinal, Node>(*comm, Xpetra::UseTpetra);
1153
1154 TEST_EQUALITY(A->Rows(), 3);
1155 TEST_EQUALITY(A->Cols(), 3);
1156 TEST_EQUALITY(A->getRangeMap()->getMinAllGlobalIndex(), 0);
1157 TEST_EQUALITY(A->getRangeMap()->getMaxAllGlobalIndex(), comm->getSize() * 300 - 1);
1158 TEST_EQUALITY(A->getRangeMap()->getMinGlobalIndex(), comm->getRank() * 100);
1159 TEST_EQUALITY(A->getRangeMap()->getMaxGlobalIndex(), comm->getSize() * 200 + comm->getRank() * 100 + 99);
1160
1161 TEST_EQUALITY(A->getRangeMap(0, false)->getMinAllGlobalIndex(), 0);
1162 TEST_EQUALITY(A->getRangeMap(0, false)->getMaxAllGlobalIndex(), comm->getSize() * 100 - 1);
1163 TEST_EQUALITY(A->getRangeMap(0, false)->getMinGlobalIndex(), comm->getRank() * 100);
1164 TEST_EQUALITY(A->getRangeMap(0, false)->getMaxGlobalIndex(), comm->getRank() * 100 + 99);
1165
1166 TEST_EQUALITY(A->getRangeMap(1, false)->getMinAllGlobalIndex(), comm->getSize() * 100);
1167 TEST_EQUALITY(A->getRangeMap(1, false)->getMaxAllGlobalIndex(), comm->getSize() * 100 * 2 - 1);
1168 TEST_EQUALITY(A->getRangeMap(1, false)->getMinGlobalIndex(), comm->getSize() * 100 + comm->getRank() * 100);
1169 TEST_EQUALITY(A->getRangeMap(1, false)->getMaxGlobalIndex(), comm->getSize() * 100 + comm->getRank() * 100 + 99);
1170
1171 TEST_EQUALITY(A->getRangeMap(2, false)->getMinAllGlobalIndex(), comm->getSize() * 200);
1172 TEST_EQUALITY(A->getRangeMap(2, false)->getMaxAllGlobalIndex(), comm->getSize() * 300 - 1);
1173 TEST_EQUALITY(A->getRangeMap(2, false)->getMinGlobalIndex(), comm->getSize() * 200 + comm->getRank() * 100);
1174 TEST_EQUALITY(A->getRangeMap(2, false)->getMaxGlobalIndex(), comm->getSize() * 200 + comm->getRank() * 100 + 99);
1175
1176 TEST_EQUALITY(A->getRangeMap(0, true)->getMinAllGlobalIndex(), 0);
1177 TEST_EQUALITY(A->getRangeMap(0, true)->getMaxAllGlobalIndex(), comm->getSize() * 100 - 1);
1178 TEST_EQUALITY(A->getRangeMap(0, true)->getMinGlobalIndex(), comm->getRank() * 100);
1179 TEST_EQUALITY(A->getRangeMap(0, true)->getMaxGlobalIndex(), comm->getRank() * 100 + 99);
1180
1181 TEST_EQUALITY(A->getRangeMap(1, true)->getMinAllGlobalIndex(), 0);
1182 TEST_EQUALITY(A->getRangeMap(1, true)->getMaxAllGlobalIndex(), comm->getSize() * 100 - 1);
1183 TEST_EQUALITY(A->getRangeMap(1, true)->getMinGlobalIndex(), comm->getRank() * 100);
1184 TEST_EQUALITY(A->getRangeMap(1, true)->getMaxGlobalIndex(), comm->getRank() * 100 + 99);
1185
1186 TEST_EQUALITY(A->getRangeMap(1, true)->getMinAllGlobalIndex(), 0);
1187 TEST_EQUALITY(A->getRangeMap(1, true)->getMaxAllGlobalIndex(), comm->getSize() * 100 - 1);
1188 TEST_EQUALITY(A->getRangeMap(1, true)->getMinGlobalIndex(), comm->getRank() * 100);
1189 TEST_EQUALITY(A->getRangeMap(1, true)->getMaxGlobalIndex(), comm->getRank() * 100 + 99);
1190
1191 TEST_EQUALITY(A->getMatrix(0, 0)->getRangeMap()->getMinAllGlobalIndex(), 0);
1192 TEST_EQUALITY(A->getMatrix(0, 0)->getRangeMap()->getMaxAllGlobalIndex(), comm->getSize() * 100 - 1);
1193 TEST_EQUALITY(A->getMatrix(0, 0)->getRangeMap()->getMinGlobalIndex(), comm->getRank() * 100);
1194 TEST_EQUALITY(A->getMatrix(0, 0)->getRangeMap()->getMaxGlobalIndex(), comm->getRank() * 100 + 99);
1195
1196 TEST_EQUALITY(A->getMatrix(0, 0)->getDomainMap()->getMinAllGlobalIndex(), 0);
1197 TEST_EQUALITY(A->getMatrix(0, 0)->getDomainMap()->getMaxAllGlobalIndex(), comm->getSize() * 100 - 1);
1198 TEST_EQUALITY(A->getMatrix(0, 0)->getDomainMap()->getMinGlobalIndex(), comm->getRank() * 100);
1199 TEST_EQUALITY(A->getMatrix(0, 0)->getDomainMap()->getMaxGlobalIndex(), comm->getRank() * 100 + 99);
1200
1201 TEST_EQUALITY(A->getMatrix(2, 1)->getDomainMap()->getMinAllGlobalIndex(), 0);
1202 TEST_EQUALITY(A->getMatrix(2, 1)->getDomainMap()->getMaxAllGlobalIndex(), comm->getSize() * 100 - 1);
1203 TEST_EQUALITY(A->getMatrix(2, 1)->getDomainMap()->getMinGlobalIndex(), comm->getRank() * 100);
1204 TEST_EQUALITY(A->getMatrix(2, 1)->getDomainMap()->getMaxGlobalIndex(), comm->getRank() * 100 + 99);
1205
1206 // build gloabl vector with one entries
1207 Teuchos::RCP<Vector> ones = VectorFactory::Build(A->getRangeMap(), true);
1208 Teuchos::RCP<Vector> res = VectorFactory::Build(A->getRangeMap(), true);
1209 ones->putScalar(Teuchos::ScalarTraits<Scalar>::one());
1210
1211 A->apply(*ones, *res);
1212
1213 TEST_EQUALITY(res->norm1(), comm->getSize() * 100 * 2 + 6);
1214 TEST_EQUALITY(res->normInf(), 2);
1215
1216 Level level;
1217 TestHelpers::TestFactory<Scalar, LocalOrdinal, GlobalOrdinal, Node>::createSingleLevelHierarchy(level);
1218 level.Set("A", Teuchos::rcp_dynamic_cast<Matrix>(A));
1219
1220 //////////////////////////////////////////////////////////////////////
1221 // Smoothers
1222 RCP<BlockedGaussSeidelSmoother> smootherPrototype = rcp(new BlockedGaussSeidelSmoother());
1223 smootherPrototype->SetParameter("Sweeps", Teuchos::ParameterEntry(10));
1224
1225 int noBlocks = Teuchos::as<int>(A->Rows());
1226 std::vector<RCP<SubBlockAFactory> > sA(noBlocks, Teuchos::null);
1227 std::vector<RCP<SmootherFactory> > sF(noBlocks, Teuchos::null);
1228 std::vector<RCP<FactoryManager> > sM(noBlocks, Teuchos::null);
1229 for (int k = 0; k < noBlocks; k++) {
1230 std::string strInfo = std::string("{ 1 }");
1231 sA[k] = rcp(new SubBlockAFactory());
1232 sA[k]->SetFactory("A", MueLu::NoFactory::getRCP());
1233 sA[k]->SetParameter("block row", Teuchos::ParameterEntry(k));
1234 sA[k]->SetParameter("block col", Teuchos::ParameterEntry(k));
1235 sA[k]->SetParameter("Range map: Striding info", Teuchos::ParameterEntry(strInfo));
1236 sA[k]->SetParameter("Domain map: Striding info", Teuchos::ParameterEntry(strInfo));
1237
1238 Teuchos::ParameterList paramList;
1239 paramList.set("relaxation: sweeps", Teuchos::as<LocalOrdinal>(10));
1240 paramList.set("relaxation: damping factor", Teuchos::as<Scalar>(0.9));
1241
1242 RCP<SmootherPrototype> smoProto = rcp(new Ifpack2Smoother(std::string("RELAXATION"), paramList, 0));
1243 smoProto->SetFactory("A", sA[k]);
1244 sF[k] = rcp(new SmootherFactory(smoProto));
1245
1246 sM[k] = rcp(new FactoryManager());
1247 sM[k]->SetFactory("A", sA[k]);
1248 sM[k]->SetFactory("Smoother", sF[k]);
1249 sM[k]->SetIgnoreUserData(true);
1250
1251 smootherPrototype->AddFactoryManager(sM[k], k);
1252 }
1253
1254 RCP<SmootherFactory> smootherFact = rcp(new SmootherFactory(smootherPrototype));
1255
1256 // main factory manager
1257 FactoryManager M;
1258 M.SetFactory("Smoother", smootherFact);
1259
1260 MueLu::SetFactoryManager SFM(Teuchos::rcpFromRef(level), Teuchos::rcpFromRef(M));
1261
1262 // request BGS smoother (and all dependencies) on level
1263 level.Request("Smoother", smootherFact.get());
1264 level.Request("PreSmoother", smootherFact.get());
1265 level.Request("PostSmoother", smootherFact.get());
1266
1267 smootherFact->Build(level);
1268
1269 level.print(std::cout, Teuchos::VERB_EXTREME);
1270
1271 RCP<SmootherBase> bgsSmoother = level.Get<RCP<SmootherBase> >("PreSmoother", smootherFact.get());
1272
1273 RCP<MultiVector> X = MultiVectorFactory::Build(A->getDomainMap(), 1);
1274 RCP<MultiVector> RHS = MultiVectorFactory::Build(A->getRangeMap(), 1);
1275
1276 // Random X
1277 X->setSeed(846930886);
1278 X->randomize();
1279
1280 typedef typename Teuchos::ScalarTraits<Scalar>::magnitudeType magnitude_type;
1281
1282 // Normalize X
1283 Array<magnitude_type> norms(1);
1284 X->norm2(norms);
1285 X->scale(1 / norms[0]);
1286
1287 // Compute RHS corresponding to X
1288 A->apply(*X, *RHS, Teuchos::NO_TRANS, (SC)1.0, (SC)0.0);
1289
1290 // Reset X to 0
1291 X->putScalar((SC)0.0);
1292
1293 RHS->norm2(norms);
1294 out << "||RHS|| = " << std::setiosflags(std::ios::fixed) << std::setprecision(10) << norms[0] << std::endl;
1295
1296 out << "solve with zero initial guess" << std::endl;
1297 Teuchos::Array<magnitude_type> initialNorms(1);
1298 X->norm2(initialNorms);
1299 out << " ||X_initial|| = " << std::setiosflags(std::ios::fixed) << std::setprecision(10) << initialNorms[0] << std::endl;
1300
1301 bgsSmoother->Apply(*X, *RHS, true); // zero initial guess
1302
1303 Teuchos::Array<magnitude_type> finalNorms(1);
1304 X->norm2(finalNorms);
1305 Teuchos::Array<magnitude_type> residualNorm1 = Utilities::ResidualNorm(*A, *X, *RHS);
1306 out << " ||Residual_final|| = " << std::setiosflags(std::ios::fixed) << std::setprecision(20) << residualNorm1[0] << std::endl;
1307 out << " ||X_final|| = " << std::setiosflags(std::ios::fixed) << std::setprecision(10) << finalNorms[0] << std::endl;
1308
1309 TEUCHOS_TEST_COMPARE(residualNorm1[0], <, 5e-4, out, success);
1310
1311 out << "solve with random initial guess" << std::endl;
1312 X->randomize();
1313 X->norm2(initialNorms);
1314 out << " ||X_initial|| = " << std::setiosflags(std::ios::fixed) << std::setprecision(10) << initialNorms[0] << std::endl;
1315
1316 bgsSmoother->Apply(*X, *RHS, false); // nonzero initial guess
1317
1318 X->norm2(finalNorms);
1319 Teuchos::Array<magnitude_type> residualNorm2 = Utilities::ResidualNorm(*A, *X, *RHS);
1320 out << " ||Residual_final|| = " << std::setiosflags(std::ios::fixed) << std::setprecision(20) << residualNorm2[0] << std::endl;
1321 out << " ||X_final|| = " << std::setiosflags(std::ios::fixed) << std::setprecision(10) << finalNorms[0] << std::endl;
1322
1323 TEUCHOS_TEST_COMPARE(residualNorm2[0], <, 1e-2, out, success);
1324
1325 if (comm->getSize() == 1) {
1326 TEST_EQUALITY(residualNorm1[0] != residualNorm2[0], true);
1327 } else {
1328 out << "Pass/Fail is only checked in serial." << std::endl;
1329 }
1330 } // end Tpetra only
1331 }
1332
1333 TEUCHOS_UNIT_TEST_TEMPLATE_4_DECL(BlockedSmoother, Thyra_Nested_BGS_Setup_Apply, Scalar, LocalOrdinal, GlobalOrdinal, Node) {
1334 #include <MueLu_UseShortNames.hpp>
1335 MUELU_TESTING_SET_OSTREAM;
1336 MUELU_TESTING_LIMIT_SCOPE(Scalar, GlobalOrdinal, Node);
1337 MUELU_TEST_ONLY_FOR(Xpetra::UseTpetra) {
1338 RCP<const Teuchos::Comm<int> > comm = Parameters::getDefaultComm();
1339
1340 RCP<BlockedCrsMatrix> A = CreateBlockedMatrixThyra<Scalar, LocalOrdinal, GlobalOrdinal, Node>(*comm, Xpetra::UseTpetra);
1341
1342 // I don't use the testApply infrastructure because it has no provision for an initial guess.
1343 Level level;
1344 TestHelpers::TestFactory<Scalar, LocalOrdinal, GlobalOrdinal, Node>::createSingleLevelHierarchy(level);
1345 level.Set("A", Teuchos::rcp_dynamic_cast<Matrix>(A));
1346
1347 // Test ReorderBlockAFactory
1348 Teuchos::RCP<ReorderBlockAFactory> rAFact = Teuchos::rcp(new ReorderBlockAFactory());
1349 rAFact->SetFactory("A", MueLu::NoFactory::getRCP());
1350 rAFact->SetParameter(std::string("Reorder Type"), Teuchos::ParameterEntry(std::string("[ 0 [1 2] ]")));
1351
1352 //////////////////////////////////////////////////////////////////////
1353 // global level
1354 RCP<BlockedGaussSeidelSmoother> smootherPrototype = rcp(new BlockedGaussSeidelSmoother());
1355 smootherPrototype->SetParameter("Sweeps", Teuchos::ParameterEntry(30));
1356 smootherPrototype->SetFactory("A", rAFact);
1357
1358 std::string strInfo = std::string("{ 1 }");
1359
1360 Teuchos::ParameterList paramList;
1361 paramList.set("relaxation: sweeps", Teuchos::as<LocalOrdinal>(30));
1362 paramList.set("relaxation: damping factor", Teuchos::as<Scalar>(0.9));
1363
1364 Teuchos::RCP<SubBlockAFactory> sA00 = Teuchos::rcp(new SubBlockAFactory());
1365 sA00->SetFactory("A", rAFact);
1366 sA00->SetParameter("block row", Teuchos::ParameterEntry(0));
1367 sA00->SetParameter("block col", Teuchos::ParameterEntry(0));
1368 sA00->SetParameter("Range map: Striding info", Teuchos::ParameterEntry(strInfo));
1369 sA00->SetParameter("Domain map: Striding info", Teuchos::ParameterEntry(strInfo));
1370
1371 RCP<SmootherPrototype> sP00 = rcp(new Ifpack2Smoother(std::string("RELAXATION"), paramList, 0));
1372 sP00->SetFactory("A", sA00);
1373 Teuchos::RCP<SmootherFactory> sF00 = Teuchos::rcp(new SmootherFactory(sP00));
1374 Teuchos::RCP<FactoryManager> sM00 = Teuchos::rcp(new FactoryManager());
1375 sM00->SetFactory("A", sA00);
1376 sM00->SetFactory("Smoother", sF00);
1377 sM00->SetIgnoreUserData(true);
1378 smootherPrototype->AddFactoryManager(sM00, 0);
1379
1380 // 2x2 blocked operator
1381 Teuchos::RCP<SubBlockAFactory> sA11 = Teuchos::rcp(new SubBlockAFactory());
1382 sA11->SetFactory("A", rAFact);
1383 sA11->SetParameter("block row", Teuchos::ParameterEntry(1));
1384 sA11->SetParameter("block col", Teuchos::ParameterEntry(1));
1385
1386 Teuchos::RCP<BlockedGaussSeidelSmoother> sP11 = Teuchos::rcp(new BlockedGaussSeidelSmoother());
1387 sP11->SetParameter("Sweeps", Teuchos::ParameterEntry(1));
1388 sP11->SetFactory("A", sA11);
1389
1390 for (int l = 0; l < 2; l++) {
1391 Teuchos::RCP<SubBlockAFactory> ssA = rcp(new SubBlockAFactory());
1392 ssA->SetFactory("A", sA11);
1393 ssA->SetParameter("block row", Teuchos::ParameterEntry(l));
1394 ssA->SetParameter("block col", Teuchos::ParameterEntry(l));
1395 ssA->SetParameter("Range map: Striding info", Teuchos::ParameterEntry(strInfo));
1396 ssA->SetParameter("Domain map: Striding info", Teuchos::ParameterEntry(strInfo));
1397 RCP<SmootherPrototype> ssP = rcp(new Ifpack2Smoother(std::string("RELAXATION"), paramList, 0));
1398 ssP->SetFactory("A", ssA);
1399 Teuchos::RCP<SmootherFactory> ssF = Teuchos::rcp(new SmootherFactory(ssP));
1400 Teuchos::RCP<FactoryManager> ssM = Teuchos::rcp(new FactoryManager());
1401 ssM->SetFactory("A", ssA);
1402 ssM->SetFactory("Smoother", ssF);
1403 ssM->SetIgnoreUserData(true);
1404 sP11->AddFactoryManager(ssM, l);
1405 }
1406
1407 Teuchos::RCP<SmootherFactory> sF11 = Teuchos::rcp(new SmootherFactory(sP11));
1408 Teuchos::RCP<FactoryManager> sM11 = Teuchos::rcp(new FactoryManager());
1409 sM11->SetFactory("A", sA11);
1410 sM11->SetFactory("Smoother", sF11);
1411 sM11->SetIgnoreUserData(true);
1412 smootherPrototype->AddFactoryManager(sM11, 1);
1413
1414 // create master smoother factory
1415 RCP<SmootherFactory> smootherFact = rcp(new SmootherFactory(smootherPrototype));
1416
1417 // main factory manager
1418 FactoryManager M;
1419 M.SetFactory("A", rAFact);
1420 M.SetFactory("Smoother", smootherFact);
1421
1422 MueLu::SetFactoryManager SFM(Teuchos::rcpFromRef(level), Teuchos::rcpFromRef(M));
1423
1424 // request BGS smoother (and all dependencies) on level
1425 level.Request("A", rAFact.get());
1426 level.Request("Smoother", smootherFact.get());
1427 level.Request("PreSmoother", smootherFact.get());
1428 level.Request("PostSmoother", smootherFact.get());
1429
1430 smootherFact->Build(level);
1431
1432 RCP<SmootherBase> bgsSmoother = level.Get<RCP<SmootherBase> >("PreSmoother", smootherFact.get());
1433
1434 RCP<Matrix> reorderedA = level.Get<RCP<Matrix> >("A", rAFact.get());
1435 RCP<BlockedCrsMatrix> reorderedbA = Teuchos::rcp_dynamic_cast<BlockedCrsMatrix>(reorderedA);
1436
1437 TEST_EQUALITY(reorderedbA->Rows(), 2);
1438 TEST_EQUALITY(reorderedbA->Cols(), 2);
1439
1440 RCP<MultiVector> X = MultiVectorFactory::Build(reorderedA->getDomainMap(), 1);
1441 RCP<MultiVector> RHS = MultiVectorFactory::Build(reorderedA->getRangeMap(), 1);
1442
1443 // Random X
1444 X->putScalar(0.0);
1445 RHS->putScalar(1.0);
1446
1447 bgsSmoother->Apply(*X, *RHS, true); // zero initial guess
1448
1449 typedef typename Teuchos::ScalarTraits<Scalar>::magnitudeType magnitude_type;
1450 Teuchos::Array<magnitude_type> finalNorms(1);
1451 X->norm2(finalNorms);
1452 Teuchos::Array<magnitude_type> residualNorm1 = Utilities::ResidualNorm(*reorderedA, *X, *RHS);
1453 out << " ||Residual_final|| = " << std::setiosflags(std::ios::fixed) << std::setprecision(20) << residualNorm1[0] << std::endl;
1454 out << " ||X_final|| = " << std::setiosflags(std::ios::fixed) << std::setprecision(10) << finalNorms[0] << std::endl;
1455
1456 magnitude_type tol = 10 * std::sqrt(Teuchos::ScalarTraits<Scalar>::eps());
1457
1458 TEUCHOS_TEST_COMPARE(residualNorm1[0], <, tol, out, success);
1459 } // end Tpetra
1460 }
1461
1462 TEUCHOS_UNIT_TEST_TEMPLATE_4_DECL(BlockedSmoother, Thyra_Nested_BGS_Setup_Apply2, Scalar, LocalOrdinal, GlobalOrdinal, Node) {
1463 #include <MueLu_UseShortNames.hpp>
1464 MUELU_TESTING_SET_OSTREAM;
1465 MUELU_TESTING_LIMIT_SCOPE(Scalar, GlobalOrdinal, Node);
1466 MUELU_TEST_ONLY_FOR(Xpetra::UseTpetra) {
1467 RCP<const Teuchos::Comm<int> > comm = Parameters::getDefaultComm();
1468
1469 int noBlocks = 4;
1470 Teuchos::RCP<const BlockedCrsMatrix> bop = CreateBlockDiagonalExampleMatrixThyra<Scalar, LocalOrdinal, GlobalOrdinal, Node, TpetraMap>(noBlocks, *comm);
1471 Teuchos::RCP<const Matrix> Aconst = Teuchos::rcp_dynamic_cast<const Matrix>(bop);
1472 Teuchos::RCP<Matrix> A = Teuchos::rcp_const_cast<Matrix>(Aconst);
1473
1474 // I don't use the testApply infrastructure because it has no provision for an initial guess.
1475 Level level;
1476 TestHelpers::TestFactory<Scalar, LocalOrdinal, GlobalOrdinal, Node>::createSingleLevelHierarchy(level);
1477 level.Set("A", A);
1478
1479 // Test ReorderBlockAFactory
1480 Teuchos::RCP<ReorderBlockAFactory> rAFact = Teuchos::rcp(new ReorderBlockAFactory());
1481 rAFact->SetFactory("A", MueLu::NoFactory::getRCP());
1482 rAFact->SetParameter(std::string("Reorder Type"), Teuchos::ParameterEntry(std::string("[ [ 3 0 ] [1 2] ]")));
1483
1484 //////////////////////////////////////////////////////////////////////
1485 // global level
1486 RCP<BlockedGaussSeidelSmoother> smootherPrototype = rcp(new BlockedGaussSeidelSmoother());
1487 smootherPrototype->SetParameter("Sweeps", Teuchos::ParameterEntry(1));
1488 smootherPrototype->SetFactory("A", rAFact);
1489
1490 for (int k = 0; k < 2; k++) {
1491 Teuchos::RCP<SubBlockAFactory> sA = Teuchos::rcp(new SubBlockAFactory());
1492 sA->SetFactory("A", rAFact);
1493 sA->SetParameter("block row", Teuchos::ParameterEntry(k)); // local block indices relative to size of blocked operator
1494 sA->SetParameter("block col", Teuchos::ParameterEntry(k));
1495
1496 Teuchos::RCP<BlockedGaussSeidelSmoother> sP = Teuchos::rcp(new BlockedGaussSeidelSmoother());
1497 sP->SetParameter("Sweeps", Teuchos::ParameterEntry(1));
1498 sP->SetFactory("A", sA);
1499
1500 for (int l = 0; l < 2; l++) {
1501 std::string strInfo = std::string("{ 1 }");
1502 Teuchos::RCP<SubBlockAFactory> ssA = rcp(new SubBlockAFactory());
1503 ssA->SetFactory("A", sA);
1504 ssA->SetParameter("block row", Teuchos::ParameterEntry(l)); // local block indices relative to size of blocked operator
1505 ssA->SetParameter("block col", Teuchos::ParameterEntry(l));
1506 ssA->SetParameter("Range map: Striding info", Teuchos::ParameterEntry(strInfo));
1507 ssA->SetParameter("Domain map: Striding info", Teuchos::ParameterEntry(strInfo));
1508 RCP<SmootherPrototype> ssP = rcp(new Ifpack2Smoother(std::string("RELAXATION"), Teuchos::ParameterList(), 0));
1509 ssP->SetFactory("A", ssA);
1510 Teuchos::RCP<SmootherFactory> ssF = Teuchos::rcp(new SmootherFactory(ssP));
1511 Teuchos::RCP<FactoryManager> ssM = Teuchos::rcp(new FactoryManager());
1512 ssM->SetFactory("A", ssA);
1513 ssM->SetFactory("Smoother", ssF);
1514 ssM->SetIgnoreUserData(true);
1515 sP->AddFactoryManager(ssM, l);
1516 }
1517
1518 Teuchos::RCP<SmootherFactory> sF = Teuchos::rcp(new SmootherFactory(sP));
1519 Teuchos::RCP<FactoryManager> sM = Teuchos::rcp(new FactoryManager());
1520 sM->SetFactory("A", sA);
1521 sM->SetFactory("Smoother", sF);
1522 sM->SetIgnoreUserData(true);
1523 smootherPrototype->AddFactoryManager(sM, k);
1524 }
1525
1526 // create master smoother factory
1527 RCP<SmootherFactory> smootherFact = rcp(new SmootherFactory(smootherPrototype));
1528
1529 // main factory manager
1530 FactoryManager M;
1531 M.SetFactory("A", rAFact);
1532 M.SetFactory("Smoother", smootherFact);
1533
1534 MueLu::SetFactoryManager SFM(Teuchos::rcpFromRef(level), Teuchos::rcpFromRef(M));
1535
1536 // request BGS smoother (and all dependencies) on level
1537 level.Request("A", rAFact.get());
1538 level.Request("Smoother", smootherFact.get());
1539 level.Request("PreSmoother", smootherFact.get());
1540 level.Request("PostSmoother", smootherFact.get());
1541
1542 smootherFact->Build(level);
1543
1544 RCP<SmootherBase> bgsSmoother = level.Get<RCP<SmootherBase> >("PreSmoother", smootherFact.get());
1545
1546 RCP<Matrix> reorderedA = level.Get<RCP<Matrix> >("A", rAFact.get());
1547 RCP<BlockedCrsMatrix> reorderedbA = Teuchos::rcp_dynamic_cast<BlockedCrsMatrix>(reorderedA);
1548
1549 TEST_EQUALITY(reorderedbA->Rows(), 2);
1550 TEST_EQUALITY(reorderedbA->Cols(), 2);
1551
1552 RCP<MultiVector> X = MultiVectorFactory::Build(reorderedA->getDomainMap(), 1);
1553 RCP<MultiVector> RHS = MultiVectorFactory::Build(reorderedA->getRangeMap(), 1);
1554
1555 // Random X
1556 X->putScalar(0.0);
1557 RHS->putScalar(1.0);
1558
1559 bgsSmoother->Apply(*X, *RHS, true); // zero initial guess
1560
1561 typedef typename Teuchos::ScalarTraits<Scalar>::magnitudeType magnitude_type;
1562 Teuchos::Array<magnitude_type> finalNorms(1);
1563 X->norm2(finalNorms);
1564 Teuchos::Array<magnitude_type> residualNorm1 = Utilities::ResidualNorm(*reorderedA, *X, *RHS);
1565 out << " ||Residual_final|| = " << std::setiosflags(std::ios::fixed) << std::setprecision(20) << residualNorm1[0] << std::endl;
1566 out << " ||X_final|| = " << std::setiosflags(std::ios::fixed) << std::setprecision(10) << finalNorms[0] << std::endl;
1567
1568 magnitude_type tol = 50. * Teuchos::ScalarTraits<Scalar>::eps();
1569
1570 TEUCHOS_TEST_COMPARE(residualNorm1[0], <, tol, out, success);
1571
1572 Teuchos::RCP<const MapExtractor> doMapExtractor = reorderedbA->getDomainMapExtractor();
1573
1574 // We should remove the boolean from ExtractVector. It is not necessary
1575 #ifdef HAVE_XPETRA_DEBUG
1576 TEST_THROW(doMapExtractor->ExtractVector(X, 0, false), Xpetra::Exceptions::RuntimeError);
1577 TEST_THROW(doMapExtractor->ExtractVector(X, 1, false), Xpetra::Exceptions::RuntimeError);
1578 #endif
1579 Teuchos::RCP<MultiVector> v0 = doMapExtractor->ExtractVector(X, 0, true);
1580 Teuchos::RCP<MultiVector> v1 = doMapExtractor->ExtractVector(X, 1, true);
1581
1582 Teuchos::RCP<BlockedMultiVector> bv0 = Teuchos::rcp_dynamic_cast<BlockedMultiVector>(v0);
1583 Teuchos::RCP<BlockedMultiVector> bv1 = Teuchos::rcp_dynamic_cast<BlockedMultiVector>(v1);
1584 TEST_EQUALITY((bv0->getMultiVector(0, true)->getData(0))[0], Teuchos::as<Scalar>(0.25));
1585 TEST_EQUALITY((bv0->getMultiVector(0, true)->getData(0))[19], Teuchos::as<Scalar>(0.25));
1586 TEST_EQUALITY((bv1->getMultiVector(0, true)->getData(0))[0], Teuchos::as<Scalar>(0.5));
1587 TEST_EQUALITY((bv1->getMultiVector(0, true)->getData(0))[4], Teuchos::as<Scalar>(0.5));
1588 TEST_EQUALITY((bv0->getMultiVector(1, true)->getData(0))[0], Teuchos::as<Scalar>(1.0));
1589 TEST_EQUALITY((bv0->getMultiVector(1, true)->getData(0))[4], Teuchos::as<Scalar>(1.0));
1590
1591 Teuchos::Array<magnitude_type> n0(1);
1592 v0->norm1(n0);
1593 Teuchos::Array<magnitude_type> n1(1);
1594 v1->norm1(n1);
1595
1596 TEST_EQUALITY(n0[0], Teuchos::as<Scalar>(comm->getSize() * 10.0));
1597 TEUCHOS_TEST_COMPARE(n1[0], <, comm->getSize() * 5.84, out, success);
1598 TEUCHOS_TEST_COMPARE(n1[0], >, comm->getSize() * 5.83, out, success);
1599
1600 TEST_EQUALITY(v0->getMap()->getMinLocalIndex(), 0);
1601 TEST_EQUALITY(v0->getMap()->getMaxLocalIndex(), 24);
1602 TEST_EQUALITY(v0->getMap()->getMinAllGlobalIndex(), 0);
1603 TEST_EQUALITY(v0->getMap()->getMinGlobalIndex(), comm->getRank() * 20);
1604 TEST_EQUALITY(v0->getMap()->getMaxGlobalIndex(), comm->getSize() * 20 + comm->getRank() * 5 + 4);
1605 TEST_EQUALITY(v0->getMap()->getMaxAllGlobalIndex(), comm->getSize() * 25 - 1);
1606
1607 TEST_EQUALITY(v1->getMap()->getMinLocalIndex(), 0);
1608 TEST_EQUALITY(v1->getMap()->getMaxLocalIndex(), 14);
1609 TEST_EQUALITY(v1->getMap()->getMinAllGlobalIndex(), 0);
1610 TEST_EQUALITY(v1->getMap()->getMinGlobalIndex(), comm->getRank() * 5);
1611 TEST_EQUALITY(v1->getMap()->getMaxGlobalIndex(), comm->getSize() * 5 + comm->getRank() * 10 + 9);
1612 TEST_EQUALITY(v1->getMap()->getMaxAllGlobalIndex(), comm->getSize() * 15 - 1);
1613
1614 Teuchos::RCP<BlockedCrsMatrix> b00 = Teuchos::rcp_dynamic_cast<BlockedCrsMatrix>(reorderedbA->getMatrix(0, 0));
1615 TEST_EQUALITY(b00->Rows(), 2);
1616 TEST_EQUALITY(b00->Cols(), 2);
1617
1618 Teuchos::RCP<const MapExtractor> me00 = b00->getDomainMapExtractor();
1619 Teuchos::RCP<MultiVector> v00 = me00->ExtractVector(v0, 0, true);
1620 Teuchos::RCP<MultiVector> v01 = me00->ExtractVector(v0, 1, true);
1621 TEST_EQUALITY((v00->getData(0))[0], Teuchos::as<Scalar>(0.25));
1622 TEST_EQUALITY((v01->getData(0))[0], Teuchos::as<Scalar>(1.0));
1623 TEST_EQUALITY(v00->getLocalLength(), 20);
1624 TEST_EQUALITY(v01->getLocalLength(), 5);
1625 TEST_EQUALITY(v00->getGlobalLength(), Teuchos::as<size_t>(comm->getSize() * 20));
1626 TEST_EQUALITY(v01->getGlobalLength(), Teuchos::as<size_t>(comm->getSize() * 5));
1627
1628 TEST_EQUALITY(v00->getMap()->getMinLocalIndex(), 0);
1629 TEST_EQUALITY(v00->getMap()->getMaxLocalIndex(), 19);
1630 TEST_EQUALITY(v00->getMap()->getMinAllGlobalIndex(), 0);
1631 TEST_EQUALITY(v00->getMap()->getMinGlobalIndex(), comm->getRank() * 20);
1632 TEST_EQUALITY(v00->getMap()->getMaxGlobalIndex(), comm->getRank() * 20 + 19);
1633 TEST_EQUALITY(v00->getMap()->getMaxAllGlobalIndex(), comm->getSize() * 20 - 1);
1634
1635 TEST_EQUALITY(v01->getMap()->getMinLocalIndex(), 0);
1636 TEST_EQUALITY(v01->getMap()->getMaxLocalIndex(), 4);
1637 TEST_EQUALITY(v01->getMap()->getMinAllGlobalIndex(), 0);
1638 TEST_EQUALITY(v01->getMap()->getMinGlobalIndex(), comm->getRank() * 5);
1639 TEST_EQUALITY(v01->getMap()->getMaxGlobalIndex(), comm->getRank() * 5 + 4);
1640 TEST_EQUALITY(v01->getMap()->getMaxAllGlobalIndex(), comm->getSize() * 5 - 1);
1641
1642 Teuchos::RCP<BlockedCrsMatrix> b11 = Teuchos::rcp_dynamic_cast<BlockedCrsMatrix>(reorderedbA->getMatrix(1, 1));
1643 TEST_EQUALITY(b11->Rows(), 2);
1644 TEST_EQUALITY(b11->Cols(), 2);
1645
1646 Teuchos::RCP<const MapExtractor> me11 = b11->getDomainMapExtractor();
1647 Teuchos::RCP<MultiVector> v10 = me11->ExtractVector(v1, 0, true);
1648 Teuchos::RCP<MultiVector> v11 = me11->ExtractVector(v1, 1, true);
1649 TEST_EQUALITY((v10->getData(0))[0], Teuchos::as<Scalar>(0.5));
1650 TEST_EQUALITY(v10->getLocalLength(), 5);
1651 TEST_EQUALITY(v11->getLocalLength(), 10);
1652 TEST_EQUALITY(v10->getGlobalLength(), Teuchos::as<size_t>(comm->getSize() * 5));
1653 TEST_EQUALITY(v11->getGlobalLength(), Teuchos::as<size_t>(comm->getSize() * 10));
1654
1655 TEST_EQUALITY(v10->getMap()->getMinLocalIndex(), 0);
1656 TEST_EQUALITY(v10->getMap()->getMaxLocalIndex(), 4);
1657 TEST_EQUALITY(v10->getMap()->getMinAllGlobalIndex(), 0);
1658 TEST_EQUALITY(v10->getMap()->getMinGlobalIndex(), comm->getRank() * 5);
1659 TEST_EQUALITY(v10->getMap()->getMaxGlobalIndex(), comm->getRank() * 5 + 4);
1660 TEST_EQUALITY(v10->getMap()->getMaxAllGlobalIndex(), comm->getSize() * 5 - 1);
1661
1662 TEST_EQUALITY(v11->getMap()->getMinLocalIndex(), 0);
1663 TEST_EQUALITY(v11->getMap()->getMaxLocalIndex(), 9);
1664 TEST_EQUALITY(v11->getMap()->getMinAllGlobalIndex(), 0);
1665 TEST_EQUALITY(v11->getMap()->getMinGlobalIndex(), comm->getRank() * 10);
1666 TEST_EQUALITY(v11->getMap()->getMaxGlobalIndex(), comm->getRank() * 10 + 9);
1667 TEST_EQUALITY(v11->getMap()->getMaxAllGlobalIndex(), comm->getSize() * 10 - 1);
1668
1669 Teuchos::Array<magnitude_type> n00(1);
1670 v00->norm1(n00);
1671 Teuchos::Array<magnitude_type> n11(1);
1672 v01->norm1(n11);
1673 Teuchos::Array<magnitude_type> n22(1);
1674 v10->norm1(n22);
1675 Teuchos::Array<magnitude_type> n33(1);
1676 v11->norm1(n33);
1677
1678 TEST_EQUALITY(n00[0], Teuchos::as<Scalar>(v00->getGlobalLength() * 0.25));
1679 TEST_EQUALITY(n11[0], Teuchos::as<Scalar>(v01->getGlobalLength() * 1.0));
1680 TEST_EQUALITY(n22[0], Teuchos::as<Scalar>(v10->getGlobalLength() * 0.5));
1681 TEST_FLOATING_EQUALITY(n33[0], v11->getGlobalLength() / 3.0, Teuchos::ScalarTraits<magnitude_type>::eps());
1682 } // end UseTpetra
1683 }
1684
1685 TEUCHOS_UNIT_TEST_TEMPLATE_4_DECL(BlockedSmoother, SIMPLE_Setup_Apply, Scalar, LocalOrdinal, GlobalOrdinal, Node) {
1686 #include <MueLu_UseShortNames.hpp>
1687 MUELU_TESTING_SET_OSTREAM;
1688 MUELU_TESTING_LIMIT_SCOPE(Scalar, GlobalOrdinal, Node);
1689 // TODO test only Tpetra because of Ifpack2 smoother!
1690 MUELU_TEST_ONLY_FOR(Xpetra::UseTpetra) {
1691 RCP<const Teuchos::Comm<int> > comm = Parameters::getDefaultComm();
1692 Xpetra::UnderlyingLib lib = MueLuTests::TestHelpers::Parameters::getLib();
1693
1694 int noBlocks = 2;
1695 Teuchos::RCP<const BlockedCrsMatrix> bop = TestHelpers::TestFactory<Scalar, LocalOrdinal, GlobalOrdinal, Node>::CreateBlockDiagonalExampleMatrix(lib, noBlocks, comm);
1696 Teuchos::RCP<const Matrix> Aconst = Teuchos::rcp_dynamic_cast<const Matrix>(bop);
1697 Teuchos::RCP<Matrix> A = Teuchos::rcp_const_cast<Matrix>(Aconst);
1698
1699 // I don't use the testApply infrastructure because it has no provision for an initial guess.
1700 Level level;
1701 TestHelpers::TestFactory<Scalar, LocalOrdinal, GlobalOrdinal, Node>::createSingleLevelHierarchy(level);
1702 level.Set("A", A);
1703
1704 //////////////////////////////////////////////////////////////////////
1705 // Smoothers
1706 RCP<SimpleSmoother> smootherPrototype = rcp(new SimpleSmoother());
1707 smootherPrototype->SetParameter("Sweeps", Teuchos::ParameterEntry(Teuchos::as<LocalOrdinal>(1)));
1708 smootherPrototype->SetParameter("Damping factor", Teuchos::ParameterEntry(Teuchos::as<Scalar>(1.0)));
1709 smootherPrototype->SetParameter("UseSIMPLEC", Teuchos::ParameterEntry(false));
1710
1711 std::vector<RCP<SubBlockAFactory> > sA(1, Teuchos::null);
1712 std::vector<RCP<SmootherFactory> > sF(2, Teuchos::null);
1713 std::vector<RCP<FactoryManager> > sM(2, Teuchos::null);
1714
1715 // prediction
1716 std::string strInfo = std::string("{ 1 }");
1717 sA[0] = rcp(new SubBlockAFactory());
1718 sA[0]->SetFactory("A", MueLu::NoFactory::getRCP());
1719 sA[0]->SetParameter("block row", Teuchos::ParameterEntry(0));
1720 sA[0]->SetParameter("block col", Teuchos::ParameterEntry(0));
1721 sA[0]->SetParameter("Range map: Striding info", Teuchos::ParameterEntry(strInfo));
1722 sA[0]->SetParameter("Domain map: Striding info", Teuchos::ParameterEntry(strInfo));
1723
1724 RCP<SmootherPrototype> smoProtoPredict = rcp(new Ifpack2Smoother(std::string("RELAXATION"), Teuchos::ParameterList(), 0));
1725 smoProtoPredict->SetFactory("A", sA[0]);
1726 sF[0] = rcp(new SmootherFactory(smoProtoPredict));
1727
1728 sM[0] = rcp(new FactoryManager());
1729 sM[0]->SetFactory("A", sA[0]);
1730 sM[0]->SetFactory("Smoother", sF[0]);
1731 sM[0]->SetIgnoreUserData(true);
1732
1733 smootherPrototype->SetVelocityPredictionFactoryManager(sM[0]);
1734
1735 // correction
1736 // define SchurComplement Factory
1737 // SchurComp gets a RCP to AFact_ which has to be the 2x2 blocked operator
1738 // It stores the resulting SchurComplement operator as "A" generated by the SchurComplementFactory
1739 // Instead of F^{-1} it uses the approximation \hat{F}^{-1} with \hat{F} = diag(F)
1740 RCP<SchurComplementFactory> SFact = Teuchos::rcp(new SchurComplementFactory());
1741 SFact->SetParameter("omega", Teuchos::ParameterEntry(Teuchos::as<Scalar>(1.0))); // for Simple, omega is always 1.0 in the SchurComplement
1742 SFact->SetFactory("A", MueLu::NoFactory::getRCP());
1743
1744 RCP<SmootherPrototype> smoProtoCorrect = rcp(new Ifpack2Smoother(std::string("RELAXATION"), Teuchos::ParameterList(), 0));
1745 smoProtoCorrect->SetFactory("A", SFact);
1746 sF[1] = rcp(new SmootherFactory(smoProtoCorrect));
1747
1748 sM[1] = rcp(new FactoryManager());
1749 sM[1]->SetFactory("A", SFact);
1750 sM[1]->SetFactory("Smoother", sF[1]);
1751 sM[1]->SetIgnoreUserData(true);
1752
1753 smootherPrototype->SetSchurCompFactoryManager(sM[1]);
1754
1755 RCP<SmootherFactory> smootherFact = rcp(new SmootherFactory(smootherPrototype));
1756
1757 // main factory manager
1758 FactoryManager M;
1759 M.SetFactory("Smoother", smootherFact);
1760
1761 MueLu::SetFactoryManager SFM(Teuchos::rcpFromRef(level), Teuchos::rcpFromRef(M));
1762
1763 // request BGS smoother (and all dependencies) on level
1764 level.Request("Smoother", smootherFact.get());
1765 level.Request("PreSmoother", smootherFact.get());
1766 level.Request("PostSmoother", smootherFact.get());
1767
1768 // smootherFact->DeclareInput(level);
1769 smootherFact->Build(level);
1770
1771 level.print(std::cout, Teuchos::VERB_EXTREME);
1772
1773 RCP<SmootherBase> simpleSmoother = level.Get<RCP<SmootherBase> >("PreSmoother", smootherFact.get());
1774
1775 RCP<MultiVector> X = MultiVectorFactory::Build(A->getDomainMap(), 1);
1776 RCP<MultiVector> RHS = MultiVectorFactory::Build(A->getRangeMap(), 1);
1777
1778 // Random X
1779 X->setSeed(846930886);
1780 X->randomize();
1781
1782 typedef typename Teuchos::ScalarTraits<Scalar>::magnitudeType magnitude_type;
1783
1784 // Normalize X
1785 Array<magnitude_type> norms(1);
1786 X->norm2(norms);
1787 X->scale(1 / norms[0]);
1788
1789 // Compute RHS corresponding to X
1790 A->apply(*X, *RHS, Teuchos::NO_TRANS, (SC)1.0, (SC)0.0);
1791
1792 // Reset X to 0
1793 X->putScalar((SC)0.0);
1794
1795 RHS->norm2(norms);
1796 out << "||RHS|| = " << std::setiosflags(std::ios::fixed) << std::setprecision(10) << norms[0] << std::endl;
1797
1798 out << "solve with zero initial guess" << std::endl;
1799 Teuchos::Array<magnitude_type> initialNorms(1);
1800 X->norm2(initialNorms);
1801 out << " ||X_initial|| = " << std::setiosflags(std::ios::fixed) << std::setprecision(10) << initialNorms[0] << std::endl;
1802
1803 simpleSmoother->Apply(*X, *RHS, true); // zero initial guess
1804
1805 Teuchos::Array<magnitude_type> finalNorms(1);
1806 X->norm2(finalNorms);
1807 Teuchos::Array<magnitude_type> residualNorm1 = Utilities::ResidualNorm(*A, *X, *RHS);
1808 out << " ||Residual_final|| = " << std::setiosflags(std::ios::fixed) << std::setprecision(20) << residualNorm1[0] << std::endl;
1809 out << " ||X_final|| = " << std::setiosflags(std::ios::fixed) << std::setprecision(10) << finalNorms[0] << std::endl;
1810
1811 magnitude_type tol = 50. * Teuchos::ScalarTraits<Scalar>::eps();
1812
1813 TEUCHOS_TEST_COMPARE(residualNorm1[0], <, tol, out, success);
1814 TEUCHOS_TEST_COMPARE(finalNorms[0] - Teuchos::ScalarTraits<Scalar>::magnitude(Teuchos::ScalarTraits<Scalar>::one()), <, tol, out, success);
1815
1816 out << "solve with random initial guess" << std::endl;
1817 X->randomize();
1818 X->norm2(initialNorms);
1819 out << " ||X_initial|| = " << std::setiosflags(std::ios::fixed) << std::setprecision(10) << initialNorms[0] << std::endl;
1820
1821 simpleSmoother->Apply(*X, *RHS, false); // nonzero initial guess
1822
1823 X->norm2(finalNorms);
1824 Teuchos::Array<magnitude_type> residualNorm2 = Utilities::ResidualNorm(*A, *X, *RHS);
1825 out << " ||Residual_final|| = " << std::setiosflags(std::ios::fixed) << std::setprecision(20) << residualNorm2[0] << std::endl;
1826 out << " ||X_final|| = " << std::setiosflags(std::ios::fixed) << std::setprecision(10) << finalNorms[0] << std::endl;
1827
1828 TEUCHOS_TEST_COMPARE(residualNorm2[0], <, tol, out, success);
1829 TEUCHOS_TEST_COMPARE(finalNorms[0] - Teuchos::ScalarTraits<Scalar>::magnitude(Teuchos::ScalarTraits<Scalar>::one()), <, tol, out, success);
1830
1831 if (comm->getSize() == 1) {
1832 TEST_EQUALITY(residualNorm1[0] != residualNorm2[0], true);
1833 } else {
1834 out << "Pass/Fail is only checked in serial." << std::endl;
1835 }
1836 } // end UseTpetra
1837 }
1838
1839 TEUCHOS_UNIT_TEST_TEMPLATE_4_DECL(BlockedSmoother, NestedII20I1I_SIMPLE_Setup_Apply, Scalar, LocalOrdinal, GlobalOrdinal, Node) {
1840 #include <MueLu_UseShortNames.hpp>
1841 MUELU_TESTING_SET_OSTREAM;
1842 MUELU_TESTING_LIMIT_SCOPE(Scalar, GlobalOrdinal, Node);
1843 // TODO test only Tpetra because of Ifpack2 smoother!
1844 MUELU_TEST_ONLY_FOR(Xpetra::UseTpetra) {
1845 RCP<const Teuchos::Comm<int> > comm = Parameters::getDefaultComm();
1846 Xpetra::UnderlyingLib lib = MueLuTests::TestHelpers::Parameters::getLib();
1847
1848 Teuchos::RCP<const BlockedCrsMatrix> bop = TestHelpers::TestFactory<Scalar, LocalOrdinal, GlobalOrdinal, Node>::CreateBlockDiagonalExampleMatrix(lib, 3, comm);
1849 Teuchos::RCP<const Matrix> Aconst = Teuchos::rcp_dynamic_cast<const Matrix>(bop);
1850 Teuchos::RCP<Matrix> A = Teuchos::rcp_const_cast<Matrix>(Aconst);
1851
1852 // I don't use the testApply infrastructure because it has no provision for an initial guess.
1853 Level level;
1854 TestHelpers::TestFactory<Scalar, LocalOrdinal, GlobalOrdinal, Node>::createSingleLevelHierarchy(level);
1855 level.Set("A", A);
1856
1857 // Test ReorderBlockAFactory
1858 Teuchos::RCP<ReorderBlockAFactory> rAFact = Teuchos::rcp(new ReorderBlockAFactory());
1859 rAFact->SetFactory("A", MueLu::NoFactory::getRCP());
1860 rAFact->SetParameter(std::string("Reorder Type"), Teuchos::ParameterEntry(std::string("[ [2 0] 1]")));
1861
1862 //////////////////////////////////////////////////////////////////////
1863 // Smoothers
1864 RCP<SimpleSmoother> smootherPrototype = rcp(new SimpleSmoother());
1865 smootherPrototype->SetFactory("A", rAFact);
1866 smootherPrototype->SetParameter("Sweeps", Teuchos::ParameterEntry(Teuchos::as<LocalOrdinal>(1)));
1867 smootherPrototype->SetParameter("Damping factor", Teuchos::ParameterEntry(Teuchos::as<Scalar>(1.0)));
1868 smootherPrototype->SetParameter("UseSIMPLEC", Teuchos::ParameterEntry(false));
1869
1870 std::vector<RCP<SubBlockAFactory> > sA(1, Teuchos::null);
1871 std::vector<RCP<SmootherFactory> > sF(2, Teuchos::null);
1872 std::vector<RCP<FactoryManager> > sM(2, Teuchos::null);
1873
1874 // prediction
1875 std::string strInfo = std::string("{ 1 }");
1876 sA[0] = rcp(new SubBlockAFactory());
1877 sA[0]->SetFactory("A", rAFact);
1878 sA[0]->SetParameter("block row", Teuchos::ParameterEntry(0));
1879 sA[0]->SetParameter("block col", Teuchos::ParameterEntry(0));
1880 sA[0]->SetParameter("Range map: Striding info", Teuchos::ParameterEntry(strInfo));
1881 sA[0]->SetParameter("Domain map: Striding info", Teuchos::ParameterEntry(strInfo));
1882
1883 // create a 2x2 SIMPLE for the prediction eq.
1884 RCP<SimpleSmoother> smoProtoPredict = Teuchos::rcp(new SimpleSmoother());
1885 smoProtoPredict->SetParameter("Sweeps", Teuchos::ParameterEntry(1));
1886 smoProtoPredict->SetParameter("Damping factor", Teuchos::ParameterEntry(Teuchos::as<Scalar>(1.0)));
1887 smoProtoPredict->SetParameter("UseSIMPLEC", Teuchos::ParameterEntry(false));
1888 smoProtoPredict->SetFactory("A", sA[0]);
1889
1890 for (int l = 0; l < 2; l++) {
1891 Teuchos::RCP<SubBlockAFactory> ssA = rcp(new SubBlockAFactory());
1892 ssA->SetFactory("A", sA[0]);
1893 ssA->SetParameter("block row", Teuchos::ParameterEntry(l)); // local block indices relative to size of blocked operator
1894 ssA->SetParameter("block col", Teuchos::ParameterEntry(l));
1895 ssA->SetParameter("Range map: Striding info", Teuchos::ParameterEntry(strInfo));
1896 ssA->SetParameter("Domain map: Striding info", Teuchos::ParameterEntry(strInfo));
1897 RCP<SmootherPrototype> ssP = rcp(new Ifpack2Smoother(std::string("RELAXATION"), Teuchos::ParameterList(), 0));
1898 ssP->SetFactory("A", ssA);
1899 Teuchos::RCP<SmootherFactory> ssF = Teuchos::rcp(new SmootherFactory(ssP));
1900 Teuchos::RCP<FactoryManager> ssM = Teuchos::rcp(new FactoryManager());
1901 ssM->SetFactory("A", ssA);
1902 ssM->SetFactory("Smoother", ssF);
1903 ssM->SetIgnoreUserData(true);
1904 if (l == 0)
1905 smoProtoPredict->SetVelocityPredictionFactoryManager(ssM);
1906 else
1907 smoProtoPredict->SetSchurCompFactoryManager(ssM);
1908 }
1909
1910 sF[0] = rcp(new SmootherFactory(smoProtoPredict));
1911
1912 sM[0] = rcp(new FactoryManager());
1913 sM[0]->SetFactory("A", sA[0]);
1914 sM[0]->SetFactory("Smoother", sF[0]);
1915 sM[0]->SetIgnoreUserData(true);
1916
1917 smootherPrototype->SetVelocityPredictionFactoryManager(sM[0]);
1918
1919 // correction
1920 // define SchurComplement Factory
1921 // SchurComp gets a RCP to AFact_ which has to be the 2x2 blocked operator
1922 // It stores the resulting SchurComplement operator as "A" generated by the SchurComplementFactory
1923 // Instead of F^{-1} it uses the approximation \hat{F}^{-1} with \hat{F} = diag(F)
1924 RCP<InverseApproximationFactory> AinvFact = Teuchos::rcp(new InverseApproximationFactory());
1925 AinvFact->SetFactory("A", rAFact);
1926
1927 RCP<SchurComplementFactory> SFact = Teuchos::rcp(new SchurComplementFactory());
1928 SFact->SetParameter("omega", Teuchos::ParameterEntry(Teuchos::as<Scalar>(1.0))); // for Simple, omega is always 1.0 in the SchurComplement
1929 SFact->SetFactory("A", rAFact);
1930 SFact->SetFactory("Ainv", AinvFact);
1931
1932 RCP<SmootherPrototype> smoProtoCorrect = rcp(new Ifpack2Smoother(std::string("RELAXATION"), Teuchos::ParameterList(), 0));
1933 smoProtoCorrect->SetFactory("A", SFact);
1934 sF[1] = rcp(new SmootherFactory(smoProtoCorrect));
1935
1936 sM[1] = rcp(new FactoryManager());
1937 sM[1]->SetFactory("A", SFact);
1938 sM[1]->SetFactory("Smoother", sF[1]);
1939 sM[1]->SetIgnoreUserData(true);
1940
1941 smootherPrototype->SetSchurCompFactoryManager(sM[1]);
1942
1943 RCP<SmootherFactory> smootherFact = rcp(new SmootherFactory(smootherPrototype));
1944
1945 // main factory manager
1946 FactoryManager M;
1947 M.SetFactory("Smoother", smootherFact);
1948 M.SetFactory("A", rAFact);
1949
1950 MueLu::SetFactoryManager SFM(Teuchos::rcpFromRef(level), Teuchos::rcpFromRef(M));
1951
1952 // request BGS smoother (and all dependencies) on level
1953 level.Request("A", rAFact.get());
1954 level.Request("Smoother", smootherFact.get());
1955 level.Request("PreSmoother", smootherFact.get());
1956 level.Request("PostSmoother", smootherFact.get());
1957
1958 // smootherFact->DeclareInput(level);
1959 smootherFact->Build(level);
1960
1961 level.print(std::cout, Teuchos::VERB_EXTREME);
1962
1963 RCP<SmootherBase> simpleSmoother = level.Get<RCP<SmootherBase> >("PreSmoother", smootherFact.get());
1964
1965 RCP<Matrix> reorderedA = level.Get<RCP<Matrix> >("A", rAFact.get());
1966 RCP<BlockedCrsMatrix> reorderedbA = Teuchos::rcp_dynamic_cast<BlockedCrsMatrix>(reorderedA);
1967
1968 TEST_EQUALITY(reorderedbA->Rows(), 2);
1969 TEST_EQUALITY(reorderedbA->Cols(), 2);
1970
1971 RCP<MultiVector> X = MultiVectorFactory::Build(reorderedA->getDomainMap(), 1);
1972 RCP<MultiVector> RHS = MultiVectorFactory::Build(reorderedA->getRangeMap(), 1);
1973
1974 // apply simple smoother
1975 RHS->putScalar((SC)1.0);
1976 X->putScalar((SC)0.0);
1977
1978 // solve system
1979 simpleSmoother->Apply(*X, *RHS, true); // zero initial guess
1980
1981 RCP<BlockedMultiVector> bX = Teuchos::rcp_dynamic_cast<BlockedMultiVector>(X);
1982 TEST_EQUALITY(bX.is_null(), false);
1983 RCP<MultiVector> XX = bX->Merge();
1984 Teuchos::ArrayRCP<const Scalar> xdata = XX->getData(0);
1985 bool bCheck = true;
1986 for (size_t i = 0; i < XX->getLocalLength(); i++) {
1987 if (i < 10) {
1988 if (xdata[i] != (SC)(1.0 / 3.0)) bCheck = false;
1989 }
1990 if (i >= 10 && i < 15) {
1991 if (xdata[i] != (SC)1.0) bCheck = false;
1992 }
1993 if (i >= 15 && i < 20) {
1994 if (xdata[i] != (SC)0.5) bCheck = false;
1995 }
1996 }
1997 TEST_EQUALITY(bCheck, true);
1998
1999 // Random X
2000 X->setSeed(846930886);
2001 X->randomize();
2002
2003 typedef typename Teuchos::ScalarTraits<Scalar>::magnitudeType magnitude_type;
2004
2005 // Normalize X
2006 Array<magnitude_type> norms(1);
2007 X->norm2(norms);
2008 X->scale(1 / norms[0]);
2009
2010 // Compute RHS corresponding to X
2011 reorderedA->apply(*X, *RHS, Teuchos::NO_TRANS, (SC)1.0, (SC)0.0);
2012
2013 // Reset X to 0
2014 X->putScalar((SC)0.0);
2015
2016 RHS->norm2(norms);
2017 out << "||RHS|| = " << std::setiosflags(std::ios::fixed) << std::setprecision(10) << norms[0] << std::endl;
2018
2019 out << "solve with zero initial guess" << std::endl;
2020 Teuchos::Array<magnitude_type> initialNorms(1);
2021 X->norm2(initialNorms);
2022 out << " ||X_initial|| = " << std::setiosflags(std::ios::fixed) << std::setprecision(10) << initialNorms[0] << std::endl;
2023
2024 simpleSmoother->Apply(*X, *RHS, true); // zero initial guess
2025
2026 Teuchos::Array<magnitude_type> finalNorms(1);
2027 X->norm2(finalNorms);
2028 Teuchos::Array<magnitude_type> residualNorm1 = Utilities::ResidualNorm(*reorderedA, *X, *RHS);
2029 out << " ||Residual_final|| = " << std::setiosflags(std::ios::fixed) << std::setprecision(20) << residualNorm1[0] << std::endl;
2030 out << " ||X_final|| = " << std::setiosflags(std::ios::fixed) << std::setprecision(10) << finalNorms[0] << std::endl;
2031
2032 magnitude_type tol = 50. * Teuchos::ScalarTraits<Scalar>::eps();
2033
2034 TEUCHOS_TEST_COMPARE(residualNorm1[0], <, tol, out, success);
2035 TEUCHOS_TEST_COMPARE(finalNorms[0] - Teuchos::ScalarTraits<Scalar>::magnitude(Teuchos::ScalarTraits<Scalar>::one()), <, tol, out, success);
2036 } // end useTpetra
2037 }
2038
2039 TEUCHOS_UNIT_TEST_TEMPLATE_4_DECL(BlockedSmoother, NestedI2I01II_SIMPLE_Setup_Apply, Scalar, LocalOrdinal, GlobalOrdinal, Node) {
2040 #include <MueLu_UseShortNames.hpp>
2041 MUELU_TESTING_SET_OSTREAM;
2042 MUELU_TESTING_LIMIT_SCOPE(Scalar, GlobalOrdinal, Node);
2043 // TODO test only Tpetra because of Ifpack2 smoother!
2044 MUELU_TEST_ONLY_FOR(Xpetra::UseTpetra) {
2045 RCP<const Teuchos::Comm<int> > comm = Parameters::getDefaultComm();
2046 Xpetra::UnderlyingLib lib = MueLuTests::TestHelpers::Parameters::getLib();
2047
2048 Teuchos::RCP<const BlockedCrsMatrix> bop = TestHelpers::TestFactory<Scalar, LocalOrdinal, GlobalOrdinal, Node>::CreateBlockDiagonalExampleMatrix(lib, 3, comm);
2049 Teuchos::RCP<const Matrix> Aconst = Teuchos::rcp_dynamic_cast<const Matrix>(bop);
2050 Teuchos::RCP<Matrix> A = Teuchos::rcp_const_cast<Matrix>(Aconst);
2051
2052 // I don't use the testApply infrastructure because it has no provision for an initial guess.
2053 Level level;
2054 TestHelpers::TestFactory<Scalar, LocalOrdinal, GlobalOrdinal, Node>::createSingleLevelHierarchy(level);
2055 level.Set("A", A);
2056
2057 // Test ReorderBlockAFactory
2058 Teuchos::RCP<ReorderBlockAFactory> rAFact = Teuchos::rcp(new ReorderBlockAFactory());
2059 rAFact->SetFactory("A", MueLu::NoFactory::getRCP());
2060 rAFact->SetParameter(std::string("Reorder Type"), Teuchos::ParameterEntry(std::string("[ 2 [0 1]]")));
2061
2062 //////////////////////////////////////////////////////////////////////
2063 // Smoothers
2064 RCP<SimpleSmoother> smootherPrototype = rcp(new SimpleSmoother());
2065 smootherPrototype->SetFactory("A", rAFact);
2066 smootherPrototype->SetParameter("Sweeps", Teuchos::ParameterEntry(Teuchos::as<LocalOrdinal>(1)));
2067 smootherPrototype->SetParameter("Damping factor", Teuchos::ParameterEntry(Teuchos::as<Scalar>(1.0)));
2068 smootherPrototype->SetParameter("UseSIMPLEC", Teuchos::ParameterEntry(false));
2069
2070 std::vector<RCP<SubBlockAFactory> > sA(1, Teuchos::null);
2071 std::vector<RCP<SmootherFactory> > sF(2, Teuchos::null);
2072 std::vector<RCP<FactoryManager> > sM(2, Teuchos::null);
2073
2074 // prediction
2075 std::string strInfo = std::string("{ 1 }");
2076 sA[0] = rcp(new SubBlockAFactory());
2077 sA[0]->SetFactory("A", rAFact);
2078 sA[0]->SetParameter("block row", Teuchos::ParameterEntry(0));
2079 sA[0]->SetParameter("block col", Teuchos::ParameterEntry(0));
2080 sA[0]->SetParameter("Range map: Striding info", Teuchos::ParameterEntry(strInfo));
2081 sA[0]->SetParameter("Domain map: Striding info", Teuchos::ParameterEntry(strInfo));
2082
2083 RCP<SmootherPrototype> smoProtoCorrect = rcp(new Ifpack2Smoother(std::string("RELAXATION"), Teuchos::ParameterList(), 0));
2084 smoProtoCorrect->SetFactory("A", sA[0]);
2085 sF[0] = rcp(new SmootherFactory(smoProtoCorrect));
2086
2087 sM[0] = rcp(new FactoryManager());
2088 sM[0]->SetFactory("A", sA[0]);
2089 sM[0]->SetFactory("Smoother", sF[0]);
2090 sM[0]->SetIgnoreUserData(true);
2091
2092 smootherPrototype->SetVelocityPredictionFactoryManager(sM[0]);
2093
2094 // correction
2095 // define SchurComplement Factory
2096 // SchurComp gets a RCP to AFact_ which has to be the 2x2 blocked operator
2097 // It stores the resulting SchurComplement operator as "A" generated by the SchurComplementFactory
2098 // Instead of F^{-1} it uses the approximation \hat{F}^{-1} with \hat{F} = diag(F)
2099 RCP<InverseApproximationFactory> AinvFact = Teuchos::rcp(new InverseApproximationFactory());
2100 AinvFact->SetFactory("A", rAFact);
2101
2102 RCP<SchurComplementFactory> SFact = Teuchos::rcp(new SchurComplementFactory());
2103 SFact->SetParameter("omega", Teuchos::ParameterEntry(Teuchos::as<Scalar>(1.0))); // for Simple, omega is always 1.0 in the SchurComplement
2104 SFact->SetFactory("A", rAFact);
2105 SFact->SetFactory("Ainv", AinvFact);
2106
2107 // create a 2x2 SIMPLE for the prediction eq.
2108 RCP<SimpleSmoother> smoProtoPredict = Teuchos::rcp(new SimpleSmoother());
2109 smoProtoPredict->SetParameter("Sweeps", Teuchos::ParameterEntry(1));
2110 smoProtoPredict->SetParameter("Damping factor", Teuchos::ParameterEntry(Teuchos::as<Scalar>(1.0)));
2111 smoProtoPredict->SetParameter("UseSIMPLEC", Teuchos::ParameterEntry(false));
2112 smoProtoPredict->SetFactory("A", SFact);
2113
2114 for (int l = 0; l < 2; l++) {
2115 Teuchos::RCP<SubBlockAFactory> ssA = rcp(new SubBlockAFactory());
2116 ssA->SetFactory("A", SFact);
2117 ssA->SetParameter("block row", Teuchos::ParameterEntry(l)); // local block indices relative to size of blocked operator
2118 ssA->SetParameter("block col", Teuchos::ParameterEntry(l));
2119 ssA->SetParameter("Range map: Striding info", Teuchos::ParameterEntry(strInfo));
2120 ssA->SetParameter("Domain map: Striding info", Teuchos::ParameterEntry(strInfo));
2121 RCP<SmootherPrototype> ssP = rcp(new Ifpack2Smoother(std::string("RELAXATION"), Teuchos::ParameterList(), 0));
2122 ssP->SetFactory("A", ssA);
2123 Teuchos::RCP<SmootherFactory> ssF = Teuchos::rcp(new SmootherFactory(ssP));
2124 Teuchos::RCP<FactoryManager> ssM = Teuchos::rcp(new FactoryManager());
2125 ssM->SetFactory("A", ssA);
2126 ssM->SetFactory("Smoother", ssF);
2127 ssM->SetIgnoreUserData(true);
2128 if (l == 0)
2129 smoProtoPredict->SetVelocityPredictionFactoryManager(ssM);
2130 else
2131 smoProtoPredict->SetSchurCompFactoryManager(ssM);
2132 }
2133
2134 sF[1] = rcp(new SmootherFactory(smoProtoPredict));
2135
2136 sM[1] = rcp(new FactoryManager());
2137 sM[1]->SetFactory("A", SFact);
2138 sM[1]->SetFactory("Smoother", sF[1]);
2139 sM[1]->SetIgnoreUserData(true);
2140
2141 smootherPrototype->SetSchurCompFactoryManager(sM[1]);
2142
2143 RCP<SmootherFactory> smootherFact = rcp(new SmootherFactory(smootherPrototype));
2144
2145 // main factory manager
2146 FactoryManager M;
2147 M.SetFactory("Smoother", smootherFact);
2148 M.SetFactory("A", rAFact);
2149
2150 MueLu::SetFactoryManager SFM(Teuchos::rcpFromRef(level), Teuchos::rcpFromRef(M));
2151
2152 // request BGS smoother (and all dependencies) on level
2153 level.Request("A", rAFact.get());
2154 level.Request("Smoother", smootherFact.get());
2155 level.Request("PreSmoother", smootherFact.get());
2156 level.Request("PostSmoother", smootherFact.get());
2157
2158 // smootherFact->DeclareInput(level);
2159 smootherFact->Build(level);
2160
2161 level.print(std::cout, Teuchos::VERB_EXTREME);
2162
2163 RCP<SmootherBase> simpleSmoother = level.Get<RCP<SmootherBase> >("PreSmoother", smootherFact.get());
2164
2165 RCP<Matrix> reorderedA = level.Get<RCP<Matrix> >("A", rAFact.get());
2166 RCP<BlockedCrsMatrix> reorderedbA = Teuchos::rcp_dynamic_cast<BlockedCrsMatrix>(reorderedA);
2167
2168 TEST_EQUALITY(reorderedbA->Rows(), 2);
2169 TEST_EQUALITY(reorderedbA->Cols(), 2);
2170
2171 RCP<MultiVector> X = MultiVectorFactory::Build(reorderedA->getDomainMap(), 1);
2172 RCP<MultiVector> RHS = MultiVectorFactory::Build(reorderedA->getRangeMap(), 1);
2173
2174 // apply simple smoother
2175 RHS->putScalar((SC)1.0);
2176 X->putScalar((SC)0.0);
2177
2178 // solve system
2179 simpleSmoother->Apply(*X, *RHS, true); // zero initial guess
2180 RCP<BlockedMultiVector> bX = Teuchos::rcp_dynamic_cast<BlockedMultiVector>(X);
2181 TEST_EQUALITY(bX.is_null(), false);
2182 RCP<MultiVector> XX = bX->Merge();
2183
2184 Teuchos::ArrayRCP<const Scalar> xdata = XX->getData(0);
2185 bool bCheck = true;
2186 for (size_t i = 0; i < XX->getLocalLength(); i++) {
2187 if (i < 10) {
2188 if (xdata[i] != (SC)(1.0 / 3.0)) bCheck = false;
2189 }
2190 if (i >= 10 && i < 15) {
2191 if (xdata[i] != (SC)1.0) bCheck = false;
2192 }
2193 if (i >= 15 && i < 20) {
2194 if (xdata[i] != (SC)0.5) bCheck = false;
2195 }
2196 }
2197 TEST_EQUALITY(bCheck, true);
2198
2199 // Random X
2200 X->setSeed(846930886);
2201 X->randomize();
2202
2203 typedef typename Teuchos::ScalarTraits<Scalar>::magnitudeType magnitude_type;
2204
2205 // Normalize X
2206 Array<magnitude_type> norms(1);
2207 X->norm2(norms);
2208 X->scale(1 / norms[0]);
2209
2210 // Compute RHS corresponding to X
2211 reorderedA->apply(*X, *RHS, Teuchos::NO_TRANS, (SC)1.0, (SC)0.0);
2212
2213 // Reset X to 0
2214 X->putScalar((SC)0.0);
2215
2216 RHS->describe(out, Teuchos::VERB_EXTREME);
2217
2218 RHS->norm2(norms);
2219 out << "||RHS|| = " << std::setiosflags(std::ios::fixed) << std::setprecision(10) << norms[0] << std::endl;
2220
2221 out << "solve with zero initial guess" << std::endl;
2222 Teuchos::Array<magnitude_type> initialNorms(1);
2223 X->norm2(initialNorms);
2224 out << " ||X_initial|| = " << std::setiosflags(std::ios::fixed) << std::setprecision(10) << initialNorms[0] << std::endl;
2225
2226 simpleSmoother->Apply(*X, *RHS, true); // zero initial guess
2227
2228 Teuchos::Array<magnitude_type> finalNorms(1);
2229 X->norm2(finalNorms);
2230 Teuchos::Array<magnitude_type> residualNorm1 = Utilities::ResidualNorm(*reorderedA, *X, *RHS);
2231 out << " ||Residual_final|| = " << std::setiosflags(std::ios::fixed) << std::setprecision(20) << residualNorm1[0] << std::endl;
2232 out << " ||X_final|| = " << std::setiosflags(std::ios::fixed) << std::setprecision(10) << finalNorms[0] << std::endl;
2233
2234 magnitude_type tol = 50. * Teuchos::ScalarTraits<Scalar>::eps();
2235
2236 TEUCHOS_TEST_COMPARE(residualNorm1[0], <, tol, out, success);
2237 TEUCHOS_TEST_COMPARE(finalNorms[0] - Teuchos::ScalarTraits<Scalar>::magnitude(Teuchos::ScalarTraits<Scalar>::one()), <, tol, out, success);
2238 } // end useTpetra
2239 }
2240
2241 TEUCHOS_UNIT_TEST_TEMPLATE_4_DECL(BlockedSmoother, NestedI2I01II_SIMPLE_Setup_Apply2, Scalar, LocalOrdinal, GlobalOrdinal, Node) {
2242 #include <MueLu_UseShortNames.hpp>
2243 MUELU_TESTING_SET_OSTREAM;
2244 MUELU_TESTING_LIMIT_SCOPE(Scalar, GlobalOrdinal, Node);
2245 // TODO test only Tpetra because of Ifpack2 smoother!
2246 MUELU_TEST_ONLY_FOR(Xpetra::UseTpetra) {
2247 RCP<const Teuchos::Comm<int> > comm = Parameters::getDefaultComm();
2248 Xpetra::UnderlyingLib lib = MueLuTests::TestHelpers::Parameters::getLib();
2249
2250 Teuchos::RCP<const BlockedCrsMatrix> bop = TestHelpers::TestFactory<Scalar, LocalOrdinal, GlobalOrdinal, Node>::CreateBlockDiagonalExampleMatrix(lib, 3, comm);
2251 Teuchos::RCP<const Matrix> Aconst = Teuchos::rcp_dynamic_cast<const Matrix>(bop);
2252 Teuchos::RCP<Matrix> A = Teuchos::rcp_const_cast<Matrix>(Aconst);
2253
2254 // I don't use the testApply infrastructure because it has no provision for an initial guess.
2255 Level level;
2256 TestHelpers::TestFactory<Scalar, LocalOrdinal, GlobalOrdinal, Node>::createSingleLevelHierarchy(level);
2257 level.Set("A", A);
2258
2259 // Test ReorderBlockAFactory
2260 Teuchos::RCP<ReorderBlockAFactory> rAFact = Teuchos::rcp(new ReorderBlockAFactory());
2261 rAFact->SetFactory("A", MueLu::NoFactory::getRCP());
2262 rAFact->SetParameter(std::string("Reorder Type"), Teuchos::ParameterEntry(std::string("[ 2 [0 1]]")));
2263
2264 //////////////////////////////////////////////////////////////////////
2265 // Smoothers
2266 RCP<SimpleSmoother> smootherPrototype = rcp(new SimpleSmoother());
2267 smootherPrototype->SetFactory("A", rAFact);
2268 smootherPrototype->SetParameter("Sweeps", Teuchos::ParameterEntry(Teuchos::as<LocalOrdinal>(1)));
2269 smootherPrototype->SetParameter("Damping factor", Teuchos::ParameterEntry(Teuchos::as<Scalar>(1.0)));
2270 smootherPrototype->SetParameter("UseSIMPLEC", Teuchos::ParameterEntry(false));
2271
2272 std::vector<RCP<SubBlockAFactory> > sA(1, Teuchos::null);
2273 std::vector<RCP<SmootherFactory> > sF(2, Teuchos::null);
2274 std::vector<RCP<FactoryManager> > sM(2, Teuchos::null);
2275
2276 // prediction
2277 std::string strInfo = std::string("{ 1 }");
2278 sA[0] = rcp(new SubBlockAFactory());
2279 sA[0]->SetFactory("A", rAFact);
2280 sA[0]->SetParameter("block row", Teuchos::ParameterEntry(0));
2281 sA[0]->SetParameter("block col", Teuchos::ParameterEntry(0));
2282 sA[0]->SetParameter("Range map: Striding info", Teuchos::ParameterEntry(strInfo));
2283 sA[0]->SetParameter("Domain map: Striding info", Teuchos::ParameterEntry(strInfo));
2284
2285 RCP<SmootherPrototype> smoProtoCorrect = rcp(new Ifpack2Smoother(std::string("RELAXATION"), Teuchos::ParameterList(), 0));
2286 smoProtoCorrect->SetFactory("A", sA[0]);
2287 sF[0] = rcp(new SmootherFactory(smoProtoCorrect));
2288
2289 sM[0] = rcp(new FactoryManager());
2290 sM[0]->SetFactory("A", sA[0]);
2291 sM[0]->SetFactory("Smoother", sF[0]);
2292 sM[0]->SetIgnoreUserData(true);
2293
2294 smootherPrototype->SetVelocityPredictionFactoryManager(sM[0]);
2295
2296 // correction
2297 // define SchurComplement Factory
2298 // SchurComp gets a RCP to AFact_ which has to be the 2x2 blocked operator
2299 // It stores the resulting SchurComplement operator as "A" generated by the SchurComplementFactory
2300 // Instead of F^{-1} it uses the approximation \hat{F}^{-1} with \hat{F} = diag(F)
2301 RCP<InverseApproximationFactory> AinvFact = Teuchos::rcp(new InverseApproximationFactory());
2302 AinvFact->SetFactory("A", rAFact);
2303
2304 RCP<SchurComplementFactory> SFact = Teuchos::rcp(new SchurComplementFactory());
2305 SFact->SetParameter("omega", Teuchos::ParameterEntry(Teuchos::as<Scalar>(1.0))); // for Simple, omega is always 1.0 in the SchurComplement
2306 SFact->SetFactory("A", rAFact);
2307 SFact->SetFactory("Ainv", AinvFact);
2308
2309 // create a 2x2 SIMPLE for the prediction eq.
2310 RCP<SimpleSmoother> smoProtoPredict = Teuchos::rcp(new SimpleSmoother());
2311 smoProtoPredict->SetParameter("Sweeps", Teuchos::ParameterEntry(1));
2312 smoProtoPredict->SetParameter("Damping factor", Teuchos::ParameterEntry(Teuchos::as<Scalar>(1.0)));
2313 smoProtoPredict->SetParameter("UseSIMPLEC", Teuchos::ParameterEntry(false));
2314 smoProtoPredict->SetFactory("A", SFact);
2315
2316 for (int l = 0; l < 2; l++) {
2317 Teuchos::RCP<SubBlockAFactory> ssA = rcp(new SubBlockAFactory());
2318 ssA->SetFactory("A", SFact);
2319 ssA->SetParameter("block row", Teuchos::ParameterEntry(l)); // local block indices relative to size of blocked operator
2320 ssA->SetParameter("block col", Teuchos::ParameterEntry(l));
2321 ssA->SetParameter("Range map: Striding info", Teuchos::ParameterEntry(strInfo));
2322 ssA->SetParameter("Domain map: Striding info", Teuchos::ParameterEntry(strInfo));
2323 RCP<SmootherPrototype> ssP = rcp(new Ifpack2Smoother(std::string("RELAXATION"), Teuchos::ParameterList(), 0));
2324 ssP->SetFactory("A", ssA);
2325 Teuchos::RCP<SmootherFactory> ssF = Teuchos::rcp(new SmootherFactory(ssP));
2326 Teuchos::RCP<FactoryManager> ssM = Teuchos::rcp(new FactoryManager());
2327 ssM->SetFactory("A", ssA);
2328 ssM->SetFactory("Smoother", ssF);
2329 ssM->SetIgnoreUserData(true);
2330 if (l == 0)
2331 smoProtoPredict->SetVelocityPredictionFactoryManager(ssM);
2332 else
2333 smoProtoPredict->SetSchurCompFactoryManager(ssM);
2334 }
2335
2336 sF[1] = rcp(new SmootherFactory(smoProtoPredict));
2337
2338 sM[1] = rcp(new FactoryManager());
2339 sM[1]->SetFactory("A", SFact);
2340 sM[1]->SetFactory("Smoother", sF[1]);
2341 sM[1]->SetIgnoreUserData(true);
2342
2343 smootherPrototype->SetSchurCompFactoryManager(sM[1]);
2344
2345 RCP<SmootherFactory> smootherFact = rcp(new SmootherFactory(smootherPrototype));
2346
2347 // main factory manager
2348 FactoryManager M;
2349 M.SetFactory("Smoother", smootherFact);
2350 M.SetFactory("A", rAFact);
2351
2352 MueLu::SetFactoryManager SFM(Teuchos::rcpFromRef(level), Teuchos::rcpFromRef(M));
2353
2354 // request BGS smoother (and all dependencies) on level
2355 level.Request("A", rAFact.get());
2356 level.Request("Smoother", smootherFact.get());
2357 level.Request("PreSmoother", smootherFact.get());
2358 level.Request("PostSmoother", smootherFact.get());
2359
2360 // smootherFact->DeclareInput(level);
2361 smootherFact->Build(level);
2362
2363 level.print(std::cout, Teuchos::VERB_EXTREME);
2364
2365 RCP<SmootherBase> simpleSmoother = level.Get<RCP<SmootherBase> >("PreSmoother", smootherFact.get());
2366
2367 RCP<Matrix> reorderedA = level.Get<RCP<Matrix> >("A", rAFact.get());
2368 RCP<ReorderedBlockedCrsMatrix> reorderedbA = Teuchos::rcp_dynamic_cast<ReorderedBlockedCrsMatrix>(reorderedA);
2369
2370 TEST_EQUALITY(reorderedbA->Rows(), 2);
2371 TEST_EQUALITY(reorderedbA->Cols(), 2);
2372
2373 RCP<BlockedMultiVector> bX = Teuchos::rcp(new BlockedMultiVector(bop->getBlockedDomainMap(), 1, true));
2374 RCP<BlockedMultiVector> bRHS = Teuchos::rcp(new BlockedMultiVector(bop->getBlockedRangeMap(), 1, true));
2375
2376 RCP<MultiVector> X = bX->Merge();
2377 RCP<MultiVector> RHS = bRHS->Merge();
2378
2379 // apply simple smoother
2380 RHS->putScalar((SC)1.0);
2381 X->putScalar((SC)0.0);
2382
2383 // solve system
2384 simpleSmoother->Apply(*X, *RHS, true); // zero initial guess
2385
2386 Teuchos::ArrayRCP<const Scalar> xdata = X->getData(0);
2387 bool bCheck = true;
2388 for (size_t i = 0; i < X->getLocalLength(); i++) {
2389 if (i < 5) {
2390 if (xdata[i] != (SC)1.0) bCheck = false;
2391 }
2392 if (i >= 5 && i < 10) {
2393 if (xdata[i] != (SC)0.5) bCheck = false;
2394 }
2395 if (i >= 10 && i < 20) {
2396 if (xdata[i] != (SC)(1.0 / 3.0)) bCheck = false;
2397 }
2398 }
2399 TEST_EQUALITY(bCheck, true);
2400
2401 // Random X
2402 {
2403 Teuchos::RCP<const Xpetra::BlockReorderManager> brm = Xpetra::blockedReorderFromString("[ 2 [0 1]]");
2404 RCP<BlockedMultiVector> test = Teuchos::rcp_dynamic_cast<BlockedMultiVector>(buildReorderedBlockedMultiVector(brm, bX));
2405 bX.swap(test);
2406 test = Teuchos::rcp_dynamic_cast<BlockedMultiVector>(buildReorderedBlockedMultiVector(brm, bRHS));
2407 bRHS.swap(test);
2408 }
2409
2410 bRHS->putScalar((SC)1.0);
2411 bX->setSeed(846930886);
2412 bX->randomize();
2413
2414 RHS = bRHS->Merge();
2415 X = bX->Merge();
2416
2417 typedef typename Teuchos::ScalarTraits<Scalar>::magnitudeType magnitude_type;
2418
2419 // Normalize X
2420 Array<magnitude_type> norms(1);
2421 X->norm2(norms);
2422 X->scale(1 / norms[0]);
2423
2424 // Compute RHS corresponding to X
2425 bop->apply(*X, *RHS, Teuchos::NO_TRANS, (SC)1.0, (SC)0.0);
2426
2427 // Reset X to 0
2428 X->putScalar((SC)0.0);
2429
2430 RHS->norm2(norms);
2431 out << "||RHS|| = " << std::setiosflags(std::ios::fixed) << std::setprecision(10) << norms[0] << std::endl;
2432
2433 out << "solve with zero initial guess" << std::endl;
2434 Teuchos::Array<magnitude_type> initialNorms(1);
2435 X->norm2(initialNorms);
2436 out << " ||X_initial|| = " << std::setiosflags(std::ios::fixed) << std::setprecision(10) << initialNorms[0] << std::endl;
2437
2438 simpleSmoother->Apply(*X, *RHS, true); // zero initial guess
2439
2440 Teuchos::Array<magnitude_type> finalNorms(1);
2441 X->norm2(finalNorms);
2442 Teuchos::Array<magnitude_type> residualNorm1 = Utilities::ResidualNorm(*reorderedA, *X, *RHS);
2443 out << " ||Residual_final|| = " << std::setiosflags(std::ios::fixed) << std::setprecision(20) << residualNorm1[0] << std::endl;
2444 out << " ||X_final|| = " << std::setiosflags(std::ios::fixed) << std::setprecision(10) << finalNorms[0] << std::endl;
2445
2446 magnitude_type tol = 50. * Teuchos::ScalarTraits<Scalar>::eps();
2447
2448 TEUCHOS_TEST_COMPARE(residualNorm1[0], <, tol, out, success);
2449 TEUCHOS_TEST_COMPARE(finalNorms[0] - Teuchos::ScalarTraits<Scalar>::magnitude(Teuchos::ScalarTraits<Scalar>::one()), <, tol, out, success);
2450 } // end useTpetra
2451 }
2452
2453 TEUCHOS_UNIT_TEST_TEMPLATE_4_DECL(BlockedSmoother, NestedII20I1I_Thyra_SIMPLE_Setup_Apply, Scalar, LocalOrdinal, GlobalOrdinal, Node) {
2454 #include <MueLu_UseShortNames.hpp>
2455 MUELU_TESTING_SET_OSTREAM;
2456 MUELU_TESTING_LIMIT_SCOPE(Scalar, GlobalOrdinal, Node);
2457 // TODO test only Tpetra because of Ifpack2 smoother!
2458 MUELU_TEST_ONLY_FOR(Xpetra::UseTpetra) {
2459 RCP<const Teuchos::Comm<int> > comm = Parameters::getDefaultComm();
2460 Xpetra::UnderlyingLib lib = MueLuTests::TestHelpers::Parameters::getLib();
2461
2462 Teuchos::RCP<const BlockedCrsMatrix> bop = TestHelpers::TestFactory<Scalar, LocalOrdinal, GlobalOrdinal, Node>::CreateBlockDiagonalExampleMatrixThyra(lib, 3, comm);
2463 Teuchos::RCP<const Matrix> Aconst = Teuchos::rcp_dynamic_cast<const Matrix>(bop);
2464 Teuchos::RCP<Matrix> A = Teuchos::rcp_const_cast<Matrix>(Aconst);
2465
2466 // I don't use the testApply infrastructure because it has no provision for an initial guess.
2467 Level level;
2468 TestHelpers::TestFactory<Scalar, LocalOrdinal, GlobalOrdinal, Node>::createSingleLevelHierarchy(level);
2469 level.Set("A", A);
2470
2471 // Test ReorderBlockAFactory
2472 Teuchos::RCP<ReorderBlockAFactory> rAFact = Teuchos::rcp(new ReorderBlockAFactory());
2473 rAFact->SetFactory("A", MueLu::NoFactory::getRCP());
2474 rAFact->SetParameter(std::string("Reorder Type"), Teuchos::ParameterEntry(std::string("[ [2 0] 1]")));
2475
2476 //////////////////////////////////////////////////////////////////////
2477 // Smoothers
2478 RCP<SimpleSmoother> smootherPrototype = rcp(new SimpleSmoother());
2479 smootherPrototype->SetFactory("A", rAFact);
2480 smootherPrototype->SetParameter("Sweeps", Teuchos::ParameterEntry(Teuchos::as<LocalOrdinal>(1)));
2481 smootherPrototype->SetParameter("Damping factor", Teuchos::ParameterEntry(Teuchos::as<Scalar>(1.0)));
2482 smootherPrototype->SetParameter("UseSIMPLEC", Teuchos::ParameterEntry(false));
2483
2484 std::vector<RCP<SubBlockAFactory> > sA(1, Teuchos::null);
2485 std::vector<RCP<SmootherFactory> > sF(2, Teuchos::null);
2486 std::vector<RCP<FactoryManager> > sM(2, Teuchos::null);
2487
2488 // prediction
2489 std::string strInfo = std::string("{ 1 }");
2490 sA[0] = rcp(new SubBlockAFactory());
2491 sA[0]->SetFactory("A", rAFact);
2492 sA[0]->SetParameter("block row", Teuchos::ParameterEntry(0));
2493 sA[0]->SetParameter("block col", Teuchos::ParameterEntry(0));
2494 sA[0]->SetParameter("Range map: Striding info", Teuchos::ParameterEntry(strInfo));
2495 sA[0]->SetParameter("Domain map: Striding info", Teuchos::ParameterEntry(strInfo));
2496
2497 // create a 2x2 SIMPLE for the prediction eq.
2498 RCP<SimpleSmoother> smoProtoPredict = Teuchos::rcp(new SimpleSmoother());
2499 smoProtoPredict->SetParameter("Sweeps", Teuchos::ParameterEntry(1));
2500 smoProtoPredict->SetParameter("Damping factor", Teuchos::ParameterEntry(Teuchos::as<Scalar>(1.0)));
2501 smoProtoPredict->SetParameter("UseSIMPLEC", Teuchos::ParameterEntry(false));
2502 smoProtoPredict->SetFactory("A", sA[0]);
2503
2504 for (int l = 0; l < 2; l++) {
2505 Teuchos::RCP<SubBlockAFactory> ssA = rcp(new SubBlockAFactory());
2506 ssA->SetFactory("A", sA[0]);
2507 ssA->SetParameter("block row", Teuchos::ParameterEntry(l)); // local block indices relative to size of blocked operator
2508 ssA->SetParameter("block col", Teuchos::ParameterEntry(l));
2509 ssA->SetParameter("Range map: Striding info", Teuchos::ParameterEntry(strInfo));
2510 ssA->SetParameter("Domain map: Striding info", Teuchos::ParameterEntry(strInfo));
2511 RCP<SmootherPrototype> ssP = rcp(new Ifpack2Smoother(std::string("RELAXATION"), Teuchos::ParameterList(), 0));
2512 ssP->SetFactory("A", ssA);
2513 Teuchos::RCP<SmootherFactory> ssF = Teuchos::rcp(new SmootherFactory(ssP));
2514 Teuchos::RCP<FactoryManager> ssM = Teuchos::rcp(new FactoryManager());
2515 ssM->SetFactory("A", ssA);
2516 ssM->SetFactory("Smoother", ssF);
2517 ssM->SetIgnoreUserData(true);
2518 if (l == 0)
2519 smoProtoPredict->SetVelocityPredictionFactoryManager(ssM);
2520 else
2521 smoProtoPredict->SetSchurCompFactoryManager(ssM);
2522 }
2523
2524 sF[0] = rcp(new SmootherFactory(smoProtoPredict));
2525
2526 sM[0] = rcp(new FactoryManager());
2527 sM[0]->SetFactory("A", sA[0]);
2528 sM[0]->SetFactory("Smoother", sF[0]);
2529 sM[0]->SetIgnoreUserData(true);
2530
2531 smootherPrototype->SetVelocityPredictionFactoryManager(sM[0]);
2532
2533 // correction
2534 // define SchurComplement Factory
2535 // SchurComp gets a RCP to AFact_ which has to be the 2x2 blocked operator
2536 // It stores the resulting SchurComplement operator as "A" generated by the SchurComplementFactory
2537 // Instead of F^{-1} it uses the approximation \hat{F}^{-1} with \hat{F} = diag(F)
2538 RCP<InverseApproximationFactory> AinvFact = Teuchos::rcp(new InverseApproximationFactory());
2539 AinvFact->SetFactory("A", rAFact);
2540
2541 RCP<SchurComplementFactory> SFact = Teuchos::rcp(new SchurComplementFactory());
2542 SFact->SetParameter("omega", Teuchos::ParameterEntry(Teuchos::as<Scalar>(1.0))); // for Simple, omega is always 1.0 in the SchurComplement
2543 SFact->SetFactory("A", rAFact);
2544 SFact->SetFactory("Ainv", AinvFact);
2545
2546 RCP<SmootherPrototype> smoProtoCorrect = rcp(new Ifpack2Smoother(std::string("RELAXATION"), Teuchos::ParameterList(), 0));
2547 smoProtoCorrect->SetFactory("A", SFact);
2548 sF[1] = rcp(new SmootherFactory(smoProtoCorrect));
2549
2550 sM[1] = rcp(new FactoryManager());
2551 sM[1]->SetFactory("A", SFact);
2552 sM[1]->SetFactory("Smoother", sF[1]);
2553 sM[1]->SetIgnoreUserData(true);
2554
2555 smootherPrototype->SetSchurCompFactoryManager(sM[1]);
2556
2557 RCP<SmootherFactory> smootherFact = rcp(new SmootherFactory(smootherPrototype));
2558
2559 // main factory manager
2560 FactoryManager M;
2561 M.SetFactory("Smoother", smootherFact);
2562 M.SetFactory("A", rAFact);
2563
2564 MueLu::SetFactoryManager SFM(Teuchos::rcpFromRef(level), Teuchos::rcpFromRef(M));
2565
2566 // request BGS smoother (and all dependencies) on level
2567 level.Request("A", rAFact.get());
2568 level.Request("Smoother", smootherFact.get());
2569 level.Request("PreSmoother", smootherFact.get());
2570 level.Request("PostSmoother", smootherFact.get());
2571
2572 // smootherFact->DeclareInput(level);
2573 smootherFact->Build(level);
2574
2575 level.print(std::cout, Teuchos::VERB_EXTREME);
2576
2577 RCP<SmootherBase> simpleSmoother = level.Get<RCP<SmootherBase> >("PreSmoother", smootherFact.get());
2578
2579 RCP<Matrix> reorderedA = level.Get<RCP<Matrix> >("A", rAFact.get());
2580 RCP<BlockedCrsMatrix> reorderedbA = Teuchos::rcp_dynamic_cast<BlockedCrsMatrix>(reorderedA);
2581
2582 TEST_EQUALITY(reorderedbA->Rows(), 2);
2583 TEST_EQUALITY(reorderedbA->Cols(), 2);
2584
2585 RCP<MultiVector> X = MultiVectorFactory::Build(reorderedA->getDomainMap(), 1);
2586 RCP<MultiVector> RHS = MultiVectorFactory::Build(reorderedA->getRangeMap(), 1);
2587
2588 // apply simple smoother
2589 RHS->putScalar((SC)1.0);
2590 X->putScalar((SC)0.0);
2591
2592 // solve system
2593 simpleSmoother->Apply(*X, *RHS, true); // zero initial guess
2594 RCP<BlockedMultiVector> bX = Teuchos::rcp_dynamic_cast<BlockedMultiVector>(X);
2595 TEST_EQUALITY(bX.is_null(), false);
2596 RCP<MultiVector> XX = bX->Merge();
2597 Teuchos::ArrayRCP<const Scalar> xdata = XX->getData(0);
2598 bool bCheck = true;
2599 for (size_t i = 0; i < XX->getLocalLength(); i++) {
2600 if (i < 10) {
2601 if (xdata[i] != (SC)(1.0 / 3.0)) bCheck = false;
2602 }
2603 if (i >= 10 && i < 15) {
2604 if (xdata[i] != (SC)1.0) bCheck = false;
2605 }
2606 if (i >= 15 && i < 20) {
2607 if (xdata[i] != (SC)0.5) bCheck = false;
2608 }
2609 }
2610 TEST_EQUALITY(bCheck, true);
2611
2612 // Random X
2613 X->setSeed(846930886);
2614 X->randomize();
2615
2616 typedef typename Teuchos::ScalarTraits<Scalar>::magnitudeType magnitude_type;
2617
2618 // Normalize X
2619 Array<magnitude_type> norms(1);
2620 X->norm2(norms);
2621 X->scale(1 / norms[0]);
2622
2623 // Compute RHS corresponding to X
2624 reorderedbA->apply(*X, *RHS, Teuchos::NO_TRANS, (SC)1.0, (SC)0.0);
2625
2626 // Reset X to 0
2627 X->putScalar((SC)0.0);
2628
2629 RHS->norm2(norms);
2630 out << "||RHS|| = " << std::setiosflags(std::ios::fixed) << std::setprecision(10) << norms[0] << std::endl;
2631
2632 out << "solve with zero initial guess" << std::endl;
2633 Teuchos::Array<magnitude_type> initialNorms(1);
2634 X->norm2(initialNorms);
2635 out << " ||X_initial|| = " << std::setiosflags(std::ios::fixed) << std::setprecision(10) << initialNorms[0] << std::endl;
2636
2637 simpleSmoother->Apply(*X, *RHS, true); // zero initial guess
2638
2639 Teuchos::Array<magnitude_type> finalNorms(1);
2640 X->norm2(finalNorms);
2641 Teuchos::Array<magnitude_type> residualNorm1 = Utilities::ResidualNorm(*reorderedbA, *X, *RHS);
2642 out << " ||Residual_final|| = " << std::setiosflags(std::ios::fixed) << std::setprecision(20) << residualNorm1[0] << std::endl;
2643 out << " ||X_final|| = " << std::setiosflags(std::ios::fixed) << std::setprecision(10) << finalNorms[0] << std::endl;
2644
2645 magnitude_type tol = 50. * Teuchos::ScalarTraits<Scalar>::eps();
2646
2647 TEUCHOS_TEST_COMPARE(residualNorm1[0], <, tol, out, success);
2648 TEUCHOS_TEST_COMPARE(finalNorms[0] - Teuchos::ScalarTraits<Scalar>::magnitude(Teuchos::ScalarTraits<Scalar>::one()), <, tol, out, success);
2649 } // end useTpetra
2650 }
2651
2652 TEUCHOS_UNIT_TEST_TEMPLATE_4_DECL(BlockedSmoother, NestedI2I01II_Thyra_SIMPLE_Setup_Apply, Scalar, LocalOrdinal, GlobalOrdinal, Node) {
2653 #include <MueLu_UseShortNames.hpp>
2654 MUELU_TESTING_SET_OSTREAM;
2655 MUELU_TESTING_LIMIT_SCOPE(Scalar, GlobalOrdinal, Node);
2656 // TODO test only Tpetra because of Ifpack2 smoother!
2657 MUELU_TEST_ONLY_FOR(Xpetra::UseTpetra) {
2658 RCP<const Teuchos::Comm<int> > comm = Parameters::getDefaultComm();
2659 Xpetra::UnderlyingLib lib = MueLuTests::TestHelpers::Parameters::getLib();
2660
2661 Teuchos::RCP<const BlockedCrsMatrix> bop = TestHelpers::TestFactory<Scalar, LocalOrdinal, GlobalOrdinal, Node>::CreateBlockDiagonalExampleMatrixThyra(lib, 3, comm);
2662 Teuchos::RCP<const Matrix> Aconst = Teuchos::rcp_dynamic_cast<const Matrix>(bop);
2663 Teuchos::RCP<Matrix> A = Teuchos::rcp_const_cast<Matrix>(Aconst);
2664
2665 // I don't use the testApply infrastructure because it has no provision for an initial guess.
2666 Level level;
2667 TestHelpers::TestFactory<Scalar, LocalOrdinal, GlobalOrdinal, Node>::createSingleLevelHierarchy(level);
2668 level.Set("A", A);
2669
2670 // Test ReorderBlockAFactory
2671 Teuchos::RCP<ReorderBlockAFactory> rAFact = Teuchos::rcp(new ReorderBlockAFactory());
2672 rAFact->SetFactory("A", MueLu::NoFactory::getRCP());
2673 rAFact->SetParameter(std::string("Reorder Type"), Teuchos::ParameterEntry(std::string("[ 2 [0 1]]")));
2674
2675 //////////////////////////////////////////////////////////////////////
2676 // Smoothers
2677 RCP<SimpleSmoother> smootherPrototype = rcp(new SimpleSmoother());
2678 smootherPrototype->SetFactory("A", rAFact);
2679 smootherPrototype->SetParameter("Sweeps", Teuchos::ParameterEntry(Teuchos::as<LocalOrdinal>(1)));
2680 smootherPrototype->SetParameter("Damping factor", Teuchos::ParameterEntry(Teuchos::as<Scalar>(1.0)));
2681 smootherPrototype->SetParameter("UseSIMPLEC", Teuchos::ParameterEntry(false));
2682
2683 std::vector<RCP<SubBlockAFactory> > sA(1, Teuchos::null);
2684 std::vector<RCP<SmootherFactory> > sF(2, Teuchos::null);
2685 std::vector<RCP<FactoryManager> > sM(2, Teuchos::null);
2686
2687 // prediction
2688 std::string strInfo = std::string("{ 1 }");
2689 sA[0] = rcp(new SubBlockAFactory());
2690 sA[0]->SetFactory("A", rAFact);
2691 sA[0]->SetParameter("block row", Teuchos::ParameterEntry(0));
2692 sA[0]->SetParameter("block col", Teuchos::ParameterEntry(0));
2693 sA[0]->SetParameter("Range map: Striding info", Teuchos::ParameterEntry(strInfo));
2694 sA[0]->SetParameter("Domain map: Striding info", Teuchos::ParameterEntry(strInfo));
2695
2696 RCP<SmootherPrototype> smoProtoCorrect = rcp(new Ifpack2Smoother(std::string("RELAXATION"), Teuchos::ParameterList(), 0));
2697 smoProtoCorrect->SetFactory("A", sA[0]);
2698 sF[0] = rcp(new SmootherFactory(smoProtoCorrect));
2699
2700 sM[0] = rcp(new FactoryManager());
2701 sM[0]->SetFactory("A", sA[0]);
2702 sM[0]->SetFactory("Smoother", sF[0]);
2703 sM[0]->SetIgnoreUserData(true);
2704
2705 smootherPrototype->SetVelocityPredictionFactoryManager(sM[0]);
2706
2707 // correction
2708 // define SchurComplement Factory
2709 // SchurComp gets a RCP to AFact_ which has to be the 2x2 blocked operator
2710 // It stores the resulting SchurComplement operator as "A" generated by the SchurComplementFactory
2711 // Instead of F^{-1} it uses the approximation \hat{F}^{-1} with \hat{F} = diag(F)
2712 RCP<InverseApproximationFactory> AinvFact = Teuchos::rcp(new InverseApproximationFactory());
2713 AinvFact->SetFactory("A", rAFact);
2714
2715 RCP<SchurComplementFactory> SFact = Teuchos::rcp(new SchurComplementFactory());
2716 SFact->SetParameter("omega", Teuchos::ParameterEntry(Teuchos::as<Scalar>(1.0))); // for Simple, omega is always 1.0 in the SchurComplement
2717 SFact->SetFactory("A", rAFact);
2718 SFact->SetFactory("Ainv", AinvFact);
2719
2720 // create a 2x2 SIMPLE for the prediction eq.
2721 RCP<SimpleSmoother> smoProtoPredict = Teuchos::rcp(new SimpleSmoother());
2722 smoProtoPredict->SetParameter("Sweeps", Teuchos::ParameterEntry(1));
2723 smoProtoPredict->SetParameter("Damping factor", Teuchos::ParameterEntry(Teuchos::as<Scalar>(1.0)));
2724 smoProtoPredict->SetParameter("UseSIMPLEC", Teuchos::ParameterEntry(false));
2725 smoProtoPredict->SetFactory("A", SFact);
2726
2727 for (int l = 0; l < 2; l++) {
2728 Teuchos::RCP<SubBlockAFactory> ssA = rcp(new SubBlockAFactory());
2729 ssA->SetFactory("A", SFact);
2730 ssA->SetParameter("block row", Teuchos::ParameterEntry(l)); // local block indices relative to size of blocked operator
2731 ssA->SetParameter("block col", Teuchos::ParameterEntry(l));
2732 ssA->SetParameter("Range map: Striding info", Teuchos::ParameterEntry(strInfo));
2733 ssA->SetParameter("Domain map: Striding info", Teuchos::ParameterEntry(strInfo));
2734 RCP<SmootherPrototype> ssP = rcp(new Ifpack2Smoother(std::string("RELAXATION"), Teuchos::ParameterList(), 0));
2735 ssP->SetFactory("A", ssA);
2736 Teuchos::RCP<SmootherFactory> ssF = Teuchos::rcp(new SmootherFactory(ssP));
2737 Teuchos::RCP<FactoryManager> ssM = Teuchos::rcp(new FactoryManager());
2738 ssM->SetFactory("A", ssA);
2739 ssM->SetFactory("Smoother", ssF);
2740 ssM->SetIgnoreUserData(true);
2741 if (l == 0)
2742 smoProtoPredict->SetVelocityPredictionFactoryManager(ssM);
2743 else
2744 smoProtoPredict->SetSchurCompFactoryManager(ssM);
2745 }
2746
2747 sF[1] = rcp(new SmootherFactory(smoProtoPredict));
2748
2749 sM[1] = rcp(new FactoryManager());
2750 sM[1]->SetFactory("A", SFact);
2751 sM[1]->SetFactory("Smoother", sF[1]);
2752 sM[1]->SetIgnoreUserData(true);
2753
2754 smootherPrototype->SetSchurCompFactoryManager(sM[1]);
2755
2756 RCP<SmootherFactory> smootherFact = rcp(new SmootherFactory(smootherPrototype));
2757
2758 // main factory manager
2759 FactoryManager M;
2760 M.SetFactory("Smoother", smootherFact);
2761 M.SetFactory("A", rAFact);
2762
2763 MueLu::SetFactoryManager SFM(Teuchos::rcpFromRef(level), Teuchos::rcpFromRef(M));
2764
2765 // request BGS smoother (and all dependencies) on level
2766 level.Request("A", rAFact.get());
2767 level.Request("Smoother", smootherFact.get());
2768 level.Request("PreSmoother", smootherFact.get());
2769 level.Request("PostSmoother", smootherFact.get());
2770
2771 // smootherFact->DeclareInput(level);
2772 smootherFact->Build(level);
2773
2774 level.print(std::cout, Teuchos::VERB_EXTREME);
2775
2776 RCP<SmootherBase> simpleSmoother = level.Get<RCP<SmootherBase> >("PreSmoother", smootherFact.get());
2777
2778 RCP<Matrix> reorderedA = level.Get<RCP<Matrix> >("A", rAFact.get());
2779 RCP<BlockedCrsMatrix> reorderedbA = Teuchos::rcp_dynamic_cast<BlockedCrsMatrix>(reorderedA);
2780
2781 TEST_EQUALITY(reorderedbA->Rows(), 2);
2782 TEST_EQUALITY(reorderedbA->Cols(), 2);
2783
2784 RCP<MultiVector> X = MultiVectorFactory::Build(reorderedA->getDomainMap(), 1);
2785 RCP<MultiVector> RHS = MultiVectorFactory::Build(reorderedA->getRangeMap(), 1);
2786
2787 // apply simple smoother
2788 RHS->putScalar((SC)1.0);
2789 X->putScalar((SC)0.0);
2790
2791 // solve system
2792 simpleSmoother->Apply(*X, *RHS, true); // zero initial guess
2793 RCP<BlockedMultiVector> bX = Teuchos::rcp_dynamic_cast<BlockedMultiVector>(X);
2794 TEST_EQUALITY(bX.is_null(), false);
2795 RCP<MultiVector> XX = bX->Merge();
2796 Teuchos::ArrayRCP<const Scalar> xdata = XX->getData(0);
2797 bool bCheck = true;
2798 for (size_t i = 0; i < XX->getLocalLength(); i++) {
2799 if (i < 10) {
2800 if (xdata[i] != (SC)(1.0 / 3.0)) bCheck = false;
2801 }
2802 if (i >= 10 && i < 15) {
2803 if (xdata[i] != (SC)1.0) bCheck = false;
2804 }
2805 if (i >= 15 && i < 20) {
2806 if (xdata[i] != (SC)0.5) bCheck = false;
2807 }
2808 }
2809 TEST_EQUALITY(bCheck, true);
2810
2811 // Random X
2812 X->setSeed(846930886);
2813 X->randomize();
2814
2815 typedef typename Teuchos::ScalarTraits<Scalar>::magnitudeType magnitude_type;
2816
2817 // Normalize X
2818 Array<magnitude_type> norms(1);
2819 X->norm2(norms);
2820 X->scale(1 / norms[0]);
2821
2822 // Compute RHS corresponding to X
2823 reorderedbA->apply(*X, *RHS, Teuchos::NO_TRANS, (SC)1.0, (SC)0.0);
2824
2825 // Reset X to 0
2826 X->putScalar((SC)0.0);
2827
2828 RHS->norm2(norms);
2829 out << "||RHS|| = " << std::setiosflags(std::ios::fixed) << std::setprecision(10) << norms[0] << std::endl;
2830
2831 out << "solve with zero initial guess" << std::endl;
2832 Teuchos::Array<magnitude_type> initialNorms(1);
2833 X->norm2(initialNorms);
2834 out << " ||X_initial|| = " << std::setiosflags(std::ios::fixed) << std::setprecision(10) << initialNorms[0] << std::endl;
2835
2836 simpleSmoother->Apply(*X, *RHS, true); // zero initial guess
2837
2838 Teuchos::Array<magnitude_type> finalNorms(1);
2839 X->norm2(finalNorms);
2840 Teuchos::Array<magnitude_type> residualNorm1 = Utilities::ResidualNorm(*reorderedbA, *X, *RHS);
2841 out << " ||Residual_final|| = " << std::setiosflags(std::ios::fixed) << std::setprecision(20) << residualNorm1[0] << std::endl;
2842 out << " ||X_final|| = " << std::setiosflags(std::ios::fixed) << std::setprecision(10) << finalNorms[0] << std::endl;
2843
2844 magnitude_type tol = 50. * Teuchos::ScalarTraits<Scalar>::eps();
2845
2846 TEUCHOS_TEST_COMPARE(residualNorm1[0], <, tol, out, success);
2847 TEUCHOS_TEST_COMPARE(finalNorms[0] - Teuchos::ScalarTraits<Scalar>::magnitude(Teuchos::ScalarTraits<Scalar>::one()), <, tol, out, success);
2848 } // end useTpetra
2849 }
2850
2851 TEUCHOS_UNIT_TEST_TEMPLATE_4_DECL(BlockedSmoother, NestedII01I2I_Thyra_SIMPLE_Setup_Apply2, Scalar, LocalOrdinal, GlobalOrdinal, Node) {
2852 #include <MueLu_UseShortNames.hpp>
2853 MUELU_TESTING_SET_OSTREAM;
2854 MUELU_TESTING_LIMIT_SCOPE(Scalar, GlobalOrdinal, Node);
2855 // TODO test only Tpetra because of Ifpack2 smoother!
2856 MUELU_TEST_ONLY_FOR(Xpetra::UseTpetra) {
2857 RCP<const Teuchos::Comm<int> > comm = Parameters::getDefaultComm();
2858 Xpetra::UnderlyingLib lib = MueLuTests::TestHelpers::Parameters::getLib();
2859
2860 Teuchos::RCP<const BlockedCrsMatrix> bop = TestHelpers::TestFactory<Scalar, LocalOrdinal, GlobalOrdinal, Node>::CreateBlocked3x3MatrixThyra(*comm, lib);
2861 Teuchos::RCP<const Matrix> Aconst = Teuchos::rcp_dynamic_cast<const Matrix>(bop);
2862 Teuchos::RCP<Matrix> A = Teuchos::rcp_const_cast<Matrix>(Aconst);
2863
2864 // I don't use the testApply infrastructure because it has no provision for an initial guess.
2865 Level level;
2866 TestHelpers::TestFactory<Scalar, LocalOrdinal, GlobalOrdinal, Node>::createSingleLevelHierarchy(level);
2867 level.Set("A", A);
2868
2869 // Test ReorderBlockAFactory
2870 Teuchos::RCP<ReorderBlockAFactory> rAFact = Teuchos::rcp(new ReorderBlockAFactory());
2871 rAFact->SetFactory("A", MueLu::NoFactory::getRCP());
2872 rAFact->SetParameter(std::string("Reorder Type"), Teuchos::ParameterEntry(std::string("[ [0 1] 2]")));
2873
2874 //////////////////////////////////////////////////////////////////////
2875 // Smoothers
2876 RCP<SimpleSmoother> smootherPrototype = rcp(new SimpleSmoother());
2877 smootherPrototype->SetFactory("A", rAFact);
2878 smootherPrototype->SetParameter("Sweeps", Teuchos::ParameterEntry(Teuchos::as<LocalOrdinal>(15)));
2879 smootherPrototype->SetParameter("Damping factor", Teuchos::ParameterEntry(Teuchos::as<Scalar>(0.9)));
2880 smootherPrototype->SetParameter("UseSIMPLEC", Teuchos::ParameterEntry(false));
2881
2882 std::vector<RCP<SubBlockAFactory> > sA(1, Teuchos::null);
2883 std::vector<RCP<SmootherFactory> > sF(2, Teuchos::null);
2884 std::vector<RCP<FactoryManager> > sM(2, Teuchos::null);
2885
2886 // prediction
2887 std::string strInfo = std::string("{ 1 }");
2888 sA[0] = rcp(new SubBlockAFactory());
2889 sA[0]->SetFactory("A", rAFact);
2890 sA[0]->SetParameter("block row", Teuchos::ParameterEntry(0));
2891 sA[0]->SetParameter("block col", Teuchos::ParameterEntry(0));
2892 sA[0]->SetParameter("Range map: Striding info", Teuchos::ParameterEntry(strInfo));
2893 sA[0]->SetParameter("Domain map: Striding info", Teuchos::ParameterEntry(strInfo));
2894
2895 // create a 2x2 BGS for the prediction eq.
2896 RCP<BlockedGaussSeidelSmoother> smoProtoPredict = Teuchos::rcp(new BlockedGaussSeidelSmoother());
2897 smoProtoPredict->SetParameter("Sweeps", Teuchos::ParameterEntry(1));
2898 smoProtoPredict->SetFactory("A", sA[0]);
2899
2900 for (int l = 0; l < 2; l++) {
2901 Teuchos::RCP<SubBlockAFactory> ssA = rcp(new SubBlockAFactory());
2902 ssA->SetFactory("A", sA[0]);
2903 ssA->SetParameter("block row", Teuchos::ParameterEntry(l)); // local block indices relative to size of blocked operator
2904 ssA->SetParameter("block col", Teuchos::ParameterEntry(l));
2905 ssA->SetParameter("Range map: Striding info", Teuchos::ParameterEntry(strInfo));
2906 ssA->SetParameter("Domain map: Striding info", Teuchos::ParameterEntry(strInfo));
2907 RCP<SmootherPrototype> ssP = rcp(new Ifpack2Smoother(std::string("RELAXATION"), Teuchos::ParameterList(), 0));
2908 ssP->SetFactory("A", ssA);
2909 Teuchos::RCP<SmootherFactory> ssF = Teuchos::rcp(new SmootherFactory(ssP));
2910 Teuchos::RCP<FactoryManager> ssM = Teuchos::rcp(new FactoryManager());
2911 ssM->SetFactory("A", ssA);
2912 ssM->SetFactory("Smoother", ssF);
2913 ssM->SetIgnoreUserData(true);
2914 smoProtoPredict->AddFactoryManager(ssM, l);
2915 }
2916
2917 sF[0] = rcp(new SmootherFactory(smoProtoPredict));
2918
2919 sM[0] = rcp(new FactoryManager());
2920 sM[0]->SetFactory("A", sA[0]);
2921 sM[0]->SetFactory("Smoother", sF[0]);
2922 sM[0]->SetIgnoreUserData(true);
2923
2924 smootherPrototype->SetVelocityPredictionFactoryManager(sM[0]);
2925
2926 // correction
2927 // define SchurComplement Factory
2928 // SchurComp gets a RCP to AFact_ which has to be the 2x2 blocked operator
2929 // It stores the resulting SchurComplement operator as "A" generated by the SchurComplementFactory
2930 // Instead of F^{-1} it uses the approximation \hat{F}^{-1} with \hat{F} = diag(F)
2931 RCP<InverseApproximationFactory> AinvFact = Teuchos::rcp(new InverseApproximationFactory());
2932 AinvFact->SetFactory("A", rAFact);
2933
2934 RCP<SchurComplementFactory> SFact = Teuchos::rcp(new SchurComplementFactory());
2935 SFact->SetParameter("omega", Teuchos::ParameterEntry(Teuchos::as<Scalar>(1.0))); // for Simple, omega is always 1.0 in the SchurComplement
2936 SFact->SetFactory("A", rAFact);
2937 SFact->SetFactory("Ainv", AinvFact);
2938
2939 Teuchos::ParameterList paramList;
2940 paramList.set("relaxation: sweeps", Teuchos::as<LocalOrdinal>(30));
2941 paramList.set("relaxation: damping factor", Teuchos::as<Scalar>(0.9));
2942 RCP<SmootherPrototype> smoProtoCorrect = rcp(new Ifpack2Smoother(std::string("RELAXATION"), paramList, 0));
2943 smoProtoCorrect->SetFactory("A", SFact);
2944 sF[1] = rcp(new SmootherFactory(smoProtoCorrect));
2945
2946 sM[1] = rcp(new FactoryManager());
2947 sM[1]->SetFactory("A", SFact);
2948 sM[1]->SetFactory("Smoother", sF[1]);
2949 sM[1]->SetIgnoreUserData(true);
2950
2951 smootherPrototype->SetSchurCompFactoryManager(sM[1]);
2952
2953 RCP<SmootherFactory> smootherFact = rcp(new SmootherFactory(smootherPrototype));
2954
2955 // main factory manager
2956 FactoryManager M;
2957 M.SetFactory("Smoother", smootherFact);
2958 M.SetFactory("A", rAFact);
2959
2960 MueLu::SetFactoryManager SFM(Teuchos::rcpFromRef(level), Teuchos::rcpFromRef(M));
2961
2962 // request BGS smoother (and all dependencies) on level
2963 level.Request("A", rAFact.get());
2964 level.Request("Smoother", smootherFact.get());
2965 level.Request("PreSmoother", smootherFact.get());
2966 level.Request("PostSmoother", smootherFact.get());
2967
2968 // smootherFact->DeclareInput(level);
2969 smootherFact->Build(level);
2970
2971 level.print(std::cout, Teuchos::VERB_EXTREME);
2972
2973 RCP<SmootherBase> simpleSmoother = level.Get<RCP<SmootherBase> >("PreSmoother", smootherFact.get());
2974
2975 RCP<Matrix> reorderedA = level.Get<RCP<Matrix> >("A", rAFact.get());
2976 RCP<BlockedCrsMatrix> reorderedbA = Teuchos::rcp_dynamic_cast<BlockedCrsMatrix>(reorderedA);
2977
2978 RCP<MultiVector> X = MultiVectorFactory::Build(reorderedA->getDomainMap(), 1);
2979 RCP<MultiVector> RHS = MultiVectorFactory::Build(reorderedA->getRangeMap(), 1);
2980
2981 // Random X
2982 X->setSeed(846930886);
2983 X->randomize();
2984
2985 typedef typename Teuchos::ScalarTraits<Scalar>::magnitudeType magnitude_type;
2986
2987 // Normalize X
2988 Array<magnitude_type> norms(1);
2989 X->norm2(norms);
2990 X->scale(1 / norms[0]);
2991
2992 // Compute RHS corresponding to X
2993 reorderedA->apply(*X, *RHS, Teuchos::NO_TRANS, (SC)1.0, (SC)0.0);
2994
2995 // Reset X to 0
2996 X->putScalar((SC)0.0);
2997
2998 RHS->norm2(norms);
2999 out << "||RHS|| = " << std::setiosflags(std::ios::fixed) << std::setprecision(10) << norms[0] << std::endl;
3000
3001 out << "solve with zero initial guess" << std::endl;
3002 Teuchos::Array<magnitude_type> initialNorms(1);
3003 X->norm2(initialNorms);
3004 out << " ||X_initial|| = " << std::setiosflags(std::ios::fixed) << std::setprecision(10) << initialNorms[0] << std::endl;
3005
3006 simpleSmoother->Apply(*X, *RHS, true); // zero initial guess
3007
3008 Teuchos::Array<magnitude_type> finalNorms(1);
3009 X->norm2(finalNorms);
3010 Teuchos::Array<magnitude_type> residualNorm1 = Utilities::ResidualNorm(*reorderedA, *X, *RHS);
3011 out << " ||Residual_final|| = " << std::setiosflags(std::ios::fixed) << std::setprecision(20) << residualNorm1[0] << std::endl;
3012 out << " ||X_final|| = " << std::setiosflags(std::ios::fixed) << std::setprecision(10) << finalNorms[0] << std::endl;
3013
3014 TEUCHOS_TEST_COMPARE(residualNorm1[0], <, 4e-3, out, success);
3015 TEUCHOS_TEST_COMPARE(residualNorm1[0], >, 2e-3, out, success);
3016 } // end UseTpetra
3017 }
3018
3019 TEUCHOS_UNIT_TEST_TEMPLATE_4_DECL(BlockedSmoother, NestedI0I21II_Thyra_SIMPLE_Setup_Apply3, Scalar, LocalOrdinal, GlobalOrdinal, Node) {
3020 #include <MueLu_UseShortNames.hpp>
3021 MUELU_TESTING_SET_OSTREAM;
3022 MUELU_TESTING_LIMIT_SCOPE(Scalar, GlobalOrdinal, Node);
3023 // TODO test only Tpetra because of Ifpack2 smoother!
3024 MUELU_TEST_ONLY_FOR(Xpetra::UseTpetra) {
3025 RCP<const Teuchos::Comm<int> > comm = Parameters::getDefaultComm();
3026 Xpetra::UnderlyingLib lib = MueLuTests::TestHelpers::Parameters::getLib();
3027
3028 Teuchos::RCP<const BlockedCrsMatrix> bop = TestHelpers::TestFactory<Scalar, LocalOrdinal, GlobalOrdinal, Node>::CreateBlocked3x3MatrixThyra(*comm, lib);
3029 Teuchos::RCP<const Matrix> Aconst = Teuchos::rcp_dynamic_cast<const Matrix>(bop);
3030 Teuchos::RCP<Matrix> A = Teuchos::rcp_const_cast<Matrix>(Aconst);
3031
3032 // I don't use the testApply infrastructure because it has no provision for an initial guess.
3033 Level level;
3034 TestHelpers::TestFactory<Scalar, LocalOrdinal, GlobalOrdinal, Node>::createSingleLevelHierarchy(level);
3035 level.Set("A", A);
3036
3037 // Test ReorderBlockAFactory
3038 Teuchos::RCP<ReorderBlockAFactory> rAFact = Teuchos::rcp(new ReorderBlockAFactory());
3039 rAFact->SetFactory("A", MueLu::NoFactory::getRCP());
3040 rAFact->SetParameter(std::string("Reorder Type"), Teuchos::ParameterEntry(std::string("[ 0 [ 2 1 ]]")));
3041
3042 //////////////////////////////////////////////////////////////////////
3043 // Smoothers
3044 RCP<SimpleSmoother> smootherPrototype = rcp(new SimpleSmoother());
3045 smootherPrototype->SetFactory("A", rAFact);
3046 smootherPrototype->SetParameter("Sweeps", Teuchos::ParameterEntry(Teuchos::as<LocalOrdinal>(15)));
3047 smootherPrototype->SetParameter("Damping factor", Teuchos::ParameterEntry(Teuchos::as<Scalar>(0.9)));
3048 smootherPrototype->SetParameter("UseSIMPLEC", Teuchos::ParameterEntry(false));
3049
3050 std::vector<RCP<SubBlockAFactory> > sA(1, Teuchos::null);
3051 std::vector<RCP<SmootherFactory> > sF(2, Teuchos::null);
3052 std::vector<RCP<FactoryManager> > sM(2, Teuchos::null);
3053
3054 // prediction
3055 std::string strInfo = std::string("{ 1 }");
3056 sA[0] = rcp(new SubBlockAFactory());
3057 sA[0]->SetFactory("A", rAFact);
3058 sA[0]->SetParameter("block row", Teuchos::ParameterEntry(0));
3059 sA[0]->SetParameter("block col", Teuchos::ParameterEntry(0));
3060 sA[0]->SetParameter("Range map: Striding info", Teuchos::ParameterEntry(strInfo));
3061 sA[0]->SetParameter("Domain map: Striding info", Teuchos::ParameterEntry(strInfo));
3062
3063 RCP<SmootherPrototype> smoProtoPredict = rcp(new Ifpack2Smoother(std::string("RELAXATION"), Teuchos::ParameterList(), 0));
3064 smoProtoPredict->SetFactory("A", sA[0]);
3065 sF[0] = rcp(new SmootherFactory(smoProtoPredict));
3066
3067 sM[0] = rcp(new FactoryManager());
3068 sM[0]->SetFactory("A", sA[0]);
3069 sM[0]->SetFactory("Smoother", sF[0]);
3070 sM[0]->SetIgnoreUserData(true);
3071
3072 smootherPrototype->SetVelocityPredictionFactoryManager(sM[0]);
3073
3074 // correction
3075 // define SchurComplement Factory
3076 // SchurComp gets a RCP to AFact_ which has to be the 2x2 blocked operator
3077 // It stores the resulting SchurComplement operator as "A" generated by the SchurComplementFactory
3078 // Instead of F^{-1} it uses the approximation \hat{F}^{-1} with \hat{F} = diag(F)
3079 RCP<InverseApproximationFactory> AinvFact = Teuchos::rcp(new InverseApproximationFactory());
3080 AinvFact->SetFactory("A", rAFact);
3081
3082 RCP<SchurComplementFactory> SFact = Teuchos::rcp(new SchurComplementFactory());
3083 SFact->SetParameter("omega", Teuchos::ParameterEntry(Teuchos::as<Scalar>(1.0))); // for Simple, omega is always 1.0 in the SchurComplement
3084 SFact->SetFactory("A", rAFact);
3085 SFact->SetFactory("Ainv", AinvFact);
3086
3087 // create a 2x2 Simple for the prediction eq.
3088 RCP<SimpleSmoother> smoProtoCorrect = Teuchos::rcp(new SimpleSmoother());
3089 smoProtoCorrect->SetParameter("Sweeps", Teuchos::ParameterEntry(1));
3090 smoProtoCorrect->SetParameter("Damping factor", Teuchos::ParameterEntry(Teuchos::as<Scalar>(1.0)));
3091 smoProtoCorrect->SetParameter("UseSIMPLEC", Teuchos::ParameterEntry(false));
3092 smoProtoCorrect->SetFactory("A", SFact);
3093
3094 for (int l = 0; l < 2; l++) {
3095 Teuchos::RCP<SubBlockAFactory> ssA = rcp(new SubBlockAFactory());
3096 ssA->SetFactory("A", SFact);
3097 ssA->SetParameter("block row", Teuchos::ParameterEntry(l)); // local block indices relative to size of blocked operator
3098 ssA->SetParameter("block col", Teuchos::ParameterEntry(l));
3099 ssA->SetParameter("Range map: Striding info", Teuchos::ParameterEntry(strInfo));
3100 ssA->SetParameter("Domain map: Striding info", Teuchos::ParameterEntry(strInfo));
3101 RCP<SmootherPrototype> ssP = rcp(new Ifpack2Smoother(std::string("RELAXATION"), Teuchos::ParameterList(), 0));
3102 ssP->SetFactory("A", ssA);
3103 Teuchos::RCP<SmootherFactory> ssF = Teuchos::rcp(new SmootherFactory(ssP));
3104 Teuchos::RCP<FactoryManager> ssM = Teuchos::rcp(new FactoryManager());
3105 ssM->SetFactory("A", ssA);
3106 ssM->SetFactory("Smoother", ssF);
3107 ssM->SetIgnoreUserData(true);
3108 if (l == 0)
3109 smoProtoCorrect->SetVelocityPredictionFactoryManager(ssM);
3110 else
3111 smoProtoCorrect->SetSchurCompFactoryManager(ssM);
3112 }
3113
3114 sF[1] = rcp(new SmootherFactory(smoProtoCorrect));
3115
3116 sM[1] = rcp(new FactoryManager());
3117 sM[1]->SetFactory("A", SFact);
3118 sM[1]->SetFactory("Smoother", sF[1]);
3119 sM[1]->SetIgnoreUserData(true);
3120
3121 smootherPrototype->SetSchurCompFactoryManager(sM[1]);
3122
3123 RCP<SmootherFactory> smootherFact = rcp(new SmootherFactory(smootherPrototype));
3124
3125 // main factory manager
3126 FactoryManager M;
3127 M.SetFactory("Smoother", smootherFact);
3128 M.SetFactory("A", rAFact);
3129
3130 MueLu::SetFactoryManager SFM(Teuchos::rcpFromRef(level), Teuchos::rcpFromRef(M));
3131
3132 // request BGS smoother (and all dependencies) on level
3133 level.Request("A", rAFact.get());
3134 level.Request("Smoother", smootherFact.get());
3135 level.Request("PreSmoother", smootherFact.get());
3136 level.Request("PostSmoother", smootherFact.get());
3137
3138 // smootherFact->DeclareInput(level);
3139 smootherFact->Build(level);
3140
3141 level.print(std::cout, Teuchos::VERB_EXTREME);
3142
3143 RCP<SmootherBase> simpleSmoother = level.Get<RCP<SmootherBase> >("PreSmoother", smootherFact.get());
3144
3145 RCP<Matrix> reorderedA = level.Get<RCP<Matrix> >("A", rAFact.get());
3146 RCP<BlockedCrsMatrix> reorderedbA = Teuchos::rcp_dynamic_cast<BlockedCrsMatrix>(reorderedA);
3147
3148 RCP<MultiVector> X = MultiVectorFactory::Build(reorderedA->getDomainMap(), 1);
3149 RCP<MultiVector> RHS = MultiVectorFactory::Build(reorderedA->getRangeMap(), 1);
3150
3151 // Random X
3152 X->setSeed(846930886);
3153 X->randomize();
3154
3155 typedef typename Teuchos::ScalarTraits<Scalar>::magnitudeType magnitude_type;
3156
3157 // Normalize X
3158 Array<magnitude_type> norms(1);
3159 X->norm2(norms);
3160 X->scale(1 / norms[0]);
3161
3162 // Compute RHS corresponding to X
3163 reorderedA->apply(*X, *RHS, Teuchos::NO_TRANS, (SC)1.0, (SC)0.0);
3164
3165 // Reset X to 0
3166 X->putScalar((SC)0.0);
3167
3168 RHS->norm2(norms);
3169 out << "||RHS|| = " << std::setiosflags(std::ios::fixed) << std::setprecision(10) << norms[0] << std::endl;
3170
3171 out << "solve with zero initial guess" << std::endl;
3172 Teuchos::Array<magnitude_type> initialNorms(1);
3173 X->norm2(initialNorms);
3174 out << " ||X_initial|| = " << std::setiosflags(std::ios::fixed) << std::setprecision(10) << initialNorms[0] << std::endl;
3175
3176 simpleSmoother->Apply(*X, *RHS, true); // zero initial guess
3177
3178 Teuchos::Array<magnitude_type> finalNorms(1);
3179 X->norm2(finalNorms);
3180 Teuchos::Array<magnitude_type> residualNorm1 = Utilities::ResidualNorm(*reorderedA, *X, *RHS);
3181 out << " ||Residual_final|| = " << std::setiosflags(std::ios::fixed) << std::setprecision(20) << residualNorm1[0] << std::endl;
3182 out << " ||X_final|| = " << std::setiosflags(std::ios::fixed) << std::setprecision(10) << finalNorms[0] << std::endl;
3183
3184 TEUCHOS_TEST_COMPARE(residualNorm1[0], <, 60e-4, out, success);
3185 TEUCHOS_TEST_COMPARE(residualNorm1[0], >, 25e-4, out, success);
3186 } // end UseTpetra
3187 }
3188
3189 TEUCHOS_UNIT_TEST_TEMPLATE_4_DECL(BlockedSmoother, BS_Setup_Apply, Scalar, LocalOrdinal, GlobalOrdinal, Node) {
3190 #include <MueLu_UseShortNames.hpp>
3191 MUELU_TESTING_SET_OSTREAM;
3192 MUELU_TESTING_LIMIT_SCOPE(Scalar, GlobalOrdinal, Node);
3193 // TODO test only Tpetra because of Ifpack2 smoother!
3194 MUELU_TEST_ONLY_FOR(Xpetra::UseTpetra) {
3195 RCP<const Teuchos::Comm<int> > comm = Parameters::getDefaultComm();
3196 Xpetra::UnderlyingLib lib = MueLuTests::TestHelpers::Parameters::getLib();
3197
3198 int noBlocks = 2;
3199 Teuchos::RCP<const BlockedCrsMatrix> bop = TestHelpers::TestFactory<Scalar, LocalOrdinal, GlobalOrdinal, Node>::CreateBlockDiagonalExampleMatrix(lib, noBlocks, comm);
3200 Teuchos::RCP<const Matrix> Aconst = Teuchos::rcp_dynamic_cast<const Matrix>(bop);
3201 Teuchos::RCP<Matrix> A = Teuchos::rcp_const_cast<Matrix>(Aconst);
3202
3203 // I don't use the testApply infrastructure because it has no provision for an initial guess.
3204 Level level;
3205 TestHelpers::TestFactory<Scalar, LocalOrdinal, GlobalOrdinal, Node>::createSingleLevelHierarchy(level);
3206 level.Set("A", A);
3207
3208 //////////////////////////////////////////////////////////////////////
3209 // Smoothers
3210 RCP<BraessSarazinSmoother> smootherPrototype = rcp(new BraessSarazinSmoother());
3211 smootherPrototype->SetParameter("Sweeps", Teuchos::ParameterEntry(Teuchos::as<LocalOrdinal>(1)));
3212 smootherPrototype->SetParameter("Damping factor", Teuchos::ParameterEntry(Teuchos::as<Scalar>(1.0)));
3213
3214 std::vector<RCP<SmootherFactory> > sF(1, Teuchos::null);
3215 std::vector<RCP<FactoryManager> > sM(1, Teuchos::null);
3216
3217 // correction
3218 // define SchurComplement Factory
3219 // SchurComp gets a RCP to AFact_ which has to be the 2x2 blocked operator
3220 // It stores the resulting SchurComplement operator as "A" generated by the SchurComplementFactory
3221 // Instead of F^{-1} it uses the approximation \hat{F}^{-1} with \hat{F} = diag(F)
3222 RCP<SchurComplementFactory> SFact = Teuchos::rcp(new SchurComplementFactory());
3223 SFact->SetParameter("omega", Teuchos::ParameterEntry(Teuchos::as<Scalar>(1.0))); // for Simple, omega is always 1.0 in the SchurComplement
3224 SFact->SetFactory("A", MueLu::NoFactory::getRCP());
3225
3226 RCP<SmootherPrototype> smoProtoCorrect = rcp(new Ifpack2Smoother(std::string("RELAXATION"), Teuchos::ParameterList(), 0));
3227 smoProtoCorrect->SetFactory("A", SFact);
3228 sF[0] = rcp(new SmootherFactory(smoProtoCorrect));
3229
3230 sM[0] = rcp(new FactoryManager());
3231 sM[0]->SetFactory("A", SFact);
3232 sM[0]->SetFactory("Smoother", sF[0]);
3233 sM[0]->SetIgnoreUserData(true);
3234
3235 smootherPrototype->AddFactoryManager(sM[0], 0);
3236
3237 RCP<SmootherFactory> smootherFact = rcp(new SmootherFactory(smootherPrototype));
3238
3239 // main factory manager
3240 FactoryManager M;
3241 M.SetFactory("Smoother", smootherFact);
3242
3243 MueLu::SetFactoryManager SFM(Teuchos::rcpFromRef(level), Teuchos::rcpFromRef(M));
3244
3245 // request BGS smoother (and all dependencies) on level
3246 level.Request("Smoother", smootherFact.get());
3247 level.Request("PreSmoother", smootherFact.get());
3248 level.Request("PostSmoother", smootherFact.get());
3249
3250 // smootherFact->DeclareInput(level);
3251 smootherFact->Build(level);
3252
3253 level.print(std::cout, Teuchos::VERB_EXTREME);
3254
3255 RCP<SmootherBase> simpleSmoother = level.Get<RCP<SmootherBase> >("PreSmoother", smootherFact.get());
3256
3257 RCP<MultiVector> X = MultiVectorFactory::Build(A->getDomainMap(), 1);
3258 RCP<MultiVector> RHS = MultiVectorFactory::Build(A->getRangeMap(), 1);
3259
3260 // Random X
3261 X->setSeed(846930886);
3262 X->randomize();
3263
3264 typedef typename Teuchos::ScalarTraits<Scalar>::magnitudeType magnitude_type;
3265
3266 // Normalize X
3267 Array<magnitude_type> norms(1);
3268 X->norm2(norms);
3269 X->scale(1 / norms[0]);
3270
3271 // Compute RHS corresponding to X
3272 A->apply(*X, *RHS, Teuchos::NO_TRANS, (SC)1.0, (SC)0.0);
3273
3274 // Reset X to 0
3275 X->putScalar((SC)0.0);
3276
3277 RHS->norm2(norms);
3278 out << "||RHS|| = " << std::setiosflags(std::ios::fixed) << std::setprecision(10) << norms[0] << std::endl;
3279
3280 out << "solve with zero initial guess" << std::endl;
3281 Teuchos::Array<magnitude_type> initialNorms(1);
3282 X->norm2(initialNorms);
3283 out << " ||X_initial|| = " << std::setiosflags(std::ios::fixed) << std::setprecision(10) << initialNorms[0] << std::endl;
3284
3285 simpleSmoother->Apply(*X, *RHS, true); // zero initial guess
3286
3287 Teuchos::Array<magnitude_type> finalNorms(1);
3288 X->norm2(finalNorms);
3289 Teuchos::Array<magnitude_type> residualNorm1 = Utilities::ResidualNorm(*A, *X, *RHS);
3290 out << " ||Residual_final|| = " << std::setiosflags(std::ios::fixed) << std::setprecision(20) << residualNorm1[0] << std::endl;
3291 out << " ||X_final|| = " << std::setiosflags(std::ios::fixed) << std::setprecision(10) << finalNorms[0] << std::endl;
3292
3293 magnitude_type tol = 50. * Teuchos::ScalarTraits<Scalar>::eps();
3294
3295 TEUCHOS_TEST_COMPARE(residualNorm1[0], <, tol, out, success);
3296 TEUCHOS_TEST_COMPARE(finalNorms[0] - Teuchos::ScalarTraits<Scalar>::magnitude(Teuchos::ScalarTraits<Scalar>::one()), <, tol, out, success);
3297
3298 out << "solve with random initial guess" << std::endl;
3299 X->randomize();
3300 X->norm2(initialNorms);
3301 out << " ||X_initial|| = " << std::setiosflags(std::ios::fixed) << std::setprecision(10) << initialNorms[0] << std::endl;
3302
3303 simpleSmoother->Apply(*X, *RHS, false); // nonzero initial guess
3304
3305 X->norm2(finalNorms);
3306 Teuchos::Array<magnitude_type> residualNorm2 = Utilities::ResidualNorm(*A, *X, *RHS);
3307 out << " ||Residual_final|| = " << std::setiosflags(std::ios::fixed) << std::setprecision(20) << residualNorm2[0] << std::endl;
3308 out << " ||X_final|| = " << std::setiosflags(std::ios::fixed) << std::setprecision(10) << finalNorms[0] << std::endl;
3309
3310 TEUCHOS_TEST_COMPARE(residualNorm2[0], <, tol, out, success);
3311 TEUCHOS_TEST_COMPARE(finalNorms[0] - Teuchos::ScalarTraits<Scalar>::magnitude(Teuchos::ScalarTraits<Scalar>::one()), <, tol, out, success);
3312
3313 if (comm->getSize() == 1) {
3314 TEST_EQUALITY(residualNorm1[0] != residualNorm2[0], true);
3315 } else {
3316 out << "Pass/Fail is only checked in serial." << std::endl;
3317 }
3318 } // end UseTpetra
3319 }
3320
3321 TEUCHOS_UNIT_TEST_TEMPLATE_4_DECL(BlockedSmoother, NestedII20I1I_BS_Setup_Apply, Scalar, LocalOrdinal, GlobalOrdinal, Node) {
3322 #include <MueLu_UseShortNames.hpp>
3323 MUELU_TESTING_SET_OSTREAM;
3324 MUELU_TESTING_LIMIT_SCOPE(Scalar, GlobalOrdinal, Node);
3325 // TODO test only Tpetra because of Ifpack2 smoother!
3326 MUELU_TEST_ONLY_FOR(Xpetra::UseTpetra) {
3327 RCP<const Teuchos::Comm<int> > comm = Parameters::getDefaultComm();
3328 Xpetra::UnderlyingLib lib = MueLuTests::TestHelpers::Parameters::getLib();
3329
3330 Teuchos::RCP<const BlockedCrsMatrix> bop = TestHelpers::TestFactory<Scalar, LocalOrdinal, GlobalOrdinal, Node>::CreateBlockDiagonalExampleMatrix(lib, 3, comm);
3331 Teuchos::RCP<const Matrix> Aconst = Teuchos::rcp_dynamic_cast<const Matrix>(bop);
3332 Teuchos::RCP<Matrix> A = Teuchos::rcp_const_cast<Matrix>(Aconst);
3333
3334 // I don't use the testApply infrastructure because it has no provision for an initial guess.
3335 Level level;
3336 TestHelpers::TestFactory<Scalar, LocalOrdinal, GlobalOrdinal, Node>::createSingleLevelHierarchy(level);
3337 level.Set("A", A);
3338
3339 // Test ReorderBlockAFactory
3340 Teuchos::RCP<ReorderBlockAFactory> rAFact = Teuchos::rcp(new ReorderBlockAFactory());
3341 rAFact->SetFactory("A", MueLu::NoFactory::getRCP());
3342 rAFact->SetParameter(std::string("Reorder Type"), Teuchos::ParameterEntry(std::string("[ [2 0] 1]")));
3343
3344 //////////////////////////////////////////////////////////////////////
3345 // Smoothers
3346 RCP<BraessSarazinSmoother> smootherPrototype = rcp(new BraessSarazinSmoother());
3347 smootherPrototype->SetFactory("A", rAFact);
3348 smootherPrototype->SetParameter("Sweeps", Teuchos::ParameterEntry(Teuchos::as<LocalOrdinal>(1)));
3349 smootherPrototype->SetParameter("Damping factor", Teuchos::ParameterEntry(Teuchos::as<Scalar>(1.0)));
3350
3351 std::vector<RCP<SmootherFactory> > sF(1, Teuchos::null);
3352 std::vector<RCP<FactoryManager> > sM(1, Teuchos::null);
3353
3354 // correction
3355 // define SchurComplement Factory
3356 // SchurComp gets a RCP to AFact_ which has to be the 2x2 blocked operator
3357 // It stores the resulting SchurComplement operator as "A" generated by the SchurComplementFactory
3358 // Instead of F^{-1} it uses the approximation \hat{F}^{-1} with \hat{F} = diag(F)
3359 RCP<InverseApproximationFactory> AinvFact = Teuchos::rcp(new InverseApproximationFactory());
3360 AinvFact->SetFactory("A", rAFact);
3361
3362 RCP<SchurComplementFactory> SFact = Teuchos::rcp(new SchurComplementFactory());
3363 SFact->SetParameter("omega", Teuchos::ParameterEntry(Teuchos::as<Scalar>(1.0))); // for Simple, omega is always 1.0 in the SchurComplement
3364 SFact->SetFactory("A", rAFact);
3365 SFact->SetFactory("Ainv", AinvFact);
3366
3367 RCP<SmootherPrototype> smoProtoCorrect = rcp(new Ifpack2Smoother(std::string("RELAXATION"), Teuchos::ParameterList(), 0));
3368 smoProtoCorrect->SetFactory("A", SFact);
3369 sF[0] = rcp(new SmootherFactory(smoProtoCorrect));
3370
3371 sM[0] = rcp(new FactoryManager());
3372 sM[0]->SetFactory("A", SFact);
3373 sM[0]->SetFactory("Smoother", sF[0]);
3374 sM[0]->SetIgnoreUserData(true);
3375
3376 smootherPrototype->AddFactoryManager(sM[0], 0);
3377
3378 RCP<SmootherFactory> smootherFact = rcp(new SmootherFactory(smootherPrototype));
3379
3380 // main factory manager
3381 FactoryManager M;
3382 M.SetFactory("Smoother", smootherFact);
3383 M.SetFactory("A", rAFact);
3384
3385 MueLu::SetFactoryManager SFM(Teuchos::rcpFromRef(level), Teuchos::rcpFromRef(M));
3386
3387 // request BGS smoother (and all dependencies) on level
3388 level.Request("A", rAFact.get());
3389 level.Request("Smoother", smootherFact.get());
3390 level.Request("PreSmoother", smootherFact.get());
3391 level.Request("PostSmoother", smootherFact.get());
3392
3393 // smootherFact->DeclareInput(level);
3394 smootherFact->Build(level);
3395
3396 level.print(std::cout, Teuchos::VERB_EXTREME);
3397
3398 RCP<SmootherBase> simpleSmoother = level.Get<RCP<SmootherBase> >("PreSmoother", smootherFact.get());
3399
3400 RCP<Matrix> reorderedA = level.Get<RCP<Matrix> >("A", rAFact.get());
3401 RCP<BlockedCrsMatrix> reorderedbA = Teuchos::rcp_dynamic_cast<BlockedCrsMatrix>(reorderedA);
3402
3403 TEST_EQUALITY(reorderedbA->Rows(), 2);
3404 TEST_EQUALITY(reorderedbA->Cols(), 2);
3405
3406 RCP<MultiVector> X = MultiVectorFactory::Build(reorderedA->getDomainMap(), 1);
3407 RCP<MultiVector> RHS = MultiVectorFactory::Build(reorderedA->getRangeMap(), 1);
3408
3409 // apply simple smoother
3410 RHS->putScalar((SC)1.0);
3411 X->putScalar((SC)0.0);
3412
3413 // solve system
3414 simpleSmoother->Apply(*X, *RHS, true); // zero initial guess
3415 RCP<BlockedMultiVector> bX = Teuchos::rcp_dynamic_cast<BlockedMultiVector>(X);
3416 TEST_EQUALITY(bX.is_null(), false);
3417 RCP<MultiVector> XX = bX->Merge();
3418 Teuchos::ArrayRCP<const Scalar> xdata = XX->getData(0);
3419 bool bCheck = true;
3420 for (size_t i = 0; i < XX->getLocalLength(); i++) {
3421 if (i < 10) {
3422 if (xdata[i] != (SC)(1.0 / 3.0)) bCheck = false;
3423 }
3424 if (i >= 10 && i < 15) {
3425 if (xdata[i] != (SC)1.0) bCheck = false;
3426 }
3427 if (i >= 15 && i < 20) {
3428 if (xdata[i] != (SC)0.5) bCheck = false;
3429 }
3430 }
3431 TEST_EQUALITY(bCheck, true);
3432
3433 // Random X
3434 X->setSeed(846930886);
3435 X->randomize();
3436
3437 typedef typename Teuchos::ScalarTraits<Scalar>::magnitudeType magnitude_type;
3438
3439 // Normalize X
3440 Array<magnitude_type> norms(1);
3441 X->norm2(norms);
3442 X->scale(1 / norms[0]);
3443
3444 // Compute RHS corresponding to X
3445 reorderedbA->apply(*X, *RHS, Teuchos::NO_TRANS, (SC)1.0, (SC)0.0);
3446
3447 // Reset X to 0
3448 X->putScalar((SC)0.0);
3449
3450 RHS->norm2(norms);
3451 out << "||RHS|| = " << std::setiosflags(std::ios::fixed) << std::setprecision(10) << norms[0] << std::endl;
3452
3453 out << "solve with zero initial guess" << std::endl;
3454 Teuchos::Array<magnitude_type> initialNorms(1);
3455 X->norm2(initialNorms);
3456 out << " ||X_initial|| = " << std::setiosflags(std::ios::fixed) << std::setprecision(10) << initialNorms[0] << std::endl;
3457
3458 simpleSmoother->Apply(*X, *RHS, true); // zero initial guess
3459
3460 Teuchos::Array<magnitude_type> finalNorms(1);
3461 X->norm2(finalNorms);
3462 Teuchos::Array<magnitude_type> residualNorm1 = Utilities::ResidualNorm(*reorderedbA, *X, *RHS);
3463 out << " ||Residual_final|| = " << std::setiosflags(std::ios::fixed) << std::setprecision(20) << residualNorm1[0] << std::endl;
3464 out << " ||X_final|| = " << std::setiosflags(std::ios::fixed) << std::setprecision(10) << finalNorms[0] << std::endl;
3465
3466 magnitude_type tol = 50. * Teuchos::ScalarTraits<Scalar>::eps();
3467
3468 TEUCHOS_TEST_COMPARE(residualNorm1[0], <, tol, out, success);
3469 TEUCHOS_TEST_COMPARE(finalNorms[0] - Teuchos::ScalarTraits<Scalar>::magnitude(Teuchos::ScalarTraits<Scalar>::one()), <, tol, out, success);
3470 } // end useTpetra
3471 }
3472
3473 TEUCHOS_UNIT_TEST_TEMPLATE_4_DECL(BlockedSmoother, NestedI2I10II_BS_Setup_Apply, Scalar, LocalOrdinal, GlobalOrdinal, Node) {
3474 #include <MueLu_UseShortNames.hpp>
3475 MUELU_TESTING_SET_OSTREAM;
3476 MUELU_TESTING_LIMIT_SCOPE(Scalar, GlobalOrdinal, Node);
3477 // TODO test only Tpetra because of Ifpack2 smoother!
3478 MUELU_TEST_ONLY_FOR(Xpetra::UseTpetra) {
3479 RCP<const Teuchos::Comm<int> > comm = Parameters::getDefaultComm();
3480 Xpetra::UnderlyingLib lib = MueLuTests::TestHelpers::Parameters::getLib();
3481
3482 Teuchos::RCP<const BlockedCrsMatrix> bop = TestHelpers::TestFactory<Scalar, LocalOrdinal, GlobalOrdinal, Node>::CreateBlockDiagonalExampleMatrix(lib, 3, comm);
3483 Teuchos::RCP<const Matrix> Aconst = Teuchos::rcp_dynamic_cast<const Matrix>(bop);
3484 Teuchos::RCP<Matrix> A = Teuchos::rcp_const_cast<Matrix>(Aconst);
3485
3486 // I don't use the testApply infrastructure because it has no provision for an initial guess.
3487 Level level;
3488 TestHelpers::TestFactory<Scalar, LocalOrdinal, GlobalOrdinal, Node>::createSingleLevelHierarchy(level);
3489 level.Set("A", A);
3490
3491 // Test ReorderBlockAFactory
3492 Teuchos::RCP<ReorderBlockAFactory> rAFact = Teuchos::rcp(new ReorderBlockAFactory());
3493 rAFact->SetFactory("A", MueLu::NoFactory::getRCP());
3494 rAFact->SetParameter(std::string("Reorder Type"), Teuchos::ParameterEntry(std::string("[ 2 [ 1 0 ]]")));
3495
3496 //////////////////////////////////////////////////////////////////////
3497 // Smoothers
3498 RCP<BraessSarazinSmoother> smootherPrototype = rcp(new BraessSarazinSmoother());
3499 smootherPrototype->SetFactory("A", rAFact);
3500 smootherPrototype->SetParameter("Sweeps", Teuchos::ParameterEntry(Teuchos::as<LocalOrdinal>(1)));
3501 smootherPrototype->SetParameter("Damping factor", Teuchos::ParameterEntry(Teuchos::as<Scalar>(1.0)));
3502
3503 std::vector<RCP<SmootherFactory> > sF(1, Teuchos::null);
3504 std::vector<RCP<FactoryManager> > sM(1, Teuchos::null);
3505
3506 // correction
3507 // define SchurComplement Factory
3508 // SchurComp gets a RCP to AFact_ which has to be the 2x2 blocked operator
3509 // It stores the resulting SchurComplement operator as "A" generated by the SchurComplementFactory
3510 // Instead of F^{-1} it uses the approximation \hat{F}^{-1} with \hat{F} = diag(F)
3511 RCP<InverseApproximationFactory> AinvFact = Teuchos::rcp(new InverseApproximationFactory());
3512 AinvFact->SetFactory("A", rAFact);
3513
3514 RCP<SchurComplementFactory> SFact = Teuchos::rcp(new SchurComplementFactory());
3515 SFact->SetParameter("omega", Teuchos::ParameterEntry(Teuchos::as<Scalar>(1.0))); // for Simple, omega is always 1.0 in the SchurComplement
3516 SFact->SetFactory("A", rAFact);
3517 SFact->SetFactory("Ainv", AinvFact);
3518
3519 RCP<BraessSarazinSmoother> smoProtoCorrect = Teuchos::rcp(new BraessSarazinSmoother());
3520 smoProtoCorrect->SetParameter("Sweeps", Teuchos::ParameterEntry(1));
3521 smoProtoCorrect->SetParameter("Damping factor", Teuchos::ParameterEntry(Teuchos::as<Scalar>(1.0)));
3522 smoProtoCorrect->SetFactory("A", SFact);
3523
3524 std::string strInfo = std::string("{ 1 }");
3525 Teuchos::RCP<SubBlockAFactory> ssA = rcp(new SubBlockAFactory());
3526 ssA->SetFactory("A", SFact);
3527 ssA->SetParameter("block row", Teuchos::ParameterEntry(1)); // local block indices relative to size of blocked operator
3528 ssA->SetParameter("block col", Teuchos::ParameterEntry(1));
3529 ssA->SetParameter("Range map: Striding info", Teuchos::ParameterEntry(strInfo));
3530 ssA->SetParameter("Domain map: Striding info", Teuchos::ParameterEntry(strInfo));
3531 RCP<SmootherPrototype> ssP = rcp(new Ifpack2Smoother(std::string("RELAXATION"), Teuchos::ParameterList(), 0));
3532 ssP->SetFactory("A", ssA);
3533 Teuchos::RCP<SmootherFactory> ssF = Teuchos::rcp(new SmootherFactory(ssP));
3534 Teuchos::RCP<FactoryManager> ssM = Teuchos::rcp(new FactoryManager());
3535 ssM->SetFactory("A", ssA);
3536 ssM->SetFactory("Smoother", ssF);
3537 ssM->SetIgnoreUserData(true);
3538 smoProtoCorrect->AddFactoryManager(ssM, 0);
3539
3540 sF[0] = rcp(new SmootherFactory(smoProtoCorrect));
3541
3542 sM[0] = rcp(new FactoryManager());
3543 sM[0]->SetFactory("A", SFact);
3544 sM[0]->SetFactory("Smoother", sF[0]);
3545 sM[0]->SetIgnoreUserData(true);
3546
3547 smootherPrototype->AddFactoryManager(sM[0], 0);
3548
3549 RCP<SmootherFactory> smootherFact = rcp(new SmootherFactory(smootherPrototype));
3550
3551 // main factory manager
3552 FactoryManager M;
3553 M.SetFactory("Smoother", smootherFact);
3554 M.SetFactory("A", rAFact);
3555
3556 MueLu::SetFactoryManager SFM(Teuchos::rcpFromRef(level), Teuchos::rcpFromRef(M));
3557
3558 // request BGS smoother (and all dependencies) on level
3559 level.Request("A", rAFact.get());
3560 level.Request("Smoother", smootherFact.get());
3561 level.Request("PreSmoother", smootherFact.get());
3562 level.Request("PostSmoother", smootherFact.get());
3563
3564 smootherFact->Build(level);
3565
3566 RCP<SmootherBase> bsSmoother = level.Get<RCP<SmootherBase> >("PreSmoother", smootherFact.get());
3567
3568 RCP<Matrix> reorderedA = level.Get<RCP<Matrix> >("A", rAFact.get());
3569 RCP<BlockedCrsMatrix> reorderedbA = Teuchos::rcp_dynamic_cast<BlockedCrsMatrix>(reorderedA);
3570
3571 TEST_EQUALITY(reorderedbA->Rows(), 2);
3572 TEST_EQUALITY(reorderedbA->Cols(), 2);
3573
3574 RCP<MultiVector> X = MultiVectorFactory::Build(reorderedA->getDomainMap(), 1);
3575 RCP<MultiVector> RHS = MultiVectorFactory::Build(reorderedA->getRangeMap(), 1);
3576
3577 // apply simple smoother
3578 RHS->putScalar((SC)1.0);
3579 X->putScalar((SC)0.0);
3580
3581 // solve system
3582 bsSmoother->Apply(*X, *RHS, true); // zero initial guess
3583
3584 RCP<BlockedMultiVector> bX = Teuchos::rcp_dynamic_cast<BlockedMultiVector>(X);
3585 TEST_EQUALITY(bX.is_null(), false);
3586 RCP<MultiVector> XX = bX->Merge();
3587 Teuchos::ArrayRCP<const Scalar> xdata = XX->getData(0);
3588 bool bCheck = true;
3589 for (size_t i = 0; i < XX->getLocalLength(); i++) {
3590 if (i < 10) {
3591 if (xdata[i] != (SC)(1.0 / 3.0)) bCheck = false;
3592 }
3593 if (i >= 10 && i < 15) {
3594 if (xdata[i] != (SC)0.5) bCheck = false;
3595 }
3596 if (i >= 15 && i < 20) {
3597 if (xdata[i] != (SC)1.0) bCheck = false;
3598 }
3599 }
3600 TEST_EQUALITY(bCheck, true);
3601
3602 // Random X
3603 X->setSeed(846930886);
3604 X->randomize();
3605
3606 typedef typename Teuchos::ScalarTraits<Scalar>::magnitudeType magnitude_type;
3607
3608 // Normalize X
3609 Array<magnitude_type> norms(1);
3610 X->norm2(norms);
3611 X->scale(1 / norms[0]);
3612
3613 // Compute RHS corresponding to X
3614 reorderedbA->apply(*X, *RHS, Teuchos::NO_TRANS, (SC)1.0, (SC)0.0);
3615
3616 // Reset X to 0
3617 X->putScalar((SC)0.0);
3618
3619 RHS->norm2(norms);
3620 out << "||RHS|| = " << std::setiosflags(std::ios::fixed) << std::setprecision(10) << norms[0] << std::endl;
3621
3622 out << "solve with zero initial guess" << std::endl;
3623 Teuchos::Array<magnitude_type> initialNorms(1);
3624 X->norm2(initialNorms);
3625 out << " ||X_initial|| = " << std::setiosflags(std::ios::fixed) << std::setprecision(10) << initialNorms[0] << std::endl;
3626
3627 bsSmoother->Apply(*X, *RHS, true); // zero initial guess
3628
3629 Teuchos::Array<magnitude_type> finalNorms(1);
3630 X->norm2(finalNorms);
3631 Teuchos::Array<magnitude_type> residualNorm1 = Utilities::ResidualNorm(*reorderedbA, *X, *RHS);
3632 out << " ||Residual_final|| = " << std::setiosflags(std::ios::fixed) << std::setprecision(20) << residualNorm1[0] << std::endl;
3633 out << " ||X_final|| = " << std::setiosflags(std::ios::fixed) << std::setprecision(10) << finalNorms[0] << std::endl;
3634
3635 magnitude_type tol = 50. * Teuchos::ScalarTraits<Scalar>::eps();
3636
3637 TEUCHOS_TEST_COMPARE(residualNorm1[0], <, tol, out, success);
3638 TEUCHOS_TEST_COMPARE(finalNorms[0] - Teuchos::ScalarTraits<Scalar>::magnitude(Teuchos::ScalarTraits<Scalar>::one()), <, tol, out, success);
3639 } // end useTpetra
3640 }
3641
3642 TEUCHOS_UNIT_TEST_TEMPLATE_4_DECL(BlockedSmoother, NestedI2I10II_BS_Setup_Apply2, Scalar, LocalOrdinal, GlobalOrdinal, Node) {
3643 #include <MueLu_UseShortNames.hpp>
3644 MUELU_TESTING_SET_OSTREAM;
3645 MUELU_TESTING_LIMIT_SCOPE(Scalar, GlobalOrdinal, Node);
3646 // TODO test only Tpetra because of Ifpack2 smoother!
3647 MUELU_TEST_ONLY_FOR(Xpetra::UseTpetra) {
3648 RCP<const Teuchos::Comm<int> > comm = Parameters::getDefaultComm();
3649 Xpetra::UnderlyingLib lib = MueLuTests::TestHelpers::Parameters::getLib();
3650
3651 Teuchos::RCP<const BlockedCrsMatrix> bop = TestHelpers::TestFactory<Scalar, LocalOrdinal, GlobalOrdinal, Node>::CreateBlockDiagonalExampleMatrix(lib, 3, comm);
3652 Teuchos::RCP<const Matrix> Aconst = Teuchos::rcp_dynamic_cast<const Matrix>(bop);
3653 Teuchos::RCP<Matrix> A = Teuchos::rcp_const_cast<Matrix>(Aconst);
3654
3655 // I don't use the testApply infrastructure because it has no provision for an initial guess.
3656 Level level;
3657 TestHelpers::TestFactory<Scalar, LocalOrdinal, GlobalOrdinal, Node>::createSingleLevelHierarchy(level);
3658 level.Set("A", A);
3659
3660 // Test ReorderBlockAFactory
3661 Teuchos::RCP<ReorderBlockAFactory> rAFact = Teuchos::rcp(new ReorderBlockAFactory());
3662 rAFact->SetFactory("A", MueLu::NoFactory::getRCP());
3663 rAFact->SetParameter(std::string("Reorder Type"), Teuchos::ParameterEntry(std::string("[ 2 [ 1 0 ]]")));
3664
3665 //////////////////////////////////////////////////////////////////////
3666 // Smoothers
3667 RCP<BraessSarazinSmoother> smootherPrototype = rcp(new BraessSarazinSmoother());
3668 smootherPrototype->SetFactory("A", rAFact);
3669 smootherPrototype->SetParameter("Sweeps", Teuchos::ParameterEntry(Teuchos::as<LocalOrdinal>(1)));
3670 smootherPrototype->SetParameter("Damping factor", Teuchos::ParameterEntry(Teuchos::as<Scalar>(1.0)));
3671
3672 std::vector<RCP<SmootherFactory> > sF(1, Teuchos::null);
3673 std::vector<RCP<FactoryManager> > sM(1, Teuchos::null);
3674
3675 // correction
3676 // define SchurComplement Factory
3677 // SchurComp gets a RCP to AFact_ which has to be the 2x2 blocked operator
3678 // It stores the resulting SchurComplement operator as "A" generated by the SchurComplementFactory
3679 // Instead of F^{-1} it uses the approximation \hat{F}^{-1} with \hat{F} = diag(F)
3680 RCP<InverseApproximationFactory> AinvFact = Teuchos::rcp(new InverseApproximationFactory());
3681 AinvFact->SetFactory("A", rAFact);
3682
3683 RCP<SchurComplementFactory> SFact = Teuchos::rcp(new SchurComplementFactory());
3684 SFact->SetParameter("omega", Teuchos::ParameterEntry(Teuchos::as<Scalar>(1.0))); // for Simple, omega is always 1.0 in the SchurComplement
3685 SFact->SetFactory("A", rAFact);
3686 SFact->SetFactory("Ainv", AinvFact);
3687
3688 RCP<BraessSarazinSmoother> smoProtoCorrect = Teuchos::rcp(new BraessSarazinSmoother());
3689 smoProtoCorrect->SetParameter("Sweeps", Teuchos::ParameterEntry(1));
3690 smoProtoCorrect->SetParameter("Damping factor", Teuchos::ParameterEntry(Teuchos::as<Scalar>(1.0)));
3691 smoProtoCorrect->SetFactory("A", SFact);
3692
3693 std::string strInfo = std::string("{ 1 }");
3694 Teuchos::RCP<SubBlockAFactory> ssA = rcp(new SubBlockAFactory());
3695 ssA->SetFactory("A", SFact);
3696 ssA->SetParameter("block row", Teuchos::ParameterEntry(1)); // local block indices relative to size of blocked operator
3697 ssA->SetParameter("block col", Teuchos::ParameterEntry(1));
3698 ssA->SetParameter("Range map: Striding info", Teuchos::ParameterEntry(strInfo));
3699 ssA->SetParameter("Domain map: Striding info", Teuchos::ParameterEntry(strInfo));
3700 RCP<SmootherPrototype> ssP = rcp(new Ifpack2Smoother(std::string("RELAXATION"), Teuchos::ParameterList(), 0));
3701 ssP->SetFactory("A", ssA);
3702 Teuchos::RCP<SmootherFactory> ssF = Teuchos::rcp(new SmootherFactory(ssP));
3703 Teuchos::RCP<FactoryManager> ssM = Teuchos::rcp(new FactoryManager());
3704 ssM->SetFactory("A", ssA);
3705 ssM->SetFactory("Smoother", ssF);
3706 ssM->SetIgnoreUserData(true);
3707 smoProtoCorrect->AddFactoryManager(ssM, 0);
3708
3709 sF[0] = rcp(new SmootherFactory(smoProtoCorrect));
3710
3711 sM[0] = rcp(new FactoryManager());
3712 sM[0]->SetFactory("A", SFact);
3713 sM[0]->SetFactory("Smoother", sF[0]);
3714 sM[0]->SetIgnoreUserData(true);
3715
3716 smootherPrototype->AddFactoryManager(sM[0], 0);
3717
3718 RCP<SmootherFactory> smootherFact = rcp(new SmootherFactory(smootherPrototype));
3719
3720 // main factory manager
3721 FactoryManager M;
3722 M.SetFactory("Smoother", smootherFact);
3723 M.SetFactory("A", rAFact);
3724
3725 MueLu::SetFactoryManager SFM(Teuchos::rcpFromRef(level), Teuchos::rcpFromRef(M));
3726
3727 // request BGS smoother (and all dependencies) on level
3728 level.Request("A", rAFact.get());
3729 level.Request("Smoother", smootherFact.get());
3730 level.Request("PreSmoother", smootherFact.get());
3731 level.Request("PostSmoother", smootherFact.get());
3732
3733 smootherFact->Build(level);
3734
3735 RCP<SmootherBase> bsSmoother = level.Get<RCP<SmootherBase> >("PreSmoother", smootherFact.get());
3736
3737 RCP<Matrix> reorderedA = level.Get<RCP<Matrix> >("A", rAFact.get());
3738 RCP<ReorderedBlockedCrsMatrix> reorderedbA = Teuchos::rcp_dynamic_cast<ReorderedBlockedCrsMatrix>(reorderedA);
3739
3740 TEST_EQUALITY(reorderedbA->Rows(), 2);
3741 TEST_EQUALITY(reorderedbA->Cols(), 2);
3742
3743 RCP<BlockedMultiVector> bX = Teuchos::rcp(new BlockedMultiVector(bop->getBlockedDomainMap(), 1, true));
3744 RCP<BlockedMultiVector> bRHS = Teuchos::rcp(new BlockedMultiVector(bop->getBlockedRangeMap(), 1, true));
3745
3746 RCP<MultiVector> X = bX->Merge();
3747 RCP<MultiVector> RHS = bRHS->Merge();
3748
3749 // apply simple smoother
3750 RHS->putScalar((SC)1.0);
3751 X->putScalar((SC)0.0);
3752
3753 // solve system
3754 bsSmoother->Apply(*X, *RHS, true); // zero initial guess
3755
3756 Teuchos::ArrayRCP<const Scalar> xdata = X->getData(0);
3757 bool bCheck = true;
3758 for (size_t i = 0; i < X->getLocalLength(); i++) {
3759 if (i < 5) {
3760 if (xdata[i] != (SC)1.0) bCheck = false;
3761 }
3762 if (i >= 5 && i < 10) {
3763 if (xdata[i] != (SC)0.5) bCheck = false;
3764 }
3765 if (i >= 10 && i < 20) {
3766 if (xdata[i] != (SC)(1.0 / 3.0)) bCheck = false;
3767 }
3768 }
3769 TEST_EQUALITY(bCheck, true);
3770
3771 // Random X
3772 {
3773 Teuchos::RCP<const Xpetra::BlockReorderManager> brm = Xpetra::blockedReorderFromString("[ 2 [0 1]]");
3774 RCP<BlockedMultiVector> test = Teuchos::rcp_dynamic_cast<BlockedMultiVector>(buildReorderedBlockedMultiVector(brm, bX));
3775 bX.swap(test);
3776 test = Teuchos::rcp_dynamic_cast<BlockedMultiVector>(buildReorderedBlockedMultiVector(brm, bRHS));
3777 bRHS.swap(test);
3778 }
3779
3780 bRHS->putScalar((SC)1.0);
3781 bX->setSeed(846930886);
3782 bX->randomize();
3783
3784 RHS = bRHS->Merge();
3785 X = bX->Merge();
3786
3787 typedef typename Teuchos::ScalarTraits<Scalar>::magnitudeType magnitude_type;
3788
3789 // Normalize X
3790 Array<magnitude_type> norms(1);
3791 X->norm2(norms);
3792 X->scale(1 / norms[0]);
3793
3794 // Compute RHS corresponding to X
3795 bop->apply(*X, *RHS, Teuchos::NO_TRANS, (SC)1.0, (SC)0.0);
3796
3797 // Reset X to 0
3798 X->putScalar((SC)0.0);
3799
3800 RHS->norm2(norms);
3801 out << "||RHS|| = " << std::setiosflags(std::ios::fixed) << std::setprecision(10) << norms[0] << std::endl;
3802
3803 out << "solve with zero initial guess" << std::endl;
3804 Teuchos::Array<magnitude_type> initialNorms(1);
3805 X->norm2(initialNorms);
3806 out << " ||X_initial|| = " << std::setiosflags(std::ios::fixed) << std::setprecision(10) << initialNorms[0] << std::endl;
3807
3808 bsSmoother->Apply(*X, *RHS, true); // zero initial guess
3809
3810 Teuchos::Array<magnitude_type> finalNorms(1);
3811 X->norm2(finalNorms);
3812 Teuchos::Array<magnitude_type> residualNorm1 = Utilities::ResidualNorm(*reorderedbA, *X, *RHS);
3813 out << " ||Residual_final|| = " << std::setiosflags(std::ios::fixed) << std::setprecision(20) << residualNorm1[0] << std::endl;
3814 out << " ||X_final|| = " << std::setiosflags(std::ios::fixed) << std::setprecision(10) << finalNorms[0] << std::endl;
3815
3816 magnitude_type tol = 50. * Teuchos::ScalarTraits<Scalar>::eps();
3817
3818 TEUCHOS_TEST_COMPARE(residualNorm1[0], <, tol, out, success);
3819 TEUCHOS_TEST_COMPARE(finalNorms[0] - Teuchos::ScalarTraits<Scalar>::magnitude(Teuchos::ScalarTraits<Scalar>::one()), <, tol, out, success);
3820 } // end useTpetra
3821 }
3822
3823 TEUCHOS_UNIT_TEST_TEMPLATE_4_DECL(BlockedSmoother, NestedII02I1I_Thyra_BS_Setup_Apply, Scalar, LocalOrdinal, GlobalOrdinal, Node) {
3824 #include <MueLu_UseShortNames.hpp>
3825 MUELU_TESTING_SET_OSTREAM;
3826 MUELU_TESTING_LIMIT_SCOPE(Scalar, GlobalOrdinal, Node);
3827 // TODO test only Tpetra because of Ifpack2 smoother!
3828 MUELU_TEST_ONLY_FOR(Xpetra::UseTpetra) {
3829 RCP<const Teuchos::Comm<int> > comm = Parameters::getDefaultComm();
3830 Xpetra::UnderlyingLib lib = MueLuTests::TestHelpers::Parameters::getLib();
3831
3832 Teuchos::RCP<const BlockedCrsMatrix> bop = TestHelpers::TestFactory<Scalar, LocalOrdinal, GlobalOrdinal, Node>::CreateBlockDiagonalExampleMatrixThyra(lib, 3, comm);
3833 Teuchos::RCP<const Matrix> Aconst = Teuchos::rcp_dynamic_cast<const Matrix>(bop);
3834 Teuchos::RCP<Matrix> A = Teuchos::rcp_const_cast<Matrix>(Aconst);
3835
3836 // I don't use the testApply infrastructure because it has no provision for an initial guess.
3837 Level level;
3838 TestHelpers::TestFactory<Scalar, LocalOrdinal, GlobalOrdinal, Node>::createSingleLevelHierarchy(level);
3839 level.Set("A", A);
3840
3841 // Test ReorderBlockAFactory
3842 Teuchos::RCP<ReorderBlockAFactory> rAFact = Teuchos::rcp(new ReorderBlockAFactory());
3843 rAFact->SetFactory("A", MueLu::NoFactory::getRCP());
3844 rAFact->SetParameter(std::string("Reorder Type"), Teuchos::ParameterEntry(std::string("[ [0 2] 1]")));
3845
3846 //////////////////////////////////////////////////////////////////////
3847 // Smoothers
3848 RCP<BraessSarazinSmoother> smootherPrototype = rcp(new BraessSarazinSmoother());
3849 smootherPrototype->SetFactory("A", rAFact);
3850 smootherPrototype->SetParameter("Sweeps", Teuchos::ParameterEntry(Teuchos::as<LocalOrdinal>(1)));
3851 smootherPrototype->SetParameter("Damping factor", Teuchos::ParameterEntry(Teuchos::as<Scalar>(1.0)));
3852
3853 std::vector<RCP<SmootherFactory> > sF(1, Teuchos::null);
3854 std::vector<RCP<FactoryManager> > sM(1, Teuchos::null);
3855
3856 // correction
3857 // define SchurComplement Factory
3858 // SchurComp gets a RCP to AFact_ which has to be the 2x2 blocked operator
3859 // It stores the resulting SchurComplement operator as "A" generated by the SchurComplementFactory
3860 // Instead of F^{-1} it uses the approximation \hat{F}^{-1} with \hat{F} = diag(F)
3861 RCP<InverseApproximationFactory> AinvFact = Teuchos::rcp(new InverseApproximationFactory());
3862 AinvFact->SetFactory("A", rAFact);
3863
3864 RCP<SchurComplementFactory> SFact = Teuchos::rcp(new SchurComplementFactory());
3865 SFact->SetParameter("omega", Teuchos::ParameterEntry(Teuchos::as<Scalar>(1.0))); // for Simple, omega is always 1.0 in the SchurComplement
3866 SFact->SetFactory("A", rAFact);
3867 SFact->SetFactory("Ainv", AinvFact);
3868
3869 RCP<SmootherPrototype> smoProtoCorrect = rcp(new Ifpack2Smoother(std::string("RELAXATION"), Teuchos::ParameterList(), 0));
3870 smoProtoCorrect->SetFactory("A", SFact);
3871 sF[0] = rcp(new SmootherFactory(smoProtoCorrect));
3872
3873 sM[0] = rcp(new FactoryManager());
3874 sM[0]->SetFactory("A", SFact);
3875 sM[0]->SetFactory("Smoother", sF[0]);
3876 sM[0]->SetIgnoreUserData(true);
3877
3878 smootherPrototype->AddFactoryManager(sM[0], 0);
3879
3880 RCP<SmootherFactory> smootherFact = rcp(new SmootherFactory(smootherPrototype));
3881
3882 // main factory manager
3883 FactoryManager M;
3884 M.SetFactory("Smoother", smootherFact);
3885 M.SetFactory("A", rAFact);
3886
3887 MueLu::SetFactoryManager SFM(Teuchos::rcpFromRef(level), Teuchos::rcpFromRef(M));
3888
3889 // request BGS smoother (and all dependencies) on level
3890 level.Request("A", rAFact.get());
3891 level.Request("Smoother", smootherFact.get());
3892 level.Request("PreSmoother", smootherFact.get());
3893 level.Request("PostSmoother", smootherFact.get());
3894
3895 // smootherFact->DeclareInput(level);
3896 smootherFact->Build(level);
3897
3898 level.print(std::cout, Teuchos::VERB_EXTREME);
3899
3900 RCP<SmootherBase> simpleSmoother = level.Get<RCP<SmootherBase> >("PreSmoother", smootherFact.get());
3901
3902 RCP<Matrix> reorderedA = level.Get<RCP<Matrix> >("A", rAFact.get());
3903 RCP<BlockedCrsMatrix> reorderedbA = Teuchos::rcp_dynamic_cast<BlockedCrsMatrix>(reorderedA);
3904
3905 TEST_EQUALITY(reorderedbA->Rows(), 2);
3906 TEST_EQUALITY(reorderedbA->Cols(), 2);
3907
3908 RCP<MultiVector> X = MultiVectorFactory::Build(reorderedA->getDomainMap(), 1);
3909 RCP<MultiVector> RHS = MultiVectorFactory::Build(reorderedA->getRangeMap(), 1);
3910
3911 // apply simple smoother
3912 RHS->putScalar((SC)1.0);
3913 X->putScalar((SC)0.0);
3914
3915 // solve system
3916 simpleSmoother->Apply(*X, *RHS, true); // zero initial guess
3917 RCP<BlockedMultiVector> bX = Teuchos::rcp_dynamic_cast<BlockedMultiVector>(X);
3918 TEST_EQUALITY(bX.is_null(), false);
3919 RCP<MultiVector> XX = bX->Merge();
3920 Teuchos::ArrayRCP<const Scalar> xdata = XX->getData(0);
3921 bool bCheck = true;
3922 for (size_t i = 0; i < XX->getLocalLength(); i++) {
3923 if (i < 5) {
3924 if (xdata[i] != (SC)1.0) bCheck = false;
3925 }
3926 if (i >= 5 && i < 15) {
3927 if (xdata[i] != (SC)(1.0 / 3.0)) bCheck = false;
3928 }
3929 if (i >= 15 && i < 20) {
3930 if (xdata[i] != (SC)0.5) bCheck = false;
3931 }
3932 }
3933 TEST_EQUALITY(bCheck, true);
3934
3935 // Random X
3936 X->setSeed(846930886);
3937 X->randomize();
3938
3939 typedef typename Teuchos::ScalarTraits<Scalar>::magnitudeType magnitude_type;
3940
3941 // Normalize X
3942 Array<magnitude_type> norms(1);
3943 X->norm2(norms);
3944 X->scale(1 / norms[0]);
3945
3946 // Compute RHS corresponding to X
3947 reorderedbA->apply(*X, *RHS, Teuchos::NO_TRANS, (SC)1.0, (SC)0.0);
3948
3949 // Reset X to 0
3950 X->putScalar((SC)0.0);
3951
3952 RHS->norm2(norms);
3953 out << "||RHS|| = " << std::setiosflags(std::ios::fixed) << std::setprecision(10) << norms[0] << std::endl;
3954
3955 out << "solve with zero initial guess" << std::endl;
3956 Teuchos::Array<magnitude_type> initialNorms(1);
3957 X->norm2(initialNorms);
3958 out << " ||X_initial|| = " << std::setiosflags(std::ios::fixed) << std::setprecision(10) << initialNorms[0] << std::endl;
3959
3960 simpleSmoother->Apply(*X, *RHS, true); // zero initial guess
3961
3962 Teuchos::Array<magnitude_type> finalNorms(1);
3963 X->norm2(finalNorms);
3964 Teuchos::Array<magnitude_type> residualNorm1 = Utilities::ResidualNorm(*reorderedbA, *X, *RHS);
3965 out << " ||Residual_final|| = " << std::setiosflags(std::ios::fixed) << std::setprecision(20) << residualNorm1[0] << std::endl;
3966 out << " ||X_final|| = " << std::setiosflags(std::ios::fixed) << std::setprecision(10) << finalNorms[0] << std::endl;
3967
3968 magnitude_type tol = 50. * Teuchos::ScalarTraits<Scalar>::eps();
3969
3970 TEUCHOS_TEST_COMPARE(residualNorm1[0], <, tol, out, success);
3971 TEUCHOS_TEST_COMPARE(finalNorms[0] - Teuchos::ScalarTraits<Scalar>::magnitude(Teuchos::ScalarTraits<Scalar>::one()), <, tol, out, success);
3972 } // end useTpetra
3973 }
3974
3975 TEUCHOS_UNIT_TEST_TEMPLATE_4_DECL(BlockedSmoother, NestedI2I01II_Thyra_BS_Setup_Apply, Scalar, LocalOrdinal, GlobalOrdinal, Node) {
3976 #include <MueLu_UseShortNames.hpp>
3977 MUELU_TESTING_SET_OSTREAM;
3978 MUELU_TESTING_LIMIT_SCOPE(Scalar, GlobalOrdinal, Node);
3979 // TODO test only Tpetra because of Ifpack2 smoother!
3980 MUELU_TEST_ONLY_FOR(Xpetra::UseTpetra) {
3981 RCP<const Teuchos::Comm<int> > comm = Parameters::getDefaultComm();
3982 Xpetra::UnderlyingLib lib = MueLuTests::TestHelpers::Parameters::getLib();
3983
3984 Teuchos::RCP<const BlockedCrsMatrix> bop = TestHelpers::TestFactory<Scalar, LocalOrdinal, GlobalOrdinal, Node>::CreateBlockDiagonalExampleMatrixThyra(lib, 3, comm);
3985 Teuchos::RCP<const Matrix> Aconst = Teuchos::rcp_dynamic_cast<const Matrix>(bop);
3986 Teuchos::RCP<Matrix> A = Teuchos::rcp_const_cast<Matrix>(Aconst);
3987
3988 // I don't use the testApply infrastructure because it has no provision for an initial guess.
3989 Level level;
3990 TestHelpers::TestFactory<Scalar, LocalOrdinal, GlobalOrdinal, Node>::createSingleLevelHierarchy(level);
3991 level.Set("A", A);
3992
3993 // Test ReorderBlockAFactory
3994 Teuchos::RCP<ReorderBlockAFactory> rAFact = Teuchos::rcp(new ReorderBlockAFactory());
3995 rAFact->SetFactory("A", MueLu::NoFactory::getRCP());
3996 rAFact->SetParameter(std::string("Reorder Type"), Teuchos::ParameterEntry(std::string("[ 2 [ 0 1 ]]")));
3997
3998 //////////////////////////////////////////////////////////////////////
3999 // Smoothers
4000 RCP<BraessSarazinSmoother> smootherPrototype = rcp(new BraessSarazinSmoother());
4001 smootherPrototype->SetFactory("A", rAFact);
4002 smootherPrototype->SetParameter("Sweeps", Teuchos::ParameterEntry(Teuchos::as<LocalOrdinal>(1)));
4003 smootherPrototype->SetParameter("Damping factor", Teuchos::ParameterEntry(Teuchos::as<Scalar>(1.0)));
4004
4005 std::vector<RCP<SmootherFactory> > sF(1, Teuchos::null);
4006 std::vector<RCP<FactoryManager> > sM(1, Teuchos::null);
4007
4008 // correction
4009 // define SchurComplement Factory
4010 // SchurComp gets a RCP to AFact_ which has to be the 2x2 blocked operator
4011 // It stores the resulting SchurComplement operator as "A" generated by the SchurComplementFactory
4012 // Instead of F^{-1} it uses the approximation \hat{F}^{-1} with \hat{F} = diag(F)
4013 RCP<InverseApproximationFactory> AinvFact = Teuchos::rcp(new InverseApproximationFactory());
4014 AinvFact->SetFactory("A", rAFact);
4015
4016 RCP<SchurComplementFactory> SFact = Teuchos::rcp(new SchurComplementFactory());
4017 SFact->SetParameter("omega", Teuchos::ParameterEntry(Teuchos::as<Scalar>(1.0))); // for Simple, omega is always 1.0 in the SchurComplement
4018 SFact->SetFactory("A", rAFact);
4019 SFact->SetFactory("Ainv", AinvFact);
4020
4021 RCP<SimpleSmoother> smoProtoCorrect = Teuchos::rcp(new SimpleSmoother());
4022 smoProtoCorrect->SetParameter("Sweeps", Teuchos::ParameterEntry(1));
4023 smoProtoCorrect->SetParameter("Damping factor", Teuchos::ParameterEntry(Teuchos::as<Scalar>(1.0)));
4024 smoProtoCorrect->SetParameter("UseSIMPLEC", Teuchos::ParameterEntry(false));
4025 smoProtoCorrect->SetFactory("A", SFact);
4026
4027 std::string strInfo = std::string("{ 1 }");
4028 for (int l = 0; l < 2; l++) {
4029 Teuchos::RCP<SubBlockAFactory> ssA = rcp(new SubBlockAFactory());
4030 ssA->SetFactory("A", SFact);
4031 ssA->SetParameter("block row", Teuchos::ParameterEntry(l)); // local block indices relative to size of blocked operator
4032 ssA->SetParameter("block col", Teuchos::ParameterEntry(l));
4033 ssA->SetParameter("Range map: Striding info", Teuchos::ParameterEntry(strInfo));
4034 ssA->SetParameter("Domain map: Striding info", Teuchos::ParameterEntry(strInfo));
4035 RCP<SmootherPrototype> ssP = rcp(new Ifpack2Smoother(std::string("RELAXATION"), Teuchos::ParameterList(), 0));
4036 ssP->SetFactory("A", ssA);
4037 Teuchos::RCP<SmootherFactory> ssF = Teuchos::rcp(new SmootherFactory(ssP));
4038 Teuchos::RCP<FactoryManager> ssM = Teuchos::rcp(new FactoryManager());
4039 ssM->SetFactory("A", ssA);
4040 ssM->SetFactory("Smoother", ssF);
4041 ssM->SetIgnoreUserData(true);
4042 if (l == 0)
4043 smoProtoCorrect->SetVelocityPredictionFactoryManager(ssM);
4044 else
4045 smoProtoCorrect->SetSchurCompFactoryManager(ssM);
4046 }
4047
4048 sF[0] = rcp(new SmootherFactory(smoProtoCorrect));
4049
4050 sM[0] = rcp(new FactoryManager());
4051 sM[0]->SetFactory("A", SFact);
4052 sM[0]->SetFactory("Smoother", sF[0]);
4053 sM[0]->SetIgnoreUserData(true);
4054
4055 smootherPrototype->AddFactoryManager(sM[0], 0);
4056
4057 RCP<SmootherFactory> smootherFact = rcp(new SmootherFactory(smootherPrototype));
4058
4059 // main factory manager
4060 FactoryManager M;
4061 M.SetFactory("Smoother", smootherFact);
4062 M.SetFactory("A", rAFact);
4063
4064 MueLu::SetFactoryManager SFM(Teuchos::rcpFromRef(level), Teuchos::rcpFromRef(M));
4065
4066 // request BGS smoother (and all dependencies) on level
4067 level.Request("A", rAFact.get());
4068 level.Request("Smoother", smootherFact.get());
4069 level.Request("PreSmoother", smootherFact.get());
4070 level.Request("PostSmoother", smootherFact.get());
4071
4072 smootherFact->Build(level);
4073
4074 RCP<SmootherBase> bsSmoother = level.Get<RCP<SmootherBase> >("PreSmoother", smootherFact.get());
4075
4076 RCP<Matrix> reorderedA = level.Get<RCP<Matrix> >("A", rAFact.get());
4077 RCP<BlockedCrsMatrix> reorderedbA = Teuchos::rcp_dynamic_cast<BlockedCrsMatrix>(reorderedA);
4078
4079 TEST_EQUALITY(reorderedbA->Rows(), 2);
4080 TEST_EQUALITY(reorderedbA->Cols(), 2);
4081
4082 RCP<MultiVector> X = MultiVectorFactory::Build(reorderedA->getDomainMap(), 1);
4083 RCP<MultiVector> RHS = MultiVectorFactory::Build(reorderedA->getRangeMap(), 1);
4084
4085 // apply simple smoother
4086 RHS->putScalar((SC)1.0);
4087 X->putScalar((SC)0.0);
4088
4089 // solve system
4090 bsSmoother->Apply(*X, *RHS, true); // zero initial guess
4091 RCP<BlockedMultiVector> bX = Teuchos::rcp_dynamic_cast<BlockedMultiVector>(X);
4092 TEST_EQUALITY(bX.is_null(), false);
4093 RCP<MultiVector> XX = bX->Merge();
4094 Teuchos::ArrayRCP<const Scalar> xdata = XX->getData(0);
4095 bool bCheck = true;
4096 for (size_t i = 0; i < XX->getLocalLength(); i++) {
4097 if (i < 10) {
4098 if (xdata[i] != (SC)(1.0 / 3.0)) bCheck = false;
4099 }
4100 if (i >= 10 && i < 15) {
4101 if (xdata[i] != (SC)1.0) bCheck = false;
4102 }
4103 if (i >= 15 && i < 20) {
4104 if (xdata[i] != (SC)0.5) bCheck = false;
4105 }
4106 }
4107 TEST_EQUALITY(bCheck, true);
4108
4109 // Random X
4110 X->setSeed(846930886);
4111 X->randomize();
4112
4113 typedef typename Teuchos::ScalarTraits<Scalar>::magnitudeType magnitude_type;
4114
4115 // Normalize X
4116 Array<magnitude_type> norms(1);
4117 X->norm2(norms);
4118 X->scale(1 / norms[0]);
4119
4120 // Compute RHS corresponding to X
4121 reorderedbA->apply(*X, *RHS, Teuchos::NO_TRANS, (SC)1.0, (SC)0.0);
4122
4123 // Reset X to 0
4124 X->putScalar((SC)0.0);
4125
4126 RHS->norm2(norms);
4127 out << "||RHS|| = " << std::setiosflags(std::ios::fixed) << std::setprecision(10) << norms[0] << std::endl;
4128
4129 out << "solve with zero initial guess" << std::endl;
4130 Teuchos::Array<magnitude_type> initialNorms(1);
4131 X->norm2(initialNorms);
4132 out << " ||X_initial|| = " << std::setiosflags(std::ios::fixed) << std::setprecision(10) << initialNorms[0] << std::endl;
4133
4134 bsSmoother->Apply(*X, *RHS, true); // zero initial guess
4135
4136 Teuchos::Array<magnitude_type> finalNorms(1);
4137 X->norm2(finalNorms);
4138 Teuchos::Array<magnitude_type> residualNorm1 = Utilities::ResidualNorm(*reorderedbA, *X, *RHS);
4139 out << " ||Residual_final|| = " << std::setiosflags(std::ios::fixed) << std::setprecision(20) << residualNorm1[0] << std::endl;
4140 out << " ||X_final|| = " << std::setiosflags(std::ios::fixed) << std::setprecision(10) << finalNorms[0] << std::endl;
4141
4142 magnitude_type tol = 50. * Teuchos::ScalarTraits<Scalar>::eps();
4143
4144 TEUCHOS_TEST_COMPARE(residualNorm1[0], <, tol, out, success);
4145 TEUCHOS_TEST_COMPARE(finalNorms[0] - Teuchos::ScalarTraits<Scalar>::magnitude(Teuchos::ScalarTraits<Scalar>::one()), <, tol, out, success);
4146 } // end useTpetra
4147 }
4148
4149 TEUCHOS_UNIT_TEST_TEMPLATE_4_DECL(BlockedSmoother, NestedI2I10II_Thyra_BS_Setup_Apply, Scalar, LocalOrdinal, GlobalOrdinal, Node) {
4150 #include <MueLu_UseShortNames.hpp>
4151 MUELU_TESTING_SET_OSTREAM;
4152 MUELU_TESTING_LIMIT_SCOPE(Scalar, GlobalOrdinal, Node);
4153 // TODO test only Tpetra because of Ifpack2 smoother!
4154 MUELU_TEST_ONLY_FOR(Xpetra::UseTpetra) {
4155 RCP<const Teuchos::Comm<int> > comm = Parameters::getDefaultComm();
4156 Xpetra::UnderlyingLib lib = MueLuTests::TestHelpers::Parameters::getLib();
4157
4158 Teuchos::RCP<const BlockedCrsMatrix> bop = TestHelpers::TestFactory<Scalar, LocalOrdinal, GlobalOrdinal, Node>::CreateBlockDiagonalExampleMatrixThyra(lib, 3, comm);
4159 Teuchos::RCP<const Matrix> Aconst = Teuchos::rcp_dynamic_cast<const Matrix>(bop);
4160 Teuchos::RCP<Matrix> A = Teuchos::rcp_const_cast<Matrix>(Aconst);
4161
4162 // I don't use the testApply infrastructure because it has no provision for an initial guess.
4163 Level level;
4164 TestHelpers::TestFactory<Scalar, LocalOrdinal, GlobalOrdinal, Node>::createSingleLevelHierarchy(level);
4165 level.Set("A", A);
4166
4167 // Test ReorderBlockAFactory
4168 Teuchos::RCP<ReorderBlockAFactory> rAFact = Teuchos::rcp(new ReorderBlockAFactory());
4169 rAFact->SetFactory("A", MueLu::NoFactory::getRCP());
4170 rAFact->SetParameter(std::string("Reorder Type"), Teuchos::ParameterEntry(std::string("[ 2 [ 1 0 ]]")));
4171
4172 //////////////////////////////////////////////////////////////////////
4173 // Smoothers
4174 RCP<BraessSarazinSmoother> smootherPrototype = rcp(new BraessSarazinSmoother());
4175 smootherPrototype->SetFactory("A", rAFact);
4176 smootherPrototype->SetParameter("Sweeps", Teuchos::ParameterEntry(Teuchos::as<LocalOrdinal>(1)));
4177 smootherPrototype->SetParameter("Damping factor", Teuchos::ParameterEntry(Teuchos::as<Scalar>(1.0)));
4178
4179 std::vector<RCP<SmootherFactory> > sF(1, Teuchos::null);
4180 std::vector<RCP<FactoryManager> > sM(1, Teuchos::null);
4181
4182 // correction
4183 // define SchurComplement Factory
4184 // SchurComp gets a RCP to AFact_ which has to be the 2x2 blocked operator
4185 // It stores the resulting SchurComplement operator as "A" generated by the SchurComplementFactory
4186 // Instead of F^{-1} it uses the approximation \hat{F}^{-1} with \hat{F} = diag(F)
4187 RCP<InverseApproximationFactory> AinvFact = Teuchos::rcp(new InverseApproximationFactory());
4188 AinvFact->SetFactory("A", rAFact);
4189
4190 RCP<SchurComplementFactory> SFact = Teuchos::rcp(new SchurComplementFactory());
4191 SFact->SetParameter("omega", Teuchos::ParameterEntry(Teuchos::as<Scalar>(1.0))); // for Simple, omega is always 1.0 in the SchurComplement
4192 SFact->SetFactory("A", rAFact);
4193 SFact->SetFactory("Ainv", AinvFact);
4194
4195 RCP<BraessSarazinSmoother> smoProtoCorrect = Teuchos::rcp(new BraessSarazinSmoother());
4196 smoProtoCorrect->SetParameter("Sweeps", Teuchos::ParameterEntry(1));
4197 smoProtoCorrect->SetParameter("Damping factor", Teuchos::ParameterEntry(Teuchos::as<Scalar>(1.0)));
4198 smoProtoCorrect->SetFactory("A", SFact);
4199
4200 std::string strInfo = std::string("{ 1 }");
4201 Teuchos::RCP<SubBlockAFactory> ssA = rcp(new SubBlockAFactory());
4202 ssA->SetFactory("A", SFact);
4203 ssA->SetParameter("block row", Teuchos::ParameterEntry(1)); // local block indices relative to size of blocked operator
4204 ssA->SetParameter("block col", Teuchos::ParameterEntry(1));
4205 ssA->SetParameter("Range map: Striding info", Teuchos::ParameterEntry(strInfo));
4206 ssA->SetParameter("Domain map: Striding info", Teuchos::ParameterEntry(strInfo));
4207 RCP<SmootherPrototype> ssP = rcp(new Ifpack2Smoother(std::string("RELAXATION"), Teuchos::ParameterList(), 0));
4208 ssP->SetFactory("A", ssA);
4209 Teuchos::RCP<SmootherFactory> ssF = Teuchos::rcp(new SmootherFactory(ssP));
4210 Teuchos::RCP<FactoryManager> ssM = Teuchos::rcp(new FactoryManager());
4211 ssM->SetFactory("A", ssA);
4212 ssM->SetFactory("Smoother", ssF);
4213 ssM->SetIgnoreUserData(true);
4214 smoProtoCorrect->AddFactoryManager(ssM, 0);
4215
4216 sF[0] = rcp(new SmootherFactory(smoProtoCorrect));
4217
4218 sM[0] = rcp(new FactoryManager());
4219 sM[0]->SetFactory("A", SFact);
4220 sM[0]->SetFactory("Smoother", sF[0]);
4221 sM[0]->SetIgnoreUserData(true);
4222
4223 smootherPrototype->AddFactoryManager(sM[0], 0);
4224
4225 RCP<SmootherFactory> smootherFact = rcp(new SmootherFactory(smootherPrototype));
4226
4227 // main factory manager
4228 FactoryManager M;
4229 M.SetFactory("Smoother", smootherFact);
4230 M.SetFactory("A", rAFact);
4231
4232 MueLu::SetFactoryManager SFM(Teuchos::rcpFromRef(level), Teuchos::rcpFromRef(M));
4233
4234 // request BGS smoother (and all dependencies) on level
4235 level.Request("A", rAFact.get());
4236 level.Request("Smoother", smootherFact.get());
4237 level.Request("PreSmoother", smootherFact.get());
4238 level.Request("PostSmoother", smootherFact.get());
4239
4240 smootherFact->Build(level);
4241
4242 RCP<SmootherBase> bsSmoother = level.Get<RCP<SmootherBase> >("PreSmoother", smootherFact.get());
4243
4244 RCP<Matrix> reorderedA = level.Get<RCP<Matrix> >("A", rAFact.get());
4245 RCP<BlockedCrsMatrix> reorderedbA = Teuchos::rcp_dynamic_cast<BlockedCrsMatrix>(reorderedA);
4246
4247 TEST_EQUALITY(reorderedbA->Rows(), 2);
4248 TEST_EQUALITY(reorderedbA->Cols(), 2);
4249
4250 RCP<MultiVector> X = MultiVectorFactory::Build(reorderedA->getDomainMap(), 1);
4251 RCP<MultiVector> RHS = MultiVectorFactory::Build(reorderedA->getRangeMap(), 1);
4252
4253 // apply simple smoother
4254 RHS->putScalar((SC)1.0);
4255 X->putScalar((SC)0.0);
4256
4257 // solve system
4258 bsSmoother->Apply(*X, *RHS, true); // zero initial guess
4259 RCP<BlockedMultiVector> bX = Teuchos::rcp_dynamic_cast<BlockedMultiVector>(X);
4260 TEST_EQUALITY(bX.is_null(), false);
4261 RCP<MultiVector> XX = bX->Merge();
4262 Teuchos::ArrayRCP<const Scalar> xdata = XX->getData(0);
4263 bool bCheck = true;
4264 for (size_t i = 0; i < XX->getLocalLength(); i++) {
4265 if (i < 10) {
4266 if (xdata[i] != (SC)(1.0 / 3.0)) bCheck = false;
4267 }
4268 if (i >= 10 && i < 15) {
4269 if (xdata[i] != (SC)0.5) bCheck = false;
4270 }
4271 if (i >= 15 && i < 20) {
4272 if (xdata[i] != (SC)1.0) bCheck = false;
4273 }
4274 }
4275 TEST_EQUALITY(bCheck, true);
4276
4277 // Random X
4278 X->setSeed(846930886);
4279 X->randomize();
4280
4281 typedef typename Teuchos::ScalarTraits<Scalar>::magnitudeType magnitude_type;
4282
4283 // Normalize X
4284 Array<magnitude_type> norms(1);
4285 X->norm2(norms);
4286 X->scale(1 / norms[0]);
4287
4288 // Compute RHS corresponding to X
4289 reorderedbA->apply(*X, *RHS, Teuchos::NO_TRANS, (SC)1.0, (SC)0.0);
4290
4291 // Reset X to 0
4292 X->putScalar((SC)0.0);
4293
4294 RHS->norm2(norms);
4295 out << "||RHS|| = " << std::setiosflags(std::ios::fixed) << std::setprecision(10) << norms[0] << std::endl;
4296
4297 out << "solve with zero initial guess" << std::endl;
4298 Teuchos::Array<magnitude_type> initialNorms(1);
4299 X->norm2(initialNorms);
4300 out << " ||X_initial|| = " << std::setiosflags(std::ios::fixed) << std::setprecision(10) << initialNorms[0] << std::endl;
4301
4302 bsSmoother->Apply(*X, *RHS, true); // zero initial guess
4303
4304 Teuchos::Array<magnitude_type> finalNorms(1);
4305 X->norm2(finalNorms);
4306 Teuchos::Array<magnitude_type> residualNorm1 = Utilities::ResidualNorm(*reorderedbA, *X, *RHS);
4307 out << " ||Residual_final|| = " << std::setiosflags(std::ios::fixed) << std::setprecision(20) << residualNorm1[0] << std::endl;
4308 out << " ||X_final|| = " << std::setiosflags(std::ios::fixed) << std::setprecision(10) << finalNorms[0] << std::endl;
4309
4310 magnitude_type tol = 50. * Teuchos::ScalarTraits<Scalar>::eps();
4311
4312 TEUCHOS_TEST_COMPARE(residualNorm1[0], <, tol, out, success);
4313 TEUCHOS_TEST_COMPARE(finalNorms[0] - Teuchos::ScalarTraits<Scalar>::magnitude(Teuchos::ScalarTraits<Scalar>::one()), <, tol, out, success);
4314 } // end useTpetra
4315 }
4316
4317 TEUCHOS_UNIT_TEST_TEMPLATE_4_DECL(BlockedSmoother, NestedII01I2I_Thyra_BS_Setup_Apply2, Scalar, LocalOrdinal, GlobalOrdinal, Node) {
4318 #include <MueLu_UseShortNames.hpp>
4319 MUELU_TESTING_SET_OSTREAM;
4320 MUELU_TESTING_LIMIT_SCOPE(Scalar, GlobalOrdinal, Node);
4321 // TODO test only Tpetra because of Ifpack2 smoother!
4322 MUELU_TEST_ONLY_FOR(Xpetra::UseTpetra) {
4323 RCP<const Teuchos::Comm<int> > comm = Parameters::getDefaultComm();
4324 Xpetra::UnderlyingLib lib = MueLuTests::TestHelpers::Parameters::getLib();
4325
4326 Teuchos::RCP<const BlockedCrsMatrix> bop = TestHelpers::TestFactory<Scalar, LocalOrdinal, GlobalOrdinal, Node>::CreateBlocked3x3MatrixThyra(*comm, lib);
4327 Teuchos::RCP<const Matrix> Aconst = Teuchos::rcp_dynamic_cast<const Matrix>(bop);
4328 Teuchos::RCP<Matrix> A = Teuchos::rcp_const_cast<Matrix>(Aconst);
4329
4330 // I don't use the testApply infrastructure because it has no provision for an initial guess.
4331 Level level;
4332 TestHelpers::TestFactory<Scalar, LocalOrdinal, GlobalOrdinal, Node>::createSingleLevelHierarchy(level);
4333 level.Set("A", A);
4334
4335 // Test ReorderBlockAFactory
4336 Teuchos::RCP<ReorderBlockAFactory> rAFact = Teuchos::rcp(new ReorderBlockAFactory());
4337 rAFact->SetFactory("A", MueLu::NoFactory::getRCP());
4338 rAFact->SetParameter(std::string("Reorder Type"), Teuchos::ParameterEntry(std::string("[ [0 1] 2]")));
4339
4340 //////////////////////////////////////////////////////////////////////
4341 // Smoothers
4342 RCP<BraessSarazinSmoother> smootherPrototype = rcp(new BraessSarazinSmoother());
4343 smootherPrototype->SetFactory("A", rAFact);
4344 smootherPrototype->SetParameter("Sweeps", Teuchos::ParameterEntry(Teuchos::as<LocalOrdinal>(30)));
4345 smootherPrototype->SetParameter("Damping factor", Teuchos::ParameterEntry(Teuchos::as<Scalar>(1.4)));
4346
4347 std::vector<RCP<SmootherFactory> > sF(1, Teuchos::null);
4348 std::vector<RCP<FactoryManager> > sM(1, Teuchos::null);
4349
4350 // correction
4351 // define SchurComplement Factory
4352 // SchurComp gets a RCP to AFact_ which has to be the 2x2 blocked operator
4353 // It stores the resulting SchurComplement operator as "A" generated by the SchurComplementFactory
4354 // Instead of F^{-1} it uses the approximation \hat{F}^{-1} with \hat{F} = diag(F)
4355 RCP<InverseApproximationFactory> AinvFact = Teuchos::rcp(new InverseApproximationFactory());
4356 AinvFact->SetFactory("A", rAFact);
4357
4358 RCP<SchurComplementFactory> SFact = Teuchos::rcp(new SchurComplementFactory());
4359 SFact->SetParameter("omega", Teuchos::ParameterEntry(Teuchos::as<Scalar>(1.0))); // for Simple, omega is always 1.0 in the SchurComplement
4360 SFact->SetFactory("A", rAFact);
4361 SFact->SetFactory("Ainv", AinvFact);
4362
4363 Teuchos::ParameterList paramList;
4364 paramList.set("relaxation: sweeps", Teuchos::as<LocalOrdinal>(30));
4365 paramList.set("relaxation: damping factor", Teuchos::as<Scalar>(0.9));
4366 RCP<SmootherPrototype> smoProtoCorrect = rcp(new Ifpack2Smoother(std::string("RELAXATION"), paramList, 0));
4367 smoProtoCorrect->SetFactory("A", SFact);
4368 sF[0] = rcp(new SmootherFactory(smoProtoCorrect));
4369
4370 sM[0] = rcp(new FactoryManager());
4371 sM[0]->SetFactory("A", SFact);
4372 sM[0]->SetFactory("Smoother", sF[0]);
4373 sM[0]->SetIgnoreUserData(true);
4374
4375 smootherPrototype->AddFactoryManager(sM[0], 0);
4376
4377 RCP<SmootherFactory> smootherFact = rcp(new SmootherFactory(smootherPrototype));
4378
4379 // main factory manager
4380 FactoryManager M;
4381 M.SetFactory("Smoother", smootherFact);
4382 M.SetFactory("A", rAFact);
4383
4384 MueLu::SetFactoryManager SFM(Teuchos::rcpFromRef(level), Teuchos::rcpFromRef(M));
4385
4386 // request BGS smoother (and all dependencies) on level
4387 level.Request("A", rAFact.get());
4388 level.Request("Smoother", smootherFact.get());
4389 level.Request("PreSmoother", smootherFact.get());
4390 level.Request("PostSmoother", smootherFact.get());
4391
4392 // smootherFact->DeclareInput(level);
4393 smootherFact->Build(level);
4394
4395 level.print(std::cout, Teuchos::VERB_EXTREME);
4396
4397 RCP<SmootherBase> bsSmoother = level.Get<RCP<SmootherBase> >("PreSmoother", smootherFact.get());
4398
4399 RCP<Matrix> reorderedA = level.Get<RCP<Matrix> >("A", rAFact.get());
4400 RCP<BlockedCrsMatrix> reorderedbA = Teuchos::rcp_dynamic_cast<BlockedCrsMatrix>(reorderedA);
4401
4402 RCP<MultiVector> X = MultiVectorFactory::Build(reorderedA->getDomainMap(), 1);
4403 RCP<MultiVector> RHS = MultiVectorFactory::Build(reorderedA->getRangeMap(), 1);
4404
4405 // Random X
4406 X->setSeed(846930886);
4407 X->randomize();
4408
4409 typedef typename Teuchos::ScalarTraits<Scalar>::magnitudeType magnitude_type;
4410
4411 // Normalize X
4412 Array<magnitude_type> norms(1);
4413 X->norm2(norms);
4414 X->scale(1 / norms[0]);
4415
4416 // Compute RHS corresponding to X
4417 reorderedA->apply(*X, *RHS, Teuchos::NO_TRANS, (SC)1.0, (SC)0.0);
4418
4419 // Reset X to 0
4420 X->putScalar((SC)0.0);
4421
4422 RHS->norm2(norms);
4423 out << "||RHS|| = " << std::setiosflags(std::ios::fixed) << std::setprecision(10) << norms[0] << std::endl;
4424
4425 out << "solve with zero initial guess" << std::endl;
4426 Teuchos::Array<magnitude_type> initialNorms(1);
4427 X->norm2(initialNorms);
4428 out << " ||X_initial|| = " << std::setiosflags(std::ios::fixed) << std::setprecision(10) << initialNorms[0] << std::endl;
4429
4430 bsSmoother->Apply(*X, *RHS, true); // zero initial guess
4431
4432 Teuchos::Array<magnitude_type> finalNorms(1);
4433 X->norm2(finalNorms);
4434 Teuchos::Array<magnitude_type> residualNorm1 = Utilities::ResidualNorm(*reorderedA, *X, *RHS);
4435 out << " ||Residual_final|| = " << std::setiosflags(std::ios::fixed) << std::setprecision(20) << residualNorm1[0] << std::endl;
4436 out << " ||X_final|| = " << std::setiosflags(std::ios::fixed) << std::setprecision(10) << finalNorms[0] << std::endl;
4437
4438 TEUCHOS_TEST_COMPARE(residualNorm1[0], <, 3e-3, out, success);
4439 TEUCHOS_TEST_COMPARE(residualNorm1[0], >, 1e-3, out, success);
4440 } // end UseTpetra
4441 }
4442
4443 TEUCHOS_UNIT_TEST_TEMPLATE_4_DECL(BlockedSmoother, NestedI0I21II_Thyra_BS_Setup_Apply3, Scalar, LocalOrdinal, GlobalOrdinal, Node) {
4444 #include <MueLu_UseShortNames.hpp>
4445 MUELU_TESTING_SET_OSTREAM;
4446 MUELU_TESTING_LIMIT_SCOPE(Scalar, GlobalOrdinal, Node);
4447 // TODO test only Tpetra because of Ifpack2 smoother!
4448 MUELU_TEST_ONLY_FOR(Xpetra::UseTpetra) {
4449 RCP<const Teuchos::Comm<int> > comm = Parameters::getDefaultComm();
4450 Xpetra::UnderlyingLib lib = MueLuTests::TestHelpers::Parameters::getLib();
4451
4452 Teuchos::RCP<const BlockedCrsMatrix> bop = TestHelpers::TestFactory<Scalar, LocalOrdinal, GlobalOrdinal, Node>::CreateBlocked3x3MatrixThyra(*comm, lib);
4453 Teuchos::RCP<const Matrix> Aconst = Teuchos::rcp_dynamic_cast<const Matrix>(bop);
4454 Teuchos::RCP<Matrix> A = Teuchos::rcp_const_cast<Matrix>(Aconst);
4455
4456 // I don't use the testApply infrastructure because it has no provision for an initial guess.
4457 Level level;
4458 TestHelpers::TestFactory<Scalar, LocalOrdinal, GlobalOrdinal, Node>::createSingleLevelHierarchy(level);
4459 level.Set("A", A);
4460
4461 // Test ReorderBlockAFactory
4462 Teuchos::RCP<ReorderBlockAFactory> rAFact = Teuchos::rcp(new ReorderBlockAFactory());
4463 rAFact->SetFactory("A", MueLu::NoFactory::getRCP());
4464 rAFact->SetParameter(std::string("Reorder Type"), Teuchos::ParameterEntry(std::string("[ 0 [ 2 1 ]]")));
4465
4466 //////////////////////////////////////////////////////////////////////
4467 // Smoothers
4468 RCP<BraessSarazinSmoother> smootherPrototype = rcp(new BraessSarazinSmoother());
4469 smootherPrototype->SetFactory("A", rAFact);
4470 smootherPrototype->SetParameter("Sweeps", Teuchos::ParameterEntry(Teuchos::as<LocalOrdinal>(15)));
4471 smootherPrototype->SetParameter("Damping factor", Teuchos::ParameterEntry(Teuchos::as<Scalar>(0.9)));
4472
4473 std::vector<RCP<SmootherFactory> > sF(1, Teuchos::null);
4474 std::vector<RCP<FactoryManager> > sM(1, Teuchos::null);
4475
4476 // correction
4477 // define SchurComplement Factory
4478 // SchurComp gets a RCP to AFact_ which has to be the 2x2 blocked operator
4479 // It stores the resulting SchurComplement operator as "A" generated by the SchurComplementFactory
4480 // Instead of F^{-1} it uses the approximation \hat{F}^{-1} with \hat{F} = diag(F)
4481 RCP<InverseApproximationFactory> AinvFact = Teuchos::rcp(new InverseApproximationFactory());
4482 AinvFact->SetFactory("A", rAFact);
4483
4484 RCP<SchurComplementFactory> SFact = Teuchos::rcp(new SchurComplementFactory());
4485 SFact->SetParameter("omega", Teuchos::ParameterEntry(Teuchos::as<Scalar>(1.0))); // for Simple, omega is always 1.0 in the SchurComplement
4486 SFact->SetFactory("A", rAFact);
4487 SFact->SetFactory("Ainv", AinvFact);
4488
4489 // create a 2x2 Simple for the prediction eq.
4490 std::string strInfo = std::string("{ 1 }");
4491 RCP<SimpleSmoother> smoProtoCorrect = Teuchos::rcp(new SimpleSmoother());
4492 smoProtoCorrect->SetParameter("Sweeps", Teuchos::ParameterEntry(1));
4493 smoProtoCorrect->SetParameter("Damping factor", Teuchos::ParameterEntry(Teuchos::as<Scalar>(1.0)));
4494 smoProtoCorrect->SetParameter("UseSIMPLEC", Teuchos::ParameterEntry(false));
4495 smoProtoCorrect->SetFactory("A", SFact);
4496
4497 for (int l = 0; l < 2; l++) {
4498 Teuchos::RCP<SubBlockAFactory> ssA = rcp(new SubBlockAFactory());
4499 ssA->SetFactory("A", SFact);
4500 ssA->SetParameter("block row", Teuchos::ParameterEntry(l)); // local block indices relative to size of blocked operator
4501 ssA->SetParameter("block col", Teuchos::ParameterEntry(l));
4502 ssA->SetParameter("Range map: Striding info", Teuchos::ParameterEntry(strInfo));
4503 ssA->SetParameter("Domain map: Striding info", Teuchos::ParameterEntry(strInfo));
4504 RCP<SmootherPrototype> ssP = rcp(new Ifpack2Smoother(std::string("RELAXATION"), Teuchos::ParameterList(), 0));
4505 ssP->SetFactory("A", ssA);
4506 Teuchos::RCP<SmootherFactory> ssF = Teuchos::rcp(new SmootherFactory(ssP));
4507 Teuchos::RCP<FactoryManager> ssM = Teuchos::rcp(new FactoryManager());
4508 ssM->SetFactory("A", ssA);
4509 ssM->SetFactory("Smoother", ssF);
4510 ssM->SetIgnoreUserData(true);
4511 if (l == 0)
4512 smoProtoCorrect->SetVelocityPredictionFactoryManager(ssM);
4513 else
4514 smoProtoCorrect->SetSchurCompFactoryManager(ssM);
4515 }
4516
4517 sF[0] = rcp(new SmootherFactory(smoProtoCorrect));
4518
4519 sM[0] = rcp(new FactoryManager());
4520 sM[0]->SetFactory("A", SFact);
4521 sM[0]->SetFactory("Smoother", sF[0]);
4522 sM[0]->SetIgnoreUserData(true);
4523
4524 smootherPrototype->AddFactoryManager(sM[0], 0);
4525
4526 RCP<SmootherFactory> smootherFact = rcp(new SmootherFactory(smootherPrototype));
4527
4528 // main factory manager
4529 FactoryManager M;
4530 M.SetFactory("Smoother", smootherFact);
4531 M.SetFactory("A", rAFact);
4532
4533 MueLu::SetFactoryManager SFM(Teuchos::rcpFromRef(level), Teuchos::rcpFromRef(M));
4534
4535 // request BGS smoother (and all dependencies) on level
4536 level.Request("A", rAFact.get());
4537 level.Request("Smoother", smootherFact.get());
4538 level.Request("PreSmoother", smootherFact.get());
4539 level.Request("PostSmoother", smootherFact.get());
4540
4541 smootherFact->Build(level);
4542
4543 level.print(std::cout, Teuchos::VERB_EXTREME);
4544
4545 RCP<SmootherBase> bsSmoother = level.Get<RCP<SmootherBase> >("PreSmoother", smootherFact.get());
4546
4547 RCP<Matrix> reorderedA = level.Get<RCP<Matrix> >("A", rAFact.get());
4548 RCP<BlockedCrsMatrix> reorderedbA = Teuchos::rcp_dynamic_cast<BlockedCrsMatrix>(reorderedA);
4549
4550 RCP<MultiVector> X = MultiVectorFactory::Build(reorderedA->getDomainMap(), 1);
4551 RCP<MultiVector> RHS = MultiVectorFactory::Build(reorderedA->getRangeMap(), 1);
4552
4553 // Random X
4554 X->setSeed(846930886);
4555 X->randomize();
4556
4557 typedef typename Teuchos::ScalarTraits<Scalar>::magnitudeType magnitude_type;
4558
4559 // Normalize X
4560 Array<magnitude_type> norms(1);
4561 X->norm2(norms);
4562 X->scale(1 / norms[0]);
4563
4564 // Compute RHS corresponding to X
4565 reorderedA->apply(*X, *RHS, Teuchos::NO_TRANS, (SC)1.0, (SC)0.0);
4566
4567 // Reset X to 0
4568 X->putScalar((SC)0.0);
4569
4570 RHS->norm2(norms);
4571 out << "||RHS|| = " << std::setiosflags(std::ios::fixed) << std::setprecision(10) << norms[0] << std::endl;
4572
4573 out << "solve with zero initial guess" << std::endl;
4574 Teuchos::Array<magnitude_type> initialNorms(1);
4575 X->norm2(initialNorms);
4576 out << " ||X_initial|| = " << std::setiosflags(std::ios::fixed) << std::setprecision(10) << initialNorms[0] << std::endl;
4577
4578 bsSmoother->Apply(*X, *RHS, true); // zero initial guess
4579
4580 Teuchos::Array<magnitude_type> finalNorms(1);
4581 X->norm2(finalNorms);
4582 Teuchos::Array<magnitude_type> residualNorm1 = Utilities::ResidualNorm(*reorderedA, *X, *RHS);
4583 out << " ||Residual_final|| = " << std::setiosflags(std::ios::fixed) << std::setprecision(20) << residualNorm1[0] << std::endl;
4584 out << " ||X_final|| = " << std::setiosflags(std::ios::fixed) << std::setprecision(10) << finalNorms[0] << std::endl;
4585
4586 TEUCHOS_TEST_COMPARE(residualNorm1[0], <, 44e-3, out, success);
4587 TEUCHOS_TEST_COMPARE(residualNorm1[0], >, 15e-3, out, success);
4588 } // end UseTpetra
4589 }
4590
4591 TEUCHOS_UNIT_TEST_TEMPLATE_4_DECL(BlockedSmoother, Uzawa_Setup_Apply, Scalar, LocalOrdinal, GlobalOrdinal, Node) {
4592 #include <MueLu_UseShortNames.hpp>
4593 MUELU_TESTING_SET_OSTREAM;
4594 MUELU_TESTING_LIMIT_SCOPE(Scalar, GlobalOrdinal, Node);
4595 MUELU_TEST_ONLY_FOR(Xpetra::UseTpetra) {
4596 RCP<const Teuchos::Comm<int> > comm = Parameters::getDefaultComm();
4597
4598 int noBlocks = 2;
4599 Teuchos::RCP<const BlockedCrsMatrix> bop = CreateBlockDiagonalExampleMatrix<Scalar, LocalOrdinal, GlobalOrdinal, Node, TpetraMap>(noBlocks, *comm);
4600 Teuchos::RCP<const Matrix> Aconst = Teuchos::rcp_dynamic_cast<const Matrix>(bop);
4601 Teuchos::RCP<Matrix> A = Teuchos::rcp_const_cast<Matrix>(Aconst);
4602
4603 // I don't use the testApply infrastructure because it has no provision for an initial guess.
4604 Level level;
4605 TestHelpers::TestFactory<Scalar, LocalOrdinal, GlobalOrdinal, Node>::createSingleLevelHierarchy(level);
4606 level.Set("A", A);
4607
4608 //////////////////////////////////////////////////////////////////////
4609 // Smoothers
4610 RCP<UzawaSmoother> smootherPrototype = rcp(new UzawaSmoother());
4611 smootherPrototype->SetParameter("Sweeps", Teuchos::ParameterEntry(1));
4612
4613 std::vector<RCP<SubBlockAFactory> > sA(noBlocks, Teuchos::null);
4614 std::vector<RCP<SmootherFactory> > sF(noBlocks, Teuchos::null);
4615 std::vector<RCP<FactoryManager> > sM(noBlocks, Teuchos::null);
4616 for (int k = 0; k < noBlocks; k++) {
4617 std::string strInfo = std::string("{ 1 }");
4618 sA[k] = rcp(new SubBlockAFactory());
4619 sA[k]->SetFactory("A", MueLu::NoFactory::getRCP());
4620 sA[k]->SetParameter("block row", Teuchos::ParameterEntry(k));
4621 sA[k]->SetParameter("block col", Teuchos::ParameterEntry(k));
4622 sA[k]->SetParameter("Range map: Striding info", Teuchos::ParameterEntry(strInfo));
4623 sA[k]->SetParameter("Domain map: Striding info", Teuchos::ParameterEntry(strInfo));
4624
4625 RCP<SmootherPrototype> smoProto = rcp(new Ifpack2Smoother(std::string("RELAXATION"), Teuchos::ParameterList(), 0));
4626 smoProto->SetFactory("A", sA[k]);
4627 sF[k] = rcp(new SmootherFactory(smoProto));
4628
4629 sM[k] = rcp(new FactoryManager());
4630 sM[k]->SetFactory("A", sA[k]);
4631 sM[k]->SetFactory("Smoother", sF[k]);
4632 sM[k]->SetIgnoreUserData(true);
4633
4634 smootherPrototype->AddFactoryManager(sM[k], k);
4635 }
4636
4637 RCP<SmootherFactory> smootherFact = rcp(new SmootherFactory(smootherPrototype));
4638
4639 // main factory manager
4640 FactoryManager M;
4641 M.SetFactory("Smoother", smootherFact);
4642
4643 MueLu::SetFactoryManager SFM(Teuchos::rcpFromRef(level), Teuchos::rcpFromRef(M));
4644
4645 // request BGS smoother (and all dependencies) on level
4646 level.Request("Smoother", smootherFact.get());
4647 level.Request("PreSmoother", smootherFact.get());
4648 level.Request("PostSmoother", smootherFact.get());
4649
4650 // smootherFact->DeclareInput(level);
4651 smootherFact->Build(level);
4652
4653 level.print(std::cout, Teuchos::VERB_EXTREME);
4654
4655 RCP<SmootherBase> uzSmoother = level.Get<RCP<SmootherBase> >("PreSmoother", smootherFact.get());
4656
4657 RCP<MultiVector> X = MultiVectorFactory::Build(A->getDomainMap(), 1);
4658 RCP<MultiVector> RHS = MultiVectorFactory::Build(A->getRangeMap(), 1);
4659
4660 // Random X
4661 X->setSeed(846930886);
4662 X->randomize();
4663
4664 typedef typename Teuchos::ScalarTraits<Scalar>::magnitudeType magnitude_type;
4665
4666 // Normalize X
4667 Array<magnitude_type> norms(1);
4668 X->norm2(norms);
4669 X->scale(1 / norms[0]);
4670
4671 // Compute RHS corresponding to X
4672 A->apply(*X, *RHS, Teuchos::NO_TRANS, (SC)1.0, (SC)0.0);
4673
4674 // Reset X to 0
4675 X->putScalar((SC)0.0);
4676
4677 RHS->norm2(norms);
4678 out << "||RHS|| = " << std::setiosflags(std::ios::fixed) << std::setprecision(10) << norms[0] << std::endl;
4679
4680 out << "solve with zero initial guess" << std::endl;
4681 Teuchos::Array<magnitude_type> initialNorms(1);
4682 X->norm2(initialNorms);
4683 out << " ||X_initial|| = " << std::setiosflags(std::ios::fixed) << std::setprecision(10) << initialNorms[0] << std::endl;
4684
4685 uzSmoother->Apply(*X, *RHS, true); // zero initial guess
4686
4687 Teuchos::Array<magnitude_type> finalNorms(1);
4688 X->norm2(finalNorms);
4689 Teuchos::Array<magnitude_type> residualNorm1 = Utilities::ResidualNorm(*A, *X, *RHS);
4690 out << " ||Residual_final|| = " << std::setiosflags(std::ios::fixed) << std::setprecision(20) << residualNorm1[0] << std::endl;
4691 out << " ||X_final|| = " << std::setiosflags(std::ios::fixed) << std::setprecision(10) << finalNorms[0] << std::endl;
4692
4693 magnitude_type tol = 50. * Teuchos::ScalarTraits<Scalar>::eps();
4694
4695 TEUCHOS_TEST_COMPARE(residualNorm1[0], <, tol, out, success);
4696 TEUCHOS_TEST_COMPARE(finalNorms[0] - Teuchos::ScalarTraits<Scalar>::magnitude(Teuchos::ScalarTraits<Scalar>::one()), <, tol, out, success);
4697
4698 out << "solve with random initial guess" << std::endl;
4699 X->randomize();
4700 X->norm2(initialNorms);
4701 out << " ||X_initial|| = " << std::setiosflags(std::ios::fixed) << std::setprecision(10) << initialNorms[0] << std::endl;
4702
4703 uzSmoother->Apply(*X, *RHS, false); // nonzero initial guess
4704
4705 X->norm2(finalNorms);
4706 Teuchos::Array<magnitude_type> residualNorm2 = Utilities::ResidualNorm(*A, *X, *RHS);
4707 out << " ||Residual_final|| = " << std::setiosflags(std::ios::fixed) << std::setprecision(20) << residualNorm2[0] << std::endl;
4708 out << " ||X_final|| = " << std::setiosflags(std::ios::fixed) << std::setprecision(10) << finalNorms[0] << std::endl;
4709
4710 TEUCHOS_TEST_COMPARE(residualNorm2[0], <, tol, out, success);
4711 TEUCHOS_TEST_COMPARE(finalNorms[0] - Teuchos::ScalarTraits<Scalar>::magnitude(Teuchos::ScalarTraits<Scalar>::one()), <, tol, out, success);
4712
4713 if (comm->getSize() == 1) {
4714 TEST_EQUALITY(residualNorm1[0] != residualNorm2[0], true);
4715 } else {
4716 out << "Pass/Fail is only checked in serial." << std::endl;
4717 }
4718 } // end UseTpetra
4719 }
4720
4721 TEUCHOS_UNIT_TEST_TEMPLATE_4_DECL(BlockedSmoother, NestedI2I01II_Uzawa_Setup_Apply, Scalar, LocalOrdinal, GlobalOrdinal, Node) {
4722 #include <MueLu_UseShortNames.hpp>
4723 MUELU_TESTING_SET_OSTREAM;
4724 MUELU_TESTING_LIMIT_SCOPE(Scalar, GlobalOrdinal, Node);
4725 // TODO test only Tpetra because of Ifpack2 smoother!
4726 MUELU_TEST_ONLY_FOR(Xpetra::UseTpetra) {
4727 RCP<const Teuchos::Comm<int> > comm = Parameters::getDefaultComm();
4728 Xpetra::UnderlyingLib lib = MueLuTests::TestHelpers::Parameters::getLib();
4729
4730 Teuchos::RCP<const BlockedCrsMatrix> bop = TestHelpers::TestFactory<Scalar, LocalOrdinal, GlobalOrdinal, Node>::CreateBlockDiagonalExampleMatrix(lib, 3, comm);
4731 Teuchos::RCP<const Matrix> Aconst = Teuchos::rcp_dynamic_cast<const Matrix>(bop);
4732 Teuchos::RCP<Matrix> A = Teuchos::rcp_const_cast<Matrix>(Aconst);
4733
4734 // I don't use the testApply infrastructure because it has no provision for an initial guess.
4735 Level level;
4736 TestHelpers::TestFactory<Scalar, LocalOrdinal, GlobalOrdinal, Node>::createSingleLevelHierarchy(level);
4737 level.Set("A", A);
4738
4739 // Test ReorderBlockAFactory
4740 Teuchos::RCP<ReorderBlockAFactory> rAFact = Teuchos::rcp(new ReorderBlockAFactory());
4741 rAFact->SetFactory("A", MueLu::NoFactory::getRCP());
4742 rAFact->SetParameter(std::string("Reorder Type"), Teuchos::ParameterEntry(std::string("[ 2 [0 1]]")));
4743
4744 //////////////////////////////////////////////////////////////////////
4745 // Smoothers
4746 RCP<UzawaSmoother> smootherPrototype = rcp(new UzawaSmoother());
4747 smootherPrototype->SetFactory("A", rAFact);
4748 smootherPrototype->SetParameter("Sweeps", Teuchos::ParameterEntry(Teuchos::as<LocalOrdinal>(1)));
4749 smootherPrototype->SetParameter("Damping factor", Teuchos::ParameterEntry(Teuchos::as<Scalar>(1.0)));
4750
4751 std::vector<RCP<SubBlockAFactory> > sA(1, Teuchos::null);
4752 std::vector<RCP<SmootherFactory> > sF(2, Teuchos::null);
4753 std::vector<RCP<FactoryManager> > sM(2, Teuchos::null);
4754
4755 // prediction
4756 std::string strInfo = std::string("{ 1 }");
4757 sA[0] = rcp(new SubBlockAFactory());
4758 sA[0]->SetFactory("A", rAFact);
4759 sA[0]->SetParameter("block row", Teuchos::ParameterEntry(0));
4760 sA[0]->SetParameter("block col", Teuchos::ParameterEntry(0));
4761 sA[0]->SetParameter("Range map: Striding info", Teuchos::ParameterEntry(strInfo));
4762 sA[0]->SetParameter("Domain map: Striding info", Teuchos::ParameterEntry(strInfo));
4763
4764 RCP<SmootherPrototype> smoProtoCorrect = rcp(new Ifpack2Smoother(std::string("RELAXATION"), Teuchos::ParameterList(), 0));
4765 smoProtoCorrect->SetFactory("A", sA[0]);
4766 sF[0] = rcp(new SmootherFactory(smoProtoCorrect));
4767
4768 sM[0] = rcp(new FactoryManager());
4769 sM[0]->SetFactory("A", sA[0]);
4770 sM[0]->SetFactory("Smoother", sF[0]);
4771 sM[0]->SetIgnoreUserData(true);
4772
4773 smootherPrototype->AddFactoryManager(sM[0], 0);
4774
4775 // correction
4776 // define SchurComplement Factory
4777 // SchurComp gets a RCP to AFact_ which has to be the 2x2 blocked operator
4778 // It stores the resulting SchurComplement operator as "A" generated by the SchurComplementFactory
4779 // Instead of F^{-1} it uses the approximation \hat{F}^{-1} with \hat{F} = diag(F)
4780 RCP<InverseApproximationFactory> AinvFact = Teuchos::rcp(new InverseApproximationFactory());
4781 AinvFact->SetFactory("A", rAFact);
4782
4783 RCP<SchurComplementFactory> SFact = Teuchos::rcp(new SchurComplementFactory());
4784 SFact->SetParameter("omega", Teuchos::ParameterEntry(Teuchos::as<Scalar>(1.0))); // for Simple, omega is always 1.0 in the SchurComplement
4785 SFact->SetFactory("A", rAFact);
4786 SFact->SetFactory("Ainv", AinvFact);
4787
4788 // create a 2x2 SIMPLE for the prediction eq.
4789 RCP<UzawaSmoother> smoProtoPredict = Teuchos::rcp(new UzawaSmoother());
4790 smoProtoPredict->SetParameter("Sweeps", Teuchos::ParameterEntry(1));
4791 smoProtoPredict->SetParameter("Damping factor", Teuchos::ParameterEntry(Teuchos::as<Scalar>(1.0)));
4792 smoProtoPredict->SetFactory("A", SFact);
4793
4794 for (int l = 0; l < 2; l++) {
4795 Teuchos::RCP<SubBlockAFactory> ssA = rcp(new SubBlockAFactory());
4796 ssA->SetFactory("A", SFact);
4797 ssA->SetParameter("block row", Teuchos::ParameterEntry(l)); // local block indices relative to size of blocked operator
4798 ssA->SetParameter("block col", Teuchos::ParameterEntry(l));
4799 ssA->SetParameter("Range map: Striding info", Teuchos::ParameterEntry(strInfo));
4800 ssA->SetParameter("Domain map: Striding info", Teuchos::ParameterEntry(strInfo));
4801 RCP<SmootherPrototype> ssP = rcp(new Ifpack2Smoother(std::string("RELAXATION"), Teuchos::ParameterList(), 0));
4802 ssP->SetFactory("A", ssA);
4803 Teuchos::RCP<SmootherFactory> ssF = Teuchos::rcp(new SmootherFactory(ssP));
4804 Teuchos::RCP<FactoryManager> ssM = Teuchos::rcp(new FactoryManager());
4805 ssM->SetFactory("A", ssA);
4806 ssM->SetFactory("Smoother", ssF);
4807 ssM->SetIgnoreUserData(true);
4808 smoProtoPredict->AddFactoryManager(ssM, l);
4809 }
4810
4811 sF[1] = rcp(new SmootherFactory(smoProtoPredict));
4812
4813 sM[1] = rcp(new FactoryManager());
4814 sM[1]->SetFactory("A", SFact);
4815 sM[1]->SetFactory("Smoother", sF[1]);
4816 sM[1]->SetIgnoreUserData(true);
4817
4818 smootherPrototype->AddFactoryManager(sM[1], 1);
4819
4820 RCP<SmootherFactory> smootherFact = rcp(new SmootherFactory(smootherPrototype));
4821
4822 // main factory manager
4823 FactoryManager M;
4824 M.SetFactory("Smoother", smootherFact);
4825 M.SetFactory("A", rAFact);
4826
4827 MueLu::SetFactoryManager SFM(Teuchos::rcpFromRef(level), Teuchos::rcpFromRef(M));
4828
4829 // request BGS smoother (and all dependencies) on level
4830 level.Request("A", rAFact.get());
4831 level.Request("Smoother", smootherFact.get());
4832 level.Request("PreSmoother", smootherFact.get());
4833 level.Request("PostSmoother", smootherFact.get());
4834
4835 // smootherFact->DeclareInput(level);
4836 smootherFact->Build(level);
4837
4838 level.print(std::cout, Teuchos::VERB_EXTREME);
4839
4840 RCP<SmootherBase> simpleSmoother = level.Get<RCP<SmootherBase> >("PreSmoother", smootherFact.get());
4841
4842 RCP<Matrix> reorderedA = level.Get<RCP<Matrix> >("A", rAFact.get());
4843 RCP<BlockedCrsMatrix> reorderedbA = Teuchos::rcp_dynamic_cast<BlockedCrsMatrix>(reorderedA);
4844
4845 TEST_EQUALITY(reorderedbA->Rows(), 2);
4846 TEST_EQUALITY(reorderedbA->Cols(), 2);
4847
4848 RCP<MultiVector> X = MultiVectorFactory::Build(reorderedA->getDomainMap(), 1);
4849 RCP<MultiVector> RHS = MultiVectorFactory::Build(reorderedA->getRangeMap(), 1);
4850
4851 // apply simple smoother
4852 RHS->putScalar((SC)1.0);
4853 X->putScalar((SC)0.0);
4854
4855 // solve system
4856 simpleSmoother->Apply(*X, *RHS, true); // zero initial guess
4857 RCP<BlockedMultiVector> bX = Teuchos::rcp_dynamic_cast<BlockedMultiVector>(X);
4858 TEST_EQUALITY(bX.is_null(), false);
4859 RCP<MultiVector> XX = bX->Merge();
4860 Teuchos::ArrayRCP<const Scalar> xdata = XX->getData(0);
4861 bool bCheck = true;
4862 for (size_t i = 0; i < XX->getLocalLength(); i++) {
4863 if (i < 10) {
4864 if (xdata[i] != (SC)(1.0 / 3.0)) bCheck = false;
4865 }
4866 if (i >= 10 && i < 15) {
4867 if (xdata[i] != (SC)1.0) bCheck = false;
4868 }
4869 if (i >= 15 && i < 20) {
4870 if (xdata[i] != (SC)0.5) bCheck = false;
4871 }
4872 }
4873 TEST_EQUALITY(bCheck, true);
4874
4875 // Random X
4876 X->setSeed(846930886);
4877 X->randomize();
4878
4879 typedef typename Teuchos::ScalarTraits<Scalar>::magnitudeType magnitude_type;
4880
4881 // Normalize X
4882 Array<magnitude_type> norms(1);
4883 X->norm2(norms);
4884 X->scale(1 / norms[0]);
4885
4886 // Compute RHS corresponding to X
4887 reorderedA->apply(*X, *RHS, Teuchos::NO_TRANS, (SC)1.0, (SC)0.0);
4888
4889 // Reset X to 0
4890 X->putScalar((SC)0.0);
4891
4892 RHS->norm2(norms);
4893 out << "||RHS|| = " << std::setiosflags(std::ios::fixed) << std::setprecision(10) << norms[0] << std::endl;
4894
4895 out << "solve with zero initial guess" << std::endl;
4896 Teuchos::Array<magnitude_type> initialNorms(1);
4897 X->norm2(initialNorms);
4898 out << " ||X_initial|| = " << std::setiosflags(std::ios::fixed) << std::setprecision(10) << initialNorms[0] << std::endl;
4899
4900 simpleSmoother->Apply(*X, *RHS, true); // zero initial guess
4901
4902 Teuchos::Array<magnitude_type> finalNorms(1);
4903 X->norm2(finalNorms);
4904 Teuchos::Array<magnitude_type> residualNorm1 = Utilities::ResidualNorm(*reorderedA, *X, *RHS);
4905 out << " ||Residual_final|| = " << std::setiosflags(std::ios::fixed) << std::setprecision(20) << residualNorm1[0] << std::endl;
4906 out << " ||X_final|| = " << std::setiosflags(std::ios::fixed) << std::setprecision(10) << finalNorms[0] << std::endl;
4907
4908 magnitude_type tol = 50. * Teuchos::ScalarTraits<Scalar>::eps();
4909
4910 TEUCHOS_TEST_COMPARE(residualNorm1[0], <, tol, out, success);
4911 TEUCHOS_TEST_COMPARE(finalNorms[0] - Teuchos::ScalarTraits<Scalar>::magnitude(Teuchos::ScalarTraits<Scalar>::one()), <, tol, out, success);
4912 } // end useTpetra
4913 }
4914
4915 TEUCHOS_UNIT_TEST_TEMPLATE_4_DECL(BlockedSmoother, NestedI2I01II_Uzawa_Setup_Apply2, Scalar, LocalOrdinal, GlobalOrdinal, Node) {
4916 #include <MueLu_UseShortNames.hpp>
4917 MUELU_TESTING_SET_OSTREAM;
4918 MUELU_TESTING_LIMIT_SCOPE(Scalar, GlobalOrdinal, Node);
4919 // TODO test only Tpetra because of Ifpack2 smoother!
4920 MUELU_TEST_ONLY_FOR(Xpetra::UseTpetra) {
4921 RCP<const Teuchos::Comm<int> > comm = Parameters::getDefaultComm();
4922 Xpetra::UnderlyingLib lib = MueLuTests::TestHelpers::Parameters::getLib();
4923
4924 Teuchos::RCP<const BlockedCrsMatrix> bop = TestHelpers::TestFactory<Scalar, LocalOrdinal, GlobalOrdinal, Node>::CreateBlockDiagonalExampleMatrix(lib, 3, comm);
4925 Teuchos::RCP<const Matrix> Aconst = Teuchos::rcp_dynamic_cast<const Matrix>(bop);
4926 Teuchos::RCP<Matrix> A = Teuchos::rcp_const_cast<Matrix>(Aconst);
4927
4928 // I don't use the testApply infrastructure because it has no provision for an initial guess.
4929 Level level;
4930 TestHelpers::TestFactory<Scalar, LocalOrdinal, GlobalOrdinal, Node>::createSingleLevelHierarchy(level);
4931 level.Set("A", A);
4932
4933 // Test ReorderBlockAFactory
4934 Teuchos::RCP<ReorderBlockAFactory> rAFact = Teuchos::rcp(new ReorderBlockAFactory());
4935 rAFact->SetFactory("A", MueLu::NoFactory::getRCP());
4936 rAFact->SetParameter(std::string("Reorder Type"), Teuchos::ParameterEntry(std::string("[ 2 [0 1]]")));
4937
4938 //////////////////////////////////////////////////////////////////////
4939 // Smoothers
4940 RCP<UzawaSmoother> smootherPrototype = rcp(new UzawaSmoother());
4941 smootherPrototype->SetFactory("A", rAFact);
4942 smootherPrototype->SetParameter("Sweeps", Teuchos::ParameterEntry(Teuchos::as<LocalOrdinal>(1)));
4943 smootherPrototype->SetParameter("Damping factor", Teuchos::ParameterEntry(Teuchos::as<Scalar>(1.0)));
4944
4945 std::vector<RCP<SubBlockAFactory> > sA(1, Teuchos::null);
4946 std::vector<RCP<SmootherFactory> > sF(2, Teuchos::null);
4947 std::vector<RCP<FactoryManager> > sM(2, Teuchos::null);
4948
4949 // prediction
4950 std::string strInfo = std::string("{ 1 }");
4951 sA[0] = rcp(new SubBlockAFactory());
4952 sA[0]->SetFactory("A", rAFact);
4953 sA[0]->SetParameter("block row", Teuchos::ParameterEntry(0));
4954 sA[0]->SetParameter("block col", Teuchos::ParameterEntry(0));
4955 sA[0]->SetParameter("Range map: Striding info", Teuchos::ParameterEntry(strInfo));
4956 sA[0]->SetParameter("Domain map: Striding info", Teuchos::ParameterEntry(strInfo));
4957
4958 RCP<SmootherPrototype> smoProtoCorrect = rcp(new Ifpack2Smoother(std::string("RELAXATION"), Teuchos::ParameterList(), 0));
4959 smoProtoCorrect->SetFactory("A", sA[0]);
4960 sF[0] = rcp(new SmootherFactory(smoProtoCorrect));
4961
4962 sM[0] = rcp(new FactoryManager());
4963 sM[0]->SetFactory("A", sA[0]);
4964 sM[0]->SetFactory("Smoother", sF[0]);
4965 sM[0]->SetIgnoreUserData(true);
4966
4967 smootherPrototype->AddFactoryManager(sM[0], 0);
4968
4969 // correction
4970 // define SchurComplement Factory
4971 // SchurComp gets a RCP to AFact_ which has to be the 2x2 blocked operator
4972 // It stores the resulting SchurComplement operator as "A" generated by the SchurComplementFactory
4973 // Instead of F^{-1} it uses the approximation \hat{F}^{-1} with \hat{F} = diag(F)
4974 RCP<InverseApproximationFactory> AinvFact = Teuchos::rcp(new InverseApproximationFactory());
4975 AinvFact->SetFactory("A", rAFact);
4976
4977 RCP<SchurComplementFactory> SFact = Teuchos::rcp(new SchurComplementFactory());
4978 SFact->SetParameter("omega", Teuchos::ParameterEntry(Teuchos::as<Scalar>(1.0))); // for Simple, omega is always 1.0 in the SchurComplement
4979 SFact->SetFactory("A", rAFact);
4980 SFact->SetFactory("Ainv", AinvFact);
4981
4982 // create a 2x2 SIMPLE for the prediction eq.
4983 RCP<UzawaSmoother> smoProtoPredict = Teuchos::rcp(new UzawaSmoother());
4984 smoProtoPredict->SetParameter("Sweeps", Teuchos::ParameterEntry(1));
4985 smoProtoPredict->SetParameter("Damping factor", Teuchos::ParameterEntry(Teuchos::as<Scalar>(1.0)));
4986 smoProtoPredict->SetFactory("A", SFact);
4987
4988 for (int l = 0; l < 2; l++) {
4989 Teuchos::RCP<SubBlockAFactory> ssA = rcp(new SubBlockAFactory());
4990 ssA->SetFactory("A", SFact);
4991 ssA->SetParameter("block row", Teuchos::ParameterEntry(l)); // local block indices relative to size of blocked operator
4992 ssA->SetParameter("block col", Teuchos::ParameterEntry(l));
4993 ssA->SetParameter("Range map: Striding info", Teuchos::ParameterEntry(strInfo));
4994 ssA->SetParameter("Domain map: Striding info", Teuchos::ParameterEntry(strInfo));
4995 RCP<SmootherPrototype> ssP = rcp(new Ifpack2Smoother(std::string("RELAXATION"), Teuchos::ParameterList(), 0));
4996 ssP->SetFactory("A", ssA);
4997 Teuchos::RCP<SmootherFactory> ssF = Teuchos::rcp(new SmootherFactory(ssP));
4998 Teuchos::RCP<FactoryManager> ssM = Teuchos::rcp(new FactoryManager());
4999 ssM->SetFactory("A", ssA);
5000 ssM->SetFactory("Smoother", ssF);
5001 ssM->SetIgnoreUserData(true);
5002 smoProtoPredict->AddFactoryManager(ssM, l);
5003 }
5004
5005 sF[1] = rcp(new SmootherFactory(smoProtoPredict));
5006
5007 sM[1] = rcp(new FactoryManager());
5008 sM[1]->SetFactory("A", SFact);
5009 sM[1]->SetFactory("Smoother", sF[1]);
5010 sM[1]->SetIgnoreUserData(true);
5011
5012 smootherPrototype->AddFactoryManager(sM[1], 1);
5013
5014 RCP<SmootherFactory> smootherFact = rcp(new SmootherFactory(smootherPrototype));
5015
5016 // main factory manager
5017 FactoryManager M;
5018 M.SetFactory("Smoother", smootherFact);
5019 M.SetFactory("A", rAFact);
5020
5021 MueLu::SetFactoryManager SFM(Teuchos::rcpFromRef(level), Teuchos::rcpFromRef(M));
5022
5023 // request BGS smoother (and all dependencies) on level
5024 level.Request("A", rAFact.get());
5025 level.Request("Smoother", smootherFact.get());
5026 level.Request("PreSmoother", smootherFact.get());
5027 level.Request("PostSmoother", smootherFact.get());
5028
5029 // smootherFact->DeclareInput(level);
5030 smootherFact->Build(level);
5031
5032 level.print(std::cout, Teuchos::VERB_EXTREME);
5033
5034 RCP<SmootherBase> simpleSmoother = level.Get<RCP<SmootherBase> >("PreSmoother", smootherFact.get());
5035
5036 RCP<Matrix> reorderedA = level.Get<RCP<Matrix> >("A", rAFact.get());
5037 RCP<ReorderedBlockedCrsMatrix> reorderedbA = Teuchos::rcp_dynamic_cast<ReorderedBlockedCrsMatrix>(reorderedA);
5038
5039 TEST_EQUALITY(reorderedbA->Rows(), 2);
5040 TEST_EQUALITY(reorderedbA->Cols(), 2);
5041
5042 RCP<BlockedMultiVector> bX = Teuchos::rcp(new BlockedMultiVector(bop->getBlockedDomainMap(), 1, true));
5043 RCP<BlockedMultiVector> bRHS = Teuchos::rcp(new BlockedMultiVector(bop->getBlockedRangeMap(), 1, true));
5044
5045 RCP<MultiVector> X = bX->Merge();
5046 RCP<MultiVector> RHS = bRHS->Merge();
5047
5048 // apply simple smoother
5049 RHS->putScalar((SC)1.0);
5050 X->putScalar((SC)0.0);
5051
5052 // solve system
5053 simpleSmoother->Apply(*X, *RHS, true); // zero initial guess
5054
5055 Teuchos::ArrayRCP<const Scalar> xdata = X->getData(0);
5056 bool bCheck = true;
5057 X->describe(out, Teuchos::VERB_EXTREME);
5058 for (size_t i = 0; i < X->getLocalLength(); i++) {
5059 if (i < 5) {
5060 if (xdata[i] != (SC)1.0) bCheck = false;
5061 }
5062 if (i >= 5 && i < 10) {
5063 if (xdata[i] != (SC)0.5) bCheck = false;
5064 }
5065 if (i >= 10 && i < 20) {
5066 if (xdata[i] != (SC)(1.0 / 3.0)) bCheck = false;
5067 }
5068 }
5069 TEST_EQUALITY(bCheck, true);
5070
5071 // Random X
5072 {
5073 Teuchos::RCP<const Xpetra::BlockReorderManager> brm = Xpetra::blockedReorderFromString("[ 2 [0 1]]");
5074 RCP<BlockedMultiVector> test = Teuchos::rcp_dynamic_cast<BlockedMultiVector>(buildReorderedBlockedMultiVector(brm, bX));
5075 bX.swap(test);
5076 test = Teuchos::rcp_dynamic_cast<BlockedMultiVector>(buildReorderedBlockedMultiVector(brm, bRHS));
5077 bRHS.swap(test);
5078 }
5079
5080 bRHS->putScalar((SC)1.0);
5081 bX->setSeed(846930886);
5082 bX->randomize();
5083
5084 RHS = bRHS->Merge();
5085 X = bX->Merge();
5086
5087 typedef typename Teuchos::ScalarTraits<Scalar>::magnitudeType magnitude_type;
5088
5089 // Normalize X
5090 Array<magnitude_type> norms(1);
5091 X->norm2(norms);
5092 X->scale(1 / norms[0]);
5093
5094 // Compute RHS corresponding to X
5095 bop->apply(*X, *RHS, Teuchos::NO_TRANS, (SC)1.0, (SC)0.0);
5096
5097 // Reset X to 0
5098 X->putScalar((SC)0.0);
5099
5100 RHS->norm2(norms);
5101 out << "||RHS|| = " << std::setiosflags(std::ios::fixed) << std::setprecision(10) << norms[0] << std::endl;
5102
5103 out << "solve with zero initial guess" << std::endl;
5104 Teuchos::Array<magnitude_type> initialNorms(1);
5105 X->norm2(initialNorms);
5106 out << " ||X_initial|| = " << std::setiosflags(std::ios::fixed) << std::setprecision(10) << initialNorms[0] << std::endl;
5107
5108 simpleSmoother->Apply(*X, *RHS, true); // zero initial guess
5109
5110 Teuchos::Array<magnitude_type> finalNorms(1);
5111 X->norm2(finalNorms);
5112 Teuchos::Array<magnitude_type> residualNorm1 = Utilities::ResidualNorm(*reorderedA, *X, *RHS);
5113 out << " ||Residual_final|| = " << std::setiosflags(std::ios::fixed) << std::setprecision(20) << residualNorm1[0] << std::endl;
5114 out << " ||X_final|| = " << std::setiosflags(std::ios::fixed) << std::setprecision(10) << finalNorms[0] << std::endl;
5115
5116 magnitude_type tol = 50. * Teuchos::ScalarTraits<Scalar>::eps();
5117
5118 TEUCHOS_TEST_COMPARE(residualNorm1[0], <, tol, out, success);
5119 TEUCHOS_TEST_COMPARE(finalNorms[0] - Teuchos::ScalarTraits<Scalar>::magnitude(Teuchos::ScalarTraits<Scalar>::one()), <, tol, out, success);
5120 } // end useTpetra
5121 }
5122
5123 TEUCHOS_UNIT_TEST_TEMPLATE_4_DECL(BlockedSmoother, NestedI2I01II_Thyra_Uzawa_Setup_Apply, Scalar, LocalOrdinal, GlobalOrdinal, Node) {
5124 #include <MueLu_UseShortNames.hpp>
5125 MUELU_TESTING_SET_OSTREAM;
5126 MUELU_TESTING_LIMIT_SCOPE(Scalar, GlobalOrdinal, Node);
5127 // TODO test only Tpetra because of Ifpack2 smoother!
5128 MUELU_TEST_ONLY_FOR(Xpetra::UseTpetra) {
5129 RCP<const Teuchos::Comm<int> > comm = Parameters::getDefaultComm();
5130 Xpetra::UnderlyingLib lib = MueLuTests::TestHelpers::Parameters::getLib();
5131
5132 Teuchos::RCP<const BlockedCrsMatrix> bop = TestHelpers::TestFactory<Scalar, LocalOrdinal, GlobalOrdinal, Node>::CreateBlockDiagonalExampleMatrixThyra(lib, 3, comm);
5133 Teuchos::RCP<const Matrix> Aconst = Teuchos::rcp_dynamic_cast<const Matrix>(bop);
5134 Teuchos::RCP<Matrix> A = Teuchos::rcp_const_cast<Matrix>(Aconst);
5135
5136 // I don't use the testApply infrastructure because it has no provision for an initial guess.
5137 Level level;
5138 TestHelpers::TestFactory<Scalar, LocalOrdinal, GlobalOrdinal, Node>::createSingleLevelHierarchy(level);
5139 level.Set("A", A);
5140
5141 // Test ReorderBlockAFactory
5142 Teuchos::RCP<ReorderBlockAFactory> rAFact = Teuchos::rcp(new ReorderBlockAFactory());
5143 rAFact->SetFactory("A", MueLu::NoFactory::getRCP());
5144 rAFact->SetParameter(std::string("Reorder Type"), Teuchos::ParameterEntry(std::string("[ 2 [0 1]]")));
5145
5146 //////////////////////////////////////////////////////////////////////
5147 // Smoothers
5148 RCP<UzawaSmoother> smootherPrototype = rcp(new UzawaSmoother());
5149 smootherPrototype->SetFactory("A", rAFact);
5150 smootherPrototype->SetParameter("Sweeps", Teuchos::ParameterEntry(Teuchos::as<LocalOrdinal>(1)));
5151 smootherPrototype->SetParameter("Damping factor", Teuchos::ParameterEntry(Teuchos::as<Scalar>(1.0)));
5152
5153 std::vector<RCP<SubBlockAFactory> > sA(1, Teuchos::null);
5154 std::vector<RCP<SmootherFactory> > sF(2, Teuchos::null);
5155 std::vector<RCP<FactoryManager> > sM(2, Teuchos::null);
5156
5157 // prediction
5158 std::string strInfo = std::string("{ 1 }");
5159 sA[0] = rcp(new SubBlockAFactory());
5160 sA[0]->SetFactory("A", rAFact);
5161 sA[0]->SetParameter("block row", Teuchos::ParameterEntry(0));
5162 sA[0]->SetParameter("block col", Teuchos::ParameterEntry(0));
5163 sA[0]->SetParameter("Range map: Striding info", Teuchos::ParameterEntry(strInfo));
5164 sA[0]->SetParameter("Domain map: Striding info", Teuchos::ParameterEntry(strInfo));
5165
5166 RCP<SmootherPrototype> smoProtoCorrect = rcp(new Ifpack2Smoother(std::string("RELAXATION"), Teuchos::ParameterList(), 0));
5167 smoProtoCorrect->SetFactory("A", sA[0]);
5168 sF[0] = rcp(new SmootherFactory(smoProtoCorrect));
5169
5170 sM[0] = rcp(new FactoryManager());
5171 sM[0]->SetFactory("A", sA[0]);
5172 sM[0]->SetFactory("Smoother", sF[0]);
5173 sM[0]->SetIgnoreUserData(true);
5174
5175 smootherPrototype->AddFactoryManager(sM[0], 0);
5176
5177 // correction
5178 // define SchurComplement Factory
5179 // SchurComp gets a RCP to AFact_ which has to be the 2x2 blocked operator
5180 // It stores the resulting SchurComplement operator as "A" generated by the SchurComplementFactory
5181 // Instead of F^{-1} it uses the approximation \hat{F}^{-1} with \hat{F} = diag(F)
5182 RCP<InverseApproximationFactory> AinvFact = Teuchos::rcp(new InverseApproximationFactory());
5183 AinvFact->SetFactory("A", rAFact);
5184
5185 RCP<SchurComplementFactory> SFact = Teuchos::rcp(new SchurComplementFactory());
5186 SFact->SetParameter("omega", Teuchos::ParameterEntry(Teuchos::as<Scalar>(1.0))); // for Simple, omega is always 1.0 in the SchurComplement
5187 SFact->SetFactory("A", rAFact);
5188 SFact->SetFactory("Ainv", AinvFact);
5189
5190 // create a 2x2 SIMPLE for the prediction eq.
5191 RCP<UzawaSmoother> smoProtoPredict = Teuchos::rcp(new UzawaSmoother());
5192 smoProtoPredict->SetParameter("Sweeps", Teuchos::ParameterEntry(1));
5193 smoProtoPredict->SetParameter("Damping factor", Teuchos::ParameterEntry(Teuchos::as<Scalar>(1.0)));
5194 smoProtoPredict->SetFactory("A", SFact);
5195
5196 for (int l = 0; l < 2; l++) {
5197 Teuchos::RCP<SubBlockAFactory> ssA = rcp(new SubBlockAFactory());
5198 ssA->SetFactory("A", SFact);
5199 ssA->SetParameter("block row", Teuchos::ParameterEntry(l)); // local block indices relative to size of blocked operator
5200 ssA->SetParameter("block col", Teuchos::ParameterEntry(l));
5201 ssA->SetParameter("Range map: Striding info", Teuchos::ParameterEntry(strInfo));
5202 ssA->SetParameter("Domain map: Striding info", Teuchos::ParameterEntry(strInfo));
5203 RCP<SmootherPrototype> ssP = rcp(new Ifpack2Smoother(std::string("RELAXATION"), Teuchos::ParameterList(), 0));
5204 ssP->SetFactory("A", ssA);
5205 Teuchos::RCP<SmootherFactory> ssF = Teuchos::rcp(new SmootherFactory(ssP));
5206 Teuchos::RCP<FactoryManager> ssM = Teuchos::rcp(new FactoryManager());
5207 ssM->SetFactory("A", ssA);
5208 ssM->SetFactory("Smoother", ssF);
5209 ssM->SetIgnoreUserData(true);
5210 smoProtoPredict->AddFactoryManager(ssM, l);
5211 }
5212
5213 sF[1] = rcp(new SmootherFactory(smoProtoPredict));
5214
5215 sM[1] = rcp(new FactoryManager());
5216 sM[1]->SetFactory("A", SFact);
5217 sM[1]->SetFactory("Smoother", sF[1]);
5218 sM[1]->SetIgnoreUserData(true);
5219
5220 smootherPrototype->AddFactoryManager(sM[1], 1);
5221
5222 RCP<SmootherFactory> smootherFact = rcp(new SmootherFactory(smootherPrototype));
5223
5224 // main factory manager
5225 FactoryManager M;
5226 M.SetFactory("Smoother", smootherFact);
5227 M.SetFactory("A", rAFact);
5228
5229 MueLu::SetFactoryManager SFM(Teuchos::rcpFromRef(level), Teuchos::rcpFromRef(M));
5230
5231 // request BGS smoother (and all dependencies) on level
5232 level.Request("A", rAFact.get());
5233 level.Request("Smoother", smootherFact.get());
5234 level.Request("PreSmoother", smootherFact.get());
5235 level.Request("PostSmoother", smootherFact.get());
5236
5237 // smootherFact->DeclareInput(level);
5238 smootherFact->Build(level);
5239
5240 level.print(std::cout, Teuchos::VERB_EXTREME);
5241
5242 RCP<SmootherBase> simpleSmoother = level.Get<RCP<SmootherBase> >("PreSmoother", smootherFact.get());
5243
5244 RCP<Matrix> reorderedA = level.Get<RCP<Matrix> >("A", rAFact.get());
5245 RCP<BlockedCrsMatrix> reorderedbA = Teuchos::rcp_dynamic_cast<BlockedCrsMatrix>(reorderedA);
5246
5247 TEST_EQUALITY(reorderedbA->Rows(), 2);
5248 TEST_EQUALITY(reorderedbA->Cols(), 2);
5249
5250 RCP<MultiVector> X = MultiVectorFactory::Build(reorderedA->getDomainMap(), 1);
5251 RCP<MultiVector> RHS = MultiVectorFactory::Build(reorderedA->getRangeMap(), 1);
5252
5253 // apply simple smoother
5254 RHS->putScalar((SC)1.0);
5255 X->putScalar((SC)0.0);
5256
5257 // solve system
5258 simpleSmoother->Apply(*X, *RHS, true); // zero initial guess
5259 RCP<BlockedMultiVector> bX = Teuchos::rcp_dynamic_cast<BlockedMultiVector>(X);
5260 TEST_EQUALITY(bX.is_null(), false);
5261 RCP<MultiVector> XX = bX->Merge();
5262 Teuchos::ArrayRCP<const Scalar> xdata = XX->getData(0);
5263 bool bCheck = true;
5264 for (size_t i = 0; i < XX->getLocalLength(); i++) {
5265 if (i < 10) {
5266 if (xdata[i] != (SC)(1.0 / 3.0)) bCheck = false;
5267 }
5268 if (i >= 10 && i < 15) {
5269 if (xdata[i] != (SC)1.0) bCheck = false;
5270 }
5271 if (i >= 15 && i < 20) {
5272 if (xdata[i] != (SC)0.5) bCheck = false;
5273 }
5274 }
5275 TEST_EQUALITY(bCheck, true);
5276
5277 // Random X
5278 X->setSeed(846930886);
5279 X->randomize();
5280
5281 typedef typename Teuchos::ScalarTraits<Scalar>::magnitudeType magnitude_type;
5282
5283 // Normalize X
5284 Array<magnitude_type> norms(1);
5285 X->norm2(norms);
5286 X->scale(1 / norms[0]);
5287
5288 // Compute RHS corresponding to X
5289 reorderedA->apply(*X, *RHS, Teuchos::NO_TRANS, (SC)1.0, (SC)0.0);
5290
5291 // Reset X to 0
5292 X->putScalar((SC)0.0);
5293
5294 RHS->norm2(norms);
5295 out << "||RHS|| = " << std::setiosflags(std::ios::fixed) << std::setprecision(10) << norms[0] << std::endl;
5296
5297 out << "solve with zero initial guess" << std::endl;
5298 Teuchos::Array<magnitude_type> initialNorms(1);
5299 X->norm2(initialNorms);
5300 out << " ||X_initial|| = " << std::setiosflags(std::ios::fixed) << std::setprecision(10) << initialNorms[0] << std::endl;
5301
5302 simpleSmoother->Apply(*X, *RHS, true); // zero initial guess
5303
5304 Teuchos::Array<magnitude_type> finalNorms(1);
5305 X->norm2(finalNorms);
5306 Teuchos::Array<magnitude_type> residualNorm1 = Utilities::ResidualNorm(*reorderedA, *X, *RHS);
5307 out << " ||Residual_final|| = " << std::setiosflags(std::ios::fixed) << std::setprecision(20) << residualNorm1[0] << std::endl;
5308 out << " ||X_final|| = " << std::setiosflags(std::ios::fixed) << std::setprecision(10) << finalNorms[0] << std::endl;
5309
5310 magnitude_type tol = 50. * Teuchos::ScalarTraits<Scalar>::eps();
5311
5312 TEUCHOS_TEST_COMPARE(residualNorm1[0], <, tol, out, success);
5313 TEUCHOS_TEST_COMPARE(finalNorms[0] - Teuchos::ScalarTraits<Scalar>::magnitude(Teuchos::ScalarTraits<Scalar>::one()), <, tol, out, success);
5314 } // end useTpetra
5315 }
5316
5317 TEUCHOS_UNIT_TEST_TEMPLATE_4_DECL(BlockedSmoother, NestedII01I2I_Thyra_Uzawa_Setup_Apply2, Scalar, LocalOrdinal, GlobalOrdinal, Node) {
5318 #include <MueLu_UseShortNames.hpp>
5319 MUELU_TESTING_SET_OSTREAM;
5320 MUELU_TESTING_LIMIT_SCOPE(Scalar, GlobalOrdinal, Node);
5321 // TODO test only Tpetra because of Ifpack2 smoother!
5322 MUELU_TEST_ONLY_FOR(Xpetra::UseTpetra) {
5323 RCP<const Teuchos::Comm<int> > comm = Parameters::getDefaultComm();
5324 Xpetra::UnderlyingLib lib = MueLuTests::TestHelpers::Parameters::getLib();
5325
5326 Teuchos::RCP<const BlockedCrsMatrix> bop = TestHelpers::TestFactory<Scalar, LocalOrdinal, GlobalOrdinal, Node>::CreateBlocked3x3MatrixThyra(*comm, lib);
5327 Teuchos::RCP<const Matrix> Aconst = Teuchos::rcp_dynamic_cast<const Matrix>(bop);
5328 Teuchos::RCP<Matrix> A = Teuchos::rcp_const_cast<Matrix>(Aconst);
5329
5330 // I don't use the testApply infrastructure because it has no provision for an initial guess.
5331 Level level;
5332 TestHelpers::TestFactory<Scalar, LocalOrdinal, GlobalOrdinal, Node>::createSingleLevelHierarchy(level);
5333 level.Set("A", A);
5334
5335 // Test ReorderBlockAFactory
5336 Teuchos::RCP<ReorderBlockAFactory> rAFact = Teuchos::rcp(new ReorderBlockAFactory());
5337 rAFact->SetFactory("A", MueLu::NoFactory::getRCP());
5338 rAFact->SetParameter(std::string("Reorder Type"), Teuchos::ParameterEntry(std::string("[ [0 1] 2]")));
5339
5340 //////////////////////////////////////////////////////////////////////
5341 // Smoothers
5342 RCP<UzawaSmoother> smootherPrototype = rcp(new UzawaSmoother());
5343 smootherPrototype->SetFactory("A", rAFact);
5344 smootherPrototype->SetParameter("Sweeps", Teuchos::ParameterEntry(Teuchos::as<LocalOrdinal>(15)));
5345 smootherPrototype->SetParameter("Damping factor", Teuchos::ParameterEntry(Teuchos::as<Scalar>(0.9)));
5346
5347 std::vector<RCP<SubBlockAFactory> > sA(1, Teuchos::null);
5348 std::vector<RCP<SmootherFactory> > sF(2, Teuchos::null);
5349 std::vector<RCP<FactoryManager> > sM(2, Teuchos::null);
5350
5351 // prediction
5352 std::string strInfo = std::string("{ 1 }");
5353 sA[0] = rcp(new SubBlockAFactory());
5354 sA[0]->SetFactory("A", rAFact);
5355 sA[0]->SetParameter("block row", Teuchos::ParameterEntry(0));
5356 sA[0]->SetParameter("block col", Teuchos::ParameterEntry(0));
5357 sA[0]->SetParameter("Range map: Striding info", Teuchos::ParameterEntry(strInfo));
5358 sA[0]->SetParameter("Domain map: Striding info", Teuchos::ParameterEntry(strInfo));
5359
5360 // create a 2x2 BGS for the prediction eq.
5361 RCP<BlockedGaussSeidelSmoother> smoProtoPredict = Teuchos::rcp(new BlockedGaussSeidelSmoother());
5362 smoProtoPredict->SetParameter("Sweeps", Teuchos::ParameterEntry(1));
5363 smoProtoPredict->SetFactory("A", sA[0]);
5364
5365 for (int l = 0; l < 2; l++) {
5366 Teuchos::RCP<SubBlockAFactory> ssA = rcp(new SubBlockAFactory());
5367 ssA->SetFactory("A", sA[0]);
5368 ssA->SetParameter("block row", Teuchos::ParameterEntry(l)); // local block indices relative to size of blocked operator
5369 ssA->SetParameter("block col", Teuchos::ParameterEntry(l));
5370 ssA->SetParameter("Range map: Striding info", Teuchos::ParameterEntry(strInfo));
5371 ssA->SetParameter("Domain map: Striding info", Teuchos::ParameterEntry(strInfo));
5372 RCP<SmootherPrototype> ssP = rcp(new Ifpack2Smoother(std::string("RELAXATION"), Teuchos::ParameterList(), 0));
5373 ssP->SetFactory("A", ssA);
5374 Teuchos::RCP<SmootherFactory> ssF = Teuchos::rcp(new SmootherFactory(ssP));
5375 Teuchos::RCP<FactoryManager> ssM = Teuchos::rcp(new FactoryManager());
5376 ssM->SetFactory("A", ssA);
5377 ssM->SetFactory("Smoother", ssF);
5378 ssM->SetIgnoreUserData(true);
5379 smoProtoPredict->AddFactoryManager(ssM, l);
5380 }
5381
5382 sF[0] = rcp(new SmootherFactory(smoProtoPredict));
5383
5384 sM[0] = rcp(new FactoryManager());
5385 sM[0]->SetFactory("A", sA[0]);
5386 sM[0]->SetFactory("Smoother", sF[0]);
5387 sM[0]->SetIgnoreUserData(true);
5388
5389 smootherPrototype->AddFactoryManager(sM[0], 0);
5390
5391 // correction
5392 // define SchurComplement Factory
5393 // SchurComp gets a RCP to AFact_ which has to be the 2x2 blocked operator
5394 // It stores the resulting SchurComplement operator as "A" generated by the SchurComplementFactory
5395 // Instead of F^{-1} it uses the approximation \hat{F}^{-1} with \hat{F} = diag(F)
5396 RCP<InverseApproximationFactory> AinvFact = Teuchos::rcp(new InverseApproximationFactory());
5397 AinvFact->SetFactory("A", rAFact);
5398
5399 RCP<SchurComplementFactory> SFact = Teuchos::rcp(new SchurComplementFactory());
5400 SFact->SetParameter("omega", Teuchos::ParameterEntry(Teuchos::as<Scalar>(1.0))); // for Simple, omega is always 1.0 in the SchurComplement
5401 SFact->SetFactory("A", rAFact);
5402 SFact->SetFactory("Ainv", AinvFact);
5403
5404 Teuchos::ParameterList paramList;
5405 paramList.set("relaxation: sweeps", Teuchos::as<LocalOrdinal>(30));
5406 paramList.set("relaxation: damping factor", Teuchos::as<Scalar>(0.9));
5407 RCP<SmootherPrototype> smoProtoCorrect = rcp(new Ifpack2Smoother(std::string("RELAXATION"), paramList, 0));
5408 smoProtoCorrect->SetFactory("A", SFact);
5409 sF[1] = rcp(new SmootherFactory(smoProtoCorrect));
5410
5411 sM[1] = rcp(new FactoryManager());
5412 sM[1]->SetFactory("A", SFact);
5413 sM[1]->SetFactory("Smoother", sF[1]);
5414 sM[1]->SetIgnoreUserData(true);
5415
5416 smootherPrototype->AddFactoryManager(sM[1], 1);
5417
5418 RCP<SmootherFactory> smootherFact = rcp(new SmootherFactory(smootherPrototype));
5419
5420 // main factory manager
5421 FactoryManager M;
5422 M.SetFactory("Smoother", smootherFact);
5423 M.SetFactory("A", rAFact);
5424
5425 MueLu::SetFactoryManager SFM(Teuchos::rcpFromRef(level), Teuchos::rcpFromRef(M));
5426
5427 // request BGS smoother (and all dependencies) on level
5428 level.Request("A", rAFact.get());
5429 level.Request("Smoother", smootherFact.get());
5430 level.Request("PreSmoother", smootherFact.get());
5431 level.Request("PostSmoother", smootherFact.get());
5432
5433 // smootherFact->DeclareInput(level);
5434 smootherFact->Build(level);
5435
5436 level.print(std::cout, Teuchos::VERB_EXTREME);
5437
5438 RCP<SmootherBase> simpleSmoother = level.Get<RCP<SmootherBase> >("PreSmoother", smootherFact.get());
5439
5440 RCP<Matrix> reorderedA = level.Get<RCP<Matrix> >("A", rAFact.get());
5441 RCP<BlockedCrsMatrix> reorderedbA = Teuchos::rcp_dynamic_cast<BlockedCrsMatrix>(reorderedA);
5442
5443 RCP<MultiVector> X = MultiVectorFactory::Build(reorderedA->getDomainMap(), 1);
5444 RCP<MultiVector> RHS = MultiVectorFactory::Build(reorderedA->getRangeMap(), 1);
5445
5446 // Random X
5447 X->setSeed(846930886);
5448 X->randomize();
5449
5450 typedef typename Teuchos::ScalarTraits<Scalar>::magnitudeType magnitude_type;
5451
5452 // Normalize X
5453 Array<magnitude_type> norms(1);
5454 X->norm2(norms);
5455 X->scale(1 / norms[0]);
5456
5457 // Compute RHS corresponding to X
5458 reorderedA->apply(*X, *RHS, Teuchos::NO_TRANS, (SC)1.0, (SC)0.0);
5459
5460 // Reset X to 0
5461 X->putScalar((SC)0.0);
5462
5463 RHS->norm2(norms);
5464 out << "||RHS|| = " << std::setiosflags(std::ios::fixed) << std::setprecision(10) << norms[0] << std::endl;
5465
5466 out << "solve with zero initial guess" << std::endl;
5467 Teuchos::Array<magnitude_type> initialNorms(1);
5468 X->norm2(initialNorms);
5469 out << " ||X_initial|| = " << std::setiosflags(std::ios::fixed) << std::setprecision(10) << initialNorms[0] << std::endl;
5470
5471 simpleSmoother->Apply(*X, *RHS, true); // zero initial guess
5472
5473 Teuchos::Array<magnitude_type> finalNorms(1);
5474 X->norm2(finalNorms);
5475 Teuchos::Array<magnitude_type> residualNorm1 = Utilities::ResidualNorm(*reorderedA, *X, *RHS);
5476 out << " ||Residual_final|| = " << std::setiosflags(std::ios::fixed) << std::setprecision(20) << residualNorm1[0] << std::endl;
5477 out << " ||X_final|| = " << std::setiosflags(std::ios::fixed) << std::setprecision(10) << finalNorms[0] << std::endl;
5478
5479 TEUCHOS_TEST_COMPARE(residualNorm1[0], <, 8e-3, out, success);
5480 TEUCHOS_TEST_COMPARE(residualNorm1[0], >, 5e-3, out, success);
5481 } // end UseTpetra
5482 }
5483
5484 TEUCHOS_UNIT_TEST_TEMPLATE_4_DECL(BlockedSmoother, NestedI0I21II_Thyra_Uzawa_Setup_Apply3, Scalar, LocalOrdinal, GlobalOrdinal, Node) {
5485 #include <MueLu_UseShortNames.hpp>
5486 MUELU_TESTING_SET_OSTREAM;
5487 MUELU_TESTING_LIMIT_SCOPE(Scalar, GlobalOrdinal, Node);
5488 // TODO test only Tpetra because of Ifpack2 smoother!
5489 MUELU_TEST_ONLY_FOR(Xpetra::UseTpetra) {
5490 RCP<const Teuchos::Comm<int> > comm = Parameters::getDefaultComm();
5491 Xpetra::UnderlyingLib lib = MueLuTests::TestHelpers::Parameters::getLib();
5492
5493 Teuchos::RCP<const BlockedCrsMatrix> bop = TestHelpers::TestFactory<Scalar, LocalOrdinal, GlobalOrdinal, Node>::CreateBlocked3x3MatrixThyra(*comm, lib);
5494 Teuchos::RCP<const Matrix> Aconst = Teuchos::rcp_dynamic_cast<const Matrix>(bop);
5495 Teuchos::RCP<Matrix> A = Teuchos::rcp_const_cast<Matrix>(Aconst);
5496
5497 // I don't use the testApply infrastructure because it has no provision for an initial guess.
5498 Level level;
5499 TestHelpers::TestFactory<Scalar, LocalOrdinal, GlobalOrdinal, Node>::createSingleLevelHierarchy(level);
5500 level.Set("A", A);
5501
5502 // Test ReorderBlockAFactory
5503 Teuchos::RCP<ReorderBlockAFactory> rAFact = Teuchos::rcp(new ReorderBlockAFactory());
5504 rAFact->SetFactory("A", MueLu::NoFactory::getRCP());
5505 rAFact->SetParameter(std::string("Reorder Type"), Teuchos::ParameterEntry(std::string("[ 0 [ 2 1 ]]")));
5506
5507 //////////////////////////////////////////////////////////////////////
5508 // Smoothers
5509 RCP<UzawaSmoother> smootherPrototype = rcp(new UzawaSmoother());
5510 smootherPrototype->SetFactory("A", rAFact);
5511 smootherPrototype->SetParameter("Sweeps", Teuchos::ParameterEntry(Teuchos::as<LocalOrdinal>(15)));
5512 smootherPrototype->SetParameter("Damping factor", Teuchos::ParameterEntry(Teuchos::as<Scalar>(0.9)));
5513
5514 std::vector<RCP<SubBlockAFactory> > sA(1, Teuchos::null);
5515 std::vector<RCP<SmootherFactory> > sF(2, Teuchos::null);
5516 std::vector<RCP<FactoryManager> > sM(2, Teuchos::null);
5517
5518 // prediction
5519 std::string strInfo = std::string("{ 1 }");
5520 sA[0] = rcp(new SubBlockAFactory());
5521 sA[0]->SetFactory("A", rAFact);
5522 sA[0]->SetParameter("block row", Teuchos::ParameterEntry(0));
5523 sA[0]->SetParameter("block col", Teuchos::ParameterEntry(0));
5524 sA[0]->SetParameter("Range map: Striding info", Teuchos::ParameterEntry(strInfo));
5525 sA[0]->SetParameter("Domain map: Striding info", Teuchos::ParameterEntry(strInfo));
5526
5527 RCP<SmootherPrototype> smoProtoPredict = rcp(new Ifpack2Smoother(std::string("RELAXATION"), Teuchos::ParameterList(), 0));
5528 smoProtoPredict->SetFactory("A", sA[0]);
5529 sF[0] = rcp(new SmootherFactory(smoProtoPredict));
5530
5531 sM[0] = rcp(new FactoryManager());
5532 sM[0]->SetFactory("A", sA[0]);
5533 sM[0]->SetFactory("Smoother", sF[0]);
5534 sM[0]->SetIgnoreUserData(true);
5535
5536 smootherPrototype->AddFactoryManager(sM[0], 0);
5537
5538 // correction
5539 // define SchurComplement Factory
5540 // SchurComp gets a RCP to AFact_ which has to be the 2x2 blocked operator
5541 // It stores the resulting SchurComplement operator as "A" generated by the SchurComplementFactory
5542 // Instead of F^{-1} it uses the approximation \hat{F}^{-1} with \hat{F} = diag(F)
5543 RCP<InverseApproximationFactory> AinvFact = Teuchos::rcp(new InverseApproximationFactory());
5544 AinvFact->SetFactory("A", rAFact);
5545
5546 RCP<SchurComplementFactory> SFact = Teuchos::rcp(new SchurComplementFactory());
5547 SFact->SetParameter("omega", Teuchos::ParameterEntry(Teuchos::as<Scalar>(1.0))); // for Simple, omega is always 1.0 in the SchurComplement
5548 SFact->SetFactory("A", rAFact);
5549 SFact->SetFactory("Ainv", AinvFact);
5550
5551 // create a 2x2 Simple for the prediction eq.
5552 RCP<UzawaSmoother> smoProtoCorrect = Teuchos::rcp(new UzawaSmoother());
5553 smoProtoCorrect->SetParameter("Sweeps", Teuchos::ParameterEntry(1));
5554 smoProtoCorrect->SetParameter("Damping factor", Teuchos::ParameterEntry(Teuchos::as<Scalar>(1.0)));
5555 smoProtoCorrect->SetFactory("A", SFact);
5556
5557 for (int l = 0; l < 2; l++) {
5558 Teuchos::RCP<SubBlockAFactory> ssA = rcp(new SubBlockAFactory());
5559 ssA->SetFactory("A", SFact);
5560 ssA->SetParameter("block row", Teuchos::ParameterEntry(l)); // local block indices relative to size of blocked operator
5561 ssA->SetParameter("block col", Teuchos::ParameterEntry(l));
5562 ssA->SetParameter("Range map: Striding info", Teuchos::ParameterEntry(strInfo));
5563 ssA->SetParameter("Domain map: Striding info", Teuchos::ParameterEntry(strInfo));
5564 RCP<SmootherPrototype> ssP = rcp(new Ifpack2Smoother(std::string("RELAXATION"), Teuchos::ParameterList(), 0));
5565 ssP->SetFactory("A", ssA);
5566 Teuchos::RCP<SmootherFactory> ssF = Teuchos::rcp(new SmootherFactory(ssP));
5567 Teuchos::RCP<FactoryManager> ssM = Teuchos::rcp(new FactoryManager());
5568 ssM->SetFactory("A", ssA);
5569 ssM->SetFactory("Smoother", ssF);
5570 ssM->SetIgnoreUserData(true);
5571 smoProtoCorrect->AddFactoryManager(ssM, l);
5572 }
5573
5574 sF[1] = rcp(new SmootherFactory(smoProtoCorrect));
5575
5576 sM[1] = rcp(new FactoryManager());
5577 sM[1]->SetFactory("A", SFact);
5578 sM[1]->SetFactory("Smoother", sF[1]);
5579 sM[1]->SetIgnoreUserData(true);
5580
5581 smootherPrototype->AddFactoryManager(sM[1], 1);
5582
5583 RCP<SmootherFactory> smootherFact = rcp(new SmootherFactory(smootherPrototype));
5584
5585 // main factory manager
5586 FactoryManager M;
5587 M.SetFactory("Smoother", smootherFact);
5588 M.SetFactory("A", rAFact);
5589
5590 MueLu::SetFactoryManager SFM(Teuchos::rcpFromRef(level), Teuchos::rcpFromRef(M));
5591
5592 // request BGS smoother (and all dependencies) on level
5593 level.Request("A", rAFact.get());
5594 level.Request("Smoother", smootherFact.get());
5595 level.Request("PreSmoother", smootherFact.get());
5596 level.Request("PostSmoother", smootherFact.get());
5597
5598 // smootherFact->DeclareInput(level);
5599 smootherFact->Build(level);
5600
5601 level.print(std::cout, Teuchos::VERB_EXTREME);
5602
5603 RCP<SmootherBase> simpleSmoother = level.Get<RCP<SmootherBase> >("PreSmoother", smootherFact.get());
5604
5605 RCP<Matrix> reorderedA = level.Get<RCP<Matrix> >("A", rAFact.get());
5606 RCP<BlockedCrsMatrix> reorderedbA = Teuchos::rcp_dynamic_cast<BlockedCrsMatrix>(reorderedA);
5607
5608 RCP<MultiVector> X = MultiVectorFactory::Build(reorderedA->getDomainMap(), 1);
5609 RCP<MultiVector> RHS = MultiVectorFactory::Build(reorderedA->getRangeMap(), 1);
5610
5611 // Random X
5612 X->setSeed(846930886);
5613 X->randomize();
5614
5615 typedef typename Teuchos::ScalarTraits<Scalar>::magnitudeType magnitude_type;
5616
5617 // Normalize X
5618 Array<magnitude_type> norms(1);
5619 X->norm2(norms);
5620 X->scale(1 / norms[0]);
5621
5622 // Compute RHS corresponding to X
5623 reorderedA->apply(*X, *RHS, Teuchos::NO_TRANS, (SC)1.0, (SC)0.0);
5624
5625 // Reset X to 0
5626 X->putScalar((SC)0.0);
5627
5628 RHS->norm2(norms);
5629 out << "||RHS|| = " << std::setiosflags(std::ios::fixed) << std::setprecision(10) << norms[0] << std::endl;
5630
5631 out << "solve with zero initial guess" << std::endl;
5632 Teuchos::Array<magnitude_type> initialNorms(1);
5633 X->norm2(initialNorms);
5634 out << " ||X_initial|| = " << std::setiosflags(std::ios::fixed) << std::setprecision(10) << initialNorms[0] << std::endl;
5635
5636 simpleSmoother->Apply(*X, *RHS, true); // zero initial guess
5637
5638 Teuchos::Array<magnitude_type> finalNorms(1);
5639 X->norm2(finalNorms);
5640 Teuchos::Array<magnitude_type> residualNorm1 = Utilities::ResidualNorm(*reorderedA, *X, *RHS);
5641 out << " ||Residual_final|| = " << std::setiosflags(std::ios::fixed) << std::setprecision(20) << residualNorm1[0] << std::endl;
5642 out << " ||X_final|| = " << std::setiosflags(std::ios::fixed) << std::setprecision(10) << finalNorms[0] << std::endl;
5643
5644 TEUCHOS_TEST_COMPARE(residualNorm1[0], <, 11e-3, out, success);
5645 TEUCHOS_TEST_COMPARE(residualNorm1[0], >, 5e-3, out, success);
5646 } // end UseTpetra
5647 }
5648
5649 TEUCHOS_UNIT_TEST_TEMPLATE_4_DECL(BlockedSmoother, Indef_Setup_Apply, Scalar, LocalOrdinal, GlobalOrdinal, Node) {
5650 #include <MueLu_UseShortNames.hpp>
5651 MUELU_TESTING_SET_OSTREAM;
5652 MUELU_TESTING_LIMIT_SCOPE(Scalar, GlobalOrdinal, Node);
5653 MUELU_TEST_ONLY_FOR(Xpetra::UseTpetra) {
5654 RCP<const Teuchos::Comm<int> > comm = Parameters::getDefaultComm();
5655
5656 int noBlocks = 2;
5657 Teuchos::RCP<const BlockedCrsMatrix> bop = CreateBlockDiagonalExampleMatrix<Scalar, LocalOrdinal, GlobalOrdinal, Node, TpetraMap>(noBlocks, *comm);
5658 Teuchos::RCP<const Matrix> Aconst = Teuchos::rcp_dynamic_cast<const Matrix>(bop);
5659 Teuchos::RCP<Matrix> A = Teuchos::rcp_const_cast<Matrix>(Aconst);
5660
5661 // I don't use the testApply infrastructure because it has no provision for an initial guess.
5662 Level level;
5663 TestHelpers::TestFactory<Scalar, LocalOrdinal, GlobalOrdinal, Node>::createSingleLevelHierarchy(level);
5664 level.Set("A", A);
5665
5666 //////////////////////////////////////////////////////////////////////
5667 // Smoothers
5668 RCP<IndefBlockedDiagonalSmoother> smootherPrototype = rcp(new IndefBlockedDiagonalSmoother());
5669 smootherPrototype->SetParameter("Sweeps", Teuchos::ParameterEntry(1));
5670
5671 std::vector<RCP<SubBlockAFactory> > sA(noBlocks, Teuchos::null);
5672 std::vector<RCP<SmootherFactory> > sF(noBlocks, Teuchos::null);
5673 std::vector<RCP<FactoryManager> > sM(noBlocks, Teuchos::null);
5674 for (int k = 0; k < noBlocks; k++) {
5675 std::string strInfo = std::string("{ 1 }");
5676 sA[k] = rcp(new SubBlockAFactory());
5677 sA[k]->SetFactory("A", MueLu::NoFactory::getRCP());
5678 sA[k]->SetParameter("block row", Teuchos::ParameterEntry(k));
5679 sA[k]->SetParameter("block col", Teuchos::ParameterEntry(k));
5680 sA[k]->SetParameter("Range map: Striding info", Teuchos::ParameterEntry(strInfo));
5681 sA[k]->SetParameter("Domain map: Striding info", Teuchos::ParameterEntry(strInfo));
5682
5683 RCP<SmootherPrototype> smoProto = rcp(new Ifpack2Smoother(std::string("RELAXATION"), Teuchos::ParameterList(), 0));
5684 smoProto->SetFactory("A", sA[k]);
5685 sF[k] = rcp(new SmootherFactory(smoProto));
5686
5687 sM[k] = rcp(new FactoryManager());
5688 sM[k]->SetFactory("A", sA[k]);
5689 sM[k]->SetFactory("Smoother", sF[k]);
5690 sM[k]->SetIgnoreUserData(true);
5691
5692 smootherPrototype->AddFactoryManager(sM[k], k);
5693 }
5694
5695 RCP<SmootherFactory> smootherFact = rcp(new SmootherFactory(smootherPrototype));
5696
5697 // main factory manager
5698 FactoryManager M;
5699 M.SetFactory("Smoother", smootherFact);
5700
5701 MueLu::SetFactoryManager SFM(Teuchos::rcpFromRef(level), Teuchos::rcpFromRef(M));
5702
5703 // request BGS smoother (and all dependencies) on level
5704 level.Request("Smoother", smootherFact.get());
5705 level.Request("PreSmoother", smootherFact.get());
5706 level.Request("PostSmoother", smootherFact.get());
5707
5708 // smootherFact->DeclareInput(level);
5709 smootherFact->Build(level);
5710
5711 level.print(std::cout, Teuchos::VERB_EXTREME);
5712
5713 RCP<SmootherBase> inSmoother = level.Get<RCP<SmootherBase> >("PreSmoother", smootherFact.get());
5714
5715 RCP<MultiVector> X = MultiVectorFactory::Build(A->getDomainMap(), 1);
5716 RCP<MultiVector> RHS = MultiVectorFactory::Build(A->getRangeMap(), 1);
5717
5718 // Random X
5719 X->setSeed(846930886);
5720 X->randomize();
5721
5722 typedef typename Teuchos::ScalarTraits<Scalar>::magnitudeType magnitude_type;
5723
5724 // Normalize X
5725 Array<magnitude_type> norms(1);
5726 X->norm2(norms);
5727 X->scale(1 / norms[0]);
5728
5729 // Compute RHS corresponding to X
5730 A->apply(*X, *RHS, Teuchos::NO_TRANS, (SC)1.0, (SC)0.0);
5731
5732 // Reset X to 0
5733 X->putScalar((SC)0.0);
5734
5735 RHS->norm2(norms);
5736 out << "||RHS|| = " << std::setiosflags(std::ios::fixed) << std::setprecision(10) << norms[0] << std::endl;
5737
5738 out << "solve with zero initial guess" << std::endl;
5739 Teuchos::Array<magnitude_type> initialNorms(1);
5740 X->norm2(initialNorms);
5741 out << " ||X_initial|| = " << std::setiosflags(std::ios::fixed) << std::setprecision(10) << initialNorms[0] << std::endl;
5742
5743 inSmoother->Apply(*X, *RHS, true); // zero initial guess
5744
5745 Teuchos::Array<magnitude_type> finalNorms(1);
5746 X->norm2(finalNorms);
5747 Teuchos::Array<magnitude_type> residualNorm1 = Utilities::ResidualNorm(*A, *X, *RHS);
5748 out << " ||Residual_final|| = " << std::setiosflags(std::ios::fixed) << std::setprecision(20) << residualNorm1[0] << std::endl;
5749 out << " ||X_final|| = " << std::setiosflags(std::ios::fixed) << std::setprecision(10) << finalNorms[0] << std::endl;
5750
5751 magnitude_type tol = 50. * Teuchos::ScalarTraits<Scalar>::eps();
5752
5753 TEUCHOS_TEST_COMPARE(residualNorm1[0], <, tol, out, success);
5754 TEUCHOS_TEST_COMPARE(finalNorms[0] - Teuchos::ScalarTraits<Scalar>::magnitude(Teuchos::ScalarTraits<Scalar>::one()), <, tol, out, success);
5755
5756 out << "solve with random initial guess" << std::endl;
5757 X->randomize();
5758 X->norm2(initialNorms);
5759 out << " ||X_initial|| = " << std::setiosflags(std::ios::fixed) << std::setprecision(10) << initialNorms[0] << std::endl;
5760
5761 inSmoother->Apply(*X, *RHS, false); // nonzero initial guess
5762
5763 X->norm2(finalNorms);
5764 Teuchos::Array<magnitude_type> residualNorm2 = Utilities::ResidualNorm(*A, *X, *RHS);
5765 out << " ||Residual_final|| = " << std::setiosflags(std::ios::fixed) << std::setprecision(20) << residualNorm2[0] << std::endl;
5766 out << " ||X_final|| = " << std::setiosflags(std::ios::fixed) << std::setprecision(10) << finalNorms[0] << std::endl;
5767
5768 TEUCHOS_TEST_COMPARE(residualNorm2[0], <, tol, out, success);
5769 TEUCHOS_TEST_COMPARE(finalNorms[0] - Teuchos::ScalarTraits<Scalar>::magnitude(Teuchos::ScalarTraits<Scalar>::one()), <, tol, out, success);
5770
5771 if (comm->getSize() == 1) {
5772 TEST_EQUALITY(residualNorm1[0] != residualNorm2[0], true);
5773 } else {
5774 out << "Pass/Fail is only checked in serial." << std::endl;
5775 }
5776 } // end UseTpetra
5777 }
5778
5779 TEUCHOS_UNIT_TEST_TEMPLATE_4_DECL(BlockedSmoother, NestedI2I01II_Indef_Setup_Apply, Scalar, LocalOrdinal, GlobalOrdinal, Node) {
5780 #include <MueLu_UseShortNames.hpp>
5781 MUELU_TESTING_SET_OSTREAM;
5782 MUELU_TESTING_LIMIT_SCOPE(Scalar, GlobalOrdinal, Node);
5783 // TODO test only Tpetra because of Ifpack2 smoother!
5784 MUELU_TEST_ONLY_FOR(Xpetra::UseTpetra) {
5785 RCP<const Teuchos::Comm<int> > comm = Parameters::getDefaultComm();
5786 Xpetra::UnderlyingLib lib = MueLuTests::TestHelpers::Parameters::getLib();
5787
5788 Teuchos::RCP<const BlockedCrsMatrix> bop = TestHelpers::TestFactory<Scalar, LocalOrdinal, GlobalOrdinal, Node>::CreateBlockDiagonalExampleMatrix(lib, 3, comm);
5789 Teuchos::RCP<const Matrix> Aconst = Teuchos::rcp_dynamic_cast<const Matrix>(bop);
5790 Teuchos::RCP<Matrix> A = Teuchos::rcp_const_cast<Matrix>(Aconst);
5791
5792 // I don't use the testApply infrastructure because it has no provision for an initial guess.
5793 Level level;
5794 TestHelpers::TestFactory<Scalar, LocalOrdinal, GlobalOrdinal, Node>::createSingleLevelHierarchy(level);
5795 level.Set("A", A);
5796
5797 // Test ReorderBlockAFactory
5798 Teuchos::RCP<ReorderBlockAFactory> rAFact = Teuchos::rcp(new ReorderBlockAFactory());
5799 rAFact->SetFactory("A", MueLu::NoFactory::getRCP());
5800 rAFact->SetParameter(std::string("Reorder Type"), Teuchos::ParameterEntry(std::string("[ 2 [0 1]]")));
5801
5802 //////////////////////////////////////////////////////////////////////
5803 // Smoothers
5804 RCP<IndefBlockedDiagonalSmoother> smootherPrototype = rcp(new IndefBlockedDiagonalSmoother());
5805 smootherPrototype->SetFactory("A", rAFact);
5806 smootherPrototype->SetParameter("Sweeps", Teuchos::ParameterEntry(Teuchos::as<LocalOrdinal>(1)));
5807 smootherPrototype->SetParameter("Damping factor", Teuchos::ParameterEntry(Teuchos::as<Scalar>(1.0)));
5808
5809 std::vector<RCP<SubBlockAFactory> > sA(1, Teuchos::null);
5810 std::vector<RCP<SmootherFactory> > sF(2, Teuchos::null);
5811 std::vector<RCP<FactoryManager> > sM(2, Teuchos::null);
5812
5813 // prediction
5814 std::string strInfo = std::string("{ 1 }");
5815 sA[0] = rcp(new SubBlockAFactory());
5816 sA[0]->SetFactory("A", rAFact);
5817 sA[0]->SetParameter("block row", Teuchos::ParameterEntry(0));
5818 sA[0]->SetParameter("block col", Teuchos::ParameterEntry(0));
5819 sA[0]->SetParameter("Range map: Striding info", Teuchos::ParameterEntry(strInfo));
5820 sA[0]->SetParameter("Domain map: Striding info", Teuchos::ParameterEntry(strInfo));
5821
5822 RCP<SmootherPrototype> smoProtoCorrect = rcp(new Ifpack2Smoother(std::string("RELAXATION"), Teuchos::ParameterList(), 0));
5823 smoProtoCorrect->SetFactory("A", sA[0]);
5824 sF[0] = rcp(new SmootherFactory(smoProtoCorrect));
5825
5826 sM[0] = rcp(new FactoryManager());
5827 sM[0]->SetFactory("A", sA[0]);
5828 sM[0]->SetFactory("Smoother", sF[0]);
5829 sM[0]->SetIgnoreUserData(true);
5830
5831 smootherPrototype->AddFactoryManager(sM[0], 0);
5832
5833 // correction
5834 // define SchurComplement Factory
5835 // SchurComp gets a RCP to AFact_ which has to be the 2x2 blocked operator
5836 // It stores the resulting SchurComplement operator as "A" generated by the SchurComplementFactory
5837 // Instead of F^{-1} it uses the approximation \hat{F}^{-1} with \hat{F} = diag(F)
5838 RCP<InverseApproximationFactory> AinvFact = Teuchos::rcp(new InverseApproximationFactory());
5839 AinvFact->SetFactory("A", rAFact);
5840
5841 RCP<SchurComplementFactory> SFact = Teuchos::rcp(new SchurComplementFactory());
5842 SFact->SetParameter("omega", Teuchos::ParameterEntry(Teuchos::as<Scalar>(1.0))); // for Simple, omega is always 1.0 in the SchurComplement
5843 SFact->SetFactory("A", rAFact);
5844 SFact->SetFactory("Ainv", AinvFact);
5845
5846 // create a 2x2 block smoother for the prediction eq.
5847 RCP<IndefBlockedDiagonalSmoother> smoProtoPredict = Teuchos::rcp(new IndefBlockedDiagonalSmoother());
5848 smoProtoPredict->SetParameter("Sweeps", Teuchos::ParameterEntry(1));
5849 smoProtoPredict->SetParameter("Damping factor", Teuchos::ParameterEntry(Teuchos::as<Scalar>(1.0)));
5850 smoProtoPredict->SetFactory("A", SFact);
5851
5852 for (int l = 0; l < 2; l++) {
5853 Teuchos::RCP<SubBlockAFactory> ssA = rcp(new SubBlockAFactory());
5854 ssA->SetFactory("A", SFact);
5855 ssA->SetParameter("block row", Teuchos::ParameterEntry(l)); // local block indices relative to size of blocked operator
5856 ssA->SetParameter("block col", Teuchos::ParameterEntry(l));
5857 ssA->SetParameter("Range map: Striding info", Teuchos::ParameterEntry(strInfo));
5858 ssA->SetParameter("Domain map: Striding info", Teuchos::ParameterEntry(strInfo));
5859 RCP<SmootherPrototype> ssP = rcp(new Ifpack2Smoother(std::string("RELAXATION"), Teuchos::ParameterList(), 0));
5860 ssP->SetFactory("A", ssA);
5861 Teuchos::RCP<SmootherFactory> ssF = Teuchos::rcp(new SmootherFactory(ssP));
5862 Teuchos::RCP<FactoryManager> ssM = Teuchos::rcp(new FactoryManager());
5863 ssM->SetFactory("A", ssA);
5864 ssM->SetFactory("Smoother", ssF);
5865 ssM->SetIgnoreUserData(true);
5866 smoProtoPredict->AddFactoryManager(ssM, l);
5867 }
5868
5869 sF[1] = rcp(new SmootherFactory(smoProtoPredict));
5870
5871 sM[1] = rcp(new FactoryManager());
5872 sM[1]->SetFactory("A", SFact);
5873 sM[1]->SetFactory("Smoother", sF[1]);
5874 sM[1]->SetIgnoreUserData(true);
5875
5876 smootherPrototype->AddFactoryManager(sM[1], 1);
5877
5878 RCP<SmootherFactory> smootherFact = rcp(new SmootherFactory(smootherPrototype));
5879
5880 // main factory manager
5881 FactoryManager M;
5882 M.SetFactory("Smoother", smootherFact);
5883 M.SetFactory("A", rAFact);
5884
5885 MueLu::SetFactoryManager SFM(Teuchos::rcpFromRef(level), Teuchos::rcpFromRef(M));
5886
5887 // request block smoother (and all dependencies) on level
5888 level.Request("A", rAFact.get());
5889 level.Request("Smoother", smootherFact.get());
5890 level.Request("PreSmoother", smootherFact.get());
5891 level.Request("PostSmoother", smootherFact.get());
5892
5893 // smootherFact->DeclareInput(level);
5894 smootherFact->Build(level);
5895
5896 level.print(std::cout, Teuchos::VERB_EXTREME);
5897
5898 RCP<SmootherBase> inSmoother = level.Get<RCP<SmootherBase> >("PreSmoother", smootherFact.get());
5899
5900 RCP<Matrix> reorderedA = level.Get<RCP<Matrix> >("A", rAFact.get());
5901 RCP<BlockedCrsMatrix> reorderedbA = Teuchos::rcp_dynamic_cast<BlockedCrsMatrix>(reorderedA);
5902
5903 TEST_EQUALITY(reorderedbA->Rows(), 2);
5904 TEST_EQUALITY(reorderedbA->Cols(), 2);
5905
5906 RCP<MultiVector> X = MultiVectorFactory::Build(reorderedA->getDomainMap(), 1);
5907 RCP<MultiVector> RHS = MultiVectorFactory::Build(reorderedA->getRangeMap(), 1);
5908
5909 // apply simple smoother
5910 RHS->putScalar((SC)1.0);
5911 X->putScalar((SC)0.0);
5912
5913 // solve system
5914 inSmoother->Apply(*X, *RHS, true); // zero initial guess
5915 RCP<BlockedMultiVector> bX = Teuchos::rcp_dynamic_cast<BlockedMultiVector>(X);
5916 TEST_EQUALITY(bX.is_null(), false);
5917 RCP<MultiVector> XX = bX->Merge();
5918 Teuchos::ArrayRCP<const Scalar> xdata = XX->getData(0);
5919 bool bCheck = true;
5920 for (size_t i = 0; i < XX->getLocalLength(); i++) {
5921 if (i < 10) {
5922 if (xdata[i] != (SC)(1.0 / 3.0)) bCheck = false;
5923 }
5924 if (i >= 10 && i < 15) {
5925 if (xdata[i] != (SC)1.0) bCheck = false;
5926 }
5927 if (i >= 15 && i < 20) {
5928 if (xdata[i] != (SC)0.5) bCheck = false;
5929 }
5930 }
5931 TEST_EQUALITY(bCheck, true);
5932
5933 // Random X
5934 X->setSeed(846930886);
5935 X->randomize();
5936
5937 typedef typename Teuchos::ScalarTraits<Scalar>::magnitudeType magnitude_type;
5938
5939 // Normalize X
5940 Array<magnitude_type> norms(1);
5941 X->norm2(norms);
5942 X->scale(1 / norms[0]);
5943
5944 // Compute RHS corresponding to X
5945 reorderedA->apply(*X, *RHS, Teuchos::NO_TRANS, (SC)1.0, (SC)0.0);
5946
5947 // Reset X to 0
5948 X->putScalar((SC)0.0);
5949
5950 RHS->norm2(norms);
5951 out << "||RHS|| = " << std::setiosflags(std::ios::fixed) << std::setprecision(10) << norms[0] << std::endl;
5952
5953 out << "solve with zero initial guess" << std::endl;
5954 Teuchos::Array<magnitude_type> initialNorms(1);
5955 X->norm2(initialNorms);
5956 out << " ||X_initial|| = " << std::setiosflags(std::ios::fixed) << std::setprecision(10) << initialNorms[0] << std::endl;
5957
5958 inSmoother->Apply(*X, *RHS, true); // zero initial guess
5959
5960 Teuchos::Array<magnitude_type> finalNorms(1);
5961 X->norm2(finalNorms);
5962 Teuchos::Array<magnitude_type> residualNorm1 = Utilities::ResidualNorm(*reorderedA, *X, *RHS);
5963 out << " ||Residual_final|| = " << std::setiosflags(std::ios::fixed) << std::setprecision(20) << residualNorm1[0] << std::endl;
5964 out << " ||X_final|| = " << std::setiosflags(std::ios::fixed) << std::setprecision(10) << finalNorms[0] << std::endl;
5965
5966 magnitude_type tol = 50. * Teuchos::ScalarTraits<Scalar>::eps();
5967
5968 TEUCHOS_TEST_COMPARE(residualNorm1[0], <, tol, out, success);
5969 TEUCHOS_TEST_COMPARE(finalNorms[0] - Teuchos::ScalarTraits<Scalar>::magnitude(Teuchos::ScalarTraits<Scalar>::one()), <, tol, out, success);
5970 } // end useTpetra
5971 }
5972
5973 TEUCHOS_UNIT_TEST_TEMPLATE_4_DECL(BlockedSmoother, NestedI2I01II_Indef_Setup_Apply2, Scalar, LocalOrdinal, GlobalOrdinal, Node) {
5974 #include <MueLu_UseShortNames.hpp>
5975 MUELU_TESTING_SET_OSTREAM;
5976 MUELU_TESTING_LIMIT_SCOPE(Scalar, GlobalOrdinal, Node);
5977 // TODO test only Tpetra because of Ifpack2 smoother!
5978 MUELU_TEST_ONLY_FOR(Xpetra::UseTpetra) {
5979 RCP<const Teuchos::Comm<int> > comm = Parameters::getDefaultComm();
5980 Xpetra::UnderlyingLib lib = MueLuTests::TestHelpers::Parameters::getLib();
5981
5982 Teuchos::RCP<const BlockedCrsMatrix> bop = TestHelpers::TestFactory<Scalar, LocalOrdinal, GlobalOrdinal, Node>::CreateBlockDiagonalExampleMatrix(lib, 3, comm);
5983 Teuchos::RCP<const Matrix> Aconst = Teuchos::rcp_dynamic_cast<const Matrix>(bop);
5984 Teuchos::RCP<Matrix> A = Teuchos::rcp_const_cast<Matrix>(Aconst);
5985
5986 // I don't use the testApply infrastructure because it has no provision for an initial guess.
5987 Level level;
5988 TestHelpers::TestFactory<Scalar, LocalOrdinal, GlobalOrdinal, Node>::createSingleLevelHierarchy(level);
5989 level.Set("A", A);
5990
5991 // Test ReorderBlockAFactory
5992 Teuchos::RCP<ReorderBlockAFactory> rAFact = Teuchos::rcp(new ReorderBlockAFactory());
5993 rAFact->SetFactory("A", MueLu::NoFactory::getRCP());
5994 rAFact->SetParameter(std::string("Reorder Type"), Teuchos::ParameterEntry(std::string("[ 2 [0 1]]")));
5995
5996 //////////////////////////////////////////////////////////////////////
5997 // Smoothers
5998 RCP<IndefBlockedDiagonalSmoother> smootherPrototype = rcp(new IndefBlockedDiagonalSmoother());
5999 smootherPrototype->SetFactory("A", rAFact);
6000 smootherPrototype->SetParameter("Sweeps", Teuchos::ParameterEntry(Teuchos::as<LocalOrdinal>(1)));
6001 smootherPrototype->SetParameter("Damping factor", Teuchos::ParameterEntry(Teuchos::as<Scalar>(1.0)));
6002
6003 std::vector<RCP<SubBlockAFactory> > sA(1, Teuchos::null);
6004 std::vector<RCP<SmootherFactory> > sF(2, Teuchos::null);
6005 std::vector<RCP<FactoryManager> > sM(2, Teuchos::null);
6006
6007 // prediction
6008 std::string strInfo = std::string("{ 1 }");
6009 sA[0] = rcp(new SubBlockAFactory());
6010 sA[0]->SetFactory("A", rAFact);
6011 sA[0]->SetParameter("block row", Teuchos::ParameterEntry(0));
6012 sA[0]->SetParameter("block col", Teuchos::ParameterEntry(0));
6013 sA[0]->SetParameter("Range map: Striding info", Teuchos::ParameterEntry(strInfo));
6014 sA[0]->SetParameter("Domain map: Striding info", Teuchos::ParameterEntry(strInfo));
6015
6016 RCP<SmootherPrototype> smoProtoCorrect = rcp(new Ifpack2Smoother(std::string("RELAXATION"), Teuchos::ParameterList(), 0));
6017 smoProtoCorrect->SetFactory("A", sA[0]);
6018 sF[0] = rcp(new SmootherFactory(smoProtoCorrect));
6019
6020 sM[0] = rcp(new FactoryManager());
6021 sM[0]->SetFactory("A", sA[0]);
6022 sM[0]->SetFactory("Smoother", sF[0]);
6023 sM[0]->SetIgnoreUserData(true);
6024
6025 smootherPrototype->AddFactoryManager(sM[0], 0);
6026
6027 // correction
6028 // define SchurComplement Factory
6029 // SchurComp gets a RCP to AFact_ which has to be the 2x2 blocked operator
6030 // It stores the resulting SchurComplement operator as "A" generated by the SchurComplementFactory
6031 // Instead of F^{-1} it uses the approximation \hat{F}^{-1} with \hat{F} = diag(F)
6032 RCP<InverseApproximationFactory> AinvFact = Teuchos::rcp(new InverseApproximationFactory());
6033 AinvFact->SetFactory("A", rAFact);
6034
6035 RCP<SchurComplementFactory> SFact = Teuchos::rcp(new SchurComplementFactory());
6036 SFact->SetParameter("omega", Teuchos::ParameterEntry(Teuchos::as<Scalar>(1.0))); // for Simple, omega is always 1.0 in the SchurComplement
6037 SFact->SetFactory("A", rAFact);
6038 SFact->SetFactory("Ainv", AinvFact);
6039
6040 // create a 2x2 block smoother for the prediction eq.
6041 RCP<IndefBlockedDiagonalSmoother> smoProtoPredict = Teuchos::rcp(new IndefBlockedDiagonalSmoother());
6042 smoProtoPredict->SetParameter("Sweeps", Teuchos::ParameterEntry(1));
6043 smoProtoPredict->SetParameter("Damping factor", Teuchos::ParameterEntry(Teuchos::as<Scalar>(1.0)));
6044 smoProtoPredict->SetFactory("A", SFact);
6045
6046 for (int l = 0; l < 2; l++) {
6047 Teuchos::RCP<SubBlockAFactory> ssA = rcp(new SubBlockAFactory());
6048 ssA->SetFactory("A", SFact);
6049 ssA->SetParameter("block row", Teuchos::ParameterEntry(l)); // local block indices relative to size of blocked operator
6050 ssA->SetParameter("block col", Teuchos::ParameterEntry(l));
6051 ssA->SetParameter("Range map: Striding info", Teuchos::ParameterEntry(strInfo));
6052 ssA->SetParameter("Domain map: Striding info", Teuchos::ParameterEntry(strInfo));
6053 RCP<SmootherPrototype> ssP = rcp(new Ifpack2Smoother(std::string("RELAXATION"), Teuchos::ParameterList(), 0));
6054 ssP->SetFactory("A", ssA);
6055 Teuchos::RCP<SmootherFactory> ssF = Teuchos::rcp(new SmootherFactory(ssP));
6056 Teuchos::RCP<FactoryManager> ssM = Teuchos::rcp(new FactoryManager());
6057 ssM->SetFactory("A", ssA);
6058 ssM->SetFactory("Smoother", ssF);
6059 ssM->SetIgnoreUserData(true);
6060 smoProtoPredict->AddFactoryManager(ssM, l);
6061 }
6062
6063 sF[1] = rcp(new SmootherFactory(smoProtoPredict));
6064
6065 sM[1] = rcp(new FactoryManager());
6066 sM[1]->SetFactory("A", SFact);
6067 sM[1]->SetFactory("Smoother", sF[1]);
6068 sM[1]->SetIgnoreUserData(true);
6069
6070 smootherPrototype->AddFactoryManager(sM[1], 1);
6071
6072 RCP<SmootherFactory> smootherFact = rcp(new SmootherFactory(smootherPrototype));
6073
6074 // main factory manager
6075 FactoryManager M;
6076 M.SetFactory("Smoother", smootherFact);
6077 M.SetFactory("A", rAFact);
6078
6079 MueLu::SetFactoryManager SFM(Teuchos::rcpFromRef(level), Teuchos::rcpFromRef(M));
6080
6081 // request block smoother (and all dependencies) on level
6082 level.Request("A", rAFact.get());
6083 level.Request("Smoother", smootherFact.get());
6084 level.Request("PreSmoother", smootherFact.get());
6085 level.Request("PostSmoother", smootherFact.get());
6086
6087 // smootherFact->DeclareInput(level);
6088 smootherFact->Build(level);
6089
6090 level.print(std::cout, Teuchos::VERB_EXTREME);
6091
6092 RCP<SmootherBase> inSmoother = level.Get<RCP<SmootherBase> >("PreSmoother", smootherFact.get());
6093
6094 RCP<Matrix> reorderedA = level.Get<RCP<Matrix> >("A", rAFact.get());
6095 RCP<ReorderedBlockedCrsMatrix> reorderedbA = Teuchos::rcp_dynamic_cast<ReorderedBlockedCrsMatrix>(reorderedA);
6096
6097 TEST_EQUALITY(reorderedbA->Rows(), 2);
6098 TEST_EQUALITY(reorderedbA->Cols(), 2);
6099
6100 RCP<BlockedMultiVector> bX = Teuchos::rcp(new BlockedMultiVector(bop->getBlockedDomainMap(), 1, true));
6101 RCP<BlockedMultiVector> bRHS = Teuchos::rcp(new BlockedMultiVector(bop->getBlockedRangeMap(), 1, true));
6102
6103 RCP<MultiVector> X = bX->Merge();
6104 RCP<MultiVector> RHS = bRHS->Merge();
6105
6106 // apply simple smoother
6107 RHS->putScalar((SC)1.0);
6108 X->putScalar((SC)0.0);
6109
6110 // solve system
6111 inSmoother->Apply(*X, *RHS, true); // zero initial guess
6112
6113 Teuchos::ArrayRCP<const Scalar> xdata = X->getData(0);
6114 bool bCheck = true;
6115 for (size_t i = 0; i < X->getLocalLength(); i++) {
6116 if (i < 5) {
6117 if (xdata[i] != (SC)1.0) bCheck = false;
6118 }
6119 if (i >= 5 && i < 10) {
6120 if (xdata[i] != (SC)0.5) bCheck = false;
6121 }
6122 if (i >= 10 && i < 20) {
6123 if (xdata[i] != (SC)(1.0 / 3.0)) bCheck = false;
6124 }
6125 }
6126 TEST_EQUALITY(bCheck, true);
6127
6128 // Random X
6129 {
6130 Teuchos::RCP<const Xpetra::BlockReorderManager> brm = Xpetra::blockedReorderFromString("[ 2 [0 1]]");
6131 RCP<BlockedMultiVector> test = Teuchos::rcp_dynamic_cast<BlockedMultiVector>(buildReorderedBlockedMultiVector(brm, bX));
6132 bX.swap(test);
6133 test = Teuchos::rcp_dynamic_cast<BlockedMultiVector>(buildReorderedBlockedMultiVector(brm, bRHS));
6134 bRHS.swap(test);
6135 }
6136
6137 bRHS->putScalar((SC)1.0);
6138 bX->setSeed(846930886);
6139 bX->randomize();
6140
6141 RHS = bRHS->Merge();
6142 X = bX->Merge();
6143
6144 typedef typename Teuchos::ScalarTraits<Scalar>::magnitudeType magnitude_type;
6145
6146 // Normalize X
6147 Array<magnitude_type> norms(1);
6148 X->norm2(norms);
6149 X->scale(1 / norms[0]);
6150
6151 // Compute RHS corresponding to X
6152 bop->apply(*X, *RHS, Teuchos::NO_TRANS, (SC)1.0, (SC)0.0);
6153
6154 // Reset X to 0
6155 X->putScalar((SC)0.0);
6156
6157 RHS->norm2(norms);
6158 out << "||RHS|| = " << std::setiosflags(std::ios::fixed) << std::setprecision(10) << norms[0] << std::endl;
6159
6160 out << "solve with zero initial guess" << std::endl;
6161 Teuchos::Array<magnitude_type> initialNorms(1);
6162 X->norm2(initialNorms);
6163 out << " ||X_initial|| = " << std::setiosflags(std::ios::fixed) << std::setprecision(10) << initialNorms[0] << std::endl;
6164
6165 inSmoother->Apply(*X, *RHS, true); // zero initial guess
6166
6167 Teuchos::Array<magnitude_type> finalNorms(1);
6168 X->norm2(finalNorms);
6169 Teuchos::Array<magnitude_type> residualNorm1 = Utilities::ResidualNorm(*reorderedA, *X, *RHS);
6170 out << " ||Residual_final|| = " << std::setiosflags(std::ios::fixed) << std::setprecision(20) << residualNorm1[0] << std::endl;
6171 out << " ||X_final|| = " << std::setiosflags(std::ios::fixed) << std::setprecision(10) << finalNorms[0] << std::endl;
6172
6173 magnitude_type tol = 50. * Teuchos::ScalarTraits<Scalar>::eps();
6174
6175 TEUCHOS_TEST_COMPARE(residualNorm1[0], <, tol, out, success);
6176 TEUCHOS_TEST_COMPARE(finalNorms[0] - Teuchos::ScalarTraits<Scalar>::magnitude(Teuchos::ScalarTraits<Scalar>::one()), <, tol, out, success);
6177 } // end useTpetra
6178 }
6179
6180 TEUCHOS_UNIT_TEST_TEMPLATE_4_DECL(BlockedSmoother, NestedI2I01II_Thyra_Indef_Setup_Apply, Scalar, LocalOrdinal, GlobalOrdinal, Node) {
6181 #include <MueLu_UseShortNames.hpp>
6182 MUELU_TESTING_SET_OSTREAM;
6183 MUELU_TESTING_LIMIT_SCOPE(Scalar, GlobalOrdinal, Node);
6184 // TODO test only Tpetra because of Ifpack2 smoother!
6185 MUELU_TEST_ONLY_FOR(Xpetra::UseTpetra) {
6186 RCP<const Teuchos::Comm<int> > comm = Parameters::getDefaultComm();
6187 Xpetra::UnderlyingLib lib = MueLuTests::TestHelpers::Parameters::getLib();
6188
6189 Teuchos::RCP<const BlockedCrsMatrix> bop = TestHelpers::TestFactory<Scalar, LocalOrdinal, GlobalOrdinal, Node>::CreateBlockDiagonalExampleMatrixThyra(lib, 3, comm);
6190 Teuchos::RCP<const Matrix> Aconst = Teuchos::rcp_dynamic_cast<const Matrix>(bop);
6191 Teuchos::RCP<Matrix> A = Teuchos::rcp_const_cast<Matrix>(Aconst);
6192
6193 // I don't use the testApply infrastructure because it has no provision for an initial guess.
6194 Level level;
6195 TestHelpers::TestFactory<Scalar, LocalOrdinal, GlobalOrdinal, Node>::createSingleLevelHierarchy(level);
6196 level.Set("A", A);
6197
6198 // Test ReorderBlockAFactory
6199 Teuchos::RCP<ReorderBlockAFactory> rAFact = Teuchos::rcp(new ReorderBlockAFactory());
6200 rAFact->SetFactory("A", MueLu::NoFactory::getRCP());
6201 rAFact->SetParameter(std::string("Reorder Type"), Teuchos::ParameterEntry(std::string("[ 2 [0 1]]")));
6202
6203 //////////////////////////////////////////////////////////////////////
6204 // Smoothers
6205 RCP<IndefBlockedDiagonalSmoother> smootherPrototype = rcp(new IndefBlockedDiagonalSmoother());
6206 smootherPrototype->SetFactory("A", rAFact);
6207 smootherPrototype->SetParameter("Sweeps", Teuchos::ParameterEntry(Teuchos::as<LocalOrdinal>(1)));
6208 smootherPrototype->SetParameter("Damping factor", Teuchos::ParameterEntry(Teuchos::as<Scalar>(1.0)));
6209
6210 std::vector<RCP<SubBlockAFactory> > sA(1, Teuchos::null);
6211 std::vector<RCP<SmootherFactory> > sF(2, Teuchos::null);
6212 std::vector<RCP<FactoryManager> > sM(2, Teuchos::null);
6213
6214 // prediction
6215 std::string strInfo = std::string("{ 1 }");
6216 sA[0] = rcp(new SubBlockAFactory());
6217 sA[0]->SetFactory("A", rAFact);
6218 sA[0]->SetParameter("block row", Teuchos::ParameterEntry(0));
6219 sA[0]->SetParameter("block col", Teuchos::ParameterEntry(0));
6220 sA[0]->SetParameter("Range map: Striding info", Teuchos::ParameterEntry(strInfo));
6221 sA[0]->SetParameter("Domain map: Striding info", Teuchos::ParameterEntry(strInfo));
6222
6223 RCP<SmootherPrototype> smoProtoCorrect = rcp(new Ifpack2Smoother(std::string("RELAXATION"), Teuchos::ParameterList(), 0));
6224 smoProtoCorrect->SetFactory("A", sA[0]);
6225 sF[0] = rcp(new SmootherFactory(smoProtoCorrect));
6226
6227 sM[0] = rcp(new FactoryManager());
6228 sM[0]->SetFactory("A", sA[0]);
6229 sM[0]->SetFactory("Smoother", sF[0]);
6230 sM[0]->SetIgnoreUserData(true);
6231
6232 smootherPrototype->AddFactoryManager(sM[0], 0);
6233
6234 // correction
6235 // define SchurComplement Factory
6236 // SchurComp gets a RCP to AFact_ which has to be the 2x2 blocked operator
6237 // It stores the resulting SchurComplement operator as "A" generated by the SchurComplementFactory
6238 // Instead of F^{-1} it uses the approximation \hat{F}^{-1} with \hat{F} = diag(F)
6239 RCP<InverseApproximationFactory> AinvFact = Teuchos::rcp(new InverseApproximationFactory());
6240 AinvFact->SetFactory("A", rAFact);
6241
6242 RCP<SchurComplementFactory> SFact = Teuchos::rcp(new SchurComplementFactory());
6243 SFact->SetParameter("omega", Teuchos::ParameterEntry(Teuchos::as<Scalar>(1.0))); // for Simple, omega is always 1.0 in the SchurComplement
6244 SFact->SetFactory("A", rAFact);
6245 SFact->SetFactory("Ainv", AinvFact);
6246
6247 // create a 2x2 SIMPLE for the prediction eq.
6248 RCP<IndefBlockedDiagonalSmoother> smoProtoPredict = Teuchos::rcp(new IndefBlockedDiagonalSmoother());
6249 smoProtoPredict->SetParameter("Sweeps", Teuchos::ParameterEntry(1));
6250 smoProtoPredict->SetParameter("Damping factor", Teuchos::ParameterEntry(Teuchos::as<Scalar>(1.0)));
6251 smoProtoPredict->SetFactory("A", SFact);
6252
6253 for (int l = 0; l < 2; l++) {
6254 Teuchos::RCP<SubBlockAFactory> ssA = rcp(new SubBlockAFactory());
6255 ssA->SetFactory("A", SFact);
6256 ssA->SetParameter("block row", Teuchos::ParameterEntry(l)); // local block indices relative to size of blocked operator
6257 ssA->SetParameter("block col", Teuchos::ParameterEntry(l));
6258 ssA->SetParameter("Range map: Striding info", Teuchos::ParameterEntry(strInfo));
6259 ssA->SetParameter("Domain map: Striding info", Teuchos::ParameterEntry(strInfo));
6260 RCP<SmootherPrototype> ssP = rcp(new Ifpack2Smoother(std::string("RELAXATION"), Teuchos::ParameterList(), 0));
6261 ssP->SetFactory("A", ssA);
6262 Teuchos::RCP<SmootherFactory> ssF = Teuchos::rcp(new SmootherFactory(ssP));
6263 Teuchos::RCP<FactoryManager> ssM = Teuchos::rcp(new FactoryManager());
6264 ssM->SetFactory("A", ssA);
6265 ssM->SetFactory("Smoother", ssF);
6266 ssM->SetIgnoreUserData(true);
6267 smoProtoPredict->AddFactoryManager(ssM, l);
6268 }
6269
6270 sF[1] = rcp(new SmootherFactory(smoProtoPredict));
6271
6272 sM[1] = rcp(new FactoryManager());
6273 sM[1]->SetFactory("A", SFact);
6274 sM[1]->SetFactory("Smoother", sF[1]);
6275 sM[1]->SetIgnoreUserData(true);
6276
6277 smootherPrototype->AddFactoryManager(sM[1], 1);
6278
6279 RCP<SmootherFactory> smootherFact = rcp(new SmootherFactory(smootherPrototype));
6280
6281 // main factory manager
6282 FactoryManager M;
6283 M.SetFactory("Smoother", smootherFact);
6284 M.SetFactory("A", rAFact);
6285
6286 MueLu::SetFactoryManager SFM(Teuchos::rcpFromRef(level), Teuchos::rcpFromRef(M));
6287
6288 // request BGS smoother (and all dependencies) on level
6289 level.Request("A", rAFact.get());
6290 level.Request("Smoother", smootherFact.get());
6291 level.Request("PreSmoother", smootherFact.get());
6292 level.Request("PostSmoother", smootherFact.get());
6293
6294 // smootherFact->DeclareInput(level);
6295 smootherFact->Build(level);
6296
6297 level.print(std::cout, Teuchos::VERB_EXTREME);
6298
6299 RCP<SmootherBase> inSmoother = level.Get<RCP<SmootherBase> >("PreSmoother", smootherFact.get());
6300
6301 RCP<Matrix> reorderedA = level.Get<RCP<Matrix> >("A", rAFact.get());
6302 RCP<BlockedCrsMatrix> reorderedbA = Teuchos::rcp_dynamic_cast<BlockedCrsMatrix>(reorderedA);
6303
6304 TEST_EQUALITY(reorderedbA->Rows(), 2);
6305 TEST_EQUALITY(reorderedbA->Cols(), 2);
6306
6307 RCP<MultiVector> X = MultiVectorFactory::Build(reorderedA->getDomainMap(), 1);
6308 RCP<MultiVector> RHS = MultiVectorFactory::Build(reorderedA->getRangeMap(), 1);
6309
6310 // apply simple smoother
6311 RHS->putScalar((SC)1.0);
6312 X->putScalar((SC)0.0);
6313
6314 // solve system
6315 inSmoother->Apply(*X, *RHS, true); // zero initial guess
6316 RCP<BlockedMultiVector> bX = Teuchos::rcp_dynamic_cast<BlockedMultiVector>(X);
6317 TEST_EQUALITY(bX.is_null(), false);
6318 RCP<MultiVector> XX = bX->Merge();
6319 Teuchos::ArrayRCP<const Scalar> xdata = XX->getData(0);
6320 bool bCheck = true;
6321 for (size_t i = 0; i < XX->getLocalLength(); i++) {
6322 if (i < 10) {
6323 if (xdata[i] != (SC)(1.0 / 3.0)) bCheck = false;
6324 }
6325 if (i >= 10 && i < 15) {
6326 if (xdata[i] != (SC)1.0) bCheck = false;
6327 }
6328 if (i >= 15 && i < 20) {
6329 if (xdata[i] != (SC)0.5) bCheck = false;
6330 }
6331 }
6332 TEST_EQUALITY(bCheck, true);
6333
6334 // Random X
6335 X->setSeed(846930886);
6336 X->randomize();
6337
6338 typedef typename Teuchos::ScalarTraits<Scalar>::magnitudeType magnitude_type;
6339
6340 // Normalize X
6341 Array<magnitude_type> norms(1);
6342 X->norm2(norms);
6343 X->scale(1 / norms[0]);
6344
6345 // Compute RHS corresponding to X
6346 reorderedA->apply(*X, *RHS, Teuchos::NO_TRANS, (SC)1.0, (SC)0.0);
6347
6348 // Reset X to 0
6349 X->putScalar((SC)0.0);
6350
6351 RHS->norm2(norms);
6352 out << "||RHS|| = " << std::setiosflags(std::ios::fixed) << std::setprecision(10) << norms[0] << std::endl;
6353
6354 out << "solve with zero initial guess" << std::endl;
6355 Teuchos::Array<magnitude_type> initialNorms(1);
6356 X->norm2(initialNorms);
6357 out << " ||X_initial|| = " << std::setiosflags(std::ios::fixed) << std::setprecision(10) << initialNorms[0] << std::endl;
6358
6359 inSmoother->Apply(*X, *RHS, true); // zero initial guess
6360
6361 Teuchos::Array<magnitude_type> finalNorms(1);
6362 X->norm2(finalNorms);
6363 Teuchos::Array<magnitude_type> residualNorm1 = Utilities::ResidualNorm(*reorderedA, *X, *RHS);
6364 out << " ||Residual_final|| = " << std::setiosflags(std::ios::fixed) << std::setprecision(20) << residualNorm1[0] << std::endl;
6365 out << " ||X_final|| = " << std::setiosflags(std::ios::fixed) << std::setprecision(10) << finalNorms[0] << std::endl;
6366
6367 magnitude_type tol = 50. * Teuchos::ScalarTraits<Scalar>::eps();
6368
6369 TEUCHOS_TEST_COMPARE(residualNorm1[0], <, tol, out, success);
6370 TEUCHOS_TEST_COMPARE(finalNorms[0] - Teuchos::ScalarTraits<Scalar>::magnitude(Teuchos::ScalarTraits<Scalar>::one()), <, tol, out, success);
6371 } // end useTpetra
6372 }
6373
6374 TEUCHOS_UNIT_TEST_TEMPLATE_4_DECL(BlockedSmoother, NestedII01I2I_Thyra_Indef_Setup_Apply2, Scalar, LocalOrdinal, GlobalOrdinal, Node) {
6375 #include <MueLu_UseShortNames.hpp>
6376 MUELU_TESTING_SET_OSTREAM;
6377 MUELU_TESTING_LIMIT_SCOPE(Scalar, GlobalOrdinal, Node);
6378 // TODO test only Tpetra because of Ifpack2 smoother!
6379 MUELU_TEST_ONLY_FOR(Xpetra::UseTpetra) {
6380 RCP<const Teuchos::Comm<int> > comm = Parameters::getDefaultComm();
6381 Xpetra::UnderlyingLib lib = MueLuTests::TestHelpers::Parameters::getLib();
6382
6383 Teuchos::RCP<const BlockedCrsMatrix> bop = TestHelpers::TestFactory<Scalar, LocalOrdinal, GlobalOrdinal, Node>::CreateBlocked3x3MatrixThyra(*comm, lib);
6384 Teuchos::RCP<const Matrix> Aconst = Teuchos::rcp_dynamic_cast<const Matrix>(bop);
6385 Teuchos::RCP<Matrix> A = Teuchos::rcp_const_cast<Matrix>(Aconst);
6386
6387 // I don't use the testApply infrastructure because it has no provision for an initial guess.
6388 Level level;
6389 TestHelpers::TestFactory<Scalar, LocalOrdinal, GlobalOrdinal, Node>::createSingleLevelHierarchy(level);
6390 level.Set("A", A);
6391
6392 // Test ReorderBlockAFactory
6393 Teuchos::RCP<ReorderBlockAFactory> rAFact = Teuchos::rcp(new ReorderBlockAFactory());
6394 rAFact->SetFactory("A", MueLu::NoFactory::getRCP());
6395 rAFact->SetParameter(std::string("Reorder Type"), Teuchos::ParameterEntry(std::string("[ [0 1] 2]")));
6396
6397 //////////////////////////////////////////////////////////////////////
6398 // Smoothers
6399 RCP<IndefBlockedDiagonalSmoother> smootherPrototype = rcp(new IndefBlockedDiagonalSmoother());
6400 smootherPrototype->SetFactory("A", rAFact);
6401 smootherPrototype->SetParameter("Sweeps", Teuchos::ParameterEntry(Teuchos::as<LocalOrdinal>(15)));
6402 smootherPrototype->SetParameter("Damping factor", Teuchos::ParameterEntry(Teuchos::as<Scalar>(0.9)));
6403
6404 std::vector<RCP<SubBlockAFactory> > sA(1, Teuchos::null);
6405 std::vector<RCP<SmootherFactory> > sF(2, Teuchos::null);
6406 std::vector<RCP<FactoryManager> > sM(2, Teuchos::null);
6407
6408 // prediction
6409 std::string strInfo = std::string("{ 1 }");
6410 sA[0] = rcp(new SubBlockAFactory());
6411 sA[0]->SetFactory("A", rAFact);
6412 sA[0]->SetParameter("block row", Teuchos::ParameterEntry(0));
6413 sA[0]->SetParameter("block col", Teuchos::ParameterEntry(0));
6414 sA[0]->SetParameter("Range map: Striding info", Teuchos::ParameterEntry(strInfo));
6415 sA[0]->SetParameter("Domain map: Striding info", Teuchos::ParameterEntry(strInfo));
6416
6417 // create a 2x2 BGS for the prediction eq.
6418 RCP<BlockedGaussSeidelSmoother> smoProtoPredict = Teuchos::rcp(new BlockedGaussSeidelSmoother());
6419 smoProtoPredict->SetParameter("Sweeps", Teuchos::ParameterEntry(1));
6420 smoProtoPredict->SetFactory("A", sA[0]);
6421
6422 for (int l = 0; l < 2; l++) {
6423 Teuchos::RCP<SubBlockAFactory> ssA = rcp(new SubBlockAFactory());
6424 ssA->SetFactory("A", sA[0]);
6425 ssA->SetParameter("block row", Teuchos::ParameterEntry(l)); // local block indices relative to size of blocked operator
6426 ssA->SetParameter("block col", Teuchos::ParameterEntry(l));
6427 ssA->SetParameter("Range map: Striding info", Teuchos::ParameterEntry(strInfo));
6428 ssA->SetParameter("Domain map: Striding info", Teuchos::ParameterEntry(strInfo));
6429 RCP<SmootherPrototype> ssP = rcp(new Ifpack2Smoother(std::string("RELAXATION"), Teuchos::ParameterList(), 0));
6430 ssP->SetFactory("A", ssA);
6431 Teuchos::RCP<SmootherFactory> ssF = Teuchos::rcp(new SmootherFactory(ssP));
6432 Teuchos::RCP<FactoryManager> ssM = Teuchos::rcp(new FactoryManager());
6433 ssM->SetFactory("A", ssA);
6434 ssM->SetFactory("Smoother", ssF);
6435 ssM->SetIgnoreUserData(true);
6436 smoProtoPredict->AddFactoryManager(ssM, l);
6437 }
6438
6439 sF[0] = rcp(new SmootherFactory(smoProtoPredict));
6440
6441 sM[0] = rcp(new FactoryManager());
6442 sM[0]->SetFactory("A", sA[0]);
6443 sM[0]->SetFactory("Smoother", sF[0]);
6444 sM[0]->SetIgnoreUserData(true);
6445
6446 smootherPrototype->AddFactoryManager(sM[0], 0);
6447
6448 // correction
6449 // define SchurComplement Factory
6450 // SchurComp gets a RCP to AFact_ which has to be the 2x2 blocked operator
6451 // It stores the resulting SchurComplement operator as "A" generated by the SchurComplementFactory
6452 // Instead of F^{-1} it uses the approximation \hat{F}^{-1} with \hat{F} = diag(F)
6453 RCP<InverseApproximationFactory> AinvFact = Teuchos::rcp(new InverseApproximationFactory());
6454 AinvFact->SetFactory("A", rAFact);
6455
6456 RCP<SchurComplementFactory> SFact = Teuchos::rcp(new SchurComplementFactory());
6457 SFact->SetParameter("omega", Teuchos::ParameterEntry(Teuchos::as<Scalar>(1.0))); // for Simple, omega is always 1.0 in the SchurComplement
6458 SFact->SetFactory("A", rAFact);
6459 SFact->SetFactory("Ainv", AinvFact);
6460
6461 Teuchos::ParameterList paramList;
6462 paramList.set("relaxation: sweeps", Teuchos::as<LocalOrdinal>(30));
6463 paramList.set("relaxation: damping factor", Teuchos::as<Scalar>(0.9));
6464 RCP<SmootherPrototype> smoProtoCorrect = rcp(new Ifpack2Smoother(std::string("RELAXATION"), paramList, 0));
6465 smoProtoCorrect->SetFactory("A", SFact);
6466 sF[1] = rcp(new SmootherFactory(smoProtoCorrect));
6467
6468 sM[1] = rcp(new FactoryManager());
6469 sM[1]->SetFactory("A", SFact);
6470 sM[1]->SetFactory("Smoother", sF[1]);
6471 sM[1]->SetIgnoreUserData(true);
6472
6473 smootherPrototype->AddFactoryManager(sM[1], 1);
6474
6475 RCP<SmootherFactory> smootherFact = rcp(new SmootherFactory(smootherPrototype));
6476
6477 // main factory manager
6478 FactoryManager M;
6479 M.SetFactory("Smoother", smootherFact);
6480 M.SetFactory("A", rAFact);
6481
6482 MueLu::SetFactoryManager SFM(Teuchos::rcpFromRef(level), Teuchos::rcpFromRef(M));
6483
6484 // request BGS smoother (and all dependencies) on level
6485 level.Request("A", rAFact.get());
6486 level.Request("Smoother", smootherFact.get());
6487 level.Request("PreSmoother", smootherFact.get());
6488 level.Request("PostSmoother", smootherFact.get());
6489
6490 // smootherFact->DeclareInput(level);
6491 smootherFact->Build(level);
6492
6493 level.print(std::cout, Teuchos::VERB_EXTREME);
6494
6495 RCP<SmootherBase> inSmoother = level.Get<RCP<SmootherBase> >("PreSmoother", smootherFact.get());
6496
6497 RCP<Matrix> reorderedA = level.Get<RCP<Matrix> >("A", rAFact.get());
6498 RCP<BlockedCrsMatrix> reorderedbA = Teuchos::rcp_dynamic_cast<BlockedCrsMatrix>(reorderedA);
6499
6500 RCP<MultiVector> X = MultiVectorFactory::Build(reorderedA->getDomainMap(), 1);
6501 RCP<MultiVector> RHS = MultiVectorFactory::Build(reorderedA->getRangeMap(), 1);
6502
6503 // Random X
6504 X->setSeed(846930886);
6505 X->randomize();
6506
6507 typedef typename Teuchos::ScalarTraits<Scalar>::magnitudeType magnitude_type;
6508
6509 // Normalize X
6510 Array<magnitude_type> norms(1);
6511 X->norm2(norms);
6512 X->scale(1 / norms[0]);
6513
6514 // Compute RHS corresponding to X
6515 reorderedA->apply(*X, *RHS, Teuchos::NO_TRANS, (SC)1.0, (SC)0.0);
6516
6517 // Reset X to 0
6518 X->putScalar((SC)0.0);
6519
6520 RHS->norm2(norms);
6521 out << "||RHS|| = " << std::setiosflags(std::ios::fixed) << std::setprecision(10) << norms[0] << std::endl;
6522
6523 out << "solve with zero initial guess" << std::endl;
6524 Teuchos::Array<magnitude_type> initialNorms(1);
6525 X->norm2(initialNorms);
6526 out << " ||X_initial|| = " << std::setiosflags(std::ios::fixed) << std::setprecision(10) << initialNorms[0] << std::endl;
6527
6528 inSmoother->Apply(*X, *RHS, true); // zero initial guess
6529
6530 Teuchos::Array<magnitude_type> finalNorms(1);
6531 X->norm2(finalNorms);
6532 Teuchos::Array<magnitude_type> residualNorm1 = Utilities::ResidualNorm(*reorderedA, *X, *RHS);
6533 out << " ||Residual_final|| = " << std::setiosflags(std::ios::fixed) << std::setprecision(20) << residualNorm1[0] << std::endl;
6534 out << " ||X_final|| = " << std::setiosflags(std::ios::fixed) << std::setprecision(10) << finalNorms[0] << std::endl;
6535
6536 TEUCHOS_TEST_COMPARE(residualNorm1[0], <, 11e-3, out, success);
6537 TEUCHOS_TEST_COMPARE(residualNorm1[0], >, 7e-3, out, success);
6538 } // end UseTpetra
6539 }
6540
6541 TEUCHOS_UNIT_TEST_TEMPLATE_4_DECL(BlockedSmoother, NestedI0I21II_Thyra_Indef_Setup_Apply3, Scalar, LocalOrdinal, GlobalOrdinal, Node) {
6542 #include <MueLu_UseShortNames.hpp>
6543 MUELU_TESTING_SET_OSTREAM;
6544 MUELU_TESTING_LIMIT_SCOPE(Scalar, GlobalOrdinal, Node);
6545 // TODO test only Tpetra because of Ifpack2 smoother!
6546 MUELU_TEST_ONLY_FOR(Xpetra::UseTpetra) {
6547 RCP<const Teuchos::Comm<int> > comm = Parameters::getDefaultComm();
6548 Xpetra::UnderlyingLib lib = MueLuTests::TestHelpers::Parameters::getLib();
6549
6550 Teuchos::RCP<const BlockedCrsMatrix> bop = TestHelpers::TestFactory<Scalar, LocalOrdinal, GlobalOrdinal, Node>::CreateBlocked3x3MatrixThyra(*comm, lib);
6551 Teuchos::RCP<const Matrix> Aconst = Teuchos::rcp_dynamic_cast<const Matrix>(bop);
6552 Teuchos::RCP<Matrix> A = Teuchos::rcp_const_cast<Matrix>(Aconst);
6553
6554 // I don't use the testApply infrastructure because it has no provision for an initial guess.
6555 Level level;
6556 TestHelpers::TestFactory<Scalar, LocalOrdinal, GlobalOrdinal, Node>::createSingleLevelHierarchy(level);
6557 level.Set("A", A);
6558
6559 // Test ReorderBlockAFactory
6560 Teuchos::RCP<ReorderBlockAFactory> rAFact = Teuchos::rcp(new ReorderBlockAFactory());
6561 rAFact->SetFactory("A", MueLu::NoFactory::getRCP());
6562 rAFact->SetParameter(std::string("Reorder Type"), Teuchos::ParameterEntry(std::string("[ 0 [ 2 1 ]]")));
6563
6564 //////////////////////////////////////////////////////////////////////
6565 // Smoothers
6566 RCP<IndefBlockedDiagonalSmoother> smootherPrototype = rcp(new IndefBlockedDiagonalSmoother());
6567 smootherPrototype->SetFactory("A", rAFact);
6568 smootherPrototype->SetParameter("Sweeps", Teuchos::ParameterEntry(Teuchos::as<LocalOrdinal>(15)));
6569 smootherPrototype->SetParameter("Damping factor", Teuchos::ParameterEntry(Teuchos::as<Scalar>(0.9)));
6570
6571 std::vector<RCP<SubBlockAFactory> > sA(1, Teuchos::null);
6572 std::vector<RCP<SmootherFactory> > sF(2, Teuchos::null);
6573 std::vector<RCP<FactoryManager> > sM(2, Teuchos::null);
6574
6575 // prediction
6576 std::string strInfo = std::string("{ 1 }");
6577 sA[0] = rcp(new SubBlockAFactory());
6578 sA[0]->SetFactory("A", rAFact);
6579 sA[0]->SetParameter("block row", Teuchos::ParameterEntry(0));
6580 sA[0]->SetParameter("block col", Teuchos::ParameterEntry(0));
6581 sA[0]->SetParameter("Range map: Striding info", Teuchos::ParameterEntry(strInfo));
6582 sA[0]->SetParameter("Domain map: Striding info", Teuchos::ParameterEntry(strInfo));
6583
6584 RCP<SmootherPrototype> smoProtoPredict = rcp(new Ifpack2Smoother(std::string("RELAXATION"), Teuchos::ParameterList(), 0));
6585 smoProtoPredict->SetFactory("A", sA[0]);
6586 sF[0] = rcp(new SmootherFactory(smoProtoPredict));
6587
6588 sM[0] = rcp(new FactoryManager());
6589 sM[0]->SetFactory("A", sA[0]);
6590 sM[0]->SetFactory("Smoother", sF[0]);
6591 sM[0]->SetIgnoreUserData(true);
6592
6593 smootherPrototype->AddFactoryManager(sM[0], 0);
6594
6595 // correction
6596 // define SchurComplement Factory
6597 // SchurComp gets a RCP to AFact_ which has to be the 2x2 blocked operator
6598 // It stores the resulting SchurComplement operator as "A" generated by the SchurComplementFactory
6599 // Instead of F^{-1} it uses the approximation \hat{F}^{-1} with \hat{F} = diag(F)
6600 RCP<InverseApproximationFactory> AinvFact = Teuchos::rcp(new InverseApproximationFactory());
6601 AinvFact->SetFactory("A", rAFact);
6602
6603 RCP<SchurComplementFactory> SFact = Teuchos::rcp(new SchurComplementFactory());
6604 SFact->SetParameter("omega", Teuchos::ParameterEntry(Teuchos::as<Scalar>(1.0))); // for Simple, omega is always 1.0 in the SchurComplement
6605 SFact->SetFactory("A", rAFact);
6606 SFact->SetFactory("Ainv", AinvFact);
6607
6608 // create a 2x2 Simple for the prediction eq.
6609 RCP<IndefBlockedDiagonalSmoother> smoProtoCorrect = Teuchos::rcp(new IndefBlockedDiagonalSmoother());
6610 smoProtoCorrect->SetParameter("Sweeps", Teuchos::ParameterEntry(1));
6611 smoProtoCorrect->SetParameter("Damping factor", Teuchos::ParameterEntry(Teuchos::as<Scalar>(1.0)));
6612 smoProtoCorrect->SetFactory("A", SFact);
6613
6614 for (int l = 0; l < 2; l++) {
6615 Teuchos::RCP<SubBlockAFactory> ssA = rcp(new SubBlockAFactory());
6616 ssA->SetFactory("A", SFact);
6617 ssA->SetParameter("block row", Teuchos::ParameterEntry(l)); // local block indices relative to size of blocked operator
6618 ssA->SetParameter("block col", Teuchos::ParameterEntry(l));
6619 ssA->SetParameter("Range map: Striding info", Teuchos::ParameterEntry(strInfo));
6620 ssA->SetParameter("Domain map: Striding info", Teuchos::ParameterEntry(strInfo));
6621 RCP<SmootherPrototype> ssP = rcp(new Ifpack2Smoother(std::string("RELAXATION"), Teuchos::ParameterList(), 0));
6622 ssP->SetFactory("A", ssA);
6623 Teuchos::RCP<SmootherFactory> ssF = Teuchos::rcp(new SmootherFactory(ssP));
6624 Teuchos::RCP<FactoryManager> ssM = Teuchos::rcp(new FactoryManager());
6625 ssM->SetFactory("A", ssA);
6626 ssM->SetFactory("Smoother", ssF);
6627 ssM->SetIgnoreUserData(true);
6628 smoProtoCorrect->AddFactoryManager(ssM, l);
6629 }
6630
6631 sF[1] = rcp(new SmootherFactory(smoProtoCorrect));
6632
6633 sM[1] = rcp(new FactoryManager());
6634 sM[1]->SetFactory("A", SFact);
6635 sM[1]->SetFactory("Smoother", sF[1]);
6636 sM[1]->SetIgnoreUserData(true);
6637
6638 smootherPrototype->AddFactoryManager(sM[1], 1);
6639
6640 RCP<SmootherFactory> smootherFact = rcp(new SmootherFactory(smootherPrototype));
6641
6642 // main factory manager
6643 FactoryManager M;
6644 M.SetFactory("Smoother", smootherFact);
6645 M.SetFactory("A", rAFact);
6646
6647 MueLu::SetFactoryManager SFM(Teuchos::rcpFromRef(level), Teuchos::rcpFromRef(M));
6648
6649 // request BGS smoother (and all dependencies) on level
6650 level.Request("A", rAFact.get());
6651 level.Request("Smoother", smootherFact.get());
6652 level.Request("PreSmoother", smootherFact.get());
6653 level.Request("PostSmoother", smootherFact.get());
6654
6655 // smootherFact->DeclareInput(level);
6656 smootherFact->Build(level);
6657
6658 level.print(std::cout, Teuchos::VERB_EXTREME);
6659
6660 RCP<SmootherBase> inSmoother = level.Get<RCP<SmootherBase> >("PreSmoother", smootherFact.get());
6661
6662 RCP<Matrix> reorderedA = level.Get<RCP<Matrix> >("A", rAFact.get());
6663 RCP<BlockedCrsMatrix> reorderedbA = Teuchos::rcp_dynamic_cast<BlockedCrsMatrix>(reorderedA);
6664
6665 RCP<MultiVector> X = MultiVectorFactory::Build(reorderedA->getDomainMap(), 1);
6666 RCP<MultiVector> RHS = MultiVectorFactory::Build(reorderedA->getRangeMap(), 1);
6667
6668 // Random X
6669 X->setSeed(846930886);
6670 X->randomize();
6671
6672 typedef typename Teuchos::ScalarTraits<Scalar>::magnitudeType magnitude_type;
6673
6674 // Normalize X
6675 Array<magnitude_type> norms(1);
6676 X->norm2(norms);
6677 X->scale(1 / norms[0]);
6678
6679 // Compute RHS corresponding to X
6680 reorderedA->apply(*X, *RHS, Teuchos::NO_TRANS, (SC)1.0, (SC)0.0);
6681
6682 // Reset X to 0
6683 X->putScalar((SC)0.0);
6684
6685 RHS->norm2(norms);
6686 out << "||RHS|| = " << std::setiosflags(std::ios::fixed) << std::setprecision(10) << norms[0] << std::endl;
6687
6688 out << "solve with zero initial guess" << std::endl;
6689 Teuchos::Array<magnitude_type> initialNorms(1);
6690 X->norm2(initialNorms);
6691 out << " ||X_initial|| = " << std::setiosflags(std::ios::fixed) << std::setprecision(10) << initialNorms[0] << std::endl;
6692
6693 inSmoother->Apply(*X, *RHS, true); // zero initial guess
6694
6695 Teuchos::Array<magnitude_type> finalNorms(1);
6696 X->norm2(finalNorms);
6697 Teuchos::Array<magnitude_type> residualNorm1 = Utilities::ResidualNorm(*reorderedA, *X, *RHS);
6698 out << " ||Residual_final|| = " << std::setiosflags(std::ios::fixed) << std::setprecision(20) << residualNorm1[0] << std::endl;
6699 out << " ||X_final|| = " << std::setiosflags(std::ios::fixed) << std::setprecision(10) << finalNorms[0] << std::endl;
6700
6701 TEUCHOS_TEST_COMPARE(residualNorm1[0], <, 25e-3, out, success);
6702 TEUCHOS_TEST_COMPARE(residualNorm1[0], >, 9e-3, out, success);
6703 } // end UseTpetra
6704 }
6705
6706 TEUCHOS_UNIT_TEST_TEMPLATE_4_DECL(BlockedSmoother, SplitReorder, Scalar, LocalOrdinal, GlobalOrdinal, Node) {
6707 #include <MueLu_UseShortNames.hpp>
6708 MUELU_TESTING_SET_OSTREAM;
6709 MUELU_TESTING_LIMIT_SCOPE(Scalar, GlobalOrdinal, Node);
6710 // TODO test only Tpetra because of Ifpack2 smoother!
6711 MUELU_TEST_ONLY_FOR(Xpetra::UseTpetra) {
6712 RCP<const Teuchos::Comm<int> > comm = Parameters::getDefaultComm();
6713 Xpetra::UnderlyingLib lib = MueLuTests::TestHelpers::Parameters::getLib();
6714
6715 Teuchos::RCP<const Matrix> Aconst = TestHelpers::TestFactory<Scalar, LocalOrdinal, GlobalOrdinal, Node>::Build2DPoisson(comm->getSize() * 3, -1, lib);
6716 Teuchos::RCP<Matrix> A = Teuchos::rcp_const_cast<Matrix>(Aconst);
6717
6718 // split local parts of row map
6719 Teuchos::RCP<const Map> map = A->getRowMap();
6720
6721 Teuchos::Array<GlobalOrdinal> myGids1;
6722 Teuchos::Array<GlobalOrdinal> myGids2;
6723 GlobalOrdinal count1 = 0;
6724 GlobalOrdinal count2 = 0;
6725 for (size_t i = 0; i < map->getLocalNumElements(); ++i) {
6726 const GlobalOrdinal gid = map->getGlobalElement(i);
6727 if (gid % 2 == 0) {
6728 myGids1.push_back(gid);
6729 count1++;
6730 } else {
6731 myGids2.push_back(gid);
6732 count2++;
6733 }
6734 }
6735 GlobalOrdinal gcount1 = 0;
6736 GlobalOrdinal gcount2 = 0;
6737 Teuchos::reduceAll(*comm, Teuchos::REDUCE_SUM, 1, &count1, &gcount1);
6738 Teuchos::reduceAll(*comm, Teuchos::REDUCE_SUM, 1, &count2, &gcount2);
6739 Teuchos::RCP<const Map> map1 = MapFactory::Build(lib, gcount1, myGids1(), 0, comm);
6740 Teuchos::RCP<const Map> map2 = MapFactory::Build(lib, gcount2, myGids2(), 0, comm);
6741
6742 // I don't use the testApply infrastructure because it has no provision for an initial guess.
6743 Level level;
6744 TestHelpers::TestFactory<Scalar, LocalOrdinal, GlobalOrdinal, Node>::createSingleLevelHierarchy(level);
6745 level.Set("A", A);
6746 level.Set("Map1", map1);
6747 level.Set("Map2", map2);
6748
6749 // Test ReorderBlockAFactory
6750 Teuchos::RCP<ReorderBlockAFactory> rAFact = Teuchos::rcp(new ReorderBlockAFactory());
6751 rAFact->SetFactory("A", MueLu::NoFactory::getRCP());
6752 rAFact->SetFactory("Map1", MueLu::NoFactory::getRCP());
6753 rAFact->SetFactory("Map2", MueLu::NoFactory::getRCP());
6754 rAFact->SetParameter(std::string("Reorder Type"), Teuchos::ParameterEntry(std::string("[ 0 1 ]")));
6755
6756 // request BGS smoother (and all dependencies) on level
6757 level.Request("A", rAFact.get());
6758
6759 // smootherFact->DeclareInput(level);
6760 rAFact->Build(level);
6761
6762 level.print(std::cout, Teuchos::VERB_EXTREME);
6763
6764 RCP<Matrix> reorderedA = level.Get<RCP<Matrix> >("A", rAFact.get());
6765 RCP<BlockedCrsMatrix> reorderedbA = Teuchos::rcp_dynamic_cast<BlockedCrsMatrix>(reorderedA);
6766
6767 TEST_EQUALITY(reorderedbA->Rows(), 2);
6768 TEST_EQUALITY(reorderedbA->Cols(), 2);
6769
6770 TEST_EQUALITY(reorderedbA->getRangeMapExtractor()->getFullMap()->isSameAs(*(A->getRowMap())), false);
6771 TEST_EQUALITY(reorderedbA->getDomainMapExtractor()->getFullMap()->isSameAs(*(A->getDomainMap())), false);
6772 TEUCHOS_TEST_COMPARE(std::abs(A->getFrobeniusNorm() - reorderedA->getFrobeniusNorm()), <, 1e4 * Teuchos::ScalarTraits<Scalar>::eps(), out, success);
6773 TEUCHOS_TEST_COMPARE(std::abs(A->getFrobeniusNorm() - reorderedbA->getFrobeniusNorm()), <, 1e4 * Teuchos::ScalarTraits<Scalar>::eps(), out, success);
6774 } // end UseTpetra
6775 }
6776
6777 #define MUELU_ETI_GROUP(SC, LO, GO, NO) \
6778 TEUCHOS_UNIT_TEST_TEMPLATE_4_INSTANT(BlockedSmoother, Jacobi_Setup_Apply, SC, LO, GO, NO) \
6779 TEUCHOS_UNIT_TEST_TEMPLATE_4_INSTANT(BlockedSmoother, BGS_Setup_Apply, SC, LO, GO, NO) \
6780 TEUCHOS_UNIT_TEST_TEMPLATE_4_INSTANT(BlockedSmoother, Reordered_BGS_Setup_Apply, SC, LO, GO, NO) \
6781 TEUCHOS_UNIT_TEST_TEMPLATE_4_INSTANT(BlockedSmoother, NestedII30II12II_BGS_Setup_Apply, SC, LO, GO, NO) \
6782 TEUCHOS_UNIT_TEST_TEMPLATE_4_INSTANT(BlockedSmoother, NestedII30II12II_BGS_Setup_Apply2, SC, LO, GO, NO) \
6783 TEUCHOS_UNIT_TEST_TEMPLATE_4_INSTANT(BlockedSmoother, Thyra_BGS_Setup_Apply, SC, LO, GO, NO) \
6784 TEUCHOS_UNIT_TEST_TEMPLATE_4_INSTANT(BlockedSmoother, Thyra_Nested_BGS_Setup_Apply, SC, LO, GO, NO) \
6785 TEUCHOS_UNIT_TEST_TEMPLATE_4_INSTANT(BlockedSmoother, Thyra_Nested_BGS_Setup_Apply2, SC, LO, GO, NO) \
6786 TEUCHOS_UNIT_TEST_TEMPLATE_4_INSTANT(BlockedSmoother, SIMPLE_Setup_Apply, SC, LO, GO, NO) \
6787 TEUCHOS_UNIT_TEST_TEMPLATE_4_INSTANT(BlockedSmoother, NestedII20I1I_SIMPLE_Setup_Apply, SC, LO, GO, NO) \
6788 TEUCHOS_UNIT_TEST_TEMPLATE_4_INSTANT(BlockedSmoother, NestedI2I01II_SIMPLE_Setup_Apply, SC, LO, GO, NO) \
6789 TEUCHOS_UNIT_TEST_TEMPLATE_4_INSTANT(BlockedSmoother, NestedI2I01II_SIMPLE_Setup_Apply2, SC, LO, GO, NO) \
6790 TEUCHOS_UNIT_TEST_TEMPLATE_4_INSTANT(BlockedSmoother, NestedII20I1I_Thyra_SIMPLE_Setup_Apply, SC, LO, GO, NO) \
6791 TEUCHOS_UNIT_TEST_TEMPLATE_4_INSTANT(BlockedSmoother, NestedI2I01II_Thyra_SIMPLE_Setup_Apply, SC, LO, GO, NO) \
6792 TEUCHOS_UNIT_TEST_TEMPLATE_4_INSTANT(BlockedSmoother, NestedII01I2I_Thyra_SIMPLE_Setup_Apply2, SC, LO, GO, NO) \
6793 TEUCHOS_UNIT_TEST_TEMPLATE_4_INSTANT(BlockedSmoother, NestedI0I21II_Thyra_SIMPLE_Setup_Apply3, SC, LO, GO, NO) \
6794 TEUCHOS_UNIT_TEST_TEMPLATE_4_INSTANT(BlockedSmoother, BS_Setup_Apply, SC, LO, GO, NO) \
6795 TEUCHOS_UNIT_TEST_TEMPLATE_4_INSTANT(BlockedSmoother, NestedII20I1I_BS_Setup_Apply, SC, LO, GO, NO) \
6796 TEUCHOS_UNIT_TEST_TEMPLATE_4_INSTANT(BlockedSmoother, NestedI2I10II_BS_Setup_Apply, SC, LO, GO, NO) \
6797 TEUCHOS_UNIT_TEST_TEMPLATE_4_INSTANT(BlockedSmoother, NestedI2I10II_BS_Setup_Apply2, SC, LO, GO, NO) \
6798 TEUCHOS_UNIT_TEST_TEMPLATE_4_INSTANT(BlockedSmoother, NestedII02I1I_Thyra_BS_Setup_Apply, SC, LO, GO, NO) \
6799 TEUCHOS_UNIT_TEST_TEMPLATE_4_INSTANT(BlockedSmoother, NestedI2I01II_Thyra_BS_Setup_Apply, SC, LO, GO, NO) \
6800 TEUCHOS_UNIT_TEST_TEMPLATE_4_INSTANT(BlockedSmoother, NestedI2I10II_Thyra_BS_Setup_Apply, SC, LO, GO, NO) \
6801 TEUCHOS_UNIT_TEST_TEMPLATE_4_INSTANT(BlockedSmoother, NestedII01I2I_Thyra_BS_Setup_Apply2, SC, LO, GO, NO) \
6802 TEUCHOS_UNIT_TEST_TEMPLATE_4_INSTANT(BlockedSmoother, NestedI0I21II_Thyra_BS_Setup_Apply3, SC, LO, GO, NO) \
6803 TEUCHOS_UNIT_TEST_TEMPLATE_4_INSTANT(BlockedSmoother, Uzawa_Setup_Apply, SC, LO, GO, NO) \
6804 TEUCHOS_UNIT_TEST_TEMPLATE_4_INSTANT(BlockedSmoother, NestedI2I01II_Uzawa_Setup_Apply, SC, LO, GO, NO) \
6805 TEUCHOS_UNIT_TEST_TEMPLATE_4_INSTANT(BlockedSmoother, NestedI2I01II_Uzawa_Setup_Apply2, SC, LO, GO, NO) \
6806 TEUCHOS_UNIT_TEST_TEMPLATE_4_INSTANT(BlockedSmoother, NestedI2I01II_Thyra_Uzawa_Setup_Apply, SC, LO, GO, NO) \
6807 TEUCHOS_UNIT_TEST_TEMPLATE_4_INSTANT(BlockedSmoother, NestedII01I2I_Thyra_Uzawa_Setup_Apply2, SC, LO, GO, NO) \
6808 TEUCHOS_UNIT_TEST_TEMPLATE_4_INSTANT(BlockedSmoother, NestedI0I21II_Thyra_Uzawa_Setup_Apply3, SC, LO, GO, NO) \
6809 TEUCHOS_UNIT_TEST_TEMPLATE_4_INSTANT(BlockedSmoother, Indef_Setup_Apply, SC, LO, GO, NO) \
6810 TEUCHOS_UNIT_TEST_TEMPLATE_4_INSTANT(BlockedSmoother, NestedI2I01II_Indef_Setup_Apply, SC, LO, GO, NO) \
6811 TEUCHOS_UNIT_TEST_TEMPLATE_4_INSTANT(BlockedSmoother, NestedI2I01II_Indef_Setup_Apply2, SC, LO, GO, NO) \
6812 TEUCHOS_UNIT_TEST_TEMPLATE_4_INSTANT(BlockedSmoother, NestedI2I01II_Thyra_Indef_Setup_Apply, SC, LO, GO, NO) \
6813 TEUCHOS_UNIT_TEST_TEMPLATE_4_INSTANT(BlockedSmoother, NestedII01I2I_Thyra_Indef_Setup_Apply2, SC, LO, GO, NO) \
6814 TEUCHOS_UNIT_TEST_TEMPLATE_4_INSTANT(BlockedSmoother, NestedI0I21II_Thyra_Indef_Setup_Apply3, SC, LO, GO, NO) \
6815 TEUCHOS_UNIT_TEST_TEMPLATE_4_INSTANT(BlockedSmoother, SplitReorder, SC, LO, GO, NO)
6816
6817 #include <MueLu_ETI_4arg.hpp>
6818
6819 } // namespace MueLuTests