foamToC.C
Go to the documentation of this file.
1 /*---------------------------------------------------------------------------*\
2  ========= |
3  \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
4  \\ / O peration | Website: https://openfoam.org
5  \\ / A nd | Copyright (C) 2022-2023 OpenFOAM Foundation
6  \\/ M anipulation |
7 -------------------------------------------------------------------------------
8 License
9  This file is part of OpenFOAM.
10 
11  OpenFOAM is free software: you can redistribute it and/or modify it
12  under the terms of the GNU General Public License as published by
13  the Free Software Foundation, either version 3 of the License, or
14  (at your option) any later version.
15 
16  OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
17  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
18  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
19  for more details.
20 
21  You should have received a copy of the GNU General Public License
22  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
23 
24 Application
25  foamToC
26 
27 Description
28  Run-time selection table of contents printing and interrogation.
29 
30  The run-time selection tables are populated by the optionally specified
31  solver class and any additional libraries listed in the \c -libs option or
32  all libraries using the \c -allLibs option. Once populated the tables can
33  be searched and printed by a range of options listed below. Table entries
34  are printed with the corresponding library they are in to aid selection
35  and the addition of \c libs entries to ensure availability to the solver.
36 
37 Usage
38  \b foamToC [OPTION]
39  - \par -noLibs
40  Load only the core libOpenFOAM.so library by default
41 
42  - \par -libs '(\"lib1.so\" ... \"libN.so\")'
43  Pre-load additional libraries
44 
45  - \par -solvers <name>
46  List solvers
47 
48  - \par -solver <name>
49  Load libraries associated with specified solver
50 
51  - \par -listLibs
52  List libraries as they are loaded
53 
54  - \par switches,
55  List all available debug, info and optimisation switches
56 
57  - \par all,
58  List the contents of all the run-time selection tables
59 
60  - \par tables
61  List the run-time selection table names (this is the default action)
62 
63  - \par table <name>
64  List the contents of the specified table or the list sub-tables
65 
66  - \par search <name> or \"<regular expression>\"
67  Search for and list the tables containing the given name
68  or all matches to the given regular expression
69 
70  - \par scalarBCs,
71  List scalar field boundary conditions (fvPatchField<scalar>)
72 
73  - \par vectorBCs,
74  List vector field boundary conditions (fvPatchField<vector>)
75 
76  - \par functionObjects,
77  List functionObjects
78 
79  - \par fvModels,
80  List fvModels
81 
82  - \par fvConstraints,
83  List fvConstraints
84 
85  Example usage:
86  - Print the list of solvers
87  \verbatim
88  foamToC -solvers
89  \endverbatim
90 
91  - Print the list of scalar boundary conditions (fvPatchField<scalar>)
92  provided by the \c fluid solver without additional libraries:
93  \verbatim
94  foamToC -solver fluid -scalarBCs
95  \endverbatim
96 
97  - Print the list of RAS momentum transport models provided by the
98  \c fluid solver:
99  \verbatim
100  foamToC -solver fluid -table RAScompressibleMomentumTransportModel
101  \endverbatim
102 
103  - Print the list of functionObjects provided by the
104  \c multicomponentFluid solver with the libfieldFunctionObjects.so
105  library:
106  \verbatim
107  foamToC -solver multicomponentFluid \
108  -libs '("libfieldFunctionObjects.so")' -functionObjects
109  \endverbatim
110 
111  - Print a complete list of all run-time selection tables:
112  \verbatim
113  foamToC -tables
114  or simply
115  foamToC
116  \endverbatim
117 
118  - Print a complete list of all entries in all run-time selection tables:
119  \verbatim
120  foamToC -all
121  \endverbatim
122 
123 \*---------------------------------------------------------------------------*/
124 
125 #include "argList.H"
126 #include "runTimeSelectionToC.H"
127 #include "dlLibraryTable.H"
128 #include "HashSet.H"
129 #include "IOmanip.H"
130 
131 using namespace Foam;
132 
133 // Enable run-time selection table of contents caching
134 // Overrides the enableRunTimeSelectionToC = false in libOpenFOAM
136 
137 
138 HashTable<HashTable<wordHashSet>> baseTypeNameToC()
139 {
140  HashTable<HashTable<wordHashSet>> baseTypeNameToC;
141 
143  (
146  iter
147  )
148  {
149  const word& baseType = iter.key();
150  const word& baseTypeName = iter().first();
151 
152  if (!baseTypeNameToC.found(baseTypeName))
153  {
154  baseTypeNameToC.insert
155  (
156  baseTypeName,
158  );
159  }
160 
161  if (!baseTypeNameToC[baseTypeName].found(baseType))
162  {
163  baseTypeNameToC[baseTypeName].insert
164  (
165  baseType,
166  iter().second()
167  );
168  }
169  }
170 
171  return baseTypeNameToC;
172 }
173 
174 
175 void printToC(const word& tableName)
176 {
177  bool found = false;
178 
179  if (debug::runTimeSelectionToC.found(tableName))
180  {
181  const wordList toc
182  (
183  debug::runTimeSelectionToC[tableName].second().sortedToc()
184  );
185 
186  Info<< "Contents of table " << tableName;
187 
188  if (debug::runTimeSelectionToC[tableName].first() != tableName)
189  {
190  Info<< ", base type "
191  << debug::runTimeSelectionToC[tableName].first();
192  }
193 
194  Info<< ":" << endl;
195 
196  forAll(toc, i)
197  {
198  Info<< " " << setf(ios_base::left) << setw(40) << toc[i]
199  << debug::runTimeSelectionToC[tableName].second()[toc[i]]
200  << endl;
201  }
202 
203  found = true;
204  }
205  else
206  {
208  (
209  baseTypeNameToC()
210  );
211 
212  if (runTimeSelectionToC.found(tableName))
213  {
214  const wordList toc(runTimeSelectionToC[tableName].sortedToc());
215 
216  Info<< "Tables of type " << tableName << endl;
217  forAll(toc, i)
218  {
219  Info<< " " << toc[i] << endl;
220  }
221 
222  found = true;
223  }
224  }
225 
226  if (!found)
227  {
228  Info<< "Table " << tableName << " not found" << endl;
229  }
230 }
231 
232 
233 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
234 
235 int main(int argc, char *argv[])
236 {
238  writeInfoHeader = false;
239 
241 
243  (
244  "noLibs",
245  "Load only the core libOpenFOAM.so library by default"
246  );
247 
249  (
250  "libs",
251  "'(\"lib1.so\" ... \"libN.so\")'",
252  "Pre-load additional libraries"
253  );
254 
256  (
257  "solvers",
258  "List solvers"
259  );
260 
262  (
263  "solver",
264  "name",
265  "Load libraries associated with specified solver"
266  );
267 
269  (
270  "listLibs",
271  "List libraries as they are loaded"
272  );
273 
275  (
276  "switches",
277  "List all available debug, info and optimisation switches"
278  );
279 
281  (
282  "all",
283  "List the contents of all the run-time selection tables"
284  );
285 
287  (
288  "tables",
289  "List the run-time selection table names"
290  );
291 
293  (
294  "table",
295  "name",
296  "List the contents of the specified table"
297  );
298 
300  (
301  "search",
302  "name",
303  "List the tables containing the given name"
304  );
305 
307  (
308  "scalarBCs",
309  "List scalar field boundary conditions (fvPatchField<scalar>)"
310  );
311 
313  (
314  "vectorBCs",
315  "List vector field boundary conditions (fvPatchField<vector>)"
316  );
317 
319  (
320  "functionObjects",
321  "List functionObjects"
322  );
323 
325  (
326  "fvModels",
327  "List fvModels"
328  );
329 
331  (
332  "fvConstraints",
333  "List fvConstraints"
334  );
335 
336  const argList args(argc, argv);
337  if (!args.checkRootCase())
338  {
340  }
341 
342  word solverName;
343  if (args.optionReadIfPresent("solver", solverName))
344  {
345  libs.open("lib" + solverName + ".so");
346  }
347 
348  const string libDir(getEnv("FOAM_LIBBIN"));
349  const fileNameList libNames(readDir(libDir));
350 
351  const bool listLibs = args.optionFound("listLibs");
352 
353  if (!args.optionFound("noLibs") && solverName == word::null)
354  {
355  if (listLibs)
356  {
357  Info << "Loading libraries:" << nl;
358  }
359  forAll(libNames, i)
360  {
361  if (libNames[i].ext() == "so")
362  {
363  if (listLibs)
364  {
365  Info << " " << libNames[i].c_str() << nl;
366  }
367  libs.open(libDir/libNames[i]);
368  }
369  }
370  Info << endl;
371  }
372 
373  bool done = false;
374 
375  if (args.optionFound("switches"))
376  {
378  done = true;
379  }
380 
381  word tableName;
382  if (args.optionReadIfPresent("table", tableName))
383  {
384  printToC(tableName);
385  done = true;
386  }
387 
388  word name;
389  if (args.optionReadIfPresent("search", name))
390  {
391  wordList names(name);
392 
393  // If the name is a regular expression pattern
394  // search for all matching occurrences
395  if (wordRe::isPattern(name))
396  {
397  wordRe nameRe(name);
398  nameRe.compile();
399 
400  HashSet<word> unsortedNames;
401 
403  (
406  iter
407  )
408  {
409  forAllConstIter(HashTable<word>, iter().second(), iter2)
410  {
411  if (nameRe.match(iter2.key()))
412  {
413  unsortedNames.insert(iter2.key());
414  }
415  }
416  }
417 
418  names = unsortedNames.toc();
419  }
420 
421  forAll(names, i)
422  {
423  const word& name = names[i];
424 
425  HashTable<HashTable<word>> baseTypeNameTables;
426 
428  (
431  iter
432  )
433  {
434  const word& baseType = iter.key();
435  const word& baseTypeName = iter().first();
436 
437  if (iter().second().found(name))
438  {
439  if (!baseTypeNameTables.found(baseTypeName))
440  {
441  baseTypeNameTables.insert
442  (
443  baseTypeName,
445  );
446  }
447 
448  baseTypeNameTables[baseTypeName].insert
449  (
450  baseType,
451  iter().second()[name]
452  );
453  }
454  }
455 
456  if (baseTypeNameTables.size())
457  {
458  const wordList toc(baseTypeNameTables.sortedToc());
459 
460  if (toc.size() == 1)
461  {
462  Info<< name << " is in table " << endl;
463  }
464  else if (toc.size() > 1)
465  {
466  Info<< name << " is in tables " << endl;
467  }
468 
469  forAll(toc, i)
470  {
471  if
472  (
473  baseTypeNameTables[toc[i]].size() == 1
474  && toc[i] == baseTypeNameTables[toc[i]].begin().key()
475  )
476  {
477  Info<< " " << setf(ios_base::left) << setw(40)
478  << toc[i] << baseTypeNameTables[toc[i]].begin()()
479  << endl;
480  }
481  else
482  {
483  const wordList tocc
484  (
485  baseTypeNameTables[toc[i]].sortedToc()
486  );
487 
488  Info<< " " << toc[i] << endl;
489  forAll(tocc, j)
490  {
491  Info<< " "
492  << setf(ios_base::left) << setw(40) << tocc[j]
493  << baseTypeNameTables[toc[i]][tocc[j]]
494  << endl;
495  }
496  }
497  }
498  }
499  else
500  {
501  Info<< name << " not found" << endl;
502  }
503  }
504 
505  done = true;
506  }
507 
508  if (args.optionFound("all"))
509  {
510  Info<< "ToC:" << nl
512  done = true;
513  }
514 
515  if (args.optionFound("solvers"))
516  {
517  printToC("solver");
518  done = true;
519  }
520 
521  if (args.optionFound("scalarBCs"))
522  {
523  Info<< "Scalar boundary conditions:" << endl;
524  printToC("fvPatchScalarField");
525  done = true;
526  }
527 
528  if (args.optionFound("vectorBCs"))
529  {
530  Info<< "vector boundary conditions:" << endl;
531  printToC("fvPatchVectorField");
532  done = true;
533  }
534 
535  if (args.optionFound("functionObjects"))
536  {
537  Info<< "functionObjects:" << endl;
538  printToC("functionObject");
539  done = true;
540  }
541 
542  if (args.optionFound("fvModels"))
543  {
544  Info<< "fvModels:" << endl;
545  printToC("fvModel");
546  done = true;
547  }
548 
549  if (args.optionFound("fvConstraints"))
550  {
551  Info<< "fvConstraints:" << endl;
552  printToC("fvConstraint");
553  done = true;
554  }
555 
556  if (args.optionFound("tables") || !done)
557  {
559  (
560  baseTypeNameToC()
561  );
562 
564 
565  Info<< "Run-time selection tables:" << nl;
566 
567  forAll(toc, i)
568  {
569  if
570  (
571  runTimeSelectionToC[toc[i]].size() == 1
572  && toc[i] == runTimeSelectionToC[toc[i]].begin().key()
573  )
574  {
575  Info<< " " << toc[i] << endl;
576  }
577  else
578  {
579  const wordList tocc
580  (
581  runTimeSelectionToC[toc[i]].sortedToc()
582  );
583 
584  Info<< " " << toc[i] << endl;
585  forAll(tocc, j)
586  {
587  Info<< " " << tocc[j] << endl;
588  }
589  }
590  }
591  }
592 
593  return 0;
594 }
595 
596 
597 // ************************************************************************* //
Istream and Ostream manipulators taking arguments.
bool found
#define forAll(list, i)
Loop across all elements in list.
Definition: UList.H:434
#define forAllConstIter(Container, container, iter)
Iterate across all elements in the container object of type.
Definition: UList.H:477
A HashTable with keys but without contents.
Definition: HashSet.H:62
bool insert(const Key &key)
Insert a new entry.
Definition: HashSet.H:111
An STL-conforming hash table.
Definition: HashTable.H:127
List< Key > sortedToc() const
Return the table of contents as a sorted list.
Definition: HashTable.C:217
List< Key > toc() const
Return the table of contents.
Definition: HashTable.C:202
iterator begin()
Iterator set to the beginning of the HashTable.
Definition: HashTableI.H:411
label size() const
Return number of elements in table.
Definition: HashTableI.H:65
bool insert(const Key &, const T &newElmt)
Insert a new hashedEntry.
Definition: HashTableI.H:80
bool found(const Key &) const
Return true if hashedEntry is found in table.
Definition: HashTable.C:113
static void clear()
Clear the options table.
Definition: argList.C:100
Extract command arguments and options from the supplied argc and argv parameters.
Definition: argList.H:103
static void addOption(const word &opt, const string &param="", const string &usage="")
Add to an option to validOptions with usage information.
Definition: argList.C:128
static void addBoolOption(const word &opt, const string &usage="")
Add to a bool option to validOptions with usage information.
Definition: argList.C:118
bool optionFound(const word &opt) const
Return true if the named option is found.
Definition: argListI.H:114
bool optionReadIfPresent(const word &opt, T &) const
Read a value from the named option if present.
Definition: argListI.H:204
bool checkRootCase() const
Check root path and case path.
Definition: argList.C:1441
bool open(const fileName &libName, const bool verbose=true)
Open the named library, optionally with warnings if problems occur.
static int disableFunctionEntries
Definition: entry.H:86
void exit(const int errNo=1)
Exit : can be called for any error to exit program.
Definition: error.C:125
A wordRe is a word, but can also have a regular expression for matching words.
Definition: wordRe.H:77
bool isPattern() const
Should be treated as a match rather than a literal string?
Definition: wordReI.H:121
A class for handling words, derived from string.
Definition: word.H:62
static const word null
An empty word.
Definition: word.H:77
int main(int argc, char *argv[])
Definition: financialFoam.C:44
runTimeSelectionToCType runTimeSelectionToC
Run-time selectable objects.
void listSwitches()
List debug switches.
Definition: debug.C:467
bool enableRunTimeSelectionToC
Switch to enable runTimeSelectionToC collection and caching.
Namespace for OpenFOAM.
dlLibraryTable libs
Table of loaded dynamic libraries.
Smanip< ios_base::fmtflags > setf(const ios_base::fmtflags flags)
Definition: IOmanip.H:164
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:251
word name(const bool)
Return a word representation of a bool.
Definition: boolIO.C:39
Omanip< int > setw(const int i)
Definition: IOmanip.H:199
bool writeInfoHeader
messageStream Info
labelList second(const UList< labelPair > &p)
Definition: patchToPatch.C:49
labelList first(const UList< labelPair > &p)
Definition: patchToPatch.C:39
string getEnv(const word &)
Return environment variable of given name.
Definition: POSIX.C:97
error FatalError
static const char nl
Definition: Ostream.H:260
fileNameList readDir(const fileName &, const fileType=fileType::file, const bool filterVariants=true, const bool followLink=true)
Read a directory and return the entries as a string list.
Definition: POSIX.C:662
Foam::argList args(argc, argv)