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