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<typename M::field_type>::real_type
operator()(
const M& m,
395 typedef typename M::field_type field_type;
396 typedef typename FieldTraits<field_type>::real_type real_type;
397 static_assert( std::is_convertible<field_type, real_type >::value,
398 "use of diagonal norm in AMG not implemented for complex field_type");
411 typedef typename FieldTraits<M>::real_type real_type;
412 static_assert( std::is_convertible<M, real_type >::value,
413 "use of diagonal norm in AMG not implemented for complex field_type");
422 static T signed_abs(
const T & v)
429 static T signed_abs(
const std::complex<T> & v)
433 return csgn(v) * std::abs(v);
438 static T csgn(
const T & v)
440 return (T(0) < v) - (v < T(0));
445 static T csgn(std::complex<T> a)
447 return csgn(a.real())+(a.real() == 0.0)*csgn(a.imag());
468 is_sign_preserving =
false
481 return m.infinity_norm();
489 is_sign_preserving =
false
496 typename FieldTraits<typename M::field_type>::real_type
operator()(
const M& m)
const
498 return m.frobenius_norm();
505 is_sign_preserving =
false
512 typename FieldTraits<typename M::field_type>::real_type
operator()(
const M& m)
const
523 template<
class M,
class Norm>
543 template<
class M,
class Norm>
606 template<
class EdgeIterator>
607 void operator()([[maybe_unused]]
const EdgeIterator& edge)
const
640 template<
class M,
class G,
class C>
641 std::tuple<int,int,int,int>
buildAggregates(
const M& matrix, G& graph,
const C& criterion,
661 template<
bool reset,
class G,
class F,
class VM>
666 VM& visitedMap)
const;
691 template<
bool remove,
bool reset,
class G,
class L,
class F1,
class F2,
class VM>
694 const G& graph, L& visited, F1& aggregateVisitor,
695 F2& nonAggregateVisitor,
696 VM& visitedMap)
const;
731 const_iterator begin()
const
736 const_iterator end()
const
756 AggregatesMap<V>& operator=(
const AggregatesMap<V>&) =
delete;
766 std::size_t noVertices_;
772 template<
class G,
class C>
774 const typename C::Matrix& matrix,
782 template<
class G,
class S>
791 typedef G MatrixGraph;
826 VertexSet& connectivity, std::vector<Vertex>& front_);
860 typename VertexSet::size_type
size();
907 std::vector<Vertex>& front_;
957 template<
class M,
class C>
958 std::tuple<int,int,int,int>
build(
const M& m, G& graph,
976 typedef std::set<Vertex,std::less<Vertex>,
Allocator> VertexSet;
981 typedef std::size_t* SphereMap;
996 std::vector<Vertex> front_;
1001 VertexSet connected_;
1014 static const Vertex NullEntry;
1022 enum { N = 1300000 };
1056 const AggregatesMap<Vertex>& aggregates,
1064 class AggregateVisitor
1124 class FrontNeighbourCounter :
public Counter
1148 class TwoWayCounter :
public Counter
1171 class OneWayCounter :
public Counter
1189 const AggregatesMap<Vertex>& aggregates)
const;
1197 class ConnectivityCounter :
public Counter
1212 const VertexSet& connected_;
1257 class DependencyCounter :
public Counter
1289 std::vector<Vertex>& front_;
1404 template<
class M,
class N>
1410 template<
class M,
class N>
1411 inline void SymmetricDependency<M,N>::initRow(
const Row& row,
int index)
1413 initRow(row, index, std::is_convertible<field_type, real_type>());
1416 template<
class M,
class N>
1417 inline void SymmetricDependency<M,N>::initRow(
const Row& row,
int index,
const std::false_type&)
1419 DUNE_THROW(InvalidStateException,
"field_type needs to convertible to real_type");
1422 template<
class M,
class N>
1423 inline void SymmetricDependency<M,N>::initRow([[maybe_unused]]
const Row& row,
int index,
const std::true_type&)
1428 diagonal_ = norm_(matrix_->operator[](row_)[row_]);
1431 template<
class M,
class N>
1432 inline void SymmetricDependency<M,N>::examine(
const ColIter& col)
1435 real_type eij = norm_(*col);
1437 matrix_->operator[](col.index()).find(row_);
1438 if ( opposite_entry == matrix_->operator[](col.index()).end() )
1443 real_type eji = norm_(*opposite_entry);
1446 if(!N::is_sign_preserving || eij<0 || eji<0)
1447 maxValue_ =
max(maxValue_,
1448 eij /diagonal_ * eji/
1449 norm_(matrix_->operator[](col.index())[col.index()]));
1452 template<
class M,
class N>
1454 inline void SymmetricDependency<M,N>::examine(G& graph,
const typename G::EdgeIterator& edge,
const ColIter& col)
1456 real_type eij = norm_(*col);
1458 matrix_->operator[](col.index()).find(row_);
1460 if ( opposite_entry == matrix_->operator[](col.index()).end() )
1465 real_type eji = norm_(*opposite_entry);
1467 if(!N::is_sign_preserving || (eij<0 || eji<0))
1468 if(eji / norm_(matrix_->operator[](edge.target())[edge.target()]) *
1469 eij/ diagonal_ > alpha() * maxValue_) {
1470 edge.properties().setDepends();
1471 edge.properties().setInfluences();
1472 typename G::EdgeProperties& other = graph.getEdgeProperties(edge.target(), edge.source());
1473 other.setInfluences();
1478 template<
class M,
class N>
1479 inline bool SymmetricDependency<M,N>::isIsolated()
1481 return maxValue_ < beta();
1485 template<
class M,
class N>
1486 inline void Dependency<M,N>::init(
const Matrix* matrix)
1491 template<
class M,
class N>
1492 inline void Dependency<M,N>::initRow([[maybe_unused]]
const Row& row,
int index)
1497 diagonal_ = norm_(matrix_->operator[](row_)[row_]);
1500 template<
class M,
class N>
1501 inline void Dependency<M,N>::examine(
const ColIter& col)
1504 maxValue_ =
max(maxValue_, -norm_(*col));
1507 template<
class M,
class N>
1509 inline void Dependency<M,N>::examine(G& graph,
const typename G::EdgeIterator& edge,
const ColIter& col)
1511 if(-norm_(*col) >= maxValue_ * alpha()) {
1512 edge.properties().setDepends();
1513 typedef typename G::EdgeDescriptor ED;
1514 ED e= graph.findEdge(edge.target(), edge.source());
1517 typename G::EdgeProperties& other = graph.getEdgeProperties(e);
1518 other.setInfluences();
1523 template<
class M,
class N>
1524 inline bool Dependency<M,N>::isIsolated()
1526 return maxValue_ < beta() * diagonal_;
1529 template<
class G,
class S>
1531 VertexSet& connected, std::vector<Vertex>&
front)
1532 : vertices_(), id_(-1), graph_(graph), aggregates_(aggregates),
1533 connected_(connected), front_(
front)
1536 template<
class G,
class S>
1544 throw "Not yet implemented";
1552 template<
class G,
class S>
1555 dvverb<<
"Connected cleared"<<std::endl;
1558 connected_.insert(
vertex);
1559 dvverb <<
" Inserting "<<
vertex<<
" size="<<connected_.size();
1565 template<
class G,
class S>
1568 vertices_.insert(
vertex);
1571 front_.erase(std::lower_bound(front_.begin(), front_.end(),
vertex));
1575 const iterator end = graph_.endEdges(
vertex);
1576 for(iterator edge = graph_.beginEdges(
vertex); edge != end; ++edge) {
1577 dvverb <<
" Inserting "<<aggregates_[edge.target()];
1578 connected_.insert(aggregates_[edge.target()]);
1579 dvverb <<
" size="<<connected_.size();
1581 !graph_.getVertexProperties(edge.target()).front())
1583 front_.push_back(edge.target());
1584 graph_.getVertexProperties(edge.target()).setFront();
1588 std::sort(front_.begin(), front_.end());
1591 template<
class G,
class S>
1595 std::size_t oldsize = vertices_.size();
1597 typedef typename std::vector<Vertex>::iterator Iterator;
1599 typedef typename VertexSet::iterator SIterator;
1601 SIterator pos=vertices_.begin();
1602 std::vector<Vertex> newFront;
1603 newFront.reserve(front_.capacity());
1605 std::set_difference(front_.begin(), front_.end(), vertices.begin(), vertices.end(),
1606 std::back_inserter(newFront));
1611 pos=vertices_.insert(pos,*
vertex);
1612 vertices_.insert(*
vertex);
1613 graph_.getVertexProperties(*vertex).resetFront();
1614 aggregates_[*
vertex]=id_;
1617 const iterator end = graph_.endEdges(*
vertex);
1618 for(iterator edge = graph_.beginEdges(*
vertex); edge != end; ++edge) {
1619 dvverb <<
" Inserting "<<aggregates_[edge.target()];
1620 connected_.insert(aggregates_[edge.target()]);
1622 !graph_.getVertexProperties(edge.target()).front())
1624 front_.push_back(edge.target());
1625 graph_.getVertexProperties(edge.target()).setFront();
1627 dvverb <<
" size="<<connected_.size();
1631 std::sort(front_.begin(), front_.end());
1632 assert(oldsize+vertices.size()==vertices_.size());
1634 template<
class G,
class S>
1642 template<
class G,
class S>
1643 inline typename Aggregate<G,S>::VertexSet::size_type
1646 return vertices_.size();
1649 template<
class G,
class S>
1650 inline typename Aggregate<G,S>::VertexSet::size_type
1653 return connected_.size();
1656 template<
class G,
class S>
1662 template<
class G,
class S>
1665 return vertices_.begin();
1668 template<
class G,
class S>
1671 return vertices_.end();
1689 delete[] aggregates_;
1696 allocate(noVertices);
1709 noVertices_ = noVertices;
1711 for(std::size_t i=0; i < noVertices; i++)
1712 aggregates_[i]=UNAGGREGATED;
1718 assert(aggregates_ != 0);
1719 delete[] aggregates_;
1727 return aggregates_[v];
1734 return aggregates_[v];
1738 template<
bool reset,
class G,
class F,
class VM>
1741 const G& graph, F& aggregateVisitor,
1742 VM& visitedMap)
const
1746 DummyEdgeVisitor dummy;
1747 return breadthFirstSearch<true,reset>(start, aggregate, graph, vlist, aggregateVisitor, dummy, visitedMap);
1751 template<
bool remove,
bool reset,
class G,
class L,
class F1,
class F2,
class VM>
1756 F1& aggregateVisitor,
1757 F2& nonAggregateVisitor,
1758 VM& visitedMap)
const
1760 typedef typename L::const_iterator ListIterator;
1761 int visitedSpheres = 0;
1763 visited.push_back(start);
1764 put(visitedMap, start,
true);
1766 ListIterator current = visited.begin();
1767 ListIterator end = visited.end();
1768 std::size_t i=0,
size=visited.size();
1772 while(current != end) {
1774 for(; i<
size; ++current, ++i) {
1775 typedef typename G::ConstEdgeIterator EdgeIterator;
1776 const EdgeIterator endEdge = graph.endEdges(*current);
1778 for(EdgeIterator edge = graph.beginEdges(*current);
1779 edge != endEdge; ++edge) {
1781 if(aggregates_[edge.target()]==aggregate) {
1782 if(!
get(visitedMap, edge.target())) {
1783 put(visitedMap, edge.target(),
true);
1784 visited.push_back(edge.target());
1785 aggregateVisitor(edge);
1788 nonAggregateVisitor(edge);
1791 end = visited.end();
1792 size = visited.size();
1798 for(current = visited.begin(); current != end; ++current)
1799 put(visitedMap, *current,
false);
1805 return visitedSpheres;
1810 : graph_(0), aggregate_(0), front_(), connected_(), size_(-1)
1819 template<
class G,
class C>
1821 const typename C::Matrix& matrix,
1822 C criterion,
bool firstlevel)
1825 typedef typename C::Matrix Matrix;
1826 typedef typename G::VertexIterator VertexIterator;
1828 criterion.init(&matrix);
1833 const Row& row = matrix[*
vertex];
1838 criterion.initRow(row, *
vertex);
1843 ColIterator end = row.end();
1844 typename FieldTraits<typename Matrix::field_type>::real_type absoffdiag=0.;
1848 for(ColIterator col = row.begin(); col != end; ++col)
1849 if(col.index()!=*
vertex) {
1850 criterion.examine(col);
1851 absoffdiag =
max(absoffdiag, Impl::asMatrix(*col).frobenius_norm());
1855 vertex.properties().setExcludedBorder();
1858 for(ColIterator col = row.begin(); col != end; ++col)
1860 criterion.examine(col);
1866 if(criterion.isIsolated()) {
1868 vertex.properties().setIsolated();
1871 auto eEnd =
vertex.end();
1872 auto col = matrix[*
vertex].begin();
1874 for(
auto edge =
vertex.begin(); edge!= eEnd; ++edge, ++col) {
1876 while(col.index()!=edge.target())
1878 criterion.examine(graph, edge, col);
1888 inline Aggregator<G>::AggregateVisitor<V>::AggregateVisitor(
const AggregatesMap<Vertex>& aggregates,
1890 : aggregates_(aggregates), aggregate_(aggregate), visitor_(&visitor)
1897 if(aggregates_[edge.target()]==aggregate_)
1898 visitor_->operator()(edge);
1903 inline void Aggregator<G>::visitAggregateNeighbours(
const Vertex&
vertex,
1905 const AggregatesMap<Vertex>& aggregates,
1909 AggregateVisitor<V> v(aggregates, aggregate, visitor);
1915 inline Aggregator<G>::Counter::Counter()
1920 inline void Aggregator<G>::Counter::increment()
1926 inline void Aggregator<G>::Counter::decrement()
1931 inline int Aggregator<G>::Counter::value()
1939 if(edge.properties().isTwoWay())
1940 Counter::increment();
1945 const AggregatesMap<Vertex>& aggregates)
const
1947 TwoWayCounter counter;
1948 visitAggregateNeighbours(
vertex, aggregate, aggregates, counter);
1949 return counter.value();
1954 const AggregatesMap<Vertex>& aggregates)
const
1956 OneWayCounter counter;
1957 visitAggregateNeighbours(
vertex, aggregate, aggregates, counter);
1958 return counter.value();
1964 if(edge.properties().isOneWay())
1965 Counter::increment();
1969 inline Aggregator<G>::ConnectivityCounter::ConnectivityCounter(
const VertexSet& connected,
1970 const AggregatesMap<Vertex>& aggregates)
1971 : Counter(), connected_(connected), aggregates_(aggregates)
1980 Counter::increment();
1982 Counter::increment();
1983 Counter::increment();
1988 inline double Aggregator<G>::connectivity(
const Vertex&
vertex,
const AggregatesMap<Vertex>& aggregates)
const
1990 ConnectivityCounter counter(connected_, aggregates);
1992 return (
double)counter.value()/noNeighbours;
1996 inline Aggregator<G>::DependencyCounter::DependencyCounter()
2003 if(edge.properties().depends())
2004 Counter::increment();
2005 if(edge.properties().influences())
2006 Counter::increment();
2010 int Aggregator<G>::unusedNeighbours(
const Vertex&
vertex,
const AggregatesMap<Vertex>& aggregates)
const
2016 std::pair<int,int> Aggregator<G>::neighbours(
const Vertex&
vertex,
2018 const AggregatesMap<Vertex>& aggregates)
const
2020 DependencyCounter unused, aggregated;
2021 typedef AggregateVisitor<DependencyCounter> CounterT;
2022 typedef std::tuple<CounterT,CounterT> CounterTuple;
2025 return std::make_pair(unused.value(), aggregated.value());
2032 DependencyCounter counter;
2033 visitAggregateNeighbours(
vertex, aggregate, aggregates, counter);
2034 return counter.value();
2038 std::size_t Aggregator<G>::distance(
const Vertex&
vertex,
const AggregatesMap<Vertex>& aggregates)
2041 typename PropertyMapTypeSelector<VertexVisitedTag,G>::Type visitedMap =
get(VertexVisitedTag(), *graph_);
2043 typename AggregatesMap<Vertex>::DummyEdgeVisitor dummy;
2044 return aggregates.template breadthFirstSearch<true,true>(
vertex,
2045 aggregate_->
id(), *graph_,
2046 vlist, dummy, dummy, visitedMap);
2050 inline Aggregator<G>::FrontMarker::FrontMarker(std::vector<Vertex>&
front,
MatrixGraph& graph)
2051 : front_(
front), graph_(graph)
2057 Vertex target = edge.target();
2059 if(!graph_.getVertexProperties(target).front()) {
2060 front_.push_back(target);
2061 graph_.getVertexProperties(target).setFront();
2069 Dune::dvverb<<
" Admissible not yet implemented!"<<std::endl;
2076 Iterator vend = graph_->endEdges(
vertex);
2077 for(Iterator edge = graph_->beginEdges(
vertex); edge != vend; ++edge) {
2079 if(edge.properties().isStrong()
2080 && aggregates[edge.target()]==aggregate)
2083 Iterator edge1 = edge;
2084 for(++edge1; edge1 != vend; ++edge1) {
2086 if(edge1.properties().isStrong()
2087 && aggregates[edge.target()]==aggregate)
2092 Iterator v2end = graph_->endEdges(edge.target());
2093 for(Iterator edge2 = graph_->beginEdges(edge.target()); edge2 != v2end; ++edge2) {
2094 if(edge2.target()==edge1.target() &&
2095 edge2.properties().isStrong()) {
2111 vend = graph_->endEdges(
vertex);
2112 for(Iterator edge = graph_->beginEdges(
vertex); edge != vend; ++edge) {
2114 if(edge.properties().isStrong()
2115 && aggregates[edge.target()]==aggregate)
2118 Iterator v1end = graph_->endEdges(edge.target());
2120 for(Iterator edge1=graph_->beginEdges(edge.target()); edge1 != v1end; ++edge1) {
2122 if(edge1.properties().isStrong()
2123 && aggregates[edge1.target()]==aggregate)
2127 Iterator v2end = graph_->endEdges(
vertex);
2128 for(Iterator edge2 = graph_->beginEdges(
vertex); edge2 != v2end; ++edge2) {
2129 if(edge2.target()==edge1.target()) {
2130 if(edge2.properties().isStrong())
2147 void Aggregator<G>::unmarkFront()
2149 typedef typename std::vector<Vertex>::const_iterator Iterator;
2152 graph_->getVertexProperties(*vertex).resetFront();
2159 Aggregator<G>::nonisoNeighbourAggregate(
const Vertex&
vertex,
2160 const AggregatesMap<Vertex>& aggregates,
2161 SLList<Vertex>& neighbours)
const
2164 Iterator end=graph_->beginEdges(
vertex);
2167 for(Iterator edge=graph_->beginEdges(
vertex); edge!=end; ++edge)
2170 neighbours.push_back(aggregates[edge.target()]);
2175 inline typename G::VertexDescriptor Aggregator<G>::mergeNeighbour(
const Vertex&
vertex,
const AggregatesMap<Vertex>& aggregates)
const
2179 Iterator end = graph_->endEdges(
vertex);
2180 for(Iterator edge = graph_->beginEdges(
vertex); edge != end; ++edge) {
2182 graph_->getVertexProperties(edge.target()).isolated() == graph_->getVertexProperties(edge.source()).isolated()) {
2183 if( graph_->getVertexProperties(
vertex).isolated() ||
2184 ((edge.properties().depends() || edge.properties().influences())
2185 && admissible(
vertex, aggregates[edge.target()], aggregates)))
2186 return edge.target();
2193 Aggregator<G>::FrontNeighbourCounter::FrontNeighbourCounter(
const MatrixGraph& graph)
2194 : Counter(), graph_(graph)
2200 if(graph_.getVertexProperties(edge.target()).front())
2201 Counter::increment();
2205 int Aggregator<G>::noFrontNeighbours(
const Vertex&
vertex)
const
2207 FrontNeighbourCounter counter(*graph_);
2209 return counter.value();
2212 inline bool Aggregator<G>::connected(
const Vertex&
vertex,
2214 const AggregatesMap<Vertex>& aggregates)
const
2216 typedef typename G::ConstEdgeIterator iterator;
2217 const iterator end = graph_->endEdges(
vertex);
2218 for(iterator edge = graph_->beginEdges(
vertex); edge != end; ++edge)
2219 if(aggregates[edge.target()]==aggregate)
2224 inline bool Aggregator<G>::connected(
const Vertex&
vertex,
2225 const SLList<AggregateDescriptor>& aggregateList,
2226 const AggregatesMap<Vertex>& aggregates)
const
2229 for(Iter i=aggregateList.begin(); i!=aggregateList.end(); ++i)
2230 if(connected(
vertex, *i, aggregates))
2237 void Aggregator<G>::growIsolatedAggregate(
const Vertex& seed,
const AggregatesMap<Vertex>& aggregates,
const C& c)
2239 SLList<Vertex> connectedAggregates;
2240 nonisoNeighbourAggregate(seed, aggregates,connectedAggregates);
2242 while(aggregate_->
size()< c.minAggregateSize() && aggregate_->
connectSize() < c.maxConnectivity()) {
2244 std::size_t maxFrontNeighbours=0;
2248 typedef typename std::vector<Vertex>::const_iterator Iterator;
2251 if(distance(*
vertex, aggregates)>c.maxDistance())
2254 if(connectedAggregates.size()>0) {
2258 if(!connected(*
vertex, connectedAggregates, aggregates))
2262 double con = connectivity(*
vertex, aggregates);
2265 std::size_t frontNeighbours = noFrontNeighbours(*
vertex);
2267 if(frontNeighbours >= maxFrontNeighbours) {
2268 maxFrontNeighbours = frontNeighbours;
2271 }
else if(con > maxCon) {
2273 maxFrontNeighbours = noFrontNeighbours(*
vertex);
2281 aggregate_->
add(candidate);
2287 void Aggregator<G>::growAggregate(
const Vertex& seed,
const AggregatesMap<Vertex>& aggregates,
const C& c)
2291 std::size_t distance_ =0;
2292 while(aggregate_->
size() < c.minAggregateSize()&& distance_<c.maxDistance()) {
2293 int maxTwoCons=0, maxOneCons=0, maxNeighbours=-1;
2296 std::vector<Vertex> candidates;
2297 candidates.reserve(30);
2299 typedef typename std::vector<Vertex>::const_iterator Iterator;
2303 if(graph_->getVertexProperties(*vertex).isolated())
2306 int twoWayCons = twoWayConnections(*
vertex, aggregate_->
id(), aggregates);
2309 if( maxTwoCons == twoWayCons && twoWayCons > 0) {
2310 double con = connectivity(*
vertex, aggregates);
2313 int neighbours = noFrontNeighbours(*
vertex);
2315 if(neighbours > maxNeighbours) {
2316 maxNeighbours = neighbours;
2318 candidates.push_back(*
vertex);
2320 candidates.push_back(*
vertex);
2322 }
else if( con > maxCon) {
2324 maxNeighbours = noFrontNeighbours(*
vertex);
2326 candidates.push_back(*
vertex);
2328 }
else if(twoWayCons > maxTwoCons) {
2329 maxTwoCons = twoWayCons;
2330 maxCon = connectivity(*
vertex, aggregates);
2331 maxNeighbours = noFrontNeighbours(*
vertex);
2333 candidates.push_back(*
vertex);
2345 int oneWayCons = oneWayConnections(*
vertex, aggregate_->
id(), aggregates);
2350 if(!admissible(*
vertex, aggregate_->
id(), aggregates))
2353 if( maxOneCons == oneWayCons && oneWayCons > 0) {
2354 double con = connectivity(*
vertex, aggregates);
2357 int neighbours = noFrontNeighbours(*
vertex);
2359 if(neighbours > maxNeighbours) {
2360 maxNeighbours = neighbours;
2362 candidates.push_back(*
vertex);
2364 if(neighbours==maxNeighbours)
2366 candidates.push_back(*
vertex);
2369 }
else if( con > maxCon) {
2371 maxNeighbours = noFrontNeighbours(*
vertex);
2373 candidates.push_back(*
vertex);
2375 }
else if(oneWayCons > maxOneCons) {
2376 maxOneCons = oneWayCons;
2377 maxCon = connectivity(*
vertex, aggregates);
2378 maxNeighbours = noFrontNeighbours(*
vertex);
2380 candidates.push_back(*
vertex);
2385 if(!candidates.size())
2387 distance_=distance(seed, aggregates);
2388 candidates.resize(
min(candidates.size(), c.maxAggregateSize()-
2389 aggregate_->
size()));
2390 aggregate_->
add(candidates);
2394 template<
typename V>
2395 template<
typename M,
typename G,
typename C>
2399 Aggregator<G> aggregator;
2400 return aggregator.build(matrix, graph, *
this, criterion, finestLevel);
2404 template<
class M,
class C>
2405 std::tuple<int,int,int,int>
Aggregator<G>::build(
const M& m, G& graph, AggregatesMap<Vertex>& aggregates,
const C& c,
2411 Stack stack_(graph, *
this, aggregates);
2415 aggregate_ =
new Aggregate<G,VertexSet>(graph, aggregates, connected_, front_);
2422 dverb<<
"Build dependency took "<< watch.elapsed()<<
" seconds."<<std::endl;
2423 int noAggregates, conAggregates, isoAggregates, oneAggregates;
2424 std::size_t maxA=0, minA=1000000, avg=0;
2425 int skippedAggregates;
2426 noAggregates = conAggregates = isoAggregates = oneAggregates =
2427 skippedAggregates = 0;
2430 Vertex seed = stack_.pop();
2432 if(seed == Stack::NullEntry)
2437 if((noAggregates+1)%10000 == 0)
2441 if(graph.getVertexProperties(seed).excludedBorder()) {
2443 ++skippedAggregates;
2447 if(graph.getVertexProperties(seed).isolated()) {
2448 if(c.skipIsolated()) {
2451 ++skippedAggregates;
2455 aggregate_->
seed(seed);
2456 growIsolatedAggregate(seed, aggregates, c);
2459 aggregate_->
seed(seed);
2460 growAggregate(seed, aggregates, c);
2464 while(!(graph.getVertexProperties(seed).isolated()) && aggregate_->
size() < c.maxAggregateSize()) {
2466 std::vector<Vertex> candidates;
2467 candidates.reserve(30);
2469 typedef typename std::vector<Vertex>::const_iterator Iterator;
2473 if(graph.getVertexProperties(*vertex).isolated())
2476 if(twoWayConnections( *
vertex, aggregate_->
id(), aggregates) == 0 &&
2477 (oneWayConnections( *
vertex, aggregate_->
id(), aggregates) == 0 ||
2478 !admissible( *
vertex, aggregate_->
id(), aggregates) ))
2481 std::pair<int,int> neighbourPair=neighbours(*
vertex, aggregate_->
id(),
2487 if(neighbourPair.first >= neighbourPair.second)
2490 if(distance(*
vertex, aggregates) > c.maxDistance())
2492 candidates.push_back(*
vertex);
2496 if(!candidates.size())
break;
2498 candidates.resize(
min(candidates.size(), c.maxAggregateSize()-
2499 aggregate_->
size()));
2500 aggregate_->
add(candidates);
2505 if(aggregate_->
size()==1 && c.maxAggregateSize()>1) {
2506 if(!graph.getVertexProperties(seed).isolated()) {
2507 Vertex mergedNeighbour = mergeNeighbour(seed, aggregates);
2511 aggregates[seed] = aggregates[mergedNeighbour];
2512 aggregate_->invalidate();
2515 minA=
min(minA,
static_cast<std::size_t
>(1));
2516 maxA=
max(maxA,
static_cast<std::size_t
>(1));
2522 minA=
min(minA,
static_cast<std::size_t
>(1));
2523 maxA=
max(maxA,
static_cast<std::size_t
>(1));
2529 avg+=aggregate_->
size();
2530 minA=
min(minA,aggregate_->
size());
2531 maxA=
max(maxA,aggregate_->
size());
2532 if(graph.getVertexProperties(seed).isolated())
2540 Dune::dinfo<<
"connected aggregates: "<<conAggregates;
2541 Dune::dinfo<<
" isolated aggregates: "<<isoAggregates;
2542 if(conAggregates+isoAggregates>0)
2543 Dune::dinfo<<
" one node aggregates: "<<oneAggregates<<
" min size="
2544 <<minA<<
" max size="<<maxA
2545 <<
" avg="<<avg/(conAggregates+isoAggregates)<<std::endl;
2548 return std::make_tuple(conAggregates+isoAggregates,isoAggregates,
2549 oneAggregates,skippedAggregates);
2554 Aggregator<G>::Stack::Stack(
const MatrixGraph& graph,
const Aggregator<G>& aggregatesBuilder,
2555 const AggregatesMap<Vertex>& aggregates)
2556 : graph_(graph), aggregatesBuilder_(aggregatesBuilder), aggregates_(aggregates), begin_(graph.begin()), end_(graph.end())
2562 Aggregator<G>::Stack::~Stack()
2573 inline typename G::VertexDescriptor Aggregator<G>::Stack::pop()
2579 typename G::VertexDescriptor current=*begin_;
2589 void printAggregates2d(
const AggregatesMap<V>& aggregates,
int n,
int m, std::ostream& os)
2593 std::ios_base::fmtflags oldOpts=os.flags();
2595 os.setf(std::ios_base::right, std::ios_base::adjustfield);
2600 for(
int i=0; i< n*m; i++)
2601 maxVal=
max(maxVal, aggregates[i]);
2603 for(
int i=10; i < 1000000; i*=10)
2609 for(
int j=0, entry=0; j < m; j++) {
2610 for(
int i=0; i<n; i++, entry++) {
2612 os<<aggregates[entry]<<
" ";
A class for temporarily storing the vertices of an aggregate in.
Definition: aggregates.hh:784
A Dummy visitor that does nothing for each visited edge.
Definition: aggregates.hh:604
Class providing information about the mapping of the vertices onto aggregates.
Definition: aggregates.hh:566
Base class of all aggregation criterions.
Definition: aggregates.hh:51
Class for building the aggregates.
Definition: aggregates.hh:915
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:457
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:525
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:545
derive error class from the base class in common
Definition: istlexception.hh:19
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
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:312
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:485
constexpr auto min
Function object that returns the smaller of the given values.
Definition: hybridutilities.hh:507
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:592
int id()
Get the id identifying the aggregate.
Norm norm_
The functor for calculating the norm.
Definition: aggregates.hh:304
MatrixGraph::VertexDescriptor Vertex
The vertex identifier.
Definition: aggregates.hh:926
AggregationCriterion()
Constructor.
Definition: aggregates.hh:68
const Matrix * matrix_
The matrix we work on.
Definition: aggregates.hh:359
auto operator()(const M &m, typename std::enable_if_t< Dune::IsNumber< M >::value > *sfinae=nullptr) const
Compute the norm of a scalar.
Definition: aggregates.hh:408
M Matrix
The matrix type we build the dependency of.
Definition: aggregates.hh:260
G MatrixGraph
The matrix graph type used.
Definition: aggregates.hh:921
Norm norm_
The functor for calculating the norm.
Definition: aggregates.hh:365
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
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:1070
std::size_t * SphereMap
Type of the mapping of aggregate members onto distance spheres.
Definition: aggregates.hh:815
Matrix::row_type Row
Constant Row iterator of the matrix.
Definition: aggregates.hh:270
VertexSet::size_type connectSize()
Get the number of connections to other aggregates.
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:810
MatrixGraph::VertexDescriptor AggregateDescriptor
The type of the aggregate descriptor.
Definition: aggregates.hh:929
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
auto operator()(const M &m) const
compute the norm of a matrix.
Definition: aggregates.hh:475
void operator()(const typename MatrixGraph::ConstEdgeIterator &edge)
Examine an edge.
FrontMarker(std::vector< Vertex > &front, MatrixGraph &graph)
Constructor.
FieldTraits< typenameM::field_type >::real_type operator()(const M &m) const
compute the norm of a matrix.
Definition: aggregates.hh:512
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:586
static const V ISOLATED
Identifier of isolated vertices.
Definition: aggregates.hh:577
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:598
ConnectivityCounter(const VertexSet &connected, const AggregatesMap< Vertex > &aggregates)
Constructor.
VertexSet::size_type size()
Get the size of the aggregate.
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:807
FieldTraits< typenameM::field_type >::real_type operator()(const M &m) const
compute the norm of a matrix.
Definition: aggregates.hh:496
static const V UNAGGREGATED
Identifier of not yet aggregated vertices.
Definition: aggregates.hh:572
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.
FieldTraits< typenameM::field_type >::real_type operator()(const M &m, typename std::enable_if_t<!Dune::IsNumber< M >::value > *sfinae=nullptr) const
compute the norm of a matrix.
Definition: aggregates.hh:392
MatrixGraph::VertexDescriptor Vertex
The vertex descriptor type.
Definition: aggregates.hh:795
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:581
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
PoolAllocator< Vertex, 100 > Allocator
The allocator we use for our lists and the set.
Definition: aggregates.hh:801
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 std::integral_constant< T, I0 > front(std::integer_sequence< T, I0, II... >)
Return the first entry of the sequence.
Definition: integersequence.hh:39
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:465
Whether this type acts as a scalar in the context of (hierarchically blocked) containers.
Definition: typetraits.hh:194
Traits for type conversions and type information.