Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026 #include "timeSelector.H"
00027 #include <OpenFOAM/ListOps.H>
00028 #include <OpenFOAM/argList.H>
00029 #include "Time.H"
00030 #include <OpenFOAM/IStringStream.H>
00031
00032
00033
00034 Foam::timeSelector::timeSelector()
00035 :
00036 scalarRanges()
00037 {}
00038
00039
00040 Foam::timeSelector::timeSelector(Istream& is)
00041 :
00042 scalarRanges(is)
00043 {}
00044
00045
00046
00047
00048 bool Foam::timeSelector::selected(const instant& value) const
00049 {
00050 return scalarRanges::selected(value.value());
00051 }
00052
00053
00054 Foam::List<bool> Foam::timeSelector::selected(const List<instant>& Times) const
00055 {
00056 List<bool> lst(Times.size(), false);
00057
00058
00059 forAll(Times, timeI)
00060 {
00061 if (Times[timeI].name() != "constant" && selected(Times[timeI]))
00062 {
00063 lst[timeI] = true;
00064 }
00065 }
00066
00067
00068 forAll(*this, rangeI)
00069 {
00070 if (operator[](rangeI).isExact())
00071 {
00072 scalar target = operator[](rangeI).value();
00073
00074 int nearestIndex = -1;
00075 scalar nearestDiff = Foam::GREAT;
00076
00077 forAll(Times, timeI)
00078 {
00079 if (Times[timeI].name() == "constant") continue;
00080
00081 scalar diff = fabs(Times[timeI].value() - target);
00082 if (diff < nearestDiff)
00083 {
00084 nearestDiff = diff;
00085 nearestIndex = timeI;
00086 }
00087 }
00088
00089 if (nearestIndex >= 0)
00090 {
00091 lst[nearestIndex] = true;
00092 }
00093 }
00094 }
00095
00096 return lst;
00097 }
00098
00099
00100 Foam::List<Foam::instant> Foam::timeSelector::select
00101 (
00102 const List<instant>& Times
00103 ) const
00104 {
00105 return subset(selected(Times), Times);
00106 }
00107
00108
00109 void Foam::timeSelector::inplaceSelect
00110 (
00111 List<instant>& Times
00112 ) const
00113 {
00114 inplaceSubset(selected(Times), Times);
00115 }
00116
00117
00118 void Foam::timeSelector::addOptions
00119 (
00120 const bool constant,
00121 const bool zeroTime
00122 )
00123 {
00124 if (constant)
00125 {
00126 argList::validOptions.insert("constant", "");
00127 }
00128 if (zeroTime)
00129 {
00130 argList::validOptions.insert("zeroTime", "");
00131 }
00132 argList::validOptions.insert("noZero", "");
00133 argList::validOptions.insert("time", "ranges");
00134 argList::validOptions.insert("latestTime", "");
00135 }
00136
00137
00138 Foam::List<Foam::instant> Foam::timeSelector::select
00139 (
00140 const List<instant>& timeDirs,
00141 const argList& args
00142 )
00143 {
00144 if (timeDirs.size())
00145 {
00146 List<bool> selectTimes(timeDirs.size(), true);
00147
00148
00149 label constantIdx = -1;
00150 label zeroIdx = -1;
00151
00152 forAll(timeDirs, timeI)
00153 {
00154 if (timeDirs[timeI].name() == "constant")
00155 {
00156 constantIdx = timeI;
00157 }
00158 else if (timeDirs[timeI].value() == 0)
00159 {
00160 zeroIdx = timeI;
00161 }
00162
00163 if (constantIdx >= 0 && zeroIdx >= 0)
00164 {
00165 break;
00166 }
00167 }
00168
00169
00170
00171 label latestIdx = -1;
00172 if (args.optionFound("latestTime"))
00173 {
00174 selectTimes = false;
00175 latestIdx = timeDirs.size() - 1;
00176
00177
00178 if (latestIdx == constantIdx)
00179 {
00180 latestIdx = -1;
00181 }
00182 }
00183
00184 if (args.optionFound("time"))
00185 {
00186
00187 selectTimes = timeSelector
00188 (
00189 args.optionLookup("time")()
00190 ).selected(timeDirs);
00191 }
00192
00193
00194
00195 if (latestIdx >= 0)
00196 {
00197 selectTimes[latestIdx] = true;
00198 }
00199
00200 if (constantIdx >= 0)
00201 {
00202
00203 selectTimes[constantIdx] = args.optionFound("constant");
00204 }
00205
00206
00207 if (zeroIdx >= 0)
00208 {
00209 if (args.optionFound("noZero"))
00210 {
00211
00212 selectTimes[zeroIdx] = false;
00213 }
00214 else if (argList::validOptions.found("zeroTime"))
00215 {
00216
00217 selectTimes[zeroIdx] = args.optionFound("zeroTime");
00218 }
00219 }
00220
00221 return subset(selectTimes, timeDirs);
00222 }
00223 else
00224 {
00225 return timeDirs;
00226 }
00227 }
00228
00229
00230 Foam::List<Foam::instant> Foam::timeSelector::select0
00231 (
00232 Time& runTime,
00233 const argList& args
00234 )
00235 {
00236 instantList timeDirs = timeSelector::select(runTime.times(), args);
00237
00238 if (timeDirs.empty())
00239 {
00240 FatalErrorIn(args.executable())
00241 << "No times selected"
00242 << exit(FatalError);
00243 }
00244
00245 runTime.setTime(timeDirs[0], 0);
00246
00247 return timeDirs;
00248 }
00249
00250
00251