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 -------------------------------------------------------------------------------
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 "valueAverageBase.H"
30 #include "Time.H"
31 
32 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
33 
34 const 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);
142 }
143 
144 
145 // * * * * * * * * * * * * * * Public Member Functions * * * * * * * * * * * //
146 
148 {
149  if (writeFile::read(dict))
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 // ************************************************************************* //
virtual OFstream & file()
Return access to the file (if only 1)
Definition: writeFile.C:264
dictionary dict
virtual void writeState(dictionary &dict)
Write state to dictionary for restarts.
Ostream & indent(Ostream &os)
Indent stream.
Definition: Ostream.H:493
dimensionedScalar log(const dimensionedScalar &ds)
virtual void writeHeader(Ostream &os, const string &str) const
Write a commented header to stream.
Definition: writeFile.C:339
A list of keyword definitions, which are a keyword followed by a number of values (eg...
Definition: dictionary.H:129
constexpr char nl
The newline &#39;\n&#39; character (0x0a)
Definition: Ostream.H:50
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:531
constexpr char tab
The tab &#39;\t&#39; character(0x09)
Definition: Ostream.H:49
static const Enum< windowType > windowTypeNames
Names for windowType enumeration.
virtual bool calculate(dictionary &dict)
Calculate the averages.
#define forAll(list, i)
Loop across all elements in list.
Definition: stdFoam.H:421
wordList fieldNames_
List of fields on which to operate.
fileName::Type type(const fileName &name, const bool followLink=true)
Return the file type: DIRECTORY or FILE, normally following symbolic links.
Definition: POSIX.C:799
virtual void writeCommented(Ostream &os, const string &str) const
Write a commented string to stream.
Definition: writeFile.C:313
word name(const expressions::valueTypeCode typeCode)
A word representation of a valueTypeCode. Empty for expressions::valueTypeCode::INVALID.
Definition: exprTraits.C:127
virtual void readState(dictionary &dict)
Read state from dictionary.
A class for handling words, derived from Foam::string.
Definition: word.H:63
virtual bool read(const dictionary &dict)
Read the field value average data.
virtual void writeFileHeader(Ostream &os) const
Output file header information.
OBJstream os(runTime.globalPath()/outputName)
virtual bool read(const dictionary &dict)
Read.
Definition: writeFile.C:241
#define WarningInFunction
Report a warning using Foam::Warning.
Enum is a wrapper around a list of names/values that represent particular enumeration (or int) values...
Definition: error.H:64
valueAverageBase(const word &name, const objectRegistry &obr, const dictionary &dict, stateFunctionObject &state, const bool writeToFile=true)
Construct from Time and dictionary.
#define Log
Definition: PDRblock.C:28
Registry of regIOobjects.
virtual void resetState(dictionary &dict)
Reset state.
static constexpr const zero Zero
Global zero (0)
Definition: zero.H:127
virtual void writeTabbed(Ostream &os, const string &str) const
Write a tabbed string to stream.
Definition: writeFile.C:329