15#ifdef NF_WITH_MPI_SUPPORT
34template<
typename RHSValueType,
typename MatrixViewType>
41 MatrixViewType matrixView,
43 MatrixViewType boundaryMatrixView,
73 typename MatrixValueType,
74 typename RHSValueType = MatrixValueType,
79 LinearSystem<MatrixValueType, RHSValueType, SystemMatrixType, BoundaryMatrixType>>
84 NF_ASSERT(matrix_.exec() == rhs_.exec(),
"Executors are not the same");
85 NF_ASSERT(matrix_.nRows() == rhs_.size(),
"Matrix and RHS size mismatch");
87 meshIteratorContext_ !=
nullptr,
88 "Mesh iterator context must be set before validating the linear system"
91 meshIteratorContext_->get() !=
nullptr,
92 "Mesh iterator strategy must be set before validating the linear system"
106 const SystemMatrixType&
matrix,
111 std::shared_ptr<MeshIterationStrategy> strategy = std::make_shared<FaceBasedIterator>()
117 meshIteratorContext_->setStrategy(strategy);
122 const SystemMatrixType&
matrix,
126 std::shared_ptr<MeshIterationStrategy> strategy = std::make_shared<FaceBasedIterator>()
134 : matrix_(ls.matrix_), rhs_(ls.rhs_), boundaryMatrix_(ls.boundaryMatrix_),
135 offDiagonalMatrix_(ls.offDiagonalMatrix_), boundaryRhs_(ls.boundaryRhs_),
137 faceFluxCorrection_(ls.faceFluxCorrection_),
138 keepFaceFluxCorrection_(ls.keepFaceFluxCorrection_),
139 diagCmpt_(ls.diagCmpt_ ? std::make_shared<
Vector<RHSValueType>>(*ls.diagCmpt_) : nullptr),
140 meshIteratorContext_(ls.meshIteratorContext_)
141#ifdef NF_WITH_MPI_SUPPORT
143 commPattern_(ls.commPattern_)
151 [[nodiscard]] SystemMatrixType&
matrix() {
return matrix_; }
153 [[nodiscard]]
const SystemMatrixType&
matrix()
const {
return matrix_; }
157 [[nodiscard]]
const BoundaryMatrixType&
offDiagonalMatrix()
const {
return offDiagonalMatrix_; }
161 [[nodiscard]]
const BoundaryMatrixType&
boundaryMatrix()
const {
return boundaryMatrix_; }
180 return faceFluxCorrection_;
185 return faceFluxCorrection_;
198 [[nodiscard]]
const std::shared_ptr<Vector<RHSValueType>>&
diagCmpt()
const
211 std::make_shared<Vector<RHSValueType>>(
exec(), rhs_.size(), zero<RHSValueType>());
221 matrix_.copyToExecutor(
exec),
222 rhs_.copyToExecutor(
exec),
223 offDiagonalMatrix_.copyToExecutor(
exec),
224 boundaryMatrix_.copyToExecutor(
exec),
225 boundaryRhs_.copyToExecutor(
exec)
229 ls.diagCmpt_ = std::make_shared<Vector<RHSValueType>>(diagCmpt_->copyToExecutor(
exec));
231#ifdef NF_WITH_MPI_SUPPORT
232 ls.commPattern_ = commPattern_;
239 fill(matrix_.values(), zero<MatrixValueType>());
240 fill(rhs_, zero<RHSValueType>());
241 fill(boundaryMatrix_.values(), zero<MatrixValueType>());
242 fill(boundaryRhs_, zero<RHSValueType>());
243 fill(offDiagonalMatrix_.values(), zero<MatrixValueType>());
244 if (diagCmpt_)
fill(*diagCmpt_, zero<RHSValueType>());
266 return {matrix_.view(), rhs_.view(), boundaryMatrix_.view(), boundaryRhs_.view()};
271 return matrix_.faceToMatrixAddress();
274#ifdef NF_WITH_MPI_SUPPORT
275 [[nodiscard]]
const CommunicationPattern& commPattern()
const {
return commPattern_; }
276 [[nodiscard]] CommunicationPattern& commPattern() {
return commPattern_; }
279 [[nodiscard]] LinearSystemView<
281 const MatrixView<MatrixValueType, SparsityView<const LinearSystemIndexType>>>
284 return {matrix_.view(), rhs_.view(), boundaryMatrix_.view(), boundaryRhs_.view()};
289 if (meshIteratorContext_ ==
nullptr)
293 return meshIteratorContext_;
300 static BoundaryMatrixType emptyMatrix(
const Executor&
exec)
302 using IndexType =
typename BoundaryMatrixType::MatrixSparsityType::SparsityIndexType;
303 auto sp = std::make_shared<const typename BoundaryMatrixType::MatrixSparsityType>(
310 SystemMatrixType matrix_;
312 Vector<RHSValueType> rhs_;
315 BoundaryMatrixType boundaryMatrix_;
319 BoundaryMatrixType offDiagonalMatrix_;
321 Vector<RHSValueType> boundaryRhs_;
325 std::shared_ptr<Vector<RHSValueType>> faceFluxCorrection_ =
nullptr;
328 bool keepFaceFluxCorrection_ =
false;
330 Dictionary auxiliaryCoefficients_;
337 std::shared_ptr<Vector<RHSValueType>> diagCmpt_ =
nullptr;
339 std::shared_ptr<MeshIteratorContext> meshIteratorContext_ =
nullptr;
341#ifdef NF_WITH_MPI_SUPPORT
342 CommunicationPattern commPattern_;
350 typename RHSValueType = ValueType,
351 typename SystemMatrixType = CSRMatrix<ValueType, localIdx>,
352 typename BoundaryMatrixType = COOMatrix<ValueType, localIdx>>
355 std::shared_ptr<MeshIterationStrategy> strategy = std::make_shared<FaceBasedIterator>()
363 typename SystemMatrixType::MatrixSparsityType,
364 typename BoundaryMatrixType::MatrixSparsityType>(mesh);
365 const auto& sp = bundle.systemSparsity;
367 const auto& bSp = bundle.boundarySparsity;
368 const auto exec = sp->exec();
371 using IndexType =
typename BoundaryMatrixType::MatrixSparsityType::SparsityIndexType;
377#ifdef NF_WITH_MPI_SUPPORT
378 auto commPattern = computeCommunicationPattern(mesh);
383 const auto faceOwnersV = faceOwnersH.
view();
386 auto rowHV = rowH.
view();
387 auto colHV = colH.view();
388 for (
localIdx i = 0; i < nProcFaces; ++i)
394 rowHV[i] = faceOwnersV[nBoundaryFaces + i];
395 colHV[i] =
static_cast<IndexType
>(commPattern.recvIdx[
static_cast<std::size_t
>(i)]);
401 std::vector<IndexType> sortedRow(
static_cast<std::size_t
>(nProcFaces));
402 std::vector<IndexType> sortedCol(
static_cast<std::size_t
>(nProcFaces));
403 for (
localIdx j = 0; j < nProcFaces; ++j)
405 auto src = offDiagRowSortPerm[
static_cast<std::size_t
>(j)];
406 sortedRow[
static_cast<std::size_t
>(j)] = rowHV[src];
407 sortedCol[
static_cast<std::size_t
>(j)] = colHV[src];
412 commPattern.offDiagRowSortPerm = std::move(offDiagRowSortPerm);
416 auto offDiagSp = std::make_shared<const typename BoundaryMatrixType::MatrixSparsityType>(
417 std::move(offDiagColIdxs), std::move(offDiagRowIdxs),
Dimensions {nCells, nCells}
421 SystemMatrixType(
Vector<ValueType>(sp->exec(), sp->nnz(), zero<ValueType>()), sp, mi),
423 BoundaryMatrixType(
Vector<ValueType>(exec, nProcFaces, zero<ValueType>()), offDiagSp),
424 BoundaryMatrixType(
Vector<ValueType>(bSp->exec(), bSp->nnz(), zero<ValueType>()), bSp),
429#ifdef NF_WITH_MPI_SUPPORT
430 ls.commPattern() = std::move(commPattern);
443 typename MatrixValueType,
444 typename RHSValueType,
445 typename SystemMatrixType,
446 typename BoundaryMatrixType>
455 auto lsView = ls.view();
456 auto& matrix = lsView.matrix;
457 auto& rhs = lsView.rhs;
458 auto& bMatrix = lsView.boundaryMatrix;
459 auto& bRhs = lsView.boundaryRhs;
461 const auto ma = ls.faceToMatrixAddress()->
view(ls.matrix().sparsity()->rowOffs().view());
465 {0, bMatrix.values.size()},
467 const auto celli = bMatrix.sparsity.rowOffs[facei];
468 Kokkos::atomic_add(&matrix.values[ma.diagIdx(celli)], bMatrix.values[facei]);
469 Kokkos::atomic_add(&rhs[celli], bRhs[facei]);
471 "removeBoundaryContributions"
const labelVector & faceOwners() const
Get the list of labels of owner cells of boundary faces.
const std::vector< localIdx > & getRowSortPerm() const
Reference executor for serial CPU execution.
MixinClass signaling copyTo is supported.
Represents an unstructured mesh in NeoN.
localIdx nProcBoundaryFaces() const
Get the number of processor-boundary faces (inter-rank faces).
localIdx nCells() const
Get the number of cells in the mesh.
localIdx nBoundaryFaces() const
Get the number of boundary faces in the mesh.
const BoundaryMesh & boundaryMesh() const
Get the boundary mesh.
A class to contain the data and executors for a field and define some basic operations.
View< ValueType > view() &&=delete
Vector< ValueType > copyToHost() const
Returns a copy of the field back to the host.
A class representing a linear system of equations.
BoundaryMatrixType & offDiagonalMatrix()
LinearSystem(const SystemMatrixType &matrix, const Vector< RHSValueType > &rhs, const BoundaryMatrixType &boundaryMatrix, const Vector< RHSValueType > &boundaryRhs, std::shared_ptr< MeshIterationStrategy > strategy=std::make_shared< FaceBasedIterator >())
std::shared_ptr< MeshIteratorContext > getMeshIterator()
LinearSystem< MatrixValueType, RHSValueType, SystemMatrixType, BoundaryMatrixType > copyToExecutor(Executor exec) const override
void keepFaceFluxCorrection(bool keep)
Vector< RHSValueType > & rhs()
typename SystemMatrixType::MatrixSparsityType::SparsityIndexType LinearSystemIndexType
std::shared_ptr< Vector< RHSValueType > > & faceFluxCorrection()
LinearSystem(const LinearSystem &ls)
BoundaryMatrixType & boundaryMatrix()
Vector< RHSValueType > & boundaryRhs()
LinearSystem(const SystemMatrixType &matrix, const Vector< RHSValueType > &rhs, const BoundaryMatrixType &offDiagonalMatrix, const BoundaryMatrixType &boundaryMatrix, const Vector< RHSValueType > &boundaryRhs, std::shared_ptr< MeshIterationStrategy > strategy=std::make_shared< FaceBasedIterator >())
const std::shared_ptr< Vector< RHSValueType > > & faceFluxCorrection() const
const BoundaryMatrixType & boundaryMatrix() const
Vector< RHSValueType > & ensureDiagCmpt()
Lazily allocate (zero-initialised, one RHSValueType per cell) and return the per-component diagonal-c...
bool keepFaceFluxCorrection() const
LinearSystemView< RHSValueType, MatrixView< MatrixValueType, SparsityView< typename SystemMatrixType::MatrixSparsityType::SparsityIndexType > > > view() const &&=delete
const BoundaryMatrixType & offDiagonalMatrix() const
std::shared_ptr< const FaceToMatrixAddress > faceToMatrixAddress() const
LinearSystemView< RHSValueType, MatrixView< MatrixValueType, SparsityView< typename SystemMatrixType::MatrixSparsityType::SparsityIndexType > > > view() &&=delete
const Executor & exec() const
const SystemMatrixType & matrix() const
LinearSystemView< const RHSValueType, const MatrixView< MatrixValueType, SparsityView< const LinearSystemIndexType > > > view() const &
const std::shared_ptr< Vector< RHSValueType > > & diagCmpt() const
Per-component diagonal correction for implicit transform BCs (slip/symmetry). nullptr when no implici...
const Vector< RHSValueType > & boundaryRhs() const
const Vector< RHSValueType > & rhs() const
SystemMatrixType & matrix()
Sparse matrix class with compact storage by row (CSR) format.
Holds and exposes the active MeshIterationStrategy.
#define NF_ERROR_EXIT(message)
Macro for printing an error message and aborting the program.
#define NF_ASSERT(condition, message)
Macro for asserting a condition and printing an error message if the condition is false.
la::LinearSystem< MatrixValueType, RHSValueType, SystemMatrixType, BoundaryMatrixType > removeBoundaryContributions(const la::LinearSystem< MatrixValueType, RHSValueType, SystemMatrixType, BoundaryMatrixType > &lsIn)
for testing purposes, this function reverses boundary contributions previously applied to the matrix ...
LinearSystem< ValueType, RHSValueType, SystemMatrixType, BoundaryMatrixType > createEmptyLinearSystem(const UnstructuredMesh &mesh, std::shared_ptr< MeshIterationStrategy > strategy=std::make_shared< FaceBasedIterator >())
SharedSparsityBundle< SystemSparsityType, BoundarySparsityType > readOrCreateSparsityBundle(const UnstructuredMesh &mesh)
std::variant< SerialExecutor, CPUExecutor, GPUExecutor > Executor
void parallelFor(const ExecutorType &, std::pair< localIdx, localIdx > range, const Kernel &kernel, std::string name)
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.
hold the number of rows and columns of a matrix
A view linear into a linear system's data.
View< RHSValueType > boundaryRhs
LinearSystemView(MatrixViewType matrixView, View< RHSValueType > rhsView, MatrixViewType boundaryMatrixView, View< RHSValueType > boundaryRhsView)
MatrixViewType boundaryMatrix
~LinearSystemView()=default
LinearSystemView()=default
A view struct to allow easy read/write on all executors.
A view struct to allow easy read/write on all executors.