solution.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 | Copyright (C) 2011-2015 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 // These are for old syntax compatibility:
30 #include "BICCG.H"
31 #include "ICCG.H"
32 #include "IStringStream.H"
33 
34 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
35 
36 namespace Foam
37 {
38  defineDebugSwitchWithName(solution, "solution", 0);
39 }
40 
41 // List of sub-dictionaries to rewrite
43 static const Foam::List<Foam::word> subDictNames
44 (
45  Foam::IStringStream("(preconditioner smoother)")()
46 );
48 
49 
50 // * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * * //
51 
53 {
54  if (dict.found("cache"))
55  {
56  cache_ = dict.subDict("cache");
57  caching_ = cache_.lookupOrDefault("active", true);
58  }
59 
60  if (dict.found("relaxationFactors"))
61  {
62  const dictionary& relaxDict(dict.subDict("relaxationFactors"));
63  if (relaxDict.found("fields") || relaxDict.found("equations"))
64  {
65  if (relaxDict.found("fields"))
66  {
67  fieldRelaxDict_ = relaxDict.subDict("fields");
68  }
69 
70  if (relaxDict.found("equations"))
71  {
72  eqnRelaxDict_ = relaxDict.subDict("equations");
73  }
74  }
75  else
76  {
77  // backwards compatibility
79 
80  const wordList entryNames(relaxDict.toc());
81  forAll(entryNames, i)
82  {
83  const word& e = entryNames[i];
84  scalar value = readScalar(relaxDict.lookup(e));
85 
86  if (e(0, 1) == "p")
87  {
88  fieldRelaxDict_.add(e, value);
89  }
90  else if (e.length() >= 3)
91  {
92  if (e(0, 3) == "rho")
93  {
94  fieldRelaxDict_.add(e, value);
95  }
96  }
97 
98  }
99 
100  eqnRelaxDict_ = relaxDict;
101  }
102 
104  fieldRelaxDict_.lookupOrDefault<scalar>("default", 0.0);
105 
107  eqnRelaxDict_.lookupOrDefault<scalar>("default", 0.0);
108 
109  if (debug)
110  {
111  Info<< "relaxation factors:" << nl
112  << "fields: " << fieldRelaxDict_ << nl
113  << "equations: " << eqnRelaxDict_ << endl;
114  }
115  }
116 
117 
118  if (dict.found("solvers"))
119  {
120  solvers_ = dict.subDict("solvers");
122  }
123 }
124 
125 
126 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
127 
129 (
130  const objectRegistry& obr,
131  const fileName& dictName
132 )
133 :
135  (
136  IOobject
137  (
138  dictName,
139  obr.time().system(),
140  obr,
141  (
145  : obr.readOpt()
146  ),
148  )
149  ),
150  cache_(dictionary::null),
151  caching_(false),
152  fieldRelaxDict_(dictionary::null),
153  eqnRelaxDict_(dictionary::null),
154  fieldRelaxDefault_(0),
155  eqnRelaxDefault_(0),
156  solvers_(dictionary::null)
157 {
158  if
159  (
160  readOpt() == IOobject::MUST_READ
161  || readOpt() == IOobject::MUST_READ_IF_MODIFIED
162  || (readOpt() == IOobject::READ_IF_PRESENT && headerOk())
163  )
164  {
165  read(solutionDict());
166  }
167 }
168 
169 
170 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
171 
173 (
174  dictionary& dict,
175  const bool verbose
176 )
177 {
178  label nChanged = 0;
179 
180  // backward compatibility:
181  // recast primitive entries into dictionary entries
182  forAllIter(dictionary, dict, iter)
183  {
184  if (!iter().isDict())
185  {
186  Istream& is = iter().stream();
187  word name(is);
188  dictionary subdict;
189 
190  if (name == "BICCG")
191  {
192  // special treatment for very old syntax
193  subdict = BICCG::solverDict(is);
194  }
195  else if (name == "ICCG")
196  {
197  // special treatment for very old syntax
198  subdict = ICCG::solverDict(is);
199  }
200  else
201  {
202  subdict.add("solver", name);
203  subdict <<= dictionary(is);
204 
205  // preconditioner and smoother entries can be
206  // 1) primitiveEntry w/o settings,
207  // 2) or a dictionaryEntry.
208  // transform primitiveEntry with settings -> dictionaryEntry
209  forAll(subDictNames, dictI)
210  {
211  const word& dictName = subDictNames[dictI];
212  entry* ePtr = subdict.lookupEntryPtr(dictName,false,false);
213 
214  if (ePtr && !ePtr->isDict())
215  {
216  Istream& is = ePtr->stream();
217  is >> name;
218 
219  if (!is.eof())
220  {
221  dictionary newDict;
222  newDict.add(dictName, name);
223  newDict <<= dictionary(is);
224 
225  subdict.set(dictName, newDict);
226  }
227  }
228  }
229  }
230 
231 
232  // write out information to help people adjust to the new syntax
233  if (verbose && Pstream::master())
234  {
235  Info<< "// using new solver syntax:\n"
236  << iter().keyword() << subdict << endl;
237  }
238 
239  // overwrite with dictionary entry
240  dict.set(iter().keyword(), subdict);
241 
242  nChanged++;
243  }
244  }
245 
246  return nChanged;
247 }
248 
249 
250 bool Foam::solution::cache(const word& name) const
251 {
252  if (caching_)
253  {
254  if (debug)
255  {
256  Info<< "Cache: find entry for " << name << endl;
257  }
258 
259  return cache_.found(name);
260  }
261  else
262  {
263  return false;
264  }
265 }
266 
267 
269 {
270  if (debug)
271  {
272  Info<< "Field relaxation factor for " << name
273  << " is " << (fieldRelaxDict_.found(name) ? "set" : "unset")
274  << endl;
275  }
276 
277  return fieldRelaxDict_.found(name) || fieldRelaxDict_.found("default");
278 }
279 
280 
282 {
283  if (debug)
284  {
285  Info<< "Find equation relaxation factor for " << name << endl;
286  }
287 
288  return eqnRelaxDict_.found(name) || eqnRelaxDict_.found("default");
289 }
290 
291 
293 {
294  if (debug)
295  {
296  Info<< "Lookup variable relaxation factor for " << name << endl;
297  }
298 
299  if (fieldRelaxDict_.found(name))
300  {
301  return readScalar(fieldRelaxDict_.lookup(name));
302  }
303  else if (fieldRelaxDefault_ > SMALL)
304  {
305  return fieldRelaxDefault_;
306  }
307  else
308  {
310  (
311  fieldRelaxDict_
312  ) << "Cannot find variable relaxation factor for '" << name
313  << "' or a suitable default value."
314  << exit(FatalIOError);
315 
316  return 0;
317  }
318 }
319 
320 
322 {
323  if (debug)
324  {
325  Info<< "Lookup equation relaxation factor for " << name << endl;
326  }
327 
328  if (eqnRelaxDict_.found(name))
329  {
330  return readScalar(eqnRelaxDict_.lookup(name));
331  }
332  else if (eqnRelaxDefault_ > SMALL)
333  {
334  return eqnRelaxDefault_;
335  }
336  else
337  {
339  (
340  eqnRelaxDict_
341  ) << "Cannot find equation relaxation factor for '" << name
342  << "' or a suitable default value."
343  << exit(FatalIOError);
344 
345  return 0;
346  }
347 }
348 
349 
351 {
352  if (found("select"))
353  {
354  return subDict(word(lookup("select")));
355  }
356  else
357  {
358  return *this;
359  }
360 }
361 
362 
364 {
365  if (debug)
366  {
368  << "Lookup solver for " << name << endl;
369  }
370 
371  return solvers_.subDict(name);
372 }
373 
374 
376 {
377  if (debug)
378  {
380  << "Lookup solver for " << name << endl;
381  }
382 
383  return solvers_.subDict(name);
384 }
385 
386 
388 {
389  if (regIOobject::read())
390  {
391  read(solutionDict());
392 
393  return true;
394  }
395  else
396  {
397  return false;
398  }
399 }
400 
401 
402 // ************************************************************************* //
Foam::entry
A keyword and a list of tokens is an 'entry'.
Definition: entry.H:65
Foam::IOdictionary
IOdictionary is derived from dictionary and IOobject to give the dictionary automatic IO functionalit...
Definition: IOdictionary.H:53
Foam::IOobject
IOobject defines the attributes of an object for which implicit objectRegistry management is supporte...
Definition: IOobject.H:91
InfoInFunction
#define InfoInFunction
Report a information message using Foam::Info.
Definition: messageStream.H:281
Foam::solution::solverDict
const dictionary & solverDict(const word &name) const
Return the solver controls dictionary for the given field.
Definition: solution.C:363
Foam::word
A class for handling words, derived from string.
Definition: word.H:59
forAllIter
#define forAllIter(Container, container, iter)
Iterate across all elements in the container object of type.
Definition: UList.H:431
Foam::fileName
A class for handling file names.
Definition: fileName.H:69
Foam::entry::stream
virtual ITstream & stream() const =0
Return token stream if this entry is a primitive entry.
Foam::defineDebugSwitchWithName
defineDebugSwitchWithName(pointMVCWeight, "pointMVCWeight", 0)
forAll
#define forAll(list, i)
Loop across all elements in list.
Definition: UList.H:406
Foam::solution::cache
bool cache(const word &name) const
Return true if the given field should be cached.
Definition: solution.C:250
Foam::solution::cache_
dictionary cache_
Dictionary of temporary fields to cache.
Definition: solution.H:55
Foam::IOstream::eof
bool eof() const
Return true if end of input seen.
Definition: IOstream.H:339
Foam::ICCG::solverDict
static dictionary solverDict(const scalar tol, const scalar relTol)
Return the dictionary constructed from the components.
Definition: ICCG.C:41
Foam::read
bool read(const char *, int32_t &)
Definition: int32IO.C:87
Foam::objectRegistry::time
const Time & time() const
Return time.
Definition: objectRegistry.H:117
Foam::dictionary::lookup
ITstream & lookup(const word &, bool recursive=false, bool patternMatch=true) const
Find and return an entry data stream.
Definition: dictionary.C:449
Foam::regIOobject::read
virtual bool read()
Read object.
Definition: regIOobjectRead.C:171
Foam::solution::upgradeSolverDict
static label upgradeSolverDict(dictionary &dict, const bool verbose=true)
Update from older solver controls syntax.
Definition: solution.C:173
Foam::dictionary::lookupOrDefault
T lookupOrDefault(const word &, const T &, bool recursive=false, bool patternMatch=true) const
Find and return a T,.
Definition: dictionaryTemplates.C:33
Foam::solution::solvers_
dictionary solvers_
Dictionary of solver parameters for all the fields.
Definition: solution.H:73
Foam::solution::solution
solution(const solution &)
Disallow default bitwise copy construct and assignment.
Foam::IOobject::MUST_READ
@ MUST_READ
Definition: IOobject.H:108
Foam::FatalIOError
IOerror FatalIOError
Foam::endl
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:251
Foam::solution::fieldRelaxDict_
dictionary fieldRelaxDict_
Dictionary of relaxation factors for all the fields.
Definition: solution.H:61
ICCG.H
Foam::IOobject::NO_WRITE
@ NO_WRITE
Definition: IOobject.H:118
IStringStream.H
Foam::BICCG::solverDict
static dictionary solverDict(const scalar tol, const scalar relTol)
Return the dictionary constructed from the components.
Definition: BICCG.C:42
Foam::objectRegistry
Registry of regIOobjects.
Definition: objectRegistry.H:50
Foam::IOobject::MUST_READ_IF_MODIFIED
@ MUST_READ_IF_MODIFIED
Definition: IOobject.H:109
dictName
const word dictName("particleTrackDict")
Foam::solution::eqnRelaxDefault_
scalar eqnRelaxDefault_
Optional default relaxation factor for all the equations.
Definition: solution.H:70
Foam::solution::fieldRelaxationFactor
scalar fieldRelaxationFactor(const word &name) const
Return the relaxation factor for the given field.
Definition: solution.C:292
Foam::label
intWM_LABEL_SIZE_t label
A label is an int32_t or int64_t as specified by the pre-processor macro WM_LABEL_SIZE.
Definition: label.H:59
Foam::dictionary::found
bool found(const word &, bool recursive=false, bool patternMatch=true) const
Search dictionary for given keyword.
Definition: dictionary.C:304
Foam::entry::isDict
virtual bool isDict() const
Return true if this entry is a dictionary.
Definition: entry.H:153
Foam::TimePaths::system
const word & system() const
Return system name.
Definition: TimePaths.H:120
Foam::IOobject::readOpt
readOption readOpt() const
Definition: IOobject.H:317
Foam::Istream
An Istream is an abstract base class for all input systems (streams, files, token lists etc)....
Definition: Istream.H:57
Foam::nl
static const char nl
Definition: Ostream.H:260
Foam::Info
messageStream Info
Foam::solution::eqnRelaxDict_
dictionary eqnRelaxDict_
Dictionary of relaxation factors for all the equations.
Definition: solution.H:64
Foam::solution::read
bool read()
Read the solution dictionary.
Definition: solution.C:387
Foam::solution::fieldRelaxDefault_
scalar fieldRelaxDefault_
Optional default relaxation factor for all the fields.
Definition: solution.H:67
dict
dictionary dict
Definition: searchingEngine.H:14
Foam::dictionary
A list of keyword definitions, which are a keyword followed by any number of values (e....
Definition: dictionary.H:137
Foam::solution::relaxEquation
bool relaxEquation(const word &name) const
Return true if the relaxation factor is given for the equation.
Definition: solution.C:281
Foam::solution::caching_
bool caching_
Switch for the caching mechanism.
Definition: solution.H:58
Foam::IStringStream
Input from memory buffer stream.
Definition: IStringStream.H:49
Foam
Namespace for OpenFOAM.
Definition: combustionModel.C:30
Foam::e
const double e
Elementary charge.
Definition: doubleFloat.H:94
Foam::solution::debug
static int debug
Debug switch.
Definition: solution.H:94
solution.H
Foam::exit
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:124
Foam::solution::equationRelaxationFactor
scalar equationRelaxationFactor(const word &name) const
Return the relaxation factor for the given eqation.
Definition: solution.C:321
Foam::UPstream::master
static bool master(const label communicator=0)
Am I the master process.
Definition: UPstream.H:399
found
bool found
Definition: TABSMDCalcMethod2.H:32
BICCG.H
Foam::dictionary::lookupEntryPtr
const entry * lookupEntryPtr(const word &, bool recursive, bool patternMatch) const
Find and return an entry data stream pointer if present.
Definition: dictionary.C:343
Foam::solution::solutionDict
const dictionary & solutionDict() const
Return the selected sub-dictionary of solvers if the "select".
Definition: solution.C:350
Foam::readScalar
bool readScalar(const char *buf, doubleScalar &s)
Read whole of buf as a scalar. Return true if succesful.
Definition: doubleScalar.H:63
Foam::List< Foam::word >
FatalIOErrorInFunction
#define FatalIOErrorInFunction(ios)
Report an error message using Foam::FatalIOError.
Definition: error.H:330
Foam::solution::solver
const dictionary & solver(const word &name) const
Return the solver controls dictionary for the given field.
Definition: solution.C:375
solutionDict
fvSolution solutionDict(runTime)
Foam::dictionary::toc
wordList toc() const
Return the table of contents.
Definition: dictionary.C:697
Foam::dictionary::subDict
const dictionary & subDict(const word &) const
Find and return a sub-dictionary.
Definition: dictionary.C:631
Foam::IOobject::READ_IF_PRESENT
@ READ_IF_PRESENT
Definition: IOobject.H:110
Foam::dictionary::clear
void clear()
Clear the dictionary.
Definition: dictionary.C:1050
Foam::solution::relaxField
bool relaxField(const word &name) const
Return true if the relaxation factor is given for the field.
Definition: solution.C:268
Foam::name
word name(const complex &)
Return a string representation of a complex.
Definition: complex.C:47
Foam::dictionary::null
static const dictionary null
Null dictionary.
Definition: dictionary.H:193
Foam::dictionary::add
bool add(entry *, bool mergeEntry=false)
Add a new entry.
Definition: dictionary.C:729
lookup
stressControl lookup("compactNormalStress") >> compactNormalStress
Foam::dictionary::set
void set(entry *)
Assign a new entry, overwrite any existing entry.
Definition: dictionary.C:856