OGS
LisVector.h
Go to the documentation of this file.
1
15#pragma once
16
17#include <lis.h>
18
19#include <cassert>
20#include <span>
21#include <string>
22#include <vector>
23
24#include "BaseLib/Error.h"
25
26namespace MathLib
27{
32{
33public:
34 using IndexType = LIS_INT;
35
36public:
41 explicit LisVector(std::size_t length);
42
48 LisVector(std::size_t length, double* data);
49
51 LisVector(LisVector const& src);
52
55 virtual ~LisVector();
56
58 std::size_t size() const;
59
61 std::size_t getRangeBegin() const { return 0; }
63 std::size_t getRangeEnd() const { return this->size(); }
64
65 // TODO preliminary
66 void setZero() { lis_vector_set_all(0.0, vec_); }
67
69 double operator[](IndexType rowId) const { return get(rowId); }
71 double get(IndexType rowId) const
72 {
73 double v = .0;
74 lis_vector_get_value(vec_, rowId, &v);
75 return v;
76 }
77
79 void set(IndexType rowId, double v)
80 {
81 lis_vector_set_value(LIS_INS_VALUE, rowId, v, vec_);
82 }
83
85 void add(IndexType rowId, double v)
86 {
87 lis_vector_set_value(LIS_ADD_VALUE, rowId, v, vec_);
88 }
89
91 void write(const std::string& filename) const;
92
94 LIS_VECTOR& getRawVector() { return vec_; }
95
97 template <class T_SUBVEC>
98 void set(const std::vector<IndexType>& pos, const T_SUBVEC& sub_vec)
99 {
100 for (std::size_t i = 0; i < pos.size(); ++i)
101 {
102 set(pos[i], sub_vec[i]);
103 }
104 }
105
107 template <class T_SUBVEC>
108 void add(const std::vector<IndexType>& pos, const T_SUBVEC& sub_vec)
109 {
110 for (std::size_t i = 0; i < pos.size(); ++i)
111 {
112 add(pos[i], sub_vec[i]);
113 }
114 }
115
119 void copyValues(std::vector<double>& u) const
120 {
121 u.resize(size());
122 lis_vector_get_values(vec_, 0, size(), u.data());
123 }
124
128 void copyValues(std::span<double> u) const
129 {
130 if (u.size() != size())
131 {
132 OGS_FATAL(
133 "LisVector::copyValues() size mismatch. Trying to copy a "
134 "vector "
135 "of size {:d} to a span of size {:d}.",
136 size(), u.size());
137 }
138 lis_vector_get_values(vec_, 0, size(), u.data());
139 }
140
141private:
142 LIS_VECTOR vec_;
143};
144
145} // namespace MathLib
#define OGS_FATAL(...)
Definition Error.h:26
Lis vector wrapper class.
Definition LisVector.h:32
void copyValues(std::span< double > u) const
Definition LisVector.h:128
void add(IndexType rowId, double v)
add entry
Definition LisVector.h:85
LisVector(std::size_t length)
Definition LisVector.cpp:21
void add(const std::vector< IndexType > &pos, const T_SUBVEC &sub_vec)
add entries
Definition LisVector.h:108
void copyValues(std::vector< double > &u) const
Definition LisVector.h:119
std::size_t getRangeEnd() const
return an end index of the active data range
Definition LisVector.h:63
void write(const std::string &filename) const
printout this equation for debugging
Definition LisVector.cpp:56
void set(const std::vector< IndexType > &pos, const T_SUBVEC &sub_vec)
set entries
Definition LisVector.h:98
void set(IndexType rowId, double v)
set entry
Definition LisVector.h:79
virtual ~LisVector()
Definition LisVector.cpp:41
LIS_VECTOR & getRawVector()
return a raw Lis vector object
Definition LisVector.h:94
std::size_t size() const
return a vector length
Definition LisVector.cpp:46
double get(IndexType rowId) const
get entry
Definition LisVector.h:71
double operator[](IndexType rowId) const
access entry
Definition LisVector.h:69
LIS_VECTOR vec_
Definition LisVector.h:142
std::size_t getRangeBegin() const
return a start index of the active data range
Definition LisVector.h:61
static const double u
static const double v