preCICE
Loading...
Searching...
No Matches
NearestNeighborGradientMapping.cpp
Go to the documentation of this file.
2
3#include <Eigen/Core>
4#include <boost/container/flat_set.hpp>
5#include <functional>
6#include <iostream>
8#include "profiling/Event.hpp"
10#include "utils/IntraComm.hpp"
11#include "utils/assertion.hpp"
12
13namespace precice::mapping {
14
16 Constraint constraint,
17 int dimensions)
18 : NearestNeighborBaseMapping(constraint, dimensions, true, "NearestNeighborGradientMapping", "nng")
19{
21
23 "The scaled-consistent mapping hasn't been specifically tested with nearest-neighbor-gradient. Please avoid using it or choose another mapping method. ");
24
25 if (isScaledConsistent()) {
28 } else {
31 }
32}
33
35{
36
37 // Initialize the offsets list
38 _offsetsMatched.resize(_vertexIndices.size());
39
40 // Calculate offsets
41 for (size_t i = 0; i < _vertexIndices.size(); ++i) {
42
43 const auto &matchedVertexCoords = searchSpace->vertex(_vertexIndices[i]).getCoords();
44 const auto &sourceVertexCoords = origins->vertex(i).getCoords();
45
46 // We calculate the distances uniformly for consistent mapping constraint as the difference (output - input)
47 // For consistent mapping: the source is the output vertex and the matched vertex is the input since we iterate over all outputs
48 // and assign each exactly one vertex form the search space, which are our origins vertices.
49 _offsetsMatched[i] = sourceVertexCoords - matchedVertexCoords;
50 }
51};
52
53void NearestNeighborGradientMapping::mapConsistent(const time::Sample &inData, Eigen::VectorXd &outData)
54{
56 precice::profiling::Event e("map." + mappingNameShort + ".mapData.From" + input()->getName() + "To" + output()->getName(), profiling::Synchronize);
57
58 PRECICE_ASSERT(inData.values.size() == 0 || inData.gradients.size() != 0,
59 "Mesh \"{}\" does not contain gradient data. Using Nearest Neighbor Gradient mapping requires gradient data.",
60 input()->getName());
61
63 PRECICE_WARN_IF(input()->empty(), "The mesh doesn't contain any vertices.");
64
65 const int valueDimensions = inData.dataDims;
66 const Eigen::VectorXd &inputValues = inData.values;
67 Eigen::VectorXd &outputValues = outData;
68 const Eigen::MatrixXd &gradients = inData.gradients;
69
70 // Consistent mapping
71 PRECICE_DEBUG("Map {} using {}", (hasConstraint(CONSISTENT) ? "consistent" : "scaled-consistent"), getName());
72 const size_t outSize = output()->nVertices();
73
74 for (size_t i = 0; i < outSize; i++) {
75 int inputIndex = _vertexIndices[i] * valueDimensions;
76
77 for (int dim = 0; dim < valueDimensions; dim++) {
78
79 const int mapOutputIndex = (i * valueDimensions) + dim;
80 const int mapInputIndex = inputIndex + dim;
81
82 outputValues(mapOutputIndex) = inputValues(mapInputIndex) + _offsetsMatched[i].transpose() * gradients.col(mapInputIndex);
83 }
84 }
85
86 PRECICE_DEBUG("Mapped values (with gradient) = {}", utils::previewRange(3, outputValues));
87}
88
89void NearestNeighborGradientMapping::mapConservative(const time::Sample & /* inData */, Eigen::VectorXd & /* outData */)
90{
91 PRECICE_ASSERT(false, "Not implemented.");
92}
93
95{
96 return "nearest-neighbor-gradient";
97}
98
99} // namespace precice::mapping
#define PRECICE_WARN_IF(condition,...)
Definition LogMacros.hpp:18
#define PRECICE_DEBUG(...)
Definition LogMacros.hpp:61
#define PRECICE_TRACE(...)
Definition LogMacros.hpp:92
#define PRECICE_ASSERT(...)
Definition assertion.hpp:85
std::size_t nVertices() const
Returns the number of vertices.
Definition Mesh.cpp:64
Vertex & vertex(VertexID id)
Mutable access to a vertex by VertexID.
Definition Mesh.cpp:42
mesh::PtrMesh output() const
Returns pointer to output mesh.
Definition Mapping.cpp:92
Constraint
Specifies additional constraints for a mapping.
Definition Mapping.hpp:30
mesh::PtrMesh input() const
Returns pointer to input mesh.
Definition Mapping.cpp:87
bool isScaledConsistent() const
Returns true if mapping is a form of scaled consistent mapping.
Definition Mapping.cpp:258
void setInputRequirement(MeshRequirement requirement)
Sets the mesh requirement for the input mesh.
Definition Mapping.cpp:97
void setOutputRequirement(MeshRequirement requirement)
Sets the mesh requirement for the output mesh.
Definition Mapping.cpp:103
virtual bool hasConstraint(const Constraint &constraint) const
Checks whether the mapping has the given constraint or not.
Definition Mapping.cpp:248
std::vector< int > _vertexIndices
Computed output vertex indices to map data from input vertices to.
NearestNeighborBaseMapping(Constraint constraint, int dimensions, bool hasGradient, std::string mappingName, std::string mappingNameShort)
Constructor.
NearestNeighborGradientMapping(Constraint constraint, int dimensions)
Constructor.
void mapConservative(const time::Sample &inData, Eigen::VectorXd &outData) final override
Maps data using a conservative constraint.
void onMappingComputed(mesh::PtrMesh origins, mesh::PtrMesh searchSpace) final override
Calculates the offsets needed for the gradient mappings after calculating the matched vertices.
void mapConsistent(const time::Sample &inData, Eigen::VectorXd &outData) final override
Maps data using a consistent constraint.
std::string getName() const final override
name of the nng mapping
Eigen::VectorXd getCoords() const
Returns the coordinates of the vertex.
Definition Vertex.hpp:114
contains data mapping from points to meshes.
std::shared_ptr< Mesh > PtrMesh
static constexpr SynchronizeTag Synchronize
Convenience instance of the SynchronizeTag.
Definition Event.hpp:28
const RangePreview< Iter > previewRange(Size n, const Range &range)
Eigen::MatrixXd gradients
The gradients of the data. Use gradients.col(d*i+k) to get the gradient of vertex i,...
Definition Sample.hpp:67
int dataDims
The dimensionality of the data.
Definition Sample.hpp:60
Eigen::VectorXd values
Definition Sample.hpp:64