NeoFOAM
WIP Prototype of a modern OpenFOAM core
Loading...
Searching...
No Matches
ddtOperator.hpp
Go to the documentation of this file.
1// SPDX-License-Identifier: MIT
2// SPDX-FileCopyrightText: 2023 NeoFOAM authors
3
4#pragma once
5
14
16{
17
18
19template<typename ValueType>
20class DdtOperator : public dsl::OperatorMixin<VolumeField<ValueType>>
21{
22
23public:
24
25 using FieldValueType = ValueType;
26
28 : dsl::OperatorMixin<VolumeField<ValueType>>(
29 field.exec(), dsl::Coeff(1.0), field, termType
30 ),
31 sparsityPattern_(SparsityPattern::readOrCreate(field.mesh())) {};
32
34 {
35 const scalar dtInver = 1.0 / dt;
36 const auto vol = this->getField().mesh().cellVolumes().span();
37 auto operatorScaling = this->getCoefficient();
38 auto [sourceSpan, field, oldField] =
39 spans(source, this->field_.internalField(), oldTime(this->field_).internalField());
40
42 source.exec(),
43 source.range(),
44 KOKKOS_LAMBDA(const size_t celli) {
45 sourceSpan[celli] += dtInver * (field[celli] - oldField[celli]) * vol[celli];
46 }
47 );
48 }
49
51 {
52 const scalar dtInver = 1.0 / dt;
53 const auto vol = this->getField().mesh().cellVolumes().span();
54 const auto operatorScaling = this->getCoefficient();
55 const auto [diagOffs, oldField] =
56 spans(sparsityPattern_->diagOffset(), oldTime(this->field_).internalField());
57 auto [values, cols, rows] = ls.matrix().span().span();
58 auto rhs = ls.rhs().span();
59
61 ls.exec(),
62 {0, oldField.size()},
63 KOKKOS_LAMBDA(const size_t celli) {
64 std::size_t idx = rows[celli] + diagOffs[celli];
65 const auto commonCoef = operatorScaling[celli] * vol[celli] * dtInver;
66 values[idx] += commonCoef * one<ValueType>();
67 rhs[celli] += commonCoef * oldField[celli];
68 }
69 );
70 }
71
73 {
74 la::LinearSystem<scalar, localIdx> ls(sparsityPattern_->linearSystem());
75 auto [A, b, sp] = ls.view();
76 const auto& exec = A.exec();
77
78 Field<ValueType> values(exec, A.nNonZeros(), zero<ValueType>());
79 Field<localIdx> mColIdxs(exec, A.colIdxs().data(), A.nNonZeros());
80 Field<localIdx> mRowPtrs(exec, A.rowPtrs().data(), A.rowPtrs().size());
81
82 la::CSRMatrix<ValueType, localIdx> matrix(values, mColIdxs, mRowPtrs);
83 Field<ValueType> rhs(exec, b.size(), zero<ValueType>());
84
85 return {matrix, rhs, ls.sparsityPattern()};
86 }
87
88
89 void build(const Input& input)
90 {
91 // do nothing
92 }
93
94 std::string getName() const { return "DdtOperator"; }
95
96private:
97
98 // const VolumeField<ValueType> coefficients_;
99 const std::shared_ptr<SparsityPattern> sparsityPattern_;
100};
101
102
103} // 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
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
la::LinearSystem< ValueType, localIdx > createEmptyLinearSystem() const
void implicitOperation(la::LinearSystem< ValueType, localIdx > &ls, scalar t, scalar dt)
DdtOperator(dsl::Operator::Type termType, VolumeField< ValueType > &field)
void explicitOperation(Field< ValueType > &source, scalar t, scalar dt)
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()
Field< ValueType > & rhs()
std::string sparsityPattern() const
CSRMatrix< ValueType, IndexType > & matrix()
FieldType & oldTime(FieldType &field)
Retrieves the old time field of a given field.
float scalar
Definition scalar.hpp:14
std::variant< Dictionary, TokenList > Input
Definition input.hpp:13
void parallelFor(const Executor &exec, std::pair< size_t, size_t > range, Kernel kernel)
auto spans(Args &... fields)