| 1 |
// SPDX-FileCopyrightText: Copyright © DUNE Project contributors, see file LICENSE.md in module root |
| 2 |
// SPDX-License-Identifier: LicenseRef-GPL-2.0-only-with-DUNE-exception |
| 3 |
// -*- tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- |
| 4 |
// vi: set et ts=4 sw=2 sts=2: |
| 5 |
|
| 6 |
// If we test with simd we need to deactivate the direct solver. |
| 7 |
// In that case we use a define to do this in the next line. |
| 8 |
@DEFINE_DEACTIVATE_AMG_DIRECTSOLVER@ |
| 9 |
|
| 10 |
#include <dune/common/parallel/mpihelper.hh> |
| 11 |
#include <dune/common/parametertree.hh> |
| 12 |
#include <dune/common/parametertreeparser.hh> |
| 13 |
#include <dune/common/simd/loop.hh> |
| 14 |
|
| 15 |
#include <dune/istl/bvector.hh> |
| 16 |
#include <dune/istl/bcrsmatrix.hh> |
| 17 |
#include <dune/istl/operators.hh> |
| 18 |
#include <dune/istl/solverfactory.hh> |
| 19 |
#include <dune/istl/solvercategory.hh> |
| 20 |
#include <dune/istl/paamg/test/anisotropic.hh> |
| 21 |
|
| 22 |
// direct solvers |
| 23 |
#include <dune/istl/cholmod.hh> |
| 24 |
#include <dune/istl/ldl.hh> |
| 25 |
#include <dune/istl/spqr.hh> |
| 26 |
#include <dune/istl/superlu.hh> |
| 27 |
#include <dune/istl/umfpack.hh> |
| 28 |
|
| 29 |
// preconditioners |
| 30 |
#include <dune/istl/preconditioners.hh> |
| 31 |
#include <dune/istl/paamg/amg.hh> |
| 32 |
|
| 33 |
// solvers |
| 34 |
#include <dune/istl/solvers.hh> |
| 35 |
|
| 36 |
#include <dune/istl/test/laplacian.hh> |
| 37 |
#include <dune/istl/paamg/test/anisotropic.hh> |
| 38 |
|
| 39 |
using Vector = Dune::BlockVector<@BLOCK@>; |
| 40 |
|
| 41 |
template<class V> |
| 42 |
struct MatrixBlock{ |
| 43 |
typedef V type; |
| 44 |
}; |
| 45 |
template<class T, int n> |
| 46 |
struct MatrixBlock<Dune::FieldVector<T,n>>{ |
| 47 |
typedef Dune::FieldMatrix<Dune::Simd::Scalar<T>,n,n> type; |
| 48 |
}; |
| 49 |
using Matrix = Dune::BCRSMatrix<typename MatrixBlock<@BLOCK@>::type>; |
| 50 |
|
| 51 |
template<class Comm> |
| 52 |
void testSeq(const Dune::ParameterTree& config, Comm c){ |
| 53 |
if(c.rank() == 0){ |
| 54 |
std::set<std::string> deactivate_solvers; |
| 55 |
|
| 56 |
#if !HAVE_SUITESPARSE_CHOLMOD |
| 57 |
deactivate_solvers.insert("Cholmod"); |
| 58 |
#endif |
| 59 |
#if !HAVE_SUITESPARSE_LDL |
| 60 |
deactivate_solvers.insert("LDL"); |
| 61 |
#endif |
| 62 |
#if !HAVE_SUITESPARSE_UMFPACK |
| 63 |
deactivate_solvers.insert("UMFPack"); |
| 64 |
#endif |
| 65 |
#if !HAVE_SUITESPARSE_SPQR |
| 66 |
deactivate_solvers.insert("SPQR"); |
| 67 |
#endif |
| 68 |
#if !HAVE_SUPERLU |
| 69 |
deactivate_solvers.insert("SuperLU"); |
| 70 |
#endif |
| 71 |
|
| 72 |
Matrix mat; |
| 73 |
int N = config.get("N", 10); |
| 74 |
setupLaplacian(mat, N); |
| 75 |
Vector x(mat.M()), b(mat.N()); |
| 76 |
|
| 77 |
using Operator = Dune::MatrixAdapter<Matrix, Vector, Vector>; |
| 78 |
std::shared_ptr<Operator> op = std::make_shared<Operator>(mat); |
| 79 |
|
| 80 |
for(const std::string& test : config.getSubKeys()){ |
| 81 |
if(deactivate_solvers.find(test) != deactivate_solvers.end()) |
| 82 |
continue; |
| 83 |
|
| 84 |
Dune::ParameterTree solverConfig = config.sub(test); |
| 85 |
std::cout << " ============== " << test << " ============== " << std::endl; |
| 86 |
try{ |
| 87 |
std::shared_ptr<Dune::InverseOperator<Vector, Vector>> solver = getSolverFromFactory(op, solverConfig); |
| 88 |
x = 0; |
| 89 |
b = 1; |
| 90 |
Dune::InverseOperatorResult res; |
| 91 |
solver->apply(x,b,res); |
| 92 |
if(!res.converged) |
| 93 |
DUNE_THROW(Dune::Exception, test << " not converged!"); |
| 94 |
}catch(Dune::UnsupportedType& e){ |
| 95 |
std::cout << e.what() << std::endl; |
| 96 |
} |
| 97 |
} |
| 98 |
} |
| 99 |
} |
| 100 |
|
| 101 |
|
| 102 |
template<class Comm> |
| 103 |
void testOverlapping(const Dune::ParameterTree& config, Comm c){ |
| 104 |
using Communication = Dune::OwnerOverlapCopyCommunication<int>; |
| 105 |
using Operator = Dune::OverlappingSchwarzOperator<Matrix,Vector,Vector,Communication>; |
| 106 |
using MatrixBlock = typename Operator::matrix_type::block_type; |
| 107 |
int N = config.get("N", 100); |
| 108 |
int n; |
| 109 |
Communication comm(MPI_COMM_WORLD); |
| 110 |
Matrix mat =setupAnisotropic2d<MatrixBlock>(N, comm.indexSet(), comm.communicator(), &n, 1); |
| 111 |
Vector x(mat.M()), b(mat.N()); |
| 112 |
b=0; |
| 113 |
x=100; |
| 114 |
setBoundary(x, b, N, comm.indexSet()); |
| 115 |
comm.remoteIndices().template rebuild<false>(); |
| 116 |
|
| 117 |
std::shared_ptr<Operator> op = std::make_shared<Operator>(mat, comm); |
| 118 |
|
| 119 |
for(const std::string& test : config.getSubKeys()){ |
| 120 |
Dune::ParameterTree solverConfig = config.sub(test); |
| 121 |
std::cout << " ============== " << test << " ============== " << std::endl; |
| 122 |
try{ |
| 123 |
std::shared_ptr<Dune::InverseOperator<Vector, Vector>> solver = getSolverFromFactory(op, solverConfig); |
| 124 |
x = 0; |
| 125 |
b = 1; |
| 126 |
Dune::InverseOperatorResult res; |
| 127 |
solver->apply(x,b,res); |
| 128 |
if(!res.converged) |
| 129 |
DUNE_THROW(Dune::Exception, test << " not converged!"); |
| 130 |
}catch(Dune::UnsupportedType& e){ |
| 131 |
std::cout << e.what() << std::endl; |
| 132 |
} |
| 133 |
} |
| 134 |
} |
| 135 |
|
| 136 |
template<class Comm> |
| 137 |
void testNonoverlapping(const Dune::ParameterTree& config, Comm c){ |
| 138 |
using Communication = Dune::OwnerOverlapCopyCommunication<int>; |
| 139 |
using Operator = Dune::NonoverlappingSchwarzOperator<Matrix,Vector,Vector,Communication>; |
| 140 |
using MatrixBlock = typename Operator::matrix_type::block_type; |
| 141 |
int N = config.get("N", 100); |
| 142 |
int n; |
| 143 |
Communication comm(MPI_COMM_WORLD, Dune::SolverCategory::nonoverlapping); |
| 144 |
/// \todo Make discretization mimic onoverlapping. |
| 145 |
Matrix mat =setupAnisotropic2d<MatrixBlock>(N, comm.indexSet(), comm.communicator(), &n, 1); |
| 146 |
Vector x(mat.M()), b(mat.N()); |
| 147 |
b=0; |
| 148 |
x=100; |
| 149 |
setBoundary(x, b, N, comm.indexSet()); |
| 150 |
comm.remoteIndices().template rebuild<false>(); |
| 151 |
|
| 152 |
std::shared_ptr<Operator> op = std::make_shared<Operator>(mat, comm); |
| 153 |
|
| 154 |
for(const std::string& test : config.getSubKeys()){ |
| 155 |
Dune::ParameterTree solverConfig = config.sub(test); |
| 156 |
std::cout << " ============== " << test << " ============== " << std::endl; |
| 157 |
try{ |
| 158 |
std::shared_ptr<Dune::InverseOperator<Vector, Vector>> solver = getSolverFromFactory(op, solverConfig); |
| 159 |
x = 0; |
| 160 |
b = 1; |
| 161 |
Dune::InverseOperatorResult res; |
| 162 |
solver->apply(x,b,res); |
| 163 |
if(!res.converged) |
| 164 |
DUNE_THROW(Dune::Exception, test << " not converged!"); |
| 165 |
}catch(Dune::UnsupportedType& e){ |
| 166 |
std::cout << e.what() << std::endl; |
| 167 |
} |
| 168 |
} |
| 169 |
} |
| 170 |
|
| 171 |
int main(int argc, char** argv){ |
| 172 |
auto& mpihelper = Dune::MPIHelper::instance(argc, argv); |
| 173 |
Dune::ParameterTree config; |
| 174 |
Dune::ParameterTreeParser::readINITree("solverfactorytest.ini", config); |
| 175 |
Dune::ParameterTreeParser::readOptions(argc, argv, config); |
| 176 |
|
| 177 |
Dune::initSolverFactories<Dune::MatrixAdapter<Matrix,Vector,Vector>>(); |
| 178 |
using Communication = Dune::OwnerOverlapCopyCommunication<int>; |
| 179 |
Dune::initSolverFactories<Dune::OverlappingSchwarzOperator<Matrix,Vector,Vector,Communication>>(); |
| 180 |
Dune::initSolverFactories<Dune::NonoverlappingSchwarzOperator<Matrix,Vector,Vector,Communication>>(); |
| 181 |
|
| 182 |
std::cout << std::endl << " Testing sequential tests... " << std::endl; |
| 183 |
testSeq(config.sub("sequential"), mpihelper.getCommunication()); |
| 184 |
std::cout << std::endl << " Testing overlapping tests... " << std::endl; |
| 185 |
testOverlapping(config.sub("overlapping"), mpihelper.getCommunication()); |
| 186 |
// Still need to implement proper discretization for non-overlapping |
| 187 |
// std::cout << std::endl << " Testing nonoverlapping tests... " << std::endl; |
| 188 |
// testNonoverlapping(config.sub("overlapping"), mpihelper.getCommunication()); |
| 189 |
return 0; |
| 190 |
} |