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 // ************************************************************************* //
static void noJobInfo()
Suppress JobInfo, overriding controlDict setting.
Definition: argList.C:567
static void noFunctionObjects(bool addWithOption=false)
Remove &#39;-noFunctionObjects&#39; option and ignore any occurrences.
Definition: argList.C:547
static void addNote(const string &note)
Add extra notes for the usage information.
Definition: argList.C:462
A class for handling file names.
Definition: fileName.H:72
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:125
messageStream InfoErr
Information stream (stderr output on master, null elsewhere)
error FatalError
Error stream (stdout output on all processes), with additional &#39;FOAM FATAL ERROR&#39; header text and sta...
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:598
constexpr char nl
The newline &#39;\n&#39; character (0x0a)
Definition: Ostream.H:50
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:531
static void addBoolOption(const word &optName, const string &usage="", bool advanced=false)
Add a bool option to validOptions with usage information.
Definition: argList.C:374
static void noParallel()
Remove the parallel options.
Definition: argList.C:584
refPtr< fileOperation > fileHandler(std::nullptr_t)
Delete current file handler - forwards to fileOperation::handler()
instantList select(const instantList &times) const
Select a list of Time values that are within the ranges.
Definition: timeSelector.C:88
static void disable() noexcept
Disallow profiling - turns the InfoSwitch off.
Definition: profiling.C:113
void inplaceSubsetList(ListType &input, const UnaryPredicate &pred, const bool invert=false)
Inplace subset of the list when predicate is true.
Various functions to operate on Lists.
word timeName
Definition: getTimeIndex.H:3
A class for handling words, derived from Foam::string.
Definition: word.H:63
const fileName & caseName() const noexcept
Return case name (parallel run) or global case (serial run)
Definition: argListI.H:62
bool rmDir(const fileName &directory, const bool silent=false, const bool emptyOnly=false)
Remove a directory and its contents recursively,.
Definition: POSIX.C:1433
static void addVerboseOption(const string &usage="", bool advanced=false)
Enable a &#39;verbose&#39; bool option, with usage information.
Definition: argList.C:520
const fileName & rootPath() const noexcept
Return root path.
Definition: argListI.H:56
fileName path() const
Return the full path to the (processor local) case.
Definition: argListI.H:74
An instant of time. Contains the time value and name. Uses Foam::Time when formatting the name...
Definition: instant.H:53
Wrapper around POSIX extended regular expressions with some additional prefix-handling. The prefix-handling is loosely oriented on PCRE regular expressions and provides a simple means of tuning the expressions.
Definition: regExpPosix.H:80
bool starts_with(char c) const
True if string starts with given character (cf. C++20)
Definition: string.H:435
Ostream & flush(Ostream &os)
Flush stream.
Definition: Ostream.H:521
instantList times() const
Search the case for valid time directories.
Definition: TimePaths.C:118
messageStream Info
Information stream (stdout output on master, null elsewhere)
static void noBanner()
Disable emitting the banner information.
Definition: argList.C:491
Pointer management similar to std::unique_ptr, with some additional methods and type checking...
Definition: HashPtrTable.H:48
#define forAllReverse(list, i)
Reverse loop across all elements in list.
Definition: stdFoam.H:437
static autoPtr< T > New(Args &&... args)
Construct autoPtr with forwarding arguments.
Definition: autoPtr.H:178
fileNameList readDir(const fileName &directory, const fileName::Type type=fileName::Type::FILE, const bool filtergz=true, const bool followLink=true)
Read a directory and return the entries as a fileName List.
Definition: POSIX.C:963
Foam::argList args(argc, argv)
int verbose() const noexcept
Return the verbose flag.
Definition: argListI.H:121
bool found(const word &optName) const
Return true if the named option is found.
Definition: argListI.H:171
static void addOptions(const bool constant=true, const bool withZero=false)
Add timeSelector options to argList::validOptions.
Definition: timeSelector.C:101
Namespace for OpenFOAM.