ENH: improve dimensions handling for setExpr* utilities (#2014)

- new '-ascii' option to write in ASCII format instead of the
  controlDict setting. This can be useful when generating fields that
  should be parsed with other tools, or for visual inspection.

- correct mismatch of option names and lookups
This commit is contained in:
Mark Olesen 2021-03-18 11:53:42 +01:00
parent cd1350a87f
commit 1ca99a2c94
4 changed files with 92 additions and 44 deletions

View File

@ -24,7 +24,7 @@ License
along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>. along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
Application Application
setExprFields setExprBoundaryFields
Group Group
grpPreProcessingUtilities grpPreProcessingUtilities
@ -61,6 +61,11 @@ int main(int argc, char *argv[])
// No -constant, no special treatment for 0/ // No -constant, no special treatment for 0/
timeSelector::addOptions(false); timeSelector::addOptions(false);
argList::addBoolOption
(
"ascii",
"Write in ASCII format instead of the controlDict setting"
);
argList::addOption argList::addOption
( (
"dict", "dict",
@ -114,10 +119,11 @@ int main(int argc, char *argv[])
instantList times = timeSelector::select0(runTime, args); instantList times = timeSelector::select0(runTime, args);
if (times.size() < 1) if (times.empty())
{ {
FatalErrorInFunction FatalErrorInFunction
<< "No times selected." << exit(FatalError); << "No times selected." << nl
<< exit(FatalError);
} }
#include "createNamedMesh.H" #include "createNamedMesh.H"
@ -125,6 +131,12 @@ int main(int argc, char *argv[])
#include "setSystemMeshDictionaryIO.H" #include "setSystemMeshDictionaryIO.H"
IOdictionary setExprDict(dictIO); IOdictionary setExprDict(dictIO);
IOstreamOption streamOpt(runTime.writeFormat());
if (args.found("ascii"))
{
streamOpt.format(IOstream::ASCII);
}
forAll(times, timei) forAll(times, timei)
{ {
runTime.setTime(times[timei], timei); runTime.setTime(times[timei], timei);
@ -270,7 +282,7 @@ int main(int argc, char *argv[])
if (!dryrun) if (!dryrun)
{ {
Info<< "Write " << fieldDict.filePath() << nl; Info<< "Write " << fieldDict.filePath() << nl;
fieldDict.regIOobject::write(); fieldDict.regIOobject::writeObject(streamOpt, true);
} }
} }
} }

View File

@ -75,11 +75,12 @@ struct setExprFieldsControl
bool dryRun; bool dryRun;
bool debugParsing; bool debugParsing;
bool cacheVariables; bool cacheVariables;
bool useDimension; bool useDimensions;
bool createNew; bool createNew;
bool keepPatches; bool keepPatches;
bool correctPatches; bool correctPatches;
bool correctBCs; bool correctBCs;
IOstreamOption streamOpt;
}; };
@ -218,9 +219,9 @@ void setField
doCorrectBoundaryConditions(ctrl.correctBCs, output); doCorrectBoundaryConditions(ctrl.correctBCs, output);
if (ctrl.useDimension) if (ctrl.useDimensions)
{ {
Info<< "Setting dimension to " << dims << endl; Info<< "Setting dimensions to " << dims << endl;
output.dimensions().reset(dims); output.dimensions().reset(dims);
} }
@ -231,7 +232,7 @@ void setField
else else
{ {
Info<< "Writing to " << output.name() << nl; Info<< "Writing to " << output.name() << nl;
output.write(); output.writeObject(ctrl.streamOpt, true);
} }
} }
@ -539,6 +540,11 @@ int main(int argc, char *argv[])
// No -constant, no special treatment for 0/ // No -constant, no special treatment for 0/
timeSelector::addOptions(false); timeSelector::addOptions(false);
argList::addBoolOption
(
"ascii",
"Write in ASCII format instead of the controlDict setting"
);
argList::addOption argList::addOption
( (
"dict", "dict",
@ -567,7 +573,7 @@ int main(int argc, char *argv[])
( (
"field", "field",
"name", "name",
"The field to overwrite" "The field to create/overwrite"
" (command-line operation)", " (command-line operation)",
true // Advanced option true // Advanced option
); );
@ -589,12 +595,14 @@ int main(int argc, char *argv[])
); );
argList::addOption argList::addOption
( (
"dimension", "dimensions",
"dims", "dims",
"The dimensions for created fields" "The dimensions for created fields"
" (command-line operation)", " (command-line operation)",
true // Advanced option true // Advanced option
); );
argList::addOptionCompat("dimensions", {"dimension", 2012});
argList::addBoolOption argList::addBoolOption
( (
"debug-parser", "debug-parser",
@ -632,7 +640,8 @@ int main(int argc, char *argv[])
argList::addBoolOption argList::addBoolOption
( (
"dummy-phi", "dummy-phi",
"(command-line operation)", "Provide a zero phi field"
" (command-line operation)",
true // Advanced option true // Advanced option
); );
@ -663,13 +672,14 @@ int main(int argc, char *argv[])
instantList times = timeSelector::select0(runTime, args); instantList times = timeSelector::select0(runTime, args);
if (times.size() < 1) if (times.empty())
{ {
FatalErrorInFunction FatalErrorInFunction
<< "No times selected." << exit(FatalError); << "No times selected." << nl
<< exit(FatalError);
} }
// Disable dimension checking during operation // Disable dimension checking during operations
dimensionSet::debug = false; dimensionSet::debug = false;
#include "createNamedMesh.H" #include "createNamedMesh.H"
@ -684,38 +694,53 @@ int main(int argc, char *argv[])
if (useCommandArgs) if (useCommandArgs)
{ {
bool fatalCombination = false;
if (args.found("dict")) if (args.found("dict"))
{ {
fatalCombination = true;
FatalErrorInFunction FatalErrorInFunction
<< "Cannot specify both dictionary and command-line arguments" << "Cannot specify both dictionary and command-line arguments"
<< nl << nl << endl;
<< endl;
} }
if (args.found("create") && args.found("keepPatches")) if (args.found("create") && args.found("keepPatches"))
{ {
fatalCombination = true;
FatalErrorInFunction FatalErrorInFunction
<< "Cannot specify both 'create' and 'keepPatches'" << nl << "Cannot specify both 'create' and 'keepPatches'" << nl
<< endl; << endl;
} }
if (!args.found("expression"))
{
fatalCombination = true;
FatalErrorInFunction
<< "Missing mandatory 'expression' option'" << nl
<< endl;
}
if (fatalCombination)
{
FatalError
<< exit(FatalError);
}
} }
else else
{ {
// Carp about inapplicable options // Carp about inapplicable options (non-fatal)
wordHashSet badOptions wordHashSet badOptions
({ ({
"create", "keepPatches", "valuePatches", "create", "keepPatches", "value-patches",
"dimension", "condition", "expression" "condition", "expression", "dimensions"
}); });
badOptions.retain(args.options()); badOptions.retain(args.options());
if (!badOptions.empty()) if (!badOptions.empty())
{ {
// Non-fatal (warning)
FatalErrorInFunction FatalErrorInFunction
<< "Using a dictionary. Cannot specify command options:" << nl << "Using a dictionary. Cannot specify these options:" << nl
<< nl
<< flatOutput(badOptions.sortedToc()) << nl << flatOutput(badOptions.sortedToc()) << nl
<< endl; << endl;
} }
@ -736,7 +761,7 @@ int main(int argc, char *argv[])
if (timei == 0) if (timei == 0)
{ {
wordList preloadFields; wordList preloadFields;
args.readListIfPresent("load-fields", preloadFieldNames); args.readListIfPresent("load-fields", preloadFields);
readFieldsHandler(mesh).execute(preloadFields); readFieldsHandler(mesh).execute(preloadFields);
} }
@ -766,7 +791,7 @@ int main(int argc, char *argv[])
runTime.functionObjects().start(); runTime.functionObjects().start();
} }
if (args.found("field")) if (useCommandArgs)
{ {
const word fieldName(args.get<word>("field")); const word fieldName(args.get<word>("field"));
@ -783,26 +808,27 @@ int main(int argc, char *argv[])
ctrl.keepPatches = args.found("keepPatches"); ctrl.keepPatches = args.found("keepPatches");
ctrl.correctPatches = !args.found("noCorrectPatches"); ctrl.correctPatches = !args.found("noCorrectPatches");
ctrl.correctBCs = args.found("correctResultBoundaryFields"); ctrl.correctBCs = args.found("correctResultBoundaryFields");
ctrl.useDimension = args.found("dimension"); ctrl.useDimensions = args.found("dimensions");
ctrl.streamOpt.format(runTime.writeFormat());
if (args.found("ascii"))
{
ctrl.streamOpt.format(IOstream::ASCII);
}
expressions::exprString expressions::exprString
expression expression
( (
args[expression], args["expression"],
dictionary::null dictionary::null
); );
expressions::exprString condition; expressions::exprString condition;
if (args.found("condition")) args.readIfPresent("condition", condition);
{
args.readIfPresent("condition", condition);
}
dimensionSet dims; dimensionSet dims;
if (ctrl.useDimensions)
if (ctrl.useDimension)
{ {
ITstream is(args.lookup("dimension")); ITstream is(args.lookup("dimensions"));
is >> dims; is >> dims;
} }
@ -814,7 +840,7 @@ int main(int argc, char *argv[])
condition, condition,
dictionary::null, dictionary::null,
dims, dims,
args.getList<word>("valuePatches", false), args.getList<word>("value-patches", false),
ctrl ctrl
); );
@ -852,8 +878,14 @@ int main(int argc, char *argv[])
ctrl.createNew = dict.getOrDefault("create", false); ctrl.createNew = dict.getOrDefault("create", false);
ctrl.keepPatches = dict.getOrDefault("keepPatches", false); ctrl.keepPatches = dict.getOrDefault("keepPatches", false);
ctrl.correctPatches = !args.found("noCorrectPatches"); ctrl.correctPatches = !args.found("noCorrectPatches");
ctrl.correctBCs = args.found("correctResultBoundaryFields"); ctrl.correctBCs = args.found("correctResultBoundaryFields");
ctrl.streamOpt.format(runTime.writeFormat());
if (args.found("ascii"))
{
ctrl.streamOpt.format(IOstream::ASCII);
}
if (ctrl.createNew && ctrl.keepPatches) if (ctrl.createNew && ctrl.keepPatches)
{ {
@ -891,17 +923,20 @@ int main(int argc, char *argv[])
); );
} }
ctrl.useDimension = dict.found("dimension");
dimensionSet dims; dimensionSet dims;
if (ctrl.useDimension)
{ {
dict.lookup("dimension") >> dims; const entry* dimPtr = dict.findCompat
(
"dimensions", {{"dimension", 2012}},
keyType::LITERAL
);
if (dimPtr)
{
dimPtr->stream() >> dims;
}
ctrl.useDimensions = bool(dimPtr);
} }
wordList valuePatches;
dict.readIfPresent("valuePatches", valuePatches);
if (verbose && !timei) if (verbose && !timei)
{ {
// Report once // Report once
@ -916,7 +951,7 @@ int main(int argc, char *argv[])
condition, condition,
dict, dict,
dims, dims,
valuePatches, dict.getOrDefault<wordList>("valuePatches", wordList()),
ctrl ctrl
); );

View File

@ -25,7 +25,7 @@ updateBCs
{ {
target value; target value;
variables ( "rho=1.2" ); variables ( "rho=1.2" );
expression #{ 0.5*rho*cbrt(mag(U)) #}; expression #{ 0.5*rho*pow(mag(U),3) #};
} }
expressions expressions

View File

@ -20,7 +20,8 @@ readFields ( U );
_value1 _value1
{ {
variables ( "rho=1.2" ); variables ( "rho=1.2" );
expression #{ 0.5*rho*cbrt(mag(U)) #}; expression #{ 0.5*rho*pow(mag(U),3) #};
dimensions [ kg s^-3 ];
} }
expressions expressions