Commit 60e26a7a authored by Tomáš Jakubec's avatar Tomáš Jakubec
Browse files

mesh signature - a hash code characteristic to the mesh. This gives a

way how to detect whether the mesh changed for example for export
purpose.
parent 5b76df65
Loading
Loading
Loading
Loading
+9 −2
Original line number Original line Diff line number Diff line
@@ -84,6 +84,7 @@ void cube(UnstructuredMesh<3, size_t, double, 6>& mesh3){


        mesh3.getCells().push_back(0);
        mesh3.getCells().push_back(0);
        mesh3.getCells().at(0).setBoundaryElementIndex(3);
        mesh3.getCells().at(0).setBoundaryElementIndex(3);
        mesh3.updateSignature();
    DBGCHECK;
    DBGCHECK;
}
}


@@ -173,6 +174,7 @@ void twoPrisms(UnstructuredMesh<3, size_t, double, 6>& mesh3){
        mesh3.getCells().push_back(1);
        mesh3.getCells().push_back(1);
        mesh3.getCells().at(1).setBoundaryElementIndex(1);
        mesh3.getCells().at(1).setBoundaryElementIndex(1);


        mesh3.updateSignature();
    DBGCHECK;
    DBGCHECK;
}
}


@@ -262,6 +264,7 @@ void twoDeformedPrisms(UnstructuredMesh<3, size_t, double, 6>& mesh3){
        mesh3.getCells().push_back(1);
        mesh3.getCells().push_back(1);
        mesh3.getCells().at(1).setBoundaryElementIndex(1);
        mesh3.getCells().at(1).setBoundaryElementIndex(1);


        mesh3.updateSignature();
    DBGCHECK;
    DBGCHECK;
}
}


@@ -310,6 +313,9 @@ void testMesh2D() {
    mesh.getCells().at(1).setBoundaryElementIndex(2);
    mesh.getCells().at(1).setBoundaryElementIndex(2);
    mesh.getCells().at(1).setIndex(1);
    mesh.getCells().at(1).setIndex(1);


    mesh.updateSignature();

    DBGVAR(mesh.getSignature());
    mesh.initializeCenters();
    mesh.initializeCenters();




@@ -451,7 +457,7 @@ void testMesh3D() {
    }
    }





    DBGVAR(mesh3.getSignature());


    DBGMSG("mesh conatiner test");
    DBGMSG("mesh conatiner test");
    MeshDataContainer<double, 3,2,1,0> cont(mesh3);
    MeshDataContainer<double, 3,2,1,0> cont(mesh3);
@@ -753,6 +759,7 @@ void test3DMeshDeformedPrisms() {
    UnstructuredMesh<3, size_t, double, 6> mesh3;
    UnstructuredMesh<3, size_t, double, 6> mesh3;
    twoDeformedPrisms(mesh3);
    twoDeformedPrisms(mesh3);


    DBGVAR(mesh3.getSignature());
    //_ComputeCenters<1,3, 3,2,1>::compute<size_t, double, 6>(centers, mesh3);
    //_ComputeCenters<1,3, 3,2,1>::compute<size_t, double, 6>(centers, mesh3);
    auto centers = ComputeCenters<DEFAULT>(mesh3);
    auto centers = ComputeCenters<DEFAULT>(mesh3);


@@ -976,7 +983,7 @@ void testFPMA_poly(){
int main()
int main()
{
{


    //testMesh2D();
    testMesh2D();
    //testMesh2DLoadAndWrite();
    //testMesh2DLoadAndWrite();
    testMesh3D();
    testMesh3D();
    test3DMeshDeformedPrisms();
    test3DMeshDeformedPrisms();
+95 −2
Original line number Original line Diff line number Diff line
@@ -47,13 +47,17 @@ private:
    };
    };






private:
private:
    _MeshElements<Dimension> innerElements;
    _MeshElements<Dimension> innerElements;
    std::vector<Cell> BoundaryCells;
    std::vector<Cell> BoundaryCells;
    //_MeshElements<Dimension> boundaryElements;
    //_MeshElements<Dimension> boundaryElements;


    /**
     * @brief Hash signature of the mash elements.
     * Use to detect changes in mesh.
     */
    size_t meshSignature;



public:
public:
    template<unsigned int dim>
    template<unsigned int dim>
@@ -207,6 +211,95 @@ public:
        }
        }
    };
    };




    /*
     * Signature computation
     */
private:

    template<unsigned int Dim = Dimension, typename Dummy = void>
    struct hashOfMeshElements{
        static size_t hash(MeshElements<Dimension, IndexType, Real, Reserve...>& mesh){
            std::hash<IndexType> indexHasher;

            // Hash of generic element eg. face
            size_t elemHash = mesh.getElements<Dim>().size();
            for(auto& element : mesh.getElements<Dim>()) {
                for (auto& subElement : element.getSubelements()) {
                    elemHash ^= indexHasher(subElement.index);
                }
            }
            return elemHash ^ hashOfMeshElements<Dim - 1>::hash(mesh);
        }
    };


    /**
     * @brief The hashOfMeshElements<Dimension, Dummy> struct hashes the cell information.
     */
    template<typename Dummy>
    struct hashOfMeshElements<Dimension, Dummy>{
        static size_t hash(MeshElements<Dimension, IndexType, Real, Reserve...>& mesh){
            std::hash<IndexType> indexHasher;

            // Hash of cells
            size_t cHash = indexHasher(mesh.getCells().size());
            for(auto& cell : mesh.getCells()) {
                cHash ^= indexHasher(cell.getBoundaryElementIndex());
            }
            return cHash ^ hashOfMeshElements<Dimension -1>::hash(mesh);
        }
    };

    template<typename Dummy>
    struct hashOfMeshElements<1, Dummy>{
        static size_t hash(MeshElements<Dimension, IndexType, Real, Reserve...>& mesh){
            std::hash<IndexType> indexHasher;

            // Hash of cells
            size_t eHash = indexHasher(mesh.getEdges().size());
            for(auto& edge : mesh.getEdges()) {
                eHash ^= edge.getVertexAIndex();
                eHash ^= edge.getVertexBIndex();
            }
            return eHash ^ hashOfMeshElements<0>::hash(mesh);
        }
    };


    template<typename Dummy>
    struct hashOfMeshElements<0, Dummy>{
        static size_t hash(MeshElements<Dimension, IndexType, Real, Reserve...>& mesh){
            std::hash<Real> vertexHasher;
            std::hash<IndexType> indexHasher;

            // Hash of vertices
            size_t vHash = indexHasher(mesh.getVertices().size());
            for(auto& vertex : mesh.getVertices()) {
                for(unsigned int i = 0; i < vertex.size(); i++) {
                    vHash ^= vertexHasher(vertex[i]);
                    //vHash >>= 2;
                }
            }
            return vHash;
        }
    };

public:

    size_t updateSignature() {

        meshSignature = hashOfMeshElements<Dimension>::hash(*this);

        return meshSignature;

    }

    size_t getSignature() const {
        return meshSignature;
    }

public:
public:
    template<unsigned int ElementDim, typename Dummy = void>
    template<unsigned int ElementDim, typename Dummy = void>
    class MeshElementWrap {
    class MeshElementWrap {
+2 −0
Original line number Original line Diff line number Diff line
@@ -144,6 +144,8 @@ public:


        loadCells(ist, mesh);
        loadCells(ist, mesh);


        mesh.updateSignature();

    }
    }




+1 −0
Original line number Original line Diff line number Diff line
@@ -447,6 +447,7 @@ public:
            DBGTRY(loadCellTypes(ist, mesh);)
            DBGTRY(loadCellTypes(ist, mesh);)
        }
        }


        mesh.updateSignature();
    }
    }




+3 −1
Original line number Original line Diff line number Diff line
@@ -52,6 +52,7 @@ public:
    static size_t computeHash(MeshElements<MeshDimension, IndexType, Real, Reserve...>& mesh){
    static size_t computeHash(MeshElements<MeshDimension, IndexType, Real, Reserve...>& mesh){


        // A vector of ones that simplifies the sum of coordinates
        // A vector of ones that simplifies the sum of coordinates
        /*
        Vertex<MeshDimension, Real> ones;
        Vertex<MeshDimension, Real> ones;
        for(unsigned int dim = 0; dim < MeshDimension; dim++){
        for(unsigned int dim = 0; dim < MeshDimension; dim++){
            ones[dim] = 1.0;
            ones[dim] = 1.0;
@@ -67,8 +68,9 @@ public:
        HashUni<IndexType, Real> uni;
        HashUni<IndexType, Real> uni;
        uni.data = {numberOfElements, totalVert};
        uni.data = {numberOfElements, totalVert};
        std::hash<std::string> hasher;
        std::hash<std::string> hasher;
        return hasher(uni.bytes);
        return hasher(uni.bytes);*/
        //return hasher(std::to_string(totalVert)+";"+std::to_string(numberOfElements));
        //return hasher(std::to_string(totalVert)+";"+std::to_string(numberOfElements));
        return mesh.updateSignature();
    }
    }
};
};