| 1 |
/* |
| 2 |
* (C) Copyright 2021- 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 <fstream> |
| 12 |
#include <iomanip> |
| 13 |
#include <unordered_set> |
| 14 |
#include <vector> |
| 15 |
|
| 16 |
#include "ConservativeSphericalPolygonInterpolation.h" |
| 17 |
|
| 18 |
#include "atlas/grid.h" |
| 19 |
#include "atlas/interpolation/Interpolation.h" |
| 20 |
#include "atlas/interpolation/method/MethodFactory.h" |
| 21 |
#include "atlas/library/FloatingPointExceptions.h" |
| 22 |
#include "atlas/mesh/actions/BuildHalo.h" |
| 23 |
#include "atlas/mesh/actions/BuildNode2CellConnectivity.h" |
| 24 |
#include "atlas/meshgenerator.h" |
| 25 |
#include "atlas/parallel/mpi/mpi.h" |
| 26 |
#include "atlas/runtime/Exception.h" |
| 27 |
#include "atlas/runtime/Log.h" |
| 28 |
#include "atlas/runtime/Trace.h" |
| 29 |
#include "atlas/util/ConvexSphericalPolygon.h" |
| 30 |
#include "atlas/util/KDTree.h" |
| 31 |
#include "atlas/util/Topology.h" |
| 32 |
#include "atlas/util/detail/filesystem.h" |
| 33 |
|
| 34 |
#include "eckit/log/Bytes.h" |
| 35 |
#include "eckit/log/ProgressTimer.h" |
| 36 |
|
| 37 |
#define PRINT_BAD_POLYGONS 0 |
| 38 |
|
| 39 |
namespace atlas { |
| 40 |
namespace interpolation { |
| 41 |
namespace method { |
| 42 |
|
| 43 |
MethodBuilder<ConservativeSphericalPolygonInterpolation> __builder("conservative-spherical-polygon"); |
| 44 |
|
| 45 |
using runtime::trace::StopWatch; |
| 46 |
using Polygon = util::ConvexSphericalPolygon; |
| 47 |
using PolygonArray = std::vector<util::ConvexSphericalPolygon>; |
| 48 |
|
| 49 |
namespace { |
| 50 |
|
| 51 |
template <typename It> |
| 52 |
std::string to_json(const It& begin, const It& end, int precision = 0) { |
| 53 |
std::stringstream ss; |
| 54 |
ss << "[\n"; |
| 55 |
size_t size = std::distance(begin,end); |
| 56 |
size_t c=0; |
| 57 |
for( auto it = begin; it != end; ++it, ++c ) { |
| 58 |
ss << " " << it->json(precision); |
| 59 |
if( c < size-1 ) { |
| 60 |
ss << ",\n"; |
| 61 |
} |
| 62 |
} |
| 63 |
ss << "\n]"; |
| 64 |
return ss.str(); |
| 65 |
} |
| 66 |
|
| 67 |
|
| 68 |
template<typename PolygonContainer> |
| 69 |
std::string to_json(const PolygonContainer& polygons, int precision = 0) { |
| 70 |
return to_json(polygons.begin(),polygons.end(),precision); |
| 71 |
} |
| 72 |
|
| 73 |
|
| 74 |
template <typename It> |
| 75 |
std::string polygons_to_json(const It& begin, const It& end, int precision = 0) { |
| 76 |
std::stringstream ss; |
| 77 |
ss << "[\n"; |
| 78 |
size_t size = std::distance(begin,end); |
| 79 |
size_t c=0; |
| 80 |
for( auto it = begin; it != end; ++it, ++c ) { |
| 81 |
ss << " " << it->json(precision); |
| 82 |
if( c < size-1 ) { |
| 83 |
ss << ",\n"; |
| 84 |
} |
| 85 |
} |
| 86 |
ss << "\n]"; |
| 87 |
return ss.str(); |
| 88 |
} |
| 89 |
|
| 90 |
|
| 91 |
template<typename Polygons> |
| 92 |
std::string polygons_to_json(const Polygons& polygons, int precision = 0) { |
| 93 |
return polygons_to_json(polygons.begin(),polygons.end(),precision); |
| 94 |
} |
| 95 |
|
| 96 |
|
| 97 |
template<typename Polygons> |
| 98 |
void dump_polygons_to_json( const Polygon& t_csp, |
| 99 |
double pointsSameEPS, |
| 100 |
const Polygons& source_polygons, |
| 101 |
const std::vector<idx_t>& source_polygons_considered_indices, |
| 102 |
const std::string folder, |
| 103 |
const std::string name) { |
| 104 |
PolygonArray csp_arr{ t_csp }; |
| 105 |
PolygonArray csp_arr_intersecting {t_csp}; |
| 106 |
PolygonArray intersections; |
| 107 |
int count = 1; |
| 108 |
for( auto& s_idx : source_polygons_considered_indices ) { |
| 109 |
auto s_csp = source_polygons[s_idx]; |
| 110 |
csp_arr.emplace_back( s_csp ); |
| 111 |
std::fstream file_plg_debug(folder + name + "_" + std::to_string(count++) + ".debug", std::ios::out); |
| 112 |
Polygon iplg = t_csp.intersect(s_csp, &file_plg_debug, pointsSameEPS); |
| 113 |
file_plg_debug.close(); |
| 114 |
if (iplg) { |
| 115 |
if( iplg.area() > 0. ) { |
| 116 |
csp_arr_intersecting.emplace_back( s_csp ); |
| 117 |
intersections.emplace_back( iplg ); |
| 118 |
} |
| 119 |
} |
| 120 |
} |
| 121 |
double tot = 0.; |
| 122 |
Log::info().indent(); |
| 123 |
std::fstream file_info(folder + name + ".info", std::ios::out); |
| 124 |
file_info << "List of intersection weights: " << std::endl; |
| 125 |
count = 1; |
| 126 |
for( auto& iplg : intersections ) { |
| 127 |
csp_arr.emplace_back( iplg ); |
| 128 |
csp_arr_intersecting.emplace_back( iplg ); |
| 129 |
tot += iplg.area() / t_csp.area(); |
| 130 |
file_info << "\t" << count++ << ": " << iplg.area() / t_csp.area() << std::endl; |
| 131 |
} |
| 132 |
file_info << std::endl << name + ": " << 100. * tot << " % covered."<< std::endl << std::endl; |
| 133 |
file_info << "Target polygon + candidate source polygons + " << intersections.size() << " intersections in file:" << std::endl; |
| 134 |
file_info << "\t" << folder + name + ".candidates\n" << std::endl; |
| 135 |
std::fstream file_plg(folder + name + ".candidates", std::ios::out); |
| 136 |
file_plg << polygons_to_json(csp_arr, 16); |
| 137 |
file_plg.close(); |
| 138 |
file_info << "Target polygon + " << csp_arr_intersecting.size() << " intersecting source polygon + " << intersections.size() << " intersections in file:" << std::endl; |
| 139 |
file_info << "\t" << folder + name + ".intersections\n" << std::endl; |
| 140 |
file_plg.open(folder + name + ".intersections", std::ios::out); |
| 141 |
file_plg << polygons_to_json(csp_arr_intersecting, 16); |
| 142 |
file_plg.close(); |
| 143 |
Log::info().unindent(); |
| 144 |
} |
| 145 |
|
| 146 |
|
| 147 |
constexpr double unit_sphere_area() { |
| 148 |
return 4. * M_PI; |
| 149 |
} |
| 150 |
|
| 151 |
|
| 152 |
template <typename T> |
| 153 |
size_t memory_of(const std::vector<T>& vector) { |
| 154 |
return sizeof(T) * vector.capacity(); |
| 155 |
} |
| 156 |
|
| 157 |
|
| 158 |
template <typename T> |
| 159 |
size_t memory_of(const std::vector<std::vector<T>>& vector_of_vector) { |
| 160 |
size_t mem = 0; |
| 161 |
for (const auto& vector : vector_of_vector) { |
| 162 |
mem += memory_of(vector); |
| 163 |
} |
| 164 |
return mem; |
| 165 |
} |
| 166 |
|
| 167 |
|
| 168 |
size_t memory_of( |
| 169 |
const std::vector<ConservativeSphericalPolygonInterpolation::InterpolationParameters>& vector_of_params) { |
| 170 |
size_t mem = 0; |
| 171 |
for (const auto& params : vector_of_params) { |
| 172 |
mem += memory_of(params.csp_ids); |
| 173 |
mem += memory_of(params.centroids); |
| 174 |
mem += memory_of(params.weights); |
| 175 |
} |
| 176 |
return mem; |
| 177 |
} |
| 178 |
|
| 179 |
|
| 180 |
Mesh extract_mesh(FunctionSpace fs) { |
| 181 |
if (functionspace::CellColumns(fs)) { |
| 182 |
return functionspace::CellColumns(fs).mesh(); |
| 183 |
} |
| 184 |
else if (functionspace::NodeColumns(fs)) { |
| 185 |
return functionspace::NodeColumns(fs).mesh(); |
| 186 |
} |
| 187 |
else { |
| 188 |
ATLAS_THROW_EXCEPTION("Cannot extract mesh from FunctionSpace" << fs.type()); |
| 189 |
} |
| 190 |
} |
| 191 |
|
| 192 |
|
| 193 |
#if ATLAS_BUILD_TYPE_DEBUG |
| 194 |
int inside_vertices(const Polygon& plg1, const Polygon& plg2, int& pout) { |
| 195 |
int points_in = 0; |
| 196 |
pout = 0; |
| 197 |
for (int j = 0; j < plg2.size(); j++) { |
| 198 |
int i = 0; |
| 199 |
for (; i < plg1.size(); i++) { |
| 200 |
int in = (i != plg1.size() - 1) ? i + 1 : 0; |
| 201 |
auto gss = Polygon::GreatCircleSegment(plg1[i], plg1[in]); |
| 202 |
if (not gss.inLeftHemisphere(plg2[j], -std::numeric_limits<double>::epsilon())) { |
| 203 |
pout++; |
| 204 |
break; |
| 205 |
}; |
| 206 |
} |
| 207 |
if (i == plg1.size()) { |
| 208 |
points_in++; |
| 209 |
} |
| 210 |
} |
| 211 |
ATLAS_ASSERT(points_in + pout == plg2.size()); |
| 212 |
return points_in; |
| 213 |
} |
| 214 |
#endif |
| 215 |
|
| 216 |
|
| 217 |
inline bool valid_point(idx_t node_idx, const array::ArrayView<int, 1>& node_flags) { |
| 218 |
return not util::Bitflags::view(node_flags(node_idx)).check(util::Topology::INVALID); |
| 219 |
} |
| 220 |
|
| 221 |
} // namespace |
| 222 |
|
| 223 |
|
| 224 |
ConservativeSphericalPolygonInterpolation::ConservativeSphericalPolygonInterpolation(const Config& config): |
| 225 |
Method(config), validate_(false), src_cell_data_(true), tgt_cell_data_(true), normalise_(false), |
| 226 |
order_(1), matrix_free_(false), n_spoints_(0), n_tpoints_(0) { |
| 227 |
config.get("validate", validate_ = false); |
| 228 |
config.get("order", order_ = 1); |
| 229 |
config.get("normalise", normalise_ = false); |
| 230 |
config.get("matrix_free", matrix_free_ = false); |
| 231 |
config.get("src_cell_data", src_cell_data_ = true); |
| 232 |
config.get("tgt_cell_data", tgt_cell_data_ = true); |
| 233 |
config.get("statistics.all", remap_stat_.all = false); |
| 234 |
config.get("statistics.accuracy", remap_stat_.accuracy = false); |
| 235 |
config.get("statistics.conservation", remap_stat_.conservation = false); |
| 236 |
config.get("statistics.intersection", remap_stat_.intersection = false); |
| 237 |
config.get("statistics.timings", remap_stat_.timings = false); |
| 238 |
if (remap_stat_.all) { |
| 239 |
Log::warning() << "statistics.all required. Enabling validate, statistics.timings, statistics.intersection, statistics.conservation, and statistics.accuracy." << std::endl; |
| 240 |
validate_ = true; |
| 241 |
remap_stat_.accuracy = true; |
| 242 |
remap_stat_.conservation = true; |
| 243 |
remap_stat_.intersection = true; |
| 244 |
remap_stat_.timings = true; |
| 245 |
} |
| 246 |
if (remap_stat_.intersection) { |
| 247 |
Log::warning() << "statistics.intersection required. Enabling validate." << std::endl; |
| 248 |
validate_ = true; |
| 249 |
} |
| 250 |
|
| 251 |
sharable_data_ = std::make_shared<Data>(); |
| 252 |
cache_ = Cache(sharable_data_); |
| 253 |
data_ = sharable_data_.get(); |
| 254 |
|
| 255 |
ATLAS_ASSERT(sharable_data_.use_count() == 2); |
| 256 |
} |
| 257 |
|
| 258 |
|
| 259 |
inline int ConservativeSphericalPolygonInterpolation::next_index(int current_index, int size) const { |
| 260 |
#if ATLAS_BUILD_TYPE_DEBUG |
| 261 |
ATLAS_ASSERT(current_index >= 0 && current_index < size); |
| 262 |
#endif |
| 263 |
return (current_index < size - 1) ? current_index + 1 : current_index + 1 - size; |
| 264 |
} |
| 265 |
|
| 266 |
|
| 267 |
inline int ConservativeSphericalPolygonInterpolation::prev_index(int current_index, int size) const { |
| 268 |
#if ATLAS_BUILD_TYPE_DEBUG |
| 269 |
ATLAS_ASSERT(current_index >= 0 && current_index < size); |
| 270 |
#endif |
| 271 |
return (current_index >= 1) ? current_index - 1 : current_index - 1 + size; |
| 272 |
} |
| 273 |
|
| 274 |
|
| 275 |
struct ConservativeSphericalPolygonInterpolation::Workspace_get_cell_neighbours { |
| 276 |
PointXYZ p0; |
| 277 |
PointLonLat p0_ll; |
| 278 |
PointXYZ p1; |
| 279 |
PointLonLat p1_ll; |
| 280 |
}; |
| 281 |
|
| 282 |
|
| 283 |
// get counter-clockwise sorted neighbours of a cell |
| 284 |
std::vector<idx_t> ConservativeSphericalPolygonInterpolation::get_cell_neighbours(Mesh& mesh, idx_t cell, Workspace_get_cell_neighbours& w) const { |
| 285 |
const auto& cell2node = mesh.cells().node_connectivity(); |
| 286 |
const auto& nodes_ll = array::make_view<double, 2>(mesh.nodes().lonlat()); |
| 287 |
const idx_t n_nodes = cell2node.cols(cell); |
| 288 |
std::vector<idx_t> nbr_cells; |
| 289 |
nbr_cells.reserve(n_nodes); |
| 290 |
if (mesh.nodes().cell_connectivity().rows() == 0) { |
| 291 |
mesh::actions::build_node_to_cell_connectivity(mesh); |
| 292 |
} |
| 293 |
const auto& node2cell = mesh.nodes().cell_connectivity(); |
| 294 |
const auto n2c_missval = node2cell.missing_value(); |
| 295 |
|
| 296 |
auto& p0 = w.p0; |
| 297 |
auto& p1 = w.p1; |
| 298 |
auto& p0_ll = w.p0_ll; |
| 299 |
auto& p1_ll = w.p1_ll; |
| 300 |
|
| 301 |
for (idx_t inode = 0; inode < n_nodes; ++inode) { |
| 302 |
idx_t node0 = cell2node(cell, inode); |
| 303 |
idx_t node1 = cell2node(cell, next_index(inode, n_nodes)); |
| 304 |
p0_ll[0] = nodes_ll(node0, 0); |
| 305 |
p0_ll[1] = nodes_ll(node0, 1); |
| 306 |
p1_ll[0] = nodes_ll(node1, 0); |
| 307 |
p1_ll[1] = nodes_ll(node1, 1); |
| 308 |
eckit::geometry::Sphere::convertSphericalToCartesian(1., p0_ll, p0); |
| 309 |
eckit::geometry::Sphere::convertSphericalToCartesian(1., p1_ll, p1); |
| 310 |
if (PointXYZ::norm(p0 - p1) < 1e-14) { |
| 311 |
continue; // edge = point |
| 312 |
} |
| 313 |
bool still_search = true; // still search the cell having vertices node0 & node1, not having index "cell" |
| 314 |
int n_cells0 = node2cell.cols(node0); |
| 315 |
int n_cells1 = node2cell.cols(node1); |
| 316 |
for (int icell0 = 0; still_search && icell0 < n_cells0; icell0++) { |
| 317 |
int cell0 = node2cell(node0, icell0); |
| 318 |
if (cell0 == cell) { |
| 319 |
continue; |
| 320 |
} |
| 321 |
for (int icell1 = 0; still_search && icell1 < n_cells1; icell1++) { |
| 322 |
int cell1 = node2cell(node1, icell1); |
| 323 |
if (cell0 == cell1 && cell0 != n2c_missval) { |
| 324 |
nbr_cells.emplace_back(cell0); |
| 325 |
still_search = false; |
| 326 |
} |
| 327 |
} |
| 328 |
} |
| 329 |
} |
| 330 |
return nbr_cells; |
| 331 |
} |
| 332 |
|
| 333 |
|
| 334 |
struct ConservativeSphericalPolygonInterpolation::Workspace_get_node_neighbours { |
| 335 |
std::vector<idx_t> nbr_nodes_od; |
| 336 |
std::vector< std::array<idx_t,2> > cnodes; |
| 337 |
}; |
| 338 |
|
| 339 |
|
| 340 |
// get cyclically sorted node neighbours without using edge connectivity |
| 341 |
std::vector<idx_t> ConservativeSphericalPolygonInterpolation::get_node_neighbours(Mesh& mesh, idx_t node_id, Workspace_get_node_neighbours& w) const { |
| 342 |
const auto& cell2node = mesh.cells().node_connectivity(); |
| 343 |
const auto node_flags = array::make_view<int, 1>(mesh.nodes().flags()); |
| 344 |
if (mesh.nodes().cell_connectivity().rows() == 0) { |
| 345 |
mesh::actions::build_node_to_cell_connectivity(mesh); |
| 346 |
} |
| 347 |
const auto& node2cell = mesh.nodes().cell_connectivity(); |
| 348 |
std::vector<idx_t> nbr_nodes; |
| 349 |
const int ncells = node2cell.cols(node_id); |
| 350 |
ATLAS_ASSERT(ncells > 0, "There is a node which does not connect to any cell"); |
| 351 |
nbr_nodes.reserve(ncells + 1); |
| 352 |
w.cnodes.resize(ncells); |
| 353 |
w.nbr_nodes_od.clear(); |
| 354 |
w.nbr_nodes_od.reserve(ncells + 1); |
| 355 |
for (idx_t icell = 0; icell < ncells; ++icell) { |
| 356 |
const idx_t cell = node2cell(node_id, icell); |
| 357 |
const int nnodes = cell2node.cols(cell); |
| 358 |
idx_t cnode = 0; |
| 359 |
for (; cnode < nnodes; ++cnode) { |
| 360 |
if (node_id == cell2node(cell, cnode)) { |
| 361 |
break; |
| 362 |
} |
| 363 |
} |
| 364 |
w.cnodes[icell][0] = cell2node(cell, prev_index(cnode, nnodes)); |
| 365 |
w.cnodes[icell][1] = cell2node(cell, next_index(cnode, nnodes)); |
| 366 |
} |
| 367 |
if (ncells == 1) { |
| 368 |
nbr_nodes.emplace_back(w.cnodes[0][0]); |
| 369 |
nbr_nodes.emplace_back(w.cnodes[0][1]); |
| 370 |
return nbr_nodes; |
| 371 |
} |
| 372 |
// cycle one direction |
| 373 |
idx_t find = w.cnodes[0][1]; |
| 374 |
idx_t prev = w.cnodes[0][0]; |
| 375 |
nbr_nodes.emplace_back(prev); |
| 376 |
nbr_nodes.emplace_back(find); |
| 377 |
for (idx_t icycle = 0; nbr_nodes[0] != find;) { |
| 378 |
idx_t jcell = 0; |
| 379 |
for (; jcell < ncells; ++jcell) { |
| 380 |
idx_t ocell = (icycle + jcell + 1) % ncells; |
| 381 |
idx_t cand0 = w.cnodes[ocell][0]; |
| 382 |
idx_t cand1 = w.cnodes[ocell][1]; |
| 383 |
if (find == cand0 && prev != cand1) { |
| 384 |
if (cand1 == nbr_nodes[0]) { |
| 385 |
return nbr_nodes; |
| 386 |
} |
| 387 |
nbr_nodes.emplace_back(cand1); |
| 388 |
prev = find; |
| 389 |
find = cand1; |
| 390 |
break; |
| 391 |
} |
| 392 |
if (find == cand1 && prev != cand0) { |
| 393 |
if (cand0 == nbr_nodes[0]) { |
| 394 |
return nbr_nodes; |
| 395 |
} |
| 396 |
nbr_nodes.emplace_back(cand0); |
| 397 |
prev = find; |
| 398 |
find = cand0; |
| 399 |
break; |
| 400 |
} |
| 401 |
} |
| 402 |
if (jcell == ncells) { // not found |
| 403 |
if (nbr_nodes[0] != find && find != nbr_nodes[nbr_nodes.size() - 1]) { |
| 404 |
nbr_nodes.emplace_back(find); |
| 405 |
} |
| 406 |
break; |
| 407 |
} |
| 408 |
else { |
| 409 |
icycle++; |
| 410 |
} |
| 411 |
} |
| 412 |
if (nbr_nodes[0] == find) { |
| 413 |
return nbr_nodes; |
| 414 |
} |
| 415 |
// cycle the oposite direction |
| 416 |
find = w.cnodes[0][0]; |
| 417 |
prev = w.cnodes[0][1]; |
| 418 |
w.nbr_nodes_od.emplace_back(prev); |
| 419 |
w.nbr_nodes_od.emplace_back(find); |
| 420 |
for (idx_t icycle = 0; w.nbr_nodes_od[0] != find;) { |
| 421 |
idx_t jcell = 0; |
| 422 |
for (; jcell < ncells; ++jcell) { |
| 423 |
idx_t ocell = (icycle + jcell + 1) % ncells; |
| 424 |
if (find == w.cnodes[ocell][0] && prev != w.cnodes[ocell][1]) { |
| 425 |
w.nbr_nodes_od.emplace_back(w.cnodes[ocell][1]); |
| 426 |
prev = find; |
| 427 |
find = w.cnodes[ocell][1]; |
| 428 |
break; |
| 429 |
} |
| 430 |
if (find == w.cnodes[ocell][1] && prev != w.cnodes[ocell][0]) { |
| 431 |
w.nbr_nodes_od.emplace_back(w.cnodes[ocell][0]); |
| 432 |
prev = find; |
| 433 |
find = w.cnodes[ocell][0]; |
| 434 |
break; |
| 435 |
} |
| 436 |
} |
| 437 |
if (jcell == ncells) { |
| 438 |
if (find != w.nbr_nodes_od[w.nbr_nodes_od.size() - 1]) { |
| 439 |
w.nbr_nodes_od.emplace_back(find); |
| 440 |
} |
| 441 |
break; |
| 442 |
} |
| 443 |
icycle++; |
| 444 |
} |
| 445 |
// put together |
| 446 |
int ow_size = w.nbr_nodes_od.size(); |
| 447 |
for (int i = 0; i < ow_size - 2; i++) { |
| 448 |
if (valid_point(w.nbr_nodes_od[ow_size - 1 - i], node_flags)) { |
| 449 |
nbr_nodes.emplace_back(w.nbr_nodes_od[ow_size - 1 - i]); |
| 450 |
} |
| 451 |
} |
| 452 |
return nbr_nodes; |
| 453 |
} |
| 454 |
|
| 455 |
|
| 456 |
void ConservativeSphericalPolygonInterpolation:: |
| 457 |
init_polygons_data( |
| 458 |
// input |
| 459 |
FunctionSpace fs, |
| 460 |
// output |
| 461 |
Data::PolygonsData& md) { |
| 462 |
using Context = Data::PolygonsData::Context; |
| 463 |
|
| 464 |
md.cell_data = functionspace::CellColumns(fs); |
| 465 |
std::vector<idx_t>& csp2node = md.csp2node; |
| 466 |
std::vector<std::vector<idx_t>>& node2csp = md.node2csp; |
| 467 |
gidx_t& csp_size = md.csp_size; |
| 468 |
std::vector<idx_t>& csp_cell_index = md.csp_cell_index; |
| 469 |
std::vector<idx_t>& csp_index = md.csp_index; |
| 470 |
|
| 471 |
auto mesh = extract_mesh(fs); |
| 472 |
const auto& cell2node = mesh.cells().node_connectivity(); |
| 473 |
const auto cell_halo = array::make_view<int, 1>(mesh.cells().halo()); |
| 474 |
const auto cell_flags = array::make_view<int, 1>(mesh.cells().flags()); |
| 475 |
const auto cell_invalid = [&cell_flags](idx_t cell) { |
| 476 |
return util::Bitflags::view(cell_flags(cell)).check(util::Topology::INVALID); |
| 477 |
}; |
| 478 |
auto fs_halo = md.cell_data ? functionspace::CellColumns(fs).halo().size() : functionspace::NodeColumns(fs).halo().size(); |
| 479 |
csp_size = 0; |
| 480 |
csp_cell_index.reserve(mesh.cells().size()); |
| 481 |
csp_index.reserve(mesh.cells().size()); |
| 482 |
if (md.cell_data) { |
| 483 |
auto skip_cell = [&](idx_t cell) { |
| 484 |
if (md.context == Context::SOURCE) { |
| 485 |
return cell_halo(cell) > fs_halo || cell_invalid(cell); |
| 486 |
} |
| 487 |
else { |
| 488 |
return cell_halo(cell) > 0; |
| 489 |
} |
| 490 |
}; |
| 491 |
for (idx_t cell = 0; cell < mesh.cells().size(); ++cell) { |
| 492 |
if (skip_cell(cell)) { |
| 493 |
continue; |
| 494 |
} |
| 495 |
csp_cell_index.emplace_back(cell); |
| 496 |
csp_index.emplace_back(cell); |
| 497 |
csp_size++; |
| 498 |
} |
| 499 |
} |
| 500 |
else { |
| 501 |
auto skip_cell = [&](idx_t cell) { |
| 502 |
if (md.context == Context::SOURCE) { |
| 503 |
return cell_halo(cell) > fs_halo || cell_invalid(cell); |
| 504 |
} |
| 505 |
else { |
| 506 |
return cell_halo(cell) > 1 || cell_invalid(cell); |
| 507 |
} |
| 508 |
}; |
| 509 |
|
| 510 |
const auto nodes_ll = array::make_view<double, 2>(mesh.nodes().lonlat()); |
| 511 |
const auto node_flags = array::make_view<int, 1>(mesh.nodes().flags()); |
| 512 |
auto ll2xyz = [](const atlas::PointLonLat& p_ll) { |
| 513 |
PointXYZ p_xyz; |
| 514 |
eckit::geometry::Sphere::convertSphericalToCartesian(1., p_ll, p_xyz); |
| 515 |
return p_xyz; |
| 516 |
}; |
| 517 |
csp_index.emplace_back(0); |
| 518 |
for (idx_t cell = 0; cell < mesh.cells().size(); ++cell) { |
| 519 |
if (skip_cell(cell)) { |
| 520 |
continue; |
| 521 |
} |
| 522 |
const idx_t n_nodes = cell2node.cols(cell); |
| 523 |
for (idx_t inode = 0; inode < n_nodes; ++inode) { |
| 524 |
idx_t node0 = cell2node(cell, inode); |
| 525 |
if (not valid_point(node0, node_flags)) { |
| 526 |
continue; |
| 527 |
} |
| 528 |
idx_t node1 = cell2node(cell, next_index(inode, n_nodes)); |
| 529 |
const PointLonLat p0_ll = PointLonLat{nodes_ll(node0, 0), nodes_ll(node0, 1)}; |
| 530 |
const PointLonLat p1_ll = PointLonLat{nodes_ll(node1, 0), nodes_ll(node1, 1)}; |
| 531 |
PointXYZ p0 = ll2xyz(p0_ll); |
| 532 |
PointXYZ p1 = ll2xyz(p1_ll); |
| 533 |
if (PointXYZ::norm(p0 - p1) < 1e-14) { |
| 534 |
continue; // skip this edge = a pole point |
| 535 |
} |
| 536 |
csp_size++; |
| 537 |
} |
| 538 |
csp_index.emplace_back(csp_size); |
| 539 |
csp_cell_index.emplace_back(cell); |
| 540 |
} |
| 541 |
csp2node.resize(csp_size); |
| 542 |
std::fill(csp2node.begin(), csp2node.end(), -1); |
| 543 |
node2csp.resize(mesh.nodes().size()); |
| 544 |
} |
| 545 |
} |
| 546 |
|
| 547 |
|
| 548 |
ConservativeSphericalPolygonInterpolation::Polygon ConservativeSphericalPolygonInterpolation:: |
| 549 |
get_csp_celldata(idx_t csp_id, const Mesh& mesh, const Data::PolygonsData& md) { |
| 550 |
|
| 551 |
// TODO: Performance optimisations: |
| 552 |
// - Do not extract array views, mesh connectivity for each csp_id |
| 553 |
// - pts_ll should also not be created/destroyed for each csp_id |
| 554 |
|
| 555 |
const auto& cell2node = mesh.cells().node_connectivity(); |
| 556 |
std::vector<PointLonLat> pts_ll; |
| 557 |
|
| 558 |
idx_t cell = csp_to_cell(csp_id, md); |
| 559 |
|
| 560 |
const idx_t n_nodes = cell2node.cols(cell); |
| 561 |
const auto nodes_ll = array::make_view<const double, 2>(mesh.nodes().lonlat()); |
| 562 |
pts_ll.clear(); |
| 563 |
pts_ll.resize(n_nodes); |
| 564 |
|
| 565 |
for (idx_t jnode = 0; jnode < n_nodes; ++jnode) { |
| 566 |
idx_t inode = cell2node(cell, jnode); |
| 567 |
pts_ll[jnode] = PointLonLat{nodes_ll(inode, 0), nodes_ll(inode, 1)}; |
| 568 |
} |
| 569 |
return Polygon(pts_ll); |
| 570 |
} |
| 571 |
|
| 572 |
|
| 573 |
ConservativeSphericalPolygonInterpolation::Polygon ConservativeSphericalPolygonInterpolation:: |
| 574 |
get_csp_nodedata(idx_t csp_id, const Mesh& mesh, Data::PolygonsData& md ) { |
| 575 |
std::vector<idx_t>& csp2node = md.csp2node; |
| 576 |
std::vector<std::vector<idx_t>>& node2csp = md.node2csp; |
| 577 |
|
| 578 |
// TODO: Performance optimisations: |
| 579 |
// - Do not extract array views, mesh connectivity for each csp_id |
| 580 |
// - pts_ll, pts_xyz, pts_idx, subpol_pts_ll should also not be created/destroyed for each csp_id |
| 581 |
|
| 582 |
const auto& cell2node = mesh.cells().node_connectivity(); |
| 583 |
const auto node_flags = array::make_view<int, 1>(mesh.nodes().flags()); |
| 584 |
std::vector<PointLonLat> pts_ll; |
| 585 |
|
| 586 |
idx_t cell, isubcell; |
| 587 |
std::tie(cell,isubcell) = csp_to_cell_and_subcell(csp_id, md); |
| 588 |
|
| 589 |
// idx_t cell; |
| 590 |
// auto isubcell = get_owner_cell_nodedata(csp_id, csp_cell_index, csp_index, cell); |
| 591 |
|
| 592 |
const idx_t n_nodes = cell2node.cols(cell); |
| 593 |
const auto nodes_ll = array::make_view<double, 2>(mesh.nodes().lonlat()); |
| 594 |
pts_ll.clear(); |
| 595 |
pts_ll.resize(n_nodes); |
| 596 |
|
| 597 |
auto xyz2ll = [](const atlas::PointXYZ& p_xyz) { |
| 598 |
PointLonLat p_ll; |
| 599 |
eckit::geometry::Sphere::convertCartesianToSpherical(1., p_xyz, p_ll); |
| 600 |
return p_ll; |
| 601 |
}; |
| 602 |
auto ll2xyz = [](const atlas::PointLonLat& p_ll) { |
| 603 |
PointXYZ p_xyz; |
| 604 |
eckit::geometry::Sphere::convertSphericalToCartesian(1., p_ll, p_xyz); |
| 605 |
return p_xyz; |
| 606 |
}; |
| 607 |
|
| 608 |
PointXYZ cell_mid(0., 0., 0.); // cell centre |
| 609 |
std::vector<PointXYZ> pts_xyz; |
| 610 |
std::vector<int> pts_idx; |
| 611 |
#if ATLAS_BUILD_TYPE_DEBUG |
| 612 |
ATLAS_ASSERT(cell < cell2node.rows()); |
| 613 |
ATLAS_ASSERT(n_nodes > 2); |
| 614 |
#endif |
| 615 |
pts_xyz.clear(); |
| 616 |
pts_ll.clear(); |
| 617 |
pts_idx.clear(); |
| 618 |
pts_xyz.reserve(n_nodes); |
| 619 |
pts_ll.reserve(n_nodes); |
| 620 |
pts_idx.reserve(n_nodes); |
| 621 |
for (idx_t inode = 0; inode < n_nodes; ++inode) { |
| 622 |
idx_t node0 = cell2node(cell, inode); |
| 623 |
if (not valid_point(node0, node_flags)) { |
| 624 |
continue; |
| 625 |
} |
| 626 |
idx_t node1 = cell2node(cell, next_index(inode, n_nodes)); |
| 627 |
const PointLonLat p0_ll = PointLonLat{nodes_ll(node0, 0), nodes_ll(node0, 1)}; |
| 628 |
const PointLonLat p1_ll = PointLonLat{nodes_ll(node1, 0), nodes_ll(node1, 1)}; |
| 629 |
PointXYZ p0 = ll2xyz(p0_ll); |
| 630 |
PointXYZ p1 = ll2xyz(p1_ll); |
| 631 |
if (PointXYZ::norm(p0 - p1) < 1e-14) { |
| 632 |
continue; // skip this edge = a pole point |
| 633 |
} |
| 634 |
pts_xyz.emplace_back(p0); |
| 635 |
pts_ll.emplace_back(p0_ll); |
| 636 |
pts_idx.emplace_back(inode); |
| 637 |
cell_mid = cell_mid + p0; |
| 638 |
cell_mid = cell_mid + p1; |
| 639 |
} |
| 640 |
cell_mid = PointXYZ::normalize(cell_mid); |
| 641 |
#if ATLAS_BUILD_TYPE_DEBUG |
| 642 |
ATLAS_ASSERT(pts_xyz.size() > 2); |
| 643 |
ATLAS_ASSERT(isubcell < pts_idx.size()); |
| 644 |
#endif |
| 645 |
PointLonLat cell_ll = xyz2ll(cell_mid); |
| 646 |
idx_t inode = isubcell; |
| 647 |
int inode_n = next_index(inode, pts_idx.size()); |
| 648 |
PointXYZ iedge_mid = PointXYZ::normalize(pts_xyz[inode] + pts_xyz[inode_n]); |
| 649 |
int inode_nn = next_index(inode_n, pts_idx.size()); |
| 650 |
if (PointXYZ::norm(pts_xyz[inode_nn] - pts_xyz[inode_n]) < 1e-14) { |
| 651 |
ATLAS_THROW_EXCEPTION("Three cell vertices on a same great arc!"); |
| 652 |
} |
| 653 |
PointXYZ jedge_mid; |
| 654 |
jedge_mid = pts_xyz[inode_nn] + pts_xyz[inode_n]; |
| 655 |
jedge_mid = PointXYZ::div(jedge_mid, PointXYZ::norm(jedge_mid)); |
| 656 |
std::array<PointLonLat, 4> subpol_pts_ll; |
| 657 |
subpol_pts_ll[0] = cell_ll; |
| 658 |
subpol_pts_ll[1] = xyz2ll(iedge_mid); |
| 659 |
subpol_pts_ll[2] = pts_ll[inode_n]; |
| 660 |
subpol_pts_ll[3] = xyz2ll(jedge_mid); |
| 661 |
|
| 662 |
idx_t node_n = cell2node(cell, inode_n); |
| 663 |
if (csp2node[csp_id] == -1) { |
| 664 |
csp2node[csp_id] = node_n; |
| 665 |
node2csp[node_n].emplace_back(csp_id); |
| 666 |
} |
| 667 |
return Polygon(subpol_pts_ll); |
| 668 |
} |
| 669 |
|
| 670 |
|
| 671 |
// Create polygons for cell-centred data. Here, the polygons are mesh cells |
| 672 |
ConservativeSphericalPolygonInterpolation::PolygonArray |
| 673 |
ConservativeSphericalPolygonInterpolation:: |
| 674 |
get_polygons_celldata(FunctionSpace fs, Data::PolygonsData& md) { |
| 675 |
//std::vector<idx_t>& csp2node, std::vector<std::vector<idx_t>>& node2csp, |
| 676 |
//gidx_t& csp_size, std::vector<idx_t>& csp_cell_index, std::vector<idx_t>& csp_index) { |
| 677 |
ATLAS_TRACE("ConservativeSphericalPolygonInterpolation: get_polygons_celldata"); |
| 678 |
PolygonArray cspolygons(md.csp_size); |
| 679 |
auto mesh = extract_mesh(fs); |
| 680 |
for(idx_t i = 0; i < md.csp_size; ++i) { |
| 681 |
cspolygons[i] = get_csp_celldata(i, mesh, md); |
| 682 |
} |
| 683 |
return cspolygons; |
| 684 |
} |
| 685 |
|
| 686 |
|
| 687 |
// Create polygons for cell-vertex data. Here, the polygons are subcells of mesh cells created as |
| 688 |
// (cell_centre, edge_centre, cell_vertex, edge_centre) |
| 689 |
// additionally, subcell-to-node and node-to-subcells mapping are computed |
| 690 |
ConservativeSphericalPolygonInterpolation::PolygonArray |
| 691 |
ConservativeSphericalPolygonInterpolation:: |
| 692 |
get_polygons_nodedata(FunctionSpace fs, Data::PolygonsData& md) { |
| 693 |
ATLAS_TRACE("ConservativeSphericalPolygonInterpolation: get_polygons_nodedata"); |
| 694 |
PolygonArray cspolygons(md.csp_size); |
| 695 |
auto mesh = extract_mesh(fs); |
| 696 |
for(idx_t csp_id = 0; csp_id < md.csp_size; ++csp_id) { |
| 697 |
cspolygons[csp_id] = get_csp_nodedata(csp_id, mesh, md); |
| 698 |
} |
| 699 |
return cspolygons; |
| 700 |
} |
| 701 |
|
| 702 |
|
| 703 |
void ConservativeSphericalPolygonInterpolation::do_setup_impl(const Grid& src_grid, const Grid& tgt_grid) { |
| 704 |
ATLAS_TRACE("ConservativeMethod: do_setup_impl"); |
| 705 |
ATLAS_ASSERT(src_grid); |
| 706 |
ATLAS_ASSERT(tgt_grid); |
| 707 |
|
| 708 |
tgt_fs_ = data_->tgt_fs_; |
| 709 |
src_fs_ = data_->src_fs_; |
| 710 |
|
| 711 |
if (not tgt_fs_) { |
| 712 |
auto tgt_mesh_config = tgt_grid.meshgenerator() | option::halo(0); |
| 713 |
ATLAS_TRACE_SCOPE("Generate target mesh") { tgt_mesh_ = MeshGenerator(tgt_mesh_config).generate(tgt_grid); } |
| 714 |
ATLAS_TRACE_SCOPE("Create target functionspace") { |
| 715 |
if (tgt_cell_data_) { |
| 716 |
tgt_fs_ = functionspace::CellColumns(tgt_mesh_, option::halo(0)); |
| 717 |
} |
| 718 |
else { |
| 719 |
tgt_fs_ = functionspace::NodeColumns(tgt_mesh_, option::halo(1)); |
| 720 |
} |
| 721 |
} |
| 722 |
sharable_data_->tgt_fs_ = tgt_fs_; |
| 723 |
ATLAS_ASSERT(data_->tgt_fs_); |
| 724 |
} |
| 725 |
|
| 726 |
if (not src_fs_) { |
| 727 |
auto src_mesh_config = src_grid.meshgenerator() | option::halo(2); |
| 728 |
ATLAS_TRACE_SCOPE("Generate source mesh") { |
| 729 |
if (mpi::size() > 1) { |
| 730 |
src_mesh_ = MeshGenerator(src_mesh_config).generate(src_grid, grid::MatchingPartitioner(tgt_mesh_)); |
| 731 |
} |
| 732 |
else { |
| 733 |
src_mesh_ = MeshGenerator(src_mesh_config).generate(src_grid); |
| 734 |
} |
| 735 |
} |
| 736 |
ATLAS_TRACE_SCOPE("Create source functionspace") { |
| 737 |
if (src_cell_data_) { |
| 738 |
src_fs_ = functionspace::CellColumns(src_mesh_, option::halo(2)); |
| 739 |
} |
| 740 |
else { |
| 741 |
src_fs_ = functionspace::NodeColumns(src_mesh_, option::halo(2)); |
| 742 |
} |
| 743 |
} |
| 744 |
sharable_data_->src_fs_ = src_fs_; |
| 745 |
ATLAS_ASSERT(data_->tgt_fs_); |
| 746 |
} |
| 747 |
|
| 748 |
do_setup(src_fs_, tgt_fs_); |
| 749 |
} |
| 750 |
|
| 751 |
|
| 752 |
void ConservativeSphericalPolygonInterpolation::do_setup(const Grid& src_grid, const Grid& tgt_grid, |
| 753 |
const interpolation::Cache& cache) { |
| 754 |
ATLAS_TRACE("ConservativeSphericalPolygonInterpolation: do_setup with grids"); |
| 755 |
|
| 756 |
if (Cache(cache)) { |
| 757 |
Log::debug() << "Interpolation data found in cache -> no polygon intersections required" << std::endl; |
| 758 |
cache_ = Cache(cache); |
| 759 |
data_ = cache_.get(); |
| 760 |
sharable_data_.reset(); |
| 761 |
|
| 762 |
src_fs_ = data_->src_fs_; |
| 763 |
tgt_fs_ = data_->tgt_fs_; |
| 764 |
|
| 765 |
src_cell_data_ = functionspace::CellColumns(src_fs_); |
| 766 |
tgt_cell_data_ = functionspace::CellColumns(tgt_fs_); |
| 767 |
|
| 768 |
src_mesh_ = extract_mesh(src_fs_); |
| 769 |
tgt_mesh_ = extract_mesh(tgt_fs_); |
| 770 |
|
| 771 |
if (order_ == 1 && matrix_free_) { |
| 772 |
// We don't need to continue with setups required for first order matrix-free |
| 773 |
// such as mesh generation and functionspace creation. |
| 774 |
return; |
| 775 |
} |
| 776 |
} |
| 777 |
|
| 778 |
if (not matrix_free_) { |
| 779 |
auto matrix_cache = interpolation::MatrixCache(cache); |
| 780 |
if (matrix_cache) { |
| 781 |
if (matrix_cache.uid() == std::to_string(order_) || matrix_cache.uid().empty()) { |
| 782 |
Log::debug() << "Matrix found in cache -> no setup required at all" << std::endl; |
| 783 |
setMatrix(matrix_cache); |
| 784 |
return; |
| 785 |
} |
| 786 |
} |
| 787 |
} |
| 788 |
|
| 789 |
do_setup_impl(src_grid, tgt_grid); |
| 790 |
} |
| 791 |
|
| 792 |
|
| 793 |
void ConservativeSphericalPolygonInterpolation::do_setup(const FunctionSpace& source, const FunctionSpace& target, |
| 794 |
const interpolation::Cache& cache) { |
| 795 |
ATLAS_TRACE("ConservativeSphericalPolygonInterpolation: do_setup with function spaces"); |
| 796 |
|
| 797 |
if (not matrix_free_) { |
| 798 |
auto matrix_cache = interpolation::MatrixCache(cache); |
| 799 |
if (matrix_cache) { |
| 800 |
if (matrix_cache.uid() == std::to_string(order_) || matrix_cache.uid().empty()) { |
| 801 |
Log::debug() << "Matrix found in cache -> no setup required at all" << std::endl; |
| 802 |
setMatrix(matrix_cache); |
| 803 |
src_fs_ = source; |
| 804 |
tgt_fs_ = target; |
| 805 |
return; |
| 806 |
} |
| 807 |
} |
| 808 |
else { |
| 809 |
Log::debug() << "Could not find matrix in cache" << std::endl; |
| 810 |
} |
| 811 |
} |
| 812 |
|
| 813 |
if (Cache(cache)) { |
| 814 |
Log::debug() << "Interpolation data found in cache -> no polygon intersections required" << std::endl; |
| 815 |
cache_ = Cache(cache); |
| 816 |
data_ = cache_.get(); |
| 817 |
sharable_data_.reset(new Data()); |
| 818 |
tgt_fs_ = target; |
| 819 |
src_fs_ = source; |
| 820 |
src_cell_data_ = functionspace::CellColumns(src_fs_); |
| 821 |
tgt_cell_data_ = functionspace::CellColumns(tgt_fs_); |
| 822 |
|
| 823 |
src_mesh_ = extract_mesh(src_fs_); |
| 824 |
tgt_mesh_ = extract_mesh(tgt_fs_); |
| 825 |
|
| 826 |
if (order_ == 1 && matrix_free_) { |
| 827 |
// We don't need to continue with setups required for first order matrix-free |
| 828 |
// such as mesh generation and functionspace creation. |
| 829 |
return; |
| 830 |
} |
| 831 |
} |
| 832 |
|
| 833 |
do_setup(source, target); |
| 834 |
} |
| 835 |
|
| 836 |
void ConservativeSphericalPolygonInterpolation::do_setup(const FunctionSpace& src_fs, const FunctionSpace& tgt_fs) { |
| 837 |
ATLAS_TRACE("ConservativeSphericalPolygonInterpolation: do_setup with function spaces"); |
| 838 |
ATLAS_ASSERT(src_fs); |
| 839 |
ATLAS_ASSERT(tgt_fs); |
| 840 |
|
| 841 |
bool compute_cache = data_->src_.points.empty(); |
| 842 |
|
| 843 |
if (not data_->tgt_fs_) { |
| 844 |
tgt_fs_ = tgt_fs; |
| 845 |
sharable_data_->tgt_fs_ = tgt_fs_; |
| 846 |
} |
| 847 |
if (not data_->src_fs_) { |
| 848 |
src_fs_ = src_fs; |
| 849 |
sharable_data_->src_fs_ = src_fs_; |
| 850 |
} |
| 851 |
|
| 852 |
src_cell_data_ = functionspace::CellColumns(src_fs_); |
| 853 |
tgt_cell_data_ = functionspace::CellColumns(tgt_fs_); |
| 854 |
|
| 855 |
src_mesh_ = extract_mesh(src_fs_); |
| 856 |
tgt_mesh_ = extract_mesh(tgt_fs_); |
| 857 |
|
| 858 |
auto& src = sharable_data_->src_; |
| 859 |
auto& tgt = sharable_data_->tgt_; |
| 860 |
auto& timings = sharable_data_->timings; |
| 861 |
|
| 862 |
if (mpi::size() > 1) { |
| 863 |
// we need src_halo_size >= 2, tgt_halo_size >= 0 for CellColumns |
| 864 |
// if target is NodeColumns, we need: |
| 865 |
// tgt_halo_size >= 1 and |
| 866 |
// src_halo_size large enough to cover the the target halo cells in the first row |
| 867 |
int halo_size = 0; |
| 868 |
src_mesh_.metadata().get("halo", halo_size); |
| 869 |
if (halo_size < 2 and order_ == 2) { |
| 870 |
Log::warning() << "The halo size on source mesh should be at least 2 for the 2nd order CSP interpolation." << std::endl; |
| 871 |
} |
| 872 |
if (not tgt_cell_data_) { |
| 873 |
Log::warning() << "The source cells should cover the half of the first target halo row." << std::endl; |
| 874 |
} |
| 875 |
halo_size = 0; |
| 876 |
tgt_mesh_.metadata().get("halo", halo_size); |
| 877 |
if (not tgt_cell_data_ and halo_size == 0) { |
| 878 |
Log::warning() << "The halo size on target mesh should be at least 1 for the target NodeColumns." << std::endl; |
| 879 |
} |
| 880 |
} |
| 881 |
|
| 882 |
PolygonArray src_csp; |
| 883 |
if (compute_cache) { |
| 884 |
ATLAS_TRACE_SCOPE("Get source polygons") { |
| 885 |
StopWatch stopwatch; |
| 886 |
stopwatch.start(); |
| 887 |
init_polygons_data(src_fs_, src); |
| 888 |
src_csp = get_polygons(src_fs_, src); |
| 889 |
stopwatch.stop(); |
| 890 |
remap_stat_.memory[Statistics::MEM_SRC_PLG] = memory_of(src_csp); |
| 891 |
timings.source_polygons_assembly = stopwatch.elapsed(); |
| 892 |
remap_stat_.counts[Statistics::NUM_SRC_PLG] = src_csp.size(); |
| 893 |
} |
| 894 |
|
| 895 |
ATLAS_TRACE_SCOPE("Get target polygons") { |
| 896 |
StopWatch stopwatch; |
| 897 |
stopwatch.start(); |
| 898 |
init_polygons_data(tgt_fs_, tgt); |
| 899 |
stopwatch.stop(); |
| 900 |
timings.target_polygons_assembly = stopwatch.elapsed(); |
| 901 |
remap_stat_.counts[Statistics::NUM_TGT_PLG] = tgt.csp_size; |
| 902 |
} |
| 903 |
} |
| 904 |
|
| 905 |
n_spoints_ = src_fs_.size(); |
| 906 |
n_tpoints_ = tgt_fs_.size(); |
| 907 |
|
| 908 |
if (compute_cache) { |
| 909 |
intersect_polygons(src_csp); |
| 910 |
|
| 911 |
// These arrays correspond to functionspace! |
| 912 |
auto& src_points = src.points; |
| 913 |
auto& src_areas = src.areas; |
| 914 |
src_points.resize(n_spoints_); |
| 915 |
src_areas.resize(n_spoints_); |
| 916 |
{ |
| 917 |
ATLAS_TRACE("Store src_areas and src_point"); |
| 918 |
if (src.cell_data) { |
| 919 |
for (idx_t scsp_id = 0; scsp_id < src.csp_size; ++scsp_id) { |
| 920 |
const idx_t spt = csp_to_cell(scsp_id, src); |
| 921 |
const auto& s_csp = src_csp[scsp_id]; |
| 922 |
src_points[spt] = s_csp.centroid(); |
| 923 |
src_areas[spt] = s_csp.area(); |
| 924 |
} |
| 925 |
} |
| 926 |
else { |
| 927 |
auto& src_node2csp = src.node2csp; |
| 928 |
const auto lonlat = array::make_view<double, 2>(src_mesh_.nodes().lonlat()); |
| 929 |
for (idx_t spt = 0; spt < n_spoints_; ++spt) { |
| 930 |
if (src_node2csp[spt].size() == 0) { |
| 931 |
// this is a node to which no subpolygon is associated |
| 932 |
// maximal twice per mesh we end here, and that is only when mesh has nodes on poles |
| 933 |
auto p = PointLonLat{lonlat(spt, 0), lonlat(spt, 1)}; |
| 934 |
eckit::geometry::Sphere::convertSphericalToCartesian(1., p, src_points[spt]); |
| 935 |
} |
| 936 |
else { |
| 937 |
// .. in the other case, start computing the barycentre |
| 938 |
src_points[spt] = PointXYZ{0., 0., 0.}; |
| 939 |
} |
| 940 |
src_areas[spt] = 0.; |
| 941 |
for (idx_t isubcell = 0; isubcell < src_node2csp[spt].size(); ++isubcell) { |
| 942 |
idx_t subcell = src_node2csp[spt][isubcell]; |
| 943 |
const auto& s_csp = src_csp[subcell]; |
| 944 |
src_areas[spt] += s_csp.area(); |
| 945 |
src_points[spt] = src_points[spt] + PointXYZ::mul(s_csp.centroid(), s_csp.area()); |
| 946 |
} |
| 947 |
double src_point_norm = PointXYZ::norm(src_points[spt]); |
| 948 |
if (src_point_norm == 0.) { |
| 949 |
// Probably encountered degenerate subpolygons that lead to zero area. Can happen sometimes in ORCA grid |
| 950 |
auto p = PointLonLat{lonlat(spt, 0), lonlat(spt, 1)}; |
| 951 |
eckit::geometry::Sphere::convertSphericalToCartesian(1., p, src_points[spt]); |
| 952 |
src_point_norm = PointXYZ::norm(src_points[spt]); |
| 953 |
} |
| 954 |
src_points[spt] = PointXYZ::div(src_points[spt], src_point_norm); |
| 955 |
} |
| 956 |
} |
| 957 |
} |
| 958 |
auto& tgt_points = tgt.points; |
| 959 |
auto& tgt_areas = tgt.areas; |
| 960 |
tgt_points.resize(n_tpoints_); |
| 961 |
tgt_areas.resize(n_tpoints_); |
| 962 |
{ |
| 963 |
ATLAS_TRACE("Store src_areas and src_point"); |
| 964 |
if (tgt_cell_data_) { |
| 965 |
for (idx_t tpt = 0; tpt < n_tpoints_; ++tpt) { |
| 966 |
auto tgt_csp = get_csp_celldata(tpt, tgt_mesh_, tgt); |
| 967 |
const auto& t_csp = tgt_csp; |
| 968 |
tgt_points[tpt] = t_csp.centroid(); |
| 969 |
tgt_areas[tpt] = t_csp.area(); |
| 970 |
} |
| 971 |
} |
| 972 |
else { |
| 973 |
auto& tgt_node2csp = tgt.node2csp; |
| 974 |
const auto lonlat = array::make_view<double, 2>(tgt_mesh_.nodes().lonlat()); |
| 975 |
for (idx_t tpt = 0; tpt < n_tpoints_; ++tpt) { |
| 976 |
if (tgt_node2csp[tpt].size() == 0) { |
| 977 |
// this is a node to which no subpolygon is associated |
| 978 |
// maximal twice per mesh we end here, and that is only when mesh has nodes on poles |
| 979 |
auto p = PointLonLat{lonlat(tpt, 0), lonlat(tpt, 1)}; |
| 980 |
eckit::geometry::Sphere::convertSphericalToCartesian(1., p, tgt_points[tpt]); |
| 981 |
} |
| 982 |
else { |
| 983 |
// .. in the other case, start computing the barycentre |
| 984 |
tgt_points[tpt] = PointXYZ{0., 0., 0.}; |
| 985 |
} |
| 986 |
tgt_areas[tpt] = 0.; |
| 987 |
for (idx_t isubcell = 0; isubcell < tgt_node2csp[tpt].size(); ++isubcell) { |
| 988 |
idx_t subcell = tgt_node2csp[tpt][isubcell]; |
| 989 |
auto tgt_csp = get_csp_nodedata(subcell, tgt_mesh_, tgt); |
| 990 |
const auto& t_csp = tgt_csp; |
| 991 |
tgt_areas[tpt] += t_csp.area(); |
| 992 |
tgt_points[tpt] = tgt_points[tpt] + PointXYZ::mul(t_csp.centroid(), t_csp.area()); |
| 993 |
} |
| 994 |
double tgt_point_norm = PointXYZ::norm(tgt_points[tpt]); |
| 995 |
if (tgt_point_norm == 0.) { |
| 996 |
// Probably encountered degenerate subpolygons cells with zero area as can happen with ORCA sometimes |
| 997 |
auto p = PointLonLat{lonlat(tpt, 0), lonlat(tpt, 1)}; |
| 998 |
eckit::geometry::Sphere::convertSphericalToCartesian(1., p, tgt_points[tpt]); |
| 999 |
tgt_point_norm = PointXYZ::norm(tgt_points[tpt]); |
| 1000 |
} |
| 1001 |
tgt_points[tpt] = PointXYZ::div(tgt_points[tpt], tgt_point_norm); |
| 1002 |
} |
| 1003 |
} |
| 1004 |
} |
| 1005 |
} |
| 1006 |
|
| 1007 |
if (not matrix_free_) { |
| 1008 |
StopWatch stopwatch; |
| 1009 |
stopwatch.start(); |
| 1010 |
switch (order_) { |
| 1011 |
case 1: { |
| 1012 |
setMatrix(n_tpoints_, n_spoints_, compute_1st_order_triplets(), "1"); |
| 1013 |
break; |
| 1014 |
} |
| 1015 |
case 2: { |
| 1016 |
setMatrix(n_tpoints_, n_spoints_, compute_2nd_order_triplets(), "2"); |
| 1017 |
break; |
| 1018 |
} |
| 1019 |
default: { |
| 1020 |
ATLAS_NOTIMPLEMENTED; |
| 1021 |
} |
| 1022 |
} |
| 1023 |
stopwatch.stop(); |
| 1024 |
if (compute_cache) { |
| 1025 |
timings.matrix_assembly = stopwatch.elapsed(); |
| 1026 |
} |
| 1027 |
} |
| 1028 |
|
| 1029 |
data_->print(Log::debug()); |
| 1030 |
|
| 1031 |
if (remap_stat_.intersection) { |
| 1032 |
setup_stat(); |
| 1033 |
} |
| 1034 |
} |
| 1035 |
|
| 1036 |
|
| 1037 |
namespace { |
| 1038 |
uint64_t xorshift(const uint64_t& n, int i){ |
| 1039 |
return n^(n>>i); |
| 1040 |
} |
| 1041 |
uint64_t hash(const uint64_t& n){ |
| 1042 |
uint64_t p = 0x5555555555555555; // pattern of alternating 0 and 1 |
| 1043 |
uint64_t c = 17316035218449499591ull;// random uneven integer constant; |
| 1044 |
return c*xorshift(p*xorshift(n,32),32); |
| 1045 |
} |
| 1046 |
uint64_t hash_combine(const uint64_t& seed, const uint64_t& v) { |
| 1047 |
uint64_t c = 17316035218449499591ull;// random integer constant; |
| 1048 |
return hash(v)^(seed+c); |
| 1049 |
} |
| 1050 |
|
| 1051 |
using PartitionAndRemoteIndex = std::pair<int, idx_t>; |
| 1052 |
struct HashPartitionAndRemoteIndex { |
| 1053 |
std::size_t operator()(const std::pair<int, idx_t>& pair) const { |
| 1054 |
uint64_t h = 0; |
| 1055 |
h = hash_combine(h, pair.first); |
| 1056 |
h = hash_combine(h, pair.second); |
| 1057 |
return h; |
| 1058 |
} |
| 1059 |
}; |
| 1060 |
} |
| 1061 |
|
| 1062 |
|
| 1063 |
void ConservativeSphericalPolygonInterpolation:: |
| 1064 |
build_source_kdtree(util::KDTree<idx_t>& kdt_search, double& max_srccell_rad, const PolygonArray& src_csp) const { |
| 1065 |
Log::debug() << "Building KDTree via mesh indices." << std::endl; |
| 1066 |
const auto& src = sharable_data_->src_; |
| 1067 |
const auto node_part = array::make_view<int, 1>(src_mesh_.nodes().partition()); |
| 1068 |
const auto node_ridx = array::make_indexview<idx_t, 1>(src_mesh_.nodes().remote_index()); |
| 1069 |
const auto node_ghost = array::make_view<int, 1>(src_mesh_.nodes().ghost()); |
| 1070 |
const auto cell_part = array::make_view<int, 1>(src_mesh_.cells().partition()); |
| 1071 |
const auto cell_halo = array::make_view<int, 1>(src_mesh_.cells().halo()); |
| 1072 |
const auto cell_ridx = array::make_indexview<idx_t, 1>(src_mesh_.cells().remote_index()); |
| 1073 |
auto mpi_rank = mpi::rank(); |
| 1074 |
ATLAS_TRACE_SCOPE("build kd-tree for source polygons") { |
| 1075 |
std::unordered_set<PartitionAndRemoteIndex, HashPartitionAndRemoteIndex> src_kdtree_set; |
| 1076 |
auto consider_src = [&](int part, idx_t ridx, int halo) { |
| 1077 |
if (not halo) { |
| 1078 |
return true; |
| 1079 |
} |
| 1080 |
else if (part != mpi_rank) { |
| 1081 |
return src_kdtree_set.emplace(part, ridx).second; |
| 1082 |
} |
| 1083 |
return false; |
| 1084 |
}; |
| 1085 |
if (! src_cell_data_) { |
| 1086 |
const auto& node2csp = src.node2csp; |
| 1087 |
for (idx_t inode = 0; inode < src_mesh_.nodes().size(); ++inode) { |
| 1088 |
const auto& csp_ids = node2csp[inode]; |
| 1089 |
if (!csp_ids.empty()) { |
| 1090 |
if (consider_src(node_part(inode), node_ridx(inode), node_ghost(inode))) { |
| 1091 |
for (const auto& csp_id : csp_ids) { |
| 1092 |
const auto& s_csp = src_csp[csp_id]; |
| 1093 |
kdt_search.insert(s_csp.centroid(), csp_id); |
| 1094 |
max_srccell_rad = std::max(max_srccell_rad, s_csp.radius()); |
| 1095 |
} |
| 1096 |
} |
| 1097 |
} |
| 1098 |
} |
| 1099 |
} |
| 1100 |
else { |
| 1101 |
for (idx_t csp_id = 0; csp_id < src.csp_size; ++csp_id) { |
| 1102 |
idx_t icell = csp_to_cell(csp_id, src); |
| 1103 |
if (consider_src(cell_part(icell), cell_ridx(icell), cell_halo(icell))) { |
| 1104 |
const auto& s_csp = src_csp[csp_id]; |
| 1105 |
kdt_search.insert(s_csp.centroid(), icell); |
| 1106 |
max_srccell_rad = std::max(max_srccell_rad, s_csp.radius()); |
| 1107 |
} |
| 1108 |
} |
| 1109 |
} |
| 1110 |
kdt_search.build(); |
| 1111 |
} |
| 1112 |
} |
| 1113 |
|
| 1114 |
|
| 1115 |
void ConservativeSphericalPolygonInterpolation:: |
| 1116 |
build_source_kdtree_centroid(util::KDTree<idx_t>& kdt_search, double& max_srccell_rad, const PolygonArray& src_csp) const { |
| 1117 |
Log::warning() << "Building KDTree via centroid (obsolete)." << std::endl; |
| 1118 |
|
| 1119 |
// Treshold at which points are considered same |
| 1120 |
double compare_pointxyz_eps = 1.e8 * std::numeric_limits<double>::epsilon(); |
| 1121 |
const char* ATLAS_COMPAREPOINTXYZ_EPS_FACTOR = ::getenv("ATLAS_COMPAREPOINTXYZ_EPS_FACTOR"); |
| 1122 |
if (ATLAS_COMPAREPOINTXYZ_EPS_FACTOR != nullptr) { |
| 1123 |
compare_pointxyz_eps = std::atof(ATLAS_COMPAREPOINTXYZ_EPS_FACTOR) * std::numeric_limits<double>::epsilon(); |
| 1124 |
} |
| 1125 |
auto compare_pointxyz = [eps=compare_pointxyz_eps] (const PointXYZ& f, const PointXYZ& s) -> bool { |
| 1126 |
if (f[0] < s[0] - eps) { |
| 1127 |
return true; |
| 1128 |
} |
| 1129 |
else if (std::abs(f[0] - s[0]) < eps) { |
| 1130 |
if (f[1] < s[1] - eps) { |
| 1131 |
return true; |
| 1132 |
} |
| 1133 |
else if (std::abs(f[1] - s[1]) < eps) { |
| 1134 |
if (f[2] < s[2] - eps) { |
| 1135 |
return true; |
| 1136 |
} |
| 1137 |
} |
| 1138 |
} |
| 1139 |
return false; |
| 1140 |
}; |
| 1141 |
std::set<PointXYZ, decltype(compare_pointxyz)> src_cent(compare_pointxyz); |
| 1142 |
auto src_already_in = [&](const PointXYZ& halo_cent) { |
| 1143 |
if (src_cent.find(halo_cent) == src_cent.end()) { |
| 1144 |
src_cent.insert(halo_cent); |
| 1145 |
return false; |
| 1146 |
} |
| 1147 |
return true; |
| 1148 |
}; |
| 1149 |
ATLAS_TRACE_SCOPE("build kd-tree for source polygons") { |
| 1150 |
for (idx_t scell = 0; scell < src_csp.size(); ++scell) { |
| 1151 |
const auto& s_csp = src_csp[scell]; |
| 1152 |
if (not src_already_in((s_csp.centroid()))) { |
| 1153 |
kdt_search.insert(s_csp.centroid(), scell); |
| 1154 |
max_srccell_rad = std::max(max_srccell_rad, s_csp.radius()); |
| 1155 |
} |
| 1156 |
} |
| 1157 |
kdt_search.build(); |
| 1158 |
} |
| 1159 |
} |
| 1160 |
|
| 1161 |
|
| 1162 |
void ConservativeSphericalPolygonInterpolation::intersect_polygons(const PolygonArray& src_csp) { |
| 1163 |
ATLAS_TRACE(); |
| 1164 |
auto& timings = sharable_data_->timings; |
| 1165 |
auto& tgt = sharable_data_->tgt_; |
| 1166 |
|
| 1167 |
StopWatch stopwatch; |
| 1168 |
util::KDTree<idx_t> kdt_search; |
| 1169 |
double max_srccell_rad = 0.; |
| 1170 |
{ |
| 1171 |
stopwatch.start(); |
| 1172 |
kdt_search.reserve(src_csp.size()); |
| 1173 |
int build_with_centroids = 0; |
| 1174 |
const char* ATLAS_BUILD_KDTREE_CENTROID = ::getenv("ATLAS_BUILD_KDTREE_CENTROID"); |
| 1175 |
if (ATLAS_BUILD_KDTREE_CENTROID != nullptr) { |
| 1176 |
build_with_centroids = std::atof(ATLAS_BUILD_KDTREE_CENTROID); |
| 1177 |
} |
| 1178 |
if (build_with_centroids) { |
| 1179 |
build_source_kdtree_centroid(kdt_search, max_srccell_rad, src_csp); |
| 1180 |
} |
| 1181 |
else { |
| 1182 |
build_source_kdtree(kdt_search, max_srccell_rad, src_csp); |
| 1183 |
} |
| 1184 |
stopwatch.stop(); |
| 1185 |
} |
| 1186 |
timings.source_kdtree_assembly = stopwatch.elapsed(); |
| 1187 |
|
| 1188 |
StopWatch stopwatch_kdtree_search; |
| 1189 |
StopWatch stopwatch_polygon_intersections; |
| 1190 |
|
| 1191 |
enum MeshSizeId |
| 1192 |
{ |
| 1193 |
SRC, |
| 1194 |
TGT, |
| 1195 |
SRC_TGT_INTERSECT, |
| 1196 |
TGT_NONINTERSECT |
| 1197 |
}; |
| 1198 |
enum AreaCoverageId |
| 1199 |
{ |
| 1200 |
TOTAL_TGT, |
| 1201 |
MAX_TGT |
| 1202 |
}; |
| 1203 |
std::array<size_t, Statistics::NUM_ENUM_SIZE> num_pol{0, 0, 0, 0, 0}; |
| 1204 |
std::array<double, 2> area_coverage{0., 0.}; |
| 1205 |
|
| 1206 |
auto& tgt_iparam_ = sharable_data_->tgt_iparam_; |
| 1207 |
auto& tgt_csp_size = tgt.csp_size; |
| 1208 |
tgt_iparam_.resize(tgt_csp_size); |
| 1209 |
|
| 1210 |
std::vector<InterpolationParameters> src_iparam; // only used for debugging |
| 1211 |
if (validate_) { |
| 1212 |
src_iparam.resize(src_csp.size()); |
| 1213 |
} |
| 1214 |
|
| 1215 |
// the worst target polygon coverage for analysis of intersection |
| 1216 |
std::pair<idx_t, double> worst_tgt_overcover; |
| 1217 |
std::pair<idx_t, double> worst_tgt_undercover; |
| 1218 |
worst_tgt_overcover.first = -1; |
| 1219 |
worst_tgt_overcover.second = -1.; |
| 1220 |
worst_tgt_undercover.first = -1; |
| 1221 |
worst_tgt_undercover.second = M_PI; // M_PI is the maximum area of a polygon on the unit sphere |
| 1222 |
|
| 1223 |
// NOTE: polygon vertex points at distance < pointsSameEPS will be replaced with one point |
| 1224 |
constexpr double pointsSameEPS = 5.e6 * std::numeric_limits<double>::epsilon(); |
| 1225 |
|
| 1226 |
std::vector<idx_t> intersection_scsp_ids; |
| 1227 |
std::vector<double> intersection_weights; |
| 1228 |
std::vector<PointXYZ> intersection_src_centroids; |
| 1229 |
|
| 1230 |
bool fpe_for_polygon = Polygon::fpe(); |
| 1231 |
Polygon::fpe(false); |
| 1232 |
bool fpe_disabled = fpe_for_polygon ? atlas::library::disable_floating_point_exception(FE_INVALID) : false; |
| 1233 |
auto restore_fpe = [fpe_disabled, fpe_for_polygon] { |
| 1234 |
if (fpe_disabled) { |
| 1235 |
atlas::library::enable_floating_point_exception(FE_INVALID); |
| 1236 |
} |
| 1237 |
Polygon::fpe(fpe_for_polygon); |
| 1238 |
}; |
| 1239 |
|
| 1240 |
const auto cell_halo = array::make_view<const int, 1>(tgt_mesh_.cells().halo()); |
| 1241 |
const auto node_ghost = array::make_view<int, 1>(tgt_mesh_.nodes().ghost()); |
| 1242 |
auto skip_target = [&] (idx_t tcsp_id) -> bool { |
| 1243 |
if (tgt.cell_data) { |
| 1244 |
auto tcell = csp_to_cell(tcsp_id, tgt); |
| 1245 |
return cell_halo(tcell); |
| 1246 |
} |
| 1247 |
else { |
| 1248 |
auto tnode = tgt.csp2node[tcsp_id]; |
| 1249 |
return node_ghost(tnode); |
| 1250 |
} |
| 1251 |
}; |
| 1252 |
|
| 1253 |
ATLAS_TRACE_SCOPE("intersecting polygons") { |
| 1254 |
eckit::Channel blackhole; |
| 1255 |
eckit::ProgressTimer progress("Intersecting polygons ", 100, " percent", double(10), |
| 1256 |
tgt_csp_size > 50 ? Log::info() : blackhole); |
| 1257 |
float last_progress_percent = 0.00; |
| 1258 |
for (idx_t tcsp_id = 0; tcsp_id < tgt_csp_size; ++tcsp_id) { |
| 1259 |
auto tgt_csp = get_tgt_csp(tcsp_id); // This needs to be before 'skip_target' because it fills in tgt.csp2node node-data |
| 1260 |
// A bit unfortunate... to be improved! |
| 1261 |
if (skip_target(tcsp_id)) { |
| 1262 |
continue; |
| 1263 |
} |
| 1264 |
intersection_scsp_ids.resize(0); |
| 1265 |
intersection_weights.resize(0); |
| 1266 |
intersection_src_centroids.resize(0); |
| 1267 |
const auto& t_csp = tgt_csp; |
| 1268 |
double tgt_cover_area = 0.; |
| 1269 |
if (remap_stat_.timings) {stopwatch_kdtree_search.start(); } |
| 1270 |
auto scsp_ids = kdt_search.closestPointsWithinRadius(t_csp.centroid(), t_csp.radius() + max_srccell_rad).payloads(); |
| 1271 |
if (remap_stat_.timings) {stopwatch_kdtree_search.stop(); } |
| 1272 |
for (const auto& scsp_id: scsp_ids) { |
| 1273 |
const auto& s_csp = src_csp[scsp_id]; |
| 1274 |
if (remap_stat_.timings) {stopwatch_polygon_intersections.start(); } |
| 1275 |
Polygon csp_i = s_csp.intersect(t_csp, nullptr, pointsSameEPS); |
| 1276 |
if (remap_stat_.timings) {stopwatch_polygon_intersections.stop(); } |
| 1277 |
double csp_i_area = csp_i.area(); |
| 1278 |
#if ATLAS_BUILD_TYPE_DEBUG |
| 1279 |
if (validate_) { |
| 1280 |
int pout; |
| 1281 |
if (inside_vertices(t_csp, s_csp, pout) > 2 && csp_i.area() < 3e-16) { |
| 1282 |
dump_intersection("Zero area intersections with inside_vertices", t_csp, src_csp, scsp_ids); |
| 1283 |
} |
| 1284 |
} |
| 1285 |
#endif |
| 1286 |
if (csp_i_area > 0) { |
| 1287 |
intersection_scsp_ids.emplace_back(scsp_id); |
| 1288 |
intersection_weights.emplace_back(csp_i_area); |
| 1289 |
if (order_ == 2 or not matrix_free_ or not matrixAllocated()) { |
| 1290 |
intersection_src_centroids.emplace_back(csp_i.centroid()); |
| 1291 |
} |
| 1292 |
tgt_cover_area += csp_i_area; |
| 1293 |
if (std::abs(1. - tgt_cover_area / tgt_csp.area()) <= 1e-10) { |
| 1294 |
break; |
| 1295 |
} |
| 1296 |
if (validate_) { |
| 1297 |
src_iparam[scsp_id].csp_ids.emplace_back(tcsp_id); |
| 1298 |
src_iparam[scsp_id].weights.emplace_back(csp_i_area); |
| 1299 |
if (csp_i_area > 1.1 * t_csp.area()) { |
| 1300 |
dump_intersection("Intersection larger than target", t_csp, src_csp, scsp_ids); |
| 1301 |
} |
| 1302 |
if (csp_i_area > 1.1 * s_csp.area()) { |
| 1303 |
dump_intersection("Intersection larger than source", t_csp, src_csp, scsp_ids); |
| 1304 |
} |
| 1305 |
} |
| 1306 |
} |
| 1307 |
} |
| 1308 |
const double tgt_cover_err = std::abs(t_csp.area() - tgt_cover_area); |
| 1309 |
const double tgt_cover_err_percent = 100. * tgt_cover_err / t_csp.area(); |
| 1310 |
if (validate_ && tgt_cover_err_percent > 0.1) { |
| 1311 |
if (mpi::size() == 1) { |
| 1312 |
dump_intersection("Target cell not exactly covered", t_csp, src_csp, scsp_ids); |
| 1313 |
if (remap_stat_.intersection) { |
| 1314 |
area_coverage[TOTAL_TGT] += tgt_cover_err; |
| 1315 |
area_coverage[MAX_TGT] = std::max(area_coverage[MAX_TGT], tgt_cover_err); |
| 1316 |
} |
| 1317 |
} |
| 1318 |
} |
| 1319 |
if (intersection_scsp_ids.size() == 0 and remap_stat_.intersection) { |
| 1320 |
num_pol[Statistics::NUM_UNCVR_FULL_TGT]++; |
| 1321 |
} |
| 1322 |
else if (tgt_cover_err_percent > 0.1 && remap_stat_.intersection) { |
| 1323 |
num_pol[Statistics::NUM_UNCVR_PART_TGT]++; |
| 1324 |
} |
| 1325 |
// if (normalise_intersections_ && tgt_cover_err_percent < 1.) { |
| 1326 |
// double wfactor = t_csp.area() / (tgt_cover_area > 0. ? tgt_cover_area : 1.); |
| 1327 |
// for (idx_t i = 0; i < intersection_weights.size(); i++) { |
| 1328 |
// intersection_weights[i] *= wfactor; |
| 1329 |
// } |
| 1330 |
// } |
| 1331 |
tgt_iparam_[tcsp_id].csp_ids = intersection_scsp_ids; |
| 1332 |
tgt_iparam_[tcsp_id].weights = intersection_weights; |
| 1333 |
if (order_ == 2 or not matrix_free_ or not matrixAllocated()) { |
| 1334 |
tgt_iparam_[tcsp_id].centroids = intersection_src_centroids; |
| 1335 |
} |
| 1336 |
if (remap_stat_.intersection) { |
| 1337 |
num_pol[Statistics::NUM_INT_PLG] += tgt_iparam_[tcsp_id].weights.size(); |
| 1338 |
} |
| 1339 |
if ( double(tcsp_id) / double(tgt_csp_size) > last_progress_percent ) { |
| 1340 |
last_progress_percent += 0.01; |
| 1341 |
++progress; |
| 1342 |
} |
| 1343 |
} |
| 1344 |
} |
| 1345 |
|
| 1346 |
restore_fpe(); |
| 1347 |
|
| 1348 |
timings.polygon_intersections = stopwatch_polygon_intersections.elapsed(); |
| 1349 |
timings.source_kdtree_search = stopwatch_kdtree_search.elapsed(); |
| 1350 |
num_pol[Statistics::NUM_SRC_PLG] = src_csp.size(); |
| 1351 |
num_pol[Statistics::NUM_TGT_PLG] = tgt_csp_size; |
| 1352 |
ATLAS_TRACE_MPI(ALLREDUCE) { |
| 1353 |
mpi::comm().allReduceInPlace(num_pol.data(), num_pol.size(), eckit::mpi::sum()); |
| 1354 |
} |
| 1355 |
remap_stat_.counts[Statistics::NUM_INT_PLG] = num_pol[Statistics::NUM_INT_PLG]; |
| 1356 |
remap_stat_.counts[Statistics::NUM_UNCVR_FULL_TGT] = num_pol[Statistics::NUM_UNCVR_FULL_TGT]; |
| 1357 |
remap_stat_.counts[Statistics::NUM_UNCVR_PART_TGT] = num_pol[Statistics::NUM_UNCVR_PART_TGT]; |
| 1358 |
|
| 1359 |
const std::string polygon_intersection_folder = "polygon_intersection/"; |
| 1360 |
if (validate_ && mpi::rank() == 0) { |
| 1361 |
if (filesystem::exists(polygon_intersection_folder)) { |
| 1362 |
filesystem::remove_all(polygon_intersection_folder); |
| 1363 |
} |
| 1364 |
filesystem::create_directory(polygon_intersection_folder); |
| 1365 |
Log::warning() << "The worst target polygon coverage on the task 0 is in the folder \e[1mpolygon_intersection\e[0m." << std::endl; |
| 1366 |
} |
| 1367 |
|
| 1368 |
if (validate_) { |
| 1369 |
ATLAS_TRACE_SCOPE("compute errors in coverting target cells with intersections") { |
| 1370 |
double geo_err_l1 = 0.; |
| 1371 |
double geo_err_linf = -1.; |
| 1372 |
for (idx_t tcsp_id = 0; tcsp_id < tgt_csp_size; ++tcsp_id) { |
| 1373 |
auto tgt_csp = get_tgt_csp(tcsp_id); |
| 1374 |
if (skip_target(tcsp_id)) { |
| 1375 |
continue; |
| 1376 |
} |
| 1377 |
auto& t_csp = tgt_csp; |
| 1378 |
auto& tiparam = tgt_iparam_[tcsp_id]; |
| 1379 |
double tgt_cover_err = 0.; |
| 1380 |
for (idx_t icell = 0; icell < tiparam.weights.size(); ++icell) { |
| 1381 |
tgt_cover_err += tiparam.weights[icell]; |
| 1382 |
} |
| 1383 |
tgt_cover_err = 100. * (tgt_cover_err - t_csp.area()) / t_csp.area(); |
| 1384 |
if (worst_tgt_overcover.second < tgt_cover_err) { |
| 1385 |
worst_tgt_overcover.second = tgt_cover_err;; |
| 1386 |
worst_tgt_overcover.first = tcsp_id; |
| 1387 |
} |
| 1388 |
if (worst_tgt_undercover.second > tgt_cover_err) { |
| 1389 |
worst_tgt_undercover.second = tgt_cover_err; |
| 1390 |
worst_tgt_undercover.first = tcsp_id; |
| 1391 |
} |
| 1392 |
if (std::abs(tgt_cover_err) > 0.5) { |
| 1393 |
std::fstream polygon_intersection_info("polygon_intersection", std::ios::out); |
| 1394 |
PointLonLat centre_ll; |
| 1395 |
eckit::geometry::Sphere::convertCartesianToSpherical(1., t_csp.centroid(), centre_ll); |
| 1396 |
polygon_intersection_info << "WARNING tgt cell " << tcsp_id << " over-covering: \e[1m" << tgt_cover_err << "\e[0m %, cell-centre: " |
| 1397 |
<< centre_ll <<"\n"; |
| 1398 |
polygon_intersection_info << "source indices: " << tiparam.csp_ids << std::endl; |
| 1399 |
dump_intersection("Target cell not exaclty covered", t_csp, src_csp, tiparam.csp_ids); |
| 1400 |
//dump_polygons_to_json(t_csp, src_csp, tiparam.csp_ids, "bad_polygon", 1.e-16); |
| 1401 |
} |
| 1402 |
geo_err_l1 += std::abs(0.01 * tgt_cover_err * t_csp.area()); |
| 1403 |
geo_err_linf = std::max(geo_err_linf, std::abs(0.01 * tgt_cover_err * t_csp.area())); |
| 1404 |
} |
| 1405 |
ATLAS_TRACE_MPI(ALLREDUCE) { |
| 1406 |
mpi::comm().allReduceInPlace(geo_err_l1, eckit::mpi::sum()); |
| 1407 |
mpi::comm().allReduceInPlace(geo_err_linf, eckit::mpi::max()); |
| 1408 |
} |
| 1409 |
remap_stat_.errors[Statistics::ERR_TGT_INTERSECTPLG_L1] = geo_err_l1 / unit_sphere_area(); |
| 1410 |
remap_stat_.errors[Statistics::ERR_TGT_INTERSECTPLG_LINF] = geo_err_linf; |
| 1411 |
} |
| 1412 |
|
| 1413 |
ATLAS_TRACE_SCOPE("compute errors in covering source cells with intersections") { |
| 1414 |
// NOTE: source cell at process boundary will not be covered by target cells, skip if MPI > 1 |
| 1415 |
// TODO: mark these source cells beforehand and compute error in them among the processes |
| 1416 |
if (remap_stat_.intersection) { |
| 1417 |
// ATLAS_TRACE("computing covering source cells errors"); |
| 1418 |
// double geo_err_l1 = 0.; |
| 1419 |
// double geo_err_linf = -1.; |
| 1420 |
// for (idx_t scell = 0; scell < src_csp.size(); ++scell) { |
| 1421 |
// if (src_csp[scell].halo_type != 0) { |
| 1422 |
// continue; // skip periodic & halo cells |
| 1423 |
// } |
| 1424 |
// const auto& s_csp = src_csp[scell]; |
| 1425 |
// auto diff_cell = s_csp.area(); |
| 1426 |
// const auto& siparam = src_iparam[scell]; |
| 1427 |
// for (idx_t icell = 0; icell < siparam.weights.size(); ++icell) { |
| 1428 |
// diff_cell -= siparam.weights[icell]; |
| 1429 |
// } |
| 1430 |
// geo_err_l1 += std::abs(diff_cell); |
| 1431 |
// geo_err_linf = std::max(geo_err_linf, std::abs(diff_cell)); |
| 1432 |
// } |
| 1433 |
} |
| 1434 |
#if PRINT_BAD_POLYGONS |
| 1435 |
// TODO: need environment variable to print out intersection of source cell with target polygons |
| 1436 |
|
| 1437 |
//Â dump polygons in json format |
| 1438 |
idx_t scell_printout = 120; |
| 1439 |
if (scell == scell_printout) { |
| 1440 |
dump_polygons_to_json(t_csp, 1.e-14, src_csp, siparam.csp_ids, "polygon_dump", "scell" + std::to_string(scell_printout)); |
| 1441 |
} |
| 1442 |
#endif |
| 1443 |
/* |
| 1444 |
// TODO: normalise to source cell |
| 1445 |
double normm = s_csp.area() / (src_cover_area > 0. ? src_cover_area : s_csp.area()); |
| 1446 |
for (idx_t icell = 0; icell < siparam.csp_ids.size(); ++icell) { |
| 1447 |
idx_t scell = siparam.csp_ids[icell]; |
| 1448 |
auto siparam = src_iparam_[scell]; |
| 1449 |
size_t src_intersectors = siparam.csp_ids.size(); |
| 1450 |
for (idx_t ticell = 0; ticell < src_intersectors; ticell++ ) { |
| 1451 |
if (siparam.csp_ids[icell] == ticell) {; |
| 1452 |
siparam.weights[icell] *= normm; |
| 1453 |
siparam.tgt_weights[icell] *= normm; |
| 1454 |
} |
| 1455 |
} |
| 1456 |
} |
| 1457 |
*/ |
| 1458 |
} |
| 1459 |
} |
| 1460 |
|
| 1461 |
if (validate_) { |
| 1462 |
ATLAS_TRACE_SCOPE("compute and output worst target polygon coverage by its intersections with source polygons") { |
| 1463 |
std::vector<idx_t> first(mpi::comm().size()); |
| 1464 |
std::vector<double> second(mpi::comm().size()); |
| 1465 |
ATLAS_TRACE_MPI(ALLGATHER) { |
| 1466 |
mpi::comm().allGather(worst_tgt_overcover.first, first.begin(), first.end()); |
| 1467 |
mpi::comm().allGather(worst_tgt_overcover.second, second.begin(), second.end()); |
| 1468 |
} |
| 1469 |
auto max_over = std::max_element(second.begin(), second.end()); |
| 1470 |
auto rank_over = std::distance(second.begin(), max_over); |
| 1471 |
Log::info() << "WARNING The worst target polygon over-coveraging: \e[1m" |
| 1472 |
<< *max_over |
| 1473 |
<< "\e[0m %. For details, check the file: worst_target_cell_overcover.info " << std::endl; |
| 1474 |
if (rank_over == mpi::rank()) { |
| 1475 |
auto tcsp_id = worst_tgt_overcover.first; |
| 1476 |
auto tgt_csp = get_tgt_csp(tcsp_id); |
| 1477 |
dump_polygons_to_json(tgt_csp, pointsSameEPS, src_csp, tgt_iparam_[tcsp_id].csp_ids, polygon_intersection_folder, "worst_target_cell_overcover"); |
| 1478 |
} |
| 1479 |
ATLAS_TRACE_MPI(ALLGATHER) { |
| 1480 |
mpi::comm().allGather(worst_tgt_undercover.first, first.begin(), first.end()); |
| 1481 |
mpi::comm().allGather(worst_tgt_undercover.second, second.begin(), second.end()); |
| 1482 |
} |
| 1483 |
auto min_under = std::min_element(second.begin(), second.end()); |
| 1484 |
auto rank_under = std::distance(second.begin(), min_under); |
| 1485 |
Log::info() << "WARNING The worst target polygon under-coveraging: \e[1m" |
| 1486 |
<< *min_under |
| 1487 |
<< "\e[0m %. For details, check the file: worst_target_cell_undercover.info " << std::endl; |
| 1488 |
|
| 1489 |
if (rank_under == mpi::rank()) { |
| 1490 |
auto tcsp_id = worst_tgt_undercover.first; |
| 1491 |
auto tgt_csp = get_tgt_csp(tcsp_id); |
| 1492 |
dump_polygons_to_json(tgt_csp, pointsSameEPS, src_csp, tgt_iparam_[tcsp_id].csp_ids, polygon_intersection_folder, "worst_target_cell_undercover"); |
| 1493 |
} |
| 1494 |
} |
| 1495 |
} |
| 1496 |
} |
| 1497 |
|
| 1498 |
|
| 1499 |
ConservativeSphericalPolygonInterpolation::Triplets ConservativeSphericalPolygonInterpolation::compute_1st_order_triplets() { |
| 1500 |
ATLAS_TRACE("ConservativeMethod::setup: build cons-1 interpolant matrix"); |
| 1501 |
ATLAS_ASSERT(not matrix_free_); |
| 1502 |
const auto& tgt_iparam = data_->tgt_iparam_; |
| 1503 |
Triplets triplets; |
| 1504 |
size_t triplets_size = 0; |
| 1505 |
// determine the size of array of triplets used to define the sparse matrix |
| 1506 |
if (tgt_cell_data_) { |
| 1507 |
for (idx_t tcsp_id = 0; tcsp_id < data_->tgt_.csp_size; ++tcsp_id) { |
| 1508 |
triplets_size += tgt_iparam[tcsp_id].csp_ids.size(); |
| 1509 |
} |
| 1510 |
} |
| 1511 |
else { |
| 1512 |
auto& tgt_node2csp = data_->tgt_.node2csp; |
| 1513 |
for (idx_t tnode = 0; tnode < n_tpoints_; ++tnode) { |
| 1514 |
for (idx_t isubcell = 0; isubcell < tgt_node2csp[tnode].size(); ++isubcell) { |
| 1515 |
idx_t subcell = tgt_node2csp[tnode][isubcell]; |
| 1516 |
triplets_size += tgt_iparam[subcell].weights.size(); |
| 1517 |
} |
| 1518 |
} |
| 1519 |
} |
| 1520 |
triplets.reserve(triplets_size); |
| 1521 |
|
| 1522 |
TargetTriplets target_triplets(normalise_); |
| 1523 |
|
| 1524 |
struct ScopedDisableFPE { |
| 1525 |
ScopedDisableFPE() { |
| 1526 |
fpe_disabled = atlas::library::disable_floating_point_exception(FE_DIVBYZERO); |
| 1527 |
} |
| 1528 |
~ScopedDisableFPE() { |
| 1529 |
if (fpe_disabled) { |
| 1530 |
atlas::library::enable_floating_point_exception(FE_DIVBYZERO); |
| 1531 |
} |
| 1532 |
} |
| 1533 |
bool fpe_disabled; |
| 1534 |
} disable_fpe; |
| 1535 |
|
| 1536 |
// assemble triplets to define the sparse matrix |
| 1537 |
const auto& tgt_areas = data_->tgt_.areas; |
| 1538 |
|
| 1539 |
if (tgt_cell_data_ && src_cell_data_) { |
| 1540 |
for (idx_t tcsp_id = 0; tcsp_id < data_->tgt_.csp_size; ++tcsp_id) { |
| 1541 |
idx_t tcell = csp_to_cell(tcsp_id, data_->tgt_); |
| 1542 |
double inv_tgt_weight = (tgt_areas[tcell] > 0. ? 1. / tgt_areas[tcell] : 0.); |
| 1543 |
const auto& iparam = tgt_iparam[tcsp_id]; |
| 1544 |
target_triplets.reset(tcell); |
| 1545 |
for (idx_t i_scsp = 0; i_scsp < iparam.csp_ids.size(); ++i_scsp) { |
| 1546 |
idx_t scsp_id = iparam.csp_ids[i_scsp]; |
| 1547 |
idx_t scell = csp_to_cell(scsp_id, data_->src_); |
| 1548 |
double weight = iparam.weights[i_scsp] * inv_tgt_weight; |
| 1549 |
target_triplets.add(scell, weight); |
| 1550 |
} |
| 1551 |
target_triplets.emplace_in(triplets); |
| 1552 |
} |
| 1553 |
} |
| 1554 |
else if (not tgt_cell_data_ && src_cell_data_) { |
| 1555 |
auto& tgt_node2csp = data_->tgt_.node2csp; |
| 1556 |
for (idx_t tnode = 0; tnode < n_tpoints_; ++tnode) { |
| 1557 |
double inv_tgt_weight = (tgt_areas[tnode] > 0. ? 1. / tgt_areas[tnode] : 0.); |
| 1558 |
target_triplets.reset(tnode); |
| 1559 |
for (idx_t isubcell = 0; isubcell < tgt_node2csp[tnode].size(); ++isubcell) { |
| 1560 |
const idx_t subcell = tgt_node2csp[tnode][isubcell]; |
| 1561 |
const auto& iparam = tgt_iparam[subcell]; |
| 1562 |
for (idx_t i_scsp = 0; i_scsp < iparam.csp_ids.size(); ++i_scsp) { |
| 1563 |
idx_t scsp_id = iparam.csp_ids[i_scsp]; |
| 1564 |
idx_t scell = csp_to_cell(scsp_id, data_->src_); |
| 1565 |
ATLAS_ASSERT(scsp_id < n_spoints_); |
| 1566 |
volatile double weight = iparam.weights[i_scsp] * inv_tgt_weight; |
| 1567 |
target_triplets.add(scell, weight); |
| 1568 |
} |
| 1569 |
} |
| 1570 |
target_triplets.emplace_in(triplets); |
| 1571 |
} |
| 1572 |
} |
| 1573 |
else if (tgt_cell_data_ && not src_cell_data_) { |
| 1574 |
auto& src_csp2node = data_->src_.csp2node; |
| 1575 |
for (idx_t tcsp_id = 0; tcsp_id < data_->tgt_.csp_size; ++tcsp_id) { |
| 1576 |
idx_t tcell = csp_to_cell(tcsp_id, data_->tgt_); |
| 1577 |
double inv_tgt_weight = (tgt_areas[tcell] > 0. ? 1. / tgt_areas[tcell] : 0.); |
| 1578 |
const auto& iparam = tgt_iparam[tcsp_id]; |
| 1579 |
target_triplets.reset(tcell); |
| 1580 |
for (idx_t i_scsp = 0; i_scsp < iparam.csp_ids.size(); ++i_scsp) { |
| 1581 |
idx_t scsp_id = iparam.csp_ids[i_scsp]; |
| 1582 |
idx_t snode = src_csp2node[scsp_id]; |
| 1583 |
ATLAS_ASSERT(snode < n_spoints_); |
| 1584 |
double weight = iparam.weights[i_scsp] * inv_tgt_weight; |
| 1585 |
target_triplets.add(snode, weight); |
| 1586 |
} |
| 1587 |
target_triplets.emplace_in(triplets); |
| 1588 |
} |
| 1589 |
} |
| 1590 |
else if (not tgt_cell_data_ && not src_cell_data_) { |
| 1591 |
auto& tgt_node2csp = data_->tgt_.node2csp; |
| 1592 |
auto& src_csp2node = data_->src_.csp2node; |
| 1593 |
for (idx_t tnode = 0; tnode < n_tpoints_; ++tnode) { |
| 1594 |
double inv_tgt_weight = (tgt_areas[tnode] > 0. ? 1. / tgt_areas[tnode] : 0.); |
| 1595 |
target_triplets.reset(tnode); |
| 1596 |
for (const auto& tcsp_id: tgt_node2csp[tnode]) { |
| 1597 |
const auto& iparam = tgt_iparam[tcsp_id]; |
| 1598 |
for (idx_t i_scsp = 0; i_scsp < iparam.csp_ids.size(); ++i_scsp) { |
| 1599 |
idx_t scsp_id = iparam.csp_ids[i_scsp]; |
| 1600 |
idx_t snode = src_csp2node[scsp_id]; |
| 1601 |
ATLAS_ASSERT(snode < n_spoints_); |
| 1602 |
double weight = iparam.weights[i_scsp] * inv_tgt_weight; |
| 1603 |
target_triplets.add(snode, weight); |
| 1604 |
} |
| 1605 |
} |
| 1606 |
target_triplets.emplace_in(triplets); |
| 1607 |
} |
| 1608 |
} |
| 1609 |
if (validate_) { |
| 1610 |
ATLAS_TRACE("ConservativeMethod::setup: Validate the cons-1 matrix"); |
| 1611 |
std::vector<double> weight_sum(n_tpoints_); |
| 1612 |
for (auto& triplet : triplets) { |
| 1613 |
weight_sum[triplet.row()] += triplet.value(); |
| 1614 |
} |
| 1615 |
if (order_ == 1) { |
| 1616 |
// first order should not give overshoots |
| 1617 |
double eps = 1e4 * std::numeric_limits<double>::epsilon(); |
| 1618 |
for( auto& triplet : triplets ) { |
| 1619 |
if (triplet.value() > 1. + eps or triplet.value() < -eps) { |
| 1620 |
Log::info() << "target point " << triplet.row() << " weight: " << triplet.value() << std::endl; |
| 1621 |
} |
| 1622 |
} |
| 1623 |
} |
| 1624 |
auto ghost = array::make_view<int,1>( data_->tgt_.cell_data ? tgt_mesh_.cells().halo() : tgt_mesh_.nodes().ghost() ); |
| 1625 |
for (size_t row = 0; row < n_tpoints_; ++row) { |
| 1626 |
if (ghost(row)) { |
| 1627 |
continue; |
| 1628 |
} |
| 1629 |
if (std::abs(weight_sum[row] - 1.) > 1e-10) { |
| 1630 |
Log::info() << "target weight in row " << row << " differs from 1 by " << std::abs(weight_sum[row] - 1.) << std::endl; |
| 1631 |
} |
| 1632 |
} |
| 1633 |
} |
| 1634 |
return triplets; |
| 1635 |
} |
| 1636 |
|
| 1637 |
|
| 1638 |
ConservativeSphericalPolygonInterpolation::Triplets ConservativeSphericalPolygonInterpolation::compute_2nd_order_triplets() { |
| 1639 |
ATLAS_TRACE("ConservativeMethod: build cons-2 interpolant matrix"); |
| 1640 |
Triplets triplets; |
| 1641 |
ATLAS_ASSERT(not matrix_free_); |
| 1642 |
const auto& src = data_->src_; |
| 1643 |
const auto& tgt = data_->tgt_; |
| 1644 |
const auto& src_points = src.points; |
| 1645 |
const auto& tgt_iparam_ = data_->tgt_iparam_; |
| 1646 |
size_t triplets_size = 0; |
| 1647 |
const auto& tgt_areas = data_->tgt_.areas; |
| 1648 |
|
| 1649 |
// struct tripple{idx_t row; idx_t column; double value; }; |
| 1650 |
// std::vector<tripple> triplets; |
| 1651 |
|
| 1652 |
struct ScopedDisableFPE { |
| 1653 |
ScopedDisableFPE() { |
| 1654 |
fpe_disabled = atlas::library::disable_floating_point_exception(FE_DIVBYZERO); |
| 1655 |
} |
| 1656 |
~ScopedDisableFPE() { |
| 1657 |
if (fpe_disabled) { |
| 1658 |
atlas::library::enable_floating_point_exception(FE_DIVBYZERO); |
| 1659 |
} |
| 1660 |
} |
| 1661 |
bool fpe_disabled; |
| 1662 |
} disable_fpe; |
| 1663 |
|
| 1664 |
|
| 1665 |
Workspace_get_cell_neighbours w_cell; |
| 1666 |
Workspace_get_node_neighbours w_node; |
| 1667 |
TargetTriplets target_triplets(normalise_); |
| 1668 |
|
| 1669 |
if (tgt_cell_data_) { |
| 1670 |
const auto tgt_halo = array::make_view<int, 1>(tgt_mesh_.cells().halo()); |
| 1671 |
|
| 1672 |
for (idx_t tcsp_id = 0; tcsp_id < data_->tgt_.csp_size; ++tcsp_id) { |
| 1673 |
const auto& iparam = tgt_iparam_[tcsp_id]; |
| 1674 |
idx_t tcell = csp_to_cell(tcsp_id, tgt); |
| 1675 |
if (iparam.csp_ids.size() == 0 || tgt_halo(tcell)) { |
| 1676 |
continue; |
| 1677 |
} |
| 1678 |
if (src_cell_data_) { |
| 1679 |
for (idx_t i_scsp = 0; i_scsp < iparam.csp_ids.size(); ++i_scsp) { |
| 1680 |
const idx_t scell = csp_to_cell(iparam.csp_ids[i_scsp], src); |
| 1681 |
const auto src_neighbours = get_cell_neighbours(src_mesh_, scell, w_cell); |
| 1682 |
triplets_size += 2 * src_neighbours.size() + 1; |
| 1683 |
} |
| 1684 |
} |
| 1685 |
else { |
| 1686 |
for (idx_t i_scsp = 0; i_scsp < iparam.csp_ids.size(); ++i_scsp) { |
| 1687 |
const idx_t scsp_id = iparam.csp_ids[i_scsp]; |
| 1688 |
const idx_t snode = src.csp2node[scsp_id]; |
| 1689 |
const auto src_neighbours = get_node_neighbours(src_mesh_, snode, w_node); |
| 1690 |
triplets_size += 2 * src_neighbours.size() + 1; |
| 1691 |
} |
| 1692 |
} |
| 1693 |
} |
| 1694 |
triplets.reserve(triplets_size); |
| 1695 |
std::vector<PointXYZ> Rsj; |
| 1696 |
std::vector<PointXYZ> Aik; |
| 1697 |
for (idx_t tcsp_id = 0; tcsp_id < tgt.csp_size; ++tcsp_id) { |
| 1698 |
idx_t tcell = csp_to_cell(tcsp_id, tgt); |
| 1699 |
const auto& iparam = tgt_iparam_[tcsp_id]; |
| 1700 |
if (iparam.csp_ids.size() == 0 || tgt_halo(tcell)) { |
| 1701 |
continue; |
| 1702 |
} |
| 1703 |
target_triplets.reset(tcell); |
| 1704 |
// better conservation after Kritsikis et al. (2017) |
| 1705 |
// NOTE: ommited here at cost of conservation due to more involved implementation in parallel |
| 1706 |
// TEMP: use barycentre computed based on the source polygon's vertices, rather then |
| 1707 |
// the numerical barycentre based on barycentres of the intersections with target cells. |
| 1708 |
// TODO: for a given source cell, collect the centroids of all its intersections with target cells |
| 1709 |
// to compute the numerical barycentre of the cell bases on intersection. |
| 1710 |
// PointXYZ Cs = {0., 0., 0.}; |
| 1711 |
// for ( idx_t icell = 0; icell < iparam.csp_ids.size(); ++icell ) { |
| 1712 |
// Cs = Cs + PointXYZ::mul( iparam.centroids[icell], iparam.weights[icell] ); |
| 1713 |
// } |
| 1714 |
double tcell_area_inv = ( tgt_areas[tcell] > 0.) ? 1. / tgt_areas[tcell] : 0.; |
| 1715 |
|
| 1716 |
Aik.resize(iparam.csp_ids.size()); |
| 1717 |
for (idx_t i_scsp = 0; i_scsp < iparam.csp_ids.size(); ++i_scsp) { |
| 1718 |
const PointXYZ& Csk = iparam.centroids[i_scsp]; |
| 1719 |
const idx_t scsp_id = iparam.csp_ids[i_scsp]; |
| 1720 |
const idx_t spt = src_cell_data_ ? csp_to_cell(scsp_id, src) : src.csp2node[scsp_id]; |
| 1721 |
const PointXYZ& Cs = src_points[spt]; // !!! this is NOT barycentre of the subcell in case of NodeColumns |
| 1722 |
Aik[i_scsp] = Csk - Cs - PointXYZ::mul(Cs, PointXYZ::dot(Cs, Csk - Cs)); |
| 1723 |
std::vector<idx_t> src_neighbours; |
| 1724 |
if (src_cell_data_) { |
| 1725 |
idx_t scell = spt; |
| 1726 |
src_neighbours = get_cell_neighbours(src_mesh_, scell, w_cell); |
| 1727 |
} |
| 1728 |
else { |
| 1729 |
idx_t snode = spt; |
| 1730 |
src_neighbours = get_node_neighbours(src_mesh_, snode, w_node); |
| 1731 |
} |
| 1732 |
Rsj.resize(src_neighbours.size()); |
| 1733 |
PointXYZ Rs = {0., 0., 0.}; |
| 1734 |
double dual_area_inv = 0.; |
| 1735 |
for (idx_t j = 0; j < src_neighbours.size(); ++j) { |
| 1736 |
idx_t sj = src_neighbours[j]; |
| 1737 |
idx_t nsj = src_neighbours[ next_index(j, src_neighbours.size()) ]; |
| 1738 |
const auto& Csj = src_points[sj]; |
| 1739 |
const auto& Cnsj = src_points[nsj]; |
| 1740 |
if (Polygon::GreatCircleSegment(Cs, Csj).inLeftHemisphere(Cnsj, -1e-16)) { |
| 1741 |
Rsj[j] = PointXYZ::cross(Cnsj, Csj); |
| 1742 |
dual_area_inv += Polygon({Cs, Csj, Cnsj}).area(); |
| 1743 |
} |
| 1744 |
else { |
| 1745 |
Rsj[j] = PointXYZ::cross(Csj, Cnsj); |
| 1746 |
dual_area_inv += Polygon({Cs, Cnsj, Csj}).area(); |
| 1747 |
} |
| 1748 |
Rs = Rs + Rsj[j]; |
| 1749 |
} |
| 1750 |
dual_area_inv = (dual_area_inv > 0.) ? 1. / dual_area_inv : 0.; |
| 1751 |
Aik[i_scsp] = PointXYZ::mul(Aik[i_scsp], iparam.weights[i_scsp] * tcell_area_inv * dual_area_inv); |
| 1752 |
|
| 1753 |
if (src_cell_data_) { |
| 1754 |
for (idx_t j = 0; j < src_neighbours.size(); ++j) { |
| 1755 |
idx_t nj = next_index(j, src_neighbours.size()); |
| 1756 |
idx_t sj = src_neighbours[j]; |
| 1757 |
idx_t nsj = src_neighbours[nj]; |
| 1758 |
double w = 0.5 * PointXYZ::dot(Rsj[j], Aik[i_scsp]); |
| 1759 |
target_triplets.add(sj, w); |
| 1760 |
target_triplets.add(nsj, w); |
| 1761 |
} |
| 1762 |
idx_t scell = spt; |
| 1763 |
target_triplets.add(scell, iparam.weights[i_scsp] * tcell_area_inv - PointXYZ::dot(Rs, Aik[i_scsp])); |
| 1764 |
} |
| 1765 |
else { |
| 1766 |
for (idx_t j = 0; j < src_neighbours.size(); ++j) { |
| 1767 |
idx_t nj = next_index(j, src_neighbours.size()); |
| 1768 |
idx_t sj = src_neighbours[j]; |
| 1769 |
idx_t nsj = src_neighbours[nj]; |
| 1770 |
double w = 0.5 * PointXYZ::dot(Rsj[j], Aik[i_scsp]); |
| 1771 |
target_triplets.add(sj, w); |
| 1772 |
target_triplets.add(nsj, w); |
| 1773 |
} |
| 1774 |
idx_t snode = spt; |
| 1775 |
target_triplets.add(snode, iparam.weights[i_scsp] * tcell_area_inv - PointXYZ::dot(Rs, Aik[i_scsp])); |
| 1776 |
} |
| 1777 |
} |
| 1778 |
target_triplets.emplace_in(triplets); |
| 1779 |
} |
| 1780 |
} |
| 1781 |
else { // if ( not tgt_cell_data_ ) |
| 1782 |
const auto tgt_ghost = array::make_view<int, 1>(tgt_mesh_.nodes().ghost()); |
| 1783 |
auto& tgt_node2csp = tgt.node2csp; |
| 1784 |
triplets_size = 0; |
| 1785 |
for (idx_t tnode = 0; tnode < n_tpoints_; ++tnode) { |
| 1786 |
if (tgt_ghost(tnode)) { |
| 1787 |
continue; |
| 1788 |
} |
| 1789 |
for (idx_t i_tcsp = 0; i_tcsp < tgt_node2csp[tnode].size(); ++i_tcsp) { |
| 1790 |
const idx_t tcsp_id = tgt_node2csp[tnode][i_tcsp]; |
| 1791 |
const auto& iparam = tgt_iparam_[tcsp_id]; |
| 1792 |
if (iparam.csp_ids.size() == 0) { |
| 1793 |
continue; |
| 1794 |
} |
| 1795 |
if (src_cell_data_) { |
| 1796 |
for (idx_t i_scsp = 0; i_scsp < iparam.csp_ids.size(); ++i_scsp) { |
| 1797 |
const idx_t scsp_id = iparam.csp_ids[i_scsp]; |
| 1798 |
// const idx_t scell = csp_to_cell(scsp_id, data_->src_); |
| 1799 |
const idx_t scell = scsp_id; |
| 1800 |
const auto src_neighbours = get_cell_neighbours(src_mesh_, scell, w_cell); |
| 1801 |
triplets_size += 2 * src_neighbours.size() + 1; |
| 1802 |
} |
| 1803 |
} |
| 1804 |
else { |
| 1805 |
for (idx_t i_scsp = 0; i_scsp < iparam.csp_ids.size(); ++i_scsp) { |
| 1806 |
const idx_t scsp_id = iparam.csp_ids[i_scsp]; |
| 1807 |
const idx_t snode = src.csp2node[scsp_id]; |
| 1808 |
const auto src_neighbours = get_node_neighbours(src_mesh_, snode, w_node); |
| 1809 |
triplets_size += 2 * src_neighbours.size() + 1; |
| 1810 |
} |
| 1811 |
} |
| 1812 |
} |
| 1813 |
} |
| 1814 |
|
| 1815 |
triplets.reserve(triplets_size); |
| 1816 |
std::vector<PointXYZ> Rsj; |
| 1817 |
std::vector<PointXYZ> Aik; |
| 1818 |
for (idx_t tnode = 0; tnode < n_tpoints_; ++tnode) { |
| 1819 |
if (tgt_ghost(tnode)) { |
| 1820 |
continue; |
| 1821 |
} |
| 1822 |
target_triplets.reset(tnode); |
| 1823 |
for (idx_t i_tcsp = 0; i_tcsp < tgt_node2csp[tnode].size(); ++i_tcsp) { |
| 1824 |
const idx_t tcsp_id = tgt_node2csp[tnode][i_tcsp]; |
| 1825 |
const auto& iparam = tgt_iparam_[tcsp_id]; |
| 1826 |
if (iparam.csp_ids.size() == 0) { |
| 1827 |
continue; |
| 1828 |
} |
| 1829 |
auto tnode_area_inv = ( tgt_areas[tnode] > 0.) ? 1. / tgt_areas[tnode] : 0.; |
| 1830 |
|
| 1831 |
// get the barycentre of the dual cell |
| 1832 |
// better conservation |
| 1833 |
// PointXYZ Cs = {0., 0., 0.}; |
| 1834 |
// for ( idx_t isubcell = 0; isubcell < src_.node2csp[snode].size(); ++isubcell ) { |
| 1835 |
// idx_t subcell = src_.node2csp[snode][isubcell]; |
| 1836 |
// const auto& iparam = src_iparam_[subcell]; |
| 1837 |
// for ( idx_t icell = 0; icell < iparam.csp_ids.size(); ++icell ) { |
| 1838 |
// Cs = Cs + PointXYZ::mul( iparam.centroids[icell], iparam.weights[icell] ); |
| 1839 |
// } |
| 1840 |
// const double Cs_norm = PointXYZ::norm( Cs ); |
| 1841 |
// ATLAS_ASSERT( Cs_norm > 0. ); |
| 1842 |
// Cs = PointXYZ::div( Cs, Cs_norm ); |
| 1843 |
// } |
| 1844 |
Aik.resize(iparam.csp_ids.size()); |
| 1845 |
for (idx_t i_scsp = 0; i_scsp < iparam.csp_ids.size(); ++i_scsp) { |
| 1846 |
const PointXYZ& Csk = iparam.centroids[i_scsp]; |
| 1847 |
const idx_t scsp_id = iparam.csp_ids[i_scsp]; |
| 1848 |
const idx_t spt = src_cell_data_ ? csp_to_cell(scsp_id, src) : src.csp2node[scsp_id]; |
| 1849 |
const PointXYZ& Cs = src_points[spt]; // !!! this is NOT barycentre of the subcell in case of NodeColumns |
| 1850 |
Aik[i_scsp] = Csk - Cs - PointXYZ::mul(Cs, PointXYZ::dot(Cs, Csk - Cs)); |
| 1851 |
double dual_area_inv = 0.; |
| 1852 |
std::vector<idx_t> src_neighbours; |
| 1853 |
if (src_cell_data_) { |
| 1854 |
idx_t scell = spt; |
| 1855 |
src_neighbours = get_cell_neighbours(src_mesh_, scell, w_cell); |
| 1856 |
} |
| 1857 |
else { |
| 1858 |
idx_t snode = spt; |
| 1859 |
src_neighbours = get_node_neighbours(src_mesh_, snode, w_node); |
| 1860 |
} |
| 1861 |
Rsj.resize(src_neighbours.size()); |
| 1862 |
PointXYZ Rs = {0., 0., 0.}; |
| 1863 |
for (idx_t j = 0; j < src_neighbours.size(); ++j) { |
| 1864 |
idx_t sj = src_neighbours[j]; |
| 1865 |
idx_t nsj = src_neighbours[ next_index(j, src_neighbours.size()) ]; |
| 1866 |
const auto& Csj = src_points[sj]; |
| 1867 |
const auto& Cnsj = src_points[nsj]; |
| 1868 |
if (Polygon::GreatCircleSegment(Cs, Csj).inLeftHemisphere(Cnsj, -1e-16)) { |
| 1869 |
Rsj[j] = PointXYZ::cross(Cnsj, Csj); |
| 1870 |
dual_area_inv += Polygon({Cs, Csj, Cnsj}).area(); |
| 1871 |
} |
| 1872 |
else { |
| 1873 |
Rsj[j] = PointXYZ::cross(Csj, Cnsj); |
| 1874 |
dual_area_inv += Polygon({Cs, Cnsj, Csj}).area(); |
| 1875 |
} |
| 1876 |
Rs = Rs + Rsj[j]; |
| 1877 |
} |
| 1878 |
dual_area_inv = (dual_area_inv > 0.) ? 1. / dual_area_inv : 0.; |
| 1879 |
Aik[i_scsp] = PointXYZ::mul(Aik[i_scsp], iparam.weights[i_scsp] * tnode_area_inv * dual_area_inv); |
| 1880 |
|
| 1881 |
if (src_cell_data_) { |
| 1882 |
for (idx_t j = 0; j < src_neighbours.size(); ++j) { |
| 1883 |
idx_t nj = next_index(j, src_neighbours.size()); |
| 1884 |
idx_t sj = src_neighbours[j]; |
| 1885 |
idx_t nsj = src_neighbours[nj]; |
| 1886 |
double w = 0.5 * PointXYZ::dot(Rsj[j], Aik[i_scsp]); |
| 1887 |
target_triplets.add(sj, w); |
| 1888 |
target_triplets.add(nsj, w); |
| 1889 |
} |
| 1890 |
idx_t scell = spt; |
| 1891 |
target_triplets.add(scell, iparam.weights[i_scsp] * tnode_area_inv - PointXYZ::dot(Rs, Aik[i_scsp])); |
| 1892 |
} |
| 1893 |
else { |
| 1894 |
for (idx_t j = 0; j < src_neighbours.size(); ++j) { |
| 1895 |
idx_t nj = next_index(j, src_neighbours.size()); |
| 1896 |
idx_t sj = src_neighbours[j]; |
| 1897 |
idx_t nsj = src_neighbours[nj]; |
| 1898 |
double w = 0.5 * PointXYZ::dot(Rsj[j], Aik[i_scsp]); |
| 1899 |
target_triplets.add(sj, w); |
| 1900 |
target_triplets.add(nsj, w); |
| 1901 |
} |
| 1902 |
idx_t snode = spt; |
| 1903 |
target_triplets.add(snode, iparam.weights[i_scsp] * tnode_area_inv - PointXYZ::dot(Rs, Aik[i_scsp])); |
| 1904 |
} |
| 1905 |
} |
| 1906 |
} |
| 1907 |
target_triplets.emplace_in(triplets); |
| 1908 |
} |
| 1909 |
} |
| 1910 |
return triplets; |
| 1911 |
} |
| 1912 |
|
| 1913 |
|
| 1914 |
void ConservativeSphericalPolygonInterpolation::do_execute(const FieldSet& src_fields, FieldSet& tgt_fields, |
| 1915 |
Metadata& metadata) const { |
| 1916 |
std::vector<Metadata> md_array; |
| 1917 |
md_array.resize( src_fields.size() ); |
| 1918 |
for (int i = 0; i < src_fields.size(); i++) { // TODO: memory-wise should we openmp this? |
| 1919 |
do_execute(src_fields[i], tgt_fields[i], md_array[i]); |
| 1920 |
} |
| 1921 |
metadata = md_array[0]; // TODO: reduce metadata of a set of variables to a single metadata of a variable? |
| 1922 |
} |
| 1923 |
|
| 1924 |
|
| 1925 |
void ConservativeSphericalPolygonInterpolation::do_execute(const Field& src_field, Field& tgt_field, |
| 1926 |
Metadata& metadata) const { |
| 1927 |
ATLAS_TRACE("ConservativeMethod: do_execute"); |
| 1928 |
{ |
| 1929 |
if (src_field.dirty()) { |
| 1930 |
ATLAS_TRACE("halo exchange source"); |
| 1931 |
src_field.haloExchange(); |
| 1932 |
} |
| 1933 |
} |
| 1934 |
StopWatch stopwatch; |
| 1935 |
stopwatch.start(); |
| 1936 |
if (order_ == 1) { |
| 1937 |
if (matrix_free_) { |
| 1938 |
ATLAS_TRACE("matrix_free_order_1"); |
| 1939 |
const auto src_vals = array::make_view<double, 1>(src_field); |
| 1940 |
auto tgt_vals = array::make_view<double, 1>(tgt_field); |
| 1941 |
const auto& tgt_iparam = data_->tgt_iparam_; |
| 1942 |
const auto& tgt_areas = data_->tgt_.areas; |
| 1943 |
|
| 1944 |
// CASE: CELL TO CELL |
| 1945 |
if (tgt_cell_data_ && src_cell_data_) { |
| 1946 |
for (idx_t tcsp_id = 0; tcsp_id < data_->tgt_.csp_size; ++tcsp_id) { |
| 1947 |
idx_t tcell = csp_to_cell(tcsp_id, data_->tgt_); |
| 1948 |
double tgt_val = 0.; |
| 1949 |
const auto& iparam = tgt_iparam[tcsp_id]; |
| 1950 |
for (idx_t i_scsp = 0; i_scsp < iparam.csp_ids.size(); ++i_scsp) { |
| 1951 |
idx_t scsp_id = iparam.csp_ids[i_scsp]; |
| 1952 |
idx_t scell = csp_to_cell(scsp_id, data_->src_); |
| 1953 |
tgt_val += iparam.weights[i_scsp] * src_vals(scell); |
| 1954 |
} |
| 1955 |
if (tgt_areas[tcell] > 0.) { |
| 1956 |
tgt_val /= tgt_areas[tcell]; |
| 1957 |
} |
| 1958 |
tgt_vals(tcell) = tgt_val; |
| 1959 |
} |
| 1960 |
} |
| 1961 |
|
| 1962 |
// CASE: NODE TO CELL |
| 1963 |
else if (not tgt_cell_data_ && src_cell_data_) { |
| 1964 |
auto& tgt_node2csp = data_->tgt_.node2csp; |
| 1965 |
for (idx_t tnode = 0; tnode < n_tpoints_; ++tnode) { |
| 1966 |
double tgt_val = 0.; |
| 1967 |
for( const auto& tcsp_id: tgt_node2csp[tnode]) { |
| 1968 |
const auto& iparam = tgt_iparam[tcsp_id]; |
| 1969 |
for (idx_t i_scsp = 0; i_scsp < iparam.csp_ids.size(); ++i_scsp) { |
| 1970 |
idx_t scsp_id = iparam.csp_ids[i_scsp]; |
| 1971 |
idx_t scell = csp_to_cell(scsp_id, data_->src_); |
| 1972 |
tgt_val += iparam.weights[i_scsp] * src_vals(scell); |
| 1973 |
} |
| 1974 |
} |
| 1975 |
if (tgt_areas[tnode] > 0.) { |
| 1976 |
tgt_val /= tgt_areas[tnode]; |
| 1977 |
} |
| 1978 |
tgt_vals(tnode) = tgt_val; |
| 1979 |
} |
| 1980 |
} |
| 1981 |
|
| 1982 |
// CASE: CELL TO NODE |
| 1983 |
else if (tgt_cell_data_ && not src_cell_data_) { |
| 1984 |
const auto& src_csp2node = data_->src_.csp2node; |
| 1985 |
for (idx_t tcsp_id = 0; tcsp_id < data_->tgt_.csp_size; ++tcsp_id) { |
| 1986 |
idx_t tcell = csp_to_cell(tcsp_id, data_->tgt_); |
| 1987 |
double tgt_val = 0.; |
| 1988 |
const auto& iparam = tgt_iparam[tcsp_id]; |
| 1989 |
for (idx_t i_scsp = 0; i_scsp < iparam.csp_ids.size(); ++i_scsp) { |
| 1990 |
idx_t scsp_id = iparam.csp_ids[i_scsp]; |
| 1991 |
idx_t snode = src_csp2node[scsp_id]; |
| 1992 |
tgt_val += iparam.weights[i_scsp] * src_vals(snode); |
| 1993 |
} |
| 1994 |
if (tgt_areas[tcell] > 0.) { |
| 1995 |
tgt_val /= tgt_areas[tcell]; |
| 1996 |
} |
| 1997 |
tgt_vals(tcell) = tgt_val; |
| 1998 |
} |
| 1999 |
} |
| 2000 |
|
| 2001 |
// CASE: NODE TO NODE |
| 2002 |
else if (not tgt_cell_data_ && not src_cell_data_) { |
| 2003 |
const auto& tgt_node2csp = data_->tgt_.node2csp; |
| 2004 |
const auto& src_csp2node = data_->src_.csp2node; |
| 2005 |
for (idx_t tnode = 0; tnode < n_tpoints_; ++tnode) { |
| 2006 |
double tgt_val = 0.; |
| 2007 |
for( const auto& tcsp_id: tgt_node2csp[tnode]) { |
| 2008 |
const auto& iparam = tgt_iparam[tcsp_id]; |
| 2009 |
for (idx_t i_scsp = 0; i_scsp < iparam.csp_ids.size(); ++i_scsp) { |
| 2010 |
idx_t scsp_id = iparam.csp_ids[i_scsp]; |
| 2011 |
idx_t snode = src_csp2node[scsp_id]; |
| 2012 |
tgt_val += iparam.weights[i_scsp] * src_vals(snode); |
| 2013 |
} |
| 2014 |
} |
| 2015 |
if (tgt_areas[tnode] > 0.) { |
| 2016 |
tgt_val /= tgt_areas[tnode]; |
| 2017 |
} |
| 2018 |
tgt_vals(tnode) = tgt_val; |
| 2019 |
} |
| 2020 |
} |
| 2021 |
} |
| 2022 |
else { |
| 2023 |
ATLAS_TRACE("matrix_order_1"); |
| 2024 |
Method::do_execute(src_field, tgt_field, metadata); |
| 2025 |
} |
| 2026 |
} |
| 2027 |
else if (order_ == 2) { |
| 2028 |
if (matrix_free_) { |
| 2029 |
ATLAS_NOTIMPLEMENTED; |
| 2030 |
/* |
| 2031 |
if (not src_cell_data_ or not tgt_cell_data_) { |
| 2032 |
} |
| 2033 |
ATLAS_TRACE("matrix_free_order_2"); |
| 2034 |
const auto& src_iparam_ = data_->src_iparam_; |
| 2035 |
const auto& tgt_.areas = data_->tgt_.areas; |
| 2036 |
auto& src_.points = data_->src_.points; |
| 2037 |
const auto src_vals = array::make_view<double, 1>(src_field); |
| 2038 |
auto tgt_vals = array::make_view<double, 1>(tgt_field); |
| 2039 |
const auto halo = array::make_view<int, 1>(src_mesh_.cells().halo()); |
| 2040 |
for (idx_t tcell = 0; tcell < tgt_vals.size(); ++tcell) { |
| 2041 |
tgt_vals(tcell) = 0.; |
| 2042 |
} |
| 2043 |
for (idx_t scell = 0; scell < src_vals.size(); ++scell) { |
| 2044 |
const auto& iparam = src_iparam_[scell]; |
| 2045 |
if (iparam.csp_ids.size() == 0 or halo(scell)) { |
| 2046 |
continue; |
| 2047 |
} |
| 2048 |
const PointXYZ& Cs = src_.points[scell]; |
| 2049 |
PointXYZ grad = {0., 0., 0.}; |
| 2050 |
PointXYZ src_barycenter = {0., 0., 0.}; |
| 2051 |
auto nb_cells = get_cell_neighbours(src_mesh_, scell); |
| 2052 |
double dual_area_inv = 0.; |
| 2053 |
for (idx_t j = 0; j < nb_cells.size(); ++j) { |
| 2054 |
idx_t nj = next_index(j, nb_cells.size()); |
| 2055 |
idx_t sj = nb_cells[j]; |
| 2056 |
idx_t nsj = nb_cells[nj]; |
| 2057 |
const auto& Csj = src_.points[sj]; |
| 2058 |
const auto& Cnsj = src_.points[nsj]; |
| 2059 |
double val = 0.5 * (src_vals(nj) + src_vals(nsj)) - src_vals(j); |
| 2060 |
if (Polygon::GreatCircleSegment(Cs, Csj).inLeftHemisphere(Cnsj, -1e-16)) { |
| 2061 |
dual_area_inv += Polygon({Cs, Csj, Cnsj}).area(); |
| 2062 |
} |
| 2063 |
else { |
| 2064 |
//val *= -1; |
| 2065 |
dual_area_inv += Polygon({Cs, Cnsj, Csj}).area(); |
| 2066 |
} |
| 2067 |
grad = grad + PointXYZ::mul(PointXYZ::cross(Csj, Cnsj), val); |
| 2068 |
} |
| 2069 |
dual_area_inv = ((dual_area_inv > 0.) ? 1. / dual_area_inv : 0.); |
| 2070 |
grad = PointXYZ::mul(grad, dual_area_inv); |
| 2071 |
for (idx_t icell = 0; icell < iparam.csp_ids.size(); ++icell) { |
| 2072 |
src_barycenter = src_barycenter + PointXYZ::mul(iparam.centroids[icell], iparam.weights[icell]); |
| 2073 |
} |
| 2074 |
src_barycenter = PointXYZ::div(src_barycenter, PointXYZ::norm(src_barycenter)); |
| 2075 |
grad = grad - PointXYZ::mul(src_barycenter, PointXYZ::dot(grad, src_barycenter)); |
| 2076 |
ATLAS_ASSERT(std::abs(PointXYZ::dot(grad, src_barycenter)) < 1e-14); |
| 2077 |
for (idx_t icell = 0; icell < iparam.csp_ids.size(); ++icell) { |
| 2078 |
tgt_vals(iparam.csp_ids[icell]) += |
| 2079 |
iparam.weights[icell] * |
| 2080 |
(src_vals(scell) + PointXYZ::dot(grad, iparam.centroids[icell] - src_barycenter)); |
| 2081 |
} |
| 2082 |
} |
| 2083 |
for (idx_t tcell = 0; tcell < tgt_vals.size(); ++tcell) { |
| 2084 |
tgt_vals[tcell] /= tgt_.areas[tcell]; |
| 2085 |
} |
| 2086 |
*/ |
| 2087 |
} |
| 2088 |
else { |
| 2089 |
ATLAS_TRACE("matrix_order_2"); |
| 2090 |
Method::do_execute(src_field, tgt_field, metadata); |
| 2091 |
} |
| 2092 |
} |
| 2093 |
|
| 2094 |
stopwatch.stop(); |
| 2095 |
|
| 2096 |
if (remap_stat_.conservation) { |
| 2097 |
const auto src_cell_halo = array::make_view<int, 1>(src_mesh_.cells().halo()); |
| 2098 |
const auto src_node_ghost = array::make_view<int, 1>(src_mesh_.nodes().ghost()); |
| 2099 |
const auto src_node_halo = array::make_view<int, 1>(src_mesh_.nodes().halo()); |
| 2100 |
const auto tgt_cell_halo = array::make_view<int, 1>(tgt_mesh_.cells().halo()); |
| 2101 |
const auto tgt_node_ghost = array::make_view<int, 1>(tgt_mesh_.nodes().ghost()); |
| 2102 |
const auto tgt_node_halo = array::make_view<int, 1>(tgt_mesh_.nodes().halo()); |
| 2103 |
const auto& src_areas = data_->src_.areas; |
| 2104 |
const auto& tgt_areas = data_->tgt_.areas; |
| 2105 |
|
| 2106 |
const auto src_vals = array::make_view<double, 1>(src_field); |
| 2107 |
const auto tgt_vals = array::make_view<double, 1>(tgt_field); |
| 2108 |
|
| 2109 |
double err_remap_cons = 0.; |
| 2110 |
if (src_cell_data_) { |
| 2111 |
for (idx_t spt = 0; spt < src_vals.size(); ++spt) { |
| 2112 |
if (src_cell_halo(spt)) { |
| 2113 |
continue; |
| 2114 |
} |
| 2115 |
err_remap_cons += src_vals(spt) * src_areas[spt]; |
| 2116 |
} |
| 2117 |
} |
| 2118 |
else { |
| 2119 |
for (idx_t spt = 0; spt < src_vals.size(); ++spt) { |
| 2120 |
if (src_node_halo(spt) or src_node_ghost(spt)) { |
| 2121 |
continue; |
| 2122 |
} |
| 2123 |
err_remap_cons += src_vals(spt) * src_areas[spt]; |
| 2124 |
} |
| 2125 |
} |
| 2126 |
if (tgt_cell_data_) { |
| 2127 |
for (idx_t tpt = 0; tpt < tgt_vals.size(); ++tpt) { |
| 2128 |
if (tgt_cell_halo(tpt)) { |
| 2129 |
continue; |
| 2130 |
} |
| 2131 |
err_remap_cons -= tgt_vals(tpt) * tgt_areas[tpt]; |
| 2132 |
} |
| 2133 |
} |
| 2134 |
else { |
| 2135 |
for (idx_t tpt = 0; tpt < tgt_vals.size(); ++tpt) { |
| 2136 |
if (tgt_node_halo(tpt) or tgt_node_ghost(tpt)) { |
| 2137 |
continue; |
| 2138 |
} |
| 2139 |
err_remap_cons -= tgt_vals(tpt) * tgt_areas[tpt]; |
| 2140 |
} |
| 2141 |
} |
| 2142 |
ATLAS_TRACE_MPI(ALLREDUCE) { mpi::comm().allReduceInPlace(&err_remap_cons, 1, eckit::mpi::sum()); } |
| 2143 |
remap_stat_.errors[Statistics::ERR_REMAP_CONS] = err_remap_cons / unit_sphere_area(); |
| 2144 |
} |
| 2145 |
|
| 2146 |
if (remap_stat_.intersection) { |
| 2147 |
metadata.set("polygons.number_of_src_polygons", remap_stat_.counts[Statistics::NUM_SRC_PLG]); |
| 2148 |
metadata.set("polygons.number_of_tgt_polygons", remap_stat_.counts[Statistics::NUM_TGT_PLG]); |
| 2149 |
metadata.set("polygons.number_of_intersections", remap_stat_.counts[Statistics::NUM_INT_PLG]); |
| 2150 |
metadata.set("polygons.number_of_full_noncovered_tgt_polygons", remap_stat_.counts[Statistics::NUM_UNCVR_FULL_TGT]); |
| 2151 |
metadata.set("polygons.number_of_part_noncovered_tgt_polygons", remap_stat_.counts[Statistics::NUM_UNCVR_PART_TGT]); |
| 2152 |
if (validate_) { |
| 2153 |
metadata.set("errors.intersections_covering_tgt_cells_sum", remap_stat_.errors[Statistics::ERR_TGT_INTERSECTPLG_L1]); |
| 2154 |
metadata.set("errors.intersections_covering_tgt_cells_max", remap_stat_.errors[Statistics::ERR_TGT_INTERSECTPLG_LINF]); |
| 2155 |
} |
| 2156 |
} |
| 2157 |
|
| 2158 |
if (remap_stat_.intersection || remap_stat_.conservation) { |
| 2159 |
remap_stat_.fillMetadata(metadata); |
| 2160 |
} |
| 2161 |
|
| 2162 |
auto& timings = data_->timings; |
| 2163 |
remap_stat_.time[Statistics::TIME_SRC_PLG] = timings.source_polygons_assembly; |
| 2164 |
remap_stat_.time[Statistics::TIME_TGT_PLG] = timings.source_polygons_assembly; |
| 2165 |
remap_stat_.time[Statistics::TIME_KDTREE_BUILD] = timings.source_kdtree_assembly; |
| 2166 |
remap_stat_.time[Statistics::TIME_MATRIX] = timings.matrix_assembly; |
| 2167 |
remap_stat_.time[Statistics::TIME_INTERS] = timings.polygon_intersections; |
| 2168 |
if (remap_stat_.timings) { |
| 2169 |
remap_stat_.time[Statistics::TIME_KDTREE_SEARCH] = timings.source_kdtree_search; |
| 2170 |
metadata.set("max_timings_in_seconds_per_task.source_kdtree_search", timings.source_kdtree_search); |
| 2171 |
remap_stat_.time[Statistics::TIME_INTERP] = timings.polygon_intersections; |
| 2172 |
metadata.set("max_timings_in_seconds_per_task.polygon_intersections", timings.polygon_intersections); |
| 2173 |
} |
| 2174 |
ATLAS_TRACE_MPI(ALLREDUCE) { |
| 2175 |
mpi::comm().allReduceInPlace(remap_stat_.time.data(), remap_stat_.time.size(), eckit::mpi::max()); |
| 2176 |
}metadata.set("max_timings_in_seconds_per_task.source_polygons_assembly", timings.source_polygons_assembly); |
| 2177 |
metadata.set("max_timings_in_seconds_per_task.target_polygons_assembly", timings.target_polygons_assembly); |
| 2178 |
metadata.set("max_timings_in_seconds_per_task.source_kdtree_assembly", timings.source_kdtree_assembly); |
| 2179 |
metadata.set("max_timings_in_seconds_per_task.matrix_assembly", timings.matrix_assembly); |
| 2180 |
metadata.set("max_timings_in_seconds_per_task.interpolation", stopwatch.elapsed()); |
| 2181 |
|
| 2182 |
remap_stat_.memory[Statistics::MEM_MATRIX] = (matrix_free_ ? 0 : matrix().footprint()); |
| 2183 |
remap_stat_.memory[Statistics::MEM_SRC] = memory_of(data_->src_.points); |
| 2184 |
remap_stat_.memory[Statistics::MEM_TGT] = memory_of(data_->tgt_.points); |
| 2185 |
remap_stat_.memory[Statistics::MEM_SRC_AREAS] = memory_of(data_->src_.areas); |
| 2186 |
remap_stat_.memory[Statistics::MEM_TGT_AREAS] = memory_of(data_->tgt_.areas); |
| 2187 |
remap_stat_.memory[Statistics::MEM_SRC_CSP2N] = memory_of(data_->src_.csp2node); |
| 2188 |
remap_stat_.memory[Statistics::MEM_SRC_N2CSP] = memory_of(data_->src_.node2csp); |
| 2189 |
remap_stat_.memory[Statistics::MEM_SRC_CSP2CI] = memory_of(data_->src_.csp_cell_index); |
| 2190 |
remap_stat_.memory[Statistics::MEM_SRC_CSP2C] = memory_of(data_->src_.csp_index); |
| 2191 |
remap_stat_.memory[Statistics::MEM_TGT_CSP2N] = memory_of(data_->tgt_.csp2node); |
| 2192 |
remap_stat_.memory[Statistics::MEM_TGT_N2CSP] = memory_of(data_->tgt_.node2csp); |
| 2193 |
remap_stat_.memory[Statistics::MEM_TGT_CSP2CI] = memory_of(data_->tgt_.csp_cell_index); |
| 2194 |
remap_stat_.memory[Statistics::MEM_TGT_CSP2C] = memory_of(data_->tgt_.csp_index); |
| 2195 |
remap_stat_.memory[Statistics::MEM_IPARAM] = memory_of(data_->tgt_iparam_); |
| 2196 |
ATLAS_TRACE_MPI(ALLREDUCE) { |
| 2197 |
mpi::comm().allReduceInPlace(remap_stat_.memory.data(), remap_stat_.memory.size(), eckit::mpi::max()); |
| 2198 |
} |
| 2199 |
metadata.set("max_memory_in_bytes_per_task.matrix", remap_stat_.memory[Statistics::MEM_MATRIX]); |
| 2200 |
metadata.set("max_memory_in_bytes_per_task.src_points", remap_stat_.memory[Statistics::MEM_SRC]); |
| 2201 |
metadata.set("max_memory_in_bytes_per_task.tgt_points", remap_stat_.memory[Statistics::MEM_TGT]); |
| 2202 |
metadata.set("max_memory_in_bytes_per_task.src_areas", remap_stat_.memory[Statistics::MEM_SRC_AREAS]); |
| 2203 |
metadata.set("max_memory_in_bytes_per_task.tgt_areas", remap_stat_.memory[Statistics::MEM_TGT_AREAS]); |
| 2204 |
metadata.set("max_memory_in_bytes_per_task.src_csp2node", remap_stat_.memory[Statistics::MEM_SRC_CSP2N]); |
| 2205 |
metadata.set("max_memory_in_bytes_per_task.tgt_csp2node", remap_stat_.memory[Statistics::MEM_TGT_CSP2N]); |
| 2206 |
metadata.set("max_memory_in_bytes_per_task.src_node2csp", remap_stat_.memory[Statistics::MEM_SRC_N2CSP]); |
| 2207 |
metadata.set("max_memory_in_bytes_per_task.tgt_node2csp", remap_stat_.memory[Statistics::MEM_TGT_N2CSP]); |
| 2208 |
metadata.set("max_memory_in_bytes_per_task.tgt_iparam", remap_stat_.memory[Statistics::MEM_IPARAM]); |
| 2209 |
metadata.set("max_memory_in_bytes_per_task.src_csp_cell_index", remap_stat_.memory[Statistics::MEM_SRC_CSP2CI]); |
| 2210 |
metadata.set("max_memory_in_bytes_per_task.src_csp_index", remap_stat_.memory[Statistics::MEM_SRC_CSP2C]); |
| 2211 |
metadata.set("max_memory_in_bytes_per_task.tgt_csp_cell_index", remap_stat_.memory[Statistics::MEM_TGT_CSP2CI]); |
| 2212 |
metadata.set("max_memory_in_bytes_per_task.tgt_csp_index", remap_stat_.memory[Statistics::MEM_TGT_CSP2C]); |
| 2213 |
} |
| 2214 |
|
| 2215 |
|
| 2216 |
void ConservativeSphericalPolygonInterpolation::print(std::ostream& out) const { |
| 2217 |
out << "ConservativeMethod{"; |
| 2218 |
out << "order:" << order_; |
| 2219 |
int halo = 0; |
| 2220 |
src_mesh_.metadata().get("halo", halo); |
| 2221 |
out << ", source:" << (src_cell_data_ ? "cells(" : "nodes(") << src_mesh_.grid().name() << ",halo=" << halo << ")"; |
| 2222 |
tgt_mesh_.metadata().get("halo", halo); |
| 2223 |
out << ", target:" << (tgt_cell_data_ ? "cells(" : "nodes(") << tgt_mesh_.grid().name() << ",halo=" << halo << ")"; |
| 2224 |
out << ", normalise:" << normalise_; |
| 2225 |
out << ", matrix_free:" << matrix_free_; |
| 2226 |
out << ", statistics.intersection:" << remap_stat_.intersection; |
| 2227 |
out << ", statistics.conservation:" << remap_stat_.conservation; |
| 2228 |
out << ", cached_matrix:" << not(matrixAllocated() || matrix_free_); |
| 2229 |
out << ", cached_data:" << bool(sharable_data_.use_count() == 0); |
| 2230 |
size_t footprint{}; |
| 2231 |
if (not matrix_free_) { |
| 2232 |
footprint += matrix().footprint(); |
| 2233 |
} |
| 2234 |
footprint += data_->footprint(); |
| 2235 |
out << ", footprint:" << eckit::Bytes(footprint); |
| 2236 |
out << "}"; |
| 2237 |
} |
| 2238 |
|
| 2239 |
|
| 2240 |
Cache ConservativeSphericalPolygonInterpolation::createCache() const { |
| 2241 |
interpolation::Cache cache; |
| 2242 |
if (not matrix_free_) { |
| 2243 |
cache.add(Method::createCache()); |
| 2244 |
} |
| 2245 |
cache.add(cache_); |
| 2246 |
return cache; |
| 2247 |
} |
| 2248 |
|
| 2249 |
|
| 2250 |
void ConservativeSphericalPolygonInterpolation::setup_stat() const { |
| 2251 |
if (! remap_stat_.intersection) { |
| 2252 |
Log::warning() << "Please enable statistics.intersection." << std::endl; |
| 2253 |
return; |
| 2254 |
} |
| 2255 |
const auto src_cell_halo = array::make_view<int, 1>(src_mesh_.cells().halo()); |
| 2256 |
const auto src_node_ghost = array::make_view<int, 1>(src_mesh_.nodes().ghost()); |
| 2257 |
const auto& src_areas = data_->src_.areas; |
| 2258 |
const auto& tgt_areas = data_->tgt_.areas; |
| 2259 |
double geo_create_err = 0.; |
| 2260 |
double src_tgt_sums[2] = {0., 0.}; |
| 2261 |
if (src_cell_data_) { |
| 2262 |
for (idx_t spt = 0; spt < src_areas.size(); ++spt) { |
| 2263 |
if (not src_cell_halo(spt)) { |
| 2264 |
src_tgt_sums[0] += src_areas[spt]; |
| 2265 |
} |
| 2266 |
} |
| 2267 |
} |
| 2268 |
else { |
| 2269 |
for (idx_t spt = 0; spt < src_areas.size(); ++spt) { |
| 2270 |
if (not src_node_ghost(spt)) { |
| 2271 |
src_tgt_sums[0] += src_areas[spt]; |
| 2272 |
} |
| 2273 |
} |
| 2274 |
} |
| 2275 |
const auto tgt_cell_halo = array::make_view<int, 1>(tgt_mesh_.cells().halo()); |
| 2276 |
const auto tgt_node_ghost = array::make_view<int, 1>(tgt_mesh_.nodes().ghost()); |
| 2277 |
if (tgt_cell_data_) { |
| 2278 |
for (idx_t tpt = 0; tpt < tgt_areas.size(); ++tpt) { |
| 2279 |
if (not tgt_cell_halo(tpt)) { |
| 2280 |
src_tgt_sums[1] += tgt_areas[tpt]; |
| 2281 |
} |
| 2282 |
} |
| 2283 |
} |
| 2284 |
else { |
| 2285 |
for (idx_t tpt = 0; tpt < tgt_areas.size(); ++tpt) { |
| 2286 |
if (not tgt_node_ghost(tpt)) { |
| 2287 |
src_tgt_sums[1] += tgt_areas[tpt]; |
| 2288 |
} |
| 2289 |
} |
| 2290 |
} |
| 2291 |
ATLAS_TRACE_MPI(ALLREDUCE) { mpi::comm().allReduceInPlace(src_tgt_sums, 2, eckit::mpi::sum()); } |
| 2292 |
|
| 2293 |
remap_stat_.src_area_sum = src_tgt_sums[0]; |
| 2294 |
remap_stat_.tgt_area_sum = src_tgt_sums[1]; |
| 2295 |
geo_create_err = std::abs(src_tgt_sums[0] - src_tgt_sums[1]) / unit_sphere_area(); |
| 2296 |
remap_stat_.errors[Statistics::ERR_SRCTGT_INTERSECTPLG_DIFF] = geo_create_err; |
| 2297 |
} |
| 2298 |
|
| 2299 |
|
| 2300 |
void ConservativeSphericalPolygonInterpolation::Statistics:: |
| 2301 |
compute_accuracy(const Interpolation& interpolation, const Field target, std::function<double(const PointLonLat&)> func, Metadata* metadata) { |
| 2302 |
auto tgt_vals = array::make_view<double, 1>(target); |
| 2303 |
auto cachable_data_ = ConservativeSphericalPolygonInterpolation::Cache(interpolation).get(); |
| 2304 |
auto tgt_mesh_ = extract_mesh(cachable_data_->src_fs_); |
| 2305 |
auto tgt_cell_data_ = extract_mesh(cachable_data_->tgt_fs_); |
| 2306 |
const auto tgt_cell_halo = array::make_view<int, 1>(tgt_mesh_.cells().halo()); |
| 2307 |
const auto tgt_node_ghost = array::make_view<int, 1>(tgt_mesh_.nodes().ghost()); |
| 2308 |
const auto tgt_node_halo = array::make_view<int, 1>(tgt_mesh_.nodes().halo()); |
| 2309 |
const auto& tgt_areas = cachable_data_->tgt_.areas; |
| 2310 |
auto& tgt_points = cachable_data_->tgt_.points; |
| 2311 |
double err_remap_l2 = 0.; |
| 2312 |
double err_remap_linf = 0.; |
| 2313 |
if (tgt_cell_data_) { |
| 2314 |
size_t ncells = std::min<size_t>(tgt_vals.size(), tgt_mesh_.cells().size()); |
| 2315 |
for (idx_t tpt = 0; tpt < ncells; ++tpt) { |
| 2316 |
ATLAS_ASSERT(tpt < tgt_cell_halo.size()); |
| 2317 |
if (tgt_cell_halo(tpt)) { |
| 2318 |
continue; |
| 2319 |
} |
| 2320 |
auto p = tgt_points[tpt]; |
| 2321 |
PointLonLat pll; |
| 2322 |
eckit::geometry::Sphere::convertCartesianToSpherical(1., p, pll); |
| 2323 |
double err_l = std::abs(tgt_vals(tpt) - func(pll)); |
| 2324 |
err_remap_l2 += err_l * err_l * tgt_areas[tpt]; |
| 2325 |
err_remap_linf = std::max(err_remap_linf, err_l); |
| 2326 |
} |
| 2327 |
} |
| 2328 |
else { |
| 2329 |
size_t nnodes = std::min<size_t>(tgt_vals.size(), tgt_mesh_.nodes().size()); |
| 2330 |
for (idx_t tpt = 0; tpt < nnodes; ++tpt) { |
| 2331 |
if (tgt_node_ghost(tpt) or tgt_node_halo(tpt)) { |
| 2332 |
continue; |
| 2333 |
} |
| 2334 |
auto p = tgt_points[tpt]; |
| 2335 |
PointLonLat pll; |
| 2336 |
eckit::geometry::Sphere::convertCartesianToSpherical(1., p, pll); |
| 2337 |
double err_l = std::abs(tgt_vals(tpt) - func(pll)); |
| 2338 |
err_remap_l2 += err_l * err_l * tgt_areas[tpt]; |
| 2339 |
err_remap_linf = std::max(err_remap_linf, err_l); |
| 2340 |
} |
| 2341 |
} |
| 2342 |
ATLAS_TRACE_MPI(ALLREDUCE) { |
| 2343 |
mpi::comm().allReduceInPlace(&err_remap_l2, 1, eckit::mpi::sum()); |
| 2344 |
mpi::comm().allReduceInPlace(&err_remap_linf, 1, eckit::mpi::max()); |
| 2345 |
} |
| 2346 |
errors[Statistics::ERR_REMAP_L2] = std::sqrt(err_remap_l2 / unit_sphere_area()); |
| 2347 |
errors[Statistics::ERR_REMAP_LINF] = err_remap_linf; |
| 2348 |
if (metadata) { |
| 2349 |
metadata->set("errors.to_solution_sum", errors[Statistics::ERR_REMAP_L2]); |
| 2350 |
metadata->set("errors.to_solution_max", errors[Statistics::ERR_REMAP_LINF]); |
| 2351 |
} |
| 2352 |
} |
| 2353 |
|
| 2354 |
|
| 2355 |
void ConservativeSphericalPolygonInterpolation::dump_intersection(const std::string msg, |
| 2356 |
const Polygon& plg_1, |
| 2357 |
const PolygonArray& plg_2_array, |
| 2358 |
const std::vector<idx_t>& plg_2_idx_array) const { |
| 2359 |
#if PRINT_BAD_POLYGONS |
| 2360 |
double plg_1_coverage = 0.; |
| 2361 |
std::vector<double> int_areas; |
| 2362 |
int_areas.resize(plg_2_idx_array.size()); |
| 2363 |
for (int i = 0; i < plg_2_idx_array.size(); ++i) { |
| 2364 |
const auto plg_2_idx = plg_2_idx_array[i]; |
| 2365 |
const auto& plg_2 = plg_2_array[plg_2_idx].polygon; |
| 2366 |
auto iplg = plg_1.intersect(plg_2, false, 1.e5 * std::numeric_limits<double>::epsilon()); |
| 2367 |
plg_1_coverage += iplg.area(); |
| 2368 |
int_areas[i] = iplg.area(); |
| 2369 |
} |
| 2370 |
if (std::abs(plg_1.area() - plg_1_coverage) > 0.01 * plg_1.area()) { |
| 2371 |
Log::info().indent(); |
| 2372 |
Log::info() << msg << ", Polygon_1.area: " << plg_1.area() << ", covered: " << plg_1_coverage << std::endl; |
| 2373 |
Log::info() << "Polygon_1 : "; |
| 2374 |
Log::info().precision(18); |
| 2375 |
plg_1.print(Log::info()); |
| 2376 |
Log::info() << std::endl << "Printing " << plg_2_idx_array.size() << " covering polygons -->" << std::endl; |
| 2377 |
Log::info().indent(); |
| 2378 |
plg_1_coverage = plg_1.area(); |
| 2379 |
for (int i = 0; i < plg_2_idx_array.size(); ++i) { |
| 2380 |
const auto plg_2_idx = plg_2_idx_array[i]; |
| 2381 |
const auto& plg_2 = plg_2_array[plg_2_idx].polygon; |
| 2382 |
plg_2.print(Log::info()); |
| 2383 |
Log::info() << "\ninters:"; |
| 2384 |
plg_1_coverage -= int_areas[i]; |
| 2385 |
auto iplg = plg_1.intersect(plg_2, false, 1.e5 * std::numeric_limits<double>::epsilon()); |
| 2386 |
Log::info().indent(); |
| 2387 |
iplg.print(Log::info()); |
| 2388 |
Log::info() << ", inters.-area: " << iplg.area() << ", plg1-area left: " << plg_1_coverage << std::endl; |
| 2389 |
Log::info().unindent(); |
| 2390 |
Log::info() << std::endl; |
| 2391 |
} |
| 2392 |
Log::info() << std::endl; |
| 2393 |
Log::info().unindent(); |
| 2394 |
Log::info().unindent(); |
| 2395 |
} |
| 2396 |
#endif |
| 2397 |
} |
| 2398 |
|
| 2399 |
ConservativeSphericalPolygonInterpolation::Cache::Cache(std::shared_ptr<InterpolationCacheEntry> entry): |
| 2400 |
interpolation::Cache(entry), entry_(dynamic_cast<Data*>(entry.get())) {} |
| 2401 |
|
| 2402 |
|
| 2403 |
ConservativeSphericalPolygonInterpolation::Cache::Cache(const interpolation::Cache& c): |
| 2404 |
interpolation::Cache(c, Data::static_type()), entry_{dynamic_cast<const Data*>(c.get(Data::static_type()))} {} |
| 2405 |
|
| 2406 |
|
| 2407 |
ConservativeSphericalPolygonInterpolation::Cache::Cache(const Interpolation& interpolation): |
| 2408 |
Cache(interpolation::Cache(interpolation)) {} |
| 2409 |
|
| 2410 |
|
| 2411 |
size_t ConservativeSphericalPolygonInterpolation::Data::footprint() const { |
| 2412 |
size_t mem_total{0}; |
| 2413 |
mem_total += memory_of(src_.points); |
| 2414 |
mem_total += memory_of(tgt_.points); |
| 2415 |
mem_total += memory_of(src_.areas); |
| 2416 |
mem_total += memory_of(tgt_.areas); |
| 2417 |
mem_total += memory_of(src_.csp2node); |
| 2418 |
mem_total += memory_of(tgt_.csp2node); |
| 2419 |
mem_total += memory_of(src_.node2csp); |
| 2420 |
mem_total += memory_of(tgt_.node2csp); |
| 2421 |
mem_total += memory_of(tgt_iparam_); |
| 2422 |
// mem_total += memory_of(src_.csp_index); // TODO need to be added |
| 2423 |
// mem_total += memory_of(src_.csp_cell_index); |
| 2424 |
// mem_total += memory_of(tgt_.csp_index); |
| 2425 |
// mem_total += memory_of(src_.csp_cell_index); |
| 2426 |
return mem_total; |
| 2427 |
} |
| 2428 |
|
| 2429 |
|
| 2430 |
void ConservativeSphericalPolygonInterpolation::Data::print(std::ostream& out) const { |
| 2431 |
out << "Memory usage of ConservativeMethod: " << eckit::Bytes(footprint()) << "\n"; |
| 2432 |
out << "- src_.points \t" << eckit::Bytes(memory_of(src_.points)) << "\n"; |
| 2433 |
out << "- tgt_.points \t" << eckit::Bytes(memory_of(tgt_.points)) << "\n"; |
| 2434 |
out << "- src_.areas \t" << eckit::Bytes(memory_of(src_.areas)) << "\n"; |
| 2435 |
out << "- tgt_.areas \t" << eckit::Bytes(memory_of(tgt_.areas)) << "\n"; |
| 2436 |
out << "- src_.csp2node \t" << eckit::Bytes(memory_of(src_.csp2node)) << "\n"; |
| 2437 |
out << "- tgt_.csp2node \t" << eckit::Bytes(memory_of(tgt_.csp2node)) << "\n"; |
| 2438 |
out << "- src_.node2csp \t" << eckit::Bytes(memory_of(src_.node2csp)) << "\n"; |
| 2439 |
out << "- tgt_.node2csp \t" << eckit::Bytes(memory_of(tgt_.node2csp)) << "\n"; |
| 2440 |
// out << "- src_.csp_index \t" << eckit::Bytes(memory_of(src_.csp_index)) << "\n"; |
| 2441 |
// out << "- src_csp_cellindex_ \t" << eckit::Bytes(memory_of(src_.csp_cell_index)) << "\n"; |
| 2442 |
// out << "- tgt_.csp_index \t" << eckit::Bytes(memory_of(tgt_.csp_index)) << "\n"; |
| 2443 |
// out << "- tgt_csp_cellindex_ \t" << eckit::Bytes(memory_of(tgt_.csp_cell_index)) << "\n"; |
| 2444 |
out << "- tgt_iparam_ \t" << eckit::Bytes(memory_of(tgt_iparam_)) << "\n"; |
| 2445 |
} |
| 2446 |
|
| 2447 |
|
| 2448 |
void ConservativeSphericalPolygonInterpolation::Statistics::fillMetadata(Metadata& metadata) { |
| 2449 |
// errors |
| 2450 |
if (intersection) { |
| 2451 |
metadata.set("errors.intersections_covering_tgt_cells_sum", errors[ERR_TGT_INTERSECTPLG_L1]); |
| 2452 |
metadata.set("errors.intersections_covering_tgt_cells_max", errors[ERR_TGT_INTERSECTPLG_LINF]); |
| 2453 |
metadata.set("errors.sum_src_.areasminus_sum_tgt_areas", errors[ERR_SRCTGT_INTERSECTPLG_DIFF]); |
| 2454 |
} |
| 2455 |
if (conservation) { |
| 2456 |
metadata.set("errors.conservation_error", errors[ERR_REMAP_CONS]); |
| 2457 |
} |
| 2458 |
if (accuracy) { |
| 2459 |
metadata.set("errors.to_solution_sum", errors[ERR_REMAP_L2]); |
| 2460 |
metadata.set("errors.to_solution_max", errors[ERR_REMAP_LINF]); |
| 2461 |
} |
| 2462 |
} |
| 2463 |
|
| 2464 |
|
| 2465 |
ConservativeSphericalPolygonInterpolation::Statistics::Statistics() { |
| 2466 |
std::fill(std::begin(counts), std::end(counts), -1); |
| 2467 |
std::fill(std::begin(errors), std::end(errors), -1.); |
| 2468 |
std::fill(std::begin(memory), std::end(memory), -1); |
| 2469 |
std::fill(std::begin(time), std::end(time), -1.); |
| 2470 |
intersection = false; |
| 2471 |
timings = false; |
| 2472 |
accuracy = false; |
| 2473 |
conservation = false; |
| 2474 |
} |
| 2475 |
|
| 2476 |
|
| 2477 |
ConservativeSphericalPolygonInterpolation::Statistics::Statistics(const Metadata& metadata): Statistics() { |
| 2478 |
if (intersection) { |
| 2479 |
metadata.get("polygons.number_of_intersections", counts[NUM_INT_PLG]); |
| 2480 |
metadata.get("polygons.number_of_full_noncovered_tgt_polygons", counts[NUM_UNCVR_FULL_TGT]); |
| 2481 |
metadata.get("polygons.number_of_part_noncovered_tgt_polygons", counts[NUM_UNCVR_PART_TGT]); |
| 2482 |
} |
| 2483 |
metadata.get("errors.intersections_covering_tgt_cells_sum", errors[ERR_TGT_INTERSECTPLG_L1]); |
| 2484 |
metadata.get("errors.intersections_covering_tgt_cells_max", errors[ERR_TGT_INTERSECTPLG_LINF]); |
| 2485 |
metadata.get("errors.sum_src_.areasminus_sum_tgt_areas", errors[ERR_SRCTGT_INTERSECTPLG_DIFF]); |
| 2486 |
metadata.get("polygons.number_of_src_polygons", counts[NUM_SRC_PLG]); |
| 2487 |
metadata.get("polygons.number_of_tgt_polygons", counts[NUM_TGT_PLG]); |
| 2488 |
} |
| 2489 |
|
| 2490 |
|
| 2491 |
} // namespace method |
| 2492 |
} // namespace interpolation |
| 2493 |
} // namespace atlas |