| 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 |
#include "atlas/grid/StencilComputer.h" |
| 12 |
#include "atlas/grid/StructuredGrid.h" |
| 13 |
#include "atlas/runtime/Exception.h" |
| 14 |
|
| 15 |
namespace atlas { |
| 16 |
namespace grid { |
| 17 |
|
| 18 |
ComputeLower::ComputeLower(const Vertical& z): nlev_{z.size()} { |
| 19 |
z_.resize(nlev_); |
| 20 |
double dz = std::numeric_limits<double>::max(); |
| 21 |
constexpr double tol = 1.e-12; |
| 22 |
ATLAS_ASSERT(dz > 0); |
| 23 |
for (idx_t jlev = 0; jlev < nlev_; ++jlev) { |
| 24 |
if (jlev + 1 < nlev_) { |
| 25 |
dz = std::min(dz, z[jlev + 1] - z[jlev]); |
| 26 |
} |
| 27 |
z_[jlev] = z[jlev] - tol; |
| 28 |
} |
| 29 |
|
| 30 |
nlevaux_ = static_cast<idx_t>(std::round(2. * (z.max() - z.min()) / dz + 0.5) + 1); |
| 31 |
rlevaux_ = double(nlevaux_); |
| 32 |
nvaux_.resize(nlevaux_ + 1); |
| 33 |
double dzaux = (z.max() - z.min()) / rlevaux_; |
| 34 |
|
| 35 |
idx_t iref = 0; |
| 36 |
for (idx_t jlevaux = 0; jlevaux <= nlevaux_; ++jlevaux) { |
| 37 |
if (iref + 1 < nlev_ && jlevaux * dzaux >= z[iref + 1]) { |
| 38 |
++iref; |
| 39 |
} |
| 40 |
nvaux_[jlevaux] = iref; |
| 41 |
} |
| 42 |
} |
| 43 |
|
| 44 |
ComputeNorth::ComputeNorth(const StructuredGrid& grid, idx_t halo) { |
| 45 |
ATLAS_ASSERT(grid); |
| 46 |
if (not grid.domain().global()) { |
| 47 |
throw_NotImplemented("Only implemented for global grids", Here()); |
| 48 |
} |
| 49 |
halo_ = halo; |
| 50 |
ny_ = grid.ny(); |
| 51 |
y_.resize(ny_ + 2 * halo_); |
| 52 |
ATLAS_ASSERT(halo_ < ny_); |
| 53 |
idx_t north_pole_included = 90. - std::abs(grid.y().front()) < tol(); |
| 54 |
idx_t south_pole_included = 90. - std::abs(grid.y().back()) < tol(); |
| 55 |
|
| 56 |
for (idx_t j = -halo_; j < 0; ++j) { |
| 57 |
idx_t jj = -j - 1 + north_pole_included; |
| 58 |
y_[halo_ + j] = 180. - grid.y(jj) + tol(); |
| 59 |
} |
| 60 |
for (idx_t j = 0; j < ny_; ++j) { |
| 61 |
y_[halo_ + j] = grid.y(j) + tol(); |
| 62 |
} |
| 63 |
for (idx_t j = ny_; j < ny_ + halo_; ++j) { |
| 64 |
idx_t jj = 2 * ny_ - j - 1 - south_pole_included; |
| 65 |
y_[halo_ + j] = -180. - grid.y(jj) + tol(); |
| 66 |
} |
| 67 |
dy_ = std::abs(grid.y(1) - grid.y(0)); |
| 68 |
} |
| 69 |
|
| 70 |
ComputeWest::ComputeWest(const StructuredGrid& grid, idx_t halo) { |
| 71 |
ATLAS_ASSERT(grid); |
| 72 |
if (not grid.domain().global()) { |
| 73 |
throw_NotImplemented("Only implemented for global grids", Here()); |
| 74 |
} |
| 75 |
halo_ = halo; |
| 76 |
idx_t north_pole_included = 90. - std::abs(grid.y().front()) < tol(); |
| 77 |
idx_t south_pole_included = 90. - std::abs(grid.y().back()) < tol(); |
| 78 |
ny_ = grid.ny(); |
| 79 |
dx.resize(ny_ + 2 * halo_); |
| 80 |
xref.resize(ny_ + 2 * halo_); |
| 81 |
for (idx_t j = -halo_; j < 0; ++j) { |
| 82 |
idx_t jj = -j - 1 + north_pole_included; |
| 83 |
dx[halo_ + j] = grid.x(1, jj) - grid.x(0, jj); |
| 84 |
xref[halo_ + j] = grid.x(0, jj) - tol(); |
| 85 |
} |
| 86 |
for (idx_t j = 0; j < ny_; ++j) { |
| 87 |
dx[halo_ + j] = std::abs(grid.x(1, j) - grid.x(0, j)); |
| 88 |
xref[halo_ + j] = grid.x(0, j) - tol(); |
| 89 |
} |
| 90 |
for (idx_t j = ny_; j < ny_ + halo_; ++j) { |
| 91 |
idx_t jj = 2 * ny_ - j - 1 - south_pole_included; |
| 92 |
dx[halo_ + j] = std::abs(grid.x(1, jj) - grid.x(0, jj)); |
| 93 |
xref[halo_ + j] = grid.x(0, jj) - tol(); |
| 94 |
} |
| 95 |
} |
| 96 |
|
| 97 |
ComputeHorizontalStencil::ComputeHorizontalStencil(const StructuredGrid& grid, idx_t stencil_width): |
| 98 |
halo_((stencil_width + 1) / 2), |
| 99 |
compute_north_(grid, halo_), |
| 100 |
compute_west_(grid, halo_), |
| 101 |
stencil_width_(stencil_width) { |
| 102 |
stencil_begin_ = stencil_width_ - idx_t(double(stencil_width_) / 2. + 1.); |
| 103 |
} |
| 104 |
|
| 105 |
ComputeVerticalStencil::ComputeVerticalStencil(const Vertical& vertical, idx_t stencil_width): |
| 106 |
compute_lower_(vertical), stencil_width_(stencil_width) { |
| 107 |
stencil_begin_ = stencil_width_ - idx_t(double(stencil_width_) / 2. + 1.); |
| 108 |
clip_begin_ = 0; |
| 109 |
clip_end_ = vertical.size(); |
| 110 |
vertical_min_ = vertical[clip_begin_]; |
| 111 |
vertical_max_ = vertical[clip_end_ - 1]; |
| 112 |
} |
| 113 |
|
| 114 |
|
| 115 |
//--------------------------------------------------------------------------------------------------------------------- |
| 116 |
|
| 117 |
} // namespace grid |
| 118 |
} // namespace atlas |