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-2020 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 (old) | "processors"
70 // Collated (new) | "processors<N>"
71 // Host collated | "processors<N>_<low>-<high>"
72 
73 const regExp matcher("processors?[0-9]+(_[0-9]+-[0-9]+)?");
74 
75 bool isProcessorDir(const string& dir)
76 {
77  return
78  (
79  dir.starts_with("processor")
80  && (dir == "processors" || matcher.match(dir))
81  );
82 }
83 
84 
85 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
86 
87 int main(int argc, char *argv[])
88 {
90  (
91  "List times using the timeSelector,"
92  " or use to remove selected time directories"
93  );
94  timeSelector::addOptions(true, true); // constant(true), zero(true)
98  argList::noFunctionObjects(); // Never use function objects
100  (
101  "processor",
102  "List times from processor0/ directory"
103  );
105  (
106  "rm",
107  "Remove selected time directories"
108  );
110  (
111  "verbose",
112  "Report progress of -rm option"
113  );
114  profiling::disable(); // Disable profiling (and its output)
115 
116  #include "setRootCase.H"
117 
118  const bool removeFiles(args.found("rm"));
119  bool verbose(args.found("verbose"));
120 
121 
122  // Get times list from the master processor and subset based on
123  // command-line options
124 
125  label nProcs = 0;
126  autoPtr<TimePaths> timePaths;
127 
128  if (args.found("processor"))
129  {
130  // Determine the processor count
131  nProcs = fileHandler().nProcs(args.path());
132 
133  if (!nProcs)
134  {
136  << "No processor* directories found"
137  << exit(FatalError);
138  }
139 
140  // Obtain time directory names from "processor0/" only
141  timePaths = autoPtr<TimePaths>::New
142  (
143  args.rootPath(),
144  args.caseName()/"processor0"
145  );
146  }
147  else
148  {
149  timePaths = autoPtr<TimePaths>::New
150  (
151  args.rootPath(),
152  args.caseName()
153  );
154  }
155 
156 
157  const instantList timeDirs(timeSelector::select(timePaths->times(), args));
158 
159  const label nTimes = timeDirs.size();
160 
161  if (removeFiles)
162  {
163  if (nProcs)
164  {
165  fileNameList procDirs
166  (
168  (
169  args.path(),
171  false, // No gzip anyhow
172  false // Do not follow linkts
173  )
174  );
175 
176  inplaceSubsetList(procDirs, isProcessorDir);
177 
178  // Perhaps not needed
180 
181  if (verbose)
182  {
183  InfoErr
184  << "Removing " << nTimes
185  << " times in " << procDirs.size()
186  << " processor directories" << endl;
187  }
188 
189  // No processor directories? - silence verbosity
190  if (procDirs.empty())
191  {
192  verbose = false;
193  }
194 
195  forAllReverse(timeDirs, timei)
196  {
197  const word& timeName = timeDirs[timei].name();
198 
199  if (verbose)
200  {
201  InfoErr
202  << " rm " << timeName
203  << " [" << (nTimes - timei) << '/' << nTimes << ']'
204  << endl;
205  }
206 
207  for (const fileName& procDir : procDirs)
208  {
209  rmDir(args.path()/procDir/timeName, true);
210  }
211  }
212  }
213  else
214  {
215  if (verbose)
216  {
217  InfoErr
218  << "Removing " << nTimes
219  << " time directories" << endl;
220  }
221 
222  forAllReverse(timeDirs, timei)
223  {
224  const word& timeName = timeDirs[timei].name();
225 
226  if (verbose)
227  {
228  InfoErr
229  << " rm " << timeName
230  << " [" << (nTimes - timei) << '/' << nTimes << ']'
231  << endl;
232  }
233 
234  rmDir(args.path()/timeName, true);
235  }
236  }
237  }
238  else
239  {
240  // List times: one per line
241  for (const instant& t : timeDirs)
242  {
243  Info<< t.name() << nl;
244  }
245  Info<< flush;
246  }
247 
248  return 0;
249 }
250 
251 
252 // ************************************************************************* //
Foam::argList::noBanner
static void noBanner()
Disable emitting the banner information.
Definition: argList.C:442
Foam::autoPtr::New
static autoPtr< T > New(Args &&... args)
Construct autoPtr of T with forwarding arguments.
profiling.H
Foam::inplaceSubsetList
void inplaceSubsetList(ListType &input, const UnaryPredicate &pred, const bool invert=false)
Inplace subset of the list when predicate is true.
Definition: ListOpsTemplates.C:701
Foam::word
A class for handling words, derived from Foam::string.
Definition: word.H:62
Foam::fileName
A class for handling file names.
Definition: fileName.H:69
TimePaths.H
Foam::string::starts_with
bool starts_with(const std::string &s) const
True if string starts with the given prefix (cf. C++20)
Definition: string.H:299
Foam::argList::addNote
static void addNote(const string &note)
Add extra notes for the usage information.
Definition: argList.C:413
Foam::timeSelector::select
instantList select(const instantList &times) const
Select a list of Time values that are within the ranges.
Definition: timeSelector.C:95
Foam::fileHandler
const fileOperation & fileHandler()
Get current file handler.
Definition: fileOperation.C:1170
Foam::endl
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:350
Foam::InfoErr
messageStream InfoErr
Information stream (uses stderr - output is on the master only)
Foam::argList::rootPath
const fileName & rootPath() const
Return root path.
Definition: argListI.H:63
Foam::flush
Ostream & flush(Ostream &os)
Flush stream.
Definition: Ostream.H:342
Foam::argList::noFunctionObjects
static void noFunctionObjects(bool addWithOption=false)
Remove '-noFunctionObjects' option and ignore any occurrences.
Definition: argList.C:454
Foam::string::match
bool match(const std::string &text) const
Test for equality.
Definition: stringI.H:260
Foam::argList::noJobInfo
static void noJobInfo()
Suppress JobInfo, overriding controlDict setting.
Definition: argList.C:474
Foam::Info
messageStream Info
Information stream (uses stdout - output is on the master only)
Foam::fileOperation::nProcs
virtual label nProcs(const fileName &dir, const fileName &local="") const
Get number of processor directories/results. Used for e.g.
Definition: fileOperation.C:915
argList.H
Foam::TimePaths::times
instantList times() const
Search the case for valid time directories.
Definition: TimePaths.C:149
Foam::regExpCxx
Wrapper around C++11 regular expressions.
Definition: regExpCxx.H:72
timeName
word timeName
Definition: getTimeIndex.H:3
Foam::argList::path
fileName path() const
Return the full path to the (processor local) case.
Definition: argListI.H:81
Foam::FatalError
error FatalError
Foam
Namespace for OpenFOAM.
Definition: atmBoundaryLayer.C:33
Foam::profiling::disable
static void disable()
Disallow profiling by forcing the InfoSwitch off.
Definition: profiling.C:118
Foam::exit
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:130
Foam::argList::addBoolOption
static void addBoolOption(const word &optName, const string &usage="", bool advanced=false)
Add a bool option to validOptions with usage information.
Definition: argList.C:325
Foam::autoPtr
Pointer management similar to std::unique_ptr, with some additional methods and type checking.
Definition: HashPtrTable.H:53
setRootCase.H
FatalErrorInFunction
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:372
Foam::nl
constexpr char nl
Definition: Ostream.H:385
Foam::rmDir
bool rmDir(const fileName &directory, const bool silent=false)
Remove a directory and its contents (optionally silencing warnings)
Definition: MSwindows.C:1018
Foam::timeSelector::addOptions
static void addOptions(const bool constant=true, const bool withZero=false)
Add timeSelector options to argList::validOptions.
Definition: timeSelector.C:108
Foam::List< instant >
Foam::fileName::DIRECTORY
A directory.
Definition: fileName.H:80
timeSelector.H
forAllReverse
#define forAllReverse(list, i)
Reverse loop across all elements in list.
Definition: stdFoam.H:309
Foam::argList::caseName
const fileName & caseName() const
Return case name (parallel run) or global case (serial run)
Definition: argListI.H:69
ListOps.H
Various functions to operate on Lists.
Foam::instant
An instant of time. Contains the time value and name.
Definition: instant.H:52
Foam::argList::noParallel
static void noParallel()
Remove the parallel options.
Definition: argList.C:491
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)
Read a directory and return the entries as a fileName List.
Definition: MSwindows.C:707
Foam::argList::found
bool found(const word &optName) const
Return true if the named option is found.
Definition: argListI.H:157
autoPtr.H