mapDistributeBaseIO.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) 2022-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 "mapDistributeBase.H"
29 #include "dictionary.H"
30 
31 // * * * * * * * * * * * * * * * Local Functions * * * * * * * * * * * * * * //
32 
33 namespace Foam
34 {
35 
36 // The maps (labelListList) are not human-modifiable but if we need to
37 // inspect them in ASCII, it is much more convenient if each sub-list
38 // is flattened on a single line.
39 static Ostream& printMaps(Ostream& os, const labelListList& maps)
40 {
41  if (os.format() == IOstreamOption::BINARY || maps.empty())
42  {
43  os << maps;
44  }
45  else
46  {
47  os << nl << maps.size() << nl
48  << token::BEGIN_LIST << nl;
49 
50  // Compact single-line output for each labelList
51  for (const labelList& map : maps)
52  {
53  map.writeList(os) << nl;
54  }
56  }
57 
58  return os;
59 }
60 
61 
62 static void writeMaps(Ostream& os, const word& key, const labelListList& maps)
63 {
64  if (os.format() == IOstreamOption::BINARY || maps.empty())
65  {
66  os.writeEntry(key, maps);
67  }
68  else
69  {
70  os << indent << key;
71  printMaps(os, maps);
72  os.endEntry();
73  }
74 }
75 
76 } // End namespace Foam
77 
78 
79 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
80 
82 (
83  const dictionary& dict,
84  const label comm
85 )
86 :
87  mapDistributeBase(comm)
88 {
89  mapDistributeBase::readDict(dict);
90 }
91 
92 
94 {
95  is >> *this;
96 }
97 
98 
99 // * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * * //
100 
102 {
103  constructSize_ = dict.get<label>("constructSize");
104 
105  // The subMap
106  {
107  const dictionary& subdict = dict.subDict("subMap");
108 
109  subdict.readEntry("flip", subHasFlip_);
110  subdict.readEntry("maps", subMap_);
111  }
112 
113  // The constructMap
114  {
115  const dictionary& subdict = dict.subDict("constructMap");
117  subdict.readEntry("flip", constructHasFlip_);
118  subdict.readEntry("maps", constructMap_);
119  }
120 }
121 
122 
123 void Foam::mapDistributeBase::writeEntries(Ostream& os) const
124 {
125  os.writeEntry("constructSize", constructSize_);
126 
127  os << nl;
128  os.beginBlock("subMap");
129  os.writeEntry("flip", subHasFlip_);
130  writeMaps(os, "maps", subMap_);
131  os.endBlock();
132 
133  os << nl;
134  os.beginBlock("constructMap");
135  os.writeEntry("flip", constructHasFlip_);
136  writeMaps(os, "maps", constructMap_);
137  os.endBlock();
138 }
139 
140 
141 // * * * * * * * * * * * * * * * IOstream Operators * * * * * * * * * * * * //
142 
143 Foam::Istream& Foam::operator>>(Istream& is, mapDistributeBase& map)
144 {
146 
147  is >> map.constructSize_
148  >> map.subMap_ >> map.constructMap_
149  >> map.subHasFlip_ >> map.constructHasFlip_
150  >> map.comm_;
151 
152  return is;
153 }
154 
155 
156 Foam::Ostream& Foam::operator<<(Ostream& os, const mapDistributeBase& map)
157 {
158  os << map.constructSize_ << token::NL;
159 
160  printMaps(os, map.subMap_) << token::NL;
161  printMaps(os, map.constructMap_) << token::NL;
162 
163  os << map.subHasFlip_ << token::SPACE
164  << map.constructHasFlip_ << token::SPACE
165  << map.comm_ << token::NL;
166 
167  return os;
168 }
169 
170 
171 template<>
172 Foam::Ostream& Foam::operator<<
173 (
174  Ostream& os,
175  const InfoProxy<mapDistributeBase>& iproxy
176 )
177 {
178  const auto& map = *iproxy;
179 
180  // Output as compact pseudo dictionary entries
181 
182  os.writeEntry("constructSize", map.constructSize());
183 
184  os << indent << "local { flip " << map.subHasFlip()
185  << "; sizes ";
186  map.subMapSizes().writeList(os) << "; }" << nl;
187 
188  os << indent << "remote { flip " << map.constructHasFlip()
189  << "; sizes ";
190  map.constructMapSizes().writeList(os) << "; }" << nl;
191 
192  return os;
193 }
194 
195 
196 // ************************************************************************* //
dictionary dict
void size(const label n)
Older name for setAddressableSize.
Definition: UList.H:116
Ostream & indent(Ostream &os)
Indent stream.
Definition: Ostream.H:493
A list of keyword definitions, which are a keyword followed by a number of values (eg...
Definition: dictionary.H:129
An Istream is an abstract base class for all input systems (streams, files, token lists etc)...
Definition: Istream.H:57
constexpr char nl
The newline &#39;\n&#39; character (0x0a)
Definition: Ostream.H:50
bool empty() const noexcept
True if List is empty (ie, size() is zero)
Definition: UList.H:666
Newline [isspace].
Definition: token.H:130
Begin list [isseparator].
Definition: token.H:161
Ostream & writeEntry(const keyType &key, const T &value)
Write a keyword/value entry.
Definition: Ostream.H:321
T get(const word &keyword, enum keyType::option matchOpt=keyType::REGEX) const
Find and return a T. FatalIOError if not found, or if the number of tokens is incorrect.
List< labelList > labelListList
List of labelList.
Definition: labelList.H:38
const dictionary & subDict(const word &keyword, enum keyType::option matchOpt=keyType::REGEX) const
Find and return a sub-dictionary.
Definition: dictionary.C:441
bool readEntry(const word &keyword, T &val, enum keyType::option matchOpt=keyType::REGEX, IOobjectOption::readOption readOpt=IOobjectOption::MUST_READ) const
Find entry and assign to T val. FatalIOError if it is found and the number of tokens is incorrect...
void readDict(const dictionary &dict)
Read entries from dictionary format.
Istream & operator>>(Istream &, directionInfo &)
virtual Ostream & endBlock()
Write end block group.
Definition: Ostream.C:108
Space [isspace].
Definition: token.H:131
End list [isseparator].
Definition: token.H:162
Class containing processor-to-processor mapping information.
An Ostream is an abstract base class for all output systems (streams, files, token lists...
Definition: Ostream.H:56
OBJstream os(runTime.globalPath()/outputName)
#define FUNCTION_NAME
Ostream & operator<<(Ostream &, const boundaryPatch &p)
Write boundaryPatch as dictionary entries (without surrounding braces)
Definition: boundaryPatch.C:77
bool fatalCheck(const char *operation) const
Check IOstream status for given operation.
Definition: IOstream.C:51
auto key(const Type &t) -> typename std::enable_if< std::is_enum< Type >::value, typename std::underlying_type< Type >::type >::type
Definition: foamGltfBase.H:103
static Ostream & printMaps(Ostream &os, const labelListList &maps)
virtual Ostream & beginBlock(const keyType &kw)
Write begin block group with the given name.
Definition: Ostream.C:90
virtual Ostream & endEntry()
Write end entry (&#39;;&#39;) followed by newline.
Definition: Ostream.C:117
mapDistributeBase() noexcept
Default construct (uses worldComm)
static void writeMaps(Ostream &os, const word &key, const labelListList &maps)
streamFormat format() const noexcept
Get the current stream format.
void writeEntries(Ostream &os) const
Write entries in dictionary format.
Namespace for OpenFOAM.