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)
199  << setw(39) << toc[i]
200  << ' ' << debug::runTimeSelectionToC[tableName].second()[toc[i]]
201  << endl;
202  }
203 
204  found = true;
205  }
206  else
207  {
209  (
210  baseTypeNameToC()
211  );
212 
213  if (runTimeSelectionToC.found(tableName))
214  {
215  const wordList toc(runTimeSelectionToC[tableName].sortedToc());
216 
217  Info<< "Tables of type " << tableName << endl;
218  forAll(toc, i)
219  {
220  Info<< " " << toc[i] << endl;
221  }
222 
223  found = true;
224  }
225  }
226 
227  if (!found)
228  {
229  Info<< "Table " << tableName << " not found" << endl;
230  }
231 }
232 
233 
234 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
235 
236 int main(int argc, char *argv[])
237 {
239  writeInfoHeader = false;
240 
242 
244  (
245  "noLibs",
246  "Load only the core libOpenFOAM.so library by default"
247  );
248 
250  (
251  "libs",
252  "'(\"lib1.so\" ... \"libN.so\")'",
253  "Pre-load additional libraries"
254  );
255 
257  (
258  "solvers",
259  "List solvers"
260  );
261 
263  (
264  "solver",
265  "name",
266  "Load libraries associated with specified solver"
267  );
268 
270  (
271  "listLibs",
272  "List libraries as they are loaded"
273  );
274 
276  (
277  "switches",
278  "List all available debug, info and optimisation switches"
279  );
280 
282  (
283  "all",
284  "List the contents of all the run-time selection tables"
285  );
286 
288  (
289  "tables",
290  "List the run-time selection table names"
291  );
292 
294  (
295  "table",
296  "name",
297  "List the contents of the specified table"
298  );
299 
301  (
302  "search",
303  "name",
304  "List the tables containing the given name"
305  );
306 
308  (
309  "scalarBCs",
310  "List scalar field boundary conditions (fvPatchField<scalar>)"
311  );
312 
314  (
315  "vectorBCs",
316  "List vector field boundary conditions (fvPatchField<vector>)"
317  );
318 
320  (
321  "functionObjects",
322  "List functionObjects"
323  );
324 
326  (
327  "fvModels",
328  "List fvModels"
329  );
330 
332  (
333  "fvConstraints",
334  "List fvConstraints"
335  );
336 
337  const argList args(argc, argv);
338  if (!args.checkRootCase())
339  {
341  }
342 
343  word solverName;
344  if (args.optionReadIfPresent("solver", solverName))
345  {
346  libs.open("lib" + solverName + ".so");
347  }
348 
349  const string libDir(getEnv("FOAM_LIBBIN"));
350  const fileNameList libNames(readDir(libDir));
351 
352  const bool listLibs = args.optionFound("listLibs");
353 
354  if (!args.optionFound("noLibs") && solverName == word::null)
355  {
356  if (listLibs)
357  {
358  Info << "Loading libraries:" << nl;
359  }
360  forAll(libNames, i)
361  {
362  if (libNames[i].ext() == "so")
363  {
364  if (listLibs)
365  {
366  Info << " " << libNames[i].c_str() << nl;
367  }
368  libs.open(libDir/libNames[i]);
369  }
370  }
371  Info << endl;
372  }
373 
374  bool done = false;
375 
376  if (args.optionFound("switches"))
377  {
379  done = true;
380  }
381 
382  word tableName;
383  if (args.optionReadIfPresent("table", tableName))
384  {
385  printToC(tableName);
386  done = true;
387  }
388 
389  word name;
390  if (args.optionReadIfPresent("search", name))
391  {
392  wordList names(name);
393 
394  // If the name is a regular expression pattern
395  // search for all matching occurrences
396  if (wordRe::isPattern(name))
397  {
398  wordRe nameRe(name);
399  nameRe.compile();
400 
401  HashSet<word> unsortedNames;
402 
404  (
407  iter
408  )
409  {
410  forAllConstIter(HashTable<word>, iter().second(), iter2)
411  {
412  if (nameRe.match(iter2.key()))
413  {
414  unsortedNames.insert(iter2.key());
415  }
416  }
417  }
418 
419  names = unsortedNames.toc();
420  }
421 
422  forAll(names, i)
423  {
424  const word& name = names[i];
425 
426  HashTable<HashTable<word>> baseTypeNameTables;
427 
429  (
432  iter
433  )
434  {
435  const word& baseType = iter.key();
436  const word& baseTypeName = iter().first();
437 
438  if (iter().second().found(name))
439  {
440  if (!baseTypeNameTables.found(baseTypeName))
441  {
442  baseTypeNameTables.insert
443  (
444  baseTypeName,
446  );
447  }
448 
449  baseTypeNameTables[baseTypeName].insert
450  (
451  baseType,
452  iter().second()[name]
453  );
454  }
455  }
456 
457  if (baseTypeNameTables.size())
458  {
459  const wordList toc(baseTypeNameTables.sortedToc());
460 
461  if (toc.size() == 1)
462  {
463  Info<< name << " is in table " << endl;
464  }
465  else if (toc.size() > 1)
466  {
467  Info<< name << " is in tables " << endl;
468  }
469 
470  forAll(toc, i)
471  {
472  if
473  (
474  baseTypeNameTables[toc[i]].size() == 1
475  && toc[i] == baseTypeNameTables[toc[i]].begin().key()
476  )
477  {
478  Info<< " " << setf(ios_base::left)
479  << setw(39) << toc[i]
480  << ' ' << baseTypeNameTables[toc[i]].begin()()
481  << endl;
482  }
483  else
484  {
485  const wordList tocc
486  (
487  baseTypeNameTables[toc[i]].sortedToc()
488  );
489 
490  Info<< " " << toc[i] << endl;
491  forAll(tocc, j)
492  {
493  Info<< " "
494  << setf(ios_base::left)
495  << setw(39) << tocc[j]
496  << ' ' << baseTypeNameTables[toc[i]][tocc[j]]
497  << endl;
498  }
499  }
500  }
501  }
502  else
503  {
504  Info<< name << " not found" << endl;
505  }
506  }
507 
508  done = true;
509  }
510 
511  if (args.optionFound("all"))
512  {
513  Info<< "ToC:" << nl
515  done = true;
516  }
517 
518  if (args.optionFound("solvers"))
519  {
520  printToC("solver");
521  done = true;
522  }
523 
524  if (args.optionFound("scalarBCs"))
525  {
526  Info<< "Scalar boundary conditions:" << endl;
527  printToC("fvPatchScalarField");
528  done = true;
529  }
530 
531  if (args.optionFound("vectorBCs"))
532  {
533  Info<< "vector boundary conditions:" << endl;
534  printToC("fvPatchVectorField");
535  done = true;
536  }
537 
538  if (args.optionFound("functionObjects"))
539  {
540  Info<< "functionObjects:" << endl;
541  printToC("functionObject");
542  done = true;
543  }
544 
545  if (args.optionFound("fvModels"))
546  {
547  Info<< "fvModels:" << endl;
548  printToC("fvModel");
549  done = true;
550  }
551 
552  if (args.optionFound("fvConstraints"))
553  {
554  Info<< "fvConstraints:" << endl;
555  printToC("fvConstraint");
556  done = true;
557  }
558 
559  if (args.optionFound("tables") || !done)
560  {
562  (
563  baseTypeNameToC()
564  );
565 
567 
568  Info<< "Run-time selection tables:" << nl;
569 
570  forAll(toc, i)
571  {
572  if
573  (
574  runTimeSelectionToC[toc[i]].size() == 1
575  && toc[i] == runTimeSelectionToC[toc[i]].begin().key()
576  )
577  {
578  Info<< " " << toc[i] << endl;
579  }
580  else
581  {
582  const wordList tocc
583  (
584  runTimeSelectionToC[toc[i]].sortedToc()
585  );
586 
587  Info<< " " << toc[i] << endl;
588  forAll(tocc, j)
589  {
590  Info<< " " << tocc[j] << endl;
591  }
592  }
593  }
594  }
595 
596  return 0;
597 }
598 
599 
600 // ************************************************************************* //
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:109
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:242
List< Key > toc() const
Return the table of contents.
Definition: HashTable.C:227
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:138
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:257
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:266
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)