OGS
LisVector.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 <cassert>
7#include <span>
8#include <string>
9#include <vector>
10
11#include "BaseLib/Error.h"
12#include "LisWrapper.h"
13
14namespace MathLib
15{
20{
21public:
22 using IndexType = LIS_INT;
23
24public:
29 explicit LisVector(std::size_t length);
30
36 LisVector(std::size_t length, double* data);
37
39 LisVector(LisVector const& src);
40
43 virtual ~LisVector();
44
46 std::size_t size() const;
47
49 std::size_t getRangeBegin() const { return 0; }
51 std::size_t getRangeEnd() const { return this->size(); }
52
53 // TODO preliminary
54 void setZero() { lis_vector_set_all(0.0, vec_); }
55
57 double operator[](IndexType rowId) const { return get(rowId); }
59 double get(IndexType rowId) const
60 {
61 double v = .0;
62 lis_vector_get_value(vec_, rowId, &v);
63 return v;
64 }
65
67 void set(IndexType rowId, double v)
68 {
69 lis_vector_set_value(LIS_INS_VALUE, rowId, v, vec_);
70 }
71
73 void add(IndexType rowId, double v)
74 {
75 lis_vector_set_value(LIS_ADD_VALUE, rowId, v, vec_);
76 }
77
79 void write(const std::string& filename) const;
80
82 LIS_VECTOR& getRawVector() { return vec_; }
83
85 template <class T_SUBVEC>
86 void set(const std::vector<IndexType>& pos, const T_SUBVEC& sub_vec)
87 {
88 for (std::size_t i = 0; i < pos.size(); ++i)
89 {
90 set(pos[i], sub_vec[i]);
91 }
92 }
93
95 template <class T_SUBVEC>
96 void add(const std::vector<IndexType>& pos, const T_SUBVEC& sub_vec)
97 {
98 for (std::size_t i = 0; i < pos.size(); ++i)
99 {
100 add(pos[i], sub_vec[i]);
101 }
102 }
103
107 void copyValues(std::vector<double>& u) const
108 {
109 u.resize(size());
110 lis_vector_get_values(vec_, 0, size(), u.data());
111 }
112
116 void copyValues(std::span<double> u) const
117 {
118 if (u.size() != size())
119 {
120 OGS_FATAL(
121 "LisVector::copyValues() size mismatch. Trying to copy a "
122 "vector "
123 "of size {:d} to a span of size {:d}.",
124 size(), u.size());
125 }
126 lis_vector_get_values(vec_, 0, size(), u.data());
127 }
128
129private:
130 LIS_VECTOR vec_;
131};
132
133} // namespace MathLib
#define OGS_FATAL(...)
Definition Error.h:19
void copyValues(std::span< double > u) const
Definition LisVector.h:116
void add(IndexType rowId, double v)
add entry
Definition LisVector.h:73
LisVector(std::size_t length)
Definition LisVector.cpp:10
void add(const std::vector< IndexType > &pos, const T_SUBVEC &sub_vec)
add entries
Definition LisVector.h:96
void copyValues(std::vector< double > &u) const
Definition LisVector.h:107
std::size_t getRangeEnd() const
return an end index of the active data range
Definition LisVector.h:51
void write(const std::string &filename) const
printout this equation for debugging
Definition LisVector.cpp:45
void set(const std::vector< IndexType > &pos, const T_SUBVEC &sub_vec)
set entries
Definition LisVector.h:86
void set(IndexType rowId, double v)
set entry
Definition LisVector.h:67
virtual ~LisVector()
Definition LisVector.cpp:30
LIS_VECTOR & getRawVector()
return a raw Lis vector object
Definition LisVector.h:82
std::size_t size() const
return a vector length
Definition LisVector.cpp:35
double get(IndexType rowId) const
get entry
Definition LisVector.h:59
double operator[](IndexType rowId) const
access entry
Definition LisVector.h:57
LIS_VECTOR vec_
Definition LisVector.h:130
std::size_t getRangeBegin() const
return a start index of the active data range
Definition LisVector.h:49
static const double u
static const double v