| 1 |
/* |
| 2 |
* (C) Crown Copyright 2021 Met Office |
| 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 |
#include "atlas/functionspace/CubedSphereColumns.h" |
| 9 |
#include "atlas/field/Field.h" |
| 10 |
#include "atlas/mesh/HybridElements.h" |
| 11 |
#include "atlas/mesh/Mesh.h" |
| 12 |
#include "atlas/mesh/Nodes.h" |
| 13 |
#include "atlas/util/detail/Cache.h" |
| 14 |
|
| 15 |
namespace atlas { |
| 16 |
namespace functionspace { |
| 17 |
|
| 18 |
|
| 19 |
// Helper functions to get fields. |
| 20 |
namespace { |
| 21 |
template <typename BaseFunctionSpace> |
| 22 |
Field getTij(const Mesh& mesh); |
| 23 |
|
| 24 |
template <typename BaseFunctionSpace> |
| 25 |
Field getGhost(const Mesh& mesh); |
| 26 |
|
| 27 |
template <> |
| 28 |
Field getTij<NodeColumns>(const Mesh& mesh) { |
| 29 |
return mesh.nodes().field("tij"); |
| 30 |
} |
| 31 |
|
| 32 |
template <> |
| 33 |
Field getTij<CellColumns>(const Mesh& mesh) { |
| 34 |
return mesh.cells().field("tij"); |
| 35 |
} |
| 36 |
|
| 37 |
template <> |
| 38 |
Field getGhost<NodeColumns>(const Mesh& mesh) { |
| 39 |
return mesh.nodes().ghost(); |
| 40 |
} |
| 41 |
|
| 42 |
template <> |
| 43 |
Field getGhost<CellColumns>(const Mesh& mesh) { |
| 44 |
// No ghost field for CellColumns. Halo field is next best thing. |
| 45 |
return mesh.cells().halo(); |
| 46 |
} |
| 47 |
} // namespace |
| 48 |
|
| 49 |
namespace { |
| 50 |
template <typename BaseFunctionSpace> |
| 51 |
class CubedSphereStructureCache : public util::Cache<std::string, detail::CubedSphereStructure>, |
| 52 |
public mesh::detail::MeshObserver { |
| 53 |
private: |
| 54 |
using Base = util::Cache<std::string, detail::CubedSphereStructure>; |
| 55 |
CubedSphereStructureCache(): Base("CubedSphereStructureCache<" + BaseFunctionSpace::type() + ">") {} |
| 56 |
|
| 57 |
public: |
| 58 |
static CubedSphereStructureCache& instance() { |
| 59 |
static CubedSphereStructureCache inst; |
| 60 |
return inst; |
| 61 |
} |
| 62 |
util::ObjectHandle<value_type> get_or_create(const BaseFunctionSpace* functionspace) { |
| 63 |
ATLAS_ASSERT(functionspace); |
| 64 |
auto& mesh = functionspace->mesh(); |
| 65 |
ATLAS_ASSERT(mesh); |
| 66 |
auto& mesh_impl = *mesh.get(); |
| 67 |
registerMesh(mesh_impl); |
| 68 |
creator_type creator = std::bind(&CubedSphereStructureCache::create, mesh, functionspace->size()); |
| 69 |
util::ObjectHandle<value_type> value = Base::get_or_create(key(mesh_impl), creator); |
| 70 |
return value; |
| 71 |
} |
| 72 |
void onMeshDestruction(mesh::detail::MeshImpl& mesh) override { remove(key(mesh)); } |
| 73 |
|
| 74 |
private: |
| 75 |
static Base::key_type key(const mesh::detail::MeshImpl& mesh) { |
| 76 |
std::ostringstream key; |
| 77 |
key << "mesh[address=" << &mesh << "]"; |
| 78 |
return key.str(); |
| 79 |
} |
| 80 |
|
| 81 |
static value_type* create(const Mesh& mesh, idx_t size) { |
| 82 |
value_type* value = new value_type(getTij<BaseFunctionSpace>(mesh), getGhost<BaseFunctionSpace>(mesh), size); |
| 83 |
return value; |
| 84 |
} |
| 85 |
}; |
| 86 |
|
| 87 |
|
| 88 |
} // namespace |
| 89 |
|
| 90 |
// All constructors pass arguments through to BaseFunctionSpace, then construct |
| 91 |
// CubedSphereStructure. |
| 92 |
template <typename BaseFunctionSpace> |
| 93 |
CubedSphereColumns<BaseFunctionSpace>::CubedSphereColumns(): |
| 94 |
BaseFunctionSpace(), cubedSphereColumnsHandle_(new detail::CubedSphereStructure()) {} |
| 95 |
|
| 96 |
template <typename BaseFunctionSpace> |
| 97 |
CubedSphereColumns<BaseFunctionSpace>::CubedSphereColumns(const FunctionSpace& functionspace): |
| 98 |
BaseFunctionSpace([&]() { |
| 99 |
bool compatible = dynamic_cast<const typename BaseFunctionSpace::Implementation*>(functionspace.get()); |
| 100 |
if (not compatible) { |
| 101 |
ATLAS_THROW_EXCEPTION("FunctionSpace " << functionspace.type() << " can not be interpreted as a " |
| 102 |
<< BaseFunctionSpace::type()); |
| 103 |
} |
| 104 |
return functionspace; |
| 105 |
}()), |
| 106 |
cubedSphereColumnsHandle_(CubedSphereStructureCache<BaseFunctionSpace>::instance().get_or_create(this)) {} |
| 107 |
|
| 108 |
template <typename BaseFunctionSpace> |
| 109 |
CubedSphereColumns<BaseFunctionSpace>::CubedSphereColumns(const Mesh& mesh, const eckit::Configuration& configuration): |
| 110 |
BaseFunctionSpace(mesh, configuration), |
| 111 |
cubedSphereColumnsHandle_(CubedSphereStructureCache<BaseFunctionSpace>::instance().get_or_create(this)) {} |
| 112 |
|
| 113 |
template <typename BaseFunctionSpace> |
| 114 |
CubedSphereColumns<BaseFunctionSpace>::CubedSphereColumns(const Mesh& mesh): |
| 115 |
BaseFunctionSpace(mesh), |
| 116 |
cubedSphereColumnsHandle_(CubedSphereStructureCache<BaseFunctionSpace>::instance().get_or_create(this)) {} |
| 117 |
|
| 118 |
template <typename BaseFunctionSpace> |
| 119 |
idx_t CubedSphereColumns<BaseFunctionSpace>::invalid_index() const { |
| 120 |
return cubedSphereColumnsHandle_.get()->invalid_index(); |
| 121 |
} |
| 122 |
|
| 123 |
template <typename BaseFunctionSpace> |
| 124 |
idx_t CubedSphereColumns<BaseFunctionSpace>::sizeOwned() const { |
| 125 |
return cubedSphereColumnsHandle_.get()->sizeOwned(); |
| 126 |
} |
| 127 |
|
| 128 |
template <typename BaseFunctionSpace> |
| 129 |
idx_t CubedSphereColumns<BaseFunctionSpace>::i_begin(idx_t t) const { |
| 130 |
return cubedSphereColumnsHandle_.get()->i_begin(t); |
| 131 |
} |
| 132 |
|
| 133 |
template <typename BaseFunctionSpace> |
| 134 |
idx_t CubedSphereColumns<BaseFunctionSpace>::i_end(idx_t t) const { |
| 135 |
return cubedSphereColumnsHandle_.get()->i_end(t); |
| 136 |
} |
| 137 |
|
| 138 |
template <typename BaseFunctionSpace> |
| 139 |
idx_t CubedSphereColumns<BaseFunctionSpace>::j_begin(idx_t t) const { |
| 140 |
return cubedSphereColumnsHandle_.get()->j_begin(t); |
| 141 |
} |
| 142 |
|
| 143 |
template <typename BaseFunctionSpace> |
| 144 |
idx_t CubedSphereColumns<BaseFunctionSpace>::j_end(idx_t t) const { |
| 145 |
return cubedSphereColumnsHandle_.get()->j_end(t); |
| 146 |
} |
| 147 |
|
| 148 |
template <typename BaseFunctionSpace> |
| 149 |
idx_t CubedSphereColumns<BaseFunctionSpace>::index(idx_t t, idx_t i, idx_t j) const { |
| 150 |
return cubedSphereColumnsHandle_.get()->index(t, i, j); |
| 151 |
} |
| 152 |
|
| 153 |
template <typename BaseFunctionSpace> |
| 154 |
bool CubedSphereColumns<BaseFunctionSpace>::is_valid_index(idx_t t, idx_t i, idx_t j) const { |
| 155 |
return cubedSphereColumnsHandle_.get()->is_valid_index(t, i, j); |
| 156 |
} |
| 157 |
|
| 158 |
template <typename BaseFunctionSpace> |
| 159 |
Field CubedSphereColumns<BaseFunctionSpace>::tij() const { |
| 160 |
return cubedSphereColumnsHandle_.get()->tij(); |
| 161 |
} |
| 162 |
|
| 163 |
// Explicit instantiation of template classes. |
| 164 |
template class CubedSphereColumns<CellColumns>; |
| 165 |
template class CubedSphereColumns<NodeColumns>; |
| 166 |
|
| 167 |
} // namespace functionspace |
| 168 |
} // namespace atlas |