FreeFOAM The Cross-Platform CFD Toolkit
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) 1991-2010 OpenCFD Ltd.
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 <OpenFOAM/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  // check the current time directory
48  if
49  (
50  name.empty()
51  ? isDir(path()/timeName()/dir)
52  :
53  (
54  isFile(path()/timeName()/dir/name)
55  && IOobject(name, timeName(), dir, *this).headerOk()
56  )
57  )
58  {
59  if (debug)
60  {
61  Info<< "Time::findInstance"
62  "(const fileName&, const word&, const IOobject::readOption)"
63  << " : found \"" << name
64  << "\" in " << timeName()/dir
65  << endl;
66  }
67 
68  return timeName();
69  }
70 
71  // Search back through the time directories to find the time
72  // closest to and lower than current time
73 
74  instantList ts = times();
75  label instanceI;
76 
77  for (instanceI = ts.size()-1; instanceI >= 0; --instanceI)
78  {
79  if (ts[instanceI].value() <= timeOutputValue())
80  {
81  break;
82  }
83  }
84 
85  // continue searching from here
86  for (; instanceI >= 0; --instanceI)
87  {
88  if
89  (
90  name.empty()
91  ? isDir(path()/ts[instanceI].name()/dir)
92  :
93  (
94  isFile(path()/ts[instanceI].name()/dir/name)
95  && IOobject(name, ts[instanceI].name(), dir, *this).headerOk()
96  )
97  )
98  {
99  if (debug)
100  {
101  Info<< "Time::findInstance"
102  "(const fileName&, const word&, const IOobject::readOption)"
103  << " : found \"" << name
104  << "\" in " << ts[instanceI].name()/dir
105  << endl;
106  }
107 
108  return ts[instanceI].name();
109  }
110 
111  // Check if hit minimum instance
112  if (ts[instanceI].name() == stopInstance)
113  {
114  if (debug)
115  {
116  Info<< "Time::findInstance"
117  "(const fileName&, const word&"
118  ", const IOobject::readOption, const word&)"
119  << " : hit stopInstance " << stopInstance
120  << endl;
121  }
122 
123  if (rOpt == IOobject::MUST_READ)
124  {
126  (
127  "Time::findInstance"
128  "(const fileName&, const word&"
129  ", const IOobject::readOption, const word&)"
130  ) << "Cannot find file \"" << name << "\" in directory "
131  << dir << " in times " << timeName()
132  << " down to " << stopInstance
133  << exit(FatalError);
134  }
135 
136  return ts[instanceI].name();
137  }
138  }
139 
140  // not in any of the time directories, try constant
141 
142  // Note. This needs to be a hard-coded constant, rather than the
143  // constant function of the time, because the latter points to
144  // the case constant directory in parallel cases
145 
146  if
147  (
148  name.empty()
149  ? isDir(path()/constant()/dir)
150  :
151  (
152  isFile(path()/constant()/dir/name)
153  && IOobject(name, constant(), dir, *this).headerOk()
154  )
155  )
156  {
157  if (debug)
158  {
159  Info<< "Time::findInstance"
160  "(const fileName&, const word&, const IOobject::readOption)"
161  << " : found \"" << name
162  << "\" in " << constant()/dir
163  << endl;
164  }
165 
166  return constant();
167  }
168 
169  if (rOpt == IOobject::MUST_READ)
170  {
172  (
173  "Time::findInstance"
174  "(const fileName&, const word&, const IOobject::readOption)"
175  ) << "Cannot find file \"" << name << "\" in directory "
176  << dir << " in times " << timeName()
177  << " down to " << constant()
178  << exit(FatalError);
179  }
180 
181  return constant();
182 }
183 
184 
185 // ************************ vim: set sw=4 sts=4 et: ************************ //