preCICE v3.1.2
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"),
27 _meshDimensionsMap(),
28 _dataConfig(std::move(config)),
29 _meshes(),
30 _neededMeshes(),
31 _meshIdManager(new utils::ManageUniqueIDs())
32{
33 using namespace xml;
34 std::string doc;
35 XMLTag tag(*this, TAG, xml::XMLTag::OCCUR_ONCE_OR_MORE);
36 doc = "Surface mesh consisting of vertices and optional connectivity information. "
37 "The vertices of a mesh can carry data, "
38 "configured by tags <use-data>. The mesh coordinates have to be "
39 "defined by a participant (see tag <provide-mesh>).";
40 tag.setDocumentation(doc);
41
42 auto attrName = XMLAttribute<std::string>(ATTR_NAME)
43 .setDocumentation("Unique name for the mesh.");
44 tag.addAttribute(attrName);
45
46 auto attrDimensions = XMLAttribute<int>(ATTR_DIMENSIONS)
47 .setDocumentation("Spatial dimensions of mesh")
48 .setOptions({2, 3});
49 tag.addAttribute(attrDimensions);
50
51 XMLTag subtagData(*this, TAG_DATA, XMLTag::OCCUR_ARBITRARY);
52 doc = "Assigns a before defined data set (see tag <data>) to the mesh.";
53 subtagData.setDocumentation(doc);
54 attrName.setDocumentation("Name of the data set.");
55 subtagData.addAttribute(attrName);
56 tag.addSubtag(subtagData);
57
58 parent.addSubtag(tag);
59}
60
62 const xml::ConfigurationContext &context,
63 xml::XMLTag & tag)
64{
66 if (tag.getName() == TAG) {
68 int dimensions = tag.getIntAttributeValue(ATTR_DIMENSIONS);
70 PRECICE_ASSERT(dimensions != 0);
72 _meshes.push_back(std::make_shared<Mesh>(name, dimensions, _meshIdManager->getFreeID()));
73 } else if (tag.getName() == TAG_DATA) {
75 bool found = false;
76 for (const DataConfiguration::ConfiguredData &data : _dataConfig->data()) {
77 auto dataDimensions = getDataDimensions(_meshes.back()->getName(), data.typeName);
78 if (data.name == name) {
79 _meshes.back()->createData(data.name, dataDimensions, _dataIDManager.getFreeID(), data.waveformDegree);
80 found = true;
81 break;
82 }
83 }
84 PRECICE_CHECK(found,
85 "Data with name \"{}\" used by mesh \"{}\" is not defined. "
86 "Please define a data tag with name=\"{}\".",
87 name, _meshes.back()->getName(), name);
88 }
89}
90
96
101
103 const mesh::PtrMesh &mesh)
104{
105 for (const PtrData &dataNewMesh : mesh->data()) {
106 bool found = false;
107 for (const DataConfiguration::ConfiguredData &data : _dataConfig->data()) {
108 if (dataNewMesh->getName() == data.name && dataNewMesh->getDimensions() == getDataDimensions(mesh->getName(), data.typeName)) {
109 found = true;
110 break;
111 }
112 }
113 PRECICE_CHECK(found, "Data {0} is not defined. Please define a data tag with name=\"{0}\".", dataNewMesh->getName());
114 }
115 _meshes.push_back(mesh);
116}
117
119{
120 return _meshes;
121}
122
127
129{
130 auto iter = std::find_if(_meshes.begin(), _meshes.end(), [&meshName](const auto &mptr) {
131 return mptr->getName() == meshName;
132 });
133 return iter != _meshes.end(); // if name was not found in _meshes, iter == _meshes.end()
134}
135
137 const std::string &meshName) const
138{
139 for (const mesh::PtrMesh &mesh : _meshes) {
140 if (mesh->getName() == meshName) {
141 return mesh;
142 }
143 }
144 return mesh::PtrMesh();
145}
146
148 const std::string &participant,
149 const std::string &mesh)
150{
151 PRECICE_TRACE(participant, mesh);
152 if (_neededMeshes.count(participant) == 0) {
154 meshes.push_back(mesh);
156 } else if (not utils::contained(mesh, _neededMeshes.find(participant)->second)) {
157 _neededMeshes.find(participant)->second.push_back(mesh);
158 }
159}
160
162 const std::string &mesh,
163 int dimensions)
164{
165 PRECICE_ASSERT(_meshDimensionsMap.count(mesh) == 0, "Mesh {} already exists in the mesh-dimensions map.", mesh);
167}
168
169int MeshConfiguration::getDataDimensions(const std::string &meshName, const Data::typeName dataTypeName) const
170{
171 if (dataTypeName == Data::typeName::VECTOR) {
172 PRECICE_ASSERT(_meshDimensionsMap.count(meshName) > 0, "Mesh {} does not exist in the mesh-dimensions map.", meshName);
173 return _meshDimensionsMap.at(meshName);
174 } else if (dataTypeName == Data::typeName::SCALAR) {
175 return 1;
176 }
177 // We should never reach this point
178 PRECICE_UNREACHABLE("Unknown data type defined on mesh \"{}\".", meshName);
179};
180
181} // namespace precice::mesh
#define PRECICE_TRACE(...)
Definition LogMacros.hpp:95
#define PRECICE_CHECK(check,...)
Definition LogMacros.hpp:35
std::string name
#define PRECICE_ASSERT(...)
Definition assertion.hpp:87
#define PRECICE_UNREACHABLE(...)
Definition assertion.hpp:95
T at(T... args)
T back(T... args)
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 addNeededMesh(const std::string &participant, const std::string &mesh)
void addMesh(const mesh::PtrMesh &mesh)
mesh::PtrMesh getMesh(const std::string &meshName) const
Returns the configured mesh with given name, or NULL.
std::unique_ptr< utils::ManageUniqueIDs > _meshIdManager
MeshConfiguration(xml::XMLTag &parent, PtrDataConfiguration config)
Constructor, takes a valid data configuration as argument.
const PtrDataConfiguration & getDataConfiguration() const
virtual void xmlTagCallback(const xml::ConfigurationContext &context, xml::XMLTag &callingTag)
Callback at begin of XML tag.
utils::ManageUniqueIDs _dataIDManager
bool hasMeshName(const std::string &meshName) const
Returns whether Mesh has Data with the dataName.
virtual void xmlEndTagCallback(const xml::ConfigurationContext &context, xml::XMLTag &callingTag)
Callback at end of XML tag and at end of subtag.
std::map< std::string, int > _meshDimensionsMap
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.
Represents an XML tag to be configured automatically.
Definition XMLTag.hpp:31
std::string getStringAttributeValue(const std::string &name, std::optional< std::string > default_value=std::nullopt) const
Definition XMLTag.cpp:142
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:129
XMLTag & addSubtag(const XMLTag &tag)
Adds an XML tag as subtag by making a copy of the given tag.
Definition XMLTag.cpp:41
T count(T... args)
T find_if(T... args)
T insert(T... args)
provides Mesh, Data and primitives.
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:39
STL namespace.
T push_back(T... args)
Tightly coupled to the parameters of Participant()
Definition XMLTag.hpp:24