zeroGradient.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) 2016-2020 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 \*---------------------------------------------------------------------------*/
27 
28 #include "zeroGradient.H"
29 #include "stringListOps.H"
30 #include "volFields.H"
31 #include "dictionary.H"
32 #include "wordRes.H"
34 
35 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
36 
37 namespace Foam
38 {
39 namespace functionObjects
40 {
41  defineTypeNameAndDebug(zeroGradient, 0);
42  addToRunTimeSelectionTable(functionObject, zeroGradient, dictionary);
43 }
44 }
45 
46 // * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * //
47 
48 bool Foam::functionObjects::zeroGradient::checkFormatName
49 (
50  const std::string& str
51 )
52 {
53  if (std::string::npos == str.find("@@"))
54  {
56  << "Bad result naming (no '@@' token found)."
57  << nl << endl;
58 
59  return false;
60  }
61  else if (str == "@@")
62  {
64  << "Bad result naming (only a '@@' token found)."
65  << nl << endl;
66 
67  return false;
68  }
69 
70  return true;
71 }
72 
73 
74 int Foam::functionObjects::zeroGradient::process(const word& fieldName)
75 {
76  int state = 0;
77  apply<scalar>(fieldName, state);
78  apply<vector>(fieldName, state);
79  apply<sphericalTensor>(fieldName, state);
80  apply<symmTensor>(fieldName, state);
81  apply<tensor>(fieldName, state);
82 
83  return state;
84 }
85 
86 
87 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
88 
90 (
91  const word& name,
92  const Time& runTime,
93  const dictionary& dict
94 )
95 :
97  selectFields_(),
98  resultName_(string::null),
99  results_()
100 {
101  read(dict);
102 }
103 
104 
105 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
106 
108 {
110 
111  dict.readEntry("fields", selectFields_);
112  selectFields_.uniq();
113 
114  Info<< type() << " fields: " << selectFields_ << nl;
115 
116  resultName_ = dict.getOrDefault<word>("result", type() + "(@@)");
117 
118  // Require '@@' token for result, unless a single (non-regex) source field
119  return
120  (
121  (selectFields_.size() == 1 && selectFields_.first().isLiteral())
122  || checkFormatName(resultName_)
123  );
124 }
125 
126 
128 {
129  results_.clear();
130 
131  wordHashSet candidates(subsetStrings(selectFields_, mesh_.names()));
132  DynamicList<word> missing(selectFields_.size());
133  DynamicList<word> ignored(selectFields_.size());
134 
135  // Check exact matches first
136  for (const wordRe& select : selectFields_)
137  {
138  if (select.isLiteral())
139  {
140  const word& fieldName = select;
141 
142  if (!candidates.erase(fieldName))
143  {
144  missing.append(fieldName);
145  }
146  else if (process(fieldName) < 1)
147  {
148  ignored.append(fieldName);
149  }
150  }
151  }
152 
153  for (const word& fieldName : candidates)
154  {
155  process(fieldName);
156  }
157 
158  if (missing.size())
159  {
161  << "Missing field " << missing << endl;
162  }
163  if (ignored.size())
164  {
166  << "Unprocessed field " << ignored << endl;
167  }
168 
169  return true;
170 }
171 
172 
174 {
175  if (results_.size())
176  {
177  Log << type() << ' ' << name() << " write:" << endl;
178  }
179 
180  // Consistent output order
181  for (const word& fieldName : results_.sortedToc())
182  {
183  const regIOobject* ioptr = findObject<regIOobject>(fieldName);
184 
185  if (ioptr)
186  {
187  Log << " " << fieldName << endl;
188 
189  ioptr->write();
190  }
191  }
192 
193  return true;
194 }
195 
196 
197 // ************************************************************************* //
volFields.H
runTime
engineTime & runTime
Definition: createEngineTime.H:13
wordRes.H
Log
#define Log
Definition: PDRblock.C:35
Foam::Time
Class to control time during OpenFOAM simulations that is also the top-level objectRegistry.
Definition: Time.H:73
Foam::word
A class for handling words, derived from Foam::string.
Definition: word.H:62
Foam::DynamicList< word >
Foam::functionObjects::zeroGradient::read
virtual bool read(const dictionary &dict)
Read the zeroGradient specification.
Definition: zeroGradient.C:107
Foam::read
bool read(const char *buf, int32_t &val)
Same as readInt32.
Definition: int32.H:108
Foam::functionObjects::zeroGradient::execute
virtual bool execute()
Calculate the zeroGradient fields.
Definition: zeroGradient.C:127
Foam::endl
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:350
Foam::HashSet< word >
Foam::functionObjects::fvMeshFunctionObject
Specialization of Foam::functionObject for an Foam::fvMesh, providing a reference to the Foam::fvMesh...
Definition: fvMeshFunctionObject.H:64
Foam::wordRe
A wordRe is a Foam::word, but can contain a regular expression for matching words or strings.
Definition: wordRe.H:72
zeroGradient.H
Foam::regIOobject::write
virtual bool write(const bool valid=true) const
Write using setting from DB.
Definition: regIOobjectWrite.C:165
Foam::HashTable::erase
bool erase(const iterator &iter)
Erase an entry specified by given iterator.
Definition: HashTable.C:392
Foam::Info
messageStream Info
Information stream (uses stdout - output is on the master only)
Foam::name
word name(const complex &c)
Return string representation of complex.
Definition: complex.C:76
dict
dictionary dict
Definition: searchingEngine.H:14
Foam::subsetStrings
StringListType subsetStrings(const regExp &matcher, const StringListType &input, const bool invert=false)
Extract elements of StringList when regular expression matches.
Definition: stringListOps.H:167
Foam::dictionary
A list of keyword definitions, which are a keyword followed by a number of values (eg,...
Definition: dictionary.H:121
Foam::string::null
static const string null
An empty string.
Definition: string.H:147
Foam::functionObjects::regionFunctionObject::read
virtual bool read(const dictionary &dict)
Read optional controls.
Definition: regionFunctionObject.C:173
addToRunTimeSelectionTable.H
Macros for easy insertion into run-time selection tables.
Foam
Namespace for OpenFOAM.
Definition: atmBoundaryLayer.C:33
Foam::regIOobject
regIOobject is an abstract class derived from IOobject to handle automatic object registration with t...
Definition: regIOobject.H:71
Foam::nl
constexpr char nl
Definition: Ostream.H:385
Foam::functionObjects::addToRunTimeSelectionTable
addToRunTimeSelectionTable(functionObject, ObukhovLength, dictionary)
Foam::type
fileName::Type type(const fileName &name, const bool followLink=true)
Return the file type: DIRECTORY or FILE, normally following symbolic links.
Definition: MSwindows.C:590
Foam::functionObjects::zeroGradient::zeroGradient
zeroGradient(const word &name, const Time &runTime, const dictionary &dict)
Construct from Time and dictionary.
Definition: zeroGradient.C:90
dictionary.H
Foam::functionObjects::defineTypeNameAndDebug
defineTypeNameAndDebug(ObukhovLength, 0)
stringListOps.H
Operations on lists of strings.
WarningInFunction
#define WarningInFunction
Report a warning using Foam::Warning.
Definition: messageStream.H:303
Foam::functionObjects::zeroGradient::write
virtual bool write()
Write the zeroGradient fields.
Definition: zeroGradient.C:173