libMesh
eigen_sparse_vector.h
Go to the documentation of this file.
1 // The libMesh Finite Element Library.
2 // Copyright (C) 2002-2019 Benjamin S. Kirk, John W. Peterson, Roy H. Stogner
3 
4 // This library is free software; you can redistribute it and/or
5 // modify it under the terms of the GNU Lesser General Public
6 // License as published by the Free Software Foundation; either
7 // version 2.1 of the License, or (at your option) any later version.
8 
9 // This library is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 // Lesser General Public License for more details.
13 
14 // You should have received a copy of the GNU Lesser General Public
15 // License along with this library; if not, write to the Free Software
16 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 
18 
19 
20 
21 #ifndef LIBMESH_EIGEN_SPARSE_VECTOR_H
22 #define LIBMESH_EIGEN_SPARSE_VECTOR_H
23 
24 
25 
26 #include "libmesh/libmesh_common.h"
27 
28 #ifdef LIBMESH_HAVE_EIGEN
29 
30 // Local includes
31 #include "libmesh/eigen_core_support.h"
32 #include "libmesh/numeric_vector.h"
33 
34 namespace libMesh
35 {
36 
37 // Forward declarations
38 template <typename T> class EigenSparseMatrix;
39 template <typename T> class EigenSparseLinearSolver;
40 template <typename T> class SparseMatrix;
41 
50 template <typename T>
51 class EigenSparseVector final : public NumericVector<T>
52 {
53 public:
54 
58  explicit
59  EigenSparseVector (const Parallel::Communicator & comm_in,
60  const ParallelType = AUTOMATIC);
61 
65  explicit
66  EigenSparseVector (const Parallel::Communicator & comm_in,
67  const numeric_index_type n,
68  const ParallelType = AUTOMATIC);
69 
74  EigenSparseVector (const Parallel::Communicator & comm_in,
75  const numeric_index_type n,
76  const numeric_index_type n_local,
77  const ParallelType = AUTOMATIC);
78 
84  EigenSparseVector (const Parallel::Communicator & comm_in,
85  const numeric_index_type N,
86  const numeric_index_type n_local,
87  const std::vector<numeric_index_type> & ghost,
88  const ParallelType = AUTOMATIC);
89 
95  EigenSparseVector<T> & operator= (const EigenSparseVector<T> & v);
96 
101  EigenSparseVector (EigenSparseVector &&) = default;
102  EigenSparseVector (const EigenSparseVector &) = default;
104  virtual ~EigenSparseVector () = default;
105 
109  typedef EigenSV DataType;
110 
111  virtual void close () override;
112 
113  virtual void clear () override;
114 
115  virtual void zero () override;
116 
117  virtual std::unique_ptr<NumericVector<T>> zero_clone () const override;
118 
119  virtual std::unique_ptr<NumericVector<T>> clone () const override;
120 
121  virtual void init (const numeric_index_type N,
122  const numeric_index_type n_local,
123  const bool fast=false,
124  const ParallelType ptype=AUTOMATIC) override;
125 
126  virtual void init (const numeric_index_type N,
127  const bool fast=false,
128  const ParallelType ptype=AUTOMATIC) override;
129 
130  virtual void init (const numeric_index_type N,
131  const numeric_index_type n_local,
132  const std::vector<numeric_index_type> & ghost,
133  const bool fast = false,
134  const ParallelType = AUTOMATIC) override;
135 
136  virtual void init (const NumericVector<T> & other,
137  const bool fast = false) override;
138 
139  virtual NumericVector<T> & operator= (const T s) override;
140 
141  virtual NumericVector<T> & operator= (const NumericVector<T> & v) override;
142 
143  virtual NumericVector<T> & operator= (const std::vector<T> & v) override;
144 
145  virtual Real min () const override;
146 
147  virtual Real max () const override;
148 
149  virtual T sum () const override;
150 
151  virtual Real l1_norm () const override;
152 
153  virtual Real l2_norm () const override;
154 
155  virtual Real linfty_norm () const override;
156 
157  virtual numeric_index_type size () const override;
158 
159  virtual numeric_index_type local_size() const override;
160 
161  virtual numeric_index_type first_local_index() const override;
162 
163  virtual numeric_index_type last_local_index() const override;
164 
165  virtual T operator() (const numeric_index_type i) const override;
166 
167  virtual NumericVector<T> & operator += (const NumericVector<T> & v) override;
168 
169  virtual NumericVector<T> & operator -= (const NumericVector<T> & v) override;
170 
171  virtual NumericVector<T> & operator *= (const NumericVector<T> & v_in) override;
172 
173  virtual NumericVector<T> & operator /= (const NumericVector<T> & v_in) override;
174 
175  virtual void reciprocal() override;
176 
177  virtual void conjugate() override;
178 
179  virtual void set (const numeric_index_type i, const T value) override;
180 
181  virtual void add (const numeric_index_type i, const T value) override;
182 
183  virtual void add (const T s) override;
184 
185  virtual void add (const NumericVector<T> & v) override;
186 
187  virtual void add (const T a, const NumericVector<T> & v) override;
188 
194 
195  virtual void add_vector (const NumericVector<T> & v,
196  const SparseMatrix<T> & A) override;
197 
198  virtual void add_vector_transpose (const NumericVector<T> & v,
199  const SparseMatrix<T> & A) override;
200 
201  virtual void scale (const T factor) override;
202 
203  virtual void abs() override;
204 
205  virtual T dot(const NumericVector<T> & v) const override;
206 
207  virtual void localize (std::vector<T> & v_local) const override;
208 
209  virtual void localize (NumericVector<T> & v_local) const override;
210 
211  virtual void localize (NumericVector<T> & v_local,
212  const std::vector<numeric_index_type> & send_list) const override;
213 
214  virtual void localize (std::vector<T> & v_local,
215  const std::vector<numeric_index_type> & indices) const override;
216 
217  virtual void localize (const numeric_index_type first_local_idx,
218  const numeric_index_type last_local_idx,
219  const std::vector<numeric_index_type> & send_list) override;
220 
221  virtual void localize_to_one (std::vector<T> & v_local,
222  const processor_id_type proc_id=0) const override;
223 
224  virtual void pointwise_mult (const NumericVector<T> & vec1,
225  const NumericVector<T> & vec2) override;
226 
227  virtual void swap (NumericVector<T> & v) override;
228 
234  DataType & vec () { return _vec; }
235  const DataType & vec () const { return _vec; }
236 
237 private:
238 
243 
247  friend class EigenSparseMatrix<T>;
248  friend class EigenSparseLinearSolver<T>;
249 };
250 
251 
252 
253 // ---------------------------------------------------------
254 // EigenSparseVector inline methods
255 template <typename T>
256 inline
257 EigenSparseVector<T>::EigenSparseVector (const Parallel::Communicator & comm_in,
258  const ParallelType ptype)
259  : NumericVector<T>(comm_in, ptype)
260 {
261  this->_type = ptype;
262 }
263 
264 
265 
266 template <typename T>
267 inline
268 EigenSparseVector<T>::EigenSparseVector (const Parallel::Communicator & comm_in,
269  const numeric_index_type n,
270  const ParallelType ptype)
271  : NumericVector<T>(comm_in, ptype)
272 {
273  this->init(n, n, false, ptype);
274 }
275 
276 
277 
278 template <typename T>
279 inline
280 EigenSparseVector<T>::EigenSparseVector (const Parallel::Communicator & comm_in,
281  const numeric_index_type n,
282  const numeric_index_type n_local,
283  const ParallelType ptype)
284  : NumericVector<T>(comm_in, ptype)
285 {
286  this->init(n, n_local, false, ptype);
287 }
288 
289 
290 
291 template <typename T>
292 inline
293 EigenSparseVector<T>::EigenSparseVector (const Parallel::Communicator & comm_in,
294  const numeric_index_type N,
295  const numeric_index_type n_local,
296  const std::vector<numeric_index_type> & ghost,
297  const ParallelType ptype)
298  : NumericVector<T>(comm_in, ptype)
299 {
300  this->init(N, n_local, ghost, false, ptype);
301 }
302 
303 
304 
305 template <typename T>
306 inline
308  const numeric_index_type n_local,
309  const bool fast,
310  const ParallelType)
311 {
312  // Eigen vectors only for serial cases,
313  // but can provide a "parallel" vector on one processor.
314  if (n != n_local)
315  libmesh_error_msg("Error: EigenSparseVectors can only be used in serial!");
316 
317  this->_type = SERIAL;
318 
319  // Clear initialized vectors
320  if (this->initialized())
321  this->clear();
322 
323  _vec.resize(n);
324 
325  this->_is_initialized = true;
326  this->_is_closed = true;
327 
328  // Optionally zero out all components
329  if (fast == false)
330  this->zero ();
331 
332  return;
333 }
334 
335 
336 
337 template <typename T>
338 inline
340  const bool fast,
341  const ParallelType ptype)
342 {
343  this->init(n,n,fast,ptype);
344 }
345 
346 
347 template <typename T>
348 inline
350  const numeric_index_type n_local,
351  const std::vector<numeric_index_type> & libmesh_dbg_var(ghost),
352  const bool fast,
353  const ParallelType ptype)
354 {
355  libmesh_assert(ghost.empty());
356  this->init(n,n_local,fast,ptype);
357 }
358 
359 
360 
361 /* Default implementation for solver packages for which ghosted
362  vectors are not yet implemented. */
363 template <class T>
365  const bool fast)
366 {
367  this->init(other.size(),other.local_size(),fast,other.type());
368 }
369 
370 
371 
372 template <typename T>
373 inline
375 {
376  libmesh_assert (this->initialized());
377 
378  this->_is_closed = true;
379 }
380 
381 
382 
383 template <typename T>
384 inline
386 {
387  _vec.resize(0);
388 
389  this->_is_initialized = false;
390  this->_is_closed = false;
391 }
392 
393 
394 
395 template <typename T> inline
397 {
398  libmesh_assert (this->initialized());
399  libmesh_assert (this->closed());
400 
401  _vec.setZero();
402 }
403 
404 
405 
406 template <typename T>
407 inline
408 std::unique_ptr<NumericVector<T>> EigenSparseVector<T>::zero_clone () const
409 {
410  NumericVector<T> * cloned_vector = new EigenSparseVector<T>(this->comm());
411  cloned_vector->init(*this);
412  return std::unique_ptr<NumericVector<T>>(cloned_vector);
413 }
414 
415 
416 
417 template <typename T>
418 inline
419 std::unique_ptr<NumericVector<T>> EigenSparseVector<T>::clone () const
420 {
421  NumericVector<T> * cloned_vector = new EigenSparseVector<T>(this->comm());
422  cloned_vector->init(*this, true);
423  *cloned_vector = *this;
424  return std::unique_ptr<NumericVector<T>>(cloned_vector);
425 }
426 
427 
428 
429 template <typename T>
430 inline
432 {
433  libmesh_assert (this->initialized());
434 
435  return static_cast<numeric_index_type>(_vec.size());
436 }
437 
438 
439 
440 template <typename T>
441 inline
443 {
444  libmesh_assert (this->initialized());
445 
446  return this->size();
447 }
448 
449 
450 
451 template <typename T>
452 inline
454 {
455  libmesh_assert (this->initialized());
456 
457  return 0;
458 }
459 
460 
461 
462 template <typename T>
463 inline
465 {
466  libmesh_assert (this->initialized());
467 
468  return this->size();
469 }
470 
471 
472 
473 template <typename T>
474 inline
476 {
477  libmesh_assert (this->initialized());
478  libmesh_assert_less (i, this->size());
479 
480  _vec[static_cast<eigen_idx_type>(i)] = value;
481 
482  this->_is_closed = false;
483 }
484 
485 
486 
487 template <typename T>
488 inline
490 {
491  libmesh_assert (this->initialized());
492  libmesh_assert_less (i, this->size());
493 
494  _vec[static_cast<eigen_idx_type>(i)] += value;
495 
496  this->_is_closed = false;
497 }
498 
499 
500 
501 template <typename T>
502 inline
504 {
505  libmesh_assert (this->initialized());
506  libmesh_assert ( ((i >= this->first_local_index()) &&
507  (i < this->last_local_index())) );
508 
509  return _vec[static_cast<eigen_idx_type>(i)];
510 }
511 
512 
513 
514 template <typename T>
515 inline
517 {
518  EigenSparseVector<T> & v = cast_ref<EigenSparseVector<T> &>(other);
519 
520  _vec.swap(v._vec);
521 
522  std::swap (this->_is_closed, v._is_closed);
523  std::swap (this->_is_initialized, v._is_initialized);
524  std::swap (this->_type, v._type);
525 }
526 
527 
528 } // namespace libMesh
529 
530 
531 #endif // #ifdef LIBMESH_HAVE_EIGEN
532 #endif // LIBMESH_EIGEN_SPARSE_VECTOR_H
libMesh::EigenSparseVector::max
virtual Real max() const override
Definition: eigen_sparse_vector.C:426
libMesh::EigenSparseVector::vec
DataType & vec()
References to the underlying Eigen data types.
Definition: eigen_sparse_vector.h:234
libMesh::EigenSparseVector::operator=
EigenSparseVector< T > & operator=(const EigenSparseVector< T > &v)
Copy assignment operator.
Definition: eigen_sparse_vector.C:301
libMesh::EigenSparseVector::operator*=
virtual NumericVector< T > & operator*=(const NumericVector< T > &v_in) override
Computes the component-wise multiplication of this vector's entries by another's, .
Definition: eigen_sparse_vector.C:110
libMesh::EigenSparseVector::clear
virtual void clear() override
Restores the NumericVector<T> to a pristine state.
Definition: eigen_sparse_vector.h:385
libMesh::EigenSparseVector::size
virtual numeric_index_type size() const override
Definition: eigen_sparse_vector.h:431
libMesh::EigenSparseVector::first_local_index
virtual numeric_index_type first_local_index() const override
Definition: eigen_sparse_vector.h:453
libMesh::EigenSparseMatrix
The EigenSparseMatrix class wraps a sparse matrix object from the Eigen library.
Definition: eigen_sparse_matrix.h:54
libMesh::EigenSparseVector::local_size
virtual numeric_index_type local_size() const override
Definition: eigen_sparse_vector.h:442
libMesh::EigenSparseVector::abs
virtual void abs() override
Sets for each entry in the vector.
Definition: eigen_sparse_vector.C:242
libMesh::EigenSparseVector::add_vector_transpose
virtual void add_vector_transpose(const NumericVector< T > &v, const SparseMatrix< T > &A) override
Computes , i.e.
Definition: eigen_sparse_vector.C:216
libMesh::EigenSparseVector::scale
virtual void scale(const T factor) override
Scale each element of the vector by the given factor.
Definition: eigen_sparse_vector.C:232
libMesh::SERIAL
Definition: enum_parallel_type.h:35
libMesh
The libMesh namespace provides an interface to certain functionality in the library.
Definition: factoryfunction.C:55
libMesh::EigenSparseVector::localize_to_one
virtual void localize_to_one(std::vector< T > &v_local, const processor_id_type proc_id=0) const override
Creates a local copy of the global vector in v_local only on processor proc_id.
Definition: eigen_sparse_vector.C:406
libMesh::EigenSparseVector::set
virtual void set(const numeric_index_type i, const T value) override
Sets v(i) = value.
Definition: eigen_sparse_vector.h:475
libMesh::EigenSparseVector::l1_norm
virtual Real l1_norm() const override
Definition: eigen_sparse_vector.C:48
libMesh::NumericVector::init
virtual void init(const numeric_index_type n, const numeric_index_type n_local, const bool fast=false, const ParallelType ptype=AUTOMATIC)=0
Change the dimension of the vector to n.
libMesh::EigenSparseVector::zero
virtual void zero() override
Set all entries to zero.
Definition: eigen_sparse_vector.h:396
libMesh::EigenSparseVector::add
virtual void add(const numeric_index_type i, const T value) override
Adds value to each entry of the vector.
Definition: eigen_sparse_vector.h:489
libMesh::EigenSparseVector::EigenSparseVector
EigenSparseVector(const Parallel::Communicator &comm_in, const ParallelType=AUTOMATIC)
Dummy-Constructor.
Definition: eigen_sparse_vector.h:257
libMesh::EigenSparseVector::~EigenSparseVector
virtual ~EigenSparseVector()=default
libMesh::SparseMatrix
Generic sparse matrix.
Definition: vector_fe_ex5.C:45
libMesh::NumericVector::size
virtual numeric_index_type size() const =0
libMesh::NumericVector
Provides a uniform interface to vector storage schemes for different linear algebra libraries.
Definition: vector_fe_ex5.C:43
libMesh::zero
const Number zero
.
Definition: libmesh.h:243
libMesh::TriangleWrapper::init
void init(triangulateio &t)
Initializes the fields of t to nullptr/0 as necessary.
libMesh::NumericVector::_type
ParallelType _type
Type of vector.
Definition: numeric_vector.h:737
libMesh::libmesh_assert
libmesh_assert(ctx)
libMesh::EigenSparseVector::dot
virtual T dot(const NumericVector< T > &v) const override
Definition: eigen_sparse_vector.C:255
libMesh::EigenSparseVector::operator()
virtual T operator()(const numeric_index_type i) const override
Definition: eigen_sparse_vector.h:503
libMesh::EigenSparseVector::operator+=
virtual NumericVector< T > & operator+=(const NumericVector< T > &v) override
Adds v to *this, .
Definition: eigen_sparse_vector.C:81
libMesh::EigenSparseVector
This class provides a nice interface to the Eigen C++-based data structures for serial vectors.
Definition: eigen_sparse_matrix.h:42
libMesh::EigenSV
Eigen::Matrix< Number, Eigen::Dynamic, 1 > EigenSV
Definition: eigen_core_support.h:79
libMesh::processor_id_type
uint8_t processor_id_type
Definition: id_types.h:104
libMesh::EigenSparseVector::localize
virtual void localize(std::vector< T > &v_local) const override
Creates a copy of the global vector in the local vector v_local.
Definition: eigen_sparse_vector.C:394
libMesh::EigenSparseVector::linfty_norm
virtual Real linfty_norm() const override
Definition: eigen_sparse_vector.C:70
libMesh::NumericVector::local_size
virtual numeric_index_type local_size() const =0
libMesh::EigenSparseVector::conjugate
virtual void conjugate() override
Negates the imaginary component of each entry in the vector.
Definition: eigen_sparse_vector.C:157
A
static PetscErrorCode Mat * A
Definition: petscdmlibmeshimpl.C:1026
libMesh::EigenSparseVector::pointwise_mult
virtual void pointwise_mult(const NumericVector< T > &vec1, const NumericVector< T > &vec2) override
Computes (summation not implied) i.e.
Definition: eigen_sparse_vector.C:417
libMesh::EigenSparseVector::_vec
DataType _vec
Actual Eigen::SparseVector<> we are wrapping.
Definition: eigen_sparse_vector.h:242
libMesh::EigenSparseVector::sum
virtual T sum() const override
Definition: eigen_sparse_vector.C:37
libMesh::numeric_index_type
dof_id_type numeric_index_type
Definition: id_types.h:99
libMesh::initialized
bool initialized()
Checks that library initialization has been done.
Definition: libmesh.C:265
libMesh::EigenSparseVector::add_vector
virtual void add_vector(const NumericVector< T > &v, const SparseMatrix< T > &A) override
Computes , i.e.
Definition: eigen_sparse_vector.C:200
libMesh::EigenSparseVector::last_local_index
virtual numeric_index_type last_local_index() const override
Definition: eigen_sparse_vector.h:464
libMesh::EigenSparseVector::reciprocal
virtual void reciprocal() override
Computes the component-wise reciprocal, .
Definition: eigen_sparse_vector.C:141
swap
void swap(Iterator &lhs, Iterator &rhs)
swap, used to implement op=
Definition: variant_filter_iterator.h:478
libMesh::EigenSparseVector::min
virtual Real min() const override
Definition: eigen_sparse_vector.C:449
libMesh::EigenSparseVector::operator-=
virtual NumericVector< T > & operator-=(const NumericVector< T > &v) override
Subtracts v from *this, .
Definition: eigen_sparse_vector.C:96
libMesh::libMeshPrivateData::_is_initialized
bool _is_initialized
Flag that tells if init() has been called.
Definition: libmesh.C:246
value
static const bool value
Definition: xdr_io.C:56
libMesh::EigenSparseLinearSolver
This class provides an interface to Eigen iterative solvers that is compatible with the libMesh Linea...
Definition: eigen_sparse_matrix.h:43
libMesh::EigenSparseVector::clone
virtual std::unique_ptr< NumericVector< T > > clone() const override
Definition: eigen_sparse_vector.h:419
libMesh::AUTOMATIC
Definition: enum_parallel_type.h:34
libMesh::ParallelType
ParallelType
Defines an enum for parallel data structure types.
Definition: enum_parallel_type.h:33
libMesh::EigenSparseVector::swap
virtual void swap(NumericVector< T > &v) override
Swaps the contents of this with v.
Definition: eigen_sparse_vector.h:516
libMesh::EigenSparseVector::close
virtual void close() override
Calls the NumericVector's internal assembly routines, ensuring that the values are consistent across ...
Definition: eigen_sparse_vector.h:374
libMesh::EigenSparseVector::zero_clone
virtual std::unique_ptr< NumericVector< T > > zero_clone() const override
Definition: eigen_sparse_vector.h:408
libMesh::Real
DIE A HORRIBLE DEATH HERE typedef LIBMESH_DEFAULT_SCALAR_TYPE Real
Definition: libmesh_common.h:121
libMesh::EigenSparseVector::DataType
EigenSV DataType
Convenient typedefs.
Definition: eigen_sparse_vector.h:109
libMesh::EigenSparseVector::vec
const DataType & vec() const
Definition: eigen_sparse_vector.h:235
libMesh::NumericVector::type
ParallelType type() const
Definition: numeric_vector.h:160
libMesh::EigenSparseVector::l2_norm
virtual Real l2_norm() const override
Definition: eigen_sparse_vector.C:59
libMesh::EigenSparseVector::init
virtual void init(const numeric_index_type N, const numeric_index_type n_local, const bool fast=false, const ParallelType ptype=AUTOMATIC) override
Change the dimension of the vector to n.
Definition: eigen_sparse_vector.h:307
libMesh::closed
bool closed()
Checks that the library has been closed.
Definition: libmesh.C:272
libMesh::EigenSparseVector::operator/=
virtual NumericVector< T > & operator/=(const NumericVector< T > &v_in) override
Computes the component-wise division of this vector's entries by another's, .
Definition: eigen_sparse_vector.C:125