ug4
ivector.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 IVECTOR_H_
34 #define IVECTOR_H_
35 
36 #include "util.h"
37 #include "te.h"
38 #include "ivector.h"
39 #include "vec_functions.h"
40 
41 namespace ug{
43 // Vector
45 
46 
48 class IVector : public TE_Vector<IVector>
49 {
50 public:
51 
52 // INTERFACE
54  virtual SmartPtr<IVector> clone() const = 0;
56  virtual SmartPtr<IVector> clone_without_values(size_t size) const = 0;
57 
58  virtual size_t capacity() const = 0;
59  virtual size_t size() const = 0;
60  virtual void resize(size_t newSize, bool bCopyValues=true) = 0;
61  virtual void clear() = 0;
62 
63 
64  // algebra
65  // use IVector_AlgebraDownCastTo to cast all these functions to VecAdd etc.
66  virtual void vec_add(double a1, double a2, const IVector &iv2) = 0;
67  virtual void vec_add(double a1, double a2, const IVector &iv2, double a3, const IVector &iv3) = 0;
68  virtual void vec_add(double a1, double a2, const IVector &iv2, double a3, const IVector &iv3, double a4, const IVector &iv4) = 0;
69  virtual double vec_prod(const IVector &v) const = 0;
70  virtual double norm2() const = 0;
71 
72  virtual void set(double d) = 0;
73 
74  virtual void print(std::ostream &output) const = 0;
75  virtual std::string short_desc() const =0;
76 
77 
78 // HELPER
79  template<typename T>
80  T &downcast()
81  {
82  T *t =dynamic_cast<T*>(this);
83  UG_ASSERT(t != NULL, "could not downcast " << TypeName(this) << " to " << TypeName<T>());
84  return *t;
85  }
86  template<typename T>
87  const T &downcast() const
88  {
89  const T *t =dynamic_cast<const T*>(this);
90  UG_ASSERT(t != NULL, "could not downcast " << TypeName(this) << " to " << TypeName<T>());
91  return *t;
92  }
93 
94 // IMPLEMENTATIONS
95  double norm() const { return sqrt(norm2()); }
96 
97  void operator = (const IVector &v)
98  {
99  vec_add(0.0, 1.0, v);
100  }
101  void operator += (const IVector &v)
102  {
103  vec_add(1.0, 1.0, v);
104  }
105  void operator -= (const IVector &v)
106  {
107  vec_add(1.0, -1.0, v);
108  }
109 
110  void operator *= (double alpha)
111  {
112  vec_add(0.0, alpha, *this);
113  }
114 
115 // TEMPLATE EXPRESSIONS IMPLEMENTATIONS
117  {
118  vec_add(0.0, t.scaling(), t.vec());
119  }
121  {
122  vec_add(0.0, t.a1, t.v1, t.a2, t.v2);
123  }
125  {
126  vec_add(0.0, t.a1, t.v1, t.a2, t.v2, t.a3, t.v3);
127  }
129  {
130  vec_add(1.0, t.a1, t.v1, t.a2, t.v2);
131  }
133  {
134  vec_add(1.0, t.a1, t.v1, t.a2, t.v2, t.a3, t.v3);
135  }
137  {
138  vec_add(1.0, -t.a1, t.v1, -t.a2, t.v2);
139  }
141  {
142  vec_add(1.0, -t.a1, t.v1, -t.a2, t.v2, -t.a3, t.v3);
143  }
144 };
145 
146 template<typename TVec>
148 {
150  TVec &downcast()
151  {
152  return *((TVec*) this);
153  }
154  const TVec &downcast() const
155  {
156  return *((TVec*) this);
157  }
158 public:
159  virtual void vec_add(double a1, double a2, const IVector &iv2)
160  {
161  VecAdd(a1, downcast(), a2, iv2.downcast<TVec>());
162  }
163  virtual void vec_add(double a1, double a2, const IVector &iv2, double a3, const IVector &iv3)
164  {
165  VecAdd(a1, downcast(), a2, iv2.downcast<TVec>(), a3, iv3.downcast<TVec>());
166  }
167  virtual void vec_add(double a1, double a2, const IVector &iv2, double a3, const IVector &iv3, double a4, const IVector &iv4)
168  {
169  VecAdd(a1, downcast(), a2, iv2.downcast<TVec>(), a3, iv3.downcast<TVec>(), a4, iv4.downcast<TVec>());
170  }
171  virtual double vec_prod(const IVector &iv) const
172  {
173  return VecProd(downcast(), iv.downcast<TVec>());
174  }
175 
176  virtual double norm2() const
177  {
178  return VecNorm2(downcast());
179  }
180 
181  virtual void set(double d)
182  {
183  TVec &t = downcast();
184  for(size_t i=0; i<t.size(); i++)
185  t[i] = d;
186  }
187 
188  virtual void print(std::ostream &output) const
189  {
190  const TVec &t = downcast();
191  output << short_desc() << "\n";
192  for(size_t i=0; i<t.size(); i++)
193  output << i << " = " << t[i] << "\n";
194  }
195 
196  virtual std::string short_desc() const
197  {
198  std::stringstream ss;
199  ss << TypeName(downcast()) << "/IVector" << "[" << size() << "]";
200  return ss.str();
201  }
202 
203  friend std::ostream &operator<<(std::ostream &output, const this_type &v)
204  {
205  v.print(output);
206  return output;
207  }
208 
209 };
210 
211 } // namespace ug
212 
213 
214 
215 #endif /* IVECTOR_H_ */
Definition: smart_pointer.h:108
Definition: ivector.h:148
const TVec & downcast() const
Definition: ivector.h:154
TVec & downcast()
Definition: ivector.h:150
virtual void vec_add(double a1, double a2, const IVector &iv2, double a3, const IVector &iv3, double a4, const IVector &iv4)
Definition: ivector.h:167
virtual void vec_add(double a1, double a2, const IVector &iv2)
Definition: ivector.h:159
virtual void set(double d)
Definition: ivector.h:181
friend std::ostream & operator<<(std::ostream &output, const this_type &v)
Definition: ivector.h:203
virtual void print(std::ostream &output) const
Definition: ivector.h:188
virtual std::string short_desc() const
Definition: ivector.h:196
virtual double vec_prod(const IVector &iv) const
Definition: ivector.h:171
virtual double norm2() const
Definition: ivector.h:176
IVector_AlgebraDownCastTo< TVec > this_type
Definition: ivector.h:149
virtual void vec_add(double a1, double a2, const IVector &iv2, double a3, const IVector &iv3)
Definition: ivector.h:163
Definition: ivector.h:49
virtual void set(double d)=0
virtual void print(std::ostream &output) const =0
double norm() const
Definition: ivector.h:95
virtual SmartPtr< IVector > clone() const =0
clones the vector (deep-copy) including values
virtual size_t capacity() const =0
virtual SmartPtr< IVector > clone_without_values(size_t size) const =0
clones the vector (deep-copy) excluding values
virtual void clear()=0
void operator-=(const IVector &v)
Definition: ivector.h:105
virtual void vec_add(double a1, double a2, const IVector &iv2)=0
virtual double norm2() const =0
const T & downcast() const
Definition: ivector.h:87
virtual void vec_add(double a1, double a2, const IVector &iv2, double a3, const IVector &iv3, double a4, const IVector &iv4)=0
virtual double vec_prod(const IVector &v) const =0
virtual std::string short_desc() const =0
void operator*=(double alpha)
Definition: ivector.h:110
virtual void resize(size_t newSize, bool bCopyValues=true)=0
void operator=(const IVector &v)
Definition: ivector.h:97
virtual size_t size() const =0
virtual void vec_add(double a1, double a2, const IVector &iv2, double a3, const IVector &iv3)=0
void operator+=(const IVector &v)
Definition: ivector.h:101
T & downcast()
Definition: ivector.h:80
Definition: te.h:106
const T & v1
Definition: te.h:109
double a1
Definition: te.h:108
double a2
Definition: te.h:110
const T & v2
Definition: te.h:111
Definition: te.h:119
const T & v3
Definition: te.h:126
double a2
Definition: te.h:123
double a3
Definition: te.h:125
const T & v1
Definition: te.h:122
const T & v2
Definition: te.h:124
double a1
Definition: te.h:121
Definition: te.h:57
double scaling() const
Definition: te.h:63
const T & vec() const
Definition: te.h:64
Definition: te.h:73
number alpha
#define UG_ASSERT(expr, msg)
Definition: assert.h:70
void VecAdd(vector_t &vOut, const vector_t &v1, const vector_t &v2)
adds two MathVector<N>s and stores the result in a third one
Definition: math_vector_functions_common_impl.hpp:185
double VecNorm2(const IVector &v1)
Definition: ivector_vec_functions.h:85
the ug namespace
double VecProd(const double &a, const double &b)
returns scal<a, b>
Definition: operations_vec.h:84
std::string TypeName(const T &t)
Definition: typename.h:40