atlas
MeshImpl.h
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"
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
57  explicit MeshImpl();
58 
60  explicit MeshImpl( eckit::Stream& );
61 
63  void encode( eckit::Stream& s ) const;
64 
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 
95  size_t footprint() const;
96 
97  idx_t partition() const;
98  idx_t nb_partitions() const;
99 
100  void updateDevice() const;
101 
102  void updateHost() const;
103 
104  void syncHostDevice() const;
105 
106  const Projection& projection() const { return projection_; }
107 
108  const PartitionGraph& partitionGraph() const;
109 
110  PartitionGraph::Neighbours nearestNeighbourPartitions() const;
111 
112  const PartitionPolygon& polygon( idx_t halo = 0 ) const;
113  const util::PartitionPolygons& polygons() const;
114 
115  const Grid grid() const { return grid_; }
116 
117  void attachObserver( MeshObserver& ) const;
118  void detachObserver( MeshObserver& ) const;
119 
120 private: // methods
121  friend class ::atlas::Mesh;
122 
123  friend std::ostream& operator<<( std::ostream& s, const MeshImpl& p ) {
124  p.print( s );
125  return s;
126  }
127 
128  void createElements();
129 
130  void setProjection( const Projection& );
131  void setGrid( const Grid& );
132 
133 private: // members
134  util::Metadata metadata_;
135 
137  // dimensionality : 2D | 3D
138  // --------
139  util::ObjectHandle<HybridElements> cells_; // 2D | 3D
140  util::ObjectHandle<HybridElements> facets_; // 1D | 2D
141  util::ObjectHandle<HybridElements> ridges_; // 0D | 1D
142  util::ObjectHandle<HybridElements> peaks_; // NA | 0D
143 
144  util::ObjectHandle<HybridElements> edges_; // alias to facets of 2D mesh, ridges of 3D mesh
145 
146  idx_t dimensionality_;
147 
148  Projection projection_;
149 
150  Grid grid_;
151 
152  mutable util::ObjectHandle<PartitionGraph> partition_graph_;
153 
154  mutable std::vector<util::ObjectHandle<PartitionPolygon>> polygons_;
155 
156  mutable util::PartitionPolygons all_polygons_; // from all partitions
157 
158  mutable std::vector<MeshObserver*> mesh_observers_;
159 };
160 
161 //----------------------------------------------------------------------------------------------------------------------
162 
164 private:
165  std::vector<const MeshImpl*> registered_meshes_;
166 
167 public:
168  void registerMesh( const MeshImpl& mesh ) {
169  if ( std::find( registered_meshes_.begin(), registered_meshes_.end(), &mesh ) == registered_meshes_.end() ) {
170  registered_meshes_.push_back( &mesh );
171  mesh.attachObserver( *this );
172  }
173  }
174  void unregisterMesh( const MeshImpl& mesh ) {
175  auto found = std::find( registered_meshes_.begin(), registered_meshes_.end(), &mesh );
176  if ( found != registered_meshes_.end() ) {
177  registered_meshes_.erase( found );
178  mesh.detachObserver( *this );
179  }
180  }
181  virtual ~MeshObserver() {
182  for ( auto mesh : registered_meshes_ ) {
183  mesh->detachObserver( *this );
184  }
185  }
186 
187  virtual void onMeshDestruction( MeshImpl& ) = 0;
188 };
189 
190 //----------------------------------------------------------------------------------------------------------------------
191 
192 } // namespace detail
193 } // namespace mesh
194 } // namespace atlas
Definition: Polygon.h:155
Definition: Projection.h:49
Definition: PartitionGraph.h:29
Definition: Object.h:18
Definition: Domain.h:19
Most general grid container.
Definition: Grid.h:64
Definition: Metadata.h:23
Nodes class that owns a collection of fields defined in nodes of the mesh.
Definition: Nodes.h:43
Definition: MeshImpl.h:54
HybridElements class that describes elements of different types.
Definition: HybridElements.h:64
Contains all atlas classes and methods.
Definition: atlas-grids.cc:33
long idx_t
Integer type for indices in connectivity tables.
Definition: config.h:42
Definition: MeshImpl.h:163
Polygon class that holds the boundary of a mesh partition.
Definition: PartitionPolygon.h:41
Definition: ObjectHandle.h:64