NeoFOAM
WIP Prototype of a modern OpenFOAM core
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Concepts
sourceTerm.hpp
Go to the documentation of this file.
1// SPDX-License-Identifier: MIT
2// SPDX-FileCopyrightText: 2023 NeoFOAM authors
3
4#pragma once
5
13
15{
16
17
18template<typename ValueType>
19class SourceTerm : public dsl::OperatorMixin<VolumeField<ValueType>>
20{
21
22public:
23
24 using FieldValueType = ValueType;
25
27 dsl::Operator::Type termType,
28 VolumeField<scalar>& coefficients,
30 )
31 : dsl::OperatorMixin<VolumeField<ValueType>>(
32 field.exec(), dsl::Coeff(1.0), field, termType
33 ),
34 coefficients_(coefficients),
35 sparsityPattern_(SparsityPattern::readOrCreate(field.mesh())) {};
36
38 {
39 auto operatorScaling = this->getCoefficient();
40 const auto vol = coefficients_.mesh().cellVolumes().span();
41 auto [sourceSpan, fieldSpan, coeff] =
42 spans(source, this->field_.internalField(), coefficients_.internalField());
44 source.exec(),
45 source.range(),
46 KOKKOS_LAMBDA(const size_t celli) {
47 sourceSpan[celli] += operatorScaling[celli] * coeff[celli] * fieldSpan[celli];
48 }
49 );
50 }
51
53 {
54 la::LinearSystem<scalar, localIdx> ls(sparsityPattern_->linearSystem());
55 auto [A, b, sp] = ls.view();
56 const auto& exec = A.exec();
57
58 Field<ValueType> values(exec, A.nNonZeros(), zero<ValueType>());
59 Field<localIdx> mColIdxs(exec, A.colIdxs().data(), A.nNonZeros());
60 Field<localIdx> mRowPtrs(exec, A.rowPtrs().data(), A.rowPtrs().size());
61
62 la::CSRMatrix<ValueType, localIdx> matrix(values, mColIdxs, mRowPtrs);
63 Field<ValueType> rhs(exec, b.size(), zero<ValueType>());
64
65 return {matrix, rhs, ls.sparsityPattern()};
66 }
67
69 {
70 const auto operatorScaling = this->getCoefficient();
71 const auto vol = coefficients_.mesh().cellVolumes().span();
72 const auto [diagOffs, coeff] =
73 spans(sparsityPattern_->diagOffset(), coefficients_.internalField());
74 auto [values, cols, rows] = ls.matrix().span().span();
75
77 ls.exec(),
78 {0, coeff.size()},
79 KOKKOS_LAMBDA(const size_t celli) {
80 std::size_t idx = rows[celli] + diagOffs[celli];
81 values[idx] +=
82 operatorScaling[celli] * coeff[celli] * vol[celli] * one<ValueType>();
83 }
84 );
85 }
86
87
88 void build(const Input& input)
89 {
90 // do nothing
91 }
92
93 std::string getName() const { return "DivOperator"; }
94
95private:
96
97 const VolumeField<scalar>& coefficients_;
98 const std::shared_ptr<SparsityPattern> sparsityPattern_;
99};
100
101
102} // namespace NeoFOAM
A class to contain the data and executors for a field and define some basic operations.
Definition field.hpp:49
std::pair< size_t, size_t > range() const
Gets the range of the field.
Definition field.hpp:414
const Executor & exec() const
Gets the executor associated with the field.
Definition field.hpp:351
std::span< ValueType > span() &&=delete
const scalarField & cellVolumes() const
Get the field of cell volumes in the mesh.
OperatorMixin(const Executor exec, const Coeff &coeffs, VolumeField< ValueType > &field, Operator::Type type)
Definition operator.hpp:32
virtual const Executor & exec() const final
Definition operator.hpp:40
const UnstructuredMesh & mesh() const
Returns a const reference to the unstructured mesh object.
const Field< ValueType > & internalField() const
Returns a const reference to the internal field.
void implicitOperation(la::LinearSystem< ValueType, localIdx > &ls)
void explicitOperation(Field< ValueType > &source)
SourceTerm(dsl::Operator::Type termType, VolumeField< scalar > &coefficients, VolumeField< ValueType > &field)
la::LinearSystem< ValueType, localIdx > createEmptyLinearSystem() const
Represents a volume field in a finite volume method.
A class representing a linear system of equations.
const Executor & exec() const
LinearSystemView< ValueType, IndexType > view()
std::string sparsityPattern() const
CSRMatrix< ValueType, IndexType > & matrix()
std::variant< Dictionary, TokenList > Input
Definition input.hpp:13
void parallelFor(const Executor &exec, std::pair< size_t, size_t > range, Kernel kernel, std::string name="parallelFor")
auto spans(Args &... fields)