PrintTable.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) 2012-2016 OpenFOAM Foundation
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 "PrintTable.H"
29 
30 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
31 
32 template<class KeyType, class DataType>
34 :
35  table_(),
36  title_(string::null)
37 {}
38 
39 
40 template<class KeyType, class DataType>
42 :
43  table_(),
44  title_(title)
45 {}
46 
47 
48 template<class KeyType, class DataType>
50 (
51  const PrintTable<KeyType, DataType>& table
52 )
53 :
54  table_(table.table_),
55  title_(table.title_)
56 {}
57 
58 
59 // * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * * //
60 
61 template<class KeyType, class DataType>
63 (
64  Ostream& os,
65  const bool printSum,
66  const bool printAverage
67 ) const
68 {
69  HashTable<Map<DataType>, KeyType> combinedTable;
70 
71  List<HashTableData> procData(Pstream::nProcs(), HashTableData());
72 
73  procData[Pstream::myProcNo()] = table_;
74 
75  Pstream::gatherList(procData);
76 
77  if (Pstream::master())
78  {
79  label largestKeyLength = 6;
80  label largestDataLength = 0;
81 
82  labelList largestProcSize(Pstream::nProcs(), Zero);
83 
84  forAll(procData, proci)
85  {
86  const HashTableData& procIData = procData[proci];
87 
88  forAllConstIters(procIData, iter)
89  {
90  if (!combinedTable.found(iter.key()))
91  {
92  combinedTable.insert
93  (
94  iter.key(),
95  Map<DataType>()
96  );
97  }
98 
99  Map<DataType>& key = combinedTable[iter.key()];
100 
101  key.insert(proci, iter.val());
102 
103  forAllConstIters(key, dataIter)
104  {
105  std::ostringstream buf;
106  buf << dataIter.val();
107 
108  largestDataLength = max
109  (
110  largestDataLength,
111  label(buf.str().length())
112  );
113  }
114 
115  std::ostringstream buf;
116  buf << iter.key();
117 
118  largestKeyLength = max
119  (
120  largestKeyLength,
121  label(buf.str().length())
122  );
123  }
124  }
125 
126  os.width(largestKeyLength);
127  os << nl << indent << tab << "# " << title_.c_str() << endl;
128 
129  os.width(largestKeyLength);
130  os << indent << "# Proc";
131 
132  forAll(procData, proci)
133  {
134  os << tab;
135  os.width(largestDataLength);
136  os << proci;
137  }
138 
139  if (printSum)
140  {
141  os << tab;
142  os.width(largestDataLength);
143  os << "Sum";
144  }
145 
146  if (printAverage)
147  {
148  os << tab;
149  os.width(largestDataLength);
150  os << "Average";
151  }
152 
153  os << endl;
154 
155  const List<KeyType>& sortedTable = combinedTable.sortedToc();
156 
157  forAll(sortedTable, keyI)
158  {
159  const Map<DataType>& procDataList
160  = combinedTable[sortedTable[keyI]];
161 
162  os.width(largestKeyLength);
163  os << indent << sortedTable[keyI];
164 
165  forAll(procDataList, elemI)
166  {
167  os << tab;
168  os.width(largestDataLength);
169  os << procDataList[elemI];
170  }
171 
172  if (printSum)
173  {
174  DataType sum = 0;
175  forAll(procDataList, elemI)
176  {
177  sum += procDataList[elemI];
178  }
179 
180  os << tab;
181  os.width(largestDataLength);
182  os << sum;
183 
184  if (printAverage)
185  {
186  os << tab;
187  os.width(largestDataLength);
188  os << sum/Pstream::nProcs();
189  }
190  }
191 
192  os << endl;
193  }
194  }
195 }
196 
197 
198 // ************************************************************************* //
Foam::labelList
List< label > labelList
A List of labels.
Definition: List.H:67
PrintTable.H
Foam::Zero
static constexpr const zero Zero
Global zero (0)
Definition: zero.H:131
Foam::PrintTable::PrintTable
PrintTable()
Null constructor.
Foam::OSstream::width
virtual int width() const
Get width of output field.
Definition: OSstream.C:314
Foam::glTF::key
auto key(const Type &t) -> typename std::enable_if< std::is_enum< Type >::value, typename std::underlying_type< Type >::type >::type
Definition: foamGltfBase.H:108
Foam::UPstream::master
static bool master(const label communicator=worldComm)
Am I the master process.
Definition: UPstream.H:457
Foam::endl
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:369
forAll
#define forAll(list, i)
Loop across all elements in list.
Definition: stdFoam.H:296
Foam::max
label max(const labelHashSet &set, label maxValue=labelMin)
Find the max value in labelHashSet, optionally limited by second argument.
Definition: hashSets.C:47
os
OBJstream os(runTime.globalPath()/outputName)
Foam::indent
Ostream & indent(Ostream &os)
Indent stream.
Definition: Ostream.H:339
Foam::Pstream::gatherList
static void gatherList(const List< commsStruct > &comms, List< T > &Values, const int tag, const label comm)
Gather data but keep individual values separate.
Definition: gatherScatterList.C:52
Foam::tab
constexpr char tab
Definition: Ostream.H:403
Foam::UPstream::myProcNo
static int myProcNo(const label communicator=worldComm)
Number of this process (starting from masterNo() = 0)
Definition: UPstream.H:463
Foam::nl
constexpr char nl
Definition: Ostream.H:404
forAllConstIters
forAllConstIters(mixture.phases(), phase)
Definition: pEqn.H:28
Foam::sum
dimensioned< Type > sum(const DimensionedField< Type, GeoMesh > &df)
Definition: DimensionedFieldFunctions.C:327
Foam::PrintTable::print
void print(Ostream &os, const bool printSum=false, const bool printAverage=false) const
Print the table.
Foam::UPstream::nProcs
static label nProcs(const label communicator=worldComm)
Number of processes in parallel run, and 1 for serial run.
Definition: UPstream.H:445