KASKADE 7 development version
Loading...
Searching...
No Matches
factorization.hh
Go to the documentation of this file.
1/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2/* */
3/* This file is part of the library KASKADE 7 */
4/* https://www.zib.de/research/projects/kaskade7-finite-element-toolbox */
5/* */
6/* Copyright (C) 2002-2024 Zuse Institute Berlin */
7/* */
8/* KASKADE 7 is distributed under the terms of the ZIB Academic License. */
9/* see $KASKADE/academic.txt */
10/* */
11/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
12
13#ifndef FACTORIZATION_HH
14#define FACTORIZATION_HH
15
16#include <sstream>
17#include <tuple>
18#include <variant>
19#include <vector>
20
21#include <dune/grid/config.h>
22
23#include "utilities/enums.hh"
24#include "dune/istl/bcrsmatrix.hh"
25
27#include "linalg/factory.hh"
28#include "linalg/triplet.hh"
29
30namespace Kaskade
31{
32
33//---------------------------------------------------------------------
34
41template <class Scalar>
43{
44public:
49
51
55 struct Info
56 {
60 struct Mumps
61 {
63 int zeroPivots = 0;
65 };
66
71
77 virtual void solve(std::vector<field_type>& b) const
78 {
79 assert(b.size() >= size());
80 solve(&b[0]);
81 }
82
88 virtual void solve(field_type* b) const = 0;
89
95 virtual void solve(std::vector<Scalar> const& b, std::vector<Scalar>& x, bool transposed=false) const
96 {
97 assert(b.size() >= size());
98 if (x.size() < size())
99 x.resize(size());
100 solve(&b[0], &x[0]);
101 }
102
108 virtual void solve(Scalar const* b, Scalar* x, bool transposed=false) const = 0;
109
111
112 Factorization() = default;
113 Factorization(Options options_) : options(options_) {}
114 virtual ~Factorization() {}
115 void setVerbose(int verbose_) { options.verbosity = verbose_; }
116 int getVerbose() const { return options.verbosity; }
117
118 Info const& info() const { return info_; }
119
123 virtual size_t size() const = 0;
124
125protected:
128};
129
130// -------------------------------------------------------------------------------------------------
131
139template <class Scalar, class Index>
140void tripletToCompressedColumn(Index nRows, Index nCols,
141 size_t nNonZeros,
142 std::vector<Index> const& ridx,
143 std::vector<Index> const& cidx,
144 std::vector<Scalar> const& values,
145 std::vector<Index>& Ap, std::vector<Index>& Ai,
146 std::vector<Scalar>& Az);
147
148// -------------------------------------------------------------------------------------------------
149
156template <class Scalar>
158{
159 // We can provide a matrix either with int or with (signed/unsigned) long row and column indices.
160 // Concrete factorization classes are responsible for conversion, if needed.
161 using Matrices = std::variant<MatrixAsTriplet<Scalar,int> const*,
164
170 using Argument = std::tuple<Matrices, FactorizationOptions>;
171
177 virtual std::unique_ptr<Factorization<Scalar>> create(Argument a) const = 0;
178
179 virtual ~Creator() {}
180
181 static std::string lookupFailureHint(DirectType direct)
182 {
183 std::ostringstream hint;
184 hint << "Solver key " << direct << " not registered.\n"
185 << "Has the corresponding object file been linked in?\n"
186 << "Did you specify the right index type (int/long/unsigned long) in your matrix?\n";
187 return hint.str();
188 }
189};
190
191
192//---------------------------------------------------------------------
193
198template <class Scalar, class Index>
199std::unique_ptr<Factorization<Scalar>> getFactorization(DirectType directType,
201 FactorizationOptions options)
202{
203 assert(A.nrows()==A.ncols());
204 if (A.nnz()==0)
205 throw SingularMatrixException("No nonzero entries.",__FILE__,__LINE__);
206
207 using Argument = typename Creator<Factorization<Scalar>>::Argument;
208 using Matrices = typename Creator<Factorization<Scalar>>::Matrices;
209
210 Argument args{Matrices(&A),options};
211
212 if (directType == DirectType::ANY)
213 return Factory<DirectType,Factorization<Scalar>>::createAny(args);
214 else
215 return Factory<DirectType,Factorization<Scalar>>::create(directType,args);
216}
217
222template <class Scalar>
223std::unique_ptr<Factorization<Scalar>>
226 FactorizationOptions options)
227{
228 using Index = typename Dune::BCRSMatrix<Dune::FieldMatrix<Scalar,1,1>>::size_type;
229 return getFactorization(directType,MatrixAsTriplet<Scalar,Index>(A),options);
230}
231
236template <class Scalar, int n, int m, class Index>
237std::unique_ptr<Factorization<Scalar>>
240 FactorizationOptions options)
241{
242 return getFactorization(directType,MatrixAsTriplet<Scalar,Index>(A),options);
243}
244
245} // namespace Kaskade
246
247#endif
Abstract base class for matrix factorizations.
Factorization(Options options_)
virtual void solve(std::vector< Scalar > const &b, std::vector< Scalar > &x, bool transposed=false) const
Solves the system for the given right hand side .
Scalar field_type
The type of matrix elements (a field type).
virtual void solve(std::vector< field_type > &b) const
Solves the system for the given right hand side .
virtual void solve(Scalar const *b, Scalar *x, bool transposed=false) const =0
Solves the system for the given right hand side .
FactorizationOptions Options
void setVerbose(int verbose_)
Info const & info() const
virtual void solve(field_type *b) const =0
Solves the system for the given right hand side .
virtual size_t size() const =0
reports the dimension of the system
A pluggable factory.
Definition factory.hh:61
size_t nnz() const
Definition triplet.hh:254
SparseIndexInt nrows() const
Returns number of rows (computes them, if not known)
Definition triplet.hh:202
SparseIndexInt ncols() const
Returns number of cols (computes them, if not known)
Definition triplet.hh:215
A NUMA-aware compressed row storage matrix adhering mostly to the Dune ISTL interface (to complete....
To be raised if the matrix is singular.
std::unique_ptr< Factorization< Scalar > > getFactorization(DirectType directType, MatrixAsTriplet< Scalar, Index > const &A, FactorizationOptions options)
Creates a factorization of the given triplet matrix.
DirectType
Available direct solvers for linear equation systems.
Definition enums.hh:35
void tripletToCompressedColumn(Index nRows, Index nCols, size_t nNonZeros, std::vector< Index > const &ridx, std::vector< Index > const &cidx, std::vector< Scalar > const &values, std::vector< Index > &Ap, std::vector< Index > &Ai, std::vector< Scalar > &Az)
Converts a matrix in triplet format to a compressed column format.
std::tuple< Matrices, FactorizationOptions > Argument
The argument type passed to factorization creators.
std::variant< MatrixAsTriplet< Scalar, int > const *, MatrixAsTriplet< Scalar, long > const *, MatrixAsTriplet< Scalar, unsigned long > const * > Matrices
virtual std::unique_ptr< Factorization< Scalar > > create(Argument a) const =0
Creates a factorization for the given pair (A,opt) of matrix A and options opt.
static std::string lookupFailureHint(DirectType direct)
Abstract base class of creators that can be registered at the pluggable factory.
Definition factory.hh:33
The Mumps struct provides output information specific to MUMPS.
int zeroPivots
number of zero eigenvalues (if inertia detection for symmetric matrix was enabled)
int negativePivots
number of negative eigenvalues (if inertia detection for symmetric matrix was enabled)
The Info struct provides output information of direct solvers.
struct Kaskade::Factorization::Info::Mumps mumps
The Options struct allows to specify several options of factorization.