| 1 |
/* |
| 2 |
* (C) Copyright 2013 ECMWF. |
| 3 |
* |
| 4 |
* This software is licensed under the terms of the Apache Licence Version 2.0 |
| 5 |
* which can be obtained at http://www.apache.org/licenses/LICENSE-2.0. |
| 6 |
* In applying this licence, ECMWF does not waive the privileges and immunities |
| 7 |
* granted to it by virtue of its status as an intergovernmental organisation |
| 8 |
* nor does it submit to any jurisdiction. |
| 9 |
*/ |
| 10 |
|
| 11 |
#pragma once |
| 12 |
|
| 13 |
#include <iosfwd> |
| 14 |
#include <memory> |
| 15 |
|
| 16 |
#include "atlas/grid/Grid.h" |
| 17 |
#include "atlas/mesh/PartitionPolygon.h" |
| 18 |
#include "atlas/mesh/detail/PartitionGraph.h" |
| 19 |
#include "atlas/projection/Projection.h" |
| 20 |
#include "atlas/util/Metadata.h" |
| 21 |
#include "atlas/util/Object.h" |
| 22 |
#include "atlas/util/ObjectHandle.h" |
| 23 |
|
| 24 |
//---------------------------------------------------------------------------------------------------------------------- |
| 25 |
|
| 26 |
namespace eckit { |
| 27 |
class Stream; |
| 28 |
} |
| 29 |
|
| 30 |
namespace atlas { |
| 31 |
namespace util { |
| 32 |
class PartitionPolygons; |
| 33 |
} |
| 34 |
class Mesh; |
| 35 |
namespace mesh { |
| 36 |
class PartitionPolygon; |
| 37 |
class Nodes; |
| 38 |
class HybridElements; |
| 39 |
typedef HybridElements Edges; |
| 40 |
typedef HybridElements Cells; |
| 41 |
} // namespace mesh |
| 42 |
} // namespace atlas |
| 43 |
|
| 44 |
//---------------------------------------------------------------------------------------------------------------------- |
| 45 |
|
| 46 |
namespace atlas { |
| 47 |
namespace mesh { |
| 48 |
namespace detail { |
| 49 |
|
| 50 |
//---------------------------------------------------------------------------------------------------------------------- |
| 51 |
|
| 52 |
class MeshObserver; |
| 53 |
|
| 54 |
class MeshImpl : public util::Object { |
| 55 |
public: // methods |
| 56 |
/// @brief Construct a empty MeshImpl |
| 57 |
explicit MeshImpl(); |
| 58 |
|
| 59 |
/// @brief Construct a mesh from a Stream (serialization) |
| 60 |
explicit MeshImpl(eckit::Stream&); |
| 61 |
|
| 62 |
/// @brief Serialization to Stream |
| 63 |
void encode(eckit::Stream& s) const; |
| 64 |
|
| 65 |
/// Destructor |
| 66 |
/// @note No need to be virtual since this is not a base class. |
| 67 |
~MeshImpl(); |
| 68 |
|
| 69 |
util::Metadata& metadata() { return metadata_; } |
| 70 |
const util::Metadata& metadata() const { return metadata_; } |
| 71 |
|
| 72 |
void print(std::ostream&) const; |
| 73 |
|
| 74 |
const Nodes& nodes() const { return *nodes_; } |
| 75 |
Nodes& nodes() { return *nodes_; } |
| 76 |
|
| 77 |
const Cells& cells() const { return *cells_; } |
| 78 |
Cells& cells() { return *cells_; } |
| 79 |
|
| 80 |
const Edges& edges() const { return *edges_; } |
| 81 |
Edges& edges() { return *edges_; } |
| 82 |
|
| 83 |
const HybridElements& facets() const { return *facets_; } |
| 84 |
HybridElements& facets() { return *facets_; } |
| 85 |
|
| 86 |
const HybridElements& ridges() const { return *ridges_; } |
| 87 |
HybridElements& ridges() { return *ridges_; } |
| 88 |
|
| 89 |
const HybridElements& peaks() const { return *peaks_; } |
| 90 |
HybridElements& peaks() { return *peaks_; } |
| 91 |
|
| 92 |
bool generated() const; |
| 93 |
|
| 94 |
/// @brief Return the memory footprint of the mesh |
| 95 |
size_t footprint() const; |
| 96 |
|
| 97 |
idx_t part() const; |
| 98 |
idx_t nb_parts() const; |
| 99 |
std::string mpi_comm() const; |
| 100 |
|
| 101 |
void updateDevice() const; |
| 102 |
|
| 103 |
void updateHost() const; |
| 104 |
|
| 105 |
void syncHostDevice() const; |
| 106 |
|
| 107 |
const Projection& projection() const { return projection_; } |
| 108 |
|
| 109 |
const PartitionGraph& partitionGraph() const; |
| 110 |
|
| 111 |
PartitionGraph::Neighbours nearestNeighbourPartitions() const; |
| 112 |
|
| 113 |
const PartitionPolygon& polygon(idx_t halo = 0) const; |
| 114 |
const util::PartitionPolygons& polygons() const; |
| 115 |
|
| 116 |
const Grid grid() const { return grid_; } |
| 117 |
|
| 118 |
void attachObserver(MeshObserver&) const; |
| 119 |
void detachObserver(MeshObserver&) const; |
| 120 |
|
| 121 |
private: // methods |
| 122 |
friend class ::atlas::Mesh; |
| 123 |
|
| 124 |
friend std::ostream& operator<<(std::ostream& s, const MeshImpl& p) { |
| 125 |
p.print(s); |
| 126 |
return s; |
| 127 |
} |
| 128 |
|
| 129 |
void createElements(); |
| 130 |
|
| 131 |
void setProjection(const Projection&); |
| 132 |
void setGrid(const Grid&); |
| 133 |
|
| 134 |
private: // members |
| 135 |
util::Metadata metadata_; |
| 136 |
|
| 137 |
util::ObjectHandle<Nodes> nodes_; |
| 138 |
// dimensionality : 2D | 3D |
| 139 |
// -------- |
| 140 |
util::ObjectHandle<HybridElements> cells_; // 2D | 3D |
| 141 |
util::ObjectHandle<HybridElements> facets_; // 1D | 2D |
| 142 |
util::ObjectHandle<HybridElements> ridges_; // 0D | 1D |
| 143 |
util::ObjectHandle<HybridElements> peaks_; // NA | 0D |
| 144 |
|
| 145 |
util::ObjectHandle<HybridElements> edges_; // alias to facets of 2D mesh, ridges of 3D mesh |
| 146 |
|
| 147 |
idx_t dimensionality_; |
| 148 |
|
| 149 |
Projection projection_; |
| 150 |
|
| 151 |
Grid grid_; |
| 152 |
|
| 153 |
mutable util::ObjectHandle<PartitionGraph> partition_graph_; |
| 154 |
|
| 155 |
mutable std::vector<util::ObjectHandle<PartitionPolygon>> polygons_; |
| 156 |
|
| 157 |
mutable util::PartitionPolygons all_polygons_; // from all partitions |
| 158 |
|
| 159 |
mutable std::vector<MeshObserver*> mesh_observers_; |
| 160 |
}; |
| 161 |
|
| 162 |
//---------------------------------------------------------------------------------------------------------------------- |
| 163 |
|
| 164 |
class MeshObserver { |
| 165 |
private: |
| 166 |
std::vector<const MeshImpl*> registered_meshes_; |
| 167 |
|
| 168 |
public: |
| 169 |
void registerMesh(const MeshImpl& mesh) { |
| 170 |
if (std::find(registered_meshes_.begin(), registered_meshes_.end(), &mesh) == registered_meshes_.end()) { |
| 171 |
registered_meshes_.push_back(&mesh); |
| 172 |
mesh.attachObserver(*this); |
| 173 |
} |
| 174 |
} |
| 175 |
void unregisterMesh(const MeshImpl& mesh) { |
| 176 |
auto found = std::find(registered_meshes_.begin(), registered_meshes_.end(), &mesh); |
| 177 |
if (found != registered_meshes_.end()) { |
| 178 |
registered_meshes_.erase(found); |
| 179 |
mesh.detachObserver(*this); |
| 180 |
} |
| 181 |
} |
| 182 |
virtual ~MeshObserver() { |
| 183 |
for (auto mesh : registered_meshes_) { |
| 184 |
mesh->detachObserver(*this); |
| 185 |
} |
| 186 |
} |
| 187 |
|
| 188 |
virtual void onMeshDestruction(MeshImpl&) = 0; |
| 189 |
}; |
| 190 |
|
| 191 |
//---------------------------------------------------------------------------------------------------------------------- |
| 192 |
|
| 193 |
} // namespace detail |
| 194 |
} // namespace mesh |
| 195 |
} // namespace atlas |