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 | Copyright (C) 2012-2013 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>
31 Foam::PrintTable<KeyType, DataType>::PrintTable()
32 :
33  table_(),
34  title_(string::null)
35 {}
36 
37 
38 template<class KeyType, class DataType>
39 Foam::PrintTable<KeyType, DataType>::PrintTable(const string& title)
40 :
41  table_(),
42  title_(title)
43 {}
44 
45 
46 template<class KeyType, class DataType>
47 Foam::PrintTable<KeyType, DataType>::PrintTable
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>
60 Foam::PrintTable<KeyType, DataType>::~PrintTable()
61 {}
62 
63 
64 // * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * * //
65 
66 template<class KeyType, class DataType>
67 void Foam::PrintTable<KeyType, DataType>::print
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 // * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * * //
215 
216 template<class KeyType, class DataType>
217 void Foam::PrintTable<KeyType, DataType>::operator=
218 (
219  const PrintTable<KeyType, DataType>& rhs
220 )
221 {
222  // Check for assignment to self
223  if (this == &rhs)
224  {
226  (
227  "Foam::PrintTable<KeyType, DataType>::operator="
228  "(const Foam::PrintTable<KeyType, DataType>&)"
229  ) << "Attempted assignment to self"
230  << abort(FatalError);
231  }
232 }
233 
234 
235 // ************************************************************************* //