setTimeStepFaRegionsFunctionObject.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) 2020-2023 OpenCFD Ltd.
9 -------------------------------------------------------------------------------
10 License
11  This file is part of OpenFOAM.
12 
13  OpenFOAM is free software: you can redistribute it and/or modify it
14  under the terms of the GNU General Public License as published by
15  the Free Software Foundation, either version 3 of the License, or
16  (at your option) any later version.
17 
18  OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
19  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
20  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
21  for more details.
22 
23  You should have received a copy of the GNU General Public License
24  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
25 
26 \*---------------------------------------------------------------------------*/
27 
30 
31 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
32 
33 namespace Foam
34 {
35 namespace functionObjects
36 {
37  defineTypeNameAndDebug(setTimeStepFaRegionsFunctionObject, 0);
39  (
40  functionObject,
41  setTimeStepFaRegionsFunctionObject,
42  dictionary
43  );
44 }
45 }
46 
47 
48 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
49 
50 Foam::functionObjects::
51 setTimeStepFaRegionsFunctionObject::
52 setTimeStepFaRegionsFunctionObject
53 (
54  const word& name,
55  const Time& runTime,
56  const dictionary& dict
57 )
58 :
60 {
61  read(dict);
62 }
63 
64 
65 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
66 
68 {
69  // Wanted timestep
70  scalar newDeltaT = regionDeltaT();
71 
72  static label index = -1;
73 
74  if ((time_.timeIndex() != index) && (newDeltaT < time_.deltaTValue()))
75  {
76  // Store current time so we don't get infinite recursion (since
77  // setDeltaT calls adjustTimeStep() again)
78  index = time_.timeIndex();
79 
80  // Set time, allow deltaT to be adjusted for writeInterval purposes
81  const_cast<Time&>(time_).setDeltaT(newDeltaT, false);
82 
83  return true;
84  }
85 
86  return false;
87 }
88 
89 
91 (
92  const dictionary& dict
93 )
94 {
96  {
97  // Ensure that adjustTimeStep is active
98  if (!time_.controlDict().getOrDefault<bool>("adjustTimeStep", false))
99  {
101  << "Need to set 'adjustTimeStep' true to allow timestep control"
102  << nl
103  << exit(FatalIOError);
104  }
105 
106  return true;
107  }
108 
109  return false;
110 }
111 
112 
113 Foam::scalar Foam::functionObjects::setTimeStepFaRegionsFunctionObject::
114 regionDeltaT() const
115 {
116  bool adjust = false;
117  scalar Co = 0;
118 
119  for (const regionFaModel& reg : time_.cobjects<regionFaModel>())
120  {
121  const scalar regionCo = reg.CourantNumber();
122  if (Co < regionCo)
123  {
124  Co = regionCo;
125  adjust = true;
126  }
127  }
128 
129  if (adjust)
130  {
131  const scalar regionFaMaxCo =
132  time_.controlDict().get<scalar>("regionFaMaxCo");
133 
134  const scalar maxDeltaTFact = regionFaMaxCo/(Co + SMALL);
135  const scalar deltaTFact =
136  min(min(maxDeltaTFact, 1.0 + 0.1*maxDeltaTFact), 1.2);
137 
138  return deltaTFact*time_.deltaTValue();
139  }
140 
141  return time_.deltaTValue();
142 }
143 
146 {
147  return true;
148 }
149 
150 
152 {
153  return true;
154 }
155 
156 
157 // ************************************************************************* //
dictionary dict
defineTypeNameAndDebug(ObukhovLength, 0)
scalar deltaTValue() const noexcept
Return time step value.
Definition: TimeStateI.H:49
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:125
A list of keyword definitions, which are a keyword followed by a number of values (eg...
Definition: dictionary.H:129
constexpr char nl
The newline &#39;\n&#39; character (0x0a)
Definition: Ostream.H:50
virtual bool adjustTimeStep()
Called at the end of Time::adjustDeltaT() if adjustTime is true.
engineTime & runTime
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.
word name(const expressions::valueTypeCode typeCode)
A word representation of a valueTypeCode. Empty for expressions::valueTypeCode::INVALID.
Definition: exprTraits.C:127
A class for handling words, derived from Foam::string.
Definition: word.H:63
virtual bool read(const dictionary &dict)
Read and set the function object if its data have changed.
label timeIndex() const noexcept
Return the current time index.
Definition: TimeStateI.H:43
label min(const labelHashSet &set, label minValue=labelMax)
Find the min value in labelHashSet, optionally limited by second argument.
Definition: hashSets.C:26
void read(Istream &, label &val, const dictionary &)
In-place read with dictionary lookup.
Base class for area region models.
addToRunTimeSelectionTable(functionObject, ObukhovLength, dictionary)
#define FatalIOErrorInFunction(ios)
Report an error message using Foam::FatalIOError.
Definition: error.H:637
virtual bool read(const dictionary &dict)
Read and set the function object if its data have changed.
Virtual base class for function objects with a reference to Time.
Namespace for OpenFOAM.
IOerror FatalIOError
Error stream (stdout output on all processes), with additional &#39;FOAM FATAL IO ERROR&#39; header text and ...
const Time & time_
Reference to the time database.