1 /*---------------------------------------------------------------------------*\
3 \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
5 \\ / A nd | Copyright (C) 2011 OpenFOAM Foundation
7 -------------------------------------------------------------------------------
9 This file is part of OpenFOAM.
11 OpenFOAM is free software: you can redistribute it and/or modify it
12 under the terms of the GNU General Public License as published by
13 the Free Software Foundation, either version 3 of the License, or
14 (at your option) any later version.
16 OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
17 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
18 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
21 You should have received a copy of the GNU General Public License
22 along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
24 \*---------------------------------------------------------------------------*/
26 #include "timeSelector.H"
30 #include "IStringStream.H"
32 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
34 Foam::timeSelector::timeSelector()
40 Foam::timeSelector::timeSelector(Istream& is)
46 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
48 bool Foam::timeSelector::selected(const instant& value) const
50 return scalarRanges::selected(value.value());
54 Foam::List<bool> Foam::timeSelector::selected(const List<instant>& Times) const
56 List<bool> lst(Times.size(), false);
58 // check ranges, avoid false positive on constant/
61 if (Times[timeI].name() != "constant" && selected(Times[timeI]))
67 // check specific values
70 if (operator[](rangeI).isExact())
72 scalar target = operator[](rangeI).value();
74 int nearestIndex = -1;
75 scalar nearestDiff = Foam::GREAT;
79 if (Times[timeI].name() == "constant") continue;
81 scalar diff = fabs(Times[timeI].value() - target);
82 if (diff < nearestDiff)
89 if (nearestIndex >= 0)
91 lst[nearestIndex] = true;
100 Foam::List<Foam::instant> Foam::timeSelector::select
102 const List<instant>& Times
105 return subset(selected(Times), Times);
109 void Foam::timeSelector::inplaceSelect
114 inplaceSubset(selected(Times), Times);
118 void Foam::timeSelector::addOptions
126 argList::addBoolOption
129 "include the 'constant/' dir in the times list"
134 argList::addBoolOption
137 "include the '0/' dir in the times list"
140 argList::addBoolOption
143 "exclude the '0/' dir from the times list, "
144 "has precedence over the -zeroTime option"
146 argList::addBoolOption
149 "select the latest time"
155 "comma-separated time ranges - eg, ':10,20,40-70,1000:'"
160 Foam::List<Foam::instant> Foam::timeSelector::select
162 const List<instant>& timeDirs,
168 List<bool> selectTimes(timeDirs.size(), true);
170 // determine locations of constant/ and 0/ directories
171 label constantIdx = -1;
174 forAll(timeDirs, timeI)
176 if (timeDirs[timeI].name() == "constant")
180 else if (timeDirs[timeI].value() == 0)
185 if (constantIdx >= 0 && zeroIdx >= 0)
191 // determine latestTime selection (if any)
192 // this must appear before the -time option processing
193 label latestIdx = -1;
194 if (args.optionFound("latestTime"))
197 latestIdx = timeDirs.size() - 1;
199 // avoid false match on constant/
200 if (latestIdx == constantIdx)
206 if (args.optionFound("time"))
208 // can match 0/, but can never match constant/
209 selectTimes = timeSelector
211 args.optionLookup("time")()
212 ).selected(timeDirs);
216 // add in latestTime (if selected)
219 selectTimes[latestIdx] = true;
222 if (constantIdx >= 0)
224 // only add constant/ if specifically requested
225 selectTimes[constantIdx] = args.optionFound("constant");
228 // special treatment for 0/
231 if (args.optionFound("noZero"))
233 // exclude 0/ if specifically requested
234 selectTimes[zeroIdx] = false;
236 else if (argList::validOptions.found("zeroTime"))
238 // with -zeroTime enabled, drop 0/ unless specifically requested
239 selectTimes[zeroIdx] = args.optionFound("zeroTime");
243 return subset(selectTimes, timeDirs);
252 Foam::List<Foam::instant> Foam::timeSelector::select0
258 instantList timeDirs = timeSelector::select(runTime.times(), args);
260 if (timeDirs.empty())
262 FatalErrorIn(args.executable())
263 << "No times selected"
267 runTime.setTime(timeDirs[0], 0);
273 // ************************************************************************* //