Loading...
Searching...
No Matches
EigenVector.h
// Copyright (C) 2015 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 __EIGEN_VECTOR_H
#define __EIGEN_VECTOR_H
#include <memory>
#include <string>
#include <utility>
#include <vector>
#include <dolfin/common/types.h>
#include <Eigen/Dense>
#include <dolfin/common/MPI.h>
#include "GenericVector.h"
namespace dolfin
{
template<typename T> class Array;
class EigenVector : public GenericVector
{
public:
EigenVector();
explicit EigenVector(MPI_Comm comm);
EigenVector(MPI_Comm comm, std::size_t N);
EigenVector(const EigenVector& x);
explicit EigenVector(std::shared_ptr<Eigen::VectorXd> x);
virtual ~EigenVector();
//--- Implementation of the GenericTensor interface ---
virtual void zero();
virtual void apply(std::string mode);
virtual MPI_Comm mpi_comm() const
{ return _mpi_comm.comm(); }
virtual std::string str(bool verbose) const;
//--- Implementation of the GenericVector interface ---
virtual std::shared_ptr<GenericVector> copy() const;
virtual void init(std::size_t N)
{
if (!empty())
{
dolfin_error("EigenVector.cpp",
"calling EigenVector::init(...)",
"Cannot call init for a non-empty vector. Use EigenVector::resize instead");
}
resize(N);
}
virtual void init(std::pair<std::size_t, std::size_t> range)
{
if (!empty())
{
dolfin_error("EigenVector.cpp",
"calling EigenVector::init(...)",
"Cannot call init for a non-empty vector. Use EigenVector::resize instead");
}
dolfin_assert(range.first == 0);
const std::size_t size = range.second - range.first;
resize(size);
}
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)
{
if (!empty())
{
dolfin_error("EigenVector.cpp",
"calling EigenVector::init(...)",
"Cannot call init for a non-empty vector. Use EigenVector::resize instead");
}
if (!ghost_indices.empty())
{
dolfin_error("EigenVector.cpp",
"calling EigenVector::init(...)",
"EigenVector does not support ghost values");
}
dolfin_assert(range.first == 0);
const std::size_t size = range.second - range.first;
resize(size);
}
// Bring init function from GenericVector into scope
using GenericVector::init;
virtual bool empty() const;
virtual std::size_t size() const;
virtual std::size_t local_size() const
{ return size(); }
virtual std::pair<std::int64_t, std::int64_t> local_range() const;
virtual bool owns_index(std::size_t i) const;
virtual void get(double* block, std::size_t m,
const dolfin::la_index* rows) const
{ get_local(block, m, rows); }
virtual void get_local(double* block, std::size_t m,
const dolfin::la_index* rows) const;
virtual void set(const double* block, std::size_t m,
const dolfin::la_index* rows);
virtual void set_local(const double* block, std::size_t m,
const dolfin::la_index* rows)
{ set(block, m, rows); }
virtual void add(const double* block, std::size_t m,
const dolfin::la_index* rows);
virtual void add_local(const double* block, std::size_t m,
const dolfin::la_index* rows)
{ add(block, m, rows); }
virtual void get_local(std::vector<double>& values) const;
virtual void set_local(const std::vector<double>& values);
virtual void add_local(const Array<double>& values);
virtual void gather(GenericVector& x,
const std::vector<dolfin::la_index>& indices) const;
virtual void gather(std::vector<double>& x,
const std::vector<dolfin::la_index>& indices) const;
virtual void gather_on_zero(std::vector<double>& x) const;
virtual void axpy(double a, const GenericVector& x);
virtual void abs();
virtual double inner(const GenericVector& x) const;
virtual double norm(std::string norm_type) const;
virtual double min() const;
virtual double max() const;
virtual double sum() const;
virtual double sum(const Array<std::size_t>& rows) const;
virtual const EigenVector& operator*= (double a);
virtual const EigenVector& operator*= (const GenericVector& x);
virtual const EigenVector& operator/= (double a);
virtual const EigenVector& operator+= (const GenericVector& x);
virtual const EigenVector& operator+= (double a);
virtual const EigenVector& operator-= (const GenericVector& x);
virtual const EigenVector& operator-= (double a);
virtual const GenericVector& operator= (const GenericVector& x);
virtual const EigenVector& operator= (double a);
//--- Special functions ---
virtual GenericLinearAlgebraFactory& factory() const;
//--- Special Eigen functions ---
virtual void resize(std::size_t N);
std::shared_ptr<const Eigen::VectorXd> vec() const
{ return _x; }
std::shared_ptr<Eigen::VectorXd> vec()
{ return _x; }
virtual double operator[] (dolfin::la_index i) const
{ return (*_x)(i); }
double& operator[] (dolfin::la_index i)
{ return (*_x)(i); }
const EigenVector& operator= (const EigenVector& x);
double* data();
const double* data() const;
private:
static void check_mpi_size(const MPI_Comm comm)
{
if (dolfin::MPI::size(comm) > 1)
{
dolfin_error("EigenVector.cpp",
"creating EigenVector",
"EigenVector does not support parallel communicators");
}
}
// Pointer to Eigen vector object
std::shared_ptr<Eigen::VectorXd> _x;
// MPI communicator
dolfin::MPI::Comm _mpi_comm;
};
}
#endif
dolfin::Array
Definition Array.h:42
dolfin::EigenVector
Definition EigenVector.h:46
dolfin::EigenVector::~EigenVector
virtual ~EigenVector()
Destructor.
Definition EigenVector.cpp:68
dolfin::EigenVector::copy
virtual std::shared_ptr< GenericVector > copy() const
Create copy of tensor.
Definition EigenVector.cpp:73
dolfin::EigenVector::empty
virtual bool empty() const
Return true if vector is empty.
Definition EigenVector.cpp:79
dolfin::EigenVector::data
double * data()
Return pointer to underlying data.
Definition EigenVector.cpp:379
dolfin::EigenVector::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 EigenVector.h:171
dolfin::EigenVector::size
virtual std::size_t size() const
Return true if vector is empty.
Definition EigenVector.cpp:88
dolfin::EigenVector::resize
virtual void resize(std::size_t N)
Resize vector to size N.
Definition EigenVector.cpp:368
dolfin::EigenVector::abs
virtual void abs()
Replace all entries in the vector by their absolute values.
Definition EigenVector.cpp:246
dolfin::EigenVector::vec
std::shared_ptr< Eigen::VectorXd > vec()
Return reference to Eigen vector (non-const version).
Definition EigenVector.h:271
dolfin::EigenVector::operator=
virtual const GenericVector & operator=(const GenericVector &x)
Assignment operator.
Definition EigenVector.cpp:260
dolfin::EigenVector::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 EigenVector.h:180
dolfin::EigenVector::min
virtual double min() const
Return minimum value of vector.
Definition EigenVector.cpp:197
dolfin::EigenVector::operator*=
virtual const EigenVector & operator*=(double a)
Multiply vector by given number.
Definition EigenVector.cpp:289
dolfin::EigenVector::owns_index
virtual bool owns_index(std::size_t i) const
Determine whether global vector index is owned by this process.
Definition EigenVector.cpp:98
dolfin::EigenVector::norm
virtual double norm(std::string norm_type) const
Compute norm of vector.
Definition EigenVector.cpp:178
dolfin::EigenVector::gather_on_zero
virtual void gather_on_zero(std::vector< double > &x) const
Gather all entries into x on process 0.
Definition EigenVector.cpp:148
dolfin::EigenVector::str
virtual std::string str(bool verbose) const
Return informal string representation (pretty-print).
Definition EigenVector.cpp:339
dolfin::EigenVector::get
virtual void get(double *block, std::size_t m, const dolfin::la_index *rows) const
Get block of values using global indices.
Definition EigenVector.h:158
dolfin::EigenVector::gather
virtual void gather(GenericVector &x, const std::vector< dolfin::la_index > &indices) const
Gather entries into local vector x.
Definition EigenVector.cpp:132
dolfin::EigenVector::inner
virtual double inner(const GenericVector &x) const
Return inner product with given vector.
Definition EigenVector.cpp:252
dolfin::EigenVector::vec
std::shared_ptr< const Eigen::VectorXd > vec() const
Return reference to Eigen vector (const version).
Definition EigenVector.h:267
dolfin::EigenVector::get_local
virtual void get_local(double *block, std::size_t m, const dolfin::la_index *rows) const
Get block of values using local indices.
Definition EigenVector.cpp:106
dolfin::EigenVector::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)
Resize vector with given ownership range and with ghost values.
Definition EigenVector.h:115
dolfin::EigenVector::local_size
virtual std::size_t local_size() const
Return local size of vector.
Definition EigenVector.h:148
dolfin::EigenVector::apply
virtual void apply(std::string mode)
Finalize assembly of tensor.
Definition EigenVector.cpp:167
dolfin::EigenVector::zero
virtual void zero()
Set all entries to zero and keep any sparse structure.
Definition EigenVector.cpp:172
dolfin::EigenVector::operator[]
virtual double operator[](dolfin::la_index i) const
Access value of given entry (const version).
Definition EigenVector.h:275
dolfin::EigenVector::init
virtual void init(std::size_t N)
Initialize vector to size N.
Definition EigenVector.h:88
dolfin::EigenVector::sum
virtual double sum() const
Return sum of values of vector.
Definition EigenVector.cpp:209
dolfin::EigenVector::add
virtual void add(const double *block, std::size_t m, const dolfin::la_index *rows)
Add block of values using global indices.
Definition EigenVector.cpp:160
dolfin::EigenVector::operator+=
virtual const EigenVector & operator+=(const GenericVector &x)
Add given vector.
Definition EigenVector.cpp:311
dolfin::EigenVector::max
virtual double max() const
Return maximum value of vector.
Definition EigenVector.cpp:203
dolfin::EigenVector::EigenVector
EigenVector()
Create empty vector (on MPI_COMM_SELF).
Definition EigenVector.cpp:34
dolfin::EigenVector::factory
virtual GenericLinearAlgebraFactory & factory() const
Return linear algebra backend factory.
Definition EigenVector.cpp:363
dolfin::EigenVector::init
virtual void init(std::pair< std::size_t, std::size_t > range)
Resize vector with given ownership range.
Definition EigenVector.h:100
dolfin::EigenVector::set
virtual void set(const double *block, std::size_t m, const dolfin::la_index *rows)
Set block of values using global indices.
Definition EigenVector.cpp:153
dolfin::EigenVector::local_range
virtual std::pair< std::int64_t, std::int64_t > local_range() const
Return local ownership range of a vector.
Definition EigenVector.cpp:93
dolfin::EigenVector::axpy
virtual void axpy(double a, const GenericVector &x)
Add multiple of given vector (AXPY operation).
Definition EigenVector.cpp:232
dolfin::EigenVector::operator/=
virtual const EigenVector & operator/=(double a)
Divide vector by given number.
Definition EigenVector.cpp:305
dolfin::EigenVector::operator-=
virtual const EigenVector & operator-=(const GenericVector &x)
Subtract given vector.
Definition EigenVector.cpp:325
dolfin::EigenVector::mpi_comm
virtual MPI_Comm mpi_comm() const
Return MPI communicator.
Definition EigenVector.h:76
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::MPI::size
static unsigned int size(MPI_Comm comm)
Definition MPI.cpp:154
dolfin
Definition adapt.h:30
dolfin::dolfin_error
void dolfin_error(std::string location, std::string task, std::string reason,...)
Definition log.cpp:129
dolfin::la_index
PetscInt la_index
Index type for compatibility with linear algebra backend(s).
Definition types.h:32