OpenFOAM logo
The Open Source CFD Toolbox
  Source Guide OpenCFD Solutions Contact OpenFOAM

solution.C

Go to the documentation of this file.
00001 /*---------------------------------------------------------------------------*\
00002   =========                 |
00003   \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
00004    \\    /   O peration     |
00005     \\  /    A nd           | Copyright (C) 1991-2009 OpenCFD Ltd.
00006      \\/     M anipulation  |
00007 -------------------------------------------------------------------------------
00008 License
00009     This file is part of OpenFOAM.
00010 
00011     OpenFOAM is free software; you can redistribute it and/or modify it
00012     under the terms of the GNU General Public License as published by the
00013     Free Software Foundation; either version 2 of the License, or (at your
00014     option) any later version.
00015 
00016     OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
00017     ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
00018     FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
00019     for more details.
00020 
00021     You should have received a copy of the GNU General Public License
00022     along with OpenFOAM; if not, write to the Free Software Foundation,
00023     Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
00024 
00025 \*---------------------------------------------------------------------------*/
00026 
00027 #include "solution.H"
00028 #include "Time.H"
00029 
00030 // These are for old syntax compatibility:
00031 #include "BICCG.H"
00032 #include "ICCG.H"
00033 #include "IStringStream.H"
00034 
00035 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
00036 
00037 int Foam::solution::debug(::Foam::debug::debugSwitch("solution", 0));
00038 
00039 // List of sub-dictionaries to rewrite
00041 static const Foam::List<Foam::word> subDictNames
00042 (
00043     Foam::IStringStream("(preconditioner smoother)")()
00044 );
00046 
00047 // * * * * * * * * * * * * * * * * Constructors  * * * * * * * * * * * * * * //
00048 
00049 Foam::solution::solution(const objectRegistry& obr, const fileName& dictName)
00050 :
00051     IOdictionary
00052     (
00053         IOobject
00054         (
00055             dictName,
00056             obr.time().system(),
00057             obr,
00058             IOobject::MUST_READ,
00059             IOobject::NO_WRITE
00060         )
00061     ),
00062     relaxationFactors_
00063     (
00064         ITstream("relaxationFactors",
00065         tokenList())()
00066     ),
00067     defaultRelaxationFactor_(0),
00068     solvers_(ITstream("solvers", tokenList())())
00069 {
00070     read();
00071 }
00072 
00073 
00074 // * * * * * * * * * * * * * * * Member Functions  * * * * * * * * * * * * * //
00075 
00076 Foam::label Foam::solution::upgradeSolverDict
00077 (
00078     dictionary& dict,
00079     const bool verbose
00080 )
00081 {
00082     label nChanged = 0;
00083 
00084     // backward compatibility:
00085     // recast primitive entries into dictionary entries
00086     forAllIter(dictionary, dict, iter)
00087     {
00088         if (!iter().isDict())
00089         {
00090             Istream& is = iter().stream();
00091             word name(is);
00092             dictionary subdict;
00093 
00094             if (name == "BICCG")
00095             {
00096                 // special treatment for very old syntax
00097                 subdict = BICCG::solverDict(is);
00098             }
00099             else if (name == "ICCG")
00100             {
00101                 // special treatment for very old syntax
00102                 subdict = ICCG::solverDict(is);
00103             }
00104             else
00105             {
00106                 subdict.add("solver", name);
00107                 subdict <<= dictionary(is);
00108 
00109                 // preconditioner and smoother entries can be
00110                 // 1) primitiveEntry w/o settings,
00111                 // 2) or a dictionaryEntry.
00112                 // transform primitiveEntry with settings -> dictionaryEntry
00113                 forAll(subDictNames, dictI)
00114                 {
00115                     const word& dictName = subDictNames[dictI];
00116                     entry* ePtr = subdict.lookupEntryPtr(dictName,false,false);
00117 
00118                     if (ePtr && !ePtr->isDict())
00119                     {
00120                         Istream& is = ePtr->stream();
00121                         is >> name;
00122 
00123                         if (!is.eof())
00124                         {
00125                             dictionary newDict;
00126                             newDict.add(dictName, name);
00127                             newDict <<= dictionary(is);
00128 
00129                             subdict.set(dictName, newDict);
00130                         }
00131                     }
00132                 }
00133             }
00134 
00135 
00136             // write out information to help people adjust to the new syntax
00137             if (verbose && Pstream::master())
00138             {
00139                 Info<< "// using new solver syntax:\n"
00140                     << iter().keyword() << subdict << endl;
00141             }
00142 
00143             // overwrite with dictionary entry
00144             dict.set(iter().keyword(), subdict);
00145 
00146             nChanged++;
00147         }
00148     }
00149 
00150     return nChanged;
00151 }
00152 
00153 
00154 bool Foam::solution::read()
00155 {
00156     if (regIOobject::read())
00157     {
00158         const dictionary& dict = solutionDict();
00159 
00160         if (dict.found("relaxationFactors"))
00161         {
00162             relaxationFactors_ = dict.subDict("relaxationFactors");
00163         }
00164 
00165         relaxationFactors_.readIfPresent("default", defaultRelaxationFactor_);
00166 
00167         if (dict.found("solvers"))
00168         {
00169             solvers_ = dict.subDict("solvers");
00170             upgradeSolverDict(solvers_);
00171         }
00172 
00173         return true;
00174     }
00175     else
00176     {
00177         return false;
00178     }
00179 }
00180 
00181 
00182 const Foam::dictionary& Foam::solution::solutionDict() const
00183 {
00184     if (found("select"))
00185     {
00186         return subDict(word(lookup("select")));
00187     }
00188     else
00189     {
00190         return *this;
00191     }
00192 }
00193 
00194 
00195 bool Foam::solution::relax(const word& name) const
00196 {
00197     if (debug)
00198     {
00199         Info<< "Find relax for " << name << endl;
00200     }
00201 
00202     return
00203         relaxationFactors_.found(name)
00204      || relaxationFactors_.found("default");
00205 }
00206 
00207 
00208 Foam::scalar Foam::solution::relaxationFactor(const word& name) const
00209 {
00210     if (debug)
00211     {
00212         Info<< "Lookup relaxationFactor for " << name << endl;
00213     }
00214 
00215     if (relaxationFactors_.found(name))
00216     {
00217         return readScalar(relaxationFactors_.lookup(name));
00218     }
00219     else if (defaultRelaxationFactor_ > SMALL)
00220     {
00221         return defaultRelaxationFactor_;
00222     }
00223     else
00224     {
00225         FatalIOErrorIn
00226         (
00227             "Foam::solution::relaxationFactor(const word&)",
00228             relaxationFactors_
00229         )   << "Cannot find relaxationFactor for '" << name
00230             << "' or a suitable default value."
00231             << exit(FatalIOError);
00232 
00233         return 0;
00234     }
00235 }
00236 
00237 
00238 const Foam::dictionary& Foam::solution::solverDict(const word& name) const
00239 {
00240     if (debug)
00241     {
00242         InfoIn("solution::solverDict(const word&)")
00243             << "Lookup solver for " << name << endl;
00244     }
00245 
00246     return solvers_.subDict(name);
00247 }
00248 
00249 
00250 const Foam::dictionary& Foam::solution::solver(const word& name) const
00251 {
00252     if (debug)
00253     {
00254         InfoIn("solution::solver(const word&)")
00255             << "Lookup solver for " << name << endl;
00256     }
00257 
00258     return solvers_.subDict(name);
00259 }
00260 
00261 
00262 // ************************************************************************* //
Copyright © 2000-2009 OpenCFD Ltd