diff --git a/etc/codeTemplates/dynamicCode/codedPatchFunction1Template.C b/etc/codeTemplates/dynamicCode/codedPatchFunction1Template.C new file mode 100644 index 0000000000..42e1d58288 --- /dev/null +++ b/etc/codeTemplates/dynamicCode/codedPatchFunction1Template.C @@ -0,0 +1,150 @@ +/*---------------------------------------------------------------------------*\ + ========= | + \\ / F ield | OpenFOAM: The Open Source CFD Toolbox + \\ / O peration | + \\ / A nd | www.openfoam.com + \\/ M anipulation | +------------------------------------------------------------------------------- + Copyright (C) YEAR YEAR AUTHOR,AFFILIATION +------------------------------------------------------------------------------- +License + This file is part of OpenFOAM. + + OpenFOAM is free software: you can redistribute it and/or modify it + under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + OpenFOAM is distributed in the hope that it will be useful, but WITHOUT + ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + for more details. + + You should have received a copy of the GNU General Public License + along with OpenFOAM. If not, see . + +\*---------------------------------------------------------------------------*/ + +#include "codedPatchFunction1Template.H" +#include "polyMesh.H" +#include "addToRunTimeSelectionTable.H" +#include "fvPatchFieldMapper.H" +#include "volFields.H" +#include "surfaceFields.H" +#include "unitConversion.H" + +//{{{ begin codeInclude +${codeInclude} +//}}} end codeInclude + + +namespace Foam +{ + +// * * * * * * * * * * * * * * * Local Functions * * * * * * * * * * * * * * // + +//{{{ begin localCode +${localCode} +//}}} end localCode + +// * * * * * * * * * * * * * * * Global Functions * * * * * * * * * * * * * // + +// dynamicCode: +// SHA1 = ${SHA1sum} +// +// unique function name that can be checked if the correct library version +// has been loaded +extern "C" void ${typeName}_${SHA1sum}(bool load) +{ + if (load) + { + // Code that can be explicitly executed after loading + } + else + { + // Code that can be explicitly executed before unloading + } +} + + +namespace PatchFunction1Types +{ + +// * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * // + +//makePatchFunction1(${typeName}PatchFunction1${FieldType}); +defineTypeNameAndDebug +( + ${typeName}PatchFunction1${FieldType}, + 0 +); +PatchFunction1<${TemplateType}>::adddictionaryConstructorToTable + <${typeName}PatchFunction1${FieldType}> + add${typeName}PatchFunction1${FieldType}ConstructorToTable_; + + + +// * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * // + +${typeName}PatchFunction1${FieldType}:: +${typeName}PatchFunction1${FieldType} +( + const polyPatch& pp, + const word& type, + const word& entryName, + const dictionary& dict, + const bool faceValues +) +: + PatchFunction1<${TemplateType}>(pp, entryName, dict, faceValues) +{ + if (${verbose:-false}) + { + printMessage("Construct ${typeName} from components"); + } +} + + +${typeName}PatchFunction1${FieldType}:: +${typeName}PatchFunction1${FieldType} +( + const ${typeName}PatchFunction1${FieldType}& ut +) +: + PatchFunction1<${TemplateType}>(ut) +{} + + +${typeName}PatchFunction1${FieldType}:: +${typeName}PatchFunction1${FieldType} +( + const ${typeName}PatchFunction1${FieldType}& ut, + const polyPatch& pp +) +: + PatchFunction1<${TemplateType}>(ut, pp) +{} + + +// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * // + +tmp> +${typeName}PatchFunction1${FieldType}::value +( + const scalar x +) const +{ +//{{{ begin code + ${code} +//}}} end code + +} + + +// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // + +} // End namespace PatchFunction1Types +} // End namespace Foam + + +// ************************************************************************* // diff --git a/etc/codeTemplates/dynamicCode/codedPatchFunction1Template.H b/etc/codeTemplates/dynamicCode/codedPatchFunction1Template.H new file mode 100644 index 0000000000..cde6199bf6 --- /dev/null +++ b/etc/codeTemplates/dynamicCode/codedPatchFunction1Template.H @@ -0,0 +1,151 @@ +/*---------------------------------------------------------------------------*\ + ========= | + \\ / F ield | OpenFOAM: The Open Source CFD Toolbox + \\ / O peration | + \\ / A nd | www.openfoam.com + \\/ M anipulation | +------------------------------------------------------------------------------- + Copyright (C) 2020 OpenCFD Ltd. + Copyright (C) YEAR AUTHOR, AFFILIATION +------------------------------------------------------------------------------- +License + This file is part of OpenFOAM. + + OpenFOAM is free software: you can redistribute it and/or modify it + under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + OpenFOAM is distributed in the hope that it will be useful, but WITHOUT + ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + for more details. + + You should have received a copy of the GNU General Public License + along with OpenFOAM. If not, see . + +Description + Template for use with dynamic code generation of a PatchFunction1 + +SourceFiles + codedPatchFunction1Template.C + +\*---------------------------------------------------------------------------*/ + +#ifndef codedPatchFunction1Template${FieldType}_H +#define codedPatchFunction1Template${FieldType}_H + +#include "PatchFunction1.H" + +// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // + +namespace Foam +{ +namespace PatchFunction1Types +{ + +/*---------------------------------------------------------------------------*\ + A coded version PatchFunction1 +\*---------------------------------------------------------------------------*/ + +class ${typeName}PatchFunction1${FieldType} +: + public PatchFunction1<${TemplateType}> +{ + // Private Member Functions + + //- Report a message with the SHA1sum + inline static void printMessage(const char* message) + { + Info<< message << " sha1: " << SHA1sum << '\n'; + } + +public: + + //- SHA1 representation of the code content + static constexpr const char* const SHA1sum = "${SHA1sum}"; + + //- Runtime type information + TypeName("${typeName}"); + + + // Constructors + + //- Construct from patch and internal field + ${typeName}PatchFunction1${FieldType} + ( + const polyPatch& pp, + const word& type, + const word& entryName, + const dictionary& dict, + const bool faceValues = true + ); + + //- Construct as copy + ${typeName}PatchFunction1${FieldType} + ( + const ${typeName}PatchFunction1${FieldType}& + ); + + //- Construct as copy, resetting patch + ${typeName}PatchFunction1${FieldType} + ( + const ${typeName}PatchFunction1${FieldType}&, + const polyPatch& pp + ); + + //- Construct and return a clone + virtual tmp> clone() const + { + return tmp> + ( + new ${typeName}PatchFunction1${FieldType}(*this) + ); + } + + //- Construct and return a clone setting patch + virtual tmp> clone + ( + const polyPatch& pp + ) const + { + return tmp> + ( + new ${typeName}PatchFunction1${FieldType}(*this, pp) + ); + } + + + //- Destructor + virtual ~${typeName}PatchFunction1${FieldType}() = default; + + + // Member Functions + + //- Return value as a function of (scalar) independent variable + virtual tmp> value(const scalar x) const; + + //- Is value constant (i.e. independent of x) + virtual bool constant() const + { + return false; + } + + //- Is value uniform (i.e. independent of coordinate) + virtual bool uniform() const + { + return false; + } +}; + + +// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // + +} // End namespace PatchFunction1Types +} // End namespace Foam + +// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // + +#endif + +// ************************************************************************* // diff --git a/src/meshTools/Make/files b/src/meshTools/Make/files index 456e1c62fc..1d46037d25 100644 --- a/src/meshTools/Make/files +++ b/src/meshTools/Make/files @@ -297,6 +297,7 @@ polyTopoChange/polyTopoChange.C PatchFunction1/makePatchFunction1s.C PatchFunction1/coordinateLabelScaling.C +PatchFunction1/CodedField/makeCodedFields.C meshStructure/meshStructure.C diff --git a/src/meshTools/PatchFunction1/CodedField/CodedField.C b/src/meshTools/PatchFunction1/CodedField/CodedField.C new file mode 100644 index 0000000000..60d2aab052 --- /dev/null +++ b/src/meshTools/PatchFunction1/CodedField/CodedField.C @@ -0,0 +1,296 @@ +/*---------------------------------------------------------------------------*\ + ========= | + \\ / F ield | OpenFOAM: The Open Source CFD Toolbox + \\ / O peration | + \\ / A nd | www.openfoam.com + \\/ M anipulation | +------------------------------------------------------------------------------- + Copyright (C) 2020 OpenCFD Ltd. +------------------------------------------------------------------------------- +License + This file is part of OpenFOAM. + + OpenFOAM is free software: you can redistribute it and/or modify it + under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + OpenFOAM is distributed in the hope that it will be useful, but WITHOUT + ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + for more details. + + You should have received a copy of the GNU General Public License + along with OpenFOAM. If not, see . + +\*---------------------------------------------------------------------------*/ + +#include "dynamicCode.H" + +// * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * // + +template +Foam::dlLibraryTable& +Foam::PatchFunction1Types::CodedField::libs() const +{ + const dlLibraryTable& timeLibs = + this->patch_.boundaryMesh().mesh().time().libs(); + + return const_cast(timeLibs); +} + + +template +void Foam::PatchFunction1Types::CodedField::prepare +( + dynamicCode& dynCode, + const dynamicCodeContext& context +) const +{ + if (context.code().empty()) + { + FatalIOErrorInFunction(dict_) + << "No code section in input dictionary for patch " + << this->patch_.name() + << " name " << name_ + << exit(FatalIOError); + } + + // Take no chances - typeName must be identical to name_ + dynCode.setFilterVariable("typeName", name_); + + // Set TemplateType and FieldType filter variables + dynCode.setFieldTemplates(); + + // Compile filtered C template + dynCode.addCompileFile(codeTemplateC); + + // Copy filtered H template + dynCode.addCopyFile(codeTemplateH); + + // Debugging: make verbose + // dynCode.setFilterVariable("verbose", "true"); + // DetailInfo + // <<"compile " << name_ << " sha1: " + // << context.sha1() << endl; + + // Define Make/options + dynCode.setMakeOptions + ( + "EXE_INC = -g \\\n" + "-I$(LIB_SRC)/meshTools/lnInclude \\\n" + "-I$(LIB_SRC)/finiteVolume/lnInclude \\\n" + + context.options() + + "\n\nLIB_LIBS = \\\n" + " -lOpenFOAM \\\n" + " -lfiniteVolume \\\n" + + context.libs() + ); +} + + +template +const Foam::dictionary& +Foam::PatchFunction1Types::CodedField::codeDict +( + const dictionary& dict +) const +{ + // Use named subdictionary if present to provide the code. This allows + // running with multiple PatchFunction1s + + return + ( + dict.found("code") + ? dict + : dict.subDict(name_) + ); + + //return + //( + // dict.found(name_) + // ? dict.subDict(name_) + // : dict + //); +} + + +template +const Foam::dictionary& +Foam::PatchFunction1Types::CodedField::codeDict() const +{ + return codeDict(dict_); +} + + +template +Foam::string +Foam::PatchFunction1Types::CodedField::description() const +{ + return "CodedField " + name_; +} + + +template +void Foam::PatchFunction1Types::CodedField::clearRedirect() const +{ + // remove instantiation of fvPatchField provided by library + redirectFunctionPtr_.clear(); +} + + +// * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * // + +template +Foam::PatchFunction1Types::CodedField::CodedField +( + const polyPatch& pp, + const word& type, + const word& entryName, + const dictionary& dict, + const bool faceValues +) +: + PatchFunction1(pp, entryName, dict, faceValues), + codedBase(), + dict_(dict), + //name_(dict.getCompat("name", {{"redirectType", 1706}})) + name_(dict.lookupOrDefault("name", entryName)) +{ + updateLibrary(name_); +} + + +template +Foam::PatchFunction1Types::CodedField::CodedField +( + const CodedField& ut +) +: + PatchFunction1(ut), + codedBase(), + dict_(ut.dict_), + name_(ut.name_) +{} + + +template +Foam::PatchFunction1Types::CodedField::CodedField +( + const CodedField& ut, + const polyPatch& pp +) +: + PatchFunction1(ut, pp), + codedBase(), + dict_(ut.dict_), + name_(ut.name_) +{} + + +// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * // + +template +const Foam::PatchFunction1& +Foam::PatchFunction1Types::CodedField::redirectFunction() const +{ + if (!redirectFunctionPtr_.valid()) + { + // Construct a PatchFunction1 containing the input code + dictionary completeDict(dict_); + + // Override the type to enforce the PatchFunction1::New constructor + // to choose our type + completeDict.set("type", name_); + + dictionary dict; + dict.add(name_, completeDict); + + redirectFunctionPtr_.set + ( + PatchFunction1::New + ( + this->patch(), + name_, + dict, + this->faceValues_ + ).ptr() + ); + } + return *redirectFunctionPtr_; +} + + +template +Foam::tmp> +Foam::PatchFunction1Types::CodedField::value +( + const scalar x +) const +{ + // Make sure library containing user-defined fvPatchField is up-to-date + updateLibrary(name_); + + return redirectFunction().value(x); +} + + +template +Foam::tmp> +Foam::PatchFunction1Types::CodedField::integrate +( + const scalar x1, + const scalar x2 +) const +{ + // Make sure library containing user-defined fvPatchField is up-to-date + updateLibrary(name_); + + return redirectFunction().integrate(x1, x2); +} + + +template +void Foam::PatchFunction1Types::CodedField::autoMap +( + const FieldMapper& mapper +) +{ + PatchFunction1::autoMap(mapper); + if (redirectFunctionPtr_) + { + redirectFunctionPtr_->autoMap(mapper); + } +} + + +template +void Foam::PatchFunction1Types::CodedField::rmap +( + const PatchFunction1& pf1, + const labelList& addr +) +{ + PatchFunction1::rmap(pf1, addr); + if (redirectFunctionPtr_) + { + redirectFunctionPtr_->rmap(pf1, addr); + } +} + + +template +void Foam::PatchFunction1Types::CodedField::writeData +( + Ostream& os +) const +{ + // Should really only output only relevant entries but since using + // PatchFunction1-from-subdict upon construction our dictionary contains + // only the relevant entries. It would be different if PatchFunction1-from + // primitiveEntry when the whole 'value' entry would be present + dict_.writeEntry(this->name(), os); +} + + +// ************************************************************************* // diff --git a/src/meshTools/PatchFunction1/CodedField/CodedField.H b/src/meshTools/PatchFunction1/CodedField/CodedField.H new file mode 100644 index 0000000000..9202edfce7 --- /dev/null +++ b/src/meshTools/PatchFunction1/CodedField/CodedField.H @@ -0,0 +1,262 @@ +/*---------------------------------------------------------------------------*\ + ========= | + \\ / F ield | OpenFOAM: The Open Source CFD Toolbox + \\ / O peration | + \\ / A nd | www.openfoam.com + \\/ M anipulation | +------------------------------------------------------------------------------- + Copyright (C) 2020 OpenCFD Ltd. +------------------------------------------------------------------------------- +License + This file is part of OpenFOAM. + + OpenFOAM is free software: you can redistribute it and/or modify it + under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + OpenFOAM is distributed in the hope that it will be useful, but WITHOUT + ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + for more details. + + You should have received a copy of the GNU General Public License + along with OpenFOAM. If not, see . + +Class + Foam::PatchFunction1Types::CodedField + +Description + PatchFunction1 with the code supplied by an on-the-fly compiled C++ + expression. + +Usage + Example: + \verbatim + + { + type uniformFixedValue; + uniformValue + { + type coded; + // Explictly supply name of generated PatchFunction1. Only needed + // if entryname ('uniformValue') would clash with existing + // runtime selection tables. + name myExpression; + + code + #{ + const polyPatch& pp = this->patch(); + Pout<< "** Patch size:" << pp.size() << endl; + return tmp::New(pp.size(), vector(1, 0, 0)) + #}; + } + + //codeInclude + //#{ + // #include "fvCFD.H" + //#}; + + //codeOptions + //#{ + // -I$(LIB_SRC)/finiteVolume/lnInclude + //#}; + } + \endverbatim + +See also + Foam::dynamicCode + Foam::codedFixedValue + Foam::functionEntries::codeStream + +SourceFiles + CodedField.C + +\*---------------------------------------------------------------------------*/ + +#ifndef PatchFunction1Types_CodedField_H +#define PatchFunction1Types_CodedField_H + +#include "PatchFunction1.H" +#include "codedBase.H" + +// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // + +namespace Foam +{ +namespace PatchFunction1Types +{ + +/*---------------------------------------------------------------------------*\ + Class CodedField Declaration +\*---------------------------------------------------------------------------*/ + +template +class CodedField +: + public PatchFunction1, + protected codedBase +{ + // Private data + + //- Dictionary contents for the boundary condition + const dictionary dict_; + + const word name_; + + mutable autoPtr> redirectFunctionPtr_; + + + // Private Member Functions + + //- Get the loaded dynamic libraries + virtual dlLibraryTable& libs() const; + + //- 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 reference to the underlying Function1 + const PatchFunction1& redirectFunction() const; + + // Get the (sub)dictionary to initialize the codeContext + virtual const dictionary& codeDict(const dictionary& fullDict) const; + + // Get the dictionary to initialize the codeContext + virtual const dictionary& codeDict() const; + + //- No copy assignment + void operator=(const CodedField&) = delete; + + +public: + + // Static data members + + //- Name of the C code template to be used + static constexpr const char* const codeTemplateC + = "codedPatchFunction1Template.C"; + + //- Name of the H code template to be used + static constexpr const char* const codeTemplateH + = "codedPatchFunction1Template.H"; + + + + // Runtime type information + TypeName("coded"); + + + // Constructors + + //- Construct from entry name and dictionary + CodedField + ( + const polyPatch& pp, + const word& type, + const word& entryName, + const dictionary& dict, + const bool faceValues = true + ); + + //- Copy constructor + explicit CodedField(const CodedField& ut); + + //- Copy constructor setting patch + explicit CodedField + ( + const CodedField& ut, + const polyPatch& pp + ); + + //- Construct and return a clone + virtual tmp> clone() const + { + return tmp> + ( + new CodedField(*this) + ); + } + + //- Construct and return a clone setting patch + virtual tmp> clone(const polyPatch& pp) const + { + return tmp> + ( + new CodedField(*this, pp) + ); + } + + + //- Destructor + virtual ~CodedField() = default; + + + // Member Functions + + // Evaluation + + //- Return CodedField value + virtual tmp> value(const scalar x) const; + + //- Is value constant (i.e. independent of x) + virtual inline bool constant() const + { + return false; + } + + //- Is value uniform (i.e. independent of coordinate) + virtual inline bool uniform() const + { + return false; + } + + //- Integrate between two values + virtual tmp> integrate + ( + const scalar x1, + const scalar x2 + ) const; + + + // Mapping + + //- Map (and resize as needed) from self given a mapping object + virtual void autoMap(const FieldMapper& mapper); + + //- Reverse map the given PatchFunction1 onto this PatchFunction1 + virtual void rmap + ( + const PatchFunction1& pf1, + const labelList& addr + ); + + + // I-O + + //- Write in dictionary format + virtual void writeData(Ostream& os) const; +}; + + +// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // + +} // End namespace PatchFunction1Types +} // End namespace Foam + +// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // + +#ifdef NoRepository + #include "CodedField.C" +#endif + +// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // + +#endif + +// ************************************************************************* // diff --git a/src/meshTools/PatchFunction1/CodedField/makeCodedFields.C b/src/meshTools/PatchFunction1/CodedField/makeCodedFields.C new file mode 100644 index 0000000000..385ff3bd0f --- /dev/null +++ b/src/meshTools/PatchFunction1/CodedField/makeCodedFields.C @@ -0,0 +1,42 @@ +/*---------------------------------------------------------------------------*\ + ========= | + \\ / F ield | OpenFOAM: The Open Source CFD Toolbox + \\ / O peration | + \\ / A nd | www.openfoam.com + \\/ M anipulation | +------------------------------------------------------------------------------- + Copyright (C) 2020 OpenCFD Ltd. +------------------------------------------------------------------------------- +License + This file is part of OpenFOAM. + + OpenFOAM is free software: you can redistribute it and/or modify it + under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + OpenFOAM is distributed in the hope that it will be useful, but WITHOUT + ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + for more details. + + You should have received a copy of the GNU General Public License + along with OpenFOAM. If not, see . + +\*---------------------------------------------------------------------------*/ + +#include "CodedField.H" +#include "SubField.H" +#include "addToRunTimeSelectionTable.H" + +namespace Foam +{ + makePatchFunction1Type(CodedField, scalar); + makePatchFunction1Type(CodedField, vector); + makePatchFunction1Type(CodedField, sphericalTensor); + makePatchFunction1Type(CodedField, symmTensor); + makePatchFunction1Type(CodedField, tensor); +} + + +// ************************************************************************* //