/*---------------------------------------------------------------------------*\ ========= | \\ / F ield | OpenFOAM: The Open Source CFD Toolbox \\ / O peration | Website: https://openfoam.org \\ / A nd | Copyright (C) 2011-2026 OpenFOAM Foundation \\/ M anipulation | ------------------------------------------------------------------------------- License This file is part of OpenFOAM. OpenFOAM is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. OpenFOAM is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenFOAM. If not, see . \*---------------------------------------------------------------------------*/ #include "sampledTriSurface.H" #include "meshSearch.H" #include "treeDataFace.H" #include "pointInCell.H" #include "OBJstream.H" #include "addToRunTimeSelectionTable.H" // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * // namespace Foam { namespace sampledSurfaces { defineTypeNameAndDebug(triSurface, 0); addToRunTimeSelectionTable ( sampledSurface, triSurface, word ); addBackwardCompatibleToRunTimeSelectionTable ( sampledSurface, triSurface, word, triSurfaceMesh, "triSurfaceMesh" ); //- Private class for finding nearest // Comprising: // - global index // - sqr(distance) typedef Tuple2 nearInfo; class nearestEqOp { public: void operator()(nearInfo& x, const nearInfo& y) const { if (y.first() < x.first()) { x = y; } } }; } } const Foam::NamedEnum < Foam::sampledSurfaces::triSurface::samplingSource, 3 > Foam::sampledSurfaces::triSurface::samplingSourceNames_ { "cells", "insideCells", "boundaryFaces" }; // * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * // const Foam::indexedOctree& Foam::sampledSurfaces::triSurface::nonCoupledboundaryTree() const { // Variant of meshBoundarySearch::boundaryTree() that only does non-coupled // boundary faces. if (!boundaryTreePtr_.valid()) { // all non-coupled boundary faces (not just walls) const polyBoundaryMesh& patches = mesh().boundary(); labelList bndFaces(mesh().nFaces()-mesh().nInternalFaces()); label bndI = 0; forAll(patches, patchi) { const polyPatch& pp = patches[patchi]; if (!pp.coupled()) { forAll(pp, i) { bndFaces[bndI++] = pp.start()+i; } } } bndFaces.setSize(bndI); treeBoundBox overallBb(mesh().points()); overallBb = overallBb.extend(1e-4); boundaryTreePtr_.reset ( new indexedOctree ( treeDataFace // all information needed to search faces ( false, // do not cache bb mesh(), bndFaces // boundary faces only ), overallBb, // overall search domain 8, // maxLevel 10, // leafsize 3.0 // duplicity ) ); } return boundaryTreePtr_(); } // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * // Foam::sampledSurfaces::triSurface::triSurface ( const word& name, const polyMesh& mesh, const word& surfaceName, const samplingSource sampleSource ) : sampledSurface(name, mesh), surface_ ( IOobject ( surfaceName, mesh.time().constant(), searchableSurface::geometryDir(mesh.time()), mesh, IOobject::MUST_READ, IOobject::NO_WRITE, false ) ), sampleSource_(sampleSource), needsUpdate_(true), sampleElements_(0), samplePoints_(0) {} Foam::sampledSurfaces::triSurface::triSurface ( const word& name, const polyMesh& mesh, const dictionary& dict ) : sampledSurface(name, mesh, dict), surface_ ( IOobject ( dict.lookup("surface"), mesh.time().constant(), searchableSurface::geometryDir(mesh.time()), mesh, IOobject::MUST_READ, IOobject::NO_WRITE, false ) ), sampleSource_(samplingSourceNames_[dict.lookup("source")]), needsUpdate_(true), sampleElements_(0), samplePoints_(0) {} Foam::sampledSurfaces::triSurface::triSurface ( const word& name, const polyMesh& mesh, const Foam::triSurface& surface, const word& sampleSourceName ) : sampledSurface(name, mesh), surface_ ( IOobject ( name, mesh.time().constant(), searchableSurface::geometryDir(mesh.time()), mesh, IOobject::NO_READ, IOobject::NO_WRITE, false ), surface ), sampleSource_(samplingSourceNames_[sampleSourceName]), needsUpdate_(true), sampleElements_(0), samplePoints_(0) {} // * * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * // Foam::sampledSurfaces::triSurface::~triSurface() {} // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * // bool Foam::sampledSurfaces::triSurface::needsUpdate() const { return needsUpdate_; } bool Foam::sampledSurfaces::triSurface::update() { if (!needsUpdate_) { return false; } // Find the cells the triangles of the surface are in. // Does approximation by looking at the face centres only const pointField& fc = surface_.faceCentres(); List nearest(fc.size()); // Global numbering for cells/faces - only used to uniquely identify local // elements globalIndex globalCells ( (sampleSource_ == cells || sampleSource_ == insideCells) ? mesh().nCells() : mesh().nFaces() ); forAll(nearest, i) { nearest[i].first() = great; nearest[i].second() = labelMax; } if (sampleSource_ == cells) { // Search for nearest cell const indexedOctree& cellTree = meshSearch::New(mesh(), pointInCellShapes::facePlanes).cellTree(); forAll(fc, triI) { pointIndexHit nearInfo = cellTree.findNearest(fc[triI], sqr(great)); if (nearInfo.hit()) { nearest[triI].first() = magSqr(nearInfo.hitPoint()-fc[triI]); nearest[triI].second() = globalCells.toGlobal(nearInfo.index()); } } } else if (sampleSource_ == insideCells) { // Search for cell containing point const indexedOctree& cellTree = meshSearch::New(mesh(), pointInCellShapes::facePlanes).cellTree(); forAll(fc, triI) { if (cellTree.bb().contains(fc[triI])) { label index = cellTree.findInside ( fc[triI], pointInCellShapes::facePlanes ); if (index != -1) { nearest[triI].first() = 0.0; nearest[triI].second() = globalCells.toGlobal(index); } } } } else { // Search on all non-coupled boundary faces const indexedOctree& bTree = nonCoupledboundaryTree(); forAll(fc, triI) { pointIndexHit nearInfo = bTree.findNearest ( fc[triI], sqr(great) ); if (nearInfo.hit()) { nearest[triI].first() = magSqr(nearInfo.hitPoint()-fc[triI]); nearest[triI].second() = globalCells.toGlobal ( bTree.shapes().faceLabels()[nearInfo.index()] ); } } } // See which processor has the nearest. Mark and subset // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Pstream::listCombineGather(nearest, nearestEqOp()); Pstream::listCombineScatter(nearest); labelList cellOrFaceLabels(fc.size(), -1); label nFound = 0; forAll(nearest, triI) { if (nearest[triI].second() == labelMax) { // Not found on any processor. How to map? } else if (globalCells.isLocal(nearest[triI].second())) { cellOrFaceLabels[triI] = globalCells.toLocal ( nearest[triI].second() ); nFound++; } } if (debug) { Pout<< "Local out of faces:" << cellOrFaceLabels.size() << " keeping:" << nFound << endl; } // Now subset the surface. Do not use triSurface::subsetMesh since requires // original surface to be in compact numbering. const Foam::triSurface& s = surface_; // Compact to original triangle labelList faceMap(s.size()); // Compact to original points labelList pointMap(s.points().size()); // From original point to compact points labelList reversePointMap(s.points().size(), -1); { label newPointi = 0; label newFacei = 0; forAll(s, facei) { if (cellOrFaceLabels[facei] != -1) { faceMap[newFacei++] = facei; const triSurface::FaceType& f = s[facei]; forAll(f, fp) { if (reversePointMap[f[fp]] == -1) { pointMap[newPointi] = f[fp]; reversePointMap[f[fp]] = newPointi++; } } } } faceMap.setSize(newFacei); pointMap.setSize(newPointi); } // Subset cellOrFaceLabels cellOrFaceLabels = UIndirectList