FreeFOAM The Cross-Platform CFD Toolkit
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) 1991-2010 OpenCFD Ltd.
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 <OpenFOAM/Time.H>
28 
29 // These are for old syntax compatibility:
30 #include <OpenFOAM/BICCG.H>
31 #include <OpenFOAM/ICCG.H>
32 #include <OpenFOAM/IStringStream.H>
33 
34 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
35 
37 
38 // List of sub-dictionaries to rewrite
40 static const Foam::List<Foam::word> subDictNames
41 (
42  Foam::IStringStream("(preconditioner smoother)")()
43 );
45 
46 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
47 
48 Foam::solution::solution(const objectRegistry& obr, const fileName& dictName)
49 :
51  (
52  IOobject
53  (
54  dictName,
55  obr.time().system(),
56  obr,
57  IOobject::MUST_READ,
58  IOobject::NO_WRITE
59  )
60  ),
61  relaxationFactors_
62  (
63  ITstream("relaxationFactors",
64  tokenList())()
65  ),
66  defaultRelaxationFactor_(0),
67  solvers_(ITstream("solvers", tokenList())())
68 {
69  read();
70 }
71 
72 
73 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
74 
76 (
77  dictionary& dict,
78  const bool verbose
79 )
80 {
81  label nChanged = 0;
82 
83  // backward compatibility:
84  // recast primitive entries into dictionary entries
85  forAllIter(dictionary, dict, iter)
86  {
87  if (!iter().isDict())
88  {
89  Istream& is = iter().stream();
90  word name(is);
91  dictionary subdict;
92 
93  if (name == "BICCG")
94  {
95  // special treatment for very old syntax
96  subdict = BICCG::solverDict(is);
97  }
98  else if (name == "ICCG")
99  {
100  // special treatment for very old syntax
101  subdict = ICCG::solverDict(is);
102  }
103  else
104  {
105  subdict.add("solver", name);
106  subdict <<= dictionary(is);
107 
108  // preconditioner and smoother entries can be
109  // 1) primitiveEntry w/o settings,
110  // 2) or a dictionaryEntry.
111  // transform primitiveEntry with settings -> dictionaryEntry
112  forAll(subDictNames, dictI)
113  {
114  const word& dictName = subDictNames[dictI];
115  entry* ePtr = subdict.lookupEntryPtr(dictName,false,false);
116 
117  if (ePtr && !ePtr->isDict())
118  {
119  Istream& is = ePtr->stream();
120  is >> name;
121 
122  if (!is.eof())
123  {
124  dictionary newDict;
125  newDict.add(dictName, name);
126  newDict <<= dictionary(is);
127 
128  subdict.set(dictName, newDict);
129  }
130  }
131  }
132  }
133 
134 
135  // write out information to help people adjust to the new syntax
136  if (verbose && Pstream::master())
137  {
138  Info<< "// using new solver syntax:\n"
139  << iter().keyword() << subdict << endl;
140  }
141 
142  // overwrite with dictionary entry
143  dict.set(iter().keyword(), subdict);
144 
145  nChanged++;
146  }
147  }
148 
149  return nChanged;
150 }
151 
152 
154 {
155  if (regIOobject::read())
156  {
157  const dictionary& dict = solutionDict();
158 
159  if (dict.found("relaxationFactors"))
160  {
161  relaxationFactors_ = dict.subDict("relaxationFactors");
162  }
163 
164  relaxationFactors_.readIfPresent("default", defaultRelaxationFactor_);
165 
166  if (dict.found("solvers"))
167  {
168  solvers_ = dict.subDict("solvers");
169  upgradeSolverDict(solvers_);
170  }
171 
172  return true;
173  }
174  else
175  {
176  return false;
177  }
178 }
179 
180 
182 {
183  if (found("select"))
184  {
185  return subDict(word(lookup("select")));
186  }
187  else
188  {
189  return *this;
190  }
191 }
192 
193 
194 bool Foam::solution::relax(const word& name) const
195 {
196  if (debug)
197  {
198  Info<< "Find relax for " << name << endl;
199  }
200 
201  return
202  relaxationFactors_.found(name)
203  || relaxationFactors_.found("default");
204 }
205 
206 
207 Foam::scalar Foam::solution::relaxationFactor(const word& name) const
208 {
209  if (debug)
210  {
211  Info<< "Lookup relaxationFactor for " << name << endl;
212  }
213 
214  if (relaxationFactors_.found(name))
215  {
216  return readScalar(relaxationFactors_.lookup(name));
217  }
218  else if (defaultRelaxationFactor_ > SMALL)
219  {
220  return defaultRelaxationFactor_;
221  }
222  else
223  {
225  (
226  "Foam::solution::relaxationFactor(const word&)",
227  relaxationFactors_
228  ) << "Cannot find relaxationFactor for '" << name
229  << "' or a suitable default value."
230  << exit(FatalIOError);
231 
232  return 0;
233  }
234 }
235 
236 
238 {
239  if (debug)
240  {
241  InfoIn("solution::solverDict(const word&)")
242  << "Lookup solver for " << name << endl;
243  }
244 
245  return solvers_.subDict(name);
246 }
247 
248 
250 {
251  if (debug)
252  {
253  InfoIn("solution::solver(const word&)")
254  << "Lookup solver for " << name << endl;
255  }
256 
257  return solvers_.subDict(name);
258 }
259 
260 
261 // ************************ vim: set sw=4 sts=4 et: ************************ //