OpenFOAM logo
The Open Source CFD Toolbox
  Source Guide OpenCFD Solutions Contact OpenFOAM

timeSelector.C

Go to the documentation of this file.
00001 /*---------------------------------------------------------------------------*\
00002   =========                 |
00003   \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
00004    \\    /   O peration     |
00005     \\  /    A nd           | Copyright (C) 1991-2009 OpenCFD Ltd.
00006      \\/     M anipulation  |
00007 -------------------------------------------------------------------------------
00008 License
00009     This file is part of OpenFOAM.
00010 
00011     OpenFOAM is free software; you can redistribute it and/or modify it
00012     under the terms of the GNU General Public License as published by the
00013     Free Software Foundation; either version 2 of the License, or (at your
00014     option) any later version.
00015 
00016     OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
00017     ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
00018     FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
00019     for more details.
00020 
00021     You should have received a copy of the GNU General Public License
00022     along with OpenFOAM; if not, write to the Free Software Foundation,
00023     Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
00024 
00025 \*---------------------------------------------------------------------------*/
00026 
00027 #include "timeSelector.H"
00028 #include "ListOps.H"
00029 #include "argList.H"
00030 #include "Time.H"
00031 #include "IStringStream.H"
00032 
00033 // * * * * * * * * * * * * * * * * Constructors  * * * * * * * * * * * * * * //
00034 
00035 Foam::timeSelector::timeSelector()
00036 :
00037     scalarRanges()
00038 {}
00039 
00040 
00041 Foam::timeSelector::timeSelector(Istream& is)
00042 :
00043     scalarRanges(is)
00044 {}
00045 
00046 
00047 // * * * * * * * * * * * * * * * Member Functions  * * * * * * * * * * * * * //
00048 
00049 bool Foam::timeSelector::selected(const instant& value) const
00050 {
00051     return scalarRanges::selected(value.value());
00052 }
00053 
00054 
00055 Foam::List<bool> Foam::timeSelector::selected(const List<instant>& Times) const
00056 {
00057     List<bool> lst(Times.size(), false);
00058 
00059     // check ranges, avoid false positive on constant/
00060     forAll(Times, timeI)
00061     {
00062         if (Times[timeI].name() != "constant" && selected(Times[timeI]))
00063         {
00064             lst[timeI] = true;
00065         }
00066     }
00067 
00068     // check specific values
00069     forAll(*this, rangeI)
00070     {
00071         if (operator[](rangeI).isExact())
00072         {
00073             scalar target = operator[](rangeI).value();
00074 
00075             int nearestIndex = -1;
00076             scalar nearestDiff = Foam::GREAT;
00077 
00078             forAll(Times, timeI)
00079             {
00080                 if (Times[timeI].name() == "constant") continue;
00081 
00082                 scalar diff = fabs(Times[timeI].value() - target);
00083                 if (diff < nearestDiff)
00084                 {
00085                     nearestDiff = diff;
00086                     nearestIndex = timeI;
00087                 }
00088             }
00089 
00090             if (nearestIndex >= 0)
00091             {
00092                 lst[nearestIndex] = true;
00093             }
00094         }
00095     }
00096 
00097     return lst;
00098 }
00099 
00100 
00101 Foam::List<Foam::instant> Foam::timeSelector::select
00102 (
00103     const List<instant>& Times
00104 ) const
00105 {
00106     return subset(selected(Times), Times);
00107 }
00108 
00109 
00110 void Foam::timeSelector::inplaceSelect
00111 (
00112     List<instant>& Times
00113 ) const
00114 {
00115     inplaceSubset(selected(Times), Times);
00116 }
00117 
00118 
00119 void Foam::timeSelector::addOptions
00120 (
00121     const bool constant,
00122     const bool zeroTime
00123 )
00124 {
00125     if (constant)
00126     {
00127         argList::validOptions.insert("constant", "");
00128     }
00129     if (zeroTime)
00130     {
00131         argList::validOptions.insert("zeroTime", "");
00132     }
00133     argList::validOptions.insert("noZero", "");
00134     argList::validOptions.insert("time", "ranges");
00135     argList::validOptions.insert("latestTime", "");
00136 }
00137 
00138 
00139 Foam::List<Foam::instant> Foam::timeSelector::select
00140 (
00141     const List<instant>& timeDirs,
00142     const argList& args
00143 )
00144 {
00145     if (timeDirs.size())
00146     {
00147         List<bool> selectTimes(timeDirs.size(), true);
00148 
00149         // determine locations of constant/ and 0/ directories
00150         label constantIdx = -1;
00151         label zeroIdx = -1;
00152 
00153         forAll(timeDirs, timeI)
00154         {
00155             if (timeDirs[timeI].name() == "constant")
00156             {
00157                 constantIdx = timeI;
00158             }
00159             else if (timeDirs[timeI].value() == 0)
00160             {
00161                 zeroIdx = timeI;
00162             }
00163 
00164             if (constantIdx >= 0 && zeroIdx >= 0)
00165             {
00166                 break;
00167             }
00168         }
00169 
00170         // determine latestTime selection (if any)
00171         // this must appear before the -time option processing
00172         label latestIdx = -1;
00173         if (args.optionFound("latestTime"))
00174         {
00175             selectTimes = false;
00176             latestIdx = timeDirs.size() - 1;
00177 
00178             // avoid false match on constant/
00179             if (latestIdx == constantIdx)
00180             {
00181                 latestIdx = -1;
00182             }
00183         }
00184 
00185         if (args.optionFound("time"))
00186         {
00187             // can match 0/, but can never match constant/
00188             selectTimes = timeSelector
00189             (
00190                 args.optionLookup("time")()
00191             ).selected(timeDirs);
00192         }
00193 
00194 
00195         // add in latestTime (if selected)
00196         if (latestIdx >= 0)
00197         {
00198             selectTimes[latestIdx] = true;
00199         }
00200 
00201         if (constantIdx >= 0)
00202         {
00203             // only add constant/ if specifically requested
00204             selectTimes[constantIdx] = args.optionFound("constant");
00205         }
00206 
00207         // special treatment for 0/
00208         if (zeroIdx >= 0)
00209         {
00210             if (args.optionFound("noZero"))
00211             {
00212                 // exclude 0/ if specifically requested
00213                 selectTimes[zeroIdx] = false;
00214             }
00215             else if (argList::validOptions.found("zeroTime"))
00216             {
00217                 // with -zeroTime enabled, drop 0/ unless specifically requested
00218                 selectTimes[zeroIdx] = args.optionFound("zeroTime");
00219             }
00220         }
00221 
00222         return subset(selectTimes, timeDirs);
00223     }
00224     else
00225     {
00226         return timeDirs;
00227     }
00228 }
00229 
00230 
00231 Foam::List<Foam::instant> Foam::timeSelector::select0
00232 (
00233     Time& runTime,
00234     const argList& args
00235 )
00236 {
00237     instantList timeDirs = timeSelector::select(runTime.times(), args);
00238 
00239     if (timeDirs.empty())
00240     {
00241         FatalErrorIn(args.executable())
00242             << "No times selected"
00243             << exit(FatalError);
00244     }
00245 
00246     runTime.setTime(timeDirs[0], 0);
00247 
00248     return timeDirs;
00249 }
00250 
00251 
00252 // ************************************************************************* //
Copyright © 2000-2009 OpenCFD Ltd