dune-pdelab  2.4-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>
10 
11 namespace Dune {
12  namespace PDELab {
13 
14  namespace istl {
15 
16  template<typename GFSV, typename GFSU, typename C, typename Stats>
17  class BCRSMatrix
18  : public Backend::impl::Wrapper<C>
19  {
20 
21  friend Backend::impl::Wrapper<C>;
22 
23  public:
24 
25  typedef typename C::field_type ElementType;
26  typedef ElementType E;
27  typedef C Container;
28  typedef C BaseT;
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 #ifndef DOXYGEN
44 
45  // some trickery to avoid exposing average users to the fact that there might
46  // be multiple statistics objects
47  typedef typename conditional<
48  (C::blocklevel > 2),
49  std::vector<PatternStatistics>,
50  PatternStatistics
51  >::type StatisticsReturnType;
52 
53 #endif // DOXYGEN
54 
55  template<typename RowCache, typename ColCache>
57 
58  template<typename RowCache, typename ColCache>
60 
61  template<typename GO>
62  explicit BCRSMatrix (const GO& go)
63  : _container(std::make_shared<Container>())
64  {
65  _stats = go.matrixBackend().buildPattern(go,*this);
66  }
67 
77  template<typename GO>
78  BCRSMatrix (const GO& go, Container& container)
79  : _container(Dune::stackobject_to_shared_ptr(container))
80  {
81  _stats = go.matrixBackend().buildPattern(go,*this);
82  }
83 
84  template<typename GO>
85  BCRSMatrix (const GO& go, const E& e)
86  : _container(std::make_shared<Container>())
87  {
88  _stats = go.matrixBackend().buildPattern(go,*this);
89  (*_container) = e;
90  }
91 
94  {}
95 
98  : _container(std::make_shared<Container>())
99  {}
100 
101  BCRSMatrix(const BCRSMatrix& rhs)
102  : _container(std::make_shared<Container>(*(rhs._container)))
103  {}
104 
106  {
107  if (this == &rhs)
108  return *this;
109  _stats.clear();
110  if (attached())
111  {
112  (*_container) = (*(rhs._container));
113  }
114  else
115  {
116  _container = std::make_shared<Container>(*(rhs._container));
117  }
118  return *this;
119  }
120 
122  const StatisticsReturnType& patternStatistics() const
123  {
124  return patternStatistics(integral_constant<bool,(C::blocklevel > 2)>());
125  }
126 
127 #ifndef DOXYGEN
128 
129  private:
130 
131  const PatternStatistics& patternStatistics(false_type multiple) const
132  {
133  if (_stats.empty())
134  DUNE_THROW(InvalidStateException,"no pattern statistics available");
135  return _stats[0];
136  }
137 
138  const std::vector<PatternStatistics>& patternStatistics(true_type multiple) const
139  {
140  if (_stats.empty())
141  DUNE_THROW(InvalidStateException,"no pattern statistics available");
142  return _stats;
143  }
144 
145  public:
146 
147 #endif
148 
149  void detach()
150  {
151  _container.reset();
152  _stats.clear();
153  }
154 
155  void attach(std::shared_ptr<Container> container)
156  {
157  _container = container;
158  }
159 
160  bool attached() const
161  {
162  return bool(_container);
163  }
164 
165  const std::shared_ptr<Container>& storage() const
166  {
167  return _container;
168  }
169 
170  size_type N() const
171  {
172  return _container->N();
173  }
174 
175  size_type M() const
176  {
177  return _container->M();
178  }
179 
181  {
182  (*_container) = e;
183  return *this;
184  }
185 
187  {
188  (*_container) *= e;
189  return *this;
190  }
191 
192  E& operator()(const RowIndex& ri, const ColIndex& ci)
193  {
194  return istl::access_matrix_element(istl::container_tag(*_container),*_container,ri,ci,ri.size()-1,ci.size()-1);
195  }
196 
197  const E& operator()(const RowIndex& ri, const ColIndex& ci) const
198  {
199  return istl::access_matrix_element(istl::container_tag(*_container),*_container,ri,ci,ri.size()-1,ci.size()-1);
200  }
201 
202  const Container&
203  DUNE_DEPRECATED_MSG("base() is deprecated and will be removed after PDELab 2.4. Use Backend::native() instead.")
204  base() const
205  {
206  return *_container;
207  }
208 
209  Container&
210  DUNE_DEPRECATED_MSG("base() is deprecated and will be removed after PDELab 2.4. Use Backend::native() instead.")
212  {
213  return *_container;
214  }
215 
216  private:
217 
218  const Container& native() const
219  {
220  return *_container;
221  }
222 
223  Container& native()
224  {
225  return *_container;
226  }
227 
228  public:
229 
230  void flush()
231  {}
232 
233  void finalize()
234  {}
235 
236  void clear_row(const RowIndex& ri, const E& diagonal_entry)
237  {
238  istl::clear_matrix_row(istl::container_tag(*_container),*_container,ri,ri.size()-1);
239  istl::write_matrix_element_if_exists(diagonal_entry,istl::container_tag(*_container),*_container,ri,ri,ri.size()-1,ri.size()-1);
240  }
241 
242  private:
243 
244  std::shared_ptr<Container> _container;
245  std::vector<PatternStatistics> _stats;
246 
247  };
248 
249  } // namespace istl
250 
251  } // namespace PDELab
252 } // namespace Dune
253 
254 #endif // DUNE_PDELAB_BACKEND_ISTL_BCRSMATRIX_HH
C::block_type block_type
Definition: bcrsmatrix.hh:30
GFSV TestGridFunctionSpace
Definition: bcrsmatrix.hh:34
C::size_type size_type
Definition: bcrsmatrix.hh:31
const std::shared_ptr< Container > & storage() const
Definition: bcrsmatrix.hh:165
const Container & base() const
Definition: bcrsmatrix.hh:204
Definition: bcrsmatrix.hh:17
BCRSMatrix(const GO &go)
Definition: bcrsmatrix.hh:62
void clear_row(const RowIndex &ri, const E &diagonal_entry)
Definition: bcrsmatrix.hh:236
tags::container< T >::type container_tag(const T &)
Gets instance of container tag associated with T.
Definition: backend/istl/tags.hh:246
Stats PatternStatistics
Definition: bcrsmatrix.hh:41
GFSV::Ordering::Traits::ContainerIndex RowIndex
Definition: bcrsmatrix.hh:36
Various tags for influencing backend behavior.
C Container
Definition: bcrsmatrix.hh:27
void detach()
Definition: bcrsmatrix.hh:149
const E & e
Definition: interpolate.hh:172
STL namespace.
Definition: uncachedmatrixview.hh:158
ElementType E
Definition: bcrsmatrix.hh:26
bool attached() const
Definition: bcrsmatrix.hh:160
BCRSMatrix(Backend::attached_container)
Creates an BCRSMatrix with an empty underlying ISTL matrix.
Definition: bcrsmatrix.hh:97
istl::build_pattern_type< C, GFSV, GFSU, typename GFSV::Ordering::ContainerAllocationTag >::type Pattern
Definition: bcrsmatrix.hh:39
C::field_type field_type
Definition: bcrsmatrix.hh:29
size_type M() const
Definition: bcrsmatrix.hh:175
void attach(std::shared_ptr< Container > container)
Definition: bcrsmatrix.hh:155
const StatisticsReturnType & patternStatistics() const
Returns pattern statistics for all contained BCRSMatrix objects.
Definition: bcrsmatrix.hh:122
C::field_type ElementType
Definition: bcrsmatrix.hh:25
void finalize()
Definition: bcrsmatrix.hh:233
Tag for requesting a vector or matrix container without a pre-attached underlying object...
Definition: backend/common/tags.hh:23
BCRSMatrix(const GO &go, const E &e)
Definition: bcrsmatrix.hh:85
Tag for requesting a vector or matrix container with a pre-attached underlying object.
Definition: backend/common/tags.hh:27
BCRSMatrix(Backend::unattached_container=Backend::unattached_container())
Creates an BCRSMatrix without allocating an underlying ISTL matrix.
Definition: bcrsmatrix.hh:93
const E & operator()(const RowIndex &ri, const ColIndex &ci) const
Definition: bcrsmatrix.hh:197
Definition: uncachedmatrixview.hh:13
BCRSMatrix & operator=(const BCRSMatrix &rhs)
Definition: bcrsmatrix.hh:105
Definition: adaptivity.hh:27
BCRSMatrix & operator*=(const E &e)
Definition: bcrsmatrix.hh:186
GFSU TrialGridFunctionSpace
Definition: bcrsmatrix.hh:33
BCRSMatrix(const GO &go, Container &container)
Construct matrix container using an externally given matrix as storage.
Definition: bcrsmatrix.hh:78
GFSU::Ordering::Traits::ContainerIndex ColIndex
Definition: bcrsmatrix.hh:37
C BaseT
Definition: bcrsmatrix.hh:28
E & operator()(const RowIndex &ri, const ColIndex &ci)
Definition: bcrsmatrix.hh:192
void flush()
Definition: bcrsmatrix.hh:230
BCRSMatrix(const BCRSMatrix &rhs)
Definition: bcrsmatrix.hh:101
size_type N() const
Definition: bcrsmatrix.hh:170