| 1 |
// @HEADER |
| 2 |
// ***************************************************************************** |
| 3 |
// Belos: Block Linear Solvers Package |
| 4 |
// |
| 5 |
// Copyright 2004-2016 NTESS and the Belos contributors. |
| 6 |
// SPDX-License-Identifier: BSD-3-Clause |
| 7 |
// ***************************************************************************** |
| 8 |
// @HEADER |
| 9 |
|
| 10 |
#include "BelosSolverFactory.hpp" |
| 11 |
#include "BelosTpetraAdapter.hpp" |
| 12 |
#include "MatrixMarket_Tpetra.hpp" |
| 13 |
#include "Tpetra_Core.hpp" |
| 14 |
#include "Tpetra_MultiVector.hpp" |
| 15 |
#include "Teuchos_UnitTestHarness.hpp" |
| 16 |
|
| 17 |
namespace { // (anonymous) |
| 18 |
|
| 19 |
// 18x18 symmetric positive definite matrix from the University of |
| 20 |
// Florida sparse matrix collection: Oberwolfach/LF10 (the main (K) |
| 21 |
// matrix). |
| 22 |
const char symPosDefMatrixString[] = |
| 23 |
"%%MatrixMarket matrix coordinate real symmetric\n" |
| 24 |
"%-------------------------------------------------------------------------------\n" |
| 25 |
"% UF Sparse Matrix Collection, Tim Davis\n" |
| 26 |
"% http://www.cise.ufl.edu/research/sparse/matrices/Oberwolfach/LF10\n" |
| 27 |
"% name: Oberwolfach/LF10\n" |
| 28 |
"% [Oberwolfach: linear 1D beam]\n" |
| 29 |
"% id: 1438\n" |
| 30 |
"% date: 2004\n" |
| 31 |
"% author: J. Lienemann, A. Greiner, J. Korvink\n" |
| 32 |
"% ed: E. Rudnyi\n" |
| 33 |
"% fields: name title A id notes aux date author ed kind\n" |
| 34 |
"% aux: M E B C\n" |
| 35 |
"% kind: model reduction problem\n" |
| 36 |
"%-------------------------------------------------------------------------------\n" |
| 37 |
"% notes:\n" |
| 38 |
"% Primary matrix in this model reduction problem is the Oberwolfach K matrix\n" |
| 39 |
"%-------------------------------------------------------------------------------\n" |
| 40 |
"18 18 50\n" |
| 41 |
"1 1 3.5344800000000003\n" |
| 42 |
"2 1 -477.1548\n" |
| 43 |
"3 1 1.7672400000000001\n" |
| 44 |
"2 2 171775.728\n" |
| 45 |
"4 2 -85887.864\n" |
| 46 |
"5 2 477.1548\n" |
| 47 |
"3 3 7.068960000000001\n" |
| 48 |
"4 3 -477.1548\n" |
| 49 |
"5 3 1.7672400000000001\n" |
| 50 |
"4 4 171775.728\n" |
| 51 |
"6 4 -85887.864\n" |
| 52 |
"7 4 477.1548\n" |
| 53 |
"5 5 7.068960000000001\n" |
| 54 |
"6 5 -477.1548\n" |
| 55 |
"7 5 1.7672400000000001\n" |
| 56 |
"6 6 171775.728\n" |
| 57 |
"8 6 -85887.864\n" |
| 58 |
"9 6 477.1548\n" |
| 59 |
"7 7 7.068960000000001\n" |
| 60 |
"8 7 -477.1548\n" |
| 61 |
"9 7 1.7672400000000001\n" |
| 62 |
"8 8 171775.728\n" |
| 63 |
"10 8 -85887.864\n" |
| 64 |
"11 8 477.1548\n" |
| 65 |
"9 9 7.068960000000001\n" |
| 66 |
"10 9 -477.1548\n" |
| 67 |
"11 9 1.7672400000000001\n" |
| 68 |
"10 10 171775.728\n" |
| 69 |
"12 10 -85887.864\n" |
| 70 |
"13 10 477.1548\n" |
| 71 |
"11 11 7.068960000000001\n" |
| 72 |
"12 11 -477.1548\n" |
| 73 |
"13 11 1.7672400000000001\n" |
| 74 |
"12 12 171775.728\n" |
| 75 |
"14 12 -85887.864\n" |
| 76 |
"15 12 477.1548\n" |
| 77 |
"13 13 7.068960000000001\n" |
| 78 |
"14 13 -477.1548\n" |
| 79 |
"15 13 1.7672400000000001\n" |
| 80 |
"14 14 171775.728\n" |
| 81 |
"16 14 -85887.864\n" |
| 82 |
"17 14 477.1548\n" |
| 83 |
"15 15 7.068960000000001\n" |
| 84 |
"16 15 -477.1548\n" |
| 85 |
"17 15 1.7672400000000001\n" |
| 86 |
"16 16 171775.728\n" |
| 87 |
"18 16 477.1548\n" |
| 88 |
"17 17 7.068960000000001\n" |
| 89 |
"18 17 1.7672400000000001\n" |
| 90 |
"18 18 3.5344800000000003\n"; |
| 91 |
|
| 92 |
#if 0 |
| 93 |
// Test matrix: nonsymmetric real. It's small enough to store as a |
| 94 |
// string, so we don't have to worry about adding a file to the |
| 95 |
// repository. |
| 96 |
const char matrixString[] = |
| 97 |
"%%MatrixMarket matrix coordinate real general\n" |
| 98 |
"%-------------------------------------------------------------------------------\n" |
| 99 |
"% UF Sparse Matrix Collection, Tim Davis\n" |
| 100 |
" % http://www.cise.ufl.edu/research/sparse/matrices/vanHeukelum/cage4\n" |
| 101 |
" % name: vanHeukelum/cage4\n" |
| 102 |
" % [DNA electrophoresis, 4 monomers in polymer. A. van Heukelum, Utrecht U.]\n" |
| 103 |
" % id: 905\n" |
| 104 |
" % date: 2003\n" |
| 105 |
" % author: A. van Heukelum\n" |
| 106 |
" % ed: T. Davis\n" |
| 107 |
" % fields: title A name id date author ed kind\n" |
| 108 |
" % kind: directed weighted graph\n" |
| 109 |
"%-------------------------------------------------------------------------------\n" |
| 110 |
"9 9 49\n" |
| 111 |
"1 1 .75\n" |
| 112 |
"2 1 .075027667114587\n" |
| 113 |
"4 1 .0916389995520797\n" |
| 114 |
"5 1 .0375138335572935\n" |
| 115 |
"8 1 .0458194997760398\n" |
| 116 |
"1 2 .137458499328119\n" |
| 117 |
"2 2 .687569167786467\n" |
| 118 |
"3 2 .0916389995520797\n" |
| 119 |
"5 2 .0375138335572935\n" |
| 120 |
"6 2 .0458194997760398\n" |
| 121 |
"2 3 .112541500671881\n" |
| 122 |
"3 3 .666666666666667\n" |
| 123 |
"4 3 .13745849932812\n" |
| 124 |
"6 3 .0458194997760398\n" |
| 125 |
"7 3 .0375138335572935\n" |
| 126 |
"1 4 .112541500671881\n" |
| 127 |
"3 4 .075027667114587\n" |
| 128 |
"4 4 .729097498880199\n" |
| 129 |
"7 4 .0375138335572935\n" |
| 130 |
"8 4 .0458194997760398\n" |
| 131 |
"1 5 .137458499328119\n" |
| 132 |
"2 5 .075027667114587\n" |
| 133 |
"5 5 .537513833557293\n" |
| 134 |
"6 5 .075027667114587\n" |
| 135 |
"7 5 .0916389995520797\n" |
| 136 |
"9 5 .0833333333333333\n" |
| 137 |
"2 6 .112541500671881\n" |
| 138 |
"3 6 .0916389995520797\n" |
| 139 |
"5 6 .13745849932812\n" |
| 140 |
"6 6 .445874834005214\n" |
| 141 |
"8 6 .13745849932812\n" |
| 142 |
"9 6 .075027667114587\n" |
| 143 |
"3 7 .075027667114587\n" |
| 144 |
"4 7 .13745849932812\n" |
| 145 |
"5 7 .112541500671881\n" |
| 146 |
"7 7 .470791832661453\n" |
| 147 |
"8 7 .112541500671881\n" |
| 148 |
"9 7 .0916389995520797\n" |
| 149 |
"1 8 .112541500671881\n" |
| 150 |
"4 8 .0916389995520797\n" |
| 151 |
"6 8 .075027667114587\n" |
| 152 |
"7 8 .0916389995520797\n" |
| 153 |
"8 8 .54581949977604\n" |
| 154 |
"9 8 .0833333333333333\n" |
| 155 |
"5 9 .25\n" |
| 156 |
"6 9 .150055334229174\n" |
| 157 |
"7 9 .183277999104159\n" |
| 158 |
"8 9 .25\n" |
| 159 |
"9 9 .166666666666667\n"; |
| 160 |
#endif // 0 |
| 161 |
|
| 162 |
using Teuchos::Comm; |
| 163 |
using Teuchos::ParameterList; |
| 164 |
using Teuchos::RCP; |
| 165 |
using Teuchos::rcp; |
| 166 |
using std::endl; |
| 167 |
typedef Tpetra::MultiVector<> MV; |
| 168 |
typedef MV::scalar_type ST; |
| 169 |
typedef MV::mag_type MT; |
| 170 |
typedef Teuchos::ScalarTraits<ST> STS; |
| 171 |
typedef Teuchos::ScalarTraits<MT> STM; |
| 172 |
typedef Tpetra::Operator<> OP; |
| 173 |
typedef Tpetra::CrsMatrix<> crs_matrix_type; |
| 174 |
|
| 175 |
// Report both computed-solution and exact-solution residuals. |
| 176 |
void |
| 177 |
testResiduals (bool& success, |
| 178 |
Teuchos::FancyOStream& out, |
| 179 |
const Tpetra::Operator<>& A, |
| 180 |
const Tpetra::MultiVector<>& X, |
| 181 |
const Tpetra::MultiVector<>& B, |
| 182 |
const Tpetra::MultiVector<>& X_exact, |
| 183 |
const MT& tol, |
| 184 |
const Belos::ReturnType& ret) |
| 185 |
{ |
| 186 |
const size_t numRhs = X.getNumVectors (); |
| 187 |
|
| 188 |
// R := B - A*X |
| 189 |
auto R = rcp (new MV (B, Teuchos::Copy)); |
| 190 |
A.apply (X, *R, Teuchos::NO_TRANS, -STS::one (), STS::one ()); |
| 191 |
Teuchos::Array<MT> norms (numRhs); |
| 192 |
R->norm2 (norms ()); |
| 193 |
|
| 194 |
// R_exact := B - A*X_exact |
| 195 |
auto R_exact = rcp (new MV (B, Teuchos::Copy)); |
| 196 |
A.apply (X_exact, *R_exact, Teuchos::NO_TRANS, -STS::one (), STS::one ()); |
| 197 |
Teuchos::Array<MT> normsExact (numRhs); |
| 198 |
R_exact->norm2 (normsExact ()); |
| 199 |
|
| 200 |
Teuchos::Array<MT> B_norms (numRhs); |
| 201 |
B.norm2 (B_norms ()); |
| 202 |
|
| 203 |
bool badRes = false; |
| 204 |
out << "Results:" << endl; |
| 205 |
{ |
| 206 |
Teuchos::OSTab tab1 (out); |
| 207 |
|
| 208 |
out << "Exact solution relative residuals: ["; |
| 209 |
for (size_t j = 0; j < numRhs; ++j) { |
| 210 |
const MT exactRelRes = normsExact[j] / B_norms[j]; |
| 211 |
out << exactRelRes; |
| 212 |
if (j + 1 < numRhs) { |
| 213 |
out << ", "; |
| 214 |
} |
| 215 |
} |
| 216 |
out << "]" << endl; |
| 217 |
|
| 218 |
out << "Computed solution relative residuals: ["; |
| 219 |
for (size_t j = 0; j < numRhs; ++j) { |
| 220 |
const MT computedRelRes = norms[j] / B_norms[j]; |
| 221 |
out << computedRelRes; |
| 222 |
if (j + 1 < numRhs) { |
| 223 |
out << ", "; |
| 224 |
} |
| 225 |
if (computedRelRes > tol) { |
| 226 |
badRes = true; |
| 227 |
} |
| 228 |
} |
| 229 |
out << "]" << endl; |
| 230 |
} |
| 231 |
|
| 232 |
if (ret != Belos::Converged || badRes) { |
| 233 |
success = false; |
| 234 |
out << "Belos did NOT converge!" << endl; |
| 235 |
} else { |
| 236 |
out << "Belos converged!" << endl; |
| 237 |
} |
| 238 |
} |
| 239 |
|
| 240 |
|
| 241 |
TEUCHOS_UNIT_TEST( MultipleSolves, GMRES ) |
| 242 |
{ |
| 243 |
Teuchos::OSTab tab0 (out); |
| 244 |
out << "Test whether Belos correctly overwrites the initial guess with the " |
| 245 |
<< "computed solution, or incorrectly does a +=, as some users have " |
| 246 |
<< "reported" << endl; |
| 247 |
Teuchos::OSTab tab1 (out); |
| 248 |
|
| 249 |
// Get the default communicator for tests. |
| 250 |
RCP<const Comm<int> > comm = Tpetra::getDefaultComm (); |
| 251 |
|
| 252 |
typedef Tpetra::MatrixMarket::Reader<crs_matrix_type> reader_type; |
| 253 |
std::istringstream matrixFile (symPosDefMatrixString); |
| 254 |
RCP<crs_matrix_type> A = reader_type::readSparse (matrixFile, comm); |
| 255 |
|
| 256 |
size_t numRhs = 1; // number of right-hand sides in the linear system |
| 257 |
int freq = 1; // frequency of status test output |
| 258 |
|
| 259 |
// Maximum number of iterations per solve. We use twice the problem |
| 260 |
// size, since the problem to solve is pretty hard for being 18x18. |
| 261 |
const int maxIters = |
| 262 |
2 * static_cast<int> (A->getRangeMap ()->getGlobalNumElements ()); |
| 263 |
// Relative residual tolerance |
| 264 |
const MT tol = static_cast<MT> (10) * STM::squareroot (STM::eps ()); |
| 265 |
|
| 266 |
const std::string solverName ("GMRES"); |
| 267 |
RCP<ParameterList> belosList (new ParameterList ("Belos")); |
| 268 |
belosList->set ("Maximum Iterations", maxIters); |
| 269 |
belosList->set ("Convergence Tolerance", tol); |
| 270 |
belosList->set ("Verbosity", Belos::Errors + Belos::Warnings + |
| 271 |
/* Belos::StatusTestDetails + */ Belos::FinalSummary); |
| 272 |
belosList->set ("Output Frequency", freq); |
| 273 |
|
| 274 |
auto X = rcp (new MV (A->getDomainMap (), numRhs)); |
| 275 |
auto X_exact = rcp (new MV (A->getDomainMap (), numRhs)); |
| 276 |
auto B = rcp (new MV (A->getRangeMap (), numRhs)); |
| 277 |
|
| 278 |
// Fill initial guess with zeros. |
| 279 |
X->putScalar (STS::one ()); |
| 280 |
|
| 281 |
// |
| 282 |
// First solve: Exact solution is all ones. |
| 283 |
// |
| 284 |
X_exact->putScalar (STS::one ()); |
| 285 |
A->apply (*X_exact, *B); |
| 286 |
|
| 287 |
typedef Belos::LinearProblem<ST, MV, OP> problem_type; |
| 288 |
auto problem = rcp (new problem_type (A, X, B)); |
| 289 |
bool set = problem->setProblem (); |
| 290 |
TEUCHOS_TEST_FOR_EXCEPTION( ! set, std::logic_error, "Failed to set Belos::LinearProblem!"); |
| 291 |
|
| 292 |
// Create a Belos solver. |
| 293 |
RCP<Belos::SolverManager<ST, MV, OP> > solver; |
| 294 |
{ |
| 295 |
Belos::SolverFactory<ST, MV, OP> factory; |
| 296 |
solver = factory.create (solverName, belosList); |
| 297 |
solver->setProblem (problem); |
| 298 |
} |
| 299 |
|
| 300 |
// Ask Belos to solve the linear system. |
| 301 |
Belos::ReturnType ret = solver->solve (); |
| 302 |
|
| 303 |
// Evaluate the result. |
| 304 |
testResiduals (success, out, *A, *X, *B, *X_exact, tol, ret); |
| 305 |
|
| 306 |
// |
| 307 |
// Second solve: Exact solution is all 1.5. We pick this because |
| 308 |
// it's not too far off from the previous exact solution -- e.g., |
| 309 |
// not the opposite sign -- so it shouldn't take too long to |
| 310 |
// converge. |
| 311 |
// |
| 312 |
const ST one = STS::one (); |
| 313 |
const ST two = one + one; |
| 314 |
X_exact->putScalar (one + one / two); |
| 315 |
A->apply (*X_exact, *B); |
| 316 |
|
| 317 |
// Don't change the initial guess this time. Keep the solution from |
| 318 |
// last time. The point is to make sure that the LinearProblem |
| 319 |
// doesn't do something weird to it. |
| 320 |
|
| 321 |
set = problem->setProblem (); |
| 322 |
TEUCHOS_TEST_FOR_EXCEPTION( ! set, std::logic_error, "Failed to set Belos::LinearProblem!"); |
| 323 |
|
| 324 |
// Ask Belos to solve the linear system. |
| 325 |
ret = solver->solve (); |
| 326 |
|
| 327 |
// Evaluate the result. |
| 328 |
testResiduals (success, out, *A, *X, *B, *X_exact, tol, ret); |
| 329 |
} |
| 330 |
|
| 331 |
|
| 332 |
TEUCHOS_UNIT_TEST( MultipleSolves, CG ) |
| 333 |
{ |
| 334 |
Teuchos::OSTab tab0 (out); |
| 335 |
out << "Test whether Belos correctly overwrites the initial guess with the " |
| 336 |
<< "computed solution, or incorrectly does a +=, as some users have " |
| 337 |
<< "reported" << endl; |
| 338 |
Teuchos::OSTab tab1 (out); |
| 339 |
|
| 340 |
// Get the default communicator for tests. |
| 341 |
RCP<const Comm<int> > comm = Tpetra::getDefaultComm (); |
| 342 |
|
| 343 |
typedef Tpetra::MatrixMarket::Reader<crs_matrix_type> reader_type; |
| 344 |
std::istringstream matrixFile (symPosDefMatrixString); |
| 345 |
RCP<crs_matrix_type> A = reader_type::readSparse (matrixFile, comm); |
| 346 |
|
| 347 |
size_t numRhs = 1; // number of right-hand sides in the linear system |
| 348 |
int freq = 1; // frequency of status test output |
| 349 |
|
| 350 |
// Maximum number of iterations per solve. We use twice the problem |
| 351 |
// size, since the problem to solve is pretty hard for being 18x18. |
| 352 |
const int maxIters = |
| 353 |
2 * static_cast<int> (A->getRangeMap ()->getGlobalNumElements ()); |
| 354 |
// Relative residual tolerance |
| 355 |
const MT tol = static_cast<MT> (10) * STM::squareroot (STM::eps ()); |
| 356 |
|
| 357 |
const std::string solverName ("CG"); |
| 358 |
RCP<ParameterList> belosList (new ParameterList ("Belos")); |
| 359 |
belosList->set ("Maximum Iterations", maxIters); |
| 360 |
belosList->set ("Convergence Tolerance", tol); |
| 361 |
belosList->set ("Verbosity", Belos::Errors + Belos::Warnings + |
| 362 |
/* Belos::StatusTestDetails + */ Belos::FinalSummary); |
| 363 |
belosList->set ("Output Frequency", freq); |
| 364 |
|
| 365 |
auto X = rcp (new MV (A->getDomainMap (), numRhs)); |
| 366 |
auto X_exact = rcp (new MV (A->getDomainMap (), numRhs)); |
| 367 |
auto B = rcp (new MV (A->getRangeMap (), numRhs)); |
| 368 |
|
| 369 |
// Fill initial guess with zeros. |
| 370 |
X->putScalar (STS::one ()); |
| 371 |
|
| 372 |
// |
| 373 |
// First solve: Exact solution is all ones. |
| 374 |
// |
| 375 |
X_exact->putScalar (STS::one ()); |
| 376 |
A->apply (*X_exact, *B); |
| 377 |
|
| 378 |
typedef Belos::LinearProblem<ST, MV, OP> problem_type; |
| 379 |
auto problem = rcp (new problem_type (A, X, B)); |
| 380 |
bool set = problem->setProblem (); |
| 381 |
TEUCHOS_TEST_FOR_EXCEPTION( ! set, std::logic_error, "Failed to set Belos::LinearProblem!"); |
| 382 |
|
| 383 |
// Create a Belos solver. |
| 384 |
RCP<Belos::SolverManager<ST, MV, OP> > solver; |
| 385 |
{ |
| 386 |
Belos::SolverFactory<ST, MV, OP> factory; |
| 387 |
solver = factory.create (solverName, belosList); |
| 388 |
solver->setProblem (problem); |
| 389 |
} |
| 390 |
|
| 391 |
// Ask Belos to solve the linear system. |
| 392 |
Belos::ReturnType ret = solver->solve (); |
| 393 |
|
| 394 |
// Evaluate the result. |
| 395 |
testResiduals (success, out, *A, *X, *B, *X_exact, tol, ret); |
| 396 |
|
| 397 |
// |
| 398 |
// Second solve: Exact solution is all 1.5. We pick this because |
| 399 |
// it's not too far off from the previous exact solution -- e.g., |
| 400 |
// not the opposite sign -- so it shouldn't take too long to |
| 401 |
// converge. |
| 402 |
// |
| 403 |
const ST one = STS::one (); |
| 404 |
const ST two = one + one; |
| 405 |
X_exact->putScalar (one + one / two); |
| 406 |
A->apply (*X_exact, *B); |
| 407 |
|
| 408 |
// Don't change the initial guess this time. Keep the solution from |
| 409 |
// last time. The point is to make sure that the LinearProblem |
| 410 |
// doesn't do something weird to it. |
| 411 |
|
| 412 |
set = problem->setProblem (); |
| 413 |
TEUCHOS_TEST_FOR_EXCEPTION( ! set, std::logic_error, "Failed to set Belos::LinearProblem!"); |
| 414 |
|
| 415 |
// Ask Belos to solve the linear system. |
| 416 |
ret = solver->solve (); |
| 417 |
|
| 418 |
// Evaluate the result. |
| 419 |
testResiduals (success, out, *A, *X, *B, *X_exact, tol, ret); |
| 420 |
} |
| 421 |
|
| 422 |
} // namespace (anonymous) |