NeoN
A framework for CFD software
Loading...
Searching...
No Matches
pointInterpolation.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
11
12namespace NeoN
13{
14
22KOKKOS_INLINE_FUNCTION
24 const Vec3& x, const Vec3& a, const Vec3& b, const Vec3& c, const Vec3& d, scalar w[4]
25)
26{
27 const Vec3 e1 = b - a, e2 = c - a, e3 = d - a, r = x - a;
28 const scalar det = (e1 ^ e2) & e3;
29 if (det == scalar(0))
30 {
31 w[0] = scalar(1);
32 w[1] = w[2] = w[3] = scalar(0);
33 return det;
34 }
35 w[1] = ((r ^ e2) & e3) / det;
36 w[2] = ((e1 ^ r) & e3) / det;
37 w[3] = ((e1 ^ e2) & r) / det;
38 w[0] = scalar(1) - w[1] - w[2] - w[3];
39 return det;
40}
41
57template<typename ValueType>
58KOKKOS_INLINE_FUNCTION ValueType interpolateCellPoint(
59 const TrackingMeshView& m,
60 const View<const ValueType>& cellValues,
61 const View<const ValueType>& pointValues,
62 const label c,
63 const Vec3& x
64)
65{
66 constexpr scalar tol = scalar(1e-15);
67 const Vec3 cc = m.cellCenters[c];
68 scalar best[4] = {scalar(1), scalar(0), scalar(0), scalar(0)};
69 label bestPts[3] = {-1, -1, -1};
70 scalar bestMin = scalar(-1e300);
71 bool found = false;
72 const auto [faceStart, faceEnd] = m.cellFaces.bounds(c);
73 for (localIdx k = faceStart; k < faceEnd && !found; k++)
74 {
75 const label face = m.cellFaces.values[k];
76 const auto [pointStart, pointEnd] = m.facePoints.bounds(face);
77 const label p0 = label(pointStart);
78 const label n = label(pointEnd) - p0;
79 const label base = m.faceBasePt[face];
80 for (label tetPt = 1; tetPt < n - 1; tetPt++)
81 {
82 // OpenFOAM tetIndices::faceTriIs (flipped for the neighbour cell)
83 label facePt = (tetPt + base) % n;
84 label otherPt = (facePt + 1) % n;
85 if (m.faceOwners[face] != c)
86 {
87 const label t = facePt;
88 facePt = otherPt;
89 otherPt = t;
90 }
91 const label ia = m.facePoints.values[p0 + base];
92 const label ib = m.facePoints.values[p0 + facePt];
93 const label ic = m.facePoints.values[p0 + otherPt];
94 scalar w[4];
95 const scalar det =
96 pointToBarycentric(x, cc, m.points[ia], m.points[ib], m.points[ic], w);
97 if (Kokkos::fabs(det / m.cellVolumes[c]) <= tol)
98 {
99 continue;
100 }
101 const scalar wMin = Kokkos::fmin(Kokkos::fmin(w[0], w[1]), Kokkos::fmin(w[2], w[3]));
102 if (wMin > bestMin)
103 {
104 bestMin = wMin;
105 for (int j = 0; j < 4; j++)
106 {
107 best[j] = w[j];
108 }
109 bestPts[0] = ia;
110 bestPts[1] = ib;
111 bestPts[2] = ic;
112 }
113 if (wMin + tol > scalar(0))
114 {
115 found = true;
116 break;
117 }
118 }
119 }
120 if (bestPts[0] < 0)
121 {
122 return cellValues[c];
123 }
124 return best[0] * cellValues[c] + best[1] * pointValues[bestPts[0]]
125 + best[2] * pointValues[bestPts[1]] + best[3] * pointValues[bestPts[2]];
126}
127
128} // namespace NeoN
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.
A class for the representation of a 3D Vec3.
Definition vec3.hpp:24
Integer types used throughout NeoN.
Definition array.hpp:18
KOKKOS_INLINE_FUNCTION scalar pointToBarycentric(const Vec3 &x, const Vec3 &a, const Vec3 &b, const Vec3 &c, const Vec3 &d, scalar w[4])
Barycentric coordinates w of x in the tetrahedron (a, b, c, d).
float scalar
Definition scalar.hpp:17
KOKKOS_INLINE_FUNCTION ValueType interpolateCellPoint(const TrackingMeshView &m, const View< const ValueType > &cellValues, const View< const ValueType > &pointValues, const label c, const Vec3 &x)
Interpolate a field at x in cell c from cell and point values, as OpenFOAM's interpolationCellPoint.
int32_t localIdx
Definition label.hpp:50
int32_t label
Definition label.hpp:44
Read-only views of a TrackingMesh, captured by value inside kernels.
SegmentedVectorView< const label, const localIdx > cellFaces
all faces of every cell
View< const Vec3 > cellCenters
View< const Vec3 > points
mesh points, on the executor
View< const label > faceOwners
View< const scalar > cellVolumes
SegmentedVectorView< const label, const localIdx > facePoints
points of every face
View< const label > faceBasePt
tet base point of every face (local index)