FreeFOAM The Cross-Platform CFD Toolkit
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) 1991-2010 OpenCFD Ltd.
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 
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  (
40  "sampledSurface::checkFieldSize(const Field<Type>&) const"
41  )
42  << "size mismatch: "
43  << "field (" << field.size()
44  << ") != surface (" << faces().size() << ")"
45  << exit(FatalError);
46  }
47 
48  return true;
49 }
50 
51 
52 template<class Type>
54 {
55  Type value = pTraits<Type>::zero;
56 
57  if (checkFieldSize(field))
58  {
59  value = sum(field * magSf());
60  }
61 
62  reduce(value, sumOp<Type>());
63  return value;
64 }
65 
66 
67 template<class Type>
69 {
70  Type value = integrate(field());
71  field.clear();
72  return value;
73 }
74 
75 
76 template<class Type>
78 {
79  Type value = pTraits<Type>::zero;
80 
81  if (checkFieldSize(field))
82  {
83  value = sum(field * magSf());
84  }
85 
86  reduce(value, sumOp<Type>());
87 
88  // avoid divide-by-zero
89  if (area())
90  {
91  return value / area();
92  }
93  else
94  {
95  return pTraits<Type>::zero;
96  }
97 }
98 
99 
100 template<class Type>
102 {
103  Type value = average(field());
104  field.clear();
105  return value;
106 }
107 
108 
109 template<class ReturnType, class Type>
110 void Foam::sampledSurface::project
111 (
112  Field<ReturnType>& res,
113  const Field<Type>& field
114 ) const
115 {
116  if (checkFieldSize(field))
117  {
118  const vectorField& norm = Sf();
119 
120  forAll(norm, faceI)
121  {
122  res[faceI] = field[faceI] & (norm[faceI] / mag(norm[faceI]));
123  }
124  }
125  else
126  {
127  res.clear();
128  }
129 }
130 
131 
132 template<class ReturnType, class Type>
133 void Foam::sampledSurface::project
134 (
135  Field<ReturnType>& res,
136  const tmp<Field<Type> >& field
137 ) const
138 {
139  project(res, field());
140  field.clear();
141 }
142 
143 
144 template<class ReturnType, class Type>
146 Foam::sampledSurface::project
147 (
148  const tmp<Field<Type> >& field
149 ) const
150 {
151  tmp<Field<ReturnType> > tRes(new Field<ReturnType>(faces().size()));
152  project(tRes(), field);
153  return tRes;
154 }
155 
156 
157 // ************************ vim: set sw=4 sts=4 et: ************************ //