OGS
MatrixVectorTraits.cpp
Go to the documentation of this file.
1
11#include "MatrixVectorTraits.h"
12
13#include <cassert>
14
16
17#ifdef USE_PETSC
18
19namespace MathLib
20{
21std::unique_ptr<PETScMatrix> MatrixVectorTraits<PETScMatrix>::newInstance()
22{
23 return std::make_unique<PETScMatrix>();
24}
25
26std::unique_ptr<PETScMatrix> MatrixVectorTraits<PETScMatrix>::newInstance(
27 PETScMatrix const& A)
28{
29 return std::make_unique<PETScMatrix>(A);
30}
31
32std::unique_ptr<PETScMatrix> MatrixVectorTraits<PETScMatrix>::newInstance(
33 MatrixSpecifications const& spec)
34{
35 auto const nrows = spec.nrows;
36 auto const ncols = spec.ncols;
37
38 if (spec.sparsity_pattern)
39 {
40 // Assert that the misuse of the sparsity pattern is consistent.
41 assert(spec.sparsity_pattern->size() == 1);
42
43 auto const max_nonzeroes = spec.sparsity_pattern->front();
44
45 PETScMatrixOption mat_opt;
46 mat_opt.d_nz = max_nonzeroes;
47 mat_opt.o_nz = max_nonzeroes;
48 mat_opt.is_global_size = false;
49 return std::make_unique<PETScMatrix>(nrows, ncols, mat_opt);
50 }
51 else
52 return std::make_unique<PETScMatrix>(nrows, ncols);
53}
54
55std::unique_ptr<PETScVector> MatrixVectorTraits<PETScVector>::newInstance()
56{
57 return std::make_unique<PETScVector>();
58}
59
60std::unique_ptr<PETScVector> MatrixVectorTraits<PETScVector>::newInstance(
61 PETScVector const& x)
62{
63 return std::make_unique<PETScVector>(x);
64}
65
66std::unique_ptr<PETScVector> MatrixVectorTraits<PETScVector>::newInstance(
67 MatrixSpecifications const& spec)
68{
69 auto const is_global_size = false;
70
71 if (spec.ghost_indices != nullptr)
72 {
73 return std::make_unique<PETScVector>(spec.nrows, *spec.ghost_indices,
74 is_global_size);
75 }
76 else
77 {
78 return std::make_unique<PETScVector>(spec.nrows, is_global_size);
79 }
80}
81
82std::unique_ptr<PETScVector> MatrixVectorTraits<PETScVector>::newInstance(
83 PETScVector::IndexType const length)
84{
85 auto const is_global_size = false;
86
87 return std::make_unique<PETScVector>(length, is_global_size);
88}
89} // namespace MathLib
90
91#else
92
93namespace MathLib
94{
95std::unique_ptr<EigenMatrix> MatrixVectorTraits<EigenMatrix>::newInstance()
96{
97 return std::make_unique<EigenMatrix>(0, 0); // TODO default constructor
98}
99
100std::unique_ptr<EigenMatrix> MatrixVectorTraits<EigenMatrix>::newInstance(
101 EigenMatrix const& A)
102{
103 return std::make_unique<EigenMatrix>(A);
104}
105
106std::unique_ptr<EigenMatrix> MatrixVectorTraits<EigenMatrix>::newInstance(
107 MatrixSpecifications const& spec)
108{
109 auto A = std::make_unique<EigenMatrix>(spec.nrows);
110
111 if (spec.sparsity_pattern)
112 {
113 setMatrixSparsity(*A, *spec.sparsity_pattern);
114 }
115
116 return A;
117}
118
119std::unique_ptr<EigenVector> MatrixVectorTraits<EigenVector>::newInstance()
120{
121 return std::make_unique<EigenVector>();
122}
123
124std::unique_ptr<EigenVector> MatrixVectorTraits<EigenVector>::newInstance(
125 EigenVector const& x)
126{
127 return std::make_unique<EigenVector>(x);
128}
129
130std::unique_ptr<EigenVector> MatrixVectorTraits<EigenVector>::newInstance(
131 MatrixSpecifications const& spec)
132{
133 return std::make_unique<EigenVector>(spec.nrows);
134}
135
136std::unique_ptr<EigenVector> MatrixVectorTraits<EigenVector>::newInstance(
137 Eigen::SparseMatrix<double>::Index const length)
138{
139 return std::make_unique<EigenVector>(length);
140}
141} // namespace MathLib
142
143#endif
void setMatrixSparsity(MATRIX &matrix, SPARSITY_PATTERN const &sparsity_pattern)