preCICE v3.1.2
Loading...
Searching...
No Matches
NearestProjectionMapping.cpp
Go to the documentation of this file.
2
3#include <Eigen/Core>
4#include <algorithm>
5#include <memory>
6#include <ostream>
7#include <utility>
8
10#include "mapping/Mapping.hpp"
11#include "mapping/Polation.hpp"
12#include "math/differences.hpp"
13#include "mesh/Data.hpp"
14#include "mesh/Mesh.hpp"
16#include "mesh/Vertex.hpp"
17#include "profiling/Event.hpp"
18#include "query/Index.hpp"
19#include "utils/IntraComm.hpp"
20#include "utils/Statistics.hpp"
21#include "utils/assertion.hpp"
22
23namespace precice::mapping {
24
26 Constraint constraint,
27 int dimensions)
28 : BarycentricBaseMapping(constraint, dimensions)
29{
30 if (constraint == CONSISTENT) {
33 } else if (constraint == CONSERVATIVE) {
36 } else {
40 }
41
42 PRECICE_CHECK(constraint != SCALED_CONSISTENT_VOLUME, "Nearest-projection can't be used with volume version of the scaled-consistent mapping. Use scaled-consistent instead.");
43}
44
46{
47 PRECICE_TRACE(input()->nVertices(), output()->nVertices());
48 const std::string baseEvent = "map.np.computeMapping.From" + input()->getName() + "To" + output()->getName();
50
51 // Setup Direction of Mapping
52 mesh::PtrMesh origins, searchSpace;
54 PRECICE_DEBUG("Compute conservative mapping");
55 origins = input();
56 searchSpace = output();
57 } else {
58 PRECICE_DEBUG("Compute consistent mapping");
59 origins = output();
60 searchSpace = input();
61 }
62
63 const auto &fVertices = origins->vertices();
64
65 if (getDimensions() == 2) {
66 PRECICE_WARN_IF(!fVertices.empty() && searchSpace->edges().empty(),
67 "2D Mesh \"{}\" does not contain edges. "
68 "Nearest projection mapping falls back to nearest neighbor mapping.",
69 searchSpace->getName());
70 } else {
71 PRECICE_WARN_IF(!fVertices.empty() && searchSpace->triangles().empty(),
72 "3D Mesh \"{}\" does not contain triangles. "
73 "Nearest projection mapping will map to primitives of lower dimension.",
74 searchSpace->getName());
75 }
76
77 // Amount of nearest elements to fetch for detailed comparison.
78 // This safety margin results in a candidate set which forms the base for the
79 // local nearest projection and counters the loss of detail due to bounding box generation.
80 // @TODO Add a configuration option for this factor
81 constexpr int nnearest = 4;
82
84
85 _interpolations.clear();
86 _interpolations.reserve(fVertices.size());
87
88 auto &index = searchSpace->index();
89 for (const auto &fVertex : fVertices) {
90 // Nearest projection element is edge for 2d if exists, if not, it is the nearest vertex
91 // Nearest projection element is triangle for 3d if exists, if not the edge and at the worst case it is the nearest vertex
92 auto match = index.findNearestProjection(fVertex.getCoords(), nnearest);
93 distanceStatistics(match.polation.distance());
94 _interpolations.push_back(std::move(match.polation));
95 }
96
97 if (distanceStatistics.empty()) {
98 PRECICE_INFO("Mapping distance not available due to empty partition.");
99 } else {
100 PRECICE_INFO("Mapping distance {}", distanceStatistics);
101 }
102
103 _hasComputedMapping = true;
104}
105
107{
108 return "nearest-projection";
109}
110
111} // namespace precice::mapping
#define PRECICE_WARN_IF(condition,...)
Definition LogMacros.hpp:21
#define PRECICE_DEBUG(...)
Definition LogMacros.hpp:64
#define PRECICE_TRACE(...)
Definition LogMacros.hpp:95
#define PRECICE_INFO(...)
Definition LogMacros.hpp:13
#define PRECICE_CHECK(check,...)
Definition LogMacros.hpp:35
unsigned int index
#define PRECICE_ASSERT(...)
Definition assertion.hpp:87
Base class for interpolation based mappings, where mapping is done using a geometry-based linear comb...
mesh::PtrMesh output() const
Returns pointer to output mesh.
Definition Mapping.cpp:91
Constraint
Specifies additional constraints for a mapping.
Definition Mapping.hpp:29
mesh::PtrMesh input() const
Returns pointer to input mesh.
Definition Mapping.cpp:86
bool _hasComputedMapping
Flag to indicate whether computeMapping() has been called.
Definition Mapping.hpp:208
bool isScaledConsistent() const
Returns true if mapping is a form of scaled consistent mapping.
Definition Mapping.cpp:257
void setInputRequirement(MeshRequirement requirement)
Sets the mesh requirement for the input mesh.
Definition Mapping.cpp:96
void setOutputRequirement(MeshRequirement requirement)
Sets the mesh requirement for the output mesh.
Definition Mapping.cpp:102
virtual bool hasConstraint(const Constraint &constraint) const
Checks whether the mapping has the given constraint or not.
Definition Mapping.cpp:247
std::string getName() const final override
name of the np mapping
void computeMapping() final override
Computes the projections and interpolation relations.
NearestProjectionMapping(Constraint constraint, int dimensions)
Constructor, taking mapping constraint.
bool empty() const
Returns count == 0.
contains data mapping from points to meshes.
static constexpr SynchronizeTag Synchronize
Convenience instance of the SynchronizeTag.
Definition Event.hpp:21