FreeFOAM The Cross-Platform CFD Toolkit
Hosted by SourceForge:
Get FreeFOAM at SourceForge.net.
            Fast, secure and Free Open Source software downloads

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-2010 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
00013     the Free Software Foundation, either version 3 of the License, or
00014     (at your 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, see <http://www.gnu.org/licenses/>.
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 // * * * * * * * * * * * * * * * * Constructors  * * * * * * * * * * * * * * //
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 // * * * * * * * * * * * * * * * Member Functions  * * * * * * * * * * * * * //
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     // check ranges, avoid false positive on constant/
00059     forAll(Times, timeI)
00060     {
00061         if (Times[timeI].name() != "constant" && selected(Times[timeI]))
00062         {
00063             lst[timeI] = true;
00064         }
00065     }
00066 
00067     // check specific values
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         // determine locations of constant/ and 0/ directories
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         // determine latestTime selection (if any)
00170         // this must appear before the -time option processing
00171         label latestIdx = -1;
00172         if (args.optionFound("latestTime"))
00173         {
00174             selectTimes = false;
00175             latestIdx = timeDirs.size() - 1;
00176 
00177             // avoid false match on constant/
00178             if (latestIdx == constantIdx)
00179             {
00180                 latestIdx = -1;
00181             }
00182         }
00183 
00184         if (args.optionFound("time"))
00185         {
00186             // can match 0/, but can never match constant/
00187             selectTimes = timeSelector
00188             (
00189                 args.optionLookup("time")()
00190             ).selected(timeDirs);
00191         }
00192 
00193 
00194         // add in latestTime (if selected)
00195         if (latestIdx >= 0)
00196         {
00197             selectTimes[latestIdx] = true;
00198         }
00199 
00200         if (constantIdx >= 0)
00201         {
00202             // only add constant/ if specifically requested
00203             selectTimes[constantIdx] = args.optionFound("constant");
00204         }
00205 
00206         // special treatment for 0/
00207         if (zeroIdx >= 0)
00208         {
00209             if (args.optionFound("noZero"))
00210             {
00211                 // exclude 0/ if specifically requested
00212                 selectTimes[zeroIdx] = false;
00213             }
00214             else if (argList::validOptions.found("zeroTime"))
00215             {
00216                 // with -zeroTime enabled, drop 0/ unless specifically requested
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 // ************************ vim: set sw=4 sts=4 et: ************************ //
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines