OGS
EigenVector.h
Go to the documentation of this file.
1// SPDX-FileCopyrightText: Copyright (c) OpenGeoSys Community (opengeosys.org)
2// SPDX-License-Identifier: BSD-3-Clause
3
4#pragma once
5
6#include <span>
7#include <vector>
8
9#ifndef NDEBUG
10#include <string>
11#endif
12
13#include <Eigen/Sparse>
14
15namespace MathLib
16{
18class EigenVector final
19{
20public:
21 using RawVectorType = Eigen::VectorXd;
22
23 // The Index type of the Eigen::VectorXd class differs from the
24 // Eigen::SparseMatrix<double> index type. Maybe an Eigen::SparseVector is a
25 // more appropriate RawVectorType for the global vectors.
26 using IndexType = Eigen::SparseMatrix<double>::Index;
27
28 // TODO: preliminary
29 EigenVector() = default;
30
33 explicit EigenVector(IndexType length) : vec_(length) {}
34
36 IndexType size() const { return static_cast<IndexType>(vec_.size()); }
37
39 static constexpr IndexType getRangeBegin() { return 0; }
40
42 IndexType getRangeEnd() const { return size(); }
43
44 // TODO preliminary
45 void setZero() { vec_.setZero(); }
46
48 double const& operator[](IndexType rowId) const { return vec_[rowId]; }
49 double& operator[](IndexType rowId) { return vec_[rowId]; }
50
52 double get(IndexType rowId) const { return vec_[rowId]; }
53
55 std::vector<double> get(std::vector<IndexType> const& indices) const
56 {
57 std::vector<double> local_x;
58 local_x.reserve(indices.size());
59
60 transform(cbegin(indices), cend(indices), back_inserter(local_x),
61 [&](auto const i) { return vec_[i]; });
62
63 return local_x;
64 }
65
67 void set(IndexType rowId, double v) { vec_[rowId] = v; }
68
70 void add(IndexType rowId, double v) { vec_[rowId] += v; }
71
73 template <class T_SUBVEC>
74 void set(const std::vector<IndexType>& pos, const T_SUBVEC& sub_vec)
75 {
76 auto const length = pos.size();
77 for (std::size_t i = 0; i < length; ++i)
78 {
79 set(pos[i], sub_vec[i]);
80 }
81 }
82
84 template <class T_SUBVEC>
85 void add(const std::vector<IndexType>& pos, const T_SUBVEC& sub_vec)
86 {
87 auto const length = pos.size();
88 for (std::size_t i = 0; i < length; ++i)
89 {
90 add(pos[i], sub_vec[i]);
91 }
92 }
93
97 void copyValues(std::vector<double>& u) const;
98
102 void copyValues(std::span<double> u) const;
103
104#ifndef NDEBUG
106 void write(const std::string& filename) const;
107#endif
108
111
113 const RawVectorType& getRawVector() const { return vec_; }
114
115private:
117};
118
119} // namespace MathLib
IndexType getRangeEnd() const
return an end index of the active data range
Definition EigenVector.h:42
void copyValues(std::vector< double > &u) const
RawVectorType vec_
void add(IndexType rowId, double v)
add entry
Definition EigenVector.h:70
IndexType size() const
return a vector length
Definition EigenVector.h:36
double get(IndexType rowId) const
get entry
Definition EigenVector.h:52
void set(IndexType rowId, double v)
set entry
Definition EigenVector.h:67
void set(const std::vector< IndexType > &pos, const T_SUBVEC &sub_vec)
set entries
Definition EigenVector.h:74
Eigen::VectorXd RawVectorType
Definition EigenVector.h:21
EigenVector(IndexType length)
Definition EigenVector.h:33
void add(const std::vector< IndexType > &pos, const T_SUBVEC &sub_vec)
add entries
Definition EigenVector.h:85
const RawVectorType & getRawVector() const
return a raw Eigen vector object
double & operator[](IndexType rowId)
Definition EigenVector.h:49
RawVectorType & getRawVector()
return a raw Eigen vector object
double const & operator[](IndexType rowId) const
access entry
Definition EigenVector.h:48
std::vector< double > get(std::vector< IndexType > const &indices) const
get entries
Definition EigenVector.h:55
Eigen::SparseMatrix< double >::Index IndexType
Definition EigenVector.h:26
void write(const std::string &filename) const
write this vector to a file for debugging
static constexpr IndexType getRangeBegin()
return a start index of the active data range
Definition EigenVector.h:39
static const double u
static const double v