42#include "BoundaryConditions.h"
94 if (!block || block->isNotActive())
97 auto kernel = block->getKernel();
101 auto bcSet = kernel->getBCSet();
105 auto bcArray = bcSet->getBCArray();
116 const int nx1 =
static_cast<int>(bcArray->getNX1());
117 const int nx2 =
static_cast<int>(bcArray->getNX2());
118 const int nx3 =
static_cast<int>(bcArray->getNX3());
120 for (
int iz = 0; iz < nx3; ++iz) {
121 for (
int iy = 0; iy < nx2; ++iy) {
122 for (
int ix = 0; ix < nx1; ++ix) {
123 if (bcArray->isSolid(ix, iy, iz))
125 if (bcArray->hasBC(ix, iy, iz)) {
126 bcNodes.insert({ ix, iy, iz });
140#if !defined(VF_HAS_FAST_WINDING)
154 auto bcSet = findBCSet(
gridPtr);
166 rebuildNodeIndexMaps();
171 for (
const auto &bc :
BCs) {
172 if (bc && bc->isTimeDependent())
207void D3Q27GridWindingInteractor::rebuildNodeIndexMaps()
220 std::vector<SPtr<Block3D>> blocks;
221 gridPtr->getBlocks(level, blocks);
223 for (
const auto &block : blocks)
239 std::vector<SPtr<Block3D>> blocks;
240 grid->getBlocks(level, blocks);
242 for (
const auto &block : blocks) {
245 auto kernel = block->getKernel();
248 auto bcSet = kernel->getBCSet();
260static QLineCollection collectQLines(
const SPtr<Grid3D> &grid,
263 QLineCollection collection;
268 std::vector<float>
qSend;
273 const int coarsest = grid->getCoarsestInitializedLevel();
274 const int finest = grid->getFinestInitializedLevel();
277 std::vector<SPtr<Block3D>> blocks;
278 grid->getBlocks(level, blocks);
279 for (
const auto &block : blocks) {
283 auto kernel = block->getKernel();
287 auto bcSet = kernel->getBCSet();
291 auto bcArray = bcSet->getBCArray();
295 const int nx =
static_cast<int>(bcArray->getNX1());
296 const int ny =
static_cast<int>(bcArray->getNX2());
297 const int nz =
static_cast<int>(bcArray->getNX3());
299 const double dx =
static_cast<double>(grid->getDeltaX(block));
301 for (
int iz = 0; iz <
nz; ++iz) {
302 for (
int iy = 0; iy <
ny; ++iy) {
303 for (
int ix = 0; ix <
nx; ++ix) {
304 if (!bcArray->hasBC(ix, iy, iz))
307 auto bc = bcArray->getBC(ix, iy, iz);
316 for (
int dir = d3q27_system::FSTARTDIR; dir <= d3q27_system::FENDDIR; ++dir) {
317 const float rawQ = bc->getQ(dir);
324 if (cx == 0 && cy == 0 && cz == 0)
329 const double hitX =
ox +
static_cast<double>(
clampedQ) *
dx *
static_cast<double>(cx);
330 const double hitY =
oy +
static_cast<double>(
clampedQ) *
dx *
static_cast<double>(cy);
331 const double hitZ =
oz +
static_cast<double>(
clampedQ) *
dx *
static_cast<double>(cz);
353 std::vector<float>
qRecv;
370 if (comm && !comm->isRoot())
406 const int coarsestLevel = grid_->getCoarsestInitializedLevel();
407 std::vector<SPtr<Block3D>> blocks;
410 cellSizeHint_ =
static_cast<double>(grid_->getDeltaX(blocks.front()));
414 template <
typename Fn>
422 const int coarsestLevel = grid_->getCoarsestInitializedLevel();
423 const int finestLevel = grid_->getFinestInitializedLevel();
426 std::vector<SPtr<Block3D>> blocks;
427 grid_->getBlocks(level, blocks);
428 for (
const auto &block : blocks) {
429 auto kernel = block->getKernel();
433 auto bcSet = kernel->getBCSet();
437 auto bcArray = bcSet->getBCArray();
441 real minX1, minX2, minX3, maxX1, maxX2, maxX3;
454 const double deltaCell =
static_cast<double>(grid_->getDeltaX(block));
467 const int NX =
static_cast<int>(bcArray->getNX1()) - 1;
468 const int NY =
static_cast<int>(bcArray->getNX2()) - 1;
469 const int NZ =
static_cast<int>(bcArray->getNX3()) - 1;
471 for (
int k = 0; k <=
NZ; ++k)
472 for (
int j = 0;
j <=
NY; ++
j)
473 for (
int i = 0;
i <=
NX; ++
i) {
492 node.
bcArray->
setSolid(
static_cast<std::size_t
>(node.
ix),
static_cast<std::size_t
>(node.
iy),
493 static_cast<std::size_t
>(node.
iz));
499 return cellSizeHint_;
504 std::array<double, 3> meshMin_;
505 std::array<double, 3> meshMax_;
506 double cellSizeHint_{ 0.0 };
518#if defined(VF_HAS_FAST_WINDING)
551 outBc = bcArray->getBC(ix, iy, iz);
553 outBc = std::make_shared<BoundaryConditions>();
554 bcArray->setBC(ix, iy, iz,
outBc);
580static_assert(std::is_trivially_copyable_v<RemoteBoundaryLinkMessage>,
581 "RemoteBoundaryLinkMessage must be trivially copyable for MPI exchange");
592 using namespace detail;
594 if (!
surface || !grid || !bcSet)
597 auto *triangles =
surface->getTriangles();
598 auto *nodes =
surface->getNodes();
599 if (!nodes || !triangles || triangles->empty())
619 int currentRank = communicator ? communicator->getProcessID() : 0;
620 int worldSize = communicator ? communicator->getNumberOfProcesses() : 1;
624 const bool isRootRank = !communicator || communicator->isRoot();
626 VF_LOG_WARNING(
"No kd-tree available for grid-winding; falling back to triangle list intersections.");
632 if (
void *
native = communicator->getNativeCommunicator()) {
636 worldSize = communicator->getNumberOfProcesses();
647 std::vector<std::vector<detail::RemoteBoundaryLinkMessage>>
remoteMessages(
674 bool solidLocal{
false };
681 if (!bcArray->isFluid(
static_cast<std::size_t
>(ix),
682 static_cast<std::size_t
>(iy),
683 static_cast<std::size_t
>(iz)))
685 bcArray->setSolid(
static_cast<std::size_t
>(ix),
686 static_cast<std::size_t
>(iy),
687 static_cast<std::size_t
>(iz));
695 auto bc = bcArray->getBC(ix, iy, iz);
698 return static_cast<double>(bc->getQ(dir));
705 if (bcArray->isSolid(
static_cast<std::size_t
>(ix),
706 static_cast<std::size_t
>(iy),
707 static_cast<std::size_t
>(iz)))
721 auto bc = bcArray->getBC(ix, iy, iz);
723 bc = std::make_shared<BoundaryConditions>();
724 bcArray->setBC(ix, iy, iz, bc);
726 bc->setQ(
static_cast<float>(q), dir);
729 const double ox = origin.X1();
730 const double oy = origin.X2();
731 const double oz = origin.X3();
748 return {
false, 0.0 };
757 auto bc = bcArray->getBC(ix, iy, iz);
759 bc = std::make_shared<BoundaryConditions>();
760 bcArray->setBC(ix, iy, iz, bc);
764 bc->setQ(
static_cast<float>(
fallbackQ), dir);
767 const double ox = origin.X1();
768 const double oy = origin.X2();
769 const double oz = origin.X3();
795 auto bcSet = kernel->getBCSet();
799 auto bcArray = bcSet->getBCArray();
812 if (!bcArray->isFluid(
static_cast<std::size_t
>(ix),
static_cast<std::size_t
>(iy),
813 static_cast<std::size_t
>(iz))) {
814 if (bcArray->isSolid(
static_cast<std::size_t
>(ix),
static_cast<std::size_t
>(iy),
815 static_cast<std::size_t
>(iz)))
817 bcArray->setFluid(
static_cast<std::size_t
>(ix),
static_cast<std::size_t
>(iy),
818 static_cast<std::size_t
>(iz));
829 const int solidX = ix + cx;
830 const int solidY = iy + cy;
831 const int solidZ = iz + cz;
834 adapter.bcArray = bcArray.get();
845 adapter.solidLocal = solidLocal;
851 origin.X2() +
dxLocal *
static_cast<double>(cy),
852 origin.X3() +
dxLocal *
static_cast<double>(cz) };
853 link.direction = dir;
880 auto setQ = [&](
double q,
int patchIndex) {
902 const int coarsestLevel = grid->getCoarsestInitializedLevel();
903 const int finestLevel = grid->getFinestInitializedLevel();
906 std::vector<SPtr<Block3D>> blocks;
907 grid->getBlocks(level, blocks);
908 for (
const auto &block : blocks) {
909 auto kernel = block->getKernel();
920 const int nx =
static_cast<int>(bcArray->getNX1());
921 const int ny =
static_cast<int>(bcArray->getNX2());
922 const int nz =
static_cast<int>(bcArray->getNX3());
928 for (
int iz = 0; iz <
nz; ++iz) {
929 for (
int iy = 0; iy <
ny; ++iy) {
930 for (
int ix = 0; ix <
nx; ++ix) {
931 if (!bcArray->isSolid(ix, iy, iz))
935 bool solidLocal =
true;
937 for (
int dir = d3q27_system::FSTARTDIR; dir <= d3q27_system::FENDDIR; ++dir) {
941 if (cx == 0 && cy == 0 && cz == 0)
944 const int fluidX = ix + cx;
945 const int fluidY = iy + cy;
946 const int fluidZ = iz + cz;
951 if (bcArray->isSolid(
static_cast<std::size_t
>(
fluidX),
static_cast<std::size_t
>(
fluidY),
952 static_cast<std::size_t
>(
fluidZ)))
956 bool fluidLocal =
true;
957 if (!solidLocal && !fluidLocal)
971 for (
int iz = 0; iz <
nz; ++iz) {
972 for (
int iy = 0; iy <
ny; ++iy) {
973 for (
int ix = 0; ix <
nx; ++ix) {
976 if (bcArray->isSolid(ix, iy, iz))
979 const bool originFluid = bcArray->isFluid(ix, iy, iz);
984 for (
int dir = d3q27_system::FSTARTDIR; dir <= d3q27_system::FENDDIR; ++dir) {
988 if (cx == 0 && cy == 0 && cz == 0)
991 const int solidX = ix + cx;
992 const int solidY = iy + cy;
993 const int solidZ = iz + cz;
998 if (!bcArray->isSolid(
static_cast<std::size_t
>(
solidX),
static_cast<std::size_t
>(
solidY),
999 static_cast<std::size_t
>(
solidZ)))
1003 const bool solidLocal =
true;
1017 if (lhs.ix != rhs.ix)
1018 return lhs.ix < rhs.ix;
1019 if (lhs.iy != rhs.iy)
1020 return lhs.iy < rhs.iy;
1021 if (lhs.iz != rhs.iz)
1022 return lhs.iz < rhs.iz;
1023 return lhs.dir < rhs.dir;
1041 if (
link.fluidLocal) {
1083 std::vector<detail::RemoteBoundaryLinkMessage>
sendBuffer;
1106 std::vector<detail::RemoteBoundaryLinkMessage>
recvBuffer(
static_cast<std::size_t
>(
totalRecv));
1146 auto kernel = block->getKernel();
1157 const int nx =
static_cast<int>(bcArray->getNX1());
1158 const int ny =
static_cast<int>(bcArray->getNX2());
1159 const int nz =
static_cast<int>(bcArray->getNX3());
1161 for (
int dir = d3q27_system::FSTARTDIR; dir <= d3q27_system::FENDDIR; ++dir) {
1165 if (cx == 0 && cy == 0 && cz == 0)
1174 if (bcArray->isSolid(
static_cast<std::size_t
>(
fluidX),
static_cast<std::size_t
>(
fluidY),
1175 static_cast<std::size_t
>(
fluidZ)))
1209 if (!
surface || !grid || !bcSet)
1214 rank = comm->getProcessID();
1217#if defined(VF_HAS_FAST_WINDING)
1222 vf::grid_winding::cpu::markSolidsWithFastWinding(
1227 VF_LOG_INFO(
"CPU fast-winding classification completed on rank {} in {} sec",
1239 VF_LOG_INFO(
"CPU subgrid link computation finished on rank {} in {} sec "
1240 "(hits={}, total={}, promoted={}, skipped={})",
1242 qTimer.getCurrentRuntimeInSeconds(),
1245 result.stats.promotedFluidNodes,
1246 result.stats.skippedNonLocalLinks);
1250 std::vector<double>
promotedZeroQ{
static_cast<double>(
result.stats.promotedZeroQNodes) };
1253 if (comm->isRoot()) {
1258 VF_LOG_INFO(
"CPU total promotions (solid) total: {}",
result.stats.promotedFluidNodes);
1259 VF_LOG_INFO(
"CPU zero-q promotions (solid) total: {}",
result.stats.promotedZeroQNodes);
1262 const auto &stats =
result.stats;
1263 if (stats.totalLinks == stats.hitLinks) {
1264 VF_LOG_INFO(
"All CPU boundary links intersected the geometry on rank {}.", rank);
1266 VF_LOG_WARNING(
"Some CPU boundary links did not intersect the geometry on rank {}.", rank);
1267 if (stats.skippedNonLocalLinks > 0) {
1278 if (comm && comm->isRoot() && !
basePath.empty()) {
1279 VF_LOG_INFO(
"CPU missing-link aggregation completed in {} sec",
1282 }
else if (comm && comm->isRoot()) {
1283 VF_LOG_INFO(
"CPU missing-link diagnostics disabled; skipping aggregation.");
1289 result.qLines = vf::grid_winding::cpu::collectQLines(
1292 if (comm && comm->isRoot() && !
basePath.empty()) {
1293 VF_LOG_INFO(
"CPU Q-line collection completed in {} sec",
1296 }
else if (comm && comm->isRoot()) {
1297 VF_LOG_INFO(
"CPU Q-line diagnostics disabled; skipping collection.");
#define VF_LOG_WARNING(...)
A class implements array to store boundary conditions flags.
std::vector< SPtr< BC > > BCs
SolidNodeIndicesMap solidNodeIndicesMap
BcNodeIndicesMap bcNodeIndicesMap
This Class provides basic 3D vector objects.
SPtr< GbObject3D > geoObject3D
void unsetTimeDependent()
std::vector< SPtr< Block3D > > bcBlocks
double cellSizeHint() const
CpuWindingSolidAccessor(const SPtr< Grid3D > &grid, std::array< double, 3 > meshMin, std::array< double, 3 > meshMax)
void forEachNode(Fn &&fn)
void setSolid(const Node &node, float) const
static std::shared_ptr< Communicator > getInstance()
std::shared_ptr< T > SPtr
bool isSolid(std::size_t x1, std::size_t x2, std::size_t x3) const
void setSolid(std::size_t x1, std::size_t x2, std::size_t x3)
void initInteractor(const real &timeStep=0) override
void initInteractor(const real &timeStep=0) override
void computeBoundaryData(const SPtr< GbTriFaceMesh3D > &surface, const SPtr< Grid3D > &grid, const SPtr< BCSet > &bcSet, const std::vector< SPtr< BC > > &boundaryAdapters, double timeStep, const SPtr< vf::parallel::Communicator > &comm, const std::string &basePath, const vf::grid_winding::cpu::BoundaryProcessingConfig &config)
Compute subgrid distances and Q-values for the current geometry/grid setup.
void updateInteractor(const real ×tep=0) override
void setQs(const real &timeStep)
void updateInteractor(const real ×tep=0) override
bool setDifferencesToGbObject3D(const SPtr< Block3D > block) override
bool greaterEqual(const T1 &value, const T2 &reference)
bool lessEqual(const T1 &value, const T2 &reference)
bool greater(const T1 &value, const T2 &reference)
bool less(const T1 &value, const T2 &reference)
void ensureBoundaryConditions(const SPtr< BCArray3D > &bcArray, int ix, int iy, int iz, SPtr< BoundaryConditions > &outBc)
void computeSubgridDistancesStandalone(const SPtr< GbTriFaceMesh3D > &surface, const SPtr< Grid3D > &grid, const SPtr< BCSet > &bcSet, const std::vector< SPtr< BC > > &boundaryAdapters, const ::D3Q27Interactor *adapterInteractor, double timeStep, SubgridDistanceStats *stats)
BoundaryProcessingResult processBoundaryData(const SPtr< GbTriFaceMesh3D > &surface, const SPtr< Grid3D > &grid, const SPtr< BCSet > &bcSet, const std::vector< SPtr< BC > > &boundaryAdapters, const ::D3Q27Interactor *adapterInteractor, double timeStep, const SPtr< vf::parallel::Communicator > &comm, const std::string &basePath, const BoundaryProcessingConfig &config)
constexpr float FastWindingDefaultAccuracyScale
Default fast-winding accuracy scale used in computeSolidAngle(...).
bool processBoundaryLink(const TriangleSurfaces &surfaces, const Vec3 &origin, double dx, int cx, int cy, int cz, int direction, PromoteSolidFn &&promoteSolid, ExistingQFn &&existingQ, SetQFn &&setQ, MarkSurfaceFn &&markSurface, GetWindingFn &&getWindingNumber, HandleMissingFn &&handleMissing, SubgridDistanceStats *stats)
QLineCollection buildQLineCollection(const std::vector< double > &coordsRecv, const std::vector< int > &dirRecv, const std::vector< float > &qRecv)
Build a QLineCollection from gathered coordinate / direction / q buffers.
double computeFaceEpsilon(const std::array< double, 3 > &meshMin, const std::array< double, 3 > &meshMax, double cellScale=0.0)
MissingLinkCollection collectMissingLinks(const SubgridDistanceStats &stats, const SPtr< vf::parallel::Communicator > &comm)
constexpr float FastWindingDefaultTolerance
Default comparison tolerance near the threshold.
bool processBoundaryLinkKdTree(const std::vector< kd_tree::Tree< double > * > &kdTrees, Adapter &adapter, const BoundaryLink &link, SubgridDistanceStats *stats=nullptr, const QComputationConfig &cfg={})
Compute Q for a single boundary link using kd-tree based ray casting.
std::vector< kd_tree::Tree< double > * > ensureKdTrees(const std::vector< GbTriFaceMesh3D * > &meshes)
std::size_t skippedNonLocalLinks
float fastWindingThreshold