Debian Codegraph @

packages/muelu/research/max/AdditiveMG/MultiplicativeBricks.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 //
11 // To compile and run this example, Trilinos must be configured with
12 // Tpetra, Amesos2, MueLu, Ifpack2, and Belos.
13 //
14 // This example will only work with Tpetra, not Epetra.
15 //
16 // Commonly used options are
17 // --matrixType
18 // --nx
19 // --ny
20 // --xmlFile
21 //
22 // "./MueLu_Repartition_ADR.exe --help" shows all supported command line options.
23 //
24 #include <iostream>
25 #include <cmath>
26
27 // Belos provides Krylov solvers
28 #include <BelosConfigDefs.hpp>
29 #include <BelosLinearProblem.hpp>
30 #include <BelosBlockCGSolMgr.hpp>
31 #include <BelosPseudoBlockCGSolMgr.hpp>
32 #include <BelosBlockGmresSolMgr.hpp>
33 #include <BelosBiCGStabSolMgr.hpp>
34 #include <BelosTpetraAdapter.hpp>
35
36 // Galeri
37 #include <Galeri_XpetraParameters.hpp>
38 #include <Galeri_XpetraProblemFactory.hpp>
39 #include <Galeri_XpetraUtils.hpp>
40 #include <Galeri_XpetraMaps.hpp>
41
42 // ADR subdirectory
43 #include "CreateADRMatrix.hpp"
44 #include <CreateADRMatrix.hpp>
45 #include <ADRProblemFactory.hpp>
46 #include <ADR_XpetraParameters.hpp>
47
48 #include "Smooth_Prolongation.cpp"
49 //#include "coloring.hpp"
50 #include "BAP.hpp"
51 #include "CreateBrickMap.hpp"
52
53 int main(int argc, char* argv[]) {
54 // Define default types
55 typedef double scalar_type;
56 typedef int local_ordinal_type;
57 typedef int global_ordinal_type;
58 typedef Tpetra::KokkosClassic::DefaultNode::DefaultNodeType node_type;
59
60 // Convenient typedef's
61 typedef Tpetra::Operator<scalar_type, local_ordinal_type, global_ordinal_type, node_type> operator_type;
62 typedef Tpetra::CrsMatrix<scalar_type, local_ordinal_type, global_ordinal_type, node_type> crs_matrix_type;
63 typedef Tpetra::RowMatrix<scalar_type, local_ordinal_type, global_ordinal_type, node_type> row_matrix_type;
64 typedef Tpetra::Vector<scalar_type, local_ordinal_type, global_ordinal_type, node_type> vector_type;
65 typedef Tpetra::MultiVector<scalar_type, local_ordinal_type, global_ordinal_type, node_type> multivector_type;
66 typedef Tpetra::Map<local_ordinal_type, global_ordinal_type, node_type> driver_map_type;
67
68 typedef MueLu::TpetraOperator<scalar_type, local_ordinal_type, global_ordinal_type, node_type> muelu_tpetra_operator_type;
69 typedef MueLu::Utilities<scalar_type, local_ordinal_type, global_ordinal_type, node_type> MueLuUtilities;
70
71 typedef Belos::LinearProblem<scalar_type, multivector_type, operator_type> linear_problem_type;
72 typedef Belos::SolverManager<scalar_type, multivector_type, operator_type> belos_solver_manager_type;
73 typedef Belos::PseudoBlockCGSolMgr<scalar_type, multivector_type, operator_type> belos_pseudocg_manager_type;
74 typedef Belos::BlockGmresSolMgr<scalar_type, multivector_type, operator_type> belos_gmres_manager_type;
75 typedef Belos::BiCGStabSolMgr<scalar_type, multivector_type, operator_type> belos_bicgstab_manager_type;
76
77 typedef Ifpack2::Preconditioner<scalar_type, local_ordinal_type, global_ordinal_type, node_type> precond_type;
78
79 // MueLu_UseShortNames.hpp wants these typedefs.
80 typedef scalar_type Scalar;
81 typedef local_ordinal_type LocalOrdinal;
82 typedef global_ordinal_type GlobalOrdinal;
83 typedef node_type Node;
84 #include <MueLu_UseShortNames.hpp>
85
86 typedef Galeri::Xpetra::Problem<Map, CrsMatrixWrap, MultiVector> GaleriXpetraProblem;
87 typedef ADR::Xpetra::Problem<Map, CrsMatrixWrap, MultiVector> ADRXpetraProblem;
88
89 using Teuchos::RCP; // reference count pointers
90 using Teuchos::rcp; // reference count pointers
91
92 //
93 // MPI initialization using Teuchos
94 //
95
96 Teuchos::GlobalMPISession mpiSession(&argc, &argv, NULL);
97 RCP<const Teuchos::Comm<int> > comm = Teuchos::DefaultComm<int>::getComm();
98 int mypid = comm->getRank();
99 /*
100 int subCommRank[3]={0,1,2};
101 Teuchos::ArrayView<int> arraySubCommRank(subCommRank, 3);
102 auto subComm = comm->createSubcommunicator(arraySubCommRank);
103 */
104 Teuchos::CommandLineProcessor clp(false);
105
106 global_ordinal_type maxIts = 10000;
107 scalar_type tol = 1e-10;
108 std::string solverOptionsFile = "final_parser.xml";
109 std::string krylovSolverType = "bicgstab";
110
111 clp.setOption("xmlFile", &solverOptionsFile, "XML file containing MueLu solver parameters");
112 clp.setOption("maxits", &maxIts, "maximum number of Krylov iterations");
113 clp.setOption("tol", &tol, "tolerance for Krylov solver");
114 clp.setOption("krylovType", &krylovSolverType, "cg or gmres solver");
115
116 switch (clp.parse(argc, argv)) {
117 case Teuchos::CommandLineProcessor::PARSE_HELP_PRINTED: return EXIT_SUCCESS;
118 case Teuchos::CommandLineProcessor::PARSE_ERROR:
119 case Teuchos::CommandLineProcessor::PARSE_UNRECOGNIZED_OPTION: return EXIT_FAILURE;
120 case Teuchos::CommandLineProcessor::PARSE_SUCCESSFUL: break;
121 }
122
123 Teuchos::ParameterList xmlParams;
124 Teuchos::ParameterList mueluParams;
125 Teuchos::ParameterList problemParams;
126 Teuchos::updateParametersFromXmlFile(solverOptionsFile, Teuchos::inoutArg(xmlParams));
127 mueluParams = xmlParams.sublist(static_cast<const std::string>("MueLu"));
128 problemParams = xmlParams.sublist(static_cast<const std::string>("Problem"));
129
130 // Problem definition
131 std::string problem_type = problemParams.get<std::string>(static_cast<const std::string>("problem type"));
132
133 // Parameters
134
135 Scalar Lx = problemParams.get<scalar_type>(static_cast<const std::string>("domain size in x-direction"));
136 Scalar Ly = problemParams.get<scalar_type>(static_cast<const std::string>("domain size in y-direction"));
137 Scalar Lz = problemParams.get<scalar_type>(static_cast<const std::string>("domain size in z-direction"));
138
139 global_ordinal_type nx = problemParams.get<int>(static_cast<const std::string>("nodes in x-direction"));
140 global_ordinal_type ny = problemParams.get<int>(static_cast<const std::string>("nodes in y-direction"));
141 global_ordinal_type nz = problemParams.get<int>(static_cast<const std::string>("nodes in z-direction"));
142
143 global_ordinal_type number_runs = problemParams.get<int>(static_cast<const std::string>("number of runs"));
144
145 MueLu::DomainPartitioning domain;
146
147 int keep_boundary = 0;
148 Scalar stretchx = (Scalar)Lx / nx;
149 Scalar stretchy = (Scalar)Ly / ny;
150 Scalar stretchz = (Scalar)Lz / nz;
151
152 ADR::Xpetra::Parameters<GO> matrixParameters(clp, nx, ny, nz, problem_type, keep_boundary, stretchx, stretchy, stretchz); // manage parameters of the test case
153 Xpetra::Parameters xpetraParameters(clp); // manage parameters of xpetra
154
155 //
156 // Construct the problem
157 //
158
159 global_ordinal_type indexBase = 0;
160 RCP<const Map> xpetraMap;
161 std::vector<global_ordinal_type> ind;
162
163 // BRICK SIZE
164 int brick_sizex = mueluParams.get<int>(static_cast<const std::string>("aggregation: brick x size"));
165 int brick_sizey = mueluParams.get<int>(static_cast<const std::string>("aggregation: brick y size"));
166 int brick_sizez = mueluParams.get<int>(static_cast<const std::string>("aggregation: brick z size"));
167
168 // Creation of the map where processor 0 gets nothing at the fine level
169 if (comm->getSize() > 1) {
170 if (problem_type == "ADR1D")
171 createBrickMap1D(matrixParameters.GetNumGlobalElements(), ind, comm);
172
173 else if (problem_type == "ADR2D")
174 createBrickMap2D(nx, brick_sizex, brick_sizey, ind, comm);
175
176 else if (problem_type == "ADR3D")
177 createBrickMap3D(nx, ny, brick_sizex, brick_sizey, brick_sizez, ind, comm);
178
179 ind.shrink_to_fit();
180
181 Teuchos::ArrayView<const global_ordinal_type> elementList(ind);
182 xpetraMap = MapFactory::Build(Xpetra::UseTpetra, matrixParameters.GetNumGlobalElements(), elementList, indexBase, comm);
183 } else if (comm->getSize() == 1)
184 xpetraMap = MapFactory::Build(Xpetra::UseTpetra, matrixParameters.GetNumGlobalElements(), indexBase, comm);
185
186 RCP<MultiVector> coordinates;
187
188 if (problem_type == "ADR1D")
189 coordinates = Galeri::Xpetra::Utils::CreateCartesianCoordinates<scalar_type, local_ordinal_type, global_ordinal_type, Map, MultiVector>("1D", xpetraMap, matrixParameters.GetParameterList());
190 else if (problem_type == "ADR2D")
191 coordinates = Galeri::Xpetra::Utils::CreateCartesianCoordinates<scalar_type, local_ordinal_type, global_ordinal_type, Map, MultiVector>("2D", xpetraMap, matrixParameters.GetParameterList());
192 else if (problem_type == "ADR3D")
193 coordinates = Galeri::Xpetra::Utils::CreateCartesianCoordinates<scalar_type, local_ordinal_type, global_ordinal_type, Map, MultiVector>("3D", xpetraMap, matrixParameters.GetParameterList());
194
195 RCP<ADRXpetraProblem> Pr = ADR::Xpetra::BuildProblem<scalar_type, local_ordinal_type, global_ordinal_type, Map, CrsMatrixWrap, MultiVector>(matrixParameters.GetMatrixType(), xpetraMap, matrixParameters.GetParameterList());
196 RCP<Matrix> xpetraA = Pr->BuildMatrix();
197
198 RCP<crs_matrix_type> A = toTpetra(xpetraA);
199 RCP<const driver_map_type> map = toTpetra(xpetraMap);
200
201 // ===================================================
202 // Domain Decomposition Preconditioner
203 // ===================================
204
205 // Creation of the MueLu list for the DD preconditioner
206 RCP<Teuchos::ParameterList> dd_list = rcp(new Teuchos::ParameterList());
207 dd_list->setName("MueLu");
208 dd_list->set("verbosity", "low");
209 dd_list->set("number of equations", 1);
210 dd_list->set("max levels", 1);
211 dd_list->set("coarse: type", "SCHWARZ"); // FOR A ONE LEVEL PRECONDITIONER THE COARSE LEVEL IS INTERPRETED AS SMOOTHING LEVEL
212
213 Teuchos::ParameterList& dd_smooth_sublist = dd_list->sublist("coarse: params");
214 dd_smooth_sublist.set("schwarz: overlap level", 0);
215 dd_smooth_sublist.set("schwarz: combine mode", "Zero");
216 dd_smooth_sublist.set("subdomain solver name", "RILUK");
217
218 Teuchos::ParameterList& coarse_subdomain_solver = dd_smooth_sublist.sublist("subdomain solver parameters");
219 coarse_subdomain_solver.set("fact: iluk level-of-fill", 3);
220 coarse_subdomain_solver.set("fact: absolute threshold", 0.);
221 coarse_subdomain_solver.set("fact: relative threshold", 1.);
222 coarse_subdomain_solver.set("fact: relax value", 0.);
223
224 RCP<muelu_tpetra_operator_type> B_DD = MueLu::CreateTpetraPreconditioner((RCP<operator_type>)A, *dd_list);
225
226 // ===================================================
227 // Multi Grid Preconditioner
228 // ===================================
229
230 RCP<muelu_tpetra_operator_type> M;
231 M = MueLu::CreateTpetraPreconditioner((RCP<operator_type>)A, mueluParams, toTpetra(coordinates));
232
233 RCP<multivector_type> X_muelu = rcp(new multivector_type(map, 1));
234 RCP<multivector_type> B = rcp(new multivector_type(map, 1));
235 RCP<linear_problem_type> Problem_muelu;
236 X_muelu->putScalar((scalar_type)0.0);
237 B->randomize();
238
239 Problem_muelu = rcp(new linear_problem_type(A, X_muelu, B));
240
241 RCP<Teuchos::ParameterList> belosList = rcp(new Teuchos::ParameterList());
242 belosList->set("Maximum Iterations", maxIts); // Maximum number of iterations allowed
243 belosList->set("Convergence Tolerance", tol); // Relative convergence tolerance requested
244 // belosList->set("Verbosity", Belos::Errors + Belos::Warnings + Belos::StatusTestDetails);
245 belosList->set("Verbosity", Belos::Errors);
246 belosList->set("Output Frequency", 1);
247 belosList->set("Output Style", Belos::Brief);
248 belosList->set("Implicit Residual Scaling", "None");
249 RCP<belos_solver_manager_type> solver;
250 if (krylovSolverType == "cg")
251 solver = rcp(new belos_pseudocg_manager_type(Problem_muelu, belosList));
252 else if (krylovSolverType == "gmres")
253 solver = rcp(new belos_gmres_manager_type(Problem_muelu, belosList));
254 else if (krylovSolverType == "bicgstab")
255 solver = rcp(new belos_bicgstab_manager_type(Problem_muelu, belosList));
256 else
257 throw std::invalid_argument("bad Krylov solver type");
258
259 for (int trial = 1; trial <= number_runs; ++trial) {
260 X_muelu->putScalar((scalar_type)0.0);
261 B->randomize();
262
263 //
264 // Set up Krylov solver and iterate.
265 //
266
267 Problem_muelu = rcp(new linear_problem_type(A, X_muelu, B));
268 Problem_muelu->setRightPrec(M);
269 Problem_muelu->setProblem();
270
271 solver->setProblem(Problem_muelu);
272 solver->solve();
273 int numIterations_muelu = solver->getNumIters();
274
275 Teuchos::Array<typename Teuchos::ScalarTraits<scalar_type>::magnitudeType> normVec_muelu(1);
276 multivector_type residual_muelu(B->getMap(), 1);
277 A->apply(*X_muelu, residual_muelu);
278 residual_muelu.update(1.0, *B, -1.0);
279 residual_muelu.norm2(normVec_muelu);
280 if (mypid == 0) {
281 std::cout << "number of iterations with MueLu preconditioner= " << numIterations_muelu << std::endl;
282 std::cout << "||Residual|| = " << normVec_muelu[0] << std::endl;
283 }
284 }
285
286 #include <Teuchos_TimeMonitor.hpp>
287 Teuchos::TimeMonitor::summarize();
288
289 return EXIT_SUCCESS;
290 }