OGS
UnifiedMatrixSetters.cpp
Go to the documentation of this file.
1// SPDX-FileCopyrightText: Copyright (c) OpenGeoSys Community (opengeosys.org)
2// SPDX-License-Identifier: BSD-3-Clause
3
5
6#include <cassert>
7
8#ifdef USE_PETSC
9
10// Global PETScMatrix/PETScVector //////////////////////////////////////////
11
12#include <numeric>
13
16
17namespace MathLib
18{
19void setVector(PETScVector& v, std::initializer_list<double> values)
20{
21 std::vector<double> const vals(values);
22 std::vector<PETScVector::IndexType> idcs(vals.size());
23 std::iota(idcs.begin(), idcs.end(), 0);
24
25 v.set(idcs, vals);
26}
27
30 double const value)
31{
32 v.set(index, value); // TODO handle negative indices
33}
34
35void setMatrix(PETScMatrix& m, std::initializer_list<double> values)
36{
37 m.setZero();
38 addToMatrix(m, values);
39}
40
41void setMatrix(PETScMatrix& m, Eigen::MatrixXd const& tmp)
42{
43 using IndexType = PETScMatrix::IndexType;
44
45 auto const rows = tmp.rows();
46 auto const cols = tmp.cols();
47
48 assert(rows == m.getNumberOfRows() && cols == m.getNumberOfColumns());
49
50 m.setZero();
51 std::vector<IndexType> row_idcs(rows);
52 std::vector<IndexType> col_idcs(cols);
53
54 std::iota(row_idcs.begin(), row_idcs.end(), 0);
55 std::iota(col_idcs.begin(), col_idcs.end(), 0);
56
57 // PETSc wants row-major
58 Eigen::Matrix<double, Eigen::Dynamic, Eigen::Dynamic, Eigen::RowMajor>
59 tmp_ = tmp;
60
61 m.add(row_idcs, col_idcs, tmp_);
62}
63
64void addToMatrix(PETScMatrix& m, std::initializer_list<double> values)
65{
66 using IndexType = PETScMatrix::IndexType;
67
68 auto const rows = m.getNumberOfRows();
69 auto const cols = m.getNumberOfColumns();
70
71 assert((IndexType)values.size() == rows * cols);
72
73 Eigen::Matrix<double, Eigen::Dynamic, Eigen::Dynamic, Eigen::RowMajor> tmp(
74 rows, cols);
75
76 auto it = values.begin();
77 for (IndexType r = 0; r < rows; ++r)
78 {
79 for (IndexType c = 0; c < cols; ++c)
80 {
81 tmp(r, c) = *(it++);
82 }
83 }
84
85 std::vector<IndexType> row_idcs(rows);
86 std::vector<IndexType> col_idcs(cols);
87
88 std::iota(row_idcs.begin(), row_idcs.end(), 0);
89 std::iota(col_idcs.begin(), col_idcs.end(), 0);
90
91 m.add(row_idcs, col_idcs, tmp);
92}
93
94} // namespace MathLib
95
96#else
97
98// Sparse global EigenMatrix/EigenVector
99// //////////////////////////////////////////
100
103
104namespace MathLib
105{
106void setVector(EigenVector& v, std::initializer_list<double> values)
107{
108 auto& w = v.getRawVector();
109 assert((std::size_t)v.size() == values.size());
110 auto it = values.begin();
111 for (std::size_t i = 0; i < values.size(); ++i)
112 {
113 w[i] = *(it++);
114 }
115}
116
119 double const value)
120{
121 v.getRawVector()[index] = value;
122}
123
124void setMatrix(EigenMatrix& m, std::initializer_list<double> values)
125{
126 auto const rows = m.getNumberOfRows();
127 auto const cols = m.getNumberOfColumns();
128
129 assert(static_cast<EigenMatrix::IndexType>(values.size()) == rows * cols);
130 Eigen::MatrixXd tmp(rows, cols);
131
132 auto it = values.begin();
133 for (GlobalIndexType r = 0; r < rows; ++r)
134 {
135 for (GlobalIndexType c = 0; c < cols; ++c)
136 {
137 tmp(r, c) = *(it++);
138 }
139 }
140
141 m.getRawMatrix() = tmp.sparseView();
142}
143
144void setMatrix(EigenMatrix& m, Eigen::MatrixXd const& tmp)
145{
146 m.getRawMatrix() = tmp.sparseView();
147}
148
149void addToMatrix(EigenMatrix& m, std::initializer_list<double> values)
150{
151 auto const rows = m.getNumberOfRows();
152 auto const cols = m.getNumberOfColumns();
153
154 assert(static_cast<EigenMatrix::IndexType>(values.size()) == rows * cols);
155 Eigen::Matrix<double, Eigen::Dynamic, Eigen::Dynamic, Eigen::RowMajor> tmp(
156 rows, cols);
157
158 auto it = values.begin();
159 for (GlobalIndexType r = 0; r < rows; ++r)
160 {
161 for (GlobalIndexType c = 0; c < cols; ++c)
162 {
163 tmp(r, c) = *(it++);
164 }
165 }
166
167 m.getRawMatrix() += tmp.sparseView();
168}
169
170} // namespace MathLib
171
172#endif
GlobalMatrix::IndexType GlobalIndexType
RawMatrixType::Index IndexType
Definition EigenMatrix.h:25
Global vector based on Eigen vector.
Definition EigenVector.h:19
Wrapper class for PETSc matrix routines for matrix.
Definition PETScMatrix.h:21
PetscInt getNumberOfColumns() const
Get the number of columns.
Definition PETScMatrix.h:65
void setZero()
Set all entries to zero.
Definition PETScMatrix.h:84
void add(const PetscInt i, const PetscInt j, const PetscScalar value)
Add value to a single entry.
PetscInt getNumberOfRows() const
Get the number of rows.
Definition PETScMatrix.h:63
Wrapper class for PETSc vector.
Definition PETScVector.h:28
void addToMatrix(PETScMatrix &m, std::initializer_list< double > values)
void setVector(PETScVector &v, std::initializer_list< double > values)
static const double r
static const double v
void setMatrix(PETScMatrix &m, std::initializer_list< double > values)