FreeFOAM The Cross-Platform CFD Toolkit
faceSourceTemplates.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) 2009-2011 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 
26 #include "faceSource.H"
28 #include <finiteVolume/volFields.H>
30 
31 // * * * * * * * * * * * * Protected Member Functions * * * * * * * * * * * //
32 
33 template<class Type>
34 bool Foam::fieldValues::faceSource::validField(const word& fieldName) const
35 {
38 
39  if (source_ != stSampledSurface && obr_.foundObject<sf>(fieldName))
40  {
41  return true;
42  }
43  else if (obr_.foundObject<vf>(fieldName))
44  {
45  return true;
46  }
47 
48  return false;
49 }
50 
51 
52 template<class Type>
54 (
55  const word& fieldName
56 ) const
57 {
60 
61  if (source_ != stSampledSurface && obr_.foundObject<sf>(fieldName))
62  {
63  return filterField(obr_.lookupObject<sf>(fieldName), true);
64  }
65  else if (obr_.foundObject<vf>(fieldName))
66  {
67  if (surfacePtr_.valid())
68  {
69  return surfacePtr_().sample(obr_.lookupObject<vf>(fieldName));
70  }
71  else
72  {
73  return filterField(obr_.lookupObject<vf>(fieldName), true);
74  }
75  }
76 
77  return tmp<Field<Type> >(new Field<Type>(0));
78 }
79 
80 
81 template<class Type>
83 (
84  const Field<Type>& values,
85  const scalarField& magSf,
86  const scalarField& weightField
87 ) const
88 {
89  Type result = pTraits<Type>::zero;
90  switch (operation_)
91  {
92  case opSum:
93  {
94  result = sum(values);
95  break;
96  }
97  case opAreaAverage:
98  {
99  result = sum(values*magSf)/sum(magSf);
100  break;
101  }
102  case opAreaIntegrate:
103  {
104  result = sum(values*magSf);
105  break;
106  }
107  case opWeightedAverage:
108  {
109  result = sum(values*weightField)/sum(weightField);
110  break;
111  }
112  case opMin:
113  {
114  result = min(values);
115  break;
116  }
117  case opMax:
118  {
119  result = max(values);
120  break;
121  }
122  default:
123  {
124  // Do nothing
125  }
126  }
127 
128  return result;
129 }
130 
131 
132 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
133 
134 template<class Type>
136 {
137  const bool ok = validField<Type>(fieldName);
138 
139  if (ok)
140  {
141  // Get (correctly oriented) field
142  Field<Type> values = combineFields(setFieldValues<Type>(fieldName)());
143 
144  // Get unoriented magSf
145  scalarField magSf;
146 
147  if (surfacePtr_.valid())
148  {
149  magSf = combineFields(surfacePtr_().magSf());
150  }
151  else
152  {
153  magSf = combineFields(filterField(mesh().magSf(), false)());
154  }
155 
156  // Get (correctly oriented) weighting field
157  scalarField weightField =
158  combineFields(setFieldValues<scalar>(weightFieldName_)());
159 
160  if (Pstream::master())
161  {
162  Type result = processValues(values, magSf, weightField);
163 
164  if (valueOutput_)
165  {
167  (
168  IOobject
169  (
170  fieldName + "_" + sourceTypeNames_[source_] + "-"
171  + sourceName_,
172  obr_.time().timeName(),
173  obr_,
176  ),
177  values
178  ).write();
179  }
180 
181  outputFilePtr_()<< tab << result;
182 
183  if (log_)
184  {
185  Info<< " " << operationTypeNames_[operation_]
186  << "(" << sourceName_ << ") for " << fieldName
187  << " = " << result << endl;
188  }
189  }
190  }
191 
192  return ok;
193 }
194 
195 
196 template<class Type>
198 (
200  const bool applyOrientation
201 ) const
202 {
203  tmp<Field<Type> > tvalues(new Field<Type>(faceId_.size()));
204  Field<Type>& values = tvalues();
205 
206  forAll(values, i)
207  {
208  label faceI = faceId_[i];
209  label patchI = facePatchId_[i];
210  if (patchI >= 0)
211  {
212  values[i] = field.boundaryField()[patchI][faceI];
213  }
214  else
215  {
217  (
218  "fieldValues::faceSource::filterField"
219  "("
220  "const GeometricField<Type, fvPatchField, volMesh>&"
221  ") const"
222  ) << type() << " " << name_ << ": "
223  << sourceTypeNames_[source_] << "(" << sourceName_ << "):"
224  << nl
225  << " Unable to process internal faces for volume field "
226  << field.name() << nl << abort(FatalError);
227  }
228  }
229 
230  if (applyOrientation)
231  {
232  forAll(values, i)
233  {
234  values[i] *= faceSign_[i];
235  }
236  }
237 
238  return tvalues;
239 }
240 
241 
242 template<class Type>
244 (
246  const bool applyOrientation
247 ) const
248 {
249  tmp<Field<Type> > tvalues(new Field<Type>(faceId_.size()));
250  Field<Type>& values = tvalues();
251 
252  forAll(values, i)
253  {
254  label faceI = faceId_[i];
255  label patchI = facePatchId_[i];
256  if (patchI >= 0)
257  {
258  values[i] = field.boundaryField()[patchI][faceI];
259  }
260  else
261  {
262  values[i] = field[faceI];
263  }
264  }
265 
266  if (applyOrientation)
267  {
268  forAll(values, i)
269  {
270  values[i] *= faceSign_[i];
271  }
272  }
273 
274  return tvalues;
275 }
276 
277 
278 // ************************ vim: set sw=4 sts=4 et: ************************ //
279