fieldSelection.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) 2017-2019 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 "fieldSelection.H"
29#include "objectRegistry.H"
30#include "dictionary.H"
31
32// * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
33
35(
36 const objectRegistry& obr,
37 const bool includeComponents
38)
39:
40 List<fieldInfo>(),
41 obr_(obr),
42 includeComponents_(includeComponents),
43 selection_()
44{}
45
46
48(
49 const HashSet<wordRe>& names
50)
51{
52 static word cmptStr(".component(");
53 static string::size_type len(cmptStr.size());
54
55 DynamicList<fieldInfo> nameAndComponent(names.size());
56
57 for (const wordRe& name : names)
58 {
59 string::size_type n = name.find(cmptStr);
60 if (n != string::npos)
61 {
62 // Field should be written <field>.component(i)
63
64 if (!includeComponents_)
65 {
67 << "Component specification not allowed for " << name
68 << exit(FatalError);
69 }
70
71 if (name.isPattern())
72 {
74 << "Cannot use \".component option\" in combination with "
75 << "wildcards for " << name
76 << exit(FatalError);
77 }
78
79 word baseName = name.substr(0, n);
80
81 // Extract the component - number between ()'s
82 string::size_type closei = name.find(')', n);
83
84 if (closei == string::npos)
85 {
87 << "Invalid field component specification for "
88 << name << nl
89 << ". Field should be expressed as <field>.component(i)"
90 << exit(FatalError);
91 }
92
93 string::size_type cmptWidth = closei - n - len;
94
95 label component
96 (
97 readLabel(IStringStream(name.substr(n+len, cmptWidth))())
98 );
99
100 nameAndComponent.append(fieldInfo(wordRe(baseName), component));
101 }
102 else
103 {
104 nameAndComponent.append(fieldInfo(name));
105 }
106 }
107
108 this->transfer(nameAndComponent);
109
110 return true;
111}
112
113
115(
116 const wordRe& name
117)
118{
119 return resetFieldFilters(HashSet<wordRe>({name}));
120}
121
122
123// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
124
126{
127 HashSet<wordRe> fields(dict.lookup("fields"));
128
129 return resetFieldFilters(fields);
130}
131
132
134{
135 for (const fieldInfo& fi : *this)
136 {
137 if (fi.name().isPattern())
138 {
139 return true;
140 }
141 }
142
143 return false;
144}
145
146
148{
149 selection_.clear();
150}
151
152
154{
155 return false;
156}
157
158
160{
161 bool ok = true;
162 for (const fieldInfo& fi : *this)
163 {
164 if (!fi.found())
165 {
167 << "Field " << fi.name() << " not found"
168 << endl;
169
170 ok = false;
171 }
172 }
173
174 return ok;
175}
176
177
178// ************************************************************************* //
label n
label size_type
The type to represent the size of a buffer.
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
A HashTable with keys but without contents that is similar to std::unordered_set.
Definition: HashSet.H:96
Input from string buffer, using a ISstream. Always UNCOMPRESSED.
Definition: StringStream.H:112
A 1D array of objects of type <T>, where the size of the vector is known and used for subscript bound...
Definition: List.H:77
virtual bool read()
Re-read model coefficients if they have changed.
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
Helper class to store a wordRe and label used by Foam::functionObjects::fieldSelection.
Definition: fieldInfo.H:58
const wordRe & name() const
Definition: fieldInfo.H:107
Helper class to manage field selections.
virtual bool updateSelection()
Update the selection.
virtual bool containsPattern() const
Return whether the field names contain a pattern.
virtual bool checkSelection()
Check that all requested fielda have been found.
virtual bool resetFieldFilters(const HashSet< wordRe > &names)
Reset the field filters to the given field names.
virtual void clearSelection()
Clear the current selection.
Registry of regIOobjects.
static constexpr uint64_t npos
Out of range position or size.
A wordRe is a Foam::word, but can contain a regular expression for matching words or strings.
Definition: wordRe.H:83
bool isPattern() const noexcept
The wordRe is a pattern, not a literal string.
Definition: wordReI.H:111
A class for handling words, derived from Foam::string.
Definition: word.H:68
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:453
#define WarningInFunction
Report a warning using Foam::Warning.
void component(FieldField< Field, typename FieldField< Field, Type >::cmptType > &sf, const FieldField< Field, Type > &f, const direction d)
label readLabel(const char *buf)
Parse entire buffer as a label, skipping leading/trailing whitespace.
Definition: label.H:66
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:372
error FatalError
word name(const expressions::valueTypeCode typeCode)
A word representation of a valueTypeCode. Empty for INVALID.
Definition: exprTraits.C:59
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:130
constexpr char nl
The newline '\n' character (0x0a)
Definition: Ostream.H:53
dictionary dict
multivariateSurfaceInterpolationScheme< scalar >::fieldTable fields
Definition: createFields.H:97