equationInitialResidualCondition.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) 2015 OpenFOAM Foundation
9  Copyright (C) 2015-2020 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 
31 #include "fvMesh.H"
32 #include "Time.H"
33 #include "volFields.H"
34 
35 #define SetResidual(Type) \
36  setResidual<Type>(mesh, solverDict, fieldName, component, canSet, residual);
37 
38 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
39 
40 namespace Foam
41 {
42 namespace functionObjects
43 {
44 namespace runTimeControls
45 {
46  defineTypeNameAndDebug(equationInitialResidualCondition, 0);
48  (
49  runTimeCondition,
50  equationInitialResidualCondition,
51  dictionary
52  );
53 
54 }
55 }
56 }
57 
58 const Foam::Enum
59 <
60  Foam
61  ::functionObjects
62  ::runTimeControls
63  ::equationInitialResidualCondition
64  ::operatingMode
65 >
68 ({
69  { operatingMode::omMin, "minimum" },
70  { operatingMode::omMax, "maximum" },
71 });
72 
73 
74 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
75 
78 (
79  const word& name,
80  const objectRegistry& obr,
81  const dictionary& dict,
82  stateFunctionObject& state
83 )
84 :
85  runTimeCondition(name, obr, dict, state),
86  fieldSelection_(obr, true),
87  value_(dict.get<scalar>("value")),
88  timeStart_(dict.getOrDefault("timeStart", -GREAT)),
89  mode_(operatingModeNames.get("mode", dict))
90 {
91  fieldSelection_.read(dict);
92 
93  if (fieldSelection_.size())
94  {
95  timeStart_ = obr.time().userTimeToTime(timeStart_);
96  }
97  else
98  {
100  << "No fields supplied: deactivating" << endl;
101 
102  active_ = false;
103  }
104 }
105 
106 
107 // * * * * * * * * * * * * * * Public Member Functions * * * * * * * * * * * //
108 
111 {
112  fieldSelection_.updateSelection();
113 
114  bool satisfied = false;
115 
116  if (!active_)
117  {
118  return true;
119  }
120 
121  if ((obr_.time().timeIndex() < 3) || (obr_.time().value() < timeStart_))
122  {
123  // Do not start checking until reached start time
124  return false;
125  }
126 
127  const fvMesh& mesh = refCast<const fvMesh>(obr_);
128  const dictionary& solverDict = mesh.solverPerformanceDict();
129 
130  const auto& selection = fieldSelection_.selection();
131  List<scalar> result(selection.size(), -VGREAT);
132 
133  forAll(selection, fieldi)
134  {
135  const auto& fieldInfo = selection[fieldi];
136  const word& fieldName = fieldInfo.name();
137 
138  if (solverDict.found(fieldName))
139  {
140  label component = fieldInfo.component();
141  scalar residual = VGREAT;
142  bool canSet = true;
143  SetResidual(scalar);
148 
149  result[fieldi] = residual;
150 
151  switch (mode_)
152  {
153  case omMin:
154  {
155  if (residual < value_)
156  {
157  satisfied = true;
158  }
159  break;
160  }
161  case omMax:
162  {
163  if (residual > value_)
164  {
165  satisfied = true;
166  }
167  break;
168  }
169  default:
170  {
172  << "Unhandled enumeration "
173  << operatingModeNames[mode_]
174  << abort(FatalError);
175  }
176  }
177  }
178  }
179 
180  bool valid = false;
181  forAll(result, i)
182  {
183  if (result[i] < 0)
184  {
186  << "Initial residual data not found for field "
187  << selection[i].name()
188  << ". Solver dictionary contains " << solverDict.sortedToc()
189  << endl;
190  }
191  else
192  {
193  valid = true;
194  }
195  }
196 
197  if (!valid)
198  {
200  << "Initial residual data not found for any fields: "
201  << "deactivating" << endl;
202 
203  active_ = false;
204  }
205 
206  if (satisfied && valid)
207  {
208  Log << type() << ": " << name_
209  << ": satisfied using threshold value: " << value_ << nl;
210 
211  forAll(result, resulti)
212  {
213  if (result[resulti] > 0)
214  {
215  Log << " field: " << selection[resulti].name()
216  << ", residual: " << result[resulti] << nl;
217  }
218  }
219  Log << endl;
220  }
221 
222  return satisfied;
223 }
224 
225 
228 {
229  // do nothing
230 }
231 
232 
233 // ************************************************************************* //
Foam::functionObjects::runTimeControls::defineTypeNameAndDebug
defineTypeNameAndDebug(averageCondition, 0)
volFields.H
Foam::Tensor< scalar >
Foam::SymmTensor< scalar >
Foam::Enum
Enum is a wrapper around a list of names/values that represent particular enumeration (or int) values...
Definition: IOstreamOption.H:57
Log
#define Log
Definition: PDRblock.C:35
Foam::functionObjects::runTimeControls::runTimeCondition
Base class for run time conditions.
Definition: runTimeCondition.H:61
Foam::component
void component(FieldField< Field, typename FieldField< Field, Type >::cmptType > &sf, const FieldField< Field, Type > &f, const direction d)
Definition: FieldFieldFunctions.C:44
Foam::word
A class for handling words, derived from Foam::string.
Definition: word.H:65
equationInitialResidualCondition.H
Foam::dictionary::found
bool found(const word &keyword, enum keyType::option matchOpt=keyType::REGEX) const
Search for an entry (const access) with the given keyword.
Definition: dictionaryI.H:87
Foam::functionObjects::fieldInfo::name
const wordRe & name() const
Definition: fieldInfo.H:107
Foam::functionObjects::fieldInfo
Helper class to store a wordRe and label used by Foam::functionObjects::fieldSelection.
Definition: fieldInfo.H:57
Foam::functionObjects::runTimeControls::equationInitialResidualCondition::operatingModeNames
static const Enum< operatingMode > operatingModeNames
Definition: equationInitialResidualCondition.H:72
Foam::functionObjects::runTimeControls::addToRunTimeSelectionTable
addToRunTimeSelectionTable(runTimeCondition, averageCondition, dictionary)
Foam::TimeState::userTimeToTime
virtual scalar userTimeToTime(const scalar theta) const
Convert the user-time (e.g. CA deg) to real-time (s).
Definition: TimeState.C:49
Foam::functionObjects::stateFunctionObject
Base class for function objects, adding functionality to read/write state information (data required ...
Definition: stateFunctionObject.H:69
Foam::endl
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:369
forAll
#define forAll(list, i)
Loop across all elements in list.
Definition: stdFoam.H:296
Foam::functionObjects::runTimeControls::equationInitialResidualCondition::write
virtual void write()
Write.
Definition: equationInitialResidualCondition.C:227
Foam::objectRegistry
Registry of regIOobjects.
Definition: objectRegistry.H:60
Foam::functionObjects::runTimeControls::equationInitialResidualCondition::equationInitialResidualCondition
equationInitialResidualCondition(const word &name, const objectRegistry &obr, const dictionary &dict, stateFunctionObject &state)
Constructor.
Definition: equationInitialResidualCondition.C:78
dict
dictionary dict
Definition: searchingEngine.H:14
Foam::FatalError
error FatalError
Foam::dictionary
A list of keyword definitions, which are a keyword followed by a number of values (eg,...
Definition: dictionary.H:123
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
fvMesh.H
Foam
Namespace for OpenFOAM.
Definition: atmBoundaryLayer.C:33
Foam::abort
errorManip< error > abort(error &err)
Definition: errorManip.H:144
Foam::SphericalTensor< scalar >
Time.H
Foam::functionObjects::runTimeControls::equationInitialResidualCondition::apply
virtual bool apply()
Apply the condition.
Definition: equationInitialResidualCondition.C:110
FatalErrorInFunction
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:453
Foam::nl
constexpr char nl
Definition: Ostream.H:404
Foam::Vector< scalar >
Foam::List< scalar >
Foam::roots::type
type
Types of root.
Definition: Roots.H:54
Foam::name
word name(const expressions::valueTypeCode typeCode)
A word representation of a valueTypeCode. Empty for INVALID.
Definition: exprTraits.C:59
Foam::functionObjects::fieldInfo::component
label component() const
Definition: fieldInfo.H:112
SetResidual
#define SetResidual(Type)
Definition: equationInitialResidualCondition.C:35
Foam::dictionary::sortedToc
wordList sortedToc() const
Return the sorted table of contents.
Definition: dictionary.C:616
WarningInFunction
#define WarningInFunction
Report a warning using Foam::Warning.
Definition: messageStream.H:328
Foam::objectRegistry::time
const Time & time() const noexcept
Return time registry.
Definition: objectRegistry.H:178