| 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 |
|
| 12 |
// Teuchos |
| 13 |
#include <Teuchos_RCP.hpp> |
| 14 |
#include <Teuchos_ParameterList.hpp> |
| 15 |
#include <Teuchos_CommandLineProcessor.hpp> |
| 16 |
#include <Teuchos_GlobalMPISession.hpp> |
| 17 |
#include <Teuchos_DefaultComm.hpp> |
| 18 |
#include <Teuchos_StandardCatchMacros.hpp> |
| 19 |
#include <Teuchos_XMLParameterListHelpers.hpp> |
| 20 |
|
| 21 |
#include <MueLu_Exceptions.hpp> |
| 22 |
#include <MueLu_ParameterListInterpreter.hpp> |
| 23 |
|
| 24 |
#include "MueLu_SemiCoarsenPFactory.hpp" // for semi-coarsening constants |
| 25 |
#include <MueLu_TestHelpers.hpp> |
| 26 |
#include <MueLu_MultiPhys.hpp> |
| 27 |
|
| 28 |
/**********************************************************************************/ |
| 29 |
/* CREATE INITAL MATRIX */ |
| 30 |
/**********************************************************************************/ |
| 31 |
#include <Xpetra_Map.hpp> |
| 32 |
#include <Xpetra_MapFactory.hpp> |
| 33 |
#include <Xpetra_CrsMatrixWrap.hpp> |
| 34 |
#include <Xpetra_Vector.hpp> |
| 35 |
#include <Xpetra_MultiVectorFactory.hpp> |
| 36 |
#include <Xpetra_VectorFactory.hpp> |
| 37 |
#include <Xpetra_Parameters.hpp> |
| 38 |
|
| 39 |
// Galeri |
| 40 |
#include <Galeri_XpetraParameters.hpp> |
| 41 |
#include <Galeri_XpetraProblemFactory.hpp> |
| 42 |
#include <Galeri_XpetraUtils.hpp> |
| 43 |
|
| 44 |
#ifdef HAVE_MUELU_BELOS |
| 45 |
#include <BelosConfigDefs.hpp> |
| 46 |
#include <BelosLinearProblem.hpp> |
| 47 |
#include <BelosSolverFactory.hpp> |
| 48 |
#ifdef HAVE_MUELU_TPETRA |
| 49 |
#include <BelosTpetraAdapter.hpp> |
| 50 |
#endif |
| 51 |
#include <BelosXpetraAdapter.hpp> // => This header defines Belos::XpetraOp |
| 52 |
#endif |
| 53 |
#define FOURBYFOUR |
| 54 |
|
| 55 |
#include <unistd.h> |
| 56 |
/**********************************************************************************/ |
| 57 |
|
| 58 |
template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node> |
| 59 |
int main_(Teuchos::CommandLineProcessor &clp, Xpetra::UnderlyingLib lib, int argc, char *argv[]) { |
| 60 |
#include <MueLu_UseShortNames.hpp> |
| 61 |
|
| 62 |
#ifdef BIGVERSION |
| 63 |
std::string suffix("big"); |
| 64 |
#else |
| 65 |
std::string suffix(""); |
| 66 |
#endif |
| 67 |
Teuchos::oblackholestream blackhole; |
| 68 |
|
| 69 |
bool success = true; |
| 70 |
bool verbose = true; |
| 71 |
try { |
| 72 |
Teuchos::RCP<const Teuchos::Comm<int>> comm = Teuchos::DefaultComm<int>::getComm(); |
| 73 |
|
| 74 |
// Four different versions of essentially the same problem can be run. |
| 75 |
// depending on whether or not FOURBYFOUR is defined and whether or not |
| 76 |
// BIGVERSION is defined BIGVERSION just uses a finer mesh than the |
| 77 |
// standard version. Data for the big and the 2x2 versions is kept |
| 78 |
// in https://gitlab-ex.sandia.gov/muelu/mueludata in multiphys directory |
| 79 |
// In the description below we use terms like |
| 80 |
// filename[big].mat. When BIGVERSION is defined, this refers to |
| 81 |
// filenamebig.mat while it refers to filename.mat when BIGVERSION is not |
| 82 |
// defined. FOURBYFOUR decides whether or not a 4x4 or a 2x2 block system |
| 83 |
// is solved. |
| 84 |
// |
| 85 |
// The 2 x 2 block system |
| 86 |
// |
| 87 |
// [ A B ; C D] |
| 88 |
// |
| 89 |
// Presently, the file multiPhys2x2[big].mat is read to define the entries of A, B, C, D. |
| 90 |
// A is supposed to be a 2D elasticity problem while D is a Laplace operator. In particular, |
| 91 |
// the map for D has one DoF per node while the map for A has 2 DoFs per node. Currently, |
| 92 |
// the B and C associated with multiPhys2x2[big].mat are both zero. The larger version |
| 93 |
// of this problem (when FOURBYFOUR is defined) just replicates this system |
| 94 |
// |
| 95 |
// [ A B 0 0 ; C D 0 0 ; 0 0 A B ; 0 0 C D ] |
| 96 |
// |
| 97 |
// and the file read is multiPhys4x4[big].mat. |
| 98 |
// |
| 99 |
// AMG is first applied twice for the regular problem. Specifically, the |
| 100 |
// file aux1[big].mat defines an auxiliary system that AMG is applied to. |
| 101 |
// This auxiliary matrix should have the same dimensions as A. The file |
| 102 |
// aux2[big].mat defines a 2nd auxiliary system that AMG is applied to. |
| 103 |
// This auxiliary matrix should have the same dimensions as D. The grid |
| 104 |
// transfer for the 2x2 matrix is a block diagonal matrix |
| 105 |
// |
| 106 |
// [ P1 0 ; 0 P2 ] |
| 107 |
// |
| 108 |
// where P1 is pulled out of the hierarchy for aux1[big].mat and P2 is pulled |
| 109 |
// out of the hieararchy for aux2[big].mat (on each level). For the 4x4 |
| 110 |
// version, 4 hierarchies are built and a 4x4 block diagonal prolongator |
| 111 |
// is constructed. |
| 112 |
// |
| 113 |
// Additionally, the files coords*[big].mat and null*[big].mat are ready to |
| 114 |
// define the auxiliary coordinates and null spaces needed by the AMG |
| 115 |
// algorithm. For our specific problem, the meshes for the elasticity |
| 116 |
// and the Poisson operator partially overlap (as this is similar |
| 117 |
// to what happens on some ARIA ablation problems). |
| 118 |
|
| 119 |
int nElas, nPois; |
| 120 |
|
| 121 |
// read in sizes of elasticity and Poisson problem |
| 122 |
|
| 123 |
FILE *fp = fopen(std::string("aux1" + suffix + ".mat").c_str(), "r"); |
| 124 |
if (fp == NULL) { |
| 125 |
std::cout << "\nERROR: File aux1" + suffix + ".mat not found" << std::endl; |
| 126 |
return EXIT_FAILURE; |
| 127 |
} |
| 128 |
|
| 129 |
int ch; |
| 130 |
int ret_val = 0; |
| 131 |
(void)ret_val; // suppress fscanf return value and unused variable warnings |
| 132 |
while ((ch = getc(fp)) != '\n') |
| 133 |
; |
| 134 |
ret_val = fscanf(fp, "%d", &nElas); |
| 135 |
|
| 136 |
fp = fopen(std::string("aux2" + suffix + ".mat").c_str(), "r"); |
| 137 |
if (fp == NULL) { |
| 138 |
std::cout << "\nERROR: File aux2" + suffix + ".mat not found" << std::endl; |
| 139 |
return EXIT_FAILURE; |
| 140 |
} |
| 141 |
while ((ch = getc(fp)) != '\n') |
| 142 |
; |
| 143 |
ret_val = fscanf(fp, "%d", &nPois); |
| 144 |
fclose(fp); |
| 145 |
|
| 146 |
// check that multiphysics problem and aux matrices are consistent with respect to sizes |
| 147 |
|
| 148 |
int itemp; |
| 149 |
|
| 150 |
#ifdef FOURBYFOUR |
| 151 |
fp = fopen(std::string("multiPhys4x4" + suffix + ".mat").c_str(), "r"); |
| 152 |
if (fp == NULL) { |
| 153 |
std::cout << "\nERROR: File multiPhys4x4" + suffix + ".mat not found" << std::endl; |
| 154 |
return EXIT_FAILURE; |
| 155 |
} |
| 156 |
while ((ch = getc(fp)) != '\n') |
| 157 |
; |
| 158 |
ret_val = fscanf(fp, "%d", &itemp); |
| 159 |
if (itemp != nElas + nPois + nElas + nPois) { |
| 160 |
std::cout << "\nERROR: multiPhys4x4" + suffix + ".mat dimension is" << itemp << "was expecting it to be " << 2 * (nElas + nPois) << std::endl; |
| 161 |
return EXIT_FAILURE; |
| 162 |
} |
| 163 |
#else |
| 164 |
fp = fopen(std::string("multiPhys2x2" + suffix + ".mat").c_str(), "r"); |
| 165 |
if (fp == NULL) { |
| 166 |
std::cout << "\nERROR: File multiPhys2x2" + suffix + ".mat not found" << std::endl; |
| 167 |
return EXIT_FAILURE; |
| 168 |
} |
| 169 |
while ((ch = getc(fp)) != '\n') |
| 170 |
; |
| 171 |
ret_val = fscanf(fp, "%d", &itemp); |
| 172 |
if (itemp != nElas + nPois) { |
| 173 |
std::cout << "\nERROR: multiPhys2x2" + suffix + ".mat dimension is" << itemp << "was expecting it to be " << (nElas + nPois) << std::endl; |
| 174 |
return EXIT_FAILURE; |
| 175 |
} |
| 176 |
#endif |
| 177 |
|
| 178 |
Teuchos::RCP<const Map> mapPois = MapFactory::Build(lib, nPois, 0, comm); |
| 179 |
Teuchos::RCP<const Map> mapElas1DofPerNode = MapFactory::Build(lib, nElas / 2, 0, comm); |
| 180 |
Teuchos::RCP<const Map> mapElas2DofPerNode = Xpetra::MapFactory<LO, GO, Node>::Build(mapElas1DofPerNode, 2); |
| 181 |
|
| 182 |
Teuchos::ArrayView<const GO> GIDsElas2DofPerNode = mapElas2DofPerNode->getLocalElementList(); |
| 183 |
Teuchos::ArrayView<const GO> GIDsPois = mapPois->getLocalElementList(); |
| 184 |
|
| 185 |
#ifdef FOURBYFOUR |
| 186 |
Teuchos::Array<GO> GIDsCombo(2 * (GIDsElas2DofPerNode.size() + GIDsPois.size())); |
| 187 |
#else |
| 188 |
Teuchos::Array<GO> GIDsCombo(GIDsElas2DofPerNode.size() + GIDsPois.size()); |
| 189 |
#endif |
| 190 |
|
| 191 |
for (int ii = 0; ii < GIDsElas2DofPerNode.size(); ii++) GIDsCombo[ii] = GIDsElas2DofPerNode[ii]; |
| 192 |
for (int ii = 0; ii < GIDsPois.size(); ii++) GIDsCombo[ii + GIDsElas2DofPerNode.size()] = GIDsPois[ii] + nElas; |
| 193 |
#ifdef FOURBYFOUR |
| 194 |
for (int ii = 0; ii < GIDsElas2DofPerNode.size(); ii++) GIDsCombo[ii + GIDsElas2DofPerNode.size() + GIDsPois.size()] = GIDsElas2DofPerNode[ii] + nElas + nPois; |
| 195 |
for (int ii = 0; ii < GIDsPois.size(); ii++) GIDsCombo[ii + 2 * GIDsElas2DofPerNode.size() + GIDsPois.size()] = GIDsPois[ii] + 2 * nElas + nPois; |
| 196 |
#endif |
| 197 |
|
| 198 |
Teuchos::RCP<const Map> mapMultiPhysA = Xpetra::MapFactory<LO, GO, Node>::Build(lib, Teuchos::OrdinalTraits<Xpetra::global_size_t>::invalid(), GIDsCombo, mapElas2DofPerNode->getIndexBase(), comm); |
| 199 |
#ifdef FOURBYFOUR |
| 200 |
Teuchos::RCP<Xpetra::Matrix<Scalar, LocalOrdinal, GlobalOrdinal, Node>> multiPhysA = Xpetra::IO<SC, LO, GO, Node>::Read("multiPhys4x4" + suffix + ".mat", mapMultiPhysA); |
| 201 |
#else |
| 202 |
Teuchos::RCP<Xpetra::Matrix<Scalar, LocalOrdinal, GlobalOrdinal, Node>> multiPhysA = Xpetra::IO<SC, LO, GO, Node>::Read("multiPhys2x2" + suffix + ".mat", mapMultiPhysA); |
| 203 |
#endif |
| 204 |
|
| 205 |
// To solve the block system a block diagonal auxiliary operator such as |
| 206 |
// |
| 207 |
// [ aux1 0 0 0 ] |
| 208 |
// [ 0 aux1 0 0 ] |
| 209 |
// [ 0 0 aux1 0 ] |
| 210 |
// [ 0 0 0 aux2 ] |
| 211 |
// |
| 212 |
// |
| 213 |
// is used in conjunction with multiple invocations of Hierarchy Setup |
| 214 |
|
| 215 |
Teuchos::RCP<Xpetra::Matrix<Scalar, LocalOrdinal, GlobalOrdinal, Node>> aux1 = Xpetra::IO<SC, LO, GO, Node>::Read("aux1" + suffix + ".mat", mapElas2DofPerNode); |
| 216 |
Teuchos::RCP<Xpetra::Matrix<Scalar, LocalOrdinal, GlobalOrdinal, Node>> aux2 = Xpetra::IO<SC, LO, GO, Node>::Read("aux2" + suffix + ".mat", mapPois); |
| 217 |
|
| 218 |
typedef Teuchos::ScalarTraits<SC> STS; |
| 219 |
typedef typename STS::magnitudeType real_type; |
| 220 |
typedef typename Teuchos::ScalarTraits<Scalar>::coordinateType coordinate_type; |
| 221 |
typedef Xpetra::MultiVector<real_type, LO, GO, NO> RealValuedMultiVector; |
| 222 |
typedef Xpetra::MultiVectorFactory<coordinate_type, LO, GO, NO> RealValuedMultiVectorFactory; |
| 223 |
|
| 224 |
Teuchos::RCP<Xpetra::MultiVector<typename Teuchos::ScalarTraits<Scalar>::magnitudeType, LocalOrdinal, GlobalOrdinal, Node>> elasCoords = Xpetra::IO<real_type, LO, GO, Node>::ReadMultiVector("coords1" + suffix + ".mat", mapElas1DofPerNode); |
| 225 |
Teuchos::RCP<Xpetra::MultiVector<typename Teuchos::ScalarTraits<Scalar>::magnitudeType, LocalOrdinal, GlobalOrdinal, Node>> poisCoords = Xpetra::IO<real_type, LO, GO, Node>::ReadMultiVector("coords2" + suffix + ".mat", mapPois); |
| 226 |
|
| 227 |
Teuchos::RCP<Xpetra::MultiVector<Scalar, LocalOrdinal, GlobalOrdinal, Node>> nullspace1 = Xpetra::IO<SC, LO, GO, Node>::ReadMultiVector("null1" + suffix + ".mat", mapElas2DofPerNode); |
| 228 |
Teuchos::RCP<Xpetra::MultiVector<Scalar, LocalOrdinal, GlobalOrdinal, Node>> nullspace2 = Xpetra::IO<SC, LO, GO, Node>::ReadMultiVector("null2" + suffix + ".mat", mapPois); |
| 229 |
|
| 230 |
// Note: Entries of arrayOfNullspaces[] may be set to null. |
| 231 |
#ifdef FOURBYFOUR |
| 232 |
Teuchos::ArrayRCP<Teuchos::RCP<Matrix>> arrayOfAuxMatrices(4); |
| 233 |
Teuchos::ArrayRCP<Teuchos::RCP<RealValuedMultiVector>> arrayOfCoords(4); |
| 234 |
Teuchos::ArrayRCP<Teuchos::RCP<MultiVector>> arrayOfNullspaces(4); |
| 235 |
|
| 236 |
arrayOfAuxMatrices[2] = aux1; |
| 237 |
arrayOfAuxMatrices[3] = aux2; |
| 238 |
arrayOfCoords[2] = elasCoords; |
| 239 |
arrayOfCoords[3] = poisCoords; |
| 240 |
arrayOfNullspaces[2] = nullspace1; |
| 241 |
arrayOfNullspaces[3] = nullspace2; |
| 242 |
#else |
| 243 |
Teuchos::ArrayRCP<Teuchos::RCP<Matrix>> arrayOfAuxMatrices(2); |
| 244 |
Teuchos::ArrayRCP<Teuchos::RCP<RealValuedMultiVector>> arrayOfCoords(2); |
| 245 |
Teuchos::ArrayRCP<Teuchos::RCP<MultiVector>> arrayOfNullspaces(2); |
| 246 |
#endif |
| 247 |
|
| 248 |
arrayOfAuxMatrices[0] = aux1; |
| 249 |
arrayOfAuxMatrices[1] = aux2; |
| 250 |
arrayOfCoords[0] = elasCoords; |
| 251 |
arrayOfCoords[1] = poisCoords; |
| 252 |
arrayOfNullspaces[0] = nullspace1; |
| 253 |
arrayOfNullspaces[1] = nullspace2; |
| 254 |
|
| 255 |
Teuchos::ParameterList comboList; |
| 256 |
Teuchos::updateParametersFromXmlFileAndBroadcast("comboP.xml", Teuchos::Ptr<Teuchos::ParameterList>(&comboList), *comm); |
| 257 |
#ifdef FOURBYFOUR |
| 258 |
Teuchos::RCP<Operator> preconditioner = rcp(new MueLu::MultiPhys<SC, LO, GO, NO>(multiPhysA, arrayOfAuxMatrices, arrayOfNullspaces, arrayOfCoords, 4, comboList, true)); |
| 259 |
#else |
| 260 |
Teuchos::RCP<Operator> preconditioner = rcp(new MueLu::MultiPhys<SC, LO, GO, NO>(multiPhysA, arrayOfAuxMatrices, arrayOfNullspaces, arrayOfCoords, 2, comboList, true)); |
| 261 |
#endif |
| 262 |
|
| 263 |
Teuchos::RCP<Teuchos::TimeMonitor> globalTimeMonitor = Teuchos::rcp(new Teuchos::TimeMonitor(*Teuchos::TimeMonitor::getNewTimer("Timings: Global Time"))); |
| 264 |
|
| 265 |
// ========================================================================= |
| 266 |
// System solution (Ax = b) |
| 267 |
// ========================================================================= |
| 268 |
typedef Teuchos::ScalarTraits<SC> STS; |
| 269 |
SC zero = STS::zero(), one = STS::one(); |
| 270 |
|
| 271 |
Teuchos::RCP<Vector> X = VectorFactory::Build(multiPhysA->getRowMap()); |
| 272 |
Teuchos::RCP<Vector> B = VectorFactory::Build(multiPhysA->getRowMap()); |
| 273 |
|
| 274 |
{ |
| 275 |
// set seed for reproducibility |
| 276 |
Utilities::SetRandomSeed(*comm); |
| 277 |
X->randomize(); |
| 278 |
multiPhysA->apply(*X, *B, Teuchos::NO_TRANS, one, zero); |
| 279 |
|
| 280 |
Teuchos::Array<typename STS::magnitudeType> norms(1); |
| 281 |
B->norm2(norms); |
| 282 |
B->scale(one / norms[0]); |
| 283 |
X->putScalar(zero); |
| 284 |
} |
| 285 |
|
| 286 |
// Belos linear problem |
| 287 |
typedef MultiVector MV; |
| 288 |
typedef Belos::OperatorT<MV> OP; |
| 289 |
|
| 290 |
Teuchos::RCP<OP> belosOp = Teuchos::rcp(new Belos::XpetraOp<SC, LO, GO, NO>(multiPhysA)); |
| 291 |
Teuchos::RCP<OP> belosPrecOp = Teuchos::rcp(new Belos::XpetraOp<SC, LO, GO, NO>(preconditioner)); // Turns a Xpetra::Matrix object into a Belos operator |
| 292 |
Teuchos::RCP<Belos::LinearProblem<SC, MV, OP>> belosProblem = rcp(new Belos::LinearProblem<SC, MV, OP>(belosOp, X, B)); |
| 293 |
belosProblem->setRightPrec(belosPrecOp); |
| 294 |
bool set = belosProblem->setProblem(); |
| 295 |
if (set == false) { |
| 296 |
std::cout << "\nERROR: Belos::LinearProblem failed to set up correctly!" << std::endl; |
| 297 |
return EXIT_FAILURE; |
| 298 |
} |
| 299 |
Teuchos::RCP<Teuchos::ParameterList> belosList = Teuchos::parameterList(); |
| 300 |
belosList->set("Maximum Iterations", 100); |
| 301 |
belosList->set("Convergence Tolerance", 1e-6); |
| 302 |
belosList->set("Verbosity", Belos::Errors + Belos::Warnings + Belos::StatusTestDetails); |
| 303 |
belosList->set("Output Frequency", 1); |
| 304 |
belosList->set("Output Style", Belos::Brief); |
| 305 |
belosList->set("Implicit Residual Scaling", "None"); |
| 306 |
|
| 307 |
Belos::SolverFactory<SC, MV, OP> solverFactory; |
| 308 |
Teuchos::RCP<Belos::SolverManager<SC, MV, OP>> solver = solverFactory.create("gmres", belosList); |
| 309 |
solver->setProblem(belosProblem); |
| 310 |
Belos::ReturnType retStatus = Belos::Unconverged; |
| 311 |
retStatus = solver->solve(); |
| 312 |
|
| 313 |
int iters = solver->getNumIters(); |
| 314 |
success = (iters < 50 && retStatus == Belos::Converged); |
| 315 |
if (comm->getRank() == 0) { |
| 316 |
if (success) |
| 317 |
std::cout << "SUCCESS! Belos converged in " << iters << " iterations." << std::endl; |
| 318 |
else |
| 319 |
std::cout << "FAILURE! Belos did not converge fast enough." << std::endl; |
| 320 |
} |
| 321 |
|
| 322 |
// Timer final summaries |
| 323 |
globalTimeMonitor = Teuchos::null; // stop this timer before summary |
| 324 |
Teuchos::TimeMonitor::summarize(); |
| 325 |
} |
| 326 |
TEUCHOS_STANDARD_CATCH_STATEMENTS(verbose, std::cerr, success); |
| 327 |
|
| 328 |
return (success ? EXIT_SUCCESS : EXIT_FAILURE); |
| 329 |
} |
| 330 |
|
| 331 |
//- -- -------------------------------------------------------- |
| 332 |
#define MUELU_AUTOMATIC_TEST_ETI_NAME main_ |
| 333 |
#include "MueLu_Test_ETI.hpp" |
| 334 |
|
| 335 |
int main(int argc, char *argv[]) { |
| 336 |
return Automatic_Test_ETI(argc, argv); |
| 337 |
} |