simpleControl.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) 2011-2016 OpenFOAM Foundation
9  Copyright (C) 2017 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 "simpleControl.H"
30 #include "Time.H"
31 
32 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
33 
34 namespace Foam
35 {
36  defineTypeNameAndDebug(simpleControl, 0);
37 }
38 
39 
40 // * * * * * * * * * * * * Protected Member Functions * * * * * * * * * * * //
41 
43 {
45  return true;
46 }
47 
48 
50 {
51  if (residualControl_.empty())
52  {
53  return false;
54  }
55 
56  bool achieved = true;
57  bool checked = false; // safety that some checks were indeed performed
58 
59  const dictionary& solverDict = mesh_.solverPerformanceDict();
60  forAllConstIters(solverDict, iter)
61  {
62  const entry& solverPerfDictEntry = *iter;
63 
64  const word& fieldName = solverPerfDictEntry.keyword();
65  const label fieldi = applyToField(fieldName);
66 
67  if (fieldi != -1)
68  {
69  Pair<scalar> residuals = maxResidual(solverPerfDictEntry);
70  checked = true;
71 
72  const bool absCheck =
73  (residuals.first() < residualControl_[fieldi].absTol);
74 
75  achieved = achieved && absCheck;
76 
77  if (debug)
78  {
79  Info<< algorithmName_ << " solution statistics:" << endl;
80 
81  Info<< " " << fieldName << ": tolerance = "
82  << residuals.first()
83  << " (" << residualControl_[fieldi].absTol << ")"
84  << endl;
85  }
86  }
87  }
88 
89  return checked && achieved;
90 }
91 
92 
93 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
94 
95 Foam::simpleControl::simpleControl
96 (
97  fvMesh& mesh,
98  const word& dictName,
99  const bool verbose
100 )
101 :
103  initialised_(false)
104 {
105  read();
106 
107  if (verbose)
108  {
109  Info<< nl << algorithmName_;
110 
111  if (residualControl_.empty())
112  {
113  const scalar duration =
114  mesh_.time().endTime().value()
115  - mesh_.time().startTime().value();
116 
117  Info<< ": no convergence criteria found. "
118  << "Calculations will run for " << duration << " steps."
119  << nl;
120  }
121  else
122  {
123  Info<< ": convergence criteria" << nl;
124  for (const fieldData& ctrl : residualControl_)
125  {
126  Info<< " field " << ctrl.name << token::TAB
127  << " tolerance " << ctrl.absTol
128  << nl;
129  }
130  }
131  Info<< endl;
132  }
133 }
134 
135 
136 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
137 
139 {
141 
142  read();
143 
144  Time& runTime = const_cast<Time&>(mesh_.time());
145 
146  if (initialised_ && criteriaSatisfied())
147  {
148  Info<< nl
149  << algorithmName_
150  << " solution converged in "
151  << runTime.timeName() << " iterations" << nl << endl;
152 
153  // Set to finalise calculation
155  }
156  else
157  {
158  initialised_ = true;
159  storePrevIterFields();
160  }
161 
162  return runTime.loop();
163 }
164 
165 
166 // ************************************************************************* //
Foam::expressions::patchExpr::debug
int debug
Static debugging option.
Foam::entry
A keyword and a list of tokens is an 'entry'.
Definition: entry.H:67
runTime
engineTime & runTime
Definition: createEngineTime.H:13
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:62
Foam::entry::keyword
const keyType & keyword() const
Return keyword.
Definition: entry.H:187
Foam::solutionControl
Base class for solution control classes.
Definition: solutionControl.H:49
Foam::read
bool read(const char *buf, int32_t &val)
Same as readInt32.
Definition: int32.H:108
Foam::Time::timeName
static word timeName(const scalar t, const int precision=precision_)
Definition: Time.C:780
dictName
const word dictName("blockMeshDict")
Foam::endl
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:350
Foam::simpleControl::criteriaSatisfied
bool criteriaSatisfied()
Return true if all convergence checks are satisfied.
Definition: simpleControl.C:49
Foam::solutionControl::setFirstIterFlag
virtual void setFirstIterFlag(const bool check=true, const bool force=false)
Set the firstIteration flag on the mesh data dictionary.
Definition: solutionControl.C:181
Foam::Time::writeAndEnd
bool writeAndEnd()
Write the objects now (not at end of iteration) and end the run.
Definition: TimeIO.C:606
Foam::Info
messageStream Info
Information stream (uses stdout - output is on the master only)
Foam::simpleControl::loop
virtual bool loop()
SIMPLE loop.
Definition: simpleControl.C:138
simpleControl.H
Foam::dictionary
A list of keyword definitions, which are a keyword followed by a number of values (eg,...
Definition: dictionary.H:121
Foam::Time::loop
virtual bool loop()
Return true if run should continue and if so increment time.
Definition: Time.C:957
mesh
dynamicFvMesh & mesh
Definition: createDynamicFvMesh.H:6
Foam::fvMesh
Mesh data needed to do the Finite Volume discretisation.
Definition: fvMesh.H:83
Foam
Namespace for OpenFOAM.
Definition: atmBoundaryLayer.C:33
Foam::solutionControl::read
virtual bool read()
Read controls from fvSolution dictionary.
Definition: solutionControl.C:136
Time.H
Foam::nl
constexpr char nl
Definition: Ostream.H:385
Foam::Pair< scalar >
forAllConstIters
forAllConstIters(mixture.phases(), phase)
Definition: pEqn.H:28
Foam::solutionControl::fieldData
Simple convenient storage of field residuals.
Definition: solutionControl.H:56
Foam::token::TAB
Tab [isspace].
Definition: token.H:118
Foam::simpleControl::read
bool read()
Read controls from fvSolution dictionary.
Definition: simpleControl.C:42
Foam::defineTypeNameAndDebug
defineTypeNameAndDebug(combustionModel, 0)