ENH: codedFunctionObject: initial version.

This commit is contained in:
mattijs 2011-03-22 22:16:48 +00:00
parent cf99e5c800
commit e24dd4fe2a
8 changed files with 1010 additions and 0 deletions

View File

@ -0,0 +1,65 @@
/*---------------------------------------------------------------------------*\
========= |
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration |
\\ / A nd | Copyright (C) 2011-2011 OpenCFD Ltd.
\\/ M anipulation |
-------------------------------------------------------------------------------
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 "FilterFunctionObjectTemplate.H"
// * * * * * * * * * * * * * * * Global Functions * * * * * * * * * * * * * //
extern "C"
{
// dynamicCode:
// SHA1 = ${SHA1sum}
//
// unique function name that can be checked if the correct library version
// has been loaded
void ${typeName}_${SHA1sum}(bool load)
{
if (load)
{
// code that can be explicitly executed after loading
}
else
{
// code that can be explicitly executed before unloading
}
}
}
// * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
namespace Foam
{
defineNamedTemplateTypeNameAndDebug(${typeName}FilterFunctionObject, 0);
//addToRunTimeSelectionTable
addRemovableToRunTimeSelectionTable
(
functionObject,
${typeName}FilterFunctionObject,
dictionary
);
}
// ************************************************************************* //

View File

@ -0,0 +1,51 @@
/*---------------------------------------------------------------------------*\
========= |
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration |
\\ / A nd | Copyright (C) 2011-2011 OpenCFD Ltd.
\\/ M anipulation |
-------------------------------------------------------------------------------
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
FunctionObject wrapper around functionObjectTemplate to allow them
to be created via the functions entry within controlDict.
SourceFiles
FilterFunctionObject.C
\*---------------------------------------------------------------------------*/
#ifndef FilterFunctionObject_H
#define FilterFunctionObject_H
#include "functionObjectTemplate.H"
#include "OutputFilterFunctionObject.H"
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
namespace Foam
{
typedef OutputFilterFunctionObject<${typeName}FunctionObject>
${typeName}FilterFunctionObject;
}
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
#endif
// ************************************************************************* //

View File

@ -0,0 +1,50 @@
/*---------------------------------------------------------------------------*\
========= |
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration |
\\ / A nd | Copyright (C) 2011-2011 OpenCFD Ltd.
\\/ M anipulation |
-------------------------------------------------------------------------------
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/>.
Typedef
Foam::IOfunctionObjectTemplate
Description
Instance of the generic IOOutputFilter for ${typeName}FunctionObject.
\*---------------------------------------------------------------------------*/
#ifndef IOfunctionObjectTemplate_H
#define IOfunctionObjectTemplate_H
#include "functionObjectTemplate.H"
#include "IOOutputFilter.H"
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
namespace Foam
{
typedef IOOutputFilter<${typeName}FunctionObject>
IO${typeName}FunctionObject;
}
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
#endif
// ************************************************************************* //

View File

@ -0,0 +1,132 @@
/*---------------------------------------------------------------------------*\
========= |
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration |
\\ / A nd | Copyright (C) 2011-2011 OpenCFD Ltd.
\\/ M anipulation |
-------------------------------------------------------------------------------
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 "functionObjectTemplate.H"
#include "Time.H"
//#include "pointFields.H"
//{{{ begin codeInclude
${codeInclude}
//}}} end codeInclude
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
namespace Foam
{
// * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
defineTypeNameAndDebug(${typeName}FunctionObject, 0);
// * * * * * * * * * * * * * * * Local Functions * * * * * * * * * * * * * * //
//{{{ begin localCode
${localCode}
//}}} end localCode
// * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
${typeName}FunctionObject::${typeName}FunctionObject
(
const word& name,
const objectRegistry& obr,
const dictionary& dict,
const bool
)
:
name_(name),
obr_(obr)
{
read(dict);
}
// * * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * //
${typeName}FunctionObject::~${typeName}FunctionObject()
{}
// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
void ${typeName}FunctionObject::read(const dictionary& dict)
{
if (${verbose:-false})
{
Info<<"read ${typeName} sha1: ${SHA1sum}\n";
}
//{{{ begin code
${codeRead}
//}}} end code
}
void ${typeName}FunctionObject::execute()
{
if (${verbose:-false})
{
Info<<"execute ${typeName} sha1: ${SHA1sum}\n";
}
//{{{ begin code
${codeExecute}
//}}} end code
}
void ${typeName}FunctionObject::end()
{
if (${verbose:-false})
{
Info<<"end ${typeName} sha1: ${SHA1sum}\n";
}
//{{{ begin code
${codeEnd}
//}}} end code
}
void ${typeName}FunctionObject::write()
{
if (${verbose:-false})
{
Info<<"write ${typeName} sha1: ${SHA1sum}\n";
}
//{{{ begin code
${code}
//}}} end code
}
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
} // End namespace Foam
// ************************************************************************* //

View File

@ -0,0 +1,138 @@
/*---------------------------------------------------------------------------*\
========= |
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration |
\\ / A nd | Copyright (C) 2011-2011 OpenCFD Ltd.
\\/ M anipulation |
-------------------------------------------------------------------------------
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
OutputFilter functionObject.
SourceFiles
functionObjectTemplate.C
\*---------------------------------------------------------------------------*/
#ifndef functionObjectTemplate_H
#define functionObjectTemplate_H
#include "stringList.H"
#include "pointField.H"
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
namespace Foam
{
// Forward declaration of classes
class objectRegistry;
class dictionary;
class mapPolyMesh;
/*---------------------------------------------------------------------------*\
A templated functionObject
\*---------------------------------------------------------------------------*/
class ${typeName}FunctionObject
{
// Private data
//- Name of this set of system calls
word name_;
//- Registry
const objectRegistry& obr_;
//{{{ begin codeData
${codeData}
//}}} end codeData
// Private Member Functions
//- Disallow default bitwise copy construct
${typeName}FunctionObject(const ${typeName}FunctionObject&);
//- Disallow default bitwise assignment
void operator=(const ${typeName}FunctionObject&);
public:
//- Runtime type information
TypeName("${typeName}");
// Constructors
//- Construct for given objectRegistry and dictionary.
// Allow the possibility to load fields from files
${typeName}FunctionObject
(
const word& name,
const objectRegistry& unused,
const dictionary&,
const bool loadFromFilesUnused = false
);
//- Destructor
virtual ~${typeName}FunctionObject();
// Member Functions
//- Return name of the system call set
virtual const word& name() const
{
return name_;
}
//- Read the system calls
virtual void read(const dictionary&);
//- Execute the "executeCalls" at each time-step
virtual void execute();
//- Execute the "endCalls" at the final time-loop
virtual void end();
//- Write, execute the "writeCalls"
virtual void write();
//- Update for changes of mesh
virtual void updateMesh(const mapPolyMesh&)
{}
//- Update for changes of mesh
virtual void movePoints(const pointField&)
{}
};
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
} // End namespace Foam
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
#endif
// ************************************************************************* //

View File

@ -1,3 +1,5 @@
codedFunctionObject/codedFunctionObject.C
staticPressure/staticPressure.C
staticPressure/staticPressureFunctionObject.C

View File

@ -0,0 +1,398 @@
/*---------------------------------------------------------------------------*\
========= |
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration |
\\ / A nd | Copyright (C) 2011-2011 OpenCFD Ltd.
\\/ M anipulation |
-------------------------------------------------------------------------------
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 "codedFunctionObject.H"
#include "volFields.H"
#include "dictionary.H"
#include "Time.H"
#include "SHA1Digest.H"
#include "dynamicCode.H"
#include "dynamicCodeContext.H"
#include "stringOps.H"
#include "addToRunTimeSelectionTable.H"
// * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
namespace Foam
{
defineTypeNameAndDebug(codedFunctionObject, 0);
addToRunTimeSelectionTable
(
functionObject,
codedFunctionObject,
dictionary
);
}
// * * * * * * * * * * * * * Static Member Functions * * * * * * * * * * * * //
void* Foam::codedFunctionObject::loadLibrary
(
const fileName& libPath,
const string& globalFuncName,
const dictionary& contextDict
)
{
void* lib = 0;
// avoid compilation by loading an existing library
if (!libPath.empty() && dlLibraryTable::open(libPath, false))
{
lib = dlLibraryTable::findLibrary(libPath);
// verify the loaded version and unload if needed
if (lib)
{
// provision for manual execution of code after loading
if (dlSymFound(lib, globalFuncName))
{
loaderFunctionType function =
reinterpret_cast<loaderFunctionType>
(
dlSym(lib, globalFuncName)
);
if (function)
{
(*function)(true); // force load
}
else
{
FatalIOErrorIn
(
"codedFunctionObject::updateLibrary()",
contextDict
) << "Failed looking up symbol " << globalFuncName << nl
<< "from " << libPath << exit(FatalIOError);
}
}
else
{
FatalIOErrorIn
(
"codedFunctionObject::loadLibrary()",
contextDict
) << "Failed looking up symbol " << globalFuncName << nl
<< "from " << libPath << exit(FatalIOError);
lib = 0;
if (!dlLibraryTable::close(libPath, false))
{
FatalIOErrorIn
(
"codedFunctionObject::loadLibrary()",
contextDict
) << "Failed unloading library "
<< libPath
<< exit(FatalIOError);
}
}
}
}
return lib;
}
void Foam::codedFunctionObject::unloadLibrary
(
const fileName& libPath,
const string& globalFuncName,
const dictionary& contextDict
)
{
void* lib = 0;
if (!libPath.empty())
{
lib = dlLibraryTable::findLibrary(libPath);
}
if (!lib)
{
return;
}
// provision for manual execution of code before unloading
if (dlSymFound(lib, globalFuncName))
{
loaderFunctionType function =
reinterpret_cast<loaderFunctionType>
(
dlSym(lib, globalFuncName)
);
if (function)
{
(*function)(false); // force unload
}
else
{
FatalIOErrorIn
(
"codedFunctionObject::unloadLibrary()",
contextDict
) << "Failed looking up symbol " << globalFuncName << nl
<< "from " << libPath << exit(FatalIOError);
}
}
if (!dlLibraryTable::close(libPath, false))
{
FatalIOErrorIn
(
"codedFunctionObject::"
"updateLibrary()",
contextDict
) << "Failed unloading library " << libPath
<< exit(FatalIOError);
}
}
// * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * //
void Foam::codedFunctionObject::createLibrary
(
dynamicCode& dynCode,
const dynamicCodeContext& context
) const
{
bool create = Pstream::master();
if (create)
{
// Write files for new library
if (!dynCode.upToDate(context))
{
Info<< "Using dynamicCode for functionObject " << name()
<< endl;
// filter with this context
dynCode.reset(context);
// Set additional rewrite rules
dynCode.setFilterVariable("typeName", redirectType_);
dynCode.setFilterVariable("codeRead", codeRead_);
dynCode.setFilterVariable("codeExecute", codeExecute_);
dynCode.setFilterVariable("codeEnd", codeEnd_);
//dynCode.setFilterVariable("codeWrite", codeWrite_);
// compile filtered C template
dynCode.addCompileFile("functionObjectTemplate.C");
dynCode.addCompileFile("FilterFunctionObjectTemplate.C");
// copy filtered H template
dynCode.addCopyFile("FilterFunctionObjectTemplate.H");
dynCode.addCopyFile("functionObjectTemplate.H");
dynCode.addCopyFile("IOfunctionObjectTemplate.H");
// debugging: make BC verbose
// dynCode.setFilterVariable("verbose", "true");
// Info<<"compile " << redirectType_ << " sha1: "
// << context.sha1() << endl;
// 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()
);
if (!dynCode.copyOrCreateFiles(true))
{
FatalIOErrorIn
(
"codedFunctionObject::createLibrary(..)",
context.dict()
) << "Failed writing files for" << nl
<< dynCode.libRelPath() << nl
<< exit(FatalIOError);
}
}
if (!dynCode.wmakeLibso())
{
FatalIOErrorIn
(
"codedFunctionObject::createLibrary(..)",
context.dict()
) << "Failed wmake " << dynCode.libRelPath() << nl
<< exit(FatalIOError);
}
}
// all processes must wait for compile to finish
reduce(create, orOp<bool>());
}
void Foam::codedFunctionObject::updateLibrary() const
{
dynamicCode::checkSecurity
(
"codedFunctionObject::updateLibrary()",
dict_
);
dynamicCodeContext context(dict_);
// codeName: redirectType + _<sha1>
// codeDir : redirectType
dynamicCode dynCode
(
redirectType_ + context.sha1().str(true),
redirectType_
);
const fileName libPath = dynCode.libPath();
// the correct library was already loaded => we are done
if (dlLibraryTable::findLibrary(libPath))
{
return;
}
// remove instantiation of fvPatchField provided by library
redirectFunctionObjectPtr_.clear();
// may need to unload old library
unloadLibrary
(
oldLibPath_,
dynamicCode::libraryBaseName(oldLibPath_),
context.dict()
);
// try loading an existing library (avoid compilation when possible)
if (!loadLibrary(libPath, dynCode.codeName(), context.dict()))
{
createLibrary(dynCode, context);
loadLibrary(libPath, dynCode.codeName(), context.dict());
}
// retain for future reference
oldLibPath_ = libPath;
}
// * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
Foam::codedFunctionObject::codedFunctionObject
(
const word& name,
const Time& time,
const dictionary& dict
)
:
functionObject(name),
time_(time),
dict_(dict)
{
read(dict_);
}
// * * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * //
Foam::codedFunctionObject::~codedFunctionObject()
{}
// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
Foam::functionObject&
Foam::codedFunctionObject::redirectFunctionObject() const
{
if (!redirectFunctionObjectPtr_.valid())
{
dictionary constructDict(dict_);
constructDict.set("type", redirectType_);
redirectFunctionObjectPtr_ = functionObject::New
(
redirectType_,
time_,
constructDict
);
}
return redirectFunctionObjectPtr_();
}
bool Foam::codedFunctionObject::start()
{
updateLibrary();
return redirectFunctionObject().start();
}
bool Foam::codedFunctionObject::execute(const bool forceWrite)
{
updateLibrary();
return redirectFunctionObject().execute(forceWrite);
}
bool Foam::codedFunctionObject::end()
{
updateLibrary();
return redirectFunctionObject().end();
}
bool Foam::codedFunctionObject::read(const dictionary& dict)
{
dict.lookup("redirectType") >> redirectType_;
if (dict.found("codeRead"))
{
codeRead_ = stringOps::trim(dict["codeRead"]);
stringOps::inplaceExpand(codeRead_, dict);
}
if (dict.found("codeExecute"))
{
codeExecute_ = stringOps::trim(dict["codeExecute"]);
stringOps::inplaceExpand(codeExecute_, dict);
}
if (dict.found("codeEnd"))
{
codeEnd_ = stringOps::trim(dict["codeEnd"]);
stringOps::inplaceExpand(codeEnd_, dict);
}
updateLibrary();
return redirectFunctionObject().read(dict);
}
// ************************************************************************* //

View File

@ -0,0 +1,174 @@
/*---------------------------------------------------------------------------*\
========= |
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration |
\\ / A nd | Copyright (C) 2011-2011 OpenCFD Ltd.
\\/ M anipulation |
-------------------------------------------------------------------------------
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/>.
Class
Foam::codedFunctionObject
Description
functionObject using dynamic code compilation.
SourceFiles
codedFunctionObject.C
\*---------------------------------------------------------------------------*/
#ifndef codedFunctionObject_H
#define codedFunctionObject_H
#include "pointFieldFwd.H"
#include "functionObject.H"
#include "dictionary.H"
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
namespace Foam
{
// Forward declaration of classes
class objectRegistry;
class dictionary;
class mapPolyMesh;
class dynamicCode;
class dynamicCodeContext;
class IOdictionary;
/*---------------------------------------------------------------------------*\
Class codedFunctionObject Declaration
\*---------------------------------------------------------------------------*/
class codedFunctionObject
:
public functionObject
{
protected:
// Protected data
//- Reference to the time database
const Time& time_;
//- Input dictionary
dictionary dict_;
word redirectType_;
string codeRead_;
string codeExecute_;
string codeEnd_;
//- Previously loaded library
mutable fileName oldLibPath_;
//- Underlying functionObject
mutable autoPtr<functionObject> redirectFunctionObjectPtr_;
// Private Member Functions
//- Global loader/unloader function type
typedef void (*loaderFunctionType)(bool);
//- Load specified library and execute globalFuncName(true)
static void* loadLibrary
(
const fileName& libPath,
const string& globalFuncName,
const dictionary& contextDict
);
//- Execute globalFuncName(false) and unload specified library
static void unloadLibrary
(
const fileName& libPath,
const string& globalFuncName,
const dictionary& contextDict
);
//- Create library based on the dynamicCodeContext
void createLibrary(dynamicCode&, const dynamicCodeContext&) const;
//- Update library as required
void updateLibrary() const;
//- Read relevant dictionary entries
void readDict();
//- Disallow default bitwise copy construct
codedFunctionObject(const codedFunctionObject&);
//- Disallow default bitwise assignment
void operator=(const codedFunctionObject&);
public:
//- Runtime type information
TypeName("coded");
// Constructors
//- Construct for given objectRegistry and dictionary.
// Allow the possibility to load fields from files
codedFunctionObject
(
const word& name,
const Time& time,
const dictionary& dict
);
//- Destructor
virtual ~codedFunctionObject();
// Member Functions
//- Dynamically compiled functionObject
functionObject& redirectFunctionObject() const;
//- Called at the start of the time-loop
virtual bool start();
//- Called at each ++ or += of the time-loop. forceWrite overrides the
// outputControl behaviour.
virtual bool execute(const bool forceWrite);
//- Called when Time::run() determines that the time-loop exits.
// By default it simply calls execute().
virtual bool end();
//- Read and set the function object if its data have changed
virtual bool read(const dictionary&);
};
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
} // End namespace Foam
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
#endif
// ************************************************************************* //