Debian Codegraph @

src/atlas/grid/detail/spacing/FocusSpacing.cc

raw
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 <cmath>
12
13 #include "eckit/config/Parametrisation.h"
14
15 #include "atlas/grid/detail/spacing/FocusSpacing.h"
16 #include "atlas/grid/detail/spacing/SpacingFactory.h"
17 #include "atlas/runtime/Exception.h"
18
19 namespace atlas {
20 namespace grid {
21 namespace spacing {
22
23 FocusSpacing::FocusSpacing(const eckit::Parametrisation& params) {
24 double xmin;
25 double xmax;
26 long N;
27
28 // retrieve xmin, xmax and N from params
29 if (!params.get("start", xmin)) {
30 throw_Exception("start missing in Params", Here());
31 }
32 if (!params.get("end", xmax)) {
33 throw_Exception("end missing in Params", Here());
34 }
35 if (!params.get("N", N)) {
36 throw_Exception("N missing in Params", Here());
37 }
38
39 // additional parameters for focus spacing
40 if (!params.get("focus_factor", focus_factor_)) {
41 throw_Exception("focus_factor missing in Params", Here());
42 }
43
44 x_.resize(N);
45 if (N == 1) {
46 x_[0] = 0.5 * (xmin + xmax);
47 }
48 else {
49 const double midpoint = 0.5 * (xmin + xmax);
50 const double d2 = 2. / double(N - 1);
51 const double c1 = (xmax - xmin) * M_1_PI;
52 const double c2 = 1. / focus_factor_;
53 x_[0] = xmin;
54 x_[N - 1] = xmax;
55 for (long i = 1; i < N - 1; ++i) {
56 const double x2 = -1. + i * d2; // x2 between -1 and 1;
57 x_[i] = midpoint + c1 * std::atan(c2 * std::tan(0.5 * M_PI * x2));
58 }
59 }
60
61 min_ = std::min(xmin, xmax);
62 max_ = std::max(xmin, xmax);
63 start_ = xmin;
64 end_ = xmax;
65 }
66
67 FocusSpacing::Spec FocusSpacing::spec() const {
68 Spec spacing_specs;
69 spacing_specs.set("type", static_type());
70 spacing_specs.set("N", size());
71 spacing_specs.set("start", start_);
72 spacing_specs.set("end", end_);
73 spacing_specs.set("focus_factor", focus_factor_);
74 return spacing_specs;
75 }
76
77 namespace {
78 static SpacingBuilder<FocusSpacing> __builder(FocusSpacing::static_type());
79 }
80
81 } // namespace spacing
82 } // namespace grid
83 } // namespace atlas