TimePaths.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 | www.openfoam.com
6  \\/ M anipulation |
7 -------------------------------------------------------------------------------
8  Copyright (C) 2011-2013 OpenFOAM Foundation
9  Copyright (C) 2016-2025 OpenCFD Ltd.
10 -------------------------------------------------------------------------------
11 License
12  This file is part of OpenFOAM.
13 
14  OpenFOAM is free software: you can redistribute it and/or modify it
15  under the terms of the GNU General Public License as published by
16  the Free Software Foundation, either version 3 of the License, or
17  (at your option) any later version.
18 
19  OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
20  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
21  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
22  for more details.
23 
24  You should have received a copy of the GNU General Public License
25  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
26 
27 \*---------------------------------------------------------------------------*/
28 
29 #include "TimePaths.H"
30 #include "argList.H"
31 #include "fileOperation.H"
32 #include "IOstreams.H"
33 
34 // * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * //
35 
36 bool Foam::TimePaths::detectProcessorCase()
37 {
38  if (processorCase_)
39  {
40  return processorCase_;
41  }
42 
43  // Look for "processor", but should really check for following digits too
44  const auto sep = globalCaseName_.rfind('/');
45  const auto pos = globalCaseName_.find
46  (
47  "processor",
48  (sep == string::npos ? 0 : sep)
49  );
50 
51  if (pos == 0)
52  {
53  globalCaseName_ = ".";
54  processorCase_ = true;
55  }
56  else if (pos != string::npos && sep != string::npos && sep == pos-1)
57  {
58  globalCaseName_.resize(sep);
59  processorCase_ = true;
60  }
61 
62  return processorCase_;
63 }
64 
65 
66 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
67 
69 (
70  const bool processorCase,
71  const fileName& rootPath,
72  const bool distributed,
73  const fileName& globalCaseName,
74  const fileName& caseName,
75  const word& systemDirName,
76  const word& constantDirName
77 )
78 :
79  processorCase_(processorCase),
80  distributed_(distributed),
81  rootPath_(rootPath),
82  globalCaseName_(globalCaseName),
83  caseName_(caseName),
84  system_(systemDirName),
85  constant_(constantDirName)
86 {
87  // Find out from case name whether it is a processor directory
88  // and set processorCase flag so file searching goes up one level.
89  detectProcessorCase();
90 }
91 
92 
94 (
95  const argList& args,
96  const word& systemDirName,
97  const word& constantDirName
98 )
99 :
100  TimePaths
101  (
102  args.runControl().parRun(), // processorCase
103  args.rootPath(),
104  args.runControl().distributed(),
105  args.globalCaseName(),
106  args.caseName(),
107  systemDirName,
108  constantDirName
109  )
110 {}
111 
112 
113 // * * * * * * * * * * * * * Static Member Functions * * * * * * * * * * * * //
114 
116 (
117  const fileName& directory,
118  const word& constantDirName
119 )
120 {
121  return fileHandler().findTimes(directory, constantDirName);
122 }
123 
124 
126 (
127  const UList<instant>& timeDirs,
128  const scalar t,
129  const word& constantDirName
130 )
131 {
132  const label nTimes = timeDirs.size();
133 
134  label nearestIndex = -1;
135  scalar deltaT = GREAT;
136 
137  for (label timei=0; timei < nTimes; ++timei)
138  {
139  if (timeDirs[timei].name() == constantDirName) continue;
140 
141  const scalar diff = Foam::mag(timeDirs[timei].value() - t);
142  if (diff < deltaT)
143  {
144  deltaT = diff;
145  nearestIndex = timei;
146  }
147  }
148 
149  return nearestIndex;
150 }
151 
152 
154 (
155  const UList<instant>& timeDirs,
156  const scalar t,
157  const word& constantDirName
158 )
159 {
160  const label nTimes = timeDirs.size();
161 
162  if (nTimes == 0)
163  {
164  // Cannot really fail at this point, but for some safety...
165  return instant(0, constantDirName);
166  }
167  else if (nTimes == 1)
168  {
169  // Only one time (likely "constant") so return it
170  return timeDirs[0];
171  }
172  else if (t < timeDirs[1].value())
173  {
174  return timeDirs[1];
175  }
176  else if (t > timeDirs[nTimes-1].value())
177  {
178  return timeDirs[nTimes-1];
179  }
180 
181  label nearestIndex = 0; // Failsafe value
182  scalar deltaT = GREAT;
183 
184  for (label timei=1; timei < nTimes; ++timei)
185  {
186  const scalar diff = Foam::mag(timeDirs[timei].value() - t);
187  if (diff < deltaT)
188  {
189  deltaT = diff;
190  nearestIndex = timei;
191  }
192  }
193 
194  return timeDirs[nearestIndex];
195 }
196 
197 
199 (
200  const UList<instant>& timeDirs,
201  const instant& t
202 )
203 {
204  // Note:
205  // - timeDirs will include constant (with value 0) as first element.
206  // For backwards compatibility make sure to find 0 in preference
207  // to constant.
208  // - list is sorted so could use binary search
209 
210  forAllReverse(timeDirs, i)
211  {
212  if (t.equal(timeDirs[i].value()))
213  {
214  return timeDirs[i].name();
215  }
216  }
217 
218  return word();
219 }
220 
221 
223 (
224  const fileName& directory,
225  const instant& t,
226  const word& constantDirName
227 )
228 {
229  // NB: uses fileHandler()
230  instantList timeDirs = findTimes(directory, constantDirName);
231  return findInstancePath(timeDirs, t);
232 }
233 
234 
235 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
236 
238 {
239  // NB: uses fileHandler()
240  return findTimes(path(), constant());
241 }
242 
243 
245 {
246  // NB: uses fileHandler()
247  return findTimes(path(layout), constant());
248 }
249 
250 
252 {
253  // NB: uses fileHandler()
254  return findClosestTime
255  (
256  findTimes(path(), constant()),
257  t,
258  constant()
259  );
260 }
261 
262 
264 (
265  IOobjectOption::Layout layout,
266  const scalar t
267 ) const
268 {
269  // NB: uses fileHandler()
270  return findClosestTime
271  (
272  findTimes(path(layout), constant()),
273  t,
274  constant()
275  );
276 }
277 
278 
280 {
281  return findInstancePath(path(), t);
282 }
283 
284 
286 (
287  IOobjectOption::Layout layout,
288  const instant& t
289 ) const
290 {
291  return findInstancePath(path(layout), t);
292 }
293 
294 
295 // ************************************************************************* //
scalar diff(const triad &A, const triad &B)
Return a quantity of the difference between two triads.
Definition: triad.C:373
void size(const label n)
Older name for setAddressableSize.
Definition: UList.H:119
Address the time paths without using the Time class.
Definition: TimePaths.H:52
A class for handling file names.
Definition: fileName.H:72
dimensioned< typename typeOfMag< Type >::type > mag(const dimensioned< Type > &dt)
static instant findClosestTime(const UList< instant > &timeDirs, const scalar t, const word &constantDirName="constant")
Search instant list for the instant closest to the specified time.
Definition: TimePaths.C:147
refPtr< fileOperation > fileHandler(std::nullptr_t)
Delete current file handler - forwards to fileOperation::handler()
static label findClosestTimeIndex(const UList< instant > &timeDirs, const scalar t, const word &constantDirName="constant")
Search instant list for the time index closest to the specified time.
Definition: TimePaths.C:119
Useful combination of include files which define Sin, Sout and Serr and the use of IO streams general...
dimensionedScalar pos(const dimensionedScalar &ds)
word name(const expressions::valueTypeCode typeCode)
A word representation of a valueTypeCode. Empty for expressions::valueTypeCode::INVALID.
Definition: exprTraits.C:127
A class for handling words, derived from Foam::string.
Definition: word.H:63
Extract command arguments and options from the supplied argc and argv parameters. ...
Definition: argList.H:118
static word findInstancePath(const UList< instant > &timeDirs, const instant &t)
Search instantList for matching time value, return the instance name or word::null if nothing is equa...
Definition: TimePaths.C:192
A 1D vector of objects of type <T>, where the size of the vector is known and can be used for subscri...
Definition: HashTable.H:105
fileName path(UMean.rootPath()/UMean.caseName()/"graphs"/UMean.instance())
Expression::UniformListWrap< scalar > constant
Wrap of constant as a list expression.
static instantList findTimes(const fileName &directory, const word &constantDirName="constant")
Search a given directory for valid time directories.
Definition: TimePaths.C:109
An instant of time. Contains the time value and name. Uses Foam::Time when formatting the name...
Definition: instant.H:53
instantList times() const
Search the case for valid time directories.
Definition: TimePaths.C:230
TimePaths(const bool processorCase, const fileName &rootPath, const bool distributed, const fileName &globalCaseName, const fileName &caseName, const word &systemDirName="system", const word &constantDirName="constant")
Construct from all components.
Definition: TimePaths.C:62
Layout
The layout of the case structure.
#define forAllReverse(list, i)
Reverse loop across all elements in list.
Definition: stdFoam.H:416
Foam::argList args(argc, argv)