Debian Codegraph @

packages/muelu/test/helmholtz/Helmholtz1D.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 <iostream>
11 #include <complex>
12
13 // Teuchos
14 #include <Teuchos_StandardCatchMacros.hpp>
15
16 // Xpetra and Galeri
17 #include <Xpetra_MultiVectorFactory.hpp>
18 #include <Galeri_XpetraParameters.hpp>
19 #include <Galeri_XpetraProblemFactory_Helmholtz.hpp>
20 #include <Galeri_XpetraUtils.hpp>
21 #include <Galeri_XpetraMaps.hpp>
22
23 // MueLu
24 #include "MueLu.hpp"
25 #include <MueLu_Level.hpp>
26 #include <MueLu_BaseClass.hpp>
27 #include <MueLu_Utilities.hpp>
28 #include <MueLu_UseDefaultTypesComplex.hpp>
29 #include <MueLu_MutuallyExclusiveTime.hpp>
30
31 // Belos
32 #include <BelosConfigDefs.hpp>
33 #include <BelosLinearProblem.hpp>
34 #include <BelosBlockGmresSolMgr.hpp>
35 #include <BelosXpetraAdapter.hpp>
36 #include <BelosMueLuAdapter.hpp>
37
38 int main(int argc, char *argv[]) {
39 #include <MueLu_UseShortNames.hpp>
40
41 typedef Tpetra::Vector<SC, LO, GO, NO> TVEC;
42 typedef Tpetra::MultiVector<SC, LO, GO, NO> TMV;
43 typedef Tpetra::CrsMatrix<SC, LO, GO, NO> TCRS;
44 typedef Xpetra::CrsMatrix<SC, LO, GO, NO> XCRS;
45 typedef Xpetra::TpetraCrsMatrix<SC, LO, GO, NO> XTCRS;
46 typedef Xpetra::Matrix<SC, LO, GO, NO> XMAT;
47 typedef Xpetra::CrsMatrixWrap<SC, LO, GO, NO> XWRAP;
48 typedef typename Teuchos::ScalarTraits<SC>::magnitudeType real_type;
49 typedef Xpetra::MultiVector<real_type, LO, GO, NO> RealValuedMultiVector;
50
51 typedef Belos::OperatorT<TMV> TOP;
52 typedef Belos::OperatorTraits<SC, TMV, TOP> TOPT;
53 typedef Belos::MultiVecTraits<SC, TMV> TMVT;
54 typedef Belos::LinearProblem<SC, TMV, TOP> TProblem;
55 typedef Belos::SolverManager<SC, TMV, TOP> TBelosSolver;
56 typedef Belos::BlockGmresSolMgr<SC, TMV, TOP> TBelosGMRES;
57
58 using Teuchos::RCP;
59 using Teuchos::rcp;
60 using Teuchos::TimeMonitor;
61
62 Teuchos::GlobalMPISession mpiSession(&argc, &argv, NULL);
63
64 bool success = false;
65 bool verbose = true;
66 try {
67 RCP<const Teuchos::Comm<int> > comm = Teuchos::DefaultComm<int>::getComm();
68 Teuchos::CommandLineProcessor clp(false);
69
70 GO nx, ny, nz;
71 nx = 100;
72 ny = 100;
73 nz = 100;
74 double stretchx, stretchy, stretchz, h, delta;
75 stretchx = 1.0;
76 stretchy = 1.0;
77 stretchz = 1.0;
78 h = 0.01;
79 delta = 2.0;
80 int PMLXL, PMLXR, PMLYL, PMLYR, PMLZL, PMLZR;
81 PMLXL = 10;
82 PMLXR = 10;
83 PMLYL = 10;
84 PMLYR = 10;
85 PMLZL = 10;
86 PMLZR = 10;
87 double omega, shift;
88 omega = 20.0 * M_PI;
89 shift = 0.5;
90
91 Galeri::Xpetra::Parameters<GO> matrixParameters(clp, nx, ny, nz, "Helmholtz1D", 0, stretchx, stretchy, stretchz,
92 h, delta, PMLXL, PMLXR, PMLYL, PMLYR, PMLZL, PMLZR, omega, shift);
93 Xpetra::Parameters xpetraParameters(clp);
94
95 RCP<TimeMonitor> globalTimeMonitor = rcp(new TimeMonitor(*TimeMonitor::getNewTimer("ScalingTest: S - Global Time")));
96 RCP<TimeMonitor> tm = rcp(new TimeMonitor(*TimeMonitor::getNewTimer("ScalingTest: 1 - Matrix Build")));
97
98 Teuchos::ParameterList pl = matrixParameters.GetParameterList();
99 RCP<RealValuedMultiVector> coordinates;
100 Teuchos::ParameterList galeriList;
101 galeriList.set("nx", pl.get("nx", nx));
102 galeriList.set("ny", pl.get("ny", ny));
103 galeriList.set("nz", pl.get("nz", nz));
104 RCP<const Map> map;
105
106 map = MapFactory::Build(xpetraParameters.GetLib(), matrixParameters.GetNumGlobalElements(), 0, comm);
107 coordinates = Galeri::Xpetra::Utils::CreateCartesianCoordinates<SC, LO, GO, Map, RealValuedMultiVector>("1D", map, matrixParameters.GetParameterList());
108
109 RCP<const Tpetra::Map<LO, GO, NO> > tmap = Xpetra::toTpetra(map);
110
111 Teuchos::ParameterList matrixParams = matrixParameters.GetParameterList();
112
113 // Build problem
114 RCP<Galeri::Xpetra::Problem_Helmholtz<Map, CrsMatrixWrap, MultiVector> > Pr =
115 Galeri::Xpetra::BuildProblem_Helmholtz<SC, LO, GO, Map, CrsMatrixWrap, MultiVector>(matrixParameters.GetMatrixType(), map, matrixParams);
116 RCP<Matrix> A = Pr->BuildMatrix();
117
118 RCP<MultiVector> nullspace = MultiVectorFactory::Build(map, 1);
119 nullspace->putScalar((SC)1.0);
120
121 comm->barrier();
122
123 tm = Teuchos::null;
124
125 // Construct a multigrid preconditioner
126 tm = rcp(new TimeMonitor(*TimeMonitor::getNewTimer("ScalingTest: 2 - MueLu Setup")));
127
128 // Multigrid Hierarchy
129 RCP<Hierarchy> H = rcp(new Hierarchy(A));
130 H->GetLevel(0)->Set("Nullspace", nullspace);
131 FactoryManager Manager;
132 H->Setup(Manager, 0, 5);
133 // H->Write(-1,-1);
134
135 tm = Teuchos::null;
136
137 // Solve Ax = b
138 tm = rcp(new TimeMonitor(*TimeMonitor::getNewTimer("ScalingTest: 3 - LHS and RHS initialization")));
139 RCP<TVEC> X = Tpetra::createVector<SC, LO, GO, NO>(tmap);
140 RCP<TVEC> B = Tpetra::createVector<SC, LO, GO, NO>(tmap);
141 X->putScalar((SC)0.0);
142 B->putScalar((SC)0.0);
143 if (comm->getRank() == 0) {
144 B->replaceGlobalValue(0, (SC)1.0);
145 }
146
147 tm = Teuchos::null;
148
149 tm = rcp(new TimeMonitor(*TimeMonitor::getNewTimer("ScalingTest: 4 - Belos Solve")));
150
151 // Define Operator and Preconditioner
152 RCP<TOP> belosOp = rcp(new Belos::XpetraOp<SC, LO, GO, NO>(A)); // Turns a Xpetra::Matrix object into a Belos operator
153 RCP<TOP> belosPrec = rcp(new Belos::MueLuOp<SC, LO, GO, NO>(H)); // Turns a MueLu::Hierarchy object into a Belos operator
154
155 // Construct a Belos LinearProblem object
156 RCP<TProblem> belosProblem = rcp(new TProblem(belosOp, X, B));
157 belosProblem->setRightPrec(belosPrec);
158 bool set = belosProblem->setProblem();
159 if (set == false) {
160 if (comm->getRank() == 0)
161 std::cout << std::endl
162 << "ERROR: Belos::LinearProblem failed to set up correctly!" << std::endl;
163 return EXIT_FAILURE;
164 }
165
166 // Belos parameter list
167 int maxIts = 100;
168 double tol = 1e-6;
169 Teuchos::ParameterList belosList;
170 belosList.set("Maximum Iterations", maxIts); // Maximum number of iterations allowed
171 belosList.set("Convergence Tolerance", tol); // Relative convergence tolerance requested
172 belosList.set("Flexible Gmres", false); // set flexible GMRES on/off
173 belosList.set("Verbosity", Belos::Errors + Belos::Warnings + Belos::StatusTestDetails);
174 belosList.set("Output Frequency", 1);
175 belosList.set("Output Style", Belos::Brief);
176
177 // Create solver manager
178 RCP<TBelosSolver> solver = rcp(new TBelosGMRES(belosProblem, rcp(&belosList, false)));
179
180 // Perform solve
181 Belos::ReturnType ret = Belos::Unconverged;
182 ret = solver->solve();
183 if (comm->getRank() == 0)
184 std::cout << "Number of iterations performed for this solve: " << solver->getNumIters() << std::endl;
185
186 // Check convergence
187 if (ret != Belos::Converged) {
188 if (comm->getRank() == 0) std::cout << std::endl
189 << "ERROR: Belos did not converge! " << std::endl;
190 } else {
191 if (comm->getRank() == 0) std::cout << std::endl
192 << "SUCCESS: Belos converged!" << std::endl;
193 }
194
195 // Get the number of iterations for this solve.
196 if (comm->getRank() == 0)
197 std::cout << "Number of iterations performed for this solve: " << solver->getNumIters() << std::endl;
198
199 tm = Teuchos::null;
200
201 globalTimeMonitor = Teuchos::null;
202
203 TimeMonitor::summarize();
204
205 success = true;
206 }
207 TEUCHOS_STANDARD_CATCH_STATEMENTS(verbose, std::cerr, success);
208
209 return (success ? EXIT_SUCCESS : EXIT_FAILURE);
210 } // main