peclet.voro 1.0.4
Device-native moving-particle Voronoi dynamics
Loading...
Searching...
No Matches
lloyd.hpp
Go to the documentation of this file.
1
22#ifndef PECLET_VORO_ENERGY_LLOYD_HPP
23#define PECLET_VORO_ENERGY_LLOYD_HPP
24
25#include <Kokkos_Core.hpp>
26
29
30namespace peclet::voro::energy {
31
33template <class Real>
35 const Kokkos::View<Real*, peclet::core::MemSpace>& pos,
36 const Kokkos::View<Real*, peclet::core::MemSpace>& out) {
37 using Exec = peclet::core::ExecSpace;
38 const int N = view.numCells();
39 Kokkos::parallel_for(
40 "energy.centroid", Kokkos::RangePolicy<Exec>(0, N), KOKKOS_LAMBDA(const int i) {
41 Real m[3] = {0, 0, 0}, V = 0;
42 for (int f = view.facetBegin(i); f < view.facetEnd(i); ++f) {
43 const Real ax = view.area(f, 0), ay = view.area(f, 1), az = view.area(f, 2);
44 const Real A = Kokkos::sqrt(ax * ax + ay * ay + az * az);
45 if (!(A > Real(0)))
46 continue;
47 const Real rx = view.connVec(f, 0), ry = view.connVec(f, 1), rz = view.connVec(f, 2);
48 const Real rl = Kokkos::sqrt(rx * rx + ry * ry + rz * rz);
49 const Real h = Real(0.5) * rl; // foot distance (conn = 2 n)
50 const Real s = rl / A;
51 const Real cx = rx - view.connect(f, 0) * s, cy = ry - view.connect(f, 1) * s,
52 cz = rz - view.connect(f, 2) * s; // face centroid, seed-relative
53 const Real Vf = h * A / Real(3);
54 V += Vf;
55 m[0] += Vf * Real(0.75) * cx;
56 m[1] += Vf * Real(0.75) * cy;
57 m[2] += Vf * Real(0.75) * cz;
58 }
59 const Real iv = V > Real(0) ? Real(1) / V : Real(0);
60 for (int c = 0; c < 3; ++c)
61 out(3 * i + c) = pos(3 * i + c) + m[c] * iv;
62 });
63}
64
67template <class Real>
69 [[maybe_unused]] const Kokkos::View<Real*, peclet::core::MemSpace>& pos,
70 Real gamma, const Kokkos::View<Real*, peclet::core::MemSpace>& force) {
71 using Exec = peclet::core::ExecSpace;
72 const int N = view.numCells();
73 const bool haveM2 = view.hasMoments();
74 Real E = 0;
75 Kokkos::parallel_reduce(
76 "energy.lloyd", Kokkos::RangePolicy<Exec>(0, N),
77 KOKKOS_LAMBDA(const int i, Real& acc) {
78 Real m[3] = {0, 0, 0}, V = 0, e = 0;
79 for (int f = view.facetBegin(i); f < view.facetEnd(i); ++f) {
80 const Real ax = view.area(f, 0), ay = view.area(f, 1), az = view.area(f, 2);
81 const Real A = Kokkos::sqrt(ax * ax + ay * ay + az * az);
82 if (!(A > Real(0)))
83 continue;
84 const Real rx = view.connVec(f, 0), ry = view.connVec(f, 1), rz = view.connVec(f, 2);
85 const Real rl = Kokkos::sqrt(rx * rx + ry * ry + rz * rz);
86 const Real h = Real(0.5) * rl;
87 const Real s = rl / A;
88 const Real cx = rx - view.connect(f, 0) * s, cy = ry - view.connect(f, 1) * s,
89 cz = rz - view.connect(f, 2) * s;
90 const Real Vf = h * A / Real(3);
91 V += Vf;
92 m[0] += Vf * Real(0.75) * cx;
93 m[1] += Vf * Real(0.75) * cy;
94 m[2] += Vf * Real(0.75) * cz;
95 if (haveM2)
96 e += h * (h * h * A + view.moment2(f)) / Real(5);
97 }
98 // gradient: 2 V (x − c) = −2 m (m = V (c − x))
99 for (int c = 0; c < 3; ++c)
100 Kokkos::atomic_add(&force(3 * i + c), Real(-2) * gamma * m[c]);
101 acc += gamma * e;
102 },
103 E);
104 return haveM2 ? E : Real(-1);
105}
106
107} // namespace peclet::voro::energy
108
109#endif // PECLET_VORO_ENERGY_LLOYD_HPP
Definition interface.hpp:23
void cellCentroids(const TessellationView< Real > &view, const Kokkos::View< Real *, peclet::core::MemSpace > &pos, const Kokkos::View< Real *, peclet::core::MemSpace > &out)
Cell centroids (3N, world = seed + relative) from the facet CSR. pos supplies the seeds.
Definition lloyd.hpp:34
Real lloydEnergyForce(const TessellationView< Real > &view, const Kokkos::View< Real *, peclet::core::MemSpace > &pos, Real gamma, const Kokkos::View< Real *, peclet::core::MemSpace > &force)
Definition lloyd.hpp:68
Rung A3: route a cell's per-facet geometry gradient to the seed DOFs on the PUBLISHED view — the one ...
Definition tessellation_view.hpp:111
KOKKOS_INLINE_FUNCTION Real moment2(int f) const
Definition tessellation_view.hpp:162
KOKKOS_INLINE_FUNCTION Real area(int f, int c) const
Definition tessellation_view.hpp:186
KOKKOS_INLINE_FUNCTION int facetEnd(int i) const
Definition tessellation_view.hpp:182
KOKKOS_INLINE_FUNCTION Real connVec(int f, int c) const
Definition tessellation_view.hpp:188
KOKKOS_INLINE_FUNCTION bool hasMoments() const
Definition tessellation_view.hpp:161
KOKKOS_INLINE_FUNCTION int facetBegin(int i) const
Definition tessellation_view.hpp:181
KOKKOS_INLINE_FUNCTION int numCells() const
Definition tessellation_view.hpp:173
KOKKOS_INLINE_FUNCTION Real connect(int f, int c) const
Definition tessellation_view.hpp:187
Published, read-only device data layer for the tessellation (migration §2.1, §3).