Debian Codegraph @

packages/muelu/test/unit_tests/Smoothers/SmootherFactory.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 <MueLu_Version.hpp>
12 #include <MueLu_TestHelpers.hpp>
13
14 #include <MueLu_Exceptions.hpp>
15 #include <MueLu_Level.hpp>
16 #include <MueLu_SmootherFactory.hpp>
17 #include <MueLu_FakeSmootherPrototype.hpp>
18
19 namespace MueLuTests {
20
21 namespace SmootherFactory {
22
23 using Teuchos::RCP;
24 using Teuchos::rcp;
25 using namespace MueLu::Exceptions;
26
27 template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
28 class SmootherFactoryHelpers {
29 #include <MueLu_UseShortNames.hpp>
30
31 public:
32 ////////////////////////////////////////////////////////////////////////////////
33 // Helper functions: class invariant
34 ////////////////////////////////////////////////////////////////////////////////
35
36 // Check the consistency of the internal state of a SmootherFactory object
37 static void check(RCP<const SmootherFactory> smootherFactory, RCP<const SmootherPrototype> preSmooProto, RCP<const SmootherPrototype> postSmooProto, Teuchos::FancyOStream &out, bool &success) {
38 RCP<SmootherPrototype> getPreSmooProto, getPostSmooProto;
39 smootherFactory->GetSmootherPrototypes(getPreSmooProto, getPostSmooProto);
40
41 TEST_EQUALITY(preSmooProto, getPreSmooProto);
42 TEST_EQUALITY(postSmooProto, getPostSmooProto);
43
44 // Check if internal prototypes are still prototypes (IsSetup() == false)
45 if (preSmooProto != Teuchos::null) TEST_EQUALITY(preSmooProto->IsSetup(), false);
46 if (postSmooProto != Teuchos::null) TEST_EQUALITY(postSmooProto->IsSetup(), false);
47 }
48
49 // Similar to check(), but with INEQUALITY.
50 // Do not check if internal prototypes verifies IsSetup() == false
51 static void ineqcheck(RCP<const SmootherFactory> smootherFactory, RCP<const SmootherBase> preSmooProto, RCP<const SmootherBase> postSmooProto, Teuchos::FancyOStream &out, bool &success) {
52 RCP<SmootherPrototype> getPreSmooProto, getPostSmooProto;
53 smootherFactory->GetSmootherPrototypes(getPreSmooProto, getPostSmooProto);
54 TEST_INEQUALITY(preSmooProto, getPreSmooProto);
55 TEST_INEQUALITY(postSmooProto, getPostSmooProto);
56 }
57
58 ////////////////////////////////////////////////////////////////////////////////
59 // Helper functions: apply a test to a collection of test cases
60 ////////////////////////////////////////////////////////////////////////////////
61
62 // Apply a test to a list of cases (one argument)
63 static void testCollection(void (*func)(RCP<SmootherPrototype>, Teuchos::FancyOStream &, bool &), Teuchos::FancyOStream &out, bool &success) {
64 RCP<SmootherPrototype> smooProto = rcp(new FakeSmootherPrototype());
65
66 // tests
67 func(smooProto, out, success);
68 func(Teuchos::null, out, success);
69 }
70
71 // Apply a test to a list of invalid cases (one argument)
72 static void testInvalidCollection(void (*func)(RCP<SmootherPrototype>, Teuchos::FancyOStream &, bool &), Teuchos::FancyOStream &out, bool &success) {
73 RCP<SmootherPrototype> invalidSmooProto = rcp(new FakeSmootherPrototype());
74 Level level;
75 invalidSmooProto->Setup(level);
76
77 // tests
78 func(invalidSmooProto, out, success);
79 }
80
81 // Apply a test to a list of cases (two argument)
82 static void testCollection(
83 void (*func)(RCP<SmootherPrototype>, RCP<SmootherPrototype>, Teuchos::FancyOStream &, bool &),
84 Teuchos::FancyOStream &out,
85 bool &success) {
86 RCP<SmootherPrototype> smooProtoA = rcp(new FakeSmootherPrototype());
87 RCP<SmootherPrototype> smooProtoB = rcp(new FakeSmootherPrototype());
88
89 // tests
90 func(Teuchos::null, Teuchos::null, out, success);
91 func(smooProtoA, smooProtoB, out, success);
92 func(smooProtoA, Teuchos::null, out, success);
93 func(Teuchos::null, smooProtoB, out, success);
94 func(smooProtoA, smooProtoA, out, success);
95 }
96
97 // Apply a test to a list of invalid cases (two argument)
98 static void testInvalidCollection(void (*func)(RCP<SmootherPrototype>, RCP<SmootherPrototype>, Teuchos::FancyOStream &, bool &), Teuchos::FancyOStream &out, bool &success) {
99 RCP<SmootherPrototype> smooProtoA = rcp(new FakeSmootherPrototype());
100 RCP<SmootherPrototype> smooProtoB = rcp(new FakeSmootherPrototype());
101 RCP<SmootherPrototype> invalidSmooProto = rcp(new FakeSmootherPrototype());
102 Level level;
103 invalidSmooProto->Setup(level);
104
105 // tests
106 func(invalidSmooProto, smooProtoB, out, success);
107 func(smooProtoA, invalidSmooProto, out, success);
108 func(invalidSmooProto, Teuchos::null, out, success);
109 func(Teuchos::null, invalidSmooProto, out, success);
110 func(invalidSmooProto, invalidSmooProto, out, success);
111 }
112
113 // Build an object and test internal state (one argument)
114 static void testConstructor1(RCP<SmootherPrototype> smooProto, Teuchos::FancyOStream &out, bool &success) {
115 check(rcp(new SmootherFactory(smooProto)), smooProto, smooProto, out, success); // One argument == same pre and post smoother prototype
116 }
117
118 // Test with invalid input argument
119 static void testInvalidConstructor1(RCP<SmootherPrototype> smooProto, Teuchos::FancyOStream &out, bool &success) {
120 TEST_THROW(rcp(new SmootherFactory(smooProto)), RuntimeError);
121 }
122
123 // Build an object and test internal state (two arguments)
124 static void testConstructor2(RCP<SmootherPrototype> smooProtoA, RCP<SmootherPrototype> smooProtoB, Teuchos::FancyOStream &out, bool &success) {
125 check(rcp(new SmootherFactory(smooProtoA, smooProtoB)), smooProtoA, smooProtoB, out, success);
126 }
127
128 // Test with invalid input arguments
129 static void testInvalidConstructor2(RCP<SmootherPrototype> smooProtoA, RCP<SmootherPrototype> smooProtoB, Teuchos::FancyOStream &out, bool &success) {
130 TEST_THROW(rcp(new SmootherFactory(smooProtoA, smooProtoB)), RuntimeError);
131 }
132
133 static void testSetSmootherPrototypes1(RCP<SmootherPrototype> smooProto, Teuchos::FancyOStream &out, bool &success) {
134 RCP<SmootherPrototype> smooProto0 = rcp(new FakeSmootherPrototype());
135 RCP<SmootherFactory> smooFact = rcp(new SmootherFactory(smooProto0));
136
137 ineqcheck(smooFact, smooProto, smooProto, out, success);
138 smooFact->SetSmootherPrototypes(smooProto);
139 check(smooFact, smooProto, smooProto, out, success); // One argument == same pre and post smoother prototype
140 }
141
142 static void testInvalidSetSmootherPrototypes1(RCP<SmootherPrototype> smooProto, Teuchos::FancyOStream &out, bool &success) {
143 SmootherFactory smooFact(Teuchos::null);
144 TEST_THROW(smooFact.SetSmootherPrototypes(smooProto), RuntimeError);
145 }
146
147 static void testSetSmootherPrototypes2(RCP<SmootherPrototype> smooProtoA, RCP<SmootherPrototype> smooProtoB, Teuchos::FancyOStream &out, bool &success) {
148 RCP<SmootherPrototype> smooProto0 = rcp(new FakeSmootherPrototype());
149 RCP<SmootherFactory> smooFact = rcp(new SmootherFactory(smooProto0));
150
151 ineqcheck(smooFact, smooProtoA, smooProtoB, out, success);
152 smooFact->SetSmootherPrototypes(smooProtoA, smooProtoB);
153 check(smooFact, smooProtoA, smooProtoB, out, success);
154 }
155
156 static void testInvalidSetSmootherPrototypes2(RCP<SmootherPrototype> smooProtoA, RCP<SmootherPrototype> smooProtoB, Teuchos::FancyOStream &out, bool &success) {
157 SmootherFactory smooFact(Teuchos::null);
158 TEST_THROW(smooFact.SetSmootherPrototypes(smooProtoA, smooProtoB), RuntimeError);
159 }
160
161 // test if a smoother created by Build() is correct (check if it corresponds to the prototype)
162 static void testBuildCheckOutput(RCP<SmootherFactory> smooFact, Level &level, RCP<SmootherPrototype> smooProto, const std::string &tag, Teuchos::FancyOStream &out, bool &success) {
163 if (smooProto == Teuchos::null) {
164 TEST_EQUALITY(level.IsAvailable(tag, smooFact.get()), false);
165 } else {
166 RCP<SmootherBase> smoother;
167 TEST_NOTHROW(smoother = level.Get<RCP<SmootherBase> >(tag, smooFact.get()));
168
169 TEST_INEQUALITY(smoother, Teuchos::null);
170 TEST_INEQUALITY(smoother, smooProto);
171
172 if (smooProto != Teuchos::null) {
173 RCP<FakeSmootherPrototype> smootherF;
174
175 // ouput test: smoothers same derived class as prototypes
176 TEST_NOTHROW(smootherF = rcp_dynamic_cast<FakeSmootherPrototype>(smoother, true));
177
178 if (smootherF != Teuchos::null) {
179 // output test: smoother parameters == prototype parameters
180 RCP<FakeSmootherPrototype> smooProtoF = rcp_dynamic_cast<FakeSmootherPrototype>(smooProto, true);
181 TEST_EQUALITY(smootherF->GetParam(), smooProtoF->GetParam());
182
183 // output test: smoothers are ready to be apply
184 TEST_EQUALITY(smootherF->IsSetup(), true);
185
186 // setup done only once.
187 TEST_EQUALITY(smootherF->GetNumOfSetupCall(), 1);
188 TEST_EQUALITY(smootherF->GetNumOfSetup(), 1);
189 }
190 }
191 }
192 }
193
194 // test outpout of Build() and BuildSmoother()
195 static void testBuildCheck(RCP<SmootherFactory> smooFact, Level &level, RCP<SmootherPrototype> smooProtoA, RCP<SmootherPrototype> smooProtoB, MueLu::PreOrPost preOrPost, Teuchos::FancyOStream &out, bool &success) {
196 // invariant: smoother prototypes kept unchanged
197 check(smooFact, smooProtoA, smooProtoB, out, success);
198
199 // output test
200 if (preOrPost == MueLu::BOTH) {
201 testBuildCheckOutput(smooFact, level, smooProtoA, "PreSmoother", out, success);
202 testBuildCheckOutput(smooFact, level, smooProtoB, "PostSmoother", out, success);
203
204 // ReUse: if pre and post prototype are the same, then pre smoother == post smoother
205 // otherwise, they are different (have not been tested by previous tests)
206 RCP<SmootherBase> smooA, smooB;
207 if (smooProtoA != Teuchos::null) {
208 smooA = level.Get<RCP<SmootherBase> >("PreSmoother", smooFact.get());
209 }
210 if (smooProtoB != Teuchos::null) {
211 smooB = level.Get<RCP<SmootherBase> >("PostSmoother", smooFact.get());
212 }
213 if (smooProtoA == smooProtoB) {
214 TEST_EQUALITY(smooA, smooB);
215 } else {
216 TEST_INEQUALITY(smooA, smooB);
217 }
218
219 } else if (preOrPost == MueLu::PRE) {
220 testBuildCheckOutput(smooFact, level, smooProtoA, "PreSmoother", out, success);
221 TEST_EQUALITY(level.IsAvailable("PostSmoother", smooFact.get()), false);
222
223 } else if (preOrPost == MueLu::POST) {
224 TEST_EQUALITY(level.IsAvailable("PreSmoother", smooFact.get()), false);
225 testBuildCheckOutput(smooFact, level, smooProtoB, "PostSmoother", out, success);
226
227 } else {
228 TEST_EQUALITY(true, false);
229 }
230 }
231
232 //
233 static void testBuild(RCP<SmootherPrototype> smooProtoA, RCP<SmootherPrototype> smooProtoB, Teuchos::FancyOStream &out, bool &success) {
234 RCP<SmootherFactory> smooFact = rcp(new SmootherFactory(smooProtoA, smooProtoB));
235
236 Level level; // level.SetupPhase(true);
237 TestHelpers::TestFactory<SC, LO, GO, NO>::createSingleLevelHierarchy(level);
238
239 level.Request("PreSmoother", smooFact.get());
240 level.Request("PostSmoother", smooFact.get());
241
242 smooFact->Build(level);
243
244 testBuildCheck(smooFact, level, smooProtoA, smooProtoB, MueLu::BOTH, out, success);
245 }
246
247 static void testBuildSmootherPreOrPost(RCP<SmootherPrototype> smooProtoA, RCP<SmootherPrototype> smooProtoB, MueLu::PreOrPost preOrPost, Teuchos::FancyOStream &out, bool &success) {
248 RCP<SmootherFactory> smooFact = rcp(new SmootherFactory(smooProtoA, smooProtoB));
249
250 Level level; // level.SetupPhase(true);
251 TestHelpers::TestFactory<SC, LO, GO, NO>::createSingleLevelHierarchy(level);
252
253 level.Request("PreSmoother", smooFact.get());
254 level.Request("PostSmoother", smooFact.get());
255
256 smooFact->BuildSmoother(level, preOrPost);
257
258 testBuildCheck(smooFact, level, smooProtoA, smooProtoB, preOrPost, out, success);
259 }
260
261 static void testBuildSmoother(RCP<SmootherPrototype> smooProtoA, RCP<SmootherPrototype> smooProtoB, Teuchos::FancyOStream &out, bool &success) {
262 testBuildSmootherPreOrPost(smooProtoA, smooProtoB, MueLu::PRE, out, success);
263 testBuildSmootherPreOrPost(smooProtoA, smooProtoB, MueLu::POST, out, success);
264 testBuildSmootherPreOrPost(smooProtoA, smooProtoB, MueLu::BOTH, out, success);
265 }
266
267 static void testBuildSmootherDefaultArg(RCP<SmootherPrototype> smooProtoA, RCP<SmootherPrototype> smooProtoB, Teuchos::FancyOStream &out, bool &success) {
268 RCP<SmootherFactory> smooFact = rcp(new SmootherFactory(smooProtoA, smooProtoB));
269
270 Level level; // level.SetupPhase(true);
271 TestHelpers::TestFactory<SC, LO, GO, NO>::createSingleLevelHierarchy(level);
272
273 level.Request("PreSmoother", smooFact.get());
274 level.Request("PostSmoother", smooFact.get());
275
276 smooFact->BuildSmoother(level);
277
278 testBuildCheck(smooFact, level, smooProtoA, smooProtoB, MueLu::BOTH, out, success);
279 }
280
281 }; // class SmootherFactoryHelpers
282
283 ////////////////////////////////////////////////////////////////////////////////
284 // Test: Constructor_OneArg
285 ////////////////////////////////////////////////////////////////////////////////
286
287 TEUCHOS_UNIT_TEST_TEMPLATE_4_DECL(SmootherFactory, Constructor_OneArg, Scalar, LocalOrdinal, GlobalOrdinal, Node) {
288 #include <MueLu_UseShortNames.hpp>
289 MUELU_TESTING_SET_OSTREAM;
290 MUELU_TESTING_LIMIT_SCOPE(Scalar, GlobalOrdinal, Node);
291 typedef SmootherFactoryHelpers<SC, LO, GO, NO> SFH;
292 SFH::testCollection(&SFH::testConstructor1, out, success); // TEST: Valid input parameter
293 SFH::testInvalidCollection(&SFH::testInvalidConstructor1, out, success); // TEST: Invalid input parameter
294 }
295
296 ////////////////////////////////////////////////////////////////////////////////
297 // Test: Constructor_TwoArg
298 ////////////////////////////////////////////////////////////////////////////////
299
300 TEUCHOS_UNIT_TEST_TEMPLATE_4_DECL(SmootherFactory, Constructor_TwoArg, Scalar, LocalOrdinal, GlobalOrdinal, Node) {
301 #include <MueLu_UseShortNames.hpp>
302 MUELU_TESTING_SET_OSTREAM;
303 MUELU_TESTING_LIMIT_SCOPE(Scalar, GlobalOrdinal, Node);
304 typedef SmootherFactoryHelpers<SC, LO, GO, NO> SFH;
305 SFH::testCollection(&SFH::testConstructor2, out, success); // TEST: Valid input parameter
306 SFH::testInvalidCollection(&SFH::testInvalidConstructor2, out, success); // TEST: Valid input parameter
307 }
308
309 ////////////////////////////////////////////////////////////////////////////////
310 // Test: SetSmootherPrototypes_OneArg
311 ////////////////////////////////////////////////////////////////////////////////
312
313 TEUCHOS_UNIT_TEST_TEMPLATE_4_DECL(SmootherFactory, SetSmootherPrototypes_OneArg, Scalar, LocalOrdinal, GlobalOrdinal, Node) {
314 #include <MueLu_UseShortNames.hpp>
315 MUELU_TESTING_SET_OSTREAM;
316 MUELU_TESTING_LIMIT_SCOPE(Scalar, GlobalOrdinal, Node);
317 typedef SmootherFactoryHelpers<SC, LO, GO, NO> SFH;
318 SFH::testCollection(&SFH::testSetSmootherPrototypes1, out, success); // TEST: Valid input parameter
319 SFH::testInvalidCollection(&SFH::testInvalidSetSmootherPrototypes1, out, success); // TEST: Invalid input parameter
320 }
321
322 ////////////////////////////////////////////////////////////////////////////////
323 // Test: SetSmootherPrototypes_TwoArg
324 ////////////////////////////////////////////////////////////////////////////////
325
326 TEUCHOS_UNIT_TEST_TEMPLATE_4_DECL(SmootherFactory, SetSmootherPrototypes_TwoArg, Scalar, LocalOrdinal, GlobalOrdinal, Node) {
327 #include <MueLu_UseShortNames.hpp>
328 MUELU_TESTING_SET_OSTREAM;
329 MUELU_TESTING_LIMIT_SCOPE(Scalar, GlobalOrdinal, Node);
330 typedef SmootherFactoryHelpers<SC, LO, GO, NO> SFH;
331 SFH::testCollection(&SFH::testSetSmootherPrototypes2, out, success); // TEST: Valid input parameter
332 SFH::testInvalidCollection(&SFH::testInvalidSetSmootherPrototypes2, out, success); // TEST: Invalid input parameter
333 }
334
335 ////////////////////////////////////////////////////////////////////////////////
336 // Test: GetSmootherPrototypes
337 ////////////////////////////////////////////////////////////////////////////////
338
339 /*
340 TEUCHOS_UNIT_TEST_TEMPLATE_4_DECL(SmootherFactory, GetSmootherPrototypes, Scalar, LocalOrdinal, GlobalOrdinal, Node)
341 {
342 // Already heavily tested by previous tests.
343 }
344 */
345
346 ////////////////////////////////////////////////////////////////////////////////
347 // Test: DeclareInput
348 ////////////////////////////////////////////////////////////////////////////////
349
350 /*
351 TEUCHOS_UNIT_TEST_TEMPLATE_4_DECL(SmootherFactory, DeclareInput, Scalar, LocalOrdinal, GlobalOrdinal, Node)
352 {
353 // TODO:
354 // Create a level, call DeclareInput() and Build() and then check that every counter == 0 at the end
355 // Same test have to be done for each factory...
356 }
357 */
358
359 ////////////////////////////////////////////////////////////////////////////////
360 // Test: Build
361 ////////////////////////////////////////////////////////////////////////////////
362
363 TEUCHOS_UNIT_TEST_TEMPLATE_4_DECL(SmootherFactory, Build, Scalar, LocalOrdinal, GlobalOrdinal, Node) {
364 #include <MueLu_UseShortNames.hpp>
365 MUELU_TESTING_SET_OSTREAM;
366 MUELU_TESTING_LIMIT_SCOPE(Scalar, GlobalOrdinal, Node);
367 typedef SmootherFactoryHelpers<SC, LO, GO, NO> SFH;
368 SFH::testCollection(&SFH::testBuild, out, success);
369 }
370
371 ////////////////////////////////////////////////////////////////////////////////
372 // Test: BuildSmoother
373 ////////////////////////////////////////////////////////////////////////////////
374
375 TEUCHOS_UNIT_TEST_TEMPLATE_4_DECL(SmootherFactory, BuildSmoother, Scalar, LocalOrdinal, GlobalOrdinal, Node) {
376 #include <MueLu_UseShortNames.hpp>
377 MUELU_TESTING_SET_OSTREAM;
378 MUELU_TESTING_LIMIT_SCOPE(Scalar, GlobalOrdinal, Node);
379 typedef SmootherFactoryHelpers<SC, LO, GO, NO> SFH;
380 SFH::testCollection(&SFH::testBuildSmootherDefaultArg, out, success); // TEST: default arg
381 SFH::testCollection(&SFH::testBuildSmoother, out, success); // TEST: PRE, POST and BOTH
382 }
383
384 ////////////////////////////////////////////////////////////////////////////////
385 // Test: Compilation
386 ////////////////////////////////////////////////////////////////////////////////
387
388 // Make sure that Constructors and SetSmootherPrototypes() are not using references as input arguments
389 // (to allow Teuchos::null as input arguments)
390 // TODO: const ref?
391 TEUCHOS_UNIT_TEST_TEMPLATE_4_DECL(SmootherFactory, Compilation, Scalar, LocalOrdinal, GlobalOrdinal, Node) {
392 #include <MueLu_UseShortNames.hpp>
393 MUELU_TESTING_SET_OSTREAM;
394 MUELU_TESTING_LIMIT_SCOPE(Scalar, GlobalOrdinal, Node);
395 typedef SmootherFactoryHelpers<SC, LO, GO, NO> SFH;
396 SmootherFactory smooFact1(Teuchos::null);
397 SmootherFactory smooFact2(Teuchos::null, Teuchos::null);
398 smooFact1.SetSmootherPrototypes(Teuchos::null);
399 smooFact1.SetSmootherPrototypes(Teuchos::null, Teuchos::null);
400 }
401
402 #define MUELU_ETI_GROUP(SC, LO, GO, NO) \
403 TEUCHOS_UNIT_TEST_TEMPLATE_4_INSTANT(SmootherFactory, Constructor_OneArg, SC, LO, GO, NO) \
404 TEUCHOS_UNIT_TEST_TEMPLATE_4_INSTANT(SmootherFactory, Constructor_TwoArg, SC, LO, GO, NO) \
405 TEUCHOS_UNIT_TEST_TEMPLATE_4_INSTANT(SmootherFactory, SetSmootherPrototypes_OneArg, SC, LO, GO, NO) \
406 TEUCHOS_UNIT_TEST_TEMPLATE_4_INSTANT(SmootherFactory, SetSmootherPrototypes_TwoArg, SC, LO, GO, NO) \
407 TEUCHOS_UNIT_TEST_TEMPLATE_4_INSTANT(SmootherFactory, Build, SC, LO, GO, NO) \
408 TEUCHOS_UNIT_TEST_TEMPLATE_4_INSTANT(SmootherFactory, BuildSmoother, SC, LO, GO, NO) \
409 TEUCHOS_UNIT_TEST_TEMPLATE_4_INSTANT(SmootherFactory, Compilation, SC, LO, GO, NO)
410 #include <MueLu_ETI_4arg.hpp>
411
412 } // namespace SmootherFactory
413
414 } // namespace MueLuTests
415
416 // TODO: advance reuse test
417 // TODO: remove some useless RCP in helper function prototypes?