Loading...
Searching...
No Matches
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-------------------------------------------------------------------------------
11License
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\*---------------------------------------------------------------------------*/
29#include "fieldMinMax.H"
30#include "volFields.H"
31
32// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
33
34template<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 {
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
116template<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 labelUList& 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
190 Pstream::allGatherList(minCells);
192
194 Pstream::allGatherList(maxCells);
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,
219 );
220}
221
222
223template<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// ************************************************************************* //
scalar maxValue
scalar minValue
#define Log
Definition PDRblock.C:28
Generic templated field type that is much like a Foam::List except that it is expected to hold numeri...
Definition Field.H:172
Generic GeometricField class.
A 1D array of objects of type <T>, where the size of the vector is known and used for subscript bound...
Definition List.H:72
Output to file stream as an OSstream, normally using std::ofstream for the actual output.
Definition OFstream.H:75
const T & first() const noexcept
Access the first element.
Definition Pair.H:137
const T & second() const noexcept
Access the second element.
Definition Pair.H:147
static void allGatherList(UList< T > &values, const int tag=UPstream::msgType(), const int communicator=UPstream::worldComm)
Gather data, but keep individual values separate. Uses MPI_Allgather or manual communication.
void size(const label n)
Older name for setAddressableSize.
Definition UList.H:118
static int myProcNo(const label communicator=worldComm)
Rank of this process in the communicator (starting from masterNo()). Negative if the process is not a...
Definition UPstream.H:1706
static bool parRun(const bool on) noexcept
Set as parallel run on/off.
Definition UPstream.H:1669
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:1697
Smooth ATC in cells next to a set of patches supplied by type.
Definition faceCells.H:55
bool location_
Flag to write location of min/max values.
modeType mode_
Mode for min/max - only applicable for ranks > 0.
void calcMinMaxFields(const word &fieldName, const modeType &mode)
Calculate the field min/max.
void calcMinMaxFieldType(const GeometricField< Type, fvPatchField, volMesh > &field, const word &outputFieldName)
Calculate the field min/max for a given field type.
static const Enum< modeType > modeTypeNames_
Mode type names.
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.
modeType
Options for the mode types.
const fvMesh & mesh_
Reference to the fvMesh.
const ObjectType & lookupObject(const word &fieldName) const
Lookup and return object (eg, a field) from the (sub) objectRegistry.
const objectRegistry & obr_
Reference to the region objectRegistry.
void setResult(const word &entryName, const Type &value)
Add result.
virtual void writeTabbed(Ostream &os, const string &str) const
Write a tabbed string to stream.
Definition writeFile.C:334
virtual OFstream & file()
Return access to the file (if only 1).
Definition writeFile.C:270
virtual void writeCurrentTime(Ostream &os) const
Write the current time to stream.
Definition writeFile.C:354
A traits class, which is primarily used for primitives and vector-space.
Definition pTraits.H:64
@ TAB
Tab [isspace].
Definition token.H:142
A class for handling words, derived from Foam::string.
Definition word.H:66
rDeltaTY field()
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition error.H:600
word outputName("finiteArea-edges.obj")
Pair< label > labelPair
A pair of labels.
Definition Pair.H:54
List< label > labelList
A List of labels.
Definition List.H:62
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...
mode_t mode(const fileName &name, const bool followLink=true)
Return the file mode, normally following symbolic links.
Definition POSIX.C:775
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 ...
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition Ostream.H:519
dimensioned< typename typeOfMag< Type >::type > mag(const dimensioned< Type > &dt)
Field< vector > vectorField
Specialisation of Field<T> for vector.
static constexpr const zero Zero
Global zero (0).
Definition zero.H:127
error FatalError
Error stream (stdout output on all processes), with additional 'FOAM FATAL ERROR' header text and sta...
Vector< scalar > vector
Definition vector.H:57
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...
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition errorManip.H:125
UList< label > labelUList
A UList of labels.
Definition UList.H:75
constexpr char nl
The newline '\n' character (0x0a).
Definition Ostream.H:50
#define forAll(list, i)
Loop across all elements in list.
Definition stdFoam.H:299