mirror of
https://develop.openfoam.com/Development/openfoam.git
synced 2025-11-28 03:28:01 +00:00
allow wildcards in changeDictionary
This commit is contained in:
@ -30,7 +30,8 @@ Description
|
|||||||
type in the field and polyMesh/boundary files.
|
type in the field and polyMesh/boundary files.
|
||||||
|
|
||||||
Reads dictionaries (fields) and entries to change from a dictionary.
|
Reads dictionaries (fields) and entries to change from a dictionary.
|
||||||
E.g. to make the @em movingWall a @em fixedValue for @em p, the
|
E.g. to make the @em movingWall a @em fixedValue for @em p but all other
|
||||||
|
@em Walls a zeroGradient boundary condition, the
|
||||||
@c system/changeDictionaryDict would contain the following:
|
@c system/changeDictionaryDict would contain the following:
|
||||||
@verbatim
|
@verbatim
|
||||||
dictionaryReplacement
|
dictionaryReplacement
|
||||||
@ -39,6 +40,10 @@ Description
|
|||||||
{
|
{
|
||||||
boundaryField
|
boundaryField
|
||||||
{
|
{
|
||||||
|
".*Wall" // entry to change
|
||||||
|
{
|
||||||
|
type zeroGradient;
|
||||||
|
}
|
||||||
movingWall // entry to change
|
movingWall // entry to change
|
||||||
{
|
{
|
||||||
type fixedValue;
|
type fixedValue;
|
||||||
@ -56,6 +61,7 @@ Description
|
|||||||
#include "IOobjectList.H"
|
#include "IOobjectList.H"
|
||||||
#include "IOPtrList.H"
|
#include "IOPtrList.H"
|
||||||
#include "volFields.H"
|
#include "volFields.H"
|
||||||
|
#include "stringListOps.H"
|
||||||
|
|
||||||
using namespace Foam;
|
using namespace Foam;
|
||||||
|
|
||||||
@ -68,17 +74,189 @@ namespace Foam
|
|||||||
|
|
||||||
|
|
||||||
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
|
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
|
||||||
|
|
||||||
|
bool merge(dictionary&, const dictionary&, const bool);
|
||||||
|
|
||||||
|
|
||||||
|
// Add thisEntry to dictionary thisDict.
|
||||||
|
bool addEntry
|
||||||
|
(
|
||||||
|
dictionary& thisDict,
|
||||||
|
entry& thisEntry,
|
||||||
|
const entry& mergeEntry,
|
||||||
|
const bool literalWildcards
|
||||||
|
)
|
||||||
|
{
|
||||||
|
bool changed = false;
|
||||||
|
|
||||||
|
// Recursively merge sub-dictionaries
|
||||||
|
// TODO: merge without copying
|
||||||
|
if (thisEntry.isDict() && mergeEntry.isDict())
|
||||||
|
{
|
||||||
|
if
|
||||||
|
(
|
||||||
|
merge
|
||||||
|
(
|
||||||
|
const_cast<dictionary&>(thisEntry.dict()),
|
||||||
|
mergeEntry.dict(),
|
||||||
|
literalWildcards
|
||||||
|
)
|
||||||
|
)
|
||||||
|
{
|
||||||
|
changed = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
// Should use in-place modification instead of adding
|
||||||
|
thisDict.add(mergeEntry.clone(thisDict).ptr(), true);
|
||||||
|
changed = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
return changed;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// Dictionary merging/editing.
|
||||||
|
// literalWildcards:
|
||||||
|
// - true: behave like dictionary::merge, i.e. add wildcards just like
|
||||||
|
// any other key.
|
||||||
|
// - false : interpret wildcard as a rule for items to be matched.
|
||||||
|
bool merge
|
||||||
|
(
|
||||||
|
dictionary& thisDict,
|
||||||
|
const dictionary& mergeDict,
|
||||||
|
const bool literalWildcards
|
||||||
|
)
|
||||||
|
{
|
||||||
|
bool changed = false;
|
||||||
|
|
||||||
|
// Save current (non-wildcard) keys before adding items.
|
||||||
|
HashSet<word> thisKeysSet;
|
||||||
|
{
|
||||||
|
List<keyType> keys = thisDict.keys(false);
|
||||||
|
forAll(keys, i)
|
||||||
|
{
|
||||||
|
thisKeysSet.insert(keys[i]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Pass 1. All literal matches
|
||||||
|
|
||||||
|
forAllConstIter(IDLList<entry>, mergeDict, mergeIter)
|
||||||
|
{
|
||||||
|
const keyType& key = mergeIter().keyword();
|
||||||
|
|
||||||
|
if (literalWildcards || !key.isPattern())
|
||||||
|
{
|
||||||
|
entry* entryPtr = thisDict.lookupEntryPtr
|
||||||
|
(
|
||||||
|
key,
|
||||||
|
false, // recursive
|
||||||
|
false // patternMatch
|
||||||
|
);
|
||||||
|
|
||||||
|
if (entryPtr)
|
||||||
|
{
|
||||||
|
|
||||||
|
// Mark thisDict entry as having been match for wildcard
|
||||||
|
// handling later on.
|
||||||
|
thisKeysSet.erase(entryPtr->keyword());
|
||||||
|
|
||||||
|
if
|
||||||
|
(
|
||||||
|
addEntry
|
||||||
|
(
|
||||||
|
thisDict,
|
||||||
|
*entryPtr,
|
||||||
|
mergeIter(),
|
||||||
|
literalWildcards
|
||||||
|
)
|
||||||
|
)
|
||||||
|
{
|
||||||
|
changed = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
// not found - just add
|
||||||
|
thisDict.add(mergeIter().clone(thisDict).ptr());
|
||||||
|
changed = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// Pass 2. Wildcard matches (if any) on any non-match keys.
|
||||||
|
|
||||||
|
if (!literalWildcards && thisKeysSet.size() > 0)
|
||||||
|
{
|
||||||
|
wordList thisKeys(thisKeysSet.toc());
|
||||||
|
|
||||||
|
forAllConstIter(IDLList<entry>, mergeDict, mergeIter)
|
||||||
|
{
|
||||||
|
const keyType& key = mergeIter().keyword();
|
||||||
|
|
||||||
|
if (key.isPattern())
|
||||||
|
{
|
||||||
|
// Find all matching entries in the original thisDict
|
||||||
|
|
||||||
|
labelList matches = findStrings(key, thisKeys);
|
||||||
|
|
||||||
|
forAll(matches, i)
|
||||||
|
{
|
||||||
|
label matchI = matches[i];
|
||||||
|
|
||||||
|
entry& thisEntry = const_cast<entry&>
|
||||||
|
(
|
||||||
|
thisDict.lookupEntry(thisKeys[matchI], false, false)
|
||||||
|
);
|
||||||
|
|
||||||
|
if
|
||||||
|
(
|
||||||
|
addEntry
|
||||||
|
(
|
||||||
|
thisDict,
|
||||||
|
thisEntry,
|
||||||
|
mergeIter(),
|
||||||
|
literalWildcards
|
||||||
|
)
|
||||||
|
)
|
||||||
|
{
|
||||||
|
changed = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return changed;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
// Main program:
|
// Main program:
|
||||||
|
|
||||||
int main(int argc, char *argv[])
|
int main(int argc, char *argv[])
|
||||||
{
|
{
|
||||||
argList::validOptions.insert("instance", "instance");
|
argList::validOptions.insert("instance", "instance");
|
||||||
|
argList::validOptions.insert("literalWildcards", "");
|
||||||
#include "addRegionOption.H"
|
#include "addRegionOption.H"
|
||||||
|
|
||||||
#include "setRootCase.H"
|
#include "setRootCase.H"
|
||||||
#include "createTime.H"
|
#include "createTime.H"
|
||||||
#include "createNamedMesh.H"
|
#include "createNamedMesh.H"
|
||||||
|
|
||||||
|
bool literalWildcards = args.optionFound("literalWildcards");
|
||||||
|
|
||||||
|
if (literalWildcards)
|
||||||
|
{
|
||||||
|
Info<< "Not interpreting any wildcards in the changeDictionaryDict."
|
||||||
|
<< endl
|
||||||
|
<< "Instead they are handled as any other entry, i.e. added if"
|
||||||
|
<< " not present." << endl;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
fileName regionPrefix = "";
|
fileName regionPrefix = "";
|
||||||
if (regionName != fvMesh::defaultRegion)
|
if (regionName != fvMesh::defaultRegion)
|
||||||
{
|
{
|
||||||
@ -163,7 +341,7 @@ int main(int argc, char *argv[])
|
|||||||
Info<< "Merging entries from " << replaceDict.toc() << endl;
|
Info<< "Merging entries from " << replaceDict.toc() << endl;
|
||||||
|
|
||||||
// Merge the replacements in
|
// Merge the replacements in
|
||||||
fieldDict.merge(replaceDict);
|
merge(fieldDict, replaceDict, literalWildcards);
|
||||||
|
|
||||||
Info<< "fieldDict:" << fieldDict << endl;
|
Info<< "fieldDict:" << fieldDict << endl;
|
||||||
|
|
||||||
@ -229,7 +407,7 @@ int main(int argc, char *argv[])
|
|||||||
Info<< "Merging entries from " << replaceDict.toc() << endl;
|
Info<< "Merging entries from " << replaceDict.toc() << endl;
|
||||||
|
|
||||||
// Merge the replacements in
|
// Merge the replacements in
|
||||||
fieldDict.merge(replaceDict);
|
merge(fieldDict, replaceDict, literalWildcards);
|
||||||
|
|
||||||
Info<< "Writing modified fieldDict " << fieldName << endl;
|
Info<< "Writing modified fieldDict " << fieldName << endl;
|
||||||
fieldDict.regIOobject::write();
|
fieldDict.regIOobject::write();
|
||||||
|
|||||||
@ -16,26 +16,30 @@ FoamFile
|
|||||||
|
|
||||||
dictionaryReplacement
|
dictionaryReplacement
|
||||||
{
|
{
|
||||||
|
boundary
|
||||||
|
{
|
||||||
|
".*"
|
||||||
|
{
|
||||||
|
type directMappedPatch;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
T
|
T
|
||||||
{
|
{
|
||||||
internalField uniform 300;
|
internalField uniform 300;
|
||||||
|
|
||||||
boundaryField
|
boundaryField
|
||||||
{
|
{
|
||||||
|
".*"
|
||||||
|
{
|
||||||
|
type zeroGradient;
|
||||||
|
}
|
||||||
|
|
||||||
minY
|
minY
|
||||||
{
|
{
|
||||||
type fixedValue;
|
type fixedValue;
|
||||||
value uniform 500;
|
value uniform 500;
|
||||||
}
|
}
|
||||||
|
|
||||||
minZ
|
|
||||||
{
|
|
||||||
type zeroGradient;
|
|
||||||
}
|
|
||||||
maxZ
|
|
||||||
{
|
|
||||||
type zeroGradient;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -45,18 +49,11 @@ dictionaryReplacement
|
|||||||
|
|
||||||
boundaryField
|
boundaryField
|
||||||
{
|
{
|
||||||
minY
|
".*"
|
||||||
{
|
|
||||||
type zeroGradient;
|
|
||||||
}
|
|
||||||
minZ
|
|
||||||
{
|
|
||||||
type zeroGradient;
|
|
||||||
}
|
|
||||||
maxZ
|
|
||||||
{
|
{
|
||||||
type zeroGradient;
|
type zeroGradient;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -66,18 +63,11 @@ dictionaryReplacement
|
|||||||
|
|
||||||
boundaryField
|
boundaryField
|
||||||
{
|
{
|
||||||
minY
|
".*"
|
||||||
{
|
|
||||||
type zeroGradient;
|
|
||||||
}
|
|
||||||
minZ
|
|
||||||
{
|
|
||||||
type zeroGradient;
|
|
||||||
}
|
|
||||||
maxZ
|
|
||||||
{
|
{
|
||||||
type zeroGradient;
|
type zeroGradient;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -87,18 +77,11 @@ dictionaryReplacement
|
|||||||
|
|
||||||
boundaryField
|
boundaryField
|
||||||
{
|
{
|
||||||
minY
|
".*"
|
||||||
{
|
|
||||||
type zeroGradient;
|
|
||||||
}
|
|
||||||
minZ
|
|
||||||
{
|
|
||||||
type zeroGradient;
|
|
||||||
}
|
|
||||||
maxZ
|
|
||||||
{
|
{
|
||||||
type zeroGradient;
|
type zeroGradient;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user