Vulpes  0.1.0
Versatile and parallel compUtational Library for high-order discontinuous Polytopal Element Simulations
Loading...
Searching...
No Matches
gnn_agglomeration.hpp
Go to the documentation of this file.
1// ----------------------------------------------------------------------
2//
3// Copyright (C) 2024 - 2026 by the Vulpes authors.
4// This file is part of Vulpes.
5//
6// Vulpes is free software; you can redistribute it and/or modify
7// it under the terms of the GNU General Public License as published by
8// the Free Software Foundation, either version 3 of the License, or
9// (at your option) any later version.
10//
11// Vulpes is distributed in the hope that it will be useful,
12// but WITHOUT ANY WARRANTY; without even the implied warranty of
13// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14// General Public License for more details.
15//
16// You should have received a copy of the GNU General Public License
17// along with Vulpes. If not, see <http://www.gnu.org/licenses/>.
18//
19// ----------------------------------------------------------------------
20
21// Author: Matteo Caldana <matteo.caldana@polimi.it>
22
23#ifndef VULPES_GEOMETRY_AGGLOMERATION_GNN_AGGLOMERATION_HPP_
24#define VULPES_GEOMETRY_AGGLOMERATION_GNN_AGGLOMERATION_HPP_
25
26#include <torch/script.h>
27#include <torch/torch.h>
28
29#include <fstream>
30#include <set>
31#include <string>
32#include <vector>
33
34#include <Eigen/Dense>
35
36#include <vtkCellArray.h>
37#include <vtkCellData.h>
38#include <vtkIntArray.h>
39#include <vtkNew.h>
40#include <vtkPoints.h>
41#include <vtkPolyData.h>
42#include <vtkSmartPointer.h>
43#include <vtkUnstructuredGrid.h>
44#include <vtkXMLPolyDataWriter.h>
45#include <vtkXMLUnstructuredGridWriter.h>
46
54
55namespace vulpes {
56namespace geometry {
63enum class SAGEType { Base = 0, Heterogeneous };
64
65namespace internal {
71template <typename T>
72constexpr torch::ScalarType toTorchType() {
73 if constexpr (std::is_same_v<T, float>) {
74 return torch::kFloat32;
75 } else if constexpr (std::is_same_v<T, double>) {
76 return torch::kFloat64;
77 } else if constexpr (std::is_same_v<T, int32_t>) {
78 return torch::kInt32;
79 } else if constexpr (std::is_same_v<T, int64_t>) {
80 return torch::kInt64;
81 } else {
82 static_assert(
83 std::is_same_v<T, void>,
84 "Unsupported type: must be float, double, int32_t, or int64_t");
85 }
86}
87} // namespace internal
88
106template <typename MeshType>
108 public:
112
113 using MeshFloatType = typename MeshType::float_t;
114 using MeshIndexType = typename MeshType::index_t;
115
120
121 using Module = torch::jit::script::Module;
123
124 static constexpr auto s_torch_mesh_float_type =
126 static constexpr auto s_torch_mesh_index_type =
128 static constexpr auto s_torch_class_index_type =
130
131 constexpr static ClassIndexType dim = MeshType::outer_dim;
132
141
142 GNNAgglomeration(const std::string& name, SAGEType SAGE_t);
143
163 std::vector<MeshType> agglomerate(
164 const MeshType& mesh,
165 MethodType method_type,
166 ParamType params,
167 bool return_hierarchy = true,
168 const std::string& segregated_tag = "",
169 const IndVec& subset = {}) override;
170
171 private:
177 const MeshType& /*mesh*/,
178 std::size_t /*n_groups*/,
179 IndexTrackerType& /*indicator*/,
180 const std::vector<bool>& /*agglomerate*/) override {
181 throw std::runtime_error(
182 "Method 'directKWay' does not exist for GNNAgglomeration");
183 }
184
196 IndVec bisectSubGraph(const MeshType& mesh, const IndVec& subgraph) override;
197
207
208 torch::Tensor bisectGraph(const GNNGraph& graph);
209
210 private:
214 torch::Tensor m_feature_tensor;
216
217 Eigen::Matrix<MeshFloatType, -1, -1, Eigen::RowMajor> m_feature_mat;
218};
219
220extern template class GNNAgglomeration<Mesh2D<double, int32_t>>;
221extern template class GNNAgglomeration<Mesh3D<double, int32_t>>;
222
223} // namespace geometry
224} // namespace vulpes
225#endif // VULPES_GEOMETRY_AGGLOMERATION_GNN_AGGLOMERATION_HPP_
Defines an abstract interface for mesh agglomeration strategies.
Definition abs_agg_model.hpp:71
double float_t
Definition abs_agg_model.hpp:73
datastructure::IndexTracker< index_t > IndexTrackerType
Definition abs_agg_model.hpp:78
datastructure::CrsMap< index_t, index_t > MapType
Definition abs_agg_model.hpp:76
std::variant< index_t, float_t > ParamType
Definition abs_agg_model.hpp:77
std::vector< index_t > IndVec
Definition abs_agg_model.hpp:75
int64_t index_t
Definition abs_agg_model.hpp:74
GNN-based agglomeration model (GraphSAGE via TorchScript).
Definition gnn_agglomeration.hpp:107
Cell< MeshType::self_dim, MeshFloatType, MeshIndexType > CellType
Definition gnn_agglomeration.hpp:116
torch::Tensor bisectGraph(const GNNGraph &graph)
Run the TorchScript SAGE model on a graph to obtain a 2-class split.
Definition gnn_agglomeration.cpp:174
typename MeshType::float_t MeshFloatType
Definition gnn_agglomeration.hpp:113
static constexpr auto s_torch_mesh_index_type
Definition gnn_agglomeration.hpp:126
typename AbstractAgglomerationModel< MeshType >::index_t ClassIndexType
Definition gnn_agglomeration.hpp:110
static constexpr auto s_torch_mesh_float_type
Definition gnn_agglomeration.hpp:124
ClassIndexType m_n_kmeans_calls
Definition gnn_agglomeration.hpp:211
typename AbstractAgglomerationModel< MeshType >::float_t ClassFloatType
Definition gnn_agglomeration.hpp:109
Module m_model
Definition gnn_agglomeration.hpp:215
torch::Tensor m_feature_tensor
Definition gnn_agglomeration.hpp:214
typename AbstractAgglomerationModel< MeshType >::MapType MapType
Definition gnn_agglomeration.hpp:111
GNNAgglomeration(const std::string &name, SAGEType SAGE_t)
Construct a GNN agglomerator.
Definition gnn_agglomeration.cpp:36
torch::jit::script::Module Module
Definition gnn_agglomeration.hpp:121
static constexpr auto s_torch_class_index_type
Definition gnn_agglomeration.hpp:128
typename AbstractAgglomerationModel< MeshType >::ParamType ParamType
Definition gnn_agglomeration.hpp:122
IndVec bisectSubGraph(const MeshType &mesh, const IndVec &subgraph) override
Backend implementation of a single bisection operation.
Definition gnn_agglomeration.cpp:138
void directKWay(const MeshType &, std::size_t, IndexTrackerType &, const std::vector< bool > &) override
Direct k-way partition is not supported by the GNN backend.
Definition gnn_agglomeration.hpp:176
Eigen::Matrix< MeshFloatType, -1, -1, Eigen::RowMajor > m_feature_mat
Definition gnn_agglomeration.hpp:217
SAGEType m_sage_t
Definition gnn_agglomeration.hpp:212
GNNGraph m_graph
Definition gnn_agglomeration.hpp:213
typename MeshType::index_t MeshIndexType
Definition gnn_agglomeration.hpp:114
typename AbstractAgglomerationModel< MeshType >::IndexTrackerType IndexTrackerType
Definition gnn_agglomeration.hpp:118
typename AbstractAgglomerationModel< MeshType >::IndVec IndVec
Definition gnn_agglomeration.hpp:117
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.
Definition gnn_agglomeration.cpp:68
static constexpr ClassIndexType dim
Definition gnn_agglomeration.hpp:131
Graph wrapper with Torch tensors for features and edges.
Definition gnn_graph.hpp:46
constexpr torch::ScalarType toTorchType()
Map C++ mesh types (float, double, int32_t, int64_t) to the corresponding Torch scalar type.
Definition gnn_agglomeration.hpp:72
Definition abs_agg_model.cpp:40
SAGEType
Variants of pretrained GraphSAGE models supported by this backend.
Definition gnn_agglomeration.hpp:63
@ Base
Definition gnn_agglomeration.hpp:63
@ Heterogeneous
Definition gnn_agglomeration.hpp:63
MethodType
Definition abs_agg_model.hpp:41
std::conditional_t< Dim==2, Cell2D< FloatType, IndexType >, Cell3D< FloatType, IndexType > > Cell
Definition cell.hpp:267
Definition cell_manager.hpp:38