ThunderEgg  1.0.0
CoarseNbrInfo.h
Go to the documentation of this file.
1 /***************************************************************************
2  * ThunderEgg, a library for solvers on adaptively refined block-structured
3  * Cartesian grids.
4  *
5  * Copyright (c) 2020-2021 Scott Aiton
6  *
7  * This program is free software: you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation, either version 3 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program. If not, see <https://www.gnu.org/licenses/>.
19  ***************************************************************************/
20 #ifndef THUNDEREGG_COARSENBRINFO_H
21 #define THUNDEREGG_COARSENBRINFO_H
22 
29 #include <ThunderEgg/NbrInfo.h>
30 #include <ThunderEgg/Orthant.h>
31 
32 namespace ThunderEgg {
38 template<int D>
39 class CoarseNbrInfo : public NbrInfo<D>
40 {
41 public:
45  int rank = 0;
49  int id = 0;
53  int local_index = -1;
57  int global_index = -1;
65  CoarseNbrInfo() = default;
66  ~CoarseNbrInfo() = default;
75  {
76  this->id = id;
77  this->orth_on_coarse = orth_on_coarse;
78  }
79  NbrType getNbrType() const override { return NbrType::Coarse; }
80  void getNbrIds(std::deque<int>& nbr_ids) const override { nbr_ids.push_back(id); };
81  void getNbrRanks(std::deque<int>& nbr_ranks) const override { nbr_ranks.push_back(rank); }
82  void setGlobalIndexes(const std::map<int, int>& id_to_global_index_map) override
83  {
84  global_index = id_to_global_index_map.at(id);
85  }
86  void setLocalIndexes(const std::map<int, int>& id_to_local_index_map) override
87  {
88  auto iter = id_to_local_index_map.find(id);
89  if (iter != id_to_local_index_map.end()) {
90  local_index = iter->second;
91  }
92  }
93  int serialize(char* buffer) const override
94  {
95  BufferWriter writer(buffer);
96  writer << rank;
97  writer << id;
98  writer << orth_on_coarse;
99  return writer.getPos();
100  }
101  int deserialize(char* buffer) override
102  {
103  BufferReader reader(buffer);
104  reader >> rank;
105  reader >> id;
106  reader >> orth_on_coarse;
107  return reader.getPos();
108  }
109  std::unique_ptr<NbrInfoBase> clone() const override
110  {
111  return std::make_unique<CoarseNbrInfo<D>>(*this);
112  }
113 };
114 template<int D>
115 void
116 to_json(tpl::nlohmann::json& j, const CoarseNbrInfo<D>& n)
117 {
118  j["type"] = NbrType::Coarse;
119  j["ids"] = { n.id };
120  j["ranks"] = { n.rank };
121  j["orth_on_coarse"] = n.orth_on_coarse;
122 }
123 template<int D>
124 void
125 from_json(const tpl::nlohmann::json& j, CoarseNbrInfo<D>& n)
126 {
127  n.id = j["ids"][0];
128  n.rank = j["ranks"][0];
129  if (j.contains("orth_on_coarse")) {
130  n.orth_on_coarse = j["orth_on_coarse"].get<Orthant<D>>();
131  } else {
132  n.orth_on_coarse = Orthant<D>::null();
133  }
134 }
135 } // namespace ThunderEgg
136 #endif
ThunderEgg::CoarseNbrInfo::getNbrIds
void getNbrIds(std::deque< int > &nbr_ids) const override
Add to a deque of neighbor ids.
Definition: CoarseNbrInfo.h:80
ThunderEgg::CoarseNbrInfo::id
int id
The id of the neighbor.
Definition: CoarseNbrInfo.h:49
ThunderEgg::CoarseNbrInfo::clone
std::unique_ptr< NbrInfoBase > clone() const override
get a clone of this object (equivalent to copy constructor)
Definition: CoarseNbrInfo.h:109
ThunderEgg::Orthant::null
static Orthant< D > null()
null value
Definition: Orthant.h:68
ThunderEgg::Orthant
An enum-style class that represents the octants of a cube.
Definition: Orthant.h:43
ThunderEgg::CoarseNbrInfo::global_index
int global_index
The global index of the neighbor.
Definition: CoarseNbrInfo.h:57
ThunderEgg::BufferWriter
Class that is used to help serialize objects into a buffer.
Definition: BufferWriter.h:38
ThunderEgg::CoarseNbrInfo::getNbrRanks
void getNbrRanks(std::deque< int > &nbr_ranks) const override
Add to a deque of neighbor ranks.
Definition: CoarseNbrInfo.h:81
ThunderEgg::NbrType::Coarse
@ Coarse
The neighbor is at a coarser refinement level.
ThunderEgg::CoarseNbrInfo::getNbrType
NbrType getNbrType() const override
Get the NbrType.
Definition: CoarseNbrInfo.h:79
ThunderEgg::CoarseNbrInfo::local_index
int local_index
The local index of the neighbor.
Definition: CoarseNbrInfo.h:53
ThunderEgg::NbrType
NbrType
The type of neighbor.
Definition: NbrType.h:34
ThunderEgg::CoarseNbrInfo::deserialize
int deserialize(char *buffer) override
Deserialize an object.
Definition: CoarseNbrInfo.h:101
ThunderEgg::CoarseNbrInfo::setLocalIndexes
void setLocalIndexes(const std::map< int, int > &id_to_local_index_map) override
Set the global indexes in the NbrInfo objects.
Definition: CoarseNbrInfo.h:86
ThunderEgg::CoarseNbrInfo::orth_on_coarse
Orthant< D > orth_on_coarse
The orthant that this patch in relation to the coarser patch's interface.
Definition: CoarseNbrInfo.h:61
ThunderEgg
The ThunderEgg namespace.
Definition: BiLinearGhostFiller.h:31
ThunderEgg::CoarseNbrInfo::CoarseNbrInfo
CoarseNbrInfo()=default
Construct a new empty CoarseNbrInfo object.
ThunderEgg::NbrInfo
Represents information about a patch's neighbor.
Definition: NbrInfo.h:36
ThunderEgg::CoarseNbrInfo
Represents a neighbor that is at a coarser refinement level.
Definition: CoarseNbrInfo.h:39
ThunderEgg::BufferReader::getPos
int getPos()
get the current position in the buffer
Definition: BufferReader.h:74
ThunderEgg::CoarseNbrInfo::serialize
int serialize(char *buffer) const override
Serialize object into buffer.
Definition: CoarseNbrInfo.h:93
NbrInfo.h
NbrInfo class.
BufferWriter.h
BufferWriter class.
ThunderEgg::CoarseNbrInfo::rank
int rank
The mpi rank that the neighbor resides on.
Definition: CoarseNbrInfo.h:45
ThunderEgg::BufferWriter::getPos
int getPos()
get the current position in the buffer
Definition: BufferWriter.h:80
ThunderEgg::BufferReader
Class that is used to help read serialized objects from a buffer.
Definition: BufferReader.h:38
BufferReader.h
BufferReader class.
ThunderEgg::CoarseNbrInfo::CoarseNbrInfo
CoarseNbrInfo(int id, Orthant< D > orth_on_coarse)
Construct a new CoarseNbrInfo object.
Definition: CoarseNbrInfo.h:74
Orthant.h
Orthant class.
ThunderEgg::CoarseNbrInfo::setGlobalIndexes
void setGlobalIndexes(const std::map< int, int > &id_to_global_index_map) override
Set the local indexes in the NbrInfo objects.
Definition: CoarseNbrInfo.h:82