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(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 // ************************************************************************* //
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:1061
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:461
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:183
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:160
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
prefixOSstream Pout
OSstream wrapped stdout (std::cout) with parallel prefix.
Namespace for OpenFOAM.
virtual bool write()
Write the registered objects.
Definition: syncObjects.C:196
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:431