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 :
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 
148 {
149  selection_.clear();
150 }
151 
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 // ************************************************************************* //
virtual bool checkSelection()
Check that all requested fielda have been found.
A HashTable with keys but without contents that is similar to std::unordered_set. ...
Definition: HashSet.H:73
dictionary dict
void size(const label n)
Older name for setAddressableSize.
Definition: UList.H:116
List< word > names(const UPtrList< T > &list, const UnaryMatchPredicate &matcher)
List of names generated by calling name() for each list item and filtered for matches.
virtual bool containsPattern() const
Return whether the field names contain a pattern.
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:125
void transfer(List< fieldInfo > &list)
Transfer the contents of the argument List into this list and annul the argument list.
Definition: List.C:452
error FatalError
Error stream (stdout output on all processes), with additional &#39;FOAM FATAL ERROR&#39; header text and sta...
A list of keyword definitions, which are a keyword followed by a number of values (eg...
Definition: dictionary.H:120
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:578
A 1D array of objects of type <T>, where the size of the vector is known and used for subscript bound...
Definition: BitOps.H:56
constexpr char nl
The newline &#39;\n&#39; character (0x0a)
Definition: Ostream.H:49
Helper class to store a wordRe and label used by Foam::functionObjects::fieldSelection.
Definition: fieldInfo.H:51
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:487
label readLabel(const char *buf)
Parse entire buffer as a label, skipping leading/trailing whitespace.
Definition: label.H:63
multivariateSurfaceInterpolationScheme< scalar >::fieldTable fields
Definition: createFields.H:97
word name(const expressions::valueTypeCode typeCode)
A word representation of a valueTypeCode. Empty for INVALID.
Definition: exprTraits.C:52
A 1D vector of objects of type <T> that resizes itself as necessary to accept the new objects...
Definition: DynamicList.H:51
const wordRe & name() const
Definition: fieldInfo.H:115
A class for handling words, derived from Foam::string.
Definition: word.H:63
virtual bool updateSelection()
Update the selection.
const bool includeComponents_
Flag to indicate whether components are allowed.
graph_traits< Graph >::vertices_size_type size_type
Definition: SloanRenumber.C:68
A wordRe is a Foam::word, but can contain a regular expression for matching words or strings...
Definition: wordRe.H:78
virtual void clearSelection()
Clear the current selection.
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.
#define WarningInFunction
Report a warning using Foam::Warning.
label n
Registry of regIOobjects.
void component(FieldField< Field, typename FieldField< Field, Type >::cmptType > &sf, const FieldField< Field, Type > &f, const direction d)
bool isPattern() const noexcept
The wordRe is a pattern, not a literal string.
Definition: wordReI.H:104