ug4
additional_math.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2013-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 #ifndef __UG__ADDITIONAL_MATH_H__
34 #define __UG__ADDITIONAL_MATH_H__
35 
36 #include "smart_ptr_vector.h"
37 
38 #define PINVIT_PROFILE_FUNC() PROFILE_FUNC_GROUP("pinvit algebra")
39 #define PINVIT_PROFILE_BEGIN(t) PROFILE_BEGIN_GROUP(t, "pinvit algebra")
40 #define PINVIT_PROFILE_END() PROFILE_END()
41 
42 namespace ug{
43 
44 /*template<typename mat_type, typename vec_type, typename densematrix_type>
45 void MultiEnergyProd(const SparseMatrix<mat_type> &A,
46  Vector<vec_type> **x,
47  DenseMatrix<densematrix_type> &rA, size_t n)
48 {
49  UG_ASSERT(n == rA.num_rows() && n == rA.num_cols(), "");
50  vec_type Ai_xc;
51  rA = 0.0;
52  for(size_t i=0; i<A.num_rows(); i++)
53  {
54  for(size_t c=0; c<n; c++)
55  {
56  // Ai_xc = A[i] * x[c].
57  Ai_xc = 0.0;
58  A.mat_mult_add_row(i, Ai_xc, 1.0, (*x[c]));
59  for(size_t r=0; r<n; r++)
60  rA(c, r) += VecDot((*x[r])[i], Ai_xc);
61  }
62  }
63 }*/
64 
65 
66 inline bool absCompare(double a, double b)
67 {
68  return fabs(a) < fabs(b);
69 }
70 
71 
72 
73 template<typename vector_type, typename densematrix_type>
75  DenseMatrix<densematrix_type> &rA, size_t n)
76 {
78 // UG_ASSERT(0, "");
79  UG_ASSERT(n == rA.num_rows() && n == rA.num_cols(), "");
80  for(size_t r=0; r<n; r++)
81  for(size_t c=r; c<n; c++)
82  rA(r, c) = px[c]->dotprod(*px[r]);
83 
84  for(size_t r=0; r<n; r++)
85  for(size_t c=0; c<r; c++)
86  rA(r,c) = rA(c, r);
87 }
88 
89 template<typename matrix_type, typename vector_type>
91 {
93 
94 #ifdef UG_PARALLEL
96  v2.change_storage_type(PST_CONSISTENT);
97 #endif
98  A.apply(tmp, v2);
99  // tmp is additive, v1 is consistent
100  double a = v1.dotprod(tmp);
101  //UG_LOG("EnergyProd " << a << "\n");
102 
103  return a;
104 }
105 
106 template<typename matrix_type, typename vector_type>
108 {
109  vector_type t;
110  CloneVector(t, v1);
111  return EnergyProd(v1, A, v2, t);
112 }
113 
114 template<typename matrix_type, typename vector_type>
116 {
117  return sqrt( EnergyProd(x, A, x, tmp) );
118 }
119 
120 template<typename matrix_type, typename vector_type>
122 {
123  vector_type tmp;
124  CloneVector(tmp, x);
125  return sqrt( EnergyProd(x, A, x, tmp) );
126 }
127 
128 
129 template<typename matrix_type, typename vector_type, typename densematrix_type>
132  DenseMatrix<densematrix_type> &rA, size_t n)
133 {
135 #ifdef UG_PARALLEL
137 #endif
138  UG_ASSERT(n == rA.num_rows() && n == rA.num_cols(), "");
139  vector_type t;
140 
141 #if 0
142  CloneVector(t, px(0));
143 
144  for(size_t r=0; r<n; r++)
145  {
146 // t.set(0.0);
147  #ifdef UG_PARALLEL
148  px(r).change_storage_type(PST_CONSISTENT);
149  #endif
150  A.apply(t, px(r));
151  // tmp is additive, v1 is consistent
152  //UG_LOG("EnergyProd " << a << "\n");
153  for(size_t c=r; c<n; c++)
154  {
155  double a = px(c).dotprod(t);
156  rA(c, r) = rA(r, c) = a; //EnergyProd(px(r), A, px(c), t);
157  }
158  }
159 
160 #else
161  CloneVector(t, *px[0]);
162 
163 #ifdef UG_PARALLEL
164  for(size_t r=0; r<n; r++)
165  px[r]->change_storage_type(PST_CONSISTENT);
166 #endif
167 
168  for(size_t r=0; r<n; r++)
169  {
170  // todo: why is SparseMatrix<T>::apply not const ?!?
171  A.apply(t, *px[r]);
172  // t additive
173 
174  for(size_t c=r; c<n; c++)
175  rA(c, r) = rA(r, c) = px[c]->dotprod(t);
176  }
177 
178 #endif
179 }
180 
181 
182 template<typename tvector>
183 void PrintStorageType(const tvector &v)
184 {
185 #ifdef UG_PARALLEL
186  if(v.has_storage_type(PST_UNDEFINED))
187  UG_LOG("PST_UNDEFINED ");
188  if(v.has_storage_type(PST_CONSISTENT))
189  UG_LOG("PST_CONSISTENT ");
190  if(v.has_storage_type(PST_ADDITIVE))
191  UG_LOG("PST_ADDITIVE ");
192  if(v.has_storage_type(PST_UNIQUE))
193  UG_LOG("PST_UNIQUE ");
194 #else
195  UG_LOG("serial ");
196 #endif
197 }
198 
199 
200 template<typename matrix_type>
201 void PrintMatrix(const matrix_type &mat, const char *name)
202 {
203  UG_LOG(name << ":\n" << name << " := matrix([\n");
204  for(size_t r=0; r<mat.num_rows(); r++)
205  {
206  UG_LOG("[");
207  for(size_t c=0; c<mat.num_cols(); c++)
208  {
209  UG_LOG(mat(r, c));
210  if(c < mat.num_cols()-1) UG_LOG(",\t");
211  }
212  UG_LOG("]\n");
213  }
214  UG_LOG("]);\n");
215 
216 }
217 
218 template<typename matrix_type>
219 void PrintMaple(const matrix_type &mat, const char *name)
220 {
221  UG_LOG(name << ":\n" << name << " := matrix([");
222  for(size_t r=0; r<mat.num_rows(); r++)
223  {
224  UG_LOG("[");
225  for(size_t c=0; c<mat.num_cols(); c++)
226  {
227  UG_LOG(mat(r, c));
228  if(c < mat.num_cols()-1) UG_LOG(", ");
229  }
230  UG_LOG("]");
231  if(r < mat.num_rows()-1) UG_LOG(", ");
232  }
233  UG_LOG("]);\n");
234  UG_LOG("(" << mat.num_rows() << " x " << mat.num_cols() << ")\n");
235 
236 }
237 
238 template<typename T>
239 void MemSwap(T &a, T &b)
240 {
241  char c[sizeof(T)];
242  memcpy(c, &a, sizeof(T));
243  memcpy(&a, &b, sizeof(T));
244  memcpy(&b, c, sizeof(T));
245 }
246 
247 } // namespace ug
248 
249 #endif /* __UG__ADDITIONAL_MATH_H__ */
location name
Definition: checkpoint_util.lua:128
Definition: pcl_process_communicator.h:70
Definition: densematrix.h:57
Wrapper for sequential matrices to handle them in parallel.
Definition: parallel_matrix.h:65
bool apply(TPVector &res, const TPVector &x) const
calculate res = A x
Definition: parallel_matrix_impl.h:83
@ PST_UNDEFINED
Definition: parallel_storage_type.h:67
@ PST_CONSISTENT
Definition: parallel_storage_type.h:68
@ PST_UNIQUE
Definition: parallel_storage_type.h:70
@ PST_ADDITIVE
Definition: parallel_storage_type.h:69
#define UG_ASSERT(expr, msg)
Definition: assert.h:70
#define UG_LOG(msg)
Definition: log.h:367
CPUAlgebra::vector_type vector_type
the ug namespace
void CloneVector(Vector< TValueType > &dest, const Vector< TValueType > &src)
Definition: vector_impl.h:341
void MultiScalProd(vector_type &px, DenseMatrix< densematrix_type > &rA, size_t n)
Definition: additional_math.h:74
double EnergyNorm(vector_type &x, matrix_type &A, vector_type &tmp)
Definition: additional_math.h:115
bool absCompare(double a, double b)
Definition: additional_math.h:66
void PrintMaple(const matrix_type &mat, const char *name)
Definition: additional_math.h:219
double EnergyProd(vector_type &v1, matrix_type &A, vector_type &v2, vector_type &tmp)
Definition: additional_math.h:90
void PrintMatrix(const matrix_type &mat, const char *name)
Definition: additional_math.h:201
void MemSwap(T &a, T &b)
Definition: additional_math.h:239
void PrintStorageType(const tvector &v)
Definition: additional_math.h:183
void MultiEnergyProd(matrix_type &A, SmartPtrVector< vector_type > &px, DenseMatrix< densematrix_type > &rA, size_t n)
Definition: additional_math.h:130
#define PINVIT_PROFILE_FUNC()
Definition: additional_math.h:38