ug4
scalar_submatrix_adapter.hh
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2013-2015: G-CSC, Goethe University Frankfurt
3  * Author: Arne Nägel
4  *
5  * This file is part of UG4.
6  *
7  * UG4 is free software: you can redistribute it and/or modify it under the
8  * terms of the GNU Lesser General Public License version 3 (as published by the
9  * Free Software Foundation) with the following additional attribution
10  * requirements (according to LGPL/GPL v3 §7):
11  *
12  * (1) The following notice must be displayed in the Appropriate Legal Notices
13  * of covered and combined works: "Based on UG4 (www.ug4.org/license)".
14  *
15  * (2) The following notice must be displayed at a prominent place in the
16  * terminal output of covered works: "Based on UG4 (www.ug4.org/license)".
17  *
18  * (3) The following bibliography is recommended for citation and must be
19  * preserved in all covered files:
20  * "Reiter, S., Vogel, A., Heppner, I., Rupp, M., and Wittum, G. A massively
21  * parallel geometric multigrid solver on hierarchically distributed grids.
22  * Computing and visualization in science 16, 4 (2013), 151-164"
23  * "Vogel, A., Reiter, S., Rupp, M., Nägel, A., and Wittum, G. UG4 -- a novel
24  * flexible software system for simulating pde based models on high performance
25  * computers. Computing and visualization in science 16, 4 (2013), 165-179"
26  *
27  * This program is distributed in the hope that it will be useful,
28  * but WITHOUT ANY WARRANTY; without even the implied warranty of
29  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
30  * GNU Lesser General Public License for more details.
31  */
32 
33 #ifndef SCALAR_SUBMATRIX_ADAPTER_HH_
34 #define SCALAR_SUBMATRIX_ADAPTER_HH_
35 
36 #include <cstdlib>
37 
41 
44 
45 #include "common/assert.h"
46 using namespace ug;
47 
48 // provides an interface for matrix of algebra type B for matrices originally of algebra type A
49 // allows to access a CPUBlockAlgebra (AT) as a scalar CPUAlgebra (ST)
50 
51 
52 template<class AT, class ST, int R, int C>
54 
55 
56 public:
57  typedef typename AT::matrix_type encapsulated_matrix_type;
59 
60  //typedef typename ST::matrix_type::const_row_iterator const_row_iterator;
61 
63  : m_src(mat), m_const(mat) {}; //, m_subr(subr), m_subc(subc) {};
64 
65  // forward
66  bool resize_and_clear(size_t newRows, size_t newCols)
67  {return m_src.resize_and_clear(newRows, newCols);}
68 
69  bool resize_and_keep_values(size_t newRows, size_t newCols)
70  {return m_src.resize_and_keep_values(newRows, newCols);}
71 
72 
73  value_type &operator() (size_t r, size_t c)
74  {
75  return BlockRef(m_src(r, c), m_subr, m_subc);
76  };
77  const value_type &operator () (size_t r, size_t c) const
78  {
79  return BlockRef(m_src(r, c), m_subr, m_subc);
80  }
81 
83  size_t num_rows() const
84  { return m_src.num_rows(); }
85 
87  size_t num_cols() const
88  { return m_src.num_cols(); }
89 
90  inline bool is_isolated(size_t i) const
91  { return m_src.is_isolated(i); }
92 
93  bool scale(double d) {return m_src.scale(d);}
94  //SparseMatrix<value_type> &operator *= (double d) { scale(d); return *this; }
95 
97  size_t total_num_connections() const
98  { return m_src.total_num_connections(); }
99 
101  void print(const char *text) const {m_src.print(text);}
102 
104  void printrow(size_t row) const {m_src.printrow(row);}
105 
106 
108  {
109  typename encapsulated_matrix_type::row_iterator iter;
110  public:
111  inline void check() const {iter.check(); }
112  row_iterator(typename encapsulated_matrix_type::row_iterator _iter)
113  : iter(_iter) {}
115  row_iterator *operator ->() { return iter.operator->(); }
116  bool operator != (const row_iterator &o) const { return *iter != o->iter; }
117  void operator ++ () { ++iter; }
118  void operator += (int nr) { iter+=nr; }
119  bool operator == (const row_iterator &other) const { return other->iter == *iter;}
120  size_t index() const { return iter.index(); }
121  value_type &value() { return BlockRef(iter.value(), m_subr, m_subc); }
122  };
123 
125  {
126  typename encapsulated_matrix_type::const_row_iterator iter;
127  public:
128  inline void check() const {iter.check(); }
129  const_row_iterator(typename encapsulated_matrix_type::const_row_iterator _iter)
130  : iter(_iter) {}
132  const_row_iterator *operator ->() { return iter.operator->(); }
133  bool operator != (const const_row_iterator &o) const { return iter!= o.iter; }
134  void operator ++ () { ++iter; }
135  void operator += (int nr) { iter+=nr; }
136  bool operator == (const const_row_iterator &other) const { return other.iter == iter;}
137  size_t index() const { return iter.index(); }
138  const value_type &value() const { return BlockRef(iter.value(), m_subr, m_subc); }
139  };
140 
141 
143  { return row_iterator(m_src.begin_row(r)); }
145  { return row_iterator(m_src.end_row(r)); }
146 
148  { return const_row_iterator(m_const.begin_row(r)); }
149  const_row_iterator end_row(size_t r) const
150  {return const_row_iterator(m_const.end_row(r)); }
151 
152 
153  bool has_connection(size_t r, size_t c) const
154  { return m_src.has_connection(r,c); }
155 
156  row_iterator get_iterator_or_next(size_t r, size_t c)
157  { return row_iterator(m_src.get_iterator_or_next(r,c));}
158 
159  const_row_iterator get_connection(size_t r, size_t c, bool &bFound) const
160  { return const_row_iterator(m_const.get_connection(r,c,bFound));}
161 
162  const_row_iterator get_connection(size_t r, size_t c) const
163  { return const_row_iterator(m_const.get_connection(r,c)); }
164 
165  row_iterator get_connection(size_t r, size_t c)
166  { return row_iterator (m_src.get_connection(r,c));}
167 
168  void defragment()
169  {m_src.defragment();}
170 
171 
172 
173 protected:
176  const static int m_subr=R;
177  const static int m_subc=C;
178 };
179 
180 
181 /*template<>
182 class ScalarSubMatrixAdapter<CPUAlgebra, CPUAlgebra, 0,0> : public CPUAlgebra{};
183 */
184 
185 #endif /* SPARSEMATRIXPROXY_HH_ */
Definition: scalar_submatrix_adapter.hh:125
const_row_iterator(typename encapsulated_matrix_type::const_row_iterator _iter)
Definition: scalar_submatrix_adapter.hh:129
void check() const
Definition: scalar_submatrix_adapter.hh:128
size_t index() const
Definition: scalar_submatrix_adapter.hh:137
const value_type & value() const
Definition: scalar_submatrix_adapter.hh:138
encapsulated_matrix_type::const_row_iterator iter
Definition: scalar_submatrix_adapter.hh:126
~const_row_iterator()
Definition: scalar_submatrix_adapter.hh:131
Definition: scalar_submatrix_adapter.hh:108
void check() const
Definition: scalar_submatrix_adapter.hh:111
size_t index() const
Definition: scalar_submatrix_adapter.hh:120
encapsulated_matrix_type::row_iterator iter
Definition: scalar_submatrix_adapter.hh:109
value_type & value()
Definition: scalar_submatrix_adapter.hh:121
row_iterator(typename encapsulated_matrix_type::row_iterator _iter)
Definition: scalar_submatrix_adapter.hh:112
~row_iterator()
Definition: scalar_submatrix_adapter.hh:114
Definition: scalar_submatrix_adapter.hh:53
const_row_iterator get_connection(size_t r, size_t c, bool &bFound) const
Definition: scalar_submatrix_adapter.hh:159
const_row_iterator get_connection(size_t r, size_t c) const
Definition: scalar_submatrix_adapter.hh:162
AT::matrix_type encapsulated_matrix_type
Definition: scalar_submatrix_adapter.hh:57
size_t num_rows() const
returns number of rows
Definition: scalar_submatrix_adapter.hh:83
const encapsulated_matrix_type & m_const
Definition: scalar_submatrix_adapter.hh:175
size_t num_cols() const
returns the number of cols
Definition: scalar_submatrix_adapter.hh:87
void printrow(size_t row) const
print (block) row of (underlying) matrix
Definition: scalar_submatrix_adapter.hh:104
row_iterator get_iterator_or_next(size_t r, size_t c)
Definition: scalar_submatrix_adapter.hh:156
bool has_connection(size_t r, size_t c) const
Definition: scalar_submatrix_adapter.hh:153
void print(const char *text) const
print (underlying) matrix
Definition: scalar_submatrix_adapter.hh:101
size_t total_num_connections() const
returns the total number of connections
Definition: scalar_submatrix_adapter.hh:97
encapsulated_matrix_type & m_src
Definition: scalar_submatrix_adapter.hh:174
row_iterator end_row(size_t r)
Definition: scalar_submatrix_adapter.hh:144
ST::matrix_type::value_type value_type
Definition: scalar_submatrix_adapter.hh:58
row_iterator get_connection(size_t r, size_t c)
Definition: scalar_submatrix_adapter.hh:165
ScalarSubMatrixAdapter(encapsulated_matrix_type &mat)
Definition: scalar_submatrix_adapter.hh:62
const_row_iterator end_row(size_t r) const
Definition: scalar_submatrix_adapter.hh:149
const_row_iterator begin_row(size_t r) const
Definition: scalar_submatrix_adapter.hh:147
row_iterator begin_row(size_t r)
Definition: scalar_submatrix_adapter.hh:142
bool resize_and_clear(size_t newRows, size_t newCols)
Definition: scalar_submatrix_adapter.hh:66
bool resize_and_keep_values(size_t newRows, size_t newCols)
Definition: scalar_submatrix_adapter.hh:69
bool scale(double d)
Definition: scalar_submatrix_adapter.hh:93
void defragment()
Definition: scalar_submatrix_adapter.hh:168
bool is_isolated(size_t i) const
Definition: scalar_submatrix_adapter.hh:90
bool operator==(const MathVector< N, T > &v, const MathVector< N, T > &w)
Definition: math_vector.h:523
bool operator!=(const MathVector< N, T > &v, const MathVector< N, T > &w)
Definition: math_vector.h:552
the ug namespace
double & BlockRef(T &vec, size_t i)
Definition: blocks.h:66
ReferenceObjectID operator++(ReferenceObjectID &roid, int)
Definition: grid_base_objects.h:89
T value_type
Definition: sparsematrix_interface.h:2
value_type & operator()(size_t r, size_t c)