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-------------------------------------------------------------------------------
11License
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
27Application
28 foamListTimes
29
30Group
31 grpPostProcessingUtilities
32
33Description
34 List times using the timeSelector, or use to remove selected time
35 directories.
36
37Usage
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
50Note
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
64using 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
72const regExp matcher("processors?[0-9]+(_[0-9]+-[0-9]+)?");
73
74bool isProcessorDir(const string& dir)
75{
76 return (dir.starts_with("processor") && matcher.match(dir));
77}
78
79
80// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
81
82int main(int argc, char *argv[])
83{
84 argList::addNote
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)
90 argList::noBanner();
91 argList::noParallel();
92 argList::noJobInfo();
93 argList::noFunctionObjects(); // Never use function objects
94 argList::addBoolOption
95 (
96 "processor",
97 "List times from processor0/ directory"
98 );
99 argList::addBoolOption
100 (
101 "rm",
102 "Remove selected time directories"
103 );
104 argList::addVerboseOption
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(),
164 fileName::DIRECTORY,
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// ************************************************************************* //
Various functions to operate on Lists.
int verbose() const noexcept
Return the verbose flag.
Definition: argListI.H:128
const fileName & rootPath() const noexcept
Return root path.
Definition: argListI.H:63
bool found(const word &optName) const
Return true if the named option is found.
Definition: argListI.H:178
fileName path() const
Return the full path to the (processor local) case.
Definition: argListI.H:81
const fileName & caseName() const noexcept
Return case name (parallel run) or global case (serial run)
Definition: argListI.H:69
Pointer management similar to std::unique_ptr, with some additional methods and type checking.
Definition: autoPtr.H:66
A class for handling file names.
Definition: fileName.H:76
virtual label nProcs(const fileName &dir, const fileName &local="") const
Get number of processor directories/results. Used for e.g.
An instant of time. Contains the time value and name. Uses Foam::Time when formatting the name.
Definition: instant.H:56
Wrapper around C++11 regular expressions with some additional prefix-handling. The prefix-handling is...
Definition: regExpCxx.H:83
bool starts_with(const std::string &s) const
True if string starts with the given prefix (cf. C++20)
Definition: string.H:297
A class for handling words, derived from Foam::string.
Definition: word.H:68
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:453
word timeName
Definition: getTimeIndex.H:3
Namespace for OpenFOAM.
const fileOperation & fileHandler()
Get current file handler.
messageStream Info
Information stream (stdout output on master, null elsewhere)
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:372
void inplaceSubsetList(ListType &input, const UnaryPredicate &pred, const bool invert=false)
Inplace subset of the list when predicate is true.
bool rmDir(const fileName &directory, const bool silent=false)
Remove a directory and its contents (optionally silencing warnings)
Definition: MSwindows.C:1036
fileNameList readDir(const fileName &directory, const fileName::Type type=fileName::FILE, const bool filtergz=true, const bool followLink=true)
Read a directory and return the entries as a fileName List.
Definition: MSwindows.C:715
messageStream InfoErr
Information stream (stderr output on master, null elsewhere)
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:130
Ostream & flush(Ostream &os)
Flush stream.
Definition: Ostream.H:364
constexpr char nl
The newline '\n' character (0x0a)
Definition: Ostream.H:53
Foam::argList args(argc, argv)
#define forAllReverse(list, i)
Reverse loop across all elements in list.
Definition: stdFoam.H:346