NeoN
A framework for CFD software
Loading...
Searching...
No Matches
linearSystem.hpp
Go to the documentation of this file.
1// SPDX-License-Identifier: MIT
2// SPDX-FileCopyrightText: 2023 NeoN authors
3#pragma once
4
9
10#include <string>
11
12namespace NeoN::la
13{
14
22template<typename ValueType, typename IndexType>
34
35// TODO move to fvcc
36template<typename ValueType, typename IndexType>
44
53template<typename ValueType, typename IndexType>
55{
56public:
57
61 const Dictionary& aux = {}
62 )
63 : matrix_(matrix), rhs_(rhs), auxiliaryCoefficients_(aux)
64 {
65 NF_ASSERT(matrix.exec() == rhs.exec(), "Executors are not the same");
66 NF_ASSERT(matrix.nRows() == rhs.size(), "Matrix and RHS size mismatch");
67 };
68
70 : matrix_(ls.matrix_), rhs_(ls.rhs_), auxiliaryCoefficients_(ls.auxiliaryCoefficients_) {};
71
72 LinearSystem(const Executor exec) : matrix_(exec), rhs_(exec, 0), auxiliaryCoefficients_() {}
73
74 ~LinearSystem() = default;
75
76 [[nodiscard]] CSRMatrix<ValueType, IndexType>& matrix() { return matrix_; }
77
78 [[nodiscard]] Vector<ValueType>& rhs() { return rhs_; }
79
80 [[nodiscard]] const CSRMatrix<ValueType, IndexType>& matrix() const { return matrix_; }
81
82 [[nodiscard]] const Vector<ValueType>& rhs() const { return rhs_; }
83
84 [[nodiscard]] LinearSystem copyToHost() const
85 {
86 return LinearSystem(matrix_.copyToHost(), rhs_.copyToHost());
87 }
88
89 void reset()
90 {
91 fill(matrix_.values(), zero<ValueType>());
92 fill(rhs_, zero<ValueType>());
93 }
94
95 [[nodiscard]] LinearSystemView<ValueType, IndexType> view() && = delete;
96
97 [[nodiscard]] LinearSystemView<ValueType, IndexType> view() const&& = delete;
98
99 [[nodiscard]] LinearSystemView<ValueType, IndexType> view() &
100 {
101 return LinearSystemView<ValueType, IndexType>(matrix_.view(), rhs_.view());
102 }
103
105 {
106 return LinearSystemView<const ValueType, const IndexType>(matrix_.view(), rhs_.view());
107 }
108
109 const Executor& exec() const { return matrix_.exec(); }
110
111 // TODO move to fvcc
112 [[nodiscard]] const Dictionary& auxiliaryCoefficients() const { return auxiliaryCoefficients_; }
113
114 [[nodiscard]] Dictionary& auxiliaryCoefficients() { return auxiliaryCoefficients_; }
115
116private:
117
120 Dictionary auxiliaryCoefficients_;
121};
122
123
124template<typename ValueTypeIn, typename IndexTypeIn, typename ValueTypeOut, typename IndexTypeOut>
125LinearSystem<ValueTypeOut, IndexTypeOut>
127{
128 auto exec = ls.exec();
129 Vector<ValueTypeOut> convertedRhs(exec, ls.rhs().data(), ls.rhs().size());
130 return {
131 convert<ValueTypeIn, IndexTypeIn, ValueTypeOut, IndexTypeOut>(exec, ls.view.matrix),
132 convertedRhs,
133 ls.sparsityPattern()
134 };
135}
136
137/*@brief helper function that creates a zero initialised linear system based on given sparsity
138 * pattern
139 */
140template<typename ValueType, typename IndexType>
141LinearSystem<ValueType, IndexType>
143{
144 const auto& exec = mesh.exec();
145 localIdx rows {sparsity.rows()};
146 localIdx nnzs {sparsity.nnz()};
147
148 localIdx nBoundaryFaces {mesh.boundaryMesh().faceCells().size()};
149
150 const auto [diagOffset, rowOffs, faceCells] =
151 views(sparsity.diagOffset(), sparsity.rowOffs(), mesh.boundaryMesh().faceCells());
152
154 Vector<ValueType>(exec, nBoundaryFaces),
155 Vector<IndexType>(exec, nBoundaryFaces),
156 Vector<ValueType>(exec, nBoundaryFaces),
157 Vector<IndexType>(exec, nBoundaryFaces)
158 };
159
160 auto [mValue, mColIdx, rhsValue, rhsIdx] =
161 views(bcCoeffs.matrixValues, bcCoeffs.matrixIdxs, bcCoeffs.rhsValues, bcCoeffs.rhsIdxs);
162
164 exec,
165 {0, nBoundaryFaces},
166 KOKKOS_LAMBDA(const localIdx bfacei) {
167 localIdx celli = faceCells[bfacei];
168
169 mValue[bfacei] = zero<ValueType>();
170 mColIdx[bfacei] = celli + diagOffset[celli];
171 rhsValue[bfacei] = zero<ValueType>();
172 rhsIdx[bfacei] = celli;
173 }
174 );
175
176 Dictionary aux;
177 aux.insert("boundaryCoefficients", bcCoeffs);
178
179 return {
181 Vector<ValueType>(exec, nnzs, zero<ValueType>()), sparsity.colIdxs(), sparsity.rowOffs()
182 },
183 Vector<ValueType> {exec, rows, zero<ValueType>()},
184 aux
185 };
186}
187
188
189} // namespace NeoN::la
const labelVector & faceCells() const
Get the field of face cells.
A class representing a dictionary that stores key-value pairs.
void insert(const std::string &key, const std::any &value)
Inserts a key-value pair into the dictionary.
Represents an unstructured mesh in NeoN.
const Executor & exec() const
Get the executor.
const BoundaryMesh & boundaryMesh() const
Get the boundary mesh.
A class to contain the data and executors for a field and define some basic operations.
Definition vector.hpp:28
localIdx size() const
Gets the size of the field.
Definition vector.hpp:233
Sparse matrix class with compact storage by row (CSR) format.
Definition CSRMatrix.hpp:84
A class representing a linear system of equations.
const CSRMatrix< ValueType, IndexType > & matrix() const
LinearSystemView< ValueType, IndexType > view() const &&=delete
CSRMatrix< ValueType, IndexType > & matrix()
LinearSystem copyToHost() const
const Vector< ValueType > & rhs() const
LinearSystem(const CSRMatrix< ValueType, IndexType > &matrix, const Vector< ValueType > &rhs, const Dictionary &aux={})
LinearSystem(const LinearSystem &ls)
LinearSystem(const Executor exec)
LinearSystemView< const ValueType, const IndexType > view() const &
const Dictionary & auxiliaryCoefficients() const
const Executor & exec() const
Vector< ValueType > & rhs()
Dictionary & auxiliaryCoefficients()
LinearSystemView< ValueType, IndexType > view() &&=delete
const Vector< localIdx > & colIdxs() const
const Vector< localIdx > & rowOffs() const
const Array< uint8_t > & diagOffset() const
#define NF_ASSERT(condition, message)
Macro for asserting a condition and printing an error message if the condition is false.
Definition error.hpp:142
LinearSystem< ValueTypeOut, IndexTypeOut > convertLinearSystem(const LinearSystem< ValueTypeIn, IndexTypeIn > &ls)
LinearSystem< ValueType, IndexType > createEmptyLinearSystem(const UnstructuredMesh &mesh, const SparsityPattern &sparsity)
void parallelFor(const Executor &exec, std::pair< localIdx, localIdx > range, Kernel kernel, std::string name="parallelFor")
int32_t localIdx
Definition label.hpp:30
std::variant< SerialExecutor, CPUExecutor, GPUExecutor > Executor
Definition executor.hpp:16
void fill(ContType< ValueType > &cont, const std::type_identity_t< ValueType > value, std::pair< localIdx, localIdx > range={0, 0})
Fill the field with a vector value using a specific executor.
auto views(Types &... args)
Unpacks all views of the passed classes.
Definition view.hpp:108
Vector< ValueType > matrixValues
Vector< IndexType > matrixIdxs
Vector< ValueType > rhsValues
A view struct to allow easy read/write on all executors.
Definition CSRMatrix.hpp:21
A view linear into a linear system's data.
LinearSystemView(CSRMatrixView< ValueType, IndexType > matrixView, View< ValueType > rhsView)
CSRMatrixView< ValueType, IndexType > matrix