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-2020 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 "stringListOps.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 // * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * //
48 
49 bool Foam::functionObjects::ddt2::checkFormatName
50 (
51  const std::string& str
52 )
53 {
54  if (std::string::npos == str.find("@@"))
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 
75 bool Foam::functionObjects::ddt2::accept(const word& fieldName) const
76 {
77  // check input vs possible result names
78  // to avoid circular calculations
79  return !denyField_.match(fieldName);
80 }
81 
82 
83 int Foam::functionObjects::ddt2::process(const word& fieldName)
84 {
85  if (!accept(fieldName))
86  {
87  return -1;
88  }
89 
90  int state = 0;
91 
92  apply<volScalarField>(fieldName, state);
93  apply<volVectorField>(fieldName, state);
94 
95  return state;
96 }
97 
98 
99 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
100 
102 (
103  const word& name,
104  const Time& runTime,
105  const dictionary& dict
106 )
107 :
109  selectFields_(),
110  resultName_(word::null),
111  denyField_(),
112  results_(),
113  mag_(dict.getOrDefault("mag", false))
114 {
115  read(dict);
116 }
117 
118 
119 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
120 
122 {
124 
125  if (word(mesh_.ddtScheme("default")) == "steadyState")
126  {
128  << typeName << " function object not appropriate for steady-state"
129  << endl;
130  return false;
131  }
132 
133  dict.readEntry("fields", selectFields_);
134  selectFields_.uniq();
135 
136  Info<< type() << " fields: " << selectFields_ << nl;
137 
138  resultName_ = dict.getOrDefault<word>
139  (
140  "result",
141  ( mag_ ? "mag(ddt(@@))" : "magSqr(ddt(@@))" )
142  );
143 
144  // Expect '@@' token for result, unless a single (non-regex) source field
145  if
146  (
147  (selectFields_.size() == 1 && selectFields_.first().isLiteral())
148  || checkFormatName(resultName_)
149  )
150  {
151  denyField_.set
152  (
153  string::quotemeta<regExp>
154  (
155  resultName_
156  ).replace("@@", "(.+)")
157  );
158 
159  return true;
160  }
161 
162  denyField_.clear();
163  return false;
164 }
165 
166 
168 {
169  results_.clear();
170 
171  wordHashSet candidates(subsetStrings(selectFields_, mesh_.names()));
172  DynamicList<word> missing(selectFields_.size());
173  DynamicList<word> ignored(selectFields_.size());
174 
175  // Check exact matches first
176  for (const wordRe& select : selectFields_)
177  {
178  if (select.isLiteral())
179  {
180  const word& fieldName = select;
181 
182  if (!candidates.erase(fieldName))
183  {
184  missing.append(fieldName);
185  }
186  else if (process(fieldName) < 1)
187  {
188  ignored.append(fieldName);
189  }
190  }
191  }
192 
193  for (const word& fieldName : candidates)
194  {
195  process(fieldName);
196  }
197 
198  if (missing.size())
199  {
201  << "Missing field " << missing << endl;
202  }
203  if (ignored.size())
204  {
206  << "Unprocessed field " << ignored << endl;
207  }
208 
209  return true;
210 }
211 
212 
214 {
215  if (results_.size())
216  {
217  Log << type() << ' ' << name() << " write:" << endl;
218  }
219 
220  // Consistent output order
221  const wordList outputList = results_.sortedToc();
222  for (const word& fieldName : outputList)
223  {
224  if (foundObject<regIOobject>(fieldName))
225  {
226  const regIOobject& io = lookupObject<regIOobject>(fieldName);
227 
228  Log << " " << fieldName << endl;
229 
230  io.write();
231  }
232  }
233 
234  return true;
235 }
236 
237 
238 // ************************************************************************* //
Foam::functionObjects::ddt2::write
virtual bool write()
Write the ddt2 fields.
Definition: ddt2.C:213
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:121
Foam::word
A class for handling words, derived from Foam::string.
Definition: word.H:62
Foam::functionObjects::ddt2::execute
virtual bool execute()
Calculate the ddt2 fields.
Definition: ddt2.C:167
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:350
Foam::HashSet< 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:72
ddt2.H
Foam::functionObjects::ddt2::ddt2
ddt2(const word &name, const Time &runTime, const dictionary &dict)
Construct from Time and dictionary.
Definition: ddt2.C:102
Foam::regIOobject::write
virtual bool write(const bool valid=true) const
Write using setting from DB.
Definition: regIOobjectWrite.C:165
Foam::HashTable::erase
bool erase(const iterator &iter)
Erase an entry specified by given iterator.
Definition: HashTable.C:392
Foam::Info
messageStream Info
Information stream (uses stdout - output is on the master only)
Foam::name
word name(const complex &c)
Return string representation of complex.
Definition: complex.C:76
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:167
Foam::dictionary
A list of keyword definitions, which are a keyword followed by a number of values (eg,...
Definition: dictionary.H:121
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::regIOobject
regIOobject is an abstract class derived from IOobject to handle automatic object registration with t...
Definition: regIOobject.H:71
Foam::nl
constexpr char nl
Definition: Ostream.H:385
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::word::null
static const word null
An empty word.
Definition: word.H:77
stringListOps.H
Operations on lists of strings.
WarningInFunction
#define WarningInFunction
Report a warning using Foam::Warning.
Definition: messageStream.H:303