From: Jean-Paul Pelteret Date: Sun, 29 Jan 2017 09:35:48 +0000 (+0100) Subject: Add tests checking result of SolverControl classes with Trilinos solver X-Git-Tag: v8.5.0-rc1~184^2~2 X-Git-Url: https://gitweb.dealii.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=23a4c829bf7ff71d873ee793ccacbeff0c79ad76;p=dealii.git Add tests checking result of SolverControl classes with Trilinos solver --- diff --git a/tests/trilinos/solver_control_01.cc b/tests/trilinos/solver_control_01.cc new file mode 100644 index 0000000000..7807d7c448 --- /dev/null +++ b/tests/trilinos/solver_control_01.cc @@ -0,0 +1,116 @@ +// --------------------------------------------------------------------- +// +// Copyright (C) 2004 - 2015 by the deal.II authors +// +// This file is part of the deal.II library. +// +// The deal.II library is free software; you can use it, redistribute +// it, and/or modify it under the terms of the GNU Lesser General +// Public License as published by the Free Software Foundation; either +// version 2.1 of the License, or (at your option) any later version. +// The full text of the license can be found in the file LICENSE at +// the top level of the deal.II distribution. +// +// --------------------------------------------------------------------- + + +// test SolverControl with Trilinos solver +// This test is adapted from tests/trilinos/solver_03.cc + + +#include "../tests.h" +#include +#include "../testmatrix.h" +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +template +void +check_solve (SolverControl &solver_control, + const MatrixType &A, + VectorType &u, + VectorType &f, + const PRECONDITION &P, + const bool expected_result) +{ + TrilinosWrappers::SolverCG solver(solver_control); + + u = 0.; + f = 1.; + bool success = false; + try + { + solver.solve(A,u,f,P); + deallog << "Success. "; + success = true; + } + catch (std::exception &e) + { + deallog << "Failure. "; + } + + deallog << "Solver stopped after " << solver_control.last_step() + << " iterations" << std::endl; + Assert(success == expected_result, ExcMessage("Incorrect result.")); +} + + +int main(int argc, char **argv) +{ + std::ofstream logfile("output"); + logfile.precision(4); + deallog.attach(logfile); + deallog.threshold_double(1.e-10); + + Utilities::MPI::MPI_InitFinalize mpi_initialization (argc, argv, testing_max_num_threads()); + + + { + const unsigned int size = 32; + unsigned int dim = (size-1)*(size-1); + + deallog << "Size " << size << " Unknowns " << dim << std::endl; + + // Make matrix + FDMatrix testproblem(size, size); + DynamicSparsityPattern csp (dim, dim); + testproblem.five_point_structure(csp); + TrilinosWrappers::SparseMatrix A; + A.reinit(csp); + testproblem.five_point(A); + + TrilinosWrappers::Vector f(dim); + TrilinosWrappers::Vector u(dim); + f = 1.; + A.compress (VectorOperation::insert); + f.compress (VectorOperation::insert); + u.compress (VectorOperation::insert); + + TrilinosWrappers::PreconditionJacobi preconditioner; + preconditioner.initialize(A); + + deallog.push("Abs tol"); + { + // Expects success + SolverControl solver_control(2000, 1.e-3); + check_solve (solver_control, A,u,f, preconditioner, true); + } + deallog.pop(); + deallog.push("Iterations"); + { + // Expects failure + SolverControl solver_control(20, 1.e-3); + check_solve (solver_control, A,u,f, preconditioner, false); + } + deallog.pop(); + } +} diff --git a/tests/trilinos/solver_control_01.output b/tests/trilinos/solver_control_01.output new file mode 100644 index 0000000000..6b2f4fd7ab --- /dev/null +++ b/tests/trilinos/solver_control_01.output @@ -0,0 +1,6 @@ + +DEAL::Size 32 Unknowns 961 +DEAL:Abs tol::Convergence step 43 value 0.000818932 +DEAL:Abs tol::Success. Solver stopped after 43 iterations +DEAL:Iterations::Failure step 20 value 6.00198 +DEAL:Iterations::Failure. Solver stopped after 20 iterations diff --git a/tests/trilinos/solver_control_02.cc b/tests/trilinos/solver_control_02.cc new file mode 100644 index 0000000000..fd64a667bb --- /dev/null +++ b/tests/trilinos/solver_control_02.cc @@ -0,0 +1,123 @@ +// --------------------------------------------------------------------- +// +// Copyright (C) 2004 - 2015 by the deal.II authors +// +// This file is part of the deal.II library. +// +// The deal.II library is free software; you can use it, redistribute +// it, and/or modify it under the terms of the GNU Lesser General +// Public License as published by the Free Software Foundation; either +// version 2.1 of the License, or (at your option) any later version. +// The full text of the license can be found in the file LICENSE at +// the top level of the deal.II distribution. +// +// --------------------------------------------------------------------- + + +// test ReductionControl with Trilinos solver +// This test is adapted from tests/trilinos/solver_03.cc + + +#include "../tests.h" +#include +#include "../testmatrix.h" +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +template +void +check_solve (SolverControl &solver_control, + const MatrixType &A, + VectorType &u, + VectorType &f, + const PRECONDITION &P, + const bool expected_result) +{ + TrilinosWrappers::SolverCG solver(solver_control); + + u = 0.; + f = 1.; + bool success = false; + try + { + solver.solve(A,u,f,P); + deallog << "Success. "; + success = true; + } + catch (std::exception &e) + { + deallog << "Failure. "; + } + + deallog << "Solver stopped after " << solver_control.last_step() + << " iterations" << std::endl; + Assert(success == expected_result, ExcMessage("Incorrect result.")); +} + + +int main(int argc, char **argv) +{ + std::ofstream logfile("output"); + logfile.precision(4); + deallog.attach(logfile); + deallog.threshold_double(1.e-10); + + Utilities::MPI::MPI_InitFinalize mpi_initialization (argc, argv, testing_max_num_threads()); + + + { + const unsigned int size = 32; + unsigned int dim = (size-1)*(size-1); + + deallog << "Size " << size << " Unknowns " << dim << std::endl; + + // Make matrix + FDMatrix testproblem(size, size); + DynamicSparsityPattern csp (dim, dim); + testproblem.five_point_structure(csp); + TrilinosWrappers::SparseMatrix A; + A.reinit(csp); + testproblem.five_point(A); + + TrilinosWrappers::Vector f(dim); + TrilinosWrappers::Vector u(dim); + f = 1.; + A.compress (VectorOperation::insert); + f.compress (VectorOperation::insert); + u.compress (VectorOperation::insert); + + TrilinosWrappers::PreconditionJacobi preconditioner; + preconditioner.initialize(A); + + deallog.push("Rel tol"); + { + // Expects success + ReductionControl solver_control(2000, 1.e-30, 1e-6); + check_solve (solver_control, A,u,f, preconditioner, true); + } + deallog.pop(); + deallog.push("Abs tol"); + { + // Expects success + ReductionControl solver_control(2000, 1.e-3, 1e-9); + check_solve (solver_control, A,u,f, preconditioner, true); + } + deallog.pop(); + deallog.push("Iterations"); + { + // Expects failure + ReductionControl solver_control(20, 1.e-30, 1e-6); + check_solve (solver_control, A,u,f, preconditioner, false); + } + deallog.pop(); + } +} diff --git a/tests/trilinos/solver_control_02.output b/tests/trilinos/solver_control_02.output new file mode 100644 index 0000000000..8043d06426 --- /dev/null +++ b/tests/trilinos/solver_control_02.output @@ -0,0 +1,11 @@ + +DEAL::Size 32 Unknowns 961 +DEAL:Rel tol::Starting value 31.0000 +DEAL:Rel tol::Convergence step 50 value 2.11364e-05 +DEAL:Rel tol::Success. Solver stopped after 50 iterations +DEAL:Abs tol::Starting value 31.0000 +DEAL:Abs tol::Convergence step 43 value 0.000818932 +DEAL:Abs tol::Success. Solver stopped after 43 iterations +DEAL:Iterations::Starting value 31.0000 +DEAL:Iterations::Failure step 20 value 6.00198 +DEAL:Iterations::Failure. Solver stopped after 20 iterations diff --git a/tests/trilinos/solver_control_03.cc b/tests/trilinos/solver_control_03.cc new file mode 100644 index 0000000000..b7bff326dd --- /dev/null +++ b/tests/trilinos/solver_control_03.cc @@ -0,0 +1,116 @@ +// --------------------------------------------------------------------- +// +// Copyright (C) 2004 - 2015 by the deal.II authors +// +// This file is part of the deal.II library. +// +// The deal.II library is free software; you can use it, redistribute +// it, and/or modify it under the terms of the GNU Lesser General +// Public License as published by the Free Software Foundation; either +// version 2.1 of the License, or (at your option) any later version. +// The full text of the license can be found in the file LICENSE at +// the top level of the deal.II distribution. +// +// --------------------------------------------------------------------- + + +// test IterationNumberControl with Trilinos solver +// This test is adapted from tests/trilinos/solver_03.cc + + +#include "../tests.h" +#include +#include "../testmatrix.h" +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +template +void +check_solve (SolverControl &solver_control, + const MatrixType &A, + VectorType &u, + VectorType &f, + const PRECONDITION &P, + const bool expected_result) +{ + TrilinosWrappers::SolverCG solver(solver_control); + + u = 0.; + f = 1.; + bool success = false; + try + { + solver.solve(A,u,f,P); + deallog << "Success. "; + success = true; + } + catch (std::exception &e) + { + deallog << "Failure. "; + } + + deallog << "Solver stopped after " << solver_control.last_step() + << " iterations" << std::endl; + Assert(success == expected_result, ExcMessage("Incorrect result.")); +} + + +int main(int argc, char **argv) +{ + std::ofstream logfile("output"); + logfile.precision(4); + deallog.attach(logfile); + deallog.threshold_double(1.e-10); + + Utilities::MPI::MPI_InitFinalize mpi_initialization (argc, argv, testing_max_num_threads()); + + + { + const unsigned int size = 32; + unsigned int dim = (size-1)*(size-1); + + deallog << "Size " << size << " Unknowns " << dim << std::endl; + + // Make matrix + FDMatrix testproblem(size, size); + DynamicSparsityPattern csp (dim, dim); + testproblem.five_point_structure(csp); + TrilinosWrappers::SparseMatrix A; + A.reinit(csp); + testproblem.five_point(A); + + TrilinosWrappers::Vector f(dim); + TrilinosWrappers::Vector u(dim); + f = 1.; + A.compress (VectorOperation::insert); + f.compress (VectorOperation::insert); + u.compress (VectorOperation::insert); + + TrilinosWrappers::PreconditionJacobi preconditioner; + preconditioner.initialize(A); + + deallog.push("Abs tol"); + { + // Expects success + IterationNumberControl solver_control(2000, 1.e-3); + check_solve (solver_control, A,u,f, preconditioner, true); + } + deallog.pop(); + deallog.push("Iterations"); + { + // Expects success + IterationNumberControl solver_control(20, 1.e-3); + check_solve (solver_control, A,u,f, preconditioner, true); + } + deallog.pop(); + } +} diff --git a/tests/trilinos/solver_control_03.output b/tests/trilinos/solver_control_03.output new file mode 100644 index 0000000000..9ac7a92628 --- /dev/null +++ b/tests/trilinos/solver_control_03.output @@ -0,0 +1,6 @@ + +DEAL::Size 32 Unknowns 961 +DEAL:Abs tol::Convergence step 43 value 0.000818932 +DEAL:Abs tol::Success. Solver stopped after 43 iterations +DEAL:Iterations::Convergence step 20 value 6.00198 +DEAL:Iterations::Success. Solver stopped after 20 iterations