| 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_epetra.cpp |
| 12 |
* |
| 13 |
* Created on: Mar 26, 2011 |
| 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_EpetraMap.hpp> |
| 38 |
#include <Xpetra_MapFactory.hpp> |
| 39 |
#include <Xpetra_Matrix.hpp> |
| 40 |
#include <Xpetra_CrsMatrixWrap.hpp> |
| 41 |
#include <Xpetra_MultiVectorFactory.hpp> |
| 42 |
#include <Xpetra_VectorFactory.hpp> |
| 43 |
#include <Xpetra_Parameters.hpp> |
| 44 |
#include <Xpetra_MapExtractorFactory.hpp> |
| 45 |
#include <Xpetra_BlockedCrsMatrix.hpp> |
| 46 |
#include <Xpetra_StridedMapFactory.hpp> |
| 47 |
#include <Xpetra_StridedMap.hpp> |
| 48 |
|
| 49 |
// MueLu |
| 50 |
#include "MueLu_ConfigDefs.hpp" |
| 51 |
#include "MueLu_Memory.hpp" |
| 52 |
#include "MueLu_Hierarchy.hpp" |
| 53 |
#include "MueLu_UncoupledAggregationFactory.hpp" |
| 54 |
#include "MueLu_PgPFactory.hpp" |
| 55 |
#include "MueLu_GenericRFactory.hpp" |
| 56 |
#include "MueLu_SaPFactory.hpp" |
| 57 |
#include "MueLu_TransPFactory.hpp" |
| 58 |
#include "MueLu_BlockedRAPFactory.hpp" |
| 59 |
#include "MueLu_TrilinosSmoother.hpp" |
| 60 |
#include "MueLu_Utilities.hpp" |
| 61 |
#include "MueLu_Exceptions.hpp" |
| 62 |
#include "MueLu_Aggregates.hpp" |
| 63 |
#include "MueLu_CoalesceDropFactory.hpp" |
| 64 |
#include "MueLu_PreDropFunctionConstVal.hpp" |
| 65 |
#include "MueLu_NullspaceFactory.hpp" |
| 66 |
#include "MueLu_TentativePFactory.hpp" |
| 67 |
#include "MueLu_SmootherFactory.hpp" |
| 68 |
#include "MueLu_DirectSolver.hpp" |
| 69 |
#include "MueLu_EpetraOperator.hpp" |
| 70 |
#include "MueLu_SubBlockAFactory.hpp" |
| 71 |
#include "MueLu_BlockedPFactory.hpp" |
| 72 |
#include "MueLu_BlockedGaussSeidelSmoother.hpp" |
| 73 |
#include "MueLu_CoarseMapFactory.hpp" |
| 74 |
#include "MueLu_AmalgamationFactory.hpp" |
| 75 |
#include <MueLu_ParameterListInterpreter.hpp> |
| 76 |
|
| 77 |
#include <Epetra_LinearProblem.h> |
| 78 |
#include <AztecOO.h> |
| 79 |
|
| 80 |
#include "Navier2D_Helpers.h" |
| 81 |
|
| 82 |
/*! |
| 83 |
* 2d Navier Stokes example (for Epetra) |
| 84 |
* |
| 85 |
* using block matrices |
| 86 |
*/ |
| 87 |
|
| 88 |
int main(int argc, char* argv[]) { |
| 89 |
#if defined(HAVE_MUELU_EPETRA) && defined(HAVE_MUELU_EPETRAEXT) |
| 90 |
typedef double Scalar; |
| 91 |
typedef int LocalOrdinal; |
| 92 |
typedef int GlobalOrdinal; |
| 93 |
typedef LocalOrdinal LO; |
| 94 |
typedef GlobalOrdinal GO; |
| 95 |
typedef Xpetra::EpetraNode Node; |
| 96 |
#include "MueLu_UseShortNames.hpp" |
| 97 |
|
| 98 |
using Teuchos::RCP; |
| 99 |
using Teuchos::rcp; |
| 100 |
using namespace MueLuTests; |
| 101 |
using namespace Teuchos; |
| 102 |
|
| 103 |
oblackholestream blackhole; |
| 104 |
GlobalMPISession mpiSession(&argc, &argv, &blackhole); |
| 105 |
|
| 106 |
bool success = false; |
| 107 |
bool verbose = true; |
| 108 |
try { |
| 109 |
// default parameters |
| 110 |
std::string xmlFile = "myXML.xml"; |
| 111 |
|
| 112 |
// Note: use --help to list available options. |
| 113 |
CommandLineProcessor clp(false); |
| 114 |
clp.setOption("xml", &xmlFile, "xml file with solver parameters for a 2x2 blocked NS example"); |
| 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 |
RCP<const Comm<int> > comm = DefaultComm<int>::getComm(); |
| 124 |
RCP<FancyOStream> out = fancyOStream(rcpFromRef(std::cout)); |
| 125 |
out->setOutputToRootOnly(0); |
| 126 |
*out << MueLu::MemUtils::PrintMemoryUsage() << std::endl; |
| 127 |
|
| 128 |
// Timing |
| 129 |
Time myTime("global"); |
| 130 |
TimeMonitor MM(myTime); |
| 131 |
|
| 132 |
GO maxCoarseSize = 1; // FIXME clp doesn't like long long int |
| 133 |
|
| 134 |
int globalNumDofs = 1500; // used for the maps |
| 135 |
int nDofsPerNode = 3; // used for generating the fine level null-space |
| 136 |
|
| 137 |
// build strided maps |
| 138 |
// striding information: 2 velocity dofs and 1 pressure dof = 3 dofs per node |
| 139 |
std::vector<size_t> stridingInfo; |
| 140 |
stridingInfo.push_back(2); |
| 141 |
stridingInfo.push_back(1); |
| 142 |
|
| 143 |
/////////////////////////////////////// build strided maps |
| 144 |
// build strided maps: |
| 145 |
// xstridedfullmap: full map (velocity and pressure dof gids), continous |
| 146 |
// xstridedvelmap: only velocity dof gid maps (i.e. 0,1,3,4,6,7...) |
| 147 |
// xstridedpremap: only pressure dof gid maps (i.e. 2,5,8,...) |
| 148 |
Xpetra::UnderlyingLib lib = Xpetra::UseEpetra; |
| 149 |
RCP<const StridedMap> xstridedfullmap = StridedMapFactory::Build(lib, globalNumDofs, 0, stridingInfo, comm, -1); |
| 150 |
RCP<const StridedMap> xstridedvelmap = StridedMapFactory::Build(xstridedfullmap, 0); |
| 151 |
RCP<const StridedMap> xstridedpremap = StridedMapFactory::Build(xstridedfullmap, 1); |
| 152 |
|
| 153 |
/////////////////////////////////////// transform Xpetra::Map objects to Epetra |
| 154 |
// this is needed for AztecOO |
| 155 |
const RCP<const Epetra_Map> fullmap = rcpFromRef(Xpetra::toEpetra(*xstridedfullmap)); |
| 156 |
RCP<const Epetra_Map> velmap = rcpFromRef(Xpetra::toEpetra(*xstridedvelmap)); |
| 157 |
RCP<const Epetra_Map> premap = rcpFromRef(Xpetra::toEpetra(*xstridedpremap)); |
| 158 |
|
| 159 |
/////////////////////////////////////// import problem matrix and RHS from files (-> Epetra) |
| 160 |
|
| 161 |
// read in problem |
| 162 |
Epetra_CrsMatrix* ptrA = 0; |
| 163 |
Epetra_Vector* ptrf = 0; |
| 164 |
Epetra_MultiVector* ptrNS = 0; |
| 165 |
|
| 166 |
*out << "Reading matrix market file" << std::endl; |
| 167 |
EpetraExt::MatrixMarketFileToCrsMatrix("A_re1000_5932.txt", *fullmap, *fullmap, *fullmap, ptrA); |
| 168 |
EpetraExt::MatrixMarketFileToVector("b_re1000_5932.txt", *fullmap, ptrf); |
| 169 |
RCP<Epetra_CrsMatrix> epA = rcp(ptrA); |
| 170 |
RCP<Epetra_Vector> epv = rcp(ptrf); |
| 171 |
RCP<Epetra_MultiVector> epNS = rcp(ptrNS); |
| 172 |
|
| 173 |
/////////////////////////////////////// split system into 2x2 block system |
| 174 |
|
| 175 |
*out << "Split matrix into 2x2 block matrix" << std::endl; |
| 176 |
|
| 177 |
// split fullA into A11,..., A22 |
| 178 |
RCP<Epetra_CrsMatrix> A11; |
| 179 |
RCP<Epetra_CrsMatrix> A12; |
| 180 |
RCP<Epetra_CrsMatrix> A21; |
| 181 |
RCP<Epetra_CrsMatrix> A22; |
| 182 |
|
| 183 |
if (SplitMatrix2x2(epA, *velmap, *premap, A11, A12, A21, A22) == false) |
| 184 |
*out << "Problem with splitting matrix" << std::endl; |
| 185 |
|
| 186 |
/////////////////////////////////////// transform Epetra objects to Xpetra (needed for MueLu) |
| 187 |
|
| 188 |
// build Xpetra objects from Epetra_CrsMatrix objects |
| 189 |
RCP<Xpetra::CrsMatrix<Scalar, LO, GO, Node> > xA11 = rcp(new Xpetra::EpetraCrsMatrixT<GO, Node>(A11)); |
| 190 |
RCP<Xpetra::CrsMatrix<Scalar, LO, GO, Node> > xA12 = rcp(new Xpetra::EpetraCrsMatrixT<GO, Node>(A12)); |
| 191 |
RCP<Xpetra::CrsMatrix<Scalar, LO, GO, Node> > xA21 = rcp(new Xpetra::EpetraCrsMatrixT<GO, Node>(A21)); |
| 192 |
RCP<Xpetra::CrsMatrix<Scalar, LO, GO, Node> > xA22 = rcp(new Xpetra::EpetraCrsMatrixT<GO, Node>(A22)); |
| 193 |
|
| 194 |
/////////////////////////////////////// generate MapExtractor object |
| 195 |
|
| 196 |
std::vector<RCP<const Xpetra::Map<LO, GO, Node> > > xmaps; |
| 197 |
xmaps.push_back(xstridedvelmap); |
| 198 |
xmaps.push_back(xstridedpremap); |
| 199 |
|
| 200 |
RCP<const Xpetra::MapExtractor<Scalar, LO, GO, Node> > map_extractor = Xpetra::MapExtractorFactory<Scalar, LO, GO, Node>::Build(xstridedfullmap->getMap(), xmaps); |
| 201 |
|
| 202 |
/////////////////////////////////////// build blocked transfer operator |
| 203 |
// using the map extractor |
| 204 |
RCP<Xpetra::BlockedCrsMatrix<Scalar, LO, GO, Node> > bOp = rcp(new Xpetra::BlockedCrsMatrix<Scalar, LO, GO, Node>(map_extractor, map_extractor, 10)); |
| 205 |
bOp->setMatrix(0, 0, Teuchos::rcp(new Xpetra::CrsMatrixWrap<Scalar, LocalOrdinal, GlobalOrdinal, Node>(xA11))); |
| 206 |
bOp->setMatrix(0, 1, Teuchos::rcp(new Xpetra::CrsMatrixWrap<Scalar, LocalOrdinal, GlobalOrdinal, Node>(xA12))); |
| 207 |
bOp->setMatrix(1, 0, Teuchos::rcp(new Xpetra::CrsMatrixWrap<Scalar, LocalOrdinal, GlobalOrdinal, Node>(xA21))); |
| 208 |
bOp->setMatrix(1, 1, Teuchos::rcp(new Xpetra::CrsMatrixWrap<Scalar, LocalOrdinal, GlobalOrdinal, Node>(xA22))); |
| 209 |
|
| 210 |
bOp->fillComplete(); |
| 211 |
|
| 212 |
//////////////////////////////////////// prepare setup |
| 213 |
ParameterListInterpreter mueLuFactory(xmlFile, *comm); |
| 214 |
|
| 215 |
RCP<Hierarchy> H = mueLuFactory.CreateHierarchy(); |
| 216 |
H->setDefaultVerbLevel(VERB_HIGH); |
| 217 |
H->SetMaxCoarseSize(maxCoarseSize); |
| 218 |
|
| 219 |
RCP<MueLu::Level> Finest = H->GetLevel(0); |
| 220 |
Finest->setDefaultVerbLevel(VERB_HIGH); |
| 221 |
Finest->Set("A", rcp_dynamic_cast<Matrix>(bOp)); |
| 222 |
|
| 223 |
////////////////////////////////////////// prepare null space for A11 |
| 224 |
RCP<MultiVector> nullspace11 = MultiVectorFactory::Build(xstridedvelmap, 2); // this is a 2D standard null space |
| 225 |
|
| 226 |
for (int i = 0; i < nDofsPerNode - 1; ++i) { |
| 227 |
ArrayRCP<Scalar> nsValues = nullspace11->getDataNonConst(i); |
| 228 |
int numBlocks = nsValues.size() / (nDofsPerNode - 1); |
| 229 |
for (int j = 0; j < numBlocks; ++j) { |
| 230 |
nsValues[j * (nDofsPerNode - 1) + i] = 1.0; |
| 231 |
} |
| 232 |
} |
| 233 |
|
| 234 |
Finest->Set("Nullspace1", nullspace11); |
| 235 |
|
| 236 |
////////////////////////////////////////// prepare null space for A22 |
| 237 |
RCP<MultiVector> nullspace22 = MultiVectorFactory::Build(xstridedpremap, 1); // this is a 2D standard null space |
| 238 |
ArrayRCP<Scalar> nsValues22 = nullspace22->getDataNonConst(0); |
| 239 |
for (int j = 0; j < nsValues22.size(); ++j) { |
| 240 |
nsValues22[j] = 1.0; |
| 241 |
} |
| 242 |
|
| 243 |
Finest->Set("Nullspace2", nullspace22); |
| 244 |
|
| 245 |
/////////////////////////////////// BEGIN setup |
| 246 |
|
| 247 |
mueLuFactory.SetupHierarchy(*H); |
| 248 |
|
| 249 |
///////////////////////////////////// END setup |
| 250 |
|
| 251 |
*out << std::endl; |
| 252 |
|
| 253 |
RCP<MultiVector> xLsg = MultiVectorFactory::Build(xstridedfullmap, 1); |
| 254 |
|
| 255 |
// Use AMG directly as an iterative method |
| 256 |
{ |
| 257 |
xLsg->putScalar((SC)0.0); |
| 258 |
|
| 259 |
// Epetra_Vector -> Xpetra::Vector |
| 260 |
RCP<Vector> xRhs = rcp(new Xpetra::EpetraVectorT<int, Node>(epv)); |
| 261 |
|
| 262 |
// calculate initial (absolute) residual |
| 263 |
Array<ScalarTraits<SC>::magnitudeType> norms(1); |
| 264 |
xRhs->norm2(norms); |
| 265 |
*out << "||x_0|| = " << norms[0] << std::endl; |
| 266 |
|
| 267 |
// apply ten multigrid iterations |
| 268 |
H->Iterate(*xRhs, *xLsg, 100); |
| 269 |
|
| 270 |
// calculate and print residual |
| 271 |
RCP<MultiVector> xTmp = MultiVectorFactory::Build(xstridedfullmap, 1); |
| 272 |
bOp->apply(*xLsg, *xTmp, NO_TRANS, (SC)1.0, (SC)0.0); |
| 273 |
xRhs->update((SC)-1.0, *xTmp, (SC)1.0); |
| 274 |
xRhs->norm2(norms); |
| 275 |
*out << "||r|| = " << norms[0] << std::endl; |
| 276 |
} |
| 277 |
|
| 278 |
// TODO: don't forget to add Aztec as prerequisite in CMakeLists.txt! |
| 279 |
// |
| 280 |
// Solve Ax = b using AMG as a preconditioner in AztecOO |
| 281 |
// |
| 282 |
{ |
| 283 |
RCP<Epetra_Vector> X = rcp(new Epetra_Vector(epv->Map())); |
| 284 |
X->PutScalar(0.0); |
| 285 |
Epetra_LinearProblem epetraProblem(epA.get(), X.get(), epv.get()); |
| 286 |
|
| 287 |
AztecOO aztecSolver(epetraProblem); |
| 288 |
aztecSolver.SetAztecOption(AZ_solver, AZ_gmres); |
| 289 |
|
| 290 |
MueLu::EpetraOperator aztecPrec(H); |
| 291 |
aztecSolver.SetPrecOperator(&aztecPrec); |
| 292 |
|
| 293 |
int maxIts = 50; |
| 294 |
double tol = 1e-8; |
| 295 |
|
| 296 |
aztecSolver.Iterate(maxIts, tol); |
| 297 |
} |
| 298 |
|
| 299 |
success = true; |
| 300 |
} |
| 301 |
TEUCHOS_STANDARD_CATCH_STATEMENTS(verbose, std::cerr, success); |
| 302 |
|
| 303 |
return (success ? EXIT_SUCCESS : EXIT_FAILURE); |
| 304 |
#else |
| 305 |
std::cout << "Epetra (and/or EpetraExt) are not available. Skip test." << std::endl; |
| 306 |
return EXIT_SUCCESS; |
| 307 |
#endif |
| 308 |
} |