Loading...
Searching...
No Matches
multiFieldValueImpl.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// * * * * * * * * * * * * * * * Local Functions * * * * * * * * * * * * * * //
32
33namespace Foam
34{
35
36//- Return magnitude of a given value unless the given Type is scalar
37template<class Type>
38static inline scalar MagOp(const Type& val)
39{
40 return Foam::mag(val);
41}
43//- Return self if a given Type is scalar
44template<>
45inline scalar MagOp<scalar>(const scalar& val)
46{
47 return val;
48}
49
50} // End namespace Foam
51
52
53// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
54
55template<class Type>
56bool Foam::functionObjects::fieldValues::multiFieldValue::applyOperation
57(
58 const word& resultType,
59 const wordList& foNames,
60 const wordList& entryNames
61)
62{
63 if (pTraits<Type>::typeName != resultType)
64 {
65 return false;
66 }
67
68 Type result = Zero;
69
70 Field<Type> values(foNames.size());
71 forAll(values, i)
72 {
73 values[i] = this->getObjectResult<Type>(foNames[i], entryNames[i]);
74 }
75
76 const word& opName = operationTypeNames_[operation_];
77
78 switch (operation_)
79 {
80 case opSum:
81 case opAdd:
82 {
83 result = sum(values);
84 break;
85 }
86 case opSubtract:
87 {
88 result = values[0];
89 for (label i = 1; i < values.size(); ++i)
90 {
91 result -= values[i];
92 }
93 break;
94 }
95 case opDivide:
96 {
97 result = values[0];
98 for (label i = 1; i < values.size(); ++i)
99 {
100 result /= stabilise(MagOp(values[i]), SMALL);
101 }
102 break;
103 }
104 case opCmptDivide:
105 {
106 result = values[0];
107 for (label i = 1; i < values.size(); ++i)
108 {
109 result = cmptDivide(result, stabilise(values[i], SMALL));
110 }
111 break;
112 }
113 case opMin:
114 {
115 result = min(values);
116 break;
117 }
118 case opMax:
119 {
120 result = max(values);
121 break;
122 }
123 case opAverage:
124 {
125 result = average(values);
126 break;
127 }
128 default:
129 {
131 << "Unable to process operation "
132 << operationTypeNames_[operation_]
133 << abort(FatalError);
134 }
135 }
136
137 OStringStream os;
138 os << opName << flatOutput(entryNames, FlatOutput::ParenComma{});
139 const word resultName(os.str());
140 Log << " " << resultName << " = " << result << endl;
141
142 this->file()<< tab << result;
143
144 // Write state/results information
145 this->setResult(resultName, result);
146
147 return true;
148}
149
150
151// ************************************************************************* //
#define Log
Definition PDRblock.C:28
static const Enum< operationType > operationTypeNames_
Operation type names.
@ opCmptDivide
Divide first entry by values componentwise.
@ opSubtract
Subtract values from first entry.
void setResult(const word &entryName, const Type &value)
Add result.
Type getObjectResult(const word &objectName, const word &entryName, const Type &defaultValue=Type(Zero)) const
Retrieve result from named object.
word resultType(const word &entryName) const
Retrieve the result type.
virtual OFstream & file()
Return access to the file (if only 1).
Definition writeFile.C:270
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition error.H:600
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:164
Namespace for OpenFOAM.
label max(const labelHashSet &set, label maxValue=labelMin)
Find the max value in labelHashSet, optionally limited by second argument.
Definition hashSets.C:40
scalar MagOp< scalar >(const scalar &val)
Return self if a given Type is scalar.
dimensioned< Type > cmptDivide(const dimensioned< Type > &, const dimensioned< Type > &)
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition Ostream.H:519
dimensioned< typename typeOfMag< Type >::type > mag(const dimensioned< Type > &dt)
label min(const labelHashSet &set, label minValue=labelMax)
Find the min value in labelHashSet, optionally limited by second argument.
Definition hashSets.C:26
FlatOutput::OutputAdaptor< Container, Delimiters > flatOutput(const Container &obj, Delimiters delim)
Global flatOutput() function with specified output delimiters.
Definition FlatOutput.H:217
errorManip< error > abort(error &err)
Definition errorManip.H:139
dimensionedScalar stabilise(const dimensionedScalar &x, const dimensionedScalar &y)
dimensioned< Type > sum(const DimensionedField< Type, GeoMesh > &f1, const label comm)
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...
static scalar MagOp(const Type &val)
Return magnitude of a given value unless the given Type is scalar.
dimensioned< Type > average(const DimensionedField< Type, GeoMesh > &f1, const label comm)
constexpr char tab
The tab '\t' character(0x09).
Definition Ostream.H:49
#define forAll(list, i)
Loop across all elements in list.
Definition stdFoam.H:299