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

findInstance.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 Description
00025     If "name" is empty: return the location of "directory"
00026     If "name" is not empty: return the location of "directory" containing the
00027     file "name".
00028     Used in reading mesh data.
00029 
00030 \*---------------------------------------------------------------------------*/
00031 
00032 #include "Time.H"
00033 #include <OpenFOAM/IOobject.H>
00034 
00035 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
00036 
00037 Foam::word Foam::Time::findInstance
00038 (
00039     const fileName& dir,
00040     const word& name,
00041     const IOobject::readOption rOpt,
00042     const word& stopInstance
00043 ) const
00044 {
00045     // Note: if name is empty, just check the directory itself
00046 
00047     // check the current time directory
00048     if
00049     (
00050         name.empty()
00051       ? isDir(path()/timeName()/dir)
00052       :
00053         (
00054             isFile(path()/timeName()/dir/name)
00055          && IOobject(name, timeName(), dir, *this).headerOk()
00056         )
00057     )
00058     {
00059         if (debug)
00060         {
00061             Info<< "Time::findInstance"
00062                 "(const fileName&, const word&, const IOobject::readOption)"
00063                 << " : found \"" << name
00064                 << "\" in " << timeName()/dir
00065                 << endl;
00066         }
00067 
00068         return timeName();
00069     }
00070 
00071     // Search back through the time directories to find the time
00072     // closest to and lower than current time
00073 
00074     instantList ts = times();
00075     label instanceI;
00076 
00077     for (instanceI = ts.size()-1; instanceI >= 0; --instanceI)
00078     {
00079         if (ts[instanceI].value() <= timeOutputValue())
00080         {
00081             break;
00082         }
00083     }
00084 
00085     // continue searching from here
00086     for (; instanceI >= 0; --instanceI)
00087     {
00088         if
00089         (
00090             name.empty()
00091           ? isDir(path()/ts[instanceI].name()/dir)
00092           :
00093             (
00094                 isFile(path()/ts[instanceI].name()/dir/name)
00095              && IOobject(name, ts[instanceI].name(), dir, *this).headerOk()
00096             )
00097         )
00098         {
00099             if (debug)
00100             {
00101                 Info<< "Time::findInstance"
00102                     "(const fileName&, const word&, const IOobject::readOption)"
00103                     << " : found \"" << name
00104                     << "\" in " << ts[instanceI].name()/dir
00105                     << endl;
00106             }
00107 
00108             return ts[instanceI].name();
00109         }
00110 
00111         // Check if hit minimum instance
00112         if (ts[instanceI].name() == stopInstance)
00113         {
00114             if (debug)
00115             {
00116                 Info<< "Time::findInstance"
00117                     "(const fileName&, const word&"
00118                     ", const IOobject::readOption, const word&)"
00119                     << " : hit stopInstance " << stopInstance
00120                     << endl;
00121             }
00122 
00123             if (rOpt == IOobject::MUST_READ)
00124             {
00125                 FatalErrorIn
00126                 (
00127                     "Time::findInstance"
00128                     "(const fileName&, const word&"
00129                     ", const IOobject::readOption, const word&)"
00130                 )   << "Cannot find file \"" << name << "\" in directory "
00131                     << dir << " in times " << timeName()
00132                     << " down to " << stopInstance
00133                     << exit(FatalError);
00134             }
00135 
00136             return ts[instanceI].name();
00137         }
00138     }
00139 
00140     // not in any of the time directories, try constant
00141 
00142     // Note. This needs to be a hard-coded constant, rather than the
00143     // constant function of the time, because the latter points to
00144     // the case constant directory in parallel cases
00145 
00146     if
00147     (
00148         name.empty()
00149       ? isDir(path()/constant()/dir)
00150       :
00151         (
00152             isFile(path()/constant()/dir/name)
00153          && IOobject(name, constant(), dir, *this).headerOk()
00154         )
00155     )
00156     {
00157         if (debug)
00158         {
00159             Info<< "Time::findInstance"
00160                 "(const fileName&, const word&, const IOobject::readOption)"
00161                 << " : found \"" << name
00162                 << "\" in " << constant()/dir
00163                 << endl;
00164         }
00165 
00166         return constant();
00167     }
00168 
00169     if (rOpt == IOobject::MUST_READ)
00170     {
00171         FatalErrorIn
00172         (
00173             "Time::findInstance"
00174             "(const fileName&, const word&, const IOobject::readOption)"
00175         )   << "Cannot find file \"" << name << "\" in directory "
00176             << dir << " in times " << timeName()
00177             << " down to " << constant()
00178             << exit(FatalError);
00179     }
00180 
00181     return constant();
00182 }
00183 
00184 
00185 // ************************ vim: set sw=4 sts=4 et: ************************ //
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines