NeoN
WIP Prototype of a modern OpenFOAM core
Loading...
Searching...
No Matches
laplacianOperator.hpp
Go to the documentation of this file.
1// SPDX-License-Identifier: MIT
2// SPDX-FileCopyrightText: 2023 NeoN authors
3
4#pragma once
5
7#include "NeoN/core/input.hpp"
10#include "NeoN/dsl/operator.hpp"
15
17{
18
19/* @class Factory class to create divergence operators by a given name using
20 * using NeoNs runTimeFactory mechanism
21 */
22template<typename ValueType>
25 LaplacianOperatorFactory<ValueType>,
26 Parameters<const Executor&, const UnstructuredMesh&, const Input&>>
27{
28
29public:
30
31 static std::unique_ptr<LaplacianOperatorFactory<ValueType>>
32 create(const Executor& exec, const UnstructuredMesh& uMesh, const Input& inputs)
33 {
34 std::string key = (std::holds_alternative<Dictionary>(inputs))
35 ? std::get<Dictionary>(inputs).get<std::string>("LaplacianOperator")
36 : std::get<TokenList>(inputs).next<std::string>();
38 return LaplacianOperatorFactory<ValueType>::table().at(key)(exec, uMesh, inputs);
39 }
40
41 static std::string name() { return "LaplacianOperatorFactory"; }
42
44 : exec_(exec), mesh_(mesh) {};
45
46 virtual ~LaplacianOperatorFactory() {} // Virtual destructor
47
48 virtual void laplacian(
50 const SurfaceField<scalar>& gamma,
52 const dsl::Coeff operatorScaling
53 ) = 0;
54
56 const SurfaceField<scalar>& gamma,
58 const dsl::Coeff operatorScaling
59 ) const = 0;
60
61 virtual void laplacian(
62 Vector<ValueType>& lapPhi,
63 const SurfaceField<scalar>& gamma,
65 const dsl::Coeff operatorScaling
66 ) = 0;
67
68 virtual void laplacian(
70 const SurfaceField<scalar>& gamma,
72 const dsl::Coeff operatorScaling
73 ) = 0;
74
75 // Pure virtual function for cloning
76 virtual std::unique_ptr<LaplacianOperatorFactory<ValueType>> clone() const = 0;
77
78protected:
79
81
83};
84
85template<typename ValueType>
86class LaplacianOperator : public dsl::OperatorMixin<VolumeField<ValueType>>
87{
88
89public:
90
91 using VectorValueType = ValueType;
92
93 // copy constructor
95 : dsl::OperatorMixin<VolumeField<ValueType>>(
96 lapOp.exec_, lapOp.coeffs_, lapOp.field_, lapOp.type_
97 ),
98 gamma_(lapOp.gamma_),
99 laplacianOperatorStrategy_(
100 lapOp.laplacianOperatorStrategy_ ? lapOp.laplacianOperatorStrategy_->clone() : nullptr
101 ) {};
102
104 dsl::Operator::Type termType,
105 const SurfaceField<scalar>& gamma,
107 Input input
108 )
109 : dsl::OperatorMixin<VolumeField<ValueType>>(phi.exec(), dsl::Coeff(1.0), phi, termType),
110 gamma_(gamma),
111 laplacianOperatorStrategy_(
112 LaplacianOperatorFactory<ValueType>::create(this->exec_, phi.mesh(), input)
113 ) {};
114
116 dsl::Operator::Type termType,
117 const SurfaceField<scalar>& gamma,
119 std::unique_ptr<LaplacianOperatorFactory<ValueType>> laplacianOperatorStrategy
120 )
121 : dsl::OperatorMixin<VolumeField<ValueType>>(phi.exec(), dsl::Coeff(1.0), phi, termType),
122 gamma_(gamma), laplacianOperatorStrategy_(std::move(laplacianOperatorStrategy)) {};
123
126 )
127 : dsl::OperatorMixin<VolumeField<ValueType>>(phi.exec(), dsl::Coeff(1.0), phi, termType),
128 gamma_(gamma), laplacianOperatorStrategy_(nullptr) {};
129
130
132 {
133 NF_ASSERT(laplacianOperatorStrategy_, "LaplacianOperatorStrategy not initialized");
134 const auto operatorScaling = this->getCoefficient();
135 NeoN::Vector<ValueType> tmpsource(source.exec(), source.size(), zero<ValueType>());
136 laplacianOperatorStrategy_->laplacian(tmpsource, gamma_, this->field_, operatorScaling);
137 source += tmpsource;
138 }
139
141 {
142 NF_ASSERT(laplacianOperatorStrategy_, "LaplacianOperatorStrategy not initialized");
143 const auto operatorScaling = this->getCoefficient();
144 laplacianOperatorStrategy_->laplacian(ls, gamma_, this->field_, operatorScaling);
145 }
146
147 // void laplacian(Vector<scalar>& lapPhi)
148 // {
149 // laplacianOperatorStrategy_->laplacian(lapPhi, gamma_, getVector());
150 // }
151
152 // void laplacian(la::LinearSystem<scalar, localIdx>& ls)
153 // {
154 // laplacianOperatorStrategy_->laplacian(ls, gamma_, getVector());
155 // };
156
158 {
159 const auto operatorScaling = this->getCoefficient();
160 laplacianOperatorStrategy_->laplacian(lapPhi, gamma_, this->getVector(), operatorScaling);
161 }
162
164 {
165 const auto operatorScaling = this->getCoefficient();
166 std::string name = "laplacian(" + gamma_.name + "," + this->field_.name + ")";
167 VolumeField<scalar> lapPhi(
168 this->exec_,
169 name,
170 this->field_.mesh(),
171 createCalculatedBCs<VolumeBoundary<scalar>>(this->field_.mesh())
172 );
173 laplacianOperatorStrategy_->laplacian(lapPhi, gamma_, this->field_, operatorScaling);
174 return lapPhi;
175 }
176
177 void build(const Input& input)
178 {
179 const UnstructuredMesh& mesh = this->field_.mesh();
180 if (std::holds_alternative<NeoN::Dictionary>(input))
181 {
182 auto dict = std::get<NeoN::Dictionary>(input);
183 std::string schemeName = "laplacian(" + gamma_.name + "," + this->field_.name + ")";
184 auto tokens = dict.subDict("laplacianSchemes").get<NeoN::TokenList>(schemeName);
185 laplacianOperatorStrategy_ =
187 }
188 else
189 {
190 auto tokens = std::get<NeoN::TokenList>(input);
191 laplacianOperatorStrategy_ =
193 }
194 }
195
196 std::string getName() const { return "LaplacianOperator"; }
197
198private:
199
200 const SurfaceField<scalar>& gamma_;
201
202 std::unique_ptr<LaplacianOperatorFactory<ValueType>> laplacianOperatorStrategy_;
203};
204
205
206} // namespace NeoN
A factory class for runtime selection of derived classes.
A class representing a list of tokens.
Definition tokenList.hpp:27
Represents an unstructured mesh in NeoN.
A class to contain the data and executors for a field and define some basic operations.
Definition vector.hpp:53
A class that represents a coefficient for the NeoN dsl.
Definition coeff.hpp:22
const Executor exec_
Executor associated with the field. (CPU, GPU, openMP, etc.)
Definition operator.hpp:57
OperatorMixin(const Executor exec, const Coeff &coeffs, VolumeField< ValueType > &field, Operator::Type type)
Definition operator.hpp:34
virtual const Executor & exec() const final
Definition operator.hpp:42
virtual void laplacian(Vector< ValueType > &lapPhi, const SurfaceField< scalar > &gamma, VolumeField< ValueType > &phi, const dsl::Coeff operatorScaling)=0
virtual std::unique_ptr< LaplacianOperatorFactory< ValueType > > clone() const =0
virtual void laplacian(VolumeField< ValueType > &lapPhi, const SurfaceField< scalar > &gamma, VolumeField< ValueType > &phi, const dsl::Coeff operatorScaling)=0
static std::unique_ptr< LaplacianOperatorFactory< ValueType > > create(const Executor &exec, const UnstructuredMesh &uMesh, const Input &inputs)
virtual void laplacian(la::LinearSystem< ValueType, localIdx > &ls, const SurfaceField< scalar > &gamma, VolumeField< ValueType > &phi, const dsl::Coeff operatorScaling)=0
LaplacianOperatorFactory(const Executor &exec, const UnstructuredMesh &mesh)
virtual VolumeField< ValueType > laplacian(const SurfaceField< scalar > &gamma, VolumeField< ValueType > &phi, const dsl::Coeff operatorScaling) const =0
LaplacianOperator(dsl::Operator::Type termType, const SurfaceField< scalar > &gamma, VolumeField< ValueType > &phi, std::unique_ptr< LaplacianOperatorFactory< ValueType > > laplacianOperatorStrategy)
LaplacianOperator(dsl::Operator::Type termType, const SurfaceField< scalar > &gamma, VolumeField< ValueType > &phi)
void explicitOperation(Vector< ValueType > &source) const
void implicitOperation(la::LinearSystem< ValueType, localIdx > &ls)
LaplacianOperator(dsl::Operator::Type termType, const SurfaceField< scalar > &gamma, VolumeField< ValueType > &phi, Input input)
Represents a surface field in a finite volume method.
Represents a volume boundary field for a cell-centered finite volume method.
Represents a volume field in a finite volume method.
A class representing a linear system of equations.
#define NF_ASSERT(condition, message)
Macro for asserting a condition and printing an error message if the condition is false.
Definition error.hpp:142
std::vector< BoundaryType > createCalculatedBCs(const UnstructuredMesh &mesh)
Definition boundary.hpp:27
std::variant< Dictionary, TokenList > Input
Definition input.hpp:13
std::variant< SerialExecutor, CPUExecutor, GPUExecutor > Executor
Definition executor.hpp:16
const std::string & name(const NeoN::Document &doc)
Retrieves the name of a Document.