Loading...
Searching...
No Matches
syncObjects.C
Go to the documentation of this file.
1/*---------------------------------------------------------------------------*\
2 ========= |
3 \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
4 \\ / O peration |
5 \\ / A nd | www.openfoam.com
6 \\/ M anipulation |
7-------------------------------------------------------------------------------
8 Copyright (C) 2020-2023 OpenCFD Ltd.
9-------------------------------------------------------------------------------
10License
11 This file is part of OpenFOAM.
12
13 OpenFOAM is free software: you can redistribute it and/or modify it
14 under the terms of the GNU General Public License as published by
15 the Free Software Foundation, either version 3 of the License, or
16 (at your option) any later version.
17
18 OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
19 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
20 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
21 for more details.
22
23 You should have received a copy of the GNU General Public License
24 along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
25
26\*---------------------------------------------------------------------------*/
27
28#include "syncObjects.H"
29#include "Time.H"
30#include "polyMesh.H"
32#include "objectRegistry.H"
33#include "mappedPatchBase.H"
35// * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
36
37namespace Foam
38{
39namespace functionObjects
40{
43 (
47 );
48}
49}
50
51
52// * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
53
54Foam::functionObjects::syncObjects::syncObjects
55(
56 const word& name,
57 const Time& runTime,
58 const dictionary& dict
59)
60:
62 obr_(runTime)
64 read(dict);
65}
66
67
68// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
69
71{
72 if (debug)
73 {
74 Pout<< type() << " : sync()"
75 << " root:" << root_ << endl;
76 }
77
78 if (!Pstream::parRun())
79 {
80 return;
81 }
82
83 const label oldWarnComm = UPstream::commWarn(UPstream::commGlobal());
84
85
86 // Send my data to all other processors
87 // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
88
89 // Note provision of explicit all-world communicator
90 PstreamBuffers pBufs(UPstream::commGlobal());
91
92
93 for (const int proci : pBufs.allProcs())
94 {
95 // Get database to send
96 const objectRegistry& sendObr = mappedPatchBase::subRegistry
97 (
98 obr_,
99 mappedPatchBase::sendPath(root_, proci)
100 );
101
102 // Pack into dictionary
103 dictionary sendDataDict;
104 mappedPatchBase::writeDict(sendObr, sendDataDict);
105
106 if (debug & 2)
107 {
108 Pout<< "** to processor " << proci
109 << " sendObr:" << sendObr.objectPath()
110 << " sending dictionary:" << sendDataDict << endl;
111 }
112 UOPstream os(proci, pBufs);
113 os << sendDataDict;
114 }
115
116 // Start sending and receiving and block
117 pBufs.finishedSends();
118
119 for (const int proci : pBufs.allProcs())
120 {
121 // Get database to receive data into
122 const objectRegistry& receiveObr = mappedPatchBase::subRegistry
123 (
124 obr_,
125 mappedPatchBase::receivePath(root_, proci)
126 );
127 UIPstream is(proci, pBufs);
128 const dictionary fromProcDict(is);
129 if (debug & 2)
130 {
131 Pout<< "** from processor " << proci
132 << " receiveObr:" << receiveObr.objectPath()
133 << " received dictionary:" << fromProcDict << endl;
134 }
136 (
137 fromProcDict,
138 const_cast<objectRegistry&>(receiveObr)
139 );
140 }
141
142 //if (debug)
143 //{
144 // dictionary allDict;
145 // // Add send subdictionary
146 // dictionary& sendDict = allDict.subDictOrAdd("send");
147 // mappedPatchBase::writeDict
148 // (
149 // mappedPatchBase::subRegistry(obr_, "send"),
150 // sendDict
151 // );
152 // // Add receive subdictionary
153 // dictionary& receiveDict = allDict.subDictOrAdd("receive");
154 // mappedPatchBase::writeDict
155 // (
156 // mappedPatchBase::subRegistry(obr_, "receive"),
157 // receiveDict
158 // );
159 // Pout<< type() << " : after synchronisation:" << allDict << endl;
160 //}
161
162 // Restore communicator settings
163 UPstream::commWarn(oldWarnComm);
164}
165
166
168{
169 if (debug)
170 {
171 Pout<< type() << " : read(const dictionary&)" << endl;
172 }
173
175 root_ = dict.getOrDefault<fileName>("root", fileName::null);
176
177 if (debug)
178 {
179 Pout<< type() << " : root:" << root_ << endl;
180 }
181
182 // Make sure that at startup we're doing a sync (execute below only gets
183 // called at end of timeloop)
184 sync();
185
186 return true;
187}
188
189
191{
192 if (debug)
193 {
194 Pout<< type() << " : execute()" << endl;
195 }
197 sync();
198
199 return true;
200}
201
202
204{
205 if (debug)
206 {
207 Pout<< type() << " : write()" << endl;
208 }
209
210 return true;
211}
212
213
214// ************************************************************************* //
Macros for easy insertion into run-time selection tables.
#define addToRunTimeSelectionTable(baseType, thisType, argNames)
Add to construction table with typeName as the key.
fileName objectPath() const
The complete path + object name.
Definition IOobjectI.H:313
Buffers for inter-processor communications streams (UOPstream, UIPstream).
UPstream::rangeType allProcs() const noexcept
Range of ranks indices associated with PstreamBuffers.
void finishedSends(const bool wait=true)
Mark the send phase as being finished.
Class to control time during OpenFOAM simulations that is also the top-level objectRegistry.
Definition Time.H:75
Input inter-processor communications stream using MPI send/recv etc. - operating on external buffer.
Definition UIPstream.H:313
Output inter-processor communications stream using MPI send/recv etc. - operating on external buffer.
Definition UOPstream.H:408
static bool parRun(const bool on) noexcept
Set as parallel run on/off.
Definition UPstream.H:1669
static label commWarn(const label communicator) noexcept
Alter communicator debugging setting. Warns for use of any communicator differing from specified....
Definition UPstream.H:1122
static constexpr int commGlobal() noexcept
Communicator for all ranks, irrespective of any local worlds.
Definition UPstream.H:1081
A list of keyword definitions, which are a keyword followed by a number of values (eg,...
Definition dictionary.H:133
A class for handling file names.
Definition fileName.H:75
static const fileName null
An empty fileName.
Definition fileName.H:111
Abstract base-class for Time/database function objects.
const word & name() const noexcept
Return the name of this functionObject.
virtual bool read(const dictionary &dict)
Read and set the function object if its data have changed.
static int debug
Flag to execute debug content.
virtual const word & type() const =0
Runtime type information.
functionObject(const word &name, const bool withNamePrefix=defaultUseNamePrefix)
Construct from components.
Copies content of local objectRegistry to all processors.
void sync()
Do all: synchronise all IOFields and objectRegistry.
Definition syncObjects.C:63
virtual bool execute()
Execute the function-object operations (no-op).
virtual bool write()
Write the function-object results.
virtual bool read(const dictionary &)
Read the function-object dictionary.
static FOAM_NO_DANGLING_REFERENCE const objectRegistry & subRegistry(const objectRegistry &obr, const wordList &names, const label index)
Lookup (sub)objectRegistry by following names of sub registries. Creates non-existing intermediate on...
static void readDict(const dictionary &d, objectRegistry &obr)
(recursively) construct and register IOFields from dictionary
static fileName sendPath(const fileName &root, const label proci)
Helper: return path to store data to be sent to processor i.
static fileName receivePath(const fileName &root, const label proci)
Helper: return path to store data to be received from processor i.
static void writeDict(const objectRegistry &obr, dictionary &dict)
Convert objectRegistry contents into dictionary.
Registry of regIOobjects.
A class for handling words, derived from Foam::string.
Definition word.H:66
#define defineTypeNameAndDebug(Type, DebugSwitch)
Define the typeName and debug information.
Definition className.H:142
engineTime & runTime
OBJstream os(runTime.globalPath()/outputName)
Namespace for handling debugging switches.
Definition debug.C:45
Function objects are OpenFOAM utilities to ease workflow configurations and enhance workflows.
Namespace for OpenFOAM.
fileName::Type type(const fileName &name, const bool followLink=true)
Return the file type: DIRECTORY or FILE, normally following symbolic links.
Definition POSIX.C:801
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition Ostream.H:519
word name(const expressions::valueTypeCode typeCode)
A word representation of a valueTypeCode. Empty for expressions::valueTypeCode::INVALID.
Definition exprTraits.C:127
prefixOSstream Pout
OSstream wrapped stdout (std::cout) with parallel prefix.
dictionary dict