PrintTable.C
Go to the documentation of this file.
1 /*---------------------------------------------------------------------------*\
2  ========= |
3  \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
4  \\ / O peration | Website: https://openfoam.org
5  \\ / A nd | Copyright (C) 2012-2019 OpenFOAM Foundation
6  \\/ M anipulation |
7 -------------------------------------------------------------------------------
8 License
9  This file is part of OpenFOAM.
10 
11  OpenFOAM is free software: you can redistribute it and/or modify it
12  under the terms of the GNU General Public License as published by
13  the Free Software Foundation, either version 3 of the License, or
14  (at your option) any later version.
15 
16  OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
17  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
18  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
19  for more details.
20 
21  You should have received a copy of the GNU General Public License
22  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
23 
24 \*---------------------------------------------------------------------------*/
25 
26 #include "PrintTable.H"
27 
28 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
29 
30 template<class KeyType, class DataType>
32 :
33  table_(),
34  title_(string::null)
35 {}
36 
37 
38 template<class KeyType, class DataType>
40 :
41  table_(),
42  title_(title)
43 {}
44 
45 
46 template<class KeyType, class DataType>
48 (
49  const PrintTable<KeyType, DataType>& table
50 )
51 :
52  table_(table.table_),
53  title_(table.title_)
54 {}
55 
56 
57 // * * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * //
58 
59 template<class KeyType, class DataType>
61 {}
62 
63 
64 // * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * * //
65 
66 template<class KeyType, class DataType>
68 (
69  Ostream& os,
70  const bool printSum,
71  const bool printAverage
72 ) const
73 {
74  HashTable<HashTable<DataType, label>, KeyType> combinedTable;
75 
76  List<HashTableData> procData(Pstream::nProcs(), HashTableData());
77 
78  procData[Pstream::myProcNo()] = table_;
79 
80  Pstream::gatherList(procData);
81 
82  if (Pstream::master())
83  {
84  label largestKeyLength = 6;
85  label largestDataLength = 0;
86 
87  List<label> largestProcSize(Pstream::nProcs(), 0);
88 
89  forAll(procData, proci)
90  {
91  const HashTableData& procIData = procData[proci];
92 
93  for
94  (
95  typename HashTableData::const_iterator iter = procIData.begin();
96  iter != procIData.end();
97  ++iter
98  )
99  {
100  if (!combinedTable.found(iter.key()))
101  {
102  combinedTable.insert
103  (
104  iter.key(),
105  HashTable<DataType, label>()
106  );
107  }
108 
109  HashTable<DataType, label>& key = combinedTable[iter.key()];
110 
111  key.insert(proci, iter());
112 
113  for
114  (
115  typename HashTable<DataType, label>
116  ::const_iterator dataIter = key.begin();
117  dataIter != key.end();
118  ++dataIter
119  )
120  {
121  std::ostringstream buf;
122  buf << dataIter();
123 
124  largestDataLength = max
125  (
126  largestDataLength,
127  label(buf.str().length())
128  );
129  }
130 
131  std::ostringstream buf;
132  buf << iter.key();
133 
134  largestKeyLength = max
135  (
136  largestKeyLength,
137  label(buf.str().length())
138  );
139  }
140  }
141 
142  os.width(largestKeyLength);
143  os << nl << indent << tab << "# " << title_.c_str() << endl;
144 
145  os.width(largestKeyLength);
146  os << indent << "# Proc";
147 
148  forAll(procData, proci)
149  {
150  os << tab;
151  os.width(largestDataLength);
152  os << proci;
153  }
154 
155  if (printSum)
156  {
157  os << tab;
158  os.width(largestDataLength);
159  os << "Sum";
160  }
161 
162  if (printAverage)
163  {
164  os << tab;
165  os.width(largestDataLength);
166  os << "Average";
167  }
168 
169  os << endl;
170 
171  const List<KeyType>& sortedTable = combinedTable.sortedToc();
172 
173  forAll(sortedTable, keyI)
174  {
175  const HashTable<DataType, label>& procDataList
176  = combinedTable[sortedTable[keyI]];
177 
178  os.width(largestKeyLength);
179  os << indent << sortedTable[keyI];
180 
181  forAll(procDataList, elemI)
182  {
183  os << tab;
184  os.width(largestDataLength);
185  os << procDataList[elemI];
186  }
187 
188  if (printSum)
189  {
190  DataType sum = 0;
191  forAll(procDataList, elemI)
192  {
193  sum += procDataList[elemI];
194  }
195 
196  os << tab;
197  os.width(largestDataLength);
198  os << sum;
199 
200  if (printAverage)
201  {
202  os << tab;
203  os.width(largestDataLength);
204  os << sum/Pstream::nProcs();
205  }
206  }
207 
208  os << endl;
209  }
210  }
211 }
212 
213 
214 // ************************************************************************* //
PrintTable()
Null constructor.
#define forAll(list, i)
Loop across all elements in list.
Definition: UList.H:434
HashTable< nil, Key, Hash >::const_iterator const_iterator
Definition: HashSet.H:67
intWM_LABEL_SIZE_t label
A label is an int32_t or int64_t as specified by the pre-processor macro WM_LABEL_SIZE.
Definition: label.H:59
static const char tab
Definition: Ostream.H:264
dimensioned< Type > max(const dimensioned< Type > &, const dimensioned< Type > &)
static int myProcNo(const label communicator=0)
Number of this process (starting from masterNo() = 0)
Definition: UPstream.H:429
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:256
static bool master(const label communicator=0)
Am I the master process.
Definition: UPstream.H:423
dimensioned< Type > sum(const DimensionedField< Type, GeoMesh > &df)
iterator begin()
Iterator set to the beginning of the HashTable.
Definition: HashTableI.H:411
virtual int width() const =0
Get width of output field.
static const char nl
Definition: Ostream.H:265
void print(Ostream &os, const bool printSum=false, const bool printAverage=false) const
Print the table.
static label nProcs(const label communicator=0)
Number of processes in parallel run.
Definition: UPstream.H:411
~PrintTable()
Destructor.
static void gatherList(const List< commsStruct > &comms, List< T > &Values, const int tag, const label comm)
Gather data but keep individual values separate.