| 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 <Teuchos_UnitTestHarness.hpp> |
| 11 |
#include <MueLu_TestHelpers.hpp> |
| 12 |
#include "MueLu_TestHelpersSmoothers.hpp" |
| 13 |
|
| 14 |
#include <MueLu_Ifpack2Smoother.hpp> |
| 15 |
#include <MueLu_Utilities.hpp> |
| 16 |
|
| 17 |
/* |
| 18 |
Comments about tests with hard coded results: |
| 19 |
1) Chebyshev smoothing must pass for any number of processors. |
| 20 |
2) Gauss-Seidel must pass for 1 and 4 processors. |
| 21 |
3) For any processor count except 1 and 4, the Gauss-Seidel test will |
| 22 |
report "passing", but this is only because the Teuchos test macro is skipped. |
| 23 |
*/ |
| 24 |
|
| 25 |
namespace MueLuTests { |
| 26 |
|
| 27 |
// this namespace already has: #include "MueLu_UseShortNames.hpp" |
| 28 |
using namespace TestHelpers::Smoothers; |
| 29 |
|
| 30 |
TEUCHOS_UNIT_TEST_TEMPLATE_4_DECL(Ifpack2Smoother, NotSetup, Scalar, LocalOrdinal, GlobalOrdinal, Node) { |
| 31 |
#include <MueLu_UseShortNames.hpp> |
| 32 |
MUELU_TESTING_SET_OSTREAM; |
| 33 |
MUELU_TESTING_LIMIT_SCOPE(Scalar, GlobalOrdinal, Node); |
| 34 |
MUELU_TEST_ONLY_FOR(Xpetra::UseTpetra) { |
| 35 |
Ifpack2Smoother smoother("RELAXATION", Teuchos::ParameterList()); |
| 36 |
testApplyNoSetup(smoother, out, success); |
| 37 |
} |
| 38 |
} |
| 39 |
|
| 40 |
// Tests interface to Ifpack2's Jacobi preconditioner. |
| 41 |
TEUCHOS_UNIT_TEST_TEMPLATE_4_DECL(Ifpack2Smoother, HardCodedResult_Jacobi, Scalar, LocalOrdinal, GlobalOrdinal, Node) { |
| 42 |
#include <MueLu_UseShortNames.hpp> |
| 43 |
MUELU_TESTING_SET_OSTREAM; |
| 44 |
MUELU_TESTING_LIMIT_SCOPE(Scalar, GlobalOrdinal, Node); |
| 45 |
MUELU_TEST_ONLY_FOR(Xpetra::UseTpetra) { |
| 46 |
if (Teuchos::ScalarTraits<Scalar>::isComplex) { |
| 47 |
out << "Skipping Tpetra for SC type \"complex\"" << std::endl; |
| 48 |
return; |
| 49 |
} |
| 50 |
Teuchos::ParameterList paramList; |
| 51 |
paramList.set("relaxation: type", "Jacobi"); |
| 52 |
paramList.set("relaxation: sweeps", (int)1); |
| 53 |
paramList.set("relaxation: damping factor", (double)0.8); |
| 54 |
paramList.set("relaxation: zero starting solution", false); |
| 55 |
|
| 56 |
Ifpack2Smoother smoother("RELAXATION", paramList); |
| 57 |
|
| 58 |
typename Teuchos::ScalarTraits<SC>::magnitudeType residualNorms = testApply_A125_X1_RHS0(smoother, out, success); |
| 59 |
|
| 60 |
RCP<const Teuchos::Comm<int> > comm = TestHelpers::Parameters::getDefaultComm(); |
| 61 |
const typename Teuchos::ScalarTraits<SC>::magnitudeType expectedNorm = 6.3245553203367577133e-01; |
| 62 |
switch (comm->getSize()) { |
| 63 |
case 1: |
| 64 |
case 4: |
| 65 |
TEST_FLOATING_EQUALITY(residualNorms, expectedNorm, 1e-12); |
| 66 |
break; |
| 67 |
default: |
| 68 |
out << "Pass/Fail is checked only for 1 and 4 processes." << std::endl; |
| 69 |
break; |
| 70 |
} // switch |
| 71 |
|
| 72 |
auto ifpack2prec = smoother.getPreconditioner(); |
| 73 |
|
| 74 |
// reuse setup & solve |
| 75 |
residualNorms = testApply_A125_X1_RHS0(smoother, out, success); |
| 76 |
switch (comm->getSize()) { |
| 77 |
case 1: |
| 78 |
case 4: |
| 79 |
TEST_FLOATING_EQUALITY(residualNorms, expectedNorm, 1e-12); |
| 80 |
break; |
| 81 |
default: |
| 82 |
out << "Pass/Fail is checked only for 1 and 4 processes." << std::endl; |
| 83 |
break; |
| 84 |
} // switch |
| 85 |
|
| 86 |
// make sure the Ifpack2 preconditioner did not get replaced |
| 87 |
TEUCHOS_ASSERT_EQUALITY(ifpack2prec.ptr(), smoother.getPreconditioner().ptr()); |
| 88 |
} |
| 89 |
} // Jacobi |
| 90 |
|
| 91 |
// Tests interface to Ifpack2's Gauss-Seidel preconditioner. |
| 92 |
TEUCHOS_UNIT_TEST_TEMPLATE_4_DECL(Ifpack2Smoother, HardCodedResult_GaussSeidel, Scalar, LocalOrdinal, GlobalOrdinal, Node) { |
| 93 |
#include <MueLu_UseShortNames.hpp> |
| 94 |
MUELU_TESTING_SET_OSTREAM; |
| 95 |
MUELU_TESTING_LIMIT_SCOPE(Scalar, GlobalOrdinal, Node); |
| 96 |
MUELU_TEST_ONLY_FOR(Xpetra::UseTpetra) { |
| 97 |
if (Teuchos::ScalarTraits<Scalar>::isComplex) { |
| 98 |
out << "Skipping Tpetra for SC type \"complex\"" << std::endl; |
| 99 |
return; |
| 100 |
} |
| 101 |
Teuchos::ParameterList paramList; |
| 102 |
paramList.set("relaxation: type", "Gauss-Seidel"); |
| 103 |
paramList.set("relaxation: sweeps", (int)1); |
| 104 |
paramList.set("relaxation: damping factor", (double)1.0); |
| 105 |
paramList.set("relaxation: zero starting solution", false); |
| 106 |
|
| 107 |
Ifpack2Smoother smoother("RELAXATION", paramList); |
| 108 |
|
| 109 |
typename Teuchos::ScalarTraits<SC>::magnitudeType residualNorms = testApply_A125_X1_RHS0(smoother, out, success); |
| 110 |
|
| 111 |
RCP<const Teuchos::Comm<int> > comm = TestHelpers::Parameters::getDefaultComm(); |
| 112 |
const typename Teuchos::ScalarTraits<SC>::magnitudeType expectedNorm = 5.773502691896257e-01; |
| 113 |
switch (comm->getSize()) { |
| 114 |
case 1: |
| 115 |
case 4: |
| 116 |
TEST_FLOATING_EQUALITY(residualNorms, expectedNorm, 1e-12); |
| 117 |
break; |
| 118 |
default: |
| 119 |
out << "Pass/Fail is checked only for 1 and 4 processes." << std::endl; |
| 120 |
break; |
| 121 |
} // switch |
| 122 |
|
| 123 |
auto ifpack2prec = smoother.getPreconditioner(); |
| 124 |
|
| 125 |
// reuse setup & solve |
| 126 |
residualNorms = testApply_A125_X1_RHS0(smoother, out, success); |
| 127 |
switch (comm->getSize()) { |
| 128 |
case 1: |
| 129 |
case 4: |
| 130 |
TEST_FLOATING_EQUALITY(residualNorms, expectedNorm, 1e-12); |
| 131 |
break; |
| 132 |
default: |
| 133 |
out << "Pass/Fail is checked only for 1 and 4 processes." << std::endl; |
| 134 |
break; |
| 135 |
} // switch |
| 136 |
|
| 137 |
// make sure the Ifpack2 preconditioner did not get replaced |
| 138 |
TEUCHOS_ASSERT_EQUALITY(ifpack2prec.ptr(), smoother.getPreconditioner().ptr()); |
| 139 |
} |
| 140 |
} // GaussSeidel |
| 141 |
|
| 142 |
// Tests interface to Ifpack2's Gauss-Seidel preconditioner. |
| 143 |
TEUCHOS_UNIT_TEST_TEMPLATE_4_DECL(Ifpack2Smoother, HardCodedResult_GaussSeidel2, Scalar, LocalOrdinal, GlobalOrdinal, Node) { |
| 144 |
#include <MueLu_UseShortNames.hpp> |
| 145 |
MUELU_TESTING_SET_OSTREAM; |
| 146 |
MUELU_TESTING_LIMIT_SCOPE(Scalar, GlobalOrdinal, Node); |
| 147 |
MUELU_TEST_ONLY_FOR(Xpetra::UseTpetra) { |
| 148 |
if (Teuchos::ScalarTraits<Scalar>::isComplex) { |
| 149 |
out << "Skipping Tpetra for SC type \"complex\"" << std::endl; |
| 150 |
return; |
| 151 |
} |
| 152 |
Teuchos::ParameterList paramList; |
| 153 |
paramList.set("relaxation: type", "Gauss-Seidel"); |
| 154 |
paramList.set("relaxation: sweeps", (int)10); |
| 155 |
paramList.set("relaxation: damping factor", (double)1.0); |
| 156 |
paramList.set("relaxation: zero starting solution", false); |
| 157 |
|
| 158 |
Ifpack2Smoother smoother("RELAXATION", paramList); |
| 159 |
|
| 160 |
typename Teuchos::ScalarTraits<SC>::magnitudeType residualNorms = testApply_A125_X1_RHS0(smoother, out, success); |
| 161 |
|
| 162 |
RCP<const Teuchos::Comm<int> > comm = TestHelpers::Parameters::getDefaultComm(); |
| 163 |
const typename Teuchos::ScalarTraits<SC>::magnitudeType expectedNorm1 = 8.326553652741774e-02; |
| 164 |
const typename Teuchos::ScalarTraits<SC>::magnitudeType expectedNorm4 = 8.326553653078517e-02; |
| 165 |
switch (comm->getSize()) { |
| 166 |
case 1: |
| 167 |
TEST_FLOATING_EQUALITY(residualNorms, expectedNorm1, 1e-12); |
| 168 |
break; |
| 169 |
case 4: |
| 170 |
TEST_FLOATING_EQUALITY(residualNorms, expectedNorm4, 1e-12); |
| 171 |
break; |
| 172 |
default: |
| 173 |
out << "Pass/Fail is checked only for 1 and 4 processes." << std::endl; |
| 174 |
break; |
| 175 |
} // switch |
| 176 |
|
| 177 |
auto ifpack2prec = smoother.getPreconditioner(); |
| 178 |
|
| 179 |
// reuse setup & solve |
| 180 |
residualNorms = testApply_A125_X1_RHS0(smoother, out, success); |
| 181 |
switch (comm->getSize()) { |
| 182 |
case 1: |
| 183 |
TEST_FLOATING_EQUALITY(residualNorms, expectedNorm1, 1e-12); |
| 184 |
break; |
| 185 |
case 4: |
| 186 |
TEST_FLOATING_EQUALITY(residualNorms, expectedNorm4, 1e-12); |
| 187 |
break; |
| 188 |
default: |
| 189 |
out << "Pass/Fail is checked only for 1 and 4 processes." << std::endl; |
| 190 |
break; |
| 191 |
} // switch |
| 192 |
|
| 193 |
// make sure the Ifpack2 preconditioner did not get replaced |
| 194 |
TEUCHOS_ASSERT_EQUALITY(ifpack2prec.ptr(), smoother.getPreconditioner().ptr()); |
| 195 |
} |
| 196 |
} // GaussSeidel |
| 197 |
|
| 198 |
// Tests interface to Ifpack2's Chebyshev preconditioner |
| 199 |
TEUCHOS_UNIT_TEST_TEMPLATE_4_DECL(Ifpack2Smoother, HardCodedResult_Chebyshev, Scalar, LocalOrdinal, GlobalOrdinal, Node) { |
| 200 |
#include <MueLu_UseShortNames.hpp> |
| 201 |
MUELU_TESTING_SET_OSTREAM; |
| 202 |
MUELU_TESTING_LIMIT_SCOPE(Scalar, GlobalOrdinal, Node); |
| 203 |
MUELU_TEST_ONLY_FOR(Xpetra::UseTpetra) { |
| 204 |
using magnitude_type = typename Teuchos::ScalarTraits<SC>::magnitudeType; |
| 205 |
using TMT = Teuchos::ScalarTraits<magnitude_type>; |
| 206 |
|
| 207 |
if (Teuchos::ScalarTraits<Scalar>::isComplex) { |
| 208 |
out << "Skipping Tpetra for SC type \"complex\"" << std::endl; |
| 209 |
return; |
| 210 |
} |
| 211 |
|
| 212 |
Teuchos::ParameterList paramList; |
| 213 |
paramList.set("chebyshev: degree", (int)3); |
| 214 |
paramList.set("chebyshev: max eigenvalue", (double)1.98476); |
| 215 |
paramList.set("chebyshev: min eigenvalue", (double)1.0); |
| 216 |
paramList.set("chebyshev: ratio eigenvalue", (double)20); |
| 217 |
paramList.set("chebyshev: zero starting solution", false); |
| 218 |
Ifpack2Smoother smoother("CHEBYSHEV", paramList); |
| 219 |
|
| 220 |
typename Teuchos::ScalarTraits<SC>::magnitudeType residualNorms = testApply_A125_X1_RHS0(smoother, out, success); |
| 221 |
const typename Teuchos::ScalarTraits<SC>::magnitudeType expectedNorm = 5.269156e-01; |
| 222 |
TEST_FLOATING_EQUALITY(residualNorms, expectedNorm, (1e-7 < TMT::eps() ? 10 * TMT::eps() : 1e-7)); // Compare to residual reported by ML |
| 223 |
|
| 224 |
auto ifpack2prec = smoother.getPreconditioner(); |
| 225 |
|
| 226 |
// reuse setup & solve |
| 227 |
residualNorms = testApply_A125_X1_RHS0(smoother, out, success); |
| 228 |
TEST_FLOATING_EQUALITY(residualNorms, expectedNorm, (1e-7 < TMT::eps() ? 10 * TMT::eps() : 1e-7)); // Compare to residual reported by ML |
| 229 |
|
| 230 |
// make sure the Ifpack2 preconditioner did not get replaced |
| 231 |
TEUCHOS_ASSERT_EQUALITY(ifpack2prec.ptr(), smoother.getPreconditioner().ptr()); |
| 232 |
} |
| 233 |
} // Chebyshev |
| 234 |
|
| 235 |
// Tests interface to Ifpack2's ILU(0) preconditioner. |
| 236 |
TEUCHOS_UNIT_TEST_TEMPLATE_4_DECL(Ifpack2Smoother, HardCodedResult_ILU, Scalar, LocalOrdinal, GlobalOrdinal, Node) { |
| 237 |
#include <MueLu_UseShortNames.hpp> |
| 238 |
MUELU_TESTING_SET_OSTREAM; |
| 239 |
MUELU_TESTING_LIMIT_SCOPE(Scalar, GlobalOrdinal, Node); |
| 240 |
MUELU_TEST_ONLY_FOR(Xpetra::UseTpetra) { |
| 241 |
// FIXME this will probably fail in parallel b/c it becomes block Jacobi |
| 242 |
using magnitude_type = typename Teuchos::ScalarTraits<SC>::magnitudeType; |
| 243 |
|
| 244 |
Teuchos::ParameterList paramList; |
| 245 |
Ifpack2Smoother smoother("ILUT", paramList); |
| 246 |
|
| 247 |
magnitude_type residualNorms = testApply_A125_X0_RandomRHS(smoother, out, success); |
| 248 |
|
| 249 |
RCP<const Teuchos::Comm<int> > comm = TestHelpers::Parameters::getDefaultComm(); |
| 250 |
if (comm->getSize() == 1) { |
| 251 |
TEST_EQUALITY(residualNorms < 100 * Teuchos::ScalarTraits<magnitude_type>::eps(), true); |
| 252 |
} else { |
| 253 |
out << "Pass/Fail is only checked in serial." << std::endl; |
| 254 |
} |
| 255 |
|
| 256 |
auto ifpack2prec = smoother.getPreconditioner(); |
| 257 |
|
| 258 |
// reuse setup & solve |
| 259 |
residualNorms = testApply_A125_X1_RHS0(smoother, out, success); |
| 260 |
if (comm->getSize() == 1) { |
| 261 |
TEST_EQUALITY(residualNorms < 100 * Teuchos::ScalarTraits<magnitude_type>::eps(), true); |
| 262 |
} else { |
| 263 |
out << "Pass/Fail is only checked in serial." << std::endl; |
| 264 |
} |
| 265 |
|
| 266 |
// make sure the Ifpack2 preconditioner did not get replaced |
| 267 |
TEUCHOS_ASSERT_EQUALITY(ifpack2prec.ptr(), smoother.getPreconditioner().ptr()); |
| 268 |
} |
| 269 |
} // ILU |
| 270 |
|
| 271 |
// Tests two sweeps of ILUT in Ifpack2 |
| 272 |
TEUCHOS_UNIT_TEST_TEMPLATE_4_DECL(Ifpack2Smoother, ILU_TwoSweeps, Scalar, LocalOrdinal, GlobalOrdinal, Node) { |
| 273 |
#include <MueLu_UseShortNames.hpp> |
| 274 |
MUELU_TESTING_SET_OSTREAM; |
| 275 |
MUELU_TESTING_LIMIT_SCOPE(Scalar, GlobalOrdinal, Node); |
| 276 |
typedef typename Teuchos::ScalarTraits<SC>::magnitudeType magnitude_type; |
| 277 |
MUELU_TEST_ONLY_FOR(Xpetra::UseTpetra) { |
| 278 |
// FIXME this will probably fail in parallel b/c it becomes block Jacobi |
| 279 |
|
| 280 |
Teuchos::ParameterList paramList; |
| 281 |
Ifpack2Smoother smoother("ILUT", paramList); |
| 282 |
|
| 283 |
// I don't use the testApply infrastructure because it has no provision for an initial guess. |
| 284 |
Teuchos::RCP<Matrix> A = TestHelpers::TestFactory<SC, LO, GO, NO>::Build1DPoisson(125); |
| 285 |
Level level; |
| 286 |
TestHelpers::TestFactory<SC, LO, GO, NO>::createSingleLevelHierarchy(level); |
| 287 |
level.Set("A", A); |
| 288 |
smoother.Setup(level); |
| 289 |
|
| 290 |
RCP<MultiVector> X = MultiVectorFactory::Build(A->getDomainMap(), 1); |
| 291 |
RCP<MultiVector> RHS = MultiVectorFactory::Build(A->getRangeMap(), 1); |
| 292 |
|
| 293 |
// Random X |
| 294 |
X->setSeed(846930886); |
| 295 |
X->randomize(); |
| 296 |
|
| 297 |
// Normalize X |
| 298 |
Array<magnitude_type> norms(1); |
| 299 |
X->norm2(norms); |
| 300 |
X->scale(1 / norms[0]); |
| 301 |
|
| 302 |
// Compute RHS corresponding to X |
| 303 |
A->apply(*X, *RHS, Teuchos::NO_TRANS, (SC)1.0, (SC)0.0); |
| 304 |
|
| 305 |
// Reset X to 0 |
| 306 |
X->putScalar((SC)0.0); |
| 307 |
|
| 308 |
RHS->norm2(norms); |
| 309 |
out << "||RHS|| = " << std::setiosflags(std::ios::fixed) << std::setprecision(10) << norms[0] << std::endl; |
| 310 |
|
| 311 |
out << "solve with zero initial guess" << std::endl; |
| 312 |
Teuchos::Array<magnitude_type> initialNorms(1); |
| 313 |
X->norm2(initialNorms); |
| 314 |
out << " ||X_initial|| = " << std::setiosflags(std::ios::fixed) << std::setprecision(10) << initialNorms[0] << std::endl; |
| 315 |
|
| 316 |
smoother.Apply(*X, *RHS, true); // zero initial guess |
| 317 |
|
| 318 |
Teuchos::Array<magnitude_type> finalNorms(1); |
| 319 |
X->norm2(finalNorms); |
| 320 |
Teuchos::Array<magnitude_type> residualNorm1 = Utilities::ResidualNorm(*A, *X, *RHS); |
| 321 |
out << " ||Residual_final|| = " << std::setiosflags(std::ios::fixed) << std::setprecision(20) << residualNorm1[0] << std::endl; |
| 322 |
out << " ||X_final|| = " << std::setiosflags(std::ios::fixed) << std::setprecision(10) << finalNorms[0] << std::endl; |
| 323 |
|
| 324 |
out << "solve with random initial guess" << std::endl; |
| 325 |
X->randomize(); |
| 326 |
X->norm2(initialNorms); |
| 327 |
out << " ||X_initial|| = " << std::setiosflags(std::ios::fixed) << std::setprecision(10) << initialNorms[0] << std::endl; |
| 328 |
|
| 329 |
smoother.Apply(*X, *RHS, false); // nonzero initial guess |
| 330 |
|
| 331 |
X->norm2(finalNorms); |
| 332 |
Teuchos::Array<magnitude_type> residualNorm2 = Utilities::ResidualNorm(*A, *X, *RHS); |
| 333 |
out << " ||Residual_final|| = " << std::setiosflags(std::ios::fixed) << std::setprecision(20) << residualNorm2[0] << std::endl; |
| 334 |
out << " ||X_final|| = " << std::setiosflags(std::ios::fixed) << std::setprecision(10) << finalNorms[0] << std::endl; |
| 335 |
|
| 336 |
RCP<const Teuchos::Comm<int> > comm = TestHelpers::Parameters::getDefaultComm(); |
| 337 |
if (comm->getSize() == 1) { |
| 338 |
// TEST_EQUALITY(residualNorms < 1e-10, true); |
| 339 |
TEST_EQUALITY(residualNorm1[0] != residualNorm2[0], true); |
| 340 |
} else { |
| 341 |
out << "Pass/Fail is only checked in serial." << std::endl; |
| 342 |
} |
| 343 |
} |
| 344 |
} // ILU |
| 345 |
|
| 346 |
// Make sure Ifpack2's Banded relaxation actually gets called |
| 347 |
TEUCHOS_UNIT_TEST_TEMPLATE_4_DECL(Ifpack2Smoother, BandedRelaxation, Scalar, LocalOrdinal, GlobalOrdinal, Node) { |
| 348 |
#include <MueLu_UseShortNames.hpp> |
| 349 |
MUELU_TESTING_SET_OSTREAM; |
| 350 |
MUELU_TESTING_LIMIT_SCOPE(Scalar, GlobalOrdinal, Node); |
| 351 |
typedef typename Teuchos::ScalarTraits<SC>::magnitudeType magnitude_type; |
| 352 |
MUELU_TEST_ONLY_FOR(Xpetra::UseTpetra) { |
| 353 |
Teuchos::ParameterList paramList; |
| 354 |
paramList.set("partitioner: PDE equations", 5); // Warning: This number has to be compatible with the problem size |
| 355 |
Ifpack2Smoother smoother("BANDED RELAXATION", paramList); |
| 356 |
|
| 357 |
// I don't use the testApply infrastructure because it has no provision for an initial guess. |
| 358 |
Teuchos::RCP<Matrix> A = TestHelpers::TestFactory<SC, LO, GO, NO>::Build1DPoisson(125); |
| 359 |
Level level; |
| 360 |
TestHelpers::TestFactory<SC, LO, GO, NO>::createSingleLevelHierarchy(level); |
| 361 |
level.Set("A", A); |
| 362 |
smoother.Setup(level); |
| 363 |
|
| 364 |
RCP<MultiVector> X = MultiVectorFactory::Build(A->getDomainMap(), 1); |
| 365 |
RCP<MultiVector> RHS = MultiVectorFactory::Build(A->getRangeMap(), 1); |
| 366 |
|
| 367 |
// Random X |
| 368 |
X->setSeed(846930886); |
| 369 |
X->randomize(); |
| 370 |
|
| 371 |
// Normalize X |
| 372 |
Array<magnitude_type> norms(1); |
| 373 |
X->norm2(norms); |
| 374 |
X->scale(1 / norms[0]); |
| 375 |
|
| 376 |
// Compute RHS corresponding to X |
| 377 |
A->apply(*X, *RHS, Teuchos::NO_TRANS, (SC)1.0, (SC)0.0); |
| 378 |
|
| 379 |
// Reset X to 0 |
| 380 |
X->putScalar((SC)0.0); |
| 381 |
|
| 382 |
RHS->norm2(norms); |
| 383 |
out << "||RHS|| = " << std::setiosflags(std::ios::fixed) << std::setprecision(10) << norms[0] << std::endl; |
| 384 |
|
| 385 |
out << "solve with zero initial guess" << std::endl; |
| 386 |
Teuchos::Array<magnitude_type> initialNorms(1); |
| 387 |
X->norm2(initialNorms); |
| 388 |
out << " ||X_initial|| = " << std::setiosflags(std::ios::fixed) << std::setprecision(10) << initialNorms[0] << std::endl; |
| 389 |
|
| 390 |
smoother.Apply(*X, *RHS, true); // zero initial guess |
| 391 |
|
| 392 |
Teuchos::Array<magnitude_type> finalNorms(1); |
| 393 |
X->norm2(finalNorms); |
| 394 |
Teuchos::Array<magnitude_type> residualNorm1 = Utilities::ResidualNorm(*A, *X, *RHS); |
| 395 |
out << " ||Residual_final|| = " << std::setiosflags(std::ios::fixed) << std::setprecision(20) << residualNorm1[0] << std::endl; |
| 396 |
out << " ||X_final|| = " << std::setiosflags(std::ios::fixed) << std::setprecision(10) << finalNorms[0] << std::endl; |
| 397 |
|
| 398 |
out << "solve with random initial guess" << std::endl; |
| 399 |
X->randomize(); |
| 400 |
X->norm2(initialNorms); |
| 401 |
out << " ||X_initial|| = " << std::setiosflags(std::ios::fixed) << std::setprecision(10) << initialNorms[0] << std::endl; |
| 402 |
|
| 403 |
smoother.Apply(*X, *RHS, false); // nonzero initial guess |
| 404 |
|
| 405 |
X->norm2(finalNorms); |
| 406 |
Teuchos::Array<magnitude_type> residualNorm2 = Utilities::ResidualNorm(*A, *X, *RHS); |
| 407 |
out << " ||Residual_final|| = " << std::setiosflags(std::ios::fixed) << std::setprecision(20) << residualNorm2[0] << std::endl; |
| 408 |
out << " ||X_final|| = " << std::setiosflags(std::ios::fixed) << std::setprecision(10) << finalNorms[0] << std::endl; |
| 409 |
|
| 410 |
RCP<const Teuchos::Comm<int> > comm = TestHelpers::Parameters::getDefaultComm(); |
| 411 |
if (comm->getSize() == 1) { |
| 412 |
// TEST_EQUALITY(residualNorms < 1e-10, true); |
| 413 |
TEST_EQUALITY(residualNorm1[0] != residualNorm2[0], true); |
| 414 |
} else { |
| 415 |
out << "Pass/Fail is only checked in serial." << std::endl; |
| 416 |
} |
| 417 |
} |
| 418 |
} // banded |
| 419 |
|
| 420 |
// Make sure Ifpack2's TriDi relaxation actually gets called |
| 421 |
TEUCHOS_UNIT_TEST_TEMPLATE_4_DECL(Ifpack2Smoother, TriDiRelaxation, Scalar, LocalOrdinal, GlobalOrdinal, Node) { |
| 422 |
#include <MueLu_UseShortNames.hpp> |
| 423 |
MUELU_TESTING_SET_OSTREAM; |
| 424 |
MUELU_TESTING_LIMIT_SCOPE(Scalar, GlobalOrdinal, Node); |
| 425 |
typedef typename Teuchos::ScalarTraits<SC>::magnitudeType magnitude_type; |
| 426 |
MUELU_TEST_ONLY_FOR(Xpetra::UseTpetra) { |
| 427 |
Teuchos::ParameterList paramList; |
| 428 |
Ifpack2Smoother smoother("TRIDI RELAXATION", paramList); |
| 429 |
|
| 430 |
// I don't use the testApply infrastructure because it has no provision for an initial guess. |
| 431 |
Teuchos::RCP<Matrix> A = TestHelpers::TestFactory<SC, LO, GO, NO>::Build1DPoisson(125); |
| 432 |
Level level; |
| 433 |
TestHelpers::TestFactory<SC, LO, GO, NO>::createSingleLevelHierarchy(level); |
| 434 |
level.Set("A", A); |
| 435 |
smoother.Setup(level); |
| 436 |
|
| 437 |
RCP<MultiVector> X = MultiVectorFactory::Build(A->getDomainMap(), 1); |
| 438 |
RCP<MultiVector> RHS = MultiVectorFactory::Build(A->getRangeMap(), 1); |
| 439 |
|
| 440 |
// Random X |
| 441 |
X->setSeed(846930886); |
| 442 |
X->randomize(); |
| 443 |
|
| 444 |
// Normalize X |
| 445 |
Array<magnitude_type> norms(1); |
| 446 |
X->norm2(norms); |
| 447 |
X->scale(1 / norms[0]); |
| 448 |
|
| 449 |
// Compute RHS corresponding to X |
| 450 |
A->apply(*X, *RHS, Teuchos::NO_TRANS, (SC)1.0, (SC)0.0); |
| 451 |
|
| 452 |
// Reset X to 0 |
| 453 |
X->putScalar((SC)0.0); |
| 454 |
|
| 455 |
RHS->norm2(norms); |
| 456 |
out << "||RHS|| = " << std::setiosflags(std::ios::fixed) << std::setprecision(10) << norms[0] << std::endl; |
| 457 |
|
| 458 |
out << "solve with zero initial guess" << std::endl; |
| 459 |
Teuchos::Array<magnitude_type> initialNorms(1); |
| 460 |
X->norm2(initialNorms); |
| 461 |
out << " ||X_initial|| = " << std::setiosflags(std::ios::fixed) << std::setprecision(10) << initialNorms[0] << std::endl; |
| 462 |
|
| 463 |
smoother.Apply(*X, *RHS, true); // zero initial guess |
| 464 |
|
| 465 |
Teuchos::Array<magnitude_type> finalNorms(1); |
| 466 |
X->norm2(finalNorms); |
| 467 |
Teuchos::Array<magnitude_type> residualNorm1 = Utilities::ResidualNorm(*A, *X, *RHS); |
| 468 |
out << " ||Residual_final|| = " << std::setiosflags(std::ios::fixed) << std::setprecision(20) << residualNorm1[0] << std::endl; |
| 469 |
out << " ||X_final|| = " << std::setiosflags(std::ios::fixed) << std::setprecision(10) << finalNorms[0] << std::endl; |
| 470 |
|
| 471 |
out << "solve with random initial guess" << std::endl; |
| 472 |
X->randomize(); |
| 473 |
X->norm2(initialNorms); |
| 474 |
out << " ||X_initial|| = " << std::setiosflags(std::ios::fixed) << std::setprecision(10) << initialNorms[0] << std::endl; |
| 475 |
|
| 476 |
smoother.Apply(*X, *RHS, false); // nonzero initial guess |
| 477 |
|
| 478 |
X->norm2(finalNorms); |
| 479 |
Teuchos::Array<magnitude_type> residualNorm2 = Utilities::ResidualNorm(*A, *X, *RHS); |
| 480 |
out << " ||Residual_final|| = " << std::setiosflags(std::ios::fixed) << std::setprecision(20) << residualNorm2[0] << std::endl; |
| 481 |
out << " ||X_final|| = " << std::setiosflags(std::ios::fixed) << std::setprecision(10) << finalNorms[0] << std::endl; |
| 482 |
|
| 483 |
RCP<const Teuchos::Comm<int> > comm = TestHelpers::Parameters::getDefaultComm(); |
| 484 |
if (comm->getSize() == 1) { |
| 485 |
// TEST_EQUALITY(residualNorms < 1e-10, true); |
| 486 |
TEST_EQUALITY(residualNorm1[0] != residualNorm2[0], true); |
| 487 |
} else { |
| 488 |
out << "Pass/Fail is only checked in serial." << std::endl; |
| 489 |
} |
| 490 |
} |
| 491 |
} // tridi |
| 492 |
|
| 493 |
TEUCHOS_UNIT_TEST_TEMPLATE_4_DECL(Ifpack2Smoother, BlockRelaxation_Autosize, Scalar, LocalOrdinal, GlobalOrdinal, Node) { |
| 494 |
#include <MueLu_UseShortNames.hpp> |
| 495 |
MUELU_TESTING_SET_OSTREAM; |
| 496 |
MUELU_TESTING_LIMIT_SCOPE(Scalar, GlobalOrdinal, Node); |
| 497 |
typedef typename Teuchos::ScalarTraits<SC>::magnitudeType magnitude_type; |
| 498 |
MUELU_TEST_ONLY_FOR(Xpetra::UseTpetra) { |
| 499 |
Teuchos::ParameterList matrixParams, ifpack2Params; |
| 500 |
|
| 501 |
matrixParams.set("matrixType", "Laplace1D"); |
| 502 |
matrixParams.set("nx", (GlobalOrdinal)20); // needs to be even |
| 503 |
Teuchos::RCP<Matrix> A = TestHelpers::TestFactory<SC, LO, GO, NO>::BuildMatrix(matrixParams, Xpetra::UseTpetra); |
| 504 |
A->SetFixedBlockSize(2); |
| 505 |
|
| 506 |
ifpack2Params.set("partitioner: type", "linear"); |
| 507 |
Ifpack2Smoother smoother("BLOCK RELAXATION", ifpack2Params); |
| 508 |
|
| 509 |
Level level; |
| 510 |
TestHelpers::TestFactory<SC, LO, GO, NO>::createSingleLevelHierarchy(level); |
| 511 |
level.Set("A", A); |
| 512 |
smoother.Setup(level); |
| 513 |
|
| 514 |
TEST_EQUALITY(1, 1); |
| 515 |
} |
| 516 |
} // banded |
| 517 |
|
| 518 |
TEUCHOS_UNIT_TEST_TEMPLATE_4_DECL(Ifpack2Smoother, BlockCrsMatrix_Relaxation_ViaPoint, Scalar, LocalOrdinal, GlobalOrdinal, Node) { |
| 519 |
#include <MueLu_UseShortNames.hpp> |
| 520 |
MUELU_TESTING_SET_OSTREAM; |
| 521 |
MUELU_TESTING_LIMIT_SCOPE(Scalar, GlobalOrdinal, Node); |
| 522 |
|
| 523 |
MUELU_TEST_ONLY_FOR(Xpetra::UseTpetra) { |
| 524 |
Teuchos::ParameterList matrixParams, ifpack2Params; |
| 525 |
|
| 526 |
matrixParams.set("matrixType", "Laplace1D"); |
| 527 |
matrixParams.set("nx", (GlobalOrdinal)20); // needs to be even |
| 528 |
|
| 529 |
RCP<Matrix> A = TestHelpers::TestFactory<SC, LO, GO, NO>::BuildBlockMatrixAsPoint(matrixParams, Xpetra::UseTpetra); |
| 530 |
ifpack2Params.set("smoother: use blockcrsmatrix storage", true); |
| 531 |
|
| 532 |
Ifpack2Smoother smoother("RELAXATION", ifpack2Params); |
| 533 |
|
| 534 |
Level level; |
| 535 |
TestHelpers::TestFactory<SC, LO, GO, NO>::createSingleLevelHierarchy(level); |
| 536 |
level.Set("A", A); |
| 537 |
smoother.Setup(level); |
| 538 |
|
| 539 |
TEST_EQUALITY(1, 1); |
| 540 |
} |
| 541 |
} |
| 542 |
|
| 543 |
TEUCHOS_UNIT_TEST_TEMPLATE_4_DECL(Ifpack2Smoother, BlockCrsMatrix_Relaxation_AsBlock, Scalar, LocalOrdinal, GlobalOrdinal, Node) { |
| 544 |
#include <MueLu_UseShortNames.hpp> |
| 545 |
MUELU_TESTING_SET_OSTREAM; |
| 546 |
MUELU_TESTING_LIMIT_SCOPE(Scalar, GlobalOrdinal, Node); |
| 547 |
|
| 548 |
MUELU_TEST_ONLY_FOR(Xpetra::UseTpetra) { |
| 549 |
Teuchos::ParameterList matrixParams, ifpack2Params; |
| 550 |
|
| 551 |
matrixParams.set("matrixType", "Laplace1D"); |
| 552 |
matrixParams.set("nx", (GlobalOrdinal)20); // needs to be even |
| 553 |
|
| 554 |
RCP<Matrix> A = TestHelpers::TpetraTestFactory<SC, LO, GO, NO>::BuildBlockMatrix(matrixParams, Xpetra::UseTpetra); |
| 555 |
ifpack2Params.set("smoother: use blockcrsmatrix storage", true); |
| 556 |
|
| 557 |
Ifpack2Smoother smoother("RELAXATION", ifpack2Params); |
| 558 |
|
| 559 |
Level level; |
| 560 |
TestHelpers::TestFactory<SC, LO, GO, NO>::createSingleLevelHierarchy(level); |
| 561 |
level.Set("A", A); |
| 562 |
smoother.Setup(level); |
| 563 |
|
| 564 |
TEST_EQUALITY(1, 1); |
| 565 |
} |
| 566 |
} |
| 567 |
|
| 568 |
#define MUELU_ETI_GROUP(SC, LO, GO, NO) \ |
| 569 |
TEUCHOS_UNIT_TEST_TEMPLATE_4_INSTANT(Ifpack2Smoother, NotSetup, SC, LO, GO, NO) \ |
| 570 |
TEUCHOS_UNIT_TEST_TEMPLATE_4_INSTANT(Ifpack2Smoother, HardCodedResult_Jacobi, SC, LO, GO, NO) \ |
| 571 |
TEUCHOS_UNIT_TEST_TEMPLATE_4_INSTANT(Ifpack2Smoother, HardCodedResult_GaussSeidel, SC, LO, GO, NO) \ |
| 572 |
TEUCHOS_UNIT_TEST_TEMPLATE_4_INSTANT(Ifpack2Smoother, HardCodedResult_GaussSeidel2, SC, LO, GO, NO) \ |
| 573 |
TEUCHOS_UNIT_TEST_TEMPLATE_4_INSTANT(Ifpack2Smoother, HardCodedResult_Chebyshev, SC, LO, GO, NO) \ |
| 574 |
TEUCHOS_UNIT_TEST_TEMPLATE_4_INSTANT(Ifpack2Smoother, HardCodedResult_ILU, SC, LO, GO, NO) \ |
| 575 |
TEUCHOS_UNIT_TEST_TEMPLATE_4_INSTANT(Ifpack2Smoother, ILU_TwoSweeps, SC, LO, GO, NO) \ |
| 576 |
TEUCHOS_UNIT_TEST_TEMPLATE_4_INSTANT(Ifpack2Smoother, BandedRelaxation, SC, LO, GO, NO) \ |
| 577 |
TEUCHOS_UNIT_TEST_TEMPLATE_4_INSTANT(Ifpack2Smoother, TriDiRelaxation, SC, LO, GO, NO) \ |
| 578 |
TEUCHOS_UNIT_TEST_TEMPLATE_4_INSTANT(Ifpack2Smoother, BlockRelaxation_Autosize, SC, LO, GO, NO) \ |
| 579 |
TEUCHOS_UNIT_TEST_TEMPLATE_4_INSTANT(Ifpack2Smoother, BlockCrsMatrix_Relaxation_ViaPoint, SC, LO, GO, NO) \ |
| 580 |
TEUCHOS_UNIT_TEST_TEMPLATE_4_INSTANT(Ifpack2Smoother, BlockCrsMatrix_Relaxation_AsBlock, SC, LO, GO, NO) |
| 581 |
|
| 582 |
#include <MueLu_ETI_4arg.hpp> |
| 583 |
|
| 584 |
} // namespace MueLuTests |