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 -------------------------------------------------------------------------------
10 License
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 
34 Foam::functionObjects::fieldSelection::fieldSelection
35 (
36  const objectRegistry& obr,
37  const bool includeComponents
38 )
39 :
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 // ************************************************************************* //
Foam::wordRe::isPattern
bool isPattern() const noexcept
The wordRe is treated as a pattern, not as literal string.
Definition: wordReI.H:107
Foam::component
void component(FieldField< Field, typename FieldField< Field, Type >::cmptType > &sf, const FieldField< Field, Type > &f, const direction d)
Definition: FieldFieldFunctions.C:44
Foam::word
A class for handling words, derived from Foam::string.
Definition: word.H:65
Foam::DynamicList
A 1D vector of objects of type <T> that resizes itself as necessary to accept the new objects.
Definition: DynamicList.H:55
Foam::functionObjects::fieldInfo::name
const wordRe & name() const
Definition: fieldInfo.H:107
Foam::functionObjects::fieldInfo
Helper class to store a wordRe and label used by Foam::functionObjects::fieldSelection.
Definition: fieldInfo.H:57
Foam::functionObjects::fieldSelection::checkSelection
virtual bool checkSelection()
Check that all requested fielda have been found.
Definition: fieldSelection.C:159
objectRegistry.H
Foam::functionObjects::fieldSelection::updateSelection
virtual bool updateSelection()
Update the selection.
Definition: fieldSelection.C:153
Foam::endl
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:369
Foam::HashSet
A HashTable with keys but without contents that is similar to std::unordered_set.
Definition: HashSet.H:77
Foam::wordRe
A wordRe is a Foam::word, but can contain a regular expression for matching words or strings.
Definition: wordRe.H:80
Foam::functionObjects::fieldSelection::containsPattern
virtual bool containsPattern() const
Return whether the field names contain a pattern.
Definition: fieldSelection.C:133
Foam::functionObjects::fieldSelection::clearSelection
virtual void clearSelection()
Clear the current selection.
Definition: fieldSelection.C:147
Foam::objectRegistry
Registry of regIOobjects.
Definition: objectRegistry.H:60
n
label n
Definition: TABSMDCalcMethod2.H:31
size_type
graph_traits< Graph >::vertices_size_type size_type
Definition: SloanRenumber.C:76
dict
dictionary dict
Definition: searchingEngine.H:14
Foam::FatalError
error FatalError
Foam::dictionary
A list of keyword definitions, which are a keyword followed by a number of values (eg,...
Definition: dictionary.H:123
Foam::IStringStream
Input from string buffer, using a ISstream. Always UNCOMPRESSED.
Definition: StringStream.H:108
Foam::exit
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:130
fieldSelection.H
Foam::functionObjects::fieldInfo::found
Switch & found() const
Definition: fieldInfo.H:117
FatalErrorInFunction
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:453
Foam::nl
constexpr char nl
Definition: Ostream.H:404
Foam::readLabel
label readLabel(const char *buf)
Parse entire buffer as a label, skipping leading/trailing whitespace.
Definition: label.H:66
Foam::List< fieldInfo >
dictionary.H
Foam::name
word name(const expressions::valueTypeCode typeCode)
A word representation of a valueTypeCode. Empty for INVALID.
Definition: exprTraits.C:59
Foam::functionObjects::fieldSelection::resetFieldFilters
virtual bool resetFieldFilters(const HashSet< wordRe > &names)
Reset the field filters to the given field names.
Definition: fieldSelection.C:48
Foam::functionObjects::fieldSelection::read
virtual bool read(const dictionary &dict)
Read the fieldSelection data from dictionary.
Definition: fieldSelection.C:125
Foam::PtrListOps::names
List< word > names(const UPtrList< T > &list, const UnaryMatchPredicate &matcher)
WarningInFunction
#define WarningInFunction
Report a warning using Foam::Warning.
Definition: messageStream.H:328
fields
multivariateSurfaceInterpolationScheme< scalar >::fieldTable fields
Definition: createFields.H:97