equationMaxIterCondition.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 
31 #include "fvMesh.H"
32 #include "Time.H"
33 
34 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
35 
36 namespace Foam
37 {
38 namespace functionObjects
39 {
40 namespace runTimeControls
41 {
42  defineTypeNameAndDebug(equationMaxIterCondition, 0);
44  (
45  runTimeCondition,
46  equationMaxIterCondition,
47  dictionary
48  );
49 }
50 }
51 }
52 
53 
54 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
55 
58 (
59  const word& name,
60  const objectRegistry& obr,
61  const dictionary& dict,
62  stateFunctionObject& state
63 )
64 :
65  runTimeCondition(name, obr, dict, state),
66  fieldNames_(dict.get<wordList>("fields")),
67  threshold_(dict.get<label>("threshold")),
68  startIter_(dict.getOrDefault("startIter", 2))
69 {
70  if (!fieldNames_.size())
71  {
73  << "No fields supplied: deactivating" << endl;
74 
75  active_ = false;
76  }
77 
78  startIter_ = max(startIter_, 2);
79 }
80 
81 
82 // * * * * * * * * * * * * * * Public Member Functions * * * * * * * * * * * //
83 
85 {
86  bool satisfied = false;
87 
88  if (!active_)
89  {
90  return true;
91  }
92 
93  if (obr_.time().timeIndex() < startIter_)
94  {
95  // Do not start checking until start iter
96  return false;
97  }
98 
99  const fvMesh& mesh = refCast<const fvMesh>(obr_);
100  const dictionary& solverDict = mesh.solverPerformanceDict();
101 
102  List<label> result(fieldNames_.size(), -1);
103 
104  forAll(fieldNames_, fieldi)
105  {
106  const word& fieldName = fieldNames_[fieldi];
107 
108  if (solverDict.found(fieldName))
109  {
110  const List<solverPerformance> sp(solverDict.lookup(fieldName));
111  const label nIterations = sp.first().nIterations();
112  result[fieldi] = nIterations;
113 
114  if (nIterations > threshold_)
115  {
116  satisfied = true;
117  }
118  }
119  }
120 
121  bool valid = false;
122  forAll(result, i)
123  {
124  if (result[i] < 0)
125  {
127  << "Number of iterations data not found for field "
128  << fieldNames_[i] << endl;
129  }
130  else
131  {
132  valid = true;
133  }
134  }
135 
136  if (!valid)
137  {
139  << "Number of iterations data not found for any fields: "
140  << "deactivating" << endl;
141 
142  active_ = false;
143  }
144 
145  if (satisfied && valid)
146  {
147  Log << type() << ": " << name_
148  << ": satisfied using threshold value: " << threshold_ << nl;
149 
150  forAll(result, resulti)
151  {
152  if (result[resulti] != -1)
153  {
154  Log << " field: " << fieldNames_[resulti]
155  << ", iterations: " << result[resulti] << nl;
156  }
157  }
158  Log << endl;
159  }
160 
161  return satisfied;
162 }
163 
164 
166 {
167  // do nothing
168 }
169 
170 
171 // ************************************************************************* //
Foam::functionObjects::runTimeControls::defineTypeNameAndDebug
defineTypeNameAndDebug(averageCondition, 0)
Log
#define Log
Definition: PDRblock.C:35
Foam::functionObjects::runTimeControls::runTimeCondition
Base class for run time conditions.
Definition: runTimeCondition.H:61
Foam::functionObjects::runTimeControls::equationMaxIterCondition::equationMaxIterCondition
equationMaxIterCondition(const word &name, const objectRegistry &obr, const dictionary &dict, stateFunctionObject &state)
Constructor.
Definition: equationMaxIterCondition.C:58
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::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::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::functionObjects::runTimeControls::equationMaxIterCondition::apply
virtual bool apply()
Apply the condition.
Definition: equationMaxIterCondition.C:84
Foam::functionObjects::runTimeControls::equationMaxIterCondition::threshold_
const label threshold_
Threshold for maximum number of iterations.
Definition: equationMaxIterCondition.H:68
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::equationMaxIterCondition::startIter_
label startIter_
Start checking from iteration - always skips first iteration.
Definition: equationMaxIterCondition.H:71
Foam::TimeState::timeIndex
label timeIndex() const noexcept
Return current time index.
Definition: TimeStateI.H:37
Foam::functionObjects::runTimeControls::runTimeCondition::obr_
const objectRegistry & obr_
Reference to the object registry.
Definition: runTimeCondition.H:72
equationMaxIterCondition.H
Foam::functionObjects::runTimeControls::equationMaxIterCondition::fieldNames_
const wordList fieldNames_
Field name.
Definition: equationMaxIterCondition.H:65
Foam::max
label max(const labelHashSet &set, label maxValue=labelMin)
Find the max value in labelHashSet, optionally limited by second argument.
Definition: hashSets.C:47
Foam::dictionary::lookup
ITstream & lookup(const word &keyword, enum keyType::option matchOpt=keyType::REGEX) const
Definition: dictionary.C:386
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
mesh
dynamicFvMesh & mesh
Definition: createDynamicFvMesh.H:6
addToRunTimeSelectionTable.H
Macros for easy insertion into run-time selection tables.
Foam::fvMesh
Mesh data needed to do the Finite Volume discretisation.
Definition: fvMesh.H:85
fvMesh.H
Foam
Namespace for OpenFOAM.
Definition: atmBoundaryLayer.C:33
Time.H
Foam::functionObjects::runTimeControls::equationMaxIterCondition::write
virtual void write()
Write.
Definition: equationMaxIterCondition.C:165
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::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