Forward compatibility: flex
[foam-extend-3.2.git] / src / foam / db / Time / timeSelector.C
blobaf82d81e03d24224f752f3ed0564eb466f50e38f
1 /*---------------------------------------------------------------------------*\
2   =========                 |
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 -------------------------------------------------------------------------------
8 License
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"
27 #include "ListOps.H"
28 #include "argList.H"
29 #include "objectRegistry.H"
30 #include "IStringStream.H"
32 // * * * * * * * * * * * * * * * * Constructors  * * * * * * * * * * * * * * //
34 Foam::timeSelector::timeSelector()
36     scalarRanges()
40 Foam::timeSelector::timeSelector(Istream& is)
42     scalarRanges(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/
59     forAll(Times, timeI)
60     {
61         if (Times[timeI].name() != "constant" && selected(Times[timeI]))
62         {
63             lst[timeI] = true;
64         }
65     }
67     // check specific values
68     forAll(*this, rangeI)
69     {
70         if (operator[](rangeI).isExact())
71         {
72             scalar target = operator[](rangeI).value();
74             int nearestIndex = -1;
75             scalar nearestDiff = Foam::GREAT;
77             forAll(Times, timeI)
78             {
79                 if (Times[timeI].name() == "constant") continue;
81                 scalar diff = fabs(Times[timeI].value() - target);
82                 if (diff < nearestDiff)
83                 {
84                     nearestDiff = diff;
85                     nearestIndex = timeI;
86                 }
87             }
89             if (nearestIndex >= 0)
90             {
91                 lst[nearestIndex] = true;
92             }
93         }
94     }
96     return lst;
100 Foam::List<Foam::instant> Foam::timeSelector::select
102     const List<instant>& Times
103 ) const
105     return subset(selected(Times), Times);
109 void Foam::timeSelector::inplaceSelect
111     List<instant>& Times
112 ) const
114     inplaceSubset(selected(Times), Times);
118 void Foam::timeSelector::addOptions
120     const bool constant,
121     const bool zeroTime
124     if (constant)
125     {
126         argList::validOptions.insert("constant", "");
127     }
128     if (zeroTime)
129     {
130         argList::validOptions.insert("zeroTime", "");
131     }
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,
141     const argList& args
144     if (timeDirs.size())
145     {
146         List<bool> selectTimes(timeDirs.size(), true);
148         // determine locations of constant/ and 0/ directories
149         label constantIdx = -1;
150         label zeroIdx = -1;
152         forAll(timeDirs, timeI)
153         {
154             if (timeDirs[timeI].name() == "constant")
155             {
156                 constantIdx = timeI;
157             }
158             else if (timeDirs[timeI].value() == 0)
159             {
160                 zeroIdx = timeI;
161             }
163             if (constantIdx >= 0 && zeroIdx >= 0)
164             {
165                 break;
166             }
167         }
169         // determine latestTime selection (if any)
170         // this must appear before the -time option processing
171         label latestIdx = -1;
172         if (args.optionFound("latestTime"))
173         {
174             selectTimes = false;
175             latestIdx = timeDirs.size() - 1;
177             // avoid false match on constant/
178             if (latestIdx == constantIdx)
179             {
180                 latestIdx = -1;
181             }
182         }
184         if (args.optionFound("time"))
185         {
186             // can match 0/, but can never match constant/
187             selectTimes = timeSelector
188             (
189                 args.optionLookup("time")()
190             ).selected(timeDirs);
191         }
194         // add in latestTime (if selected)
195         if (latestIdx >= 0)
196         {
197             selectTimes[latestIdx] = true;
198         }
200         if (constantIdx >= 0)
201         {
202             // only add constant/ if specifically requested
203             selectTimes[constantIdx] = args.optionFound("constant");
204         }
206         // special treatment for 0/
207         if (zeroIdx >= 0)
208         {
209             if (args.optionFound("noZero"))
210             {
211                 // exclude 0/ if specifically requested
212                 selectTimes[zeroIdx] = false;
213             }
214             else if (argList::validOptions.found("zeroTime"))
215             {
216                 // with -zeroTime enabled, drop 0/ unless specifically requested
217                 selectTimes[zeroIdx] = args.optionFound("zeroTime");
218             }
219         }
221         return subset(selectTimes, timeDirs);
222     }
223     else
224     {
225         return timeDirs;
226     }
230 Foam::List<Foam::instant> Foam::timeSelector::select0
232     Time& runTime,
233     const argList& args
236     instantList timeDirs = timeSelector::select(runTime.times(), args);
238     if (timeDirs.empty())
239     {
240         FatalErrorIn(args.executable())
241             << "No times selected"
242             << exit(FatalError);
243     }
245     runTime.setTime(timeDirs[0], 0);
247     return timeDirs;
251 // ************************************************************************* //