valueAverageBase.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#include "valueAverageBase.H"
30#include "Time.H"
31
32// * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
33
34const Foam::Enum
35<
37>
39({
40 { windowType::NONE, "none" },
41 { windowType::APPROXIMATE, "approximate" },
42 { windowType::EXACT, "exact" }
43});
44
45
46// * * * * * * * * * * * * * * Protected Member Functions * * * * * * * * * //
47
49{
50 writeHeader(os, "Value averages");
51 writeCommented(os, "Time");
52 forAll(fieldNames_, fieldi)
53 {
54 writeTabbed(os, fieldNames_[fieldi]);
55 }
56 os << endl;
57}
58
59
61{
62 if (resetOnRestart_)
63 {
64 resetState(dict);
65 return;
66 }
67
68 forAll(fieldNames_, fieldi)
69 {
70 const word& fieldName = fieldNames_[fieldi];
71
72 if (dict.found(fieldName))
73 {
74 const dictionary& valueDict = dict.subDict(fieldName);
75 valueDict.readEntry("totalTime", totalTime_[fieldi]);
76 }
77 else
78 {
79 dict.set(fieldName, dictionary());
80 totalTime_[fieldi] = 0;
81 }
82 }
83}
84
85
87{
88 forAll(fieldNames_, fieldi)
89 {
90 const word& fieldName = fieldNames_[fieldi];
91
92 if (dict.found(fieldName))
93 {
94 dictionary& valueDict = dict.subDict(fieldName);
95 valueDict.add("totalTime", totalTime_[fieldi], true);
96 }
97 else
98 {
99 dictionary valueDict;
100 valueDict.add("totalTime", totalTime_[fieldi], true);
101 dict.add(fieldName, valueDict);
102 }
103 }
104}
105
106
108{
109 forAll(fieldNames_, fieldi)
110 {
111 dict.set(fieldNames_[fieldi], dictionary());
112 totalTime_[fieldi] = 0;
113 }
114}
115
116
117// * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
118
120(
121 const word& name,
122 const objectRegistry& obr,
123 const dictionary& dict,
124 stateFunctionObject& state,
125 const bool writeToFile
126)
127:
128 writeFile(obr, name, state.type(), dict, writeToFile),
129 log(true),
130 resetOnRestart_(false),
131 windowType_(windowType::NONE),
132 state_(state),
133 functionObjectName_("unknown-functionObject"),
134 fieldNames_(),
135 tolerance_(dict.getOrDefault<scalar>("tolerance", -1)),
136 window_(-1),
137 totalTime_()
138{
139 read(dict);
140
142}
143
144
145// * * * * * * * * * * * * * * Public Member Functions * * * * * * * * * * * //
146
148{
150 {
151 // Make certain that the values are consistent with the defaults:
152 resetOnRestart_ = false;
153
154 dict.readEntry("functionObject", functionObjectName_);
155 dict.readEntry("fields", fieldNames_);
156 if (dict.readIfPresent("window", window_))
157 {
158 window_ = state_.time().userTimeToTime(window_);
159
160 if (window_ > 0)
161 {
162 windowType_ = windowTypeNames.get("windowType", dict);
163 }
164 }
165
166 totalTime_.resize(fieldNames_.size(), Zero);
167
168 dict.readIfPresent("resetOnRestart", resetOnRestart_);
169
170 dict.readIfPresent("log", log);
171
172 return true;
173 }
174
175 return false;
176}
177
178
180{
181 scalar dt = state_.time().deltaTValue();
182
183 Log << indent << state_.type() << ": " << prefix_.c_str()
184 << " averages:" << nl;
185
186 file() << state_.time().timeName();
187
188 DynamicList<word> unprocessedFields(fieldNames_.size());
189
190 bool converged = true;
191
192 forAll(fieldNames_, fieldi)
193 {
194 totalTime_[fieldi] += dt;
195
196 const bool processed =
197 (
198 calc<label, scalar>(fieldi, converged, dict)
199 || calc<scalar>(fieldi, converged, dict)
200 || calc<vector>(fieldi, converged, dict)
201 || calc<sphericalTensor>(fieldi, converged, dict)
202 || calc<symmTensor>(fieldi, converged, dict)
203 || calc<tensor>(fieldi, converged, dict)
204 );
205
206 if (!processed)
207 {
208 unprocessedFields.append(fieldNames_[fieldi]);
209
210 file() << tab << "n/a";
211 }
212 }
213
214 file() << endl;
215
216 if (unprocessedFields.size())
217 {
219 << "From function object: " << functionObjectName_ << nl
220 << "Unprocessed fields:" << nl;
221
222 for (const word& fieldName : unprocessedFields)
223 {
224 Log << indent << " " << fieldName << nl;
225 }
226
227 if (unprocessedFields.size() == fieldNames_.size())
228 {
229 converged = false;
230 }
231 }
232
233 Log << endl;
234
235 return converged;
236}
237
238
239// ************************************************************************* //
#define Log
Definition: PDRblock.C:35
A 1D vector of objects of type <T> that resizes itself as necessary to accept the new objects.
Definition: DynamicList.H:72
void append(const T &val)
Copy append an element to the end of this list.
Definition: DynamicListI.H:503
Enum is a wrapper around a list of names/values that represent particular enumeration (or int) values...
Definition: Enum.H:61
An Ostream is an abstract base class for all output systems (streams, files, token lists,...
Definition: Ostream.H:62
virtual bool read()
Re-read model coefficients if they have changed.
void size(const label n)
Older name for setAddressableSize.
Definition: UList.H:114
A list of keyword definitions, which are a keyword followed by a number of values (eg,...
Definition: dictionary.H:126
entry * add(entry *entryPtr, bool mergeEntry=false)
Add a new entry.
Definition: dictionary.C:640
bool readEntry(const word &keyword, T &val, enum keyType::option matchOpt=keyType::REGEX, bool mandatory=true) const
Computes the natural logarithm of an input volScalarField.
Definition: log.H:230
Base class for function objects, adding functionality to read/write state information (data required ...
Base class that computes the ensemble- or time-based singular-value average values,...
wordList fieldNames_
List of fields on which to operate.
virtual void writeState(dictionary &dict)
Write state to dictionary for restarts.
virtual bool calculate(dictionary &dict)
Calculate the averages.
virtual bool read(const dictionary &dict)
Read the field value average data.
virtual void writeFileHeader(Ostream &os) const
Output file header information.
static const Enum< windowType > windowTypeNames
Names for windowType enumeration.
virtual void resetState(dictionary &dict)
Reset state.
Base class for writing single files from the function objects.
Definition: writeFile.H:120
virtual void writeTabbed(Ostream &os, const string &str) const
Write a tabbed string to stream.
Definition: writeFile.C:285
virtual void writeHeader(Ostream &os, const string &str) const
Write a commented header to stream.
Definition: writeFile.C:295
virtual OFstream & file()
Return access to the file (if only 1)
Definition: writeFile.C:233
virtual void writeCommented(Ostream &os, const string &str) const
Write a commented string to stream.
Definition: writeFile.C:269
bool readState()
Read state from file, applying relaxation as requested.
Registry of regIOobjects.
A class for handling words, derived from Foam::string.
Definition: word.H:68
OBJstream os(runTime.globalPath()/outputName)
#define WarningInFunction
Report a warning using Foam::Warning.
fileName::Type type(const fileName &name, const bool followLink=true)
Return the file type: DIRECTORY or FILE, normally following symbolic links.
Definition: MSwindows.C:598
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:372
Ostream & indent(Ostream &os)
Indent stream.
Definition: Ostream.H:342
static constexpr const zero Zero
Global zero (0)
Definition: zero.H:131
word name(const expressions::valueTypeCode typeCode)
A word representation of a valueTypeCode. Empty for INVALID.
Definition: exprTraits.C:59
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
#define forAll(list, i)
Loop across all elements in list.
Definition: stdFoam.H:333