averageCondition.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-2020 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 "averageCondition.H"
31 #include "Time.H"
32 
33 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
34 
35 namespace Foam
36 {
37 namespace functionObjects
38 {
39 namespace runTimeControls
40 {
43 }
44 }
45 }
46 
47 const Foam::Enum
48 <
50 >
52 ({
53  { windowType::NONE, "none" },
54  { windowType::APPROXIMATE, "approximate" },
55  { windowType::EXACT, "exact" }
56 });
57 
58 
59 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
60 
62 (
63  const word& name,
64  const objectRegistry& obr,
65  const dictionary& dict,
66  stateFunctionObject& state
67 )
68 :
69  runTimeCondition(name, obr, dict, state),
70  functionObjectName_(dict.get<word>("functionObject")),
71  fieldNames_(dict.get<wordList>("fields")),
72  tolerance_(dict.get<scalar>("tolerance")),
73  window_(dict.getOrDefault<scalar>("window", -1)),
74  windowType_
75  (
76  window_ > 0
77  ? windowTypeNames.get("windowType", dict)
78  : windowType::NONE
79  ),
80  totalTime_(fieldNames_.size(), scalar(0)),
81  resetOnRestart_(dict.getOrDefault("resetOnRestart", false)),
82  nIterStartUp_(dict.getOrDefault<label>("nIterStartUp", 10)),
83  iter_(-1)
84 {
85  dictionary& conditionDict = this->conditionDict();
86 
87  forAll(fieldNames_, fieldi)
88  {
89  const word& fieldName = fieldNames_[fieldi];
90 
91  if (resetOnRestart_)
92  {
93  conditionDict.set(fieldName, dictionary());
94  }
95  else
96  {
97  if (conditionDict.found(fieldName))
98  {
99  const dictionary& valueDict = conditionDict.subDict(fieldName);
100  valueDict.readIfPresent("totalTime", totalTime_[fieldi]);
101  }
102  else
103  {
104  conditionDict.set(fieldName, dictionary());
105  }
106  }
107  }
108 
109  conditionDict.readIfPresent("iter", iter_);
110 }
111 
112 
113 // * * * * * * * * * * * * * * Public Member Functions * * * * * * * * * * * //
114 
116 {
117  if (!active_)
118  {
119  return true;
120  }
121 
122  bool satisfied = iter_ > nIterStartUp_;
123 
124  ++iter_;
125 
126  const scalar dt = obr_.time().deltaTValue();
127 
128  Log << " " << type() << ": " << name_ << " averages:" << nl;
129 
130  DynamicList<label> unprocessedFields(fieldNames_.size());
131 
132  forAll(fieldNames_, fieldi)
133  {
134  totalTime_[fieldi] += dt;
135 
136  bool processed = false;
137  calc<scalar>(fieldi, satisfied, processed);
138  calc<vector>(fieldi, satisfied, processed);
139  calc<sphericalTensor>(fieldi, satisfied, processed);
140  calc<symmTensor>(fieldi, satisfied, processed);
141  calc<tensor>(fieldi, satisfied, processed);
142 
143  if (!processed)
144  {
145  unprocessedFields.append(fieldi);
146  }
147  }
148 
149  if (unprocessedFields.size())
150  {
152  << "From function object: " << functionObjectName_ << nl
153  << "Unprocessed fields:" << nl;
154 
155  for (const label fieldi : unprocessedFields)
156  {
157  Info<< " " << fieldNames_[fieldi] << nl;
158  }
159 
160  if (unprocessedFields.size() == fieldNames_.size())
161  {
162  satisfied = false;
163  }
164  }
165 
166  Log << endl;
167 
168  return satisfied;
169 }
170 
171 
173 {
174  dictionary& conditionDict = this->conditionDict();
175 
176  forAll(fieldNames_, fieldi)
177  {
178  const word& fieldName = fieldNames_[fieldi];
179 
180  if (conditionDict.found(fieldName))
181  {
182  dictionary& valueDict = conditionDict.subDict(fieldName);
183  valueDict.add("totalTime", totalTime_[fieldi], true);
184  }
185  else
186  {
187  dictionary valueDict;
188  valueDict.add("totalTime", totalTime_[fieldi], true);
189  conditionDict.add(fieldName, valueDict);
190  }
191  }
192 
193  conditionDict.set("iter", iter_);
194 }
195 
196 
197 // ************************************************************************* //
Foam::functionObjects::runTimeControls::defineTypeNameAndDebug
defineTypeNameAndDebug(averageCondition, 0)
Foam::Enum
Enum is a wrapper around a list of names/values that represent particular enumeration (or int) values...
Definition: IOstreamOption.H:57
Log
#define Log
Definition: PDRblock.C:35
Foam::functionObjects::runTimeControls::runTimeCondition
Base class for run time conditions.
Definition: runTimeCondition.H:61
Foam::functionObjects::runTimeControls::averageCondition::totalTime_
List< scalar > totalTime_
Average time per field.
Definition: averageCondition.H:97
Foam::word
A class for handling words, derived from Foam::string.
Definition: word.H:65
Foam::functionObjects::runTimeControls::runTimeCondition::active_
bool active_
On/off switch.
Definition: runTimeCondition.H:78
Foam::DynamicList< label >
Foam::dictionary::found
bool found(const word &keyword, enum keyType::option matchOpt=keyType::REGEX) const
Search for an entry (const access) with the given keyword.
Definition: dictionaryI.H:87
Foam::functionObjects::runTimeControls::addToRunTimeSelectionTable
addToRunTimeSelectionTable(runTimeCondition, averageCondition, dictionary)
Foam::functionObjects::runTimeControls::averageCondition::nIterStartUp_
label nIterStartUp_
Number of start-up iterations before allowing satisfied checks.
Definition: averageCondition.H:103
Foam::functionObjects::stateFunctionObject
Base class for function objects, adding functionality to read/write state information (data required ...
Definition: stateFunctionObject.H:69
Foam::endl
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:369
Foam::dictionary::set
entry * set(entry *entryPtr)
Assign a new entry, overwriting any existing entry.
Definition: dictionary.C:780
Foam::TimeState::deltaTValue
scalar deltaTValue() const noexcept
Return time step value.
Definition: TimeStateI.H:43
forAll
#define forAll(list, i)
Loop across all elements in list.
Definition: stdFoam.H:296
Foam::objectRegistry
Registry of regIOobjects.
Definition: objectRegistry.H:60
Foam::functionObjects::runTimeControls::averageCondition
Average run time condition - satisfied when average does not change by more than a given value.
Definition: averageCondition.H:59
Foam::functionObjects::runTimeControls::averageCondition::functionObjectName_
word functionObjectName_
Name of function object to retrieve data from.
Definition: averageCondition.H:82
Foam::functionObjects::runTimeControls::runTimeCondition::obr_
const objectRegistry & obr_
Reference to the object registry.
Definition: runTimeCondition.H:72
Foam::functionObjects::runTimeControls::averageCondition::windowType
windowType
Definition: averageCondition.H:67
Foam::Info
messageStream Info
Information stream (stdout output on master, null elsewhere)
Foam::functionObjects::runTimeControls::averageCondition::iter_
label iter_
Current iteration count.
Definition: averageCondition.H:106
Foam::dictionary::subDict
const dictionary & subDict(const word &keyword, enum keyType::option matchOpt=keyType::REGEX) const
Find and return a sub-dictionary.
Definition: dictionary.C:460
Foam::functionObjects::runTimeControls::averageCondition::write
virtual void write()
Write.
Definition: averageCondition.C:172
averageCondition.H
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
addToRunTimeSelectionTable.H
Macros for easy insertion into run-time selection tables.
Foam::functionObjects::runTimeControls::averageCondition::apply
virtual bool apply()
Apply the condition.
Definition: averageCondition.C:115
Foam
Namespace for OpenFOAM.
Definition: atmBoundaryLayer.C:33
Foam::functionObjects::runTimeControls::averageCondition::averageCondition
averageCondition(const word &name, const objectRegistry &obr, const dictionary &dict, stateFunctionObject &state)
Constructor.
Definition: averageCondition.C:62
Foam::functionObjects::runTimeControls::averageCondition::fieldNames_
wordList fieldNames_
List of fields on which to operate.
Definition: averageCondition.H:85
Time.H
Foam::nl
constexpr char nl
Definition: Ostream.H:404
Foam::List< word >
Foam::type
fileName::Type type(const fileName &name, const bool followLink=true)
Return the file type: DIRECTORY or FILE, normally following symbolic links.
Definition: MSwindows.C:590
Foam::name
word name(const expressions::valueTypeCode typeCode)
A word representation of a valueTypeCode. Empty for INVALID.
Definition: exprTraits.C:59
Foam::dictionary::add
entry * add(entry *entryPtr, bool mergeEntry=false)
Add a new entry.
Definition: dictionary.C:640
Foam::functionObjects::runTimeControls::runTimeCondition::name_
word name_
Condition name.
Definition: runTimeCondition.H:69
WarningInFunction
#define WarningInFunction
Report a warning using Foam::Warning.
Definition: messageStream.H:328
Foam::objectRegistry::time
const Time & time() const noexcept
Return time registry.
Definition: objectRegistry.H:178
Foam::dictionary::readIfPresent
bool readIfPresent(const word &keyword, T &val, enum keyType::option matchOpt=keyType::REGEX) const
Definition: dictionaryTemplates.C:405
Foam::functionObjects::runTimeControls::averageCondition::windowTypeNames
static const Enum< windowType > windowTypeNames
Definition: averageCondition.H:74