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

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-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 Description
00026     If "name" is empty: return the location of "directory"
00027     If "name" is not empty: return the location of "directory" containing the
00028     file "name".
00029     Used in reading mesh data.
00030 
00031 \*---------------------------------------------------------------------------*/
00032 
00033 #include "Time.H"
00034 #include "IOobject.H"
00035 
00036 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
00037 
00038 Foam::word Foam::Time::findInstance
00039 (
00040     const fileName& dir,
00041     const word& name,
00042     const IOobject::readOption rOpt
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 
00112 
00113     // not in any of the time directories, try constant
00114 
00115     // Note. This needs to be a hard-coded constant, rather than the
00116     // constant function of the time, because the latter points to
00117     // the case constant directory in parallel cases
00118 
00119     if
00120     (
00121         name.empty()
00122       ? isDir(path()/constant()/dir)
00123       :
00124         (
00125             isFile(path()/constant()/dir/name)
00126          && IOobject(name, constant(), dir, *this).headerOk()
00127         )
00128     )
00129     {
00130         if (debug)
00131         {
00132             Info<< "Time::findInstance"
00133                 "(const fileName&, const word&, const IOobject::readOption)"
00134                 << " : found \"" << name
00135                 << "\" in " << constant()/dir
00136                 << endl;
00137         }
00138 
00139         return constant();
00140     }
00141 
00142     if (rOpt == IOobject::MUST_READ)
00143     {
00144         FatalErrorIn
00145         (
00146             "Time::findInstance"
00147             "(const fileName&, const word&, const IOobject::readOption)"
00148         )   << "Cannot find file \"" << name << "\" in directory "
00149             << constant()/dir
00150             << exit(FatalError);
00151     }
00152 
00153     return constant();
00154 }
00155 
00156 
00157 // ************************************************************************* //
Copyright © 2000-2009 OpenCFD Ltd