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  obr_(obr),
41  includeComponents_(includeComponents)
42 {}
43 
44 
45 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
46 
47 template<class Container>
48 bool 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  }
108 
109  this->transfer(nameAndComponent);
110 
111  return true;
112 }
113 
114 
116 (
117  const HashSet<wordRe>& names
118 )
119 {
120  return resetFieldFiltersImpl(names);
121 }
122 
123 
125 (
126  const wordRe& name
127 )
128 {
129  List<wordRe> names(1, name);
131  return resetFieldFiltersImpl(names);
132 }
133 
134 
135 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
136 
138 {
140  dict.readEntry("fields", fields);
141 
142  return resetFieldFilters(fields);
143 }
144 
145 
147 {
148  for (const fieldInfo& fi : *this)
149  {
150  if (fi.name().isPattern())
151  {
152  return true;
153  }
154  }
155 
156  return false;
157 }
158 
161 {
162  selection_.clear();
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 // ************************************************************************* //
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
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:129
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:598
constexpr char nl
The newline &#39;\n&#39; character (0x0a)
Definition: Ostream.H:50
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:531
label readLabel(const char *buf)
Parse entire buffer as a label, skipping leading/trailing whitespace.
Definition: label.H:63
bool found() const noexcept
Return the found state.
Definition: fieldInfo.H:127
multivariateSurfaceInterpolationScheme< scalar >::fieldTable fields
Definition: createFields.H:97
word name(const expressions::valueTypeCode typeCode)
A word representation of a valueTypeCode. Empty for expressions::valueTypeCode::INVALID.
Definition: exprTraits.C:127
A 1D vector of objects of type <T> that resizes itself as necessary to accept the new objects...
Definition: DynamicList.H:51
virtual bool updateSelection()
Update the selection.
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.
const wordRe & name() const noexcept
Return the selector pattern for the field name(s)
Definition: fieldInfo.H:117
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