| 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/detail/spacing/LinearSpacing.h" |
| 12 |
|
| 13 |
#include <cmath> |
| 14 |
#include <ostream> |
| 15 |
|
| 16 |
|
| 17 |
#include "eckit/config/Parametrisation.h" |
| 18 |
|
| 19 |
#include "atlas/grid/detail/spacing/SpacingFactory.h" |
| 20 |
#include "atlas/runtime/Exception.h" |
| 21 |
|
| 22 |
namespace atlas { |
| 23 |
namespace grid { |
| 24 |
namespace spacing { |
| 25 |
|
| 26 |
LinearSpacing::Params::Params(const eckit::Parametrisation& params) { |
| 27 |
endpoint = true; |
| 28 |
params.get("endpoint", endpoint); |
| 29 |
std::vector<double> interval; |
| 30 |
if (params.get("N", N)) { |
| 31 |
// Only one remaining combinations possible: |
| 32 |
if (params.get("start", start) && params.get("end", end)) { |
| 33 |
// OK |
| 34 |
} |
| 35 |
else if (params.get("interval", interval)) { |
| 36 |
start = interval[0]; |
| 37 |
end = interval[1]; |
| 38 |
} |
| 39 |
else if (params.get("start", start) && params.get("length", length)) { |
| 40 |
end = start + length; |
| 41 |
} |
| 42 |
else { |
| 43 |
throw_Exception("Invalid combination of parameters", Here()); |
| 44 |
} |
| 45 |
} |
| 46 |
else { |
| 47 |
throw_Exception("Invalid combination of parameters", Here()); |
| 48 |
} |
| 49 |
length = end - start; |
| 50 |
|
| 51 |
if (endpoint && N > 1) { |
| 52 |
step = length / double(N - 1); |
| 53 |
} |
| 54 |
else { |
| 55 |
step = length / double(N); |
| 56 |
} |
| 57 |
} |
| 58 |
|
| 59 |
LinearSpacing::Params::Params(double _start, double _end, long _N, bool _endpoint): |
| 60 |
start(_start), end(_end), N(_N), endpoint(_endpoint) { |
| 61 |
length = end - start; |
| 62 |
if (endpoint && N > 1) { |
| 63 |
step = length / double(N - 1); |
| 64 |
} |
| 65 |
else { |
| 66 |
step = length / double(N); |
| 67 |
} |
| 68 |
} |
| 69 |
|
| 70 |
LinearSpacing::LinearSpacing(const eckit::Parametrisation& params) { |
| 71 |
Params p(params); |
| 72 |
setup(p.start, p.end, p.N, p.endpoint); |
| 73 |
} |
| 74 |
|
| 75 |
LinearSpacing::LinearSpacing(double start, double end, long N, bool endpoint) { |
| 76 |
setup(start, end, N, endpoint); |
| 77 |
} |
| 78 |
|
| 79 |
LinearSpacing::LinearSpacing(const Interval& interval, long N, bool endpoint) { |
| 80 |
setup(interval[0], interval[1], N, endpoint); |
| 81 |
} |
| 82 |
|
| 83 |
void LinearSpacing::setup(double start, double end, long N, bool endpoint) { |
| 84 |
x_.resize(N); |
| 85 |
|
| 86 |
double step; |
| 87 |
volatile double _N = N; // volatile keyword prevents agressive optimization by Cray compiler |
| 88 |
// deepcode ignore FloatingPointEquals: Expect possible bit-identical start and end |
| 89 |
if (start == end) { |
| 90 |
step = 0.; |
| 91 |
} |
| 92 |
else if (endpoint && N > 1) { |
| 93 |
step = (end - start) / (_N - 1); |
| 94 |
} |
| 95 |
else { |
| 96 |
step = (end - start) / _N; |
| 97 |
} |
| 98 |
|
| 99 |
for (long i = 0; i < N; ++i) { |
| 100 |
x_[i] = start + i * step; |
| 101 |
} |
| 102 |
|
| 103 |
min_ = std::min(start, end); |
| 104 |
max_ = std::max(start, end); |
| 105 |
|
| 106 |
start_ = start; |
| 107 |
end_ = end; |
| 108 |
N_ = N; |
| 109 |
endpoint_ = endpoint; |
| 110 |
|
| 111 |
// For exact comparisons: |
| 112 |
if (N > 1) { |
| 113 |
x_.front() = start; |
| 114 |
} |
| 115 |
if (N > 2 && endpoint) { |
| 116 |
x_.back() = end; |
| 117 |
} |
| 118 |
} |
| 119 |
|
| 120 |
double LinearSpacing::step() const { |
| 121 |
if (size() > 1) { |
| 122 |
return x_[1] - x_[0]; |
| 123 |
} |
| 124 |
else { |
| 125 |
return 0.; |
| 126 |
} |
| 127 |
} |
| 128 |
|
| 129 |
bool LinearSpacing::endpoint() const { |
| 130 |
return std::abs(x_.back() - max_) < 1.e-12; |
| 131 |
} |
| 132 |
|
| 133 |
LinearSpacing::Spec LinearSpacing::spec() const { |
| 134 |
Spec spacing_specs; |
| 135 |
spacing_specs.set("type", static_type()); |
| 136 |
spacing_specs.set("start", start_); |
| 137 |
spacing_specs.set("end", end_); |
| 138 |
spacing_specs.set("N", N_); |
| 139 |
spacing_specs.set("endpoint", endpoint_); |
| 140 |
return spacing_specs; |
| 141 |
} |
| 142 |
|
| 143 |
namespace { |
| 144 |
static SpacingBuilder<LinearSpacing> __builder(LinearSpacing::static_type()); |
| 145 |
} |
| 146 |
|
| 147 |
} // namespace spacing |
| 148 |
} // namespace grid |
| 149 |
} // namespace atlas |