// @HEADER // ***************************************************************************** // MueLu: A package for multigrid based preconditioning // // Copyright 2012 NTESS and the MueLu contributors. // SPDX-License-Identifier: BSD-3-Clause // ***************************************************************************** // @HEADER /* * Navier2D_blocked_bssmoother.cpp * * Created on: Jun 18, 2012 * Author: wiesner */ #include #include #include // Teuchos #include #include #include #include #include #include #include // Epetra #include #include #include // Xpetra #include #include #include #include #include #include #include #include #include #include #include // MueLu #include "MueLu_ConfigDefs.hpp" #include "MueLu_Memory.hpp" #include "MueLu_Hierarchy.hpp" #include "MueLu_PgPFactory.hpp" #include "MueLu_GenericRFactory.hpp" #include "MueLu_SaPFactory.hpp" #include "MueLu_TransPFactory.hpp" #include "MueLu_TrilinosSmoother.hpp" #include "MueLu_Utilities.hpp" #include "MueLu_Exceptions.hpp" #include "MueLu_Aggregates.hpp" #include "MueLu_CoalesceDropFactory.hpp" #include "MueLu_PreDropFunctionConstVal.hpp" #include "MueLu_NullspaceFactory.hpp" #include "MueLu_TentativePFactory.hpp" #include "MueLu_SmootherFactory.hpp" #include "MueLu_DirectSolver.hpp" #include "MueLu_EpetraOperator.hpp" #include "MueLu_SubBlockAFactory.hpp" #include "MueLu_BlockedGaussSeidelSmoother.hpp" #include "MueLu_BraessSarazinSmoother.hpp" #include "MueLu_SchurComplementFactory.hpp" #include "MueLu_Utilities.hpp" #include "MueLu_TopSmootherFactory.hpp" #include "MueLu_HierarchyUtils.hpp" #include #include #include "Navier2D_Helpers.h" /*! * 2d Navier Stokes example (for Epetra) * * using block matrices */ int main(int argc, char* argv[]) { #if defined(HAVE_MUELU_EPETRA) && defined(HAVE_MUELU_EPETRAEXT) typedef double Scalar; typedef int LocalOrdinal; typedef int GlobalOrdinal; typedef LocalOrdinal LO; typedef GlobalOrdinal GO; typedef Xpetra::EpetraNode Node; #include "MueLu_UseShortNames.hpp" using Teuchos::RCP; using Teuchos::rcp; using namespace MueLuTests; using namespace Teuchos; oblackholestream blackhole; GlobalMPISession mpiSession(&argc, &argv, &blackhole); bool success = false; bool verbose = true; try { RCP > comm = DefaultComm::getComm(); RCP out = fancyOStream(rcpFromRef(std::cout)); out->setOutputToRootOnly(0); *out << MueLu::MemUtils::PrintMemoryUsage() << std::endl; // Timing Time myTime("global"); TimeMonitor MM(myTime); // read in input parameters // default parameters LO BS_nSweeps = 100; Scalar BS_omega = 1.7; LO SC_nSweeps = 1; Scalar SC_omega = 1.0; int SC_bUseDirectSolver = 0; // Note: use --help to list available options. CommandLineProcessor clp(false); clp.setOption("BraessSarazin_sweeps", &BS_nSweeps, "number of sweeps with BraessSarazin smoother"); clp.setOption("BraessSarazin_omega", &BS_omega, "scaling factor for BraessSarazin smoother"); clp.setOption("SchurComp_sweeps", &SC_nSweeps, "number of sweeps for BraessSarazin internal SchurComp solver/smoother (GaussSeidel)"); clp.setOption("SchurComp_omega", &SC_omega, "damping parameter for BraessSarazin internal SchurComp solver/smoother (GaussSeidel)"); clp.setOption("SchurComp_solver", &SC_bUseDirectSolver, "if 1: use direct solver for SchurComp equation, otherwise use GaussSeidel smoother (=default)"); switch (clp.parse(argc, argv)) { case Teuchos::CommandLineProcessor::PARSE_HELP_PRINTED: return EXIT_SUCCESS; case Teuchos::CommandLineProcessor::PARSE_ERROR: case Teuchos::CommandLineProcessor::PARSE_UNRECOGNIZED_OPTION: return EXIT_FAILURE; case Teuchos::CommandLineProcessor::PARSE_SUCCESSFUL: break; } int globalNumDofs = 1500; // used for the maps // int nDofsPerNode = 3; // used for generating the fine level null-space // build strided maps // striding information: 2 velocity dofs and 1 pressure dof = 3 dofs per node std::vector stridingInfo; stridingInfo.push_back(2); stridingInfo.push_back(1); /////////////////////////////////////// build strided maps // build strided maps: // xstridedfullmap: full map (velocity and pressure dof gids), continous // xstridedvelmap: only velocity dof gid maps (i.e. 0,1,3,4,6,7...) // xstridedpremap: only pressure dof gid maps (i.e. 2,5,8,...) Xpetra::UnderlyingLib lib = Xpetra::UseEpetra; RCP xstridedfullmap = StridedMapFactory::Build(lib, globalNumDofs, 0, stridingInfo, comm, -1); RCP xstridedvelmap = StridedMapFactory::Build(xstridedfullmap, 0); RCP xstridedpremap = StridedMapFactory::Build(xstridedfullmap, 1); /////////////////////////////////////// transform Xpetra::Map objects to Epetra // this is needed for our splitting routine const RCP fullmap = rcpFromRef(Xpetra::toEpetra(*xstridedfullmap)); RCP velmap = rcpFromRef(Xpetra::toEpetra(*xstridedvelmap)); RCP premap = rcpFromRef(Xpetra::toEpetra(*xstridedpremap)); /////////////////////////////////////// import problem matrix and RHS from files (-> Epetra) // read in problem Epetra_CrsMatrix* ptrA = 0; Epetra_Vector* ptrf = 0; Epetra_MultiVector* ptrNS = 0; *out << "Reading matrix market file" << std::endl; EpetraExt::MatrixMarketFileToCrsMatrix("A_re1000_5932.txt", *fullmap, *fullmap, *fullmap, ptrA); EpetraExt::MatrixMarketFileToVector("b_re1000_5932.txt", *fullmap, ptrf); RCP epA = rcp(ptrA); RCP epv = rcp(ptrf); RCP epNS = rcp(ptrNS); /////////////////////////////////////// split system into 2x2 block system *out << "Split matrix into 2x2 block matrix" << std::endl; // split fullA into A11,..., A22 RCP A11; RCP A12; RCP A21; RCP A22; if (SplitMatrix2x2(epA, *velmap, *premap, A11, A12, A21, A22) == false) *out << "Problem with splitting matrix" << std::endl; /////////////////////////////////////// transform Epetra objects to Xpetra (needed for MueLu) // build Xpetra objects from Epetra_CrsMatrix objects RCP > xA11 = rcp(new Xpetra::EpetraCrsMatrixT(A11)); RCP > xA12 = rcp(new Xpetra::EpetraCrsMatrixT(A12)); RCP > xA21 = rcp(new Xpetra::EpetraCrsMatrixT(A21)); RCP > xA22 = rcp(new Xpetra::EpetraCrsMatrixT(A22)); /////////////////////////////////////// generate MapExtractor object std::vector > > xmaps; xmaps.push_back(xstridedvelmap); xmaps.push_back(xstridedpremap); RCP > map_extractor = Xpetra::MapExtractorFactory::Build(xstridedfullmap, xmaps); /////////////////////////////////////// build blocked transfer operator // using the map extractor RCP > bOp = rcp(new Xpetra::BlockedCrsMatrix(map_extractor, map_extractor, 10)); bOp->setMatrix(0, 0, Teuchos::rcp(new Xpetra::CrsMatrixWrap(xA11))); bOp->setMatrix(0, 1, Teuchos::rcp(new Xpetra::CrsMatrixWrap(xA12))); bOp->setMatrix(1, 0, Teuchos::rcp(new Xpetra::CrsMatrixWrap(xA21))); bOp->setMatrix(1, 1, Teuchos::rcp(new Xpetra::CrsMatrixWrap(xA22))); bOp->fillComplete(); //////////////////////////////////////////////////////// finest Level RCP Finest = rcp(new Level()); Finest->setDefaultVerbLevel(VERB_NONE); Finest->Set("A", rcp_dynamic_cast(bOp)); /////////////////////////////////// // Test Braess Sarazin Smoother as a solver *out << "Test: Creating Braess Sarazin Smoother" << std::endl; *out << "Test: Omega for BraessSarazin = " << BS_omega << std::endl; *out << "Test: Number of sweeps for BraessSarazin = " << BS_nSweeps << std::endl; *out << "Test: Omega for Schur Complement solver= " << SC_omega << std::endl; *out << "Test: Number of Schur Complement solver= " << SC_nSweeps << std::endl; *out << "Test: Setting up Braess Sarazin Smoother" << std::endl; // define BraessSarazin Smoother with BS_nSweeps and BS_omega as scaling factor // AFact_ = null (= default) for the 2x2 blocked operator RCP BraessSarazinSm = rcp(new BraessSarazinSmoother()); BraessSarazinSm->SetParameter("Sweeps", Teuchos::ParameterEntry(BS_nSweeps)); BraessSarazinSm->SetParameter("Damping factor", Teuchos::ParameterEntry(BS_omega)); RCP smootherFact = rcp(new SmootherFactory(BraessSarazinSm)); /*note that omega must be the same in the SchurComplementFactory and in the BraessSarazinSmoother*/ // define SchurComplement Factory // SchurComp gets a RCP to AFact_ which has to be the 2x2 blocked operator // and the scaling/damping factor omega that is used for BraessSarazin // It stores the resulting SchurComplement operator as "A" generated by the SchurComplementFactory // Instead of F^{-1} it uses the approximation \hat{F}^{-1} with \hat{F} = diag(F) RCP SFact = rcp(new SchurComplementFactory()); SFact->SetParameter("omega", ParameterEntry(BS_omega)); SFact->SetFactory("A", MueLu::NoFactory::getRCP()); // define smoother/solver for BraessSarazin RCP smoProtoSC = null; if (SC_bUseDirectSolver != 1) { // Smoother Factory, using SFact as a factory for A std::string ifpackSCType; ParameterList ifpackSCList; ifpackSCList.set("relaxation: sweeps", SC_nSweeps); ifpackSCList.set("relaxation: damping factor", SC_omega); ifpackSCType = "RELAXATION"; ifpackSCList.set("relaxation: type", "Gauss-Seidel"); smoProtoSC = rcp(new TrilinosSmoother(ifpackSCType, ifpackSCList, 0)); smoProtoSC->SetFactory("A", SFact); } else { ParameterList ifpackDSList; std::string ifpackDSType; smoProtoSC = rcp(new DirectSolver(ifpackDSType, ifpackDSList)); smoProtoSC->SetFactory("A", SFact); } RCP SmooSCFact = rcp(new SmootherFactory(smoProtoSC)); // define temporary FactoryManager that is used as input for BraessSarazin smoother RCP MB = rcp(new FactoryManager()); MB->SetFactory("A", SFact); // SchurComplement operator for correction step (defined as "A") MB->SetFactory("Smoother", SmooSCFact); // solver/smoother for correction step MB->SetFactory("PreSmoother", SmooSCFact); MB->SetFactory("PostSmoother", SmooSCFact); MB->SetIgnoreUserData(true); // always use data from factories defined in factory manager BraessSarazinSm->AddFactoryManager(MB, 0); // set temporary factory manager in BraessSarazin smoother // setup main factory manager RCP M = rcp(new FactoryManager()); M->SetFactory("A", MueLu::NoFactory::getRCP()); // this is the 2x2 blocked operator M->SetFactory("Smoother", smootherFact); // BraessSarazin block smoother M->SetFactory("PreSmoother", smootherFact); M->SetFactory("PostSmoother", smootherFact); MueLu::SetFactoryManager SFMCoarse(Finest, M); Finest->Request(MueLu::TopSmootherFactory(M, "Smoother")); // call setup (= extract blocks and extract diagonal of F) BraessSarazinSm->Setup(*Finest); RCP xtest = MultiVectorFactory::Build(xstridedfullmap, 1); xtest->putScalar((SC)0.0); RCP xR = rcp(new Xpetra::EpetraVectorT(epv)); // calculate initial (absolute) residual Array::magnitudeType> norms(1); xR->norm2(norms); *out << "Test: ||x_0|| = " << norms[0] << std::endl; *out << "Test: Applying Braess-Sarazin Smoother" << std::endl; *out << "Test: START DATA" << std::endl; *out << "iterations\tVelocity_residual\tPressure_residual" << std::endl; BraessSarazinSm->Apply(*xtest, *xR); xtest->norm2(norms); *out << "Test: ||x_1|| = " << norms[0] << std::endl; Array::magnitudeType> test = MueLu::Utilities::ResidualNorm(*bOp, *xtest, *xR); *out << "residual norm: " << test[0] << std::endl; success = true; } TEUCHOS_STANDARD_CATCH_STATEMENTS(verbose, std::cerr, success); return (success ? EXIT_SUCCESS : EXIT_FAILURE); #else std::cout << "Epetra (and/or EpetraExt) are not available. Skip test." << std::endl; return EXIT_SUCCESS; #endif // #if defined(HAVE_MUELU_SERIAL) && defined(HAVE_MUELU_EPETRA) }