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,
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 writeTime 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;
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 // ************************************************************************* //
const Type & value() const noexcept
Return const reference to value.
dictionary dict
static bool initialised_(false)
A list of keyword definitions, which are a keyword followed by a number of values (eg...
Definition: dictionary.H:129
SIMPLE control class for single runs (i.e. not optimisation). Time acts as in simpleFoam, with all solver control read through optimisationDict.
virtual void setEndTime(const dimensionedScalar &endTime)
Reset end time.
Definition: Time.C:985
constexpr char nl
The newline &#39;\n&#39; character (0x0a)
Definition: Ostream.H:50
virtual bool loop()
Return true if run should continue and if so increment time.
Definition: Time.C:889
virtual bool read()
Read controls from optimisationDict.
engineTime & runTime
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:531
virtual dimensionedScalar endTime() const
Return end time.
Definition: Time.C:795
bool writeTime() const noexcept
True if this is a write interval.
Definition: TimeStateI.H:73
T get(const word &keyword, enum keyType::option matchOpt=keyType::REGEX) const
Find and return a T. FatalIOError if not found, or if the number of tokens is incorrect.
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
dynamicFvMesh & mesh
virtual void setFirstIterFlag(const bool check=true, const bool force=false)
Set the firstIteration flag on the mesh data dictionary.
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
virtual bool write(const bool valid=true) const
Whether to call time.write() or not.
static word timeName(const scalar t, const int precision=precision_)
Return a time name for the given scalar time value formatted with the given precision.
Definition: Time.C:714
defineTypeNameAndDebug(combustionModel, 0)
void writeNow()
Write fields, even if it is not a writeTime.
virtual void storePrevIterFields() const
Store previous iteration fields.
void checkEndTime(bool &isRunning)
Check whether endTime has been overwritten by a run-time modification of controlDict.
virtual void readIters()
Set end time if number of iters has changed.
messageStream Info
Information stream (stdout output on master, null elsewhere)
bool writeNow()
Write the objects immediately (not at end of iteration) and continue the run.
Definition: TimeIO.C:582
Namespace for OpenFOAM.
addToRunTimeSelectionTable(functionObject, pointHistory, dictionary)
static constexpr const zero Zero
Global zero (0)
Definition: zero.H:127