| 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 <iostream> |
| 7 |
#include <tuple> |
| 8 |
#include <dune/common/enumset.hh> |
| 9 |
#include <dune/common/propertymap.hh> |
| 10 |
#include <dune/common/parallel/communicator.hh> |
| 11 |
#include <dune/istl/paamg/galerkin.hh> |
| 12 |
#include <dune/istl/paamg/dependency.hh> |
| 13 |
#include <dune/istl/paamg/globalaggregates.hh> |
| 14 |
#include <dune/istl/paamg/pinfo.hh> |
| 15 |
#include <dune/istl/io.hh> |
| 16 |
#include <dune/istl/bcrsmatrix.hh> |
| 17 |
#include <dune/istl/paamg/indicescoarsener.hh> |
| 18 |
#include <dune/istl/paamg/galerkin.hh> |
| 19 |
#include <dune/istl/owneroverlapcopy.hh> |
| 20 |
#include "anisotropic.hh" |
| 21 |
|
| 22 |
template<int BS> |
| 23 |
void testCoarsenIndices(int N) |
| 24 |
{ |
| 25 |
|
| 26 |
int procs, rank; |
| 27 |
MPI_Comm_rank(MPI_COMM_WORLD, &rank); |
| 28 |
MPI_Comm_size(MPI_COMM_WORLD, &procs); |
| 29 |
|
| 30 |
|
| 31 |
typedef Dune::OwnerOverlapCopyCommunication<int> ParallelInformation; |
| 32 |
typedef typename ParallelInformation::ParallelIndexSet ParallelIndexSet; |
| 33 |
typedef typename ParallelInformation::RemoteIndices RemoteIndices; |
| 34 |
typedef Dune::FieldMatrix<double,BS,BS> Block; |
| 35 |
typedef Dune::BCRSMatrix<Block> BCRSMat; |
| 36 |
int n; |
| 37 |
|
| 38 |
ParallelInformation pinfo(MPI_COMM_WORLD); |
| 39 |
ParallelIndexSet& indices = pinfo.indexSet(); |
| 40 |
RemoteIndices& remoteIndices = pinfo.remoteIndices(); |
| 41 |
|
| 42 |
typedef Dune::Communication<MPI_Comm> Comm; |
| 43 |
Comm cc(MPI_COMM_WORLD); |
| 44 |
|
| 45 |
BCRSMat mat = setupAnisotropic2d<Block>(N, indices, cc, &n); |
| 46 |
|
| 47 |
pinfo.remoteIndices().template rebuild<false>(); |
| 48 |
|
| 49 |
typedef Dune::Amg::MatrixGraph<BCRSMat> MatrixGraph; |
| 50 |
typedef Dune::Amg::SubGraph<Dune::Amg::MatrixGraph<BCRSMat>,std::vector<bool> > SubGraph; |
| 51 |
typedef Dune::Amg::PropertiesGraph<SubGraph,Dune::Amg::VertexProperties, |
| 52 |
Dune::Amg::EdgeProperties, Dune::IdentityMap, typename SubGraph::EdgeIndexMap> PropertiesGraph; |
| 53 |
typedef typename PropertiesGraph::VertexDescriptor Vertex; |
| 54 |
typedef Dune::Amg::SymmetricCriterion<BCRSMat,Dune::Amg::FirstDiagonal> |
| 55 |
Criterion; |
| 56 |
|
| 57 |
MatrixGraph mg(mat); |
| 58 |
std::vector<bool> excluded(mat.N()); |
| 59 |
|
| 60 |
typedef ParallelIndexSet::iterator IndexIterator; |
| 61 |
IndexIterator iend = indices.end(); |
| 62 |
typename std::vector<bool>::iterator iter=excluded.begin(); |
| 63 |
|
| 64 |
for(IndexIterator index = indices.begin(); index != iend; ++index, ++iter) |
| 65 |
*iter = (index->local().attribute()==GridAttributes::copy); |
| 66 |
|
| 67 |
SubGraph sg(mg, excluded); |
| 68 |
PropertiesGraph pg(sg, Dune::IdentityMap(), sg.getEdgeIndexMap()); |
| 69 |
Dune::Amg::AggregatesMap<Vertex> aggregatesMap(pg.noVertices()); |
| 70 |
|
| 71 |
std::cout << "fine indices: "<<indices << std::endl; |
| 72 |
std::cout << "fine remote: "<<remoteIndices << std::endl; |
| 73 |
|
| 74 |
int noAggregates, isoAggregates, oneAggregates, skipped; |
| 75 |
|
| 76 |
std::tie(noAggregates, isoAggregates, oneAggregates,skipped) = aggregatesMap.buildAggregates(mat, pg, Criterion(), false); |
| 77 |
|
| 78 |
Dune::Amg::printAggregates2d(aggregatesMap, n, N, std::cout); |
| 79 |
|
| 80 |
ParallelInformation coarseInfo(MPI_COMM_WORLD); |
| 81 |
ParallelIndexSet& coarseIndices = coarseInfo.indexSet(); |
| 82 |
RemoteIndices& coarseRemote = coarseInfo.remoteIndices(); |
| 83 |
|
| 84 |
typename Dune::PropertyMapTypeSelector<Dune::Amg::VertexVisitedTag,PropertiesGraph>::Type visitedMap = Dune::get(Dune::Amg::VertexVisitedTag(), pg); |
| 85 |
|
| 86 |
pinfo.buildGlobalLookup(aggregatesMap.noVertices()); |
| 87 |
|
| 88 |
int noCoarseVertices = Dune::Amg::IndicesCoarsener<ParallelInformation,Dune::EnumItem<GridFlag,GridAttributes::copy> >::coarsen(pinfo, |
| 89 |
pg, |
| 90 |
visitedMap, |
| 91 |
aggregatesMap, |
| 92 |
coarseInfo, |
| 93 |
noAggregates); |
| 94 |
|
| 95 |
coarseInfo.buildGlobalLookup(noCoarseVertices); |
| 96 |
std::cout << rank <<": coarse indices: " <<coarseIndices << std::endl; |
| 97 |
std::cout << rank <<": coarse remote indices:"<<coarseRemote <<std::endl; |
| 98 |
|
| 99 |
typedef Dune::Interface Interface; |
| 100 |
typedef Dune::BufferedCommunicator Communicator; |
| 101 |
Interface interface; |
| 102 |
interface.build(remoteIndices, Dune::EnumItem<GridFlag,GridAttributes::owner>(), Dune::EnumItem<GridFlag, GridAttributes::copy>()); |
| 103 |
Communicator communicator; |
| 104 |
|
| 105 |
typedef Dune::Amg::GlobalAggregatesMap<Vertex,ParallelIndexSet> GlobalMap; |
| 106 |
|
| 107 |
GlobalMap gmap(aggregatesMap, coarseInfo.globalLookup()); |
| 108 |
|
| 109 |
communicator.build<GlobalMap>(interface); |
| 110 |
|
| 111 |
Dune::Amg::printAggregates2d(aggregatesMap, n, N, std::cout); |
| 112 |
|
| 113 |
communicator.template forward<Dune::Amg::AggregatesGatherScatter<typename MatrixGraph::VertexDescriptor,ParallelIndexSet> >(gmap); |
| 114 |
|
| 115 |
std::cout<<"Communicated: "; |
| 116 |
Dune::Amg::printAggregates2d(aggregatesMap, n, N, std::cout); |
| 117 |
|
| 118 |
Dune::Amg::GalerkinProduct<ParallelInformation> productBuilder; |
| 119 |
|
| 120 |
typedef Dune::IteratorPropertyMap<bool*, Dune::IdentityMap> VisitedMap2; |
| 121 |
/* |
| 122 |
Vector visited; |
| 123 |
visited.reserve(mg.maxVertex()); |
| 124 |
Iterator visitedIterator=visited.begin(); |
| 125 |
*/ |
| 126 |
std::cout<<n*n<<"=="<<mg.noVertices()<<std::endl; |
| 127 |
|
| 128 |
assert(mat.N()==mg.maxVertex()+1); |
| 129 |
|
| 130 |
bool* visitedIterator = new bool[mat.N()]; |
| 131 |
|
| 132 |
for(Vertex i=0; i < mat.N(); ++i) |
| 133 |
visitedIterator[i]=false; |
| 134 |
|
| 135 |
VisitedMap2 visitedMap2(visitedIterator, Dune::IdentityMap()); |
| 136 |
|
| 137 |
BCRSMat* coarseMat = productBuilder.build(mg, visitedMap2, pinfo, |
| 138 |
aggregatesMap, noCoarseVertices, |
| 139 |
Dune::EnumItem<GridFlag,GridAttributes::copy>()); |
| 140 |
|
| 141 |
delete[] visitedIterator; |
| 142 |
pinfo.freeGlobalLookup(); |
| 143 |
productBuilder.calculate(mat, aggregatesMap, *coarseMat, coarseInfo, Dune::EnumItem<GridFlag,GridAttributes::copy>()); |
| 144 |
|
| 145 |
if(N<5) { |
| 146 |
Dune::printmatrix(std::cout,mat,"fine","row",9,1); |
| 147 |
Dune::printmatrix(std::cout,*coarseMat,"coarse","row",9,1); |
| 148 |
} |
| 149 |
} |
| 150 |
|
| 151 |
|
| 152 |
int main(int argc, char **argv) |
| 153 |
{ |
| 154 |
MPI_Init(&argc, &argv); |
| 155 |
int N=5; |
| 156 |
|
| 157 |
if(argc>1) |
| 158 |
N = atoi(argv[1]); |
| 159 |
std::cout << "Galerkin test with N=" << N << std::endl; |
| 160 |
testCoarsenIndices<1>(N); |
| 161 |
MPI_Finalize(); |
| 162 |
} |