Debian Codegraph @

dune/istl/paamg/test/coarsentest.cc

raw
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 #include <dune/common/parallel/mpihelper.hh>
7 #include <dune/istl/paamg/matrixhierarchy.hh>
8 #include <dune/istl/owneroverlapcopy.hh>
9 #include <dune/istl/schwarz.hh>
10 #include "anisotropic.hh"
11
12 #include <utility>
13
14 constexpr int blockSize = 10;
15
16 using Communication = Dune::OwnerOverlapCopyCommunication<int,int>;
17 using ParallelIndexSet = Communication::ParallelIndexSet;
18 using MatrixBlock = Dune::FieldMatrix<double,blockSize,blockSize>;
19 using BCRSMat = Dune::BCRSMatrix<MatrixBlock>;
20 using VectorBlock = Dune::FieldVector<double,blockSize>;
21 using Vector = Dune::BlockVector<VectorBlock>;
22
23 /**
24 * Check if the matrices A and B are the same.
25 *
26 * @param BCRSMat A
27 * @param BCRSMat B
28 * @return bool - True if A and B are the same, false if they are not.
29 */
30 bool areSame(BCRSMat A, BCRSMat B)
31 {
32 for (auto rowIt = A.begin(); rowIt != A.end(); ++rowIt)
33 for (auto colIt = rowIt->begin(); colIt != rowIt->end(); ++colIt)
34 for (size_t k = 0; k < blockSize; k++)
35 for (size_t l = 0; l < blockSize; l++) {
36 std::ignore /*entryA*/ = (*colIt)[k][l];
37 try {
38 std::ignore /*entryB*/ = B[rowIt.index()][colIt.index()][k][l];
39 } catch (Dune::ISTLError& e) {
40 return false; // If the entry B[rowIt.index()][colIt.index()][k][l] does not exist, then the matrices are not the same.
41 }
42 if (std::abs((*colIt)[k][l]) > 1e-7 and std::abs(B[rowIt.index()][colIt.index()][k][l]) > 1e-7
43 and std::abs((*colIt)[k][l] - B[rowIt.index()][colIt.index()][k][l])/(*colIt)[k][l] > 1e-7) {
44 return false; // Check if the entries are approximately the same.
45 }
46 }
47 return true;
48 }
49
50 /**
51 * Builds two AMG matrix hierarchies and checks if they are equal.
52 * When setting up such matrix hierarchies on multiple processes, they get created in a non-deterministic way and the two matrix hierarchies might differ.
53 *
54 * @param int N Total number of unknowns
55 * @param int rank Rank of current process
56 * @param int noProcesses Number of all processes
57 * @return int - 0 if the hierarchies are the same, 1 if they are not.
58 */
59 int testHierarchy(int N, int rank, int noProcesses)
60 {
61 int n;
62 Communication pinfo(Dune::MPIHelper::getCommunicator());
63 ParallelIndexSet& indices = pinfo.indexSet();
64
65 using RemoteIndices = Dune::RemoteIndices<ParallelIndexSet>;
66 RemoteIndices& remoteIndices = pinfo.remoteIndices();
67
68 BCRSMat mat = setupAnisotropic2d<MatrixBlock>(N, indices, Dune::MPIHelper::getCommunication(), &n);
69
70 remoteIndices.rebuild<false>();
71
72 using OverlapFlags = Dune::NegateSet<Communication::OwnerSet>;
73 using Operator = Dune::OverlappingSchwarzOperator<BCRSMat,Vector,Vector,Communication>;
74 using Hierarchy = Dune::Amg::MatrixHierarchy<Operator,Communication>;
75
76 Operator op(mat, pinfo);
77 Hierarchy hierarchyA(
78 Dune::stackobject_to_shared_ptr(op),
79 Dune::stackobject_to_shared_ptr(pinfo));
80
81 Hierarchy hierarchyB(
82 Dune::stackobject_to_shared_ptr(op),
83 Dune::stackobject_to_shared_ptr(pinfo));
84
85 using Criterion = Dune::Amg::CoarsenCriterion<Dune::Amg::SymmetricCriterion<BCRSMat,Dune::Amg::FirstDiagonal> >;
86
87 Criterion criterionA(1000,10,1.2,1.6,Dune::Amg::AccumulationMode::successiveAccu,true);
88 Criterion criterionB(1000,10,1.2,1.6,Dune::Amg::AccumulationMode::successiveAccu,true);
89
90 hierarchyA.template build<OverlapFlags>(criterionA);
91 hierarchyB.template build<OverlapFlags>(criterionB);
92
93 MPI_Barrier(MPI_COMM_WORLD);
94 //Check if the second finest matrices are the same
95 auto finestMatrixA = hierarchyA.matrices().finest();
96 finestMatrixA++;
97 auto finestMatrixB = hierarchyB.matrices().finest();
98 finestMatrixB++;
99 if (!areSame(finestMatrixA->getmat(), finestMatrixB->getmat())) {
100 std::cerr << "During run with " << noProcesses << " processes: The matrix hierarchy on rank " << rank << " was created in a non-deterministic way." << std::endl;
101 return 1;
102 }
103 return 0;
104 }
105
106
107 int main(int argc, char** argv)
108 {
109 Dune::MPIHelper& mpiHelper = Dune::MPIHelper::instance(argc, argv);
110 int N=10;
111
112 if(argc>1)
113 N = atoi(argv[1]);
114
115 return testHierarchy(N, mpiHelper.rank(), mpiHelper.size());
116 }