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,
52 const VolumeField<ValueType>& phi,
53 const dsl::Coeff operatorScaling
54 ) = 0;
55
57 const SurfaceField<scalar>& gamma,
58 const VolumeField<ValueType>& phi,
59 const dsl::Coeff operatorScaling
60 ) const = 0;
61
62 virtual void laplacian(
63 Vector<ValueType>& lapPhi,
64 const SurfaceField<scalar>& gamma,
65 const VolumeField<ValueType>& phi,
66 const dsl::Coeff operatorScaling
67 ) = 0;
68
69 virtual void laplacian(
71 const SurfaceField<scalar>& gamma,
72 const VolumeField<ValueType>& phi,
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
79 [[deprecated("This function will be removed")]] const la::SparsityPattern&
81 {
82 return sparsityPattern_;
83 }
84
85protected:
86
88
90
92};
93
94template<typename ValueType>
95class LaplacianOperator : public dsl::OperatorMixin<VolumeField<ValueType>>
96{
97
98public:
99
100 using VectorValueType = ValueType;
101
102 // copy constructor
104 : dsl::OperatorMixin<VolumeField<ValueType>>(
105 lapOp.exec_, lapOp.coeffs_, lapOp.field_, lapOp.type_
106 ),
107 gamma_(lapOp.gamma_),
108 laplacianOperatorStrategy_(
109 lapOp.laplacianOperatorStrategy_ ? lapOp.laplacianOperatorStrategy_->clone() : nullptr
110 ) {};
111
113 dsl::Operator::Type termType,
114 const SurfaceField<scalar>& gamma,
116 Input input
117 )
118 : dsl::OperatorMixin<VolumeField<ValueType>>(phi.exec(), dsl::Coeff(1.0), phi, termType),
119 gamma_(gamma),
120 laplacianOperatorStrategy_(
121 LaplacianOperatorFactory<ValueType>::create(this->exec_, phi.mesh(), input)
122 ) {};
123
125 dsl::Operator::Type termType,
126 const SurfaceField<scalar>& gamma,
128 std::unique_ptr<LaplacianOperatorFactory<ValueType>> laplacianOperatorStrategy
129 )
130 : dsl::OperatorMixin<VolumeField<ValueType>>(phi.exec(), dsl::Coeff(1.0), phi, termType),
131 gamma_(gamma), laplacianOperatorStrategy_(std::move(laplacianOperatorStrategy)) {};
132
135 )
136 : dsl::OperatorMixin<VolumeField<ValueType>>(phi.exec(), dsl::Coeff(1.0), phi, termType),
137 gamma_(gamma), laplacianOperatorStrategy_(nullptr) {};
138
139
141 {
142 NF_ASSERT(laplacianOperatorStrategy_, "LaplacianOperatorStrategy not initialized");
143 const auto operatorScaling = this->getCoefficient();
144 NeoN::Vector<ValueType> tmpsource(source.exec(), source.size(), zero<ValueType>());
145 laplacianOperatorStrategy_->laplacian(tmpsource, gamma_, this->field_, operatorScaling);
146 source += tmpsource;
147 }
148
150 {
151 NF_ASSERT(laplacianOperatorStrategy_, "LaplacianOperatorStrategy not initialized");
152 const auto operatorScaling = this->getCoefficient();
153 laplacianOperatorStrategy_->laplacian(ls, gamma_, this->field_, operatorScaling);
154 }
155
156 [[deprecated("use explicit or implicit operation")]] void laplacian(VolumeField<scalar>& lapPhi)
157 {
158 const auto operatorScaling = this->getCoefficient();
159 laplacianOperatorStrategy_->laplacian(lapPhi, gamma_, this->getVector(), operatorScaling);
160 }
161
162 [[deprecated("use explicit or implicit operation")]] VolumeField<scalar> laplacian()
163 {
164 const auto operatorScaling = this->getCoefficient();
165 std::string name = "laplacian(" + gamma_.name + "," + this->field_.name + ")";
166 VolumeField<scalar> lapPhi(
167 this->exec_,
168 name,
169 this->field_.mesh(),
170 createCalculatedBCs<VolumeBoundary<scalar>>(this->field_.mesh())
171 );
172 laplacianOperatorStrategy_->laplacian(lapPhi, gamma_, this->field_, operatorScaling);
173 return lapPhi;
174 }
175
176 void read(const Input& input)
177 {
178 const UnstructuredMesh& mesh = this->field_.mesh();
179 if (std::holds_alternative<NeoN::Dictionary>(input))
180 {
181 auto dict = std::get<NeoN::Dictionary>(input);
182 std::string schemeName = "laplacian(" + gamma_.name + "," + this->field_.name + ")";
183 auto tokens = dict.subDict("laplacianSchemes").get<NeoN::TokenList>(schemeName);
184 laplacianOperatorStrategy_ =
186 }
187 else
188 {
189 auto tokens = std::get<NeoN::TokenList>(input);
190 laplacianOperatorStrategy_ =
192 }
193 }
194
195 std::string getName() const { return "LaplacianOperator"; }
196
197private:
198
199 const SurfaceField<scalar>& gamma_;
200
201 std::unique_ptr<LaplacianOperatorFactory<ValueType>> laplacianOperatorStrategy_;
202};
203
204
205} // 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 VolumeField< ValueType > & field_
Definition operator.hpp:68
const Executor exec_
Executor associated with the field. (CPU, GPU, openMP, etc.)
Definition operator.hpp:64
OperatorMixin(const Executor exec, const Coeff &coeffs, const VolumeField< ValueType > &field, Operator::Type type)
Definition operator.hpp:41
const VolumeField< ValueType > & getVector() const
Definition operator.hpp:57
virtual const Executor & exec() const final
Definition operator.hpp:51
virtual VolumeField< ValueType > laplacian(const SurfaceField< scalar > &gamma, const VolumeField< ValueType > &phi, const dsl::Coeff operatorScaling) const =0
static std::unique_ptr< LaplacianOperatorFactory< ValueType > > create(const Executor &exec, const UnstructuredMesh &mesh, const Input &inputs)
virtual std::unique_ptr< LaplacianOperatorFactory< ValueType > > clone() const =0
virtual void laplacian(la::LinearSystem< ValueType, localIdx > &ls, const SurfaceField< scalar > &gamma, const VolumeField< ValueType > &phi, const dsl::Coeff operatorScaling)=0
virtual void laplacian(VolumeField< ValueType > &lapPhi, const SurfaceField< scalar > &gamma, const VolumeField< ValueType > &phi, const dsl::Coeff operatorScaling)=0
LaplacianOperatorFactory(const Executor &exec, const UnstructuredMesh &mesh)
virtual void laplacian(Vector< ValueType > &lapPhi, const SurfaceField< scalar > &gamma, const VolumeField< ValueType > &phi, const dsl::Coeff operatorScaling)=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.