NeoN
A framework for CFD software
Loading...
Searching...
No Matches
linearUpwind.hpp
Go to the documentation of this file.
1// SPDX-FileCopyrightText: 2023 - 2026 NeoN authors
2//
3// SPDX-License-Identifier: MIT
4
5#pragma once
6
14
15#include <Kokkos_Core.hpp>
16
17#include <functional>
18#include <string>
19
21{
22
23namespace detail
24{
25
26/* @brief maps a face-interpolated field type to the type of its cell gradient:
27** a scalar field has a Vec3 gradient, a Vec3 field has a Tensor gradient.
28*/
29template<typename ValueType>
31
32template<>
34{
35 using type = Vec3;
36};
37
38template<>
40{
41 using type = Tensor;
42};
43
44} // namespace detail
45
46/* @brief computational kernel to perform a linearUpwind interpolation
47** from a source volume field to a surface field. The face value is the upwind
48** cell value plus a gradient-based correction:
49**
50** phi_f = phi_U + (Cf - C_U) & grad(phi)_U
51**
52** where U is the upwind cell (owner if faceFlux >= 0, neighbour otherwise),
53** Cf is the face centre and C_U the upwind cell centre. The gradient is computed
54** internally with a Gauss-Green scheme. This reduces to plain upwind where the
55** field is locally constant and is second-order accurate for smooth fields.
56**
57** @pre src.correctBoundaryConditions() must have been called by the caller so that
58** processor-boundary ghost values are up to date before the gradient is computed.
59** @param src the input field
60** @param flux the face flux determining the upwind direction
61** @param faceDeltaOwner Cf - C_owner per internal face (from the geometry scheme)
62** @param faceDeltaNeighbour Cf - C_neighbour per internal face (from the geometry scheme)
63** @param dst the target surface field
64** @param limiterWeights when given, the central-differencing weights for OpenFOAM's linearUpwindV
65** directional limiter (see applyLinearUpwind); null for plain linearUpwind
66*/
67template<typename ValueType>
69 const VolumeField<ValueType>& src,
70 const SurfaceField<scalar>& flux,
71 const SurfaceField<Vec3>& faceDeltaOwner,
72 const SurfaceField<Vec3>& faceDeltaNeighbour,
74 const bool cellLimitedGradient = false,
75 const SurfaceField<scalar>* limiterWeights = nullptr
76);
77
78/* @brief computes only the gradient correction part of the linearUpwind face value,
79** `(Cf - C_U) & grad(phi)_U`, into dst (without the upwind cell value). Boundary faces are set to
80** zero, matching OpenFOAM which corrects only coupled patches. Used for the implicit deferred
81** correction (the explicit RHS source added by the divergence operator).
82**
83** @pre src.correctBoundaryConditions() must have been called by the caller so that
84** processor-boundary ghost values are up to date before the gradient is computed.
85*/
86template<typename ValueType>
88 const VolumeField<ValueType>& src,
89 const SurfaceField<scalar>& flux,
90 const SurfaceField<Vec3>& faceDeltaOwner,
91 const SurfaceField<Vec3>& faceDeltaNeighbour,
93 const bool cellLimitedGradient = false,
94 const SurfaceField<scalar>* limiterWeights = nullptr
95);
96
97// @tparam CellLimited when true this is OpenFOAM's "linearUpwindV": the gradient correction of each
98// face is limited so it never points against, nor overshoots, the weighted jump between the two
99// cells (see applyLinearUpwind). The gradient itself is the one named in the spec, as for
100// linearUpwind. Only meaningful for Vec3 fields.
101template<typename ValueType, bool CellLimited = false>
103 public SurfaceInterpolationFactory<ValueType>::template Register<
104 LinearUpwind<ValueType, CellLimited>>
105{
108
109public:
110
111 LinearUpwind(const Executor& exec, const UnstructuredMesh& mesh, Input input)
112 : Base(exec, mesh), geometryScheme_(GeometryScheme::readOrCreate(mesh)),
113 gradSchemeName_(readGradSchemeName(input)),
114 cellLimitedGradient_(readCellLimited(gradSchemeName_))
115 {
116 // Opt in to the geometry scheme's per-internal-face cell-to-face offset vectors. These are
117 // computed lazily and only for schemes that need them, so non-linearUpwind runs never
118 // allocate them. Done in the constructor — while the mesh centres are still alive — because
119 // the geometry scheme frees those centres on the first read of any cached geometry field.
120 geometryScheme_->ensureFaceDeltas();
121 };
122
123 static std::string name() { return CellLimited ? "linearUpwindV" : "linearUpwind"; }
124
125 static std::string doc()
126 {
127 return CellLimited ? "linearUpwindV interpolation (directionally limited correction)"
128 : "linearUpwind interpolation";
129 }
130
131 static std::string schema() { return "none"; }
132
134 [[maybe_unused]] const VolumeField<ValueType>& src,
135 [[maybe_unused]] SurfaceField<ValueType>& dst
136 ) const override
137 {
138 NF_ERROR_EXIT("linearUpwind interpolation scheme requires a faceFlux");
139 }
140
142 const SurfaceField<scalar>& flux,
143 const VolumeField<ValueType>& src,
145 ) const override
146 {
148 src,
149 flux,
150 geometryScheme_->faceDeltaOwner(),
151 geometryScheme_->faceDeltaNeighbour(),
152 dst,
153 cellLimitedGradient_,
154 CellLimited ? &geometryScheme_->weights() : nullptr
155 );
156 }
157
158 // linearUpwind shares upwind's implicit weights; the gradient correction is an explicit
159 // (deferred) term applied through interpolate() above, mirroring OpenFOAM's corrected scheme.
161 {
162 NF_ERROR_EXIT("linearUpwind interpolation scheme requires a faceFlux");
163 }
164
165 void weight(
166 const SurfaceField<scalar>& faceFlux,
167 const VolumeField<ValueType>& src,
168 SurfaceField<scalar>& weights
169 ) const override
170 {
171 computeUpwindInterpolationWeights(faceFlux, src, weights);
172 }
173
174 // linearUpwind carries an explicit gradient correction beyond the upwind weights.
175 bool corrected() const override { return true; }
176
178 const SurfaceField<scalar>& faceFlux,
179 const VolumeField<ValueType>& src,
181 ) const override
182 {
184 src,
185 faceFlux,
186 geometryScheme_->faceDeltaOwner(),
187 geometryScheme_->faceDeltaNeighbour(),
188 corr,
189 cellLimitedGradient_,
190 CellLimited ? &geometryScheme_->weights() : nullptr
191 );
192 }
193
194 std::unique_ptr<SurfaceInterpolationFactory<ValueType>> clone() const override
195 {
196 return std::make_unique<LinearUpwind>(*this);
197 }
198
199private:
200
201 // The scheme spec is "linearUpwind <gradScheme>", e.g.
202 // "linearUpwind cellLimited Gauss linear 1". The deferred correction has to be built from
203 // the gradient named there: falling back to the unlimited Gauss-Green gradient leaves the
204 // reconstruction unclipped, a systematically more aggressive convection term. So the
205 // leading word decides whether the correction is cell-limited.
206 //
207 // A bare key ("linearUpwind limited", naming an entry in the caller's gradient-scheme
208 // table) cannot be resolved here -- NeoN holds no such table -- and falls back to the
209 // unlimited gradient. Callers that own the scheme dictionary should expand such a key to
210 // its definition before handing the spec over.
211 static std::string readGradSchemeName(Input& input)
212 {
213 if (std::holds_alternative<NeoN::TokenList>(input))
214 {
215 auto& tokens = std::get<NeoN::TokenList>(input);
216 if (tokens.peekIs<std::string>())
217 {
218 return tokens.next<std::string>();
219 }
220 }
221 return "Gauss";
222 }
223
224 // Honour the gradient named in the spec, for linearUpwindV as well: OpenFOAM's linearUpwindV
225 // bounds the correction with its directional limiter, not by limiting the gradient.
226 static bool readCellLimited(const std::string& gradSchemeName)
227 {
228 return gradSchemeName == "cellLimited";
229 }
230
231 const std::shared_ptr<GeometryScheme> geometryScheme_;
232 std::string gradSchemeName_;
233 bool cellLimitedGradient_;
234};
235
236} // namespace NeoN::finiteVolume::cellCentred
237
238namespace NeoN
239{
240
242
243template class fvcc::LinearUpwind<scalar>;
244template class fvcc::LinearUpwind<Vec3>;
245// linearUpwindV: directionally limited gradient correction, vector fields only.
246template class fvcc::LinearUpwind<Vec3, true>;
247
248}
A class for the representation of a 3x3 tensor.
Definition tensor.hpp:26
Represents an unstructured mesh in NeoN.
A class for the representation of a 3D Vec3.
Definition vec3.hpp:24
LinearUpwind(const Executor &exec, const UnstructuredMesh &mesh, Input input)
void correction(const SurfaceField< scalar > &faceFlux, const VolumeField< ValueType > &src, SurfaceField< ValueType > &corr) const override
void interpolate(const VolumeField< ValueType > &src, SurfaceField< ValueType > &dst) const override
void interpolate(const SurfaceField< scalar > &flux, const VolumeField< ValueType > &src, SurfaceField< ValueType > &dst) const override
void weight(const VolumeField< ValueType > &, SurfaceField< scalar > &) const override
std::unique_ptr< SurfaceInterpolationFactory< ValueType > > clone() const override
void weight(const SurfaceField< scalar > &faceFlux, const VolumeField< ValueType > &src, SurfaceField< scalar > &weights) const override
Represents a surface field in a finite volume method.
Represents a volume field in a finite volume method.
A template class for registering derived classes with a base class.
#define NF_ERROR_EXIT(message)
Macro for printing an error message and aborting the program.
Definition error.hpp:90
void computeUpwindInterpolationWeights(const SurfaceField< scalar > &flux, const VolumeField< ValueType > &src, SurfaceField< scalar > &weights)
void computeLinearUpwindCorrection(const VolumeField< ValueType > &src, const SurfaceField< scalar > &flux, const SurfaceField< Vec3 > &faceDeltaOwner, const SurfaceField< Vec3 > &faceDeltaNeighbour, SurfaceField< ValueType > &dst, const bool cellLimitedGradient=false, const SurfaceField< scalar > *limiterWeights=nullptr)
void computeLinearUpwindInterpolation(const VolumeField< ValueType > &src, const SurfaceField< scalar > &flux, const SurfaceField< Vec3 > &faceDeltaOwner, const SurfaceField< Vec3 > &faceDeltaNeighbour, SurfaceField< ValueType > &dst, const bool cellLimitedGradient=false, const SurfaceField< scalar > *limiterWeights=nullptr)
Integer types used throughout NeoN.
Definition array.hpp:18
std::variant< SerialExecutor, CPUExecutor, GPUExecutor > Executor
Definition executor.hpp:21
std::variant< Dictionary, TokenList > Input
Definition input.hpp:15
float scalar
Definition scalar.hpp:17