| 1 |
// SPDX-FileCopyrightText: Copyright © DUNE Project contributors, see file LICENSE.md in module root |
| 2 |
// SPDX-License-Identifier: LicenseRef-GPL-2.0-only-with-DUNE-exception |
| 3 |
// -*- tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- |
| 4 |
// vi: set et ts=4 sw=2 sts=2: |
| 5 |
#ifndef DUNE_ISTL_BCCSMATRIX_HH |
| 6 |
#define DUNE_ISTL_BCCSMATRIX_HH |
| 7 |
|
| 8 |
#include <dune/common/fmatrix.hh> |
| 9 |
#include <dune/common/fvector.hh> |
| 10 |
#include <dune/common/typetraits.hh> |
| 11 |
|
| 12 |
namespace Dune::ISTL::Impl |
| 13 |
{ |
| 14 |
/** |
| 15 |
* @brief A block matrix with compressed-column storage |
| 16 |
* |
| 17 |
* @tparam B The type of a matrix entry (may be a matrix itself) |
| 18 |
* @tparam I The type used for row and column indices. |
| 19 |
* When using BCCSMatrix to set up a SuiteSparse solver, this type |
| 20 |
* should be set to the index type used by the solver. That way, |
| 21 |
* the solver can use the row/column information without any copying. |
| 22 |
* |
| 23 |
* Currently this class is mainly a vehicle to get matrices into the solvers |
| 24 |
* of the SuiteSparse package. The class tries to follow the dune-istl |
| 25 |
* matrix interface, but it doesn't implement the interface entirely. |
| 26 |
*/ |
| 27 |
template<class B, class I = typename std::allocator<B>::size_type> |
| 28 |
class BCCSMatrix |
| 29 |
{ |
| 30 |
public: |
| 31 |
using Index = I; |
| 32 |
using size_type = std::size_t; |
| 33 |
|
| 34 |
/** \brief Default constructor |
| 35 |
*/ |
| 36 |
BCCSMatrix() |
| 37 |
: N_(0), M_(0), Nnz_(0), values(0), rowindex(0), colstart(0) |
| 38 |
{} |
| 39 |
|
| 40 |
/** @brief Destructor */ |
| 41 |
~BCCSMatrix() |
| 42 |
{ |
| 43 |
if(N_+M_+Nnz_!=0) |
| 44 |
free(); |
| 45 |
} |
| 46 |
|
| 47 |
/** \brief Set matrix size */ |
| 48 |
void setSize(size_type rows, size_type columns) |
| 49 |
{ |
| 50 |
N_ = rows; |
| 51 |
M_ = columns; |
| 52 |
} |
| 53 |
|
| 54 |
/** |
| 55 |
* @brief Get the number of rows. |
| 56 |
* @return The number of rows. |
| 57 |
*/ |
| 58 |
size_type N() const |
| 59 |
{ |
| 60 |
return N_; |
| 61 |
} |
| 62 |
|
| 63 |
/** \brief Return the number of nonzero block entries */ |
| 64 |
size_type nonzeroes() const |
| 65 |
{ |
| 66 |
return Nnz_; |
| 67 |
} |
| 68 |
|
| 69 |
/** |
| 70 |
* @brief Get the number of columns. |
| 71 |
* @return The number of columns. |
| 72 |
*/ |
| 73 |
size_type M() const |
| 74 |
{ |
| 75 |
return M_; |
| 76 |
} |
| 77 |
|
| 78 |
/** \brief Direct access to the array of matrix entries |
| 79 |
* |
| 80 |
* This is meant to be handed directly to SuiteSparse solvers. |
| 81 |
* Note that the 'Index' type is controllable via the second |
| 82 |
* class template argument. |
| 83 |
*/ |
| 84 |
B* getValues() const |
| 85 |
{ |
| 86 |
return values; |
| 87 |
} |
| 88 |
|
| 89 |
/** \brief Direct access to the array of row indices |
| 90 |
* |
| 91 |
* This is meant to be handed directly to SuiteSparse solvers. |
| 92 |
* Note that the 'Index' type is controllable via the second |
| 93 |
* class template argument. |
| 94 |
*/ |
| 95 |
Index* getRowIndex() const |
| 96 |
{ |
| 97 |
return rowindex; |
| 98 |
} |
| 99 |
|
| 100 |
/** \brief Direct access to the array of column entry points |
| 101 |
* |
| 102 |
* This is meant to be handed directly to SuiteSparse solvers. |
| 103 |
* Note that the 'Index' type is controllable via the second |
| 104 |
* class template argument. |
| 105 |
*/ |
| 106 |
Index* getColStart() const |
| 107 |
{ |
| 108 |
return colstart; |
| 109 |
} |
| 110 |
|
| 111 |
/** \brief Assignment operator */ |
| 112 |
BCCSMatrix& operator=(const BCCSMatrix& mat) |
| 113 |
{ |
| 114 |
if(N_+M_+Nnz_!=0) |
| 115 |
free(); |
| 116 |
N_=mat.N_; |
| 117 |
M_=mat.M_; |
| 118 |
Nnz_= mat.Nnz_; |
| 119 |
if(M_>0) { |
| 120 |
colstart=new size_type[M_+1]; |
| 121 |
for(size_type i=0; i<=M_; ++i) |
| 122 |
colstart[i]=mat.colstart[i]; |
| 123 |
} |
| 124 |
|
| 125 |
if(Nnz_>0) { |
| 126 |
values = new B[Nnz_]; |
| 127 |
rowindex = new size_type[Nnz_]; |
| 128 |
|
| 129 |
for(size_type i=0; i<Nnz_; ++i) |
| 130 |
values[i]=mat.values[i]; |
| 131 |
|
| 132 |
for(size_type i=0; i<Nnz_; ++i) |
| 133 |
rowindex[i]=mat.rowindex[i]; |
| 134 |
} |
| 135 |
return *this; |
| 136 |
} |
| 137 |
|
| 138 |
/** @brief free allocated space. */ |
| 139 |
virtual void free() |
| 140 |
{ |
| 141 |
delete[] values; |
| 142 |
delete[] rowindex; |
| 143 |
delete[] colstart; |
| 144 |
N_ = 0; |
| 145 |
M_ = 0; |
| 146 |
Nnz_ = 0; |
| 147 |
} |
| 148 |
|
| 149 |
public: |
| 150 |
size_type N_, M_, Nnz_; |
| 151 |
B* values; |
| 152 |
Index* rowindex; |
| 153 |
Index* colstart; |
| 154 |
}; |
| 155 |
|
| 156 |
} |
| 157 |
#endif |