NeoN
A framework for CFD software
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& mesh, 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, mesh, inputs);
39 }
40
41 static std::string name() { return "LaplacianOperatorFactory"; }
42
44 : exec_(exec), mesh_(mesh), sparsityPattern_(la::SparsityPattern::readOrCreate(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
79
80protected:
81
83
85
87};
88
89template<typename ValueType>
90class LaplacianOperator : public dsl::OperatorMixin<VolumeField<ValueType>>
91{
92
93public:
94
95 using VectorValueType = ValueType;
96
97 // copy constructor
99 : dsl::OperatorMixin<VolumeField<ValueType>>(
100 lapOp.exec_, lapOp.coeffs_, lapOp.field_, lapOp.type_
101 ),
102 gamma_(lapOp.gamma_),
103 laplacianOperatorStrategy_(
104 lapOp.laplacianOperatorStrategy_ ? lapOp.laplacianOperatorStrategy_->clone() : nullptr
105 ) {};
106
108 dsl::Operator::Type termType,
109 const SurfaceField<scalar>& gamma,
111 Input input
112 )
113 : dsl::OperatorMixin<VolumeField<ValueType>>(phi.exec(), dsl::Coeff(1.0), phi, termType),
114 gamma_(gamma),
115 laplacianOperatorStrategy_(
116 LaplacianOperatorFactory<ValueType>::create(this->exec_, phi.mesh(), input)
117 ) {};
118
120 dsl::Operator::Type termType,
121 const SurfaceField<scalar>& gamma,
123 std::unique_ptr<LaplacianOperatorFactory<ValueType>> laplacianOperatorStrategy
124 )
125 : dsl::OperatorMixin<VolumeField<ValueType>>(phi.exec(), dsl::Coeff(1.0), phi, termType),
126 gamma_(gamma), laplacianOperatorStrategy_(std::move(laplacianOperatorStrategy)) {};
127
130 )
131 : dsl::OperatorMixin<VolumeField<ValueType>>(phi.exec(), dsl::Coeff(1.0), phi, termType),
132 gamma_(gamma), laplacianOperatorStrategy_(nullptr) {};
133
134
136 {
137 NF_ASSERT(laplacianOperatorStrategy_, "LaplacianOperatorStrategy not initialized");
138 const auto operatorScaling = this->getCoefficient();
139 NeoN::Vector<ValueType> tmpsource(source.exec(), source.size(), zero<ValueType>());
140 laplacianOperatorStrategy_->laplacian(tmpsource, gamma_, this->field_, operatorScaling);
141 source += tmpsource;
142 }
143
145 {
146 NF_ASSERT(laplacianOperatorStrategy_, "LaplacianOperatorStrategy not initialized");
147 const auto operatorScaling = this->getCoefficient();
148 laplacianOperatorStrategy_->laplacian(ls, gamma_, this->field_, operatorScaling);
149 }
150
151 // void laplacian(Vector<scalar>& lapPhi)
152 // {
153 // laplacianOperatorStrategy_->laplacian(lapPhi, gamma_, getVector());
154 // }
155
156 // void laplacian(la::LinearSystem<scalar, localIdx>& ls)
157 // {
158 // laplacianOperatorStrategy_->laplacian(ls, gamma_, getVector());
159 // };
160
162 {
163 const auto operatorScaling = this->getCoefficient();
164 laplacianOperatorStrategy_->laplacian(lapPhi, gamma_, this->getVector(), operatorScaling);
165 }
166
168 {
169 const auto operatorScaling = this->getCoefficient();
170 std::string name = "laplacian(" + gamma_.name + "," + this->field_.name + ")";
171 VolumeField<scalar> lapPhi(
172 this->exec_,
173 name,
174 this->field_.mesh(),
175 createCalculatedBCs<VolumeBoundary<scalar>>(this->field_.mesh())
176 );
177 laplacianOperatorStrategy_->laplacian(lapPhi, gamma_, this->field_, operatorScaling);
178 return lapPhi;
179 }
180
181 void read(const Input& input)
182 {
183 const UnstructuredMesh& mesh = this->field_.mesh();
184 if (std::holds_alternative<NeoN::Dictionary>(input))
185 {
186 auto dict = std::get<NeoN::Dictionary>(input);
187 std::string schemeName = "laplacian(" + gamma_.name + "," + this->field_.name + ")";
188 auto tokens = dict.subDict("laplacianSchemes").get<NeoN::TokenList>(schemeName);
189 laplacianOperatorStrategy_ =
191 }
192 else
193 {
194 auto tokens = std::get<NeoN::TokenList>(input);
195 laplacianOperatorStrategy_ =
197 }
198 }
199
200 std::string getName() const { return "LaplacianOperator"; }
201
202private:
203
204 const SurfaceField<scalar>& gamma_;
205
206 std::unique_ptr<LaplacianOperatorFactory<ValueType>> laplacianOperatorStrategy_;
207};
208
209
210} // 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:28
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:35
virtual const Executor & exec() const final
Definition operator.hpp:42
static std::unique_ptr< LaplacianOperatorFactory< ValueType > > create(const Executor &exec, const UnstructuredMesh &mesh, const Input &inputs)
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
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)
void implicitOperation(la::LinearSystem< ValueType, localIdx > &ls) const
LaplacianOperator(dsl::Operator::Type termType, const SurfaceField< scalar > &gamma, VolumeField< ValueType > &phi)
void explicitOperation(Vector< ValueType > &source) const
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.