| 1 |
|
| 2 |
/* |
| 3 |
* (C) Copyright 2013 ECMWF. |
| 4 |
* |
| 5 |
* This software is licensed under the terms of the Apache Licence Version 2.0 |
| 6 |
* which can be obtained at http://www.apache.org/licenses/LICENSE-2.0. |
| 7 |
* In applying this licence, ECMWF does not waive the privileges and immunities |
| 8 |
* granted to it by virtue of its status as an intergovernmental organisation |
| 9 |
* nor does it submit to any jurisdiction. |
| 10 |
*/ |
| 11 |
|
| 12 |
#include "eckit/config/Resource.h" |
| 13 |
|
| 14 |
#include "atlas/array/ArrayView.h" |
| 15 |
#include "atlas/array/MakeView.h" |
| 16 |
#include "atlas/interpolation/method/PointIndex3.h" |
| 17 |
#include "atlas/mesh/HybridElements.h" |
| 18 |
#include "atlas/runtime/Trace.h" |
| 19 |
#include "atlas/util/Topology.h" |
| 20 |
|
| 21 |
namespace atlas { |
| 22 |
namespace interpolation { |
| 23 |
namespace method { |
| 24 |
|
| 25 |
ElemIndex3* create_element_kdtree(const Mesh& mesh, const Field& field_centres) { |
| 26 |
ATLAS_TRACE(); |
| 27 |
const array::ArrayView<const double, 2> centres = array::make_view<double, 2>(field_centres); |
| 28 |
const array::ArrayView<const int, 1> flags = array::make_view<int, 1>(mesh.cells().flags()); |
| 29 |
auto include_element = [&](unsigned int e) { |
| 30 |
using util::Topology; |
| 31 |
return not Topology::view(flags(e)).check(Topology::INVALID); |
| 32 |
}; |
| 33 |
|
| 34 |
static bool fastBuildKDTrees = eckit::Resource<bool>("$ATLAS_FAST_BUILD_KDTREES", true); |
| 35 |
|
| 36 |
ElemIndex3* tree = new ElemIndex3(); |
| 37 |
const size_t nb_cells = centres.shape(0); |
| 38 |
|
| 39 |
if (fastBuildKDTrees) { |
| 40 |
std::vector<ElemIndex3::Value> p; |
| 41 |
p.reserve(nb_cells); |
| 42 |
|
| 43 |
for (unsigned int j = 0; j < nb_cells; ++j) { |
| 44 |
if (include_element(j)) { |
| 45 |
p.emplace_back(ElemIndex3::Point(centres(j, XX), centres(j, YY), centres(j, ZZ)), |
| 46 |
ElemIndex3::Payload(j)); |
| 47 |
} |
| 48 |
} |
| 49 |
|
| 50 |
tree->build(p.begin(), p.end()); |
| 51 |
} |
| 52 |
else { |
| 53 |
for (unsigned int j = 0; j < nb_cells; ++j) { |
| 54 |
if (include_element(j)) { |
| 55 |
tree->insert(ElemIndex3::Value(ElemIndex3::Point(centres(j, XX), centres(j, YY), centres(j, ZZ)), |
| 56 |
ElemIndex3::Payload(j))); |
| 57 |
} |
| 58 |
} |
| 59 |
} |
| 60 |
return tree; |
| 61 |
} |
| 62 |
|
| 63 |
ElemIndex3* create_element_centre_index(const Mesh& mesh) { |
| 64 |
return create_element_kdtree(mesh, mesh.cells().field("centre")); |
| 65 |
} |
| 66 |
|
| 67 |
} // namespace method |
| 68 |
} // namespace interpolation |
| 69 |
} // namespace atlas |