Loading...
Searching...
No Matches
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\*---------------------------------------------------------------------------*/
28#include "fieldSelection.H"
29#include "objectRegistry.H"
30#include "dictionary.H"
31
32// * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
33
34Foam::functionObjects::fieldSelection::fieldSelection
35(
36 const objectRegistry& obr,
37 const bool includeComponents
38)
39:
40 obr_(obr),
41 includeComponents_(includeComponents)
42{}
43
44
45// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
46
47template<class Container>
48bool Foam::functionObjects::fieldSelection::resetFieldFiltersImpl
49(
50 const Container& names
51)
52{
53 static std::string cmptStr(".component(");
54 static std::string::size_type len(cmptStr.size());
55
56 DynamicList<fieldInfo> nameAndComponent(names.size());
57
58 for (const wordRe& name : names)
59 {
60 const auto n = name.find(cmptStr);
61 if (n != std::string::npos)
62 {
63 // Field should be written <field>.component(i)
64
65 if (!includeComponents_)
66 {
68 << "Component specification not allowed for " << name
69 << exit(FatalError);
70 }
71
72 if (name.isPattern())
73 {
75 << "Cannot use \".component option\" in combination with "
76 << "wildcards for " << name
77 << exit(FatalError);
78 }
79
80 const word baseName(name.substr(0, n));
81
82 // Extract the component - number between ()'s
83 const auto closei = name.find(')', n);
84
85 if (closei == std::string::npos)
86 {
88 << "Invalid field component specification for "
89 << name << nl
90 << ". Field should be expressed as <field>.component(i)"
91 << exit(FatalError);
92 }
93
94 const auto cmptWidth = (closei - n - len);
95
96 label component
97 (
98 readLabel(name.substr(n+len, cmptWidth))
99 );
100
101 nameAndComponent.emplace_back(wordRe(baseName), component);
102 }
103 else
104 {
105 nameAndComponent.emplace_back(name);
106 }
107 }
109 this->transfer(nameAndComponent);
110
111 return true;
112}
113
114
116(
118)
119{
120 return resetFieldFiltersImpl(names);
121}
122
123
125(
126 const wordRe& name
127)
128{
131 return resetFieldFiltersImpl(names);
132}
133
134
135// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
136
138{
140 dict.readEntry("fields", fields);
141
143}
144
145
147{
148 for (const fieldInfo& fi : *this)
149 {
150 if (fi.name().isPattern())
151 {
152 return true;
154 }
155
156 return false;
157}
158
163}
164
167{
168 return false;
169}
170
171
173{
174 bool ok = true;
175 for (const fieldInfo& fi : *this)
176 {
177 if (!fi.found())
178 {
180 << "Field " << fi.name() << " not found"
181 << endl;
182
183 ok = false;
184 }
185 }
186
187 return ok;
188}
189
190
191// ************************************************************************* //
label n
A 1D vector of objects of type <T> that resizes itself as necessary to accept the new objects.
Definition DynamicList.H:68
A HashTable with keys but without contents that is similar to std::unordered_set.
Definition HashSet.H:96
A 1D array of objects of type <T>, where the size of the vector is known and used for subscript bound...
Definition List.H:72
void transfer(List< fieldInfo > &list)
Definition List.C:347
A list of keyword definitions, which are a keyword followed by a number of values (eg,...
Definition dictionary.H:133
Helper class to store a wordRe and label used by Foam::functionObjects::fieldSelection.
Definition fieldInfo.H:52
bool found() const noexcept
Return the found state.
Definition fieldInfo.H:127
const wordRe & name() const noexcept
Return the selector pattern for the field name(s).
Definition fieldInfo.H:117
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.
const objectRegistry & obr_
Reference to the database.
const bool includeComponents_
Flag to indicate whether components are allowed.
virtual bool read(const dictionary &dict)
Read the fieldSelection data from dictionary.
virtual bool resetFieldFilters(const HashSet< wordRe > &names)
Reset the field filters to the given field names.
virtual void clearSelection()
Clear the current selection.
List< fieldInfo > selection_
Current field selection.
Registry of regIOobjects.
A wordRe is a Foam::word, but can contain a regular expression for matching words or strings.
Definition wordRe.H:81
bool isPattern() const noexcept
The wordRe is a pattern, not a literal string.
Definition wordReI.H:104
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition error.H:600
auto & name
auto & names
#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:63
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition Ostream.H:519
error FatalError
Error stream (stdout output on all processes), with additional 'FOAM FATAL ERROR' header text and sta...
word name(const expressions::valueTypeCode typeCode)
A word representation of a valueTypeCode. Empty for expressions::valueTypeCode::INVALID.
Definition exprTraits.C:127
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition errorManip.H:125
constexpr char nl
The newline '\n' character (0x0a).
Definition Ostream.H:50
dictionary dict
multivariateSurfaceInterpolationScheme< scalar >::fieldTable fields