| 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 |
/// @file HybridElements.h |
| 12 |
/// @author Willem Deconinck |
| 13 |
/// @date October 2015 |
| 14 |
/// |
| 15 |
/// This file describes the HybridElements class for a Mesh. |
| 16 |
|
| 17 |
#pragma once |
| 18 |
|
| 19 |
#include <map> |
| 20 |
|
| 21 |
#include "atlas/util/Object.h" |
| 22 |
#include "atlas/util/ObjectHandle.h" |
| 23 |
|
| 24 |
#include "atlas/field/Field.h" |
| 25 |
#include "atlas/util/Metadata.h" |
| 26 |
|
| 27 |
#include "atlas/mesh/Connectivity.h" |
| 28 |
|
| 29 |
namespace atlas { |
| 30 |
namespace field { |
| 31 |
class FieldImpl; |
| 32 |
} |
| 33 |
} // namespace atlas |
| 34 |
|
| 35 |
namespace atlas { |
| 36 |
class Mesh; |
| 37 |
} |
| 38 |
namespace atlas { |
| 39 |
namespace mesh { |
| 40 |
class ElementType; |
| 41 |
} |
| 42 |
} // namespace atlas |
| 43 |
namespace atlas { |
| 44 |
namespace mesh { |
| 45 |
class Elements; |
| 46 |
} |
| 47 |
} // namespace atlas |
| 48 |
|
| 49 |
namespace atlas { |
| 50 |
namespace mesh { |
| 51 |
template <class T> |
| 52 |
class ConnectivityInterface; |
| 53 |
class MultiBlockConnectivityImpl; |
| 54 |
using MultiBlockConnectivity = ConnectivityInterface<MultiBlockConnectivityImpl>; |
| 55 |
} // namespace mesh |
| 56 |
} // namespace atlas |
| 57 |
|
| 58 |
namespace atlas { |
| 59 |
namespace mesh { |
| 60 |
|
| 61 |
// ------------------------------------------------------------------------------- |
| 62 |
|
| 63 |
/// @brief HybridElements class that describes elements of different types |
| 64 |
class HybridElements : public util::Object { |
| 65 |
friend class Elements; |
| 66 |
|
| 67 |
public: |
| 68 |
typedef MultiBlockConnectivity Connectivity; |
| 69 |
|
| 70 |
public: // methods |
| 71 |
//-- Constructors |
| 72 |
HybridElements(); |
| 73 |
virtual ~HybridElements(); |
| 74 |
|
| 75 |
//-- Accessors |
| 76 |
|
| 77 |
/// @brief Number of elements |
| 78 |
idx_t size() const; |
| 79 |
|
| 80 |
/// @brief Number of nodes for given element |
| 81 |
idx_t nb_nodes(idx_t elem_idx) const; |
| 82 |
|
| 83 |
/// @brief Number of edges for given element |
| 84 |
idx_t nb_edges(idx_t elem_idx) const; |
| 85 |
|
| 86 |
/// @brief Element type index for given element |
| 87 |
idx_t type_idx(idx_t elem_idx) const; |
| 88 |
|
| 89 |
/// @brief Element type name for given element |
| 90 |
const std::string& name(idx_t elem_idx) const; |
| 91 |
|
| 92 |
/// @brief Element to Node connectivity table |
| 93 |
const HybridElements::Connectivity& node_connectivity() const; |
| 94 |
HybridElements::Connectivity& node_connectivity(); |
| 95 |
|
| 96 |
/// @brief Element to Edge connectivity table |
| 97 |
const HybridElements::Connectivity& edge_connectivity() const; |
| 98 |
HybridElements::Connectivity& edge_connectivity(); |
| 99 |
|
| 100 |
/// @brief Element to Cell connectivity table |
| 101 |
const HybridElements::Connectivity& cell_connectivity() const; |
| 102 |
HybridElements::Connectivity& cell_connectivity(); |
| 103 |
|
| 104 |
/// @brief Number of types present in HybridElements |
| 105 |
idx_t nb_types() const; |
| 106 |
|
| 107 |
/// @brief The element_type description for given type |
| 108 |
const ElementType& element_type(idx_t type_idx) const; |
| 109 |
|
| 110 |
/// @brief Sub-elements convenience class for given type |
| 111 |
/// This allows optimized access to connectivities and loops. |
| 112 |
const Elements& elements(idx_t type_idx) const; |
| 113 |
Elements& elements(idx_t type_idx); |
| 114 |
|
| 115 |
const Field& field(const std::string& name) const; |
| 116 |
Field& field(const std::string& name); |
| 117 |
bool has_field(const std::string& name) const { return (fields_.find(name) != fields_.end()); } |
| 118 |
|
| 119 |
const Field& field(idx_t) const; |
| 120 |
Field& field(idx_t); |
| 121 |
idx_t nb_fields() const { return static_cast<idx_t>(fields_.size()); } |
| 122 |
|
| 123 |
const util::Metadata& metadata() const { return metadata_; } |
| 124 |
util::Metadata& metadata() { return metadata_; } |
| 125 |
|
| 126 |
const Field& global_index() const { return field("glb_idx"); } |
| 127 |
Field& global_index() { return field("glb_idx"); } |
| 128 |
|
| 129 |
const Field& remote_index() const { return field("remote_idx"); } |
| 130 |
Field& remote_index() { return field("remote_idx"); } |
| 131 |
|
| 132 |
const Field& partition() const { return field("partition"); } |
| 133 |
Field& partition() { return field("partition"); } |
| 134 |
|
| 135 |
const Field& halo() const { return field("halo"); } |
| 136 |
Field& halo() { return field("halo"); } |
| 137 |
|
| 138 |
const Field& flags() const { return field("flags"); } |
| 139 |
Field& flags() { return field("flags"); } |
| 140 |
|
| 141 |
// -- Modifiers |
| 142 |
|
| 143 |
/// @brief Add a new element type with given number of elements |
| 144 |
/// @return type_idx of the added element type |
| 145 |
idx_t add(const ElementType*, idx_t nb_elements); |
| 146 |
|
| 147 |
/// @brief Add a new element type with given number of elements and |
| 148 |
/// node-connectivity |
| 149 |
/// @return type_idx of the added element type |
| 150 |
idx_t add(const ElementType*, idx_t nb_elements, const std::vector<idx_t>& node_connectivity); |
| 151 |
|
| 152 |
/// @brief Add a new element type with given number of elements and |
| 153 |
/// node-connectivity |
| 154 |
/// @return type_idx of the added element type |
| 155 |
idx_t add(const ElementType*, idx_t nb_elements, const idx_t node_connectivity[]); |
| 156 |
|
| 157 |
/// @brief Add a new element type with given number of elements and |
| 158 |
/// node-connectivity |
| 159 |
/// @return type_idx of the added element type |
| 160 |
idx_t add(const ElementType*, idx_t nb_elements, const idx_t node_connectivity[], bool fortran_array); |
| 161 |
|
| 162 |
/// @brief Add a new element type from existing Elements. |
| 163 |
/// Data will be copied. |
| 164 |
/// @return type_idx of the added element type |
| 165 |
idx_t add(const Elements&); |
| 166 |
|
| 167 |
Field add(const Field& field); |
| 168 |
|
| 169 |
void remove_field(const std::string& name); |
| 170 |
|
| 171 |
void insert(idx_t type_idx, idx_t position, idx_t nb_elements = 1); |
| 172 |
|
| 173 |
void updateDevice() const; |
| 174 |
|
| 175 |
void updateHost() const; |
| 176 |
|
| 177 |
void syncHostDevice() const; |
| 178 |
|
| 179 |
void clear(); |
| 180 |
|
| 181 |
/// @brief Return the memory footprint of the elements |
| 182 |
size_t footprint() const; |
| 183 |
|
| 184 |
private: // -- types |
| 185 |
typedef std::map<std::string, Field> FieldMap; |
| 186 |
typedef std::map<std::string, util::ObjectHandle<Connectivity>> ConnectivityMap; |
| 187 |
|
| 188 |
private: // -- methods |
| 189 |
void resize(idx_t size); |
| 190 |
|
| 191 |
idx_t elemtype_nb_nodes(idx_t elem_idx) const; |
| 192 |
idx_t elemtype_nb_edges(idx_t elem_idx) const; |
| 193 |
|
| 194 |
Connectivity& add(Connectivity*); |
| 195 |
|
| 196 |
private: // -- Data |
| 197 |
// -- Total number of elements |
| 198 |
idx_t size_; |
| 199 |
|
| 200 |
// -- Data: one value per type |
| 201 |
std::vector<idx_t> elements_size_; |
| 202 |
std::vector<idx_t> elements_begin_; |
| 203 |
std::vector<util::ObjectHandle<const ElementType>> element_types_; |
| 204 |
|
| 205 |
// -- Data: one value per element |
| 206 |
std::vector<idx_t> type_idx_; |
| 207 |
|
| 208 |
// -- Sub elements |
| 209 |
std::vector<util::ObjectHandle<Elements>> elements_; |
| 210 |
|
| 211 |
// -- Fields and connectivities |
| 212 |
FieldMap fields_; |
| 213 |
ConnectivityMap connectivities_; |
| 214 |
|
| 215 |
// -- Metadata |
| 216 |
util::Metadata metadata_; |
| 217 |
|
| 218 |
// -- Cached shortcuts to specific connectivities in connectivities_ |
| 219 |
Connectivity* node_connectivity_; |
| 220 |
Connectivity* edge_connectivity_; |
| 221 |
Connectivity* cell_connectivity_; |
| 222 |
}; |
| 223 |
|
| 224 |
// ----------------------------------------------------------------------------------------------------- |
| 225 |
|
| 226 |
inline idx_t HybridElements::size() const { |
| 227 |
return size_; |
| 228 |
} |
| 229 |
|
| 230 |
inline idx_t HybridElements::nb_types() const { |
| 231 |
return static_cast<idx_t>(element_types_.size()); |
| 232 |
} |
| 233 |
|
| 234 |
inline const ElementType& HybridElements::element_type(idx_t type_idx) const { |
| 235 |
return *element_types_[type_idx].get(); |
| 236 |
} |
| 237 |
|
| 238 |
inline const HybridElements::Connectivity& HybridElements::node_connectivity() const { |
| 239 |
return *node_connectivity_; |
| 240 |
} |
| 241 |
|
| 242 |
inline HybridElements::Connectivity& HybridElements::node_connectivity() { |
| 243 |
return *node_connectivity_; |
| 244 |
} |
| 245 |
|
| 246 |
inline const HybridElements::Connectivity& HybridElements::edge_connectivity() const { |
| 247 |
return *edge_connectivity_; |
| 248 |
} |
| 249 |
|
| 250 |
inline HybridElements::Connectivity& HybridElements::edge_connectivity() { |
| 251 |
return *edge_connectivity_; |
| 252 |
} |
| 253 |
|
| 254 |
inline const HybridElements::Connectivity& HybridElements::cell_connectivity() const { |
| 255 |
return *cell_connectivity_; |
| 256 |
} |
| 257 |
|
| 258 |
inline HybridElements::Connectivity& HybridElements::cell_connectivity() { |
| 259 |
return *cell_connectivity_; |
| 260 |
} |
| 261 |
|
| 262 |
inline const Elements& HybridElements::elements(idx_t type_idx) const { |
| 263 |
return *elements_[type_idx].get(); |
| 264 |
} |
| 265 |
|
| 266 |
inline Elements& HybridElements::elements(idx_t type_idx) { |
| 267 |
return *elements_[type_idx].get(); |
| 268 |
} |
| 269 |
|
| 270 |
inline idx_t HybridElements::nb_nodes(idx_t elem_idx) const { |
| 271 |
return node_connectivity_->rows() ? node_connectivity_->cols(elem_idx) : elemtype_nb_nodes(elem_idx); |
| 272 |
} |
| 273 |
|
| 274 |
inline idx_t HybridElements::nb_edges(idx_t elem_idx) const { |
| 275 |
return edge_connectivity_->rows() ? edge_connectivity_->cols(elem_idx) : elemtype_nb_edges(elem_idx); |
| 276 |
} |
| 277 |
|
| 278 |
inline idx_t HybridElements::type_idx(idx_t elem_idx) const { |
| 279 |
return type_idx_[elem_idx]; |
| 280 |
} |
| 281 |
|
| 282 |
// ------------------------------------------------------------------------------------------------------ |
| 283 |
|
| 284 |
extern "C" { |
| 285 |
HybridElements* atlas__mesh__HybridElements__create(); |
| 286 |
void atlas__mesh__HybridElements__delete(HybridElements* This); |
| 287 |
MultiBlockConnectivity* atlas__mesh__HybridElements__node_connectivity(HybridElements* This); |
| 288 |
MultiBlockConnectivity* atlas__mesh__HybridElements__edge_connectivity(HybridElements* This); |
| 289 |
MultiBlockConnectivity* atlas__mesh__HybridElements__cell_connectivity(HybridElements* This); |
| 290 |
|
| 291 |
idx_t atlas__mesh__HybridElements__size(const HybridElements* This); |
| 292 |
void atlas__mesh__HybridElements__add_elements(HybridElements* This, ElementType* elementtype, idx_t nb_elements); |
| 293 |
void atlas__mesh__HybridElements__add_elements_with_nodes(HybridElements* This, ElementType* elementtype, |
| 294 |
idx_t nb_elements, idx_t node_connectivity[], |
| 295 |
int fortran_array); |
| 296 |
void atlas__mesh__HybridElements__add_field(HybridElements* This, field::FieldImpl* field); |
| 297 |
int atlas__mesh__HybridElements__has_field(const HybridElements* This, char* name); |
| 298 |
int atlas__mesh__HybridElements__nb_fields(const HybridElements* This); |
| 299 |
int atlas__mesh__HybridElements__nb_types(const HybridElements* This); |
| 300 |
field::FieldImpl* atlas__mesh__HybridElements__field_by_name(HybridElements* This, char* name); |
| 301 |
field::FieldImpl* atlas__mesh__HybridElements__field_by_idx(HybridElements* This, idx_t idx); |
| 302 |
field::FieldImpl* atlas__mesh__HybridElements__global_index(HybridElements* This); |
| 303 |
field::FieldImpl* atlas__mesh__HybridElements__remote_index(HybridElements* This); |
| 304 |
field::FieldImpl* atlas__mesh__HybridElements__partition(HybridElements* This); |
| 305 |
field::FieldImpl* atlas__mesh__HybridElements__halo(HybridElements* This); |
| 306 |
|
| 307 |
Elements* atlas__mesh__HybridElements__elements(HybridElements* This, idx_t idx); |
| 308 |
} |
| 309 |
|
| 310 |
} // namespace mesh |
| 311 |
} // namespace atlas |