| 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 <cmath> |
| 14 |
#include <vector> |
| 15 |
|
| 16 |
#include "atlas/grid/Vertical.h" |
| 17 |
#include "atlas/library/config.h" |
| 18 |
#include "atlas/runtime/Exception.h" |
| 19 |
|
| 20 |
namespace atlas { |
| 21 |
class StructuredGrid; |
| 22 |
} // namespace atlas |
| 23 |
|
| 24 |
namespace atlas { |
| 25 |
namespace grid { |
| 26 |
|
| 27 |
class ComputeLower { |
| 28 |
std::vector<double> z_; |
| 29 |
std::vector<idx_t> nvaux_; |
| 30 |
idx_t nlev_; |
| 31 |
idx_t nlevaux_; |
| 32 |
double rlevaux_; |
| 33 |
|
| 34 |
public: |
| 35 |
ComputeLower() = default; |
| 36 |
|
| 37 |
ComputeLower(const Vertical& z); |
| 38 |
|
| 39 |
idx_t operator()(double z) const { |
| 40 |
idx_t idx = static_cast<idx_t>(std::floor(z * rlevaux_)); |
| 41 |
#ifndef NDEBUG |
| 42 |
ATLAS_ASSERT(idx < static_cast<idx_t>(nvaux_.size()) && idx >= 0); |
| 43 |
#endif |
| 44 |
idx = nvaux_[idx]; |
| 45 |
if (idx < nlev_ - 1 && z > z_[idx + 1]) { |
| 46 |
++idx; |
| 47 |
} |
| 48 |
return idx; |
| 49 |
} |
| 50 |
}; |
| 51 |
|
| 52 |
//----------------------------------------------------------------------------- |
| 53 |
|
| 54 |
class ComputeNorth { |
| 55 |
std::vector<double> y_; |
| 56 |
double dy_; |
| 57 |
idx_t halo_; |
| 58 |
idx_t ny_; |
| 59 |
static constexpr double tol() { return 0.5e-6; } |
| 60 |
|
| 61 |
public: |
| 62 |
ComputeNorth() = default; |
| 63 |
|
| 64 |
ComputeNorth(const StructuredGrid& grid, idx_t halo); |
| 65 |
|
| 66 |
idx_t operator()(double y) const { |
| 67 |
idx_t j = static_cast<idx_t>(std::floor((y_[halo_ + 0] - y) / dy_)); |
| 68 |
j = std::max<idx_t>(halo_, std::min<idx_t>(j, halo_ + ny_ - 1)); |
| 69 |
while (y_[halo_ + j] > y) { |
| 70 |
++j; |
| 71 |
} |
| 72 |
do { |
| 73 |
--j; |
| 74 |
} while (y_[halo_ + j] < y); |
| 75 |
|
| 76 |
return j; |
| 77 |
} |
| 78 |
}; |
| 79 |
|
| 80 |
//----------------------------------------------------------------------------- |
| 81 |
|
| 82 |
class ComputeWest { |
| 83 |
std::vector<double> dx; |
| 84 |
std::vector<double> xref; |
| 85 |
idx_t halo_; // halo in north-south direction |
| 86 |
idx_t ny_; |
| 87 |
static constexpr double tol() { return 0.5e-6; } |
| 88 |
|
| 89 |
public: |
| 90 |
ComputeWest() = default; |
| 91 |
|
| 92 |
ComputeWest(const StructuredGrid& grid, idx_t halo = 0); |
| 93 |
|
| 94 |
idx_t operator()(const double& x, idx_t j) const { |
| 95 |
idx_t jj = halo_ + j; |
| 96 |
idx_t i = static_cast<idx_t>(std::floor((x - xref[jj]) / dx[jj])); |
| 97 |
return i; |
| 98 |
} |
| 99 |
}; |
| 100 |
|
| 101 |
|
| 102 |
//----------------------------------------------------------------------------- |
| 103 |
|
| 104 |
/// @class ComputeHorizontalStencil |
| 105 |
/// @brief Compute stencil in horizontal direction (i,j) |
| 106 |
/// |
| 107 |
/// @details |
| 108 |
/// Given a stencil width, the stencil for a given P{x,y} is: |
| 109 |
/// @code |
| 110 |
/// i[0] i[1] i[2] i[3] |
| 111 |
/// x x x x j + 0 |
| 112 |
/// x x x x j + 1 |
| 113 |
/// P |
| 114 |
/// x x x x j + 2 |
| 115 |
/// x x x x j + 3 |
| 116 |
/// @endcode |
| 117 |
/// |
| 118 |
/// In case the x-component of P is aligned with any |
| 119 |
/// stencil, gridpoint, the stencil will assume the grid point |
| 120 |
/// is on the point P's left side: |
| 121 |
/// @code |
| 122 |
/// i[0] i[1] i[2] i[3] |
| 123 |
/// x x x x j + 0 |
| 124 |
/// x x x x j + 1 |
| 125 |
/// P |
| 126 |
/// x x x x j + 2 |
| 127 |
/// x x x x j + 3 |
| 128 |
/// @endcode |
| 129 |
class ComputeHorizontalStencil { |
| 130 |
idx_t halo_; |
| 131 |
ComputeNorth compute_north_; |
| 132 |
ComputeWest compute_west_; |
| 133 |
idx_t stencil_width_; |
| 134 |
idx_t stencil_begin_; |
| 135 |
|
| 136 |
public: |
| 137 |
ComputeHorizontalStencil() = default; |
| 138 |
|
| 139 |
ComputeHorizontalStencil(const StructuredGrid& grid, idx_t stencil_width); |
| 140 |
|
| 141 |
template <typename stencil_t> |
| 142 |
void operator()(const double& x, const double& y, stencil_t& stencil) const { |
| 143 |
stencil.j_begin_ = compute_north_(y) - stencil_begin_; |
| 144 |
for (idx_t jj = 0; jj < stencil_width_; ++jj) { |
| 145 |
stencil.i_begin_[jj] = compute_west_(x, stencil.j_begin_ + jj) - stencil_begin_; |
| 146 |
} |
| 147 |
} |
| 148 |
}; |
| 149 |
|
| 150 |
|
| 151 |
//----------------------------------------------------------------------------- |
| 152 |
|
| 153 |
//--------------------------------------------------------------------------------------------------------------------- |
| 154 |
/// @class ComputeVerticalStencil |
| 155 |
/// @brief Compute lower vertical level index for given coordinate |
| 156 |
/// zcoord: |
| 157 |
/// @verbatim |
| 158 |
/// 0----1----2----3--...--(n-1)----(n)----(n+1) |
| 159 |
/// --->|<---|<---|<--...-|<-------------------- |
| 160 |
/// @endverbatim |
| 161 |
/// If coordinate falls on vertical level (+- epsilon), that level is returned |
| 162 |
/// If coordinate falls in range [0,1) or [n,n+1], |
| 163 |
/// the index is snapped to 1 and (n-1) respectively. This allows reliably that |
| 164 |
/// the returned index can be used for stencil operations. |
| 165 |
/// |
| 166 |
/// IFS full levels don't have a level at the boundaries (0.,1.) |
| 167 |
/// It is the "half" levels that contain (0.,1.). For reasons of boundary conditions |
| 168 |
/// however, the full levels also have 0. prepended and 1. appended. |
| 169 |
/// |
| 170 |
/// Example IFS full levels for regular distribution dz ( level 0 and n+1 are added for boundary conditions ) |
| 171 |
/// 0 : 0.0 |
| 172 |
/// jlev : jlev*dz - 0.5*dz |
| 173 |
/// nlev : nlev*dz - 0.5*dz |
| 174 |
/// nlev+1 : 1.0 |
| 175 |
class ComputeVerticalStencil { |
| 176 |
ComputeLower compute_lower_; |
| 177 |
idx_t stencil_width_; |
| 178 |
idx_t stencil_begin_; |
| 179 |
|
| 180 |
idx_t clip_begin_; |
| 181 |
idx_t clip_end_; |
| 182 |
double vertical_min_; |
| 183 |
double vertical_max_; |
| 184 |
|
| 185 |
public: |
| 186 |
ComputeVerticalStencil() = default; |
| 187 |
|
| 188 |
ComputeVerticalStencil(const Vertical& vertical, idx_t stencil_width); |
| 189 |
|
| 190 |
template <typename stencil_t> |
| 191 |
void operator()(const double& z, stencil_t& stencil) const { |
| 192 |
idx_t k_begin = compute_lower_(z) - stencil_begin_; |
| 193 |
idx_t k_end = k_begin + stencil_width_; |
| 194 |
idx_t move = 0; |
| 195 |
|
| 196 |
if (k_begin < clip_begin_) { |
| 197 |
move = k_begin - clip_begin_; |
| 198 |
if (z < vertical_min_) { |
| 199 |
--k_begin; |
| 200 |
--move; |
| 201 |
} |
| 202 |
} |
| 203 |
else if (k_end > clip_end_) { |
| 204 |
move = k_end - clip_end_; |
| 205 |
} |
| 206 |
stencil.k_begin_ = k_begin - move; |
| 207 |
stencil.k_interval_ = stencil_begin_ + move; |
| 208 |
} |
| 209 |
}; |
| 210 |
|
| 211 |
//--------------------------------------------------------------------------------------------------------------------- |
| 212 |
|
| 213 |
} // namespace grid |
| 214 |
} // namespace atlas |