Vulpes  0.1.0
Versatile and parallel compUtational Library for high-order discontinuous Polytopal Element Simulations
Loading...
Searching...
No Matches
vulpes::geometry::KMeansAgglomeration< MeshType > Class Template Reference

Agglomeration model that clusters cell barycenters with K-Means. More...

#include <kmeans_agglomeration.hpp>

Inheritance diagram for vulpes::geometry::KMeansAgglomeration< MeshType >:
[legend]
Collaboration diagram for vulpes::geometry::KMeansAgglomeration< MeshType >:
[legend]

Public Types

using ClassFloatType = typename AbstractAgglomerationModel<MeshType>::float_t
using ClassIndexType = typename AbstractAgglomerationModel<MeshType>::index_t
using MapType = typename AbstractAgglomerationModel<MeshType>::MapType
using MeshFloatType = typename MeshType::float_t
using MeshIndexType = typename MeshType::index_t
using CellType = Cell<MeshType::self_dim, MeshFloatType, MeshIndexType>
using IndVec = typename AbstractAgglomerationModel<MeshType>::IndVec
using MatType
using IndexTrackerType
using ParamType = typename AbstractAgglomerationModel<MeshType>::ParamType
using float_t = double
using index_t = int64_t

Public Member Functions

 KMeansAgglomeration (KMeansAlgType alg_t=KMeansAlgType::Base, size_t max_iter=1e6, ClassFloatType tol=1e-6, size_t seed=0)
 Construct a K-Means agglomerator.
std::vector< MeshType > agglomerate (const MeshType &mesh, MethodType method_type, ParamType params, bool return_hierarchy=true, const std::string &segregated_tag="", const IndVec &subset={}) override
 Agglomerate a mesh using a selected high-level strategy.
void directKWay (const MeshType &mesh, std::size_t n_groups, IndexTrackerType &indicator, const std::vector< bool > &agglomerate) override
 Direct k-way partition via K-Means on barycenters.
std::vector< IndVec > multFactor (const MeshType &mesh, const float_t target_h, IndexTrackerType &indicator, const std::vector< bool > &agglomerate)
 Partitions the mesh based on a multiplicative factor strategy.
std::vector< IndVec > bisectNRef (const MeshType &mesh, const index_t n_refs, IndexTrackerType &indicator, const std::vector< bool > &agglomerate)
 Applies recursive bisection n_refs times.

Static Public Attributes

static constexpr ClassIndexType dim = MeshType::self_dim

Protected Member Functions

std::vector< IndVec > invokeMethod (const MeshType &mesh, MethodType method_type, ParamType params, const std::string &segregation_tag, const IndVec &subset)
 Dispatches to the correct agglomeration method to compute cell labels.
void connectedComponents (const IndVec &subset, IndexTrackerType &indicator, index_t &max_id)
 Splits a set of cells into its connected components.
void groupToBisection (const IndVec &subset, const IndVec &classes, IndexTrackerType &indicator, index_t new_id)
 Post-processes a binary labeling to ensure two connected partitions.

Protected Attributes

MapType m_dual_map
 The dual graph of the mesh, cached for performance.

Private Member Functions

IndVec bisectSubGraph (const MeshType &mesh, const IndVec &subgraph) override
 Bisect a subgraph by clustering barycenters with 2-means.
IndVec splitSubsetKWay (const MeshType &mesh, std::size_t n_groups, const IndVec &subgraph)

Private Attributes

MatType m_barycenters_mat
 Cached barycenter matrix used in the current call.
KMeansAlgType m_alg_t
 Selected K-Means algorithm family.
size_t m_max_iter
 Maximum number of iterations.
float_t m_tol
 Convergence tolerance on centroids.
size_t m_seed
 Random seed for initialization.

Detailed Description

template<typename MeshType>
class vulpes::geometry::KMeansAgglomeration< MeshType >

Agglomeration model that clusters cell barycenters with K-Means.

Implements the low-level split primitive bisectSubGraph() via 2-means and a direct directKWay() partition via k-means on the selected cells. High-level strategies (e.g., BisectNRef, MultFactor) are inherited from AbstractAgglomerationModel.

See also
AbstractAgglomerationModel, MethodType, KMeansAlgType, vulpes::geometry::kmeans

Member Typedef Documentation

◆ CellType

template<typename MeshType>
using vulpes::geometry::KMeansAgglomeration< MeshType >::CellType = Cell<MeshType::self_dim, MeshFloatType, MeshIndexType>

◆ ClassFloatType

template<typename MeshType>
using vulpes::geometry::KMeansAgglomeration< MeshType >::ClassFloatType = typename AbstractAgglomerationModel<MeshType>::float_t

◆ ClassIndexType

template<typename MeshType>
using vulpes::geometry::KMeansAgglomeration< MeshType >::ClassIndexType = typename AbstractAgglomerationModel<MeshType>::index_t

◆ float_t

template<typename MeshType>
using vulpes::geometry::AbstractAgglomerationModel< MeshType >::float_t = double
inherited

◆ index_t

template<typename MeshType>
using vulpes::geometry::AbstractAgglomerationModel< MeshType >::index_t = int64_t
inherited

◆ IndexTrackerType

template<typename MeshType>
using vulpes::geometry::KMeansAgglomeration< MeshType >::IndexTrackerType
Initial value:
datastructure::IndexTracker< index_t > IndexTrackerType
Definition abs_agg_model.hpp:78

◆ IndVec

template<typename MeshType>
using vulpes::geometry::KMeansAgglomeration< MeshType >::IndVec = typename AbstractAgglomerationModel<MeshType>::IndVec

◆ MapType

template<typename MeshType>
using vulpes::geometry::KMeansAgglomeration< MeshType >::MapType = typename AbstractAgglomerationModel<MeshType>::MapType

◆ MatType

template<typename MeshType>
using vulpes::geometry::KMeansAgglomeration< MeshType >::MatType
Initial value:
Eigen::Matrix<ClassFloatType, MeshType::self_dim, Eigen::Dynamic>

◆ MeshFloatType

template<typename MeshType>
using vulpes::geometry::KMeansAgglomeration< MeshType >::MeshFloatType = typename MeshType::float_t

◆ MeshIndexType

template<typename MeshType>
using vulpes::geometry::KMeansAgglomeration< MeshType >::MeshIndexType = typename MeshType::index_t

◆ ParamType

template<typename MeshType>
using vulpes::geometry::KMeansAgglomeration< MeshType >::ParamType = typename AbstractAgglomerationModel<MeshType>::ParamType

Constructor & Destructor Documentation

◆ KMeansAgglomeration()

template<typename MeshType>
vulpes::geometry::KMeansAgglomeration< MeshType >::KMeansAgglomeration ( KMeansAlgType alg_t = KMeansAlgType::Base,
size_t max_iter = 1e6,
ClassFloatType tol = 1e-6,
size_t seed = 0 )
inlineexplicit

Construct a K-Means agglomerator.

Parameters
alg_tAlgorithm variant (Base or Parallel). Default: Parallel.
max_iterMaximum k-means iterations (1e6 by default).
tolConvergence tolerance on centroid updates.
seedRNG seed for centroid initialization.

Member Function Documentation

◆ agglomerate()

template<typename MeshType>
std::vector< MeshType > vulpes::geometry::KMeansAgglomeration< MeshType >::agglomerate ( const MeshType & mesh,
MethodType method_type,
ParamType params,
bool return_hierarchy = true,
const std::string & segregated_tag = "",
const IndVec & subset = {} )
overridevirtual

Agglomerate a mesh using a selected high-level strategy.

Delegates to the base-class dispatcher and uses K-Means for the underlying splits/partitions.

Parameters
meshInput mesh.
method_typeStrategy to apply (see MethodType).
paramsStrategy parameter (k, n_refs, or mult_fact).
return_hierarchyIf true, returns all intermediate meshes from recursive strategies. If false, returns only the final agglomerated mesh.
segregated_tagIf non-empty, specifies the mesh tag for segregated agglomeration, where each tagged region is processed independently.
subsetOptional list of cell IDs to agglomerate. If empty, the entire mesh is processed.
Returns
Agglomerated mesh.
See also
AbstractAgglomerationModel::agglomerate()

Implements vulpes::geometry::AbstractAgglomerationModel< MeshType >.

◆ bisectNRef()

template<typename MeshType>
std::vector< typename AbstractAgglomerationModel< MeshType >::IndVec > vulpes::geometry::AbstractAgglomerationModel< MeshType >::bisectNRef ( const MeshType & mesh,
const index_t n_refs,
IndexTrackerType & indicator,
const std::vector< bool > & agglomerate )
inherited

Applies recursive bisection n_refs times.

This method takes an initial partitioning and refines it by applying the bisectSubGraph() primitive recursively for n_refs levels. Each step splits a partition into two. The base cases are finalized by ensuring connectivity via connectedComponents().

Parameters
meshThe input mesh.
n_refsThe number of recursive bisection levels to apply ( \(n_{refs} \ge 0\)).
[in,out]indicatorThe label tracker, which is updated with the results.
agglomerateA boolean vector indicating which initial classes in indicator are eligible for bisection.
Returns
A hierarchy of label vectors, capturing the state of partitioning at each level of bisection.
See also
bisectSubGraph(), connectedComponents()

◆ bisectSubGraph()

template<typename MeshType>
KMeansAgglomeration< MeshType >::IndVec vulpes::geometry::KMeansAgglomeration< MeshType >::bisectSubGraph ( const MeshType & mesh,
const IndVec & subgraph )
overrideprivatevirtual

Bisect a subgraph by clustering barycenters with 2-means.

Computes the barycenter matrix for subset and runs k-means with k=2. One class is assigned to new_id, the other retains the previous id in indicator.

Parameters
meshThe input mesh.
subgraphA vector of cell IDs to be split.
Returns
A vector of binary labels (e.g., 0s and 1s) of the same size as subgraph, assigning each cell to one of the two new partitions.
Precondition
subgraph.size() >= 2.

Implements vulpes::geometry::AbstractAgglomerationModel< MeshType >.

◆ connectedComponents()

template<typename MeshType>
void vulpes::geometry::AbstractAgglomerationModel< MeshType >::connectedComponents ( const IndVec & subset,
IndexTrackerType & indicator,
index_t & max_id )
protectedinherited

Splits a set of cells into its connected components.

This function examines the dual graph restricted to the cells in subset. If the subgraph is disconnected, each connected component is assigned a new, unique label. This is often used as a finalization step in recursive strategies.

Parameters
subsetThe set of cell IDs to process.
[in,out]indicatorThe global label tracker, which will be updated.
[in,out]max_idThe last used label ID; it is incremented for each new component found.

◆ directKWay()

template<typename MeshType>
void vulpes::geometry::KMeansAgglomeration< MeshType >::directKWay ( const MeshType & mesh,
std::size_t n_groups,
IndexTrackerType & indicator,
const std::vector< bool > & agglomerate )
overridevirtual

Direct k-way partition via K-Means on barycenters.

Runs k-means with k=k on the barycenters of subset (or all cells) and returns a dense label vector.

Parameters
meshThe input mesh.
n_groupsThe target number of partitions.
[in,out]indicatorThe label tracker. It can be pre-filled to provide a warm start. It will be updated with the final labels.
agglomerateA boolean vector where the i-th element indicates if the i-th class in the initial indicator should be re-partitioned.

Implements vulpes::geometry::AbstractAgglomerationModel< MeshType >.

◆ groupToBisection()

template<typename MeshType>
void vulpes::geometry::AbstractAgglomerationModel< MeshType >::groupToBisection ( const IndVec & subset,
const IndVec & classes,
IndexTrackerType & indicator,
index_t new_id )
protectedinherited

Post-processes a binary labeling to ensure two connected partitions.

Given a provisional two-class labeling for a subset of cells, this method refines the labels to ensure that the resulting two partitions are each internally connected. It can relabel "islands" of cells to join the surrounding partition.

Parameters
subsetThe set of cell IDs under consideration.
classesA vector of provisional binary labels for the cells in subset.
[in,out]indicatorThe global label tracker to update with the final bisection.
new_idThe fresh label ID to be assigned to one of the two final partitions.

◆ invokeMethod()

template<typename MeshType>
std::vector< typename AbstractAgglomerationModel< MeshType >::IndVec > vulpes::geometry::AbstractAgglomerationModel< MeshType >::invokeMethod ( const MeshType & mesh,
MethodType method_type,
ParamType params,
const std::string & segregation_tag,
const IndVec & subset )
protectedinherited

Dispatches to the correct agglomeration method to compute cell labels.

This is a wrapper function typically used by agglomerate(). It selects the appropriate backend method based on method_type and restricts the computation to subset if it is non-empty.

Parameters
meshThe input mesh.
method_typeThe strategy to apply.
paramsThe parameters for the chosen strategy.
segregation_tagIf non-empty, the mesh tag to use for segregated processing.
subsetAn optional subset of cell IDs to operate on.
Returns
A hierarchy of label vectors. For cells not in subset, the label is typically unchanged or set to a sentinel value (e.g., -1).

◆ multFactor()

template<typename MeshType>
std::vector< typename AbstractAgglomerationModel< MeshType >::IndVec > vulpes::geometry::AbstractAgglomerationModel< MeshType >::multFactor ( const MeshType & mesh,
const float_t target_h,
IndexTrackerType & indicator,
const std::vector< bool > & agglomerate )
inherited

Partitions the mesh based on a multiplicative factor strategy.

This method recursively bisects any group whose vertex-vertex diameter exceeds the target_h. After geometric splitting, each resulting group is finalized by splitting it into its connected components. It uses bisectSubGraph() as the core splitting primitive.

Parameters
meshThe input mesh.
target_hThe target characteristic length. Any group with a diameter larger than this will be split.
[in,out]indicatorThe label tracker, which is updated with the results.
agglomerateA boolean vector indicating which initial classes in indicator are eligible for splitting.
Returns
A hierarchy of label vectors, where each vector represents the partitioning at a successive level of refinement.
Precondition
target_h must be a positive value.
See also
connectedComponents(), bisectSubGraph()

◆ splitSubsetKWay()

template<typename MeshType>
KMeansAgglomeration< MeshType >::IndVec vulpes::geometry::KMeansAgglomeration< MeshType >::splitSubsetKWay ( const MeshType & mesh,
std::size_t n_groups,
const IndVec & subgraph )
private

Member Data Documentation

◆ dim

template<typename MeshType>
ClassIndexType vulpes::geometry::KMeansAgglomeration< MeshType >::dim = MeshType::self_dim
staticconstexpr

◆ m_alg_t

template<typename MeshType>
KMeansAlgType vulpes::geometry::KMeansAgglomeration< MeshType >::m_alg_t
private

Selected K-Means algorithm family.

◆ m_barycenters_mat

template<typename MeshType>
MatType vulpes::geometry::KMeansAgglomeration< MeshType >::m_barycenters_mat
private

Cached barycenter matrix used in the current call.

◆ m_dual_map

template<typename MeshType>
MapType vulpes::geometry::AbstractAgglomerationModel< MeshType >::m_dual_map
protectedinherited

The dual graph of the mesh, cached for performance.

◆ m_max_iter

template<typename MeshType>
size_t vulpes::geometry::KMeansAgglomeration< MeshType >::m_max_iter
private

Maximum number of iterations.

◆ m_seed

template<typename MeshType>
size_t vulpes::geometry::KMeansAgglomeration< MeshType >::m_seed
private

Random seed for initialization.

◆ m_tol

template<typename MeshType>
float_t vulpes::geometry::KMeansAgglomeration< MeshType >::m_tol
private

Convergence tolerance on centroids.


The documentation for this class was generated from the following files: