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-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 "ddt2.H"
29 #include "stringOps.H"
30 #include "stringListOps.H"
31 #include "volFields.H"
32 #include "dictionary.H"
33 #include "wordRes.H"
34 #include "steadyStateDdtScheme.H"
36 
37 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
38 
39 namespace Foam
40 {
41 namespace functionObjects
42 {
43  defineTypeNameAndDebug(ddt2, 0);
44  addToRunTimeSelectionTable(functionObject, ddt2, dictionary);
45 }
46 }
47 
48 
49 // * * * * * * * * * * * * * * * Local Functions * * * * * * * * * * * * * * //
50 
51 namespace Foam
52 {
53 
54 // Check that string contains the appropriate substitution token(s)
55 static bool checkFormatName(const word& str)
56 {
57  if (!str.contains("@@"))
58  {
60  << "Bad result naming (no '@@' token found)."
61  << nl << endl;
62 
63  return false;
64  }
65  else if (str == "@@")
66  {
68  << "Bad result naming (only a '@@' token found)."
69  << nl << endl;
70 
71  return false;
72  }
73 
74  return true;
75 }
76 
77 } // End namespace Foam
78 
79 
80 // * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * //
81 
82 bool Foam::functionObjects::ddt2::accept(const word& fieldName) const
83 {
84  // check input vs possible result names
85  // to avoid circular calculations
86  return !denyField_.match(fieldName);
87 }
88 
89 
90 int Foam::functionObjects::ddt2::process(const word& fieldName)
91 {
92  if (!accept(fieldName))
93  {
94  return -1;
95  }
96 
97  int state = 0;
98 
99  apply<volScalarField>(fieldName, state);
100  apply<volVectorField>(fieldName, state);
101 
102  return state;
103 }
104 
105 
106 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
107 
109 (
110  const word& name,
111  const Time& runTime,
112  const dictionary& dict
113 )
114 :
116  selectFields_(),
117  resultName_(),
118  denyField_(),
119  results_(),
120  mag_(dict.getOrDefault("mag", false))
121 {
122  read(dict);
123 }
124 
125 
126 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
127 
129 {
131 
132  if (word(mesh_.ddtScheme("default")) == "steadyState")
133  {
135  << typeName << " function object not appropriate for steady-state"
136  << endl;
137  return false;
138  }
139 
140  dict.readEntry("fields", selectFields_);
141  selectFields_.uniq();
142 
143  Info<< type() << " fields: " << selectFields_ << nl;
144 
145  resultName_ = dict.getOrDefault<word>
146  (
147  "result",
148  ( mag_ ? "mag(ddt(@@))" : "magSqr(ddt(@@))" )
149  );
150 
151  // Expect '@@' token for result, unless a single (non-regex) source field
152  if
153  (
154  (selectFields_.size() == 1 && selectFields_.first().isLiteral())
155  || checkFormatName(resultName_)
156  )
157  {
158  denyField_.set
159  (
160  stringOps::quotemeta(resultName_, regExp::meta())
161  .replace("@@", "(.+)")
162  );
163 
164  return true;
165  }
166 
167  denyField_.clear();
168  return false;
169 }
170 
171 
173 {
174  results_.clear();
175 
176  wordHashSet candidates(subsetStrings(selectFields_, mesh_.names()));
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:165
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
ddt2(const word &name, const Time &runTime, const dictionary &dict)
Construct from Time and dictionary.
Definition: ddt2.C:102
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
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.
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:121
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:48
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:472
List< word > wordList
List of word.
Definition: fileName.H:59
#define WarningInFunction
Report a warning using Foam::Warning.
#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...
IOobject io("surfaceFilmProperties", mesh.time().constant(), mesh, IOobject::READ_IF_PRESENT, IOobject::NO_WRITE, IOobject::NO_REGISTER)
Namespace for OpenFOAM.