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-2024 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 "volFields.H"
30 #include "dictionary.H"
31 #include "wordRes.H"
33 
34 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
35 
36 namespace Foam
37 {
38 namespace functionObjects
39 {
40  defineTypeNameAndDebug(zeroGradient, 0);
41  addToRunTimeSelectionTable(functionObject, zeroGradient, dictionary);
42 }
43 }
44 
45 
46 // * * * * * * * * * * * * * * * Local Functions * * * * * * * * * * * * * * //
47 
48 namespace Foam
49 {
50 
51 // Check that string contains the appropriate substitution token(s)
52 static bool checkFormatName(const word& str)
53 {
54  if (!str.contains("@@"))
55  {
57  << "Bad result naming (no '@@' token found)."
58  << nl << endl;
59 
60  return false;
61  }
62  else if (str == "@@")
63  {
65  << "Bad result naming (only a '@@' token found)."
66  << nl << endl;
67 
68  return false;
69  }
70 
71  return true;
72 }
73 
74 } // End namespace Foam
75 
76 
77 // * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * //
78 
79 int Foam::functionObjects::zeroGradient::process(const word& fieldName)
80 {
81  int state = 0;
82  apply<scalar>(fieldName, state);
83  apply<vector>(fieldName, state);
84  apply<sphericalTensor>(fieldName, state);
85  apply<symmTensor>(fieldName, state);
86  apply<tensor>(fieldName, state);
87 
88  return state;
89 }
90 
91 
92 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
93 
95 (
96  const word& name,
97  const Time& runTime,
98  const dictionary& dict
99 )
100 :
102  selectFields_(),
103  resultName_(),
104  results_()
105 {
106  read(dict);
107 }
108 
109 
110 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
111 
113 {
115 
116  dict.readEntry("fields", selectFields_);
117  selectFields_.uniq();
118 
119  Info<< type() << " fields: " << selectFields_ << nl;
120 
121  resultName_ =
122  dict.getOrDefault<word>("result", scopedName(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(mesh_.names<regIOobject>(selectFields_));
138 
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:521
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
virtual bool execute()
Calculate the zeroGradient fields.
Definition: zeroGradient.C:126
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:88
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:47
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:496
#define WarningInFunction
Report a warning using Foam::Warning.
virtual bool read(const dictionary &dict)
Read the zeroGradient specification.
Definition: zeroGradient.C:105
#define Log
Definition: PDRblock.C:28
regIOobject is an abstract class derived from IOobject to handle automatic object registration with t...
Definition: regIOobject.H:68
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.