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-------------------------------------------------------------------------------
10License
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
32template<class KeyType, class DataType>
34:
35 table_(),
36 title_(string::null)
37{}
38
39
40template<class KeyType, class DataType>
42:
43 table_(),
44 title_(title)
45{}
46
47
48template<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
61template<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// ************************************************************************* //
Y[inertIndex] max(0.0)
PrintTable()
Null constructor.
scalar print()
Print to screen.
OBJstream os(runTime.globalPath()/outputName)
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
List< label > labelList
A List of labels.
Definition: List.H:66
dimensioned< Type > sum(const DimensionedField< Type, GeoMesh > &df)
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:372
Ostream & indent(Ostream &os)
Indent stream.
Definition: Ostream.H:342
constexpr char nl
The newline '\n' character (0x0a)
Definition: Ostream.H:53
constexpr char tab
The tab '\t' character(0x09)
Definition: Ostream.H:52
#define forAll(list, i)
Loop across all elements in list.
Definition: stdFoam.H:333
#define forAllConstIters(container, iter)
Iterate across all elements of the container object with const access.
Definition: stdFoam.H:278