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 -------------------------------------------------------------------------------
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 "FlatOutput.H"
30 
31 // * * * * * * * * * * * * * * * Local Functions * * * * * * * * * * * * * * //
32 
33 namespace Foam
34 {
35 
36 //- Return magnitude of a given value unless the given Type is scalar
37 template<class Type>
38 static inline scalar MagOp(const Type& val)
39 {
40  return Foam::mag(val);
41 }
42 
43 //- Return self if a given Type is scalar
44 template<>
45 inline scalar MagOp<scalar>(const scalar& val)
46 {
47  return val;
48 }
49 
50 } // End namespace Foam
51 
52 
53 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
54 
55 template<class Type>
56 bool 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 // ************************************************************************* //
virtual OFstream & file()
Return access to the file (if only 1)
Definition: writeFile.C:264
dimensioned< Type > average(const DimensionedField< Type, GeoMesh > &f1)
dimensioned< Type > sum(const DimensionedField< Type, GeoMesh > &f1)
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
label max(const labelHashSet &set, label maxValue=labelMin)
Find the max value in labelHashSet, optionally limited by second argument.
Definition: hashSets.C:40
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:531
static scalar MagOp(const Type &val)
Return magnitude of a given value unless the given Type is scalar.
void setResult(const word &entryName, const Type &value)
Add result.
dimensionedScalar stabilise(const dimensionedScalar &x, const dimensionedScalar &y)
constexpr char tab
The tab &#39;\t&#39; character(0x09)
Definition: Ostream.H:49
dimensioned< Type > cmptDivide(const dimensioned< Type > &, const dimensioned< Type > &)
#define forAll(list, i)
Loop across all elements in list.
Definition: stdFoam.H:421
List< T > values(const HashTable< T, Key, Hash > &tbl, const bool doSort=false)
List of values from HashTable, optionally sorted.
Definition: HashOps.H:164
word resultType(const word &entryName) const
Retrieve the result type.
static const Enum< operationType > operationTypeNames_
Operation type names.
label min(const labelHashSet &set, label minValue=labelMax)
Find the min value in labelHashSet, optionally limited by second argument.
Definition: hashSets.C:26
scalar MagOp< scalar >(const scalar &val)
Return self if a given Type is scalar.
errorManip< error > abort(error &err)
Definition: errorManip.H:139
OBJstream os(runTime.globalPath()/outputName)
List< word > wordList
List of word.
Definition: fileName.H:59
#define Log
Definition: PDRblock.C:28
Namespace for OpenFOAM.
FlatOutput::OutputAdaptor< Container, Delimiters > flatOutput(const Container &obj, Delimiters delim)
Global flatOutput() function with specified output delimiters.
Definition: FlatOutput.H:225
static constexpr const zero Zero
Global zero (0)
Definition: zero.H:127