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