Debian Codegraph @

src/atlas/numerics/fvm/Method.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/array/MakeView.h"
15 #include "atlas/functionspace/EdgeColumns.h"
16 #include "atlas/functionspace/NodeColumns.h"
17 #include "atlas/mesh/HybridElements.h"
18 #include "atlas/mesh/Mesh.h"
19 #include "atlas/mesh/Nodes.h"
20 #include "atlas/mesh/actions/BuildDualMesh.h"
21 #include "atlas/mesh/actions/BuildEdges.h"
22 #include "atlas/mesh/actions/BuildParallelFields.h"
23 #include "atlas/numerics/fvm/Method.h"
24 #include "atlas/parallel/omp/omp.h"
25 #include "atlas/runtime/Exception.h"
26 #include "atlas/runtime/Trace.h"
27 #include "atlas/util/CoordinateEnums.h"
28 #include "atlas/util/Earth.h"
29
30 // =======================================================
31
32 using namespace atlas::mesh::actions;
33
34 namespace atlas {
35 namespace numerics {
36 namespace fvm {
37
38 namespace {
39
40 mesh::Halo get_halo(const eckit::Parametrisation& params) {
41 size_t halo_size(1);
42 params.get("halo", halo_size);
43 return mesh::Halo(halo_size);
44 }
45
46 size_t get_levels(const eckit::Parametrisation& params) {
47 size_t levels(0);
48 params.get("levels", levels);
49 return levels;
50 }
51
52 double get_radius(const eckit::Parametrisation& params) {
53 double radius = util::Earth::radius();
54 params.get("radius", radius);
55 return radius;
56 }
57
58 } // namespace
59
60 Method::Method(Mesh& mesh): Method::Method(mesh, util::NoConfig()) {}
61
62 Method::Method(Mesh& mesh, const mesh::Halo& halo): Method::Method(mesh, util::Config("halo", halo.size())) {}
63
64 Method::Method(Mesh& mesh, const eckit::Configuration& params):
65 mesh_(mesh),
66 levels_(get_levels(params)),
67 halo_(get_halo(params)),
68 nodes_(mesh.nodes()),
69 edges_(mesh.edges()),
70 radius_(get_radius(params)) {
71 setup();
72 }
73
74 void Method::setup() {
75 ATLAS_TRACE("fvm::Method::setup ");
76 util::Config config;
77 config.set("halo", halo_.size());
78 if (levels_) {
79 config.set("levels", levels_);
80 }
81 node_columns_ = functionspace::NodeColumns(mesh(), config);
82 edge_columns_ = functionspace::EdgeColumns(mesh(), config);
83
84 {
85 ATLAS_TRACE_SCOPE("build_median_dual_mesh") build_median_dual_mesh(mesh());
86 ATLAS_TRACE_SCOPE("build_node_to_edge_connectivity") build_node_to_edge_connectivity(mesh());
87
88 const idx_t nnodes = nodes_.size();
89
90 auto edge_flags = array::make_view<int, 1>(edges_.flags());
91 using Topology = mesh::Nodes::Topology;
92 auto is_pole_edge = [&](size_t e) { return Topology::check(edge_flags(e), Topology::POLE); };
93
94 // Compute sign
95 {
96 const mesh::Connectivity& node_edge_connectivity = nodes_.edge_connectivity();
97 const mesh::MultiBlockConnectivity& edge_node_connectivity = edges_.node_connectivity();
98 if (!nodes_.has_field("node2edge_sign")) {
99 nodes_.add(Field("node2edge_sign", array::make_datatype<double>(),
100 array::make_shape(nnodes, node_edge_connectivity.maxcols())));
101 }
102 array::ArrayView<double, 2> node2edge_sign = array::make_view<double, 2>(nodes_.field("node2edge_sign"));
103
104 atlas_omp_parallel_for(idx_t jnode = 0; jnode < nnodes; ++jnode) {
105 for (idx_t jedge = 0; jedge < node_edge_connectivity.cols(jnode); ++jedge) {
106 idx_t iedge = node_edge_connectivity(jnode, jedge);
107 idx_t ip1 = edge_node_connectivity(iedge, 0);
108 if (jnode == ip1) {
109 node2edge_sign(jnode, jedge) = 1.;
110 }
111 else {
112 node2edge_sign(jnode, jedge) = -1.;
113 if (is_pole_edge(iedge)) {
114 node2edge_sign(jnode, jedge) = 1.;
115 }
116 }
117 }
118 }
119 }
120 }
121 }
122
123 // ------------------------------------------------------------------------------------------
124 extern "C" {
125 Method* atlas__numerics__fvm__Method__new(Mesh::Implementation* mesh, const eckit::Configuration* config) {
126 ATLAS_ASSERT(mesh != nullptr, "Cannot access uninitialised atlas_Mesh");
127 ATLAS_ASSERT(config != nullptr, "Cannot access uninitialised atlas_Config");
128 Mesh m(mesh);
129 return new Method(m, *config);
130 }
131
132 const functionspace::detail::NodeColumns* atlas__numerics__fvm__Method__functionspace_nodes(Method* This) {
133 ATLAS_ASSERT(This != nullptr, "Cannot access uninitialisd atlas_Method");
134 return dynamic_cast<const functionspace::detail::NodeColumns*>(This->node_columns().get());
135 }
136
137 const functionspace::detail::EdgeColumns* atlas__numerics__fvm__Method__functionspace_edges(Method* This) {
138 ATLAS_ASSERT(This != nullptr, "Cannot access uninitialisd atlas_Method");
139 return dynamic_cast<const functionspace::detail::EdgeColumns*>(This->edge_columns().get());
140 }
141 }
142 // ------------------------------------------------------------------------------------------
143
144 } // namespace fvm
145 } // namespace numerics
146 } // namespace atlas