From 4cbb849edaf14738de8cd25fbd31bb04e5ae7d91 Mon Sep 17 00:00:00 2001 From: Henry Weller Date: Thu, 6 Feb 2020 16:04:21 +0000 Subject: [PATCH] codedFixedValueFvPatchField: Updated and simplified using the new CodedBase class reducing code duplication and maintenance overhead. --- .../db/dynamicLibrary/codedBase/CodedBase.H | 4 + .../Function1/Coded/CodedFunction1.C | 27 ++- .../Function1/Coded/CodedFunction1.H | 2 +- .../codedFixedValueFvPatchField.C | 215 ++++-------------- .../codedFixedValueFvPatchField.H | 47 ++-- .../codedFixedValueFvPatchFields.C | 16 +- .../codedFunctionObject/codedFunctionObject.C | 9 +- 7 files changed, 94 insertions(+), 226 deletions(-) diff --git a/src/OpenFOAM/db/dynamicLibrary/codedBase/CodedBase.H b/src/OpenFOAM/db/dynamicLibrary/codedBase/CodedBase.H index ad84b742fe..af4ccc9448 100644 --- a/src/OpenFOAM/db/dynamicLibrary/codedBase/CodedBase.H +++ b/src/OpenFOAM/db/dynamicLibrary/codedBase/CodedBase.H @@ -85,6 +85,10 @@ public: // Constructors + //- Construct null + CodedBase() + {} + //- Construct from dictionary CodedBase(const dictionary& dict); diff --git a/src/OpenFOAM/primitives/functions/Function1/Coded/CodedFunction1.C b/src/OpenFOAM/primitives/functions/Function1/Coded/CodedFunction1.C index fac77d65c1..5f07d8fe7a 100644 --- a/src/OpenFOAM/primitives/functions/Function1/Coded/CodedFunction1.C +++ b/src/OpenFOAM/primitives/functions/Function1/Coded/CodedFunction1.C @@ -59,20 +59,23 @@ void Foam::Function1s::Coded::prepare // Copy filtered H template dynCode.addCopyFile(codeTemplateH); - // debugging: make verbose - // dynCode.setFilterVariable("verbose", "true"); - // Info<<"compile " << codeName() << " sha1: " - // << context.sha1() << endl; + // Debugging: make verbose + if (debug) + { + dynCode.setFilterVariable("verbose", "true"); + Info<<"compile " << codeName() << " sha1: " + << context.sha1() << endl; + } - // define Make/options + // Define Make/options dynCode.setMakeOptions - ( - "EXE_INC = -g \\\n" - + context.options() - + "\n\nLIB_LIBS = \\\n" - + " -lOpenFOAM \\\n" - + context.libs() - ); + ( + "EXE_INC = -g \\\n" + + context.options() + + "\n\nLIB_LIBS = \\\n" + + " -lOpenFOAM \\\n" + + context.libs() + ); } diff --git a/src/OpenFOAM/primitives/functions/Function1/Coded/CodedFunction1.H b/src/OpenFOAM/primitives/functions/Function1/Coded/CodedFunction1.H index dc785b429f..348e68ea00 100644 --- a/src/OpenFOAM/primitives/functions/Function1/Coded/CodedFunction1.H +++ b/src/OpenFOAM/primitives/functions/Function1/Coded/CodedFunction1.H @@ -78,7 +78,7 @@ namespace Function1s { /*---------------------------------------------------------------------------*\ - Class coded Declaration + Class coded Declaration \*---------------------------------------------------------------------------*/ class coded diff --git a/src/finiteVolume/fields/fvPatchFields/derived/codedFixedValue/codedFixedValueFvPatchField.C b/src/finiteVolume/fields/fvPatchFields/derived/codedFixedValue/codedFixedValueFvPatchField.C index f3d8ba2e89..1d54921f86 100644 --- a/src/finiteVolume/fields/fvPatchFields/derived/codedFixedValue/codedFixedValueFvPatchField.C +++ b/src/finiteVolume/fields/fvPatchFields/derived/codedFixedValue/codedFixedValueFvPatchField.C @@ -31,74 +31,8 @@ License #include "dynamicCodeContext.H" #include "stringOps.H" -// * * * * * * * * * * * * Private Static Data Members * * * * * * * * * * * // - -template -const Foam::wordList Foam::codedFixedValueFvPatchField::codeKeys_ = - {"code", "codeInclude", "localCode"}; - - -// * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * // - -template -const Foam::word Foam::codedFixedValueFvPatchField::codeTemplateC = - "fixedValueFvPatchFieldTemplate.C"; - -template -const Foam::word Foam::codedFixedValueFvPatchField::codeTemplateH = - "fixedValueFvPatchFieldTemplate.H"; - - -// * * * * * * * * * * * * * Static Member Functions * * * * * * * * * * * * // - -template -void Foam::codedFixedValueFvPatchField::setFieldTemplates -( - dynamicCode& dynCode -) -{ - word fieldType(pTraits::typeName); - - // template type for fvPatchField - dynCode.setFilterVariable("TemplateType", fieldType); - - // Name for fvPatchField - eg, ScalarField, VectorField, ... - fieldType[0] = toupper(fieldType[0]); - dynCode.setFilterVariable("FieldType", fieldType + "Field"); -} - - // * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * // -template -const Foam::IOdictionary& Foam::codedFixedValueFvPatchField::dict() const -{ - const objectRegistry& obr = this->db(); - - if (obr.foundObject("codeDict")) - { - return obr.lookupObject("codeDict"); - } - else - { - return obr.store - ( - new IOdictionary - ( - IOobject - ( - "codeDict", - this->db().time().system(), - this->db(), - IOobject::MUST_READ_IF_MODIFIED, - IOobject::NO_WRITE - ) - ) - ); - } -} - - template void Foam::codedFixedValueFvPatchField::prepare ( @@ -106,75 +40,51 @@ void Foam::codedFixedValueFvPatchField::prepare const dynamicCodeContext& context ) const { - // take no chances - typeName must be identical to name_ - dynCode.setFilterVariable("typeName", name_); + dynCode.setFilterVariable("typeName", codeName()); - // set TemplateType and FieldType filter variables + // Set TemplateType and FieldType filter variables // (for fvPatchField) - setFieldTemplates(dynCode); + word fieldType(pTraits::typeName); - // compile filtered C template + // Template type for fvPatchField + dynCode.setFilterVariable("TemplateType", fieldType); + + // Name for fvPatchField - eg, ScalarField, VectorField, ... + fieldType[0] = toupper(fieldType[0]); + dynCode.setFilterVariable("FieldType", fieldType + "Field"); + + // Compile filtered C template dynCode.addCompileFile(codeTemplateC); - // copy filtered H template + // Copy filtered H template dynCode.addCopyFile(codeTemplateH); + // Debugging: make BC verbose + if (debug) + { + dynCode.setFilterVariable("verbose", "true"); + Info<<"compile " << codeName() << " sha1: " + << context.sha1() << endl; + } - // debugging: make BC verbose - // dynCode.setFilterVariable("verbose", "true"); - // Info<<"compile " << name_ << " sha1: " - // << context.sha1() << endl; - - // define Make/options + // Define Make/options dynCode.setMakeOptions - ( - "EXE_INC = -g \\\n" - "-I$(LIB_SRC)/finiteVolume/lnInclude \\\n" - + context.options() - + "\n\nLIB_LIBS = \\\n" - + " -lOpenFOAM \\\n" - + " -lfiniteVolume \\\n" - + context.libs() - ); -} - - -template -const Foam::dictionary& Foam::codedFixedValueFvPatchField::codeDict() -const -{ - // use system/codeDict or in-line - return ( - dict_.found("code") - ? dict_ - : this->dict().subDict(name_) + "EXE_INC = -g \\\n" + "-I$(LIB_SRC)/finiteVolume/lnInclude \\\n" + + context.options() + + "\n\nLIB_LIBS = \\\n" + + " -lOpenFOAM \\\n" + + " -lfiniteVolume \\\n" + + context.libs() ); } -template -const Foam::wordList& Foam::codedFixedValueFvPatchField::codeKeys() const -{ - return codeKeys_; -} - - -template -Foam::string Foam::codedFixedValueFvPatchField::description() const -{ - return - "patch " - + this->patch().name() - + " on field " - + this->internalField().name(); -} - - template void Foam::codedFixedValueFvPatchField::clearRedirect() const { - // remove instantiation of fvPatchField provided by library + // Remove instantiation of fvPatchField provided by library redirectPatchFieldPtr_.clear(); } @@ -189,7 +99,7 @@ Foam::codedFixedValueFvPatchField::codedFixedValueFvPatchField ) : fixedValueFvPatchField(p, iF), - codedBase(), + CodedBase(), redirectPatchFieldPtr_() {} @@ -204,9 +114,7 @@ Foam::codedFixedValueFvPatchField::codedFixedValueFvPatchField ) : fixedValueFvPatchField(ptf, p, iF, mapper), - codedBase(), - dict_(ptf.dict_), - name_(ptf.name_), + CodedBase(ptf), redirectPatchFieldPtr_() {} @@ -220,17 +128,10 @@ Foam::codedFixedValueFvPatchField::codedFixedValueFvPatchField ) : fixedValueFvPatchField(p, iF, dict), - codedBase(), - dict_(dict), - name_ - ( - dict.found("redirectType") - ? dict.lookup("redirectType") - : dict.lookup("name") - ), + CodedBase(dict), redirectPatchFieldPtr_() { - updateLibrary(name_); + updateLibrary(codeName()); } @@ -241,9 +142,7 @@ Foam::codedFixedValueFvPatchField::codedFixedValueFvPatchField ) : fixedValueFvPatchField(ptf), - codedBase(), - dict_(ptf.dict_), - name_(ptf.name_), + CodedBase(ptf), redirectPatchFieldPtr_() {} @@ -256,9 +155,7 @@ Foam::codedFixedValueFvPatchField::codedFixedValueFvPatchField ) : fixedValueFvPatchField(ptf, iF), - codedBase(), - dict_(ptf.dict_), - name_(ptf.name_), + CodedBase(ptf), redirectPatchFieldPtr_() {} @@ -275,7 +172,7 @@ Foam::codedFixedValueFvPatchField::redirectPatchField() const // Make sure to construct the patchfield with up-to-date value OStringStream os; - writeEntry(os, "type", name_); + writeEntry(os, "type", codeName()); writeEntry(os, "value", *this); IStringStream is(os.str()); dictionary dict(is); @@ -290,6 +187,7 @@ Foam::codedFixedValueFvPatchField::redirectPatchField() const ).ptr() ); } + return redirectPatchFieldPtr_(); } @@ -303,7 +201,7 @@ void Foam::codedFixedValueFvPatchField::updateCoeffs() } // Make sure library containing user-defined fvPatchField is up-to-date - updateLibrary(name_); + updateLibrary(codeName()); const fvPatchField& fvp = redirectPatchField(); @@ -323,7 +221,7 @@ void Foam::codedFixedValueFvPatchField::evaluate ) { // Make sure library containing user-defined fvPatchField is up-to-date - updateLibrary(name_); + updateLibrary(codeName()); const fvPatchField& fvp = redirectPatchField(); @@ -337,42 +235,7 @@ template void Foam::codedFixedValueFvPatchField::write(Ostream& os) const { fixedValueFvPatchField::write(os); - writeEntry(os, "name", name_); - - if (dict_.found("codeInclude")) - { - writeKeyword(os, "codeInclude"); - os.write(verbatimString(dict_["codeInclude"])) - << token::END_STATEMENT << nl; - } - - if (dict_.found("localCode")) - { - writeKeyword(os, "localCode"); - os.write(verbatimString(dict_["localCode"])) - << token::END_STATEMENT << nl; - } - - if (dict_.found("code")) - { - writeKeyword(os, "code"); - os.write(verbatimString(dict_["code"])) - << token::END_STATEMENT << nl; - } - - if (dict_.found("codeOptions")) - { - writeKeyword(os, "codeOptions"); - os.write(verbatimString(dict_["codeOptions"])) - << token::END_STATEMENT << nl; - } - - if (dict_.found("codeLibs")) - { - writeKeyword(os, "codeLibs"); - os.write(verbatimString(dict_["codeLibs"])) - << token::END_STATEMENT << nl; - } + writeCode(os); } diff --git a/src/finiteVolume/fields/fvPatchFields/derived/codedFixedValue/codedFixedValueFvPatchField.H b/src/finiteVolume/fields/fvPatchFields/derived/codedFixedValue/codedFixedValueFvPatchField.H index cc65c39eaa..c05f01d406 100644 --- a/src/finiteVolume/fields/fvPatchFields/derived/codedFixedValue/codedFixedValueFvPatchField.H +++ b/src/finiteVolume/fields/fvPatchFields/derived/codedFixedValue/codedFixedValueFvPatchField.H @@ -81,13 +81,25 @@ SourceFiles #define codedFixedValueFvPatchField_H #include "fixedValueFvPatchFields.H" -#include "codedBase.H" +#include "CodedBase.H" // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // namespace Foam { +/*---------------------------------------------------------------------------*\ + Class codedFixedValueFvPatchFieldBase Declaration +\*---------------------------------------------------------------------------*/ + +class codedFixedValueFvPatchFieldBase +{ +public: + + ClassNameNoDebug("fixedValueFvPatchField"); +}; + + /*---------------------------------------------------------------------------*\ Class codedFixedValueFvPatchField Declaration \*---------------------------------------------------------------------------*/ @@ -96,58 +108,27 @@ template class codedFixedValueFvPatchField : public fixedValueFvPatchField, - public codedBase + public CodedBase { - // Private static data - - //- The keywords associated with source code - static const wordList codeKeys_; - - // Private Data - //- Dictionary contents for the boundary condition - const dictionary dict_; - - const word name_; - mutable autoPtr> redirectPatchFieldPtr_; // Private Member Functions - const IOdictionary& dict() const; - //- Set the rewrite vars controlling the Type static void setFieldTemplates(dynamicCode& dynCode); //- Adapt the context for the current object virtual void prepare(dynamicCode&, const dynamicCodeContext&) const; - //- Return a description (type + name) for the output - virtual string description() const; - //- Clear the ptr to the redirected object virtual void clearRedirect() const; - //- Get the dictionary to initialize the codeContext - virtual const dictionary& codeDict() const; - - //- Get the keywords associated with source code - virtual const wordList& codeKeys() const; - public: - // Static Data Members - - //- Name of the C code template to be used - static const word codeTemplateC; - - //- Name of the H code template to be used - static const word codeTemplateH; - - //- Runtime type information TypeName("codedFixedValue"); diff --git a/src/finiteVolume/fields/fvPatchFields/derived/codedFixedValue/codedFixedValueFvPatchFields.C b/src/finiteVolume/fields/fvPatchFields/derived/codedFixedValue/codedFixedValueFvPatchFields.C index 514ff81b5f..ccb2fb8b02 100644 --- a/src/finiteVolume/fields/fvPatchFields/derived/codedFixedValue/codedFixedValueFvPatchFields.C +++ b/src/finiteVolume/fields/fvPatchFields/derived/codedFixedValue/codedFixedValueFvPatchFields.C @@ -2,7 +2,7 @@ ========= | \\ / F ield | OpenFOAM: The Open Source CFD Toolbox \\ / O peration | Website: https://openfoam.org - \\ / A nd | Copyright (C) 2011-2018 OpenFOAM Foundation + \\ / A nd | Copyright (C) 2011-2020 OpenFOAM Foundation \\/ M anipulation | ------------------------------------------------------------------------------- License @@ -27,6 +27,20 @@ License #include "addToRunTimeSelectionTable.H" #include "volFields.H" +// * * * * * * * * * * * * Private Static Data Members * * * * * * * * * * * // + +defineTypeName(Foam::codedFixedValueFvPatchFieldBase); + +template<> +const Foam::wordList +Foam::CodedBase::codeKeys_ = +{ + "code", + "codeInclude", + "localCode" +}; + + // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // namespace Foam diff --git a/src/functionObjects/utilities/codedFunctionObject/codedFunctionObject.C b/src/functionObjects/utilities/codedFunctionObject/codedFunctionObject.C index f666e30dcc..45dc60796d 100644 --- a/src/functionObjects/utilities/codedFunctionObject/codedFunctionObject.C +++ b/src/functionObjects/utilities/codedFunctionObject/codedFunctionObject.C @@ -80,9 +80,12 @@ void Foam::codedFunctionObject::prepare dynCode.addCopyFile("functionObjectTemplate.H"); // Debugging: make verbose - // dynCode.setFilterVariable("verbose", "true"); - // Info<<"compile " << codeName() << " sha1: " - // << context.sha1() << endl; + if (debug) + { + dynCode.setFilterVariable("verbose", "true"); + Info<<"compile " << codeName() << " sha1: " + << context.sha1() << endl; + } // Define Make/options dynCode.setMakeOptions