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-2024 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  The timeSelector provides a convenient means of selecting multiple
32  times.
33 
34  A typical use would be the following:
35  \verbatim
36  timeSelector::addOptions();
37  ...
38  #include "setRootCase.H"
39  #include "createTime.H"
40  instantList timeDirs = timeSelector::select0(runTime, args);
41  ...
42  forAll(timeDirs, timei)
43  {
44  ...
45  }
46  \endverbatim
47 
48  With the \c addOptions() method, application receives
49  \b -time, \b -latestTime, \b -constant and \b -noZero options.
50  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  There may however also be many cases in which neither the \c constant/
54  directory nor the \c 0/ directory contain particularly relevant
55  information. This might occur, for example, when post-processing
56  results. In this case, addOptions is called with optional boolean
57  arguments.
58 
59  \verbatim
60  timeSelector::addOptions(false, true);
61  \endverbatim
62 
63  The first argument avoids adding the \b -constant option. The second
64  argument adds an additional \b -withZero option and also prevents the
65  \c 0/ directory from being included in the default time range and in the
66  \b -latestTime selection.
67 
68  It is also possible to use the timeSelector for setting a single time.
69  Typical use would be the following:
70 
71  \verbatim
72  timeSelector::addOptions_singleTime();
73  ...
74  #include "setRootCase.H"
75  #include "createTime.H"
76  timeSelector::setTimeIfPresent(runTime, args);
77  \endverbatim
78 
79  With the \c addOptions_singleTime() method, application receives
80  \b -time, \b -latestTime, \b -constant options. In the case,
81  the \b -time option is intended to be a single value and not include
82  any ranges.
83 
84  The subsequent call to \c setTimeIfPresent() will scan the arguments
85  for relevant time options and use them to set the time.
86 
87 SourceFiles
88  timeSelector.C
89 
90 \*---------------------------------------------------------------------------*/
91 
92 #ifndef Foam_timeSelector_H
93 #define Foam_timeSelector_H
94 
95 #include "scalarRanges.H"
96 #include "instantList.H"
97 
98 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
99 
100 namespace Foam
101 {
102 
103 // Forward Declarations
104 class argList;
105 class Time;
107 /*---------------------------------------------------------------------------*\
108  Class timeSelector Declaration
109 \*---------------------------------------------------------------------------*/
110 
111 class timeSelector
112 {
113  // Private Data
114 
115  //- The selectable time ranges
116  scalarRanges ranges_;
117 
118 
119 public:
120 
121  // Constructors
122 
123  //- Default construct
124  timeSelector() noexcept = default;
125 
126  //- Construct by parsing string for time ranges
127  explicit timeSelector(const std::string& str);
128 
129 
130  // Member Functions
131 
132  //- True if value is within any of the ranges
133  bool contains(const scalar value) const;
134 
135  //- True if value of the instant is within any of the ranges
136  bool contains(const instant& t) const;
137 
138  //- True if value of the instant is within any of the ranges
139  bool selected(const instant& t) const { return contains(t); }
140 
141  //- Return the set of selected instants in the given list that are
142  //- within the ranges
143  List<bool> selected(const instantList& times) const;
144 
145  //- Select a list of Time values that are within the ranges
146  instantList select(const instantList& times) const;
147 
148  //- Select a list of Time values that are within the ranges
149  void inplaceSelect(instantList& times) const;
150 
151 
152  // Static Member Functions
153 
154  //- Add timeSelector options to argList::validOptions
155  //
156  // \par Options added:
157  // - \c -constant
158  // - \c -time
159  // - \c -latestTime
160  // - \c -noZero
161  // - \c -withZero
162  // .
163  //
164  // \param constant
165  // Add the \b -constant option to include the \c constant/ directory
166  //
167  // \param withZero
168  // Enable the \b -withZero option and alter the normal time selection
169  // behaviour (and \b -latestTime behaviour) to exclude the \c 0/
170  // directory. The \c 0/ directory will only be included when
171  // \b -withZero is specified.
172  // The \b -noZero option has precedence over the \b -withZero option.
173  static void addOptions
174  (
175  const bool constant=true,
176  const bool withZero=false
177  );
178 
179  //- Add single-time timeSelector options to argList::validOptions()
180  //
181  // \par Options added:
182  // - \c -constant
183  // - \c -time
184  // - \c -latestTime
185  // - \c -noZero (ignored)
186  // .
187  static void addOptions_singleTime();
188 
189  //- Return the set of times selected based on the argList options
190  static instantList select
191  (
192  const instantList& times,
193  const argList& args,
194  const word& constantName = "constant"
195  );
196 
197  //- Return the set of times selected based on the argList options
198  //- and also set the runTime to the first instance or the
199  //- \c constant/ directory if no instances are specified or available
200  static instantList select0
201  (
202  Time& runTime,
203  const argList& args
204  );
205 
206  //- If any time option provided return the set of times -
207  //- as per select0() - otherwise return just the current time.
208  // Also set the runTime to the first instance
210  (
211  Time& runTime,
212  const argList& args
213  );
214 
215  //- Set the runTime based on \c -constant (if present),
216  //- \c -time (value), or \c -latestTime.
217  // This method is a no-op if no relevant options have been specified.
218  static bool setTimeIfPresent
219  (
220  Time& runTime,
221  const argList& args,
223  const bool forceInitial = false
224  );
225 };
226 
227 
228 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
229 
230 } // End namespace Foam
231 
232 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
233 
234 #endif
235 
236 // ************************************************************************* //
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
static bool setTimeIfPresent(Time &runTime, const argList &args, const bool forceInitial=false)
Set the runTime based on -constant (if present), -time (value), or -latestTime.
Definition: timeSelector.C:314
bool selected(const instant &t) const
True if value of the instant is within any of the ranges.
Definition: timeSelector.H:146
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 void addOptions_singleTime()
Add single-time timeSelector options to argList::validOptions()
Definition: timeSelector.C:146
static instantList selectIfPresent(Time &runTime, const argList &args)
If any time option provided return the set of times - as per select0() - otherwise return just the cu...
Definition: timeSelector.C:291
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:260
The timeSelector provides a convenient means of selecting multiple times.
Definition: timeSelector.H:106
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.