OGS
EigenVector.h
Go to the documentation of this file.
1 
11 #pragma once
12 
13 #include <vector>
14 
15 #ifndef NDEBUG
16 #include <string>
17 #endif
18 
19 #include <Eigen/Sparse>
20 
21 namespace MathLib
22 {
24 class EigenVector final
25 {
26 public:
27  using RawVectorType = Eigen::VectorXd;
28 
29  // The Index type of the Eigen::VectorXd class differs from the
30  // Eigen::SparseMatrix<double> index type. Maybe an Eigen::SparseVector is a
31  // more appropriate RawVectorType for the global vectors.
32  using IndexType = Eigen::SparseMatrix<double>::Index;
33 
34  // TODO: preliminary
35  EigenVector() = default;
36 
39  explicit EigenVector(IndexType length) : vec_(length) {}
40 
42  IndexType size() const { return static_cast<IndexType>(vec_.size()); }
43 
45  static constexpr IndexType getRangeBegin() { return 0; }
46 
48  IndexType getRangeEnd() const { return size(); }
49 
50  // TODO preliminary
51  void setZero() { vec_.setZero(); }
52 
54  double const& operator[](IndexType rowId) const { return vec_[rowId]; }
55  double& operator[](IndexType rowId) { return vec_[rowId]; }
56 
58  double get(IndexType rowId) const { return vec_[rowId]; }
59 
61  std::vector<double> get(std::vector<IndexType> const& indices) const
62  {
63  std::vector<double> local_x;
64  local_x.reserve(indices.size());
65 
66  transform(cbegin(indices), cend(indices), back_inserter(local_x),
67  [&](auto const i) { return vec_[i]; });
68 
69  return local_x;
70  }
71 
73  void set(IndexType rowId, double v) { vec_[rowId] = v; }
74 
76  void add(IndexType rowId, double v) { vec_[rowId] += v; }
77 
79  template <class T_SUBVEC>
80  void set(const std::vector<IndexType>& pos, const T_SUBVEC& sub_vec)
81  {
82  auto const length = pos.size();
83  for (std::size_t i = 0; i < length; ++i)
84  {
85  set(pos[i], sub_vec[i]);
86  }
87  }
88 
90  template <class T_SUBVEC>
91  void add(const std::vector<IndexType>& pos, const T_SUBVEC& sub_vec)
92  {
93  auto const length = pos.size();
94  for (std::size_t i = 0; i < length; ++i)
95  {
96  add(pos[i], sub_vec[i]);
97  }
98  }
99 
103  void copyValues(std::vector<double>& u) const;
104 
105 #ifndef NDEBUG
107  void write(const std::string& filename) const;
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:25
double const & operator[](IndexType rowId) const
access entry
Definition: EigenVector.h:54
IndexType getRangeEnd() const
return an end index of the active data range
Definition: EigenVector.h:48
void copyValues(std::vector< double > &u) const
Definition: EigenVector.cpp:21
RawVectorType vec_
Definition: EigenVector.h:117
void add(IndexType rowId, double v)
add entry
Definition: EigenVector.h:76
double & operator[](IndexType rowId)
Definition: EigenVector.h:55
IndexType size() const
return a vector length
Definition: EigenVector.h:42
double get(IndexType rowId) const
get entry
Definition: EigenVector.h:58
void set(IndexType rowId, double v)
set entry
Definition: EigenVector.h:73
void set(const std::vector< IndexType > &pos, const T_SUBVEC &sub_vec)
set entries
Definition: EigenVector.h:80
RawVectorType & getRawVector()
return a raw Eigen vector object
Definition: EigenVector.h:111
Eigen::VectorXd RawVectorType
Definition: EigenVector.h:27
EigenVector(IndexType length)
Definition: EigenVector.h:39
void add(const std::vector< IndexType > &pos, const T_SUBVEC &sub_vec)
add entries
Definition: EigenVector.h:91
const RawVectorType & getRawVector() const
return a raw Eigen vector object
Definition: EigenVector.h:114
Eigen::SparseMatrix< double >::Index IndexType
Definition: EigenVector.h:32
void write(const std::string &filename) const
write this vector to a file for debugging
Definition: EigenVector.cpp:28
std::vector< double > get(std::vector< IndexType > const &indices) const
get entries
Definition: EigenVector.h:61
static constexpr IndexType getRangeBegin()
return a start index of the active data range
Definition: EigenVector.h:45
static const double u
static const double v