solution.C
Go to the documentation of this file.
1 /*---------------------------------------------------------------------------*\
2  ========= |
3  \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
4  \\ / O peration | Website: https://openfoam.org
5  \\ / A nd | Copyright (C) 2011-2026 OpenFOAM Foundation
6  \\/ M anipulation |
7 -------------------------------------------------------------------------------
8 License
9  This file is part of OpenFOAM.
10 
11  OpenFOAM is free software: you can redistribute it and/or modify it
12  under the terms of the GNU General Public License as published by
13  the Free Software Foundation, either version 3 of the License, or
14  (at your option) any later version.
15 
16  OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
17  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
18  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
19  for more details.
20 
21  You should have received a copy of the GNU General Public License
22  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
23 
24 \*---------------------------------------------------------------------------*/
25 
26 #include "solution.H"
27 #include "Time.H"
28 
29 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
30 
31 namespace Foam
32 {
34 }
35 
36 
37 // * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * * //
38 
39 void Foam::solution::readDict()
40 {
41  if (found("cache"))
42  {
43  printDictionary print(subDict("cache"));
44  cache_ = subDict("cache");
45  caching_ = cache_.lookupOrDefault("active", true);
46  }
47 
48  fieldRelaxDict_ = &dictionary::null;
49  eqnRelaxDict_ = &dictionary::null;
50  if (found("relaxationFactors"))
51  {
52  const dictionary& relaxDict(subDict("relaxationFactors"));
53  printDictionary print(relaxDict);
54 
55  if (relaxDict.found("fields") || relaxDict.found("equations"))
56  {
57  if (relaxDict.found("fields"))
58  {
59  fieldRelaxDict_ = &relaxDict.subDict("fields");
60  }
61 
62  if (relaxDict.found("equations"))
63  {
64  eqnRelaxDict_ = &relaxDict.subDict("equations");
65  }
66  }
67  else
68  {
69  IOWarningInFunction(*this)
70  << "Neither fields nor equations specified" << endl;
71  }
72  }
73 
74  if (found("solvers"))
75  {
76  solvers_ = &subDict("solvers");
77  printDictionary print(*solvers_);
78  }
79  else
80  {
81  solvers_ = &dictionary::null;
82  }
83 }
84 
85 
86 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
87 
89 (
90  const objectRegistry& obr,
91  const fileName& dictName
92 )
93 :
95  (
96  IOobject
97  (
98  dictName,
99  obr.time().system(),
100  obr,
101  IOobject::MUST_READ_IF_MODIFIED,
102  IOobject::NO_WRITE
103  )
104  ),
105  cache_("cache", *this),
106  caching_(false),
107  fieldRelaxDict_(nullptr),
108  eqnRelaxDict_(nullptr),
109  fieldRelaxDefault_(0),
110  eqnRelaxDefault_(0),
111  solvers_(nullptr)
112 {
113  readDict();
114 }
115 
116 
117 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
118 
119 bool Foam::solution::cache(const word& name) const
120 {
121  if (caching_)
122  {
123  if (debug)
124  {
125  Info<< "Cache: find entry for " << name << endl;
126  }
127 
128  return cache_.found(name);
129  }
130  else
131  {
132  return false;
133  }
134 }
135 
136 
138 {
139  caching_ = true;
140 
141  if (debug)
142  {
143  Info<< "Enable cache for " << name << endl;
144  }
145 
146  cache_.add(name, true);
147 }
148 
149 
151 {
152  if (debug)
153  {
154  Info<< "Field relaxation factor for " << name
155  << " is " << (fieldRelaxDict_->found(name) ? "set" : "unset")
156  << endl;
157  }
158 
159  return fieldRelaxDict_->found(name) || fieldRelaxDict_->found("default");
160 }
161 
162 
164 {
165  if (debug)
166  {
167  Info<< "Find equation relaxation factor for " << name << endl;
168  }
169 
170  return eqnRelaxDict_->found(name) || eqnRelaxDict_->found("default");
171 }
172 
173 
175 {
176  if (debug)
177  {
178  Info<< "Lookup variable relaxation factor for " << name << endl;
179  }
180 
181  if (fieldRelaxDict_->found(name))
182  {
183  return fieldRelaxDict_->lookup<scalar>(name);
184  }
185  else if (fieldRelaxDefault_ > small)
186  {
187  return fieldRelaxDefault_;
188  }
189  else
190  {
192  (
193  *fieldRelaxDict_
194  ) << "Cannot find variable relaxation factor for '" << name
195  << "' or a suitable default value."
196  << exit(FatalIOError);
197 
198  return 0;
199  }
200 }
201 
202 
204 {
205  if (debug)
206  {
207  Info<< "Lookup equation relaxation factor for " << name << endl;
208  }
209 
210  if (eqnRelaxDict_->found(name))
211  {
212  return eqnRelaxDict_->lookup<scalar>(name);
213  }
214  else if (eqnRelaxDefault_ > small)
215  {
216  return eqnRelaxDefault_;
217  }
218  else
219  {
221  (
222  *eqnRelaxDict_
223  ) << "Cannot find equation relaxation factor for '" << name
224  << "' or a suitable default value."
225  << exit(FatalIOError);
226 
227  return 0;
228  }
229 }
230 
231 
233 {
234  return *solvers_;
235 }
236 
237 
239 {
240  if (debug)
241  {
242  Info<< "Lookup solver for " << name << endl;
243  }
244 
245  return solvers_->subDict(name);
246 }
247 
248 
250 {
251  if (regIOobject::read())
252  {
253  readDict();
254 
255  return true;
256  }
257  else
258  {
259  return false;
260  }
261 }
262 
263 
264 // ************************************************************************* //
IOdictionary is derived from dictionary and IOobject to give the dictionary automatic IO functionalit...
Definition: IOdictionary.H:57
IOobject defines the attributes of an object for which implicit objectRegistry management is supporte...
Definition: IOobject.H:99
A list of keywords followed by any number of values (e.g. words and numbers) or sub-dictionaries.
Definition: dictionary.H:162
T lookupOrDefault(const word &, const T &) const
Find and return a T, if not found return the given default.
dictionary()
Construct top-level dictionary null.
Definition: dictionary.C:255
const dictionary & subDict(const word &) const
Find and return a sub-dictionary.
Definition: dictionary.C:778
bool found(const word &, bool recursive=false, bool patternMatch=true) const
Search dictionary for given keyword.
Definition: dictionary.C:468
static const dictionary null
Null dictionary.
Definition: dictionary.H:261
A class for handling file names.
Definition: fileName.H:82
Registry of regIOobjects.
virtual bool read()
Read object.
Selector class for relaxation factors, solver type and solution.
Definition: solution.H:51
void enableCache(const word &name) const
Enable caching of the given field.
Definition: solution.C:137
solution(const objectRegistry &obr, const fileName &dictName)
Construct for given objectRegistry and dictionary.
Definition: solution.C:89
bool cache(const word &name) const
Return true if the given field should be cached.
Definition: solution.C:119
const dictionary & solversDict() const
Return the solver controls dictionary.
Definition: solution.C:232
bool relaxField(const word &name) const
Return true if the relaxation factor is given for the field.
Definition: solution.C:150
scalar fieldRelaxationFactor(const word &name) const
Return the relaxation factor for the given field.
Definition: solution.C:174
const dictionary & solverDict(const word &name) const
Return the solver controls dictionary for the given field.
Definition: solution.C:238
scalar equationRelaxationFactor(const word &name) const
Return the relaxation factor for the given eqation.
Definition: solution.C:203
bool relaxEquation(const word &name) const
Return true if the relaxation factor is given for the equation.
Definition: solution.C:163
bool read()
Read the solution dictionary.
Definition: solution.C:249
A class for handling words, derived from string.
Definition: word.H:63
#define FatalIOErrorInFunction(ios)
Report an error message using Foam::FatalIOError.
Definition: error.H:346
#define IOWarningInFunction(ios)
Report an IO warning using Foam::Warning.
const dimensionSet time
Namespace for OpenFOAM.
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:124
int system(const std::string &command)
Execute the specified command.
Definition: POSIX.C:1230
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:288
messageStream Info
IOerror FatalIOError
word name(const LagrangianState state)
Return a string representation of a Lagrangian state enumeration.
defineTypeNameAndDebug(atmosphericBoundaryLayer, 0)