5#ifndef DUNE_AMG_AGGREGATES_HH
6#define DUNE_AMG_AGGREGATES_HH
12#include "combinedfunctor.hh"
86 this->setMaxDistance(diameter-1);
91 this->setMaxDistance(this->maxDistance()+diameter-1);
93 this->setMinAggregateSize(csize);
94 this->setMaxAggregateSize(
static_cast<std::size_t
>(csize*1.5));
110 this->setMaxDistance(this->maxDistance()+dim-1);
115 std::ostream& operator<<(std::ostream& os,
const AggregationCriterion<T>& criterion)
117 os<<
"{ maxdistance="<<criterion.maxDistance()<<
" minAggregateSize="
118 <<criterion.minAggregateSize()<<
" maxAggregateSize="<<criterion.maxAggregateSize()
119 <<
" connectivity="<<criterion.maxConnectivity()<<
" debugLevel="<<criterion.debugLevel()<<
"}";
134 template<
class M,
class N>
158 void init(
const Matrix* matrix);
160 void initRow(
const Row& row,
int index);
162 void examine(
const ColIter& col);
165 void examine(G& graph,
const typename G::EdgeIterator& edge,
const ColIter& col);
182 typedef typename FieldTraits<field_type>::real_type real_type;
190 std::vector<real_type> vals_;
191 typename std::vector<real_type>::iterator valIter_;
196 template<
class M,
class N>
202 template<
class M,
class N>
203 inline void SymmetricMatrixDependency<M,N>::initRow(
const Row& row,
int index)
206 vals_.assign(row.size(), 0.0);
207 assert(vals_.size()==row.size());
208 valIter_=vals_.begin();
211 diagonal_=norm_(row[index]);
215 template<
class M,
class N>
216 inline void SymmetricMatrixDependency<M,N>::examine(
const ColIter& col)
220 real_type eij = norm_(*col);
221 if(!N::is_sign_preserving || eij<0)
223 *valIter_ = eij/diagonal_*eij/norm_(matrix_->operator[](col.index())[col.index()]);
224 maxValue_ =
max(maxValue_, *valIter_);
230 template<
class M,
class N>
232 inline void SymmetricMatrixDependency<M,N>::examine(G&,
const typename G::EdgeIterator& edge,
const ColIter&)
234 if(*valIter_ > alpha() * maxValue_) {
235 edge.properties().setDepends();
236 edge.properties().setInfluences();
241 template<
class M,
class N>
242 inline bool SymmetricMatrixDependency<M,N>::isIsolated()
246 valIter_=vals_.begin();
247 return maxValue_ < beta();
253 template<
class M,
class N>
277 void init(
const Matrix* matrix);
279 void initRow(
const Row& row,
int index);
281 void examine(
const ColIter& col);
284 void examine(G& graph,
const typename G::EdgeIterator& edge,
const ColIter& col);
301 typedef typename FieldTraits<field_type>::real_type real_type;
314 template<
class M,
class N>
338 void init(
const Matrix* matrix);
340 void initRow(
const Row& row,
int index);
342 void examine(
const ColIter& col);
345 void examine(G& graph,
const typename G::EdgeIterator& edge,
const ColIter& col);
362 typedef typename FieldTraits<field_type>::real_type real_type;
371 void initRow(
const Row& row,
int index,
const std::true_type&);
372 void initRow(
const Row& row,
int index,
const std::false_type&);
384 is_sign_preserving =
true
392 typename FieldTraits<M>::real_type
operator()(
const M& m)
const
394 using field_type =
typename FieldTraits<M>::field_type;
395 using real_type =
typename FieldTraits<field_type>::real_type;
396 static_assert( std::is_convertible_v<field_type, real_type >,
397 "use of diagonal norm in AMG not implemented for complex field_type");
398 auto&& mm = Impl::asMatrix(m);
399#ifdef DUNE_ISTL_WITH_CHECKING
400 if (mm.N() <= N || mm.M() <= N)
402 "Diagonal<" << N <<
"> norm requested entry (" << N <<
"," << N <<
") for block of size "
403 <<
"(" << mm.N() <<
"x" << mm.M() <<
").");
413 static T signed_abs(
const T & v)
420 static T signed_abs(
const std::complex<T> & v)
424 return csgn(v) * std::abs(v);
429 static T csgn(
const T & v)
431 return (T(0) < v) - (v < T(0));
436 static T csgn(std::complex<T> a)
438 return csgn(a.real())+(a.real() == 0.0)*csgn(a.imag());
459 is_sign_preserving =
false
466 typename FieldTraits<M>::real_type
operator()(
const M& m)
const
468 return Impl::asMatrix(m).infinity_norm();
476 is_sign_preserving =
false
483 typename FieldTraits<M>::real_type
operator()(
const M& m)
const
485 return Impl::asMatrix(m).frobenius_norm();
492 is_sign_preserving =
false
499 typename FieldTraits<M>::real_type
operator()(
const M& )
const
510 template<
class M,
class Norm>
530 template<
class M,
class Norm>
593 template<
class EdgeIterator>
594 void operator()([[maybe_unused]]
const EdgeIterator& edge)
const
627 template<
class M,
class G,
class C>
628 std::tuple<int,int,int,int>
buildAggregates(
const M& matrix, G& graph,
const C& criterion,
648 template<
bool reset,
class G,
class F,
class VM>
653 VM& visitedMap)
const;
678 template<
bool remove,
bool reset,
class G,
class L,
class F1,
class F2,
class VM>
681 const G& graph, L& visited, F1& aggregateVisitor,
682 F2& nonAggregateVisitor,
683 VM& visitedMap)
const;
718 const_iterator begin()
const
723 const_iterator end()
const
743 AggregatesMap<V>& operator=(
const AggregatesMap<V>&) =
delete;
753 std::size_t noVertices_;
759 template<
class G,
class C>
761 const typename C::Matrix& matrix,
769 template<
class G,
class S>
778 typedef G MatrixGraph;
818 VertexSet& connectivity, std::vector<Vertex>& front_);
899 std::vector<Vertex>& front_;
949 template<
class M,
class C>
950 std::tuple<int,int,int,int>
build(
const M& m, G& graph,
968 typedef std::set<Vertex,std::less<Vertex>,
Allocator> VertexSet;
973 typedef std::size_t* SphereMap;
988 std::vector<Vertex> front_;
993 VertexSet connected_;
1006 static const Vertex NullEntry;
1014 enum { N = 1300000 };
1048 const AggregatesMap<Vertex>& aggregates,
1056 class AggregateVisitor
1116 class FrontNeighbourCounter :
public Counter
1140 class TwoWayCounter :
public Counter
1163 class OneWayCounter :
public Counter
1181 const AggregatesMap<Vertex>& aggregates)
const;
1189 class ConnectivityCounter :
public Counter
1203 const VertexSet& connected_;
1248 class DependencyCounter :
public Counter
1280 std::vector<Vertex>& front_;
1395 template<
class M,
class N>
1401 template<
class M,
class N>
1402 inline void SymmetricDependency<M,N>::initRow(
const Row& row,
int index)
1404 initRow(row, index, std::is_convertible<field_type, real_type>());
1407 template<
class M,
class N>
1408 inline void SymmetricDependency<M,N>::initRow(
const Row& ,
int ,
const std::false_type&)
1410 DUNE_THROW(InvalidStateException,
"field_type needs to convertible to real_type");
1413 template<
class M,
class N>
1414 inline void SymmetricDependency<M,N>::initRow(
const Row& ,
int index,
const std::true_type&)
1419 diagonal_ = norm_(matrix_->operator[](row_)[row_]);
1422 template<
class M,
class N>
1423 inline void SymmetricDependency<M,N>::examine(
const ColIter& col)
1426 real_type eij = norm_(*col);
1428 matrix_->operator[](col.index()).find(row_);
1429 if ( opposite_entry == matrix_->operator[](col.index()).end() )
1434 real_type eji = norm_(*opposite_entry);
1437 if(!N::is_sign_preserving || eij<0 || eji<0)
1438 maxValue_ =
max(maxValue_,
1439 eij /diagonal_ * eji/
1440 norm_(matrix_->operator[](col.index())[col.index()]));
1443 template<
class M,
class N>
1445 inline void SymmetricDependency<M,N>::examine(G& graph,
const typename G::EdgeIterator& edge,
const ColIter& col)
1447 real_type eij = norm_(*col);
1449 matrix_->operator[](col.index()).find(row_);
1451 if ( opposite_entry == matrix_->operator[](col.index()).end() )
1456 real_type eji = norm_(*opposite_entry);
1458 if(!N::is_sign_preserving || (eij<0 || eji<0))
1459 if(eji / norm_(matrix_->operator[](edge.target())[edge.target()]) *
1460 eij/ diagonal_ > alpha() * maxValue_) {
1461 edge.properties().setDepends();
1462 edge.properties().setInfluences();
1463 typename G::EdgeProperties& other = graph.getEdgeProperties(edge.target(), edge.source());
1464 other.setInfluences();
1469 template<
class M,
class N>
1470 inline bool SymmetricDependency<M,N>::isIsolated()
1472 return maxValue_ < beta();
1476 template<
class M,
class N>
1477 inline void Dependency<M,N>::init(
const Matrix* matrix)
1482 template<
class M,
class N>
1483 inline void Dependency<M,N>::initRow([[maybe_unused]]
const Row& row,
int index)
1488 diagonal_ = norm_(matrix_->operator[](row_)[row_]);
1491 template<
class M,
class N>
1492 inline void Dependency<M,N>::examine(
const ColIter& col)
1495 maxValue_ =
max(maxValue_, -norm_(*col));
1498 template<
class M,
class N>
1500 inline void Dependency<M,N>::examine(G& graph,
const typename G::EdgeIterator& edge,
const ColIter& col)
1502 if(-norm_(*col) >= maxValue_ * alpha()) {
1503 edge.properties().setDepends();
1504 typedef typename G::EdgeDescriptor ED;
1505 ED e= graph.findEdge(edge.target(), edge.source());
1508 typename G::EdgeProperties& other = graph.getEdgeProperties(e);
1509 other.setInfluences();
1514 template<
class M,
class N>
1515 inline bool Dependency<M,N>::isIsolated()
1517 return maxValue_ < beta() * diagonal_;
1520 template<
class G,
class S>
1522 VertexSet& connected, std::vector<Vertex>&
front)
1523 : vertices_(), id_(-1), graph_(graph), aggregates_(aggregates),
1524 connected_(connected), front_(
front)
1527 template<
class G,
class S>
1535 throw "Not yet implemented";
1543 template<
class G,
class S>
1546 dvverb<<
"Connected cleared"<<std::endl;
1549 connected_.insert(
vertex);
1550 dvverb <<
" Inserting "<<
vertex<<
" size="<<connected_.size();
1556 template<
class G,
class S>
1559 vertices_.insert(
vertex);
1562 front_.erase(std::lower_bound(front_.begin(), front_.end(),
vertex));
1566 const iterator end = graph_.endEdges(
vertex);
1567 for(iterator edge = graph_.beginEdges(
vertex); edge != end; ++edge) {
1568 dvverb <<
" Inserting "<<aggregates_[edge.target()];
1569 connected_.insert(aggregates_[edge.target()]);
1570 dvverb <<
" size="<<connected_.size();
1572 !graph_.getVertexProperties(edge.target()).front())
1574 front_.push_back(edge.target());
1575 graph_.getVertexProperties(edge.target()).setFront();
1579 std::sort(front_.begin(), front_.end());
1582 template<
class G,
class S>
1586 std::size_t oldsize = vertices_.size();
1588 typedef typename std::vector<Vertex>::iterator Iterator;
1590 typedef typename VertexSet::iterator SIterator;
1592 SIterator pos=vertices_.begin();
1593 std::vector<Vertex> newFront;
1594 newFront.reserve(front_.capacity());
1596 std::set_difference(front_.begin(), front_.end(), vertices.begin(), vertices.end(),
1597 std::back_inserter(newFront));
1602 pos=vertices_.insert(pos,*
vertex);
1603 vertices_.insert(*
vertex);
1604 graph_.getVertexProperties(*vertex).resetFront();
1605 aggregates_[*
vertex]=id_;
1608 const iterator end = graph_.endEdges(*
vertex);
1609 for(iterator edge = graph_.beginEdges(*
vertex); edge != end; ++edge) {
1610 dvverb <<
" Inserting "<<aggregates_[edge.target()];
1611 connected_.insert(aggregates_[edge.target()]);
1613 !graph_.getVertexProperties(edge.target()).front())
1615 front_.push_back(edge.target());
1616 graph_.getVertexProperties(edge.target()).setFront();
1618 dvverb <<
" size="<<connected_.size();
1622 std::sort(front_.begin(), front_.end());
1623 assert(oldsize+vertices.size()==vertices_.size());
1625 template<
class G,
class S>
1633 template<
class G,
class S>
1637 return vertices_.size();
1640 template<
class G,
class S>
1644 return connected_.size();
1647 template<
class G,
class S>
1653 template<
class G,
class S>
1656 return vertices_.begin();
1659 template<
class G,
class S>
1662 return vertices_.end();
1680 delete[] aggregates_;
1687 allocate(noVertices);
1700 noVertices_ = noVertices;
1702 for(std::size_t i=0; i < noVertices; i++)
1703 aggregates_[i]=UNAGGREGATED;
1709 assert(aggregates_ != 0);
1710 delete[] aggregates_;
1718 return aggregates_[v];
1725 return aggregates_[v];
1729 template<
bool reset,
class G,
class F,
class VM>
1732 const G& graph, F& aggregateVisitor,
1733 VM& visitedMap)
const
1737 DummyEdgeVisitor dummy;
1738 return breadthFirstSearch<true,reset>(start, aggregate, graph, vlist, aggregateVisitor, dummy, visitedMap);
1742 template<
bool remove,
bool reset,
class G,
class L,
class F1,
class F2,
class VM>
1747 F1& aggregateVisitor,
1748 F2& nonAggregateVisitor,
1749 VM& visitedMap)
const
1751 typedef typename L::const_iterator ListIterator;
1752 int visitedSpheres = 0;
1754 visited.push_back(start);
1755 put(visitedMap, start,
true);
1757 ListIterator current = visited.begin();
1758 ListIterator end = visited.end();
1759 std::size_t i=0,
size=visited.size();
1763 while(current != end) {
1765 for(; i<
size; ++current, ++i) {
1766 typedef typename G::ConstEdgeIterator EdgeIterator;
1767 const EdgeIterator endEdge = graph.endEdges(*current);
1769 for(EdgeIterator edge = graph.beginEdges(*current);
1770 edge != endEdge; ++edge) {
1772 if(aggregates_[edge.target()]==aggregate) {
1773 if(!
get(visitedMap, edge.target())) {
1774 put(visitedMap, edge.target(),
true);
1775 visited.push_back(edge.target());
1776 aggregateVisitor(edge);
1779 nonAggregateVisitor(edge);
1782 end = visited.end();
1783 size = visited.size();
1789 for(current = visited.begin(); current != end; ++current)
1790 put(visitedMap, *current,
false);
1796 return visitedSpheres;
1801 : graph_(0), aggregate_(0), front_(), connected_(), size_(-1)
1810 template<
class G,
class C>
1812 const typename C::Matrix& matrix,
1813 C criterion,
bool firstlevel)
1816 typedef typename C::Matrix Matrix;
1817 typedef typename G::VertexIterator VertexIterator;
1819 criterion.init(&matrix);
1824 const Row& row = matrix[*
vertex];
1829 criterion.initRow(row, *
vertex);
1834 ColIterator end = row.end();
1835 typename FieldTraits<typename Matrix::field_type>::real_type absoffdiag=0.;
1839 for(ColIterator col = row.begin(); col != end; ++col)
1840 if(col.index()!=*
vertex) {
1841 criterion.examine(col);
1842 absoffdiag =
max(absoffdiag, Impl::asMatrix(*col).frobenius_norm());
1846 vertex.properties().setExcludedBorder();
1849 for(ColIterator col = row.begin(); col != end; ++col)
1851 criterion.examine(col);
1857 if(criterion.isIsolated()) {
1859 vertex.properties().setIsolated();
1862 auto eEnd =
vertex.end();
1863 auto col = matrix[*
vertex].begin();
1865 for(
auto edge =
vertex.begin(); edge!= eEnd; ++edge, ++col) {
1867 while(col.index()!=edge.target())
1869 criterion.examine(graph, edge, col);
1879 inline Aggregator<G>::AggregateVisitor<V>::AggregateVisitor(
const AggregatesMap<Vertex>& aggregates,
1881 : aggregates_(aggregates), aggregate_(aggregate), visitor_(&visitor)
1888 if(aggregates_[edge.target()]==aggregate_)
1889 visitor_->operator()(edge);
1894 inline void Aggregator<G>::visitAggregateNeighbours(
const Vertex&
vertex,
1896 const AggregatesMap<Vertex>& aggregates,
1900 AggregateVisitor<V> v(aggregates, aggregate, visitor);
1906 inline Aggregator<G>::Counter::Counter()
1911 inline void Aggregator<G>::Counter::increment()
1917 inline void Aggregator<G>::Counter::decrement()
1922 inline int Aggregator<G>::Counter::value()
1930 if(edge.properties().isTwoWay())
1931 Counter::increment();
1936 const AggregatesMap<Vertex>& aggregates)
const
1938 TwoWayCounter counter;
1939 visitAggregateNeighbours(
vertex, aggregate, aggregates, counter);
1940 return counter.value();
1945 const AggregatesMap<Vertex>& aggregates)
const
1947 OneWayCounter counter;
1948 visitAggregateNeighbours(
vertex, aggregate, aggregates, counter);
1949 return counter.value();
1955 if(edge.properties().isOneWay())
1956 Counter::increment();
1960 inline Aggregator<G>::ConnectivityCounter::ConnectivityCounter(
const VertexSet& connected,
1961 const AggregatesMap<Vertex>& aggregates)
1962 : Counter(), connected_(connected), aggregates_(aggregates)
1971 Counter::increment();
1973 Counter::increment();
1974 Counter::increment();
1979 inline double Aggregator<G>::connectivity(
const Vertex&
vertex,
const AggregatesMap<Vertex>& aggregates)
const
1981 ConnectivityCounter counter(connected_, aggregates);
1983 return (
double)counter.value()/noNeighbours;
1987 inline Aggregator<G>::DependencyCounter::DependencyCounter()
1994 if(edge.properties().depends())
1995 Counter::increment();
1996 if(edge.properties().influences())
1997 Counter::increment();
2001 int Aggregator<G>::unusedNeighbours(
const Vertex&
vertex,
const AggregatesMap<Vertex>& aggregates)
const
2007 std::pair<int,int> Aggregator<G>::neighbours(
const Vertex&
vertex,
2009 const AggregatesMap<Vertex>& aggregates)
const
2011 DependencyCounter unused, aggregated;
2012 typedef AggregateVisitor<DependencyCounter> CounterT;
2013 typedef std::tuple<CounterT,CounterT> CounterTuple;
2016 return std::make_pair(unused.value(), aggregated.value());
2023 DependencyCounter counter;
2024 visitAggregateNeighbours(
vertex, aggregate, aggregates, counter);
2025 return counter.value();
2029 std::size_t Aggregator<G>::distance(
const Vertex&
vertex,
const AggregatesMap<Vertex>& aggregates)
2032 typename PropertyMapTypeSelector<VertexVisitedTag,G>::Type visitedMap =
get(VertexVisitedTag(), *graph_);
2034 typename AggregatesMap<Vertex>::DummyEdgeVisitor dummy;
2035 return aggregates.template breadthFirstSearch<true,true>(
vertex,
2036 aggregate_->
id(), *graph_,
2037 vlist, dummy, dummy, visitedMap);
2041 inline Aggregator<G>::FrontMarker::FrontMarker(std::vector<Vertex>&
front,
MatrixGraph& graph)
2042 : front_(
front), graph_(graph)
2048 Vertex target = edge.target();
2050 if(!graph_.getVertexProperties(target).front()) {
2051 front_.push_back(target);
2052 graph_.getVertexProperties(target).setFront();
2060 Dune::dvverb<<
" Admissible not yet implemented!"<<std::endl;
2067 Iterator vend = graph_->endEdges(
vertex);
2068 for(Iterator edge = graph_->beginEdges(
vertex); edge != vend; ++edge) {
2070 if(edge.properties().isStrong()
2071 && aggregates[edge.target()]==aggregate)
2074 Iterator edge1 = edge;
2075 for(++edge1; edge1 != vend; ++edge1) {
2077 if(edge1.properties().isStrong()
2078 && aggregates[edge.target()]==aggregate)
2083 Iterator v2end = graph_->endEdges(edge.target());
2084 for(Iterator edge2 = graph_->beginEdges(edge.target()); edge2 != v2end; ++edge2) {
2085 if(edge2.target()==edge1.target() &&
2086 edge2.properties().isStrong()) {
2102 vend = graph_->endEdges(
vertex);
2103 for(Iterator edge = graph_->beginEdges(
vertex); edge != vend; ++edge) {
2105 if(edge.properties().isStrong()
2106 && aggregates[edge.target()]==aggregate)
2109 Iterator v1end = graph_->endEdges(edge.target());
2111 for(Iterator edge1=graph_->beginEdges(edge.target()); edge1 != v1end; ++edge1) {
2113 if(edge1.properties().isStrong()
2114 && aggregates[edge1.target()]==aggregate)
2118 Iterator v2end = graph_->endEdges(
vertex);
2119 for(Iterator edge2 = graph_->beginEdges(
vertex); edge2 != v2end; ++edge2) {
2120 if(edge2.target()==edge1.target()) {
2121 if(edge2.properties().isStrong())
2138 void Aggregator<G>::unmarkFront()
2140 typedef typename std::vector<Vertex>::const_iterator Iterator;
2143 graph_->getVertexProperties(*vertex).resetFront();
2150 Aggregator<G>::nonisoNeighbourAggregate(
const Vertex&
vertex,
2151 const AggregatesMap<Vertex>& aggregates,
2152 SLList<Vertex>& neighbours)
const
2155 Iterator end=graph_->beginEdges(
vertex);
2158 for(Iterator edge=graph_->beginEdges(
vertex); edge!=end; ++edge)
2161 neighbours.push_back(aggregates[edge.target()]);
2166 inline typename G::VertexDescriptor Aggregator<G>::mergeNeighbour(
const Vertex&
vertex,
const AggregatesMap<Vertex>& aggregates)
const
2170 Iterator end = graph_->endEdges(
vertex);
2171 for(Iterator edge = graph_->beginEdges(
vertex); edge != end; ++edge) {
2173 graph_->getVertexProperties(edge.target()).isolated() == graph_->getVertexProperties(edge.source()).isolated()) {
2174 if( graph_->getVertexProperties(
vertex).isolated() ||
2175 ((edge.properties().depends() || edge.properties().influences())
2176 && admissible(
vertex, aggregates[edge.target()], aggregates)))
2177 return edge.target();
2184 Aggregator<G>::FrontNeighbourCounter::FrontNeighbourCounter(
const MatrixGraph& graph)
2185 : Counter(), graph_(graph)
2191 if(graph_.getVertexProperties(edge.target()).front())
2192 Counter::increment();
2196 int Aggregator<G>::noFrontNeighbours(
const Vertex&
vertex)
const
2198 FrontNeighbourCounter counter(*graph_);
2200 return counter.value();
2203 inline bool Aggregator<G>::connected(
const Vertex&
vertex,
2205 const AggregatesMap<Vertex>& aggregates)
const
2207 typedef typename G::ConstEdgeIterator iterator;
2208 const iterator end = graph_->endEdges(
vertex);
2209 for(iterator edge = graph_->beginEdges(
vertex); edge != end; ++edge)
2210 if(aggregates[edge.target()]==aggregate)
2215 inline bool Aggregator<G>::connected(
const Vertex&
vertex,
2216 const SLList<AggregateDescriptor>& aggregateList,
2217 const AggregatesMap<Vertex>& aggregates)
const
2220 for(Iter i=aggregateList.begin(); i!=aggregateList.end(); ++i)
2221 if(connected(
vertex, *i, aggregates))
2228 void Aggregator<G>::growIsolatedAggregate(
const Vertex& seed,
const AggregatesMap<Vertex>& aggregates,
const C& c)
2230 SLList<Vertex> connectedAggregates;
2231 nonisoNeighbourAggregate(seed, aggregates,connectedAggregates);
2233 while(aggregate_->
size()< c.minAggregateSize() && aggregate_->
connectSize() < c.maxConnectivity()) {
2235 std::size_t maxFrontNeighbours=0;
2239 typedef typename std::vector<Vertex>::const_iterator Iterator;
2242 if(distance(*
vertex, aggregates)>c.maxDistance())
2245 if(connectedAggregates.size()>0) {
2249 if(!connected(*
vertex, connectedAggregates, aggregates))
2253 double con = connectivity(*
vertex, aggregates);
2256 std::size_t frontNeighbours = noFrontNeighbours(*
vertex);
2258 if(frontNeighbours >= maxFrontNeighbours) {
2259 maxFrontNeighbours = frontNeighbours;
2262 }
else if(con > maxCon) {
2264 maxFrontNeighbours = noFrontNeighbours(*
vertex);
2272 aggregate_->
add(candidate);
2278 void Aggregator<G>::growAggregate(
const Vertex& seed,
const AggregatesMap<Vertex>& aggregates,
const C& c)
2282 std::size_t distance_ =0;
2283 while(aggregate_->
size() < c.minAggregateSize()&& distance_<c.maxDistance()) {
2284 int maxTwoCons=0, maxOneCons=0, maxNeighbours=-1;
2287 std::vector<Vertex> candidates;
2288 candidates.reserve(30);
2290 typedef typename std::vector<Vertex>::const_iterator Iterator;
2294 if(graph_->getVertexProperties(*vertex).isolated())
2297 int twoWayCons = twoWayConnections(*
vertex, aggregate_->
id(), aggregates);
2300 if( maxTwoCons == twoWayCons && twoWayCons > 0) {
2301 double con = connectivity(*
vertex, aggregates);
2304 int neighbours = noFrontNeighbours(*
vertex);
2306 if(neighbours > maxNeighbours) {
2307 maxNeighbours = neighbours;
2309 candidates.push_back(*
vertex);
2311 candidates.push_back(*
vertex);
2313 }
else if( con > maxCon) {
2315 maxNeighbours = noFrontNeighbours(*
vertex);
2317 candidates.push_back(*
vertex);
2319 }
else if(twoWayCons > maxTwoCons) {
2320 maxTwoCons = twoWayCons;
2321 maxCon = connectivity(*
vertex, aggregates);
2322 maxNeighbours = noFrontNeighbours(*
vertex);
2324 candidates.push_back(*
vertex);
2336 int oneWayCons = oneWayConnections(*
vertex, aggregate_->
id(), aggregates);
2341 if(!admissible(*
vertex, aggregate_->
id(), aggregates))
2344 if( maxOneCons == oneWayCons && oneWayCons > 0) {
2345 double con = connectivity(*
vertex, aggregates);
2348 int neighbours = noFrontNeighbours(*
vertex);
2350 if(neighbours > maxNeighbours) {
2351 maxNeighbours = neighbours;
2353 candidates.push_back(*
vertex);
2355 if(neighbours==maxNeighbours)
2357 candidates.push_back(*
vertex);
2360 }
else if( con > maxCon) {
2362 maxNeighbours = noFrontNeighbours(*
vertex);
2364 candidates.push_back(*
vertex);
2366 }
else if(oneWayCons > maxOneCons) {
2367 maxOneCons = oneWayCons;
2368 maxCon = connectivity(*
vertex, aggregates);
2369 maxNeighbours = noFrontNeighbours(*
vertex);
2371 candidates.push_back(*
vertex);
2376 if(!candidates.size())
2378 distance_=distance(seed, aggregates);
2379 candidates.resize(
min(candidates.size(), c.maxAggregateSize()-
2380 aggregate_->
size()));
2381 aggregate_->
add(candidates);
2385 template<
typename V>
2386 template<
typename M,
typename G,
typename C>
2390 Aggregator<G> aggregator;
2391 return aggregator.build(matrix, graph, *
this, criterion, finestLevel);
2395 template<
class M,
class C>
2396 std::tuple<int,int,int,int>
Aggregator<G>::build(
const M& m, G& graph, AggregatesMap<Vertex>& aggregates,
const C& c,
2402 Stack stack_(graph, *
this, aggregates);
2406 aggregate_ =
new Aggregate<G,VertexSet>(graph, aggregates, connected_, front_);
2413 dverb<<
"Build dependency took "<< watch.elapsed()<<
" seconds."<<std::endl;
2414 int noAggregates, conAggregates, isoAggregates, oneAggregates;
2415 std::size_t maxA=0, minA=1000000, avg=0;
2416 int skippedAggregates;
2417 noAggregates = conAggregates = isoAggregates = oneAggregates =
2418 skippedAggregates = 0;
2421 Vertex seed = stack_.pop();
2423 if(seed == Stack::NullEntry)
2428 if((noAggregates+1)%10000 == 0)
2432 if(graph.getVertexProperties(seed).excludedBorder()) {
2434 ++skippedAggregates;
2438 if(graph.getVertexProperties(seed).isolated()) {
2439 if(c.skipIsolated()) {
2442 ++skippedAggregates;
2446 aggregate_->
seed(seed);
2447 growIsolatedAggregate(seed, aggregates, c);
2450 aggregate_->
seed(seed);
2451 growAggregate(seed, aggregates, c);
2455 while(!(graph.getVertexProperties(seed).isolated()) && aggregate_->
size() < c.maxAggregateSize()) {
2457 std::vector<Vertex> candidates;
2458 candidates.reserve(30);
2460 typedef typename std::vector<Vertex>::const_iterator Iterator;
2464 if(graph.getVertexProperties(*vertex).isolated())
2467 if(twoWayConnections( *
vertex, aggregate_->
id(), aggregates) == 0 &&
2468 (oneWayConnections( *
vertex, aggregate_->
id(), aggregates) == 0 ||
2469 !admissible( *
vertex, aggregate_->
id(), aggregates) ))
2472 std::pair<int,int> neighbourPair=neighbours(*
vertex, aggregate_->
id(),
2478 if(neighbourPair.first >= neighbourPair.second)
2481 if(distance(*
vertex, aggregates) > c.maxDistance())
2483 candidates.push_back(*
vertex);
2487 if(!candidates.size())
break;
2489 candidates.resize(
min(candidates.size(), c.maxAggregateSize()-
2490 aggregate_->
size()));
2491 aggregate_->
add(candidates);
2496 if(aggregate_->
size()==1 && c.maxAggregateSize()>1) {
2497 if(!graph.getVertexProperties(seed).isolated()) {
2498 Vertex mergedNeighbour = mergeNeighbour(seed, aggregates);
2502 aggregates[seed] = aggregates[mergedNeighbour];
2503 aggregate_->invalidate();
2506 minA=
min(minA,
static_cast<std::size_t
>(1));
2507 maxA=
max(maxA,
static_cast<std::size_t
>(1));
2513 minA=
min(minA,
static_cast<std::size_t
>(1));
2514 maxA=
max(maxA,
static_cast<std::size_t
>(1));
2520 avg+=aggregate_->
size();
2521 minA=
min(minA,aggregate_->
size());
2522 maxA=
max(maxA,aggregate_->
size());
2523 if(graph.getVertexProperties(seed).isolated())
2531 Dune::dinfo<<
"connected aggregates: "<<conAggregates;
2532 Dune::dinfo<<
" isolated aggregates: "<<isoAggregates;
2533 if(conAggregates+isoAggregates>0)
2534 Dune::dinfo<<
" one node aggregates: "<<oneAggregates<<
" min size="
2535 <<minA<<
" max size="<<maxA
2536 <<
" avg="<<avg/(conAggregates+isoAggregates)<<std::endl;
2539 return std::make_tuple(conAggregates+isoAggregates,isoAggregates,
2540 oneAggregates,skippedAggregates);
2545 Aggregator<G>::Stack::Stack(
const MatrixGraph& graph,
const Aggregator<G>& aggregatesBuilder,
2546 const AggregatesMap<Vertex>& aggregates)
2547 : graph_(graph), aggregatesBuilder_(aggregatesBuilder), aggregates_(aggregates), begin_(graph.begin()), end_(graph.end())
2553 Aggregator<G>::Stack::~Stack()
2564 inline typename G::VertexDescriptor Aggregator<G>::Stack::pop()
2570 typename G::VertexDescriptor current=*begin_;
2580 void printAggregates2d(
const AggregatesMap<V>& aggregates,
int n,
int m, std::ostream& os)
2584 std::ios_base::fmtflags oldOpts=os.flags();
2586 os.setf(std::ios_base::right, std::ios_base::adjustfield);
2591 for(
int i=0; i< n*m; i++)
2592 maxVal=
max(maxVal, aggregates[i]);
2594 for(
int i=10; i < 1000000; i*=10)
2600 for(
int j=0, entry=0; j < m; j++) {
2601 for(
int i=0; i<n; i++, entry++) {
2603 os<<aggregates[entry]<<
" ";
A class for temporarily storing the vertices of an aggregate in.
Definition: aggregates.hh:771
A Dummy visitor that does nothing for each visited edge.
Definition: aggregates.hh:591
Class providing information about the mapping of the vertices onto aggregates.
Definition: aggregates.hh:553
Base class of all aggregation criterions.
Definition: aggregates.hh:51
Class for building the aggregates.
Definition: aggregates.hh:907
Dependency policy for symmetric matrices.
Definition: aggregates.hh:255
Norm that uses only the [N][N] entry of the block to determine couplings.
Definition: aggregates.hh:381
Norm that uses only the [0][0] entry of the block to determine couplings.
Definition: aggregates.hh:448
Iterator over all edges starting from a vertex.
Definition: graph.hh:95
The vertex iterator type of the graph.
Definition: graph.hh:209
The (undirected) graph of a matrix.
Definition: graph.hh:51
M::size_type VertexDescriptor
The vertex descriptor.
Definition: graph.hh:73
EdgeIteratorT< const MatrixGraph< Matrix > > ConstEdgeIterator
The constant edge iterator type.
Definition: graph.hh:298
All parameters for AMG.
Definition: parameters.hh:416
Criterion taking advantage of symmetric matrices.
Definition: aggregates.hh:512
Dependency policy for symmetric matrices.
Definition: aggregates.hh:316
Dependency policy for symmetric matrices.
Definition: aggregates.hh:136
Criterion suitable for unsymmetric matrices.
Definition: aggregates.hh:532
derive error class from the base class in common
Definition: istlexception.hh:19
Default exception if a function was called while the object is not in a valid state for that function...
Definition: exceptions.hh:375
A generic dynamic dense matrix.
Definition: matrix.hh:561
typename Imp::BlockTraits< T >::field_type field_type
Export the type representing the underlying field.
Definition: matrix.hh:565
row_type::const_iterator ConstColIterator
Const iterator for the entries of each row.
Definition: matrix.hh:589
MatrixImp::DenseMatrixBase< T, A >::window_type row_type
The type implementing a matrix row.
Definition: matrix.hh:574
An allocator managing a pool of objects for reuse.
Definition: poolallocator.hh:223
A single linked list.
Definition: sllist.hh:44
Traits for type conversions and type information.
Type traits to determine the type of reals (when working with complex numbers)
Provides classes for building the matrix graph.
SLListConstIterator< T, A > const_iterator
The constant iterator of the list.
Definition: sllist.hh:74
#define DUNE_THROW(E,...)
Definition: exceptions.hh:314
constexpr GeometryType vertex
GeometryType representing a vertex.
Definition: type.hh:492
constexpr auto max
Function object that returns the greater of the given values.
Definition: hybridutilities.hh:489
constexpr auto min
Function object that returns the smaller of the given values.
Definition: hybridutilities.hh:511
Matrix::ConstColIterator ColIter
Constant column iterator of the matrix.
Definition: aggregates.hh:275
Matrix::ConstColIterator ColIter
Constant column iterator of the matrix.
Definition: aggregates.hh:156
std::size_t breadthFirstSearch(const VertexDescriptor &start, const AggregateDescriptor &aggregate, const G &graph, L &visited, F1 &aggregateVisitor, F2 &nonAggregateVisitor, VM &visitedMap) const
Breadth first search within an aggregate.
PoolAllocator< VertexDescriptor, 100 > Allocator
The allocator we use for our lists and the set.
Definition: aggregates.hh:579
FieldTraits< M >::real_type operator()(const M &m) const
compute the norm of a matrix.
Definition: aggregates.hh:392
int id()
Get the id identifying the aggregate.
Norm norm_
The functor for calculating the norm.
Definition: aggregates.hh:304
size_type size()
Get the size of the aggregate.
MatrixGraph::VertexDescriptor Vertex
The vertex identifier.
Definition: aggregates.hh:918
AggregationCriterion()
Constructor.
Definition: aggregates.hh:68
const Matrix * matrix_
The matrix we work on.
Definition: aggregates.hh:359
FieldTraits< M >::real_type operator()(const M &m) const
compute the norm of a matrix.
Definition: aggregates.hh:483
M Matrix
The matrix type we build the dependency of.
Definition: aggregates.hh:260
G MatrixGraph
The matrix graph type used.
Definition: aggregates.hh:913
Norm norm_
The functor for calculating the norm.
Definition: aggregates.hh:365
FieldTraits< M >::real_type operator()(const M &) const
compute the norm of a matrix.
Definition: aggregates.hh:499
typename VertexSet::size_type size_type
Type of size used by allocator of sllist.
Definition: aggregates.hh:799
M Matrix
The matrix type we build the dependency of.
Definition: aggregates.hh:321
real_type diagonal_
The norm of the current diagonal.
Definition: aggregates.hh:189
N Norm
The norm to use for examining the matrix entries.
Definition: aggregates.hh:265
int row_
index of the currently evaluated row.
Definition: aggregates.hh:187
std::tuple< int, int, int, int > build(const M &m, G &graph, AggregatesMap< Vertex > &aggregates, const C &c, bool finestLevel)
Build the aggregates.
FrontNeighbourCounter(const MatrixGraph &front)
Constructor.
Matrix::row_type Row
Constant Row iterator of the matrix.
Definition: aggregates.hh:331
const Matrix * matrix_
The matrix we work on.
Definition: aggregates.hh:298
size_type connectSize()
Get the number of connections to other aggregates.
const AggregateDescriptor & operator[](const VertexDescriptor &v) const
Get the aggregate a vertex belongs to.
AggregateVisitor(const AggregatesMap< Vertex > &aggregates, const AggregateDescriptor &aggregate, Visitor &visitor)
Constructor.
Matrix::ConstColIterator ColIter
Constant column iterator of the matrix.
Definition: aggregates.hh:336
~AggregatesMap()
Destructor.
Matrix::field_type field_type
The current max value.
Definition: aggregates.hh:181
void decrement()
Decrement counter.
Aggregate(MatrixGraph &graph, AggregatesMap< Vertex > &aggregates, VertexSet &connectivity, std::vector< Vertex > &front_)
Constructor.
V Visitor
The type of the adapted visitor.
Definition: aggregates.hh:1062
std::size_t * SphereMap
Type of the mapping of aggregate members onto distance spheres.
Definition: aggregates.hh:807
Matrix::row_type Row
Constant Row iterator of the matrix.
Definition: aggregates.hh:270
N Norm
The norm to use for examining the matrix entries.
Definition: aggregates.hh:326
Norm norm_
The functor for calculating the norm.
Definition: aggregates.hh:185
VertexSet::const_iterator const_iterator
Const iterator over a vertex list.
Definition: aggregates.hh:802
MatrixGraph::VertexDescriptor AggregateDescriptor
The type of the aggregate descriptor.
Definition: aggregates.hh:921
real_type diagonal_
The norm of the current diagonal.
Definition: aggregates.hh:369
void add(const Vertex &vertex)
Add a vertex to the aggregate.
T DependencyPolicy
The policy for calculating the dependency graph.
Definition: aggregates.hh:57
void operator()(const typename MatrixGraph::ConstEdgeIterator &edge)
Examine an edge.
FrontMarker(std::vector< Vertex > &front, MatrixGraph &graph)
Constructor.
int visitNeighbours(const G &graph, const typename G::VertexDescriptor &vertex, V &visitor)
Visit all neighbour vertices of a vertex in a graph.
void setDefaultValuesIsotropic(std::size_t dim, std::size_t diameter=2)
Sets reasonable default values for an isotropic problem.
Definition: aggregates.hh:84
V AggregateDescriptor
The aggregate descriptor type.
Definition: aggregates.hh:573
static const V ISOLATED
Identifier of isolated vertices.
Definition: aggregates.hh:564
int row_
index of the currently evaluated row.
Definition: aggregates.hh:367
DependencyCounter()
Constructor.
real_type diagonal_
The norm of the current diagonal.
Definition: aggregates.hh:308
std::size_t noVertices() const
Get the number of vertices.
void setDefaultValuesAnisotropic(std::size_t dim, std::size_t diameter=2)
Sets reasonable default values for an aisotropic problem.
Definition: aggregates.hh:107
AggregatesMap(std::size_t noVertices)
Constructs with allocating memory.
Matrix::field_type field_type
The current max value.
Definition: aggregates.hh:361
AggregateDescriptor & operator[](const VertexDescriptor &v)
Get the aggregate a vertex belongs to.
AggregatesMap()
Constructs without allocating memory.
int value()
Access the current count.
SLList< VertexDescriptor, Allocator > VertexList
The type of a single linked list of vertex descriptors.
Definition: aggregates.hh:585
ConnectivityCounter(const VertexSet &connected, const AggregatesMap< Vertex > &aggregates)
Constructor.
const_iterator end() const
get an iterator over the vertices of the aggregate.
int row_
index of the currently evaluated row.
Definition: aggregates.hh:306
M Matrix
The matrix type we build the dependency of.
Definition: aggregates.hh:141
const Matrix * matrix_
The matrix we work on.
Definition: aggregates.hh:179
S VertexSet
The type of a single linked list of vertex descriptors.
Definition: aggregates.hh:794
static const V UNAGGREGATED
Identifier of not yet aggregated vertices.
Definition: aggregates.hh:559
std::size_t breadthFirstSearch(const VertexDescriptor &start, const AggregateDescriptor &aggregate, const G &graph, F &aggregateVisitor, VM &visitedMap) const
Breadth first search within an aggregate.
Matrix::field_type field_type
The current max value.
Definition: aggregates.hh:300
void allocate(std::size_t noVertices)
Allocate memory for holding the information.
N Norm
The norm to use for examining the matrix entries.
Definition: aggregates.hh:146
void reconstruct(const Vertex &vertex)
Reconstruct the aggregat from an seed node.
const_iterator begin() const
get an iterator over the vertices of the aggregate.
MatrixGraph::VertexDescriptor Vertex
The vertex descriptor type.
Definition: aggregates.hh:782
void seed(const Vertex &vertex)
Initialize the aggregate with one vertex.
void clear()
Clear the aggregate.
void free()
Free the allocated memory.
void increment()
Increment counter.
void buildDependency(G &graph, const typename C::Matrix &matrix, C criterion, bool finestLevel)
Build the dependency of the matrix graph.
V VertexDescriptor
The vertex descriptor type.
Definition: aggregates.hh:568
std::tuple< int, int, int, int > buildAggregates(const M &matrix, G &graph, const C &criterion, bool finestLevel)
Build the aggregates.
Matrix::row_type Row
Constant Row iterator of the matrix.
Definition: aggregates.hh:151
FieldTraits< M >::real_type operator()(const M &m) const
compute the norm of a matrix.
Definition: aggregates.hh:466
PoolAllocator< Vertex, 100 > Allocator
The allocator we use for our lists and the set.
Definition: aggregates.hh:788
DVVerbType dvverb(std::cout)
stream for very verbose output.
Definition: stdstreams.hh:96
DInfoType dinfo(std::cout)
Stream for informative output.
Definition: stdstreams.hh:141
DVerbType dverb(std::cout)
Singleton of verbose debug stream.
Definition: stdstreams.hh:117
Dune namespace
Definition: alignedallocator.hh:13
constexpr std::integral_constant< std::size_t, sizeof...(II)> size(std::integer_sequence< T, II... >)
Return the size of the sequence.
Definition: integersequence.hh:75
constexpr auto front(const HybridMultiIndex< T... > &tp) -> decltype(tp.front())
Returns a copy of the first element of the HybridMultiIndex.
Definition: hybridmultiindex.hh:217
constexpr auto get(std::integer_sequence< T, II... >, std::integral_constant< std::size_t, pos >={})
Return the entry at position pos of the given sequence.
Definition: integersequence.hh:22
Parameter classes for customizing AMG.
An stl-compliant pool allocator.
Provides classes for handling internal properties in a graph.
Implements a scalar matrix view wrapper around an existing scalar.
Implements a singly linked list together with the necessary iterators.
Standard Dune debug streams.
Functor using the row sum (infinity) norm to determine strong couplings.
Definition: aggregates.hh:456