fieldMinMaxTemplates.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) 2011-2017 OpenFOAM Foundation
9  Copyright (C) 2015-2022 OpenCFD Ltd.
10 -------------------------------------------------------------------------------
11 License
12  This file is part of OpenFOAM.
13 
14  OpenFOAM is free software: you can redistribute it and/or modify it
15  under the terms of the GNU General Public License as published by
16  the Free Software Foundation, either version 3 of the License, or
17  (at your option) any later version.
18 
19  OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
20  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
21  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
22  for more details.
23 
24  You should have received a copy of the GNU General Public License
25  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
26 
27 \*---------------------------------------------------------------------------*/
28 
29 #include "fieldMinMax.H"
30 #include "volFields.H"
31 
32 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
33 
34 template<class Type>
36 (
37  const word& fieldName,
38  const word& outputName,
39  const label minCell,
40  const label maxCell,
41  const vector& minC,
42  const vector& maxC,
43  const label minProci,
44  const label maxProci,
45  const Type& minValue,
46  const Type& maxValue
47 )
48 {
49  OFstream& file = this->file();
50 
51  if (location_)
52  {
53  writeCurrentTime(file);
54 
55  writeTabbed(file, fieldName);
56 
58  << token::TAB << minC;
59 
60  if (Pstream::parRun())
61  {
62  file<< token::TAB << minProci;
63  }
64 
66  << token::TAB << maxC;
67 
68  if (Pstream::parRun())
69  {
70  file<< token::TAB << maxProci;
71  }
72 
73  file<< endl;
74 
75  Log << " min(" << outputName << ") = " << minValue
76  << " in cell " << minCell
77  << " at location " << minC;
78 
79  if (Pstream::parRun())
80  {
81  Log << " on processor " << minProci;
82  }
83 
84  Log << nl << " max(" << outputName << ") = " << maxValue
85  << " in cell " << maxCell
86  << " at location " << maxC;
87 
88  if (Pstream::parRun())
89  {
90  Log << " on processor " << maxProci;
91  }
92  }
93  else
94  {
96 
97  Log << " min/max(" << outputName << ") = "
98  << minValue << ' ' << maxValue;
99  }
100 
101  Log << endl;
102 
103  // Write state/results information
104  word nameStr('(' + outputName + ')');
105  this->setResult("min" + nameStr, minValue);
106  this->setResult("min" + nameStr + "_cell", minCell);
107  this->setResult("min" + nameStr + "_position", minC);
108  this->setResult("min" + nameStr + "_processor", minProci);
109  this->setResult("max" + nameStr, maxValue);
110  this->setResult("max" + nameStr + "_cell", maxCell);
111  this->setResult("max" + nameStr + "_position", maxC);
112  this->setResult("max" + nameStr + "_processor", maxProci);
113 }
114 
115 
116 template<class Type>
118 (
120  const word& outputFieldName
121 )
122 {
123  const label proci = Pstream::myProcNo();
124 
125  // Find min/max internal field value info
126 
128  labelList minCells(Pstream::nProcs(), Zero);
130 
132  labelList maxCells(Pstream::nProcs(), Zero);
134 
135  labelPair minMaxIds = findMinMax(field);
136 
137  label minId = minMaxIds.first();
138  if (minId != -1)
139  {
140  minVs[proci] = field[minId];
141  minCells[proci] = minId;
142  minCs[proci] = mesh_.C()[minId];
143  }
144 
145  label maxId = minMaxIds.second();
146  if (maxId != -1)
147  {
148  maxVs[proci] = field[maxId];
149  maxCells[proci] = maxId;
150  maxCs[proci] = mesh_.C()[maxId];
151  }
152 
153 
154  // Find min/max boundary field info
155  const auto& fieldBoundary = field.boundaryField();
156  const auto& CfBoundary = mesh_.C().boundaryField();
157 
158  forAll(fieldBoundary, patchi)
159  {
160  const Field<Type>& fp = fieldBoundary[patchi];
161  if (fp.size())
162  {
163  const vectorField& Cfp = CfBoundary[patchi];
164 
165  const labelList& faceCells =
166  fieldBoundary[patchi].patch().faceCells();
167 
168  minMaxIds = findMinMax(fp);
169 
170  minId = minMaxIds.first();
171  if (minVs[proci] > fp[minId])
172  {
173  minVs[proci] = fp[minId];
174  minCells[proci] = faceCells[minId];
175  minCs[proci] = Cfp[minId];
176  }
177 
178  maxId = minMaxIds.second();
179  if (maxVs[proci] < fp[maxId])
180  {
181  maxVs[proci] = fp[maxId];
182  maxCells[proci] = faceCells[maxId];
183  maxCs[proci] = Cfp[maxId];
184  }
185  }
186  }
187 
188  // Collect info from all processors and output
189  Pstream::allGatherList(minVs);
190  Pstream::allGatherList(minCells);
191  Pstream::allGatherList(minCs);
192 
193  Pstream::allGatherList(maxVs);
194  Pstream::allGatherList(maxCells);
195  Pstream::allGatherList(maxCs);
196 
197  minId = findMin(minVs);
198  const Type& minValue = minVs[minId];
199  const label minCell = minCells[minId];
200  const vector& minC = minCs[minId];
201 
202  maxId = findMax(maxVs);
203  const Type& maxValue = maxVs[maxId];
204  const label maxCell = maxCells[maxId];
205  const vector& maxC = maxCs[maxId];
206 
207  output
208  (
209  field.name(),
210  outputFieldName,
211  minCell,
212  maxCell,
213  minC,
214  maxC,
215  minId,
216  maxId,
217  minValue,
219  );
220 }
221 
222 
223 template<class Type>
225 (
226  const word& fieldName,
227  const modeType& mode
228 )
229 {
231 
232  if (obr_.foundObject<fieldType>(fieldName))
233  {
234  const fieldType& field = lookupObject<fieldType>(fieldName);
235 
236  switch (mode)
237  {
238  case mdMag:
239  {
240  calcMinMaxFieldType<scalar>
241  (
242  mag(field),
243  word("mag(" + fieldName + ")")
244  );
245  break;
246  }
247  case mdCmpt:
248  {
249  calcMinMaxFieldType(field, fieldName);
250  break;
251  }
252  default:
253  {
255  << "Unknown min/max mode: " << modeTypeNames_[mode_]
256  << exit(FatalError);
257  }
258  }
259  }
260 }
261 
262 
263 // ************************************************************************* //
label findMax(const ListType &input, label start=0)
Linear search for the index of the max element, similar to std::max_element but for lists and returns...
const T & first() const noexcept
Access the first element.
Definition: Pair.H:137
virtual OFstream & file()
Return access to the file (if only 1)
Definition: writeFile.C:264
rDeltaTY field()
labelPair findMinMax(const ListType &input, label start=0)
Linear search for the index of the min/max element, similar to std::minmax_element but for lists and ...
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:125
dimensioned< typename typeOfMag< Type >::type > mag(const dimensioned< Type > &dt)
error FatalError
Error stream (stdout output on all processes), with additional &#39;FOAM FATAL ERROR&#39; header text and sta...
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:598
Tab [isspace].
Definition: token.H:129
scalar minValue
Output to file stream, using an OSstream.
Definition: OFstream.H:49
constexpr char nl
The newline &#39;\n&#39; character (0x0a)
Definition: Ostream.H:50
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:531
A traits class, which is primarily used for primitives and vector-space.
Definition: pTraits.H:75
static bool & parRun() noexcept
Test if this a parallel run.
Definition: UPstream.H:1049
void setResult(const word &entryName, const Type &value)
Add result.
Generic GeometricField class.
Definition: areaFieldsFwd.H:50
static int myProcNo(const label communicator=worldComm)
Rank of this process in the communicator (starting from masterNo()). Can be negative if the process i...
Definition: UPstream.H:1074
#define forAll(list, i)
Loop across all elements in list.
Definition: stdFoam.H:421
void calcMinMaxFields(const word &fieldName, const modeType &mode)
Calculate the field min/max.
static void allGatherList(List< T > &values, const int tag=UPstream::msgType(), const label comm=UPstream::worldComm)
Gather data, but keep individual values separate. Uses linear/tree communication. ...
label findMin(const ListType &input, label start=0)
Linear search for the index of the min element, similar to std::min_element but for lists and returns...
scalar maxValue
static label nProcs(const label communicator=worldComm)
Number of ranks in parallel run (for given communicator). It is 1 for serial run. ...
Definition: UPstream.H:1065
word outputName("finiteArea-edges.obj")
A class for handling words, derived from Foam::string.
Definition: word.H:63
Vector< scalar > vector
Definition: vector.H:57
A Vector of values with scalar precision, where scalar is float/double depending on the compilation f...
bool location_
Flag to write location of min/max values.
Definition: fieldMinMax.H:198
virtual void writeCurrentTime(Ostream &os) const
Write the current time to stream.
Definition: writeFile.C:349
void output(const word &fieldName, const word &outputName, const label minCell, const label maxCell, const vector &minC, const vector &maxC, const label minProci, const label maxProci, const Type &minValue, const Type &maxValue)
Helper function to write the output.
#define Log
Definition: PDRblock.C:28
static Ostream & output(Ostream &os, const IntRange< T > &range)
Definition: IntRanges.C:44
Field< vector > vectorField
Specialisation of Field<T> for vector.
void calcMinMaxFieldType(const GeometricField< Type, fvPatchField, volMesh > &field, const word &outputFieldName)
Calculate the field min/max for a given field type.
List< label > labelList
A List of labels.
Definition: List.H:62
const T & second() const noexcept
Access the second element.
Definition: Pair.H:147
mode_t mode(const fileName &name, const bool followLink=true)
Return the file mode, normally following symbolic links.
Definition: POSIX.C:773
static constexpr const zero Zero
Global zero (0)
Definition: zero.H:127
virtual void writeTabbed(Ostream &os, const string &str) const
Write a tabbed string to stream.
Definition: writeFile.C:329