averageConditionTemplates.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) 2015 OpenFOAM Foundation
9  Copyright (C) 2015-2016 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 "Time.H"
30 #include "FIFOStack.H"
31 
32 // * * * * * * * * * * * * * Protected Member Functions * * * * * * * * * * //
33 
34 template<class Type>
36 (
37  const label fieldi,
38  bool& satisfied,
39  bool& processed
40 )
41 {
42  const word& fieldName = fieldNames_[fieldi];
43 
44  const word valueType =
45  state_.objectResultType(functionObjectName_, fieldName);
46 
47  if (pTraits<Type>::typeName != valueType)
48  {
49  return;
50  }
51 
52  const scalar dt = obr_.time().deltaTValue();
53 
54  const Type currentValue =
55  state_.getObjectResult<Type>(functionObjectName_, fieldName);
56  const word meanName(fieldName + "Mean");
57 
58  // Current mean value
59  Type meanValue = state_.getResult<Type>(meanName);
60 
61  switch (windowType_)
62  {
63  case windowType::NONE:
64  {
65  const scalar Dt = totalTime_[fieldi];
66  const scalar alpha = (Dt - dt)/Dt;
67  const scalar beta = dt/Dt;
68  meanValue = alpha*meanValue + beta*currentValue;
69 
70  break;
71  }
72  case windowType::APPROXIMATE:
73  {
74  const scalar Dt = totalTime_[fieldi];
75  scalar alpha = (Dt - dt)/Dt;
76  scalar beta = dt/Dt;
77  if (Dt - dt >= window_)
78  {
79  alpha = (window_ - dt)/window_;
80  beta = dt/window_;
81  }
82  else
83  {
84  // Ensure that averaging is performed over window time
85  // before condition can be satisfied
86  satisfied = false;
87  }
88 
89  meanValue = alpha*meanValue + beta*currentValue;
90  totalTime_[fieldi] += dt;
91  break;
92  }
93  case windowType::EXACT:
94  {
95  FIFOStack<scalar> windowTimes;
96  FIFOStack<Type> windowValues;
97  dictionary& dict = this->conditionDict().subDict(fieldName);
98  dict.readIfPresent("windowTimes", windowTimes);
99  dict.readIfPresent("windowValues", windowValues);
100 
101  // Increment time for all existing values
102  for (scalar& dti : windowTimes)
103  {
104  dti += dt;
105  }
106 
107  // Remove any values outside the window
108  bool removeValue = true;
109  while (removeValue && windowTimes.size())
110  {
111  removeValue = windowTimes.first() > window_;
112 
113  if (removeValue)
114  {
115  windowTimes.pop();
116  windowValues.pop();
117  }
118  }
119 
120  // Add the current value
121  windowTimes.push(dt);
122  windowValues.push(currentValue);
123 
124  // Calculate the window average
125  auto timeIter = windowTimes.cbegin();
126  auto valueIter = windowValues.cbegin();
127 
128  meanValue = pTraits<Type>::zero;
129  Type valueOld(pTraits<Type>::zero);
130 
131  for
132  (
133  label i = 0;
134  timeIter.good();
135  ++i, ++timeIter, ++valueIter
136  )
137  {
138  const Type& value = valueIter();
139  const scalar dt = timeIter();
140 
141  meanValue += dt*value;
142 
143  if (i)
144  {
145  meanValue -= dt*valueOld;
146  }
147 
148  valueOld = value;
149  }
150 
151  meanValue /= windowTimes.first();
152 
153  // Store the state information for the next step
154  dict.set("windowTimes", windowTimes);
155  dict.set("windowValues", windowValues);
156 
157  break;
158  }
159  }
160 
161  scalar delta = mag(meanValue - currentValue);
162 
163  Log << " " << meanName << ": " << meanValue
164  << ", delta: " << delta << nl;
165 
166  // Note: Writing result to owner function object and not the local run-time
167  // condition
168  state_.setResult(meanName, meanValue);
169 
170  if (delta > tolerance_)
171  {
172  satisfied = false;
173  }
174 
175  processed = true;
176 }
177 
178 
179 // ************************************************************************* //
FIFOStack.H
Log
#define Log
Definition: PDRblock.C:35
Foam::word
A class for handling words, derived from Foam::string.
Definition: word.H:65
Foam::constant::atomic::alpha
const dimensionedScalar alpha
Fine-structure constant: default SI units: [].
Definition: readThermalProperties.H:212
Foam::FIFOStack::push
void push(const T &element)
Push an element onto the back of the stack.
Definition: FIFOStack.H:78
delta
scalar delta
Definition: LISASMDCalcMethod2.H:8
beta
dimensionedScalar beta("beta", dimless/dimTemperature, laminarTransport)
Foam::functionObjects::runTimeControls::averageCondition::calc
void calc(const label fieldi, bool &satisfied, bool &processed)
Templated function to calculate the average.
Definition: averageConditionTemplates.C:36
dict
dictionary dict
Definition: searchingEngine.H:14
Foam::dictionary
A list of keyword definitions, which are a keyword followed by a number of values (eg,...
Definition: dictionary.H:123
Time.H
Foam::FIFOStack::pop
T pop()
Pop the bottom element off the stack.
Definition: FIFOStack.H:90
Foam::nl
constexpr char nl
Definition: Ostream.H:404
Foam::pTraits
A traits class, which is primarily used for primitives.
Definition: pTraits.H:56
Foam::mag
dimensioned< typename typeOfMag< Type >::type > mag(const dimensioned< Type > &dt)
Foam::LList::cbegin
const_iterator cbegin() const
Iterator to first item in list with const access.
Definition: LList.H:503
Foam::FIFOStack< scalar >