| 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 <limits> |
| 9 |
|
| 10 |
#include "atlas/functionspace/detail/CubedSphereStructure.h" |
| 11 |
#include "atlas/runtime/Log.h" |
| 12 |
#include "atlas/runtime/Trace.h" |
| 13 |
|
| 14 |
using namespace atlas::meshgenerator::detail::cubedsphere; |
| 15 |
|
| 16 |
namespace atlas { |
| 17 |
namespace functionspace { |
| 18 |
namespace detail { |
| 19 |
|
| 20 |
namespace { |
| 21 |
#if ATLAS_BUILD_TYPE_DEBUG |
| 22 |
constexpr bool checkBounds = true; |
| 23 |
#else |
| 24 |
constexpr bool checkBounds = false; |
| 25 |
#endif |
| 26 |
} // namespace |
| 27 |
|
| 28 |
CubedSphereStructure::BoundingBox::BoundingBox() { |
| 29 |
iBegin = std::numeric_limits<idx_t>::max(); |
| 30 |
iEnd = std::numeric_limits<idx_t>::min(); |
| 31 |
jBegin = std::numeric_limits<idx_t>::max(); |
| 32 |
jEnd = std::numeric_limits<idx_t>::min(); |
| 33 |
} |
| 34 |
|
| 35 |
CubedSphereStructure::CubedSphereStructure(const Field& tij, const Field& ghost, idx_t size): |
| 36 |
tij_(tij), ghost_(ghost), nElems_(size) { |
| 37 |
ATLAS_TRACE(); |
| 38 |
Log::debug() << "CubedSphereStructure bounds checking is set to " + std::to_string(checkBounds) << std::endl; |
| 39 |
|
| 40 |
// Make array views. |
| 41 |
const auto tijView_ = array::make_view<idx_t, 2>(tij_); |
| 42 |
const auto ghostView_ = array::make_view<int, 1>(ghost_); |
| 43 |
|
| 44 |
// loop over tij and find min and max ij bounds. |
| 45 |
for (idx_t index = 0; index < nElems_; ++index) { |
| 46 |
const size_t t = static_cast<size_t>(tijView_(index, Coordinates::T)); |
| 47 |
const idx_t i = tijView_(index, Coordinates::I); |
| 48 |
const idx_t j = tijView_(index, Coordinates::J); |
| 49 |
|
| 50 |
ijBounds_[t].iBegin = std::min(i, ijBounds_[t].iBegin); |
| 51 |
ijBounds_[t].jBegin = std::min(j, ijBounds_[t].jBegin); |
| 52 |
ijBounds_[t].iEnd = std::max(i + 1, ijBounds_[t].iEnd); |
| 53 |
ijBounds_[t].jEnd = std::max(j + 1, ijBounds_[t].jEnd); |
| 54 |
|
| 55 |
// Keep track of highest non-ghost index. |
| 56 |
if (!ghostView_(index)) { |
| 57 |
nOwnedElems_ = index + 1; |
| 58 |
} |
| 59 |
} |
| 60 |
|
| 61 |
// Set tijToIdx vectors |
| 62 |
for (idx_t t = 0; t < 6; ++t) { |
| 63 |
// Set data array. |
| 64 |
size_t vecSize = 0; |
| 65 |
if( j_end(t) >= j_begin(t) && i_end(t) >= i_begin(t) ) { |
| 66 |
vecSize = static_cast<size_t>((j_end(t) - j_begin(t)) * (i_end(t) - i_begin(t))); |
| 67 |
} |
| 68 |
tijToIdx_.emplace_back(vecSize, invalid_index()); |
| 69 |
} |
| 70 |
|
| 71 |
// loop over ijt_ and set ijtToIdx |
| 72 |
for (idx_t index = 0; index < nElems_; ++index) { |
| 73 |
const idx_t t = tijView_(index, Coordinates::T); |
| 74 |
const idx_t i = tijView_(index, Coordinates::I); |
| 75 |
const idx_t j = tijView_(index, Coordinates::J); |
| 76 |
|
| 77 |
tijToIdx_[static_cast<size_t>(t)][vecIndex(t, i, j)] = index; |
| 78 |
} |
| 79 |
} |
| 80 |
|
| 81 |
idx_t CubedSphereStructure::size() const { |
| 82 |
return nElems_; |
| 83 |
} |
| 84 |
|
| 85 |
idx_t CubedSphereStructure::sizeOwned() const { |
| 86 |
return nOwnedElems_; |
| 87 |
} |
| 88 |
|
| 89 |
idx_t CubedSphereStructure::i_begin(idx_t t) const { |
| 90 |
if (checkBounds) { |
| 91 |
tBoundsCheck(t); |
| 92 |
} |
| 93 |
return ijBounds_[static_cast<size_t>(t)].iBegin; |
| 94 |
} |
| 95 |
|
| 96 |
idx_t CubedSphereStructure::i_end(idx_t t) const { |
| 97 |
if (checkBounds) { |
| 98 |
tBoundsCheck(t); |
| 99 |
} |
| 100 |
return ijBounds_[static_cast<size_t>(t)].iEnd; |
| 101 |
} |
| 102 |
|
| 103 |
idx_t CubedSphereStructure::j_begin(idx_t t) const { |
| 104 |
if (checkBounds) { |
| 105 |
tBoundsCheck(t); |
| 106 |
} |
| 107 |
return ijBounds_[static_cast<size_t>(t)].jBegin; |
| 108 |
} |
| 109 |
|
| 110 |
idx_t CubedSphereStructure::j_end(idx_t t) const { |
| 111 |
if (checkBounds) { |
| 112 |
tBoundsCheck(t); |
| 113 |
} |
| 114 |
return ijBounds_[static_cast<size_t>(t)].jEnd; |
| 115 |
} |
| 116 |
|
| 117 |
idx_t CubedSphereStructure::index(idx_t t, idx_t i, idx_t j) const { |
| 118 |
if (checkBounds) { |
| 119 |
iBoundsCheck(i, t); |
| 120 |
jBoundsCheck(j, t); |
| 121 |
} |
| 122 |
|
| 123 |
return tijToIdx_[static_cast<size_t>(t)][vecIndex(t, i, j)]; |
| 124 |
} |
| 125 |
|
| 126 |
bool CubedSphereStructure::is_valid_index(idx_t t, idx_t i, idx_t j) const { |
| 127 |
// Check if t is in range. |
| 128 |
if (t < 0 || t > 5) { |
| 129 |
return false; |
| 130 |
} |
| 131 |
|
| 132 |
// Check if i and j are in range in index method. |
| 133 |
if (i < i_begin(t) || i >= i_end(t) || j < j_begin(t) || j >= j_end(t)) { |
| 134 |
return false; |
| 135 |
} |
| 136 |
|
| 137 |
// Check if (t, i, j) is a valid index. |
| 138 |
if (index(t, i, j) == invalid_index()) { |
| 139 |
return false; |
| 140 |
} |
| 141 |
|
| 142 |
return true; |
| 143 |
} |
| 144 |
|
| 145 |
Field CubedSphereStructure::tij() const { |
| 146 |
return tij_; |
| 147 |
} |
| 148 |
|
| 149 |
Field CubedSphereStructure::ghost() const { |
| 150 |
return ghost_; |
| 151 |
} |
| 152 |
|
| 153 |
void CubedSphereStructure::tBoundsCheck(idx_t t) const { |
| 154 |
if (t < 0 || t > 5) { |
| 155 |
throw_OutOfRange("t", t, 6); |
| 156 |
} |
| 157 |
} |
| 158 |
|
| 159 |
void CubedSphereStructure::jBoundsCheck(idx_t j, idx_t t) const { |
| 160 |
const idx_t jSize = j_end(t) - j_begin(t); |
| 161 |
j -= j_begin(t); |
| 162 |
if (j < 0 || j >= jSize) { |
| 163 |
throw_OutOfRange("j - jMin", j, jSize); |
| 164 |
} |
| 165 |
} |
| 166 |
|
| 167 |
void CubedSphereStructure::iBoundsCheck(idx_t i, idx_t t) const { |
| 168 |
const idx_t iSize = i_end(t) - i_begin(t); |
| 169 |
i -= i_begin(t); |
| 170 |
if (i < 0 || i >= iSize) { |
| 171 |
throw_OutOfRange("i - iMin", i, iSize); |
| 172 |
} |
| 173 |
} |
| 174 |
|
| 175 |
size_t CubedSphereStructure::vecIndex(idx_t t, idx_t i, idx_t j) const { |
| 176 |
return static_cast<size_t>((j - j_begin(t)) * (i_end(t) - i_begin(t)) + i - i_begin(t)); |
| 177 |
} |
| 178 |
|
| 179 |
|
| 180 |
} // namespace detail |
| 181 |
} // namespace functionspace |
| 182 |
} // namespace atlas |