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-2018 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 
44  (
45  functionObject,
46  zeroGradient,
47  dictionary
48  );
49 }
50 }
51 
52 // * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * //
53 
54 bool Foam::functionObjects::zeroGradient::checkFormatName
55 (
56  const std::string& str
57 )
58 {
59  if (std::string::npos == str.find("@@"))
60  {
62  << "Bad result naming (no '@@' token found)."
63  << nl << endl;
64 
65  return false;
66  }
67  else if (str == "@@")
68  {
70  << "Bad result naming (only a '@@' token found)."
71  << nl << endl;
72 
73  return false;
74  }
75 
76  return true;
77 }
78 
79 
80 int Foam::functionObjects::zeroGradient::process(const word& fieldName)
81 {
82  int state = 0;
83  apply<scalar>(fieldName, state);
84  apply<vector>(fieldName, state);
85  apply<sphericalTensor>(fieldName, state);
86  apply<symmTensor>(fieldName, state);
87  apply<tensor>(fieldName, state);
88 
89  return state;
90 }
91 
92 
93 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
94 
95 Foam::functionObjects::zeroGradient::zeroGradient
96 (
97  const word& name,
98  const Time& runTime,
99  const dictionary& dict
100 )
101 :
103  selectFields_(),
104  resultName_(string::null),
105  results_()
106 {
107  read(dict);
108 }
109 
110 
111 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
112 
114 {
116 
117  dict.readEntry("fields", selectFields_);
118  selectFields_.uniq();
119 
120  Info<< type() << " fields: " << selectFields_ << nl;
121 
122  resultName_ = dict.lookupOrDefault<word>("result", type() + "(@@)");
123 
124  // Require '@@' token for result, unless a single (non-regex) source field
125  return
126  (
127  (selectFields_.size() == 1 && selectFields_.first().isLiteral())
128  || checkFormatName(resultName_)
129  );
130 }
131 
132 
134 {
135  results_.clear();
136 
137  wordHashSet candidates(subsetStrings(selectFields_, mesh_.names()));
138  DynamicList<word> missing(selectFields_.size());
139  DynamicList<word> ignored(selectFields_.size());
140 
141  // Check exact matches first
142  for (const wordRe& select : selectFields_)
143  {
144  if (select.isLiteral())
145  {
146  const word& fieldName = select;
147 
148  if (!candidates.erase(fieldName))
149  {
150  missing.append(fieldName);
151  }
152  else if (process(fieldName) < 1)
153  {
154  ignored.append(fieldName);
155  }
156  }
157  }
158 
159  for (const word& fieldName : candidates)
160  {
161  process(fieldName);
162  }
163 
164  if (missing.size())
165  {
167  << "Missing field " << missing << endl;
168  }
169  if (ignored.size())
170  {
172  << "Unprocessed field " << ignored << endl;
173  }
174 
175  return true;
176 }
177 
178 
180 {
181  if (results_.size())
182  {
183  Log << type() << ' ' << name() << " write:" << endl;
184  }
185 
186  // Consistent output order
187  for (const word& fieldName : results_.sortedToc())
188  {
189  const regIOobject* ioptr = findObject<regIOobject>(fieldName);
190 
191  if (ioptr)
192  {
193  Log << " " << fieldName << endl;
194 
195  ioptr->write();
196  }
197  }
198 
199  return true;
200 }
201 
202 
203 // ************************************************************************* //
volFields.H
runTime
engineTime & runTime
Definition: createEngineTime.H:13
wordRes.H
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:113
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:133
Foam::endl
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:337
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
Foam::functionObjects::addToRunTimeSelectionTable
addToRunTimeSelectionTable(functionObject, add, dictionary)
zeroGradient.H
Foam::regIOobject::write
virtual bool write(const bool valid=true) const
Write using setting from DB.
Definition: regIOobjectWrite.C:170
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:166
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:166
addToRunTimeSelectionTable.H
Macros for easy insertion into run-time selection tables.
Foam
Namespace for OpenFOAM.
Definition: atmBoundaryLayer.C:33
Foam::functionObjects::defineTypeNameAndDebug
defineTypeNameAndDebug(fvMeshFunctionObject, 0)
Foam::regIOobject
regIOobject is an abstract class derived from IOobject to handle automatic object registration with t...
Definition: regIOobject.H:67
Foam::nl
constexpr char nl
Definition: Ostream.H:372
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
dictionary.H
stringListOps.H
Operations on lists of strings.
WarningInFunction
#define WarningInFunction
Report a warning using Foam::Warning.
Definition: messageStream.H:294
Foam::functionObjects::zeroGradient::write
virtual bool write()
Write the zeroGradient fields.
Definition: zeroGradient.C:179
Log
#define Log
Report write to Foam::Info if the local log switch is true.
Definition: messageStream.H:332