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-2019 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 
57  file<< token::TAB << minValue
58  << token::TAB << minC;
59 
60  if (Pstream::parRun())
61  {
62  file<< token::TAB << minProci;
63  }
64 
65  file<< token::TAB << maxValue
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  {
95  file<< token::TAB << minValue << token::TAB << maxValue;
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 
127  List<Type> minVs(Pstream::nProcs(), pTraits<Type>::max);
128  labelList minCells(Pstream::nProcs(), Zero);
129  List<vector> minCs(Pstream::nProcs(), Zero);
130 
131  List<Type> maxVs(Pstream::nProcs(), pTraits<Type>::min);
132  labelList maxCells(Pstream::nProcs(), Zero);
133  List<vector> maxCs(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::gatherList(minVs);
190  Pstream::scatterList(minVs);
191  Pstream::gatherList(minCells);
192  Pstream::scatterList(minCells);
193  Pstream::gatherList(minCs);
194  Pstream::scatterList(minCs);
195 
196  Pstream::gatherList(maxVs);
197  Pstream::scatterList(maxVs);
198  Pstream::gatherList(maxCells);
199  Pstream::scatterList(maxCells);
200  Pstream::gatherList(maxCs);
201  Pstream::scatterList(maxCs);
202 
203  minId = findMin(minVs);
204  const Type& minValue = minVs[minId];
205  const label minCell = minCells[minId];
206  const vector& minC = minCs[minId];
207 
208  maxId = findMax(maxVs);
209  const Type& maxValue = maxVs[maxId];
210  const label maxCell = maxCells[maxId];
211  const vector& maxC = maxCs[maxId];
212 
213  output
214  (
215  field.name(),
216  outputFieldName,
217  minCell,
218  maxCell,
219  minC,
220  maxC,
221  minId,
222  maxId,
223  minValue,
224  maxValue
225  );
226 }
227 
228 
229 template<class Type>
231 (
232  const word& fieldName,
233  const modeType& mode
234 )
235 {
237 
238  if (obr_.foundObject<fieldType>(fieldName))
239  {
240  const fieldType& field = lookupObject<fieldType>(fieldName);
241 
242  switch (mode)
243  {
244  case mdMag:
245  {
246  calcMinMaxFieldType<scalar>
247  (
248  mag(field),
249  word("mag(" + fieldName + ")")
250  );
251  break;
252  }
253  case mdCmpt:
254  {
255  calcMinMaxFieldType(field, fieldName);
256  break;
257  }
258  default:
259  {
261  << "Unknown min/max mode: " << modeTypeNames_[mode_]
262  << exit(FatalError);
263  }
264  }
265  }
266 }
267 
268 
269 // ************************************************************************* //
Foam::Pair::second
const T & second() const noexcept
Return second element, which is also the last element.
Definition: PairI.H:122
volFields.H
Log
#define Log
Definition: PDRblock.C:35
Foam::output
static Ostream & output(Ostream &os, const IntRange< T > &range)
Definition: IntRanges.C:66
Foam::word
A class for handling words, derived from Foam::string.
Definition: word.H:65
Foam::Zero
static constexpr const zero Zero
Global zero (0)
Definition: zero.H:131
Foam::functionObjects::fieldMinMax::calcMinMaxFieldType
void calcMinMaxFieldType(const GeometricField< Type, fvPatchField, volMesh > &field, const word &outputFieldName)
Calculate the field min/max for a given field type.
Definition: fieldMinMaxTemplates.C:118
Foam::endl
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:369
fieldMinMax.H
forAll
#define forAll(list, i)
Loop across all elements in list.
Definition: stdFoam.H:296
Foam::mode
mode_t mode(const fileName &name, const bool followLink=true)
Return the file mode, normally following symbolic links.
Definition: MSwindows.C:564
outputName
word outputName("finiteArea-edges.obj")
Foam::findMinMax
labelPair findMinMax(const ListType &input, label start=0)
Foam::Field
Generic templated field type.
Definition: Field.H:63
Foam::functionObjects::fieldMinMax::modeType
modeType
Definition: fieldMinMax.H:183
Foam::functionObjects::fieldMinMax::output
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.
Definition: fieldMinMaxTemplates.C:36
field
rDeltaTY field()
Foam::functionObjects::fieldMinMax::calcMinMaxFields
void calcMinMaxFields(const word &fieldName, const modeType &mode)
Calculate the field min/max.
Definition: fieldMinMaxTemplates.C:231
Foam::FatalError
error FatalError
Foam::exit
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:130
Foam::OFstream
Output to file stream, using an OSstream.
Definition: OFstream.H:53
FatalErrorInFunction
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:453
Foam::nl
constexpr char nl
Definition: Ostream.H:404
Foam::Pair< label >
Foam::Vector< scalar >
Foam::findMax
label findMax(const ListType &input, label start=0)
Foam::List< Type >
Foam::pTraits
A traits class, which is primarily used for primitives.
Definition: pTraits.H:56
Foam::mag
dimensioned< typename typeOfMag< Type >::type > mag(const dimensioned< Type > &dt)
minValue
scalar minValue
Definition: LISASMDCalcMethod2.H:12
Foam::GeometricField< Type, fvPatchField, volMesh >
Foam::findMin
label findMin(const ListType &input, label start=0)
Foam::faceCells
Smooth ATC in cells next to a set of patches supplied by type.
Definition: faceCells.H:56
maxValue
scalar maxValue
Definition: LISASMDCalcMethod1.H:5