Debian Codegraph @

src/atlas/mesh/actions/BuildTorusXYZField.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 "atlas/array/ArrayView.h"
14 #include "atlas/domain/detail/GlobalDomain.h"
15 #include "atlas/domain/detail/RectangularDomain.h"
16 #include "atlas/field/Field.h"
17 #include "atlas/mesh/Mesh.h"
18 #include "atlas/mesh/Nodes.h"
19 #include "atlas/mesh/actions/BuildTorusXYZField.h"
20 #include "atlas/runtime/Exception.h"
21
22 namespace atlas {
23 namespace mesh {
24 namespace actions {
25
26 //----------------------------------------------------------------------------------------------------------------------
27
28 BuildTorusXYZField::BuildTorusXYZField(const std::string& name): name_(name) {}
29
30 Field& BuildTorusXYZField::operator()(Mesh& mesh, const Domain& dom, double r0, double r1, idx_t nx, idx_t ny) const {
31 return operator()(mesh.nodes(), dom, r0, r1, nx, ny);
32 }
33
34 Field& BuildTorusXYZField::operator()(mesh::Nodes& nodes, const Domain& dom, double r0, double r1, idx_t nx,
35 idx_t ny) const {
36 // fill xyz with torus coordinates. r0 and r1 are large and small radii,
37 // respectively.
38
39 auto domain = RectangularDomain(dom);
40 ATLAS_ASSERT(domain);
41 const double xmin = domain.xmin();
42 const double xmax = domain.xmax();
43 const double ymin = domain.ymin();
44 const double ymax = domain.ymax();
45
46 if (!nodes.has_field(name_)) {
47 const idx_t npts = nodes.size();
48 const array::ArrayView<double, 2> lonlat = array::make_view<double, 2>(nodes.xy());
49 array::ArrayView<double, 2> xyz = array::make_view<double, 2>(
50 nodes.add(Field(name_, array::make_datatype<double>(), array::make_shape(npts, 3))));
51
52 const double pi = M_PI;
53 const double c1 = 2. * pi / double(nx) * (nx - 1) / (xmax - xmin);
54 const double c2 = 2. * pi / double(ny) * (ny - 1) / (ymax - ymin);
55 for (idx_t n = 0; n < npts; ++n) {
56 double lon = -pi + c1 * (lonlat(n, 0) - xmin);
57 double lat = -pi + c2 * (lonlat(n, 1) - ymin);
58
59 xyz(n, 0) = std::cos(lon) * (r0 + r1 * std::cos(lat));
60 xyz(n, 1) = std::sin(lon) * (r0 + r1 * std::cos(lat));
61 xyz(n, 2) = r1 * std::sin(lat);
62 }
63 }
64 return nodes.field(name_);
65 }
66
67 //----------------------------------------------------------------------------------------------------------------------
68
69 } // namespace actions
70 } // namespace mesh
71 } // namespace atlas