preCICE v3.2.0
Loading...
Searching...
No Matches
NearestNeighborBaseMapping.cpp
Go to the documentation of this file.
2
3#include <boost/container/flat_set.hpp>
4#include <functional>
5#include <iostream>
7#include "mapping/Mapping.hpp"
9#include "mesh/Vertex.hpp"
10#include "profiling/Event.hpp"
11#include "utils/IntraComm.hpp"
12#include "utils/Parallel.hpp"
13#include "utils/Statistics.hpp"
14#include "utils/assertion.hpp"
15
16namespace precice::mapping {
17
29
31{
32 PRECICE_TRACE(input()->nVertices());
33
34 PRECICE_ASSERT(input().get() != nullptr);
35 PRECICE_ASSERT(output().get() != nullptr);
36
37 const std::string baseEvent = "map." + mappingNameShort + ".computeMapping.From" + input()->getName() + "To" + output()->getName();
39
40 // Setup Direction of Mapping
41 mesh::PtrMesh origins, searchSpace;
43 PRECICE_DEBUG("Compute conservative mapping");
44 origins = input();
45 searchSpace = output();
46 } else {
47 PRECICE_DEBUG((hasConstraint(CONSISTENT) ? "Compute consistent mapping" : "Compute scaled-consistent mapping"));
48 origins = output();
49 searchSpace = input();
50 }
51
52 // Set up of output arrays
53 const size_t verticesSize = origins->nVertices();
54 const auto &sourceVertices = origins->vertices();
55 _vertexIndices.resize(verticesSize);
56
57 // Needed for error calculations
59
60 auto &index = searchSpace->index();
61 for (size_t i = 0; i < verticesSize; ++i) {
62 const auto &sourceCoords = sourceVertices[i].getCoords();
63 const auto matchedVertex = index.getClosestVertex(sourceCoords);
64 _vertexIndices[i] = matchedVertex.index;
65
66 // Compute distance between input and output vertiex for the stats
67 const auto &matchCoords = searchSpace->vertex(matchedVertex.index).getCoords();
68 auto distance = (sourceCoords - matchCoords).norm();
69 distanceStatistics(distance);
70 }
71
72 // For gradient mapping, the calculation of offsets between source and matched vertex necessary
73 onMappingComputed(origins, searchSpace);
74
75 // This is the distance object between the coordinates of the vertices and its match in the mesh.
76 // This prints min, max, average and count of the distances.
77 if (distanceStatistics.empty()) {
78 PRECICE_INFO("Mapping distance not available due to empty partition.");
79 } else {
80 PRECICE_INFO("Mapping distance {}", distanceStatistics);
81 }
82
84}
85
87{
89 _vertexIndices.clear();
90 _hasComputedMapping = false;
91
93 _offsetsMatched.clear();
94
95 if (getConstraint() == CONSISTENT) {
96 input()->index().clear();
97 } else {
98 output()->index().clear();
99 }
100}
101
103{
104 // Does nothing by default
105}
106
108{
110 precice::profiling::Event e("map." + mappingNameShort + ".tagMeshFirstRound.From" + input()->getName() + "To" + output()->getName(), profiling::Synchronize);
111
112 // parallel partitioning for just-in-time mapping:
113 if (this->isJustInTimeMapping()) {
114 // in the usual case, we make use of the indexSet, which is pre-computed from the mapping
115 // for the just-in-time mapping, we can't do that since we don't have the output (local) mesh
116 // what we would need to do in theory for a perfect partitioning:
117 // find all nearest-neighbors at the 'boundary' of the access region, which would require an
118 // infinite fine sampling of output mesh nodes to be used in the computeMapping below
119 // for now, we simply tag everything and move on. The remote mesh is here already filtered
120 // through the geometric filter setting.
121 //
122 // Depending on the mapping constraint, one of these tagAll calls will do nothing, as the vertex
123 // set of the mesh is empty. From a practical point of view, we only need to apply the
124 // tagging to one of the meshes (the remote one). But calling it on both sides reliefs us from any
125 // conditional code.
126 output()->tagAll();
127 input()->tagAll();
128 return;
129 }
130
132
133 // Lookup table of all indices used in the mapping
134 const boost::container::flat_set<int> indexSet(_vertexIndices.begin(), _vertexIndices.end());
135
136 // Get the source mesh depending on the constraint
137 const mesh::PtrMesh &source = hasConstraint(CONSERVATIVE) ? output() : input();
138
139 // Tag all vertices used in the mapping
140 for (mesh::Vertex &v : source->vertices()) {
141 if (indexSet.count(v.getID()) != 0) {
142 v.tag();
143 }
144 }
145
146 clear();
147}
148
150{
152 // for NN mapping no operation needed here
153}
154
155} // namespace precice::mapping
#define PRECICE_DEBUG(...)
Definition LogMacros.hpp:61
#define PRECICE_TRACE(...)
Definition LogMacros.hpp:92
#define PRECICE_INFO(...)
Definition LogMacros.hpp:14
#define PRECICE_ASSERT(...)
Definition assertion.hpp:85
mesh::PtrMesh output() const
Returns pointer to output mesh.
Definition Mapping.cpp:92
Constraint
Specifies additional constraints for a mapping.
Definition Mapping.hpp:30
Mapping(Constraint constraint, int dimensions, bool requiresGradientData, InitialGuessRequirement initialGuessRequirement)
Constructor, takes mapping constraint.
Definition Mapping.cpp:12
mesh::PtrMesh input() const
Returns pointer to input mesh.
Definition Mapping.cpp:87
bool _hasComputedMapping
Flag to indicate whether computeMapping() has been called.
Definition Mapping.hpp:307
bool isJustInTimeMapping() const
Definition Mapping.cpp:263
Constraint getConstraint() const
Returns the constraint (consistent/conservative) of the mapping.
Definition Mapping.cpp:46
bool requiresGradientData() const
Returns whether the mapping requires gradient data.
Definition Mapping.cpp:114
virtual std::string getName() const =0
Returns the name of the mapping method for logging purpose.
virtual bool hasConstraint(const Constraint &constraint) const
Checks whether the mapping has the given constraint or not.
Definition Mapping.cpp:248
InitialGuessRequirement
Specifies whether the mapping requires an initial guess.
Definition Mapping.hpp:64
void clear() final override
Removes a computed mapping.
std::string mappingName
NearestNeighborMapping or NearestNeighborGradientMapping.
virtual void onMappingComputed(mesh::PtrMesh origins, mesh::PtrMesh searchSpace)
std::vector< int > _vertexIndices
Computed output vertex indices to map data from input vertices to.
void tagMeshSecondRound() final override
Method used by partition. Tags vertices that can be filtered out.
void computeMapping() final override
Computes the mapping coefficients from the in- and output mesh.
NearestNeighborBaseMapping(Constraint constraint, int dimensions, bool hasGradient, std::string mappingName, std::string mappingNameShort)
Constructor.
void tagMeshFirstRound() final override
Method used by partition. Tags vertices that could be owned by this rank.
Vertex of a mesh.
Definition Vertex.hpp:16
bool empty() const
Returns count == 0.
contains data mapping from points to meshes.
std::shared_ptr< Mesh > PtrMesh
static constexpr SynchronizeTag Synchronize
Convenience instance of the SynchronizeTag.
Definition Event.hpp:21
constexpr auto get(span< E, S > s) -> decltype(s[N])
Definition span.hpp:602
STL namespace.