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
8#include "BaseLib/Error.h"
10
11#ifdef USE_PETSC
12
13namespace MathLib
14{
15std::unique_ptr<PETScMatrix> MatrixVectorTraits<PETScMatrix>::newInstance()
16{
17 return std::make_unique<PETScMatrix>();
18}
19
20std::unique_ptr<PETScMatrix> MatrixVectorTraits<PETScMatrix>::newInstance(
21 PETScMatrix const& A)
22{
23 return std::make_unique<PETScMatrix>(A);
24}
25
26std::unique_ptr<PETScMatrix> MatrixVectorTraits<PETScMatrix>::newInstance(
27 MatrixSpecifications const& spec)
28{
29 auto const nrows = spec.nrows;
30 auto const ncols = spec.ncols;
31
32 if (!spec.sparsity_pattern)
33 {
35 "PETSc matrix creation requires a sparsity pattern for memory "
36 "preallocation.");
37 }
38 return std::make_unique<PETScMatrix>(nrows, ncols, *spec.sparsity_pattern);
39}
40
41std::unique_ptr<PETScVector> MatrixVectorTraits<PETScVector>::newInstance()
42{
43 return std::make_unique<PETScVector>();
44}
45
46std::unique_ptr<PETScVector> MatrixVectorTraits<PETScVector>::newInstance(
47 PETScVector const& x)
48{
49 return std::make_unique<PETScVector>(x);
50}
51
52std::unique_ptr<PETScVector> MatrixVectorTraits<PETScVector>::newInstance(
53 MatrixSpecifications const& spec)
54{
55 auto const is_global_size = false;
56
57 if (spec.ghost_indices != nullptr)
58 {
59 return std::make_unique<PETScVector>(spec.nrows, *spec.ghost_indices,
60 is_global_size);
61 }
62 else
63 {
64 return std::make_unique<PETScVector>(spec.nrows, is_global_size);
65 }
66}
67
68std::unique_ptr<PETScVector> MatrixVectorTraits<PETScVector>::newInstance(
69 PETScVector::IndexType const length)
70{
71 auto const is_global_size = false;
72
73 return std::make_unique<PETScVector>(length, is_global_size);
74}
75} // namespace MathLib
76
77#else
78
79namespace MathLib
80{
81std::unique_ptr<EigenMatrix> MatrixVectorTraits<EigenMatrix>::newInstance()
82{
83 return std::make_unique<EigenMatrix>(0, 0); // TODO default constructor
84}
85
86std::unique_ptr<EigenMatrix> MatrixVectorTraits<EigenMatrix>::newInstance(
87 EigenMatrix const& A)
88{
89 return std::make_unique<EigenMatrix>(A);
90}
91
92std::unique_ptr<EigenMatrix> MatrixVectorTraits<EigenMatrix>::newInstance(
93 MatrixSpecifications const& spec)
94{
95 auto A = std::make_unique<EigenMatrix>(spec.nrows);
96
97 if (spec.sparsity_pattern)
98 {
99 setMatrixSparsity(*A, *spec.sparsity_pattern);
100 }
101
102 return A;
103}
104
105std::unique_ptr<EigenVector> MatrixVectorTraits<EigenVector>::newInstance()
106{
107 return std::make_unique<EigenVector>();
108}
109
110std::unique_ptr<EigenVector> MatrixVectorTraits<EigenVector>::newInstance(
111 EigenVector const& x)
112{
113 return std::make_unique<EigenVector>(x);
114}
115
116std::unique_ptr<EigenVector> MatrixVectorTraits<EigenVector>::newInstance(
117 MatrixSpecifications const& spec)
118{
119 return std::make_unique<EigenVector>(spec.nrows);
120}
121
122std::unique_ptr<EigenVector> MatrixVectorTraits<EigenVector>::newInstance(
123 Eigen::SparseMatrix<double>::Index const length)
124{
125 return std::make_unique<EigenVector>(length);
126}
127} // namespace MathLib
128
129#endif
#define OGS_FATAL(...)
Definition Error.h:10
Global vector based on Eigen vector.
Definition EigenVector.h:19
Wrapper class for PETSc matrix routines for matrix.
Definition PETScMatrix.h:23
Wrapper class for PETSc vector.
Definition PETScVector.h:28
void setMatrixSparsity(MATRIX &matrix, SPARSITY_PATTERN const &sparsity_pattern)