Loading...
Searching...
No Matches
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-------------------------------------------------------------------------------
10License
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"
36// * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
37
38namespace Foam
39{
40namespace functionObjects
41{
44}
45}
46
48// * * * * * * * * * * * * * * * Local Functions * * * * * * * * * * * * * * //
49
50namespace Foam
51{
52
53// Check that string contains the appropriate substitution token(s)
54static 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
81bool 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
89int 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);
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))
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;
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// ************************************************************************* //
#define Log
Definition PDRblock.C:28
Macros for easy insertion into run-time selection tables.
#define addToRunTimeSelectionTable(baseType, thisType, argNames)
Add to construction table with typeName as the key.
A 1D vector of objects of type <T> that resizes itself as necessary to accept the new objects.
Definition DynamicList.H:68
void append(const T &val)
Copy append an element to the end of this list.
bool erase(const iterator &iter)
Erase an entry specified by given iterator.
Definition HashTable.C:489
Class to control time during OpenFOAM simulations that is also the top-level objectRegistry.
Definition Time.H:75
void size(const label n)
Older name for setAddressableSize.
Definition UList.H:118
A list of keyword definitions, which are a keyword followed by a number of values (eg,...
Definition dictionary.H:133
Abstract base-class for Time/database function objects.
virtual bool read(const dictionary &dict)
Read and set the function object if its data have changed.
Computes the magnitude or magnitude squared of the Eulerian time derivative of an input volume field ...
Definition ddt2.H:167
ddt2(const word &name, const Time &runTime, const dictionary &dict)
Construct from name, Time and dictionary.
Definition ddt2.C:101
virtual bool read(const dictionary &dict)
Read the function-object dictionary.
Definition ddt2.C:120
virtual bool execute()
Execute the function-object operations.
Definition ddt2.C:164
virtual bool write()
Write the function-object results.
Definition ddt2.C:211
Specialization of Foam::functionObject for an Foam::fvMesh, providing a reference to the Foam::fvMesh...
const fvMesh & mesh_
Reference to the fvMesh.
fvMeshFunctionObject(const fvMeshFunctionObject &)=delete
No copy construct.
const ObjectType & lookupObject(const word &fieldName) const
Lookup and return object (eg, a field) from the (sub) objectRegistry.
bool foundObject(const word &fieldName) const
Find object (eg, a field) in the (sub) objectRegistry.
bool match(const std::string &text) const
True if the regex matches the entire text.
Definition regExpCxxI.H:282
regIOobject is an abstract class derived from IOobject to handle automatic object registration with t...
Definition regIOobject.H:71
bool contains(char c) const noexcept
True if string contains given character (cf. C++23).
Definition string.H:412
A wordRe is a Foam::word, but can contain a regular expression for matching words or strings.
Definition wordRe.H:81
A class for handling words, derived from Foam::string.
Definition word.H:66
#define defineTypeNameAndDebug(Type, DebugSwitch)
Define the typeName and debug information.
Definition className.H:142
engineTime & runTime
const auto & io
auto & name
#define WarningInFunction
Report a warning using Foam::Warning.
Function objects are OpenFOAM utilities to ease workflow configurations and enhance workflows.
StringType quotemeta(const StringType &str, const UnaryPredicate &meta, const char quote='\\')
Quote any meta-characters in given string.
Namespace for OpenFOAM.
static bool checkFormatName(const word &str)
Definition ddt2.C:47
List< word > wordList
List of word.
Definition fileName.H:60
HashSet< word, Hash< word > > wordHashSet
A HashSet of words, uses string hasher.
Definition HashSet.H:80
bool read(const char *buf, int32_t &val)
Same as readInt32.
Definition int32.H:127
messageStream Info
Information stream (stdout output on master, null elsewhere).
fileName::Type type(const fileName &name, const bool followLink=true)
Return the file type: DIRECTORY or FILE, normally following symbolic links.
Definition POSIX.C:801
static void apply(bitSet &selection, const Detail::parcelSelection::actionType action, const Predicate &accept, const UList< Type > &list, const AccessOp &aop)
const word GlobalIOList< Tuple2< scalar, vector > >::typeName("scalarVectorTable")
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition Ostream.H:519
word name(const expressions::valueTypeCode typeCode)
A word representation of a valueTypeCode. Empty for expressions::valueTypeCode::INVALID.
Definition exprTraits.C:127
constexpr char nl
The newline '\n' character (0x0a).
Definition Ostream.H:50
dictionary dict
Functor wrapper for testing meta-characters.
Definition regExpCxx.H:166