| 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 |
#ifndef DUNE_GALERKIN_HH |
| 6 |
#define DUNE_GALERKIN_HH |
| 7 |
|
| 8 |
#include "aggregates.hh" |
| 9 |
#include "pinfo.hh" |
| 10 |
#include <dune/common/poolallocator.hh> |
| 11 |
#include <dune/common/enumset.hh> |
| 12 |
#include <set> |
| 13 |
#include <limits> |
| 14 |
#include <algorithm> |
| 15 |
|
| 16 |
namespace Dune |
| 17 |
{ |
| 18 |
namespace Amg |
| 19 |
{ |
| 20 |
/** |
| 21 |
* @addtogroup ISTL_PAAMG |
| 22 |
* |
| 23 |
* @{ |
| 24 |
*/ |
| 25 |
/** @file |
| 26 |
* @author Markus Blatt |
| 27 |
* @brief Provides a class for building the galerkin product |
| 28 |
* based on a aggregation scheme. |
| 29 |
*/ |
| 30 |
|
| 31 |
template<class T> |
| 32 |
struct OverlapVertex |
| 33 |
{ |
| 34 |
/** |
| 35 |
* @brief The aggregate descriptor. |
| 36 |
*/ |
| 37 |
typedef T Aggregate; |
| 38 |
|
| 39 |
/** |
| 40 |
* @brief The vertex descriptor. |
| 41 |
*/ |
| 42 |
typedef T Vertex; |
| 43 |
|
| 44 |
/** |
| 45 |
* @brief The aggregate the vertex belongs to. |
| 46 |
*/ |
| 47 |
Aggregate* aggregate; |
| 48 |
|
| 49 |
/** |
| 50 |
* @brief The vertex descriptor. |
| 51 |
*/ |
| 52 |
Vertex vertex; |
| 53 |
}; |
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
/** |
| 58 |
* @brief Functor for building the sparsity pattern of the matrix |
| 59 |
* using examineConnectivity. |
| 60 |
*/ |
| 61 |
template<class M> |
| 62 |
class SparsityBuilder |
| 63 |
{ |
| 64 |
public: |
| 65 |
/** |
| 66 |
* @brief Constructor. |
| 67 |
* @param matrix The matrix whose sparsity pattern we |
| 68 |
* should set up. |
| 69 |
*/ |
| 70 |
SparsityBuilder(M& matrix); |
| 71 |
|
| 72 |
void insert(const typename M::size_type& index); |
| 73 |
|
| 74 |
void operator++(); |
| 75 |
|
| 76 |
std::size_t minRowSize(); |
| 77 |
|
| 78 |
std::size_t maxRowSize(); |
| 79 |
|
| 80 |
std::size_t sumRowSize(); |
| 81 |
std::size_t index() |
| 82 |
{ |
| 83 |
return row_.index(); |
| 84 |
} |
| 85 |
private: |
| 86 |
/** @brief Create iterator for the current row. */ |
| 87 |
typename M::CreateIterator row_; |
| 88 |
/** @brief The minim row size. */ |
| 89 |
std::size_t minRowSize_; |
| 90 |
/** @brief The maximum row size. */ |
| 91 |
std::size_t maxRowSize_; |
| 92 |
std::size_t sumRowSize_; |
| 93 |
#ifdef DUNE_ISTL_WITH_CHECKING |
| 94 |
bool diagonalInserted; |
| 95 |
#endif |
| 96 |
}; |
| 97 |
|
| 98 |
class BaseGalerkinProduct |
| 99 |
{ |
| 100 |
public: |
| 101 |
/** |
| 102 |
* @brief Calculate the galerkin product. |
| 103 |
* @param fine The fine matrix. |
| 104 |
* @param aggregates The aggregate mapping. |
| 105 |
* @param coarse The coarse Matrix. |
| 106 |
* @param pinfo Parallel information about the fine level. |
| 107 |
* @param copy The attribute set identifying the copy nodes of the graph. |
| 108 |
*/ |
| 109 |
template<class M, class V, class I, class O> |
| 110 |
void calculate(const M& fine, const AggregatesMap<V>& aggregates, M& coarse, |
| 111 |
const I& pinfo, const O& copy); |
| 112 |
|
| 113 |
}; |
| 114 |
|
| 115 |
template<class T> |
| 116 |
class GalerkinProduct |
| 117 |
: public BaseGalerkinProduct |
| 118 |
{ |
| 119 |
public: |
| 120 |
typedef T ParallelInformation; |
| 121 |
|
| 122 |
/** |
| 123 |
* @brief Calculates the coarse matrix via a Galerkin product. |
| 124 |
* @param fineGraph The graph of the fine matrix. |
| 125 |
* @param visitedMap Map for marking vertices as visited. |
| 126 |
* @param pinfo Parallel information about the fine level. |
| 127 |
* @param aggregates The mapping of the fine level unknowns onto aggregates. |
| 128 |
* @param size The number of columns and rows of the coarse matrix. |
| 129 |
* @param copy The attribute set identifying the copy nodes of the graph. |
| 130 |
*/ |
| 131 |
template<class G, class V, class Set> |
| 132 |
typename G::MutableMatrix* build(G& fineGraph, V& visitedMap, |
| 133 |
const ParallelInformation& pinfo, |
| 134 |
AggregatesMap<typename G::VertexDescriptor>& aggregates, |
| 135 |
const typename G::Matrix::size_type& size, |
| 136 |
const Set& copy); |
| 137 |
private: |
| 138 |
|
| 139 |
/** |
| 140 |
* @brief Builds the data structure needed for rebuilding the aggregates int the overlap. |
| 141 |
* @param graph The graph of the matrix. |
| 142 |
* @param pinfo The parallel information. |
| 143 |
* @param aggregates The mapping onto the aggregates. |
| 144 |
*/ |
| 145 |
template<class G, class I, class Set> |
| 146 |
const OverlapVertex<typename G::VertexDescriptor>* |
| 147 |
buildOverlapVertices(const G& graph, const I& pinfo, |
| 148 |
AggregatesMap<typename G::VertexDescriptor>& aggregates, |
| 149 |
const Set& overlap, |
| 150 |
std::size_t& overlapCount); |
| 151 |
|
| 152 |
template<class A> |
| 153 |
struct OVLess |
| 154 |
{ |
| 155 |
bool operator()(const OverlapVertex<A>& o1, const OverlapVertex<A>& o2) |
| 156 |
{ |
| 157 |
return *o1.aggregate < *o2.aggregate; |
| 158 |
} |
| 159 |
}; |
| 160 |
}; |
| 161 |
|
| 162 |
template<> |
| 163 |
class GalerkinProduct<SequentialInformation> |
| 164 |
: public BaseGalerkinProduct |
| 165 |
{ |
| 166 |
public: |
| 167 |
/** |
| 168 |
* @brief Calculates the coarse matrix via a Galerkin product. |
| 169 |
* @param fineGraph The graph of the fine matrix. |
| 170 |
* @param visitedMap Map for marking vertices as visited. |
| 171 |
* @param pinfo Parallel information about the fine level. |
| 172 |
* @param aggregates The mapping of the fine level unknowns onto aggregates. |
| 173 |
* @param size The number of columns and rows of the coarse matrix. |
| 174 |
* @param copy The attribute set identifying the copy nodes of the graph. |
| 175 |
*/ |
| 176 |
template<class G, class V, class Set> |
| 177 |
typename G::MutableMatrix* build(G& fineGraph, V& visitedMap, |
| 178 |
const SequentialInformation& pinfo, |
| 179 |
const AggregatesMap<typename G::VertexDescriptor>& aggregates, |
| 180 |
const typename G::Matrix::size_type& size, |
| 181 |
const Set& copy); |
| 182 |
}; |
| 183 |
|
| 184 |
struct BaseConnectivityConstructor |
| 185 |
{ |
| 186 |
template<class R, class G, class V> |
| 187 |
static void constructOverlapConnectivity(R& row, G& graph, V& visitedMap, |
| 188 |
const AggregatesMap<typename G::VertexDescriptor>& aggregates, |
| 189 |
const OverlapVertex<typename G::VertexDescriptor>*& seed, |
| 190 |
const OverlapVertex<typename G::VertexDescriptor>* overlapEnd); |
| 191 |
|
| 192 |
/** |
| 193 |
* @brief Construct the connectivity of an aggregate in the overlap. |
| 194 |
*/ |
| 195 |
template<class R, class G, class V> |
| 196 |
static void constructNonOverlapConnectivity(R& row, G& graph, V& visitedMap, |
| 197 |
const AggregatesMap<typename G::VertexDescriptor>& aggregates, |
| 198 |
const typename G::VertexDescriptor& seed); |
| 199 |
|
| 200 |
|
| 201 |
/** |
| 202 |
* @brief Visitor for identifying connected aggregates during a breadthFirstSearch. |
| 203 |
*/ |
| 204 |
template<class G, class S, class V> |
| 205 |
class ConnectedBuilder |
| 206 |
{ |
| 207 |
public: |
| 208 |
/** |
| 209 |
* @brief The type of the graph. |
| 210 |
*/ |
| 211 |
typedef G Graph; |
| 212 |
/** |
| 213 |
* @brief The constant edge iterator. |
| 214 |
*/ |
| 215 |
typedef typename Graph::ConstEdgeIterator ConstEdgeIterator; |
| 216 |
|
| 217 |
/** |
| 218 |
* @brief The type of the connected set. |
| 219 |
*/ |
| 220 |
typedef S Set; |
| 221 |
|
| 222 |
/** |
| 223 |
* @brief The type of the map for marking vertices as visited. |
| 224 |
*/ |
| 225 |
typedef V VisitedMap; |
| 226 |
|
| 227 |
/** |
| 228 |
* @brief The vertex descriptor of the graph. |
| 229 |
*/ |
| 230 |
typedef typename Graph::VertexDescriptor Vertex; |
| 231 |
|
| 232 |
/** |
| 233 |
* @brief Constructor |
| 234 |
* @param aggregates The mapping of the vertices onto the aggregates. |
| 235 |
* @param graph The graph to work on. |
| 236 |
* @param visitedMap The map for marking vertices as visited |
| 237 |
* @param connected The set to added the connected aggregates to. |
| 238 |
*/ |
| 239 |
ConnectedBuilder(const AggregatesMap<Vertex>& aggregates, Graph& graph, |
| 240 |
VisitedMap& visitedMap, Set& connected); |
| 241 |
|
| 242 |
/** |
| 243 |
* @brief Process an edge pointing to another aggregate. |
| 244 |
* @param edge The iterator positioned at the edge. |
| 245 |
*/ |
| 246 |
void operator()(const ConstEdgeIterator& edge); |
| 247 |
|
| 248 |
private: |
| 249 |
/** |
| 250 |
* @brief The mapping of the vertices onto the aggregates. |
| 251 |
*/ |
| 252 |
const AggregatesMap<Vertex>& aggregates_; |
| 253 |
|
| 254 |
Graph& graph_; |
| 255 |
|
| 256 |
/** |
| 257 |
* @brief The map for marking vertices as visited. |
| 258 |
*/ |
| 259 |
VisitedMap& visitedMap_; |
| 260 |
|
| 261 |
/** |
| 262 |
* @brief The set to add the connected aggregates to. |
| 263 |
*/ |
| 264 |
Set& connected_; |
| 265 |
}; |
| 266 |
|
| 267 |
}; |
| 268 |
|
| 269 |
template<class G, class T> |
| 270 |
struct ConnectivityConstructor : public BaseConnectivityConstructor |
| 271 |
{ |
| 272 |
typedef typename G::VertexDescriptor Vertex; |
| 273 |
|
| 274 |
template<class V, class O, class R> |
| 275 |
static void examine(G& graph, |
| 276 |
V& visitedMap, |
| 277 |
const T& pinfo, |
| 278 |
const AggregatesMap<Vertex>& aggregates, |
| 279 |
const O& overlap, |
| 280 |
const OverlapVertex<Vertex>* overlapVertices, |
| 281 |
const OverlapVertex<Vertex>* overlapEnd, |
| 282 |
R& row); |
| 283 |
}; |
| 284 |
|
| 285 |
template<class G> |
| 286 |
struct ConnectivityConstructor<G,SequentialInformation> : public BaseConnectivityConstructor |
| 287 |
{ |
| 288 |
typedef typename G::VertexDescriptor Vertex; |
| 289 |
|
| 290 |
template<class V, class R> |
| 291 |
static void examine(G& graph, |
| 292 |
V& visitedMap, |
| 293 |
const SequentialInformation& pinfo, |
| 294 |
const AggregatesMap<Vertex>& aggregates, |
| 295 |
R& row); |
| 296 |
}; |
| 297 |
|
| 298 |
template<class T> |
| 299 |
struct DirichletBoundarySetter |
| 300 |
{ |
| 301 |
template<class M, class O> |
| 302 |
static void set(M& coarse, const T& pinfo, const O& copy); |
| 303 |
}; |
| 304 |
|
| 305 |
template<> |
| 306 |
struct DirichletBoundarySetter<SequentialInformation> |
| 307 |
{ |
| 308 |
template<class M, class O> |
| 309 |
static void set(M& coarse, const SequentialInformation& pinfo, const O& copy); |
| 310 |
}; |
| 311 |
|
| 312 |
template<class R, class G, class V> |
| 313 |
void BaseConnectivityConstructor::constructNonOverlapConnectivity(R& row, G& graph, V& visitedMap, |
| 314 |
const AggregatesMap<typename G::VertexDescriptor>& aggregates, |
| 315 |
const typename G::VertexDescriptor& seed) |
| 316 |
{ |
| 317 |
assert(row.index()==aggregates[seed]); |
| 318 |
row.insert(aggregates[seed]); |
| 319 |
ConnectedBuilder<G,R,V> conBuilder(aggregates, graph, visitedMap, row); |
| 320 |
typedef typename G::VertexDescriptor Vertex; |
| 321 |
typedef std::allocator<Vertex> Allocator; |
| 322 |
typedef SLList<Vertex,Allocator> VertexList; |
| 323 |
typedef typename AggregatesMap<Vertex>::DummyEdgeVisitor DummyVisitor; |
| 324 |
VertexList vlist; |
| 325 |
DummyVisitor dummy; |
| 326 |
aggregates.template breadthFirstSearch<true,false>(seed,aggregates[seed], graph, vlist, dummy, |
| 327 |
conBuilder, visitedMap); |
| 328 |
} |
| 329 |
|
| 330 |
template<class R, class G, class V> |
| 331 |
void BaseConnectivityConstructor::constructOverlapConnectivity(R& row, G& graph, V& visitedMap, |
| 332 |
const AggregatesMap<typename G::VertexDescriptor>& aggregates, |
| 333 |
const OverlapVertex<typename G::VertexDescriptor>*& seed, |
| 334 |
const OverlapVertex<typename G::VertexDescriptor>* overlapEnd) |
| 335 |
{ |
| 336 |
ConnectedBuilder<G,R,V> conBuilder(aggregates, graph, visitedMap, row); |
| 337 |
const typename G::VertexDescriptor aggregate=*seed->aggregate; |
| 338 |
|
| 339 |
if (row.index()==*seed->aggregate) { |
| 340 |
while(seed != overlapEnd && aggregate == *seed->aggregate) { |
| 341 |
row.insert(*seed->aggregate); |
| 342 |
// Walk over all neighbours and add them to the connected array. |
| 343 |
visitNeighbours(graph, seed->vertex, conBuilder); |
| 344 |
// Mark vertex as visited |
| 345 |
put(visitedMap, seed->vertex, true); |
| 346 |
++seed; |
| 347 |
} |
| 348 |
} |
| 349 |
} |
| 350 |
|
| 351 |
template<class G, class S, class V> |
| 352 |
BaseConnectivityConstructor::ConnectedBuilder<G,S,V>::ConnectedBuilder(const AggregatesMap<Vertex>& aggregates, |
| 353 |
Graph& graph, VisitedMap& visitedMap, |
| 354 |
Set& connected) |
| 355 |
: aggregates_(aggregates), graph_(graph), visitedMap_(visitedMap), connected_(connected) |
| 356 |
{} |
| 357 |
|
| 358 |
template<class G, class S, class V> |
| 359 |
void BaseConnectivityConstructor::ConnectedBuilder<G,S,V>::operator()(const ConstEdgeIterator& edge) |
| 360 |
{ |
| 361 |
const Vertex& vertex = aggregates_[edge.target()]; |
| 362 |
assert(vertex!= AggregatesMap<Vertex>::UNAGGREGATED); |
| 363 |
if(vertex!= AggregatesMap<Vertex>::ISOLATED) |
| 364 |
connected_.insert(vertex); |
| 365 |
} |
| 366 |
|
| 367 |
template<class T> |
| 368 |
template<class G, class I, class Set> |
| 369 |
const OverlapVertex<typename G::VertexDescriptor>* |
| 370 |
GalerkinProduct<T>::buildOverlapVertices(const G& graph, const I& pinfo, |
| 371 |
AggregatesMap<typename G::VertexDescriptor>& aggregates, |
| 372 |
const Set& overlap, |
| 373 |
std::size_t& overlapCount) |
| 374 |
{ |
| 375 |
// count the overlap vertices. |
| 376 |
typedef typename G::ConstVertexIterator ConstIterator; |
| 377 |
typedef typename I::GlobalLookupIndexSet GlobalLookup; |
| 378 |
typedef typename GlobalLookup::IndexPair IndexPair; |
| 379 |
|
| 380 |
const ConstIterator end = graph.end(); |
| 381 |
overlapCount = 0; |
| 382 |
|
| 383 |
const GlobalLookup& lookup=pinfo.globalLookup(); |
| 384 |
|
| 385 |
for(ConstIterator vertex=graph.begin(); vertex != end; ++vertex) { |
| 386 |
const IndexPair* pair = lookup.pair(*vertex); |
| 387 |
|
| 388 |
if(pair!=0 && overlap.contains(pair->local().attribute())) |
| 389 |
++overlapCount; |
| 390 |
} |
| 391 |
// Allocate space |
| 392 |
typedef typename G::VertexDescriptor Vertex; |
| 393 |
|
| 394 |
OverlapVertex<Vertex>* overlapVertices = new OverlapVertex<Vertex>[overlapCount=0 ? 1 : overlapCount]; |
| 395 |
if(overlapCount==0) |
| 396 |
return overlapVertices; |
| 397 |
|
| 398 |
// Initialize them |
| 399 |
overlapCount=0; |
| 400 |
for(ConstIterator vertex=graph.begin(); vertex != end; ++vertex) { |
| 401 |
const IndexPair* pair = lookup.pair(*vertex); |
| 402 |
|
| 403 |
if(pair!=0 && overlap.contains(pair->local().attribute())) { |
| 404 |
overlapVertices[overlapCount].aggregate = &aggregates[pair->local()]; |
| 405 |
overlapVertices[overlapCount].vertex = pair->local(); |
| 406 |
++overlapCount; |
| 407 |
} |
| 408 |
} |
| 409 |
|
| 410 |
dverb << overlapCount<<" overlap vertices"<<std::endl; |
| 411 |
|
| 412 |
std::sort(overlapVertices, overlapVertices+overlapCount, OVLess<Vertex>()); |
| 413 |
// due to the sorting the isolated aggregates (to be skipped) are at the end. |
| 414 |
|
| 415 |
return overlapVertices; |
| 416 |
} |
| 417 |
|
| 418 |
template<class G, class T> |
| 419 |
template<class V, class O, class R> |
| 420 |
void ConnectivityConstructor<G,T>::examine(G& graph, |
| 421 |
V& visitedMap, |
| 422 |
const T& pinfo, |
| 423 |
const AggregatesMap<Vertex>& aggregates, |
| 424 |
const O& overlap, |
| 425 |
const OverlapVertex<Vertex>* overlapVertices, |
| 426 |
const OverlapVertex<Vertex>* overlapEnd, |
| 427 |
R& row) |
| 428 |
{ |
| 429 |
typedef typename T::GlobalLookupIndexSet GlobalLookup; |
| 430 |
const GlobalLookup& lookup = pinfo.globalLookup(); |
| 431 |
|
| 432 |
typedef typename G::VertexIterator VertexIterator; |
| 433 |
|
| 434 |
VertexIterator vend=graph.end(); |
| 435 |
|
| 436 |
#ifdef DUNE_ISTL_WITH_CHECKING |
| 437 |
std::set<Vertex> examined; |
| 438 |
#endif |
| 439 |
|
| 440 |
// The aggregates owned by the process have lower local indices |
| 441 |
// then those not owned. We process them in the first pass. |
| 442 |
// They represent the rows 0, 1, ..., n of the coarse matrix |
| 443 |
for(VertexIterator vertex = graph.begin(); vertex != vend; ++vertex) |
| 444 |
if(!get(visitedMap, *vertex)) { |
| 445 |
// In the first pass we only process owner nodes |
| 446 |
typedef typename GlobalLookup::IndexPair IndexPair; |
| 447 |
const IndexPair* pair = lookup.pair(*vertex); |
| 448 |
if(pair==0 || !overlap.contains(pair->local().attribute())) { |
| 449 |
#ifdef DUNE_ISTL_WITH_CHECKING |
| 450 |
assert(examined.find(aggregates[*vertex])==examined.end()); |
| 451 |
examined.insert(aggregates[*vertex]); |
| 452 |
#endif |
| 453 |
constructNonOverlapConnectivity(row, graph, visitedMap, aggregates, *vertex); |
| 454 |
|
| 455 |
// only needed for ALU |
| 456 |
// (ghosts with same global id as owners on the same process) |
| 457 |
if (SolverCategory::category(pinfo) == static_cast<int>(SolverCategory::nonoverlapping)) { |
| 458 |
if(overlapVertices != overlapEnd) { |
| 459 |
if(*overlapVertices->aggregate!=AggregatesMap<Vertex>::ISOLATED) { |
| 460 |
constructOverlapConnectivity(row, graph, visitedMap, aggregates, overlapVertices, overlapEnd); |
| 461 |
} |
| 462 |
else{ |
| 463 |
++overlapVertices; |
| 464 |
} |
| 465 |
} |
| 466 |
} |
| 467 |
++row; |
| 468 |
} |
| 469 |
} |
| 470 |
|
| 471 |
dvverb<<"constructed "<<row.index()<<" non-overlapping rows"<<std::endl; |
| 472 |
|
| 473 |
// Now come the aggregates not owned by use. |
| 474 |
// They represent the rows n+1, ..., N |
| 475 |
while(overlapVertices != overlapEnd) |
| 476 |
if(*overlapVertices->aggregate!=AggregatesMap<Vertex>::ISOLATED) { |
| 477 |
|
| 478 |
#ifdef DUNE_ISTL_WITH_CHECKING |
| 479 |
typedef typename GlobalLookup::IndexPair IndexPair; |
| 480 |
const IndexPair* pair = lookup.pair(overlapVertices->vertex); |
| 481 |
assert(pair!=0 && overlap.contains(pair->local().attribute())); |
| 482 |
assert(examined.find(aggregates[overlapVertices->vertex])==examined.end()); |
| 483 |
examined.insert(aggregates[overlapVertices->vertex]); |
| 484 |
#endif |
| 485 |
constructOverlapConnectivity(row, graph, visitedMap, aggregates, overlapVertices, overlapEnd); |
| 486 |
++row; |
| 487 |
}else{ |
| 488 |
++overlapVertices; |
| 489 |
} |
| 490 |
} |
| 491 |
|
| 492 |
template<class G> |
| 493 |
template<class V, class R> |
| 494 |
void ConnectivityConstructor<G,SequentialInformation>::examine(G& graph, |
| 495 |
V& visitedMap, |
| 496 |
[[maybe_unused]] const SequentialInformation& pinfo, |
| 497 |
const AggregatesMap<Vertex>& aggregates, |
| 498 |
R& row) |
| 499 |
{ |
| 500 |
typedef typename G::VertexIterator VertexIterator; |
| 501 |
|
| 502 |
VertexIterator vend=graph.end(); |
| 503 |
for(VertexIterator vertex = graph.begin(); vertex != vend; ++vertex) { |
| 504 |
if(!get(visitedMap, *vertex)) { |
| 505 |
constructNonOverlapConnectivity(row, graph, visitedMap, aggregates, *vertex); |
| 506 |
++row; |
| 507 |
} |
| 508 |
} |
| 509 |
|
| 510 |
} |
| 511 |
|
| 512 |
template<class M> |
| 513 |
SparsityBuilder<M>::SparsityBuilder(M& matrix) |
| 514 |
: row_(matrix.createbegin()), |
| 515 |
minRowSize_(std::numeric_limits<std::size_t>::max()), |
| 516 |
maxRowSize_(0), sumRowSize_(0) |
| 517 |
{ |
| 518 |
#ifdef DUNE_ISTL_WITH_CHECKING |
| 519 |
diagonalInserted = false; |
| 520 |
#endif |
| 521 |
} |
| 522 |
template<class M> |
| 523 |
std::size_t SparsityBuilder<M>::maxRowSize() |
| 524 |
{ |
| 525 |
return maxRowSize_; |
| 526 |
} |
| 527 |
template<class M> |
| 528 |
std::size_t SparsityBuilder<M>::minRowSize() |
| 529 |
{ |
| 530 |
return minRowSize_; |
| 531 |
} |
| 532 |
|
| 533 |
template<class M> |
| 534 |
std::size_t SparsityBuilder<M>::sumRowSize() |
| 535 |
{ |
| 536 |
return sumRowSize_; |
| 537 |
} |
| 538 |
template<class M> |
| 539 |
void SparsityBuilder<M>::operator++() |
| 540 |
{ |
| 541 |
sumRowSize_ += row_.size(); |
| 542 |
minRowSize_=std::min<std::size_t>(minRowSize_, row_.size()); |
| 543 |
maxRowSize_=std::max<std::size_t>(maxRowSize_, row_.size()); |
| 544 |
++row_; |
| 545 |
#ifdef DUNE_ISTL_WITH_CHECKING |
| 546 |
assert(diagonalInserted); |
| 547 |
diagonalInserted = false; |
| 548 |
#endif |
| 549 |
} |
| 550 |
|
| 551 |
template<class M> |
| 552 |
void SparsityBuilder<M>::insert(const typename M::size_type& index) |
| 553 |
{ |
| 554 |
row_.insert(index); |
| 555 |
#ifdef DUNE_ISTL_WITH_CHECKING |
| 556 |
diagonalInserted = diagonalInserted || row_.index()==index; |
| 557 |
#endif |
| 558 |
} |
| 559 |
|
| 560 |
template<class T> |
| 561 |
template<class G, class V, class Set> |
| 562 |
typename G::MutableMatrix* |
| 563 |
GalerkinProduct<T>::build(G& fineGraph, V& visitedMap, |
| 564 |
const ParallelInformation& pinfo, |
| 565 |
AggregatesMap<typename G::VertexDescriptor>& aggregates, |
| 566 |
const typename G::Matrix::size_type& size, |
| 567 |
const Set& overlap) |
| 568 |
{ |
| 569 |
typedef OverlapVertex<typename G::VertexDescriptor> OverlapVertex; |
| 570 |
|
| 571 |
std::size_t count; |
| 572 |
|
| 573 |
const OverlapVertex* overlapVertices = buildOverlapVertices(fineGraph, |
| 574 |
pinfo, |
| 575 |
aggregates, |
| 576 |
overlap, |
| 577 |
count); |
| 578 |
typedef typename G::MutableMatrix M; |
| 579 |
M* coarseMatrix = new M(size, size, M::row_wise); |
| 580 |
|
| 581 |
// Reset the visited flags of all vertices. |
| 582 |
// As the isolated nodes will be skipped we simply mark them as visited |
| 583 |
|
| 584 |
typedef typename G::VertexIterator Vertex; |
| 585 |
Vertex vend = fineGraph.end(); |
| 586 |
for(Vertex vertex = fineGraph.begin(); vertex != vend; ++vertex) { |
| 587 |
assert(aggregates[*vertex] != AggregatesMap<typename G::VertexDescriptor>::UNAGGREGATED); |
| 588 |
put(visitedMap, *vertex, aggregates[*vertex]==AggregatesMap<typename G::VertexDescriptor>::ISOLATED); |
| 589 |
} |
| 590 |
|
| 591 |
typedef typename G::MutableMatrix M; |
| 592 |
SparsityBuilder<M> sparsityBuilder(*coarseMatrix); |
| 593 |
|
| 594 |
ConnectivityConstructor<G,T>::examine(fineGraph, visitedMap, pinfo, |
| 595 |
aggregates, overlap, |
| 596 |
overlapVertices, |
| 597 |
overlapVertices+count, |
| 598 |
sparsityBuilder); |
| 599 |
|
| 600 |
dinfo<<pinfo.communicator().rank()<<": Matrix ("<<coarseMatrix->N()<<"x"<<coarseMatrix->M()<<" row: min="<<sparsityBuilder.minRowSize()<<" max=" |
| 601 |
<<sparsityBuilder.maxRowSize()<<" avg=" |
| 602 |
<<static_cast<double>(sparsityBuilder.sumRowSize())/coarseMatrix->N() |
| 603 |
<<std::endl; |
| 604 |
|
| 605 |
delete[] overlapVertices; |
| 606 |
|
| 607 |
return coarseMatrix; |
| 608 |
} |
| 609 |
|
| 610 |
template<class G, class V, class Set> |
| 611 |
typename G::MutableMatrix* |
| 612 |
GalerkinProduct<SequentialInformation>::build(G& fineGraph, V& visitedMap, |
| 613 |
const SequentialInformation& pinfo, |
| 614 |
const AggregatesMap<typename G::VertexDescriptor>& aggregates, |
| 615 |
const typename G::Matrix::size_type& size, |
| 616 |
[[maybe_unused]] const Set& overlap) |
| 617 |
{ |
| 618 |
typedef typename G::MutableMatrix M; |
| 619 |
M* coarseMatrix = new M(size, size, M::row_wise); |
| 620 |
|
| 621 |
// Reset the visited flags of all vertices. |
| 622 |
// As the isolated nodes will be skipped we simply mark them as visited |
| 623 |
|
| 624 |
typedef typename G::VertexIterator Vertex; |
| 625 |
Vertex vend = fineGraph.end(); |
| 626 |
for(Vertex vertex = fineGraph.begin(); vertex != vend; ++vertex) { |
| 627 |
assert(aggregates[*vertex] != AggregatesMap<typename G::VertexDescriptor>::UNAGGREGATED); |
| 628 |
put(visitedMap, *vertex, aggregates[*vertex]==AggregatesMap<typename G::VertexDescriptor>::ISOLATED); |
| 629 |
} |
| 630 |
|
| 631 |
SparsityBuilder<M> sparsityBuilder(*coarseMatrix); |
| 632 |
|
| 633 |
ConnectivityConstructor<G,SequentialInformation>::examine(fineGraph, visitedMap, pinfo, |
| 634 |
aggregates, sparsityBuilder); |
| 635 |
dinfo<<"Matrix row: min="<<sparsityBuilder.minRowSize()<<" max=" |
| 636 |
<<sparsityBuilder.maxRowSize()<<" average=" |
| 637 |
<<static_cast<double>(sparsityBuilder.sumRowSize())/coarseMatrix->N()<<std::endl; |
| 638 |
return coarseMatrix; |
| 639 |
} |
| 640 |
|
| 641 |
template<class M, class V, class P, class O> |
| 642 |
void BaseGalerkinProduct::calculate(const M& fine, const AggregatesMap<V>& aggregates, M& coarse, |
| 643 |
const P& pinfo, [[maybe_unused]] const O& copy) |
| 644 |
{ |
| 645 |
coarse = static_cast<typename M::field_type>(0); |
| 646 |
|
| 647 |
typedef typename M::ConstIterator RowIterator; |
| 648 |
RowIterator endRow = fine.end(); |
| 649 |
|
| 650 |
for(RowIterator row = fine.begin(); row != endRow; ++row) |
| 651 |
if(aggregates[row.index()] != AggregatesMap<V>::ISOLATED) { |
| 652 |
assert(aggregates[row.index()]!=AggregatesMap<V>::UNAGGREGATED); |
| 653 |
typedef typename M::ConstColIterator ColIterator; |
| 654 |
ColIterator endCol = row->end(); |
| 655 |
|
| 656 |
for(ColIterator col = row->begin(); col != endCol; ++col) |
| 657 |
if(aggregates[col.index()] != AggregatesMap<V>::ISOLATED) { |
| 658 |
assert(aggregates[row.index()]!=AggregatesMap<V>::UNAGGREGATED); |
| 659 |
coarse[aggregates[row.index()]][aggregates[col.index()]]+=*col; |
| 660 |
} |
| 661 |
} |
| 662 |
|
| 663 |
// get the right diagonal matrix values on copy lines from owner processes |
| 664 |
typedef typename M::block_type BlockType; |
| 665 |
std::vector<BlockType> rowsize(coarse.N(),BlockType(0)); |
| 666 |
for (RowIterator row = coarse.begin(); row != coarse.end(); ++row) |
| 667 |
rowsize[row.index()]=coarse[row.index()][row.index()]; |
| 668 |
pinfo.copyOwnerToAll(rowsize,rowsize); |
| 669 |
for (RowIterator row = coarse.begin(); row != coarse.end(); ++row) |
| 670 |
coarse[row.index()][row.index()] = rowsize[row.index()]; |
| 671 |
|
| 672 |
// don't set dirichlet boundaries for copy lines to make novlp case work, |
| 673 |
// the preconditioner yields slightly different results now. |
| 674 |
|
| 675 |
// Set the dirichlet border |
| 676 |
//DirichletBoundarySetter<P>::template set<M>(coarse, pinfo, copy); |
| 677 |
|
| 678 |
} |
| 679 |
|
| 680 |
template<class T> |
| 681 |
template<class M, class O> |
| 682 |
void DirichletBoundarySetter<T>::set(M& coarse, const T& pinfo, const O& copy) |
| 683 |
{ |
| 684 |
typedef typename T::ParallelIndexSet::const_iterator ConstIterator; |
| 685 |
ConstIterator end = pinfo.indexSet().end(); |
| 686 |
typedef typename M::block_type Block; |
| 687 |
Block identity=Block(0.0); |
| 688 |
for(typename Block::RowIterator b=identity.begin(); b != identity.end(); ++b) |
| 689 |
b->operator[](b.index())=1.0; |
| 690 |
|
| 691 |
for(ConstIterator index = pinfo.indexSet().begin(); |
| 692 |
index != end; ++index) { |
| 693 |
if(copy.contains(index->local().attribute())) { |
| 694 |
typedef typename M::ColIterator ColIterator; |
| 695 |
typedef typename M::row_type Row; |
| 696 |
Row row = coarse[index->local()]; |
| 697 |
ColIterator cend = row.find(index->local()); |
| 698 |
ColIterator col = row.begin(); |
| 699 |
for(; col != cend; ++col) |
| 700 |
*col = 0; |
| 701 |
|
| 702 |
cend = row.end(); |
| 703 |
|
| 704 |
assert(col != cend); // There should be a diagonal entry |
| 705 |
*col = identity; |
| 706 |
|
| 707 |
for(++col; col != cend; ++col) |
| 708 |
*col = 0; |
| 709 |
} |
| 710 |
} |
| 711 |
} |
| 712 |
|
| 713 |
template<class M, class O> |
| 714 |
void DirichletBoundarySetter<SequentialInformation>::set(M& coarse, |
| 715 |
const SequentialInformation& pinfo, |
| 716 |
const O& overlap) |
| 717 |
{} |
| 718 |
|
| 719 |
} // namespace Amg |
| 720 |
} // namespace Dune |
| 721 |
#endif |