preCICE
Loading...
Searching...
No Matches
MeshConfiguration.cpp
Go to the documentation of this file.
2#include <memory>
3
4#include <sstream>
5#include <stdexcept>
6#include <utility>
7
9#include "mesh/Data.hpp"
10#include "mesh/Mesh.hpp"
12#include "utils/Helpers.hpp"
13#include "utils/assertion.hpp"
14#include "xml/ConfigParser.hpp"
15#include "xml/XMLAttribute.hpp"
16
17namespace precice::mesh {
18
20 xml::XMLTag &parent,
22 : TAG("mesh"),
23 ATTR_NAME("name"),
24 ATTR_DIMENSIONS("dimensions"),
25 TAG_DATA("use-data"),
26 ATTR_SIDE_INDEX("side"),
28 _dataConfig(std::move(config)),
29 _meshes(),
31{
32 using namespace xml;
33 std::string doc;
34 XMLTag tag(*this, TAG, xml::XMLTag::OCCUR_ONCE_OR_MORE);
35 doc = "Surface mesh consisting of vertices and optional connectivity information. "
36 "The vertices of a mesh can carry data, "
37 "configured by tags <use-data>. The mesh coordinates have to be "
38 "defined by a participant (see tag <provide-mesh>).";
39 tag.setDocumentation(doc);
40
41 auto attrName = XMLAttribute<std::string>(ATTR_NAME)
42 .setDocumentation("Unique name for the mesh.");
43 tag.addAttribute(attrName);
44
45 auto attrDimensions = XMLAttribute<int>(ATTR_DIMENSIONS)
46 .setDocumentation("Spatial dimensions of mesh")
47 .setOptions({2, 3});
48 tag.addAttribute(attrDimensions);
49
50 XMLTag subtagData(*this, TAG_DATA, XMLTag::OCCUR_ARBITRARY);
51 doc = "Assigns a before defined data set (see tag <data>) to the mesh.";
52 subtagData.setDocumentation(doc);
53 attrName.setDocumentation("Name of the data set.");
54 subtagData.addAttribute(attrName);
55 tag.addSubtag(subtagData);
56
57 parent.addSubtag(tag);
58}
59
61 const xml::ConfigurationContext &context,
62 xml::XMLTag &tag)
63{
65 if (tag.getName() == TAG) {
66 std::string name = tag.getStringAttributeValue(ATTR_NAME);
67 int dimensions = tag.getIntAttributeValue(ATTR_DIMENSIONS);
68 insertMeshToMeshDimensionsMap(name, dimensions);
69 PRECICE_ASSERT(dimensions != 0);
70 _meshes.push_back(std::make_shared<Mesh>(name, dimensions, _meshIdManager.getFreeID()));
71 } else if (tag.getName() == TAG_DATA) {
72 std::string name = tag.getStringAttributeValue(ATTR_NAME);
73 bool found = false;
74 for (const DataConfiguration::ConfiguredData &data : _dataConfig->data()) {
75 auto dataDimensions = getDataDimensions(_meshes.back()->getName(), data.typeName);
76 auto lowerBound = data.lowerBound;
77 auto upperBound = data.upperBound;
78 lowerBound.resize(dataDimensions);
79 upperBound.resize(dataDimensions);
80 if (data.name == name) {
81 _meshes.back()->createData(data.name, dataDimensions, _dataIDManager.getFreeID(), data.waveformDegree, lowerBound, upperBound);
82 found = true;
83 break;
84 }
85 }
86 PRECICE_CHECK(found,
87 "Data with name \"{}\" used by mesh \"{}\" is not defined. "
88 "Please define a data tag with name=\"{}\".",
89 name, _meshes.back()->getName(), name);
90 }
91}
92
98
103
105{
106 return std::make_shared<mesh::Mesh>("(just-in-time mapping)", dimension, mesh::Mesh::MESH_ID_UNDEFINED, true);
107}
108
110 const mesh::PtrMesh &mesh)
111{
112 for (const PtrData &dataNewMesh : mesh->data()) {
113 bool found = false;
114 for (const DataConfiguration::ConfiguredData &data : _dataConfig->data()) {
115 if (dataNewMesh->getName() == data.name && dataNewMesh->getDimensions() == getDataDimensions(mesh->getName(), data.typeName)) {
116 found = true;
117 break;
118 }
119 }
120 PRECICE_CHECK(found, "Data {0} is not defined. Please define a data tag with name=\"{0}\".", dataNewMesh->getName());
121 }
122 _meshes.push_back(mesh);
123}
124
125const std::vector<PtrMesh> &MeshConfiguration::meshes() const
126{
127 return _meshes;
128}
129
130std::vector<PtrMesh> &MeshConfiguration::meshes()
131{
132 return _meshes;
133}
134
135bool MeshConfiguration::hasMeshName(const std::string &meshName) const
136{
137 auto iter = std::find_if(_meshes.begin(), _meshes.end(), [&meshName](const auto &mptr) {
138 return mptr->getName() == meshName;
139 });
140 return iter != _meshes.end(); // if name was not found in _meshes, iter == _meshes.end()
141}
142
144 const std::string &meshName) const
145{
146 for (const mesh::PtrMesh &mesh : _meshes) {
147 if (mesh->getName() == meshName) {
148 return mesh;
149 }
150 }
151 return mesh::PtrMesh();
152}
153
155 const std::string &participant,
156 const std::string &mesh)
157{
158 PRECICE_TRACE(participant, mesh);
159 if (_neededMeshes.count(participant) == 0) {
160 std::vector<std::string> meshes;
161 meshes.push_back(mesh);
162 _neededMeshes.insert(std::pair<std::string, std::vector<std::string>>(participant, meshes));
163 } else if (not utils::contained(mesh, _neededMeshes.find(participant)->second)) {
164 _neededMeshes.find(participant)->second.push_back(mesh);
165 }
166}
167
169 const std::string &mesh,
170 int dimensions)
171{
172 PRECICE_ASSERT(_meshDimensionsMap.count(mesh) == 0, "Mesh {} already exists in the mesh-dimensions map.", mesh);
173 _meshDimensionsMap.insert(std::pair<std::string, int>(mesh, dimensions));
174}
175
176int MeshConfiguration::getDataDimensions(const std::string &meshName, const Data::typeName dataTypeName) const
177{
178 if (dataTypeName == Data::typeName::VECTOR) {
179 PRECICE_ASSERT(_meshDimensionsMap.count(meshName) > 0, "Mesh {} does not exist in the mesh-dimensions map.", meshName);
180 return _meshDimensionsMap.at(meshName);
181 } else if (dataTypeName == Data::typeName::SCALAR) {
182 return 1;
183 }
184 // We should never reach this point
185 PRECICE_UNREACHABLE("Unknown data type defined on mesh \"{}\".", meshName);
186};
187
188} // namespace precice::mesh
#define PRECICE_TRACE(...)
Definition LogMacros.hpp:92
#define PRECICE_CHECK(check,...)
Definition LogMacros.hpp:32
#define PRECICE_ASSERT(...)
Definition assertion.hpp:85
#define PRECICE_UNREACHABLE(...)
Definition assertion.hpp:93
std::map< std::string, std::vector< std::string > > _neededMeshes
to check later if all meshes that any coupling scheme needs are actually used by the participants
std::vector< PtrMesh > _meshes
Configured meshes.
void xmlTagCallback(const xml::ConfigurationContext &context, xml::XMLTag &callingTag) override
Callback at begin of XML tag.
void addNeededMesh(const std::string &participant, const std::string &mesh)
utils::ManageUniqueIDs _meshIdManager
void addMesh(const mesh::PtrMesh &mesh)
mesh::PtrMesh getMesh(const std::string &meshName) const
Returns the configured mesh with given name, or NULL.
MeshConfiguration(xml::XMLTag &parent, PtrDataConfiguration config)
Constructor, takes a valid data configuration as argument.
const PtrDataConfiguration & getDataConfiguration() const
utils::ManageUniqueIDs _dataIDManager
void xmlEndTagCallback(const xml::ConfigurationContext &context, xml::XMLTag &callingTag) override
Callback at end of XML tag and at end of subtag.
bool hasMeshName(const std::string &meshName) const
Returns whether Mesh has Data with the dataName.
std::map< std::string, int > _meshDimensionsMap
static mesh::PtrMesh getJustInTimeMappingMesh(int dimension)
int getDataDimensions(const std::string &meshName, const Data::typeName typeName) const
Get the number of dimensions that data values of this type (scalar/vector) have on this mesh.
PtrDataConfiguration _dataConfig
Data configuration.
void insertMeshToMeshDimensionsMap(const std::string &mesh, int dimensions)
Initialize the map between meshes and dimensions, for unit tests that directly create mesh objects wi...
const std::vector< PtrMesh > & meshes() const
Returns all configured meshes.
static constexpr MeshID MESH_ID_UNDEFINED
Use if the id of the mesh is not necessary.
Definition Mesh.hpp:57
Represents an XML tag to be configured automatically.
Definition XMLTag.hpp:28
std::string getStringAttributeValue(const std::string &name, std::optional< std::string > default_value=std::nullopt) const
Definition XMLTag.cpp:145
const std::string & getName() const
Returns name (without namespace).
Definition XMLTag.hpp:153
int getIntAttributeValue(const std::string &name, std::optional< int > default_value=std::nullopt) const
Definition XMLTag.cpp:131
XMLTag & addSubtag(const XMLTag &tag)
Adds an XML tag as subtag by making a copy of the given tag.
Definition XMLTag.cpp:41
provides Mesh, Data and primitives.
std::shared_ptr< Data > PtrData
std::shared_ptr< DataConfiguration > PtrDataConfiguration
std::shared_ptr< Mesh > PtrMesh
bool contained(const ELEMENT_T &element, const std::vector< ELEMENT_T > &vec)
Returns true, if given element is in vector, otherwise false.
Definition Helpers.hpp:38
contains the XML configuration parser.
STL namespace.
Tightly coupled to the parameters of Participant()
Definition XMLTag.hpp:21