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