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-2022 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 
47 // * * * * * * * * * * * * * * * Local Functions * * * * * * * * * * * * * * //
48 
49 namespace Foam
50 {
51 
52 // Check that string contains the appropriate substitution token(s)
53 static bool checkFormatName(const word& str)
54 {
55  if (!str.contains("@@"))
56  {
58  << "Bad result naming (no '@@' token found)."
59  << nl << endl;
60 
61  return false;
62  }
63  else if (str == "@@")
64  {
66  << "Bad result naming (only a '@@' token found)."
67  << nl << endl;
68 
69  return false;
70  }
71 
72  return true;
73 }
74 
75 } // End namespace Foam
76 
77 
78 // * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * //
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 
96 (
97  const word& name,
98  const Time& runTime,
99  const dictionary& dict
100 )
101 :
103  selectFields_(),
104  resultName_(),
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_ =
123  dict.getOrDefault<word>("result", scopedName(type() + "(@@)"));
124 
125  // Require '@@' token for result, unless a single (non-regex) source field
126  return
127  (
128  (selectFields_.size() == 1 && selectFields_.first().isLiteral())
129  || checkFormatName(resultName_)
130  );
131 }
132 
133 
135 {
136  results_.clear();
137 
138  wordHashSet candidates(subsetStrings(selectFields_, mesh_.names()));
139  DynamicList<word> missing(selectFields_.size());
140  DynamicList<word> ignored(selectFields_.size());
141 
142  // Check exact matches first
143  for (const wordRe& select : selectFields_)
144  {
145  if (select.isLiteral())
146  {
147  const word& fieldName = select;
148 
149  if (!candidates.erase(fieldName))
150  {
151  missing.append(fieldName);
152  }
153  else if (process(fieldName) < 1)
154  {
155  ignored.append(fieldName);
156  }
157  }
158  }
159 
160  for (const word& fieldName : candidates)
161  {
162  process(fieldName);
163  }
164 
165  if (missing.size())
166  {
168  << "Missing field " << missing << endl;
169  }
170  if (ignored.size())
171  {
173  << "Unprocessed field " << ignored << endl;
174  }
175 
176  return true;
177 }
178 
179 
181 {
182  if (results_.size())
183  {
184  Log << type() << ' ' << name() << " write:" << endl;
185  }
186 
187  // Consistent output order
188  for (const word& fieldName : results_.sortedToc())
189  {
190  const regIOobject* ioptr = findObject<regIOobject>(fieldName);
191 
192  if (ioptr)
193  {
194  Log << " " << fieldName << endl;
195 
196  ioptr->write();
197  }
198  }
199 
200  return true;
201 }
202 
203 
204 // ************************************************************************* //
dictionary dict
defineTypeNameAndDebug(ObukhovLength, 0)
bool contains(char c) const noexcept
True if string contains given character (cf. C++23)
Definition: string.H:411
A list of keyword definitions, which are a keyword followed by a number of values (eg...
Definition: dictionary.H:129
void append(const T &val)
Append an element at the end of the list.
Definition: List.H:517
List< bool > select(const label n, const labelUList &locations)
Construct a selection list of bools (all false) with the given pre-size, subsequently add specified l...
Definition: BitOps.C:134
constexpr char nl
The newline &#39;\n&#39; character (0x0a)
Definition: Ostream.H:50
engineTime & runTime
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:531
StringListType subsetStrings(const regExp &matcher, const StringListType &input, const bool invert=false)
Extract elements of StringList when regular expression matches.
Operations on lists of strings.
virtual bool execute()
Calculate the zeroGradient fields.
Definition: zeroGradient.C:127
Class to control time during OpenFOAM simulations that is also the top-level objectRegistry.
Definition: Time.H:69
Macros for easy insertion into run-time selection tables.
bool read(const char *buf, int32_t &val)
Same as readInt32.
Definition: int32.H:127
fileName::Type type(const fileName &name, const bool followLink=true)
Return the file type: DIRECTORY or FILE, normally following symbolic links.
Definition: POSIX.C:799
zeroGradient(const word &name, const Time &runTime, const dictionary &dict)
Construct from Time and dictionary.
Definition: zeroGradient.C:89
word name(const expressions::valueTypeCode typeCode)
A word representation of a valueTypeCode. Empty for expressions::valueTypeCode::INVALID.
Definition: exprTraits.C:127
A class for handling words, derived from Foam::string.
Definition: word.H:63
virtual bool write(const token &tok)=0
Write token to stream or otherwise handle it.
A wordRe is a Foam::word, but can contain a regular expression for matching words or strings...
Definition: wordRe.H:78
static bool checkFormatName(const word &str)
Definition: ddt2.C:48
addToRunTimeSelectionTable(functionObject, ObukhovLength, dictionary)
virtual bool write()
Write the zeroGradient fields.
Definition: zeroGradient.C:173
bool erase(const iterator &iter)
Erase an entry specified by given iterator.
Definition: HashTable.C:472
#define WarningInFunction
Report a warning using Foam::Warning.
virtual bool read(const dictionary &dict)
Read the zeroGradient specification.
Definition: zeroGradient.C:106
#define Log
Definition: PDRblock.C:28
messageStream Info
Information stream (stdout output on master, null elsewhere)
virtual bool read(const dictionary &dict)
Read optional controls.
Specialization of Foam::functionObject for an Foam::fvMesh, providing a reference to the Foam::fvMesh...
Namespace for OpenFOAM.