18#ifndef PECLET_VORO_FV_POLYMESH_HPP
19#define PECLET_VORO_FV_POLYMESH_HPP
24#include <Kokkos_Core.hpp>
26#include <unordered_map>
29#include "peclet/core/common/view.hpp"
58 std::size_t h = (std::size_t)k.
a * 73856093u;
59 h ^= (std::size_t)k.
b * 19349663u;
60 h ^= (std::size_t)k.
c * 83492791u;
69 while (parent[a] != a)
70 a = parent[a] = parent[parent[a]];
78 std::vector<char> onWall(nP, 0);
79 for (
int f = 0; f < m.
nFaces; ++f)
84 for (
int p = 0; p < nP; ++p)
93 const std::vector<int>& candidates, std::vector<int>& parent) {
94 for (
int q : candidates) {
98 for (
int k = 0; k < 3; ++k) {
100 d -= L[k] * std::round(d / L[k]);
115 const std::vector<int>& ids, std::vector<int>& parent) {
119 for (
int k = 0; k < 3; ++k)
120 Mq[k] = std::max<long long>(1, (
long long)std::floor(L[k] / tol));
121 auto cellOf = [&](Real x,
int k) {
122 long long c = (
long long)std::floor(x / L[k] * (Real)Mq[k]);
124 return c < 0 ? c + Mq[k] : c;
128 cellOf(m.
points[3 * p + 2], 2)}]
130 const Real tol2 = tol * tol;
132 const long long c0[3] = {cellOf(m.
points[3 * p], 0), cellOf(m.
points[3 * p + 1], 1),
133 cellOf(m.
points[3 * p + 2], 2)};
134 for (
int dz = -1; dz <= 1; ++dz)
135 for (
int dy = -1; dy <= 1; ++dy)
136 for (
int dx = -1; dx <= 1; ++dx) {
137 auto w = [&](
long long c,
long long M) {
return ((c % M) + M) % M; };
139 detail::VertexKey{w(c0[0] + dx, Mq[0]), w(c0[1] + dy, Mq[1]), w(c0[2] + dz, Mq[2])});
140 if (it == grid.end())
151 std::vector<int>& parent) {
153 std::vector<Real> acc(3 * (
size_t)nP, Real(0));
154 std::vector<int> cnt(nP, 0);
158 for (
int k = 0; k < 3; ++k) {
160 d -= L[k] * std::round(d / L[k]);
169 for (
int k = 0; k < 3; ++k)
170 m.
points[3 * p + k] += acc[3 * p + k] / (Real)cnt[p];
178 std::vector<int> newVerts, newOff(1, 0), newOwner, newNbr, newPatch;
179 std::vector<int> faceMap(m.
nFaces, -1);
180 for (
int f = 0; f < m.
nFaces; ++f) {
181 std::vector<int> poly;
184 if (poly.empty() || poly.back() != v)
187 while (poly.size() > 1 && poly.front() == poly.back())
191 faceMap[f] = (int)newOff.size() - 1;
192 newVerts.insert(newVerts.end(), poly.begin(), poly.end());
193 newOff.push_back((
int)newVerts.size());
194 newOwner.push_back(m.
owner[f]);
196 newPatch.push_back(m.
patch[f]);
198 std::vector<int> cf, cfo(1, 0);
199 for (
int i = 0; i < m.
nCells; ++i) {
202 cf.push_back(faceMap[m.
cellFace[q]]);
203 cfo.push_back((
int)cf.size());
206 for (
int f = 0; f < (int)newPatch.size(); ++f)
207 nInt += newPatch[f] == 0;
215 m.
nFaces = (int)newPatch.size();
234 std::vector<int> parent(nP);
235 for (
int p = 0; p < nP; ++p)
248 std::unordered_map<VertexKey, int, VertexKeyHash>
vmap;
254 vmap.reserve((
size_t)N * 8);
255 for (
int k = 0; k < 3; ++k)
256 M[k] = (
long long)std::llround(Real(1) /
quantum);
258 static long long wrapq(
long long v,
long long m) {
260 return v < 0 ? v + m : v;
267 auto it =
vmap.find(k);
268 if (it !=
vmap.end())
288template <
class Real,
class Cell,
class PosM,
class RefM,
class RecM,
class CntM>
290 const CntM& wcntM,
const Real L[3]) {
291 const Real Lh[3] = {L[0] / 2, L[1] / 2, L[2] / 2};
292 const Real sx = ph(3 * i), sy = ph(3 * i + 1), sz = ph(3 * i + 2);
293 Real dx = sx - xrM(3 * i), dy = sy - xrM(3 * i + 1), dz = sz - xrM(3 * i + 2);
294 dx = dx > Lh[0] ? dx - L[0] : (dx < -Lh[0] ? dx + L[0] : dx);
295 dy = dy > Lh[1] ? dy - L[1] : (dy < -Lh[1] ? dy + L[1] : dy);
296 dz = dz > Lh[2] ? dz - L[2] : (dz < -Lh[2] ? dz + L[2] : dz);
299 const int nrec = wcntM(i);
300 for (
int k = 6; k < c.np; ++k) {
301 if (c.pnbr[k] >= 0 || r >= nrec)
304 const Real ux = wrecM(o), uy = wrecM(o + 1), uz = wrecM(o + 2);
305 const Real h = wrecM(o + 3) - (ux * dx + uy * dy + uz * dz);
316template <
class Real,
class Cell>
320 for (
int k = 0; k < c.np; ++k) {
321 Real fx[Cell::MAXFV], fy[Cell::MAXFV], fz[Cell::MAXFV];
322 const int nv = c.faceOrdered(k, fx, fy, fz);
325 const int j = c.pnbr[k];
327 for (
int q = 1; q + 1 < nv; ++q) {
328 const Real ax = fx[0], ay = fy[0], az = fz[0], bx = fx[q], by = fy[q], bz = fz[q],
329 cx = fx[q + 1], cy = fy[q + 1], cz = fz[q + 1];
330 V += std::fabs(ax * (by * cz - bz * cy) - ay * (bx * cz - bz * cx) +
331 az * (bx * cy - by * cx)) /
336 const int fid = (int)faces.
owner.size();
337 faces.
owner.push_back(i);
338 faces.
nbr.push_back(j >= 0 ? j : -1);
340 for (
int q = 0; q < nv; ++q)
341 faces.
verts.push_back(vertexId(m, sx + fx[q], sy + fy[q], sz + fz[q]));
342 faces.
off.push_back((
int)faces.
verts.size());
353 const int nF = (int)faces.
owner.size();
354 std::vector<int> perm(nF), inv(nF);
356 for (
int f = 0; f < nF; ++f)
357 if (faces.
patch[f] == 0)
360 for (
int f = 0; f < nF; ++f)
361 if (faces.
patch[f] != 0)
363 for (
int f = 0; f < nF; ++f)
367 for (
int f = 0; f < nF; ++f) {
368 const int o = perm[f];
372 for (
int v = faces.
off[o]; v < faces.
off[o + 1]; ++v)
377 for (
int i = 0; i < N; ++i) {
386template <
class Real,
int MAXP,
int MAXT>
388 const Kokkos::View<Real*, peclet::core::MemSpace>& pos,
int N,
390 Kokkos::View<Real*, peclet::core::MemSpace> xRef = {},
391 Real quantum = Real(1e-9)) {
392 using Cell = ConvexCell<Real, MAXP, MAXT, false>;
393 auto np = Kokkos::create_mirror_view_and_copy(Kokkos::HostSpace{}, store.
np);
394 auto nt = Kokkos::create_mirror_view_and_copy(Kokkos::HostSpace{}, store.
nt);
395 auto pnbr = Kokkos::create_mirror_view_and_copy(Kokkos::HostSpace{}, store.
pnbr);
396 auto tri = Kokkos::create_mirror_view_and_copy(Kokkos::HostSpace{}, store.
tri);
397 auto ph = Kokkos::create_mirror_view_and_copy(Kokkos::HostSpace{}, pos);
399 auto loadHost = [&](
int i, Cell& c) {
400 c.initBoxPlanes(L[0], L[1], L[2]);
401 const int npi = np(i), nti = nt(i);
405 for (
int k = 6; k < npi; ++k)
406 c.pnbr[k] = pnbr((
size_t)i * MAXP + k);
407 for (
int t = 0; t < nti; ++t) {
408 const unsigned w = tri((
size_t)i * MAXT + t);
409 c.t0[t] = (
unsigned char)(w & 0xffu);
410 c.t1[t] = (
unsigned char)((w >> 8) & 0xffu);
411 c.t2[t] = (
unsigned char)((w >> 16) & 0xffu);
412 c.alive[t] = ((w >> 24) & 1u) != 0u;
415 const bool hasWall = wall.cnt.extent(0) > 0;
416 typename Kokkos::View<Real*, peclet::core::MemSpace>::host_mirror_type wrecM, xrM;
417 typename Kokkos::View<int*, peclet::core::MemSpace>::host_mirror_type wcntM;
419 wrecM = Kokkos::create_mirror_view_and_copy(Kokkos::HostSpace{}, wall.rec);
420 wcntM = Kokkos::create_mirror_view_and_copy(Kokkos::HostSpace{}, wall.cnt);
421 xrM = Kokkos::create_mirror_view_and_copy(Kokkos::HostSpace{}, xRef);
425 m.cellVolume.assign(N, Real(0));
428 detail::PolyFaceList faces(N);
429 for (
int i = 0; i < N; ++i) {
432 const Real sx = ph(3 * i), sy = ph(3 * i + 1), sz = ph(3 * i + 2);
435 c.reevalGeometry(sx, sy, sz, ph.data(), L[0]);
447 const std::vector<Real>* cellScalar =
nullptr,
const char* scalarName =
"scalar") {
448 std::FILE* fp = std::fopen(path.c_str(),
"w");
453 "<?xml version=\"1.0\"?>\n<VTKFile type=\"UnstructuredGrid\" version=\"1.0\" "
454 "byte_order=\"LittleEndian\">\n<UnstructuredGrid>\n<Piece NumberOfPoints=\"%d\" "
455 "NumberOfCells=\"%d\">\n",
458 fp,
"<Points>\n<DataArray type=\"Float64\" NumberOfComponents=\"3\" format=\"ascii\">\n");
459 for (
int p = 0; p < nP; ++p)
460 std::fprintf(fp,
"%.17g %.17g %.17g\n", (
double)m.
points[3 * p], (
double)m.
points[3 * p + 1],
461 (
double)m.
points[3 * p + 2]);
462 std::fprintf(fp,
"</DataArray>\n</Points>\n<Cells>\n");
464 std::vector<int> conn, connOff, faces, faceOff;
465 std::vector<char> seen(nP, 0);
466 for (
int i = 0; i < nC; ++i) {
467 std::vector<int> touched;
474 touched.push_back(p);
479 for (
int p : touched)
481 connOff.push_back((
int)conn.size());
496 faceOff.push_back((
int)faces.size());
498 std::fprintf(fp,
"<DataArray type=\"Int64\" Name=\"connectivity\" format=\"ascii\">\n");
500 std::fprintf(fp,
"%d\n", v);
501 std::fprintf(fp,
"</DataArray>\n<DataArray type=\"Int64\" Name=\"offsets\" format=\"ascii\">\n");
502 for (
int o : connOff)
503 std::fprintf(fp,
"%d\n", o);
504 std::fprintf(fp,
"</DataArray>\n<DataArray type=\"UInt8\" Name=\"types\" format=\"ascii\">\n");
505 for (
int i = 0; i < nC; ++i)
506 std::fprintf(fp,
"42\n");
507 std::fprintf(fp,
"</DataArray>\n<DataArray type=\"Int64\" Name=\"faces\" format=\"ascii\">\n");
509 std::fprintf(fp,
"%d\n", v);
511 "</DataArray>\n<DataArray type=\"Int64\" Name=\"faceoffsets\" format=\"ascii\">\n");
512 for (
int o : faceOff)
513 std::fprintf(fp,
"%d\n", o);
514 std::fprintf(fp,
"</DataArray>\n</Cells>\n<CellData>\n");
515 std::fprintf(fp,
"<DataArray type=\"Float64\" Name=\"volume\" format=\"ascii\">\n");
516 for (
int i = 0; i < nC; ++i)
517 std::fprintf(fp,
"%.17g\n", (
double)m.
cellVolume[i]);
518 std::fprintf(fp,
"</DataArray>\n");
520 std::fprintf(fp,
"<DataArray type=\"Float64\" Name=\"%s\" format=\"ascii\">\n", scalarName);
521 for (
int i = 0; i < nC; ++i)
522 std::fprintf(fp,
"%.17g\n", (
double)(*cellScalar)[i]);
523 std::fprintf(fp,
"</DataArray>\n");
525 std::fprintf(fp,
"</CellData>\n</Piece>\n</UnstructuredGrid>\n</VTKFile>\n");
KOKKOS_INLINE_FUNCTION Dual< Real, K > dnum(Real c)
Definition convex_cell.hpp:53
void storeInteriorFirst(PolyMesh< Real > &m, const PolyFaceList &faces, int N)
Copy the emitted faces into m, interior faces first, and the cell face lists renumbered.
Definition polymesh.hpp:352
Real emitCellFaces(const Cell &c, int i, Real sx, Real sy, Real sz, PeriodicVertexIds< Real > &vertexId, PolyMesh< Real > &m, PolyFaceList &faces)
Definition polymesh.hpp:317
void unionWithinTolerance(const PolyMesh< Real > &m, const Real L[3], Real tol2, int p, const std::vector< int > &candidates, std::vector< int > &parent)
Join vertex p with every candidate q > p within the min-image distance sqrt(tol2).
Definition polymesh.hpp:92
int moveRootsToGroupMean(PolyMesh< Real > &m, const Real L[3], const std::vector< int > &ids, std::vector< int > &parent)
Definition polymesh.hpp:150
void unionNearWallVertices(const PolyMesh< Real > &m, const Real L[3], Real tol, const std::vector< int > &ids, std::vector< int > &parent)
Definition polymesh.hpp:114
int unionFindRoot(std::vector< int > &parent, int a)
Root of a in a union–find forest, halving the path on the way (mergeWallVertices).
Definition polymesh.hpp:68
void rewriteMergedFaces(PolyMesh< Real > &m, std::vector< int > &parent)
Definition polymesh.hpp:177
void restoreWallPlanesHost(Cell &c, int i, const PosM &ph, const RefM &xrM, const RecM &wrecM, const CntM &wcntM, const Real L[3])
Definition polymesh.hpp:289
std::vector< int > wallVertexIds(const PolyMesh< Real > &m)
The vertices of the wall faces (patch 1), ascending.
Definition polymesh.hpp:76
Definition collocated.hpp:38
PolyMesh< Real > buildPolyMesh(const TopologyStore< MAXP, MAXT > &store, const Kokkos::View< Real *, peclet::core::MemSpace > &pos, int N, const Real L[3], WallStore< Real > wall={}, Kokkos::View< Real *, peclet::core::MemSpace > xRef={}, Real quantum=Real(1e-9))
Assemble the polyhedral mesh of a store at positions posHost (3N, world, in [0,L)).
Definition polymesh.hpp:387
bool writeVtu(const std::string &path, const PolyMesh< Real > &m, const std::vector< Real > *cellScalar=nullptr, const char *scalarName="scalar")
Definition polymesh.hpp:446
int mergeWallVertices(PolyMesh< Real > &m, const Real L[3], Real tol)
Definition polymesh.hpp:231
constexpr int kBoundaryFacet
Definition sdf.hpp:36
Definition topology_store.hpp:39
Kokkos::View< int *, MemSpace > np
Definition topology_store.hpp:42
Kokkos::View< unsigned *, MemSpace > tri
Definition topology_store.hpp:45
Kokkos::View< int *, MemSpace > nt
Definition topology_store.hpp:43
Kokkos::View< int *, MemSpace > pnbr
Definition topology_store.hpp:44
Definition polymesh.hpp:37
std::vector< int > faceOffset
Definition polymesh.hpp:40
std::vector< Real > points
Definition polymesh.hpp:39
std::vector< int > patch
Definition polymesh.hpp:44
std::vector< int > neighbour
Definition polymesh.hpp:43
std::vector< int > cellFaceOffset
Definition polymesh.hpp:45
std::vector< int > faceVerts
Definition polymesh.hpp:41
int nInterior
Definition polymesh.hpp:38
int nCells
Definition polymesh.hpp:38
int nPoints() const
Definition polymesh.hpp:48
std::vector< Real > cellVolume
Definition polymesh.hpp:47
std::vector< int > cellFace
Definition polymesh.hpp:46
std::vector< int > owner
Definition polymesh.hpp:42
int nFaces
Definition polymesh.hpp:38
Definition polymesh.hpp:247
long long M[3]
Definition polymesh.hpp:249
static long long wrapq(long long v, long long m)
Definition polymesh.hpp:258
PeriodicVertexIds(const Real L_[3], Real quantum_, int N)
Definition polymesh.hpp:253
std::unordered_map< VertexKey, int, VertexKeyHash > vmap
Definition polymesh.hpp:248
const Real * L
Definition polymesh.hpp:251
Real quantum
Definition polymesh.hpp:250
int operator()(PolyMesh< Real > &m, Real x, Real y, Real z)
The id of the vertex at (x, y, z), appended to m.points if it is new.
Definition polymesh.hpp:263
The faces of a PolyMesh as they are emitted cell by cell, before the interior-first order.
Definition polymesh.hpp:280
std::vector< std::vector< int > > cellFaces
Definition polymesh.hpp:281
std::vector< int > verts
Definition polymesh.hpp:282
std::vector< int > nbr
Definition polymesh.hpp:282
std::vector< int > patch
Definition polymesh.hpp:282
PolyFaceList(int N)
Definition polymesh.hpp:283
std::vector< int > off
Definition polymesh.hpp:282
std::vector< int > owner
Definition polymesh.hpp:282
Definition polymesh.hpp:56
std::size_t operator()(const VertexKey &k) const
Definition polymesh.hpp:57
Definition polymesh.hpp:52
bool operator==(const VertexKey &o) const
Definition polymesh.hpp:54
long long b
Definition polymesh.hpp:53
long long a
Definition polymesh.hpp:53
long long c
Definition polymesh.hpp:53