precice / precice

A coupling library for partitioned multi-physics simulations, including, but not restricted to fluid-structure interaction and conjugate heat transfer simulations.
https://precice.org/
GNU Lesser General Public License v3.0
732 stars 177 forks source link

Generalize Mesh adding and filtering #506

Open fsimonis opened 5 years ago

fsimonis commented 5 years ago

We currently have slight variations of the same code that handles adding one mesh to another.

  1. void Mesh::addMesh(Mesh const& diff); adds the diff to the current mesh.
  2. void ReceivedPartition::filterMesh(mesh::Mesh &filteredMesh, const bool filterByBB) This adds the internal mesh to filteredMesh and filters vertices based on a predicate: tagged vertices, or vertices inside a bounding-box.

Both functions can be generalized to:

// Generalized version filtering vertices based on a given unary predicate.
template<typename UnaryPredicate>
void Mesh::addMesh(Mesh const& other, UnaryPredicate p);

// Version that simply adds the Mesh
void Mesh::addMesh(Mesh const& other) {
    addMesh(other, [](mesh::Vertex const &) { return true; });
}

// The new possible implementation.
void ReceivedPartition::filterMesh(mesh::Mesh &filteredMesh, const bool filterByBB) {
    if (filterByBB) {
        filteredMesh.addMesh(_mesh,
            [this](mesh::Vertex const & v){ return this->isVertexinBB(v);});
    } else {
        filteredMesh.addMesh(_mesh, 
            [](mesh::Vertex const & v){ return v.isTagged();});
    }
}

This makes the code DRY, easier to maintain and allows us to optimize a single function.

fsimonis commented 1 year ago

Once we implement #1322, this issue will become drastically simpler as there is no need to filter by edge IDs.