Commit 7f936ad0 authored by mattijs's avatar mattijs
Browse files

ENH: coded version of PatchFunction1. See #1709.

parent 7a063c58
/*---------------------------------------------------------------------------*\
========= |
\\ / 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 <http://www.gnu.org/licenses/>.
\*---------------------------------------------------------------------------*/
#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<Field<${TemplateType}>>
${typeName}PatchFunction1${FieldType}::value
(
const scalar x
) const
{
//{{{ begin code
${code}
//}}} end code
}
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
} // End namespace PatchFunction1Types
} // End namespace Foam
// ************************************************************************* //
/*---------------------------------------------------------------------------*\
========= |
\\ / 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 <http://www.gnu.org/licenses/>.
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<PatchFunction1<${TemplateType}>> clone() const
{
return tmp<PatchFunction1<${TemplateType}>>
(
new ${typeName}PatchFunction1${FieldType}(*this)
);
}
//- Construct and return a clone setting patch
virtual tmp<PatchFunction1<${TemplateType}>> clone
(
const polyPatch& pp
) const
{
return tmp<PatchFunction1<${TemplateType}>>
(
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<Field<${TemplateType}>> 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
// ************************************************************************* //
......@@ -297,6 +297,7 @@ polyTopoChange/polyTopoChange.C
PatchFunction1/makePatchFunction1s.C
PatchFunction1/coordinateLabelScaling.C
PatchFunction1/CodedField/makeCodedFields.C
meshStructure/meshStructure.C
......
/*---------------------------------------------------------------------------*\
========= |
\\ / 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 <http://www.gnu.org/licenses/>.
\*---------------------------------------------------------------------------*/
#include "dynamicCode.H"
// * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * //
template<class Type>
Foam::dlLibraryTable&
Foam::PatchFunction1Types::CodedField<Type>::libs() const
{
const dlLibraryTable& timeLibs =
this->patch_.boundaryMesh().mesh().time().libs();
return const_cast<dlLibraryTable&>(timeLibs);
}
template<class Type>
void Foam::PatchFunction1Types::CodedField<Type>::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<Type>();
// 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<class Type>
const Foam::dictionary&
Foam::PatchFunction1Types::CodedField<Type>::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<class Type>
const Foam::dictionary&
Foam::PatchFunction1Types::CodedField<Type>::codeDict() const
{
return codeDict(dict_);
}
template<class Type>
Foam::string
Foam::PatchFunction1Types::CodedField<Type>::description() const
{
return "CodedField " + name_;
}
template<class Type>
void Foam::PatchFunction1Types::CodedField<Type>::clearRedirect() const
{
// remove instantiation of fvPatchField provided by library
redirectFunctionPtr_.clear();
}
// * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
template<class Type>
Foam::PatchFunction1Types::CodedField<Type>::CodedField
(
const polyPatch& pp,
const word& type,
const word& entryName,
const dictionary& dict,
const bool faceValues
)
:
PatchFunction1<Type>(pp, entryName, dict, faceValues),
codedBase(),
dict_(dict),
//name_(dict.getCompat<word>("name", {{"redirectType", 1706}}))
name_(dict.lookupOrDefault<word>("name", entryName))
{
updateLibrary(name_);
}
template<class Type>
Foam::PatchFunction1Types::CodedField<Type>::CodedField
(
const CodedField<Type>& ut
)
:
PatchFunction1<Type>(ut),
codedBase(),
dict_(ut.dict_),
name_(ut.name_)
{}
template<class Type>
Foam::PatchFunction1Types::CodedField<Type>::CodedField
(
const CodedField<Type>& ut,
const polyPatch& pp
)
:
PatchFunction1<Type>(ut, pp),
codedBase(),
dict_(ut.dict_),
name_(ut.name_)
{}
// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
template<class Type>
const Foam::PatchFunction1<Type>&
Foam::PatchFunction1Types::CodedField<Type>::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<Type>::New
(
this->patch(),
name_,
dict,
this->faceValues_
).ptr()
);
}
return *redirectFunctionPtr_;
}
template<class Type>
Foam::tmp<Foam::Field<Type>>
Foam::PatchFunction1Types::CodedField<Type>::value
(
const scalar x
) const
{
// Make sure library containing user-defined fvPatchField is up-to-date
updateLibrary(name_);
return redirectFunction().value(x);
}
template<class Type>
Foam::tmp<Foam::Field<Type>>
Foam::PatchFunction1Types::CodedField<Type>::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<class Type>
void Foam::PatchFunction1Types::CodedField<Type>::autoMap
(
const FieldMapper& mapper
)
{
PatchFunction1<Type>::autoMap(mapper);
if (redirectFunctionPtr_)
{
redirectFunctionPtr_->autoMap(mapper);
}
}
template<class Type>
void Foam::PatchFunction1Types::CodedField<Type>::rmap
(
const PatchFunction1<Type>& pf1,
const labelList& addr
)
{
PatchFunction1<Type>::rmap(pf1, addr);
if (redirectFunctionPtr_)
{
redirectFunctionPtr_->rmap(pf1, addr);
}
}
template<class Type>
void Foam::PatchFunction1Types::CodedField<Type>::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);
}