SIMPLEControlSingleRun.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 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 "SIMPLEControlSingleRun.H"
32 #include "Time.H"
33 
34 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
35 
36 namespace Foam
37 {
38  defineTypeNameAndDebug(SIMPLEControlSingleRun, 0);
40  (
41  SIMPLEControl,
42  SIMPLEControlSingleRun,
43  dictionary
44  );
45 }
46 
47 // * * * * * * * * * * * * Protected Member Functions * * * * * * * * * * * //
48 
50 {
51  return SIMPLEControl::read();
52 }
53 
54 
56 {
57  label nItersOld = nIters_;
58  nIters_ = dict().get<label>("nIters");
59 
60  if (nIters_ != nItersOld || iter_ == 0)
61  {
62  Time& runTime = const_cast<Time&>(mesh_.time());
63  if (iter_ == 0)
64  {
65  startTime_ = runTime.value();
66  }
67  Info<< "Setting endTime to " << startTime_ + nIters_ << endl;
68  runTime.setEndTime(startTime_ + nIters_);
69  endTime_ = runTime.endTime().value();
70  }
71 }
72 
73 
75 {
76  // If controlDict is modified during run-time, time.endTime() is reset
77  // to what is read from controlDict and overwrites the one set through
78  // nIters. Silently reset
79  Time& time = const_cast<Time&>(mesh_.time());
80 
81  if (time.endTime().value() != endTime_)
82  {
83  time.setEndTime(startTime_ + nIters_);
84  endTime_ = time.endTime().value();
85  isRunning =
86  time.value() < (time.endTime().value() - 0.5*time.deltaTValue());
87  }
88 }
89 
90 
91 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
92 
93 Foam::SIMPLEControlSingleRun::SIMPLEControlSingleRun
94 (
95  fvMesh& mesh,
96  const word& managerType,
97  const solver& solver
98 )
99 :
100  SIMPLEControl(mesh, managerType, solver),
101  startTime_(Zero),
102  endTime_(Zero)
103 {}
104 
105 
106 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
107 
108 bool Foam::SIMPLEControlSingleRun::write(const bool valid) const
109 {
110  Time& time = const_cast<Time&>(mesh_.time());
111  time.write();
112  solver_.write();
113 
114  return true;
115 }
116 
117 
119 {
120  Time& time = const_cast<Time&>(mesh_.time());
121  // Avoid writing fields if already in an outputTime iter
122  // since results will be written by the solver class either way
123  if (!time.writeTime())
124  {
125  time.writeNow();
126  solver_.writeNow();
127  }
128 }
129 
130 
132 {
134 
135  this->read();
136  ++iter_;
137 
138  Time& runTime = const_cast<Time&>(mesh_.time());
139 
140  if (initialised_ && criteriaSatisfied())
141  {
142  Info<< nl
143  << solver_.solverName()
144  << " solution converged in "
145  << runTime.timeName() << " iterations" << nl << endl;
146 
147  // write fields (including dummy turbulence fields in multi-point runs)
148  writeNow();
149 
150  // Check whether mean fields have not been computed due to an
151  // unexpectedly early convergence
152  checkMeanSolution();
153 
154  return false;
155  }
156  else
157  {
158  initialised_ = true;
159  storePrevIterFields();
160  }
161 
162  bool isRunning = runTime.loop();
163  checkEndTime(isRunning);
164 
165  if (!isRunning)
166  {
167  Info<< nl
168  << solver_.solverName()
169  << " solution reached max. number of iterations "
170  << nIters_ << nl << endl;
171 
172  // Write fields (including dummy turbulence fields in multi-point runs)
173  writeNow();
174  }
175 
176  return isRunning;
177 }
178 
179 
180 // ************************************************************************* //
Foam::addToRunTimeSelectionTable
addToRunTimeSelectionTable(decompositionMethod, kahipDecomp, dictionary)
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:65
Foam::Zero
static constexpr const zero Zero
Global zero (0)
Definition: zero.H:131
Foam::SIMPLEControlSingleRun::readIters
virtual void readIters()
Set end time if number of iters has changed.
Definition: SIMPLEControlSingleRun.C:55
Foam::read
bool read(const char *buf, int32_t &val)
Same as readInt32.
Definition: int32.H:108
Foam::SIMPLEControlSingleRun::loop
virtual bool loop()
Loop.
Definition: SIMPLEControlSingleRun.C:131
Foam::Time::timeName
static word timeName(const scalar t, const int precision=precision_)
Definition: Time.C:780
Foam::endl
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:369
Foam::dimensioned::value
const Type & value() const
Return const reference to value.
Definition: dimensionedType.C:434
Foam::dictionary::get
T get(const word &keyword, enum keyType::option matchOpt=keyType::REGEX) const
Definition: dictionaryTemplates.C:107
Foam::TimeState::deltaTValue
scalar deltaTValue() const noexcept
Return time step value.
Definition: TimeStateI.H:43
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::regIOobject::write
virtual bool write(const bool valid=true) const
Write using setting from DB.
Definition: regIOobjectWrite.C:132
Foam::TimeState::writeTime
bool writeTime() const noexcept
True if this is a write time.
Definition: TimeStateI.H:67
Foam::Time::setEndTime
virtual void setEndTime(const dimensionedScalar &endTime)
Reset end time.
Definition: Time.C:1054
Foam::solver
Base class for solution control classes.
Definition: solver.H:51
Foam::Info
messageStream Info
Information stream (stdout output on master, null elsewhere)
SIMPLEControlSingleRun.H
Foam::SIMPLEControl::read
virtual bool read()
Read controls from fvSolution dictionary.
Definition: SIMPLEControl.C:86
dict
dictionary dict
Definition: searchingEngine.H:14
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
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::SIMPLEControlSingleRun::write
virtual bool write(const bool valid=true) const
Whether to call time.write() or not.
Definition: SIMPLEControlSingleRun.C:108
Foam::SIMPLEControlSingleRun::writeNow
void writeNow()
Write fields, even if it is not a writeTime.
Definition: SIMPLEControlSingleRun.C:118
Time.H
Foam::nl
constexpr char nl
Definition: Ostream.H:404
Foam::Time::writeNow
bool writeNow()
Definition: TimeIO.C:596
Foam::Time::endTime
virtual dimensionedScalar endTime() const
Return end time.
Definition: Time.C:873
Foam::SIMPLEControlSingleRun::checkEndTime
void checkEndTime(bool &isRunning)
Definition: SIMPLEControlSingleRun.C:74
Foam::defineTypeNameAndDebug
defineTypeNameAndDebug(combustionModel, 0)
Foam::SIMPLEControlSingleRun::read
virtual bool read()
Read controls from optimisationDict.
Definition: SIMPLEControlSingleRun.C:49
Foam::SIMPLEControl
SIMPLE control class to supply convergence information/checks for the SIMPLE loop.
Definition: SIMPLEControl.H:52