1 /*---------------------------------------------------------------------------*\
3 \\ / F ield | foam-extend: Open Source CFD
4 \\ / O peration | Version: 3.2
5 \\ / A nd | Web: http://www.foam-extend.org
6 \\/ M anipulation | For copyright notice see file Copyright
7 -------------------------------------------------------------------------------
9 This file is part of foam-extend.
11 foam-extend is free software: you can redistribute it and/or modify it
12 under the terms of the GNU General Public License as published by the
13 Free Software Foundation, either version 3 of the License, or (at your
14 option) any later version.
16 foam-extend is distributed in the hope that it will be useful, but
17 WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 General Public License for more details.
21 You should have received a copy of the GNU General Public License
22 along with foam-extend. If not, see <http://www.gnu.org/licenses/>.
24 \*---------------------------------------------------------------------------*/
26 #include "timeSelector.H"
29 #include "objectRegistry.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::validOptions.insert("constant", "");
130 argList::validOptions.insert("zeroTime", "");
132 argList::validOptions.insert("noZero", "");
133 argList::validOptions.insert("time", "ranges");
134 argList::validOptions.insert("latestTime", "");
138 Foam::List<Foam::instant> Foam::timeSelector::select
140 const List<instant>& timeDirs,
146 List<bool> selectTimes(timeDirs.size(), true);
148 // determine locations of constant/ and 0/ directories
149 label constantIdx = -1;
152 forAll(timeDirs, timeI)
154 if (timeDirs[timeI].name() == "constant")
158 else if (timeDirs[timeI].value() == 0)
163 if (constantIdx >= 0 && zeroIdx >= 0)
169 // determine latestTime selection (if any)
170 // this must appear before the -time option processing
171 label latestIdx = -1;
172 if (args.optionFound("latestTime"))
175 latestIdx = timeDirs.size() - 1;
177 // avoid false match on constant/
178 if (latestIdx == constantIdx)
184 if (args.optionFound("time"))
186 // can match 0/, but can never match constant/
187 selectTimes = timeSelector
189 args.optionLookup("time")()
190 ).selected(timeDirs);
194 // add in latestTime (if selected)
197 selectTimes[latestIdx] = true;
200 if (constantIdx >= 0)
202 // only add constant/ if specifically requested
203 selectTimes[constantIdx] = args.optionFound("constant");
206 // special treatment for 0/
209 if (args.optionFound("noZero"))
211 // exclude 0/ if specifically requested
212 selectTimes[zeroIdx] = false;
214 else if (argList::validOptions.found("zeroTime"))
216 // with -zeroTime enabled, drop 0/ unless specifically requested
217 selectTimes[zeroIdx] = args.optionFound("zeroTime");
221 return subset(selectTimes, timeDirs);
230 Foam::List<Foam::instant> Foam::timeSelector::select0
236 instantList timeDirs = timeSelector::select(runTime.times(), args);
238 if (timeDirs.empty())
240 FatalErrorIn(args.executable())
241 << "No times selected"
245 runTime.setTime(timeDirs[0], 0);
251 // ************************************************************************* //