Skip to content

Instantly share code, notes, and snippets.

@hertzsprung
Created March 2, 2017 16:22
Show Gist options
  • Save hertzsprung/dd14ad39e5ad03b8e16dcd8f92ab2ab1 to your computer and use it in GitHub Desktop.
Save hertzsprung/dd14ad39e5ad03b8e16dcd8f92ab2ab1 to your computer and use it in GitHub Desktop.
OpenFOAM application to find faces that share points (vertices) with other faces
#include "fvCFD.H"
using namespace fv;
int main(int argc, char *argv[])
{
# include "setRootCase.H"
# include "createTime.H"
# include "createMesh.H"
for (label faceI = 0; faceI < mesh.nInternalFaces(); faceI++)
{
const face& f = mesh.faces()[faceI];
labelList connectedFaceList;
forAll(f, pointForFaceI)
{
const label pointI = f[pointForFaceI];
const labelList& connectedFaces = mesh.pointFaces()[pointI];
forAll(connectedFaces, i)
{
connectedFaceList.append(connectedFaces[i]);
}
}
HashSet<label, Hash<label>> connectedFaceSet(connectedFaceList);
Info << "faces connected to faceI " << faceI << " are " << connectedFaceSet << endl;
}
}
@olesenm
Copy link

olesenm commented Mar 16, 2017

You could use a PackedBoolList instead of a HashSet (you'll need to recheck the code - ie, untested).
But the important thing is to have your hashing or bool-list to accumulate the values and build the list from that. You should not build the list and then use a hash or whatever to filter afterward.

PackedBoolList connectedFaces(mesh.nInternalFaces()); // scratch space - allocate once

const labelListList& pointFaces = mesh.pointFaces();  // invariant
for (label facei = 0; facei < mesh.nInternalFaces(); ++facei)
{
    const face& f = mesh.faces()[facei];
    connectedFaces.reset();    // reset all entries to false, leaving size unchanged

    forAll(f, fp)
    {
        connectedFaces.set(pointFaces[f[fp]]);
    }

    labelList facesUsed(connectedFaces.used());
    Info<< "faces connected to face " << facei << " = " << facesUsed << endl;
}

If it works (and you like it), it would be good to update the gist and tweet so that people can find it.

/mark

Using a HashSet (perhaps not here, but somewhere else), now looks almost the same.
For this particular case I think that using a hash will be better than the PackedBoolList, since the number of faces connected to any particular face would be rather small. The PackedBoolList storage would require (nInternalFaces() / sizeof(int)) storage and the HashSet would use less. However, the hasher will need a few more operations (see Hasher.C) to calculate an index compared to a PackedBoolList. I'd be interested to see which speed difference you can observe (on which size mesh).

labelHashSet connectedFaces; // scratch space - allocate once

const labelListList& pointFaces = mesh.pointFaces();  // invariant
for (label facei = 0; facei < mesh.nInternalFaces(); ++facei)
{
    const face& f = mesh.faces()[facei];
    connectedFaces.clear();

    forAll(f, fp)
    {
        connectedFaces.set(pointFaces[f[fp]]);
    }

    labelList facesUsed(connectedFaces.sortedToc());
    Info<< "faces connected to face " << facei << " = " << facesUsed << endl;
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment