NeoFOAM
WIP Prototype of a modern OpenFOAM core
Loading...
Searching...
No Matches
divOperator.hpp
Go to the documentation of this file.
1// SPDX-License-Identifier: MIT
2// SPDX-FileCopyrightText: 2023 NeoFOAM authors
3
4#pragma once
5
12
14{
15
16/* @class Factory class to create divergence operators by a given name using
17 * using NeoFOAMs runTimeFactory mechanism
18 */
21 DivOperatorFactory,
22 Parameters<const Executor&, const UnstructuredMesh&, const Input&>>
23{
24
25public:
26
27 static std::unique_ptr<DivOperatorFactory>
28 create(const Executor& exec, const UnstructuredMesh& uMesh, Input inputs)
29 {
30 std::string key = (std::holds_alternative<Dictionary>(inputs))
31 ? std::get<Dictionary>(inputs).get<std::string>("DivOperator")
32 : std::get<TokenList>(inputs).popFront<std::string>();
33 keyExistsOrError(key);
34 return table().at(key)(exec, uMesh, inputs);
35 }
36
37 static std::string name() { return "DivOperatorFactory"; }
38
40 : exec_(exec), mesh_(mesh) {};
41
42 virtual ~DivOperatorFactory() {} // Virtual destructor
43
44 virtual void
46 ) = 0;
47
48 virtual void
49 div(Field<scalar>& divPhi, const SurfaceField<scalar>& faceFlux, VolumeField<scalar>& phi) = 0;
50
52 div(const SurfaceField<scalar>& faceFlux, VolumeField<scalar>& phi) = 0;
53
54 // Pure virtual function for cloning
55 virtual std::unique_ptr<DivOperatorFactory> clone() const = 0;
56
57protected:
58
60
62};
63
64class DivOperator : public dsl::OperatorMixin<VolumeField<scalar>>
65{
66
67public:
68
69 // copy constructor
71 : dsl::OperatorMixin<VolumeField<scalar>>(divOp.exec_, divOp.field_, divOp.type_),
72 faceFlux_(divOp.faceFlux_),
73 divOperatorStrategy_(
74 divOp.divOperatorStrategy_ ? divOp.divOperatorStrategy_->clone() : nullptr
75 ) {};
76
78 dsl::Operator::Type termType,
79 const SurfaceField<scalar>& faceFlux,
81 Input input
82 )
83 : dsl::OperatorMixin<VolumeField<scalar>>(phi.exec(), phi, termType), faceFlux_(faceFlux),
84 divOperatorStrategy_(DivOperatorFactory::create(exec_, phi.mesh(), input)) {};
85
87 dsl::Operator::Type termType,
88 const SurfaceField<scalar>& faceFlux,
90 std::unique_ptr<DivOperatorFactory> divOperatorStrategy
91 )
92 : dsl::OperatorMixin<VolumeField<scalar>>(phi.exec(), phi, termType), faceFlux_(faceFlux),
93 divOperatorStrategy_(std::move(divOperatorStrategy)) {};
94
97 )
98 : dsl::OperatorMixin<VolumeField<scalar>>(phi.exec(), phi, termType), faceFlux_(faceFlux),
99 divOperatorStrategy_(nullptr) {};
100
101
103 {
104 if (divOperatorStrategy_ == nullptr)
105 {
106 NF_ERROR_EXIT("DivOperatorStrategy not initialized");
107 }
108 NeoFOAM::Field<NeoFOAM::scalar> tmpsource(source.exec(), source.size(), 0.0);
109 divOperatorStrategy_->div(tmpsource, faceFlux_, field_);
110 source += tmpsource;
111 }
112
113 void div(Field<scalar>& divPhi) { divOperatorStrategy_->div(divPhi, faceFlux_, getField()); }
114
116 {
117 divOperatorStrategy_->div(divPhi, faceFlux_, getField());
118 }
119
120
121 void build(const Input& input)
122 {
123 const UnstructuredMesh& mesh = field_.mesh();
124 if (std::holds_alternative<NeoFOAM::Dictionary>(input))
125 {
126 auto dict = std::get<NeoFOAM::Dictionary>(input);
127 std::string schemeName = "div(" + faceFlux_.name + "," + field_.name + ")";
128 auto tokens = dict.subDict("divSchemes").get<NeoFOAM::TokenList>(schemeName);
129 divOperatorStrategy_ = DivOperatorFactory::create(exec(), mesh, tokens);
130 }
131 else
132 {
133 auto tokens = std::get<NeoFOAM::TokenList>(input);
134 divOperatorStrategy_ = DivOperatorFactory::create(exec(), mesh, tokens);
135 }
136 }
137
138 std::string getName() const { return "DivOperator"; }
139
140private:
141
142 const SurfaceField<NeoFOAM::scalar>& faceFlux_;
143
144 std::unique_ptr<DivOperatorFactory> divOperatorStrategy_;
145};
146
147
148} // namespace NeoFOAM
A class to contain the data and executors for a field and define some basic operations.
Definition field.hpp:49
size_t size() const
Gets the size of the field.
Definition field.hpp:355
const Executor & exec() const
Gets the executor associated with the field.
Definition field.hpp:349
A factory class for runtime selection of derived classes.
A class representing a list of tokens.
Definition tokenList.hpp:22
Represents an unstructured mesh in NeoFOAM.
virtual const Executor & exec() const final
Definition operator.hpp:208
OperatorMixin(const Executor exec, VolumeField< scalar > &field, Operator::Type type)
Definition operator.hpp:201
const Executor exec_
Executor associated with the field. (CPU, GPU, openMP, etc.)
Definition operator.hpp:223
virtual void div(Field< scalar > &divPhi, const SurfaceField< scalar > &faceFlux, VolumeField< scalar > &phi)=0
virtual void div(VolumeField< scalar > &divPhi, const SurfaceField< scalar > &faceFlux, VolumeField< scalar > &phi)=0
static std::unique_ptr< DivOperatorFactory > create(const Executor &exec, const UnstructuredMesh &uMesh, Input inputs)
DivOperatorFactory(const Executor &exec, const UnstructuredMesh &mesh)
virtual std::unique_ptr< DivOperatorFactory > clone() const =0
virtual VolumeField< scalar > div(const SurfaceField< scalar > &faceFlux, VolumeField< scalar > &phi)=0
DivOperator(dsl::Operator::Type termType, const SurfaceField< scalar > &faceFlux, VolumeField< scalar > &phi)
DivOperator(dsl::Operator::Type termType, const SurfaceField< scalar > &faceFlux, VolumeField< scalar > &phi, std::unique_ptr< DivOperatorFactory > divOperatorStrategy)
void div(VolumeField< scalar > &divPhi)
DivOperator(dsl::Operator::Type termType, const SurfaceField< scalar > &faceFlux, VolumeField< scalar > &phi, Input input)
void explicitOperation(Field< scalar > &source)
const UnstructuredMesh & mesh() const
Returns a const reference to the unstructured mesh object.
Represents a surface field in a finite volume method.
Represents a volume field in a finite volume method.
#define NF_ERROR_EXIT(message)
Macro for printing an error message and aborting the program.
Definition error.hpp:108
float scalar
Definition scalar.hpp:11
std::variant< Dictionary, TokenList > Input
Definition input.hpp:13
std::variant< SerialExecutor, CPUExecutor, GPUExecutor > Executor
Definition executor.hpp:16