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 return 0;
85 lis_matrix_set_value(LIS_INS_VALUE, rowId, colId, v, AA_);
86 if (rowId == colId)
87 lis_vector_set_value(LIS_INS_VALUE, rowId, v, diag_);
88 is_assembled_ = false;
89 return 0;
90}
91
92int LisMatrix::add(IndexType rowId, IndexType colId, double v)
93{
94 if (v == 0.0)
95 return 0;
96 lis_matrix_set_value(LIS_ADD_VALUE, rowId, colId, v, AA_);
97 if (rowId == colId)
98 lis_vector_set_value(LIS_ADD_VALUE, rowId, v, diag_);
99 is_assembled_ = false;
100 return 0;
101}
102
103void LisMatrix::write(const std::string& filename) const
104{
105 if (!is_assembled_)
106 {
107 OGS_FATAL("LisMatrix::write(): matrix not assembled.");
108 }
109 lis_output_matrix(AA_, LIS_FMT_MM, const_cast<char*>(filename.c_str()));
110}
111
113{
114 LIS_MATRIX& A = mat.getRawMatrix();
115
116 if (!mat.isAssembled())
117 {
118 int ierr =
119 lis_matrix_set_type(A, static_cast<int>(mat.getMatrixType()));
120 checkLisError(ierr);
121 ierr = lis_matrix_assemble(A);
122 checkLisError(ierr);
123 mat.is_assembled_ = true;
124 }
125 return true;
126}
127
128} // 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:92
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 &)