sampledSurfaceTemplates.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 | Copyright (C) 2011-2016 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 "sampledSurface.H"
27 
28 template<class Type>
29 bool Foam::sampledSurface::checkFieldSize(const Field<Type>& field) const
30 {
31  if (faces().empty() || field.empty())
32  {
33  return false;
34  }
35 
36  if (field.size() != faces().size())
37  {
39  << "size mismatch: "
40  << "field (" << field.size()
41  << ") != surface (" << faces().size() << ")"
42  << exit(FatalError);
43  }
44 
45  return true;
46 }
47 
48 
49 template<class Type>
51 {
52  Type value = Zero;
53 
54  if (checkFieldSize(field))
55  {
56  value = sum(field*magSf());
57  }
58 
59  reduce(value, sumOp<Type>());
60  return value;
61 }
62 
63 
64 template<class Type>
66 {
67  Type value = integrate(field());
68  field.clear();
69  return value;
70 }
71 
72 
73 template<class Type>
75 {
76  Type value = Zero;
77 
78  if (checkFieldSize(field))
79  {
80  value = sum(field*magSf());
81  }
82 
83  reduce(value, sumOp<Type>());
84 
85  // avoid divide-by-zero
86  if (area())
87  {
88  return value/area();
89  }
90  else
91  {
92  return Zero;
93  }
94 }
95 
96 
97 template<class Type>
99 {
100  Type value = average(field());
101  field.clear();
102  return value;
103 }
104 
105 
106 template<class ReturnType, class Type>
107 void Foam::sampledSurface::project
108 (
109  Field<ReturnType>& res,
110  const Field<Type>& field
111 ) const
112 {
113  if (checkFieldSize(field))
114  {
115  const vectorField& norm = Sf();
116 
117  forAll(norm, facei)
118  {
119  res[facei] = field[facei] & (norm[facei]/mag(norm[facei]));
120  }
121  }
122  else
123  {
124  res.clear();
125  }
126 }
127 
128 
129 template<class ReturnType, class Type>
130 void Foam::sampledSurface::project
131 (
132  Field<ReturnType>& res,
133  const tmp<Field<Type>>& field
134 ) const
135 {
136  project(res, field());
137  field.clear();
138 }
139 
140 
141 template<class ReturnType, class Type>
143 Foam::sampledSurface::project
144 (
145  const tmp<Field<Type>>& field
146 ) const
147 {
148  tmp<Field<ReturnType>> tRes(new Field<ReturnType>(faces().size()));
149  project(tRes(), field);
150  return tRes;
151 }
152 
153 
154 template<class Type>
157 (
159 ) const
160 {
161  const fvMesh& mesh = dynamic_cast<const fvMesh&>(pfld.mesh()());
162 
164  (
166  (
167  IOobject
168  (
169  "cellAvg",
170  mesh.time().timeName(),
171  pfld.db(),
174  false
175  ),
176  mesh,
177  dimensioned<Type>("zero", dimless, Zero)
178  )
179  );
180  GeometricField<Type, fvPatchField, volMesh>& cellAvg = tcellAvg.ref();
181 
182  labelField nPointCells(mesh.nCells(), 0);
183  {
184  for (label pointi = 0; pointi < mesh.nPoints(); pointi++)
185  {
186  const labelList& pCells = mesh.pointCells(pointi);
187 
188  forAll(pCells, i)
189  {
190  label celli = pCells[i];
191 
192  cellAvg[celli] += pfld[pointi];
193  nPointCells[celli]++;
194  }
195  }
196  }
197  forAll(cellAvg, celli)
198  {
199  cellAvg[celli] /= nPointCells[celli];
200  }
201  // Give value to calculatedFvPatchFields
202  cellAvg.correctBoundaryConditions();
203 
204  return tcellAvg;
205 }
206 
207 
208 // ************************************************************************* //
Type integrate(const Field< Type > &) const
Integration of a field across the surface.
#define forAll(list, i)
Loop across all elements in list.
Definition: UList.H:428
bool empty() const
Return true if the UList is empty (ie, size() is zero)
Definition: UListI.H:313
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
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:124
error FatalError
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:319
T & ref() const
Return non-const reference or generate a fatal error.
Definition: tmpI.H:174
void size(const label)
Override size to be inconsistent with allocated storage.
Definition: ListI.H:163
label nCells() const
Generic GeometricField class.
static word timeName(const scalar, const int precision=precision_)
Return time name of given scalar time.
Definition: Time.C:644
Generic dimensioned Type class.
const Time & time() const
Return the top-level database.
Definition: fvMesh.H:243
dimensioned< Type > sum(const DimensionedField< Type, GeoMesh > &df)
virtual const faceList & faces() const =0
Faces of surface.
tmp< GeometricField< Type, fvPatchField, volMesh > > pointAverage(const GeometricField< Type, pointPatchField, pointMesh > &pfld) const
Interpolate from points to cell centre.
Pre-declare SubField and related Field type.
Definition: Field.H:57
void clear()
Clear the list, i.e. set size to zero.
Definition: ListI.H:124
Type average(const Field< Type > &) const
Area-averaged value of a field across the surface.
static const zero Zero
Definition: zero.H:91
scalar area() const
The total surface area.
const polyMesh & mesh() const
Access to the underlying mesh.
const labelListList & pointCells() const
const Mesh & mesh() const
Return mesh.
virtual const vectorField & Sf() const
Return face area vectors.
void reduce(const List< UPstream::commsStruct > &comms, T &Value, const BinaryOp &bop, const int tag, const label comm)
const dimensionSet dimless(0, 0, 0, 0, 0, 0, 0)
Definition: dimensionSets.H:47
virtual const scalarField & magSf() const
Return face area magnitudes.
Mesh data needed to do the Finite Volume discretisation.
Definition: fvMesh.H:78
void correctBoundaryConditions()
Correct boundary field.
dimensioned< scalar > mag(const dimensioned< Type > &)
label nPoints() const
A class for managing temporary objects.
Definition: PtrList.H:53
const objectRegistry & db() const
Return the local objectRegistry.
Definition: IOobject.C:331
IOobject defines the attributes of an object for which implicit objectRegistry management is supporte...
Definition: IOobject.H:92