/
timeSelector.C
345 lines (291 loc) · 7.94 KB
/
timeSelector.C
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
/*---------------------------------------------------------------------------*\
========= |
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration |
\\ / A nd | Copyright (C) 2011-2017 OpenFOAM Foundation
\\/ M anipulation |
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
OpenFOAM is free software: you can redistribute it and/or modify it
under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
for more details.
You should have received a copy of the GNU General Public License
along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
\*---------------------------------------------------------------------------*/
#include "timeSelector.H"
#include "ListOps.H"
#include "argList.H"
#include "Time.H"
#include "IStringStream.H"
// * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
Foam::timeSelector::timeSelector()
:
scalarRanges()
{}
Foam::timeSelector::timeSelector(Istream& is)
:
scalarRanges(is)
{}
// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
bool Foam::timeSelector::selected(const instant& value) const
{
return scalarRanges::selected(value.value());
}
Foam::List<bool> Foam::timeSelector::selected(const instantList& Times) const
{
List<bool> lst(Times.size(), false);
// Check ranges, avoid false positive on constant/
forAll(Times, timeI)
{
if (Times[timeI].name() != "constant" && selected(Times[timeI]))
{
lst[timeI] = true;
}
}
// Check specific values
forAll(*this, rangeI)
{
if (operator[](rangeI).isExact())
{
scalar target = operator[](rangeI).value();
int nearestIndex = -1;
scalar nearestDiff = Foam::GREAT;
forAll(Times, timeI)
{
if (Times[timeI].name() == "constant") continue;
scalar diff = fabs(Times[timeI].value() - target);
if (diff < nearestDiff)
{
nearestDiff = diff;
nearestIndex = timeI;
}
}
if (nearestIndex >= 0)
{
lst[nearestIndex] = true;
}
}
}
return lst;
}
Foam::instantList Foam::timeSelector::select(const instantList& Times)
const
{
return subset(selected(Times), Times);
}
void Foam::timeSelector::inplaceSelect(instantList& Times) const
{
inplaceSubset(selected(Times), Times);
}
void Foam::timeSelector::addOptions
(
const bool constant,
const bool withZero
)
{
if (constant)
{
argList::addBoolOption
(
"constant",
"include the 'constant/' dir in the times list"
);
}
if (withZero)
{
argList::addBoolOption
(
"withZero",
"include the '0/' dir in the times list"
);
}
argList::addBoolOption
(
"noZero",
string("exclude the '0/' dir from the times list")
+ (
withZero
? ", has precedence over the -withZero option"
: ""
)
);
argList::addBoolOption
(
"latestTime",
"select the latest time"
);
argList::addBoolOption
(
"newTimes",
"select the new times"
);
argList::addOption
(
"time",
"ranges",
"comma-separated time ranges - eg, ':10,20,40:70,1000:'"
);
}
Foam::instantList Foam::timeSelector::select
(
const instantList& timeDirs,
const argList& args,
const word& constantName
)
{
if (timeDirs.size())
{
List<bool> selectTimes(timeDirs.size(), true);
// Determine locations of constant/ and 0/ directories
label constantIdx = -1;
label zeroIdx = -1;
forAll(timeDirs, timeI)
{
if (timeDirs[timeI].name() == constantName)
{
constantIdx = timeI;
}
else if (timeDirs[timeI].value() == 0)
{
zeroIdx = timeI;
}
if (constantIdx >= 0 && zeroIdx >= 0)
{
break;
}
}
// Determine latestTime selection (if any)
// This must appear before the -time option processing
label latestIdx = -1;
if (args.optionFound("latestTime"))
{
selectTimes = false;
latestIdx = timeDirs.size() - 1;
// Avoid false match on constant/
if (latestIdx == constantIdx)
{
latestIdx = -1;
}
}
if (args.optionFound("time"))
{
// Can match 0/, but can never match constant/
selectTimes = timeSelector
(
args.optionLookup("time")()
).selected(timeDirs);
}
// Add in latestTime (if selected)
if (latestIdx >= 0)
{
selectTimes[latestIdx] = true;
}
if (constantIdx >= 0)
{
// Only add constant/ if specifically requested
selectTimes[constantIdx] = args.optionFound("constant");
}
// Special treatment for 0/
if (zeroIdx >= 0)
{
if (args.optionFound("noZero"))
{
// Exclude 0/ if specifically requested
selectTimes[zeroIdx] = false;
}
else if (argList::validOptions.found("withZero"))
{
// With -withZero enabled, drop 0/ unless specifically requested
selectTimes[zeroIdx] = args.optionFound("withZero");
}
}
return subset(selectTimes, timeDirs);
}
else
{
return timeDirs;
}
}
Foam::instantList Foam::timeSelector::select0
(
Time& runTime,
const argList& args
)
{
instantList timeDirs
(
timeSelector::select
(
runTime.times(),
args,
runTime.constant()
)
);
if (timeDirs.empty())
{
WarningInFunction
<< "No time specified or available, selecting 'constant'"
<< endl;
timeDirs.append(instant(0, runTime.constant()));
}
runTime.setTime(timeDirs[0], 0);
return timeDirs;
}
Foam::instantList Foam::timeSelector::selectIfPresent
(
Time& runTime,
const argList& args
)
{
if
(
args.optionFound("latestTime")
|| args.optionFound("time")
|| args.optionFound("constant")
|| args.optionFound("noZero")
|| args.optionFound("withZero")
)
{
return select0(runTime, args);
}
else
{
// No timeSelector option specified. Do not change runTime.
return instantList(1, instant(runTime.value(), runTime.timeName()));
}
}
Foam::instantList Foam::timeSelector::select
(
Time& runTime,
const argList& args,
const word& fName
)
{
instantList timeDirs(timeSelector::select0(runTime, args));
if (timeDirs.size() && args.optionFound("newTimes"))
{
List<bool> selectTimes(timeDirs.size(), true);
forAll(timeDirs, timeI)
{
selectTimes[timeI] =
!fileHandler().exists
(
runTime.path()
/timeDirs[timeI].name()
/fName
);
}
return subset(selectTimes, timeDirs);
}
else
{
return timeDirs;
}
}
// ************************************************************************* //