ddt2.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 "ddt2.H"
29 #include "stringOps.H"
30 #include "volFields.H"
31 #include "dictionary.H"
32 #include "wordRes.H"
33 #include "steadyStateDdtScheme.H"
35 
36 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
37 
38 namespace Foam
39 {
40 namespace functionObjects
41 {
42  defineTypeNameAndDebug(ddt2, 0);
43  addToRunTimeSelectionTable(functionObject, ddt2, dictionary);
44 }
45 }
46 
47 
48 // * * * * * * * * * * * * * * * Local Functions * * * * * * * * * * * * * * //
49 
50 namespace Foam
51 {
52 
53 // Check that string contains the appropriate substitution token(s)
54 static bool checkFormatName(const word& str)
55 {
56  if (!str.contains("@@"))
57  {
59  << "Bad result naming (no '@@' token found)."
60  << nl << endl;
61 
62  return false;
63  }
64  else if (str == "@@")
65  {
67  << "Bad result naming (only a '@@' token found)."
68  << nl << endl;
69 
70  return false;
71  }
72 
73  return true;
74 }
75 
76 } // End namespace Foam
77 
78 
79 // * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * //
80 
81 bool Foam::functionObjects::ddt2::accept(const word& fieldName) const
82 {
83  // check input vs possible result names
84  // to avoid circular calculations
85  return !denyField_.match(fieldName);
86 }
87 
88 
89 int Foam::functionObjects::ddt2::process(const word& fieldName)
90 {
91  if (!accept(fieldName))
92  {
93  return -1;
94  }
95 
96  int state = 0;
97 
98  apply<volScalarField>(fieldName, state);
99  apply<volVectorField>(fieldName, state);
100 
101  return state;
102 }
103 
104 
105 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
106 
108 (
109  const word& name,
110  const Time& runTime,
111  const dictionary& dict
112 )
113 :
115  selectFields_(),
116  resultName_(),
117  denyField_(),
118  results_(),
119  mag_(dict.getOrDefault("mag", false))
120 {
121  read(dict);
122 }
123 
124 
125 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
126 
128 {
130 
131  if (word(mesh_.ddtScheme("default")) == "steadyState")
132  {
134  << typeName << " function object not appropriate for steady-state"
135  << endl;
136  return false;
137  }
138 
139  dict.readEntry("fields", selectFields_);
140  selectFields_.uniq();
141 
142  Info<< type() << " fields: " << selectFields_ << nl;
143 
144  resultName_ = dict.getOrDefault<word>
145  (
146  "result",
147  ( mag_ ? "mag(ddt(@@))" : "magSqr(ddt(@@))" )
148  );
149 
150  // Expect '@@' token for result, unless a single (non-regex) source field
151  if
152  (
153  (selectFields_.size() == 1 && selectFields_.first().isLiteral())
154  || checkFormatName(resultName_)
155  )
156  {
157  denyField_.set
158  (
159  stringOps::quotemeta(resultName_, regExp::meta())
160  .replace("@@", "(.+)")
161  );
162 
163  return true;
164  }
165 
166  denyField_.clear();
167  return false;
168 }
169 
170 
172 {
173  results_.clear();
174 
175  wordHashSet candidates(mesh_.names<regIOobject>(selectFields_));
176 
177  DynamicList<word> missing(selectFields_.size());
178  DynamicList<word> ignored(selectFields_.size());
179 
180  // Check exact matches first
181  for (const wordRe& select : selectFields_)
182  {
183  if (select.isLiteral())
184  {
185  const word& fieldName = select;
186 
187  if (!candidates.erase(fieldName))
188  {
189  missing.append(fieldName);
190  }
191  else if (process(fieldName) < 1)
192  {
193  ignored.append(fieldName);
194  }
195  }
196  }
197 
198  for (const word& fieldName : candidates)
199  {
200  process(fieldName);
201  }
202 
203  if (missing.size())
204  {
206  << "Missing field " << missing << endl;
207  }
208  if (ignored.size())
209  {
211  << "Unprocessed field " << ignored << endl;
212  }
213 
214  return true;
215 }
216 
217 
219 {
220  if (results_.size())
221  {
222  Log << type() << ' ' << name() << " write:" << endl;
223  }
224 
225  // Consistent output order
226  const wordList outputList = results_.sortedToc();
227  for (const word& fieldName : outputList)
228  {
229  if (foundObject<regIOobject>(fieldName))
230  {
231  const regIOobject& io = lookupObject<regIOobject>(fieldName);
232 
233  Log << " " << fieldName << endl;
234 
235  io.write();
236  }
237  }
238 
239  return true;
240 }
241 
242 
243 // ************************************************************************* //
dictionary dict
defineTypeNameAndDebug(ObukhovLength, 0)
virtual bool execute()
Calculate the ddt2 fields.
Definition: ddt2.C:164
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
ddt2(const word &name, const Time &runTime, const dictionary &dict)
Construct from Time and dictionary.
Definition: ddt2.C:101
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
virtual bool write()
Write the ddt2 fields.
Definition: ddt2.C:211
engineTime & runTime
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:531
Class to control time during OpenFOAM simulations that is also the top-level objectRegistry.
Definition: Time.H:69
virtual bool read(const dictionary &)
Read the ddt2 specification.
Definition: ddt2.C:120
Macros for easy insertion into run-time selection tables.
StringType quotemeta(const StringType &str, const UnaryPredicate &meta, const char quote='\\')
Quote any meta-characters in given string.
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
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
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)
bool match(const std::string &text) const
True if the regex matches the entire text.
Definition: regExpPosix.C:187
Functor wrapper for testing meta-characters.
Definition: regExpCxx.H:164
bool erase(const iterator &iter)
Erase an entry specified by given iterator.
Definition: HashTable.C:496
List< word > wordList
List of word.
Definition: fileName.H:59
#define WarningInFunction
Report a warning using Foam::Warning.
#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...
IOobject io("surfaceFilmProperties", mesh.time().constant(), mesh, IOobject::READ_IF_PRESENT, IOobject::NO_WRITE, IOobject::NO_REGISTER)
Namespace for OpenFOAM.