Chi-Tech
chi_meshcontinuum_globalcellhandler.cc
Go to the documentation of this file.
1#include "chi_meshcontinuum.h"
2
3#include "chi_runtime.h"
4#include "chi_mpi.h"
5#include "chi_log.h"
6
7//###################################################################
8/**Adds a new cell to grid registry.*/
10 push_back(std::unique_ptr<chi_mesh::Cell> new_cell)
11{
12 if (new_cell->partition_id_ == static_cast<uint64_t>(Chi::mpi.location_id))
13 {
14 new_cell->local_id_ = local_cells_ref_.size();
15
16 local_cells_ref_.push_back(std::move(new_cell));
17
18 const auto& cell = local_cells_ref_.back();
19
20 global_cell_id_to_native_id_map.insert(std::make_pair(
21 cell->global_id_, local_cells_ref_.size() - 1));
22 }
23 else
24 {
25 ghost_cells_ref_.push_back(std::move(new_cell));
26
27 const auto& cell = ghost_cells_ref_.back();
28
29 global_cell_id_to_foreign_id_map.insert(std::make_pair(
30 cell->global_id_, ghost_cells_ref_.size() - 1));
31 }
32
33}
34
35//###################################################################
36/**Returns a reference to a cell given its global cell index.*/
38 operator[](uint64_t cell_global_index)
39{
40 auto native_location = global_cell_id_to_native_id_map.find(cell_global_index);
41
42 if (native_location != global_cell_id_to_native_id_map.end())
43 return *local_cells_ref_[native_location->second];
44 else
45 {
46 auto foreign_location = global_cell_id_to_foreign_id_map.find(cell_global_index);
47 if (foreign_location != global_cell_id_to_foreign_id_map.end())
48 return *ghost_cells_ref_[foreign_location->second];
49 }
50
51 std::stringstream ostr;
52 ostr << "chi_mesh::MeshContinuum::cells. Mapping error."
53 << "\n"
54 << cell_global_index;
55
56 throw std::invalid_argument(ostr.str());
57}
58
59//###################################################################
60/**Returns a const reference to a cell given its global cell index.*/
62 operator[](uint64_t cell_global_index) const
63{
64 auto native_location = global_cell_id_to_native_id_map.find(cell_global_index);
65
66 if (native_location != global_cell_id_to_native_id_map.end())
67 return *local_cells_ref_[native_location->second];
68 else
69 {
70 auto foreign_location = global_cell_id_to_foreign_id_map.find(cell_global_index);
71 if (foreign_location != global_cell_id_to_foreign_id_map.end())
72 return *ghost_cells_ref_[foreign_location->second];
73 }
74
75 std::stringstream ostr;
76 ostr << "chi_mesh::MeshContinuum::cells. Mapping error."
77 << "\n"
78 << cell_global_index;
79
80 throw std::invalid_argument(ostr.str());
81}
82
83//###################################################################
84/**Returns the total number of global cells.*/
86{
87 size_t num_local_cells = local_cells_.size();
88 size_t num_globl_cells = 0;
89
90 MPI_Allreduce(&num_local_cells,
91 &num_globl_cells,
92 1,
93 MPI_UNSIGNED_LONG_LONG,
94 MPI_SUM,
95 Chi::mpi.comm);
96
97 return num_globl_cells;
98}
99
100//###################################################################
101/**Returns the cell global ids of all ghost cells. These are cells that
102 * neighbors to this partition's cells but are on a different
103 * partition.*/
104std::vector<uint64_t> chi_mesh::GlobalCellHandler::
105 GetGhostGlobalIDs() const
106{
107 std::vector<uint64_t> ids;
108 ids.reserve(GetNumGhosts());
109
110 for (auto& cell : ghost_cells_ref_)
111 ids.push_back(cell->global_id_);
112
113 return ids;
114}
115
116//###################################################################
117/**Returns the local storage address of a ghost cell. If the
118 * ghost is not truly a ghost then -1 is returned, but is wasteful and
119 * therefore the user of this function should implement code
120 * to prevent it.*/
122 GetGhostLocalID(uint64_t cell_global_index) const
123{
124 auto foreign_location =
125 global_cell_id_to_foreign_id_map.find(cell_global_index);
126
127 if (foreign_location != global_cell_id_to_foreign_id_map.end())
128 return foreign_location->second;
129
130 std::stringstream ostr;
131 ostr << "Grid GetGhostLocalID failed to find cell " << cell_global_index;
132
133 throw std::invalid_argument(ostr.str());
134}
static chi::MPI_Info & mpi
Definition: chi_runtime.h:78
const int & location_id
Current process rank.
Definition: mpi_info.h:26
std::map< uint64_t, uint64_t > & global_cell_id_to_foreign_id_map
std::vector< uint64_t > GetGhostGlobalIDs() const
uint64_t GetGhostLocalID(uint64_t cell_global_index) const
chi_mesh::Cell & operator[](uint64_t cell_global_index)
std::vector< std::unique_ptr< chi_mesh::Cell > > & local_cells_ref_
void push_back(std::unique_ptr< chi_mesh::Cell > new_cell)
std::map< uint64_t, uint64_t > & global_cell_id_to_native_id_map
std::vector< std::unique_ptr< chi_mesh::Cell > > & ghost_cells_ref_