ug4
scalar_matrix_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_MATRIX_ADAPTER_HH_
34 #define SCALAR_MATRIX_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 MT>
53 inline void TruncateOffDiag(MT &A, size_t ncmp)
54 { UG_ASSERT(0, "Implement!"); }
55 
56 template<>
57 inline void TruncateOffDiag(CPUAlgebra::matrix_type &A, size_t ncmp)
58 {
60 
61  // for each row
62  for (size_t i=0; i<A.num_rows(); ++i)
63  {
64  // for each column
65  for (matrix_type::row_iterator matij = A.begin_row(i);
66  matij != A.end_row(i); ++matij)
67  {
68  size_t j = matij.index();
69  if ((i%ncmp) != (j%ncmp))
70  {
71  // std::cerr << "Eliminating " << i << ", " << j<< std::endl;
72  matij.value() = 0.0;
73  }
74  }
75  }
76 
77 }
78 
79 template<class AT, class ST=CPUAlgebra>
81 
82 public:
83  typedef typename AT::matrix_type encapsulated_matrix_type;
85  static const int blockSize = AT::blockSize;
86 
87  //typedef typename ST::matrix_type::const_row_iterator const_row_iterator;
88 
89  ScalarMatrixAdapter(encapsulated_matrix_type& mat) : m_src(mat), m_const(mat) {};
90 
91  void resize_and_clear(size_t newRows, size_t newCols)
92  { m_src.resize_and_clear(newRows/blockSize, newCols/blockSize);}
93 
94  bool resize_and_keep_values(size_t newRows, size_t newCols)
95  {return m_src.resize_and_keep_values(newRows/blockSize, newCols/blockSize);}
96 
97 
98  value_type &operator() (size_t r, size_t c)
99  {
100  UG_ASSERT(r < num_rows(), "row index is too large");
101  UG_ASSERT(c < num_cols(), "col index is too large");
102  return BlockRef(m_src(r/blockSize, c/blockSize), r%blockSize, c%blockSize);
103  };
104 
105  const value_type &operator () (size_t r, size_t c) const
106  {
107  UG_ASSERT(r < num_rows(), "row index is too large");
108  UG_ASSERT(c < num_cols(), "col index is too large");
109  return BlockRef(m_src(r/blockSize, c/blockSize), r%blockSize, c%blockSize);
110  }
111 
113  size_t num_rows() const
114  { return m_src.num_rows()*blockSize; }
115 
117  size_t num_cols() const
118  { return m_src.num_cols()*blockSize; }
119 
121  size_t total_num_connections() const
122  { return m_src.total_num_connections()*blockSize*blockSize; }
123 
125  void print(const char *text) const {m_src.print(text);}
126 
128  void printrow(size_t row) const {m_src.printrow(row/blockSize);}
129 
131  friend std::ostream& operator<<(std::ostream& os, ScalarMatrixAdapter<AT,ST> const &a)
132  { a.outputToStream(os); return os; }
133 
141  {
142  typename encapsulated_matrix_type::row_iterator iter;
143  public:
144  inline void check() const {iter.check(); }
145  row_iterator(typename encapsulated_matrix_type::row_iterator _iter)
146  : iter(_iter) {}
148  row_iterator *operator ->() { return iter.operator->(); }
149  bool operator != (const row_iterator &o) const { return *iter != o->iter; }
150  void operator ++ () { ++iter; }
151  void operator += (int nr) { iter+=nr; }
152  bool operator == (const row_iterator &other) const { return other->iter == *iter;}
153  size_t index() const { return iter.index(); }
154  value_type &value() { return BlockRef(iter.value(), blockSize, blockSize); }
155  };
156 
158  {
159  typename encapsulated_matrix_type::const_row_iterator iter;
160  public:
161  inline void check() const {iter.check(); }
162  const_row_iterator(typename encapsulated_matrix_type::const_row_iterator _iter)
163  : iter(_iter) {}
165  const_row_iterator *operator ->() { return iter.operator->(); }
166  bool operator != (const const_row_iterator &o) const { return iter!= o.iter; }
167  void operator ++ () { ++iter; }
168  void operator += (int nr) { iter+=nr; }
169  bool operator == (const const_row_iterator &other) const { return other.iter == iter;}
170  size_t index() const { return iter.index(); }
171  const value_type &value() const { return BlockRef(iter.value(), blockSize, blockSize); }
172  };
173 
174 
176  { return row_iterator(m_src.begin_row(r)); }
178  { return row_iterator(m_src.end_row(r)); }
179 
181  { return const_row_iterator(m_const.begin_row(r)); }
182  const_row_iterator end_row(size_t r) const
183  { return const_row_iterator(m_const.end_row(r)); }
184 
185 protected:
186  std::ostream& outputToStream(std::ostream& os) const
187  { return os << m_src; }
188 
191 };
192 
193 
194 // partielle Spezialisierung fuer CPUAlgebra
195 template<>
198 { return m_src(r,c); };
199 
200 #endif /* SPARSEMATRIXPROXY_HH_ */
Definition: scalar_matrix_adapter.hh:158
~const_row_iterator()
Definition: scalar_matrix_adapter.hh:164
void check() const
Definition: scalar_matrix_adapter.hh:161
encapsulated_matrix_type::const_row_iterator iter
Definition: scalar_matrix_adapter.hh:159
const value_type & value() const
Definition: scalar_matrix_adapter.hh:171
const_row_iterator(typename encapsulated_matrix_type::const_row_iterator _iter)
Definition: scalar_matrix_adapter.hh:162
size_t index() const
Definition: scalar_matrix_adapter.hh:170
Definition: scalar_matrix_adapter.hh:141
void check() const
Definition: scalar_matrix_adapter.hh:144
encapsulated_matrix_type::row_iterator iter
Definition: scalar_matrix_adapter.hh:142
~row_iterator()
Definition: scalar_matrix_adapter.hh:147
value_type & value()
Definition: scalar_matrix_adapter.hh:154
row_iterator(typename encapsulated_matrix_type::row_iterator _iter)
Definition: scalar_matrix_adapter.hh:145
size_t index() const
Definition: scalar_matrix_adapter.hh:153
Definition: scalar_matrix_adapter.hh:80
ScalarMatrixAdapter(encapsulated_matrix_type &mat)
Definition: scalar_matrix_adapter.hh:89
void resize_and_clear(size_t newRows, size_t newCols)
Definition: scalar_matrix_adapter.hh:91
bool resize_and_keep_values(size_t newRows, size_t newCols)
Definition: scalar_matrix_adapter.hh:94
const encapsulated_matrix_type & m_const
Definition: scalar_matrix_adapter.hh:190
value_type & operator()(size_t r, size_t c)
Definition: scalar_matrix_adapter.hh:98
size_t total_num_connections() const
returns the total number of connections
Definition: scalar_matrix_adapter.hh:121
AT::matrix_type encapsulated_matrix_type
Definition: scalar_matrix_adapter.hh:83
row_iterator begin_row(size_t r)
Definition: scalar_matrix_adapter.hh:175
encapsulated_matrix_type & m_src
Definition: scalar_matrix_adapter.hh:189
size_t num_rows() const
returns number of rows
Definition: scalar_matrix_adapter.hh:113
ST::matrix_type::value_type value_type
Definition: scalar_matrix_adapter.hh:84
const_row_iterator end_row(size_t r) const
Definition: scalar_matrix_adapter.hh:182
row_iterator end_row(size_t r)
Definition: scalar_matrix_adapter.hh:177
size_t num_cols() const
returns the number of cols
Definition: scalar_matrix_adapter.hh:117
std::ostream & outputToStream(std::ostream &os) const
Definition: scalar_matrix_adapter.hh:186
void printrow(size_t row) const
print (block) row of (underlying) matrix
Definition: scalar_matrix_adapter.hh:128
const_row_iterator begin_row(size_t r) const
Definition: scalar_matrix_adapter.hh:180
void print(const char *text) const
print (underlying) matrix
Definition: scalar_matrix_adapter.hh:125
friend std::ostream & operator<<(std::ostream &os, ScalarMatrixAdapter< AT, ST > const &a)
operator overloading for streams
Definition: scalar_matrix_adapter.hh:131
Wrapper for sequential matrices to handle them in parallel.
Definition: parallel_matrix.h:65
#define UG_ASSERT(expr, msg)
Definition: assert.h:70
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
void TruncateOffDiag(MT &A, size_t ncmp)
Definition: scalar_matrix_adapter.hh:53
T value_type
Definition: sparsematrix_interface.h:2
size_t num_rows() const
Definition: sparsematrix_interface.h:38
value_type & operator()(size_t r, size_t c)
size_t num_cols() const
Definition: sparsematrix_interface.h:39