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 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 
45  (
46  functionObject,
47  ddt2,
48  dictionary
49  );
50 }
51 }
52 
53 // * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * //
54 
55 bool Foam::functionObjects::ddt2::checkFormatName
56 (
57  const std::string& str
58 )
59 {
60  if (std::string::npos == str.find("@@"))
61  {
63  << "Bad result naming (no '@@' token found)."
64  << nl << endl;
65 
66  return false;
67  }
68  else if (str == "@@")
69  {
71  << "Bad result naming (only a '@@' token found)."
72  << nl << endl;
73 
74  return false;
75  }
76 
77  return true;
78 }
79 
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 !blacklist_.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 
107 Foam::functionObjects::ddt2::ddt2
108 (
109  const word& name,
110  const Time& runTime,
111  const dictionary& dict
112 )
113 :
115  selectFields_(),
116  resultName_(word::null),
117  blacklist_(),
118  results_(),
119  mag_(dict.lookupOrDefault("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.lookupOrDefault<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  blacklist_.set
158  (
159  string::quotemeta<regExp>
160  (
161  resultName_
162  ).replace("@@", "(.+)")
163  );
164 
165  return true;
166  }
167 
168  blacklist_.clear();
169  return false;
170 }
171 
172 
174 {
175  results_.clear();
176 
177  wordHashSet candidates(subsetStrings(selectFields_, mesh_.names()));
178  DynamicList<word> missing(selectFields_.size());
179  DynamicList<word> ignored(selectFields_.size());
180 
181  // Check exact matches first
182  for (const wordRe& select : selectFields_)
183  {
184  if (select.isLiteral())
185  {
186  const word& fieldName = select;
187 
188  if (!candidates.erase(fieldName))
189  {
190  missing.append(fieldName);
191  }
192  else if (process(fieldName) < 1)
193  {
194  ignored.append(fieldName);
195  }
196  }
197  }
198 
199  for (const word& fieldName : candidates)
200  {
201  process(fieldName);
202  }
203 
204  if (missing.size())
205  {
207  << "Missing field " << missing << endl;
208  }
209  if (ignored.size())
210  {
212  << "Unprocessed field " << ignored << endl;
213  }
214 
215  return true;
216 }
217 
218 
220 {
221  if (results_.size())
222  {
223  Log << type() << ' ' << name() << " write:" << endl;
224  }
225 
226  // Consistent output order
227  const wordList outputList = results_.sortedToc();
228  for (const word& fieldName : outputList)
229  {
230  if (foundObject<regIOobject>(fieldName))
231  {
232  const regIOobject& io = lookupObject<regIOobject>(fieldName);
233 
234  Log << " " << fieldName << endl;
235 
236  io.write();
237  }
238  }
239 
240  return true;
241 }
242 
243 
244 // ************************************************************************* //
Foam::functionObjects::ddt2::write
virtual bool write()
Write the ddt fields.
Definition: ddt2.C:219
volFields.H
runTime
engineTime & runTime
Definition: createEngineTime.H:13
wordRes.H
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:127
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:173
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:337
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
Foam::functionObjects::addToRunTimeSelectionTable
addToRunTimeSelectionTable(functionObject, add, dictionary)
ddt2.H
Foam::regIOobject::write
virtual bool write(const bool valid=true) const
Write using setting from DB.
Definition: regIOobjectWrite.C:170
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:166
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:166
addToRunTimeSelectionTable.H
Macros for easy insertion into run-time selection tables.
Foam
Namespace for OpenFOAM.
Definition: atmBoundaryLayer.C:33
Foam::functionObjects::defineTypeNameAndDebug
defineTypeNameAndDebug(fvMeshFunctionObject, 0)
Foam::regIOobject
regIOobject is an abstract class derived from IOobject to handle automatic object registration with t...
Definition: regIOobject.H:67
Foam::nl
constexpr char nl
Definition: Ostream.H:372
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::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:294
Log
#define Log
Report write to Foam::Info if the local log switch is true.
Definition: messageStream.H:332