ug4
local_helper.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2010-2015: G-CSC, Goethe University Frankfurt
3  * Author: Martin Rupp
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 /*
34  * Diese Funktionen sollen das Benutzen von SparseMatrix::add(M&m) erleichtern.
35  */
36 #ifndef __H__UG__MARTIN_ALGEBRA__LOCAL_HELPER__
37 #define __H__UG__MARTIN_ALGEBRA__LOCAL_HELPER__
38 
39 namespace ug
40 {
41 
42 
43 template<typename M>
45 {
46 public:
47  localMatrix_from_mat_and_array(M &m_, const size_t *rows_, const size_t *cols_) : rows(rows_), cols(cols_)
48  {
49  m = &m_;
50  }
51 
53  {
54 
55  }
56 
57  size_t num_rows() const { return m->num_rows(); }
58  size_t num_cols() const { return m->num_cols(); }
59  size_t row_index(size_t i) const { return rows[i]; }
60  size_t col_index(size_t i) const { return cols[i]; }
61  typename M::value_type &operator()(size_t i, size_t j) { return (*m)(i,j); }
62  const typename M::value_type &operator()(size_t i, size_t j) const { return (*m)(i,j); }
63 
64 private:
65  M *m;
66  const size_t *rows;
67  const size_t *cols;
68 };
69 
70 template<typename M>
72 {
73 public:
74  const_localMatrix_from_mat_and_array(const M &m_, const size_t *rows_, const size_t *cols_) : m(m_), rows(rows_), cols(cols_)
75  { }
76 
77  size_t num_rows() const { return m.num_rows(); }
78  size_t num_cols() const { return m.num_cols(); }
79  size_t row_index(size_t i) const { return rows[i]; }
80  size_t col_index(size_t i) const { return cols[i]; }
81  const typename M::value_type &operator()(size_t i, size_t j) const { return m(i,j); }
82 
83 private:
84  const M &m;
85  const size_t *rows;
86  const size_t *cols;
87 };
88 
89 template<typename T>
91 {
92 public:
93  localMatrix_from_col_major_and_array(size_t numrows_, size_t numcols_, T *m_, const size_t *rows_, const size_t *cols_)
94  : m(m_), rows(rows_), cols(cols_)
95  {
96  numrows = numrows_;
97  numcols = numcols_;
98  }
99 
100  size_t num_rows() const { return numrows; }
101  size_t num_cols() const { return numcols; }
102  size_t row_index(size_t i) const { return rows[i]; }
103  size_t col_index(size_t i) const { return cols[i]; }
104  T &operator()(size_t i, size_t j) { return m[i + j*numcols]; }
105  const T &operator()(size_t i, size_t j) const { return m[i + j*numcols]; }
106 
107 private:
108  T *m;
109  size_t numrows;
110  size_t numcols;
111  const size_t *rows;
112  const size_t *cols;
113 };
114 
115 template<typename T>
117 {
118 public:
119  localMatrix_from_row_major_and_array(size_t numrows_, size_t numcols_, T *m_, const size_t *rows_, const size_t *cols_)
120  : m(m_), rows(rows_), cols(cols_)
121  {
122  numrows = numrows_;
123  numcols = numcols_;
124  }
125 
126  size_t num_rows() const { return numrows; }
127  size_t num_cols() const { return numcols; }
128  size_t row_index(size_t i) const { return rows[i]; }
129  size_t col_index(size_t i) const { return cols[i]; }
130  T &operator()(size_t i, size_t j) { return m[i*numrows + j]; }
131  const T &operator()(size_t i, size_t j) const { return m[i*numrows + j]; }
132 
133 private:
134  T *m;
135  const size_t *rows;
136  const size_t *cols;
137  size_t numrows;
138  size_t numcols;
139 };
140 
141 template<typename T>
143 {
144 public:
145  localVector_from_array(size_t N_, T *v_, const size_t *indices_)
146  : N(N_), v(v_), indices(indices_)
147  {
148  }
149 
150  size_t size() const { return N; }
151  size_t index(size_t i) const { return indices[i]; }
152 
153  T &operator[](size_t i) { return v[i]; }
154  const T &operator[](size_t i) const { return v[i]; }
155 
156 private:
157  size_t N;
158  T *v;
159  const size_t *indices;
160 };
161 
162 
163 
165 
166 
178 template<typename TGlobalMatrix, typename TLocalMatrix>
179 inline bool AddLocalMatrix(TGlobalMatrix &mat, const TLocalMatrix &localMat)
180 {
181  mat.add(localMat);
182  return true;
183 }
184 template<typename TGlobalMatrix, typename TLocalMatrix>
185 inline bool SetLocalMatrix(TGlobalMatrix &mat, const TLocalMatrix &localMat)
186 {
187  mat.set(localMat);
188  return true;
189 }
190 template<typename TGlobalMatrix, typename TLocalMatrix>
191 inline bool GetLocalMatrix(const TGlobalMatrix &mat, TLocalMatrix &localMat)
192 {
193  mat.get(localMat);
194  return true;
195 }
196 
197 template<typename TGlobalMatrix, typename TLocalMatrix>
198 inline bool AddLocalMatrix(TGlobalMatrix &mat, const TLocalMatrix &localMat, const size_t *rowIndices, const size_t *colIndices)
199 {
200  const_localMatrix_from_mat_and_array<TLocalMatrix> loc(localMat, rowIndices, colIndices);
201  return AddLocalMatrix(mat, loc);
202 }
203 
204 template<typename TGlobalMatrix, typename TLocalMatrix>
205 inline bool SetLocalMatrix(TGlobalMatrix &mat, TLocalMatrix &localMat, const size_t *rowIndices, const size_t *colIndices)
206 {
207  const_localMatrix_from_mat_and_array<TLocalMatrix> loc(localMat, rowIndices, colIndices);
208  return AddLocalMatrix(mat, loc);
209 }
210 
211 template<typename TGlobalMatrix, typename TLocalMatrix>
212 inline bool GetLocalMatrix(const TGlobalMatrix &mat, TLocalMatrix &localMat, const size_t *rowIndices, const size_t *colIndices)
213 {
214  localMatrix_from_mat_and_array<TLocalMatrix> loc(localMat, rowIndices, colIndices);
215  return GetLocalMatrix(mat, loc);
216 }
217 
218 
219 }
220 
221 #endif
Definition: local_helper.h:72
const_localMatrix_from_mat_and_array(const M &m_, const size_t *rows_, const size_t *cols_)
Definition: local_helper.h:74
const M & m
Definition: local_helper.h:84
const size_t * cols
Definition: local_helper.h:86
size_t col_index(size_t i) const
Definition: local_helper.h:80
const size_t * rows
Definition: local_helper.h:85
size_t row_index(size_t i) const
Definition: local_helper.h:79
const M::value_type & operator()(size_t i, size_t j) const
Definition: local_helper.h:81
size_t num_cols() const
Definition: local_helper.h:78
size_t num_rows() const
Definition: local_helper.h:77
Definition: local_helper.h:91
size_t col_index(size_t i) const
Definition: local_helper.h:103
T & operator()(size_t i, size_t j)
Definition: local_helper.h:104
const size_t * cols
Definition: local_helper.h:112
size_t row_index(size_t i) const
Definition: local_helper.h:102
size_t num_cols() const
Definition: local_helper.h:101
size_t num_rows() const
Definition: local_helper.h:100
const T & operator()(size_t i, size_t j) const
Definition: local_helper.h:105
localMatrix_from_col_major_and_array(size_t numrows_, size_t numcols_, T *m_, const size_t *rows_, const size_t *cols_)
Definition: local_helper.h:93
T * m
Definition: local_helper.h:108
const size_t * rows
Definition: local_helper.h:111
size_t numrows
Definition: local_helper.h:109
size_t numcols
Definition: local_helper.h:110
Definition: local_helper.h:45
localMatrix_from_mat_and_array(M &m_, const size_t *rows_, const size_t *cols_)
Definition: local_helper.h:47
M::value_type & operator()(size_t i, size_t j)
Definition: local_helper.h:61
const size_t * rows
Definition: local_helper.h:66
size_t row_index(size_t i) const
Definition: local_helper.h:59
size_t num_cols() const
Definition: local_helper.h:58
size_t num_rows() const
Definition: local_helper.h:57
size_t col_index(size_t i) const
Definition: local_helper.h:60
M * m
Definition: local_helper.h:65
const M::value_type & operator()(size_t i, size_t j) const
Definition: local_helper.h:62
~localMatrix_from_mat_and_array()
Definition: local_helper.h:52
const size_t * cols
Definition: local_helper.h:67
Definition: local_helper.h:117
size_t num_cols() const
Definition: local_helper.h:127
size_t row_index(size_t i) const
Definition: local_helper.h:128
size_t numrows
Definition: local_helper.h:137
size_t numcols
Definition: local_helper.h:138
size_t num_rows() const
Definition: local_helper.h:126
size_t col_index(size_t i) const
Definition: local_helper.h:129
localMatrix_from_row_major_and_array(size_t numrows_, size_t numcols_, T *m_, const size_t *rows_, const size_t *cols_)
Definition: local_helper.h:119
const T & operator()(size_t i, size_t j) const
Definition: local_helper.h:131
const size_t * rows
Definition: local_helper.h:135
T * m
Definition: local_helper.h:134
const size_t * cols
Definition: local_helper.h:136
T & operator()(size_t i, size_t j)
Definition: local_helper.h:130
Definition: local_helper.h:143
size_t size() const
Definition: local_helper.h:150
T * v
Definition: local_helper.h:158
size_t N
Definition: local_helper.h:157
T & operator[](size_t i)
Definition: local_helper.h:153
size_t index(size_t i) const
Definition: local_helper.h:151
const size_t * indices
Definition: local_helper.h:159
localVector_from_array(size_t N_, T *v_, const size_t *indices_)
Definition: local_helper.h:145
const T & operator[](size_t i) const
Definition: local_helper.h:154
the ug namespace
bool SetLocalMatrix(TGlobalMatrix &mat, const TLocalMatrix &localMat)
Definition: local_helper.h:185
bool GetLocalMatrix(const TGlobalMatrix &mat, TLocalMatrix &localMat)
Definition: local_helper.h:191
bool AddLocalMatrix(TGlobalMatrix &mat, const TLocalMatrix &localMat)
Definition: local_helper.h:179
T value_type
Definition: sparsematrix_interface.h:2