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-2021 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 // * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * //
49 
50 bool Foam::functionObjects::ddt2::checkFormatName
51 (
52  const std::string& str
53 )
54 {
55  if (std::string::npos == str.find("@@"))
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 
76 bool Foam::functionObjects::ddt2::accept(const word& fieldName) const
77 {
78  // check input vs possible result names
79  // to avoid circular calculations
80  return !denyField_.match(fieldName);
81 }
82 
83 
84 int Foam::functionObjects::ddt2::process(const word& fieldName)
85 {
86  if (!accept(fieldName))
87  {
88  return -1;
89  }
90 
91  int state = 0;
92 
93  apply<volScalarField>(fieldName, state);
94  apply<volVectorField>(fieldName, state);
95 
96  return state;
97 }
98 
99 
100 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
101 
103 (
104  const word& name,
105  const Time& runTime,
106  const dictionary& dict
107 )
108 :
110  selectFields_(),
111  resultName_(),
112  denyField_(),
113  results_(),
114  mag_(dict.getOrDefault("mag", false))
115 {
116  read(dict);
117 }
118 
119 
120 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
121 
123 {
125 
126  if (word(mesh_.ddtScheme("default")) == "steadyState")
127  {
129  << typeName << " function object not appropriate for steady-state"
130  << endl;
131  return false;
132  }
133 
134  dict.readEntry("fields", selectFields_);
135  selectFields_.uniq();
136 
137  Info<< type() << " fields: " << selectFields_ << nl;
138 
139  resultName_ = dict.getOrDefault<word>
140  (
141  "result",
142  ( mag_ ? "mag(ddt(@@))" : "magSqr(ddt(@@))" )
143  );
144 
145  // Expect '@@' token for result, unless a single (non-regex) source field
146  if
147  (
148  (selectFields_.size() == 1 && selectFields_.first().isLiteral())
149  || checkFormatName(resultName_)
150  )
151  {
152  denyField_.set
153  (
154  stringOps::quotemeta(resultName_, regExp::meta())
155  .replace("@@", "(.+)")
156  );
157 
158  return true;
159  }
160 
161  denyField_.clear();
162  return false;
163 }
164 
165 
167 {
168  results_.clear();
169 
170  wordHashSet candidates(subsetStrings(selectFields_, mesh_.names()));
171  DynamicList<word> missing(selectFields_.size());
172  DynamicList<word> ignored(selectFields_.size());
173 
174  // Check exact matches first
175  for (const wordRe& select : selectFields_)
176  {
177  if (select.isLiteral())
178  {
179  const word& fieldName = select;
180 
181  if (!candidates.erase(fieldName))
182  {
183  missing.append(fieldName);
184  }
185  else if (process(fieldName) < 1)
186  {
187  ignored.append(fieldName);
188  }
189  }
190  }
191 
192  for (const word& fieldName : candidates)
193  {
194  process(fieldName);
195  }
196 
197  if (missing.size())
198  {
200  << "Missing field " << missing << endl;
201  }
202  if (ignored.size())
203  {
205  << "Unprocessed field " << ignored << endl;
206  }
207 
208  return true;
209 }
210 
211 
213 {
214  if (results_.size())
215  {
216  Log << type() << ' ' << name() << " write:" << endl;
217  }
218 
219  // Consistent output order
220  const wordList outputList = results_.sortedToc();
221  for (const word& fieldName : outputList)
222  {
223  if (foundObject<regIOobject>(fieldName))
224  {
225  const regIOobject& io = lookupObject<regIOobject>(fieldName);
226 
227  Log << " " << fieldName << endl;
228 
229  io.write();
230  }
231  }
232 
233  return true;
234 }
235 
236 
237 // ************************************************************************* //
Foam::functionObjects::ddt2::write
virtual bool write()
Write the ddt2 fields.
Definition: ddt2.C:212
volFields.H
runTime
engineTime & runTime
Definition: createEngineTime.H:13
wordRes.H
Log
#define Log
Definition: PDRblock.C:35
steadyStateDdtScheme.H
Foam::Time
Class to control time during OpenFOAM simulations that is also the top-level objectRegistry.
Definition: Time.H:73
Foam::functionObjects::ddt2::read
virtual bool read(const dictionary &)
Read the ddt2 specification.
Definition: ddt2.C:122
Foam::word
A class for handling words, derived from Foam::string.
Definition: word.H:65
Foam::functionObjects::ddt2::execute
virtual bool execute()
Calculate the ddt2 fields.
Definition: ddt2.C:166
Foam::DynamicList< word >
Foam::read
bool read(const char *buf, int32_t &val)
Same as readInt32.
Definition: int32.H:108
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
ddt2.H
Foam::functionObjects::ddt2::ddt2
ddt2(const word &name, const Time &runTime, const dictionary &dict)
Construct from Time and dictionary.
Definition: ddt2.C:103
Foam::regIOobject::write
virtual bool write(const bool valid=true) const
Write using setting from DB.
Definition: regIOobjectWrite.C:132
Foam::stringOps::quotemeta
StringType quotemeta(const StringType &str, const UnaryPredicate &meta, const char quote='\\')
Quote any meta-characters in given string.
Definition: stringOpsTemplates.C:32
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::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::regExpCxx::meta
Functor wrapper for testing meta-characters.
Definition: regExpCxx.H:146
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::List< word >
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
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.
stringOps.H
WarningInFunction
#define WarningInFunction
Report a warning using Foam::Warning.
Definition: messageStream.H:328