Debian Codegraph @

packages/muelu/test/contact/SegregatedAggFromFactoryBlockedA.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 // Test
11 #include "contact_Helpers.hpp"
12
13 // MueLu
14 #include "MueLu_AmalgamationFactory.hpp"
15 #include "MueLu_BlockedCoarseMapFactory.hpp"
16 #include "MueLu_BlockedPFactory.hpp"
17 #include "MueLu_BlockedRAPFactory.hpp"
18 #include "MueLu_CoalesceDropFactory.hpp"
19 #include "MueLu_CoarseMapFactory.hpp"
20 #include "MueLu_CreateXpetraPreconditioner.hpp"
21 #include "MueLu_FactoryManager.hpp"
22 #include "MueLu_GenericRFactory.hpp"
23 #include "MueLu_InterfaceAggregationFactory.hpp"
24 #include "MueLu_MapTransferFactory.hpp"
25 #include "MueLu_NullspaceFactory.hpp"
26 #include "MueLu_ParameterListInterpreter.hpp"
27 #include "MueLu_SegregatedAFactory.hpp"
28 #include "MueLu_SubBlockAFactory.hpp"
29 #include "MueLu_TentativePFactory.hpp"
30 #include "MueLu_UncoupledAggregationFactory.hpp"
31
32 // Teuchos
33 #include <Teuchos_XMLParameterListHelpers.hpp>
34
35 // Xpetra
36 #include <Xpetra_BlockedMap.hpp>
37 #include <Xpetra_MatrixUtils.hpp>
38 #include <Xpetra_MapFactory.hpp>
39 #include <Xpetra_StridedMapFactory.hpp>
40
41 template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
42 int main_(Teuchos::CommandLineProcessor &clp, Xpetra::UnderlyingLib &lib, int argc, char *argv[]) {
43 #include "MueLu_UseShortNames.hpp"
44
45 // The SegregatedAFactory tests only work with real Scalar types,
46 if (Teuchos::ScalarTraits<Scalar>::isComplex) return EXIT_SUCCESS;
47
48 using Teuchos::Array;
49 using Teuchos::ArrayRCP;
50 using Teuchos::ParameterList;
51 using Teuchos::RCP;
52 using Teuchos::rcp;
53
54 using ST = Teuchos::ScalarTraits<Scalar>;
55 using GST = Teuchos::ScalarTraits<GO>;
56
57 RCP<const Teuchos::Comm<int>> comm = Teuchos::DefaultComm<int>::getComm();
58 auto numRanks = comm->getSize();
59
60 std::string probName = "";
61 clp.setOption("probName", &probName, "Short name of the problem. Used to read-in the problem from files.");
62 GO numGlobalDofPrimal = -GST::one();
63 clp.setOption("nPrimalDof", &numGlobalDofPrimal, "total number of primal Dof");
64 GO numGlobalDofDual = -GST::one();
65 clp.setOption("nDualDof", &numGlobalDofDual, "total number of dual Dof");
66 int numPrimalDofPerNode = -1;
67 clp.setOption("numPrimalDofPerNode", &numPrimalDofPerNode, "number of primal Dof per mesh node");
68 int numDualDofPerNode = -1;
69 clp.setOption("numDualDofPerNode", &numDualDofPerNode, "number of dual Dof per interface node");
70
71 clp.recogniseAllOptions(true);
72 switch (clp.parse(argc, argv)) {
73 case Teuchos::CommandLineProcessor::PARSE_HELP_PRINTED:
74 return EXIT_SUCCESS;
75 case Teuchos::CommandLineProcessor::PARSE_ERROR:
76 case Teuchos::CommandLineProcessor::PARSE_UNRECOGNIZED_OPTION:
77 return EXIT_FAILURE;
78 case Teuchos::CommandLineProcessor::PARSE_SUCCESSFUL:
79 break;
80 }
81
82 TEUCHOS_TEST_FOR_EXCEPTION(probName == "", MueLu::Exceptions::InvalidArgument,
83 "Please specify a valid problem name.");
84 TEUCHOS_TEST_FOR_EXCEPTION(numGlobalDofPrimal == -GST::one(), MueLu::Exceptions::InvalidArgument,
85 "Please specify the global number of primal Dof on the command line.");
86 TEUCHOS_TEST_FOR_EXCEPTION(numGlobalDofDual == -GST::one(), MueLu::Exceptions::InvalidArgument,
87 "Please specify the global number of dual Dof on the command line.");
88 TEUCHOS_TEST_FOR_EXCEPTION(numPrimalDofPerNode == -1, MueLu::Exceptions::InvalidArgument,
89 "Please specify the number of primal Dof per mesh node on the command line.");
90 TEUCHOS_TEST_FOR_EXCEPTION(numDualDofPerNode == -1, MueLu::Exceptions::InvalidArgument,
91 "Please specify the number of dual Dof per interface node on the command line.");
92
93 const std::string matrixFileName = probName + "_block_matrix.mm";
94 const std::string nullspace1FileName = probName + "_nullspace.mm";
95 const std::string primalInterfaceMapFileName = probName + "_interface_dof_map_MPI" + std::to_string(numRanks) + ".mm";
96 const std::string dropMap1Name = "dropMap1";
97 const std::string dropMap2Name = "dropMap2";
98 const std::string droppingScheme = "map-pair";
99
100 /// Rowmaps
101 // Primal dofmap
102 std::vector<size_t> stridingInfoPrimal;
103 stridingInfoPrimal.push_back(numPrimalDofPerNode);
104 RCP<const StridedMap> StridedDofRowMapPrimal = StridedMapFactory::Build(lib, numGlobalDofPrimal,
105 Teuchos::ScalarTraits<GO>::zero(),
106 stridingInfoPrimal, comm, -1);
107
108 // Dual dofmap
109 std::vector<size_t> stridingInfoDual;
110 stridingInfoDual.push_back(numDualDofPerNode);
111 RCP<const StridedMap> StridedDofRowMapDual = StridedMapFactory::Build(lib, numGlobalDofDual,
112 Teuchos::ScalarTraits<GO>::zero(),
113 stridingInfoDual, comm, -1, numGlobalDofPrimal);
114
115 // Construct the blocked map of the global system
116 std::vector<RCP<const Map>> rowmaps;
117 rowmaps.push_back(StridedDofRowMapPrimal);
118 rowmaps.push_back(StridedDofRowMapDual);
119 RCP<const Map> fullRowMap = MapUtils::concatenateMaps(rowmaps);
120 RCP<const BlockedMap> blockedMap = rcp(new BlockedMap(fullRowMap, rowmaps));
121
122 /// Matrix A
123 // Read the matrix from file and transform it into a block matrix
124 RCP<Matrix> mat = Xpetra::IO<SC, LO, GO, NO>::Read(matrixFileName, fullRowMap);
125 RCP<MapExtractor> rangeMapExtractor = Xpetra::MapExtractorFactory<SC, LO, GO, NO>::Build(fullRowMap, rowmaps);
126 RCP<BlockedCrsMatrix> blockedMatrix = Xpetra::MatrixUtils<SC, LO, GO, NO>::SplitMatrix(*mat, rangeMapExtractor,
127 rangeMapExtractor);
128
129 blockedMatrix->fillComplete();
130
131 /// Nullspaces
132 // Read the nullspace vector of the (0,0)-block from file
133 RCP<MultiVector> nullspace1 = Xpetra::IO<SC, LO, GO, NO>::ReadMultiVector(nullspace1FileName, StridedDofRowMapPrimal);
134
135 // Create the default nullspace vector of the (1,1)-block
136 RCP<MultiVector> nullspace2 = MultiVectorFactory::Build(StridedDofRowMapDual, 2, true);
137 const int dimNS = 2;
138 for (int dim = 0; dim < dimNS; ++dim) {
139 ArrayRCP<Scalar> nsValues = nullspace2->getDataNonConst(dim);
140 const int numBlocks = nsValues.size() / dimNS;
141 for (int j = 0; j < numBlocks; ++j)
142 nsValues[j * dimNS + dim] = Teuchos::ScalarTraits<Scalar>::one();
143 }
144
145 // Read the interface integration side dof row map from file
146 TEUCHOS_ASSERT(!primalInterfaceMapFileName.empty());
147 RCP<const Map> primalInterfaceDofMap = Xpetra::IO<SC, LO, GO, NO>::ReadMap(primalInterfaceMapFileName, lib, comm);
148 // Create the interface projection side dof row map (for map-pair for matrix filtering in segregatedAFactory)
149 std::vector<GO> projection_side_map_entries{0, 1, 6, 7, 22, 23};
150 RCP<const Map> projection_side_map = Xpetra::MapFactory<LO, GO, NO>::Build(lib, projection_side_map_entries.size(), projection_side_map_entries, 0,
151 comm);
152
153 ///////////// Factories Definitions /////////////
154 /// Factories: level 0
155 /// Block (0,0)
156 // define SubBlockAFactory for block (0,0) that provides Matrix A to SegregatedAFactory
157 RCP<SubBlockAFactory> subBlockAFact00LevelZero = rcp(new SubBlockAFactory());
158 subBlockAFact00LevelZero->SetFactory("A", MueLu::NoFactory::getRCP());
159 subBlockAFact00LevelZero->SetParameter("block row", Teuchos::ParameterEntry(0));
160 subBlockAFact00LevelZero->SetParameter("block col", Teuchos::ParameterEntry(0));
161
162 // define segregatedAFactory that provides Matrix A to CoalesceDropFactory
163 RCP<SegregatedAFactory> segregatedAFactLevelZero = rcp(new SegregatedAFactory());
164 segregatedAFactLevelZero->SetFactory("A", subBlockAFact00LevelZero);
165 segregatedAFactLevelZero->SetParameter("droppingScheme", Teuchos::ParameterEntry(droppingScheme));
166 segregatedAFactLevelZero->SetFactory("dropMap1", MueLu::NoFactory::getRCP());
167 segregatedAFactLevelZero->SetFactory("dropMap2", MueLu::NoFactory::getRCP());
168
169 // define coarse Map factory
170 RCP<CoarseMapFactory> coarseMapFact00LevelZero = rcp(new CoarseMapFactory());
171
172 // define amalgamation factory
173 RCP<AmalgamationFactory> amalgFact00LevelZero = rcp(new AmalgamationFactory());
174 amalgFact00LevelZero->SetFactory("A", subBlockAFact00LevelZero);
175
176 // define CoalesceDropFactory that provides Graph
177 RCP<CoalesceDropFactory> dropFact00LevelZero = rcp(new CoalesceDropFactory());
178 dropFact00LevelZero->SetFactory("UnAmalgamationInfo", amalgFact00LevelZero);
179 dropFact00LevelZero->SetFactory("A", segregatedAFactLevelZero);
180
181 // Setup aggregation factory
182 RCP<UncoupledAggregationFactory> uncoupledAggFactLevelZero = rcp(new UncoupledAggregationFactory());
183 uncoupledAggFactLevelZero->SetFactory("Graph", dropFact00LevelZero);
184 uncoupledAggFactLevelZero->SetFactory("DofsPerNode", dropFact00LevelZero);
185 uncoupledAggFactLevelZero->SetOrdering("graph");
186 uncoupledAggFactLevelZero->SetParameter("aggregation: min agg size", Teuchos::ParameterEntry(2));
187 uncoupledAggFactLevelZero->SetParameter("aggregation: max agg size", Teuchos::ParameterEntry(3));
188
189 // Setup TentativePFactory
190 RCP<TentativePFactory> tentativePFact00LevelZero = rcp(new TentativePFactory());
191 tentativePFact00LevelZero->SetFactory("A", segregatedAFactLevelZero);
192 tentativePFact00LevelZero->SetFactory("Aggregates", uncoupledAggFactLevelZero);
193 tentativePFact00LevelZero->SetFactory("CoarseMap", coarseMapFact00LevelZero);
194 tentativePFact00LevelZero->SetParameter("tentative: calculate qr", Teuchos::ParameterEntry(true));
195 tentativePFact00LevelZero->SetParameter("tentative: build coarse coordinates", Teuchos::ParameterEntry(false));
196
197 // Setup nullspace Factory
198 RCP<NullspaceFactory> nullspace1FactLevelZero = rcp(new NullspaceFactory());
199 nullspace1FactLevelZero->SetParameter("Fine level nullspace", Teuchos::ParameterEntry(std::string("Nullspace1")));
200 nullspace1FactLevelZero->SetFactory("Nullspace1", tentativePFact00LevelZero);
201
202 /// Block (1,1)
203 // SubBlockAFact for block (1,1)
204 RCP<SubBlockAFactory> subBlockAFact11LevelZero = rcp(new SubBlockAFactory());
205 subBlockAFact11LevelZero->SetFactory("A", MueLu::NoFactory::getRCP());
206 subBlockAFact11LevelZero->SetParameter("block row", Teuchos::ParameterEntry(1));
207 subBlockAFact11LevelZero->SetParameter("block col", Teuchos::ParameterEntry(1));
208
209 /// Block (0,1) (off-diagonal) for InterfaceAggregationFactory
210 // SubBlockAFact for block (0,1)
211 RCP<SubBlockAFactory> subBlockAFact01LevelZero = rcp(new SubBlockAFactory);
212 subBlockAFact01LevelZero->SetFactory("A", MueLu::NoFactory::getRCP());
213 subBlockAFact01LevelZero->SetParameter("block row", Teuchos::ParameterEntry(0));
214 subBlockAFact01LevelZero->SetParameter("block col", Teuchos::ParameterEntry(1));
215
216 // define BlockedCoarseMapFactory
217 RCP<BlockedCoarseMapFactory> coarseMapFact01LevelZero = rcp(new BlockedCoarseMapFactory());
218
219 // define factory for interface aggregates
220 RCP<InterfaceAggregationFactory> interfaceAggFactLevelZero = rcp(new InterfaceAggregationFactory());
221 interfaceAggFactLevelZero->SetFactory("A", subBlockAFact01LevelZero);
222 interfaceAggFactLevelZero->SetFactory("Aggregates", uncoupledAggFactLevelZero);
223 interfaceAggFactLevelZero->SetParameter("Dual/primal mapping strategy",
224 Teuchos::ParameterEntry(std::string("dof-based")));
225 interfaceAggFactLevelZero->SetFactory("Primal interface DOF map", MueLu::NoFactory::getRCP());
226
227 // define tentativeP Factory
228 RCP<TentativePFactory> tentativePFact01LevelZero = rcp(new TentativePFactory());
229 tentativePFact01LevelZero->SetFactory("A", subBlockAFact11LevelZero);
230 tentativePFact01LevelZero->SetFactory("Aggregates", interfaceAggFactLevelZero);
231 tentativePFact01LevelZero->SetFactory("CoarseMap", coarseMapFact01LevelZero);
232 tentativePFact01LevelZero->SetFactory("UnAmalgamationInfo", interfaceAggFactLevelZero);
233 tentativePFact01LevelZero->SetParameter("tentative: calculate qr", Teuchos::ParameterEntry(true));
234 tentativePFact01LevelZero->SetParameter("tentative: build coarse coordinates", Teuchos::ParameterEntry(false));
235
236 // define Factory for nullspace2
237 RCP<NullspaceFactory> nullspace2FactLevelZero = rcp(new NullspaceFactory());
238 nullspace2FactLevelZero->SetParameter("Fine level nullspace", Teuchos::ParameterEntry(std::string("Nullspace2")));
239 nullspace2FactLevelZero->SetFactory("Nullspace2", tentativePFact01LevelZero);
240
241 // define CoarseMapFact01 dependencies
242 coarseMapFact01LevelZero->SetFactory("Aggregates", interfaceAggFactLevelZero);
243 coarseMapFact01LevelZero->SetFactory("CoarseMap", coarseMapFact00LevelZero);
244
245 /// Factory Managers
246 // First group
247 RCP<FactoryManager> M1LevelZero = rcp(new FactoryManager());
248 M1LevelZero->SetFactory("A", subBlockAFact00LevelZero);
249 M1LevelZero->SetFactory("P", tentativePFact00LevelZero);
250 M1LevelZero->SetFactory("Aggregates", uncoupledAggFactLevelZero);
251 M1LevelZero->SetFactory("Nullspace", nullspace1FactLevelZero);
252 M1LevelZero->SetFactory("CoarseMap", coarseMapFact00LevelZero);
253 M1LevelZero->SetFactory("UnAmalgamationInfo", amalgFact00LevelZero);
254
255 // Second group
256 RCP<FactoryManager> M2LevelZero = rcp(new FactoryManager());
257 M2LevelZero->SetFactory("A", subBlockAFact11LevelZero);
258 M2LevelZero->SetFactory("P", tentativePFact01LevelZero);
259 M2LevelZero->SetFactory("Aggregates", interfaceAggFactLevelZero);
260 M2LevelZero->SetFactory("Nullspace", nullspace2FactLevelZero);
261 M2LevelZero->SetFactory("CoarseMap", coarseMapFact01LevelZero);
262
263 /// Blocked Transfer Operators
264 RCP<BlockedPFactory> blockedPFactLevelZero = rcp(new BlockedPFactory);
265 blockedPFactLevelZero->AddFactoryManager(M1LevelZero);
266 blockedPFactLevelZero->AddFactoryManager(M2LevelZero);
267
268 RCP<GenericRFactory> blockedRFactLevelZero = rcp(new GenericRFactory());
269 blockedRFactLevelZero->SetFactory("P", blockedPFactLevelZero);
270
271 RCP<BlockedRAPFactory> blockedRAPFactLevelZero = rcp(new BlockedRAPFactory());
272 blockedRAPFactLevelZero->SetFactory("P", blockedPFactLevelZero);
273 blockedRAPFactLevelZero->SetFactory("R", blockedRFactLevelZero);
274
275 /// Main Factory Manager
276 RCP<FactoryManager> managerLevelZero = rcp(new FactoryManager());
277 managerLevelZero->SetFactory("P", blockedPFactLevelZero);
278 managerLevelZero->SetFactory("R", blockedRFactLevelZero);
279 managerLevelZero->SetFactory("A", blockedRAPFactLevelZero);
280 managerLevelZero->SetFactory(dropMap1Name, MueLu::NoFactory::getRCP());
281 managerLevelZero->SetFactory(dropMap2Name, MueLu::NoFactory::getRCP());
282 managerLevelZero->SetKokkosRefactor(false);
283
284 ///////////// Level Definition /////////////
285 RCP<Level> levelZero = rcp(new Level());
286 RCP<Level> levelOne = rcp(new Level());
287
288 levelZero->SetLevelID(0);
289 levelOne->SetLevelID(1);
290 levelOne->SetPreviousLevel(levelZero);
291
292 TEUCHOS_ASSERT_EQUALITY(levelZero->GetLevelID(), 0);
293 TEUCHOS_ASSERT_EQUALITY(levelOne->GetLevelID(), 1);
294
295 levelZero->Set("A", Teuchos::rcp_dynamic_cast<Matrix>(blockedMatrix));
296 levelZero->Set("Nullspace1", nullspace1);
297 levelZero->Set("Nullspace2", nullspace2);
298 levelZero->Set("Primal interface DOF map", primalInterfaceDofMap);
299 levelZero->Set(dropMap1Name, primalInterfaceDofMap);
300 levelZero->Set(dropMap2Name, projection_side_map);
301
302 TEUCHOS_ASSERT(levelZero->IsAvailable("A", MueLu::NoFactory::get()));
303 TEUCHOS_ASSERT(levelZero->IsAvailable("Nullspace1", MueLu::NoFactory::get()));
304 TEUCHOS_ASSERT(levelZero->IsAvailable("Nullspace2", MueLu::NoFactory::get()));
305 TEUCHOS_ASSERT(levelZero->IsAvailable("Primal interface DOF map", MueLu::NoFactory::get()));
306 TEUCHOS_ASSERT(levelZero->IsAvailable(dropMap1Name, MueLu::NoFactory::get()));
307 TEUCHOS_ASSERT(levelZero->IsAvailable(dropMap2Name, MueLu::NoFactory::get()));
308
309 levelZero->SetFactoryManager(managerLevelZero);
310
311 ///////////// Request and build level info /////////////
312 // Check level 0
313 levelZero->Request("Aggregates", uncoupledAggFactLevelZero.get());
314 levelOne->Request("A", blockedRAPFactLevelZero.get());
315 levelOne->Request("P", blockedPFactLevelZero.get());
316 TEUCHOS_ASSERT(levelZero->IsRequested("Aggregates", uncoupledAggFactLevelZero.get()));
317 TEUCHOS_ASSERT(levelOne->IsRequested("A", blockedRAPFactLevelZero.get()));
318 TEUCHOS_ASSERT(levelOne->IsRequested("P", blockedPFactLevelZero.get()));
319 blockedRAPFactLevelZero->Build(*levelZero, *levelOne);
320
321 TEUCHOS_ASSERT(levelZero->IsAvailable("Aggregates", uncoupledAggFactLevelZero.get()));
322 RCP<Aggregates> primalAggsLevelZero = levelZero->Get<RCP<Aggregates>>("Aggregates", uncoupledAggFactLevelZero.get());
323 MueLuTests::checkAggregatesMapPair<SC, LO, GO, NO>(primalAggsLevelZero, stridingInfoPrimal,
324 levelZero->Get<RCP<const Map>>(dropMap1Name, MueLu::NoFactory::get()),
325 levelZero->Get<RCP<const Map>>(dropMap2Name, MueLu::NoFactory::get()));
326
327 return EXIT_SUCCESS;
328 }
329
330 //-----------------------------------------------------------
331 #define MUELU_AUTOMATIC_TEST_ETI_NAME main_
332 #include "MueLu_Test_ETI.hpp"
333
334 int main(int argc, char *argv[]) {
335 return Automatic_Test_ETI(argc, argv);
336 }