Merge branch 'master' of /home/noisy3/OpenFOAM/OpenFOAM-dev

This commit is contained in:
andy
2011-03-23 18:20:12 +00:00
185 changed files with 1605 additions and 289 deletions

View File

@ -37,6 +37,7 @@ Description
#include "fileStat.H"
#include "timer.H"
#include "IFstream.H"
#include "DynamicList.H"
#include <fstream>
#include <cstdlib>
@ -52,6 +53,7 @@ Description
#include <sys/socket.h>
#include <netdb.h>
#include <dlfcn.h>
#include <link.h>
#include <netinet/in.h>
@ -1107,10 +1109,20 @@ void* Foam::dlOpen(const fileName& lib)
{
if (POSIX::debug)
{
Info<< "dlOpen(const fileName&)"
<< " : dlopen of " << lib << endl;
std::cout<< "dlOpen(const fileName&)"
<< " : dlopen of " << lib << std::endl;
}
return ::dlopen(lib.c_str(), RTLD_LAZY|RTLD_GLOBAL);
void* handle = ::dlopen(lib.c_str(), RTLD_LAZY|RTLD_GLOBAL);
if (POSIX::debug)
{
std::cout
<< "dlOpen(const fileName&)"
<< " : dlopen of " << lib
<< " handle " << handle << std::endl;
}
return handle;
}
@ -1118,8 +1130,9 @@ bool Foam::dlClose(void* handle)
{
if (POSIX::debug)
{
Info<< "dlClose(void*)"
<< " : dlclose" << endl;
std::cout
<< "dlClose(void*)"
<< " : dlclose of handle " << handle << std::endl;
}
return ::dlclose(handle) == 0;
}
@ -1129,8 +1142,9 @@ void* Foam::dlSym(void* handle, const std::string& symbol)
{
if (POSIX::debug)
{
Info<< "dlSym(void*, const std::string&)"
<< " : dlsym of " << symbol << endl;
std::cout
<< "dlSym(void*, const std::string&)"
<< " : dlsym of " << symbol << std::endl;
}
// clear any old errors - see manpage dlopen
(void) ::dlerror();
@ -1158,8 +1172,9 @@ bool Foam::dlSymFound(void* handle, const std::string& symbol)
{
if (POSIX::debug)
{
Info<< "dlSymFound(void*, const std::string&)"
<< " : dlsym of " << symbol << endl;
std::cout
<< "dlSymFound(void*, const std::string&)"
<< " : dlsym of " << symbol << std::endl;
}
// clear any old errors - see manpage dlopen
@ -1178,4 +1193,32 @@ bool Foam::dlSymFound(void* handle, const std::string& symbol)
}
static int collectLibsCallback
(
struct dl_phdr_info *info,
size_t size,
void *data
)
{
Foam::DynamicList<Foam::fileName>* ptr =
reinterpret_cast<Foam::DynamicList<Foam::fileName>*>(data);
ptr->append(info->dlpi_name);
return 0;
}
Foam::fileNameList Foam::dlLoaded()
{
DynamicList<fileName> libs;
dl_iterate_phdr(collectLibsCallback, &libs);
if (POSIX::debug)
{
std::cout
<< "dlLoaded()"
<< " : determined loaded libraries :" << libs.size() << endl;
}
return libs;
}
// ************************************************************************* //

View File

@ -136,7 +136,9 @@ bool Foam::functionEntries::codeStream::execute
(
"EXE_INC = -g \\\n"
+ context.options()
+ "\n\nLIB_LIBS ="
+ "\n\nLIB_LIBS = \\\n"
+ " -lOpenFOAM \\\n"
+ context.libs()
);
if (!dynCode.copyOrCreateFiles(true))
@ -172,6 +174,8 @@ bool Foam::functionEntries::codeStream::execute
"functionEntries::codeStream::execute(..)",
parentDict
) << "Failed loading library " << libPath << nl
<< "Did you add all libraries to the 'libs' entry"
<< " in system/controlDict?"
<< exit(FatalIOError);
}

View File

@ -31,8 +31,6 @@ License
#include "OFstream.H"
#include "OSspecific.H"
#include "dictionary.H"
#include "dlLibraryTable.H"
// * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
@ -557,22 +555,4 @@ bool Foam::dynamicCode::upToDate(const dynamicCodeContext& context) const
}
// bool Foam::dynamicCode::openLibrary() const
// {
// return dlLibraryTable::openLibrary(this->libPath(), false);
// }
//
//
// bool Foam::dynamicCode::closeLibrary() const
// {
// return dlLibraryTable::closeLibrary(this->libPath(), false);
// }
//
//
// void* Foam::dynamicCode::findLibrary() const
// {
// return dlLibraryTable::findLibrary(this->libPath());
// }
// ************************************************************************* //

View File

@ -37,10 +37,8 @@ SourceFiles
#define dynamicCode_H
#include "Tuple2.H"
#include "SHA1Digest.H"
#include "HashTable.H"
#include "DynamicList.H"
#include "dlLibraryTable.H"
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
@ -51,6 +49,7 @@ namespace Foam
class dynamicCodeContext;
class ISstream;
class OSstream;
class SHA1Digest;
/*---------------------------------------------------------------------------*\
Class dynamicCode Declaration
@ -283,16 +282,6 @@ public:
//- Compile a libso
bool wmakeLibso() const;
// //- Open the libPath() library
// bool openLibrary() const;
//
// //- Close the libPath() library
// bool closeLibrary() const;
//
// //- Find the handle of the libPath() library
// void* findLibrary() const;
};

View File

@ -37,7 +37,8 @@ Foam::dynamicCodeContext::dynamicCodeContext(const dictionary& dict)
code_(stringOps::trim(dict["code"])),
localCode_(),
include_(),
options_()
options_(),
libs_()
{
// expand dictionary entries
stringOps::inplaceExpand(code_, dict);
@ -67,9 +68,16 @@ Foam::dynamicCodeContext::dynamicCodeContext(const dictionary& dict)
stringOps::inplaceExpand(options_, dict);
}
// optional
if (dict.found("codeLibs"))
{
libs_ = stringOps::trim(dict["codeLibs"]);
stringOps::inplaceExpand(libs_, dict);
}
// calculate SHA1 digest from include, options, localCode, code
OSHA1stream os;
os << include_ << options_ << localCode_ << code_;
os << include_ << options_ << libs_ << localCode_ << code_;
sha1_ = os.digest();
}

View File

@ -66,6 +66,9 @@ class dynamicCodeContext
//- Optional "codeOptions" entry
string options_;
//- Optional "codeLib" entry
string libs_;
//- Calculated SHA1Digest
SHA1Digest sha1_;
@ -96,6 +99,12 @@ public:
return options_;
}
//- Return the code-libs
const string& libs() const
{
return libs_;
}
//- Return the code
const string& code() const
{

View File

@ -2,7 +2,7 @@
========= |
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration |
\\ / A nd | Copyright (C) 2004-2010 OpenCFD Ltd.
\\ / A nd | Copyright (C) 2004-2011 OpenCFD Ltd.
\\/ M anipulation |
-------------------------------------------------------------------------------
License
@ -39,6 +39,7 @@ void Foam::OutputFilterFunctionObject<OutputFilter>::readDict()
dict_.readIfPresent("storeFilter", storeFilter_);
}
template<class OutputFilter>
void Foam::OutputFilterFunctionObject<OutputFilter>::allocateFilter()
{
@ -68,12 +69,14 @@ void Foam::OutputFilterFunctionObject<OutputFilter>::allocateFilter()
}
}
template<class OutputFilter>
void Foam::OutputFilterFunctionObject<OutputFilter>::destroyFilter()
{
ptr_.reset();
}
// * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
template<class OutputFilter>

View File

@ -2,7 +2,7 @@
========= |
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration |
\\ / A nd | Copyright (C) 2004-2010 OpenCFD Ltd.
\\ / A nd | Copyright (C) 2004-2011 OpenCFD Ltd.
\\/ M anipulation |
-------------------------------------------------------------------------------
License
@ -51,7 +51,7 @@ void Foam::DimensionedField<Type, GeoMesh>::readIfPresent
{
if
(
(this->headerOk() && this->readOpt() == IOobject::READ_IF_PRESENT)
(this->readOpt() == IOobject::READ_IF_PRESENT && this->headerOk())
|| this->readOpt() == IOobject::MUST_READ
|| this->readOpt() == IOobject::MUST_READ_IF_MODIFIED
)

View File

@ -196,6 +196,9 @@ void* dlSym(void* handle, const std::string& symbol);
//- Report if symbol in a dlopened library could be found
bool dlSymFound(void* handle, const std::string& symbol);
//- Return all loaded libraries
fileNameList dlLoaded();
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //

View File

@ -31,6 +31,45 @@ License
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
//! \cond fileScope
// Find the type/position of the ":-" or ":+" alternative values
//
static inline int findParameterAlternative
(
const std::string& s,
std::string::size_type& pos,
std::string::size_type endPos
)
{
while (pos != std::string::npos)
{
pos = s.find(':', pos);
if (pos != std::string::npos)
{
if (pos < endPos)
{
// in-range: check for '+' or '-' following the ':'
const int altType = s[pos+1];
if (altType == '+' || altType == '-')
{
return altType;
}
++pos; // unknown/unsupported - continue at next position
}
else
{
// out-of-range: abort
pos = std::string::npos;
}
}
}
return 0;
}
//! \endcond
Foam::string Foam::stringOps::expand
(
const string& original,
@ -66,7 +105,8 @@ Foam::string& Foam::stringOps::inplaceExpand
string::size_type endVar = begVar;
string::size_type delim = 0;
// The position of the ":-" default value
// The type/position of the ":-" or ":+" alternative values
int altType = 0;
string::size_type altPos = string::npos;
if (s[begVar+1] == '{')
@ -74,14 +114,11 @@ Foam::string& Foam::stringOps::inplaceExpand
endVar = s.find('}', begVar);
delim = 1;
// looks like ${parameter:-word}
// check for ${parameter:-word} or ${parameter:+word}
if (endVar != string::npos)
{
altPos = s.find(":-", begVar);
if (altPos != string::npos && altPos > endVar)
{
altPos = string::npos;
}
altPos = begVar;
altType = findParameterAlternative(s, altPos, endVar);
}
}
else
@ -134,7 +171,7 @@ Foam::string& Foam::stringOps::inplaceExpand
std::string altValue;
if (altPos != string::npos)
{
// had ":-" default value
// had ":-" or ":+" alternative value
altValue = s.substr
(
altPos + 2,
@ -148,17 +185,32 @@ Foam::string& Foam::stringOps::inplaceExpand
if (fnd != HashTable<string, word, string::hash>::end())
{
s.std::string::replace
(
begVar,
endVar - begVar + 1,
*fnd
);
begVar += (*fnd).size();
if (altPos != string::npos && altType == '+')
{
// was found, use ":+" alternative
s.std::string::replace
(
begVar,
endVar - begVar + 1,
altValue
);
begVar += altValue.size();
}
else
{
// was found, use value
s.std::string::replace
(
begVar,
endVar - begVar + 1,
*fnd
);
begVar += (*fnd).size();
}
}
else if (altPos != string::npos)
else if (altPos != string::npos && altType == '-')
{
// use alternative provided
// was not found, use ":-" alternative
s.std::string::replace
(
begVar,
@ -169,12 +221,8 @@ Foam::string& Foam::stringOps::inplaceExpand
}
else
{
s.std::string::replace
(
begVar,
endVar - begVar + 1,
""
);
// substitute with nothing, also for ":+" alternative
s.std::string::erase(begVar, endVar - begVar + 1);
}
}
}
@ -351,7 +399,8 @@ Foam::string& Foam::stringOps::inplaceExpand
string::size_type endVar = begVar;
string::size_type delim = 0;
// The position of the ":-" default value
// The type/position of the ":-" or ":+" alternative values
int altType = 0;
string::size_type altPos = string::npos;
if (s[begVar+1] == '{')
@ -359,14 +408,11 @@ Foam::string& Foam::stringOps::inplaceExpand
endVar = s.find('}', begVar);
delim = 1;
// looks like ${parameter:-word}
// check for ${parameter:-word} or ${parameter:+word}
if (endVar != string::npos)
{
altPos = s.find(":-", begVar);
if (altPos != string::npos && altPos > endVar)
{
altPos = string::npos;
}
altPos = begVar;
altType = findParameterAlternative(s, altPos, endVar);
}
}
else
@ -413,7 +459,7 @@ Foam::string& Foam::stringOps::inplaceExpand
std::string altValue;
if (altPos != string::npos)
{
// had ":-" default value
// had ":-" or ":+" alternative value
altValue = s.substr
(
altPos + 2,
@ -424,34 +470,53 @@ Foam::string& Foam::stringOps::inplaceExpand
const string varValue = getEnv(varName);
if (varValue.size())
{
// direct replacement
s.std::string::replace
(
begVar,
endVar - begVar + 1,
varValue
);
begVar += varValue.size();
if (altPos != string::npos && altType == '+')
{
// was found, use ":+" alternative
s.std::string::replace
(
begVar,
endVar - begVar + 1,
altValue
);
begVar += altValue.size();
}
else
{
// was found, use value
s.std::string::replace
(
begVar,
endVar - begVar + 1,
varValue
);
begVar += varValue.size();
}
}
else if (altPos != string::npos)
{
// use alternative provided
s.std::string::replace
(
begVar,
endVar - begVar + 1,
altValue
);
begVar += altValue.size();
// use ":-" or ":+" alternative values
if (altType == '-')
{
// was not found, use ":-" alternative
s.std::string::replace
(
begVar,
endVar - begVar + 1,
altValue
);
begVar += altValue.size();
}
else
{
// was not found, ":+" alternative implies
// substitute with nothing
s.std::string::erase(begVar, endVar - begVar + 1);
}
}
else if (allowEmpty)
{
s.std::string::replace
(
begVar,
endVar - begVar + 1,
""
);
s.std::string::erase(begVar, endVar - begVar + 1);
}
else
{
@ -459,7 +524,7 @@ Foam::string& Foam::stringOps::inplaceExpand
(
"stringOps::inplaceExpand(string&, const bool)"
)
<< "Unknown variable name " << varName << '.'
<< "Unknown variable name '" << varName << "'"
<< exit(FatalError);
}
}

View File

@ -62,6 +62,13 @@ namespace stringOps
// If parameter is unset or null, the \c defValue is substituted.
// Otherwise, the value of parameter is substituted.
//
// Supports alternative values as per the Bourne/Korn shell.
// \code
// "${parameter:+altValue}"
// \endcode
// If parameter is unset or null, nothing is substituted.
// Otherwise the \c altValue is substituted.
//
// Any unknown entries are removed silently.
//
// Malformed entries (eg, brace mismatch, sigil followed by bad character)
@ -89,6 +96,13 @@ namespace stringOps
// If parameter is unset or null, the \c defValue is substituted.
// Otherwise, the value of parameter is substituted.
//
// Supports alternative values as per the Bourne/Korn shell.
// \code
// "${parameter:+altValue}"
// \endcode
// If parameter is unset or null, nothing is substituted.
// Otherwise the \c altValue is substituted.
//
// Any unknown entries are removed silently.
//
// Malformed entries (eg, brace mismatch, sigil followed by bad character)
@ -155,6 +169,13 @@ namespace stringOps
// If parameter is unset or null, the \c defValue is substituted.
// Otherwise, the value of parameter is substituted.
//
// Supports alternative values as per the Bourne/Korn shell.
// \code
// "${parameter:+altValue}"
// \endcode
// If parameter is unset or null, nothing is substituted.
// Otherwise the \c altValue is substituted.
//
// Any unknown entries are removed silently, if allowEmpty is true.
//
// Malformed entries (eg, brace mismatch, sigil followed by bad character)
@ -187,6 +208,13 @@ namespace stringOps
// If parameter is unset or null, the \c defValue is substituted.
// Otherwise, the value of parameter is substituted.
//
// Supports alternative values as per the Bourne/Korn shell.
// \code
// "${parameter:+altValue}"
// \endcode
// If parameter is unset or null, nothing is substituted.
// Otherwise the \c altValue is substituted.
//
// Any unknown entries are removed silently, if allowEmpty is true.
//
// Malformed entries (eg, brace mismatch, sigil followed by bad character)

View File

@ -37,6 +37,9 @@ License
#include "stringOps.H"
#include "IOdictionary.H"
#include <dlfcn.h>
#include <link.h>
// * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
template<class Type>
@ -270,7 +273,10 @@ void Foam::codedFixedValueFvPatchField<Type>::createLibrary
"EXE_INC = -g \\\n"
"-I$(LIB_SRC)/finiteVolume/lnInclude\\\n"
+ context.options()
+ "\n\nLIB_LIBS = "
+ "\n\nLIB_LIBS = \\\n"
+ " -lOpenFOAM \\\n"
+ " -lfiniteVolume \\\n"
+ context.libs()
);
if (!dynCode.copyOrCreateFiles(true))

View File

@ -80,6 +80,9 @@ SourceFiles
#include "fixedValueFvPatchFields.H"
#include <dlfcn.h>
#include <link.h>
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
namespace Foam
@ -119,6 +122,13 @@ class codedFixedValueFvPatchField
//- Global loader/unloader function type
typedef void (*loaderFunctionType)(bool);
static int collectLibsCallback
(
struct dl_phdr_info *info,
size_t size,
void *data
);
//- Load specified library and execute globalFuncName(true)
static void* loadLibrary
(

View File

@ -332,7 +332,19 @@ void timeVaryingMappedFixedValueFvPatchField<Type>::readSamplePoints()
(
referenceCS().localPosition(samplePoints)
);
const vectorField& localVertices = tlocalVertices();
vectorField& localVertices = tlocalVertices();
// Shear to avoid degenerate cases
forAll(localVertices, i)
{
point& pt = localVertices[i];
const scalar magPt = mag(pt);
const point nptDir = pt/magPt;
if (magPt > ROOTVSMALL)
{
pt += pow(magPt, 1.1 + Foam::sqrt(SMALL))*nptDir;
}
}
// Determine triangulation
List<vector2D> localVertices2D(localVertices.size());
@ -342,7 +354,7 @@ void timeVaryingMappedFixedValueFvPatchField<Type>::readSamplePoints()
localVertices2D[i][1] = localVertices[i][1];
}
tmp<pointField> localFaceCentres
tmp<pointField> tlocalFaceCentres
(
referenceCS().localPosition
(
@ -350,6 +362,20 @@ void timeVaryingMappedFixedValueFvPatchField<Type>::readSamplePoints()
)
);
pointField& localFaceCentres = tlocalFaceCentres();
// Shear to avoid degenerate cases
forAll(localFaceCentres, i)
{
point& pt = localFaceCentres[i];
const scalar magPt = mag(pt);
const point nptDir = pt/magPt;
if (magPt > ROOTVSMALL)
{
pt += pow(magPt, 1.1 + Foam::sqrt(SMALL))*nptDir;
}
}
if (debug)
{
OFstream str
@ -360,9 +386,9 @@ void timeVaryingMappedFixedValueFvPatchField<Type>::readSamplePoints()
Pout<< "readSamplePoints :"
<< " Dumping face centres to " << str.name() << endl;
forAll(localFaceCentres(), i)
forAll(localFaceCentres, i)
{
const point& p = localFaceCentres()[i];
const point& p = localFaceCentres[i];
str<< "v " << p.x() << ' ' << p.y() << ' ' << p.z() << nl;
}
}
@ -380,9 +406,9 @@ void timeVaryingMappedFixedValueFvPatchField<Type>::readSamplePoints()
Pout<< "readSamplePoints :"
<< " Dumping face centres to " << str.name() << endl;
forAll(localFaceCentres(), i)
forAll(localFaceCentres, i)
{
const point& p = localFaceCentres()[i];
const point& p = localFaceCentres[i];
str<< "v " << p.x() << ' ' << p.y() << ' ' << p.z() << nl;
}
}

View File

@ -2,7 +2,7 @@
========= |
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration |
\\ / A nd | Copyright (C) 2004-2010 OpenCFD Ltd.
\\ / A nd | Copyright (C) 2004-2011 OpenCFD Ltd.
\\/ M anipulation |
-------------------------------------------------------------------------------
License

View File

@ -2,7 +2,7 @@
========= |
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration |
\\ / A nd | Copyright (C) 2004-2010 OpenCFD Ltd.
\\ / A nd | Copyright (C) 2004-2011 OpenCFD Ltd.
\\/ M anipulation |
-------------------------------------------------------------------------------
License

View File

@ -2,7 +2,7 @@
========= |
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration |
\\ / A nd | Copyright (C) 2004-2010 OpenCFD Ltd.
\\ / A nd | Copyright (C) 2004-2011 OpenCFD Ltd.
\\/ M anipulation |
-------------------------------------------------------------------------------
License
@ -91,7 +91,24 @@ Foam::pointIndexHit Foam::searchableCylinder::findNearest
// distance to cylinder wall: magV-radius_
// Nearest cylinder point
point cylPt = sample + (radius_-magV)*v;
point cylPt;
if (magV < ROOTVSMALL)
{
// Point exactly on centre line. Take any point on wall.
vector e1 = point(1,0,0) ^ unitDir_;
scalar magE1 = mag(e1);
if (magE1 < SMALL)
{
e1 = point(0,1,0) ^ unitDir_;
magE1 = mag(e1);
}
e1 /= magE1;
cylPt = sample + radius_*e1;
}
else
{
cylPt = sample + (radius_-magV)*v;
}
if (parallel < 0.5*magDir_)
{

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
// ************************************************************************* //