MFEM  v3.2
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 
50  explicit Vector (int s);
51 
53  Vector (double *_data, int _size)
54  { data = _data; size = _size; allocsize = -size; }
55 
57  void Load (std::istream ** in, int np, int * dim);
58 
60  void Load(std::istream &in, int Size);
61 
63  void Load(std::istream &in) { int s; in >> s; Load (in, s); }
64 
67  void SetSize(int s);
68 
69  void SetData(double *d) { data = d; }
70 
71  void SetDataAndSize(double *d, int s)
72  { data = d; size = s; allocsize = -s; }
73 
74  void NewDataAndSize(double *d, int s)
75  {
76  if (allocsize > 0) { delete [] data; }
77  SetDataAndSize(d, s);
78  }
79 
80  void MakeDataOwner() { allocsize = abs(allocsize); }
81 
83  void Destroy();
84 
86  inline int Size() const { return size; }
87 
88  // double *GetData() { return data; }
89 
90  inline double *GetData() const { return data; }
91 
92  inline operator double *() { return data; }
93 
94  inline operator const double *() const { return data; }
95 
96  inline bool OwnsData() const { return (allocsize > 0); }
97 
99  inline void StealData(double **p)
100  { *p = data; data = 0; size = allocsize = 0; }
101 
103  inline double *StealData() { double *p; StealData(&p); return p; }
104 
106  double & Elem (int i);
107 
109  const double & Elem (int i) const;
110 
112  inline double & operator() (int i);
113 
115  inline const double & operator() (int i) const;
116 
117  double operator*(const double *) const;
118 
120  double operator*(const Vector &v) const;
121 
122  Vector & operator=(const double *v);
123 
125  Vector & operator=(const Vector &v);
126 
128  Vector & operator=(double value);
129 
130  Vector & operator*=(double c);
131 
132  Vector & operator/=(double c);
133 
134  Vector & operator-=(double c);
135 
136  Vector & operator-=(const Vector &v);
137 
138  Vector & operator+=(const Vector &v);
139 
141  Vector & Add(const double a, const Vector &Va);
142 
144  Vector & Set(const double a, const Vector &x);
145 
146  void SetVector (const Vector &v, int offset);
147 
149  void Neg();
150 
152  inline void Swap(Vector &other);
153 
155  friend void add(const Vector &v1, const Vector &v2, Vector &v);
156 
158  friend void add(const Vector &v1, double alpha, const Vector &v2, Vector &v);
159 
161  friend void add(const double a, const Vector &x, const Vector &y, Vector &z);
162 
164  friend void add (const double a, const Vector &x,
165  const double b, const Vector &y, Vector &z);
166 
168  friend void subtract(const Vector &v1, const Vector &v2, Vector &v);
169 
171  friend void subtract(const double a, const Vector &x,
172  const Vector &y, Vector &z);
173 
175  void median(const Vector &lo, const Vector &hi);
176 
177  void GetSubVector(const Array<int> &dofs, Vector &elemvect) const;
178  void GetSubVector(const Array<int> &dofs, double *elem_data) const;
179 
180  void SetSubVector(const Array<int> &dofs, const Vector &elemvect);
181  void SetSubVector(const Array<int> &dofs, double *elem_data);
182 
184  void AddElementVector(const Array<int> & dofs, const Vector & elemvect);
185  void AddElementVector(const Array<int> & dofs, double *elem_data);
186  void AddElementVector(const Array<int> & dofs, const double a,
187  const Vector & elemvect);
188 
190  void SetSubVectorComplement(const Array<int> &dofs, const double val);
191 
193  void Print(std::ostream & out = std::cout, int width = 8) const;
194 
196  void Print_HYPRE(std::ostream &out) const;
197 
199  void Randomize(int seed = 0);
201  double Norml2() const;
203  double Normlinf() const;
205  double Norml1() const;
207  double Normlp(double p) const;
209  double Max() const;
211  double Min() const;
213  double Sum() const;
215  double DistanceTo (const double *p) const;
216 
219  int CheckFinite() const { return mfem::CheckFinite(data, size); }
220 
222  virtual ~Vector ();
223 };
224 
225 // Inline methods
226 
227 inline bool IsFinite(const double &val)
228 {
229  // isfinite didn't appear in a standard until C99, and later C++11
230  // It wasn't standard in C89 or C++98. PGI as of 14.7 still defines
231  // it as a macro, which sort of screws up everybody else.
232 #ifdef isfinite
233  return isfinite(val);
234 #else
235  return std::isfinite(val);
236 #endif
237 }
238 
239 inline int CheckFinite(const double *v, const int n)
240 {
241  int bad = 0;
242  for (int i = 0; i < n; i++)
243  {
244  if (!IsFinite(v[i])) { bad++; }
245  }
246  return bad;
247 }
248 
249 inline Vector::Vector (int s)
250 {
251  if (s > 0)
252  {
253  allocsize = size = s;
254  data = new double[s];
255  }
256  else
257  {
258  allocsize = size = 0;
259  data = NULL;
260  }
261 }
262 
263 inline void Vector::SetSize(int s)
264 {
265  if (s == size)
266  {
267  return;
268  }
269  if (s <= abs(allocsize))
270  {
271  size = s;
272  return;
273  }
274  if (allocsize > 0)
275  {
276  delete [] data;
277  }
278  allocsize = size = s;
279  data = new double[s];
280 }
281 
282 inline void Vector::Destroy()
283 {
284  if (allocsize > 0)
285  {
286  delete [] data;
287  }
288  allocsize = size = 0;
289  data = NULL;
290 }
291 
292 inline double & Vector::operator() (int i)
293 {
294  MFEM_ASSERT(data && i >= 0 && i < size,
295  "index [" << i << "] is out of range [0," << size << ")");
296 
297  return data[i];
298 }
299 
300 inline const double & Vector::operator() (int i) const
301 {
302  MFEM_ASSERT(data && i >= 0 && i < size,
303  "index [" << i << "] is out of range [0," << size << ")");
304 
305  return data[i];
306 }
307 
308 inline void Vector::Swap(Vector &other)
309 {
310  mfem::Swap(size, other.size);
312  mfem::Swap(data, other.data);
313 }
314 
316 template<> inline void Swap<Vector>(Vector &a, Vector &b)
317 {
318  a.Swap(b);
319 }
320 
322 {
323  if (allocsize > 0)
324  {
325  delete [] data;
326  }
327 }
328 
329 inline double Distance(const double *x, const double *y, const int n)
330 {
331  using namespace std;
332  double d = 0.0;
333 
334  for (int i = 0; i < n; i++)
335  {
336  d += (x[i]-y[i])*(x[i]-y[i]);
337  }
338 
339  return sqrt(d);
340 }
341 
342 }
343 
344 #endif
void SetVector(const Vector &v, int offset)
Definition: vector.cpp:233
int CheckFinite(const double *v, const int n)
Definition: vector.hpp:239
Vector()
Default constructor for Vector. Sets size = 0 and data = NULL.
Definition: vector.hpp:43
void NewDataAndSize(double *d, int s)
Definition: vector.hpp:74
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)
Resize the vector if the new size is different.
Definition: vector.hpp:263
void MakeDataOwner()
Definition: vector.hpp:80
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:292
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:99
int Size() const
Returns the size of the vector.
Definition: vector.hpp:86
void Swap< Vector >(Vector &a, Vector &b)
Specialization of the template function Swap&lt;&gt; for class Vector.
Definition: vector.hpp:316
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:90
double operator*(const double *) const
Definition: vector.cpp:85
Vector & operator=(const double *v)
Definition: vector.cpp:112
bool IsFinite(const double &val)
Definition: vector.hpp:227
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:96
double Normlp(double p) const
Returns the l_p norm of the vector.
Definition: vector.cpp:669
int dim
Definition: ex3.cpp:47
void Swap(Vector &other)
Swap the contents of two Vectors.
Definition: vector.hpp:308
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:63
double * StealData()
Changes the ownership of the data; after the call the Vector is empty.
Definition: vector.hpp:103
void SetData(double *d)
Definition: vector.hpp:69
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:733
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:53
Vector & operator*=(double c)
Definition: vector.cpp:142
double Min() const
Returns the minimal element of the vector.
Definition: vector.cpp:708
double Norml1() const
Returns the l_1 norm of the vector.
Definition: vector.cpp:659
void Swap(Array< T > &, Array< T > &)
Definition: array.hpp:322
double Distance(const double *x, const double *y, const int n)
Definition: vector.hpp:329
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:71
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:695
void Destroy()
Destroy a vector.
Definition: vector.hpp:282
Vector & operator-=(double c)
Definition: vector.cpp:161
int CheckFinite() const
Definition: vector.hpp:219
const double alpha
Definition: ex15.cpp:337
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:721
virtual ~Vector()
Destroys vector.
Definition: vector.hpp:321
void Neg()
(*this) = -(*this)
Definition: vector.cpp:251