NeoN
A framework for CFD software
Loading...
Searching...
No Matches
trackingMesh.hpp
Go to the documentation of this file.
1// SPDX-FileCopyrightText: 2026 NeoN authors
2//
3// SPDX-License-Identifier: MIT
4
5#pragma once
6
12
13namespace NeoN
14{
15
18{
20 cyclic = 1,
21 escape = 2
22};
23
50
64{
65public:
66
82 Vector<label> faceBasePt,
83 Vector<label> faceType,
84 Vector<label> cyclicPartnerCell,
85 Vector<Vec3> cyclicShift,
86 Vector<Vec3> points
87 )
88 : mesh_(mesh), cellFaces_(std::move(cellFaces)), facePoints_(std::move(facePoints)),
89 faceBasePt_(std::move(faceBasePt)), faceType_(std::move(faceType)),
90 cyclicPartnerCell_(std::move(cyclicPartnerCell)), cyclicShift_(std::move(cyclicShift)),
91 points_(std::move(points))
92 {}
93
95 [[nodiscard]] TrackingMeshView view() const
96 {
97 return {
98 {cellFaces_.values().view(), cellFaces_.segments().view()},
99 {facePoints_.values().view(), facePoints_.segments().view()},
100 faceBasePt_.view(),
101 faceType_.view(),
102 cyclicPartnerCell_.view(),
103 cyclicShift_.view(),
104 points_.view(),
105 mesh_.cellCenters().view(),
106 mesh_.cellVolumes().view(),
107 mesh_.faceCenters().view(),
108 mesh_.faceNormals().view(),
109 mesh_.faceOwners().view(),
110 mesh_.faceNeighbors().view(),
111 static_cast<localIdx>(mesh_.nInternalFaces()),
112 static_cast<localIdx>(mesh_.nCells())
113 };
114 }
115
116 [[nodiscard]] const UnstructuredMesh& mesh() const { return mesh_; }
117
118private:
119
120 const UnstructuredMesh& mesh_;
123 Vector<label> faceBasePt_;
124 Vector<label> faceType_;
125 Vector<label> cyclicPartnerCell_;
126 Vector<Vec3> cyclicShift_;
127 Vector<Vec3> points_;
128};
129
133KOKKOS_INLINE_FUNCTION
134bool pointInCell(const Vec3& x, const label c, const TrackingMeshView& m)
135{
136 const auto [start, end] = m.cellFaces.bounds(c);
137 for (localIdx k = start; k < end; k++)
138 {
139 const label f = m.cellFaces.values[k];
140 Vec3 normal = m.faceAreaVectors[f];
141 if (m.faceOwners[f] != c)
142 {
143 normal = scalar(-1) * normal;
144 }
145 if ((normal & (x - m.faceCenters[f])) > scalar(0))
146 {
147 return false;
148 }
149 }
150 return true;
151}
152
165KOKKOS_INLINE_FUNCTION
167{
168 label c = hint;
169 if (pointInCell(x, c, m))
170 {
171 return c;
172 }
173
174 Vec3 d = x - m.cellCenters[c];
175 scalar nearestDistSqr = d & d;
176 // every step strictly decreases the distance to x, so no cell is visited twice and
177 // the walk ends after at most nCells steps (the -1 return below is the real exit)
178 for (localIdx step = 0; step < m.nCells; step++)
179 {
180 label nearestCell = -1;
181 Vec3 nearestX = x;
182 const auto [start, end] = m.cellFaces.bounds(c);
183 for (localIdx k = start; k < end; k++)
184 {
185 const label f = m.cellFaces.values[k];
186 label n;
187 Vec3 xn = x;
189 {
190 // internal faces have f < nInternalFaces, so faceNeighbors[f] exists
191 n = (m.faceOwners[f] == c) ? m.faceNeighbors[f] : m.faceOwners[f];
192 }
193 else if (m.faceType[f] == label(TrackingFaceType::cyclic))
194 {
195 n = m.cyclicPartnerCell[f];
196 xn = x + m.cyclicShift[f];
197 }
198 else
199 {
200 continue;
201 }
202
203 if (pointInCell(xn, n, m))
204 {
205 x = xn;
206 return n;
207 }
208 const Vec3 dn = xn - m.cellCenters[n];
209 const scalar distSqr = dn & dn;
210 if (distSqr < nearestDistSqr)
211 {
212 nearestDistSqr = distSqr;
213 nearestCell = n;
214 nearestX = xn;
215 }
216 }
217 if (nearestCell == -1)
218 {
219 return -1;
220 }
221 c = nearestCell;
222 x = nearestX;
223 }
224 return -1;
225}
226
227} // namespace NeoN
A class representing a segment of indices.
View< ValueType > values
A View with the values.
KOKKOS_INLINE_FUNCTION Kokkos::pair< IndexType, IndexType > bounds(localIdx segI) const
Get the bounds of a segment.
Data structure that stores a segmented fields or a vector of vectors.
TrackingMeshView view() const
Views for use inside kernels.
const UnstructuredMesh & mesh() const
TrackingMesh(const UnstructuredMesh &mesh, SegmentedVector< label, localIdx > cellFaces, SegmentedVector< label, localIdx > facePoints, Vector< label > faceBasePt, Vector< label > faceType, Vector< label > cyclicPartnerCell, Vector< Vec3 > cyclicShift, Vector< Vec3 > points)
Construct from a mesh and the connectivity it lacks.
Represents an unstructured mesh in NeoN.
A class for the representation of a 3D Vec3.
Definition vec3.hpp:24
An executor-aware contiguous container for numerical data with vector-style arithmetic semantics.
Definition vector.hpp:40
Integer types used throughout NeoN.
Definition array.hpp:18
KOKKOS_INLINE_FUNCTION bool pointInCell(const Vec3 &x, const label c, const TrackingMeshView &m)
Is x inside cell c? Face-plane test, as OpenFOAM primitiveMesh::pointInCell (FACE_PLANES): x is not i...
float scalar
Definition scalar.hpp:17
TrackingFaceType
What happens to a point that leaves a cell through a face.
@ cyclic
periodic: shift the position, continue in the partner cell
@ internal
go to the neighbour cell
@ escape
any other boundary: the point leaves the domain
int32_t localIdx
Definition label.hpp:50
int32_t label
Definition label.hpp:44
KOKKOS_INLINE_FUNCTION label locateCell(Vec3 &x, label hint, const TrackingMeshView &m)
Locate the cell containing x, starting from cell hint.
Read-only views of a TrackingMesh, captured by value inside kernels.
SegmentedVectorView< const label, const localIdx > cellFaces
all faces of every cell
View< const label > cyclicPartnerCell
cyclic faces: cell behind the paired face
View< const label > faceNeighbors
internal faces only
View< const Vec3 > cellCenters
View< const Vec3 > cyclicShift
cyclic faces: translation to the paired face
View< const Vec3 > points
mesh points, on the executor
View< const label > faceOwners
View< const scalar > cellVolumes
View< const label > faceType
TrackingFaceType of every face.
View< const Vec3 > faceAreaVectors
UnstructuredMesh::faceNormals(), i.e. Sf.
SegmentedVectorView< const label, const localIdx > facePoints
points of every face
View< const Vec3 > faceCenters
View< const label > faceBasePt
tet base point of every face (local index)