Chi-Tech
PartitionerPredicate.cc
Go to the documentation of this file.
2
7
8#include "ChiObjectFactory.h"
9
10
12{
13
15
17{
19
20 params.SetGeneralDescription("Field operation that will write to the "
21 "field the result of a partitioning operation");
22 params.SetDocGroup("DocFieldOperation");
23
24 params.AddRequiredParameter<size_t>(
25 "partitioner",
26 "Handle to a GraphPartitioner object to use for parallel partitioning.");
27
28 params.AddRequiredParameter<size_t>(
29 "result_field",
30 "Handle to, or name of, the field function that should "
31 "receive the result of the operation.");
32 params.SetParameterTypeMismatchAllowed("result_field");
33
34 params.AddRequiredParameter<size_t>(
35 "num_partitions", "Number of parts to apply to the partitioning");
36
38 "result_component",
39 0,
40 "Resulting component into which the result will be written.");
41
42 return params;
43}
44
46 : FieldOperation(params),
47 partitioner_(Chi::GetStackItem<chi::GraphPartitioner>(
48 Chi::object_stack,
49 params.GetParamValue<size_t>("partitioner"),
50 __FUNCTION__)),
51 result_field_param_(params.GetParam("result_field")),
52 num_partitions_(params.GetParamValue<size_t>("num_partitions")),
53 result_component_(params.GetParamValue<size_t>("result_component"))
54{
55}
56
58{
59 std::shared_ptr<FieldFunctionGridBased> grid_ff_ptr;
61 {
62 const size_t handle = result_field_param_.GetValue<size_t>();
63 auto ff_ptr = Chi::GetStackItemPtrAsType<FieldFunction>(
64 Chi::field_function_stack, handle, __FUNCTION__);
65 grid_ff_ptr = std::dynamic_pointer_cast<FieldFunctionGridBased>(ff_ptr);
66
68 not grid_ff_ptr,
69 "Could not cast field function to FieldFunctionGridBased");
70 }
72 {
73 const std::string ff_name = result_field_param_.GetValue<std::string>();
74 for (auto& ff_ptr : Chi::field_function_stack)
75 if (ff_ptr->TextName() == ff_name)
76 {
77 grid_ff_ptr = std::dynamic_pointer_cast<FieldFunctionGridBased>(ff_ptr);
78
80 not grid_ff_ptr,
81 "Could not cast field function to FieldFunctionGridBased");
82 }
83 }
84
86 not grid_ff_ptr, "Could not find the associated resulting field function");
87
88 //============================================= Build cell graph and centroids
89 const auto& sdm = grid_ff_ptr->GetSpatialDiscretization();
90 const auto& grid = sdm.Grid();
91
92 typedef std::vector<uint64_t> CellGraphNode;
93 typedef std::vector<CellGraphNode> CellGraph;
94 CellGraph cell_graph;
95 std::vector<chi_mesh::Vector3> cell_centroids;
96
97 const size_t num_local_cells = grid.local_cells.size();
98
99 cell_graph.reserve(num_local_cells);
100 cell_centroids.reserve(num_local_cells);
101 {
102 for (const auto& cell : grid.local_cells)
103 {
104 CellGraphNode cell_graph_node; // <-- Note A
105 for (const auto& face : cell.faces_)
106 if (face.has_neighbor_) cell_graph_node.push_back(face.neighbor_id_);
107
108 cell_graph.push_back(cell_graph_node);
109 cell_centroids.push_back(cell.centroid_);
110 }
111 }
112
113 //============================================= Create partition
114 auto cell_pids = partitioner_.Partition(
115 cell_graph, cell_centroids, static_cast<int>(num_partitions_));
116
117 auto& local_data = grid_ff_ptr->FieldVector();
118 const auto& uk_man = grid_ff_ptr->GetUnknownManager();
119
120 for (const auto& cell : grid.local_cells)
121 {
122 const auto& cell_mapping = sdm.GetCellMapping(cell);
123 const size_t num_nodes = cell_mapping.NumNodes();
124
125 const int64_t cell_pid = cell_pids.at(cell.local_id_);
126
127 for (uint32_t i = 0; i < num_nodes; ++i)
128 {
129 const int64_t dof_map =
130 sdm.MapDOFLocal(cell, i, uk_man, 0, result_component_);
131
132 local_data[dof_map] = static_cast<double>(cell_pid);
133 }
134 } // for cell
135}
136
137} // namespace chi_physics::field_operations
#define ChiLogicalErrorIf(condition, message)
#define ChiInvalidArgumentIf(condition, message)
static std::vector< chi_physics::FieldFunctionPtr > field_function_stack
Definition: chi_runtime.h:92
virtual std::vector< int64_t > Partition(const std::vector< std::vector< uint64_t > > &graph, const std::vector< chi_mesh::Vector3 > &centroids, int number_of_parts)=0
void SetDocGroup(const std::string &doc_group)
void AddRequiredParameter(const std::string &name, const std::string &doc_string)
void SetParameterTypeMismatchAllowed(const std::string &param_name)
Sets a tag for the given parameter that will allow its type to be mismatched upon assignment.
void AddOptionalParameter(const std::string &name, T value, const std::string &doc_string)
void SetGeneralDescription(const std::string &description)
ParameterBlockType Type() const
static chi::InputParameters GetInputParameters()
PartitionerPredicate(const chi::InputParameters &params)
RegisterChiObject(chi_physics::field_operations, FieldCopyOperation)