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

changeDictionary.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 Application
00026     changeDictionary
00027 
00028 Description
00029     Utility to change dictionary entries, e.g. can be used to change the patch
00030     type in the field and polyMesh/boundary files.
00031 
00032     Reads dictionaries (fields) and entries to change from a dictionary.
00033     E.g. to make the @em movingWall a @em fixedValue for @em p, the
00034     @c system/changeDictionaryDict would contain the following:
00035     @verbatim
00036     dictionaryReplacement
00037     {
00038         p                           // field to change
00039         {
00040             boundaryField
00041             {
00042                 movingWall          // entry to change
00043                 {
00044                     type            fixedValue;
00045                     value           uniform 123.45;
00046                 }
00047             }
00048         }
00049     }
00050     @endverbatim
00051 
00052 
00053 \*---------------------------------------------------------------------------*/
00054 
00055 #include "argList.H"
00056 #include "IOobjectList.H"
00057 #include "IOPtrList.H"
00058 #include "volFields.H"
00059 
00060 using namespace Foam;
00061 
00062 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
00063 
00064 namespace Foam
00065 {
00066     defineTemplateTypeNameAndDebug(IOPtrList<entry>, 0);
00067 }
00068 
00069 
00070 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
00071 // Main program:
00072 
00073 int main(int argc, char *argv[])
00074 {
00075 #   include "addRegionOption.H"
00076 #   include "setRootCase.H"
00077 #   include "createTime.H"
00078 #   include "createNamedMesh.H"
00079 
00080     fileName regionPrefix = "";
00081     if (regionName != fvMesh::defaultRegion)
00082     {
00083         regionPrefix = regionName;
00084     }
00085 
00086     // Get the replacement rules from a dictionary
00087     IOdictionary dict
00088     (
00089         IOobject
00090         (
00091             "changeDictionaryDict",
00092             runTime.system(),
00093             mesh,
00094             IOobject::MUST_READ,
00095             IOobject::NO_WRITE
00096         )
00097     );
00098     const dictionary& replaceDicts = dict.subDict("dictionaryReplacement");
00099     Info<< "Read dictionary " << dict.name()
00100         << " with replacements for dictionaries "
00101         << replaceDicts.toc() << endl;
00102 
00103 
00104     // Every replacement is a dictionary name and a keyword in this
00105 
00106     forAllConstIter(dictionary, replaceDicts, fieldIter)
00107     {
00108         const word& fieldName = fieldIter().keyword();
00109         Info<< "Replacing entries in dictionary " << fieldName << endl;
00110 
00111         // Handle 'boundary' specially:
00112         // - is PtrList of dictionaries
00113         // - is in polyMesh/
00114         if (fieldName == "boundary")
00115         {
00116             Info<< "Special handling of " << fieldName
00117                 << " as polyMesh/boundary file." << endl;
00118 
00119             // Read PtrList of dictionary as dictionary.
00120             const word oldTypeName = IOPtrList<entry>::typeName;
00121             const_cast<word&>(IOPtrList<entry>::typeName) = word::null;
00122             IOPtrList<entry> dictList
00123             (
00124                 IOobject
00125                 (
00126                     fieldName,
00127                     runTime.findInstance
00128                     (
00129                         regionPrefix/polyMesh::meshSubDir,
00130                         fieldName
00131                     ),
00132                     polyMesh::meshSubDir,
00133                     mesh,
00134                     IOobject::MUST_READ,
00135                     IOobject::NO_WRITE,
00136                     false
00137                 )
00138             );
00139             const_cast<word&>(IOPtrList<entry>::typeName) = oldTypeName;
00140             // Fake type back to what was in field
00141             const_cast<word&>(dictList.type()) = dictList.headerClassName();
00142 
00143             // Temporary convert to dictionary
00144             dictionary fieldDict;
00145             forAll(dictList, i)
00146             {
00147                 fieldDict.add(dictList[i].keyword(), dictList[i].dict());
00148             }
00149 
00150             Info<< "Loaded dictionary " << fieldName
00151                 << " with entries " << fieldDict.toc() << endl;
00152 
00153             // Get the replacement dictionary for the field
00154             const dictionary& replaceDict = fieldIter().dict();
00155             Info<< "Merging entries from " << replaceDict.toc() << endl;
00156 
00157             // Merge the replacements in
00158             fieldDict.merge(replaceDict);
00159 
00160             Info<< "fieldDict:" << fieldDict << endl;
00161 
00162             // Convert back into dictList
00163             wordList doneKeys(dictList.size());
00164 
00165             label nEntries = fieldDict.size();
00166             forAll(dictList, i)
00167             {
00168                 doneKeys[i] = dictList[i].keyword();
00169                 dictList.set
00170                 (
00171                     i,
00172                     fieldDict.lookupEntry
00173                     (
00174                         doneKeys[i],
00175                         false,
00176                         true
00177                     ).clone()
00178                 );
00179                 fieldDict.remove(doneKeys[i]);
00180             }
00181             // Add remaining entries
00182             label sz = dictList.size();
00183             dictList.setSize(nEntries);
00184             forAllConstIter(dictionary, fieldDict, iter)
00185             {
00186                 dictList.set(sz, iter().clone());
00187             }
00188 
00189             Info<< "Writing modified fieldDict " << fieldName << endl;
00190             dictList.write();
00191         }
00192         else
00193         {
00194             // Read dictionary. (disable class type checking so we can load
00195             // field)
00196             Info<< "Loading dictionary " << fieldName << endl;
00197             const word oldTypeName = IOdictionary::typeName;
00198             const_cast<word&>(IOdictionary::typeName) = word::null;
00199             IOdictionary fieldDict
00200             (
00201                 IOobject
00202                 (
00203                     fieldName,
00204                     runTime.timeName(),
00205                     mesh,
00206                     IOobject::MUST_READ,
00207                     IOobject::NO_WRITE,
00208                     false
00209                 )
00210             );
00211             const_cast<word&>(IOdictionary::typeName) = oldTypeName;
00212             // Fake type back to what was in field
00213             const_cast<word&>(fieldDict.type()) = fieldDict.headerClassName();
00214 
00215             Info<< "Loaded dictionary " << fieldName
00216                 << " with entries " << fieldDict.toc() << endl;
00217 
00218             // Get the replacement dictionary for the field
00219             const dictionary& replaceDict = fieldIter().dict();
00220             Info<< "Merging entries from " << replaceDict.toc() << endl;
00221 
00222             // Merge the replacements in
00223             fieldDict.merge(replaceDict);
00224 
00225             Info<< "Writing modified fieldDict " << fieldName << endl;
00226             fieldDict.regIOobject::write();
00227         }
00228     }
00229 
00230     Info<< endl;
00231 
00232     Info<< "End\n" << endl;
00233 
00234     return 0;
00235 }
00236 
00237 
00238 // ************************************************************************* //
Copyright © 2000-2009 OpenCFD Ltd