preCICE
Loading...
Searching...
No Matches
ParticipantState.hpp
Go to the documentation of this file.
1#pragma once
2
3#include <boost/range/adaptor/map.hpp>
4#include <cmath>
5#include <deque>
6#include <memory>
7#include <stddef.h>
8#include <string>
9#include <string_view>
10#include <utility>
11#include <variant>
12#include <vector>
13
14#include "SharedPointer.hpp"
17#include "io/ExportContext.hpp"
19#include "logging/Logger.hpp"
29#include "utils/IntraComm.hpp"
31
32namespace precice::impl {
33
35using MeshContextVariant = std::variant<ProvidedMeshContext *, ReceivedMeshContext *>;
36
39{
40 return std::visit([](auto *p) -> MeshContext * { return p; }, variant);
41}
42
44inline mesh::Mesh &getMesh(const MeshContextVariant &variant)
45{
46 return std::visit([](auto *p) -> mesh::Mesh & { return *p->mesh; }, variant);
47}
48
51{
52 return std::visit([](auto *p) -> partition::Partition & { return *p->partition; }, variant);
53}
54
55struct MappingContext;
56} // namespace precice::impl
57
58// Forward declaration to friend the boost test struct
59
61struct TestConfigurationPeano;
62struct TestConfigurationComsol;
63} // namespace Integration::Serial::Whitebox
64
65namespace precice {
66namespace utils {
67class ManageUniqueIDs;
68} // namespace utils
69
70namespace impl {
71
73template <typename T>
77 template <typename Other>
78 bool operator<(const MeshDataKey<Other> &other) const
79 {
80 if (mesh < other.mesh) {
81 return true;
82 }
83 if (other.mesh < mesh) {
84 return false;
85 }
86 return data < other.data;
87 }
88};
89
91template <class T>
93
96public:
102
109 std::string name,
110 mesh::PtrMeshConfiguration &meshConfig);
111
115 void addWriteData(
116 const mesh::PtrData &data,
117 const mesh::PtrMesh &mesh);
118
120 void addReadData(
121 const mesh::PtrData &data,
122 const mesh::PtrMesh &mesh);
123
125 void addReadMappingContext(const MappingContext &mappingContext);
126
128 void addWriteMappingContext(const MappingContext &mappingContext);
129
131 void addWatchPoint(const PtrWatchPoint &watchPoint);
132
134 void addWatchIntegral(const PtrWatchIntegral &watchIntegral);
135
138
141
143 void addExportContext(const io::ExportContext &context);
144
147
150 const std::string &fromParticipant,
151 double safetyFactor,
153 const bool allowDirectAccess);
155
158
161 const ReadDataContext &readDataContext(std::string_view mesh, std::string_view data) const;
162
166 ReadDataContext &readDataContext(std::string_view mesh, std::string_view data);
167
174 mesh::PtrMesh findMesh(std::string_view data) const;
175
179 const WriteDataContext &writeDataContext(std::string_view mesh, std::string_view data) const;
180
184 WriteDataContext &writeDataContext(std::string_view mesh, std::string_view data);
185
190 {
191 return _writeDataContexts | boost::adaptors::map_values;
192 }
193
198 {
199 return _readDataContexts | boost::adaptors::map_values;
200 }
201
203 bool hasData(std::string_view mesh, std::string_view data) const;
204
206 bool isDataUsed(std::string_view mesh, std::string_view data) const;
207
209 bool isDataRead(std::string_view mesh, std::string_view data) const;
210
212 bool isDataWrite(std::string_view mesh, std::string_view data) const;
214
217 /*** Provides direct access to a \ref MeshContext given the \ref meshid
218 * @param[in] meshID the id of the \ref Mesh
219 * @returns a reference to the matching \ref MeshContext
220 * @pre the \ref Mesh with \ref meshID is used by the ParticipantState
221 */
222 const MeshContext &meshContext(std::string_view mesh) const;
223
224 /*** Provides direct access to a \ref MeshContext given the \ref meshid
225 * @param[in] meshID the id of the \ref Mesh
226 * @returns a reference to the matching \ref MeshContext
227 * @pre the \ref Mesh with \ref meshID is used by the ParticipantState
228 */
229 MeshContext &meshContext(std::string_view mesh);
230
234 const std::vector<MeshContextVariant> &usedMeshContexts() const;
235
239 std::vector<MeshContextVariant> &usedMeshContexts();
240
242 bool hasMesh(std::string_view mesh) const;
243
245 bool isMeshUsed(std::string_view mesh) const;
246
248 bool isMeshProvided(std::string_view mesh) const;
249
251 bool isMeshReceived(std::string_view mesh) const;
252
255 bool isDirectAccessAllowed(std::string_view mesh) const;
256
263
269 const ReceivedMeshContext &receivedMeshContext(std::string_view mesh) const;
270
277
283 const ProvidedMeshContext &providedMeshContext(std::string_view mesh) const;
284
288 std::deque<ProvidedMeshContext> &providedMeshContexts();
289
293 const std::deque<ProvidedMeshContext> &providedMeshContexts() const;
294
298 std::deque<ReceivedMeshContext> &receivedMeshContexts();
299
303 const std::deque<ReceivedMeshContext> &receivedMeshContexts() const;
305
307 void initializeMappingDataCache(std::string_view mappingType);
308
310 void configureInputMeshContext(std::string_view fromMesh, impl::MappingContext &mappingContext, mapping::Mapping::MeshRequirement requirement);
311
313 void configureOutputMeshContext(std::string_view toMesh, impl::MappingContext &mappingContext, mapping::Mapping::MeshRequirement requirement);
314
318 void exportInitial();
319
322 size_t iteration;
323 double time;
325 bool final;
326 };
327
330
332
336 const std::string &getName() const;
337
339 bool useIntraComm() const;
340
342 bool hasReadMappings() const;
343
345 bool hasWriteMappings() const;
346
348 std::vector<MappingContext> &readMappingContexts();
349
351 std::vector<MappingContext> &writeMappingContexts();
352
354 std::vector<PtrWatchPoint> &watchPoints();
355
357 std::vector<PtrWatchIntegral> &watchIntegrals();
358
360 std::vector<action::PtrAction> &actions();
361
363 const std::vector<action::PtrAction> &actions() const;
364
366 const std::vector<io::ExportContext> &exportContexts() const;
367
369 bool hasExports() const;
371
374 std::string hintForMesh(std::string_view mesh) const;
375 std::string hintForMeshData(std::string_view mesh, std::string_view data) const;
377
378private:
379 mutable logging::Logger _log{"impl::ParticipantState"};
380
381 std::string _name;
382
383 std::vector<PtrWatchPoint> _watchPoints;
384
385 std::vector<PtrWatchIntegral> _watchIntegrals;
386
388 std::vector<io::ExportContext> _exportContexts;
389
390 std::vector<action::PtrAction> _actions;
391
392 template <typename T>
393 using MeshMap = std::map<std::string, T, std::less<>>;
394
395 template <typename T>
396 using DataMap = std::map<MeshDataKey<std::string>, T, std::less<>>;
397
400
402 std::deque<ProvidedMeshContext> _providedMeshContexts;
403
405 std::deque<ReceivedMeshContext> _receivedMeshContexts;
406
408 std::vector<MappingContext> _readMappingContexts;
409
411 std::vector<MappingContext> _writeMappingContexts;
412
414 std::vector<MeshContextVariant> _usedMeshContexts;
415
417
419
420 bool _useIntraComm = false;
421
422 template <typename ELEMENT_T>
423 bool isDataValid(
424 const std::vector<ELEMENT_T> &data,
425 const ELEMENT_T &newElement) const;
426
427 void checkDuplicatedUse(std::string_view mesh);
428
429 void checkDuplicatedData(std::string_view mesh, std::string_view data);
430
434};
435
436// --------------------------------------------------------- HEADER DEFINITIONS
437
438template <typename ELEMENT_T>
440 const std::vector<ELEMENT_T> &data,
441 const ELEMENT_T &newElement) const
442{
443 for (size_t i = 0; i < data.size(); i++) {
444 if (data[i].name == newElement.name) {
445 return false;
446 }
447 }
448 return true;
449}
450
451} // namespace impl
452} // namespace precice
bool isMeshReceived(std::string_view mesh) const
Is a mesh with this name received by this participant?
ProvidedMeshContext & providedMeshContext(std::string_view mesh)
std::string hintForMeshData(std::string_view mesh, std::string_view data) 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.
MeshMap< MeshContext * > _meshContexts
All mesh contexts involved in a simulation (for name-based lookup)
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.
const MeshContext & meshContext(std::string_view mesh) const
Mesh queries.
std::map< std::string, T, std::less<> > MeshMap
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.
std::deque< ProvidedMeshContext > & providedMeshContexts()
ParticipantState(std::string name, mesh::PtrMeshConfiguration &meshConfig)
Constructor.
std::vector< action::PtrAction > _actions
std::vector< MeshContextVariant > _usedMeshContexts
Mesh contexts used by the participant.
bool isDataValid(const std::vector< ELEMENT_T > &data, const ELEMENT_T &newElement) const
bool hasExports() const
Returns true, if the participant has any exports enabled.
std::vector< action::PtrAction > & actions()
Provided access to all Action.
friend struct Integration::Serial::Whitebox::TestConfigurationPeano
To allow white box tests.
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.
ReceivedMeshContext & receivedMeshContext(std::string_view mesh)
std::deque< ProvidedMeshContext > _providedMeshContexts
Provided mesh contexts (owning container)
void provideMesh(mesh::PtrMesh mesh)
Adds a mesh to be provided by the participant.
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
friend struct Integration::Serial::Whitebox::TestConfigurationComsol
void addWatchIntegral(const PtrWatchIntegral &watchIntegral)
Adds a configured WatchIntegral to the ParticipantState.
std::vector< MappingContext > _writeMappingContexts
Write mapping contexts used by the participant.
void receiveMesh(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.
const std::vector< MeshContextVariant > & usedMeshContexts() const
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::deque< ReceivedMeshContext > _receivedMeshContexts
Received mesh contexts (owning container)
std::vector< PtrWatchPoint > _watchPoints
std::map< MeshDataKey< std::string >, T, std::less<> > DataMap
bool isDirectAccessAllowed(std::string_view mesh) const
void addExportContext(const io::ExportContext &context)
Adds a configured ExportContext to export meshes and data.
std::deque< ReceivedMeshContext > & receivedMeshContexts()
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.
const WriteDataContext & writeDataContext(std::string_view mesh, std::string_view data) const
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...
This class provides a lightweight logger.
Definition Logger.hpp:17
MeshRequirement
Specifies requirements for the input and output meshes of a mapping.
Definition Mapping.hpp:46
Container and creator for meshes.
Definition Mesh.hpp:38
Abstract base class for partitions.
Definition Partition.hpp:28
GeometricFilter
Defines the type of geometric filter used.
Manages a set of unique IDs.
contains actions to modify exchanged data.
Definition Action.hpp:6
std::unique_ptr< Action > PtrAction
std::shared_ptr< WatchPoint > PtrWatchPoint
std::variant< ProvidedMeshContext *, ReceivedMeshContext * > MeshContextVariant
Type alias for variant holding either provided or received mesh context pointers.
MeshDataKey(T, T) -> MeshDataKey< T >
Deduction guide for two identical parameter types.
MeshContext * getMeshContext(const MeshContextVariant &variant)
Helper to extract base MeshContext pointer from variant.
partition::Partition & getPartition(const MeshContextVariant &variant)
Helper to extract partition from variant.
mesh::Mesh & getMesh(const MeshContextVariant &variant)
Helper to extract mesh from variant.
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
contains precice-related utilities.
Main namespace of the precice library.
Holds a data mapping and related information.
Type that represent a compound key of two values.
bool operator<(const MeshDataKey< Other > &other) const
Context for a mesh provided by this participant.
Context for a mesh received from another participant.