Debian Codegraph @

packages/muelu/test/unit_tests_kokkos/TentativePFactory_kokkos.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 #include <Teuchos_DefaultComm.hpp>
12
13 #include <Xpetra_MultiVector.hpp>
14 #include <Xpetra_Matrix.hpp>
15
16 #include "MueLu_TestHelpers_kokkos.hpp"
17 #include "MueLu_Version.hpp"
18
19 #include "MueLu_AmalgamationFactory.hpp"
20 #include "MueLu_CoalesceDropFactory_kokkos.hpp"
21 #include "MueLu_CoarseMapFactory.hpp"
22 #include "MueLu_TentativePFactory_kokkos.hpp"
23 #include "MueLu_UncoupledAggregationFactory.hpp"
24 #include "MueLu_NullspaceFactory.hpp"
25
26 namespace MueLuTests {
27
28 TEUCHOS_UNIT_TEST_TEMPLATE_4_DECL(TentativePFactory_kokkos, Constructor, Scalar, LocalOrdinal, GlobalOrdinal, Node) {
29 #include "MueLu_UseShortNames.hpp"
30 MUELU_TESTING_SET_OSTREAM;
31 MUELU_TESTING_LIMIT_SCOPE(Scalar, GlobalOrdinal, NO);
32 if (TestHelpers_kokkos::Parameters::getLib() == Xpetra::UseEpetra) {
33 out << "skipping test for linAlgebra==UseEpetra" << std::endl;
34 return;
35 }
36 out << "version: " << MueLu::Version() << std::endl;
37
38 RCP<TentativePFactory_kokkos> tentPFact = rcp(new TentativePFactory_kokkos);
39 TEST_EQUALITY(tentPFact != Teuchos::null, true);
40 }
41
42 #if 0
43 TEUCHOS_UNIT_TEST(TentativePFactory, MakeTentative_LapackQR)
44 {
45 #include "MueLu_UseShortNames.hpp"
46 MUELU_TESTING_SET_OSTREAM;
47 MUELU_TESTING_LIMIT_SCOPE(Scalar,GlobalOrdinal,NO);
48 out << "version: " << MueLu::Version() << std::endl;
49
50 out << "Test QR with user-supplied nullspace" << std::endl;
51
52 using magnitude_type = typename Teuchos::ScalarTraits<Scalar>::magnitudeType;
53 using TMT = Teuchos::ScalarTraits<magnitude_type>;
54
55 Level fineLevel, coarseLevel;
56 TestHelpers::TestFactory<SC, LO, GO, NO>::createTwoLevelHierarchy(fineLevel, coarseLevel);
57 fineLevel.SetFactoryManager(Teuchos::null); // factory manager is not used on this test
58 coarseLevel.SetFactoryManager(Teuchos::null);
59
60 RCP<Matrix> A = TestHelpers::TestFactory<SC, LO, GO, NO>::Build1DPoisson(/*199*/29);
61 A->SetFixedBlockSize(1);
62 fineLevel.Set("A",A);
63
64 LO NSdim = 2;
65 RCP<MultiVector> nullSpace = MultiVectorFactory::Build(A->getRowMap(),NSdim);
66 nullSpace->randomize();
67 fineLevel.Set("Nullspace",nullSpace);
68
69 RCP<AmalgamationFactory> amalgFact = rcp(new AmalgamationFactory());
70 RCP<CoalesceDropFactory> dropFact = rcp(new CoalesceDropFactory());
71 dropFact->SetFactory("UnAmalgamationInfo", amalgFact);
72
73 RCP<UncoupledAggregationFactory> UncoupledAggFact = rcp(new UncoupledAggregationFactory());
74 UncoupledAggFact->SetFactory("Graph", dropFact);
75
76 UncoupledAggFact->SetMinNodesPerAggregate(3);
77 UncoupledAggFact->SetMaxNeighAlreadySelected(0);
78 UncoupledAggFact->SetOrdering("natural");
79
80 RCP<CoarseMapFactory> coarseMapFact = rcp(new CoarseMapFactory());
81 coarseMapFact->SetFactory("Aggregates", UncoupledAggFact);
82
83 RCP<TentativePFactory> TentativePFact = rcp(new TentativePFactory());
84 TentativePFact->SetFactory("Aggregates", UncoupledAggFact);
85 TentativePFact->SetFactory("UnAmalgamationInfo", amalgFact);
86 TentativePFact->SetFactory("CoarseMap", coarseMapFact);
87
88 coarseLevel.Request("P",TentativePFact.get()); // request Ptent
89 coarseLevel.Request("Nullspace",TentativePFact.get()); // request coarse nullspace
90 coarseLevel.Request(*TentativePFact);
91 TentativePFact->Build(fineLevel,coarseLevel);
92
93 RCP<Matrix> Ptent;
94 coarseLevel.Get("P",Ptent,TentativePFact.get());
95
96 RCP<MultiVector> coarseNullspace = coarseLevel.Get<RCP<MultiVector> >("Nullspace", TentativePFact.get());
97
98 //check interpolation
99 RCP<MultiVector> PtN = MultiVectorFactory::Build(Ptent->getRangeMap(),NSdim);
100 Ptent->apply(*coarseNullspace,*PtN,Teuchos::NO_TRANS,1.0,0.0);
101
102 RCP<MultiVector> diff = MultiVectorFactory::Build(A->getRowMap(),NSdim);
103 diff->putScalar(0.0);
104
105 coarseLevel.Release("P",TentativePFact.get()); // release Ptent
106 coarseLevel.Release("Nullspace",TentativePFact.get()); // release coarse nullspace
107
108 //diff = fineNS + (-1.0)*(P*coarseNS) + 0*diff
109 diff->update(1.0,*nullSpace,-1.0,*PtN,0.0);
110
111 Teuchos::Array<magnitude_type> norms(NSdim);
112 diff->norm2(norms);
113 for (LO i=0; i<NSdim; ++i) {
114 out << "||diff_" << i << "||_2 = " << norms[i] << std::endl;
115 TEST_COMPARE_CONST(norms[i], <, 100*TMT::eps());
116 }
117
118 Teuchos::ArrayRCP<const double> col1 = coarseNullspace->getData(0);
119 Teuchos::ArrayRCP<const double> col2 = coarseNullspace->getData(1);
120 TEST_EQUALITY(col1.size() == col2.size(), true);
121
122 } // MakeTentative Lapack QR
123 #endif
124
125 TEUCHOS_UNIT_TEST_TEMPLATE_4_DECL(TentativePFactory_kokkos, MakeTentative, Scalar, LocalOrdinal, GlobalOrdinal, Node) {
126 #include "MueLu_UseShortNames.hpp"
127 MUELU_TESTING_SET_OSTREAM;
128 MUELU_TESTING_LIMIT_SCOPE(SC, GO, NO);
129
130 using STS = Teuchos::ScalarTraits<Scalar>;
131 using magnitude_type = typename Teuchos::ScalarTraits<Scalar>::magnitudeType;
132 using TMT = Teuchos::ScalarTraits<magnitude_type>;
133
134 if (TestHelpers_kokkos::Parameters::getLib() == Xpetra::UseEpetra) {
135 out << "skipping test for linAlgebra==UseEpetra" << std::endl;
136 return;
137 }
138
139 RCP<const Teuchos::Comm<int> > comm = Teuchos::DefaultComm<int>::getComm();
140
141 out << "version: " << MueLu::Version() << std::endl;
142 out << "Test QR with user-supplied nullspace" << std::endl;
143
144 Level fineLevel, coarseLevel;
145 TestHelpers_kokkos::TestFactory<SC, LO, GO, NO>::createTwoLevelHierarchy(fineLevel, coarseLevel);
146
147 fineLevel.SetFactoryManager(Teuchos::null); // factory manager is not used on this test
148 coarseLevel.SetFactoryManager(Teuchos::null);
149
150 RCP<Matrix> A = TestHelpers_kokkos::TestFactory<SC, LO, GO, NO>::Build1DPoisson(199);
151 fineLevel.Request("A");
152 fineLevel.Set("A", A);
153
154 // Only one NS vector -> exercises manual orthogonalization
155 LO NSdim = 1;
156 RCP<MultiVector> nullSpace = MultiVectorFactory::Build(A->getRowMap(), NSdim);
157 nullSpace->randomize();
158 fineLevel.Set("Nullspace", nullSpace);
159
160 RCP<AmalgamationFactory> amalgFact = rcp(new AmalgamationFactory());
161
162 RCP<CoalesceDropFactory_kokkos> dropFact = rcp(new CoalesceDropFactory_kokkos());
163 dropFact->SetFactory("UnAmalgamationInfo", amalgFact);
164
165 RCP<UncoupledAggregationFactory> aggFact = rcp(new UncoupledAggregationFactory());
166 ParameterList aggParams;
167 aggParams.set("aggregation: ordering", "natural");
168 aggParams.set("aggregation: min agg size", 3);
169 aggParams.set("aggregation: max selected neighbors", 0);
170 aggFact->SetParameterList(aggParams);
171 aggFact->SetFactory("DofsPerNode", dropFact);
172 aggFact->SetFactory("Graph", dropFact);
173
174 RCP<CoarseMapFactory> coarseMapFact = rcp(new CoarseMapFactory());
175 coarseMapFact->SetFactory("Aggregates", aggFact);
176
177 RCP<TentativePFactory_kokkos> TentativePFact = rcp(new TentativePFactory_kokkos());
178 TentativePFact->SetFactory("Aggregates", aggFact);
179 TentativePFact->SetFactory("UnAmalgamationInfo", amalgFact);
180 TentativePFact->SetFactory("CoarseMap", coarseMapFact);
181
182 coarseLevel.Request("P", TentativePFact.get()); // request Ptent
183 coarseLevel.Request("Nullspace", TentativePFact.get());
184 coarseLevel.Request(*TentativePFact);
185 TentativePFact->Build(fineLevel, coarseLevel);
186
187 RCP<Matrix> Ptent;
188 coarseLevel.Get("P", Ptent, TentativePFact.get());
189
190 RCP<MultiVector> coarseNullspace = coarseLevel.Get<RCP<MultiVector> >("Nullspace", TentativePFact.get());
191
192 // Check interpolation by computing ||fineNS - P*coarseNS||
193 RCP<MultiVector> PtN = MultiVectorFactory::Build(Ptent->getRangeMap(), NSdim);
194 Ptent->apply(*coarseNullspace, *PtN, Teuchos::NO_TRANS, 1.0, 0.0);
195
196 RCP<MultiVector> diff = MultiVectorFactory::Build(A->getRowMap(), NSdim);
197 diff->putScalar(0.0);
198
199 coarseLevel.Release("P", TentativePFact.get()); // release Ptent
200 coarseLevel.Release("Nullspace", TentativePFact.get());
201
202 // diff = fineNS - (P*coarseNS)
203 diff->update(1.0, *nullSpace, -1.0, *PtN, 0.0);
204
205 Array<typename Teuchos::ScalarTraits<SC>::magnitudeType> norms(NSdim);
206 diff->norm2(norms);
207 for (LO i = 0; i < NSdim; ++i) {
208 out << "||diff_" << i << "||_2 = " << norms[i] << std::endl;
209 TEST_COMPARE_CONST(norms[i], <, 100 * TMT::eps());
210 }
211
212 // Check normalization and orthogonality of prolongator columns by
213 // making sure that P^T*P = I
214 RCP<Matrix> PtentTPtent = MatrixMatrix::Multiply(*Ptent, true, *Ptent, false, out);
215 RCP<Vector> diagVec = VectorFactory::Build(PtentTPtent->getRowMap());
216 PtentTPtent->getLocalDiagCopy(*diagVec);
217 if (STS::name().find("complex") == std::string::npos) { // skip check for Scalar=complex
218 TEST_EQUALITY(diagVec->norm1(), diagVec->getGlobalLength());
219 TEST_FLOATING_EQUALITY(diagVec->normInf(), TMT::one(), 100 * TMT::eps());
220 }
221 if (STS::name().find("complex") == std::string::npos) // skip check for Scalar=complex
222 TEST_FLOATING_EQUALITY(STS::magnitude(diagVec->meanValue()), TMT::one(), 100 * TMT::eps());
223 TEST_EQUALITY(PtentTPtent->getGlobalNumEntries(), diagVec->getGlobalLength());
224 } // MakeTentative
225
226 TEUCHOS_UNIT_TEST_TEMPLATE_4_DECL(TentativePFactory_kokkos, MakeTentativeVectorBasedUsingDefaultNullspace, Scalar, LocalOrdinal, GlobalOrdinal, Node) {
227 #include "MueLu_UseShortNames.hpp"
228 MUELU_TESTING_SET_OSTREAM;
229 MUELU_TESTING_LIMIT_SCOPE(SC, GO, NO);
230
231 using STS = Teuchos::ScalarTraits<Scalar>;
232 using magnitude_type = typename Teuchos::ScalarTraits<Scalar>::magnitudeType;
233 using TMT = Teuchos::ScalarTraits<magnitude_type>;
234
235 if (TestHelpers_kokkos::Parameters::getLib() == Xpetra::UseEpetra) {
236 out << "skipping test for linAlgebra==UseEpetra" << std::endl;
237 return;
238 }
239
240 out << "version: " << MueLu::Version() << std::endl;
241 out << "Test QR when nullspace isn't supplied by user" << std::endl;
242
243 Level fineLevel, coarseLevel;
244 TestHelpers_kokkos::TestFactory<SC, LO, GO, NO>::createTwoLevelHierarchy(fineLevel, coarseLevel);
245
246 auto A = TestHelpers_kokkos::TestFactory<SC, LO, GO, NO>::Build1DPoisson(200);
247
248 A->SetFixedBlockSize(2);
249
250 fineLevel.Set("A", A);
251
252 auto amalgFact = rcp(new AmalgamationFactory());
253
254 auto dropFact = rcp(new CoalesceDropFactory_kokkos());
255 dropFact->SetFactory("UnAmalgamationInfo", amalgFact);
256
257 auto aggFact = rcp(new UncoupledAggregationFactory());
258 ParameterList aggParams;
259 aggParams.set("aggregation: ordering", "natural");
260 aggParams.set("aggregation: min agg size", 3);
261 aggParams.set("aggregation: max selected neighbors", 0);
262 aggFact->SetParameterList(aggParams);
263 aggFact->SetFactory("DofsPerNode", dropFact);
264 aggFact->SetFactory("Graph", dropFact);
265
266 auto coarseMapFact = rcp(new CoarseMapFactory());
267 coarseMapFact->SetFactory("Aggregates", aggFact);
268
269 auto TentativePFact = rcp(new TentativePFactory_kokkos());
270 TentativePFact->SetFactory("Aggregates", aggFact);
271 TentativePFact->SetFactory("UnAmalgamationInfo", amalgFact);
272 TentativePFact->SetFactory("CoarseMap", coarseMapFact);
273
274 coarseLevel.Request("P", TentativePFact.get()); // request Ptent
275 coarseLevel.Request("Nullspace", TentativePFact.get()); // request coarse nullspace
276 coarseLevel.Request(*TentativePFact);
277 TentativePFact->Build(fineLevel, coarseLevel);
278
279 RCP<Matrix> Ptent;
280 coarseLevel.Get("P", Ptent, TentativePFact.get());
281
282 auto coarseNullspace = coarseLevel.Get<RCP<MultiVector> >("Nullspace", TentativePFact.get());
283
284 size_t NSdim = coarseNullspace->getNumVectors();
285 TEST_EQUALITY(NSdim, 2);
286
287 // coarseNullspace->describe(out, Teuchos::VERB_EXTREME);
288
289 // Check interpolation by computing ||fineNS - P*coarseNS||
290 auto PtN = MultiVectorFactory::Build(Ptent->getRangeMap(), NSdim);
291 Ptent->apply(*coarseNullspace, *PtN, Teuchos::NO_TRANS, 1.0, 0.0);
292
293 auto diff = MultiVectorFactory::Build(A->getRowMap(), NSdim);
294 diff->putScalar(0.0);
295
296 coarseLevel.Release("P", TentativePFact.get()); // release Ptent
297 coarseLevel.Release("Nullspace", TentativePFact.get());
298
299 auto nspFact = Teuchos::rcp(new NullspaceFactory());
300 fineLevel.Request("Nullspace", nspFact.get());
301
302 nspFact->Build(fineLevel);
303
304 auto fineNullspace = fineLevel.Get<RCP<MultiVector> >("Nullspace", nspFact.get());
305
306 TEST_EQUALITY(fineNullspace->getNumVectors(), 2);
307
308 // diff = fineNS - (P*coarseNS)
309 diff->update(1.0, *fineNullspace, -1.0, *PtN, 0.0);
310
311 Array<magnitude_type> norms(NSdim);
312 diff->norm2(norms);
313 for (decltype(NSdim) i = 0; i < NSdim; ++i)
314 TEST_COMPARE_CONST(norms[i], <, 100 * TMT::eps());
315
316 // Check normalization and orthogonality of prolongator columns by
317 // making sure that P^T*P = I
318 RCP<Matrix> PtentTPtent = MatrixMatrix::Multiply(*Ptent, true, *Ptent, false, out);
319 RCP<Vector> diagVec = VectorFactory::Build(PtentTPtent->getRowMap());
320 PtentTPtent->getLocalDiagCopy(*diagVec);
321 if (STS::name().find("complex") == std::string::npos) // skip check for Scalar=complex
322 TEST_FLOATING_EQUALITY(diagVec->norm1(), STS::magnitude(diagVec->getGlobalLength()), 100 * TMT::eps());
323 TEST_FLOATING_EQUALITY(diagVec->normInf(), TMT::one(), 100 * TMT::eps());
324 if (STS::name().find("complex") == std::string::npos) // skip check for Scalar=complex
325 TEST_FLOATING_EQUALITY(STS::magnitude(diagVec->meanValue()), TMT::one(), 100 * TMT::eps());
326 TEST_EQUALITY(PtentTPtent->getGlobalNumEntries(), diagVec->getGlobalLength());
327 } // MakeTentativeVectorBasedUsingDefaultNullspace
328
329 TEUCHOS_UNIT_TEST_TEMPLATE_4_DECL(TentativePFactory_kokkos, MakeTentativeUsingDefaultNullspace, Scalar, LocalOrdinal, GlobalOrdinal, Node) {
330 #include "MueLu_UseShortNames.hpp"
331 MUELU_TESTING_SET_OSTREAM;
332 MUELU_TESTING_LIMIT_SCOPE(SC, GO, NO);
333
334 using STS = Teuchos::ScalarTraits<Scalar>;
335 using magnitude_type = typename Teuchos::ScalarTraits<Scalar>::magnitudeType;
336 using TMT = Teuchos::ScalarTraits<magnitude_type>;
337
338 if (TestHelpers_kokkos::Parameters::getLib() == Xpetra::UseEpetra) {
339 out << "skipping test for linAlgebra==UseEpetra" << std::endl;
340 return;
341 }
342
343 out << "version: " << MueLu::Version() << std::endl;
344 out << "Test QR when nullspace isn't supplied by user" << std::endl;
345
346 Level fineLevel, coarseLevel;
347 TestHelpers_kokkos::TestFactory<SC, LO, GO, NO>::createTwoLevelHierarchy(fineLevel, coarseLevel);
348
349 auto A = TestHelpers_kokkos::TestFactory<SC, LO, GO, NO>::Build1DPoisson(199);
350
351 fineLevel.Set("A", A);
352
353 auto amalgFact = rcp(new AmalgamationFactory());
354
355 auto dropFact = rcp(new CoalesceDropFactory_kokkos());
356 dropFact->SetFactory("UnAmalgamationInfo", amalgFact);
357
358 auto aggFact = rcp(new UncoupledAggregationFactory());
359 ParameterList aggParams;
360 aggParams.set("aggregation: ordering", "natural");
361 aggParams.set("aggregation: min agg size", 3);
362 aggParams.set("aggregation: max selected neighbors", 0);
363 aggFact->SetParameterList(aggParams);
364 aggFact->SetFactory("DofsPerNode", dropFact);
365 aggFact->SetFactory("Graph", dropFact);
366
367 auto coarseMapFact = rcp(new CoarseMapFactory());
368 coarseMapFact->SetFactory("Aggregates", aggFact);
369
370 auto TentativePFact = rcp(new TentativePFactory_kokkos());
371 TentativePFact->SetFactory("Aggregates", aggFact);
372 TentativePFact->SetFactory("UnAmalgamationInfo", amalgFact);
373 TentativePFact->SetFactory("CoarseMap", coarseMapFact);
374
375 coarseLevel.Request("P", TentativePFact.get()); // request Ptent
376 coarseLevel.Request("Nullspace", TentativePFact.get()); // request coarse nullspace
377 coarseLevel.Request(*TentativePFact);
378 TentativePFact->Build(fineLevel, coarseLevel);
379
380 RCP<Matrix> Ptent;
381 coarseLevel.Get("P", Ptent, TentativePFact.get());
382
383 auto coarseNullspace = coarseLevel.Get<RCP<MultiVector> >("Nullspace", TentativePFact.get());
384
385 coarseLevel.Release("P", TentativePFact.get()); // release Ptent
386 coarseLevel.Release("Nullspace", TentativePFact.get()); // release coarse nullspace
387
388 // grab default fine level nullspace (vector of all ones)
389 auto nullSpace = MultiVectorFactory::Build(A->getRowMap(), 1);
390 nullSpace->putScalar(1.0);
391
392 // check interpolation
393 LO NSdim = 1;
394 auto PtN = MultiVectorFactory::Build(A->getRowMap(), NSdim);
395 Ptent->apply(*coarseNullspace, *PtN, Teuchos::NO_TRANS, 1.0, 0.0);
396
397 auto diff = MultiVectorFactory::Build(A->getRowMap(), NSdim);
398 diff->putScalar(0.0);
399
400 // diff = fineNS + (-1.0)*(P*coarseNS) + 0*diff
401 diff->update(1.0, *nullSpace, -1.0, *PtN, 0.0);
402
403 Teuchos::Array<magnitude_type> norms(NSdim);
404 diff->norm2(norms);
405 for (LO i = 0; i < NSdim; ++i)
406 TEST_COMPARE_CONST(norms[i], <, 100 * TMT::eps());
407
408 } // MakeTentativeUsingDefaultNullspace
409
410 #if 0
411 TEUCHOS_UNIT_TEST(TentativePFactory, NonStandardMaps)
412 {
413
414 //#warning Unit test PgPFactory NonStandardMaps disabled
415 // return;
416
417 RCP<const Teuchos::Comm<int> > comm = Teuchos::DefaultComm<int>::getComm();
418 Xpetra::UnderlyingLib lib = MueLuTests::TestHelpers::Parameters::getLib();
419
420 // generate problem
421 LO maxLevels = 3;
422 //LO its=10;
423 GO nEle = 63;
424 GO nIndexBase = 10;
425 const RCP<const Map> map = MapFactory::Build(lib, nEle, nIndexBase, comm);
426 RCP<Matrix> mtx = Galeri::Xpetra::MatrixTraits<Map,CrsMatrixWrap>::Build(map, 3);
427
428 LocalOrdinal NumMyElements = map->getLocalNumElements();
429 Teuchos::ArrayView<const GlobalOrdinal> MyGlobalElements = map->getLocalElementList();
430 GlobalOrdinal NumGlobalElements = map->getGlobalNumElements();
431 assert(NumGlobalElements == nEle);
432
433 GlobalOrdinal NumEntries;
434 LocalOrdinal nnz=2;
435 std::vector<Scalar> Values(nnz);
436 std::vector<GlobalOrdinal> Indices(nnz);
437
438 Scalar a = 2.0;
439 Scalar b = -1.0;
440 Scalar c = -1.0;
441
442 for (LocalOrdinal i = 0; i < NumMyElements; ++i)
443 {
444 if (MyGlobalElements[i] == nIndexBase)
445 {
446 // off-diagonal for first row
447 Indices[0] = nIndexBase;
448 NumEntries = 1;
449 Values[0] = c;
450 }
451 else if (MyGlobalElements[i] == nIndexBase + NumGlobalElements - 1)
452 {
453 // off-diagonal for last row
454 Indices[0] = nIndexBase + NumGlobalElements - 2;
455 NumEntries = 1;
456 Values[0] = b;
457 }
458 else
459 {
460 // off-diagonal for internal row
461 Indices[0] = MyGlobalElements[i] - 1;
462 Values[1] = b;
463 Indices[1] = MyGlobalElements[i] + 1;
464 Values[0] = c;
465 NumEntries = 2;
466 }
467
468 // put the off-diagonal entries
469 // Xpetra wants ArrayViews (sigh)
470 Teuchos::ArrayView<Scalar> av(&Values[0],NumEntries);
471 Teuchos::ArrayView<GlobalOrdinal> iv(&Indices[0],NumEntries);
472 mtx->insertGlobalValues(MyGlobalElements[i], iv, av);
473
474 // Put in the diagonal entry
475 mtx->insertGlobalValues(MyGlobalElements[i],
476 Teuchos::tuple<GlobalOrdinal>(MyGlobalElements[i]),
477 Teuchos::tuple<Scalar>(a) );
478
479 } //for (LocalOrdinal i = 0; i < NumMyElements; ++i)
480
481
482 mtx->fillComplete(map,map);
483
484 std::cout << map->getIndexBase() << std::endl;
485
486 RCP<Matrix> Op = Teuchos::rcp_dynamic_cast<Matrix>(mtx);
487
488 // build nullspace
489 RCP<MultiVector> nullSpace = MultiVectorFactory::Build(map,1);
490 nullSpace->putScalar( (SC) 1.0);
491
492 // fill hierarchy
493 RCP<Hierarchy> H = rcp( new Hierarchy() );
494 H->setDefaultVerbLevel(Teuchos::VERB_HIGH);
495 RCP<Level> Finest = H->GetLevel(); // first associate level with hierarchy (for defaultFactoryHandler!)
496
497 Finest->setDefaultVerbLevel(Teuchos::VERB_HIGH);
498 Finest->Set("A",Op); // set fine level matrix
499 Finest->Set("Nullspace",nullSpace); // set null space information for finest level
500
501 // define transfer operators
502 RCP<UncoupledAggregationFactory> UncoupledAggFact = rcp(new UncoupledAggregationFactory());
503 UncoupledAggFact->SetMinNodesPerAggregate(3);
504 UncoupledAggFact->SetMaxNeighAlreadySelected(0);
505 UncoupledAggFact->SetOrdering("natural");
506
507 RCP<TentativePFactory> Pfact = rcp(new TentativePFactory());
508 RCP<Factory> Rfact = rcp( new TransPFactory() );
509 RCP<RAPFactory> Acfact = rcp( new RAPFactory() );
510 H->SetMaxCoarseSize(1);
511
512 // setup smoothers
513 Teuchos::ParameterList smootherParamList;
514 smootherParamList.set("relaxation: type", "Symmetric Gauss-Seidel");
515 smootherParamList.set("relaxation: sweeps", (LO) 1);
516 smootherParamList.set("relaxation: damping factor", (SC) 1.0);
517 RCP<SmootherPrototype> smooProto = rcp( new TrilinosSmoother("RELAXATION", smootherParamList) );
518 RCP<SmootherFactory> SmooFact = rcp( new SmootherFactory(smooProto) );
519 Acfact->setVerbLevel(Teuchos::VERB_HIGH);
520
521 FactoryManager M;
522 M.SetFactory("P", Pfact);
523 M.SetFactory("R", Rfact);
524 M.SetFactory("A", Acfact);
525 M.SetFactory("Ptent", Pfact);
526 M.SetFactory("Aggregates", UncoupledAggFact);
527 M.SetFactory("Smoother", SmooFact);
528 M.SetFactory("CoarseSolver", SmooFact);
529
530 H->Setup(M, 0, maxLevels);
531
532 RCP<Level> coarseLevel = H->GetLevel(1);
533 TEST_EQUALITY(coarseLevel->IsRequested("A",MueLu::NoFactory::get()), false);
534 TEST_EQUALITY(coarseLevel->IsRequested("P",MueLu::NoFactory::get()), false);
535 TEST_EQUALITY(coarseLevel->IsRequested("PreSmoother",MueLu::NoFactory::get()), false);
536 TEST_EQUALITY(coarseLevel->IsRequested("PostSmoother",MueLu::NoFactory::get()), false);
537 TEST_EQUALITY(coarseLevel->IsRequested("R",MueLu::NoFactory::get()), false);
538 TEST_EQUALITY(coarseLevel->IsAvailable("A",MueLu::NoFactory::get()), true);
539
540 TEST_EQUALITY(coarseLevel->IsAvailable("P",MueLu::NoFactory::get()), true);
541 TEST_EQUALITY(coarseLevel->IsAvailable("PreSmoother",MueLu::NoFactory::get()), true);
542 TEST_EQUALITY(coarseLevel->IsAvailable("PostSmoother",MueLu::NoFactory::get()), true);
543 TEST_EQUALITY(coarseLevel->IsAvailable("R",MueLu::NoFactory::get()), true);
544 TEST_EQUALITY(coarseLevel->GetKeepFlag("A",MueLu::NoFactory::get()), MueLu::Final);
545 TEST_EQUALITY(coarseLevel->GetKeepFlag("P",MueLu::NoFactory::get()), MueLu::Final);
546 TEST_EQUALITY(coarseLevel->GetKeepFlag("PreSmoother",MueLu::NoFactory::get()), MueLu::Final);
547 TEST_EQUALITY(coarseLevel->GetKeepFlag("PostSmoother",MueLu::NoFactory::get()), MueLu::Final);
548 TEST_EQUALITY(coarseLevel->GetKeepFlag("R",MueLu::NoFactory::get()), MueLu::Final);
549 TEST_EQUALITY(coarseLevel->IsRequested("P",Pfact.get()), false);
550 TEST_EQUALITY(coarseLevel->IsRequested("PreSmoother",SmooFact.get()), false);
551 TEST_EQUALITY(coarseLevel->IsRequested("PostSmoother",SmooFact.get()), false);
552 TEST_EQUALITY(coarseLevel->IsRequested("R",Rfact.get()), false);
553 TEST_EQUALITY(coarseLevel->IsAvailable("P",Pfact.get()), false);
554 TEST_EQUALITY(coarseLevel->IsAvailable("PreSmoother",SmooFact.get()), false);
555 TEST_EQUALITY(coarseLevel->IsAvailable("PostSmoother",SmooFact.get()), false);
556 TEST_EQUALITY(coarseLevel->IsAvailable("R",Rfact.get()), false);
557 TEST_EQUALITY(coarseLevel->GetKeepFlag("P",Pfact.get()), 0);
558 TEST_EQUALITY(coarseLevel->GetKeepFlag("PreSmoother",SmooFact.get()), 0);
559 TEST_EQUALITY(coarseLevel->GetKeepFlag("PostSmoother",SmooFact.get()), 0);
560 TEST_EQUALITY(coarseLevel->GetKeepFlag("R",Rfact.get()), 0);
561 RCP<Level> coarseLevel2 = H->GetLevel(2);
562 TEST_EQUALITY(coarseLevel2->IsRequested("A",MueLu::NoFactory::get()), false);
563 TEST_EQUALITY(coarseLevel2->IsRequested("P",MueLu::NoFactory::get()), false);
564 TEST_EQUALITY(coarseLevel2->IsRequested("R",MueLu::NoFactory::get()), false);
565 TEST_EQUALITY(coarseLevel2->IsRequested("PreSmoother",MueLu::NoFactory::get()), false);
566 TEST_EQUALITY(coarseLevel2->IsRequested("PostSmoother",MueLu::NoFactory::get()), false);
567 TEST_EQUALITY(coarseLevel2->IsAvailable("A",MueLu::NoFactory::get()), true);
568 TEST_EQUALITY(coarseLevel2->IsAvailable("P",MueLu::NoFactory::get()), true);
569 TEST_EQUALITY(coarseLevel2->IsAvailable("PreSmoother",MueLu::NoFactory::get()), true);
570 TEST_EQUALITY(coarseLevel2->IsAvailable("PostSmoother",MueLu::NoFactory::get()), false); // coarse level only has presmoother = coarse solver
571 TEST_EQUALITY(coarseLevel2->IsAvailable("R",MueLu::NoFactory::get()), true);
572 TEST_EQUALITY(coarseLevel2->GetKeepFlag("A",MueLu::NoFactory::get()), MueLu::Final);
573 TEST_EQUALITY(coarseLevel2->GetKeepFlag("P",MueLu::NoFactory::get()), MueLu::Final);
574 TEST_EQUALITY(coarseLevel2->GetKeepFlag("PreSmoother",MueLu::NoFactory::get()), MueLu::Final);
575 // TEST_EQUALITY(coarseLevel2->GetKeepFlag("PostSmoother",MueLu::NoFactory::get()), MueLu::Final); // coarse level only has presmoother = coarse solver
576 TEST_EQUALITY(coarseLevel2->GetKeepFlag("R",MueLu::NoFactory::get()), MueLu::Final);
577 TEST_EQUALITY(coarseLevel2->IsRequested("P",Pfact.get()), false);
578 TEST_EQUALITY(coarseLevel2->IsRequested("R",Rfact.get()), false);
579 TEST_EQUALITY(coarseLevel2->IsAvailable("P",Pfact.get()), false);
580 TEST_EQUALITY(coarseLevel2->IsAvailable("PreSmoother",SmooFact.get()), false);
581 TEST_EQUALITY(coarseLevel2->IsAvailable("PostSmoother",SmooFact.get()), false);
582 TEST_EQUALITY(coarseLevel2->IsAvailable("R",Rfact.get()), false);
583 TEST_EQUALITY(coarseLevel2->GetKeepFlag("P",Pfact.get()), 0);
584 TEST_EQUALITY(coarseLevel2->GetKeepFlag("PreSmoother",SmooFact.get()), 0);
585 TEST_EQUALITY(coarseLevel2->GetKeepFlag("PostSmoother",SmooFact.get()), 0);
586 TEST_EQUALITY(coarseLevel2->GetKeepFlag("R",Rfact.get()), 0);
587
588 }
589
590 #if defined(HAVE_MUELU_EPETRA) && defined(HAVE_MUELU_EPETRAEXT) && defined(HAVE_MUELU_IFPACK) && defined(HAVE_MUELU_IFPACK2)
591 TEUCHOS_UNIT_TEST(TentativePFactory, EpetraVsTpetra)
592 {
593 out << "version: " << MueLu::Version() << std::endl;
594 out << "Test QR when nullspace isn't supplied by user" << std::endl;
595
596 RCP<const Teuchos::Comm<int> > comm = Teuchos::DefaultComm<int>::getComm();
597
598 using TST = Teuchos::ScalarTraits<SC>;
599 using magnitude_type = typename TST::magnitudeType;
600 using TMT = Teuchos::ScalarTraits<magnitudeType>;
601
602 Teuchos::Array<magnitude_type> results(2);
603
604 // run test only on 1 proc
605 if(comm->getSize() == 1)
606 {
607 Xpetra::UnderlyingLib lib = Xpetra::UseEpetra;
608
609 // run Epetra and Tpetra test
610 for (int run = 0; run < 2; run++)
611 {
612 if (run == 0) lib = Xpetra::UseEpetra;
613 else lib = Xpetra::UseTpetra;
614
615 // generate problem
616 LO maxLevels = 3;
617 LO its=10;
618 LO nEle = 63;
619 const RCP<const Map> map = MapFactory::Build(lib, nEle, 0, comm);
620 Teuchos::ParameterList matrixParameters;
621 matrixParameters.set("nx", Teuchos::as<GO>(nEle));
622 RCP<Galeri::Xpetra::Problem<Map,CrsMatrixWrap,MultiVector> > Pr =
623 Galeri::Xpetra::BuildProblem<SC, LO, GO, Map, CrsMatrixWrap,MultiVector>("Laplace1D", map, matrixParameters);
624 RCP<Matrix> Op = Pr->BuildMatrix();
625
626 // build nullspace
627 RCP<MultiVector> nullSpace = MultiVectorFactory::Build(map,1);
628 nullSpace->putScalar( (SC) 1.0);
629 Teuchos::Array<magnitude_type> norms(1);
630 nullSpace->norm1(norms);
631 if (comm->getRank() == 0)
632 out << "||NS|| = " << norms[0] << std::endl;
633
634 // fill hierarchy
635 RCP<Hierarchy> H = rcp( new Hierarchy() );
636 H->setDefaultVerbLevel(Teuchos::VERB_HIGH);
637 RCP<Level> Finest = H->GetLevel(); // first associate level with hierarchy (for defaultFactoryHandler!)
638
639 Finest->setDefaultVerbLevel(Teuchos::VERB_HIGH);
640 Finest->Set("A",Op); // set fine level matrix
641 Finest->Set("Nullspace",nullSpace); // set null space information for finest level
642
643 // define transfer operators
644 RCP<UncoupledAggregationFactory> UncoupledAggFact = rcp(new UncoupledAggregationFactory());
645 UncoupledAggFact->SetMinNodesPerAggregate(3);
646 UncoupledAggFact->SetMaxNeighAlreadySelected(0);
647 UncoupledAggFact->SetOrdering("natural");
648
649 RCP<TentativePFactory> Pfact = rcp(new TentativePFactory());
650 RCP<Factory> Rfact = rcp( new TransPFactory() );
651 RCP<RAPFactory> Acfact = rcp( new RAPFactory() );
652 H->SetMaxCoarseSize(1);
653
654 // setup smoothers
655 Teuchos::ParameterList smootherParamList;
656 smootherParamList.set("relaxation: type", "Symmetric Gauss-Seidel");
657 smootherParamList.set("relaxation: sweeps", (LO) 1);
658 smootherParamList.set("relaxation: damping factor", (SC) 1.0);
659 RCP<SmootherPrototype> smooProto = rcp( new TrilinosSmoother("RELAXATION", smootherParamList) );
660 RCP<SmootherFactory> SmooFact = rcp( new SmootherFactory(smooProto) );
661 Acfact->setVerbLevel(Teuchos::VERB_HIGH);
662
663 RCP<SmootherFactory> coarseSolveFact = rcp(new SmootherFactory(smooProto, Teuchos::null));
664
665 FactoryManager M;
666 M.SetFactory("P", Pfact);
667 M.SetFactory("R", Rfact);
668 M.SetFactory("A", Acfact);
669 M.SetFactory("Ptent", Pfact);
670 M.SetFactory("Aggregates", UncoupledAggFact);
671 M.SetFactory("Smoother", SmooFact);
672 M.SetFactory("CoarseSolver", coarseSolveFact);
673
674 H->Setup(M, 0, maxLevels);
675
676 // test some basic multgrid data
677 RCP<Level> coarseLevel = H->GetLevel(1);
678 RCP<Matrix> P1 = coarseLevel->Get< RCP<Matrix> >("P");
679 RCP<Matrix> R1 = coarseLevel->Get< RCP<Matrix> >("R");
680 TEST_EQUALITY(P1->getGlobalNumRows(), 63);
681 TEST_EQUALITY(P1->getGlobalNumCols(), 21);
682 TEST_EQUALITY(R1->getGlobalNumRows(), 21);
683 TEST_EQUALITY(R1->getGlobalNumCols(), 63);
684 RCP<Level> coarseLevel2 = H->GetLevel(2);
685 RCP<Matrix> P2 = coarseLevel2->Get< RCP<Matrix> >("P");
686 RCP<Matrix> R2 = coarseLevel2->Get< RCP<Matrix> >("R");
687 TEST_EQUALITY(P2->getGlobalNumRows(), 21);
688 TEST_EQUALITY(P2->getGlobalNumCols(), 7);
689 TEST_EQUALITY(R2->getGlobalNumRows(), 7);
690 TEST_EQUALITY(R2->getGlobalNumCols(), 21);
691
692 Teuchos::RCP<Xpetra::Matrix<Scalar,LO,GO,Node> > PtentTPtent = Xpetra::MatrixMatrix<Scalar,LO,GO,Node>::Multiply(*P1,true,*P1,false,out);
693 Teuchos::RCP<Xpetra::Vector<Scalar,LO,GO,Node> > diagVec = Xpetra::VectorFactory<Scalar,LO,GO,Node>::Build(PtentTPtent->getRowMap());
694 PtentTPtent->getLocalDiagCopy(*diagVec);
695 TEST_FLOATING_EQUALITY(diagVec->norm1(), TST::magnitude(diagVec->getGlobalLength()), 100*TMT:eps());
696 TEST_FLOATING_EQUALITY(diagVec->normInf(), TMT::one(), 100*TMT:eps());
697 TEST_FLOATING_EQUALITY(diagVec->meanValue(), TMT::one(), 100*TMT:eps());
698 TEST_EQUALITY(PtentTPtent->getGlobalNumEntries(), diagVec->getGlobalLength());
699
700 // Define RHS
701 RCP<MultiVector> X = MultiVectorFactory::Build(map,1);
702 RCP<MultiVector> RHS = MultiVectorFactory::Build(map,1);
703
704 X->putScalar(1.0);
705 X->norm2(norms);
706 if (comm->getRank() == 0)
707 out << "||X_true|| = " << std::setiosflags(std::ios::fixed) << std::setprecision(10) << norms[0] << std::endl;
708
709 Op->apply(*X,*RHS,Teuchos::NO_TRANS,(SC)1.0,(SC)0.0);
710
711 // Use AMG directly as an iterative method
712 {
713 X->putScalar( (SC) 0.0);
714
715 H->Iterate(*RHS,*X,its);
716
717 X->norm2(norms);
718 if (comm->getRank() == 0)
719 out << "||X_" << std::setprecision(2) << its << "|| = " << std::setiosflags(std::ios::fixed) << std::setprecision(10) << norms[0] << std::endl;
720 results[run] = norms[0];
721 }
722 }
723
724 TEST_FLOATING_EQUALITY(results[0], results[1], 100*TMT::eps()); // check results of EPETRA vs TPETRA
725 } // comm->getSize == 1
726
727 } // TentativePFactory_EpetraVsTpetra
728 #endif
729 #endif
730
731 #define MUELU_ETI_GROUP(SC, LO, GO, NO) \
732 TEUCHOS_UNIT_TEST_TEMPLATE_4_INSTANT(TentativePFactory_kokkos, Constructor, SC, LO, GO, NO) \
733 TEUCHOS_UNIT_TEST_TEMPLATE_4_INSTANT(TentativePFactory_kokkos, MakeTentative, SC, LO, GO, NO) \
734 TEUCHOS_UNIT_TEST_TEMPLATE_4_INSTANT(TentativePFactory_kokkos, MakeTentativeUsingDefaultNullspace, SC, LO, GO, NO) \
735 TEUCHOS_UNIT_TEST_TEMPLATE_4_INSTANT(TentativePFactory_kokkos, MakeTentativeVectorBasedUsingDefaultNullspace, SC, LO, GO, NO)
736
737 #include <MueLu_ETI_4arg.hpp>
738
739 } // namespace MueLuTests