OGS
MatrixVectorTraits.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
5
6#include <cassert>
7
9
10#ifdef USE_PETSC
11
12namespace MathLib
13{
14std::unique_ptr<PETScMatrix> MatrixVectorTraits<PETScMatrix>::newInstance()
15{
16 return std::make_unique<PETScMatrix>();
17}
18
19std::unique_ptr<PETScMatrix> MatrixVectorTraits<PETScMatrix>::newInstance(
20 PETScMatrix const& A)
21{
22 return std::make_unique<PETScMatrix>(A);
23}
24
25std::unique_ptr<PETScMatrix> MatrixVectorTraits<PETScMatrix>::newInstance(
26 MatrixSpecifications const& spec)
27{
28 auto const nrows = spec.nrows;
29 auto const ncols = spec.ncols;
30
31 if (spec.sparsity_pattern)
32 {
33 // Assert that the misuse of the sparsity pattern is consistent.
34 assert(spec.sparsity_pattern->size() == 1);
35
36 auto const max_nonzeroes = spec.sparsity_pattern->front();
37
38 PETScMatrixOption mat_opt;
39 mat_opt.d_nz = max_nonzeroes;
40 mat_opt.o_nz = max_nonzeroes;
41 mat_opt.is_global_size = false;
42 return std::make_unique<PETScMatrix>(nrows, ncols, mat_opt);
43 }
44 else
45 return std::make_unique<PETScMatrix>(nrows, ncols);
46}
47
48std::unique_ptr<PETScVector> MatrixVectorTraits<PETScVector>::newInstance()
49{
50 return std::make_unique<PETScVector>();
51}
52
53std::unique_ptr<PETScVector> MatrixVectorTraits<PETScVector>::newInstance(
54 PETScVector const& x)
55{
56 return std::make_unique<PETScVector>(x);
57}
58
59std::unique_ptr<PETScVector> MatrixVectorTraits<PETScVector>::newInstance(
60 MatrixSpecifications const& spec)
61{
62 auto const is_global_size = false;
63
64 if (spec.ghost_indices != nullptr)
65 {
66 return std::make_unique<PETScVector>(spec.nrows, *spec.ghost_indices,
67 is_global_size);
68 }
69 else
70 {
71 return std::make_unique<PETScVector>(spec.nrows, is_global_size);
72 }
73}
74
75std::unique_ptr<PETScVector> MatrixVectorTraits<PETScVector>::newInstance(
76 PETScVector::IndexType const length)
77{
78 auto const is_global_size = false;
79
80 return std::make_unique<PETScVector>(length, is_global_size);
81}
82} // namespace MathLib
83
84#else
85
86namespace MathLib
87{
88std::unique_ptr<EigenMatrix> MatrixVectorTraits<EigenMatrix>::newInstance()
89{
90 return std::make_unique<EigenMatrix>(0, 0); // TODO default constructor
91}
92
93std::unique_ptr<EigenMatrix> MatrixVectorTraits<EigenMatrix>::newInstance(
94 EigenMatrix const& A)
95{
96 return std::make_unique<EigenMatrix>(A);
97}
98
99std::unique_ptr<EigenMatrix> MatrixVectorTraits<EigenMatrix>::newInstance(
100 MatrixSpecifications const& spec)
101{
102 auto A = std::make_unique<EigenMatrix>(spec.nrows);
103
104 if (spec.sparsity_pattern)
105 {
106 setMatrixSparsity(*A, *spec.sparsity_pattern);
107 }
108
109 return A;
110}
111
112std::unique_ptr<EigenVector> MatrixVectorTraits<EigenVector>::newInstance()
113{
114 return std::make_unique<EigenVector>();
115}
116
117std::unique_ptr<EigenVector> MatrixVectorTraits<EigenVector>::newInstance(
118 EigenVector const& x)
119{
120 return std::make_unique<EigenVector>(x);
121}
122
123std::unique_ptr<EigenVector> MatrixVectorTraits<EigenVector>::newInstance(
124 MatrixSpecifications const& spec)
125{
126 return std::make_unique<EigenVector>(spec.nrows);
127}
128
129std::unique_ptr<EigenVector> MatrixVectorTraits<EigenVector>::newInstance(
130 Eigen::SparseMatrix<double>::Index const length)
131{
132 return std::make_unique<EigenVector>(length);
133}
134} // namespace MathLib
135
136#endif
Global vector based on Eigen vector.
Definition EigenVector.h:19
Wrapper class for PETSc matrix routines for matrix.
Definition PETScMatrix.h:21
Wrapper class for PETSc vector.
Definition PETScVector.h:28
void setMatrixSparsity(MATRIX &matrix, SPARSITY_PATTERN const &sparsity_pattern)
This a struct data containing the configuration information to create a PETSc type matrix.
PetscInt d_nz
Number of nonzeros per row in the diagonal portion of local submatrix (same value is used for all loc...