Debian Codegraph @

packages/muelu/test/unit_tests/Smoothers/MueLu_TestHelpersSmoothers.hpp

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 #ifndef MUELU_TEST_HELPERS_SMOOTHERS_H
11 #define MUELU_TEST_HELPERS_SMOOTHERS_H
12
13 #include <Teuchos_FancyOStream.hpp>
14 #include <Xpetra_Matrix.hpp>
15
16 #include "MueLu_ConfigDefs.hpp"
17 #include "MueLu_SmootherBase.hpp"
18 #include "MueLu_SmootherPrototype.hpp"
19
20 // Helper functions to test if derived classes conforms to the SmootherBase and SmootherPrototype interfaces
21
22 namespace MueLuTests {
23 namespace TestHelpers {
24 namespace Smoothers {
25
26 // SmootherPrototype test
27 template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
28 void testApplyNoSetup(const MueLu::SmootherPrototype<Scalar, LocalOrdinal, GlobalOrdinal, Node>& smoother, Teuchos::FancyOStream& out, bool& success) {
29 #include "MueLu_UseShortNames.hpp"
30
31 GO numGlobalElements = 125;
32 RCP<const Map> map = MapFactory::Build(Parameters::getLib(), numGlobalElements, 0, Parameters::getDefaultComm());
33
34 RCP<MultiVector> X = MultiVectorFactory::Build(map, 1);
35 RCP<MultiVector> RHS = MultiVectorFactory::Build(map, 1);
36
37 TEST_THROW(smoother.Apply(*X, *RHS), MueLu::Exceptions::RuntimeError);
38 }
39
40 // SmootherBase test
41 template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
42 typename Teuchos::ScalarTraits<Scalar>::magnitudeType
43 testApply(const Xpetra::Matrix<Scalar, LocalOrdinal, GlobalOrdinal, Node>& A,
44 const MueLu::SmootherBase<Scalar, LocalOrdinal, GlobalOrdinal, Node>& smoother,
45 Xpetra::MultiVector<Scalar, LocalOrdinal, GlobalOrdinal, Node>& X,
46 const Xpetra::MultiVector<Scalar, LocalOrdinal, GlobalOrdinal, Node>& RHS,
47 Teuchos::FancyOStream& out, bool& success) {
48 #include "MueLu_UseShortNames.hpp"
49 typedef Teuchos::ScalarTraits<SC> ST;
50
51 Array<typename ST::magnitudeType> norms(1);
52
53 RHS.norm2(norms);
54 out << "||RHS|| = " << std::setiosflags(std::ios::fixed) << std::setprecision(10) << norms[0] << std::endl;
55
56 Teuchos::Array<typename ST::magnitudeType> initialNorms(1);
57 X.norm2(initialNorms);
58 out << "||X_initial|| = " << std::setiosflags(std::ios::fixed) << std::setprecision(10) << initialNorms[0] << std::endl;
59
60 smoother.Apply(X, RHS); // TODO: bool const &InitialGuessIsZero=false
61
62 Teuchos::Array<typename ST::magnitudeType> finalNorms(1);
63 X.norm2(finalNorms);
64 out << "||X_final|| = " << std::setiosflags(std::ios::fixed) << std::setprecision(25) << norms[0] << std::endl;
65
66 Teuchos::Array<typename ST::magnitudeType> residualNorms = Utilities::ResidualNorm(A, X, RHS);
67 out << "||Residual|| = " << std::setiosflags(std::ios::fixed) << std::setprecision(20) << residualNorms[0] << std::endl;
68
69 return residualNorms[0];
70 }
71
72 // SmootherBase test
73 template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
74 typename Teuchos::ScalarTraits<Scalar>::magnitudeType
75 testApply_X1_RHS0(const Xpetra::Matrix<Scalar, LocalOrdinal, GlobalOrdinal, Node>& A,
76 const MueLu::SmootherBase<Scalar, LocalOrdinal, GlobalOrdinal, Node>& smoother,
77 Teuchos::FancyOStream& out, bool& success) {
78 #include "MueLu_UseShortNames.hpp"
79
80 RCP<MultiVector> X = MultiVectorFactory::Build(A.getDomainMap(), 1);
81 RCP<MultiVector> RHS = MultiVectorFactory::Build(A.getRangeMap(), 1);
82 X->putScalar((SC)1.0);
83 RHS->putScalar((SC)0.0);
84
85 return testApply(A, smoother, *X, *RHS, out, success);
86 }
87
88 // SmootherBase test
89 template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
90 typename Teuchos::ScalarTraits<Scalar>::magnitudeType
91 testApply_X0_RandomRHS(const Xpetra::Matrix<Scalar, LocalOrdinal, GlobalOrdinal, Node>& A,
92 const MueLu::SmootherBase<Scalar, LocalOrdinal, GlobalOrdinal, Node>& smoother,
93 Teuchos::FancyOStream& out, bool& success) {
94 #include "MueLu_UseShortNames.hpp"
95
96 RCP<MultiVector> X = MultiVectorFactory::Build(A.getDomainMap(), 1);
97 RCP<MultiVector> RHS = MultiVectorFactory::Build(A.getRangeMap(), 1);
98
99 // Random X
100 X->setSeed(846930886);
101 X->randomize();
102
103 // Normalize X
104 typedef Teuchos::ScalarTraits<SC> ST;
105 Array<typename ST::magnitudeType> norms(1);
106 X->norm2(norms);
107 X->scale(1 / norms[0]);
108
109 // Compute RHS corresponding to X
110 A.apply(*X, *RHS, Teuchos::NO_TRANS, (SC)1.0, (SC)0.0);
111
112 // Reset X to 0
113 X->putScalar((SC)0.0);
114
115 return testApply(A, smoother, *X, *RHS, out, success);
116 }
117
118 // SmootherPrototype helper function
119 template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
120 void setupSmoother(Teuchos::RCP<Xpetra::Matrix<Scalar, LocalOrdinal, GlobalOrdinal, Node>>& A,
121 MueLu::SmootherPrototype<Scalar, LocalOrdinal, GlobalOrdinal, Node>& smoother,
122 Teuchos::FancyOStream& out, bool& success) {
123 #include "MueLu_UseShortNames.hpp"
124
125 Level level;
126 TestHelpers::TestFactory<SC, LO, GO, NO>::createSingleLevelHierarchy(level);
127 level.Set("A", A);
128 smoother.Setup(level);
129 }
130
131 // SmootherPrototype test
132 template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
133 typename Teuchos::ScalarTraits<Scalar>::magnitudeType
134 testApply_A125_X1_RHS0(MueLu::SmootherPrototype<Scalar, LocalOrdinal, GlobalOrdinal, Node>& smoother,
135 Teuchos::FancyOStream& out, bool& success) {
136 #include "MueLu_UseShortNames.hpp"
137
138 Teuchos::RCP<Matrix> A = TestHelpers::TestFactory<SC, LO, GO, NO>::Build1DPoisson(125);
139
140 setupSmoother(A, smoother, out, success);
141 return testApply_X1_RHS0(*A, smoother, out, success); // in MueLuTests::SmootherBase
142 }
143
144 // SmootherPrototype test
145 template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
146 typename Teuchos::ScalarTraits<Scalar>::magnitudeType
147 testApply_A125_X0_RandomRHS(MueLu::SmootherPrototype<Scalar, LocalOrdinal, GlobalOrdinal, Node>& smoother,
148 Teuchos::FancyOStream& out, bool& success) {
149 #include "MueLu_UseShortNames.hpp"
150
151 Teuchos::RCP<Matrix> A = TestHelpers::TestFactory<SC, LO, GO, NO>::Build1DPoisson(125);
152
153 setupSmoother(A, smoother, out, success);
154 return testApply_X0_RandomRHS(*A, smoother, out, success); // in MueLuTests::SmootherBase
155 }
156
157 template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
158 void testDirectSolver(MueLu::SmootherPrototype<Scalar, LocalOrdinal, GlobalOrdinal, Node>& smoother,
159 Teuchos::FancyOStream& out, bool& success) {
160 #include "MueLu_UseShortNames.hpp"
161
162 using ST = Teuchos::ScalarTraits<SC>;
163 using magnitude_type = typename Teuchos::ScalarTraits<SC>::magnitudeType;
164 using MT = Teuchos::ScalarTraits<magnitude_type>;
165
166 magnitude_type residualNorms = testApply_A125_X0_RandomRHS(smoother, out, success);
167 TEST_EQUALITY(residualNorms < 100 * MT::eps(), true);
168 }
169
170 } // namespace Smoothers
171 } // namespace TestHelpers
172 } // namespace MueLuTests
173
174 #endif // MUELU_TEST_HELPERS_SMOOTHERS_H