multiFieldValueTemplates.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 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\*---------------------------------------------------------------------------*/
28
29#include "FlatOutput.H"
30
31// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
32
33template<class Type>
34bool Foam::functionObjects::fieldValues::multiFieldValue::applyOperation
35(
36 const word& resultType,
37 const wordList& foNames,
38 const wordList& entryNames
39)
40{
41 if (pTraits<Type>::typeName != resultType)
42 {
43 return false;
44 }
45
46 Type result = Zero;
47
48 Field<Type> values(foNames.size());
49 forAll(values, i)
50 {
51 values[i] = this->getObjectResult<Type>(foNames[i], entryNames[i]);
52 }
53
54 const word& opName = operationTypeNames_[operation_];
55
56 switch (operation_)
57 {
58 case opSum:
59 case opAdd:
60 {
61 result = sum(values);
62 break;
63 }
64 case opSubtract:
65 {
66 result = values[0];
67 for (label i = 1; i < values.size(); ++i)
68 {
69 result -= values[i];
70 }
71 break;
72 }
73 case opMin:
74 {
75 result = min(values);
76 break;
77 }
78 case opMax:
79 {
80 result = max(values);
81 break;
82 }
83 case opAverage:
84 {
85 result = average(values);
86 break;
87 }
88 default:
89 {
91 << "Unable to process operation "
92 << operationTypeNames_[operation_]
93 << abort(FatalError);
94 }
95 }
96
97 OStringStream os;
98 os << opName << flatOutput(entryNames, FlatOutput::ParenComma{});
99 const word resultName(os.str());
100 Log << " " << resultName << " = " << result << endl;
101
102 this->file()<< tab << result;
103
104 // Write state/results information
105 this->setResult(resultName, result);
106
107 return true;
108}
109
110
111// ************************************************************************* //
#define Log
Definition: PDRblock.C:35
static const Enum< operationType > operationTypeNames_
Operation type names.
@ opSubtract
Subtract values from first entry.
void setResult(const word &entryName, const Type &value)
Add result.
word resultType(const word &entryName) const
Retrieve the result type.
virtual OFstream & file()
Return access to the file (if only 1)
Definition: writeFile.C:233
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:453
OBJstream os(runTime.globalPath()/outputName)
List< T > values(const HashTable< T, Key, Hash > &tbl, const bool doSort=false)
List of values from HashTable, optionally sorted.
Definition: HashOps.H:149
label max(const labelHashSet &set, label maxValue=labelMin)
Find the max value in labelHashSet, optionally limited by second argument.
Definition: hashSets.C:47
dimensioned< Type > sum(const DimensionedField< Type, GeoMesh > &df)
dimensioned< Type > average(const DimensionedField< Type, GeoMesh > &df)
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:372
label min(const labelHashSet &set, label minValue=labelMax)
Find the min value in labelHashSet, optionally limited by second argument.
Definition: hashSets.C:33
FlatOutput::OutputAdaptor< Container, Delimiters > flatOutput(const Container &obj, Delimiters delim)
Global flatOutput() function with specified output delimiters.
Definition: FlatOutput.H:215
errorManip< error > abort(error &err)
Definition: errorManip.H:144
static constexpr const zero Zero
Global zero (0)
Definition: zero.H:131
error FatalError
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