findInstance.C
Go to the documentation of this file.
1 /*---------------------------------------------------------------------------*\
2  ========= |
3  \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
4  \\ / O peration |
5  \\ / A nd | Copyright (C) 2011-2015 OpenFOAM Foundation
6  \\/ M anipulation |
7 -------------------------------------------------------------------------------
8 License
9  This file is part of OpenFOAM.
10 
11  OpenFOAM is free software: you can redistribute it and/or modify it
12  under the terms of the GNU General Public License as published by
13  the Free Software Foundation, either version 3 of the License, or
14  (at your option) any later version.
15 
16  OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
17  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
18  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
19  for more details.
20 
21  You should have received a copy of the GNU General Public License
22  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
23 
24 Description
25  If "name" is empty: return the location of "directory"
26  If "name" is not empty: return the location of "directory" containing the
27  file "name".
28  Used in reading mesh data.
29 
30 \*---------------------------------------------------------------------------*/
31 
32 #include "Time.H"
33 #include "IOobject.H"
34 
35 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
36 
38 (
39  const fileName& dir,
40  const word& name,
41  const IOobject::readOption rOpt,
42  const word& stopInstance
43 ) const
44 {
45  // Note: if name is empty, just check the directory itself
46 
47 
48  const fileName tPath(path());
49  const fileName dirPath(tPath/timeName()/dir);
50 
51  // check the current time directory
52  if
53  (
54  name.empty()
55  ? isDir(dirPath)
56  :
57  (
58  isFile(dirPath/name)
59  && IOobject(name, timeName(), dir, *this).headerOk()
60  )
61  )
62  {
63  if (debug)
64  {
65  Info<< "Time::findInstance"
66  "(const fileName&, const word&"
67  ", const IOobject::readOption, const word&)"
68  << " : found \"" << name
69  << "\" in " << timeName()/dir
70  << endl;
71  }
72 
73  return timeName();
74  }
75 
76  // Search back through the time directories to find the time
77  // closest to and lower than current time
78 
79  instantList ts = times();
80  label instanceI;
81 
82  for (instanceI = ts.size()-1; instanceI >= 0; --instanceI)
83  {
84  if (ts[instanceI].value() <= timeOutputValue())
85  {
86  break;
87  }
88  }
89 
90  // continue searching from here
91  for (; instanceI >= 0; --instanceI)
92  {
93  if
94  (
95  name.empty()
96  ? isDir(tPath/ts[instanceI].name()/dir)
97  :
98  (
99  isFile(tPath/ts[instanceI].name()/dir/name)
100  && IOobject(name, ts[instanceI].name(), dir, *this).headerOk()
101  )
102  )
103  {
104  if (debug)
105  {
106  Info<< "Time::findInstance"
107  "(const fileName&, const word&"
108  ", const IOobject::readOption, const word&)"
109  << " : found \"" << name
110  << "\" in " << ts[instanceI].name()/dir
111  << endl;
112  }
113 
114  return ts[instanceI].name();
115  }
116 
117  // Check if hit minimum instance
118  if (ts[instanceI].name() == stopInstance)
119  {
120  if (debug)
121  {
122  Info<< "Time::findInstance"
123  "(const fileName&, const word&"
124  ", const IOobject::readOption, const word&)"
125  << " : hit stopInstance " << stopInstance
126  << endl;
127  }
128 
129  if
130  (
131  rOpt == IOobject::MUST_READ
132  || rOpt == IOobject::MUST_READ_IF_MODIFIED
133  )
134  {
135  if (name.empty())
136  {
138  << "Cannot find directory "
139  << dir << " in times " << timeName()
140  << " down to " << stopInstance
141  << exit(FatalError);
142  }
143  else
144  {
146  << "Cannot find file \"" << name << "\" in directory "
147  << dir << " in times " << timeName()
148  << " down to " << stopInstance
149  << exit(FatalError);
150  }
151  }
152 
153  return ts[instanceI].name();
154  }
155  }
156 
157 
158  // not in any of the time directories, try constant
159 
160  // Note. This needs to be a hard-coded constant, rather than the
161  // constant function of the time, because the latter points to
162  // the case constant directory in parallel cases
163 
164  if
165  (
166  name.empty()
167  ? isDir(tPath/constant()/dir)
168  :
169  (
170  isFile(tPath/constant()/dir/name)
171  && IOobject(name, constant(), dir, *this).headerOk()
172  )
173  )
174  {
175  if (debug)
176  {
177  Info<< "Time::findInstance"
178  "(const fileName&, const word&"
179  ", const IOobject::readOption, const word&)"
180  << " : found \"" << name
181  << "\" in " << constant()/dir
182  << endl;
183  }
184 
185  return constant();
186  }
187 
188  if (rOpt == IOobject::MUST_READ || rOpt == IOobject::MUST_READ_IF_MODIFIED)
189  {
191  << "Cannot find file \"" << name << "\" in directory "
192  << dir << " in times " << timeName()
193  << " down to " << constant()
194  << exit(FatalError);
195  }
196 
197  return constant();
198 }
199 
200 
201 // ************************************************************************* //
Foam::IOobject
IOobject defines the attributes of an object for which implicit objectRegistry management is supporte...
Definition: IOobject.H:91
Foam::word
A class for handling words, derived from string.
Definition: word.H:59
Foam::fileName
A class for handling file names.
Definition: fileName.H:69
Time.H
Foam::endl
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:251
constant
Constant dispersed-phase particle diameter model.
Foam::label
intWM_LABEL_SIZE_t label
A label is an int32_t or int64_t as specified by the pre-processor macro WM_LABEL_SIZE.
Definition: label.H:59
Foam::Info
messageStream Info
IOobject.H
Foam::FatalError
error FatalError
Foam::isFile
bool isFile(const fileName &, const bool checkGzip=true)
Does the name exist as a FILE in the file system?
Definition: POSIX.C:622
Foam::isDir
bool isDir(const fileName &)
Does the name exist as a DIRECTORY in the file system?
Definition: POSIX.C:615
Foam::Time::findInstance
word findInstance(const fileName &dir, const word &name=word::null, const IOobject::readOption rOpt=IOobject::MUST_READ, const word &stopInstance=word::null) const
Return the location of "dir" containing the file "name".
Definition: findInstance.C:38
Foam::exit
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:124
FatalErrorInFunction
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:318
Foam::List
A 1D array of objects of type <T>, where the size of the vector is known and used for subscript bound...
Definition: HashTable.H:59
path
fileName path(UMean.rootPath()/UMean.caseName()/"graphs"/UMean.instance())
timeName
word timeName
Definition: getTimeIndex.H:3
Foam::IOobject::readOption
readOption
Enumeration defining the read options.
Definition: IOobject.H:106
Foam::List::size
void size(const label)
Override size to be inconsistent with allocated storage.
Foam::name
word name(const complex &)
Return a string representation of a complex.
Definition: complex.C:47