DOLFIN
DOLFIN C++ interface
EigenVector.h
1 // Copyright (C) 2015 Garth N. Wells
2 //
3 // This file is part of DOLFIN.
4 //
5 // DOLFIN is free software: you can redistribute it and/or modify
6 // it under the terms of the GNU Lesser General Public License as published by
7 // the Free Software Foundation, either version 3 of the License, or
8 // (at your option) any later version.
9 //
10 // DOLFIN is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU Lesser General Public License for more details.
14 //
15 // You should have received a copy of the GNU Lesser General Public License
16 // along with DOLFIN. If not, see <http://www.gnu.org/licenses/>.
17 
18 #ifndef __EIGEN_VECTOR_H
19 #define __EIGEN_VECTOR_H
20 
21 #include <memory>
22 #include <string>
23 #include <utility>
24 #include <vector>
25 
26 #include <dolfin/common/types.h>
27 #include <Eigen/Dense>
28 
29 #include <dolfin/common/MPI.h>
30 #include "GenericVector.h"
31 
32 namespace dolfin
33 {
34 
35  template<typename T> class Array;
36 
44 
45  class EigenVector : public GenericVector
46  {
47  public:
48 
50  EigenVector();
51 
53  explicit EigenVector(MPI_Comm comm);
54 
56  EigenVector(MPI_Comm comm, std::size_t N);
57 
59  EigenVector(const EigenVector& x);
60 
62  explicit EigenVector(std::shared_ptr<Eigen::VectorXd> x);
63 
65  virtual ~EigenVector();
66 
67  //--- Implementation of the GenericTensor interface ---
68 
70  virtual void zero();
71 
73  virtual void apply(std::string mode);
74 
76  virtual MPI_Comm mpi_comm() const
77  { return _mpi_comm.comm(); }
78 
80  virtual std::string str(bool verbose) const;
81 
82  //--- Implementation of the GenericVector interface ---
83 
85  virtual std::shared_ptr<GenericVector> copy() const;
86 
88  virtual void init(std::size_t N)
89  {
90  if (!empty())
91  {
92  dolfin_error("EigenVector.cpp",
93  "calling EigenVector::init(...)",
94  "Cannot call init for a non-empty vector. Use EigenVector::resize instead");
95  }
96  resize(N);
97  }
98 
100  virtual void init(std::pair<std::size_t, std::size_t> range)
101  {
102  if (!empty())
103  {
104  dolfin_error("EigenVector.cpp",
105  "calling EigenVector::init(...)",
106  "Cannot call init for a non-empty vector. Use EigenVector::resize instead");
107  }
108 
109  dolfin_assert(range.first == 0);
110  const std::size_t size = range.second - range.first;
111  resize(size);
112  }
113 
115  virtual void init(std::pair<std::size_t, std::size_t> range,
116  const std::vector<std::size_t>& local_to_global_map,
117  const std::vector<la_index>& ghost_indices)
118  {
119  if (!empty())
120  {
121  dolfin_error("EigenVector.cpp",
122  "calling EigenVector::init(...)",
123  "Cannot call init for a non-empty vector. Use EigenVector::resize instead");
124  }
125 
126  if (!ghost_indices.empty())
127  {
128  dolfin_error("EigenVector.cpp",
129  "calling EigenVector::init(...)",
130  "EigenVector does not support ghost values");
131  }
132 
133  dolfin_assert(range.first == 0);
134  const std::size_t size = range.second - range.first;
135  resize(size);
136  }
137 
138  // Bring init function from GenericVector into scope
139  using GenericVector::init;
140 
142  virtual bool empty() const;
143 
145  virtual std::size_t size() const;
146 
148  virtual std::size_t local_size() const
149  { return size(); }
150 
152  virtual std::pair<std::int64_t, std::int64_t> local_range() const;
153 
155  virtual bool owns_index(std::size_t i) const;
156 
158  virtual void get(double* block, std::size_t m,
159  const dolfin::la_index* rows) const
160  { get_local(block, m, rows); }
161 
163  virtual void get_local(double* block, std::size_t m,
164  const dolfin::la_index* rows) const;
165 
167  virtual void set(const double* block, std::size_t m,
168  const dolfin::la_index* rows);
169 
171  virtual void set_local(const double* block, std::size_t m,
172  const dolfin::la_index* rows)
173  { set(block, m, rows); }
174 
176  virtual void add(const double* block, std::size_t m,
177  const dolfin::la_index* rows);
178 
180  virtual void add_local(const double* block, std::size_t m,
181  const dolfin::la_index* rows)
182  { add(block, m, rows); }
183 
185  virtual void get_local(std::vector<double>& values) const;
186 
188  virtual void set_local(const std::vector<double>& values);
189 
191  virtual void add_local(const Array<double>& values);
192 
194  virtual void gather(GenericVector& x,
195  const std::vector<dolfin::la_index>& indices) const;
196 
198  virtual void gather(std::vector<double>& x,
199  const std::vector<dolfin::la_index>& indices) const;
200 
202  virtual void gather_on_zero(std::vector<double>& x) const;
203 
205  virtual void axpy(double a, const GenericVector& x);
206 
208  virtual void abs();
209 
211  virtual double inner(const GenericVector& x) const;
212 
214  virtual double norm(std::string norm_type) const;
215 
217  virtual double min() const;
218 
220  virtual double max() const;
221 
223  virtual double sum() const;
224 
227  virtual double sum(const Array<std::size_t>& rows) const;
228 
230  virtual const EigenVector& operator*= (double a);
231 
233  virtual const EigenVector& operator*= (const GenericVector& x);
234 
236  virtual const EigenVector& operator/= (double a);
237 
239  virtual const EigenVector& operator+= (const GenericVector& x);
240 
242  virtual const EigenVector& operator+= (double a);
243 
245  virtual const EigenVector& operator-= (const GenericVector& x);
246 
248  virtual const EigenVector& operator-= (double a);
249 
251  virtual const GenericVector& operator= (const GenericVector& x);
252 
254  virtual const EigenVector& operator= (double a);
255 
256  //--- Special functions ---
257 
259  virtual GenericLinearAlgebraFactory& factory() const;
260 
261  //--- Special Eigen functions ---
262 
264  virtual void resize(std::size_t N);
265 
267  std::shared_ptr<const Eigen::VectorXd> vec() const
268  { return _x; }
269 
271  std::shared_ptr<Eigen::VectorXd> vec()
272  { return _x; }
273 
275  virtual double operator[] (dolfin::la_index i) const
276  { return (*_x)(i); }
277 
280  { return (*_x)(i); }
281 
283  const EigenVector& operator= (const EigenVector& x);
284 
286  double* data();
287 
289  const double* data() const;
290 
291  private:
292 
293  static void check_mpi_size(const MPI_Comm comm)
294  {
295  if (dolfin::MPI::size(comm) > 1)
296  {
297  dolfin_error("EigenVector.cpp",
298  "creating EigenVector",
299  "EigenVector does not support parallel communicators");
300  }
301  }
302 
303  // Pointer to Eigen vector object
304  std::shared_ptr<Eigen::VectorXd> _x;
305 
306  // MPI communicator
307  dolfin::MPI::Comm _mpi_comm;
308 
309  };
310 
311 }
312 
313 #endif
virtual const EigenVector & operator/=(double a)
Divide vector by given number.
Definition: EigenVector.cpp:305
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
virtual double sum() const
Return sum of values of vector.
Definition: EigenVector.cpp:209
virtual void resize(std::size_t N)
Resize vector to size N.
Definition: EigenVector.cpp:368
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
virtual void apply(std::string mode)
Finalize assembly of tensor.
Definition: EigenVector.cpp:167
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
virtual void gather(GenericVector &x, const std::vector< dolfin::la_index > &indices) const
Gather entries into local vector x.
Definition: EigenVector.cpp:132
virtual void init(const TensorLayout &tensor_layout)
Definition: GenericVector.h:58
virtual double min() const
Return minimum value of vector.
Definition: EigenVector.cpp:197
Definition: adapt.h:29
virtual const EigenVector & operator+=(const GenericVector &x)
Add given vector.
Definition: EigenVector.cpp:311
Definition: Array.h:41
virtual std::string str(bool verbose) const
Return informal string representation (pretty-print)
Definition: EigenVector.cpp:339
virtual std::shared_ptr< GenericVector > copy() const
Create copy of tensor.
Definition: EigenVector.cpp:73
virtual double inner(const GenericVector &x) const
Return inner product with given vector.
Definition: EigenVector.cpp:252
std::shared_ptr< const Eigen::VectorXd > vec() const
Return reference to Eigen vector (const version)
Definition: EigenVector.h:267
virtual double norm(std::string norm_type) const
Compute norm of vector.
Definition: EigenVector.cpp:178
virtual void init(std::pair< std::size_t, std::size_t > range)
Resize vector with given ownership range.
Definition: EigenVector.h:100
static unsigned int size(MPI_Comm comm)
Definition: MPI.cpp:154
virtual void init(std::size_t N)
Initialize vector to size N.
Definition: EigenVector.h:88
virtual bool empty() const
Return true if vector is empty.
Definition: EigenVector.cpp:79
virtual const GenericVector & operator=(const GenericVector &x)
Assignment operator.
Definition: EigenVector.cpp:260
virtual bool owns_index(std::size_t i) const
Determine whether global vector index is owned by this process.
Definition: EigenVector.cpp:98
virtual void zero()
Set all entries to zero and keep any sparse structure.
Definition: EigenVector.cpp:172
virtual ~EigenVector()
Destructor.
Definition: EigenVector.cpp:68
Base class for LinearAlgebra factories.
Definition: GenericLinearAlgebraFactory.h:46
virtual const EigenVector & operator-=(const GenericVector &x)
Subtract given vector.
Definition: EigenVector.cpp:325
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
MPI_Comm comm() const
Return the underlying MPI_Comm object.
Definition: MPI.cpp:117
double * data()
Return pointer to underlying data.
Definition: EigenVector.cpp:379
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
virtual void abs()
Replace all entries in the vector by their absolute values.
Definition: EigenVector.cpp:246
std::shared_ptr< Eigen::VectorXd > vec()
Return reference to Eigen vector (non-const version)
Definition: EigenVector.h:271
virtual std::size_t local_size() const
Return local size of vector.
Definition: EigenVector.h:148
virtual std::pair< std::int64_t, std::int64_t > local_range() const
Return local ownership range of a vector.
Definition: EigenVector.cpp:93
PetscInt la_index
Index type for compatibility with linear algebra backend(s)
Definition: types.h:32
EigenVector()
Create empty vector (on MPI_COMM_SELF)
Definition: EigenVector.cpp:34
Definition: EigenVector.h:45
void dolfin_error(std::string location, std::string task, std::string reason,...)
Definition: log.cpp:129
virtual MPI_Comm mpi_comm() const
Return MPI communicator.
Definition: EigenVector.h:76
virtual double operator[](dolfin::la_index i) const
Access value of given entry (const version)
Definition: EigenVector.h:275
virtual GenericLinearAlgebraFactory & factory() const
Return linear algebra backend factory.
Definition: EigenVector.cpp:363
virtual const EigenVector & operator*=(double a)
Multiply vector by given number.
Definition: EigenVector.cpp:289
virtual void axpy(double a, const GenericVector &x)
Add multiple of given vector (AXPY operation)
Definition: EigenVector.cpp:232
virtual void gather_on_zero(std::vector< double > &x) const
Gather all entries into x on process 0.
Definition: EigenVector.cpp:148
This class defines a common interface for vectors.
Definition: GenericVector.h:47
Definition: MPI.h:76
virtual double max() const
Return maximum value of vector.
Definition: EigenVector.cpp:203
virtual std::size_t size() const
Return true if vector is empty.
Definition: EigenVector.cpp:88