timeSelector.H
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) 2011-2015 OpenFOAM Foundation
9  Copyright (C) 2018-2022 OpenCFD Ltd.
10 -------------------------------------------------------------------------------
11 License
12  This file is part of OpenFOAM.
13 
14  OpenFOAM is free software: you can redistribute it and/or modify it
15  under the terms of the GNU General Public License as published by
16  the Free Software Foundation, either version 3 of the License, or
17  (at your option) any later version.
18 
19  OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
20  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
21  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
22  for more details.
23 
24  You should have received a copy of the GNU General Public License
25  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
26 
27 Class
28  Foam::timeSelector
29 
30 Description
31  A List of scalarRange for selecting times.
32 
33  The timeSelector provides a convenient means of selecting multiple
34  times. A typical use would be the following:
35 
36  \verbatim
37  timeSelector::addOptions();
38  // add other options
39  #include "setRootCase.H"
40  #include "createTime.H"
41  instantList timeDirs = timeSelector::select0(runTime, args);
42  ...
43  forAll(timeDirs, timei)
44  {
45  ...
46  }
47  \endverbatim
48 
49  The result program would receive \b -time, \b -latestTime, \b -constant
50  and \b -noZero options. The \b -constant option explicitly includes the
51  \c constant/ directory in the time list and the \b -noZero option
52  explicitly excludes the \c 0/ directory from the time list.
53 
54  There may however also be many cases in which neither the \c constant/
55  directory nor the \c 0/ directory contain particularly relevant
56  information. This might occur, for example, when post-processing
57  results. In this case, addOptions is called with optional boolean
58  arguments.
59 
60  \verbatim
61  timeSelector::addOptions(false, true);
62  \endverbatim
63 
64  The first argument avoids adding the \b -constant option. The second
65  argument adds an additional \b -withZero option and also prevents the
66  \c 0/ directory from being included in the default time range and in the
67  \b -latestTime selection.
68 
69 SourceFiles
70  timeSelector.C
71 
72 \*---------------------------------------------------------------------------*/
73 
74 #ifndef Foam_timeSelector_H
75 #define Foam_timeSelector_H
76 
77 #include "scalarRanges.H"
78 #include "instantList.H"
79 
80 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
81 
82 namespace Foam
83 {
84 
85 // Forward Declarations
86 class argList;
87 class Time;
88 
89 /*---------------------------------------------------------------------------*\
90  Class timeSelector Declaration
91 \*---------------------------------------------------------------------------*/
92 
93 class timeSelector
94 {
95  // Private Data
96 
97  //- The selectable time ranges
98  scalarRanges ranges_;
99 
100 
101 public:
102 
103  // Constructors
104 
105  //- Default construct
106  timeSelector() noexcept = default;
107 
108  //- Construct by parsing string for time ranges
109  explicit timeSelector(const std::string& str);
110 
111 
112  // Member Functions
113 
114  //- True if value is within any of the ranges
115  bool contains(const scalar value) const;
116 
117  //- True if value of the instant is within any of the ranges
118  bool contains(const instant& t) const;
119 
120  //- True if value of the instant is within any of the ranges
121  bool selected(const instant& t) const { return contains(t); }
122 
123  //- Return the set of selected instants in the given list that are
124  //- within the ranges
125  List<bool> selected(const instantList& times) const;
126 
127  //- Select a list of Time values that are within the ranges
128  instantList select(const instantList& times) const;
129 
130  //- Select a list of Time values that are within the ranges
131  void inplaceSelect(instantList& times) const;
132 
133 
134  // Static Member Functions
135 
136  //- Add timeSelector options to argList::validOptions
137  //
138  // \param constant
139  // Add the \b -constant option to include the \c constant/ directory
140  //
141  // \param withZero
142  // Enable the \b -withZero option and alter the normal time selection
143  // behaviour (and \b -latestTime behaviour) to exclude the \c 0/
144  // directory. The \c 0/ directory will only be included when
145  // \b -withZero is specified.
146  // The \b -noZero option has precedence over the \b -withZero option.
147  static void addOptions
148  (
149  const bool constant=true,
150  const bool withZero=false
151  );
152 
153  //- Return the set of times selected based on the argList options
154  static instantList select
155  (
156  const instantList& times,
157  const argList& args,
158  const word& constantName = "constant"
159  );
160 
161  //- Return the set of times selected based on the argList options
162  //- and also set the runTime to the first instance or the
163  //- \c constant/ directory if no instances are specified or available
164  static instantList select0
165  (
166  Time& runTime,
167  const argList& args
168  );
169 
170  //- If any time option provided return the set of times (as select0)
171  //- otherwise return just the current time.
172  // Also set the runTime to the first instance
174  (
175  Time& runTime,
176  const argList& args
177  );
178 };
179 
180 
181 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
182 
183 } // End namespace Foam
184 
185 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
186 
187 #endif
188 
189 // ************************************************************************* //
void inplaceSelect(instantList &times) const
Select a list of Time values that are within the ranges.
Definition: timeSelector.C:94
timeSelector() noexcept=default
Default construct.
engineTime & runTime
bool selected(const instant &t) const
True if value of the instant is within any of the ranges.
Definition: timeSelector.H:128
Class to control time during OpenFOAM simulations that is also the top-level objectRegistry.
Definition: Time.H:69
instantList select(const instantList &times) const
Select a list of Time values that are within the ranges.
Definition: timeSelector.C:88
A class for handling words, derived from Foam::string.
Definition: word.H:63
Extract command arguments and options from the supplied argc and argv parameters. ...
Definition: argList.H:118
static instantList selectIfPresent(Time &runTime, const argList &args)
If any time option provided return the set of times (as select0) otherwise return just the current ti...
Definition: timeSelector.C:265
const direction noexcept
Definition: Scalar.H:258
static instantList select0(Time &runTime, const argList &args)
Return the set of times selected based on the argList options and also set the runTime to the first i...
Definition: timeSelector.C:234
A List of scalarRange for selecting times.
Definition: timeSelector.H:88
An instant of time. Contains the time value and name. Uses Foam::Time when formatting the name...
Definition: instant.H:53
A collection of scalar bounds to be used as a unary predicate.
Definition: scalarRanges.H:51
Foam::argList args(argc, argv)
bool contains(const scalar value) const
True if value is within any of the ranges.
Definition: timeSelector.C:37
static void addOptions(const bool constant=true, const bool withZero=false)
Add timeSelector options to argList::validOptions.
Definition: timeSelector.C:101
Namespace for OpenFOAM.