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-2019 PCOpt/NTUA
9  Copyright (C) 2013-2019 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  // Sub-cycle time if this is the first iter
129  if (!subCycledTimePtr_)
130  {
131  subCycledTimePtr_.reset(new subCycleTime(runTime, nIters()));
132  Info<< "Solving equations for solver "
133  << solver_.solverName() << "\n" << endl;
134  deltaTSubSycle_ = runTime.deltaTValue();
135 
136  // Reset iteration count to zero
137  iter_ = 0;
138  }
139 
140  // Increase index
141  subCycledTimePtr_()++;
142  iter_ = subCycledTimePtr_().index();
143 
144  bool doNextIter(true);
145 
146  if (criteriaSatisfied())
147  {
148  Info<< nl
149  << solver_.solverName()
150  << " solution converged in "
151  << subCycledTimePtr_->index() << " iterations" << nl << endl;
152 
153  subCycledTimePtr_->endSubCycle();
154  subCycledTimePtr_.clear();
155 
156  // Write solution before continuing to next solver
157  runTime.write();
158  solver_.write();
159 
160  // Check whether mean fields have not been computed due to an
161  // unexpectedly early convergence
162  checkMeanSolution();
163 
164  doNextIter = false;
165  }
166  else if (subCycledTimePtr_->end())
167  {
168  Info<< nl
169  << solver_.solverName()
170  << " solution reached max. number of iterations "
171  << subCycledTimePtr_().nSubCycles() << nl << endl;
172 
173  subCycledTimePtr_->endSubCycle();
174  subCycledTimePtr_.clear();
175 
176  // Write solution before continuing to next solver
177  runTime.write();
178  solver_.write();
179 
180  doNextIter = false;
181  }
182  else
183  {
184  // Since dicts are not updated when Time is sub-cycled,
185  // do it manually here
187  resetDeltaT();
188 
189  DebugInfo
190  << "Iteration " << subCycledTimePtr_().index()
191  << "|" << subCycledTimePtr_().nSubCycles() << endl;
192 
193  storePrevIterFields();
194 
195  doNextIter = true;
196  }
197 
198  return doNextIter;
199 }
200 
201 
202 // ************************************************************************* //
scalar deltaTValue() const noexcept
Return time step value.
Definition: TimeStateI.H:36
A list of keyword definitions, which are a keyword followed by a number of values (eg...
Definition: dictionary.H:120
constexpr char nl
The newline &#39;\n&#39; character (0x0a)
Definition: Ostream.H:49
engineTime & runTime
virtual bool read()
Read controls from optimisationDict.
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:487
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:468
addToRunTimeSelectionTable(decompositionMethod, kahipDecomp, dictionary)
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:1061
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.
static constexpr const zero Zero
Global zero (0)
Definition: zero.H:133