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