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_ =
117  dict.getOrDefault<word>("result", scopedName(type() + "(@@)"));
118 
119  // Require '@@' token for result, unless a single (non-regex) source field
120  return
121  (
122  (selectFields_.size() == 1 && selectFields_.first().isLiteral())
123  || checkFormatName(resultName_)
124  );
125 }
126 
127 
129 {
130  results_.clear();
131 
132  wordHashSet candidates(subsetStrings(selectFields_, mesh_.names()));
133  DynamicList<word> missing(selectFields_.size());
134  DynamicList<word> ignored(selectFields_.size());
135 
136  // Check exact matches first
137  for (const wordRe& select : selectFields_)
138  {
139  if (select.isLiteral())
140  {
141  const word& fieldName = select;
142 
143  if (!candidates.erase(fieldName))
144  {
145  missing.append(fieldName);
146  }
147  else if (process(fieldName) < 1)
148  {
149  ignored.append(fieldName);
150  }
151  }
152  }
153 
154  for (const word& fieldName : candidates)
155  {
156  process(fieldName);
157  }
158 
159  if (missing.size())
160  {
162  << "Missing field " << missing << endl;
163  }
164  if (ignored.size())
165  {
167  << "Unprocessed field " << ignored << endl;
168  }
169 
170  return true;
171 }
172 
173 
175 {
176  if (results_.size())
177  {
178  Log << type() << ' ' << name() << " write:" << endl;
179  }
180 
181  // Consistent output order
182  for (const word& fieldName : results_.sortedToc())
183  {
184  const regIOobject* ioptr = findObject<regIOobject>(fieldName);
185 
186  if (ioptr)
187  {
188  Log << " " << fieldName << endl;
189 
190  ioptr->write();
191  }
192  }
193 
194  return true;
195 }
196 
197 
198 // ************************************************************************* //
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:65
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:128
Foam::endl
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:369
Foam::HashSet< word, Hash< 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:80
zeroGradient.H
Foam::regIOobject::write
virtual bool write(const bool valid=true) const
Write using setting from DB.
Definition: regIOobjectWrite.C:132
Foam::Info
messageStream Info
Information stream (stdout output on master, null elsewhere)
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:177
Foam::dictionary
A list of keyword definitions, which are a keyword followed by a number of values (eg,...
Definition: dictionary.H:123
Foam::string::null
static const string null
An empty string.
Definition: string.H:169
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:73
Foam::nl
constexpr char nl
Definition: Ostream.H:404
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)
Foam::name
word name(const expressions::valueTypeCode typeCode)
A word representation of a valueTypeCode. Empty for INVALID.
Definition: exprTraits.C:59
stringListOps.H
Operations on lists of strings.
WarningInFunction
#define WarningInFunction
Report a warning using Foam::Warning.
Definition: messageStream.H:328
Foam::functionObjects::zeroGradient::write
virtual bool write()
Write the zeroGradient fields.
Definition: zeroGradient.C:174