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 namespace MathLib
23 {
25 class EigenVector final
26 {
27 public:
28  using RawVectorType = Eigen::VectorXd;
29 
30  // The Index type of the Eigen::VectorXd class differs from the
31  // Eigen::SparseMatrix<double> index type. Maybe an Eigen::SparseVector is a
32  // more appropriate RawVectorType for the global vectors.
33  using IndexType = Eigen::SparseMatrix<double>::Index;
34 
35  // TODO: preliminary
36  EigenVector() = default;
37 
40  explicit EigenVector(IndexType length) : vec_(length) {}
41 
43  EigenVector(EigenVector const& src) = default;
44 
46  IndexType size() const { return static_cast<IndexType>(vec_.size()); }
47 
49  static constexpr IndexType getRangeBegin() { return 0; }
50 
52  IndexType getRangeEnd() const { return size(); }
53 
54  // TODO preliminary
55  void setZero() { vec_.setZero(); }
56 
58  double const& operator[](IndexType rowId) const { return vec_[rowId]; }
59  double& operator[](IndexType rowId) { return vec_[rowId]; }
60 
62  double get(IndexType rowId) const { return vec_[rowId]; }
63 
65  std::vector<double> get(std::vector<IndexType> const& indices) const
66  {
67  std::vector<double> local_x;
68  local_x.reserve(indices.size());
69 
70  transform(cbegin(indices), cend(indices), back_inserter(local_x),
71  [&](auto const i) { return vec_[i]; });
72 
73  return local_x;
74  }
75 
77  void set(IndexType rowId, double v) { vec_[rowId] = v; }
78 
80  void add(IndexType rowId, double v) { vec_[rowId] += v; }
81 
83  template <class T_SUBVEC>
84  void add(const std::vector<IndexType>& pos, const T_SUBVEC& sub_vec)
85  {
86  auto const length = pos.size();
87  for (std::size_t i = 0; i < length; ++i)
88  {
89  add(pos[i], sub_vec[i]);
90  }
91  }
92 
94  void copyValues(std::vector<double>& u) const
95  {
96  assert(u.size() == (std::size_t)vec_.size());
97  copy_n(vec_.data(), vec_.size(), u.begin());
98  }
99 
100 #ifndef NDEBUG
102  void write(const std::string& filename) const
103  {
104  std::ofstream os(filename);
105  os << vec_;
106  }
107 #endif
108 
111 
113  const RawVectorType& getRawVector() const { return vec_; }
114 
115 private:
117 };
118 
119 } // namespace MathLib
Global vector based on Eigen vector.
Definition: EigenVector.h:26
double const & operator[](IndexType rowId) const
access entry
Definition: EigenVector.h:58
IndexType getRangeEnd() const
return an end index of the active data range
Definition: EigenVector.h:52
void copyValues(std::vector< double > &u) const
Copy vector values.
Definition: EigenVector.h:94
RawVectorType vec_
Definition: EigenVector.h:116
void add(IndexType rowId, double v)
add entry
Definition: EigenVector.h:80
double & operator[](IndexType rowId)
Definition: EigenVector.h:59
IndexType size() const
return a vector length
Definition: EigenVector.h:46
double get(IndexType rowId) const
get entry
Definition: EigenVector.h:62
void set(IndexType rowId, double v)
set entry
Definition: EigenVector.h:77
EigenVector(EigenVector const &src)=default
copy constructor
RawVectorType & getRawVector()
return a raw Eigen vector object
Definition: EigenVector.h:110
Eigen::VectorXd RawVectorType
Definition: EigenVector.h:28
EigenVector(IndexType length)
Definition: EigenVector.h:40
void add(const std::vector< IndexType > &pos, const T_SUBVEC &sub_vec)
add entries
Definition: EigenVector.h:84
const RawVectorType & getRawVector() const
return a raw Eigen vector object
Definition: EigenVector.h:113
Eigen::SparseMatrix< double >::Index IndexType
Definition: EigenVector.h:33
void write(const std::string &filename) const
printout this equation for debugging
Definition: EigenVector.h:102
std::vector< double > get(std::vector< IndexType > const &indices) const
get entries
Definition: EigenVector.h:65
static constexpr IndexType getRangeBegin()
return a start index of the active data range
Definition: EigenVector.h:49