NeoN
A framework for CFD software
Loading...
Searching...
No Matches
linearSystem.hpp
Go to the documentation of this file.
1// SPDX-FileCopyrightText: 2023 - 2025 NeoN authors
2//
3// SPDX-License-Identifier: MIT
4
5#pragma once
6
11
12#include <string>
13
14namespace NeoN::la
15{
16
24template<typename ValueType, typename IndexType>
36
37// TODO move to fvcc
38template<typename ValueType, typename IndexType>
46
55template<typename ValueType, typename IndexType>
57{
58public:
59
63 const Dictionary& aux = {}
64 )
65 : matrix_(matrix), rhs_(rhs), auxiliaryCoefficients_(aux)
66 {
67 NF_ASSERT(matrix.exec() == rhs.exec(), "Executors are not the same");
68 NF_ASSERT(matrix.nRows() == rhs.size(), "Matrix and RHS size mismatch");
69 };
70
72 : matrix_(ls.matrix_), rhs_(ls.rhs_), auxiliaryCoefficients_(ls.auxiliaryCoefficients_) {};
73
74 LinearSystem(const Executor exec) : matrix_(exec), rhs_(exec, 0), auxiliaryCoefficients_() {}
75
76 ~LinearSystem() = default;
77
78 [[nodiscard]] CSRMatrix<ValueType, IndexType>& matrix() { return matrix_; }
79
80 [[nodiscard]] Vector<ValueType>& rhs() { return rhs_; }
81
82 [[nodiscard]] const CSRMatrix<ValueType, IndexType>& matrix() const { return matrix_; }
83
84 [[nodiscard]] const Vector<ValueType>& rhs() const { return rhs_; }
85
86 [[nodiscard]] LinearSystem copyToHost() const
87 {
88 return LinearSystem(matrix_.copyToHost(), rhs_.copyToHost());
89 }
90
91 void reset()
92 {
93 fill(matrix_.values(), zero<ValueType>());
94 fill(rhs_, zero<ValueType>());
95 }
96
97 [[nodiscard]] LinearSystemView<ValueType, IndexType> view() && = delete;
98
99 [[nodiscard]] LinearSystemView<ValueType, IndexType> view() const&& = delete;
100
101 [[nodiscard]] LinearSystemView<ValueType, IndexType> view() &
102 {
103 return LinearSystemView<ValueType, IndexType>(matrix_.view(), rhs_.view());
104 }
105
107 {
108 return LinearSystemView<const ValueType, const IndexType>(matrix_.view(), rhs_.view());
109 }
110
111 const Executor& exec() const { return matrix_.exec(); }
112
113 // TODO move to fvcc
114 [[nodiscard]] const Dictionary& auxiliaryCoefficients() const { return auxiliaryCoefficients_; }
115
116 [[nodiscard]] Dictionary& auxiliaryCoefficients() { return auxiliaryCoefficients_; }
117
118private:
119
122 Dictionary auxiliaryCoefficients_;
123};
124
125
126template<typename ValueTypeIn, typename IndexTypeIn, typename ValueTypeOut, typename IndexTypeOut>
127LinearSystem<ValueTypeOut, IndexTypeOut>
129{
130 auto exec = ls.exec();
131 Vector<ValueTypeOut> convertedRhs(exec, ls.rhs().data(), ls.rhs().size());
132 return {
133 convert<ValueTypeIn, IndexTypeIn, ValueTypeOut, IndexTypeOut>(exec, ls.view.matrix),
134 convertedRhs,
135 ls.sparsityPattern()
136 };
137}
138
139/*@brief helper function that creates a zero initialised linear system based on given sparsity
140 * pattern
141 */
142template<typename ValueType, typename IndexType>
143LinearSystem<ValueType, IndexType>
145{
146 const auto& exec = mesh.exec();
147 localIdx rows {sparsity.rows()};
148 localIdx nnzs {sparsity.nnz()};
149
150 localIdx nBoundaryFaces {mesh.boundaryMesh().faceCells().size()};
151
152 const auto [diagOffset, rowOffs, faceCells] =
153 views(sparsity.diagOffset(), sparsity.rowOffs(), mesh.boundaryMesh().faceCells());
154
156 Vector<ValueType>(exec, nBoundaryFaces),
157 Vector<IndexType>(exec, nBoundaryFaces),
158 Vector<ValueType>(exec, nBoundaryFaces),
159 Vector<IndexType>(exec, nBoundaryFaces)
160 };
161
162 auto [mValue, mColIdx, rhsValue, rhsIdx] =
163 views(bcCoeffs.matrixValues, bcCoeffs.matrixIdxs, bcCoeffs.rhsValues, bcCoeffs.rhsIdxs);
164
166 exec,
167 {0, nBoundaryFaces},
168 KOKKOS_LAMBDA(const localIdx bfacei) {
169 localIdx celli = faceCells[bfacei];
170
171 mValue[bfacei] = zero<ValueType>();
172 mColIdx[bfacei] = celli + diagOffset[celli];
173 rhsValue[bfacei] = zero<ValueType>();
174 rhsIdx[bfacei] = celli;
175 }
176 );
177
178 Dictionary aux;
179 aux.insert("boundaryCoefficients", bcCoeffs);
180
181 return {
183 Vector<ValueType>(exec, nnzs, zero<ValueType>()), sparsity.colIdxs(), sparsity.rowOffs()
184 },
185 Vector<ValueType> {exec, rows, zero<ValueType>()},
186 aux
187 };
188}
189
190
191} // 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:30
localIdx size() const
Gets the size of the field.
Definition vector.hpp:235
Sparse matrix class with compact storage by row (CSR) format.
Definition CSRMatrix.hpp:86
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:144
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:32
std::variant< SerialExecutor, CPUExecutor, GPUExecutor > Executor
Definition executor.hpp:18
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:110
Vector< ValueType > matrixValues
Vector< IndexType > matrixIdxs
Vector< ValueType > rhsValues
A view struct to allow easy read/write on all executors.
Definition CSRMatrix.hpp:23
A view linear into a linear system's data.
LinearSystemView(CSRMatrixView< ValueType, IndexType > matrixView, View< ValueType > rhsView)
CSRMatrixView< ValueType, IndexType > matrix