Debian Codegraph @

packages/muelu/test/navierstokes/Navier2DBlocked_SimpleSmoother.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 <unistd.h>
11 #include <iostream>
12 #include <fstream>
13
14 // Teuchos
15 #include <Teuchos_RCP.hpp>
16 #include <Teuchos_ParameterList.hpp>
17 #include <Teuchos_CommandLineProcessor.hpp>
18 #include <Teuchos_GlobalMPISession.hpp>
19 #include <Teuchos_DefaultComm.hpp>
20 #include <Teuchos_ArrayRCP.hpp>
21 #include <Teuchos_StandardCatchMacros.hpp>
22
23 // Epetra
24 #include <EpetraExt_CrsMatrixIn.h>
25 #include <EpetraExt_VectorIn.h>
26 #include <EpetraExt_MultiVectorIn.h>
27
28 // Xpetra
29 #include <Xpetra_Map.hpp>
30 #include <Xpetra_MapFactory.hpp>
31 #include <Xpetra_Matrix.hpp>
32 #include <Xpetra_CrsMatrixWrap.hpp>
33 #include <Xpetra_MultiVectorFactory.hpp>
34 #include <Xpetra_VectorFactory.hpp>
35 #include <Xpetra_Parameters.hpp>
36 #include <Xpetra_MapExtractorFactory.hpp>
37 #include <Xpetra_BlockedCrsMatrix.hpp>
38 #include <Xpetra_StridedMapFactory.hpp>
39 #include <Xpetra_StridedMap.hpp>
40
41 // MueLu
42 #include "MueLu_ConfigDefs.hpp"
43 #include "MueLu_Memory.hpp"
44 #include "MueLu_Hierarchy.hpp"
45 #include "MueLu_PgPFactory.hpp"
46 #include "MueLu_GenericRFactory.hpp"
47 #include "MueLu_SaPFactory.hpp"
48 #include "MueLu_TransPFactory.hpp"
49 #include "MueLu_TrilinosSmoother.hpp"
50 #include "MueLu_Utilities.hpp"
51 #include "MueLu_Exceptions.hpp"
52 #include "MueLu_Aggregates.hpp"
53 #include "MueLu_CoalesceDropFactory.hpp"
54 #include "MueLu_PreDropFunctionConstVal.hpp"
55 #include "MueLu_NullspaceFactory.hpp"
56 #include "MueLu_TentativePFactory.hpp"
57 #include "MueLu_SmootherFactory.hpp"
58 #include "MueLu_DirectSolver.hpp"
59 #include "MueLu_EpetraOperator.hpp"
60 #include "MueLu_SubBlockAFactory.hpp"
61 #include "MueLu_SimpleSmoother.hpp"
62 #include "MueLu_SchurComplementFactory.hpp"
63 #include "MueLu_Utilities.hpp"
64 #include "MueLu_TopSmootherFactory.hpp"
65 #include "MueLu_HierarchyUtils.hpp"
66 #include "MueLu_InverseApproximationFactory.hpp"
67
68 #include <Epetra_LinearProblem.h>
69 #include <AztecOO.h>
70
71 #include "Navier2D_Helpers.h"
72
73 /*!
74 * 2d Navier Stokes example (for Epetra)
75 *
76 * using block matrices
77 */
78
79 int main(int argc, char* argv[]) {
80 #if defined(HAVE_MUELU_EPETRA)
81 typedef double Scalar;
82 typedef int LocalOrdinal;
83 typedef int GlobalOrdinal;
84 typedef LocalOrdinal LO;
85 typedef GlobalOrdinal GO;
86 typedef Xpetra::EpetraNode Node;
87 #include "MueLu_UseShortNames.hpp"
88
89 using Teuchos::RCP;
90 using Teuchos::rcp;
91 using namespace MueLuTests;
92
93 Teuchos::oblackholestream blackhole;
94 Teuchos::GlobalMPISession mpiSession(&argc, &argv, &blackhole);
95
96 bool success = false;
97 bool verbose = true;
98 try {
99 RCP<const Teuchos::Comm<int> > comm = Teuchos::DefaultComm<int>::getComm();
100 RCP<Teuchos::FancyOStream> out = Teuchos::fancyOStream(Teuchos::rcpFromRef(std::cout));
101 out->setOutputToRootOnly(0);
102 *out << MueLu::MemUtils::PrintMemoryUsage() << std::endl;
103
104 // Timing
105 Teuchos::Time myTime("global");
106 Teuchos::TimeMonitor MM(myTime);
107
108 // read in input parameters
109
110 // default parameters
111 LO SIMPLE_nSweeps = 600;
112 Scalar SIMPLE_omega = 0.5;
113 LO SC_nSweeps = 10;
114 Scalar SC_omega = 1.0;
115 LO PRED_nSweeps = 3;
116 Scalar PRED_omega = 1.0;
117 LO useSIMPLEC = 0;
118
119 int SC_bUseDirectSolver = 0;
120
121 // Note: use --help to list available options.
122 Teuchos::CommandLineProcessor clp(false);
123 clp.setOption("SIMPLE_sweeps", &SIMPLE_nSweeps, "number of sweeps with SIMPLE smoother");
124 clp.setOption("SIMPLE_omega", &SIMPLE_omega, "scaling factor for SIMPLE smoother");
125 clp.setOption("Predict_sweeps", &PRED_nSweeps, "number of sweeps for SIMPLE internal velocity prediction smoother (GaussSeidel)");
126 clp.setOption("Predict_omega", &PRED_omega, "damping parameter for SIMPLE internal velocity prediction smoother (GaussSeidel)");
127 clp.setOption("SchurComp_sweeps", &SC_nSweeps, "number of sweeps for SIMPLE internal SchurComp solver/smoother (GaussSeidel)");
128 clp.setOption("SchurComp_omega", &SC_omega, "damping parameter for SIMPLE internal SchurComp solver/smoother (GaussSeidel)");
129 clp.setOption("SchurComp_solver", &SC_bUseDirectSolver, "if 1: use direct solver for SchurComp equation, otherwise use GaussSeidel smoother");
130 clp.setOption("useSIMPLEC", &useSIMPLEC, "if 1: use SIMPLEC instead of SIMPLE (default = 0 (SIMPLE))");
131
132 switch (clp.parse(argc, argv)) {
133 case Teuchos::CommandLineProcessor::PARSE_HELP_PRINTED: return EXIT_SUCCESS;
134 case Teuchos::CommandLineProcessor::PARSE_ERROR:
135 case Teuchos::CommandLineProcessor::PARSE_UNRECOGNIZED_OPTION: return EXIT_FAILURE;
136 case Teuchos::CommandLineProcessor::PARSE_SUCCESSFUL: break;
137 }
138
139 int globalNumDofs = 1500; // used for the maps
140
141 // build strided maps
142 // striding information: 2 velocity dofs and 1 pressure dof = 3 dofs per node
143 std::vector<size_t> stridingInfo;
144 stridingInfo.push_back(2);
145 stridingInfo.push_back(1);
146
147 /////////////////////////////////////// build strided maps
148 // build strided maps:
149 // xstridedfullmap: full map (velocity and pressure dof gids), continous
150 // xstridedvelmap: only velocity dof gid maps (i.e. 0,1,3,4,6,7...)
151 // xstridedpremap: only pressure dof gid maps (i.e. 2,5,8,...)
152 Xpetra::UnderlyingLib lib = Xpetra::UseEpetra;
153 RCP<const StridedMap> xstridedfullmap = StridedMapFactory::Build(lib, globalNumDofs, 0, stridingInfo, comm, -1);
154 RCP<const StridedMap> xstridedvelmap = StridedMapFactory::Build(xstridedfullmap, 0);
155 RCP<const StridedMap> xstridedpremap = StridedMapFactory::Build(xstridedfullmap, 1);
156
157 /////////////////////////////////////// transform Xpetra::Map objects to Epetra
158 // this is needed for our splitting routine
159 const RCP<const Epetra_Map> fullmap = Teuchos::rcpFromRef(Xpetra::toEpetra(*xstridedfullmap));
160 RCP<const Epetra_Map> velmap = Teuchos::rcpFromRef(Xpetra::toEpetra(*xstridedvelmap));
161 RCP<const Epetra_Map> premap = Teuchos::rcpFromRef(Xpetra::toEpetra(*xstridedpremap));
162
163 /////////////////////////////////////// import problem matrix and RHS from files (-> Epetra)
164
165 // read in problem
166 Epetra_CrsMatrix* ptrA = 0;
167 Epetra_Vector* ptrf = 0;
168 Epetra_MultiVector* ptrNS = 0;
169
170 *out << "Reading matrix market file" << std::endl;
171
172 EpetraExt::MatrixMarketFileToCrsMatrix("A_re1000_5932.txt", *fullmap, *fullmap, *fullmap, ptrA);
173 EpetraExt::MatrixMarketFileToVector("b_re1000_5932.txt", *fullmap, ptrf);
174
175 RCP<Epetra_CrsMatrix> epA = Teuchos::rcp(ptrA);
176 RCP<Epetra_Vector> epv = Teuchos::rcp(ptrf);
177 RCP<Epetra_MultiVector> epNS = Teuchos::rcp(ptrNS);
178
179 /////////////////////////////////////// split system into 2x2 block system
180
181 *out << "Split matrix into 2x2 block matrix" << std::endl;
182
183 // split fullA into A11,..., A22
184 Teuchos::RCP<Epetra_CrsMatrix> A11;
185 Teuchos::RCP<Epetra_CrsMatrix> A12;
186 Teuchos::RCP<Epetra_CrsMatrix> A21;
187 Teuchos::RCP<Epetra_CrsMatrix> A22;
188
189 if (SplitMatrix2x2(epA, *velmap, *premap, A11, A12, A21, A22) == false)
190 *out << "Problem with splitting matrix" << std::endl;
191
192 /////////////////////////////////////// transform Epetra objects to Xpetra (needed for MueLu)
193
194 // build Xpetra objects from Epetra_CrsMatrix objects
195 Teuchos::RCP<Xpetra::CrsMatrix<Scalar, LO, GO, Node> > xA11 = Teuchos::rcp(new Xpetra::EpetraCrsMatrixT<GO, Node>(A11));
196 Teuchos::RCP<Xpetra::CrsMatrix<Scalar, LO, GO, Node> > xA12 = Teuchos::rcp(new Xpetra::EpetraCrsMatrixT<GO, Node>(A12));
197 Teuchos::RCP<Xpetra::CrsMatrix<Scalar, LO, GO, Node> > xA21 = Teuchos::rcp(new Xpetra::EpetraCrsMatrixT<GO, Node>(A21));
198 Teuchos::RCP<Xpetra::CrsMatrix<Scalar, LO, GO, Node> > xA22 = Teuchos::rcp(new Xpetra::EpetraCrsMatrixT<GO, Node>(A22));
199
200 /////////////////////////////////////// generate MapExtractor object
201
202 std::vector<Teuchos::RCP<const Xpetra::Map<LO, GO, Node> > > xmaps;
203
204 xmaps.push_back(xstridedvelmap);
205 xmaps.push_back(xstridedpremap);
206
207 Teuchos::RCP<const Xpetra::MapExtractor<Scalar, LO, GO, Node> > map_extractor = Xpetra::MapExtractorFactory<Scalar, LO, GO, Node>::Build(xstridedfullmap, xmaps);
208
209 /////////////////////////////////////// build blocked transfer operator
210 // using the map extractor
211 Teuchos::RCP<Xpetra::BlockedCrsMatrix<Scalar, LO, GO, Node> > bOp = Teuchos::rcp(new Xpetra::BlockedCrsMatrix<Scalar, LO, GO, Node>(map_extractor, map_extractor, 10));
212 bOp->setMatrix(0, 0, Teuchos::rcp(new Xpetra::CrsMatrixWrap<Scalar, LocalOrdinal, GlobalOrdinal, Node>(xA11)));
213 bOp->setMatrix(0, 1, Teuchos::rcp(new Xpetra::CrsMatrixWrap<Scalar, LocalOrdinal, GlobalOrdinal, Node>(xA12)));
214 bOp->setMatrix(1, 0, Teuchos::rcp(new Xpetra::CrsMatrixWrap<Scalar, LocalOrdinal, GlobalOrdinal, Node>(xA21)));
215 bOp->setMatrix(1, 1, Teuchos::rcp(new Xpetra::CrsMatrixWrap<Scalar, LocalOrdinal, GlobalOrdinal, Node>(xA22)));
216
217 bOp->fillComplete();
218 //////////////////////////////////////////////////////// finest Level
219 RCP<MueLu::Level> Finest = rcp(new Level());
220 Finest->setDefaultVerbLevel(Teuchos::VERB_NONE);
221 Finest->Set("A", Teuchos::rcp_dynamic_cast<Matrix>(bOp));
222
223 ///////////////////////////////////
224 // Test Braess Sarazin Smoother as a solver
225
226 *out << "Test: Creating SIMPLE Smoother" << std::endl;
227 *out << "Test: Omega for SIMPLE = " << SIMPLE_omega << std::endl;
228 *out << "Test: Number of sweeps for SIMPLE = " << SIMPLE_nSweeps << std::endl;
229 *out << "Test: Omega for Schur Complement solver= " << SC_omega << std::endl;
230 *out << "Test: Number of Schur Complement solver= " << SC_nSweeps << std::endl;
231 *out << "Test: Setting up Braess Sarazin Smoother" << std::endl;
232
233 // define SIMPLE Smoother with SIMPLE_nSweeps and SIMPLE_omega as scaling factor
234 // AFact_ = Teuchos::null (= default) for the 2x2 blocked operator
235 RCP<SimpleSmoother> SimpleSm = rcp(new SimpleSmoother());
236 SimpleSm->SetParameter("Sweeps", Teuchos::ParameterEntry(SIMPLE_nSweeps));
237 SimpleSm->SetParameter("Damping factor", Teuchos::ParameterEntry(SIMPLE_omega));
238 if (useSIMPLEC == 1) SimpleSm->SetParameter("UseSIMPLEC", Teuchos::ParameterEntry(true));
239
240 RCP<SmootherFactory> smootherFact = rcp(new SmootherFactory(SimpleSm));
241
242 // define smoother for velocity prediction
243 // RCP<SubBlockAFactory> A00Fact = Teuchos::rcp(new SubBlockAFactory(MueLu::NoFactory::getRCP(), 0, 0));
244 RCP<SubBlockAFactory> A00Fact = rcp(new SubBlockAFactory());
245 A00Fact->SetFactory("A", MueLu::NoFactory::getRCP());
246 A00Fact->SetParameter("block row", Teuchos::ParameterEntry(0));
247 A00Fact->SetParameter("block col", Teuchos::ParameterEntry(0));
248 RCP<SmootherPrototype> smoProtoPredict = Teuchos::null;
249 std::string ifpackPredictType;
250 Teuchos::ParameterList ifpackPredictList;
251 ifpackPredictList.set("relaxation: sweeps", PRED_nSweeps);
252 ifpackPredictList.set("relaxation: damping factor", PRED_omega);
253 ifpackPredictType = "RELAXATION";
254 ifpackPredictList.set("relaxation: type", "Gauss-Seidel");
255 smoProtoPredict = rcp(new TrilinosSmoother(ifpackPredictType, ifpackPredictList, 0));
256 smoProtoPredict->SetFactory("A", A00Fact);
257 RCP<SmootherFactory> SmooPredictFact = rcp(new SmootherFactory(smoProtoPredict));
258 // define temporary FactoryManager that is used as input for BraessSarazin smoother
259 RCP<FactoryManager> MPredict = rcp(new FactoryManager());
260 MPredict->SetFactory("A", A00Fact);
261 MPredict->SetFactory("Smoother", SmooPredictFact); // solver/smoother for correction step
262 MPredict->SetFactory("PreSmoother", SmooPredictFact);
263 MPredict->SetFactory("PostSmoother", SmooPredictFact);
264 MPredict->SetIgnoreUserData(true); // always use data from factories defined in factory manager
265 SimpleSm->SetVelocityPredictionFactoryManager(MPredict); // set temporary factory manager in BraessSarazin smoother
266
267 // define SchurComplement Factory
268 // SchurComp gets a RCP to AFact_ which has to be the 2x2 blocked operator
269 // It stores the resulting SchurComplement operator as "A" generated by the SchurComplementFactory
270 // Instead of F^{-1} it uses the approximation \hat{F}^{-1} with \hat{F} = diag(F)
271 // InverseApproximation
272 Teuchos::RCP<InverseApproximationFactory> AinvFact = Teuchos::rcp(new InverseApproximationFactory());
273 AinvFact->SetFactory("A", A00Fact);
274 if (useSIMPLEC == 1) AinvFact->SetParameter("inverse: approximation type", Teuchos::ParameterEntry(std::string("lumping")));
275
276 RCP<SchurComplementFactory> SFact = Teuchos::rcp(new SchurComplementFactory());
277 SFact->SetParameter("omega", Teuchos::ParameterEntry(1.0)); // for Simple, omega is always 1.0 in the SchurComplement
278 SFact->SetFactory("A", MueLu::NoFactory::getRCP());
279
280 // define smoother/solver for BraessSarazin
281 RCP<SmootherPrototype> smoProtoSC = Teuchos::null;
282 if (SC_bUseDirectSolver != 1) {
283 // Smoother Factory, using SFact as a factory for A
284 std::string ifpackSCType;
285 Teuchos::ParameterList ifpackSCList;
286 ifpackSCList.set("relaxation: sweeps", SC_nSweeps);
287 ifpackSCList.set("relaxation: damping factor", SC_omega);
288 ifpackSCType = "RELAXATION";
289 ifpackSCList.set("relaxation: type", "Gauss-Seidel");
290 smoProtoSC = rcp(new TrilinosSmoother(ifpackSCType, ifpackSCList, 0));
291 smoProtoSC->SetFactory("A", SFact);
292 } else {
293 Teuchos::ParameterList ifpackDSList;
294 std::string ifpackDSType;
295 smoProtoSC = rcp(new DirectSolver(ifpackDSType, ifpackDSList));
296 smoProtoSC->SetFactory("A", SFact);
297 }
298
299 RCP<SmootherFactory> SmooSCFact = rcp(new SmootherFactory(smoProtoSC));
300
301 // define temporary FactoryManager that is used as input for BraessSarazin smoother
302 RCP<FactoryManager> MB = rcp(new FactoryManager());
303 MB->SetFactory("A", SFact); // SchurComplement operator for correction step (defined as "A")
304 MB->SetFactory("Smoother", SmooSCFact); // solver/smoother for correction step
305 MB->SetFactory("PreSmoother", SmooSCFact);
306 MB->SetFactory("PostSmoother", SmooSCFact);
307 MB->SetIgnoreUserData(true); // always use data from factories defined in factory manager
308 SimpleSm->SetSchurCompFactoryManager(MB); // set temporary factory manager in BraessSarazin smoother
309
310 // setup main factory manager
311 RCP<FactoryManager> M = rcp(new FactoryManager());
312 M->SetFactory("A", MueLu::NoFactory::getRCP()); // this is the 2x2 blocked operator
313 M->SetFactory("Smoother", smootherFact); // BraessSarazin block smoother
314 M->SetFactory("PreSmoother", smootherFact);
315 M->SetFactory("PostSmoother", smootherFact);
316
317 MueLu::SetFactoryManager SFMCoarse(Finest, M);
318 Finest->Request(MueLu::TopSmootherFactory<Scalar, LO, GO, Node>(M, "Smoother"));
319
320 // call setup (= extract blocks and extract diagonal of F)
321 SimpleSm->Setup(*Finest);
322
323 RCP<MultiVector> xtest = MultiVectorFactory::Build(xstridedfullmap, 1);
324 xtest->putScalar((Scalar)0.0);
325
326 RCP<Vector> xR = Teuchos::rcp(new Xpetra::EpetraVectorT<int, Node>(epv));
327 // calculate initial (absolute) residual
328 Teuchos::Array<Teuchos::ScalarTraits<Scalar>::magnitudeType> norms(1);
329
330 xR->norm2(norms);
331 *out << "Test: ||x_0|| = " << norms[0] << std::endl;
332 *out << "Test: Applying Simple Smoother" << std::endl;
333 *out << "Test: START DATA" << std::endl;
334 *out << "iterations\tVelocity_residual\tPressure_residual" << std::endl;
335 SimpleSm->Apply(*xtest, *xR);
336 xtest->norm2(norms);
337 *out << "Test: ||x_1|| = " << norms[0] << std::endl;
338
339 Teuchos::Array<Teuchos::ScalarTraits<Scalar>::magnitudeType> test = MueLu::Utilities<Scalar, LO, GO, Node>::ResidualNorm(*bOp, *xtest, *xR);
340 *out << "residual norm: " << test[0] << std::endl;
341
342 success = (test[0] < 1.0e-7);
343 if (!success)
344 *out << "no convergence" << std::endl;
345 }
346 TEUCHOS_STANDARD_CATCH_STATEMENTS(verbose, std::cerr, success);
347
348 return (success ? EXIT_SUCCESS : EXIT_FAILURE);
349 #else
350 std::cout << "Epetra needs Serial node. Please recompile MueLu with the Serial node enabled." << std::endl;
351 return EXIT_SUCCESS;
352 #endif // #if defined(HAVE_MUELU_SERIAL) && defined(HAVE_MUELU_EPETRA)
353 }