setExprBoundaryFields.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 | www.openfoam.com
6  \\/ M anipulation |
7 -------------------------------------------------------------------------------
8  Copyright (C) 2019 OpenCFD Ltd.
9 -------------------------------------------------------------------------------
10 License
11  This file is part of OpenFOAM.
12 
13  OpenFOAM is free software: you can redistribute it and/or modify it
14  under the terms of the GNU General Public License as published by
15  the Free Software Foundation, either version 3 of the License, or
16  (at your option) any later version.
17 
18  OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
19  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
20  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
21  for more details.
22 
23  You should have received a copy of the GNU General Public License
24  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
25 
26 Application
27  setExprFields
28 
29 Group
30  grpPreProcessingUtilities
31 
32 Description
33  Set boundary values using an expression
34 
35 Note
36  Based on funkySetBoundaryFields
37  Copyright 2006-2018 Bernhard Gschaider <bgschaid@hfd-research.com>
38 
39 \*---------------------------------------------------------------------------*/
40 
41 #include "argList.H"
42 #include "Time.H"
43 #include "fvMesh.H"
44 #include "pointMesh.H"
45 #include "volFields.H"
46 #include "surfaceFields.H"
47 #include "surfaceFields.H"
48 #include "pointFields.H"
49 #include "patchExprDriver.H"
50 #include "timeSelector.H"
51 
52 using namespace Foam;
53 
54 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
55 // Main program:
56 
57 int main(int argc, char *argv[])
58 {
60 
61  // No -constant, no special treatment for 0/
63 
65  (
66  "dict",
67  "file",
68  "Alternative dictionary for setExprBoundaryFieldsDict"
69  );
70 
72  (
73  "cache-fields",
74  "Cache fields between calls",
75  true // Advanced
76  );
78  (
79  "backup",
80  "Preserve sub-entry as .backup",
81  true // Advanced
82  );
83 
85  (
86  "dry-run",
87  "Evaluate but do not write"
88  );
89 
90  #include "addRegionOption.H"
91  #include "setRootCase.H"
92 
93  const bool dryrun = args.found("dry-run");
94  const bool backup = args.found("backup");
95  const bool cacheFields = args.found("cache-fields");
96 
97  if (cacheFields)
98  {
99  Warning
100  << "The current cache-fields behaviour (caching disk reads) "
101  << "may lead to unexpected behaviour as previous modifications "
102  << "will not be visible."
103  << endl;
104  }
105 
106  const word dictName("setExprBoundaryFieldsDict");
107 
108  #include "createTime.H"
109 
111 
112  if (times.size() < 1)
113  {
115  << "No times selected." << exit(FatalError);
116  }
117 
118  #include "createNamedMesh.H"
119 
120  #include "setSystemMeshDictionaryIO.H"
121  IOdictionary setExprDict(dictIO);
122 
123  forAll(times, timei)
124  {
125  runTime.setTime(times[timei], timei);
126 
127  Info<< "\nTime = " << runTime.timeName() << endl;
128 
129  mesh.readUpdate();
130 
131  for (const entry& dEntry : setExprDict)
132  {
133  if (!dEntry.isDict())
134  {
135  Info<< "Ignoring non-dictionary entry "
136  << dEntry.keyword() << nl;
137  continue;
138  }
139 
140  const dictionary& dict = dEntry.dict();
141 
142  const word fieldName(dict.get<word>("field"));
143 
144  List<dictionary> exprDicts;
145  dict.readEntry("expressions", exprDicts);
146 
147  if (exprDicts.empty())
148  {
149  Info<< "No expressions for " << fieldName << nl;
150  continue;
151  }
152 
153 
154  // Read dictionary
155  // Note: disable class type checking so we can load field
156  const word oldTypeName = IOdictionary::typeName;
157  const_cast<word&>(IOdictionary::typeName) = word::null;
158 
159  IOobject fieldHeader
160  (
161  fieldName,
162  mesh.thisDb().time().timeName(),
163  mesh.thisDb(),
166  false
167  );
168 
169  const bool headOk = fieldHeader.typeHeaderOk<IOdictionary>(false);
170 
171  if (!headOk)
172  {
173  // Restore type
174  const_cast<word&>(IOdictionary::typeName) = oldTypeName;
175 
177  << "Requested field to change " << fieldName
178  << " does not exist in " << fieldHeader.path() << endl;
179  continue;
180  }
181 
182  IOdictionary fieldDict(fieldHeader);
183 
184  // Restore type
185  const_cast<word&>(IOdictionary::typeName) = oldTypeName;
186 
187  // Fake type back to what was in field
188  const_cast<word&>(fieldDict.type()) = fieldDict.headerClassName();
189 
190  Info<< "Processing field " << fieldName << nl;
191 
192  dictionary& boundaryFieldDict = fieldDict.subDict("boundaryField");
193 
194  for (const dictionary& currDict : exprDicts)
195  {
196  const word targetName = currDict.get<word>("target");
197  const word patchName = currDict.get<word>("patch");
198 
199  dictionary& patchDict = boundaryFieldDict.subDict(patchName);
200 
202  (
203  currDict.get<string>("expression"),
204  currDict,
205  true // strip comments
206  );
207 
208  Info<< "Set boundaryField/" << patchName << '/'
209  << targetName << nl
210  << "with expression" << nl
211  << "<<<<" << nl
212  << expr.c_str() << nl
213  << ">>>>" << nl;
214 
215  expressions::patchExprDriver driver(currDict, mesh);
216 
217  // Search on disc
218  driver.setSearchBehaviour(cacheFields, false, true);
219 
220  driver.clearVariables();
221  driver.parse(expr);
222 
223  // Serializing via Field::writeEntry etc
224  OStringStream serialize;
225  driver.result().writeEntry("", serialize);
226 
227  if (backup && !dryrun)
228  {
229  patchDict.changeKeyword
230  (
231  targetName,
232  word(targetName + ".backup"),
233  true // Overwrite
234  );
235  }
236 
237  patchDict.set(targetName, serialize.str().c_str());
238 
239  if (dryrun)
240  {
241  Info<< "Evaluated:" << nl
242  << "<<<<" << nl
243  << serialize.str().c_str() // (already includes nl)
244  << ">>>>" << nl;
245  }
246  }
247 
248  if (!dryrun)
249  {
250  Info<< "Write " << fieldDict.filePath() << nl;
251  fieldDict.regIOobject::write();
252  }
253  }
254  }
255 
256  Info<< "\nEnd\n" << endl;
257 
258  return 0;
259 }
260 
261 
262 // ************************************************************************* //
Foam::entry
A keyword and a list of tokens is an 'entry'.
Definition: entry.H:67
Foam::IOobject::NO_WRITE
Definition: IOobject.H:130
volFields.H
Foam::IOdictionary
IOdictionary is derived from dictionary and IOobject to give the dictionary automatic IO functionalit...
Definition: IOdictionary.H:54
runTime
engineTime & runTime
Definition: createEngineTime.H:13
Foam::IOobject
Defines the attributes of an object for which implicit objectRegistry management is supported,...
Definition: IOobject.H:104
Foam::word
A class for handling words, derived from Foam::string.
Definition: word.H:62
Foam::fvMesh::thisDb
virtual const objectRegistry & thisDb() const
Return the object registry - resolve conflict polyMesh/lduMesh.
Definition: fvMesh.H:260
Foam::IOobject::typeHeaderOk
bool typeHeaderOk(const bool checkType=true, const bool search=true, const bool verbose=true)
Read header (uses typeFilePath to find file) and check its info.
Definition: IOobjectTemplates.C:39
patchExprDriver.H
Foam::Warning
messageStream Warning
Foam::objectRegistry::time
const Time & time() const
Return time.
Definition: objectRegistry.H:186
Foam::Time::timeName
static word timeName(const scalar t, const int precision=precision_)
Definition: Time.C:785
dictName
const word dictName("blockMeshDict")
Foam::endl
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:350
surfaceFields.H
Foam::surfaceFields.
Foam::dictionary::get
T get(const word &keyword, enum keyType::option matchOpt=keyType::REGEX) const
Definition: dictionaryTemplates.C:81
setSystemMeshDictionaryIO.H
forAll
#define forAll(list, i)
Loop across all elements in list.
Definition: stdFoam.H:296
Foam::fvMesh::readUpdate
virtual readUpdateState readUpdate()
Update the mesh based on the mesh files saved in time.
Definition: fvMesh.C:519
Foam::argList::noFunctionObjects
static void noFunctionObjects(bool addWithOption=false)
Remove '-noFunctionObjects' option and ignore any occurrences.
Definition: argList.C:454
Foam::Info
messageStream Info
Information stream (uses stdout - output is on the master only)
argList.H
Foam::dictionary::subDict
const dictionary & subDict(const word &keyword, enum keyType::option matchOpt=keyType::REGEX) const
Find and return a sub-dictionary.
Definition: dictionary.C:528
addRegionOption.H
Foam::dictionary::readEntry
bool readEntry(const word &keyword, T &val, enum keyType::option matchOpt=keyType::REGEX, bool mandatory=true) const
Definition: dictionaryTemplates.C:314
dict
dictionary dict
Definition: searchingEngine.H:14
Foam::FatalError
error FatalError
Foam::dictionary
A list of keyword definitions, which are a keyword followed by a number of values (eg,...
Definition: dictionary.H:121
createNamedMesh.H
mesh
dynamicFvMesh & mesh
Definition: createDynamicFvMesh.H:6
dictIO
IOobject dictIO
Definition: setConstantMeshDictionaryIO.H:1
fvMesh.H
Foam
Namespace for OpenFOAM.
Definition: atmBoundaryLayer.C:33
Foam::expressions::patchExpr::parseDriver
Driver for patch expressions.
Definition: patchExprDriver.H:140
Foam::exit
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:130
Foam::Detail::StringStreamAllocator::str
Foam::string str() const
Get the string - as Foam::string rather than std::string.
Definition: StringStream.H:91
Foam::argList::addBoolOption
static void addBoolOption(const word &optName, const string &usage="", bool advanced=false)
Add a bool option to validOptions with usage information.
Definition: argList.C:325
Time.H
setRootCase.H
FatalErrorInFunction
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:372
Foam::nl
constexpr char nl
Definition: Ostream.H:385
Foam::timeSelector::addOptions
static void addOptions(const bool constant=true, const bool withZero=false)
Add timeSelector options to argList::validOptions.
Definition: timeSelector.C:108
Foam::List< instant >
Foam::OStringStream
Output to string buffer, using a OSstream.
Definition: StringStream.H:196
Foam::Time::setTime
virtual void setTime(const Time &t)
Reset the time and time-index to those of the given time.
Definition: Time.C:1006
Foam::expressions::exprString
Definition: exprString.H:60
Foam::word::null
static const word null
An empty word.
Definition: word.H:77
timeSelector.H
createTime.H
Foam::IOobject::MUST_READ_IF_MODIFIED
Definition: IOobject.H:121
Foam::Ostream::writeEntry
Ostream & writeEntry(const keyType &key, const T &value)
Write a keyword/value entry.
Definition: Ostream.H:232
Foam::timeSelector::select0
static instantList select0(Time &runTime, const argList &args)
Definition: timeSelector.C:241
Foam::argList::addOption
static void addOption(const word &optName, const string &param="", const string &usage="", bool advanced=false)
Add an option to validOptions with usage information.
Definition: argList.C:336
args
Foam::argList args(argc, argv)
WarningInFunction
#define WarningInFunction
Report a warning using Foam::Warning.
Definition: messageStream.H:298
pointFields.H
pointMesh.H
Foam::argList::found
bool found(const word &optName) const
Return true if the named option is found.
Definition: argListI.H:157