SIMPLEControlOpt.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) 2007-2023 PCOpt/NTUA
9  Copyright (C) 2013-2023 FOSS GP
10  Copyright (C) 2019-2020 OpenCFD Ltd.
11 -------------------------------------------------------------------------------
12 License
13  This file is part of OpenFOAM.
14 
15  OpenFOAM is free software: you can redistribute it and/or modify it
16  under the terms of the GNU General Public License as published by
17  the Free Software Foundation, either version 3 of the License, or
18  (at your option) any later version.
19 
20  OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
21  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
22  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
23  for more details.
24 
25  You should have received a copy of the GNU General Public License
26  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
27 
28 \*---------------------------------------------------------------------------*/
29 
30 #include "SIMPLEControlOpt.H"
32 #include "Time.H"
33 
34 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
35 
36 namespace Foam
37 {
40 }
41 
42 // * * * * * * * * * * * * Protected Member Functions * * * * * * * * * * * //
43 
45 {
46  nInitialIters_ = dict().getOrDefault<label>("nInitialIters", nIters_);
47  return SIMPLEControl::read();
48 }
49 
50 
52 {
53  bool satisfied(false);
54 
55  // Do not check criteria in the first iteration of the algorithm.
56  // Used to avoid stopping the solution of the flow equations
57  // due to a converged solution in the previous optimisation cycle
58  if (subCycledTimePtr_().index() == 1)
59  {
60  satisfied = false;
61  }
62  else
63  {
65  }
66 
67  return satisfied;
68 }
69 
70 
71 const Foam::label& Foam::SIMPLEControlOpt::nIters() const
72 {
73  if (mesh_.time().timeIndex() == mesh_.time().startTimeIndex() + 1)
74  {
75  return nInitialIters_;
76  }
77  else
78  {
79  return nIters_;
80  }
81 }
82 
83 
85 {
86  Time& runTime = const_cast<Time&>(mesh_.time());
87  if (runTime.deltaTValue() != deltaTSubSycle_)
88  {
89  runTime.setDeltaT(deltaTSubSycle_, false);
90  }
91 }
92 
93 
94 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
95 
96 Foam::SIMPLEControlOpt::SIMPLEControlOpt
97 (
98  fvMesh& mesh,
99  const word& managerType,
100  const solver& solver
101 )
102 :
103  SIMPLEControl(mesh, managerType, solver),
104  nInitialIters_(0),
105  subCycledTimePtr_(nullptr),
106  deltaTSubSycle_(Zero)
107 {
108  this->read();
109 }
110 
111 
112 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
113 
114 bool Foam::SIMPLEControlOpt::write(const bool valid) const
115 {
116  // Intentionally does nothing
117  // Write is called only at the last iteration of the cycle
118  return false;
119 }
120 
121 
123 {
124  this->read();
125 
126  Time& runTime = const_cast<Time&>(mesh_.time());
127 
128  bool doNextIter(true);
129 
130  if (nIters() > 0)
131  {
132  // Sub-cycle time if this is the first iter
133  if (!subCycledTimePtr_)
134  {
135  subCycledTimePtr_.reset(new subCycleTime(runTime, nIters()));
136  Info<< "Solving equations for solver "
137  << solver_.solverName() << "\n" << endl;
138  deltaTSubSycle_ = runTime.deltaTValue();
139 
140  // Reset iteration count to zero
141  iter_ = 0;
142 
143  // Reset previous time index of fvMesh::data, to avoid the rare
144  // occurance of a solver satisfying the convergence criteria at the
145  // first iteration, which then causes all subsequent optimisation
146  // cycles to be seen as converged, irrespective of the residual
147  // level, since the data::prevTimeIndex_ is not updated
148  //mesh_.data::setPreviousTimeIndex(0);
149  }
150 
151  // Increase index
152  subCycledTimePtr_()++;
153  iter_ = subCycledTimePtr_().index();
154 
155 
156  if (criteriaSatisfied())
157  {
158  Info<< nl
159  << solver_.solverName()
160  << " solution converged in "
161  << subCycledTimePtr_->index() << " iterations" << nl << endl;
162 
163  subCycledTimePtr_->endSubCycle();
164  subCycledTimePtr_.clear();
165 
166  // Write solution before continuing to next solver
167  runTime.write();
168  solver_.write();
169 
170  // Check whether mean fields have not been computed due to an
171  // unexpectedly early convergence
172  checkMeanSolution();
173 
174  doNextIter = false;
175  }
176  else if (subCycledTimePtr_->end())
177  {
178  Info<< nl
179  << solver_.solverName()
180  << " solution reached max. number of iterations "
181  << subCycledTimePtr_().nSubCycles() << nl << endl;
182 
183  subCycledTimePtr_->endSubCycle();
184  subCycledTimePtr_.clear();
185 
186  // Write solution before continuing to next solver
187  runTime.write();
188  solver_.write();
189 
190  doNextIter = false;
191  }
192  else
193  {
194  // Since dicts are not updated when Time is sub-cycled,
195  // do it manually here
197  resetDeltaT();
198 
199  DebugInfo
200  << "Iteration " << subCycledTimePtr_().index()
201  << "|" << subCycledTimePtr_().nSubCycles() << endl;
202 
204 
205  doNextIter = true;
206  }
207  }
208  else
209  {
211  << "Number of iterations is non-positive (" << nIters() << ")."
212  << nl
213  << "Skipping the solution of the equations corresponding to solver "
214  << solver_.solverName()
215  << nl << endl;
216 
217  doNextIter = false;
218  }
219 
220  return doNextIter;
221 }
222 
223 
224 // ************************************************************************* //
scalar deltaTValue() const noexcept
Return time step value.
Definition: TimeStateI.H:49
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
engineTime & runTime
virtual bool read()
Read controls from optimisationDict.
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:531
Class to control time during OpenFOAM simulations that is also the top-level objectRegistry.
Definition: Time.H:69
Macros for easy insertion into run-time selection tables.
bool read(const char *buf, int32_t &val)
Same as readInt32.
Definition: int32.H:127
virtual bool loop()
Loop.
dynamicFvMesh & mesh
virtual bool write(const bool writeOnProc=true) const
Write using setting from DB.
virtual bool read()
Read controls from fvSolution dictionary.
Definition: SIMPLEControl.C:79
#define DebugInfo
Report an information message using Foam::Info.
const label & nIters() const
Maximum number of solver iterations.
label nInitialIters_
Number of iterations for the first optimisation cycle.
defineTypeNameAndDebug(combustionModel, 0)
SIMPLE control class for optimisation runs. Each time is sub-cycled and corresponds to one optimisati...
virtual bool criteriaSatisfied()
Return true if all convergence checks are satisfied.
virtual const dictionary dict() const
Return the solution dictionary.
void readModifiedObjects()
Read the objects that have been modified.
Definition: TimeIO.C:452
virtual void storePrevIterFields() const
Store previous iteration fields.
#define WarningInFunction
Report a warning using Foam::Warning.
virtual bool write(const bool valid=true) const
Whether to call time.write() or not.
virtual void setDeltaT(const dimensionedScalar &deltaT, const bool adjust=true)
Reset time step, normally also calling adjustDeltaT()
Definition: Time.C:998
bool criteriaSatisfied()
Return true if all convergence checks are satisfied.
Definition: simpleControl.C:42
SIMPLE control class to supply convergence information/checks for the SIMPLE loop.
Definition: SIMPLEControl.H:46
messageStream Info
Information stream (stdout output on master, null elsewhere)
void resetDeltaT()
Reset deltaT in case controlDict has been re-written at run-time.
label nIters_
Number of SIMPLE iterations.
Definition: SIMPLEControl.H:64
T getOrDefault(const word &keyword, const T &deflt, enum keyType::option matchOpt=keyType::REGEX) const
Find and return a T, or return the given default value. FatalIOError if it is found and the number of...
A class for managing sub-cycling times.
Definition: subCycleTime.H:46
Namespace for OpenFOAM.
addToRunTimeSelectionTable(functionObject, pointHistory, dictionary)
static constexpr const zero Zero
Global zero (0)
Definition: zero.H:127