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 {
38  defineTypeNameAndDebug(SIMPLEControlOpt, 1);
39  addToRunTimeSelectionTable( SIMPLEControl, SIMPLEControlOpt, dictionary);
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 // ************************************************************************* //
Foam::addToRunTimeSelectionTable
addToRunTimeSelectionTable(decompositionMethod, kahipDecomp, dictionary)
runTime
engineTime & runTime
Definition: createEngineTime.H:13
Foam::SIMPLEControlOpt::loop
virtual bool loop()
Loop.
Definition: SIMPLEControlOpt.C:122
Foam::Time
Class to control time during OpenFOAM simulations that is also the top-level objectRegistry.
Definition: Time.H:73
Foam::word
A class for handling words, derived from Foam::string.
Definition: word.H:65
Foam::Zero
static constexpr const zero Zero
Global zero (0)
Definition: zero.H:131
Foam::SIMPLEControlOpt::read
virtual bool read()
Read controls from optimisationDict.
Definition: SIMPLEControlOpt.C:44
Foam::read
bool read(const char *buf, int32_t &val)
Same as readInt32.
Definition: int32.H:108
Foam::endl
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:369
Foam::subCycleTime
A class for managing sub-cycling times.
Definition: subCycleTime.H:50
Foam::simpleControl::criteriaSatisfied
bool criteriaSatisfied()
Return true if all convergence checks are satisfied.
Definition: simpleControl.C:49
Foam::TimeState::deltaTValue
scalar deltaTValue() const noexcept
Return time step value.
Definition: TimeStateI.H:43
Foam::regIOobject::write
virtual bool write(const bool valid=true) const
Write using setting from DB.
Definition: regIOobjectWrite.C:132
Foam::solver
Base class for solution control classes.
Definition: solver.H:51
Foam::Info
messageStream Info
Information stream (stdout output on master, null elsewhere)
SIMPLEControlOpt.H
Foam::SIMPLEControl::dict
virtual const dictionary dict() const
Return the solution dictionary.
Definition: SIMPLEControlI.H:33
Foam::SIMPLEControl::read
virtual bool read()
Read controls from fvSolution dictionary.
Definition: SIMPLEControl.C:86
Foam::SIMPLEControlOpt::nIters
const label & nIters() const
Maximum number of solver iterations.
Definition: SIMPLEControlOpt.C:71
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
Foam
Namespace for OpenFOAM.
Definition: atmBoundaryLayer.C:33
Foam::Time::setDeltaT
virtual void setDeltaT(const dimensionedScalar &deltaT, const bool adjust=true)
Reset time step, normally also calling adjustDeltaT()
Definition: Time.C:1067
Time.H
Foam::Time::readModifiedObjects
void readModifiedObjects()
Read the objects that have been modified.
Definition: TimeIO.C:467
DebugInfo
#define DebugInfo
Report an information message using Foam::Info.
Definition: messageStream.H:382
Foam::nl
constexpr char nl
Definition: Ostream.H:404
Foam::SIMPLEControlOpt::write
virtual bool write(const bool valid=true) const
Whether to call time.write() or not.
Definition: SIMPLEControlOpt.C:114
Foam::SIMPLEControlOpt::criteriaSatisfied
virtual bool criteriaSatisfied()
Return true if all convergence checks are satisfied.
Definition: SIMPLEControlOpt.C:51
Foam::dictionary::getOrDefault
T getOrDefault(const word &keyword, const T &deflt, enum keyType::option matchOpt=keyType::REGEX) const
Definition: dictionaryTemplates.C:148
Foam::SIMPLEControl::nIters_
label nIters_
Number of SIMPLE iterations.
Definition: SIMPLEControl.H:66
Foam::defineTypeNameAndDebug
defineTypeNameAndDebug(combustionModel, 0)
Foam::SIMPLEControlOpt::nInitialIters_
label nInitialIters_
Number of iterations for the first optimisation cycle.
Definition: SIMPLEControlOpt.H:61
Foam::SIMPLEControl
SIMPLE control class to supply convergence information/checks for the SIMPLE loop.
Definition: SIMPLEControl.H:52
Foam::SIMPLEControlOpt::resetDeltaT
void resetDeltaT()
Reset deltaT in case controlDict has been re-written at run-time.
Definition: SIMPLEControlOpt.C:84