Debian Codegraph @

packages/muelu/research/luc/region_algorithms/Driver.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 // Teuchos
11 #include <Teuchos_StandardCatchMacros.hpp>
12 #include <Teuchos_XMLParameterListHelpers.hpp>
13
14 #include <MueLu.hpp>
15 #include <MueLu_ParameterListInterpreter.hpp> // TODO: move into MueLu.hpp
16 #include <MueLu_Utilities.hpp>
17 #include <MueLu_AmalgamationFactory.hpp>
18 #include <MueLu_CoalesceDropFactory.hpp>
19 #include <MueLu_UncoupledAggregationFactory.hpp>
20 #include <MueLu_CoarseMapFactory.hpp>
21 #include <MueLu_TentativePFactory.hpp>
22
23 #include <MueLu_CreateXpetraPreconditioner.hpp>
24
25 #ifdef HAVE_MUELU_BELOS
26 #include <BelosConfigDefs.hpp>
27 #include <BelosLinearProblem.hpp>
28 #include <BelosBlockCGSolMgr.hpp>
29 #include <BelosPseudoBlockGmresSolMgr.hpp>
30 #include <BelosXpetraAdapter.hpp> // => This header defines Belos::XpetraOp
31 #include <BelosMueLuAdapter.hpp> // => This header defines Belos::MueLuOp
32 #endif
33
34 template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
35 void createTwoLevelHierarchy(MueLu::Level& fineLevel, MueLu::Level& coarseLevel,
36 Teuchos::RCP<typename Xpetra::Matrix<Scalar, LocalOrdinal, GlobalOrdinal, Node> > A);
37
38 template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
39 int main_(Teuchos::CommandLineProcessor& clp, Xpetra::UnderlyingLib lib, int argc, char* argv[]) {
40 #include <MueLu_UseShortNames.hpp>
41
42 using Teuchos::Array;
43 using Teuchos::ArrayView;
44 using Teuchos::RCP;
45 using Teuchos::rcp;
46 using Teuchos::TimeMonitor;
47 using Teuchos::tuple;
48
49 typedef Tpetra::Map<LO, GO, NO> map_type;
50 typedef Tpetra::CrsMatrix<SC, LO, GO, NO> crs_matrix_type;
51
52 typedef typename crs_matrix_type::scalar_type scalar_type;
53 typedef typename crs_matrix_type::local_ordinal_type local_ordinal_type;
54 typedef typename crs_matrix_type::global_ordinal_type global_ordinal_type;
55 typedef typename crs_matrix_type::node_type node_type;
56 typedef typename crs_matrix_type::mag_type magnitude_type;
57
58 bool success = false;
59 bool verbose = true;
60
61 try {
62 RCP<const Teuchos::Comm<int> > comm = Teuchos::DefaultComm<int>::getComm();
63 const local_ordinal_type myRank = comm->getRank();
64
65 // Manage the way output stream works
66 RCP<Teuchos::FancyOStream> fancy = Teuchos::fancyOStream(Teuchos::rcpFromRef(std::cout));
67 fancy->setShowAllFrontMatter(false).setShowProcRank(true);
68 Teuchos::FancyOStream& out = *fancy;
69 // out.setOutputToRootOnly(0);
70 // // Do not print anything
71 // Teuchos::oblackholestream blackhole;
72 // std::ostream& out = blackhole;
73
74 // =========================================================================
75 // Parameters initialization
76 // =========================================================================
77
78 // GO nx = 100, ny = 100, nz = 100;
79 // Galeri::Xpetra::Parameters<GO> matrixParameters(clp, nx, ny, nz, "Laplace2D"); // manage parameters of the test case
80 Xpetra::Parameters xpetraParameters(clp); // manage parameters of Xpetra
81
82 std::string xmlFileName = "driver.xml";
83 clp.setOption("xml", &xmlFileName, "read parameters from a file. Otherwise, this example uses by default 'scalingTest.xml'");
84
85 switch (clp.parse(argc, argv)) {
86 case Teuchos::CommandLineProcessor::PARSE_HELP_PRINTED: return EXIT_SUCCESS; break;
87 case Teuchos::CommandLineProcessor::PARSE_ERROR:
88 case Teuchos::CommandLineProcessor::PARSE_UNRECOGNIZED_OPTION: return EXIT_FAILURE; break;
89 case Teuchos::CommandLineProcessor::PARSE_SUCCESSFUL: break;
90 }
91
92 global_ordinal_type gNumCompFineGIDs = 10, lCompFineGIDOffset = 0;
93 global_ordinal_type lRegFineGIDOffset = 0; // gNumRegFineGIDs = 11,
94 local_ordinal_type lNumCompFineGIDs = 0, lNumRegFineGIDs = 0;
95 local_ordinal_type lNumRegCoarseGIDs = 0; // lNumCompCoarseGIDs = 0,
96 if (myRank == 0) {
97 lNumCompFineGIDs = 7;
98 lNumRegFineGIDs = 7;
99
100 lCompFineGIDOffset = 0;
101 lRegFineGIDOffset = 0;
102
103 // lNumCompCoarseGIDs = 3;
104 lNumRegCoarseGIDs = 3;
105 } else if (myRank == 1) {
106 lNumCompFineGIDs = 3;
107 lNumRegFineGIDs = 4;
108
109 lCompFineGIDOffset = 7;
110 lRegFineGIDOffset = 6;
111
112 // lNumCompCoarseGIDs = 1;
113 lNumRegCoarseGIDs = 2;
114 }
115
116 // The initial focus is on getting rowMaps as they are also used as rangeMap and potentially
117 // domainMaps...
118 Array<global_ordinal_type> fineCompRowGIDs(lNumCompFineGIDs);
119 for (local_ordinal_type dof = 0; dof < lNumCompFineGIDs; ++dof) {
120 fineCompRowGIDs[dof] = lCompFineGIDOffset + dof;
121 }
122 out << "fineCompRowGIDs: " << fineCompRowGIDs << std::endl;
123 RCP<map_type> fineCompRowMap = rcp(new map_type(10, fineCompRowGIDs, 0, comm));
124
125 // Now the columnMap for the initial composite operator is needed
126 Array<global_ordinal_type> fineCompColGIDs(lNumCompFineGIDs + 1);
127 if (myRank == 0) {
128 for (local_ordinal_type dof = 0; dof < lNumCompFineGIDs + 1; ++dof) {
129 fineCompColGIDs[dof] = lCompFineGIDOffset + dof;
130 }
131 } else if (myRank == 1) {
132 for (local_ordinal_type dof = 0; dof < lNumCompFineGIDs + 1; ++dof) {
133 fineCompColGIDs[dof] = lCompFineGIDOffset - 1 + dof;
134 }
135 }
136 out << "fineCompColGIDs: " << fineCompColGIDs << std::endl;
137 RCP<map_type> fineCompColMap = rcp(new map_type(12, fineCompColGIDs, 0, comm));
138
139 // Create the matrix using our maps and assuming at most 3 entries per row
140 RCP<crs_matrix_type> compA(new crs_matrix_type(fineCompRowMap, fineCompColMap, 3));
141
142 // Now each row needs to be filled
143 const scalar_type one = static_cast<scalar_type>(1.0);
144 const scalar_type two = static_cast<scalar_type>(2.0);
145 const scalar_type negOne = static_cast<scalar_type>(-1.0);
146 for (local_ordinal_type lclRow = 0; lclRow < static_cast<local_ordinal_type>(lNumCompFineGIDs); ++lclRow) {
147 const global_ordinal_type gblRow = fineCompRowMap->getGlobalElement(lclRow);
148 // A(0, 0) = [1]
149 if (gblRow == 0) {
150 compA->insertGlobalValues(gblRow,
151 tuple<global_ordinal_type>(gblRow),
152 tuple<scalar_type>(one));
153 }
154 // A(N-1, N-2:N-1) = [-1, 1]
155 else if (gblRow == gNumCompFineGIDs - 1) {
156 compA->insertGlobalValues(gblRow,
157 tuple<global_ordinal_type>(gblRow - 1, gblRow),
158 tuple<scalar_type>(negOne, one));
159 }
160 // A(i, i-1:i+1) = [-1, 2, -1]
161 else {
162 compA->insertGlobalValues(gblRow,
163 tuple<global_ordinal_type>(gblRow - 1, gblRow, gblRow + 1),
164 tuple<scalar_type>(negOne, two, negOne));
165 }
166 }
167 compA->fillComplete(fineCompRowMap, fineCompRowMap);
168 // compA->print(out);
169
170 out << std::endl
171 << "Now switching to the region matrix" << std::endl;
172
173 Array<global_ordinal_type> fineRegRowGIDs(lNumRegFineGIDs);
174 for (local_ordinal_type dof = 0; dof < lNumRegFineGIDs; ++dof) {
175 fineRegRowGIDs[dof] = (lRegFineGIDOffset + dof == 6 && myRank == 1) ? 10 : lRegFineGIDOffset + dof;
176 }
177 out << "fineRegRowGIDs: " << fineRegRowGIDs << std::endl;
178 RCP<map_type> fineRegRowMap = rcp(new map_type(11, fineRegRowGIDs, 0, comm));
179
180 // Create the matrix using the simplest constructor as we assume that the region matrix
181 // has now off processor entries, hence no column map is required.
182 RCP<crs_matrix_type> regA(new crs_matrix_type(fineRegRowMap, 3));
183
184 // Now each row needs to be filled
185 for (local_ordinal_type lclRow = 0; lclRow < static_cast<local_ordinal_type>(lNumRegFineGIDs); ++lclRow) {
186 const global_ordinal_type prevGblRow = (lclRow > 0) ? fineRegRowMap->getGlobalElement(lclRow - 1) : -1;
187 const global_ordinal_type curGblRow = fineRegRowMap->getGlobalElement(lclRow);
188 const global_ordinal_type nextGblRow = (lclRow < lNumRegFineGIDs - 1) ? fineRegRowMap->getGlobalElement(lclRow + 1) : -1;
189 // A(0, 0) = [1]
190 if (lclRow == 0) {
191 if (curGblRow == 0) { // Apply Dirichlet BC to the left of the mesh
192 regA->insertGlobalValues(curGblRow,
193 tuple<global_ordinal_type>(curGblRow),
194 tuple<scalar_type>(one));
195 } else { // Here I hard code the column associated with "-1" because I'm lazy...
196 regA->insertGlobalValues(curGblRow,
197 tuple<global_ordinal_type>(curGblRow, nextGblRow),
198 tuple<scalar_type>(one, negOne));
199 }
200 }
201 // A(N-1, N-2:N-1) = [-1, 1]
202 else if (lclRow == static_cast<local_ordinal_type>(lNumRegFineGIDs - 1)) {
203 regA->insertGlobalValues(curGblRow,
204 tuple<global_ordinal_type>(prevGblRow, curGblRow),
205 tuple<scalar_type>(negOne, one));
206 }
207 // A(i, i-1:i+1) = [-1, 2, -1]
208 else {
209 regA->insertGlobalValues(curGblRow,
210 tuple<global_ordinal_type>(prevGblRow, curGblRow, nextGblRow),
211 tuple<scalar_type>(negOne, two, negOne));
212 }
213 }
214 regA->fillComplete();
215
216 Tpetra::MatrixMarket::Writer<crs_matrix_type>::
217 writeSparseFile("regA.m", regA, "regA",
218 "region representation of the operator.");
219
220 out << std::endl
221 << "Forming the prolongator" << std::endl;
222
223 MueLu::Level fineLevel, coarseLevel;
224 RCP<Xpetra::Matrix<SC, LO, GO, NO> > matA = MueLu::TpetraCrs_To_XpetraMatrix<SC, LO, GO, NO>(regA);
225 createTwoLevelHierarchy<SC, LO, GO, NO>(fineLevel, coarseLevel, matA);
226
227 // Now the prolongator needs to be created
228 Array<global_ordinal_type> coarseRegGIDs(lNumRegCoarseGIDs);
229 if (myRank == 0) {
230 coarseRegGIDs[0] = 0;
231 coarseRegGIDs[1] = 3;
232 coarseRegGIDs[2] = 6;
233 } else if (myRank == 1) {
234 coarseRegGIDs[0] = 10;
235 coarseRegGIDs[1] = 9;
236 }
237 out << "coarseRegGIDs: " << coarseRegGIDs << std::endl;
238 RCP<map_type> colMapP = rcp(new map_type(5, coarseRegGIDs, 0, comm));
239 // RCP<crs_matrix_type> regP (new crs_matrix_type (fineRegRowMap, 1));
240 RCP<crs_matrix_type> regP(new crs_matrix_type(fineRegRowMap, colMapP, 1));
241
242 // Now each row needs to be filled
243 for (local_ordinal_type lclRow = 0; lclRow < static_cast<local_ordinal_type>(lNumRegFineGIDs); ++lclRow) {
244 const global_ordinal_type gblRow = fineRegRowMap->getGlobalElement(lclRow);
245 const global_ordinal_type gblCol = colMapP->getGlobalElement((lclRow + 1) / 3);
246 regP->insertGlobalValues(gblRow,
247 tuple<global_ordinal_type>(gblCol),
248 tuple<scalar_type>(one));
249 }
250 regP->fillComplete();
251
252 Tpetra::MatrixMarket::Writer<crs_matrix_type>::
253 writeSparseFile("regP.m", regP, "regP",
254 "region representation of the prolongator.");
255
256 out << std::endl
257 << "Computing AP = AxP" << std::endl;
258
259 // The number of non-zeros per row is set to zero because it's hard to guess what it will be...
260 RCP<crs_matrix_type> regAP = rcp(new crs_matrix_type(fineRegRowMap, 0));
261 Tpetra::MatrixMatrix::
262 Multiply<scalar_type, local_ordinal_type, global_ordinal_type, node_type>(*regA, false, *regP,
263 false, *regAP);
264
265 Tpetra::MatrixMarket::Writer<crs_matrix_type>::
266 writeSparseFile("regAP.m", regAP, "regAP",
267 "region representation of AP.");
268
269 out << std::endl
270 << "Computing Ac = P'xAP" << std::endl;
271
272 // The number of non-zeros per row is set to zero because it's hard to guess what it will be...
273 RCP<crs_matrix_type> regAc = rcp(new crs_matrix_type(regP->getDomainMap(), 0));
274 Tpetra::MatrixMatrix::
275 Multiply<scalar_type, local_ordinal_type, global_ordinal_type, node_type>(*regP, true, *regAP,
276 false, *regAc);
277
278 Tpetra::MatrixMarket::Writer<crs_matrix_type>::
279 writeSparseFile("regAc.m", regAc, "regAc",
280 "region representation of Ac.");
281
282 success = true;
283 }
284 TEUCHOS_STANDARD_CATCH_STATEMENTS(verbose, std::cerr, success);
285
286 return (success ? EXIT_SUCCESS : EXIT_FAILURE);
287 } // main
288
289 template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
290 void createTwoLevelHierarchy(MueLu::Level& fineLevel, MueLu::Level& coarseLevel,
291 Teuchos::RCP<typename Xpetra::Matrix<Scalar, LocalOrdinal, GlobalOrdinal, Node> > A) {
292 #include <MueLu_UseShortNames.hpp>
293 using Teuchos::RCP;
294 using Teuchos::rcp;
295 RCP<FactoryManagerBase> factoryHandler = rcp(new FactoryManager());
296 fineLevel.SetFactoryManager(factoryHandler);
297 coarseLevel.SetFactoryManager(factoryHandler);
298
299 coarseLevel.SetPreviousLevel(rcpFromRef(fineLevel));
300
301 fineLevel.SetLevelID(0);
302 coarseLevel.SetLevelID(1);
303
304 fineLevel.SetFactoryManager(Teuchos::null); // factory manager is not used on this test
305 coarseLevel.SetFactoryManager(Teuchos::null);
306
307 A->SetFixedBlockSize(1);
308 fineLevel.Request("A");
309 fineLevel.Set("A", A);
310 fineLevel.Set("DofsPerNode", 2);
311
312 RCP<MultiVector> nullSpace = MultiVectorFactory::Build(A->getRowMap(), 1);
313 nullSpace->randomize();
314 fineLevel.Set("Nullspace", nullSpace);
315
316 RCP<AmalgamationFactory> amalgFact = rcp(new AmalgamationFactory());
317 RCP<CoalesceDropFactory> dropFact = rcp(new CoalesceDropFactory());
318 dropFact->SetFactory("UnAmalgamationInfo", amalgFact);
319 RCP<UncoupledAggregationFactory> UnCoupledAggFact = rcp(new UncoupledAggregationFactory());
320 UnCoupledAggFact->SetFactory("Graph", dropFact);
321
322 RCP<CoarseMapFactory> coarseMapFact = rcp(new CoarseMapFactory());
323 coarseMapFact->SetFactory("Aggregates", UnCoupledAggFact);
324 RCP<TentativePFactory> TentativePFact = rcp(new TentativePFactory());
325 TentativePFact->SetFactory("Aggregates", UnCoupledAggFact);
326 TentativePFact->SetFactory("UnAmalgamationInfo", amalgFact);
327 TentativePFact->SetFactory("CoarseMap", coarseMapFact);
328
329 coarseLevel.Request("P", TentativePFact.get()); // request Ptent
330 coarseLevel.Request("Nullspace", TentativePFact.get());
331 coarseLevel.Request(*TentativePFact);
332 Teuchos::ParameterList paramList;
333 paramList.set("tentative: calculate qr", false);
334 TentativePFact->SetParameterList(paramList);
335 TentativePFact->Build(fineLevel, coarseLevel);
336
337 RCP<Matrix> Ptent;
338 coarseLevel.Get("P", Ptent, TentativePFact.get());
339 }
340
341 //- -- --------------------------------------------------------
342 #define MUELU_AUTOMATIC_TEST_ETI_NAME main_
343 #include "MueLu_Test_ETI.hpp"
344
345 int main(int argc, char* argv[]) {
346 return Automatic_Test_ETI(argc, argv);
347 }