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 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) << token::END_STATEMENT << nl;
72  }
73 }
74 
75 } // End namespace Foam
76 
77 
78 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
79 
81 (
82  const dictionary& dict,
83  const label comm
84 )
85 :
86  mapDistributeBase(comm)
87 {
88  mapDistributeBase::readDict(dict);
89 }
90 
91 
93 {
94  is >> *this;
95 }
96 
97 
98 // * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * * //
99 
101 {
102  constructSize_ = dict.get<label>("constructSize");
103 
104  // The subMap
105  {
106  const dictionary& subdict = dict.subDict("subMap");
107 
108  subdict.readEntry("flip", subHasFlip_);
109  subdict.readEntry("maps", subMap_);
110  }
111 
112  // The constructMap
113  {
114  const dictionary& subdict = dict.subDict("constructMap");
116  subdict.readEntry("flip", constructHasFlip_);
117  subdict.readEntry("maps", constructMap_);
118  }
119 }
120 
121 
122 void Foam::mapDistributeBase::writeEntries(Ostream& os) const
123 {
124  os.writeEntry("constructSize", constructSize_);
125 
126  os << nl;
127  os.beginBlock("subMap");
128  os.writeEntry("flip", subHasFlip_);
129  writeMaps(os, "maps", subMap_);
130  os.endBlock();
131 
132  os << nl;
133  os.beginBlock("constructMap");
134  os.writeEntry("flip", constructHasFlip_);
135  writeMaps(os, "maps", constructMap_);
136  os.endBlock();
137 }
138 
139 
140 // * * * * * * * * * * * * * * * IOstream Operators * * * * * * * * * * * * //
141 
142 Foam::Istream& Foam::operator>>(Istream& is, mapDistributeBase& map)
143 {
145 
146  is >> map.constructSize_
147  >> map.subMap_ >> map.constructMap_
148  >> map.subHasFlip_ >> map.constructHasFlip_
149  >> map.comm_;
150 
151  return is;
152 }
153 
154 
155 Foam::Ostream& Foam::operator<<(Ostream& os, const mapDistributeBase& map)
156 {
157  os << map.constructSize_ << token::NL;
158 
159  printMaps(os, map.subMap_) << token::NL;
160  printMaps(os, map.constructMap_) << token::NL;
161 
162  os << map.subHasFlip_ << token::SPACE
163  << map.constructHasFlip_ << token::SPACE
164  << map.comm_ << token::NL;
165 
166  return os;
167 }
168 
169 
170 template<>
171 Foam::Ostream& Foam::operator<<
172 (
173  Ostream& os,
174  const InfoProxy<mapDistributeBase>& ip
175 )
176 {
177  const auto& map = ip.t_;
178 
179  // Output as compact pseudo dictionary entries
180 
181  os.writeEntry("constructSize", map.constructSize());
182 
183  os << indent << "local { flip " << map.subHasFlip()
184  << "; sizes ";
185  map.subMapSizes().writeList(os) << "; }" << nl;
186 
187  os << indent << "remote { flip " << map.constructHasFlip()
188  << "; sizes ";
189  map.constructMapSizes().writeList(os) << "; }" << nl;
190 
191  return os;
192 }
193 
194 
195 // ************************************************************************* //
List< labelList > labelListList
A List of labelList.
Definition: labelList.H:51
dictionary dict
void size(const label n)
Older name for setAddressableSize.
Definition: UList.H:118
Ostream & indent(Ostream &os)
Indent stream.
Definition: Ostream.H:449
A list of keyword definitions, which are a keyword followed by a number of values (eg...
Definition: dictionary.H:120
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:49
bool empty() const noexcept
True if the UList is empty (ie, size() is zero)
Definition: UListI.H:420
Newline [isspace].
Definition: token.H:127
Begin list [isseparator].
Definition: token.H:158
Ostream & writeEntry(const keyType &key, const T &value)
Write a keyword/value entry.
Definition: Ostream.H:312
End entry [isseparator].
Definition: token.H:157
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.
const dictionary & subDict(const word &keyword, enum keyType::option matchOpt=keyType::REGEX) const
Find and return a sub-dictionary.
Definition: dictionary.C:453
mapDistributeBase()
Default construct (uses worldComm)
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:98
Space [isspace].
Definition: token.H:128
End list [isseparator].
Definition: token.H:159
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:55
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:76
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:80
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.