From 38b99504c01e7b19b9af7cb54793fd72c03e455a Mon Sep 17 00:00:00 2001 From: Henry Weller Date: Wed, 18 May 2016 12:20:03 +0100 Subject: [PATCH] dynamicCode: the "code" entry is now optional codedFunctionObject: Added the "codeWrite" entry for the "write" function for consistency. The previous method of using the "code" entry for the "write" function was inconsistent and very confusing. --- .../dynamicCode/functionObjectTemplate.C | 45 +++++-------- .../dynamicCode/functionObjectTemplate.H | 3 - .../dynamicCode/dynamicCodeContext.C | 38 +++++------ .../dynamicCode/dynamicCodeContext.H | 7 +- .../codedFunctionObject/codedFunctionObject.C | 66 ++++++++----------- .../codedFunctionObject/codedFunctionObject.H | 6 +- .../potentialFoam/cylinder/system/controlDict | 5 +- 7 files changed, 70 insertions(+), 100 deletions(-) diff --git a/etc/codeTemplates/dynamicCode/functionObjectTemplate.C b/etc/codeTemplates/dynamicCode/functionObjectTemplate.C index 612d4f1b53..f18764fa5b 100644 --- a/etc/codeTemplates/dynamicCode/functionObjectTemplate.C +++ b/etc/codeTemplates/dynamicCode/functionObjectTemplate.C @@ -136,6 +136,21 @@ bool ${typeName}FunctionObject::execute(const bool postProcess) } +bool ${typeName}FunctionObject::write(const bool postProcess) +{ + if (${verbose:-false}) + { + Info<<"write ${typeName} sha1: ${SHA1sum}\n"; + } + +//{{{ begin code + ${codeWrite} +//}}} end code + + return true; +} + + bool ${typeName}FunctionObject::end() { if (${verbose:-false}) @@ -151,36 +166,6 @@ bool ${typeName}FunctionObject::end() } -bool ${typeName}FunctionObject::timeSet() -{ - if (${verbose:-false}) - { - Info<<"timeSet ${typeName} sha1: ${SHA1sum}\n"; - } - -//{{{ begin codeTime - ${codeTimeSet} -//}}} end code - - return true; -} - - -bool ${typeName}FunctionObject::write(const bool postProcess) -{ - if (${verbose:-false}) - { - Info<<"write ${typeName} sha1: ${SHA1sum}\n"; - } - -//{{{ begin code - ${code} -//}}} end code - - return true; -} - - // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // } // End namespace Foam diff --git a/etc/codeTemplates/dynamicCode/functionObjectTemplate.H b/etc/codeTemplates/dynamicCode/functionObjectTemplate.H index 17dd441436..0662516a0a 100644 --- a/etc/codeTemplates/dynamicCode/functionObjectTemplate.H +++ b/etc/codeTemplates/dynamicCode/functionObjectTemplate.H @@ -107,9 +107,6 @@ public: //- Write, execute the "writeCalls" virtual bool write(const bool postProcess = false); - - //- Called when time was set at the end of the Time::operator++ - virtual bool timeSet(); }; diff --git a/src/OpenFOAM/db/dynamicLibrary/dynamicCode/dynamicCodeContext.C b/src/OpenFOAM/db/dynamicLibrary/dynamicCode/dynamicCodeContext.C index 22c49df9c0..6f41078867 100644 --- a/src/OpenFOAM/db/dynamicLibrary/dynamicCode/dynamicCodeContext.C +++ b/src/OpenFOAM/db/dynamicLibrary/dynamicCode/dynamicCodeContext.C @@ -2,7 +2,7 @@ ========= | \\ / F ield | OpenFOAM: The Open Source CFD Toolbox \\ / O peration | - \\ / A nd | Copyright (C) 2011-2012 OpenFOAM Foundation + \\ / A nd | Copyright (C) 2011-2016 OpenFOAM Foundation \\/ M anipulation | ------------------------------------------------------------------------------- License @@ -27,7 +27,6 @@ License #include "stringOps.H" #include "OSHA1stream.H" - // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * // Foam::dynamicCodeContext::dynamicCodeContext(const dictionary& dict) @@ -39,19 +38,24 @@ Foam::dynamicCodeContext::dynamicCodeContext(const dictionary& dict) options_(), libs_() { - // expand dictionary entries + // Expand dictionary entries - { - const entry& codeEntry = dict.lookupEntry("code", false, false); - code_ = stringOps::trim(codeEntry.stream()); - stringOps::inplaceExpand(code_, dict); - } - - // note: removes any leading/trailing whitespace + // Note: removes any leading/trailing whitespace // - necessary for compilation options, convenient for includes // and body. - // optional + const entry* codePtr = dict.lookupEntryPtr + ( + "code", + false, + false + ); + if (codePtr) + { + code_ = stringOps::trim(codePtr->stream()); + stringOps::inplaceExpand(code_, dict); + } + const entry* includePtr = dict.lookupEntryPtr ( "codeInclude", @@ -64,7 +68,6 @@ Foam::dynamicCodeContext::dynamicCodeContext(const dictionary& dict) stringOps::inplaceExpand(include_, dict); } - // optional const entry* optionsPtr = dict.lookupEntryPtr ( "codeOptions", @@ -77,7 +80,6 @@ Foam::dynamicCodeContext::dynamicCodeContext(const dictionary& dict) stringOps::inplaceExpand(options_, dict); } - // optional const entry* libsPtr = dict.lookupEntryPtr("codeLibs", false, false); if (libsPtr) { @@ -85,7 +87,6 @@ Foam::dynamicCodeContext::dynamicCodeContext(const dictionary& dict) stringOps::inplaceExpand(libs_, dict); } - // optional const entry* localPtr = dict.lookupEntryPtr("localCode", false, false); if (localPtr) { @@ -93,20 +94,20 @@ Foam::dynamicCodeContext::dynamicCodeContext(const dictionary& dict) stringOps::inplaceExpand(localCode_, dict); } - // calculate SHA1 digest from include, options, localCode, code + // Calculate SHA1 digest from include, options, localCode, code OSHA1stream os; os << include_ << options_ << libs_ << localCode_ << code_; sha1_ = os.digest(); - // Add line number after calculating sha1 since includes processorDDD // in path which differs between processors. + if (codePtr) { - const entry& codeEntry = dict.lookupEntry("code", false, false); - addLineDirective(code_, codeEntry.startLineNumber(), dict.name()); + addLineDirective(code_, codePtr->startLineNumber(), dict.name()); } + if (includePtr) { addLineDirective(include_, includePtr->startLineNumber(), dict.name()); @@ -114,7 +115,6 @@ Foam::dynamicCodeContext::dynamicCodeContext(const dictionary& dict) // Do not add line directive to options_ (Make/options) and libs since // they are preprocessed as a single line at this point. Can be fixed. - if (localPtr) { addLineDirective(localCode_, localPtr->startLineNumber(), dict.name()); diff --git a/src/OpenFOAM/db/dynamicLibrary/dynamicCode/dynamicCodeContext.H b/src/OpenFOAM/db/dynamicLibrary/dynamicCode/dynamicCodeContext.H index f447e612a7..c114d1303c 100644 --- a/src/OpenFOAM/db/dynamicLibrary/dynamicCode/dynamicCodeContext.H +++ b/src/OpenFOAM/db/dynamicLibrary/dynamicCode/dynamicCodeContext.H @@ -2,7 +2,7 @@ ========= | \\ / F ield | OpenFOAM: The Open Source CFD Toolbox \\ / O peration | - \\ / A nd | Copyright (C) 2011-2012 OpenFOAM Foundation + \\ / A nd | Copyright (C) 2011-2016 OpenFOAM Foundation \\/ M anipulation | ------------------------------------------------------------------------------- License @@ -54,7 +54,7 @@ class dynamicCodeContext //- The parent dictionary context const dictionary& dict_; - //- Mandatory "code" entry + //- Optional "code" entry string code_; //- Optional "localCode" entry @@ -72,6 +72,7 @@ class dynamicCodeContext //- Calculated SHA1Digest SHA1Digest sha1_; + public: // Constructors @@ -79,6 +80,7 @@ public: //- Construct from a dictionary dynamicCodeContext(const dictionary&); + // Member functions //- Return the parent dictionary context @@ -130,7 +132,6 @@ public: const label lineNum, const fileName& name ); - }; diff --git a/src/postProcessing/functionObjects/utilities/codedFunctionObject/codedFunctionObject.C b/src/postProcessing/functionObjects/utilities/codedFunctionObject/codedFunctionObject.C index 35342d1b39..8eb568a95c 100644 --- a/src/postProcessing/functionObjects/utilities/codedFunctionObject/codedFunctionObject.C +++ b/src/postProcessing/functionObjects/utilities/codedFunctionObject/codedFunctionObject.C @@ -57,12 +57,11 @@ void Foam::codedFunctionObject::prepare { // Set additional rewrite rules dynCode.setFilterVariable("typeName", redirectType_); + dynCode.setFilterVariable("codeData", codeData_); dynCode.setFilterVariable("codeRead", codeRead_); dynCode.setFilterVariable("codeExecute", codeExecute_); + dynCode.setFilterVariable("codeWrite", codeWrite_); dynCode.setFilterVariable("codeEnd", codeEnd_); - dynCode.setFilterVariable("codeData", codeData_); - dynCode.setFilterVariable("codeTimeSet", codeTimeSet_); - //dynCode.setFilterVariable("codeWrite", codeWrite_); // Compile filtered C template dynCode.addCompileFile("functionObjectTemplate.C"); @@ -71,9 +70,9 @@ void Foam::codedFunctionObject::prepare dynCode.addCopyFile("functionObjectTemplate.H"); // Debugging: make BC verbose - // dynCode.setFilterVariable("verbose", "true"); - // Info<<"compile " << redirectType_ << " sha1: " - // << context.sha1() << endl; + // dynCode.setFilterVariable("verbose", "true"); + // Info<<"compile " << redirectType_ << " sha1: " + // << context.sha1() << endl; // Define Make/options dynCode.setMakeOptions @@ -81,12 +80,12 @@ void Foam::codedFunctionObject::prepare "EXE_INC = -g \\\n" "-I$(LIB_SRC)/finiteVolume/lnInclude \\\n" "-I$(LIB_SRC)/meshTools/lnInclude \\\n" - + context.options() - + "\n\nLIB_LIBS = \\\n" - + " -lOpenFOAM \\\n" - + " -lfiniteVolume \\\n" - + " -lmeshTools \\\n" - + context.libs() + + context.options() + + "\n\nLIB_LIBS = \\\n" + + " -lOpenFOAM \\\n" + + " -lfiniteVolume \\\n" + + " -lmeshTools \\\n" + + context.libs() ); } @@ -183,13 +182,6 @@ bool Foam::codedFunctionObject::end() } -bool Foam::codedFunctionObject::timeSet() -{ - updateLibrary(redirectType_); - return redirectFunctionObject().timeSet(); -} - - bool Foam::codedFunctionObject::read(const dictionary& dict) { dict.lookup("redirectType") >> redirectType_; @@ -248,6 +240,24 @@ bool Foam::codedFunctionObject::read(const dictionary& dict) ); } + const entry* writePtr = dict.lookupEntryPtr + ( + "codeWrite", + false, + false + ); + if (writePtr) + { + codeWrite_ = stringOps::trim(writePtr->stream()); + stringOps::inplaceExpand(codeWrite_, dict); + dynamicCodeContext::addLineDirective + ( + codeWrite_, + writePtr->startLineNumber(), + dict.name() + ); + } + const entry* endPtr = dict.lookupEntryPtr ( "codeEnd", @@ -266,24 +276,6 @@ bool Foam::codedFunctionObject::read(const dictionary& dict) ); } - const entry* timeSetPtr = dict.lookupEntryPtr - ( - "codeTimeSet", - false, - false - ); - if (timeSetPtr) - { - codeTimeSet_ = stringOps::trim(timeSetPtr->stream()); - stringOps::inplaceExpand(codeTimeSet_, dict); - dynamicCodeContext::addLineDirective - ( - codeTimeSet_, - timeSetPtr->startLineNumber(), - dict.name() - ); - } - updateLibrary(redirectType_); return redirectFunctionObject().read(dict); } diff --git a/src/postProcessing/functionObjects/utilities/codedFunctionObject/codedFunctionObject.H b/src/postProcessing/functionObjects/utilities/codedFunctionObject/codedFunctionObject.H index a2197b23e0..3e0377dcdf 100644 --- a/src/postProcessing/functionObjects/utilities/codedFunctionObject/codedFunctionObject.H +++ b/src/postProcessing/functionObjects/utilities/codedFunctionObject/codedFunctionObject.H @@ -41,7 +41,6 @@ Description codeRead : c++; upon functionObject::read(); codeEnd : c++; upon functionObject::end(); codeData : c++; local member data (null constructed); - codeTimeSet : c++; upon functionObject::timeSet(); localCode : c++; local static functions Example of function object specification: @@ -108,8 +107,8 @@ protected: string codeData_; string codeRead_; string codeExecute_; + string codeWrite_; string codeEnd_; - string codeTimeSet_; //- Underlying functionObject mutable autoPtr redirectFunctionObjectPtr_; @@ -182,9 +181,6 @@ public: // By default it simply calls execute(). virtual bool end(); - //- Called when time was set at the end of the Time::operator++ - virtual bool timeSet(); - //- Read and set the function object if its data have changed virtual bool read(const dictionary&); }; diff --git a/tutorials/basic/potentialFoam/cylinder/system/controlDict b/tutorials/basic/potentialFoam/cylinder/system/controlDict index c326838ddb..88dbc9a997 100644 --- a/tutorials/basic/potentialFoam/cylinder/system/controlDict +++ b/tutorials/basic/potentialFoam/cylinder/system/controlDict @@ -54,11 +54,10 @@ functions type coded; - writeControl timeStep; - // Name of on-the-fly generated functionObject redirectType error; - code + + codeEnd #{ // Lookup U Info<< "Looking up field U\n" << endl;