Test-fvSolutionCombine.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) 2011-2013 OpenFOAM Foundation
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 Application
25  fvSolutionCombine
26 
27 Description
28  Simple utility for combining fvSolution solution entries.
29 
30 \*---------------------------------------------------------------------------*/
31 
32 #include "argList.H"
33 #include "Time.H"
34 #include "wordRe.H"
35 #include "OSspecific.H"
36 
37 using namespace Foam;
38 
39 // check for identical dictionary content, regardless of the order
40 bool checkDictionaryContent(const dictionary& dict1, const dictionary& dict2)
41 {
42  // trivial cases first
43  if (&dict1 == &dict2)
44  {
45  return true;
46  }
47  else if (dict1.size() != dict2.size())
48  {
49  return false;
50  }
51 
52 
53  forAllConstIter(dictionary, dict1, iter1)
54  {
55  const entry* entryPtr = dict2.lookupEntryPtr
56  (
57  iter1().keyword(),
58  false,
59  false
60  );
61 
62  if (!entryPtr)
63  {
64  return false;
65  }
66 
67  const entry& entry1 = iter1();
68  const entry& entry2 = *entryPtr;
69 
70  bool ok = false;
71  if (entry1.isDict())
72  {
73  if (entry2.isDict())
74  {
75  ok = checkDictionaryContent(entry1.dict(), entry2.dict());
76  }
77  }
78  else
79  {
80  ok = (entry1 == entry2);
81  }
82 
83 
84  if (!ok)
85  {
86  return false;
87  }
88  }
89 
90  return true;
91 }
92 
93 
94 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
95 // Main program:
96 
97 int main(int argc, char *argv[])
98 {
100  argList::addBoolOption("rewrite");
101  argList::addBoolOption("show");
102 
103  argList args(argc, argv);
104 
105  Time runTime(args.rootPath(), args.caseName());
106 
107  const word dictName("fvSolution");
108 
109  bool optRewrite = args.optionFound("rewrite");
110  bool optShow = args.optionFound("show");
111 
113  (
114  IOobject
115  (
116  dictName,
117  runTime.system(),
118  runTime,
121  false
122  )
123  );
124 
125  if (!solutionDict.found("solvers"))
126  {
127  Info<<"no solvers entry found in : " << dictName << endl;
128  return 2;
129  }
130 
131  if (optRewrite && solutionDict.instance() != runTime.system())
132  {
133  Info<<"instance is not " << runTime.system()
134  << "- disabling rewrite for this file" << nl;
135  optRewrite = false;
136  }
137 
138  dictionary& solverDict = solutionDict.subDict("solvers");
139 
140  wordList names = solverDict.toc();
141  wordList oldNames = names;
142 
143  bool changed = false;
144  for (label orig = 0; orig < names.size()-1; ++orig)
145  {
146  // skip patterns or entries that have already been done
147  if (names[orig].empty() || wordRe::isPattern(names[orig]))
148  {
149  continue;
150  }
151 
152  const dictionary& dict1 = solverDict.subDict(names[orig]);
153 
154  for (label check = orig+1; check < names.size(); ++check)
155  {
156  // skip patterns or entries that have already been done
157  if (names[check].empty() || wordRe::isPattern(names[check]))
158  {
159  continue;
160  }
161 
162  const dictionary& dict2 = solverDict.subDict(names[check]);
163 
164  // check for identical content
165  if (checkDictionaryContent(dict1, dict2))
166  {
167  names[orig] += "|" + names[check];
168  names[check].clear();
169  changed = true;
170  }
171  }
172  }
173 
174  if (changed)
175  {
176  forAll(names, nameI)
177  {
178  if (names[nameI].empty())
179  {
180  solverDict.remove(oldNames[nameI]);
181  Info<<" #remove " << oldNames[nameI];
182  }
183  else
184  {
185  Info<< " " << oldNames[nameI];
186 
187  if (names[nameI] != oldNames[nameI])
188  {
189  // make "(abc|def)" pattern
190  keyType renamed( "(" + names[nameI] + ")", true);
191 
192  solverDict.changeKeyword(oldNames[nameI], renamed);
193 
194  Info<< " -> " << renamed;
195  }
196  }
197  Info<< endl;
198  }
199 
200  if (optRewrite)
201  {
202  mvBak(solutionDict.objectPath(), "orig");
203  Info<< "Backup to .orig" << nl
204  << "Writing " << solutionDict.objectPath() << nl << endl;
205 
206  solutionDict.regIOobject::write();
207  }
208  else if (optShow)
209  {
211  solutionDict.dictionary::write(Info, false);
212  }
213  else
214  {
215  Info<< "\nFile not rewritten" << endl;
216  }
217  }
218  else
219  {
220  Info<< "no changes" << endl;
221  }
222 
223  Info<< "Done\n" << endl;
224 
225  return changed ? 0 : 1;
226 }
227 
228 
229 // ************************************************************************* //
Foam::entry
A keyword and a list of tokens is an 'entry'.
Definition: entry.H:65
Foam::IOdictionary
IOdictionary is derived from dictionary and IOobject to give the dictionary automatic IO functionalit...
Definition: IOdictionary.H:53
Foam::IOobject
IOobject defines the attributes of an object for which implicit objectRegistry management is supporte...
Definition: IOobject.H:91
OSspecific.H
Functions used by OpenFOAM that are specific to POSIX compliant operating systems and need to be repl...
Foam::Time
Class to control time during OpenFOAM simulations that is also the top-level objectRegistry.
Definition: Time.H:68
Foam::word
A class for handling words, derived from string.
Definition: word.H:59
forAll
#define forAll(list, i)
Loop across all elements in list.
Definition: UList.H:406
Foam::argList::addBoolOption
static void addBoolOption(const word &opt, const string &usage="")
Add to a bool option to validOptions with usage information.
Definition: argList.C:98
Foam::dictionary::remove
bool remove(const word &)
Remove an entry specified by keyword.
Definition: dictionary.C:881
Foam::argList
Extract command arguments and options from the supplied argc and argv parameters.
Definition: argList.H:97
Foam::endl
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:251
Foam::IOobject::instance
const fileName & instance() const
Definition: IOobject.H:350
Foam::argList::rootPath
const fileName & rootPath() const
Return root path.
Definition: argListI.H:36
Foam::IOobject::NO_WRITE
@ NO_WRITE
Definition: IOobject.H:118
forAllConstIter
forAllConstIter(PtrDictionary< phaseModel >, mixture.phases(), phase)
Definition: pEqn.H:39
Foam::IOobject::objectPath
fileName objectPath() const
Return complete path + object name.
Definition: IOobject.H:376
Foam::IOobject::MUST_READ_IF_MODIFIED
@ MUST_READ_IF_MODIFIED
Definition: IOobject.H:109
Foam::dictionary::changeKeyword
bool changeKeyword(const keyType &oldKeyword, const keyType &newKeyword, bool forceOverwrite=false)
Change the keyword for an entry,.
Definition: dictionary.C:914
dictName
const word dictName("particleTrackDict")
Foam::keyType
A class for handling keywords in dictionaries.
Definition: keyType.H:56
Foam::label
intWM_LABEL_SIZE_t label
A label is an int32_t or int64_t as specified by the pre-processor macro WM_LABEL_SIZE.
Definition: label.H:59
Foam::dictionary::found
bool found(const word &, bool recursive=false, bool patternMatch=true) const
Search dictionary for given keyword.
Definition: dictionary.C:304
Foam::entry::isDict
virtual bool isDict() const
Return true if this entry is a dictionary.
Definition: entry.H:153
Foam::TimePaths::system
const word & system() const
Return system name.
Definition: TimePaths.H:120
Foam::nl
static const char nl
Definition: Ostream.H:260
Foam::Info
messageStream Info
wordRe.H
argList.H
Foam::mvBak
bool mvBak(const fileName &, const std::string &ext="bak")
Rename to a corresponding backup file.
Definition: POSIX.C:917
checkDictionaryContent
bool checkDictionaryContent(const dictionary &dict1, const dictionary &dict2)
Definition: Test-fvSolutionCombine.C:37
Foam::dictionary
A list of keyword definitions, which are a keyword followed by any number of values (e....
Definition: dictionary.H:137
Foam
Namespace for OpenFOAM.
Definition: combustionModel.C:30
Foam::entry::dict
virtual const dictionary & dict() const =0
Return dictionary if this entry is a dictionary.
Foam::IOobject::writeDivider
static Stream & writeDivider(Stream &os)
Write the standard file section divider.
Definition: IOobjectI.H:108
Foam::dictionary::lookupEntryPtr
const entry * lookupEntryPtr(const word &, bool recursive, bool patternMatch) const
Find and return an entry data stream pointer if present.
Definition: dictionary.C:343
main
int main(int argc, char *argv[])
Definition: Test-fvSolutionCombine.C:94
Foam::List
A 1D array of objects of type <T>, where the size of the vector is known and used for subscript bound...
Definition: HashTable.H:59
Foam::List::clear
void clear()
Clear the list, i.e. set size to zero.
Definition: List.C:379
Foam::argList::optionFound
bool optionFound(const word &opt) const
Return true if the named option is found.
Definition: argListI.H:108
Foam::argList::caseName
const fileName & caseName() const
Return case name (parallel run) or global case (serial run)
Definition: argListI.H:42
solutionDict
fvSolution solutionDict(runTime)
Foam::dictionary::toc
wordList toc() const
Return the table of contents.
Definition: dictionary.C:697
Foam::List::size
void size(const label)
Override size to be inconsistent with allocated storage.
Foam::argList::noParallel
static void noParallel()
Remove the parallel options.
Definition: argList.C:161
Foam::dictionary::subDict
const dictionary & subDict(const word &) const
Find and return a sub-dictionary.
Definition: dictionary.C:631
args
Foam::argList args(argc, argv)
Foam::wordRe::isPattern
bool isPattern() const
Should be treated as a match rather than a literal string?
Definition: wordReI.H:121