OGS 6.1.0-1721-g6382411ad
LisMatrix.cpp
Go to the documentation of this file.
1 
15 #include "LisMatrix.h"
16 
17 #include <cmath>
18 #include <cstdlib>
19 
20 #include <logog/include/logog.hpp>
21 
22 #include "BaseLib/Error.h"
23 #include "LisVector.h"
24 #include "LisCheck.h"
25 
26 namespace MathLib
27 {
28 
29 LisMatrix::LisMatrix(std::size_t n_rows, MatrixType mat_type)
30  : _n_rows(n_rows), _mat_type(mat_type), _is_assembled(false), _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 
41 LisMatrix::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),
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  ierr = lis_matrix_unset(_AA);
67  ierr = lis_matrix_destroy(_AA);
68  checkLisError(ierr);
69  ierr = lis_vector_destroy(_diag);
70  checkLisError(ierr);
71 }
72 
74 {
75  // A matrix has to be destroyed and created again because Lis doesn't provide a
76  // function to set matrix entries to zero
77  int ierr = lis_matrix_destroy(_AA);
78  checkLisError(ierr);
79  ierr = lis_matrix_create(0, &_AA);
80  checkLisError(ierr);
81  ierr = lis_matrix_set_size(_AA, 0, _n_rows);
82  checkLisError(ierr);
83  ierr = lis_vector_set_all(0.0, _diag);
84  checkLisError(ierr);
85 
86  _is_assembled = false;
87 }
88 
89 int LisMatrix::setValue(IndexType rowId, IndexType colId, double v)
90 {
91  if (v == 0.0) return 0;
92  lis_matrix_set_value(LIS_INS_VALUE, rowId, colId, v, _AA);
93  if (rowId==colId)
94  lis_vector_set_value(LIS_INS_VALUE, rowId, v, _diag);
95  _is_assembled = false;
96  return 0;
97 }
98 
99 int LisMatrix::add(IndexType rowId, IndexType colId, double v)
100 {
101  if (v == 0.0) return 0;
102  lis_matrix_set_value(LIS_ADD_VALUE, rowId, colId, v, _AA);
103  if (rowId==colId)
104  lis_vector_set_value(LIS_ADD_VALUE, rowId, v, _diag);
105  _is_assembled = false;
106  return 0;
107 }
108 
109 void LisMatrix::write(const std::string &filename) const
110 {
111  if (!_is_assembled)
112  {
113  OGS_FATAL("LisMatrix::write(): matrix not assembled.");
114  }
115  lis_output_matrix(_AA, LIS_FMT_MM, const_cast<char*>(filename.c_str()));
116 }
117 
119 {
120  double abs_max_entry;
121  int ierr = lis_vector_get_value(_diag, 0, &abs_max_entry);
122  checkLisError(ierr);
123  abs_max_entry = std::abs(abs_max_entry);
124  for (std::size_t k(1); k<_n_rows; ++k) {
125  double tmp;
126  ierr = lis_vector_get_value(_diag, k, &tmp);
127  checkLisError(ierr);
128  if (abs_max_entry < std::abs(tmp)) {
129  abs_max_entry = std::abs(tmp);
130  }
131  }
132 
133  return abs_max_entry;
134 }
135 
137 {
138  LIS_MATRIX &A = mat.getRawMatrix();
139 
140  if (!mat.isAssembled()) {
141  int ierr = lis_matrix_set_type(A, static_cast<int>(mat.getMatrixType()));
142  checkLisError(ierr);
143  ierr = lis_matrix_assemble(A);
144  checkLisError(ierr);
145  mat._is_assembled = true;
146  }
147  return true;
148 }
149 
150 
151 } //MathLib
MatrixType const _mat_type
Definition: LisMatrix.h:152
void setZero()
reset this matrix with keeping its original dimension
Definition: LisMatrix.cpp:73
IndexType _is
location where the partial matrix _AA starts in global matrix.
Definition: LisMatrix.h:156
LIS_MATRIX _AA
Definition: LisMatrix.h:153
void write(const std::string &filename) const
printout this equation for debugging
Definition: LisMatrix.cpp:109
IndexType _ie
location where the partial matrix _AA ends in global matrix.
Definition: LisMatrix.h:157
friend bool finalizeMatrixAssembly(LisMatrix &mat)
finish assembly to make this matrix be ready for use
Definition: LisMatrix.cpp:136
int setValue(IndexType rowId, IndexType colId, double v)
set entry
Definition: LisMatrix.cpp:89
LisMatrix(std::size_t n_rows, MatrixType mat_type=MatrixType::CRS)
Definition: LisMatrix.cpp:29
LIS_MATRIX & getRawMatrix()
return a raw Lis matrix object
Definition: LisMatrix.h:118
bool isAssembled() const
return if this matrix is already assembled or not
Definition: LisMatrix.h:148
bool _use_external_arrays
Definition: LisMatrix.h:158
MatrixType getMatrixType() const
get this matrix type
Definition: LisMatrix.h:145
std::size_t const _n_rows
Definition: LisMatrix.h:151
LIS_VECTOR _diag
Definition: LisMatrix.h:154
double getMaxDiagCoeff()
get a maximum value in diagonal entries
Definition: LisMatrix.cpp:118
int add(IndexType rowId, IndexType colId, double v)
add value
Definition: LisMatrix.cpp:99
MatrixType
Matrix type.
Definition: LisMatrix.h:48
LisMatrix is a wrapper class for matrix types of the linear iterative solvers library.
Definition: LisMatrix.h:44
bool checkLisError(int err)
Definition: LisCheck.h:30
virtual ~LisMatrix()
Definition: LisMatrix.cpp:62
#define OGS_FATAL(fmt,...)
Definition: Error.h:71
Definition of the LisMatrix class.
LIS_INT IndexType
Definition: LisMatrix.h:63
Definition of the LisVector class.