OGS
EigenVector.h
Go to the documentation of this file.
1 
11 #pragma once
12 
13 #include <vector>
14 #ifndef NDEBUG
15 #include <fstream>
16 #include <string>
17 #endif
18 
19 #include <Eigen/Eigen>
20 #include <Eigen/Sparse>
21 
22 #include "EigenMapTools.h"
23 
24 namespace MathLib
25 {
27 class EigenVector final
28 {
29 public:
30  using RawVectorType = Eigen::VectorXd;
31 
32  // The Index type of the Eigen::VectorXd class differs from the
33  // Eigen::SparseMatrix<double> index type. Maybe an Eigen::SparseVector is a
34  // more appropriate RawVectorType for the global vectors.
35  using IndexType = Eigen::SparseMatrix<double>::Index;
36 
37  // TODO: preliminary
38  EigenVector() = default;
39 
42  explicit EigenVector(IndexType length) : vec_(length) {}
43 
45  IndexType size() const { return static_cast<IndexType>(vec_.size()); }
46 
48  static constexpr IndexType getRangeBegin() { return 0; }
49 
51  IndexType getRangeEnd() const { return size(); }
52 
53  // TODO preliminary
54  void setZero() { vec_.setZero(); }
55 
57  double const& operator[](IndexType rowId) const { return vec_[rowId]; }
58  double& operator[](IndexType rowId) { return vec_[rowId]; }
59 
61  double get(IndexType rowId) const { return vec_[rowId]; }
62 
64  std::vector<double> get(std::vector<IndexType> const& indices) const
65  {
66  std::vector<double> local_x;
67  local_x.reserve(indices.size());
68 
69  transform(cbegin(indices), cend(indices), back_inserter(local_x),
70  [&](auto const i) { return vec_[i]; });
71 
72  return local_x;
73  }
74 
76  void set(IndexType rowId, double v) { vec_[rowId] = v; }
77 
79  void add(IndexType rowId, double v) { vec_[rowId] += v; }
80 
82  template <class T_SUBVEC>
83  void add(const std::vector<IndexType>& pos, const T_SUBVEC& sub_vec)
84  {
85  auto const length = pos.size();
86  for (std::size_t i = 0; i < length; ++i)
87  {
88  add(pos[i], sub_vec[i]);
89  }
90  }
91 
95  void copyValues(std::vector<double>& u) const
96  {
97  u.resize(size());
98  toVector(u) = vec_;
99  }
100 
101 #ifndef NDEBUG
103  void write(const std::string& filename) const
104  {
105  std::ofstream os(filename);
106  os << vec_;
107  }
108 #endif
109 
112 
114  const RawVectorType& getRawVector() const { return vec_; }
115 
116 private:
118 };
119 
120 } // namespace MathLib
Global vector based on Eigen vector.
Definition: EigenVector.h:28
double const & operator[](IndexType rowId) const
access entry
Definition: EigenVector.h:57
IndexType getRangeEnd() const
return an end index of the active data range
Definition: EigenVector.h:51
void copyValues(std::vector< double > &u) const
Definition: EigenVector.h:95
RawVectorType vec_
Definition: EigenVector.h:117
void add(IndexType rowId, double v)
add entry
Definition: EigenVector.h:79
double & operator[](IndexType rowId)
Definition: EigenVector.h:58
IndexType size() const
return a vector length
Definition: EigenVector.h:45
double get(IndexType rowId) const
get entry
Definition: EigenVector.h:61
void set(IndexType rowId, double v)
set entry
Definition: EigenVector.h:76
RawVectorType & getRawVector()
return a raw Eigen vector object
Definition: EigenVector.h:111
Eigen::VectorXd RawVectorType
Definition: EigenVector.h:30
EigenVector(IndexType length)
Definition: EigenVector.h:42
void add(const std::vector< IndexType > &pos, const T_SUBVEC &sub_vec)
add entries
Definition: EigenVector.h:83
const RawVectorType & getRawVector() const
return a raw Eigen vector object
Definition: EigenVector.h:114
Eigen::SparseMatrix< double >::Index IndexType
Definition: EigenVector.h:35
void write(const std::string &filename) const
printout this equation for debugging
Definition: EigenVector.h:103
std::vector< double > get(std::vector< IndexType > const &indices) const
get entries
Definition: EigenVector.h:64
static constexpr IndexType getRangeBegin()
return a start index of the active data range
Definition: EigenVector.h:48
Eigen::Map< const Vector > toVector(std::vector< double > const &data, Eigen::VectorXd::Index size)
Creates an Eigen mapped vector from the given data vector.
static const double u
static const double v