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 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 
32 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
33 
34 namespace Foam
35 {
36 namespace functionObjects
37 {
38  defineTypeNameAndDebug(setTimeStepFaRegionsFunctionObject, 0);
40  (
41  functionObject,
42  setTimeStepFaRegionsFunctionObject,
43  dictionary
44  );
45 }
46 }
47 
48 
49 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
50 
51 Foam::functionObjects::
52 setTimeStepFaRegionsFunctionObject::
53 setTimeStepFaRegionsFunctionObject
54 (
55  const word& name,
56  const Time& runTime,
57  const dictionary& dict
58 )
59 :
61 {
62  read(dict);
63 }
64 
65 
66 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
67 
69 {
70  // Wanted timestep
71  scalar newDeltaT = regionDeltaT();
72 
73  static label index = -1;
74 
75  if ((time_.timeIndex() != index) && (newDeltaT < time_.deltaTValue()))
76  {
77  // Store current time so we don't get infinite recursion (since
78  // setDeltaT calls adjustTimeStep() again)
79  index = time_.timeIndex();
80 
81  // Set time, allow deltaT to be adjusted for writeInterval purposes
82  const_cast<Time&>(time_).setDeltaT(newDeltaT, false);
83 
84  return true;
85  }
86 
87  return false;
88 }
89 
90 
92 (
93  const dictionary& dict
94 )
95 {
97  {
98  // Ensure that adjustTimeStep is active
99  if (!time_.controlDict().lookupOrDefault<bool>("adjustTimeStep", false))
100  {
102  << "Need to set 'adjustTimeStep' true to allow timestep control"
103  << nl
104  << exit(FatalIOError);
105  }
106 
107  return true;
108  }
109 
110  return false;
111 }
112 
113 
114 Foam::scalar Foam::functionObjects::setTimeStepFaRegionsFunctionObject::
115 regionDeltaT() const
116 {
117  const wordList names(time_.sortedNames<regionFaModel>());
118 
119  scalar Co = 0.0;
120 
121  forAll (names, i)
122  {
123  const auto* regionFa = time_.cfindObject<regionFaModel>(names[i]);
124 
125  if (regionFa)
126  {
127  const scalar regionCo = regionFa->CourantNumber();
128  if (regionCo > Co)
129  {
130  Co = regionCo;
131  }
132  }
133  }
134 
135  if (names.size() > 0)
136  {
137  const scalar regionFaMaxCo =
138  time_.controlDict().get<scalar>("regionFaMaxCo");
139 
140  const scalar maxDeltaTFact = regionFaMaxCo/(Co + SMALL);
141  const scalar deltaTFact =
142  min(min(maxDeltaTFact, 1.0 + 0.1*maxDeltaTFact), 1.2);
143 
144  return deltaTFact*time_.deltaTValue();
145  }
146 
147  return time_.deltaTValue();
148 }
149 
150 
152 {
153  return true;
154 }
155 
156 
158 {
159  return true;
160 }
161 
162 
163 // ************************************************************************* //
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::functionObjects::timeFunctionObject::time_
const Time & time_
Reference to the time database.
Definition: timeFunctionObject.H:65
Foam::functionObjects::setTimeStepFaRegionsFunctionObject::adjustTimeStep
virtual bool adjustTimeStep()
Called at the end of Time::adjustDeltaT() if adjustTime is true.
Definition: setTimeStepFaRegionsFunctionObject.C:68
Foam::functionObjects::timeFunctionObject
Virtual base class for function objects with a reference to Time.
Definition: timeFunctionObject.H:56
Foam::regionModels::regionFaModel::CourantNumber
virtual scalar CourantNumber() const
Courant number of the region.
Definition: regionFaModel.C:199
Foam::FatalIOError
IOerror FatalIOError
Foam::min
label min(const labelHashSet &set, label minValue=labelMax)
Find the min value in labelHashSet, optionally limited by second argument.
Definition: hashSets.C:33
Foam::TimeState::deltaTValue
scalar deltaTValue() const noexcept
Return time step value.
Definition: TimeStateI.H:43
forAll
#define forAll(list, i)
Loop across all elements in list.
Definition: stdFoam.H:296
Foam::functionObjects::setTimeStepFaRegionsFunctionObject::write
virtual bool write()
Write does nothing.
Definition: setTimeStepFaRegionsFunctionObject.C:157
Foam::blockMeshTools::read
void read(Istream &, label &val, const dictionary &)
In-place read with dictionary lookup.
Definition: blockMeshTools.C:57
Foam::TimeState::timeIndex
label timeIndex() const noexcept
Return current time index.
Definition: TimeStateI.H:37
Foam::functionObject::read
virtual bool read(const dictionary &dict)
Read and set the function object if its data have changed.
Definition: functionObject.C:163
dict
dictionary dict
Definition: searchingEngine.H:14
Foam::dictionary
A list of keyword definitions, which are a keyword followed by a number of values (eg,...
Definition: dictionary.H:123
addToRunTimeSelectionTable.H
Macros for easy insertion into run-time selection tables.
Foam
Namespace for OpenFOAM.
Definition: atmBoundaryLayer.C:33
Foam::functionObjects::setTimeStepFaRegionsFunctionObject::execute
virtual bool execute()
Execute does nothing.
Definition: setTimeStepFaRegionsFunctionObject.C:151
Foam::exit
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:130
setTimeStepFaRegionsFunctionObject.H
Foam::nl
constexpr char nl
Definition: Ostream.H:404
Foam::functionObjects::addToRunTimeSelectionTable
addToRunTimeSelectionTable(functionObject, ObukhovLength, dictionary)
Foam::List< word >
Foam::regionModels::regionFaModel
Base class for area region models.
Definition: regionFaModel.H:113
Foam::functionObjects::defineTypeNameAndDebug
defineTypeNameAndDebug(ObukhovLength, 0)
Foam::functionObjects::setTimeStepFaRegionsFunctionObject::read
virtual bool read(const dictionary &dict)
Read and set the function object if its data have changed.
Definition: setTimeStepFaRegionsFunctionObject.C:92
Foam::name
word name(const expressions::valueTypeCode typeCode)
A word representation of a valueTypeCode. Empty for INVALID.
Definition: exprTraits.C:59
FatalIOErrorInFunction
#define FatalIOErrorInFunction(ios)
Report an error message using Foam::FatalIOError.
Definition: error.H:473
Foam::PtrListOps::names
List< word > names(const UPtrList< T > &list, const UnaryMatchPredicate &matcher)