valueAverageBaseTemplates.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-2022 OpenCFD Ltd.
10-------------------------------------------------------------------------------
11License
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// * * * * * * * * * * * * * Protected Member Functions * * * * * * * * * * //
30
31template<class Type, class Type2>
33(
34 const label fieldi,
35 bool& converged,
37)
38{
39 const word& fieldName = fieldNames_[fieldi];
40
41 const word valueType =
43
44 if (pTraits<Type>::typeName != valueType)
45 {
46 return false;
47 }
48
49 const scalar dt = state_.time().deltaTValue();
50
51 const Type2 currentValue =
53
54 // Current mean value
55 const word meanName(fieldName + "Mean");
56 Type2 meanValue = state_.getResult<Type2>(meanName);
57
58 switch (windowType_)
59 {
61 {
62 const scalar Dt = totalTime_[fieldi];
63 const scalar beta = dt/Dt;
64 meanValue = (1 - beta)*meanValue + beta*currentValue;
65
66 break;
67 }
69 {
70 const scalar Dt = totalTime_[fieldi];
71 scalar beta = dt/Dt;
72 if (Dt - dt >= window_)
73 {
74 beta = dt/window_;
75 }
76 else
77 {
78 converged = false;
79 }
80
81 meanValue = (1 - beta)*meanValue + beta*currentValue;
82
83 break;
84 }
86 {
87 FIFOStack<scalar> windowTimes;
88 FIFOStack<Type2> windowValues;
89 dictionary& fieldDict = dict.subDict(fieldName);
90 fieldDict.readIfPresent("windowTimes", windowTimes);
91 fieldDict.readIfPresent("windowValues", windowValues);
92
93 // Increment time for all existing values
94 for (scalar& dti : windowTimes)
95 {
96 dti += dt;
97 }
98
99 // Remove any values outside the window
100 bool removeValue = true;
101 while (removeValue && windowTimes.size())
102 {
103 removeValue = windowTimes.first() > window_;
104
105 if (removeValue)
106 {
107 windowTimes.pop();
108 windowValues.pop();
109 }
110 }
111
112 // Add the current value
113 windowTimes.push(dt);
114 windowValues.push(currentValue);
115
116 // Calculate the window average
117 auto timeIter = windowTimes.cbegin();
118 auto valueIter = windowValues.cbegin();
119
120 meanValue = pTraits<Type2>::zero;
121 Type valueOld(pTraits<Type2>::zero);
122
123 for
124 (
125 label i = 0;
126 timeIter.good();
127 ++i, ++timeIter, ++valueIter
128 )
129 {
130 const Type2& value = valueIter();
131 const scalar dt = timeIter();
132
133 meanValue += dt*value;
134
135 if (i)
136 {
137 meanValue -= dt*valueOld;
138 }
139
140 valueOld = value;
141 }
142
143 meanValue /= windowTimes.first();
144
145 // Store the state information for the next step
146 fieldDict.set("windowTimes", windowTimes);
147 fieldDict.set("windowValues", windowValues);
148
149 break;
150 }
151 }
152
153 scalar delta = mag(meanValue - currentValue);
154
155 Log << indent << " " << meanName << ": " << meanValue
156 << ", delta: " << delta << nl;
157
158 file() << tab << meanValue;
159
160 state_.setResult(meanName, meanValue);
161
162 if ((tolerance_ > 0) && (delta > tolerance_))
163 {
164 converged = false;
165 }
166
167 return true;
168}
169
170
171// ************************************************************************* //
scalar delta
#define Log
Definition: PDRblock.C:35
A FIFO stack based on a singly-linked list.
Definition: FIFOStack.H:54
void push(const T &elem)
Push an element onto the back of the stack.
Definition: FIFOStack.H:78
T pop()
Pop the bottom element off the stack.
Definition: FIFOStack.H:90
const_iterator cbegin() const
Iterator to first item in list with const access.
Definition: LList.H:509
reference first()
The first entry in the list.
Definition: LList.H:208
scalar deltaTValue() const noexcept
Return time step value.
Definition: TimeStateI.H:43
A list of keyword definitions, which are a keyword followed by a number of values (eg,...
Definition: dictionary.H:126
const dictionary & subDict(const word &keyword, enum keyType::option matchOpt=keyType::REGEX) const
Find and return a sub-dictionary.
Definition: dictionary.C:460
bool readIfPresent(const word &keyword, T &val, enum keyType::option matchOpt=keyType::REGEX) const
entry * set(entry *entryPtr)
Assign a new entry, overwriting any existing entry.
Definition: dictionary.C:780
Computes the magnitude of an input field.
Definition: mag.H:153
void setResult(const word &entryName, const Type &value)
Add result.
word objectResultType(const word &objectName, const word &entryName) const
Return the type of result.
Type getResult(const word &entryName, const Type &defaultValue=Type(Zero)) const
Retrieve result.
Type getObjectResult(const word &objectName, const word &entryName, const Type &defaultValue=Type(Zero)) const
Retrieve result from named object.
const Time & time() const
Return time database.
word functionObjectName_
Name of function object to retrieve data from.
scalar tolerance_
Optional tolerance to check for converged results.
wordList fieldNames_
List of fields on which to operate.
List< scalar > totalTime_
Average time per field.
bool calc(const label fieldi, bool &converged, dictionary &dict)
Templated function to calculate the average.
stateFunctionObject & state_
Reference to the state functionObject.
virtual OFstream & file()
Return access to the file (if only 1)
Definition: writeFile.C:233
A traits class, which is primarily used for primitives.
Definition: pTraits.H:59
A class for handling words, derived from Foam::string.
Definition: word.H:68
Ostream & indent(Ostream &os)
Indent stream.
Definition: Ostream.H:342
constexpr char nl
The newline '\n' character (0x0a)
Definition: Ostream.H:53
constexpr char tab
The tab '\t' character(0x09)
Definition: Ostream.H:52
dictionary dict
dimensionedScalar beta("beta", dimless/dimTemperature, laminarTransport)
A non-counting (dummy) refCount.
Definition: refCount.H:59