MFEM  v3.1
Finite element discretization library
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Pages
vector.hpp
Go to the documentation of this file.
1 // Copyright (c) 2010, Lawrence Livermore National Security, LLC. Produced at
2 // the Lawrence Livermore National Laboratory. LLNL-CODE-443211. All Rights
3 // reserved. See file COPYRIGHT for details.
4 //
5 // This file is part of the MFEM library. For more information and source code
6 // availability see http://mfem.org.
7 //
8 // MFEM is free software; you can redistribute it and/or modify it under the
9 // terms of the GNU Lesser General Public License (as published by the Free
10 // Software Foundation) version 2.1 dated February 1999.
11 
12 #ifndef MFEM_VECTOR
13 #define MFEM_VECTOR
14 
15 // Data type vector
16 
17 #include "../general/array.hpp"
18 #include <cmath>
19 #include <iostream>
20 #if defined(_MSC_VER) && (_MSC_VER < 1800)
21 #include <float.h>
22 #define isfinite _finite
23 #endif
24 
25 namespace mfem
26 {
27 
30 inline int CheckFinite(const double *v, const int n);
31 
33 class Vector
34 {
35 protected:
36 
38  double * data;
39 
40 public:
41 
43  Vector () { allocsize = size = 0; data = 0; }
44 
46  Vector(const Vector &);
47 
49  explicit Vector (int s);
50 
52  Vector (double *_data, int _size)
53  { data = _data; size = _size; allocsize = -size; }
54 
56  void Load (std::istream ** in, int np, int * dim);
57 
59  void Load(std::istream &in, int Size);
60 
62  void Load(std::istream &in) { int s; in >> s; Load (in, s); }
63 
65  void SetSize(int s);
66 
67  void SetData(double *d) { data = d; }
68 
69  void SetDataAndSize(double *d, int s)
70  { data = d; size = s; allocsize = -s; }
71 
72  void NewDataAndSize(double *d, int s)
73  {
74  if (allocsize > 0) { delete [] data; }
75  SetDataAndSize(d, s);
76  }
77 
78  void MakeDataOwner() { allocsize = abs(allocsize); }
79 
81  void Destroy();
82 
84  inline int Size() const { return size; }
85 
86  // double *GetData() { return data; }
87 
88  inline double *GetData() const { return data; }
89 
90  inline operator double *() { return data; }
91 
92  inline operator const double *() const { return data; }
93 
94  inline bool OwnsData() const { return (allocsize > 0); }
95 
97  inline void StealData(double **p)
98  { *p = data; data = 0; size = allocsize = 0; }
99 
101  inline double *StealData() { double *p; StealData(&p); return p; }
102 
104  double & Elem (int i);
105 
107  const double & Elem (int i) const;
108 
110  inline double & operator() (int i);
111 
113  inline const double & operator() (int i) const;
114 
115  double operator*(const double *) const;
116 
118  double operator*(const Vector &v) const;
119 
120  Vector & operator=(const double *v);
121 
123  Vector & operator=(const Vector &v);
124 
126  Vector & operator=(double value);
127 
128  Vector & operator*=(double c);
129 
130  Vector & operator/=(double c);
131 
132  Vector & operator-=(double c);
133 
134  Vector & operator-=(const Vector &v);
135 
136  Vector & operator+=(const Vector &v);
137 
139  Vector & Add(const double a, const Vector &Va);
140 
142  Vector & Set(const double a, const Vector &x);
143 
144  void SetVector (const Vector &v, int offset);
145 
147  void Neg();
148 
150  inline void Swap(Vector &other);
151 
153  friend void add(const Vector &v1, const Vector &v2, Vector &v);
154 
156  friend void add(const Vector &v1, double alpha, const Vector &v2, Vector &v);
157 
159  friend void add(const double a, const Vector &x, const Vector &y, Vector &z);
160 
162  friend void add (const double a, const Vector &x,
163  const double b, const Vector &y, Vector &z);
164 
166  friend void subtract(const Vector &v1, const Vector &v2, Vector &v);
167 
169  friend void subtract(const double a, const Vector &x,
170  const Vector &y, Vector &z);
171 
173  void median(const Vector &lo, const Vector &hi);
174 
175  void GetSubVector(const Array<int> &dofs, Vector &elemvect) const;
176  void GetSubVector(const Array<int> &dofs, double *elem_data) const;
177 
178  void SetSubVector(const Array<int> &dofs, const Vector &elemvect);
179  void SetSubVector(const Array<int> &dofs, double *elem_data);
180 
182  void AddElementVector(const Array<int> & dofs, const Vector & elemvect);
183  void AddElementVector(const Array<int> & dofs, double *elem_data);
184  void AddElementVector(const Array<int> & dofs, const double a,
185  const Vector & elemvect);
186 
188  void SetSubVectorComplement(const Array<int> &dofs, const double val);
189 
191  void Print(std::ostream & out = std::cout, int width = 8) const;
192 
194  void Print_HYPRE(std::ostream &out) const;
195 
197  void Randomize(int seed = 0);
199  double Norml2() const;
201  double Normlinf() const;
203  double Norml1() const;
205  double Normlp(double p) const;
207  double Max() const;
209  double Min() const;
211  double Sum() const;
213  double DistanceTo (const double *p) const;
214 
217  int CheckFinite() const { return mfem::CheckFinite(data, size); }
218 
220  virtual ~Vector ();
221 };
222 
223 // Inline methods
224 
225 inline int CheckFinite(const double *v, const int n)
226 {
227  // isfinite didn't appear in a standard until C99, and later C++11
228  // It wasn't standard in C89 or C++98. PGI as of 14.7 still defines
229  // it as a macro, which sort of screws up everybody else.
230  int bad = 0;
231  for (int i = 0; i < n; i++)
232  {
233 #ifdef isfinite
234  if (!isfinite(v[i]))
235 #else
236  if (!std::isfinite(v[i]))
237 #endif
238  {
239  bad++;
240  }
241  }
242  return bad;
243 }
244 
245 inline Vector::Vector (int s)
246 {
247  if (s > 0)
248  {
249  allocsize = size = s;
250  data = new double[s];
251  }
252  else
253  {
254  allocsize = size = 0;
255  data = NULL;
256  }
257 }
258 
259 inline void Vector::SetSize(int s)
260 {
261  if (s == size)
262  {
263  return;
264  }
265  if (s <= abs(allocsize))
266  {
267  size = s;
268  return;
269  }
270  if (allocsize > 0)
271  {
272  delete [] data;
273  }
274  allocsize = size = s;
275  data = new double[s];
276 }
277 
278 inline void Vector::Destroy()
279 {
280  if (allocsize > 0)
281  {
282  delete [] data;
283  }
284  allocsize = size = 0;
285  data = NULL;
286 }
287 
288 inline double & Vector::operator() (int i)
289 {
290  MFEM_ASSERT(data && i >= 0 && i < size,
291  "index [" << i << "] is out of range [0," << size << ")");
292 
293  return data[i];
294 }
295 
296 inline const double & Vector::operator() (int i) const
297 {
298  MFEM_ASSERT(data && i >= 0 && i < size,
299  "index [" << i << "] is out of range [0," << size << ")");
300 
301  return data[i];
302 }
303 
304 inline void Vector::Swap(Vector &other)
305 {
306  mfem::Swap(size, other.size);
308  mfem::Swap(data, other.data);
309 }
310 
312 template<> inline void Swap<Vector>(Vector &a, Vector &b)
313 {
314  a.Swap(b);
315 }
316 
318 {
319  if (allocsize > 0)
320  {
321  delete [] data;
322  }
323 }
324 
325 inline double Distance(const double *x, const double *y, const int n)
326 {
327  using namespace std;
328  double d = 0.0;
329 
330  for (int i = 0; i < n; i++)
331  {
332  d += (x[i]-y[i])*(x[i]-y[i]);
333  }
334 
335  return sqrt(d);
336 }
337 
338 }
339 
340 #endif
void SetVector(const Vector &v, int offset)
Definition: vector.cpp:233
int CheckFinite(const double *v, const int n)
Definition: vector.hpp:225
Vector()
Default constructor for Vector. Sets size = 0 and data = NULL.
Definition: vector.hpp:43
void NewDataAndSize(double *d, int s)
Definition: vector.hpp:72
double & Elem(int i)
Sets value in vector. Index i = 0 .. size-1.
Definition: vector.cpp:75
void Print(std::ostream &out=std::cout, int width=8) const
Prints vector to stream out.
Definition: vector.cpp:583
void SetSize(int s)
Resizes the vector if the new size is different.
Definition: vector.hpp:259
void MakeDataOwner()
Definition: vector.hpp:78
friend void subtract(const Vector &v1, const Vector &v2, Vector &v)
Do v = v1 - v2.
Definition: vector.cpp:385
double Norml2() const
Returns the l2 norm of the vector.
Definition: vector.cpp:644
double & operator()(int i)
Sets value in vector. Index i = 0 .. size-1.
Definition: vector.hpp:288
void GetSubVector(const Array< int > &dofs, Vector &elemvect) const
Definition: vector.cpp:457
void StealData(double **p)
Changes the ownership of the data; after the call the Vector is empty.
Definition: vector.hpp:97
int Size() const
Returns the size of the vector.
Definition: vector.hpp:84
void Swap< Vector >(Vector &a, Vector &b)
Specialization of the template function Swap&lt;&gt; for class Vector.
Definition: vector.hpp:312
double Normlinf() const
Returns the l_infinity norm of the vector.
Definition: vector.cpp:649
void Randomize(int seed=0)
Set random values in the vector.
Definition: vector.cpp:625
double * GetData() const
Definition: vector.hpp:88
double operator*(const double *) const
Definition: vector.cpp:85
Vector & operator=(const double *v)
Definition: vector.cpp:112
void Load(std::istream **in, int np, int *dim)
Reads a vector from multiple files.
Definition: vector.cpp:45
bool OwnsData() const
Definition: vector.hpp:94
double Normlp(double p) const
Returns the l_p norm of the vector.
Definition: vector.cpp:674
int dim
Definition: ex3.cpp:48
void Swap(Vector &other)
Swap the contents of two Vectors.
Definition: vector.hpp:304
void median(const Vector &lo, const Vector &hi)
v = median(v,lo,hi) entrywise. Implementation assumes lo &lt;= hi.
Definition: vector.cpp:440
void Load(std::istream &in)
Load a vector from an input stream.
Definition: vector.hpp:62
double * StealData()
Changes the ownership of the data; after the call the Vector is empty.
Definition: vector.hpp:101
void SetData(double *d)
Definition: vector.hpp:67
void AddElementVector(const Array< int > &dofs, const Vector &elemvect)
Add (element) subvector to the vector.
Definition: vector.cpp:527
Vector & operator/=(double c)
Definition: vector.cpp:151
double DistanceTo(const double *p) const
Compute the Euclidean distance to another vector.
Definition: vector.cpp:738
void SetSubVectorComplement(const Array< int > &dofs, const double val)
Set all vector entries NOT in the &#39;dofs&#39; array to the given &#39;val&#39;.
Definition: vector.cpp:575
Vector(double *_data, int _size)
Creates a vector referencing an array of doubles, owned by someone else.
Definition: vector.hpp:52
Vector & operator*=(double c)
Definition: vector.cpp:142
double Min() const
Returns the minimal element of the vector.
Definition: vector.cpp:713
double Norml1() const
Returns the l_1 norm of the vector.
Definition: vector.cpp:662
void Swap(Array< T > &, Array< T > &)
Definition: array.hpp:314
double Distance(const double *x, const double *y, const int n)
Definition: vector.hpp:325
void SetSubVector(const Array< int > &dofs, const Vector &elemvect)
Definition: vector.cpp:493
void Print_HYPRE(std::ostream &out) const
Prints vector to stream out in HYPRE_Vector format.
Definition: vector.cpp:607
void SetDataAndSize(double *d, int s)
Definition: vector.hpp:69
Vector & Set(const double a, const Vector &x)
(*this) = a * x
Definition: vector.cpp:218
Vector & Add(const double a, const Vector &Va)
(*this) += a * Va
Definition: vector.cpp:200
double Max() const
Returns the maximal element of the vector.
Definition: vector.cpp:700
void Destroy()
Destroy a vector.
Definition: vector.hpp:278
Vector & operator-=(double c)
Definition: vector.cpp:161
int CheckFinite() const
Definition: vector.hpp:217
Vector data type.
Definition: vector.hpp:33
friend void add(const Vector &v1, const Vector &v2, Vector &v)
Do v = v1 + v2.
Definition: vector.cpp:259
int allocsize
Definition: vector.hpp:37
Vector & operator+=(const Vector &v)
Definition: vector.cpp:185
double * data
Definition: vector.hpp:38
double Sum() const
Return the sum of the vector entries.
Definition: vector.cpp:726
virtual ~Vector()
Destroys vector.
Definition: vector.hpp:317
void Neg()
(*this) = -(*this)
Definition: vector.cpp:251