Chi-Tech
SPDS.cc
Go to the documentation of this file.
1#include "SPDS.h"
2
5
6#include "chi_runtime.h"
7#include "chi_log.h"
8#include "chi_mpi.h"
10#include "utils/chi_timer.h"
11
12#include <algorithm>
13
14// ###################################################################
15/** Given a location J index, maps to a predecessor location.*/
17{
18 for (int i = 0; i < location_dependencies_.size(); i++)
19 {
20 if (location_dependencies_[i] == locJ) { return i; }
21 }
22
23 for (int i = 0; i < delayed_location_dependencies_.size(); i++)
24 {
25 if (delayed_location_dependencies_[i] == locJ) { return -(i + 1); }
26 }
27
29 << "SPDS Invalid mapping encountered in MapLocJToPrelocI.";
30 Chi::Exit(EXIT_FAILURE);
31 return 0;
32}
33
34// ###################################################################
35/** Given a location J index, maps to a dependent location.*/
37{
38 for (int i = 0; i < location_successors_.size(); i++)
39 {
40 if (location_successors_[i] == locJ) { return i; }
41 }
42
44 << "SPDS Invalid mapping encountered in MapLocJToDeplocI.";
45 Chi::Exit(EXIT_FAILURE);
46 return 0;
47}
48
49// ###################################################################
50/**Populates cell relationships*/
52 const chi_mesh::Vector3& omega,
53 std::set<int>& location_dependencies,
54 std::set<int>& location_successors,
55 std::vector<std::set<std::pair<int, double>>>& cell_successors)
56{
57 constexpr double tolerance = 1.0e-16;
58
59 constexpr auto FOPARALLEL = FaceOrientation::PARALLEL;
60 constexpr auto FOINCOMING = FaceOrientation::INCOMING;
61 constexpr auto FOOUTGOING = FaceOrientation::OUTGOING;
62
63 cell_face_orientations_.assign(grid_.local_cells.size(), {});
64 for (auto& cell : grid_.local_cells)
65 cell_face_orientations_[cell.local_id_].assign(cell.faces_.size(),
66 FOPARALLEL);
67
68 for (auto& cell : grid_.local_cells)
69 {
70 size_t f = 0;
71 for (auto& face : cell.faces_)
72 {
73 //======================================= Determine if the face
74 // is incident
75 FaceOrientation orientation = FOPARALLEL;
76 const double mu = omega.Dot(face.normal_);
77
78 bool owns_face = true;
79 if (face.has_neighbor_ and cell.global_id_ > face.neighbor_id_)
80 owns_face = false;
81
82 if (owns_face)
83 {
84 // clang-format off
85 if (mu > tolerance) orientation = FOOUTGOING;
86 else if (mu < tolerance) orientation = FOINCOMING;
87
88 cell_face_orientations_[cell.local_id_][f] = orientation;
89
90 if (face.has_neighbor_ and grid_.IsCellLocal(face.neighbor_id_))
91 {
92 const auto& adj_cell = grid_.cells[face.neighbor_id_];
93 const auto ass_face = face.GetNeighborAssociatedFace(grid_);
94 auto& adj_face_ori =
95 cell_face_orientations_[adj_cell.local_id_][ass_face];
96
97 switch (orientation)
98 {
99 case FOPARALLEL: adj_face_ori = FOPARALLEL; break;
100 case FOINCOMING: adj_face_ori = FOOUTGOING; break;
101 case FOOUTGOING: adj_face_ori = FOINCOMING; break;
102 }
103 }
104 // clang-format on
105 } // if face owned
106 else if (face.has_neighbor_ and not grid_.IsCellLocal(face.neighbor_id_))
107 {
108 const auto& adj_cell = grid_.cells[face.neighbor_id_];
109 const auto ass_face = face.GetNeighborAssociatedFace(grid_);
110 const auto& adj_face = adj_cell.faces_[ass_face];
111
112 auto& cur_face_ori = cell_face_orientations_[cell.local_id_][f];
113
114 const double adj_mu = omega.Dot(adj_face.normal_);
115 if (adj_mu > tolerance) orientation = FOOUTGOING;
116 else if (adj_mu < tolerance)
117 orientation = FOINCOMING;
118
119 switch (orientation)
120 {
121 case FOPARALLEL:
122 cur_face_ori = FOPARALLEL;
123 break;
124 case FOINCOMING:
125 cur_face_ori = FOOUTGOING;
126 break;
127 case FOOUTGOING:
128 cur_face_ori = FOINCOMING;
129 break;
130 }
131 } // if not face owned locally at all
132
133 ++f;
134 } // for face
135 }
136
137 //============================================= Make directed connections
138 for (auto& cell : grid_.local_cells)
139 {
140 const uint64_t c = cell.local_id_;
141 size_t f = 0;
142 for (auto& face : cell.faces_)
143 {
144 const double mu = omega.Dot(face.normal_);
145 //======================================= If outgoing determine if
146 // it is to a local cell
147 if (cell_face_orientations_[cell.local_id_][f] == FOOUTGOING)
148 {
149 //================================ If it is a cell and not bndry
150 if (face.has_neighbor_)
151 {
152 //========================= If it is in the current location
153 if (face.IsNeighborLocal(grid_))
154 {
155 double weight = mu * face.ComputeFaceArea(grid_);
156 cell_successors[c].insert(
157 std::make_pair(face.GetNeighborLocalID(grid_), weight));
158 }
159 else
160 location_successors.insert(face.GetNeighborPartitionID(grid_));
161 }
162 }
163 //======================================= If not outgoing determine
164 // what it is dependent on
165 else
166 {
167 //================================if it is a cell and not bndry
168 if (face.has_neighbor_ and not face.IsNeighborLocal(grid_))
169 location_dependencies.insert(face.GetNeighborPartitionID(grid_));
170 }
171 ++f;
172 } // for face
173 } // for cell
174}
175
176
177
178// ###################################################################
180{
181 constexpr double tolerance = 1.0e-16;
182
183 for (int p = 0; p < Chi::mpi.process_count; ++p)
184 {
185 if (p == Chi::mpi.location_id)
186 {
187 std::cout << "// Printing directed graph for location " << p << ":\n";
188 std::cout << "digraph DG {\n";
189 std::cout << " splines=\"FALSE\";\n";
190 std::cout << " rankdir=\"LR\";\n\n";
191 std::cout << " /* Vertices */\n";
192
193 for (const auto& cell : grid_.local_cells)
194 {
195 std::cout << " " << cell.global_id_ << " [shape=\"circle\"]\n";
196
197 for (const auto& face : cell.faces_)
198 {
199 if (face.has_neighbor_ and (not grid_.IsCellLocal(face.neighbor_id_)))
200 std::cout << " " << face.neighbor_id_
201 << " [shape=\"circle\", style=filled fillcolor=red "
202 "fontcolor=white] //proc="
203 << grid_.cells[face.neighbor_id_].partition_id_ << "\n";
204 }
205 }
206
207 std::cout << "\n"
208 << " /* Edges */\n";
209 for (const auto& cell : grid_.local_cells)
210 {
211 for (const auto& face : cell.faces_)
212 {
213 if (face.has_neighbor_ and (cell.global_id_ > face.neighbor_id_))
214 {
215 if (omega_.Dot(face.normal_) > tolerance)
216 std::cout << " " << cell.global_id_ << " -> "
217 << face.neighbor_id_ << "\n";
218 else if (omega_.Dot(face.normal_) < tolerance)
219 std::cout << " " << face.neighbor_id_ << " -> "
220 << cell.global_id_ << "\n";
221 } // if outgoing
222 }
223 }
224 std::cout << "\n";
225 const auto ghost_ids = grid_.cells.GetGhostGlobalIDs();
226 for (const uint64_t global_id : ghost_ids)
227 {
228 const auto& cell = grid_.cells[global_id];
229 for (const auto& face : cell.faces_)
230 {
231 if (face.has_neighbor_ and (cell.global_id_ > face.neighbor_id_) and
232 grid_.IsCellLocal(face.neighbor_id_))
233 {
234 if (omega_.Dot(face.normal_) > tolerance)
235 std::cout << " " << cell.global_id_ << " -> "
236 << face.neighbor_id_ << "\n";
237 else if (omega_.Dot(face.normal_) < tolerance)
238 std::cout << " " << face.neighbor_id_ << " -> "
239 << cell.global_id_ << "\n";
240 } // if outgoing
241 }
242 }
243 std::cout << "}\n";
244
245 } // if current location
247 } // for p
248}
static void Exit(int error_code)
Definition: chi_runtime.cc:342
static chi::ChiLog & log
Definition: chi_runtime.h:81
static chi::MPI_Info & mpi
Definition: chi_runtime.h:78
LogStream LogAllError()
Definition: chi_log.h:239
void Barrier() const
Definition: mpi_info.cc:38
const int & process_count
Total number of processes.
Definition: mpi_info.h:27
std::vector< int > location_dependencies_
Definition: SPDS.h:63
int MapLocJToPrelocI(int locJ) const
Definition: SPDS.cc:16
int MapLocJToDeplocI(int locJ) const
Definition: SPDS.cc:36
void PrintedGhostedGraph() const
Definition: SPDS.cc:179
void PopulateCellRelationships(const chi_mesh::Vector3 &omega, std::set< int > &location_dependencies, std::set< int > &location_successors, std::vector< std::set< std::pair< int, double > > > &cell_successors)
Definition: SPDS.cc:51
std::vector< int > delayed_location_dependencies_
Definition: SPDS.h:65
Vector3 Dot(const chi_mesh::TensorRank2Dim3 &that) const