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-2018 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 argList& args,
71  const word& systemName,
72  const word& constantName
73 )
74 :
75  processorCase_(args.runControl().parRun()),
76  distributed_(args.runControl().distributed()),
77  rootPath_(args.rootPath()),
78  globalCaseName_(args.globalCaseName()),
79  case_(args.caseName()),
80  system_(systemName),
81  constant_(constantName)
82 {
83  // For convenience: find out from case name whether it is a
84  // processor directory and set processorCase flag so file searching
85  // goes up one level.
86  detectProcessorCase();
87 }
88 
89 
91 (
92  const fileName& rootPath,
93  const fileName& caseName,
94  const word& systemName,
95  const word& constantName
96 )
97 :
98  processorCase_(false),
99  distributed_(false),
100  rootPath_(rootPath),
101  globalCaseName_(caseName),
102  case_(caseName),
103  system_(systemName),
104  constant_(constantName)
105 {
106  // Find out from case name whether a processor directory
107  detectProcessorCase();
108 }
109 
110 
112 (
113  const bool processorCase,
114  const fileName& rootPath,
115  const bool distributed,
116  const fileName& globalCaseName,
117  const fileName& caseName,
118  const word& systemName,
119  const word& constantName
120 )
121 :
122  processorCase_(processorCase),
123  distributed_(distributed),
124  rootPath_(rootPath),
125  globalCaseName_(globalCaseName),
126  case_(caseName),
127  system_(systemName),
128  constant_(constantName)
129 {
130  // For convenience: find out from case name whether it is a
131  // processor directory and set processorCase flag so file searching
132  // goes up one level.
133  detectProcessorCase();
134 }
135 
136 
137 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
138 
140 (
141  const fileName& directory,
142  const word& constantName
143 )
144 {
145  return fileHandler().findTimes(directory, constantName);
146 }
147 
148 
150 {
151  return findTimes(path(), constant());
152 }
153 
154 
156 (
157  const instantList& timeDirs,
158  const scalar t,
159  const word& constantName
160 )
161 {
162  const label nTimes = timeDirs.size();
163 
164  label nearestIndex = -1;
165  scalar deltaT = GREAT;
166 
167  for (label timei=0; timei < nTimes; ++timei)
168  {
169  if (timeDirs[timei].name() == constantName) continue;
170 
171  const scalar diff = mag(timeDirs[timei].value() - t);
172  if (diff < deltaT)
173  {
174  deltaT = diff;
175  nearestIndex = timei;
176  }
177  }
178 
179  return nearestIndex;
180 }
181 
182 
184 {
185  instantList timeDirs = findTimes(path(), constant());
186 
187  const label nTimes = timeDirs.size();
188 
189  // There is only one time (likely "constant") so return it
190  if (nTimes == 1)
191  {
192  return timeDirs.first();
193  }
194 
195  if (t < timeDirs[1].value())
196  {
197  return timeDirs[1];
198  }
199  else if (t > timeDirs.last().value())
200  {
201  return timeDirs.last();
202  }
203 
204  label nearestIndex = -1;
205  scalar deltaT = GREAT;
206 
207  for (label timei=1; timei < nTimes; ++timei)
208  {
209  const scalar diff = mag(timeDirs[timei].value() - t);
210  if (diff < deltaT)
211  {
212  deltaT = diff;
213  nearestIndex = timei;
214  }
215  }
216 
217  return timeDirs[nearestIndex];
218 }
219 
220 
221 // ************************************************************************* //
Foam::TimePaths::findTimes
static instantList findTimes(const fileName &directory, const word &constantName="constant")
Search a given directory for valid time directories.
Definition: TimePaths.C:140
Foam::TimePaths::findClosestTimeIndex
static label findClosestTimeIndex(const instantList &timeDirs, const scalar t, const word &constantName="constant")
Search instantList for the time index closest to the specified time.
Definition: TimePaths.C:156
IOstreams.H
Useful combination of include files which define Sin, Sout and Serr and the use of IO streams general...
Foam::word
A class for handling words, derived from Foam::string.
Definition: word.H:65
Foam::fileName
A class for handling file names.
Definition: fileName.H:73
TimePaths.H
Foam::TimePaths::TimePaths
TimePaths(const argList &args, const word &systemName="system", const word &constantName="constant")
Construct using characteristics given by the argList.
Definition: TimePaths.C:69
Foam::ParRunControl::parRun
bool parRun() const noexcept
True if this is a parallel run.
Definition: parRun.H:98
Foam::argList::caseName
const fileName & caseName() const noexcept
Return case name (parallel run) or global case (serial run)
Definition: argListI.H:69
Foam::argList::globalCaseName
const fileName & globalCaseName() const noexcept
Return global case name.
Definition: argListI.H:75
Foam::argList
Extract command arguments and options from the supplied argc and argv parameters.
Definition: argList.H:123
Foam::fileHandler
const fileOperation & fileHandler()
Get current file handler.
Definition: fileOperation.C:1485
Foam::fileOperation::findTimes
virtual instantList findTimes(const fileName &, const word &) const
Get sorted list of times.
Definition: fileOperation.C:949
Foam::diff
scalar diff(const triad &A, const triad &B)
Return a quantity of the difference between two triads.
Definition: triad.C:378
argList.H
Foam::TimePaths::times
instantList times() const
Search the case for valid time directories.
Definition: TimePaths.C:149
Foam::ParRunControl::distributed
bool distributed() const noexcept
True if this is a parallel run and uses distributed roots.
Definition: parRun.H:104
Foam::TimePaths::findClosestTime
instant findClosestTime(const scalar t) const
Search the case for the time closest to the given time.
Definition: TimePaths.C:183
Foam::argList::runControl
const ParRunControl & runControl() const noexcept
Return the run control (parallel, dry-run etc)
Definition: argListI.H:104
fileOperation.H
Foam::List< instant >
Foam::mag
dimensioned< typename typeOfMag< Type >::type > mag(const dimensioned< Type > &dt)
path
fileName path(UMean.rootPath()/UMean.caseName()/"graphs"/UMean.instance())
Foam::name
word name(const expressions::valueTypeCode typeCode)
A word representation of a valueTypeCode. Empty for INVALID.
Definition: exprTraits.C:59
Foam::instant
An instant of time. Contains the time value and name.
Definition: instant.H:52
args
Foam::argList args(argc, argv)
constant
constant condensation/saturation model.
Foam::argList::rootPath
const fileName & rootPath() const noexcept
Return root path.
Definition: argListI.H:63
Foam::pos
dimensionedScalar pos(const dimensionedScalar &ds)
Definition: dimensionedScalar.C:177