dune-pdelab  2.5-dev
bcrsmatrix.hh
Go to the documentation of this file.
1 // -*- tab-width: 4; indent-tabs-mode: nil -*-
2 #ifndef DUNE_PDELAB_BACKEND_ISTL_BCRSMATRIX_HH
3 #define DUNE_PDELAB_BACKEND_ISTL_BCRSMATRIX_HH
4 
5 #include <dune/common/typetraits.hh>
11 
12 namespace Dune {
13  namespace PDELab {
14 
15  namespace ISTL {
16 
17  template<typename GFSV, typename GFSU, typename C, typename Stats>
18  class BCRSMatrix
19  : public Backend::impl::Wrapper<C>
20  {
21 
22  friend Backend::impl::Wrapper<C>;
23 
24  public:
25 
26  typedef typename C::field_type ElementType;
27  typedef ElementType E;
28  typedef C Container;
29  typedef typename C::field_type field_type;
30  typedef typename C::block_type block_type;
31  typedef typename C::size_type size_type;
32 
33  typedef GFSU TrialGridFunctionSpace;
34  typedef GFSV TestGridFunctionSpace;
35 
36  typedef typename GFSV::Ordering::Traits::ContainerIndex RowIndex;
37  typedef typename GFSU::Ordering::Traits::ContainerIndex ColIndex;
38 
39  typedef typename ISTL::build_pattern_type<C,GFSV,GFSU,typename GFSV::Ordering::ContainerAllocationTag>::type Pattern;
40 
41  typedef Stats PatternStatistics;
42 
43  using value_type = E;
44 
45 #ifndef DOXYGEN
46 
47  // some trickery to avoid exposing average users to the fact that there might
48  // be multiple statistics objects
49  typedef typename std::conditional<
50  (C::blocklevel > 2),
51  std::vector<PatternStatistics>,
53  >::type StatisticsReturnType;
54 
55 #endif // DOXYGEN
56 
57  template<typename RowCache, typename ColCache>
59 
60  template<typename RowCache, typename ColCache>
62 
63  template<typename RowCache, typename ColCache>
65 
66  template<typename RowCache, typename ColCache>
68 
69  template<typename GO>
70  explicit BCRSMatrix (const GO& go)
71  : _container(std::make_shared<Container>())
72  {
73  _stats = go.matrixBackend().buildPattern(go,*this);
74  }
75 
85  template<typename GO>
86  BCRSMatrix (const GO& go, Container& container)
87  : _container(Dune::stackobject_to_shared_ptr(container))
88  {
89  _stats = go.matrixBackend().buildPattern(go,*this);
90  }
91 
92  template<typename GO>
93  BCRSMatrix (const GO& go, const E& e)
94  : _container(std::make_shared<Container>())
95  {
96  _stats = go.matrixBackend().buildPattern(go,*this);
97  (*_container) = e;
98  }
99 
102  {}
103 
106  : _container(std::make_shared<Container>())
107  {}
108 
109  BCRSMatrix(const BCRSMatrix& rhs)
110  : _container(std::make_shared<Container>(*(rhs._container)))
111  {}
112 
114  {
115  if (this == &rhs)
116  return *this;
117  _stats.clear();
118  if (attached())
119  {
120  (*_container) = (*(rhs._container));
121  }
122  else
123  {
124  _container = std::make_shared<Container>(*(rhs._container));
125  }
126  return *this;
127  }
128 
130  const StatisticsReturnType& patternStatistics() const
131  {
132  return patternStatistics(std::integral_constant<bool,(C::blocklevel > 2)>());
133  }
134 
135 #ifndef DOXYGEN
136 
137  private:
138 
139  const PatternStatistics& patternStatistics(std::false_type multiple) const
140  {
141  if (_stats.empty())
142  DUNE_THROW(InvalidStateException,"no pattern statistics available");
143  return _stats[0];
144  }
145 
146  const std::vector<PatternStatistics>& patternStatistics(std::true_type multiple) const
147  {
148  if (_stats.empty())
149  DUNE_THROW(InvalidStateException,"no pattern statistics available");
150  return _stats;
151  }
152 
153  public:
154 
155 #endif
156 
157  void detach()
158  {
159  _container.reset();
160  _stats.clear();
161  }
162 
163  void attach(std::shared_ptr<Container> container)
164  {
165  _container = container;
166  }
167 
168  bool attached() const
169  {
170  return bool(_container);
171  }
172 
173  const std::shared_ptr<Container>& storage() const
174  {
175  return _container;
176  }
177 
178  size_type N() const
179  {
180  return _container->N();
181  }
182 
183  size_type M() const
184  {
185  return _container->M();
186  }
187 
189  {
190  (*_container) = e;
191  return *this;
192  }
193 
195  {
196  (*_container) *= e;
197  return *this;
198  }
199 
200  E& operator()(const RowIndex& ri, const ColIndex& ci)
201  {
202  return ISTL::access_matrix_element(ISTL::container_tag(*_container),*_container,ri,ci,ri.size()-1,ci.size()-1);
203  }
204 
205  const E& operator()(const RowIndex& ri, const ColIndex& ci) const
206  {
207  return ISTL::access_matrix_element(ISTL::container_tag(*_container),*_container,ri,ci,ri.size()-1,ci.size()-1);
208  }
209 
210  private:
211 
212  const Container& native() const
213  {
214  return *_container;
215  }
216 
217  Container& native()
218  {
219  return *_container;
220  }
221 
222  public:
223 
224  template<typename RowCache, typename ColCache>
225  value_type* data(const RowCache& row_cache, const ColCache& col_cache)
226  {
227  return &((*this)(row_cache.containerIndex(0),col_cache.containerIndex(0)));
228  }
229 
230  template<typename RowCache, typename ColCache>
231  const value_type* data(const RowCache& row_cache, const ColCache& col_cache) const
232  {
233  return &((*this)(row_cache.containerIndex(0),col_cache.containerIndex(0)));
234  }
235 
236  void flush()
237  {}
238 
239  void finalize()
240  {}
241 
242  void clear_row(const RowIndex& ri, const E& diagonal_entry)
243  {
244  ISTL::clear_matrix_row(ISTL::container_tag(*_container),*_container,ri,ri.size()-1);
245  ISTL::write_matrix_element_if_exists(diagonal_entry,ISTL::container_tag(*_container),*_container,ri,ri,ri.size()-1,ri.size()-1);
246  }
247 
248  void clear_row_block(const RowIndex& ri, const E& diagonal_entry)
249  {
250  ISTL::clear_matrix_row_block(ISTL::container_tag(*_container),*_container,ri,ri.size()-1);
251  ISTL::write_matrix_element_if_exists_to_block(diagonal_entry,ISTL::container_tag(*_container),*_container,ri,ri,ri.size()-1,ri.size()-1);
252  }
253 
254  private:
255 
256  std::shared_ptr<Container> _container;
257  std::vector<PatternStatistics> _stats;
258 
259  };
260 
261  } // namespace ISTL
262 
263  } // namespace PDELab
264 } // namespace Dune
265 
266 #endif // DUNE_PDELAB_BACKEND_ISTL_BCRSMATRIX_HH
Dune::PDELab::ConstUncachedMatrixView
Definition: uncachedmatrixview.hh:12
Dune::PDELab::ISTL::container_tag
tags::container< T >::type container_tag(const T &)
Gets instance of container tag associated with T.
Definition: backend/istl/tags.hh:246
Dune::PDELab::ISTL::BCRSMatrix::detach
void detach()
Definition: bcrsmatrix.hh:157
Dune::PDELab::AliasedMatrixView
Definition: aliasedmatrixview.hh:166
Dune::PDELab::ISTL::BCRSMatrix::field_type
C::field_type field_type
Definition: bcrsmatrix.hh:29
Dune::PDELab::Backend::attached_container
Tag for requesting a vector or matrix container with a pre-attached underlying object.
Definition: backend/common/tags.hh:27
Dune::PDELab::ISTL::BCRSMatrix::BCRSMatrix
BCRSMatrix(Backend::attached_container)
Creates an BCRSMatrix with an empty underlying ISTL matrix.
Definition: bcrsmatrix.hh:105
Dune
For backward compatibility – Do not use this!
Definition: adaptivity.hh:28
Dune::PDELab::ISTL::BCRSMatrix::clear_row_block
void clear_row_block(const RowIndex &ri, const E &diagonal_entry)
Definition: bcrsmatrix.hh:248
Dune::PDELab::ISTL::BCRSMatrix::BCRSMatrix
BCRSMatrix(const GO &go, const E &e)
Definition: bcrsmatrix.hh:93
Dune::PDELab::ISTL::BCRSMatrix::E
ElementType E
Definition: bcrsmatrix.hh:27
Dune::PDELab::ConstAliasedMatrixView
Definition: aliasedmatrixview.hh:12
Dune::PDELab::ISTL::BCRSMatrix::BCRSMatrix
BCRSMatrix(Backend::unattached_container=Backend::unattached_container())
Creates an BCRSMatrix without allocating an underlying ISTL matrix.
Definition: bcrsmatrix.hh:101
Dune::PDELab::ISTL::BCRSMatrix::TestGridFunctionSpace
GFSV TestGridFunctionSpace
Definition: bcrsmatrix.hh:34
Dune::PDELab::ISTL::BCRSMatrix::PatternStatistics
Stats PatternStatistics
Definition: bcrsmatrix.hh:41
Dune::PDELab::ISTL::BCRSMatrix::RowIndex
GFSV::Ordering::Traits::ContainerIndex RowIndex
Definition: bcrsmatrix.hh:36
Dune::PDELab::UncachedMatrixView
Definition: uncachedmatrixview.hh:165
Dune::PDELab::ISTL::BCRSMatrix::Pattern
ISTL::build_pattern_type< C, GFSV, GFSU, typename GFSV::Ordering::ContainerAllocationTag >::type Pattern
Definition: bcrsmatrix.hh:39
Dune::PDELab::ISTL::BCRSMatrix::patternStatistics
const StatisticsReturnType & patternStatistics() const
Returns pattern statistics for all contained BCRSMatrix objects.
Definition: bcrsmatrix.hh:130
Dune::PDELab::ISTL::BCRSMatrix::TrialGridFunctionSpace
GFSU TrialGridFunctionSpace
Definition: bcrsmatrix.hh:33
Dune::PDELab::ISTL::BCRSMatrix::BCRSMatrix
BCRSMatrix(const BCRSMatrix &rhs)
Definition: bcrsmatrix.hh:109
Dune::PDELab::ISTL::BCRSMatrix::M
size_type M() const
Definition: bcrsmatrix.hh:183
aliasedmatrixview.hh
Dune::PDELab::ISTL::BCRSMatrix::flush
void flush()
Definition: bcrsmatrix.hh:236
Dune::PDELab::ISTL::PatternStatistics
Statistics about the pattern of a BCRSMatrix.
Definition: patternstatistics.hh:13
Dune::PDELab::ISTL::BCRSMatrix::storage
const std::shared_ptr< Container > & storage() const
Definition: bcrsmatrix.hh:173
Dune::PDELab::ISTL::BCRSMatrix::ColIndex
GFSU::Ordering::Traits::ContainerIndex ColIndex
Definition: bcrsmatrix.hh:37
matrixhelpers.hh
Dune::PDELab::ISTL::BCRSMatrix::value_type
E value_type
Definition: bcrsmatrix.hh:43
e
const Entity & e
Definition: localfunctionspace.hh:120
Dune::PDELab::ISTL::BCRSMatrix::clear_row
void clear_row(const RowIndex &ri, const E &diagonal_entry)
Definition: bcrsmatrix.hh:242
tags.hh
Various tags for influencing backend behavior.
Dune::PDELab::ISTL::BCRSMatrix::data
value_type * data(const RowCache &row_cache, const ColCache &col_cache)
Definition: bcrsmatrix.hh:225
Dune::PDELab::ISTL::BCRSMatrix::block_type
C::block_type block_type
Definition: bcrsmatrix.hh:30
Dune::PDELab::ISTL::BCRSMatrix::Container
C Container
Definition: bcrsmatrix.hh:28
uncachedmatrixview.hh
Dune::PDELab::ISTL::BCRSMatrix::operator*=
BCRSMatrix & operator*=(const E &e)
Definition: bcrsmatrix.hh:194
Dune::PDELab::ISTL::BCRSMatrix
Definition: bcrsmatrix.hh:18
Dune::PDELab::Backend::unattached_container
Tag for requesting a vector or matrix container without a pre-attached underlying object.
Definition: backend/common/tags.hh:23
Dune::PDELab::ISTL::BCRSMatrix::operator()
const E & operator()(const RowIndex &ri, const ColIndex &ci) const
Definition: bcrsmatrix.hh:205
Dune::PDELab::ISTL::BCRSMatrix::operator()
E & operator()(const RowIndex &ri, const ColIndex &ci)
Definition: bcrsmatrix.hh:200
Dune::PDELab::ISTL::BCRSMatrix::data
const value_type * data(const RowCache &row_cache, const ColCache &col_cache) const
Definition: bcrsmatrix.hh:231
Dune::PDELab::ISTL::BCRSMatrix::attach
void attach(std::shared_ptr< Container > container)
Definition: bcrsmatrix.hh:163
Dune::PDELab::ISTL::BCRSMatrix::N
size_type N() const
Definition: bcrsmatrix.hh:178
Dune::PDELab::ISTL::BCRSMatrix::BCRSMatrix
BCRSMatrix(const GO &go, Container &container)
Construct matrix container using an externally given matrix as storage.
Definition: bcrsmatrix.hh:86
Dune::PDELab::ISTL::BCRSMatrix::BCRSMatrix
BCRSMatrix(const GO &go)
Definition: bcrsmatrix.hh:70
Dune::PDELab::ISTL::BCRSMatrix::size_type
C::size_type size_type
Definition: bcrsmatrix.hh:31
descriptors.hh
Dune::PDELab::ISTL::BCRSMatrix::ElementType
C::field_type ElementType
Definition: bcrsmatrix.hh:26
Dune::PDELab::ISTL::BCRSMatrix::operator=
BCRSMatrix & operator=(const BCRSMatrix &rhs)
Definition: bcrsmatrix.hh:113
Dune::PDELab::ISTL::BCRSMatrix::attached
bool attached() const
Definition: bcrsmatrix.hh:168
Dune::PDELab::ISTL::BCRSMatrix::finalize
void finalize()
Definition: bcrsmatrix.hh:239