Skip to content

Commit

Permalink
util: Add function for filtering cells according to number of edges
Browse files Browse the repository at this point in the history
  • Loading branch information
Riccardo Milani committed Apr 9, 2024
1 parent 710c255 commit 1949a15
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 1 deletion.
2 changes: 2 additions & 0 deletions examples/scripts/ex_rae.py
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,8 @@ def limit_line_length(idxs, cells, max_cells_in_line):

# The mesh is hybrid with squares in the BL and triangles else where
squares = [i for i, cell in enumerate(mesh.cells) if len(cell) == 4]
# Or
# squares = CoMMA.filter_cells_by_n_edges(adjMatrix_row_ptr, n_bnd_faces, {4})
foil = [i for i in squares if mesh.boundary_cells[i] > 0]
foil_distance = compute_neighbourhood_wall_distance(
adjMatrix_row_ptr, adjMatrix_col_ind, foil
Expand Down
31 changes: 31 additions & 0 deletions include/CoMMA/Util.h
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,37 @@ inline std::unordered_set<KeyT> d_keys_to_set(
return s_neighbours_of_seed;
}

/** @brief Given the connectivity of the graph, filter cells keeping only those
* with the desired number of edges / neighbours.
* @tparam IndexT Type for indices.
* @tparam IntT Type for integers.
* @param[in] indices Indices of the connectivity of the graph.
* @param[in] n_bnd_faces Number of boundary faces per cell.
* @param[in] allowed Set with the accepted number of edges.
* @param[out] filtered Filtered cells.
*/
template<typename IndexT, typename IntT>
inline void filter_cells_by_n_edges(
const std::vector<IndexT> &indices,
const std::vector<IntT> &n_bnd_faces,
const std::unordered_set<IntT> allowed,
std::vector<IndexT> &filtered
) {
const auto n_cells = static_cast<IndexT>(n_bnd_faces.size());
filtered.clear();
filtered.reserve(n_cells);
//
for (auto c = decltype(n_cells){0}; c < n_cells; ++c) {
if (allowed.find(
static_cast<IntT>(indices[c + 1] - indices[c] + n_bnd_faces[c])
)
!= allowed.end())
filtered.emplace_back(c);
} // for c
//
filtered.shrink_to_fit();
}

/** @brief Compute a neighbourhood-base wall-distance, that is, the distance of
* a given cell from a wall is the number of cells though which the minimum path
* starting from the cell and ending at the wall. For example, in a Cartesian
Expand Down
22 changes: 21 additions & 1 deletion src/CoMMA.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,12 @@

#include <optional>
#include <type_traits>
#include <unordered_set>
#include <vector>

#include "CoMMA/CoMMAConfig.h"
#include "CoMMA/CoMMADefs.h"
#include "CoMMA/Util.h"

#define CoMMA_xstr(s) CoMMA_str(s)
#define CoMMA_str(s) #s
Expand Down Expand Up @@ -189,7 +192,7 @@ PYBIND11_MODULE(CoMMA, module_handle) {
const vector<CoMMAIndexT> &neighs,
const vector<CoMMAIndexT> &wall
) {
vector<CoMMASignedIndexT> dist{};
std::vector<CoMMASignedIndexT> dist{};
compute_neighbourhood_based_wall_distance<CoMMAIndexT, CoMMASignedIndexT>(
neigh_idxs, neighs, wall, dist
);
Expand All @@ -200,6 +203,23 @@ PYBIND11_MODULE(CoMMA, module_handle) {
"neighs"_a,
"wall"_a
);

module_handle.def(
"filter_cells_by_n_edges",
[](
const std::vector<CoMMAIndexT> &indices,
const std::vector<CoMMAIntT> &n_bnd_faces,
const std::unordered_set<CoMMAIntT> allowed
) {
std::vector<CoMMAIndexT> filtered{};
filter_cells_by_n_edges(indices, n_bnd_faces, allowed, filtered);
return filtered;
},
"Filter cells according to their number of edges",
"indices"_a,
"n_bnd_face"_a,
"allowed"_a
);
}

#undef CoMMA_str
Expand Down

0 comments on commit 1949a15

Please sign in to comment.