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,
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 {
92 
93  if (fieldSelection_.size())
94  {
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.data().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 
235 {
236  // do nothing
237 }
238 
239 
240 // ************************************************************************* //
List< ReturnType > get(const UPtrList< T > &list, const AccessOp &aop)
List of values generated by applying the access operation to each list item.
dictionary dict
void size(const label n)
Older name for setAddressableSize.
Definition: UList.H:116
error FatalError
Error stream (stdout output on all processes), with additional &#39;FOAM FATAL ERROR&#39; header text and sta...
A list of keyword definitions, which are a keyword followed by a number of values (eg...
Definition: dictionary.H:129
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:598
constexpr char nl
The newline &#39;\n&#39; character (0x0a)
Definition: Ostream.H:50
equationInitialResidualCondition(const word &name, const objectRegistry &obr, const dictionary &dict, stateFunctionObject &state)
Constructor.
Tensor< scalar > tensor
Definition: symmTensor.H:57
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:531
#define SetResidual(Type)
Macros for easy insertion into run-time selection tables.
#define forAll(list, i)
Loop across all elements in list.
Definition: stdFoam.H:421
fileName::Type type(const fileName &name, const bool followLink=true)
Return the file type: DIRECTORY or FILE, normally following symbolic links.
Definition: POSIX.C:799
dynamicFvMesh & mesh
word name(const expressions::valueTypeCode typeCode)
A word representation of a valueTypeCode. Empty for expressions::valueTypeCode::INVALID.
Definition: exprTraits.C:127
virtual scalar userTimeToTime(const scalar theta) const
Convert the user-time (e.g. CA deg) to real-time (s).
Definition: TimeState.C:42
SymmTensor< scalar > symmTensor
SymmTensor of scalars, i.e. SymmTensor<scalar>.
Definition: symmTensor.H:55
A class for handling words, derived from Foam::string.
Definition: word.H:63
const Time & time() const noexcept
Return time registry.
Vector< scalar > vector
Definition: vector.H:57
errorManip< error > abort(error &err)
Definition: errorManip.H:139
defineTypeNameAndDebug(averageCondition, 0)
virtual bool read(const dictionary &dict)
Read the fieldSelection data from dictionary.
addToRunTimeSelectionTable(runTimeCondition, averageCondition, dictionary)
#define WarningInFunction
Report a warning using Foam::Warning.
Enum is a wrapper around a list of names/values that represent particular enumeration (or int) values...
Definition: error.H:64
#define Log
Definition: PDRblock.C:28
Base class for function objects, adding functionality to read/write state information (data required ...
SphericalTensor< scalar > sphericalTensor
SphericalTensor of scalars, i.e. SphericalTensor<scalar>.
Registry of regIOobjects.
scalar timeStart_
Start checking from time - always skips first iteration.
void component(FieldField< Field, typename FieldField< Field, Type >::cmptType > &sf, const FieldField< Field, Type > &f, const direction d)
Namespace for OpenFOAM.