foamListTimes.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-2016 OpenFOAM Foundation
9  Copyright (C) 2016-2021 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 Application
28  foamListTimes
29 
30 Group
31  grpPostProcessingUtilities
32 
33 Description
34  List times using the timeSelector, or use to remove selected time
35  directories.
36 
37 Usage
38  \b foamListTimes [OPTION]
39 
40  Options:
41  - \par -processor
42  Times from processor0/ directory
43 
44  - \par -rm
45  Remove selected time directories
46 
47  - \par -verbose
48  Report progress during removal
49 
50 Note
51  The OpenFOAM banner information is suppressed so that the output can be
52  piped into another command.
53 
54 \*---------------------------------------------------------------------------*/
55 
56 #include "argList.H"
57 #include "autoPtr.H"
58 #include "profiling.H"
59 #include "timeSelector.H"
60 #include "TimePaths.H"
61 #include "ListOps.H"
62 #include "stringOps.H"
63 
64 using namespace Foam;
65 
66 // Many ways to name processor directories
67 //
68 // Uncollated | "processor0", "processor1" ...
69 // Collated | "processors<N>"
70 // Host collated | "processors<N>_<low>-<high>"
71 
72 const regExp matcher("processors?[0-9]+(_[0-9]+-[0-9]+)?");
73 
74 bool isProcessorDir(const string& dir)
75 {
76  return (dir.starts_with("processor") && matcher.match(dir));
77 }
78 
79 
80 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
81 
82 int main(int argc, char *argv[])
83 {
85  (
86  "List times using the timeSelector,"
87  " or use to remove selected time directories"
88  );
89  timeSelector::addOptions(true, true); // constant(true), zero(true)
93  argList::noFunctionObjects(); // Never use function objects
95  (
96  "processor",
97  "List times from processor0/ directory"
98  );
100  (
101  "rm",
102  "Remove selected time directories"
103  );
105  (
106  "Report progress of -rm option"
107  );
108  profiling::disable(); // Disable profiling (and its output)
109 
110  #include "setRootCase.H"
111 
112  const bool removeFiles(args.found("rm"));
113  bool verbose(args.verbose());
114 
115 
116  // Get times list from the master processor and subset based on
117  // command-line options
118 
119  label nProcs = 0;
120  autoPtr<TimePaths> timePaths;
121 
122  if (args.found("processor"))
123  {
124  // Determine the processor count
125  nProcs = fileHandler().nProcs(args.path());
126 
127  if (!nProcs)
128  {
130  << "No processor* directories found"
131  << exit(FatalError);
132  }
133 
134  // Obtain time directory names from "processor0/" only
135  timePaths = autoPtr<TimePaths>::New
136  (
137  args.rootPath(),
138  args.caseName()/"processor0"
139  );
140  }
141  else
142  {
143  timePaths = autoPtr<TimePaths>::New
144  (
145  args.rootPath(),
146  args.caseName()
147  );
148  }
149 
150 
151  const instantList timeDirs(timeSelector::select(timePaths->times(), args));
152 
153  const label nTimes = timeDirs.size();
154 
155  if (removeFiles)
156  {
157  if (nProcs)
158  {
159  fileNameList procDirs
160  (
162  (
163  args.path(),
165  false, // No gzip anyhow
166  false // Do not follow linkts
167  )
168  );
169 
170  inplaceSubsetList(procDirs, isProcessorDir);
171 
172  // Perhaps not needed
174 
175  if (verbose)
176  {
177  InfoErr
178  << "Removing " << nTimes
179  << " times in " << procDirs.size()
180  << " processor directories" << endl;
181  }
182 
183  // No processor directories? - silence verbosity
184  if (procDirs.empty())
185  {
186  verbose = false;
187  }
188 
189  forAllReverse(timeDirs, timei)
190  {
191  const word& timeName = timeDirs[timei].name();
192 
193  if (verbose)
194  {
195  InfoErr
196  << " rm " << timeName
197  << " [" << (nTimes - timei) << '/' << nTimes << ']'
198  << endl;
199  }
200 
201  for (const fileName& procDir : procDirs)
202  {
203  rmDir(args.path()/procDir/timeName, true);
204  }
205  }
206  }
207  else
208  {
209  if (verbose)
210  {
211  InfoErr
212  << "Removing " << nTimes
213  << " time directories" << endl;
214  }
215 
216  forAllReverse(timeDirs, timei)
217  {
218  const word& timeName = timeDirs[timei].name();
219 
220  if (verbose)
221  {
222  InfoErr
223  << " rm " << timeName
224  << " [" << (nTimes - timei) << '/' << nTimes << ']'
225  << endl;
226  }
227 
228  rmDir(args.path()/timeName, true);
229  }
230  }
231  }
232  else
233  {
234  // List times: one per line
235  for (const instant& t : timeDirs)
236  {
237  Info<< t.name() << nl;
238  }
239  Info<< flush;
240  }
241 
242  return 0;
243 }
244 
245 
246 // ************************************************************************* //
Foam::argList::noBanner
static void noBanner()
Definition: argList.C:434
Foam::autoPtr::New
static autoPtr< T > New(Args &&... args)
profiling.H
Foam::regExpPosix
Wrapper around POSIX extended regular expressions with some additional prefix-handling....
Definition: regExpPosix.H:80
Foam::inplaceSubsetList
void inplaceSubsetList(ListType &input, const UnaryPredicate &pred, const bool invert=false)
Definition: ListOpsTemplates.C:687
Foam::word
A class for handling words, derived from Foam::string.
Definition: word.H:63
Foam::fileName
A class for handling file names.
Definition: fileName.H:71
TimePaths.H
Foam::string::starts_with
bool starts_with(const std::string &s) const
Definition: string.H:293
Foam::argList::caseName
const fileName & caseName() const noexcept
Definition: argListI.H:62
Foam::argList::addNote
static void addNote(const string &note)
Definition: argList.C:405
Foam::timeSelector::select
instantList select(const instantList &times) const
Definition: timeSelector.C:82
Foam::fileHandler
const fileOperation & fileHandler()
Definition: fileOperation.C:1478
Foam::endl
Ostream & endl(Ostream &os)
Definition: Ostream.H:381
Foam::InfoErr
messageStream InfoErr
Foam::flush
Ostream & flush(Ostream &os)
Definition: Ostream.H:371
Foam::argList::noFunctionObjects
static void noFunctionObjects(bool addWithOption=false)
Definition: argList.C:466
Foam::argList::noJobInfo
static void noJobInfo()
Definition: argList.C:486
Foam::Info
messageStream Info
Foam::fileOperation::nProcs
virtual label nProcs(const fileName &dir, const fileName &local="") const
Definition: fileOperation.C:1186
argList.H
timeName
word timeName
Definition: getTimeIndex.H:3
Foam::argList::path
fileName path() const
Definition: argListI.H:74
Foam::FatalError
error FatalError
Foam::argList::verbose
int verbose() const noexcept
Definition: argListI.H:121
Foam
Definition: atmBoundaryLayer.C:26
Foam::profiling::disable
static void disable()
Definition: profiling.C:110
Foam::exit
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:125
Foam::argList::addBoolOption
static void addBoolOption(const word &optName, const string &usage="", bool advanced=false)
Definition: argList.C:317
Foam::autoPtr
Pointer management similar to std::unique_ptr, with some additional methods and type checking.
Definition: HashPtrTable.H:49
setRootCase.H
FatalErrorInFunction
#define FatalErrorInFunction
Definition: error.H:465
Foam::nl
constexpr char nl
Definition: Ostream.H:424
Foam::rmDir
bool rmDir(const fileName &directory, const bool silent=false)
Definition: POSIX.C:1310
Foam::timeSelector::addOptions
static void addOptions(const bool constant=true, const bool withZero=false)
Definition: timeSelector.C:95
Foam::List
A 1D array of objects of type <T>, where the size of the vector is known and used for subscript bound...
Definition: BitOps.H:58
Foam::fileName::DIRECTORY
@ DIRECTORY
A directory.
Definition: fileName.H:82
timeSelector.H
forAllReverse
#define forAllReverse(list, i)
Definition: stdFoam.H:365
ListOps.H
Various functions to operate on Lists.
Foam::instant
An instant of time. Contains the time value and name.
Definition: instant.H:48
Foam::argList::noParallel
static void noParallel()
Definition: argList.C:503
args
Foam::argList args(argc, argv)
stringOps.H
Foam::readDir
fileNameList readDir(const fileName &directory, const fileName::Type type=fileName::FILE, const bool filtergz=true, const bool followLink=true)
Definition: POSIX.C:878
Foam::argList::rootPath
const fileName & rootPath() const noexcept
Definition: argListI.H:56
Foam::argList::addVerboseOption
static void addVerboseOption(const string &usage, bool advanced=false)
Definition: argList.C:457
Foam::argList::found
bool found(const word &optName) const
Definition: argListI.H:171
autoPtr.H