Line data Source code
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>
44 : class localMatrix_from_mat_and_array
45 : {
46 : public:
47 0 : localMatrix_from_mat_and_array(M &m_, const size_t *rows_, const size_t *cols_) : rows(rows_), cols(cols_)
48 : {
49 0 : m = &m_;
50 : }
51 :
52 : ~localMatrix_from_mat_and_array()
53 : {
54 :
55 0 : }
56 :
57 0 : size_t num_rows() const { return m->num_rows(); }
58 0 : size_t num_cols() const { return m->num_cols(); }
59 0 : size_t row_index(size_t i) const { return rows[i]; }
60 0 : size_t col_index(size_t i) const { return cols[i]; }
61 0 : 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>
71 : class const_localMatrix_from_mat_and_array
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>
90 : class localMatrix_from_col_major_and_array
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>
116 : class localMatrix_from_row_major_and_array
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>
142 : class localVector_from_array
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 :
164 : ////////////////////////////////////////////////////////////////////////////////////////////////////
165 :
166 :
167 : /** Add a local matrix
168 : *
169 : * The local matrix type must declare the following members:
170 : * - num_rows()
171 : * - num_cols()
172 : * - row_index(size_t i)
173 : * - col_index(size_t j)
174 : * - operator()(size_t i, size_t j)
175 : * so that mat(i,j) will go to SparseMat(mat.row_index(i), mat.col_index(j))
176 : * \param mat the whole local matrix type
177 : */
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 0 : 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
|