initial commit for version 1.6.x patch release
[OpenFOAM-1.6.x.git] / applications / utilities / preProcessing / changeDictionary / changeDictionary.C
blob6ff9bbce1881c8fc89abe895574f7fc7ceba445b
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 Application
26     changeDictionary
28 Description
29     Utility to change dictionary entries, e.g. can be used to change the patch
30     type in the field and polyMesh/boundary files.
32     Reads dictionaries (fields) and entries to change from a dictionary.
33     E.g. to make the @em movingWall a @em fixedValue for @em p, the
34     @c system/changeDictionaryDict would contain the following:
35     @verbatim
36     dictionaryReplacement
37     {
38         p                           // field to change
39         {
40             boundaryField
41             {
42                 movingWall          // entry to change
43                 {
44                     type            fixedValue;
45                     value           uniform 123.45;
46                 }
47             }
48         }
49     }
50     @endverbatim
53 \*---------------------------------------------------------------------------*/
55 #include "argList.H"
56 #include "IOobjectList.H"
57 #include "IOPtrList.H"
58 #include "volFields.H"
60 using namespace Foam;
62 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
64 namespace Foam
66     defineTemplateTypeNameAndDebug(IOPtrList<entry>, 0);
70 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
71 // Main program:
73 int main(int argc, char *argv[])
75 #   include "addRegionOption.H"
76 #   include "setRootCase.H"
77 #   include "createTime.H"
78 #   include "createNamedMesh.H"
80     fileName regionPrefix = "";
81     if (regionName != fvMesh::defaultRegion)
82     {
83         regionPrefix = regionName;
84     }
86     // Get the replacement rules from a dictionary
87     IOdictionary dict
88     (
89         IOobject
90         (
91             "changeDictionaryDict",
92             runTime.system(),
93             mesh,
94             IOobject::MUST_READ,
95             IOobject::NO_WRITE
96         )
97     );
98     const dictionary& replaceDicts = dict.subDict("dictionaryReplacement");
99     Info<< "Read dictionary " << dict.name()
100         << " with replacements for dictionaries "
101         << replaceDicts.toc() << endl;
104     // Every replacement is a dictionary name and a keyword in this
106     forAllConstIter(dictionary, replaceDicts, fieldIter)
107     {
108         const word& fieldName = fieldIter().keyword();
109         Info<< "Replacing entries in dictionary " << fieldName << endl;
111         // Handle 'boundary' specially:
112         // - is PtrList of dictionaries
113         // - is in polyMesh/
114         if (fieldName == "boundary")
115         {
116             Info<< "Special handling of " << fieldName
117                 << " as polyMesh/boundary file." << endl;
119             // Read PtrList of dictionary as dictionary.
120             const word oldTypeName = IOPtrList<entry>::typeName;
121             const_cast<word&>(IOPtrList<entry>::typeName) = word::null;
122             IOPtrList<entry> dictList
123             (
124                 IOobject
125                 (
126                     fieldName,
127                     runTime.findInstance
128                     (
129                         regionPrefix/polyMesh::meshSubDir,
130                         fieldName
131                     ),
132                     polyMesh::meshSubDir,
133                     mesh,
134                     IOobject::MUST_READ,
135                     IOobject::NO_WRITE,
136                     false
137                 )
138             );
139             const_cast<word&>(IOPtrList<entry>::typeName) = oldTypeName;
140             // Fake type back to what was in field
141             const_cast<word&>(dictList.type()) = dictList.headerClassName();
143             // Temporary convert to dictionary
144             dictionary fieldDict;
145             forAll(dictList, i)
146             {
147                 fieldDict.add(dictList[i].keyword(), dictList[i].dict());
148             }
150             Info<< "Loaded dictionary " << fieldName
151                 << " with entries " << fieldDict.toc() << endl;
153             // Get the replacement dictionary for the field
154             const dictionary& replaceDict = fieldIter().dict();
155             Info<< "Merging entries from " << replaceDict.toc() << endl;
157             // Merge the replacements in
158             fieldDict.merge(replaceDict);
160             Info<< "fieldDict:" << fieldDict << endl;
162             // Convert back into dictList
163             wordList doneKeys(dictList.size());
165             label nEntries = fieldDict.size();
166             forAll(dictList, i)
167             {
168                 doneKeys[i] = dictList[i].keyword();
169                 dictList.set
170                 (
171                     i,
172                     fieldDict.lookupEntry
173                     (
174                         doneKeys[i],
175                         false,
176                         true
177                     ).clone()
178                 );
179                 fieldDict.remove(doneKeys[i]);
180             }
181             // Add remaining entries
182             label sz = dictList.size();
183             dictList.setSize(nEntries);
184             forAllConstIter(dictionary, fieldDict, iter)
185             {
186                 dictList.set(sz, iter().clone());
187             }
189             Info<< "Writing modified fieldDict " << fieldName << endl;
190             dictList.write();
191         }
192         else
193         {
194             // Read dictionary. (disable class type checking so we can load
195             // field)
196             Info<< "Loading dictionary " << fieldName << endl;
197             const word oldTypeName = IOdictionary::typeName;
198             const_cast<word&>(IOdictionary::typeName) = word::null;
199             IOdictionary fieldDict
200             (
201                 IOobject
202                 (
203                     fieldName,
204                     runTime.timeName(),
205                     mesh,
206                     IOobject::MUST_READ,
207                     IOobject::NO_WRITE,
208                     false
209                 )
210             );
211             const_cast<word&>(IOdictionary::typeName) = oldTypeName;
212             // Fake type back to what was in field
213             const_cast<word&>(fieldDict.type()) = fieldDict.headerClassName();
215             Info<< "Loaded dictionary " << fieldName
216                 << " with entries " << fieldDict.toc() << endl;
218             // Get the replacement dictionary for the field
219             const dictionary& replaceDict = fieldIter().dict();
220             Info<< "Merging entries from " << replaceDict.toc() << endl;
222             // Merge the replacements in
223             fieldDict.merge(replaceDict);
225             Info<< "Writing modified fieldDict " << fieldName << endl;
226             fieldDict.regIOobject::write();
227         }
228     }
230     Info<< endl;
232     Info<< "End\n" << endl;
234     return 0;
238 // ************************************************************************* //