NeoN
A framework for CFD software
Loading...
Searching...
No Matches
laplacianOperator.hpp
Go to the documentation of this file.
1// SPDX-FileCopyrightText: 2023 - 2025 NeoN authors
2//
3// SPDX-License-Identifier: MIT
4
5#pragma once
6
8#include "NeoN/core/input.hpp"
11#include "NeoN/dsl/operator.hpp"
16
18{
19
20/* @class Factory class to create divergence operators by a given name using
21 * using NeoNs runTimeFactory mechanism
22 */
23template<typename ValueType>
26 LaplacianOperatorFactory<ValueType>,
27 Parameters<const Executor&, const UnstructuredMesh&, const Input&>>
28{
29
30public:
31
32 static std::unique_ptr<LaplacianOperatorFactory<ValueType>>
33 create(const Executor& exec, const UnstructuredMesh& mesh, const Input& inputs)
34 {
35 std::string key = (std::holds_alternative<Dictionary>(inputs))
36 ? std::get<Dictionary>(inputs).get<std::string>("LaplacianOperator")
37 : std::get<TokenList>(inputs).next<std::string>();
39 return LaplacianOperatorFactory<ValueType>::table().at(key)(exec, mesh, inputs);
40 }
41
42 static std::string name() { return "LaplacianOperatorFactory"; }
43
45 : exec_(exec), mesh_(mesh), sparsityPattern_(la::SparsityPattern::readOrCreate(mesh)) {};
46
47 virtual ~LaplacianOperatorFactory() {} // Virtual destructor
48
49 virtual void laplacian(
51 const SurfaceField<scalar>& gamma,
53 const dsl::Coeff operatorScaling
54 ) = 0;
55
57 const SurfaceField<scalar>& gamma,
59 const dsl::Coeff operatorScaling
60 ) const = 0;
61
62 virtual void laplacian(
63 Vector<ValueType>& lapPhi,
64 const SurfaceField<scalar>& gamma,
66 const dsl::Coeff operatorScaling
67 ) = 0;
68
69 virtual void laplacian(
71 const SurfaceField<scalar>& gamma,
73 const dsl::Coeff operatorScaling
74 ) = 0;
75
76 // Pure virtual function for cloning
77 virtual std::unique_ptr<LaplacianOperatorFactory<ValueType>> clone() const = 0;
78
80
81protected:
82
84
86
88};
89
90template<typename ValueType>
91class LaplacianOperator : public dsl::OperatorMixin<VolumeField<ValueType>>
92{
93
94public:
95
96 using VectorValueType = ValueType;
97
98 // copy constructor
100 : dsl::OperatorMixin<VolumeField<ValueType>>(
101 lapOp.exec_, lapOp.coeffs_, lapOp.field_, lapOp.type_
102 ),
103 gamma_(lapOp.gamma_),
104 laplacianOperatorStrategy_(
105 lapOp.laplacianOperatorStrategy_ ? lapOp.laplacianOperatorStrategy_->clone() : nullptr
106 ) {};
107
109 dsl::Operator::Type termType,
110 const SurfaceField<scalar>& gamma,
112 Input input
113 )
114 : dsl::OperatorMixin<VolumeField<ValueType>>(phi.exec(), dsl::Coeff(1.0), phi, termType),
115 gamma_(gamma),
116 laplacianOperatorStrategy_(
117 LaplacianOperatorFactory<ValueType>::create(this->exec_, phi.mesh(), input)
118 ) {};
119
121 dsl::Operator::Type termType,
122 const SurfaceField<scalar>& gamma,
124 std::unique_ptr<LaplacianOperatorFactory<ValueType>> laplacianOperatorStrategy
125 )
126 : dsl::OperatorMixin<VolumeField<ValueType>>(phi.exec(), dsl::Coeff(1.0), phi, termType),
127 gamma_(gamma), laplacianOperatorStrategy_(std::move(laplacianOperatorStrategy)) {};
128
131 )
132 : dsl::OperatorMixin<VolumeField<ValueType>>(phi.exec(), dsl::Coeff(1.0), phi, termType),
133 gamma_(gamma), laplacianOperatorStrategy_(nullptr) {};
134
135
137 {
138 NF_ASSERT(laplacianOperatorStrategy_, "LaplacianOperatorStrategy not initialized");
139 const auto operatorScaling = this->getCoefficient();
140 NeoN::Vector<ValueType> tmpsource(source.exec(), source.size(), zero<ValueType>());
141 laplacianOperatorStrategy_->laplacian(tmpsource, gamma_, this->field_, operatorScaling);
142 source += tmpsource;
143 }
144
146 {
147 NF_ASSERT(laplacianOperatorStrategy_, "LaplacianOperatorStrategy not initialized");
148 const auto operatorScaling = this->getCoefficient();
149 laplacianOperatorStrategy_->laplacian(ls, gamma_, this->field_, operatorScaling);
150 }
151
152 // void laplacian(Vector<scalar>& lapPhi)
153 // {
154 // laplacianOperatorStrategy_->laplacian(lapPhi, gamma_, getVector());
155 // }
156
157 // void laplacian(la::LinearSystem<scalar, localIdx>& ls)
158 // {
159 // laplacianOperatorStrategy_->laplacian(ls, gamma_, getVector());
160 // };
161
163 {
164 const auto operatorScaling = this->getCoefficient();
165 laplacianOperatorStrategy_->laplacian(lapPhi, gamma_, this->getVector(), operatorScaling);
166 }
167
169 {
170 const auto operatorScaling = this->getCoefficient();
171 std::string name = "laplacian(" + gamma_.name + "," + this->field_.name + ")";
172 VolumeField<scalar> lapPhi(
173 this->exec_,
174 name,
175 this->field_.mesh(),
176 createCalculatedBCs<VolumeBoundary<scalar>>(this->field_.mesh())
177 );
178 laplacianOperatorStrategy_->laplacian(lapPhi, gamma_, this->field_, operatorScaling);
179 return lapPhi;
180 }
181
182 void read(const Input& input)
183 {
184 const UnstructuredMesh& mesh = this->field_.mesh();
185 if (std::holds_alternative<NeoN::Dictionary>(input))
186 {
187 auto dict = std::get<NeoN::Dictionary>(input);
188 std::string schemeName = "laplacian(" + gamma_.name + "," + this->field_.name + ")";
189 auto tokens = dict.subDict("laplacianSchemes").get<NeoN::TokenList>(schemeName);
190 laplacianOperatorStrategy_ =
192 }
193 else
194 {
195 auto tokens = std::get<NeoN::TokenList>(input);
196 laplacianOperatorStrategy_ =
198 }
199 }
200
201 std::string getName() const { return "LaplacianOperator"; }
202
203private:
204
205 const SurfaceField<scalar>& gamma_;
206
207 std::unique_ptr<LaplacianOperatorFactory<ValueType>> laplacianOperatorStrategy_;
208};
209
210
211} // namespace NeoN
A factory class for runtime selection of derived classes.
A class representing a list of tokens.
Definition tokenList.hpp:29
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:30
A class that represents a coefficient for the NeoN dsl.
Definition coeff.hpp:24
const Executor exec_
Executor associated with the field. (CPU, GPU, openMP, etc.)
Definition operator.hpp:59
OperatorMixin(const Executor exec, const Coeff &coeffs, VolumeField< ValueType > &field, Operator::Type type)
Definition operator.hpp:37
virtual const Executor & exec() const final
Definition operator.hpp:44
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:144
std::vector< BoundaryType > createCalculatedBCs(const UnstructuredMesh &mesh)
Definition boundary.hpp:28
std::variant< Dictionary, TokenList > Input
Definition input.hpp:15
std::variant< SerialExecutor, CPUExecutor, GPUExecutor > Executor
Definition executor.hpp:18
const std::string & name(const NeoN::Document &doc)
Retrieves the name of a Document.