preCICE v3.2.0
Loading...
Searching...
No Matches
ParticipantState.cpp
Go to the documentation of this file.
2#include <algorithm>
3#include <ostream>
4#include <sstream>
5#include <string>
6#include <string_view>
7#include <utility>
8
9#include "MappingContext.hpp"
10#include "MeshContext.hpp"
11#include "WatchIntegral.hpp"
12#include "WatchPoint.hpp"
13#include "action/Action.hpp"
14#include "io/Export.hpp"
15#include "logging/LogMacros.hpp"
16#include "mesh/Data.hpp"
17#include "mesh/Mesh.hpp"
23#include "utils/String.hpp"
24#include "utils/assertion.hpp"
25#include "utils/fmt.hpp"
26
27namespace precice::impl {
28
30 std::string name,
32 : _name(std::move(name))
33{
34}
35
37{
38 for (MeshContext *context : _usedMeshContexts) {
39 delete context;
40 }
41 _usedMeshContexts.clear();
42}
43
45
47{
48 auto &context = meshContext(action->getMesh()->getName());
49 context.require(action->getMeshRequirement());
50 _actions.push_back(std::move(action));
51}
52
57
59 const PtrWatchPoint &watchPoint)
60{
61 _watchPoints.push_back(watchPoint);
62}
63
65 const PtrWatchIntegral &watchIntegral)
66{
67 _watchIntegrals.push_back(watchIntegral);
68}
69
71{
72 std::string meshName = mesh->getName();
73 PRECICE_TRACE(_name, meshName);
74 checkDuplicatedUse(meshName);
75
76 auto context = new MeshContext();
77 context->mesh = mesh;
78 context->provideMesh = true;
79 _meshContexts[std::move(meshName)] = context;
80 _usedMeshContexts.push_back(context);
81}
82
84 const std::string &fromParticipant,
85 double safetyFactor,
87 const bool allowDirectAccess)
88{
89 std::string meshName = mesh->getName();
90 PRECICE_TRACE(_name, meshName);
91 checkDuplicatedUse(meshName);
92 PRECICE_ASSERT(!fromParticipant.empty());
93 PRECICE_ASSERT(safetyFactor >= 0);
94 auto context = new MeshContext();
95 context->mesh = mesh;
96 context->receiveMeshFrom = fromParticipant;
97 context->safetyFactor = safetyFactor;
98 context->provideMesh = false;
99 context->geoFilter = geoFilter;
100 context->allowDirectAccess = allowDirectAccess;
101
102 _meshContexts[std::move(meshName)] = context;
103
104 _usedMeshContexts.push_back(context);
105}
106
108 const mesh::PtrData &data,
109 const mesh::PtrMesh &mesh)
110{
111 checkDuplicatedData(mesh->getName(), data->getName());
112 _writeDataContexts.emplace(MeshDataKey{mesh->getName(), data->getName()}, WriteDataContext(data, mesh));
113}
114
116 const mesh::PtrData &data,
117 const mesh::PtrMesh &mesh)
118{
119 checkDuplicatedData(mesh->getName(), data->getName());
120 _readDataContexts.emplace(MeshDataKey{mesh->getName(), data->getName()}, ReadDataContext(data, mesh));
121}
122
124 const MappingContext &mappingContext)
125{
126 _readMappingContexts.push_back(mappingContext);
127}
128
130 const MappingContext &mappingContext)
131{
132 _writeMappingContexts.push_back(mappingContext);
133}
134
135// Data queries
137{
138 auto it = _readDataContexts.find(MeshDataKey{mesh, data});
139 PRECICE_CHECK(it != _readDataContexts.end(), "Data \"{}\" does not exist for mesh \"{}\".", data, mesh);
140 return it->second;
141}
142
144{
145 auto it = _readDataContexts.find(MeshDataKey{mesh, data});
146 PRECICE_CHECK(it != _readDataContexts.end(), "Data \"{}\" does not exist for mesh \"{}\".", data, mesh);
147 return it->second;
148}
149
151{
152 for (const auto &meshContext : _meshContexts) {
153 const auto &mesh = meshContext.second->mesh->getName();
155 const auto it = _readDataContexts.find(key);
156 if (it != _readDataContexts.end()) {
157 return meshContext.second->mesh;
158 }
159 }
160 return nullptr;
161}
162
164{
165 auto it = _writeDataContexts.find(MeshDataKey{mesh, data});
166 PRECICE_CHECK(it != _writeDataContexts.end(), "Data \"{}\" does not exist in write direction.", data);
167 return it->second;
168}
169
171{
172 auto it = _writeDataContexts.find(MeshDataKey{mesh, data});
173 PRECICE_CHECK(it != _writeDataContexts.end(), "Data \"{}\" does not exist in write direction.", data);
174 return it->second;
175}
176
178{
179 return std::any_of(
180 _meshContexts.begin(), _meshContexts.end(),
181 [data](const auto &mckv) {
182 const auto &meshData = mckv.second->mesh->data();
183 return std::any_of(meshData.begin(), meshData.end(), [data](const auto &dptr) {
184 return dptr->getName() == data;
185 });
186 });
187}
188
190{
191 const auto &meshData = meshContext(mesh).mesh->data();
192 const auto match = std::find_if(meshData.begin(), meshData.end(), [data](auto &dptr) { return dptr->getName() == data; });
193 return match != meshData.end();
194}
195
200
205
207
209{
210 auto pos = _meshContexts.find(mesh);
211 PRECICE_ASSERT(pos != _meshContexts.end());
212 return *pos->second;
213}
214
216{
217 auto pos = _meshContexts.find(mesh);
218 PRECICE_ASSERT(pos != _meshContexts.end());
219 return *pos->second;
220}
221
226
231
233{
234 auto pos = std::find_if(_usedMeshContexts.begin(), _usedMeshContexts.end(),
235 [mesh](MeshContext const *context) {
236 return context->mesh->getName() == mesh;
237 });
238 PRECICE_ASSERT(pos != _usedMeshContexts.end());
239 return **pos;
240}
241
243{
244 auto pos = std::find_if(_usedMeshContexts.begin(), _usedMeshContexts.end(),
245 [mesh](MeshContext const *context) {
246 return context->mesh->getName() == mesh;
247 });
248 PRECICE_ASSERT(pos != _usedMeshContexts.end());
249 return **pos;
250}
251
253{
254 return _meshContexts.count(mesh) > 0;
255}
256
258{
259 return std::any_of(
261 [mesh](const MeshContext *mcptr) {
262 return mcptr->mesh->getName() == mesh;
263 });
264}
265
267{
268 if (!hasMesh(mesh)) {
269 return false;
270 }
271 return usedMeshContext(mesh).provideMesh;
272}
273
275{
276 if (!hasMesh(mesh)) {
277 return false;
278 }
279 return !usedMeshContext(mesh).provideMesh;
280}
281
283{
284 if (!hasMesh(mesh)) {
285 return false;
286 }
287 return meshContext(mesh).allowDirectAccess;
288}
289
290// Other queries
291
293{
294 return !_readMappingContexts.empty();
295}
296
298{
299 return !_writeMappingContexts.empty();
300}
301
306
311
316
321
323 const io::ExportContext &exportContext)
324{
325 _exportContexts.push_back(exportContext);
326}
327
332
337
342
344{
345 return _useIntraComm;
346}
347
349{
350 return _name;
351}
352
354{
355 for (const io::ExportContext &context : exportContexts()) {
356 if (context.everyNTimeWindows < 1) {
357 continue;
358 }
359
360 PRECICE_DEBUG("Exporting initial mesh {} to location \"{}\"", context.meshName, context.location);
361 context.exporter->doExport(0, 0.0);
362 }
363
364 for (const PtrWatchPoint &watchPoint : watchPoints()) {
365 watchPoint->exportPointData(0.0);
366 }
367
368 for (const PtrWatchIntegral &watchIntegral : watchIntegrals()) {
369 watchIntegral->exportIntegralData(0.0);
370 }
371}
372
374{
375 return !_exportContexts.empty() || !_watchPoints.empty() || !_watchIntegrals.empty();
376}
377
379{
380 for (const io::ExportContext &context : exportContexts()) {
381 if (context.everyIteration) {
382 PRECICE_DEBUG("Exporting mesh {} for iteration {} to location \"{}\"", context.meshName, exp.iteration, context.location);
383 context.exporter->doExport(exp.iteration, exp.time);
384 continue;
385 }
386 if (exp.complete) {
387 PRECICE_DEBUG("Exporting mesh {} for timewindow {} to location \"{}\"", context.meshName, exp.timewindow, context.location);
388 context.exporter->doExport(exp.timewindow, exp.time);
389 }
390 if (exp.final) {
391 PRECICE_DEBUG("Exporting seried file of mesh {} to location \"{}\"", context.meshName, context.location);
392 context.exporter->exportSeries();
393 }
394 }
395
396 if (exp.complete) {
397 // Export watch point data
398 for (const PtrWatchPoint &watchPoint : watchPoints()) {
399 watchPoint->exportPointData(exp.time);
400 }
401
402 for (const PtrWatchIntegral &watchIntegral : watchIntegrals()) {
403 watchIntegral->exportIntegralData(exp.time);
404 }
405 }
406}
407
408// private
409
411{
412 PRECICE_CHECK(_meshContexts.count(mesh) == 0,
413 "Mesh \"{} cannot be used twice by participant {}. "
414 "Please remove one of the provide/receive-mesh nodes with name=\"{}\"./>",
415 mesh, _name, mesh);
416}
417
419{
420 PRECICE_CHECK(!isDataWrite(mesh, data) && !isDataRead(mesh, data),
421 "ParticipantState \"{}\" can read/write data \"{}\" from/to mesh \"{}\" only once. "
422 "Please remove any duplicate instances of write-data/read-data nodes.",
423 _name, mesh, data);
424}
425
427{
430
431 if (_meshContexts.size() == 1) {
432 return " This participant only knows mesh \"" + _meshContexts.begin()->first + "\".";
433 }
434
435 auto matches = utils::computeMatches(mesh, _meshContexts | boost::adaptors::map_keys);
436 if (matches.front().distance < 3) {
437 return " Did you mean mesh \"" + matches.front().name + "\"?";
438 } else {
439 return fmt::format(" Available meshes are: {}", fmt::join(_meshContexts | boost::adaptors::map_keys, ", "));
440 }
441}
442
444{
446 PRECICE_ASSERT(!hasData(mesh, data));
448
449 // Is there such data in other meshes?
450 std::vector<std::string> otherMeshes;
451 for (const auto &[_, mc] : _meshContexts) {
452 if (mc->mesh->hasDataName(data)) {
453 return " Did you mean the data of mesh \"" + mc->mesh->getName() + "\"?";
454 }
455 }
456
457 // Is there other data in the given mesh?
458 auto localData = meshContext(mesh).mesh->availableData();
459
460 if (localData.size() == 1) {
461 return " This mesh only knows data \"" + localData.front() + "\".";
462 }
463
464 // Was the data typoed?
465 auto matches = utils::computeMatches(data, localData);
466 if (matches.front().distance < 3) {
467 return " Did you mean data \"" + matches.front().name + "\"?";
468 }
469
470 return fmt::format(" Available data are: {}", fmt::join(localData, ", "));
471}
472
474{
475 if (mappingType == "write") {
476 for (auto &context : writeDataContexts()) {
477 context.initializeMappingDataCache();
478 }
479 } else {
480 for (auto &context : readDataContexts()) {
481 context.initializeMappingDataCache();
482 }
483 }
484}
485
487{
488 meshContext(fromMesh).meshRequirement = std::max(meshContext(fromMesh).meshRequirement, requirement);
489 meshContext(fromMesh).fromMappingContexts.push_back(mappingContext);
490}
491
493{
494 meshContext(toMesh).toMappingContexts.push_back(mappingContext);
495 meshContext(toMesh).meshRequirement = std::max(meshContext(toMesh).meshRequirement, requirement);
496}
497
498} // namespace precice::impl
#define PRECICE_DEBUG(...)
Definition LogMacros.hpp:61
#define PRECICE_TRACE(...)
Definition LogMacros.hpp:92
#define PRECICE_CHECK(check,...)
Definition LogMacros.hpp:32
T any_of(T... args)
#define PRECICE_ASSERT(...)
Definition assertion.hpp:85
bool isMeshReceived(std::string_view mesh) const
Is a mesh with this name received by this participant?
std::string hintForMeshData(std::string_view mesh, std::string_view data) const
const std::vector< MeshContext * > & usedMeshContexts() const
bool isDataUsed(std::string_view mesh, std::string_view data) const
Is the data used by this participant?
bool isMeshUsed(std::string_view mesh) const
Is a mesh with this name used by this participant?
std::vector< io::ExportContext > _exportContexts
Export contexts to export meshes, data, and more.
void receiveMesh(const mesh::PtrMesh &mesh, const std::string &fromParticipant, double safetyFactor, partition::ReceivedPartition::GeometricFilter geoFilter, const bool allowDirectAccess)
Adds a mesh to be received by the participant.
MeshMap< MeshContext * > _meshContexts
All mesh contexts involved in a simulation.
bool isDataWrite(std::string_view mesh, std::string_view data) const
Is the participant allowed to write the data?
const ReadDataContext & readDataContext(std::string_view mesh, std::string_view data) const
DataMap< ReadDataContext > _readDataContexts
void addAction(action::PtrAction &&action)
Adds a configured Action to the participant.
bool isDataRead(std::string_view mesh, std::string_view data) const
Is the participant allowed to read the data?
std::string hintForMesh(std::string_view mesh) const
std::vector< PtrWatchPoint > & watchPoints()
Provided access to all WatchPoints.
std::vector< MeshContext * > _usedMeshContexts
Mesh contexts used by the participant.
const MeshContext & meshContext(std::string_view mesh) const
Mesh queries.
void checkDuplicatedData(std::string_view mesh, std::string_view data)
void addWatchPoint(const PtrWatchPoint &watchPoint)
Adds a configured WatchPoint to the ParticipantState.
std::vector< MappingContext > _readMappingContexts
Read mapping contexts used by the participant.
void addWriteMappingContext(const MappingContext &mappingContext)
Adds a configured write Mapping to the ParticipantState.
ParticipantState(std::string name, mesh::PtrMeshConfiguration &meshConfig)
Constructor.
std::vector< action::PtrAction > _actions
bool hasExports() const
Returns true, if the participant has any exports enabled.
std::vector< action::PtrAction > & actions()
Provided access to all Action.
std::vector< PtrWatchIntegral > _watchIntegrals
bool hasWriteMappings() const
Returns true, if the participant has at least one write mapping.
void setUsePrimaryRank(bool useIntraComm)
Sets weather the participant was configured with a primary tag.
void exportIntermediate(IntermediateExport exp)
Exports timewindows and iterations of meshes and watchpoints.
mesh::PtrMesh findMesh(std::string_view data) const
Returns the mesh associated with ReadDataContext with given data name in _readDataContexts of this Pa...
void addReadMappingContext(const MappingContext &mappingContext)
Adds a configured read Mapping to the ParticipantState.
bool hasMesh(std::string_view mesh) const
Does preCICE know a mesh with this name?
DataMap< WriteDataContext > _writeDataContexts
void addWatchIntegral(const PtrWatchIntegral &watchIntegral)
Adds a configured WatchIntegral to the ParticipantState.
std::vector< MappingContext > _writeMappingContexts
Write mapping contexts used by the participant.
bool hasData(std::string_view mesh, std::string_view data) const
Is the dataID know to preCICE?
void configureOutputMeshContext(std::string_view toMesh, impl::MappingContext &mappingContext, mapping::Mapping::MeshRequirement requirement)
Configures the mesh context with connectivity requirements and adds it to the mappingcontext.
void addWriteData(const mesh::PtrData &data, const mesh::PtrMesh &mesh)
void addReadData(const mesh::PtrData &data, const mesh::PtrMesh &mesh)
Adds a configured read Data to the ParticipantState.
const std::string & getName() const
std::vector< PtrWatchPoint > _watchPoints
bool isDirectAccessAllowed(std::string_view mesh) const
void addExportContext(const io::ExportContext &context)
Adds a configured ExportContext to export meshes and data.
bool useIntraComm() const
Returns true, if the participant uses a primary tag.
bool isMeshProvided(std::string_view mesh) const
Is a mesh with this name provided by this participant?
void configureInputMeshContext(std::string_view fromMesh, impl::MappingContext &mappingContext, mapping::Mapping::MeshRequirement requirement)
Configures the mesh context with connectivity requirements and adds it to the mappingcontext.
std::vector< MappingContext > & writeMappingContexts()
Provided access to all write MappingContext.
const std::vector< io::ExportContext > & exportContexts() const
Returns all ExportContext for exporting meshes and data.
bool hasReadMappings() const
Returns true, if the participant has at least one read mapping.
std::vector< MappingContext > & readMappingContexts()
Provided access to all read MappingContext.
void provideMesh(const mesh::PtrMesh &mesh)
Adds a mesh to be provided by the participant.
const WriteDataContext & writeDataContext(std::string_view mesh, std::string_view data) const
MeshContext & usedMeshContext(std::string_view name)
void initializeMappingDataCache(std::string_view mappingType)
Initializes the MappingDataCache in the DataContext after having computed the mappings.
void checkDuplicatedUse(std::string_view mesh)
std::vector< PtrWatchIntegral > & watchIntegrals()
Provided access to all WatchIntegrals.
Stores one Data object with related mesh. Context stores data to be read from and potentially provide...
Stores one Data object with related mesh. Context stores data to be written to and potentially provid...
MeshRequirement
Specifies requirements for the input and output meshes of a mapping.
Definition Mapping.hpp:46
GeometricFilter
Defines the type of geometric filter used.
T empty(T... args)
T find_if(T... args)
T max(T... args)
contains actions to modify exchanged data.
Definition Action.hpp:6
std::unique_ptr< Action > PtrAction
std::shared_ptr< WatchPoint > PtrWatchPoint
std::shared_ptr< WatchIntegral > PtrWatchIntegral
provides Mesh, Data and primitives.
std::shared_ptr< Data > PtrData
std::shared_ptr< Mesh > PtrMesh
std::shared_ptr< MeshConfiguration > PtrMeshConfiguration
std::vector< StringMatch > computeMatches(std::string_view given, const Container &expected)
Definition String.hpp:95
STL namespace.
Holds a data mapping and related information.
Stores a mesh and related objects and data.
Type that represent a compound key of two values.