| 1 |
/* |
| 2 |
* (C) Copyright 2023 UCAR. |
| 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 |
*/ |
| 7 |
|
| 8 |
#pragma once |
| 9 |
|
| 10 |
#include "eckit/config/Configuration.h" |
| 11 |
|
| 12 |
#include "atlas/parallel/mpi/mpi.h" |
| 13 |
#include "atlas/util/Config.h" |
| 14 |
#include "atlas/mdspan.h" |
| 15 |
#include "atlas/mesh/Mesh.h" |
| 16 |
|
| 17 |
#include <array> |
| 18 |
#include <vector> |
| 19 |
|
| 20 |
namespace atlas { |
| 21 |
namespace mesh { |
| 22 |
|
| 23 |
//----------------------------------------------------------------------------- |
| 24 |
|
| 25 |
/** |
| 26 |
* \brief Construct a Mesh by importing external connectivity data |
| 27 |
* |
| 28 |
* Given a list of nodes and corresponding cell-to-node connectivity, sets up a Mesh. Not all Mesh |
| 29 |
* fields are initialized, but enough are to build halos and construct a NodeColumns FunctionSpace. |
| 30 |
* |
| 31 |
* Some limitations of the current implementation (but not inherent): |
| 32 |
* - can only set up triangle and quad cells. |
| 33 |
* - cannot import halos, i.e., cells owned by other MPI tasks; halos can still be subsequently |
| 34 |
* computed by calling the BuildMesh action. |
| 35 |
* - cannot import node-to-cell connectivity information. |
| 36 |
* |
| 37 |
* The Mesh's Grid can be initialized via the call operator's optional config argument. |
| 38 |
*/ |
| 39 |
class MeshBuilder : public eckit::Owned { |
| 40 |
public: |
| 41 |
MeshBuilder(const eckit::Configuration& = util::NoConfig()) {} |
| 42 |
|
| 43 |
/** |
| 44 |
* \brief C-interface to construct a Mesh from external connectivity data |
| 45 |
* |
| 46 |
* The inputs lons, lats, ghosts, global_indices, remote_indices, and partitions are vectors of |
| 47 |
* size nb_nodes, ranging over the nodes locally owned by (or in the ghost nodes of) the MPI |
| 48 |
* task. The global index is a uniform labeling of the nodes across all MPI tasks; the remote |
| 49 |
* index is a remote_index_base-based vector index for the node on its owning task. |
| 50 |
* |
| 51 |
* The tri/quad connectivities (boundary_nodes and global_indices) are vectors ranging over the |
| 52 |
* cells owned by the MPI task. Each cell is defined by a list of nodes defining its boundary; |
| 53 |
* note that each boundary node must be locally known (whether as an owned of ghost node on the |
| 54 |
* MPI task), in other words, must be an element of the node global_indices. The boundary nodes |
| 55 |
* are ordered node-varies-fastest, element-varies-slowest order. The cell global index is, |
| 56 |
* here also, a uniform labeling over the of the cells across all MPI tasks. |
| 57 |
* |
| 58 |
* The config argument can be used to |
| 59 |
* - Request the Mesh's Grid to be constructed, usually from the config. If the Grid is either |
| 60 |
* an UnstructuredGrid or a Structured grid, the `validate` bool option can be used to trigger |
| 61 |
* a simple check that the grid is consistent with the lons/lats passed in to the MeshBuilder. |
| 62 |
* In the special case where `grid.type` is unstructured and the `grid.xy` coordinates are |
| 63 |
* _not_ given, then the grid is constructed from the lons/lats passed to the MeshBuilder. |
| 64 |
* - Select which MPI communicator to use. |
| 65 |
*/ |
| 66 |
|
| 67 |
// operator (1) deprecated, use operator (2) |
| 68 |
[[deprecated("Use operator (2) with extra global_index_base argument")]] |
| 69 |
Mesh operator()(size_t nb_nodes, const double lon[], const double lat[], const int ghost[], |
| 70 |
const gidx_t global_index[], const idx_t remote_index[], const idx_t remote_index_base, |
| 71 |
const int partition[], |
| 72 |
size_t nb_triags, const gidx_t triag_nodes_global[], const gidx_t triag_global_index[], |
| 73 |
size_t nb_quads, const gidx_t quad_nodes_global[], const gidx_t quad_global_index[], |
| 74 |
const eckit::Configuration& config = util::NoConfig()) const; |
| 75 |
|
| 76 |
// operator (2) = slightly reordered arguments of (1) and extra global_index_base argument |
| 77 |
Mesh operator()(size_t nb_nodes, const gidx_t global_index[], |
| 78 |
const double lon[], const double lat[], |
| 79 |
const int ghost[], const int partition[], const idx_t remote_index[], const idx_t remote_index_base, |
| 80 |
size_t nb_triags, const gidx_t triag_nodes_global[], const gidx_t triag_global_index[], |
| 81 |
size_t nb_quads, const gidx_t quad_nodes_global[], const gidx_t quad_global_index[], |
| 82 |
gidx_t global_index_base, |
| 83 |
const eckit::Configuration& config = util::NoConfig()) const; |
| 84 |
|
| 85 |
// operator (3) deprecated, use (4) |
| 86 |
// Delegate to next operator with global index base = 1 |
| 87 |
[[deprecated("Use operator (4) with extra global_index_base argument")]] |
| 88 |
Mesh operator()(size_t nb_nodes, const gidx_t global_index[], |
| 89 |
const double x[], const double y[], size_t xstride, size_t ystride, |
| 90 |
const double lon[], const double lat[], size_t lonstride, size_t latstride, |
| 91 |
const int ghost[], const int partition[], const idx_t remote_index[], const idx_t remote_index_base, |
| 92 |
size_t nb_triags, const gidx_t triag_global_index[], const gidx_t triag_nodes_global[], |
| 93 |
size_t nb_quads, const gidx_t quad_global_index[], const gidx_t quad_nodes_global[], |
| 94 |
const eckit::Configuration& config = util::NoConfig()) const; |
| 95 |
|
| 96 |
// operator (4) = operator (3) with extra global_index_base argument |
| 97 |
Mesh operator()(size_t nb_nodes, const gidx_t global_index[], |
| 98 |
const double x[], const double y[], size_t xstride, size_t ystride, |
| 99 |
const double lon[], const double lat[], size_t lonstride, size_t latstride, |
| 100 |
const int ghost[], const int partition[], const idx_t remote_index[], const idx_t remote_index_base, |
| 101 |
size_t nb_triags, const gidx_t triag_global_index[], const gidx_t triag_nodes_global[], |
| 102 |
size_t nb_quads, const gidx_t quad_global_index[], const gidx_t quad_nodes_global[], |
| 103 |
gidx_t global_index_base, |
| 104 |
const eckit::Configuration& config = util::NoConfig()) const; |
| 105 |
|
| 106 |
template<typename T> |
| 107 |
using span = mdspan<T,dims<1>,layout_right>; |
| 108 |
|
| 109 |
template<typename T> |
| 110 |
using strided_span = mdspan<T,dims<1>,layout_stride>; |
| 111 |
|
| 112 |
// operator (5) |
| 113 |
Mesh operator()(span<const gidx_t> global_index, |
| 114 |
strided_span<const double> x, strided_span<const double> y, |
| 115 |
strided_span<const double> lon, strided_span<const double> lat, |
| 116 |
span<const int> ghost, span<const int> partition, |
| 117 |
span<const idx_t> remote_index, const idx_t remote_index_base, |
| 118 |
span<const gidx_t> triag_global_index, mdspan<const gidx_t, extents<size_t,dynamic_extent,3>> triag_nodes_global, |
| 119 |
span<const gidx_t> quad_global_index, mdspan<const gidx_t, extents<size_t,dynamic_extent,4>> quad_nodes_global, |
| 120 |
const gidx_t global_index_base, |
| 121 |
const eckit::Configuration& config = util::NoConfig()) const; |
| 122 |
|
| 123 |
/** |
| 124 |
* \brief C++-interface to construct a Mesh from external connectivity data |
| 125 |
* |
| 126 |
* Provides a wrapper to the C-interface using STL containers. |
| 127 |
*/ |
| 128 |
[[deprecated("Use mdspan based operator with extra global_index_base argument")]] |
| 129 |
Mesh operator()(const std::vector<double>& lons, const std::vector<double>& lats, const std::vector<int>& ghosts, |
| 130 |
const std::vector<gidx_t>& global_index, const std::vector<idx_t>& remote_index, |
| 131 |
const idx_t remote_index_base, const std::vector<int>& partitions, |
| 132 |
const std::vector<std::array<gidx_t, 3>>& triag_nodes_global, |
| 133 |
const std::vector<gidx_t>& triag_global_index, |
| 134 |
const std::vector<std::array<gidx_t, 4>>& quad_nodes_global, |
| 135 |
const std::vector<gidx_t>& quad_global_index, |
| 136 |
const eckit::Configuration& config = util::NoConfig()) const; |
| 137 |
}; |
| 138 |
|
| 139 |
//----------------------------------------------------------------------------- |
| 140 |
|
| 141 |
|
| 142 |
class TriangularMeshBuilder { |
| 143 |
public: |
| 144 |
TriangularMeshBuilder(const eckit::Configuration& config = util::NoConfig()) : |
| 145 |
meshbuilder_(config) {} |
| 146 |
|
| 147 |
/** |
| 148 |
* \brief C-interface to construct a Triangular Mesh from external connectivity data |
| 149 |
* |
| 150 |
* The inputs x, y, lons, lats, ghost, global_index, remote_index, and partition are vectors of |
| 151 |
* size nb_nodes, ranging over the nodes locally owned by (or in the ghost nodes of) the MPI |
| 152 |
* task. The global index is a uniform labeling of the nodes across all MPI tasks; the remote |
| 153 |
* index is a remote_index_base-based vector index for the node on its owning task. |
| 154 |
* |
| 155 |
* The triangle connectivities (boundary_nodes and global_index) are vectors ranging over the |
| 156 |
* cells owned by the MPI task. Each cell is defined by a list of nodes defining its boundary; |
| 157 |
* note that each boundary node must be locally known (whether as an owned of ghost node on the |
| 158 |
* MPI task), in other words, must be an element of the node global_indices. The boundary nodes |
| 159 |
* are ordered node-varies-fastest, element-varies-slowest order. The cell global index is, |
| 160 |
* here also, a uniform labeling over the of the cells across all MPI tasks. |
| 161 |
*/ |
| 162 |
[[deprecated("Use operator with extra global_index_base argument and strides")]] |
| 163 |
Mesh operator()(size_t nb_nodes, const gidx_t node_global_index[], |
| 164 |
const double x[], const double y[], |
| 165 |
const double lon[], const double lat[], |
| 166 |
size_t nb_triags, const gidx_t triangle_global_index[], const gidx_t triangle_nodes_global_index[]) const; |
| 167 |
|
| 168 |
Mesh operator()(size_t nb_nodes, const gidx_t node_global_index[], |
| 169 |
const double x[], const double y[], size_t xstride, size_t ystride, |
| 170 |
const double lon[], const double lat[], size_t lonstride, size_t latstride, |
| 171 |
size_t nb_triags, const gidx_t triangle_global_index[], const gidx_t triangle_nodes_global_index[], |
| 172 |
gidx_t global_index_base) const; |
| 173 |
|
| 174 |
private: |
| 175 |
MeshBuilder meshbuilder_; |
| 176 |
}; |
| 177 |
|
| 178 |
//----------------------------------------------------------------------------- |
| 179 |
|
| 180 |
} // namespace mesh |
| 181 |
} // namespace atlas |