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-2020 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 "stringListOps.H"
30#include "volFields.H"
31#include "dictionary.H"
32#include "wordRes.H"
34
35// * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
36
37namespace Foam
38{
39namespace functionObjects
40{
43}
44}
45
46// * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * //
47
48bool Foam::functionObjects::zeroGradient::checkFormatName
49(
50 const std::string& str
51)
52{
53 if (std::string::npos == str.find("@@"))
54 {
56 << "Bad result naming (no '@@' token found)."
57 << nl << endl;
58
59 return false;
60 }
61 else if (str == "@@")
62 {
64 << "Bad result naming (only a '@@' token found)."
65 << nl << endl;
66
67 return false;
68 }
69
70 return true;
71}
72
73
74int Foam::functionObjects::zeroGradient::process(const word& fieldName)
75{
76 int state = 0;
77 apply<scalar>(fieldName, state);
78 apply<vector>(fieldName, state);
79 apply<sphericalTensor>(fieldName, state);
80 apply<symmTensor>(fieldName, state);
81 apply<tensor>(fieldName, state);
82
83 return state;
84}
85
86
87// * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
88
90(
91 const word& name,
92 const Time& runTime,
93 const dictionary& dict
94)
95:
97 selectFields_(),
98 resultName_(string::null),
99 results_()
100{
101 read(dict);
102}
103
104
105// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
106
108{
110
111 dict.readEntry("fields", selectFields_);
112 selectFields_.uniq();
113
114 Info<< type() << " fields: " << selectFields_ << nl;
115
116 resultName_ =
117 dict.getOrDefault<word>("result", scopedName(type() + "(@@)"));
118
119 // Require '@@' token for result, unless a single (non-regex) source field
120 return
121 (
122 (selectFields_.size() == 1 && selectFields_.first().isLiteral())
123 || checkFormatName(resultName_)
124 );
125}
126
127
129{
130 results_.clear();
131
132 wordHashSet candidates(subsetStrings(selectFields_, mesh_.names()));
133 DynamicList<word> missing(selectFields_.size());
134 DynamicList<word> ignored(selectFields_.size());
135
136 // Check exact matches first
137 for (const wordRe& select : selectFields_)
138 {
139 if (select.isLiteral())
140 {
141 const word& fieldName = select;
142
143 if (!candidates.erase(fieldName))
144 {
145 missing.append(fieldName);
146 }
147 else if (process(fieldName) < 1)
148 {
149 ignored.append(fieldName);
150 }
151 }
152 }
153
154 for (const word& fieldName : candidates)
155 {
156 process(fieldName);
157 }
158
159 if (missing.size())
160 {
162 << "Missing field " << missing << endl;
163 }
164 if (ignored.size())
165 {
167 << "Unprocessed field " << ignored << endl;
168 }
169
170 return true;
171}
172
173
175{
176 if (results_.size())
177 {
178 Log << type() << ' ' << name() << " write:" << endl;
179 }
180
181 // Consistent output order
182 for (const word& fieldName : results_.sortedToc())
183 {
184 const regIOobject* ioptr = findObject<regIOobject>(fieldName);
185
186 if (ioptr)
187 {
188 Log << " " << fieldName << endl;
189
190 ioptr->write();
191 }
192 }
193
194 return true;
195}
196
197
198// ************************************************************************* //
#define Log
Definition: PDRblock.C:35
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:72
void append(const T &val)
Copy append an element to the end of this list.
Definition: DynamicListI.H:503
bool erase(const iterator &iter)
Erase an entry specified by given iterator.
Definition: HashTable.C:440
virtual bool read()
Re-read model coefficients if they have changed.
Class to control time during OpenFOAM simulations that is also the top-level objectRegistry.
Definition: Time.H:80
void size(const label n)
Older name for setAddressableSize.
Definition: UList.H:114
A list of keyword definitions, which are a keyword followed by a number of values (eg,...
Definition: dictionary.H:126
Abstract base-class for Time/database function objects.
Specialization of Foam::functionObject for an Foam::fvMesh, providing a reference to the Foam::fvMesh...
Creates a volume field with zero-gradient boundary conditions from another volume field.
Definition: zeroGradient.H:170
virtual bool read(const dictionary &dict)
Read the zeroGradient specification.
Definition: zeroGradient.C:107
virtual bool execute()
Calculate the zeroGradient fields.
Definition: zeroGradient.C:128
virtual bool write()
Write the zeroGradient fields.
Definition: zeroGradient.C:174
regIOobject is an abstract class derived from IOobject to handle automatic object registration with t...
Definition: regIOobject.H:76
virtual bool write(const bool valid=true) const
Write using setting from DB.
A class for handling character strings derived from std::string.
Definition: string.H:79
A wordRe is a Foam::word, but can contain a regular expression for matching words or strings.
Definition: wordRe.H:83
A class for handling words, derived from Foam::string.
Definition: word.H:68
#define defineTypeNameAndDebug(Type, DebugSwitch)
Define the typeName and debug information.
Definition: className.H:121
engineTime & runTime
#define WarningInFunction
Report a warning using Foam::Warning.
Namespace for OpenFOAM.
StringListType subsetStrings(const regExp &matcher, const StringListType &input, const bool invert=false)
Extract elements of StringList when regular expression matches.
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: MSwindows.C:598
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:372
word name(const expressions::valueTypeCode typeCode)
A word representation of a valueTypeCode. Empty for INVALID.
Definition: exprTraits.C:59
constexpr char nl
The newline '\n' character (0x0a)
Definition: Ostream.H:53
dictionary dict
Operations on lists of strings.