From 1008c0dd4d853d8a775dc7da25f056e95339a1eb Mon Sep 17 00:00:00 2001 From: mattijs Date: Fri, 18 Sep 2009 08:44:06 +0100 Subject: [PATCH] allow wildcards in changeDictionary --- .../changeDictionary/changeDictionary.C | 184 +++++++++++++++++- .../changeDictionary/changeDictionaryDict | 55 ++---- 2 files changed, 200 insertions(+), 39 deletions(-) diff --git a/applications/utilities/preProcessing/changeDictionary/changeDictionary.C b/applications/utilities/preProcessing/changeDictionary/changeDictionary.C index e8e441b165..3117a5da99 100644 --- a/applications/utilities/preProcessing/changeDictionary/changeDictionary.C +++ b/applications/utilities/preProcessing/changeDictionary/changeDictionary.C @@ -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(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 thisKeysSet; + { + List keys = thisDict.keys(false); + forAll(keys, i) + { + thisKeysSet.insert(keys[i]); + } + } + + // Pass 1. All literal matches + + forAllConstIter(IDLList, 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, 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 + ( + 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(); diff --git a/applications/utilities/preProcessing/changeDictionary/changeDictionaryDict b/applications/utilities/preProcessing/changeDictionary/changeDictionaryDict index 0518358678..164641cc1d 100644 --- a/applications/utilities/preProcessing/changeDictionary/changeDictionaryDict +++ b/applications/utilities/preProcessing/changeDictionary/changeDictionaryDict @@ -16,26 +16,30 @@ FoamFile dictionaryReplacement { + boundary + { + ".*" + { + type directMappedPatch; + } + } + T { internalField uniform 300; boundaryField { + ".*" + { + type zeroGradient; + } + minY { type fixedValue; value uniform 500; } - - minZ - { - type zeroGradient; - } - maxZ - { - type zeroGradient; - } } } @@ -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; } + } } }