OGS
LisMatrix.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
4#include "LisMatrix.h"
5
6#include <cmath>
7#include <cstdlib>
8
9#include "BaseLib/Error.h"
10#include "LisCheck.h"
11#include "LisVector.h"
12
13namespace MathLib
14{
15LisMatrix::LisMatrix(std::size_t n_rows, MatrixType mat_type)
16 : n_rows_(n_rows),
17 mat_type_(mat_type),
18 is_assembled_(false),
20{
21 int ierr = lis_matrix_create(0, &AA_);
22 checkLisError(ierr);
23 ierr = lis_matrix_set_size(AA_, 0, n_rows);
24 checkLisError(ierr);
25 lis_matrix_get_range(AA_, &is_, &ie_);
26 ierr = lis_vector_duplicate(AA_, &diag_);
27 checkLisError(ierr);
28}
29
30LisMatrix::LisMatrix(std::size_t n_rows, int nnz, IndexType* row_ptr,
31 IndexType* col_idx, double* data)
32 : n_rows_(n_rows),
34 is_assembled_(false),
36{
37 int ierr = lis_matrix_create(0, &AA_);
38 checkLisError(ierr);
39 ierr = lis_matrix_set_size(AA_, 0, n_rows);
40 checkLisError(ierr);
41 ierr = lis_matrix_set_csr(nnz, row_ptr, col_idx, data, AA_);
42 checkLisError(ierr);
43 ierr = lis_matrix_assemble(AA_);
44 checkLisError(ierr);
45 is_assembled_ = true;
46 lis_matrix_get_range(AA_, &is_, &ie_);
47 ierr = lis_vector_duplicate(AA_, &diag_);
48 checkLisError(ierr);
49}
50
52{
53 int ierr = 0;
55 {
56 ierr = lis_matrix_unset(AA_);
57 checkLisError(ierr);
58 }
59 ierr = lis_matrix_destroy(AA_);
60 checkLisError(ierr);
61 ierr = lis_vector_destroy(diag_);
62 checkLisError(ierr);
63}
64
66{
67 // A matrix has to be destroyed and created again because Lis doesn't
68 // provide a function to set matrix entries to zero
69 int ierr = lis_matrix_destroy(AA_);
70 checkLisError(ierr);
71 ierr = lis_matrix_create(0, &AA_);
72 checkLisError(ierr);
73 ierr = lis_matrix_set_size(AA_, 0, n_rows_);
74 checkLisError(ierr);
75 ierr = lis_vector_set_all(0.0, diag_);
76 checkLisError(ierr);
77
78 is_assembled_ = false;
79}
80
81int LisMatrix::setValue(IndexType rowId, IndexType colId, double v)
82{
83 if (v == 0.0)
84 {
85 return 0;
86 }
87 lis_matrix_set_value(LIS_INS_VALUE, rowId, colId, v, AA_);
88 if (rowId == colId)
89 {
90 lis_vector_set_value(LIS_INS_VALUE, rowId, v, diag_);
91 }
92 is_assembled_ = false;
93 return 0;
94}
95
96int LisMatrix::add(IndexType rowId, IndexType colId, double v)
97{
98 if (v == 0.0)
99 {
100 return 0;
101 }
102 lis_matrix_set_value(LIS_ADD_VALUE, rowId, colId, v, AA_);
103 if (rowId == colId)
104 {
105 lis_vector_set_value(LIS_ADD_VALUE, rowId, v, diag_);
106 }
107 is_assembled_ = false;
108 return 0;
109}
110
111void LisMatrix::write(const std::string& filename) const
112{
113 if (!is_assembled_)
114 {
115 OGS_FATAL("LisMatrix::write(): matrix not assembled.");
116 }
117 lis_output_matrix(AA_, LIS_FMT_MM, const_cast<char*>(filename.c_str()));
118}
119
121{
122 LIS_MATRIX& A = mat.getRawMatrix();
123
124 if (!mat.isAssembled())
125 {
126 int ierr =
127 lis_matrix_set_type(A, static_cast<int>(mat.getMatrixType()));
128 checkLisError(ierr);
129 ierr = lis_matrix_assemble(A);
130 checkLisError(ierr);
131 mat.is_assembled_ = true;
132 }
133 return true;
134}
135
136} // namespace MathLib
#define OGS_FATAL(...)
Definition Error.h:19
IndexType ie_
location where the partial matrix AA_ ends in global matrix.
Definition LisMatrix.h:143
LisMatrix(std::size_t n_rows, MatrixType mat_type=MatrixType::CRS)
Definition LisMatrix.cpp:15
std::size_t const n_rows_
Definition LisMatrix.h:135
LIS_VECTOR diag_
Definition LisMatrix.h:138
MatrixType const mat_type_
Definition LisMatrix.h:136
int setValue(IndexType rowId, IndexType colId, double v)
set entry
Definition LisMatrix.cpp:81
int add(IndexType rowId, IndexType colId, double v)
add value
Definition LisMatrix.cpp:96
void setZero()
reset this matrix with keeping its original dimension
Definition LisMatrix.cpp:65
MatrixType getMatrixType() const
get this matrix type
Definition LisMatrix.h:129
void write(const std::string &filename) const
printout this equation for debugging
MatrixType
Matrix type.
Definition LisMatrix.h:35
LIS_MATRIX & getRawMatrix()
return a raw Lis matrix object
Definition LisMatrix.h:102
virtual ~LisMatrix()
Definition LisMatrix.cpp:51
bool isAssembled() const
return if this matrix is already assembled or not
Definition LisMatrix.h:132
bool checkLisError(int err)
Definition LisCheck.h:20
static const double v
bool finalizeMatrixAssembly(MAT_T &)