2 #ifndef DUNE_PDELAB_BACKEND_SIMPLE_SPARSE_HH
3 #define DUNE_PDELAB_BACKEND_SIMPLE_SPARSE_HH
9 #include <unordered_set>
11 #include <dune/common/typetraits.hh>
12 #include <dune/common/shared_ptr.hh>
22 template<
typename _RowOrdering,
typename _ColOrdering>
24 :
public std::vector< std::unordered_set<std::size_t> >
32 typedef std::unordered_set<std::size_t>
col_type;
34 template<
typename RI,
typename CI>
37 this->resize(_row_ordering.blockCount());
38 (*this)[ri.back()].insert(ci.back());
42 : _row_ordering(row_ordering)
43 , _col_ordering(col_ordering)
48 const RowOrdering& _row_ordering;
49 const ColOrdering& _col_ordering;
53 template<
template<
typename>
class C,
typename ET,
typename I>
87 template<
typename GFSV,
typename GFSU,
template<
typename>
class C,
typename ET,
typename I>
103 typedef typename GFSV::Ordering::Traits::ContainerIndex
RowIndex;
104 typedef typename GFSU::Ordering::Traits::ContainerIndex
ColIndex;
106 template<
typename RowCache,
typename ColCache>
109 template<
typename RowCache,
typename ColCache>
113 typename GFSV::Ordering::Traits::DOFIndex,
114 typename GFSV::Ordering::Traits::ContainerIndex
118 typename GFSU::Ordering::Traits::DOFIndex,
119 typename GFSU::Ordering::Traits::ContainerIndex
124 template<
typename GO>
131 template<
typename GO>
171 void attach(shared_ptr<Container> container)
204 using namespace std::placeholders;
210 void mv(
const V& x, V& y)
const
212 assert(y.N() ==
N());
213 assert(x.N() ==
M());
214 for (std::size_t r = 0; r <
N(); ++r)
221 void usmv(
const ElementType alpha,
const V& x, V& y)
const
223 assert(y.N() ==
N());
224 assert(x.N() ==
M());
225 for (std::size_t r = 0; r <
N(); ++r)
231 ElementType&
operator()(
const RowIndex& ri,
const ColIndex& ci)
236 auto it = std::lower_bound(begin,end,ci[0]);
241 const ElementType&
operator()(
const RowIndex& ri,
const ColIndex& ci)
const
246 auto it = std::lower_bound(begin,end,ci[0]);
267 void clear_row(
const RowIndex& ri,
const ElementType& diagonal_entry)
272 (*this)(ri,ri) = diagonal_entry;
276 template<
typename GO>
277 static void allocate_matrix(shared_ptr<Container> & c,
const GO & go,
const ElementType&
e)
280 Pattern pattern(go.testGridFunctionSpace().ordering(),go.trialGridFunctionSpace().ordering());
281 go.fill_pattern(pattern);
283 c->_rows = go.testGridFunctionSpace().size();
284 c->_cols = go.trialGridFunctionSpace().size();
286 c->_rowoffset.resize(c->_rows+1);
288 auto calc_offset = [=](
const col_type & entry)
mutable ->
size_t { offset += entry.size();
return offset; };
289 std::transform(pattern.begin(), pattern.end(),
290 c->_rowoffset.begin()+1,
293 c->_non_zeros = c->_rowoffset.back();
295 c->_data.resize(c->_non_zeros, e);
296 c->_colindex.resize(c->_non_zeros);
298 auto colit = c->_colindex.begin();
299 c->_rowoffset[0] = 0;
300 for (
auto & row : pattern)
302 auto last = std::copy(row.begin(),row.end(),colit);
303 std::sort(colit, last);
310 std::size_t begin =
_container->_rowoffset[row];
311 std::size_t end =
_container->_rowoffset[row+1];
312 auto accu = [](
const ElementType & a,
const ElementType & b) -> ElementType {
return a+b; };
313 auto mult = [=](
const ElementType & a,
const index_type & i) -> ElementType {
return a * x.base()[i]; };
314 return std::inner_product(
330 #endif // DUNE_PDELAB_BACKEND_SIMPLE_SPARSE_HH
Tag for requesting a vector or matrix container without a pre-attached underlying object...
Definition: backend/tags.hh:23
SparseMatrixContainer(const GO &go)
Definition: sparse.hh:125
SparseMatrixContainer(const SparseMatrixContainer &rhs)
Definition: sparse.hh:147
SparseMatrixContainer & operator*=(const ElementType &e)
Definition: sparse.hh:202
bool attached() const
Definition: sparse.hh:176
shared_ptr< Container > _container
Definition: sparse.hh:323
GFSU TrialGridFunctionSpace
Definition: sparse.hh:100
SparseMatrixContainer & operator=(const SparseMatrixContainer &rhs)
Definition: sparse.hh:151
I index_type
Definition: sparse.hh:57
SparseMatrixPattern(const RowOrdering &row_ordering, const ColOrdering &col_ordering)
Definition: sparse.hh:41
void clear_row(const RowIndex &ri, const ElementType &diagonal_entry)
Definition: sparse.hh:267
_ColOrdering ColOrdering
Definition: sparse.hh:30
Container & base()
Definition: sparse.hh:256
ElementType sparse_inner_product(std::size_t row, const V &x) const
Definition: sparse.hh:309
std::size_t _non_zeros
Definition: sparse.hh:61
_RowOrdering RowOrdering
Definition: sparse.hh:29
Various tags for influencing backend behavior.
size_type N() const
Definition: sparse.hh:186
const shared_ptr< Container > & storage() const
Definition: sparse.hh:181
SparseMatrixPattern< RowOrdering, ColOrdering > Pattern
Definition: sparse.hh:122
ElementType field_type
Definition: sparse.hh:96
ET ElementType
Definition: sparse.hh:56
GFSV TestGridFunctionSpace
Definition: sparse.hh:101
Definition: orderingbase.hh:22
GFSV::Ordering::Traits::ContainerIndex RowIndex
Definition: sparse.hh:103
Definition: adaptivity.hh:26
OrderingBase< typename GFSU::Ordering::Traits::DOFIndex, typename GFSU::Ordering::Traits::ContainerIndex > ColOrdering
Definition: sparse.hh:120
ET ElementType
Definition: sparse.hh:94
void finalize()
Definition: sparse.hh:264
size_type M() const
Definition: sparse.hh:191
C< index_type > _rowoffset
Definition: sparse.hh:64
static void allocate_matrix(shared_ptr< Container > &c, const GO &go, const ElementType &e)
Definition: sparse.hh:277
const std::size_t offset
Definition: localfunctionspace.hh:74
const E & e
Definition: interpolate.hh:172
C< index_type > _colindex
Definition: sparse.hh:63
SparseMatrixContainer(tags::attached_container)
Creates an SparseMatrixContainer with an empty underlying ISTL matrix.
Definition: sparse.hh:143
OrderingBase< typename GFSV::Ordering::Traits::DOFIndex, typename GFSV::Ordering::Traits::ContainerIndex > RowOrdering
Definition: sparse.hh:115
SparseMatrixContainer(const GO &go, const ElementType &e)
Definition: sparse.hh:132
void flush()
Definition: sparse.hh:261
I index_type
Definition: sparse.hh:98
Definition: uncachedmatrixview.hh:14
std::size_t size_type
Definition: sparse.hh:58
SparseMatrixContainer & operator=(const ElementType &e)
Definition: sparse.hh:196
void add_link(const RI &ri, const CI &ci)
Definition: sparse.hh:35
std::unordered_set< std::size_t > col_type
Definition: sparse.hh:32
Container::size_type size_type
Definition: sparse.hh:97
SparseMatrixData< C, ET, I > Container
Definition: sparse.hh:93
const Container & base() const
Definition: sparse.hh:251
const ElementType & operator()(const RowIndex &ri, const ColIndex &ci) const
Definition: sparse.hh:241
SparseMatrixContainer(tags::unattached_container=tags::unattached_container())
Creates an SparseMatrixContainer without allocating an underlying ISTL matrix.
Definition: sparse.hh:139
Definition: uncachedmatrixview.hh:159
void mv(const V &x, V &y) const
Definition: sparse.hh:210
GFSU::Ordering::Traits::ContainerIndex ColIndex
Definition: sparse.hh:104
Tag for requesting a vector or matrix container with a pre-attached underlying object.
Definition: backend/tags.hh:27
void attach(shared_ptr< Container > container)
Definition: sparse.hh:171
std::size_t _cols
Definition: sparse.hh:60
void detach()
Definition: sparse.hh:166
C< ElementType > _data
Definition: sparse.hh:62
void usmv(const ElementType alpha, const V &x, V &y) const
Definition: sparse.hh:221
ElementType & operator()(const RowIndex &ri, const ColIndex &ci)
Definition: sparse.hh:231
std::size_t _rows
Definition: sparse.hh:59