controlSwitches: - We are now completely free from the global controlDict file.

This file is no longer necessary.
                 - If necessary, one can still use a global controlDict file
                   using the environment variable FOAM_GLOBAL_CONTROLDICT
                 - Case specific global control switches values can now be
                   supplied in the case's system/controlDict file.
                 - Renaming $WM_PROJECT_DIR/etc/controlDict to
                   $WM_PROJECT_DIR/etc/controlDict-EXAMPLE
                 - Some code cleanup.
This commit is contained in:
Martin Beaudoin 2015-05-05 08:45:24 -04:00
parent 4706995f5f
commit 473f85c949
6 changed files with 278 additions and 111 deletions

View file

@ -33,8 +33,6 @@ License
#include "JobInfo.H" #include "JobInfo.H"
#include "labelList.H" #include "labelList.H"
#include "SortableList.H" #include "SortableList.H"
//#include "dimensionedConstants.H"
// * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * // // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
@ -44,29 +42,6 @@ Foam::HashTable<Foam::string> Foam::argList::validParOptions;
Foam::word Foam::argList::appDictName_(""); Foam::word Foam::argList::appDictName_("");
bool Foam::argList::bannerEnabled(true); bool Foam::argList::bannerEnabled(true);
template<>
const char*
Foam::NamedEnum
<
Foam::debug::globalControlDictSwitchSet,
5
>::names[] =
{
"DebugSwitches",
"InfoSwitches",
"OptimisationSwitches",
"Tolerances",
"DimensionedConstants"
};
const Foam::NamedEnum
<
Foam::debug::globalControlDictSwitchSet,
5
>
Foam::argList::globalControlDictSwitchSetNames_;
Foam::argList::initValidTables::initValidTables() Foam::argList::initValidTables::initValidTables()
{ {
@ -74,11 +49,24 @@ Foam::argList::initValidTables::initValidTables()
validOptions.set("parallel", ""); validOptions.set("parallel", "");
validParOptions.set("parallel", ""); validParOptions.set("parallel", "");
validOptions.set("noFunctionObjects", ""); validOptions.set("noFunctionObjects", "");
validOptions.set("DebugSwitches", "key1=val1,key2=val2,...");
validOptions.set("InfoSwitches", "key1=val1,key2=val2,..."); // Add the parameters for modifying the controlDict
validOptions.set("OptimisationSwitches", "key1=val1,key2=val2,..."); // switches from the command-line
validOptions.set("Tolerances", "key1=val1,key2=val2,...");
validOptions.set("DimensionedConstants", "key1=val1,key2=val2,..."); // Instantiate a NamedEnum for the controlDict switches names
const Foam::NamedEnum
<
Foam::debug::globalControlDictSwitchSet,
DIM_GLOBALCONTROLDICTSWITCHSET
>
globalControlDictSwitchSetNames;
forAll(globalControlDictSwitchSetNames, gI)
{
word switchSetName = globalControlDictSwitchSetNames.names[gI];
validOptions.set(switchSetName, "key1=val1,key2=val2,...");
}
Pstream::addValidParOptions(validParOptions); Pstream::addValidParOptions(validParOptions);
} }
@ -354,24 +342,10 @@ Foam::argList::argList
<< "Date : " << dateString.c_str() << nl << "Date : " << dateString.c_str() << nl
<< "Time : " << timeString.c_str() << nl << "Time : " << timeString.c_str() << nl
<< "Host : " << hostName() << nl << "Host : " << hostName() << nl
<< "PID : " << pid() << nl << "PID : " << pid()
<< "CtrlDict : " << ctrlDictString.c_str()
<< endl; << endl;
} }
// Command-line override for central controlDict's variables
forAll(globalControlDictSwitchSetNames_, cI)
{
word switchSetName = globalControlDictSwitchSetNames_.names[cI];
if( optionFound(switchSetName) )
Foam::debug::updateCentralDictVars
(
globalControlDictSwitchSetNames_[switchSetName],
option(switchSetName)
);
}
jobInfo.add("startDate", dateString); jobInfo.add("startDate", dateString);
jobInfo.add("startTime", timeString); jobInfo.add("startTime", timeString);
jobInfo.add("userName", userName()); jobInfo.add("userName", userName());
@ -540,6 +514,62 @@ Foam::argList::argList
case_ = globalCase_; case_ = globalCase_;
} }
// Managing the overrides for the global control switches:
//
// Here is the order of precedence for the definition/overriding of the
// control switches, from lowest to highest:
// - source code definitions from the various libraries/solvers
// - file specified by the env. variable FOAM_GLOBAL_CONTROLDICT
// - case's system/controlDict file
// - command-line parameters
//
// First, we allow the users to specify the location of a centralized
// global controlDict dictionary using the environment variable
// FOAM_GLOBAL_CONTROLDICT.
fileName optionalGlobControlDictFileName =
getEnv("FOAM_GLOBAL_CONTROLDICT");
if (optionalGlobControlDictFileName.size() )
{
debug::updateCentralDictVars
(
optionalGlobControlDictFileName,
Pstream::master() && bannerEnabled
);
}
// Now that the rootPath_/globalCase_ directory is known (following the call
// to getRootCase()), we grab any global control switches overrides from the
// current case's controlDict.
debug::updateCentralDictVars
(
rootPath_/globalCase_/"system/controlDict",
Pstream::master() && bannerEnabled
);
// Finally, a command-line override for central controlDict's variables.
// This is the ultimate override for the global control switches.
// Instantiate a NamedEnum for the controlDict switches names
const Foam::NamedEnum
<
Foam::debug::globalControlDictSwitchSet,
DIM_GLOBALCONTROLDICTSWITCHSET
>
globalControlDictSwitchSetNames;
forAll(globalControlDictSwitchSetNames, gI)
{
word switchSetName = globalControlDictSwitchSetNames.names[gI];
if( optionFound(switchSetName) )
Foam::debug::updateCentralDictVars
(
globalControlDictSwitchSetNames[switchSetName],
option(switchSetName)
);
}
wordList slaveProcs; wordList slaveProcs;

View file

@ -126,9 +126,6 @@ public:
// Static data members // Static data members
//- Central dictionary switchSet names
static const NamedEnum<Foam::debug::globalControlDictSwitchSet, 5> globalControlDictSwitchSetNames_;
//- A list of valid (mandatory) arguments //- A list of valid (mandatory) arguments
static SLList<string> validArgs; static SLList<string> validArgs;

View file

@ -37,6 +37,8 @@ Description
#include "optimisationSwitch.H" #include "optimisationSwitch.H"
#include "tolerancesSwitch.H" #include "tolerancesSwitch.H"
#include "constantsSwitch.H" #include "constantsSwitch.H"
#include "fileName.H"
#include "NamedEnum.H"
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
@ -45,6 +47,22 @@ namespace Foam
namespace debug namespace debug
{ {
template<>
const char*
Foam::NamedEnum
<
Foam::debug::globalControlDictSwitchSet,
5
>::names[] =
{
"DebugSwitches",
"InfoSwitches",
"OptimisationSwitches",
"Tolerances",
"DimensionedConstants"
};
//! @cond ignoreDocumentation - local scope //! @cond ignoreDocumentation - local scope
dictionary* controlDictPtr_(NULL); dictionary* controlDictPtr_(NULL);
dictionary* debugSwitchesPtr_(NULL); dictionary* debugSwitchesPtr_(NULL);
@ -95,19 +113,46 @@ Foam::dictionary& Foam::debug::controlDict()
{ {
if (!controlDictPtr_) if (!controlDictPtr_)
{ {
// Allow users to override the location of the global controlDict // We are totally getting rid of the central controlDict file located
// dictionary using an environment variable. Using this environment // under $WM_PROJECT_DIR/etc/controlDict.
// variable, one can assign a different global controlDict for each // But we still need to create a central dictionary for the global control
// case, without having to modify the "default" ones. // switches.
fileName globControlDictFileName = getEnv("FOAM_GLOBAL_CONTROLDICT"); // At first, all the different subdicts will be empty, like this:
// DebugSwitches{}
// InfoSwitches{}
// OptimisationSwitches{}
// Tolerances{}
// DimensionedConstants{}
//
// We will also merge with other predefined dictionaries as we go along.
// Then, as the libraries will instantiate the various control switches,
// the corresponding subdicts will get populated.
// MB 05/2015
// Fallback to default locations if filename is empty or not valid const string dictEmptySectionStr("{}\n");
if( ! isFile(globControlDictFileName) ) string emptyGlobalDictStr;
globControlDictFileName = findEtcFile("controlDict", true);
// This is a bit less readable than explicitely reusing the hard-coded
// string values, but we then get the added flexibility to change the
// name of the subDicts without too much impact to the rest of source
// code.
const Foam::NamedEnum
<
Foam::debug::globalControlDictSwitchSet,
DIM_GLOBALCONTROLDICTSWITCHSET
>
globalControlDictSwitchSetNames;
forAll(globalControlDictSwitchSetNames, gI)
{
emptyGlobalDictStr +=
globalControlDictSwitchSetNames.names[gI] +
dictEmptySectionStr;
}
controlDictPtr_ = new dictionary controlDictPtr_ = new dictionary
( (
IFstream(globControlDictFileName)() IStringStream(emptyGlobalDictStr)()
); );
} }
@ -117,12 +162,21 @@ Foam::dictionary& Foam::debug::controlDict()
Foam::dictionary& Foam::debug::switchSet Foam::dictionary& Foam::debug::switchSet
( (
const char* subDictName, const globalControlDictSwitchSet globalControlDictSwitchSetName,
dictionary*& subDictPtr dictionary*& subDictPtr
) )
{ {
if (!subDictPtr) if (!subDictPtr)
{ {
const Foam::NamedEnum
<
Foam::debug::globalControlDictSwitchSet,
DIM_GLOBALCONTROLDICTSWITCHSET
>
globalControlDictSwitchSetNames;
word subDictName = globalControlDictSwitchSetNames[globalControlDictSwitchSetName];
entry* ePtr = controlDict().lookupEntryPtr entry* ePtr = controlDict().lookupEntryPtr
( (
subDictName, false, false subDictName, false, false
@ -130,7 +184,7 @@ Foam::dictionary& Foam::debug::switchSet
if (!ePtr || !ePtr->isDict()) if (!ePtr || !ePtr->isDict())
{ {
cerr<< "debug::switchSet(const char*, dictionary*&):\n" cerr<< "debug::switchSet(const globalControlDictSwitchSet, dictionary*&):\n"
<< " Cannot find " << subDictName << " in dictionary " << " Cannot find " << subDictName << " in dictionary "
<< controlDict().name().c_str() << controlDict().name().c_str()
<< std::endl << std::endl; << std::endl << std::endl;
@ -147,30 +201,30 @@ Foam::dictionary& Foam::debug::switchSet
Foam::dictionary& Foam::debug::debugSwitches() Foam::dictionary& Foam::debug::debugSwitches()
{ {
return switchSet("DebugSwitches", debugSwitchesPtr_); return switchSet(debug::DEBUGSWITCHES, debugSwitchesPtr_);
} }
Foam::dictionary& Foam::debug::infoSwitches() Foam::dictionary& Foam::debug::infoSwitches()
{ {
return switchSet("InfoSwitches", infoSwitchesPtr_); return switchSet(debug::INFOSWITCHES, infoSwitchesPtr_);
} }
Foam::dictionary& Foam::debug::optimisationSwitches() Foam::dictionary& Foam::debug::optimisationSwitches()
{ {
return switchSet("OptimisationSwitches", optimisationSwitchesPtr_); return switchSet(debug::OPTIMISATIONSWITCHES, optimisationSwitchesPtr_);
} }
Foam::dictionary& Foam::debug::tolerances() Foam::dictionary& Foam::debug::tolerances()
{ {
return switchSet("Tolerances", tolerancesPtr_); return switchSet(debug::TOLERANCES, tolerancesPtr_);
} }
Foam::dictionary& Foam::debug::constants() Foam::dictionary& Foam::debug::constants()
{ {
return switchSet("DimensionedConstants", constantsPtr_); return switchSet(debug::DIMENSIONEDCONSTANTS, constantsPtr_);
} }
@ -258,8 +312,9 @@ double Foam::debug::constantsFromDict
void Foam::debug::updateCentralDictVars void Foam::debug::updateCentralDictVars
( (
Foam::debug::globalControlDictSwitchSet globalControlDictSwitchSetName, const Foam::debug::globalControlDictSwitchSet globalControlDictSwitchSetName,
Foam::string keyValues const Foam::string& keyValues,
const bool verbose
) )
{ {
Foam::word token; Foam::word token;
@ -298,12 +353,14 @@ void Foam::debug::updateCentralDictVars
) )
) )
{ {
Info << endl if (verbose)
<< "Warning: Modification of DebugSwitches: " {
<< key << endl Info << "CtrlDict : Modification of DebugSwitches: "
<< " Old value: " << oldDebugValue << endl << key
<< " New value: " << newDebugValue << endl << ": old value: " << oldDebugValue
<< ", new value: " << newDebugValue
<< endl; << endl;
}
Foam::debug::debugSwitches().set Foam::debug::debugSwitches().set
( (
@ -382,12 +439,14 @@ void Foam::debug::updateCentralDictVars
) )
) )
{ {
Info << endl if (verbose)
<< "Warning: Modification of InfoSwitches: " {
<< key << endl Info << "CtrlDict : Modification of InfoSwitches: "
<< " Old value: " << oldInfoValue << endl << key
<< " New value: " << newInfoValue << endl << ": old value: " << oldInfoValue
<< ", new value: " << newInfoValue
<< endl; << endl;
}
Foam::debug::infoSwitches().set(key, newInfoValue); Foam::debug::infoSwitches().set(key, newInfoValue);
@ -498,15 +557,17 @@ void Foam::debug::updateCentralDictVars
if(keyIsPresent) if(keyIsPresent)
{ {
Info << endl if (verbose)
<< "Warning: Modification of " {
Info << "CtrlDict : Modification of "
<< "OptimisationSwitches: " << "OptimisationSwitches: "
<< key << endl << key
<< " Old value: " << ": old value: "
<< oldOptimisationValueStr << endl << oldOptimisationValueStr
<< " New value: " << ", new value: "
<< newOptimisationValueStr << endl << newOptimisationValueStr
<< endl; << endl;
}
Foam::debug::optimisationSwitches().set Foam::debug::optimisationSwitches().set
( (
@ -517,7 +578,6 @@ void Foam::debug::updateCentralDictVars
std::list<controlSwitches<int> *> curList = std::list<controlSwitches<int> *> curList =
optimisationSwitchValues[key]; optimisationSwitchValues[key];
std::cout << "curList.size(): " << curList.size() << std::endl;
// Modify all entries for this key // Modify all entries for this key
forAllIter forAllIter
( (
@ -586,14 +646,14 @@ void Foam::debug::updateCentralDictVars
) )
) )
{ {
Info << endl if (verbose)
<< "Warning: Modification of Tolerances: " {
<< key << endl Info << "CtrlDict : Modification of Tolerances: "
<< " Old value: " << key
<< oldTolerancesValue << endl << ": old value: " << oldTolerancesValue
<< " New value: " << ", new value: " << newTolerancesValue
<< newTolerancesValue << endl
<< endl; << endl;
}
Foam::debug::tolerances().set Foam::debug::tolerances().set
( (
@ -672,14 +732,16 @@ void Foam::debug::updateCentralDictVars
) )
) )
{ {
Info << endl if (verbose)
<< "Warning: Modification of DimensionedConstants: " {
<< key << endl Info << "CtrlDict : Modification of DimensionedConstants: "
<< " Old value: " << key
<< oldDimensionedConstantsValue << endl << ": old value: "
<< " New value: " << oldDimensionedConstantsValue
<< newDimensionedConstantsValue << endl << ", new value: "
<< newDimensionedConstantsValue
<< endl; << endl;
}
Foam::dimensionedConstants().set Foam::dimensionedConstants().set
( (
@ -748,4 +810,65 @@ void Foam::debug::updateCentralDictVars
} }
} }
void Foam::debug::updateCentralDictVars
(
const Foam::fileName& fName,
const bool verbose
)
{
// Add a trace to the banner
if (verbose)
{
Info << "CtrlDict : " << fName << endl;
}
if(fName.size() && isFile(fName))
{
const dictionary* optionalGlobControlDictPtr_ = new dictionary
(
IFstream(fName)()
);
const dictionary& optionalGlobControlDict = *optionalGlobControlDictPtr_;
const Foam::NamedEnum
<
Foam::debug::globalControlDictSwitchSet,
DIM_GLOBALCONTROLDICTSWITCHSET
>
globalControlDictSwitchSetNames;
// We merge the content of each subdicts
forAll(globalControlDictSwitchSetNames, gI)
{
string subDictName(globalControlDictSwitchSetNames.names[gI]);
if (optionalGlobControlDict.isDict(subDictName))
{
const dictionary& subD = optionalGlobControlDict.subDict(subDictName);
const wordList toc = subD.toc();
OStringStream keyValuePairs;
forAll(toc, tocI)
{
token value;
subD.lookup(toc[tocI]).read(value);
keyValuePairs << toc[tocI] << "=" << value << ", ";
}
// Update global dictionary
updateCentralDictVars
(
globalControlDictSwitchSetNames[subDictName],
keyValuePairs.str(),
false // keep quiet when using files...
);
}
}
// Cleaning up
delete optionalGlobControlDictPtr_;
}
}
// ************************************************************************* // // ************************************************************************* //

View file

@ -40,8 +40,15 @@ SourceFiles
namespace Foam namespace Foam
{ {
// Number of global controlDict switches sets
// Currently, we have:
// DebugSwitches, InfoSwitches, OptimisationSwitches, Tolerances,
// and DimensionedConstants
#define DIM_GLOBALCONTROLDICTSWITCHSET 5
// Forward declaration of classes // Forward declaration of classes
class dictionary; class dictionary;
class fileName;
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
@ -58,8 +65,6 @@ namespace debug
}; };
//- The central control dictionary. //- The central control dictionary.
// Located in $WM_PROJECT_DIR/etc
// @sa Foam::findEtcFile()
dictionary& controlDict(); dictionary& controlDict();
//- The DebugSwitches sub-dictionary in the central controlDict. //- The DebugSwitches sub-dictionary in the central controlDict.
@ -94,14 +99,26 @@ namespace debug
double constantsFromDict(const char* name, const double defaultValue = 0); double constantsFromDict(const char* name, const double defaultValue = 0);
//- Internal function to lookup a sub-dictionary from controlDict. //- Internal function to lookup a sub-dictionary from controlDict.
dictionary& switchSet(const char* subDictName, dictionary*& subDictPtr); dictionary& switchSet
(
const globalControlDictSwitchSet globalControlDictSwitchSetName,
dictionary*& subDictPtr
);
//- Update central controlDict variables //- Update central controlDict variables
// supported keyValues format: key1=value1,key2=value2 // supported keyValues format: key1=value1,key2=value2
void updateCentralDictVars void updateCentralDictVars
( (
globalControlDictSwitchSet centralDictSwitchSetName, const globalControlDictSwitchSet centralDictSwitchSetName,
Foam::string keyValues const Foam::string& keyValues,
const bool verbose=true
);
//- Update central controlDict variables from a file
void updateCentralDictVars
(
const Foam::fileName& file,
const bool verbose=true
); );
} // End namespace debug } // End namespace debug

View file

@ -38,7 +38,7 @@ dictionary& dimensionedConstants()
{ {
return debug::switchSet return debug::switchSet
( (
"DimensionedConstants", debug::DIMENSIONEDCONSTANTS,
dimensionedConstantsPtr_ dimensionedConstantsPtr_
); );
} }