Loading...
Searching...
No Matches
zeroGradient.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 "zeroGradient.H"
29#include "volFields.H"
30#include "dictionary.H"
31#include "wordRes.H"
34// * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
35
36namespace Foam
37{
38namespace functionObjects
39{
42}
43}
44
46// * * * * * * * * * * * * * * * Local Functions * * * * * * * * * * * * * * //
47
48namespace Foam
49{
50
51// Check that string contains the appropriate substitution token(s)
52static bool checkFormatName(const word& str)
53{
54 if (!str.contains("@@"))
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} // End namespace Foam
75
76
77// * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * //
78
79int Foam::functionObjects::zeroGradient::process(const word& fieldName)
80{
81 int state = 0;
82 apply<scalar>(fieldName, state);
83 apply<vector>(fieldName, state);
84 apply<sphericalTensor>(fieldName, state);
85 apply<symmTensor>(fieldName, state);
86 apply<tensor>(fieldName, state);
88 return state;
89}
90
91
92// * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
93
95(
96 const word& name,
97 const Time& runTime,
98 const dictionary& dict
99)
100:
102 selectFields_(),
103 resultName_(),
104 results_()
106 read(dict);
107}
108
109
110// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
111
113{
115
116 dict.readEntry("fields", selectFields_);
117 selectFields_.uniq();
118
119 Info<< type() << " fields: " << selectFields_ << nl;
120
121 resultName_ =
122 dict.getOrDefault<word>("result", scopedName(type() + "(@@)"));
123
124 // Require '@@' token for result, unless a single (non-regex) source field
125 return
127 (selectFields_.size() == 1 && selectFields_.first().isLiteral())
128 || checkFormatName(resultName_)
129 );
130}
131
132
134{
135 results_.clear();
136
137 wordHashSet candidates(mesh_.names<regIOobject>(selectFields_));
138
139 DynamicList<word> missing(selectFields_.size());
140 DynamicList<word> ignored(selectFields_.size());
141
142 // Check exact matches first
143 for (const wordRe& select : selectFields_)
144 {
145 if (select.isLiteral())
146 {
147 const word& fieldName = select;
148
149 if (!candidates.erase(fieldName))
150 {
151 missing.append(fieldName);
152 }
153 else if (process(fieldName) < 1)
154 {
155 ignored.append(fieldName);
156 }
157 }
158 }
159
160 for (const word& fieldName : candidates)
161 {
162 process(fieldName);
163 }
164
165 if (missing.size())
166 {
168 << "Missing field " << missing << endl;
169 }
170 if (ignored.size())
171 {
173 << "Unprocessed field " << ignored << endl;
174 }
175
176 return true;
177}
178
179
181{
182 if (results_.size())
183 {
184 Log << type() << ' ' << name() << " write:" << endl;
185 }
186
187 // Consistent output order
188 for (const word& fieldName : results_.sortedToc())
189 {
190 const regIOobject* ioptr = findObject<regIOobject>(fieldName);
191
192 if (ioptr)
193 {
194 Log << " " << fieldName << endl;
195
196 ioptr->write();
197 }
198 }
199
200 return true;
201}
202
203
204// ************************************************************************* //
#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
virtual bool write(const token &tok)=0
Write token to stream or otherwise handle it.
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.
word scopedName(const word &name) const
Return a scoped (prefixed) name.
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 * findObject(const word &fieldName) const
Return const pointer to the object (eg, a field) in the (sub) objectRegistry.
Creates a volume field with zero-gradient boundary conditions from another volume field.
virtual bool read(const dictionary &dict)
Read the function-object dictionary.
virtual bool execute()
Execute the function-object operations.
zeroGradient(const word &name, const Time &runTime, const dictionary &dict)
Construct from name, Time and dictionary.
virtual bool write()
Write the function-object results.
regIOobject is an abstract class derived from IOobject to handle automatic object registration with t...
Definition regIOobject.H:71
virtual bool write(const bool writeOnProc=true) const
Write using setting from DB.
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
auto & name
#define WarningInFunction
Report a warning using Foam::Warning.
Function objects are OpenFOAM utilities to ease workflow configurations and enhance workflows.
Namespace for OpenFOAM.
static bool checkFormatName(const word &str)
Definition ddt2.C:47
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
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