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-2022 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::warnComm;
85 
86 
87  // Send my data to all other processors
88  // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
89 
90  // Note provision of explicit all-world communicator
91  PstreamBuffers pBufs
92  (
95  0
96  );
97 
98 
99  for (const int proci : pBufs.allProcs())
100  {
101  // Get database to send
102  const objectRegistry& sendObr = mappedPatchBase::subRegistry
103  (
104  obr_,
105  mappedPatchBase::sendPath(root_, proci)
106  );
107 
108  // Pack into dictionary
109  dictionary sendDataDict;
110  mappedPatchBase::writeDict(sendObr, sendDataDict);
111 
112  if (debug & 2)
113  {
114  Pout<< "** to processor " << proci
115  << " sendObr:" << sendObr.objectPath()
116  << " sending dictionary:" << sendDataDict << endl;
117  }
118  UOPstream os(proci, pBufs);
119  os << sendDataDict;
120  }
121 
122  // Start sending and receiving and block
123  pBufs.finishedSends();
124 
125  for (const int proci : pBufs.allProcs())
126  {
127  // Get database to receive data into
128  const objectRegistry& receiveObr = mappedPatchBase::subRegistry
129  (
130  obr_,
131  mappedPatchBase::receivePath(root_, proci)
132  );
133  UIPstream is(proci, pBufs);
134  const dictionary fromProcDict(is);
135  if (debug & 2)
136  {
137  Pout<< "** from processor " << proci
138  << " receiveObr:" << receiveObr.objectPath()
139  << " received dictionary:" << fromProcDict << endl;
140  }
142  (
143  fromProcDict,
144  const_cast<objectRegistry&>(receiveObr)
145  );
146  }
147 
148  //if (debug)
149  //{
150  // dictionary allDict;
151  // // Add send subdictionary
152  // dictionary& sendDict = allDict.subDictOrAdd("send");
153  // mappedPatchBase::writeDict
154  // (
155  // mappedPatchBase::subRegistry(obr_, "send"),
156  // sendDict
157  // );
158  // // Add receive subdictionary
159  // dictionary& receiveDict = allDict.subDictOrAdd("receive");
160  // mappedPatchBase::writeDict
161  // (
162  // mappedPatchBase::subRegistry(obr_, "receive"),
163  // receiveDict
164  // );
165  // Pout<< type() << " : after synchronisation:" << allDict << endl;
166  //}
167 
168  UPstream::warnComm = oldWarnComm;
169 }
170 
171 
173 {
174  if (debug)
175  {
176  Pout<< type() << " : read(const dictionary&)" << endl;
177  }
178 
180  root_ = dict.getOrDefault<fileName>("root", fileName::null);
181 
182  if (debug)
183  {
184  Pout<< type() << " : root:" << root_ << endl;
185  }
186 
187  // Make sure that at startup we're doing a sync (execute below only gets
188  // called at end of timeloop)
189  sync();
190 
191  return true;
192 }
193 
194 
196 {
197  if (debug)
198  {
199  Pout<< type() << " : execute()" << endl;
200  }
202  sync();
203 
204  return true;
205 }
206 
207 
209 {
210  if (debug)
211  {
212  Pout<< type() << " : write()" << endl;
213  }
214 
215  return true;
216 }
217 
218 
219 // ************************************************************************* //
dictionary dict
defineTypeNameAndDebug(ObukhovLength, 0)
A class for handling file names.
Definition: fileName.H:71
A list of keyword definitions, which are a keyword followed by a number of values (eg...
Definition: dictionary.H:120
static const fileName null
An empty fileName.
Definition: fileName.H:110
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:487
static bool & parRun() noexcept
Test if this a parallel run.
Definition: UPstream.H:639
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
static int & msgType() noexcept
Message tag of standard messages.
Definition: UPstream.H:806
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 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:752
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 INVALID.
Definition: exprTraits.C:52
A class for handling words, derived from Foam::string.
Definition: word.H:63
virtual bool execute()
Do nothing.
Definition: syncObjects.C:188
virtual const word & type() const =0
Runtime type information.
static label warnComm
Debugging: warn for use of any communicator differing from warnComm.
Definition: UPstream.H:366
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:165
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:201
static int debug
Flag to execute debug content.