Commit 1008c0dd authored by mattijs's avatar mattijs
Browse files

allow wildcards in changeDictionary

parent 8977f1f0
......@@ -30,7 +30,8 @@ Description
type in the field and polyMesh/boundary files.
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:
@verbatim
dictionaryReplacement
......@@ -39,6 +40,10 @@ Description
{
boundaryField
{
".*Wall" // entry to change
{
type zeroGradient;
}
movingWall // entry to change
{
type fixedValue;
......@@ -56,6 +61,7 @@ Description
#include "IOobjectList.H"
#include "IOPtrList.H"
#include "volFields.H"
#include "stringListOps.H"
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:
int main(int argc, char *argv[])
{
argList::validOptions.insert("instance", "instance");
argList::validOptions.insert("literalWildcards", "");
#include "addRegionOption.H"
#include "setRootCase.H"
#include "createTime.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 = "";
if (regionName != fvMesh::defaultRegion)
{
......@@ -163,7 +341,7 @@ int main(int argc, char *argv[])
Info<< "Merging entries from " << replaceDict.toc() << endl;
// Merge the replacements in
fieldDict.merge(replaceDict);
merge(fieldDict, replaceDict, literalWildcards);
Info<< "fieldDict:" << fieldDict << endl;
......@@ -229,7 +407,7 @@ int main(int argc, char *argv[])
Info<< "Merging entries from " << replaceDict.toc() << endl;
// Merge the replacements in
fieldDict.merge(replaceDict);
merge(fieldDict, replaceDict, literalWildcards);
Info<< "Writing modified fieldDict " << fieldName << endl;
fieldDict.regIOobject::write();
......
......@@ -16,25 +16,29 @@ FoamFile
dictionaryReplacement
{
boundary
{
".*"
{
type directMappedPatch;
}
}
T
{
internalField uniform 300;
boundaryField
{
minY
{
type fixedValue;
value uniform 500;
}
minZ
".*"
{
type zeroGradient;
}
maxZ
minY
{
type zeroGradient;
type fixedValue;
value uniform 500;
}
}
}
......@@ -45,18 +49,11 @@ dictionaryReplacement
boundaryField
{
minY
{
type zeroGradient;
}
minZ
{
type zeroGradient;
}
maxZ
".*"
{
type zeroGradient;
}
}
}
......@@ -66,18 +63,11 @@ dictionaryReplacement
boundaryField
{
minY
{
type zeroGradient;
}
minZ
{
type zeroGradient;
}
maxZ
".*"
{
type zeroGradient;
}
}
}
......@@ -87,18 +77,11 @@ dictionaryReplacement
boundaryField
{
minY
{
type zeroGradient;
}
minZ
{
type zeroGradient;
}
maxZ
".*"
{
type zeroGradient;
}
}
}
}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment