NeoN
A framework for CFD software
Loading...
Searching...
No Matches
surfaceField.hpp
Go to the documentation of this file.
1// SPDX-FileCopyrightText: 2024 - 2026 NeoN authors
2//
3// SPDX-License-Identifier: MIT
4
5#pragma once
6
7#include <vector>
14
16{
17
28template<typename ValueType>
29class SurfaceField : public DomainMixin<ValueType>, public FieldDatabaseMixin
30{
31
32public:
33
34 using VectorValueType = ValueType;
35
45 const Executor& exec,
46 std::string fieldName,
49 )
50 : DomainMixin<ValueType>(
51 exec,
52 fieldName,
53 mesh,
54 Field<ValueType>(exec, mesh.nInternalFaces(), mesh.boundaryMesh().offset())
55 ),
56 FieldDatabaseMixin(), boundaryConditions_(boundaryConditions)
57 {}
58
59 /* @brief Constructor for a surfaceVector with a given internal field
60 *
61 * @param exec The executor
62 * @param mesh The underlying mesh
63 * @param internalVector the underlying internal field
64 * @param boundaryConditions a vector of boundary conditions
65 */
67 const Executor& exec,
69 const Field<ValueType>& domainVector,
71 )
72 : DomainMixin<ValueType>(exec, mesh, domainVector), FieldDatabaseMixin(),
73 boundaryConditions_(boundaryConditions)
74 {}
75
76 /* @brief Constructor for a surfaceVector with a given internal field
77 *
78 * @param exec The executor
79 * @param mesh The underlying mesh
80 * @param internalVector the underlying internal field
81 * @param boundaryVectors the underlying boundary data fields
82 * @param boundaryConditions a vector of boundary conditions
83 */
85 const Executor& exec,
88 const BoundaryData<ValueType>& boundaryVectors,
90 )
91 : DomainMixin<ValueType>(exec, mesh, {exec, mesh, internalVector, boundaryVectors}),
92 FieldDatabaseMixin(), boundaryConditions_(boundaryConditions)
93 {}
94
95 /* @brief Constructor for a surface field with a given internal field and database registration.
96 *
97 * Mirrors the VolumeField db-registering constructor.
98 *
99 * @param exec The executor
100 * @param fieldName The name of the field
101 * @param mesh The underlying mesh
102 * @param domainVector the underlying domain field (internal + boundary laid out as Field)
103 * @param boundaryConditions a vector of boundary conditions
104 * @param db The database to register with
105 * @param dbKey The key of the field in the database
106 * @param collectionName The name of the field collection in the database
107 */
109 const Executor& exec,
110 std::string fieldName,
111 const UnstructuredMesh& mesh,
112 Field<ValueType> domainVector,
114 Database& db,
115 std::string dbKey,
116 std::string collectionName
117 )
118 : DomainMixin<ValueType>(exec, fieldName, mesh, std::move(domainVector)),
119 FieldDatabaseMixin(db, std::move(dbKey), std::move(collectionName)),
120 boundaryConditions_(boundaryConditions)
121 {}
122
129 : DomainMixin<ValueType>(other), FieldDatabaseMixin(other),
130 boundaryConditions_(other.boundaryConditions_)
131 {}
132
140 : DomainMixin<ValueType>(std::move(other)), FieldDatabaseMixin(std::move(other)),
141 boundaryConditions_(std::move(other.boundaryConditions_))
142 {}
143
151 {
152 // One-time set() pass: initialise constant patch data once per field instance (e.g. the
153 // processor BC's unused mixed-BC coefficients).
154 if (!boundaryConditionsSet_)
155 {
156 for (auto& boundaryCondition : boundaryConditions_)
157 {
158 boundaryCondition.set(this->field_);
159 }
160 boundaryConditionsSet_ = true;
161 }
162 // Per-iteration update(): proc patches post their (already face-valued) data without
163 // draining, so all are in flight before any completes.
164 for (auto& boundaryCondition : boundaryConditions_)
165 {
166 boundaryCondition.update(this->field_);
167 }
168 // Drain any processor-halo exchange once, after all proc patches have posted. See
169 // VolumeField::correctBoundaryConditions for the rationale (batched post-then-wait).
170 this->field_.boundaryData().waitAll();
171 }
172
173 std::vector<SurfaceBoundary<ValueType>> boundaryConditions() const
174 {
175 return boundaryConditions_;
176 }
177
178private:
179
180 std::vector<SurfaceBoundary<ValueType>>
181 boundaryConditions_; // The vector of boundary conditions
182 std::optional<Database*> db_; // The optional pointer to the database
183
184 // Whether the one-time boundary set() pass has run for this field instance. Default-init to
185 // false so copies re-run set() on their first correctBoundaryConditions() call.
186 bool boundaryConditionsSet_ {false};
187};
188
189inline SurfaceField<scalar>
191{
192 SurfaceField<scalar> result(lhs);
193 result.internalVector() *= rhs.internalVector();
194 result.boundaryData().value() *= rhs.boundaryData().value();
195 return result;
196}
197
198inline SurfaceField<scalar>
200{
201 SurfaceField<scalar> result(lhs);
202 result.internalVector() += rhs.internalVector();
203 result.boundaryData().value() += rhs.boundaryData().value();
204 return result;
205}
206
207inline SurfaceField<scalar>
209{
210 SurfaceField<scalar> result(lhs);
211 result.internalVector() -= rhs.internalVector();
212 result.boundaryData().value() -= rhs.boundaryData().value();
213 return result;
214}
215
217{
218 SurfaceField<scalar> result(fld);
219 result.internalVector() *= s;
220 result.boundaryData().value() *= s;
221 return result;
222}
223
225{
226 auto [outV, coeffV] = views(out, coeff);
228 out.exec(),
229 {0, out.size()},
230 NEON_LAMBDA(const localIdx i) { outV[i] = outV[i] * coeffV[i]; },
231 "surfaceField::scaleVec3ByScalar"
232 );
233}
234
236{
237 SurfaceField<Vec3> result(v);
239 surfaceFieldScaleVec3ByScalar(result.boundaryData().value(), s.boundaryData().value());
240 return result;
241}
242
244{
245 return s * v;
246}
247
249{
250 SurfaceField<Vec3> result(lhs);
251 result.internalVector() += rhs.internalVector();
252 result.boundaryData().value() += rhs.boundaryData().value();
253 return result;
254}
255
257{
258 SurfaceField<Vec3> result(lhs);
259 result.internalVector() -= rhs.internalVector();
260 result.boundaryData().value() -= rhs.boundaryData().value();
261 return result;
262}
263
264} // namespace NeoN
Represents the boundary fields for a computational domain.
Represents the domain fields for a computational domain.
Definition field.hpp:34
Represents an unstructured mesh in NeoN.
An executor-aware contiguous container for numerical data with vector-style arithmetic semantics.
Definition vector.hpp:40
const Executor & exec() const
Returns the executor associated with the Vector.
Definition vector.hpp:325
DomainMixin(const Executor &exec, std::string fieldName, const UnstructuredMesh &mesh, Field< ValueType > field)
Constructor for DomainMixin.
Definition domain.hpp:44
const BoundaryData< ValueType > & boundaryData() const
Returns a const reference to the boundary field.
Definition domain.hpp:103
const Vector< scalar > & internalVector() const
Definition domain.hpp:82
Represents a surface boundary field for a cell-centered finite volume method.
Represents a surface field in a finite volume method.
std::vector< SurfaceBoundary< scalar > > boundaryConditions() const
SurfaceField(const Executor &exec, std::string fieldName, const UnstructuredMesh &mesh, Field< ValueType > domainVector, const std::vector< SurfaceBoundary< ValueType > > &boundaryConditions, Database &db, std::string dbKey, std::string collectionName)
SurfaceField(SurfaceField &&other)
Move constructor. Without it, moving a SurfaceField would copy its data, since the user-declared copy...
SurfaceField(const Executor &exec, std::string fieldName, const UnstructuredMesh &mesh, const std::vector< SurfaceBoundary< ValueType > > &boundaryConditions)
Constructor for a surfaceVector with a given name and mesh.
SurfaceField(const SurfaceField &other)
Copy constructor for a surface field.
SurfaceField(const Executor &exec, const UnstructuredMesh &mesh, const Field< ValueType > &domainVector, const std::vector< SurfaceBoundary< ValueType > > &boundaryConditions)
void correctBoundaryConditions()
Corrects the boundary conditions of the surface field.
SurfaceField(const Executor &exec, const UnstructuredMesh &mesh, const Vector< ValueType > &internalVector, const BoundaryData< ValueType > &boundaryVectors, const std::vector< SurfaceBoundary< ValueType > > &boundaryConditions)
SurfaceField< scalar > operator+(const SurfaceField< scalar > &lhs, const SurfaceField< scalar > &rhs)
SurfaceField< scalar > operator-(const SurfaceField< scalar > &lhs, const SurfaceField< scalar > &rhs)
void surfaceFieldScaleVec3ByScalar(Vector< Vec3 > &out, const Vector< scalar > &coeff)
SurfaceField< scalar > operator*(const SurfaceField< scalar > &lhs, const SurfaceField< scalar > &rhs)
std::variant< SerialExecutor, CPUExecutor, GPUExecutor > Executor
Definition executor.hpp:21
float scalar
Definition scalar.hpp:17
void parallelFor(const ExecutorType &, std::pair< localIdx, localIdx > range, const Kernel &kernel, std::string name)
int32_t localIdx
Definition label.hpp:50
auto views(Types &... args)
Unpacks all views of the passed classes.
Definition view.hpp:108
#define NEON_LAMBDA