initial commit for version 1.6.x patch release
[OpenFOAM-1.6.x.git] / src / OpenFOAM / matrices / solution / solution.C
blob030b3df9ae1296ebc8d42fe69b725e89a29503d4
1 /*---------------------------------------------------------------------------*\
2   =========                 |
3   \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
4    \\    /   O peration     |
5     \\  /    A nd           | Copyright (C) 1991-2009 OpenCFD Ltd.
6      \\/     M anipulation  |
7 -------------------------------------------------------------------------------
8 License
9     This file is part of OpenFOAM.
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 the
13     Free Software Foundation; either version 2 of the License, or (at your
14     option) any later version.
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.
21     You should have received a copy of the GNU General Public License
22     along with OpenFOAM; if not, write to the Free Software Foundation,
23     Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
25 \*---------------------------------------------------------------------------*/
27 #include "solution.H"
28 #include "Time.H"
30 // These are for old syntax compatibility:
31 #include "BICCG.H"
32 #include "ICCG.H"
33 #include "IStringStream.H"
35 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
37 int Foam::solution::debug(::Foam::debug::debugSwitch("solution", 0));
39 // List of sub-dictionaries to rewrite
40 //! @cond localScope
41 static const Foam::List<Foam::word> subDictNames
43     Foam::IStringStream("(preconditioner smoother)")()
45 //! @endcond localScope
47 // * * * * * * * * * * * * * * * * Constructors  * * * * * * * * * * * * * * //
49 Foam::solution::solution(const objectRegistry& obr, const fileName& dictName)
51     IOdictionary
52     (
53         IOobject
54         (
55             dictName,
56             obr.time().system(),
57             obr,
58             IOobject::MUST_READ,
59             IOobject::NO_WRITE
60         )
61     ),
62     relaxationFactors_
63     (
64         ITstream("relaxationFactors",
65         tokenList())()
66     ),
67     defaultRelaxationFactor_(0),
68     solvers_(ITstream("solvers", tokenList())())
70     read();
74 // * * * * * * * * * * * * * * * Member Functions  * * * * * * * * * * * * * //
76 Foam::label Foam::solution::upgradeSolverDict
78     dictionary& dict,
79     const bool verbose
82     label nChanged = 0;
84     // backward compatibility:
85     // recast primitive entries into dictionary entries
86     forAllIter(dictionary, dict, iter)
87     {
88         if (!iter().isDict())
89         {
90             Istream& is = iter().stream();
91             word name(is);
92             dictionary subdict;
94             if (name == "BICCG")
95             {
96                 // special treatment for very old syntax
97                 subdict = BICCG::solverDict(is);
98             }
99             else if (name == "ICCG")
100             {
101                 // special treatment for very old syntax
102                 subdict = ICCG::solverDict(is);
103             }
104             else
105             {
106                 subdict.add("solver", name);
107                 subdict <<= dictionary(is);
109                 // preconditioner and smoother entries can be
110                 // 1) primitiveEntry w/o settings,
111                 // 2) or a dictionaryEntry.
112                 // transform primitiveEntry with settings -> dictionaryEntry
113                 forAll(subDictNames, dictI)
114                 {
115                     const word& dictName = subDictNames[dictI];
116                     entry* ePtr = subdict.lookupEntryPtr(dictName,false,false);
118                     if (ePtr && !ePtr->isDict())
119                     {
120                         Istream& is = ePtr->stream();
121                         is >> name;
123                         if (!is.eof())
124                         {
125                             dictionary newDict;
126                             newDict.add(dictName, name);
127                             newDict <<= dictionary(is);
129                             subdict.set(dictName, newDict);
130                         }
131                     }
132                 }
133             }
136             // write out information to help people adjust to the new syntax
137             if (verbose && Pstream::master())
138             {
139                 Info<< "// using new solver syntax:\n"
140                     << iter().keyword() << subdict << endl;
141             }
143             // overwrite with dictionary entry
144             dict.set(iter().keyword(), subdict);
146             nChanged++;
147         }
148     }
150     return nChanged;
154 bool Foam::solution::read()
156     if (regIOobject::read())
157     {
158         const dictionary& dict = solutionDict();
160         if (dict.found("relaxationFactors"))
161         {
162             relaxationFactors_ = dict.subDict("relaxationFactors");
163         }
165         relaxationFactors_.readIfPresent("default", defaultRelaxationFactor_);
167         if (dict.found("solvers"))
168         {
169             solvers_ = dict.subDict("solvers");
170             upgradeSolverDict(solvers_);
171         }
173         return true;
174     }
175     else
176     {
177         return false;
178     }
182 const Foam::dictionary& Foam::solution::solutionDict() const
184     if (found("select"))
185     {
186         return subDict(word(lookup("select")));
187     }
188     else
189     {
190         return *this;
191     }
195 bool Foam::solution::relax(const word& name) const
197     if (debug)
198     {
199         Info<< "Find relax for " << name << endl;
200     }
202     return
203         relaxationFactors_.found(name)
204      || relaxationFactors_.found("default");
208 Foam::scalar Foam::solution::relaxationFactor(const word& name) const
210     if (debug)
211     {
212         Info<< "Lookup relaxationFactor for " << name << endl;
213     }
215     if (relaxationFactors_.found(name))
216     {
217         return readScalar(relaxationFactors_.lookup(name));
218     }
219     else if (defaultRelaxationFactor_ > SMALL)
220     {
221         return defaultRelaxationFactor_;
222     }
223     else
224     {
225         FatalIOErrorIn
226         (
227             "Foam::solution::relaxationFactor(const word&)",
228             relaxationFactors_
229         )   << "Cannot find relaxationFactor for '" << name
230             << "' or a suitable default value."
231             << exit(FatalIOError);
233         return 0;
234     }
238 const Foam::dictionary& Foam::solution::solverDict(const word& name) const
240     if (debug)
241     {
242         InfoIn("solution::solverDict(const word&)")
243             << "Lookup solver for " << name << endl;
244     }
246     return solvers_.subDict(name);
250 const Foam::dictionary& Foam::solution::solver(const word& name) const
252     if (debug)
253     {
254         InfoIn("solution::solver(const word&)")
255             << "Lookup solver for " << name << endl;
256     }
258     return solvers_.subDict(name);
262 // ************************************************************************* //