volFieldValueTemplates.C
Go to the documentation of this file.
1 /*---------------------------------------------------------------------------*\
2  ========= |
3  \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
4  \\ / O peration | Website: https://openfoam.org
5  \\ / A nd | Copyright (C) 2011-2020 OpenFOAM Foundation
6  \\/ M anipulation |
7 -------------------------------------------------------------------------------
8 License
9  This file is part of OpenFOAM.
10 
11  OpenFOAM is free software: you can redistribute it and/or modify it
12  under the terms of the GNU General Public License as published by
13  the Free Software Foundation, either version 3 of the License, or
14  (at your option) any later version.
15 
16  OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
17  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
18  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
19  for more details.
20 
21  You should have received a copy of the GNU General Public License
22  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
23 
24 \*---------------------------------------------------------------------------*/
25 
26 #include "volFieldValue.H"
27 #include "volFields.H"
28 
29 // * * * * * * * * * * * * Protected Member Functions * * * * * * * * * * * //
30 
31 template<class Type>
33 (
34  const word& fieldName
35 ) const
36 {
38 
39  if (obr_.foundObject<vf>(fieldName))
40  {
41  return true;
42  }
43 
44  return false;
45 }
46 
47 
48 template<class Type>
51 (
52  const word& fieldName,
53  const bool mustGet
54 ) const
55 {
57 
58  if (obr_.foundObject<vf>(fieldName))
59  {
60  return filterField(obr_.lookupObject<vf>(fieldName));
61  }
62 
63  if (mustGet)
64  {
66  << "Field " << fieldName << " not found in database"
67  << abort(FatalError);
68  }
69 
70  return tmp<Field<Type>>(new Field<Type>(0.0));
71 }
72 
73 
74 template<class Type>
76 (
77  const Field<Type>& values,
78  const scalarField& V,
79  const scalarField& weightField
80 ) const
81 {
82  Type result = Zero;
83  switch (operation_)
84  {
85  case operationType::sum:
86  {
87  result = gSum(values);
88  break;
89  }
90  case operationType::weightedSum:
91  {
92  result = gSum(weightField*values);
93  break;
94  }
96  {
97  result = gSum(cmptMag(values));
98  break;
99  }
101  {
102  result = gSum(values)/nCells();
103  break;
104  }
105  case operationType::weightedAverage:
106  {
107  result = gSum(weightField*values)/max(gSum(weightField), vSmall);
108  break;
109  }
110  case operationType::volAverage:
111  {
112  result = gSum(V*values)/this->V();
113  break;
114  }
115  case operationType::weightedVolAverage:
116  {
117  result =
118  gSum(weightField*V*values)/max(gSum(weightField*V), vSmall);
119  break;
120  }
121  case operationType::volIntegrate:
122  {
123  result = gSum(V*values);
124  break;
125  }
126  case operationType::weightedVolIntegrate:
127  {
128  result = gSum(weightField*V*values);
129  break;
130  }
131  case operationType::min:
132  {
133  result = gMin(values);
134  break;
135  }
136  case operationType::max:
137  {
138  result = gMax(values);
139  break;
140  }
141  case operationType::CoV:
142  {
143  Type meanValue = gSum(values*V)/this->V();
144 
145  const label nComp = pTraits<Type>::nComponents;
146 
147  for (direction d=0; d<nComp; ++d)
148  {
149  scalarField vals(values.component(d));
150  scalar mean = component(meanValue, d);
151  scalar& res = setComponent(result, d);
152 
153  res = sqrt(gSum(V*sqr(vals - mean))/this->V())/mean;
154  }
155 
156  break;
157  }
158  case operationType::none:
159  {}
160  }
161 
162  return result;
163 }
164 
165 
166 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
167 
168 template<class Type>
170 (
171  const word& fieldName
172 )
173 {
174  const bool ok = validField<Type>(fieldName);
175 
176  if (ok)
177  {
178  Field<Type> values(setFieldValues<Type>(fieldName));
179  scalarField V(filterField(fieldValue::mesh_.V()));
180  scalarField weightField(values.size(), 1.0);
181 
182  forAll(weightFieldNames_, i)
183  {
184  weightField *= setFieldValues<scalar>(weightFieldNames_[i], true);
185  }
186 
187  Type result = processValues(values, V, weightField);
188 
189  if (Pstream::master())
190  {
191  // Add to result dictionary, over-writing any previous entry
192  resultDict_.add(fieldName, result, true);
193 
194  if (writeFields_)
195  {
197  (
198  IOobject
199  (
200  fieldName + '_' + regionTypeNames_[regionType_]
201  + '-' + volRegion::regionName_,
202  obr_.time().timeName(),
203  obr_,
204  IOobject::NO_READ,
205  IOobject::NO_WRITE
206  ),
207  (weightField*values).ref()
208  ).write();
209  }
210 
211 
212  file()<< tab << result;
213 
214  Log << " " << operationTypeNames_[operation_]
215  << "(" << volRegion::regionName_ << ") of " << fieldName
216  << " = " << result << endl;
217  }
218  }
219 
220  return ok;
221 }
222 
223 
224 template<class Type>
227 (
228  const Field<Type>& field
229 ) const
230 {
231  if (isNull(cellIDs()))
232  {
233  return field;
234  }
235  else
236  {
237  return tmp<Field<Type>>(new Field<Type>(field, cellIDs()));
238  }
239 }
240 
241 
242 // ************************************************************************* //
#define forAll(list, i)
Loop across all elements in list.
Definition: UList.H:434
intWM_LABEL_SIZE_t label
A label is an int32_t or int64_t as specified by the pre-processor macro WM_LABEL_SIZE.
Definition: label.H:59
static const char tab
Definition: Ostream.H:259
error FatalError
dimensioned< Type > max(const dimensioned< Type > &, const dimensioned< Type > &)
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:319
Type gMin(const FieldField< Field, Type > &f)
tmp< Field< Type > > setFieldValues(const word &fieldName, const bool mustGet=false) const
Insert field values into values list.
dimensionedSymmTensor sqr(const dimensionedVector &dv)
uint8_t direction
Definition: direction.H:45
void size(const label)
Override size to be inconsistent with allocated storage.
Definition: ListI.H:164
dimensionedScalar sqrt(const dimensionedScalar &ds)
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:251
Traits class for primitives.
Definition: pTraits.H:50
Generic GeometricField class.
dimensioned< Type > sum(const DimensionedField< Type, GeoMesh > &df)
bool isNull(const T &t)
Return true if t is a reference to the nullObject of type T.
Definition: nullObjectI.H:46
Type gSum(const FieldField< Field, Type > &f)
Pre-declare SubField and related Field type.
Definition: Field.H:56
bool validField(const word &fieldName) const
Return true if the field name is valid.
A class for handling words, derived from string.
Definition: word.H:59
tmp< Field< Type > > filterField(const Field< Type > &field) const
Filter a field according to cellIds.
Type processValues(const Field< Type > &values, const scalarField &V, const scalarField &weightField) const
Apply the &#39;operation&#39; to the values.
void write(std::ostream &os, const bool binary, List< floatScalar > &fField)
Write floats ascii or binary.
static const zero Zero
Definition: zero.H:97
errorManip< error > abort(error &err)
Definition: errorManip.H:131
tmp< Field< cmptType > > component(const direction) const
Return a component field of the field.
Definition: Field.C:460
void cmptMag(FieldField< Field, Type > &cf, const FieldField< Field, Type > &f)
Type gMax(const FieldField< Field, Type > &f)
dimensioned< Type > min(const dimensioned< Type > &, const dimensioned< Type > &)
#define Log
Report write to Foam::Info if the local log switch is true.
tmp< GeometricField< Type, fvPatchField, volMesh > > average(const GeometricField< Type, fvsPatchField, surfaceMesh > &ssf)
Area-weighted average a surfaceField creating a volField.
Definition: fvcAverage.C:46
rDeltaTY field()
A class for managing temporary objects.
Definition: PtrList.H:53
bool writeValues(const word &fieldName)
Templated helper function to output field values.
label & setComponent(label &l, const direction)
Definition: label.H:86
IOobject defines the attributes of an object for which implicit objectRegistry management is supporte...
Definition: IOobject.H:92
void component(FieldField< Field, typename FieldField< Field, Type >::cmptType > &sf, const FieldField< Field, Type > &f, const direction d)
A primitive field of type <T> with automated input and output.
Definition: IOField.H:50
dimensioned< scalar > sumMag(const DimensionedField< Type, GeoMesh > &df)