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 -------------------------------------------------------------------------------
10 License
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"
34 
35 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
36 
37 namespace Foam
38 {
39 namespace functionObjects
40 {
41  defineTypeNameAndDebug(syncObjects, 0);
43  (
44  functionObject,
45  syncObjects,
46  dictionary
47  );
48 }
49 }
50 
51 
52 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
53 
54 Foam::functionObjects::syncObjects::syncObjects
55 (
56  const word& name,
57  const Time& runTime,
58  const dictionary& dict
59 )
60 :
62  obr_(runTime)
63 {
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
91  (
94  );
95 
96 
97  for (const int proci : pBufs.allProcs())
98  {
99  // Get database to send
100  const objectRegistry& sendObr = mappedPatchBase::subRegistry
101  (
102  obr_,
103  mappedPatchBase::sendPath(root_, proci)
104  );
105 
106  // Pack into dictionary
107  dictionary sendDataDict;
108  mappedPatchBase::writeDict(sendObr, sendDataDict);
109 
110  if (debug & 2)
111  {
112  Pout<< "** to processor " << proci
113  << " sendObr:" << sendObr.objectPath()
114  << " sending dictionary:" << sendDataDict << endl;
115  }
116  UOPstream os(proci, pBufs);
117  os << sendDataDict;
118  }
119 
120  // Start sending and receiving and block
121  pBufs.finishedSends();
122 
123  for (const int proci : pBufs.allProcs())
124  {
125  // Get database to receive data into
126  const objectRegistry& receiveObr = mappedPatchBase::subRegistry
127  (
128  obr_,
129  mappedPatchBase::receivePath(root_, proci)
130  );
131  UIPstream is(proci, pBufs);
132  const dictionary fromProcDict(is);
133  if (debug & 2)
134  {
135  Pout<< "** from processor " << proci
136  << " receiveObr:" << receiveObr.objectPath()
137  << " received dictionary:" << fromProcDict << endl;
138  }
140  (
141  fromProcDict,
142  const_cast<objectRegistry&>(receiveObr)
143  );
144  }
145 
146  //if (debug)
147  //{
148  // dictionary allDict;
149  // // Add send subdictionary
150  // dictionary& sendDict = allDict.subDictOrAdd("send");
151  // mappedPatchBase::writeDict
152  // (
153  // mappedPatchBase::subRegistry(obr_, "send"),
154  // sendDict
155  // );
156  // // Add receive subdictionary
157  // dictionary& receiveDict = allDict.subDictOrAdd("receive");
158  // mappedPatchBase::writeDict
159  // (
160  // mappedPatchBase::subRegistry(obr_, "receive"),
161  // receiveDict
162  // );
163  // Pout<< type() << " : after synchronisation:" << allDict << endl;
164  //}
165 
166  // Restore communicator settings
167  UPstream::commWarn(oldWarnComm);
168 }
169 
170 
172 {
173  if (debug)
174  {
175  Pout<< type() << " : read(const dictionary&)" << endl;
176  }
177 
179  root_ = dict.getOrDefault<fileName>("root", fileName::null);
180 
181  if (debug)
182  {
183  Pout<< type() << " : root:" << root_ << endl;
184  }
185 
186  // Make sure that at startup we're doing a sync (execute below only gets
187  // called at end of timeloop)
188  sync();
189 
190  return true;
191 }
192 
193 
195 {
196  if (debug)
197  {
198  Pout<< type() << " : execute()" << endl;
199  }
201  sync();
202 
203  return true;
204 }
205 
206 
208 {
209  if (debug)
210  {
211  Pout<< type() << " : write()" << endl;
212  }
213 
214  return true;
215 }
216 
217 
218 // ************************************************************************* //
dictionary dict
defineTypeNameAndDebug(ObukhovLength, 0)
A class for handling file names.
Definition: fileName.H:72
A list of keyword definitions, which are a keyword followed by a number of values (eg...
Definition: dictionary.H:129
static const fileName null
An empty fileName.
Definition: fileName.H:111
static fileName sendPath(const fileName &root, const label proci)
Helper: return path to store data to be sent to processor i.
engineTime & runTime
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:531
static bool & parRun() noexcept
Test if this a parallel run.
Definition: UPstream.H:1049
static 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...
void sync()
Do all: synchronise all IOFields and objectRegistry.
Definition: syncObjects.C:63
Abstract base-class for Time/database function objects.
Class to control time during OpenFOAM simulations that is also the top-level objectRegistry.
Definition: Time.H:69
Macros for easy insertion into run-time selection tables.
static label commWarn(const label communicator) noexcept
Alter communicator debugging setting. Warns for use of any communicator differing from specified...
Definition: UPstream.H:449
static void writeDict(const objectRegistry &obr, dictionary &dict)
Convert objectRegistry contents into dictionary.
fileName::Type type(const fileName &name, const bool followLink=true)
Return the file type: DIRECTORY or FILE, normally following symbolic links.
Definition: POSIX.C:799
static fileName receivePath(const fileName &root, const label proci)
Helper: return path to store data to be received from processor i.
word name(const expressions::valueTypeCode typeCode)
A word representation of a valueTypeCode. Empty for expressions::valueTypeCode::INVALID.
Definition: exprTraits.C:127
A class for handling words, derived from Foam::string.
Definition: word.H:63
virtual bool execute()
Do nothing.
Definition: syncObjects.C:187
virtual const word & type() const =0
Runtime type information.
void read(Istream &, label &val, const dictionary &)
In-place read with dictionary lookup.
int debug
Static debugging option.
OBJstream os(runTime.globalPath()/outputName)
addToRunTimeSelectionTable(functionObject, ObukhovLength, dictionary)
virtual bool read(const dictionary &)
Read the syncObjects data.
Definition: syncObjects.C:164
virtual bool read(const dictionary &dict)
Read and set the function object if its data have changed.
static void readDict(const dictionary &d, objectRegistry &obr)
(recursively) construct and register IOFields from dictionary
"nonBlocking" : (MPI_Isend, MPI_Irecv)
prefixOSstream Pout
OSstream wrapped stdout (std::cout) with parallel prefix.
Namespace for OpenFOAM.
virtual bool write()
Write the registered objects.
Definition: syncObjects.C:200
static int debug
Flag to execute debug content.
static constexpr label commGlobal() noexcept
Communicator for all ranks, irrespective of any local worlds.
Definition: UPstream.H:419