Loading...
Searching...
No Matches
Vector.h
// Copyright (C) 2007 Garth N. Wells
//
// This file is part of DOLFIN.
//
// DOLFIN is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// DOLFIN is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with DOLFIN. If not, see <http://www.gnu.org/licenses/>.
#ifndef __DOLFIN_VECTOR_H
#define __DOLFIN_VECTOR_H
#include <string>
#include <utility>
#include <memory>
#include <dolfin/common/types.h>
#include "DefaultFactory.h"
#include "GenericVector.h"
namespace dolfin
{
template<typename T> class Array;
class Vector : public GenericVector
{
public:
Vector() : Vector(MPI_COMM_WORLD) {}
explicit Vector(MPI_Comm comm)
{
DefaultFactory factory;
vector = factory.create_vector(comm);
}
Vector(MPI_Comm comm, std::size_t N)
{
DefaultFactory factory;
vector = factory.create_vector(comm);
vector->init(N);
}
Vector(const Vector& x) : vector(x.vector->copy()) {}
Vector(const GenericVector& x) : vector(x.copy()) {}
//--- Implementation of the GenericTensor interface ---
virtual std::shared_ptr<GenericVector> copy() const
{
std::shared_ptr<Vector> x(new Vector(*this));
return x;
}
virtual void zero()
{ vector->zero(); }
virtual void apply(std::string mode)
{ vector->apply(mode); }
virtual MPI_Comm mpi_comm() const
{ return vector->mpi_comm(); }
virtual std::string str(bool verbose) const
{ return "<Vector wrapper of " + vector->str(verbose) + ">"; }
//--- Implementation of the GenericVector interface ---
virtual void init(std::size_t N)
{ vector->init(N); }
virtual void init(std::pair<std::size_t, std::size_t> range)
{ vector->init(range); }
virtual void init(std::pair<std::size_t, std::size_t> range,
const std::vector<std::size_t>& local_to_global_map,
const std::vector<la_index>& ghost_indices)
{ vector->init(range, local_to_global_map, ghost_indices); }
// Bring init function from GenericVector into scope
using GenericVector::init;
virtual bool empty() const
{ return vector->empty(); }
virtual std::size_t size() const
{ return vector->size(); }
virtual std::size_t local_size() const
{ return vector->local_size(); }
virtual std::pair<std::int64_t, std::int64_t> local_range() const
{ return vector->local_range(); }
virtual bool owns_index(std::size_t i) const
{ return vector->owns_index(i); }
virtual void get(double* block, std::size_t m,
const dolfin::la_index* rows) const
{ vector->get(block, m, rows); }
virtual void get_local(double* block, std::size_t m,
const dolfin::la_index* rows) const
{ vector->get_local(block, m, rows); }
virtual void set(const double* block, std::size_t m,
const dolfin::la_index* rows)
{ vector->set(block, m, rows); }
virtual void set_local(const double* block, std::size_t m,
const dolfin::la_index* rows)
{ vector->set_local(block, m, rows); }
virtual void add(const double* block, std::size_t m,
const dolfin::la_index* rows)
{ vector->add(block, m, rows); }
virtual void add_local(const double* block, std::size_t m,
const dolfin::la_index* rows)
{ vector->add_local(block, m, rows); }
virtual void get_local(std::vector<double>& values) const
{ vector->get_local(values); }
virtual void set_local(const std::vector<double>& values)
{ vector->set_local(values); }
virtual void add_local(const Array<double>& values)
{ vector->add_local(values); }
virtual void gather(GenericVector& x,
const std::vector<dolfin::la_index>& indices) const
{ vector->gather(x, indices); }
virtual void gather(std::vector<double>& x,
const std::vector<dolfin::la_index>& indices) const
{ vector->gather(x, indices); }
virtual void gather_on_zero(std::vector<double>& x) const
{ vector->gather_on_zero(x); }
virtual void axpy(double a, const GenericVector& x)
{ vector->axpy(a, x); }
virtual void abs()
{ vector->abs(); }
virtual double inner(const GenericVector& x) const
{ return vector->inner(x); }
virtual double norm(std::string norm_type) const
{ return vector->norm(norm_type); }
virtual double min() const
{ return vector->min(); }
virtual double max() const
{ return vector->max(); }
virtual double sum() const
{ return vector->sum(); }
virtual double sum(const Array<std::size_t>& rows) const
{ return vector->sum(rows); }
virtual const Vector& operator*= (double a)
{ *vector *= a; return *this; }
virtual const Vector& operator*= (const GenericVector& x)
{ *vector *= x; return *this; }
virtual const Vector& operator/= (double a)
{ *this *= 1.0 / a; return *this; }
virtual const Vector& operator+= (const GenericVector& x)
{ axpy(1.0, x); return *this; }
virtual const GenericVector& operator+= (double a)
{ *vector += a; return *this; }
virtual const Vector& operator-= (const GenericVector& x)
{ axpy(-1.0, x); return *this; }
virtual const GenericVector& operator-= (double a)
{ *vector -= a; return *this; }
virtual const GenericVector& operator= (const GenericVector& x)
{ *vector = x; return *this; }
const Vector& operator= (double a)
{ *vector = a; return *this; }
//--- Special functions ---
virtual GenericLinearAlgebraFactory& factory() const
{ return vector->factory(); }
//--- Special functions, intended for library use only ---
virtual const GenericVector* instance() const
{ return vector.get(); }
virtual GenericVector* instance()
{ return vector.get(); }
virtual std::shared_ptr<const LinearAlgebraObject> shared_instance() const
{ return vector; }
virtual std::shared_ptr<LinearAlgebraObject> shared_instance()
{ return vector; }
//--- Special Vector functions ---
const Vector& operator= (const Vector& x)
{ *vector = *x.vector; return *this; }
private:
// Pointer to concrete implementation
std::shared_ptr<GenericVector> vector;
};
}
#endif
dolfin::Array
Definition Array.h:42
dolfin::DefaultFactory
Default linear algebra factory based on global parameter "linear_algebra_backend".
Definition DefaultFactory.h:36
dolfin::GenericLinearAlgebraFactory
Base class for LinearAlgebra factories.
Definition GenericLinearAlgebraFactory.h:47
dolfin::GenericVector
This class defines a common interface for vectors.
Definition GenericVector.h:48
dolfin::GenericVector::init
virtual void init(const TensorLayout &tensor_layout)
Definition GenericVector.h:58
dolfin::GenericVector::get
virtual void get(double *block, const dolfin::la_index *num_rows, const dolfin::la_index *const *rows) const
Get block of values using global indices.
Definition GenericVector.h:104
dolfin::Vector::gather_on_zero
virtual void gather_on_zero(std::vector< double > &x) const
Gather all entries into x on process 0.
Definition Vector.h:185
dolfin::Vector::operator+=
virtual const Vector & operator+=(const GenericVector &x)
Add given vector.
Definition Vector.h:232
dolfin::Vector::Vector
Vector(MPI_Comm comm, std::size_t N)
Create vector of size N.
Definition Vector.h:51
dolfin::Vector::instance
virtual GenericVector * instance()
Return concrete instance / unwrap (non-const version).
Definition Vector.h:268
dolfin::Vector::axpy
virtual void axpy(double a, const GenericVector &x)
Add multiple of given vector (AXPY operation).
Definition Vector.h:189
dolfin::Vector::copy
virtual std::shared_ptr< GenericVector > copy() const
Return copy of vector.
Definition Vector.h:67
dolfin::Vector::Vector
Vector(MPI_Comm comm)
Create empty vector.
Definition Vector.h:44
dolfin::Vector::owns_index
virtual bool owns_index(std::size_t i) const
Determine whether global vector index is owned by this process.
Definition Vector.h:126
dolfin::Vector::min
virtual double min() const
Return minimum value of vector.
Definition Vector.h:205
dolfin::Vector::init
virtual void init(std::pair< std::size_t, std::size_t > range)
Initialize vector with given ownership range.
Definition Vector.h:96
dolfin::Vector::abs
virtual void abs()
Replace all entries in the vector by their absolute values.
Definition Vector.h:193
dolfin::Vector::norm
virtual double norm(std::string norm_type) const
Return norm of vector.
Definition Vector.h:201
dolfin::Vector::add_local
virtual void add_local(const Array< double > &values)
Add values to each entry on local process.
Definition Vector.h:170
dolfin::Vector::empty
virtual bool empty() const
Return true if vector is empty.
Definition Vector.h:110
dolfin::Vector::operator=
virtual const GenericVector & operator=(const GenericVector &x)
Assignment operator.
Definition Vector.h:248
dolfin::Vector::set_local
virtual void set_local(const double *block, std::size_t m, const dolfin::la_index *rows)
Set block of values using local indices.
Definition Vector.h:147
dolfin::Vector::operator*=
virtual const Vector & operator*=(double a)
Multiply vector by given number.
Definition Vector.h:220
dolfin::Vector::shared_instance
virtual std::shared_ptr< const LinearAlgebraObject > shared_instance() const
Return concrete shared ptr instance / unwrap (const version).
Definition Vector.h:271
dolfin::Vector::get_local
virtual void get_local(double *block, std::size_t m, const dolfin::la_index *rows) const
Definition Vector.h:137
dolfin::Vector::Vector
Vector(const GenericVector &x)
Create a Vector from a GenericVector.
Definition Vector.h:62
dolfin::Vector::operator/=
virtual const Vector & operator/=(double a)
Divide vector by given number.
Definition Vector.h:228
dolfin::Vector::get_local
virtual void get_local(std::vector< double > &values) const
Get all values on local process.
Definition Vector.h:162
dolfin::Vector::inner
virtual double inner(const GenericVector &x) const
Return inner product with given vector.
Definition Vector.h:197
dolfin::Vector::size
virtual std::size_t size() const
Return size of vector.
Definition Vector.h:114
dolfin::Vector::local_size
virtual std::size_t local_size() const
Return local size of vector.
Definition Vector.h:118
dolfin::Vector::init
virtual void init(std::size_t N)
Initialize vector to size N.
Definition Vector.h:92
dolfin::Vector::shared_instance
virtual std::shared_ptr< LinearAlgebraObject > shared_instance()
Return concrete shared ptr instance / unwrap (non-const version).
Definition Vector.h:274
dolfin::Vector::mpi_comm
virtual MPI_Comm mpi_comm() const
Return MPI communicator.
Definition Vector.h:82
dolfin::Vector::sum
virtual double sum() const
Return sum of values of vector.
Definition Vector.h:213
dolfin::Vector::gather
virtual void gather(std::vector< double > &x, const std::vector< dolfin::la_index > &indices) const
Gather entries (given by global indices) into x.
Definition Vector.h:180
dolfin::Vector::operator-=
virtual const Vector & operator-=(const GenericVector &x)
Subtract given vector.
Definition Vector.h:240
dolfin::Vector::instance
virtual const GenericVector * instance() const
Return concrete instance / unwrap (const version).
Definition Vector.h:264
dolfin::Vector::factory
virtual GenericLinearAlgebraFactory & factory() const
Return linear algebra backend factory.
Definition Vector.h:258
dolfin::Vector::set_local
virtual void set_local(const std::vector< double > &values)
Set all values on local process.
Definition Vector.h:166
dolfin::Vector::zero
virtual void zero()
Set all entries to zero and keep any sparse structure.
Definition Vector.h:74
dolfin::Vector::local_range
virtual std::pair< std::int64_t, std::int64_t > local_range() const
Return local ownership range of a vector.
Definition Vector.h:122
dolfin::Vector::max
virtual double max() const
Return maximum value of vector.
Definition Vector.h:209
dolfin::Vector::Vector
Vector()
Create empty vector.
Definition Vector.h:41
dolfin::Vector::set
virtual void set(const double *block, std::size_t m, const dolfin::la_index *rows)
Set block of values using global indices.
Definition Vector.h:142
dolfin::Vector::Vector
Vector(const Vector &x)
Copy constructor.
Definition Vector.h:59
dolfin::Vector::init
virtual void init(std::pair< std::size_t, std::size_t > range, const std::vector< std::size_t > &local_to_global_map, const std::vector< la_index > &ghost_indices)
Definition Vector.h:101
dolfin::Vector::add_local
virtual void add_local(const double *block, std::size_t m, const dolfin::la_index *rows)
Add block of values using local indices.
Definition Vector.h:157
dolfin::Vector::apply
virtual void apply(std::string mode)
Finalize assembly of tensor.
Definition Vector.h:78
dolfin::Vector::gather
virtual void gather(GenericVector &x, const std::vector< dolfin::la_index > &indices) const
Definition Vector.h:175
dolfin::Vector::get
virtual void get(double *block, std::size_t m, const dolfin::la_index *rows) const
Definition Vector.h:131
dolfin::Vector::add
virtual void add(const double *block, std::size_t m, const dolfin::la_index *rows)
Add block of values using global indices.
Definition Vector.h:152
dolfin::Vector::str
virtual std::string str(bool verbose) const
Return informal string representation (pretty-print).
Definition Vector.h:86
dolfin::Vector::sum
virtual double sum(const Array< std::size_t > &rows) const
Definition Vector.h:216
dolfin
Definition adapt.h:30
dolfin::la_index
PetscInt la_index
Index type for compatibility with linear algebra backend(s).
Definition types.h:32