MFEM  v4.0
Finite element discretization library
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Pages
Public Member Functions | Protected Attributes | Friends | List of all members
mfem::Vector Class Reference

Vector data type. More...

#include <vector.hpp>

Inheritance diagram for mfem::Vector:
[legend]
Collaboration diagram for mfem::Vector:
[legend]

Public Member Functions

 Vector ()
 Default constructor for Vector. Sets size = 0 and data = NULL. More...
 
 Vector (const Vector &)
 Copy constructor. Allocates a new data array and copies the data. More...
 
 Vector (int s)
 Creates vector of size s. More...
 
 Vector (double *_data, int _size)
 Creates a vector referencing an array of doubles, owned by someone else. More...
 
 Vector (int size_, MemoryType mt)
 Create a Vector of size size_ using MemoryType mt. More...
 
void UseDevice (bool use_dev) const
 Enable execution of Vector operations using the mfem::Device. More...
 
bool UseDevice () const
 Return the device flag of the Memory object used by the Vector. More...
 
void Load (std::istream **in, int np, int *dim)
 Reads a vector from multiple files. More...
 
void Load (std::istream &in, int Size)
 Load a vector from an input stream. More...
 
void Load (std::istream &in)
 Load a vector from an input stream, reading the size from the stream. More...
 
void SetSize (int s)
 Resize the vector to size s. More...
 
void SetSize (int s, MemoryType mt)
 Resize the vector to size s using MemoryType mt. More...
 
void SetSize (int s, Vector &v)
 Resize the vector to size s using the MemoryType of v. More...
 
void SetData (double *d)
 
void SetDataAndSize (double *d, int s)
 Set the Vector data and size. More...
 
void NewDataAndSize (double *d, int s)
 Set the Vector data and size, deleting the old data, if owned. More...
 
void NewMemoryAndSize (const Memory< double > &mem, int s, bool own_mem)
 Reset the Vector to use the given external Memory mem and size s. More...
 
void MakeDataOwner () const
 Set the Vector data (host pointer) ownership flag. More...
 
void Destroy ()
 Destroy a vector. More...
 
int Size () const
 Returns the size of the vector. More...
 
int Capacity () const
 Return the size of the currently allocated data array. More...
 
double * GetData () const
 Return a pointer to the beginning of the Vector data. More...
 
 operator double * ()
 Conversion to double *. More...
 
 operator const double * () const
 Conversion to const double *. More...
 
Memory< double > & GetMemory ()
 Return a reference to the Memory object used by the Vector. More...
 
const Memory< double > & GetMemory () const
 Return a reference to the Memory object used by the Vector, const version. More...
 
void SyncMemory (const Vector &v)
 Update the memory location of the vector to match v. More...
 
void SyncAliasMemory (const Vector &v)
 Update the alias memory location of the vector to match v. More...
 
bool OwnsData () const
 Read the Vector data (host pointer) ownership flag. More...
 
void StealData (double **p)
 Changes the ownership of the data; after the call the Vector is empty. More...
 
double * StealData ()
 Changes the ownership of the data; after the call the Vector is empty. More...
 
double & Elem (int i)
 Access Vector entries. Index i = 0 .. size-1. More...
 
const double & Elem (int i) const
 Read only access to Vector entries. Index i = 0 .. size-1. More...
 
double & operator() (int i)
 Access Vector entries using () for 0-based indexing. More...
 
const double & operator() (int i) const
 Read only access to Vector entries using () for 0-based indexing. More...
 
double operator* (const double *) const
 Dot product with a double * array. More...
 
double operator* (const Vector &v) const
 Return the inner-product. More...
 
Vectoroperator= (const double *v)
 Copy Size() entries from v. More...
 
Vectoroperator= (const Vector &v)
 Copy assignment. More...
 
Vectoroperator= (double value)
 Redefine '=' for vector = constant. More...
 
Vectoroperator*= (double c)
 
Vectoroperator/= (double c)
 
Vectoroperator-= (double c)
 
Vectoroperator-= (const Vector &v)
 
Vectoroperator+= (const Vector &v)
 
VectorAdd (const double a, const Vector &Va)
 (*this) += a * Va More...
 
VectorSet (const double a, const Vector &x)
 (*this) = a * x More...
 
void SetVector (const Vector &v, int offset)
 
void Neg ()
 (*this) = -(*this) More...
 
void Swap (Vector &other)
 Swap the contents of two Vectors. More...
 
void median (const Vector &lo, const Vector &hi)
 v = median(v,lo,hi) entrywise. Implementation assumes lo <= hi. More...
 
void GetSubVector (const Array< int > &dofs, Vector &elemvect) const
 
void GetSubVector (const Array< int > &dofs, double *elem_data) const
 
void SetSubVector (const Array< int > &dofs, const double value)
 Set the entries listed in dofs to the given value. More...
 
void SetSubVector (const Array< int > &dofs, const Vector &elemvect)
 
void SetSubVector (const Array< int > &dofs, double *elem_data)
 
void AddElementVector (const Array< int > &dofs, const Vector &elemvect)
 Add (element) subvector to the vector. More...
 
void AddElementVector (const Array< int > &dofs, double *elem_data)
 
void AddElementVector (const Array< int > &dofs, const double a, const Vector &elemvect)
 
void SetSubVectorComplement (const Array< int > &dofs, const double val)
 Set all vector entries NOT in the 'dofs' array to the given 'val'. More...
 
void Print (std::ostream &out=mfem::out, int width=8) const
 Prints vector to stream out. More...
 
void Print_HYPRE (std::ostream &out) const
 Prints vector to stream out in HYPRE_Vector format. More...
 
void Randomize (int seed=0)
 Set random values in the vector. More...
 
double Norml2 () const
 Returns the l2 norm of the vector. More...
 
double Normlinf () const
 Returns the l_infinity norm of the vector. More...
 
double Norml1 () const
 Returns the l_1 norm of the vector. More...
 
double Normlp (double p) const
 Returns the l_p norm of the vector. More...
 
double Max () const
 Returns the maximal element of the vector. More...
 
double Min () const
 Returns the minimal element of the vector. More...
 
double Sum () const
 Return the sum of the vector entries. More...
 
double DistanceSquaredTo (const double *p) const
 Compute the square of the Euclidean distance to another vector. More...
 
double DistanceTo (const double *p) const
 Compute the Euclidean distance to another vector. More...
 
int CheckFinite () const
 Count the number of entries in the Vector for which isfinite is false, i.e. the entry is a NaN or +/-Inf. More...
 
virtual ~Vector ()
 Destroys vector. More...
 
const double * Read (bool on_dev=true) const
 Shortcut for mfem::Read(vec.GetMemory(), vec.Size(), on_dev). More...
 
const double * HostRead () const
 Shortcut for mfem::Read(vec.GetMemory(), vec.Size(), false). More...
 
double * Write (bool on_dev=true)
 Shortcut for mfem::Write(vec.GetMemory(), vec.Size(), on_dev). More...
 
double * HostWrite ()
 Shortcut for mfem::Write(vec.GetMemory(), vec.Size(), false). More...
 
double * ReadWrite (bool on_dev=true)
 Shortcut for mfem::ReadWrite(vec.GetMemory(), vec.Size(), on_dev). More...
 
double * HostReadWrite ()
 Shortcut for mfem::ReadWrite(vec.GetMemory(), vec.Size(), false). More...
 
 Vector (N_Vector nv)
 Construct a wrapper Vector from SUNDIALS N_Vector. More...
 
virtual N_Vector ToNVector ()
 Return a new wrapper SUNDIALS N_Vector of type SUNDIALS_NVEC_SERIAL. More...
 
virtual void ToNVector (N_Vector &nv)
 Update an existing wrapper SUNDIALS N_Vector to point to this Vector. More...
 

Protected Attributes

Memory< double > data
 
int size
 

Friends

void add (const Vector &v1, const Vector &v2, Vector &v)
 Set v = v1 + v2. More...
 
void add (const Vector &v1, double alpha, const Vector &v2, Vector &v)
 Set v = v1 + alpha * v2. More...
 
void add (const double a, const Vector &x, const Vector &y, Vector &z)
 z = a * (x + y) More...
 
void add (const double a, const Vector &x, const double b, const Vector &y, Vector &z)
 z = a * x + b * y More...
 
void subtract (const Vector &v1, const Vector &v2, Vector &v)
 Set v = v1 - v2. More...
 
void subtract (const double a, const Vector &x, const Vector &y, Vector &z)
 z = a * (x - y) More...
 

Detailed Description

Vector data type.

Definition at line 48 of file vector.hpp.

Constructor & Destructor Documentation

mfem::Vector::Vector ( )
inline

Default constructor for Vector. Sets size = 0 and data = NULL.

Definition at line 58 of file vector.hpp.

mfem::Vector::Vector ( const Vector v)

Copy constructor. Allocates a new data array and copies the data.

Definition at line 33 of file vector.cpp.

mfem::Vector::Vector ( int  s)
inlineexplicit

Creates vector of size s.

Warning
Entries are not initialized to zero!

Definition at line 386 of file vector.hpp.

mfem::Vector::Vector ( double *  _data,
int  _size 
)
inline

Creates a vector referencing an array of doubles, owned by someone else.

The pointer _data can be NULL. The data array can be replaced later with SetData().

Definition at line 70 of file vector.hpp.

mfem::Vector::Vector ( int  size_,
MemoryType  mt 
)
inline

Create a Vector of size size_ using MemoryType mt.

Definition at line 74 of file vector.hpp.

mfem::Vector::~Vector ( )
inlinevirtual

Destroys vector.

Definition at line 495 of file vector.hpp.

mfem::Vector::Vector ( N_Vector  nv)
explicit

Construct a wrapper Vector from SUNDIALS N_Vector.

Definition at line 1038 of file vector.cpp.

Member Function Documentation

Vector & mfem::Vector::Add ( const double  a,
const Vector Va 
)

(*this) += a * Va

Definition at line 190 of file vector.cpp.

void mfem::Vector::AddElementVector ( const Array< int > &  dofs,
const Vector elemvect 
)

Add (element) subvector to the vector.

Definition at line 564 of file vector.cpp.

void mfem::Vector::AddElementVector ( const Array< int > &  dofs,
double *  elem_data 
)

Definition at line 589 of file vector.cpp.

void mfem::Vector::AddElementVector ( const Array< int > &  dofs,
const double  a,
const Vector elemvect 
)

Definition at line 607 of file vector.cpp.

int mfem::Vector::Capacity ( ) const
inline

Return the size of the currently allocated data array.

It is always true that Capacity() >= Size().

Definition at line 154 of file vector.hpp.

int mfem::Vector::CheckFinite ( ) const
inline

Count the number of entries in the Vector for which isfinite is false, i.e. the entry is a NaN or +/-Inf.

Definition at line 320 of file vector.hpp.

void mfem::Vector::Destroy ( )
inline

Destroy a vector.

Definition at line 458 of file vector.hpp.

double mfem::Vector::DistanceSquaredTo ( const double *  p) const
inline

Compute the square of the Euclidean distance to another vector.

Definition at line 517 of file vector.hpp.

double mfem::Vector::DistanceTo ( const double *  p) const
inline

Compute the Euclidean distance to another vector.

Definition at line 522 of file vector.hpp.

double & mfem::Vector::Elem ( int  i)

Access Vector entries. Index i = 0 .. size-1.

Definition at line 83 of file vector.cpp.

const double & mfem::Vector::Elem ( int  i) const

Read only access to Vector entries. Index i = 0 .. size-1.

Definition at line 88 of file vector.cpp.

double* mfem::Vector::GetData ( ) const
inline

Return a pointer to the beginning of the Vector data.

Warning
This method should be used with caution as it gives write access to the data of const-qualified Vectors.

Definition at line 159 of file vector.hpp.

Memory<double>& mfem::Vector::GetMemory ( )
inline

Return a reference to the Memory object used by the Vector.

Definition at line 173 of file vector.hpp.

const Memory<double>& mfem::Vector::GetMemory ( ) const
inline

Return a reference to the Memory object used by the Vector, const version.

Definition at line 177 of file vector.hpp.

void mfem::Vector::GetSubVector ( const Array< int > &  dofs,
Vector elemvect 
) const

Definition at line 472 of file vector.cpp.

void mfem::Vector::GetSubVector ( const Array< int > &  dofs,
double *  elem_data 
) const

Definition at line 487 of file vector.cpp.

const double* mfem::Vector::HostRead ( ) const
inline

Shortcut for mfem::Read(vec.GetMemory(), vec.Size(), false).

Definition at line 330 of file vector.hpp.

double* mfem::Vector::HostReadWrite ( )
inline

Shortcut for mfem::ReadWrite(vec.GetMemory(), vec.Size(), false).

Definition at line 346 of file vector.hpp.

double* mfem::Vector::HostWrite ( )
inline

Shortcut for mfem::Write(vec.GetMemory(), vec.Size(), false).

Definition at line 338 of file vector.hpp.

void mfem::Vector::Load ( std::istream **  in,
int  np,
int *  dim 
)

Reads a vector from multiple files.

Definition at line 51 of file vector.cpp.

void mfem::Vector::Load ( std::istream &  in,
int  Size 
)

Load a vector from an input stream.

Definition at line 73 of file vector.cpp.

void mfem::Vector::Load ( std::istream &  in)
inline

Load a vector from an input stream, reading the size from the stream.

Definition at line 98 of file vector.hpp.

void mfem::Vector::MakeDataOwner ( ) const
inline

Set the Vector data (host pointer) ownership flag.

Definition at line 144 of file vector.hpp.

double mfem::Vector::Max ( ) const

Returns the maximal element of the vector.

Definition at line 816 of file vector.cpp.

void mfem::Vector::median ( const Vector lo,
const Vector hi 
)

v = median(v,lo,hi) entrywise. Implementation assumes lo <= hi.

Definition at line 448 of file vector.cpp.

double mfem::Vector::Min ( ) const

Returns the minimal element of the vector.

Definition at line 980 of file vector.cpp.

void mfem::Vector::Neg ( )

(*this) = -(*this)

Definition at line 230 of file vector.cpp.

void mfem::Vector::NewDataAndSize ( double *  d,
int  s 
)
inline

Set the Vector data and size, deleting the old data, if owned.

The Vector does not assume ownership of the new data. The new size is also used as the new Capacity().

See Also
SetDataAndSize().

Definition at line 131 of file vector.hpp.

void mfem::Vector::NewMemoryAndSize ( const Memory< double > &  mem,
int  s,
bool  own_mem 
)
inline

Reset the Vector to use the given external Memory mem and size s.

If own_mem is false, the Vector will not own any of the pointers of mem.

See Also
NewDataAndSize().

Definition at line 449 of file vector.hpp.

double mfem::Vector::Norml1 ( ) const

Returns the l_1 norm of the vector.

Definition at line 756 of file vector.cpp.

double mfem::Vector::Norml2 ( ) const

Returns the l2 norm of the vector.

Definition at line 709 of file vector.cpp.

double mfem::Vector::Normlinf ( ) const

Returns the l_infinity norm of the vector.

Definition at line 746 of file vector.cpp.

double mfem::Vector::Normlp ( double  p) const

Returns the l_p norm of the vector.

Definition at line 766 of file vector.cpp.

mfem::Vector::operator const double * ( ) const
inline

Conversion to const double *.

Note
This conversion function makes it possible to use [] for indexing in addition to the overloaded operator()(int).

Definition at line 170 of file vector.hpp.

mfem::Vector::operator double * ( )
inline

Conversion to double *.

Note
This conversion function makes it possible to use [] for indexing in addition to the overloaded operator()(int).

Definition at line 165 of file vector.hpp.

double & mfem::Vector::operator() ( int  i)
inline

Access Vector entries using () for 0-based indexing.

Note
If MFEM_DEBUG is enabled, bounds checking is performed.

Definition at line 467 of file vector.hpp.

const double & mfem::Vector::operator() ( int  i) const
inline

Read only access to Vector entries using () for 0-based indexing.

Note
If MFEM_DEBUG is enabled, bounds checking is performed.

Definition at line 475 of file vector.hpp.

double mfem::Vector::operator* ( const double *  v) const

Dot product with a double * array.

Definition at line 93 of file vector.cpp.

double mfem::Vector::operator* ( const Vector v) const

Return the inner-product.

Definition at line 934 of file vector.cpp.

Vector & mfem::Vector::operator*= ( double  c)

Definition at line 138 of file vector.cpp.

Vector & mfem::Vector::operator+= ( const Vector v)

Definition at line 178 of file vector.cpp.

Vector & mfem::Vector::operator-= ( double  c)

Definition at line 157 of file vector.cpp.

Vector & mfem::Vector::operator-= ( const Vector v)

Definition at line 166 of file vector.cpp.

Vector & mfem::Vector::operator/= ( double  c)

Definition at line 147 of file vector.cpp.

Vector & mfem::Vector::operator= ( const double *  v)

Copy Size() entries from v.

Definition at line 106 of file vector.cpp.

Vector & mfem::Vector::operator= ( const Vector v)

Copy assignment.

Note
Defining this method overwrites the implicitly defined copy assignemnt operator.

Definition at line 112 of file vector.cpp.

Vector & mfem::Vector::operator= ( double  value)

Redefine '=' for vector = constant.

Definition at line 129 of file vector.cpp.

bool mfem::Vector::OwnsData ( ) const
inline

Read the Vector data (host pointer) ownership flag.

Definition at line 187 of file vector.hpp.

void mfem::Vector::Print ( std::ostream &  out = mfem::out,
int  width = 8 
) const

Prints vector to stream out.

Definition at line 647 of file vector.cpp.

void mfem::Vector::Print_HYPRE ( std::ostream &  out) const

Prints vector to stream out in HYPRE_Vector format.

Definition at line 671 of file vector.cpp.

void mfem::Vector::Randomize ( int  seed = 0)

Set random values in the vector.

Definition at line 690 of file vector.cpp.

const double* mfem::Vector::Read ( bool  on_dev = true) const
inline

Shortcut for mfem::Read(vec.GetMemory(), vec.Size(), on_dev).

Definition at line 326 of file vector.hpp.

double* mfem::Vector::ReadWrite ( bool  on_dev = true)
inline

Shortcut for mfem::ReadWrite(vec.GetMemory(), vec.Size(), on_dev).

Definition at line 342 of file vector.hpp.

Vector & mfem::Vector::Set ( const double  a,
const Vector x 
)

(*this) = a * x

Definition at line 205 of file vector.cpp.

void mfem::Vector::SetData ( double *  d)
inline

Set the Vector data.

Warning
This method should be called only when OwnsData() is false.

Definition at line 118 of file vector.hpp.

void mfem::Vector::SetDataAndSize ( double *  d,
int  s 
)
inline

Set the Vector data and size.

The Vector does not assume ownership of the new data. The new size is also used as the new Capacity().

Warning
This method should be called only when OwnsData() is false.
See Also
NewDataAndSize().

Definition at line 125 of file vector.hpp.

void mfem::Vector::SetSize ( int  s)
inline

Resize the vector to size s.

If the new size is less than or equal to Capacity() then the internal data array remains the same. Otherwise, the old array is deleted, if owned, and a new array of size s is allocated without copying the previous content of the Vector.

Warning
In the second case above (new size greater than current one), the vector will allocate new data array, even if it did not own the original data! Also, new entries are not initialized!

Definition at line 400 of file vector.hpp.

void mfem::Vector::SetSize ( int  s,
MemoryType  mt 
)
inline

Resize the vector to size s using MemoryType mt.

Definition at line 420 of file vector.hpp.

void mfem::Vector::SetSize ( int  s,
Vector v 
)
inline

Resize the vector to size s using the MemoryType of v.

Definition at line 114 of file vector.hpp.

void mfem::Vector::SetSubVector ( const Array< int > &  dofs,
const double  value 
)

Set the entries listed in dofs to the given value.

Definition at line 498 of file vector.cpp.

void mfem::Vector::SetSubVector ( const Array< int > &  dofs,
const Vector elemvect 
)

Definition at line 519 of file vector.cpp.

void mfem::Vector::SetSubVector ( const Array< int > &  dofs,
double *  elem_data 
)

Definition at line 545 of file vector.cpp.

void mfem::Vector::SetSubVectorComplement ( const Array< int > &  dofs,
const double  val 
)

Set all vector entries NOT in the 'dofs' array to the given 'val'.

Definition at line 633 of file vector.cpp.

void mfem::Vector::SetVector ( const Vector v,
int  offset 
)

Definition at line 217 of file vector.cpp.

int mfem::Vector::Size ( ) const
inline

Returns the size of the vector.

Definition at line 150 of file vector.hpp.

void mfem::Vector::StealData ( double **  p)
inline

Changes the ownership of the data; after the call the Vector is empty.

Definition at line 190 of file vector.hpp.

double* mfem::Vector::StealData ( )
inline

Changes the ownership of the data; after the call the Vector is empty.

Definition at line 194 of file vector.hpp.

double mfem::Vector::Sum ( ) const

Return the sum of the vector entries.

Definition at line 833 of file vector.cpp.

void mfem::Vector::Swap ( Vector other)
inline

Swap the contents of two Vectors.

Definition at line 483 of file vector.hpp.

void mfem::Vector::SyncAliasMemory ( const Vector v)
inline

Update the alias memory location of the vector to match v.

Definition at line 183 of file vector.hpp.

void mfem::Vector::SyncMemory ( const Vector v)
inline

Update the memory location of the vector to match v.

Definition at line 180 of file vector.hpp.

virtual N_Vector mfem::Vector::ToNVector ( )
inlinevirtual

Return a new wrapper SUNDIALS N_Vector of type SUNDIALS_NVEC_SERIAL.

The returned N_Vector must be destroyed by the caller.

Reimplemented in mfem::HypreParVector.

Definition at line 355 of file vector.hpp.

void mfem::Vector::ToNVector ( N_Vector &  nv)
virtual

Update an existing wrapper SUNDIALS N_Vector to point to this Vector.

Reimplemented in mfem::HypreParVector.

Definition at line 1062 of file vector.cpp.

void mfem::Vector::UseDevice ( bool  use_dev) const
inline

Enable execution of Vector operations using the mfem::Device.

The default is to use Backend::CPU (serial execution on each MPI rank), regardless of the mfem::Device configuration.

When appropriate, MFEM functions and class methods will enable the use of the mfem::Device for their Vector parameters.

Some derived classes, e.g. GridFunction, enable the use of the mfem::Device by default.

Definition at line 86 of file vector.hpp.

bool mfem::Vector::UseDevice ( ) const
inline

Return the device flag of the Memory object used by the Vector.

Definition at line 89 of file vector.hpp.

double* mfem::Vector::Write ( bool  on_dev = true)
inline

Shortcut for mfem::Write(vec.GetMemory(), vec.Size(), on_dev).

Definition at line 334 of file vector.hpp.

Friends And Related Function Documentation

void add ( const Vector v1,
const Vector v2,
Vector v 
)
friend

Set v = v1 + v2.

Definition at line 238 of file vector.cpp.

void add ( const Vector v1,
double  alpha,
const Vector v2,
Vector v 
)
friend

Set v = v1 + alpha * v2.

Definition at line 260 of file vector.cpp.

void add ( const double  a,
const Vector x,
const Vector y,
Vector z 
)
friend

z = a * (x + y)

Definition at line 296 of file vector.cpp.

void add ( const double  a,
const Vector x,
const double  b,
const Vector y,
Vector z 
)
friend

z = a * x + b * y

Definition at line 333 of file vector.cpp.

void subtract ( const Vector v1,
const Vector v2,
Vector v 
)
friend

Set v = v1 - v2.

Definition at line 385 of file vector.cpp.

void subtract ( const double  a,
const Vector x,
const Vector y,
Vector z 
)
friend

z = a * (x - y)

Definition at line 411 of file vector.cpp.

Member Data Documentation

Memory<double> mfem::Vector::data
protected

Definition at line 52 of file vector.hpp.

int mfem::Vector::size
protected

Definition at line 53 of file vector.hpp.


The documentation for this class was generated from the following files: