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