OGS 6.2.1-97-g73d1aeda3
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  {
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 provide a
79  // 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 
92 int LisMatrix::setValue(IndexType rowId, IndexType colId, double v)
93 {
94  if (v == 0.0) return 0;
95  lis_matrix_set_value(LIS_INS_VALUE, rowId, colId, v, _AA);
96  if (rowId==colId)
97  lis_vector_set_value(LIS_INS_VALUE, rowId, v, _diag);
98  _is_assembled = false;
99  return 0;
100 }
101 
102 int LisMatrix::add(IndexType rowId, IndexType colId, double v)
103 {
104  if (v == 0.0) return 0;
105  lis_matrix_set_value(LIS_ADD_VALUE, rowId, colId, v, _AA);
106  if (rowId==colId)
107  lis_vector_set_value(LIS_ADD_VALUE, rowId, v, _diag);
108  _is_assembled = false;
109  return 0;
110 }
111 
112 void LisMatrix::write(const std::string &filename) const
113 {
114  if (!_is_assembled)
115  {
116  OGS_FATAL("LisMatrix::write(): matrix not assembled.");
117  }
118  lis_output_matrix(_AA, LIS_FMT_MM, const_cast<char*>(filename.c_str()));
119 }
120 
122 {
123  double abs_max_entry;
124  int ierr = lis_vector_get_value(_diag, 0, &abs_max_entry);
125  checkLisError(ierr);
126  abs_max_entry = std::abs(abs_max_entry);
127  for (std::size_t k(1); k<_n_rows; ++k) {
128  double tmp;
129  ierr = lis_vector_get_value(_diag, k, &tmp);
130  checkLisError(ierr);
131  if (abs_max_entry < std::abs(tmp)) {
132  abs_max_entry = std::abs(tmp);
133  }
134  }
135 
136  return abs_max_entry;
137 }
138 
140 {
141  LIS_MATRIX &A = mat.getRawMatrix();
142 
143  if (!mat.isAssembled()) {
144  int ierr = lis_matrix_set_type(A, static_cast<int>(mat.getMatrixType()));
145  checkLisError(ierr);
146  ierr = lis_matrix_assemble(A);
147  checkLisError(ierr);
148  mat._is_assembled = true;
149  }
150  return true;
151 }
152 
153 
154 } //MathLib
MatrixType const _mat_type
Definition: LisMatrix.h:152
void setZero()
reset this matrix with keeping its original dimension
Definition: LisMatrix.cpp:76
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:112
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:139
int setValue(IndexType rowId, IndexType colId, double v)
set entry
Definition: LisMatrix.cpp:92
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:121
int add(IndexType rowId, IndexType colId, double v)
add value
Definition: LisMatrix.cpp:102
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:63
Definition of the LisMatrix class.
LIS_INT IndexType
Definition: LisMatrix.h:63
Definition of the LisVector class.