ENH: #codeStream : new functionEntry

This commit is contained in:
mattijs 2011-02-21 10:13:01 +00:00
parent a218fb16ac
commit 5538eb4f95
12 changed files with 1613 additions and 2 deletions

View File

@ -868,6 +868,9 @@ InfoSwitches
{
writePrecision 6;
writeJobInfo 0;
// Allow case-supplied c++ code (#codeStream, codedFixedValue)
allowSystemOperations 0;
}
OptimisationSwitches

View File

@ -159,6 +159,8 @@ $(dictionaryEntry)/dictionaryEntry.C
$(dictionaryEntry)/dictionaryEntryIO.C
functionEntries = $(dictionary)/functionEntries
$(functionEntries)/codeStream/codeStream.C
$(functionEntries)/codeStream/codeStreamTools.C
$(functionEntries)/functionEntry/functionEntry.C
$(functionEntries)/includeEntry/includeEntry.C
$(functionEntries)/includeIfPresentEntry/includeIfPresentEntry.C

View File

@ -0,0 +1,242 @@
/*---------------------------------------------------------------------------*\
========= |
\\ / 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 "codeStream.H"
#include "addToMemberFunctionSelectionTable.H"
#include "IStringStream.H"
#include "OStringStream.H"
#include "IOstreams.H"
#include "SHA1Digest.H"
#include "OSHA1stream.H"
#include "codeStreamTools.H"
#include "dlLibraryTable.H"
#include "OSspecific.H"
#include "Time.H"
#include "Pstream.H"
// * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
namespace Foam
{
namespace functionEntries
{
defineTypeNameAndDebug(codeStream, 0);
addToMemberFunctionSelectionTable
(
functionEntry,
codeStream,
execute,
primitiveEntryIstream
);
}
}
// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
bool Foam::functionEntries::codeStream::execute
(
const dictionary& parentDict,
primitiveEntry& entry,
Istream& is
)
{
if (isAdministrator())
{
FatalIOErrorIn
(
"functionEntries::codeStream::execute(..)",
parentDict
) << "This code should not be executed by someone with administrator"
<< " rights due to security reasons." << endl
<< "(it writes a shared library which then gets loaded "
<< "using dlopen)"
<< exit(FatalIOError);
}
// Read three sections of code. Remove any leading empty lines
// (necessary for compilation options, just visually pleasing for includes
// and body).
dictionary codeDict(is);
string codeInclude = "";
if (codeDict.found("codeInclude"))
{
codeInclude = codeStreamTools::stripLeading(codeDict["codeInclude"]);
}
string code = codeStreamTools::stripLeading(codeDict["code"]);
string codeOptions = "";
if (codeDict.found("codeOptions"))
{
codeOptions = codeStreamTools::stripLeading(codeDict["codeOptions"]);
}
// Create name out of contents
SHA1Digest sha;
{
OSHA1stream os;
os << codeInclude << code << codeOptions;
sha = os.digest();
}
fileName name;
{
OStringStream str;
str << sha;
name = "codeStream" + str.str();
}
fileName dir;
if (isA<IOdictionary>(parentDict))
{
const IOdictionary& d = static_cast<const IOdictionary&>(parentDict);
dir = d.db().time().constantPath()/"codeStream"/name;
}
else
{
dir = "codeStream"/name;
}
fileName libPath
(
Foam::getEnv("FOAM_USER_LIBBIN")
/ "lib"
+ name
+ ".so"
);
void* lib = dlLibraryTable::findLibrary(libPath);
if (!lib)
{
if (Pstream::master())
{
if (!codeStreamTools::upToDate(dir, sha))
{
Info<< "Creating new library in " << libPath << endl;
fileName templates(Foam::getEnv("OTF_TEMPLATE_DIR"));
if (!templates.size())
{
FatalIOErrorIn
(
"functionEntries::codeStream::execute(..)",
parentDict
) << "Please set environment variable OTF_TEMPLATE_DIR"
<< " to point to the location of codeStreamTemplate.C"
<< exit(FatalIOError);
}
List<fileAndVars> copyFiles(1);
copyFiles[0].first() = templates/"codeStreamTemplate.C";
stringPairList bodyVars(2);
bodyVars[0] = Pair<string>("OTF_INCLUDES", codeInclude);
bodyVars[1] = Pair<string>("OTF_BODY", code);
copyFiles[0].second() = bodyVars;
List<fileAndContent> filesContents(2);
// Write Make/files
filesContents[0].first() = "Make/files";
filesContents[0].second() =
"codeStreamTemplate.C \n\
LIB = $(FOAM_USER_LIBBIN)/lib" + name;
// Write Make/options
filesContents[1].first() = "Make/options";
filesContents[1].second() =
"EXE_INC = -g\\\n" + codeOptions + "\n\nLIB_LIBS = ";
codeStreamTools writer(name, copyFiles, filesContents);
if (!writer.copyFilesContents(dir))
{
FatalIOErrorIn
(
"functionEntries::codeStream::execute(..)",
parentDict
) << "Failed writing " << endl
<< copyFiles << endl
<< filesContents
<< exit(FatalIOError);
}
}
Foam::string wmakeCmd("wmake libso " + dir);
Info<< "Invoking " << wmakeCmd << endl;
if (Foam::system(wmakeCmd))
{
FatalIOErrorIn
(
"functionEntries::codeStream::execute(..)",
parentDict
) << "Failed " << wmakeCmd << exit(FatalIOError);
}
}
if (!dlLibraryTable::open(libPath))
{
FatalIOErrorIn
(
"functionEntries::codeStream::execute(..)",
parentDict
) << "Failed loading library " << libPath << exit(FatalIOError);
}
lib = dlLibraryTable::findLibrary(libPath);
}
else
{
Info<< "Reusing library in " << libPath << endl;
}
// Find the library handle.
void (*function)(const dictionary&, Ostream& os);
*(void **) (&function) = dlSym(lib, name);
if (!function)
{
FatalIOErrorIn
(
"functionEntries::codeStream::execute(..)",
parentDict
) << "Failed looking up symbol " << name
<< " in library " << lib << exit(FatalIOError);
}
OStringStream os;
(*function)(parentDict, os);
IStringStream resultStream(os.str());
entry.read(parentDict, resultStream);
return true;
}
// ************************************************************************* //

View File

@ -0,0 +1,136 @@
/*---------------------------------------------------------------------------*\
========= |
\\ / 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::functionEntries::codeStream
Description
Dictionary entry that contains C++ OpenFOAM code that is compiled to
generate the entry itself. So
- codeStream reads three entries: 'code', 'codeInclude' (optional),
'codeOptions' (optional)
and uses those to generate library sources inside constant/codeStream/
- these get compiled using 'wmake libso'
- the resulting library is loaded in executed with as arguments
const dictionary& dict,
Ostream& os
where the dictionary is the current dictionary.
- the code has to write into Ostream which is then used to construct
the actual dictionary entry.
E.g. to set the internal field of a field:
internalField #codeStream
{
code
#{
const IOdictionary& d = static_cast<const IOdictionary&>(dict);
const fvMesh& mesh = refCast<const fvMesh>(d.db());
scalarField fld(mesh.nCells(), 12.34);
fld.writeEntry("", os);
#};
//- Optional:
codeInclude
#{
#include "fvCFD.H"
#};
codeOptions
#{
-I$(LIB_SRC)/finiteVolume/lnInclude
#};
};
Note the #{ #} syntax which is just a way of inputting strings with embedded
newlines.
Limitations:
- '~' symbol not allowed inside the code sections.
- probably some other limitations (uses string::expand which expands $, ~)
SourceFiles
codeStream.C
\*---------------------------------------------------------------------------*/
#ifndef codeStream_H
#define codeStream_H
#include "functionEntry.H"
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
namespace Foam
{
class ISstream;
namespace functionEntries
{
/*---------------------------------------------------------------------------*\
Class codeStream Declaration
\*---------------------------------------------------------------------------*/
class codeStream
:
public functionEntry
{
// Private Member Functions
//- Disallow default bitwise copy construct
codeStream(const codeStream&);
//- Disallow default bitwise assignment
void operator=(const codeStream&);
public:
//- Runtime type information
ClassName("codeStream");
// Member Functions
static bool execute
(
const dictionary& parentDict,
primitiveEntry& entry,
Istream& is
);
};
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
} // End namespace functionEntries
} // End namespace Foam
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
#endif
// ************************************************************************* //

View File

@ -0,0 +1,282 @@
/*---------------------------------------------------------------------------*\
========= |
\\ / 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 2 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, write to the Free Software Foundation,
Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
\*---------------------------------------------------------------------------*/
#include "codeStreamTools.H"
#include "IFstream.H"
#include "OFstream.H"
#include "OSspecific.H"
#include "dictionary.H"
#include "dlLibraryTable.H"
// * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
int Foam::codeStreamTools::allowSystemOperations
(
Foam::debug::infoSwitch("allowSystemOperations", 0)
);
// * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * //
void Foam::codeStreamTools::copyAndExpand
(
ISstream& sourceStr,
OSstream& destStr
) const
{
if (!sourceStr.good())
{
FatalErrorIn
(
"codeStreamTools::copyAndExpand()"
" const"
) << "Failed opening for reading " << sourceStr.name()
<< exit(FatalError);
}
if (!destStr.good())
{
FatalErrorIn
(
"codeStreamTools::copyAndExpand()"
" const"
) << "Failed writing " << destStr.name() << exit(FatalError);
}
// Copy file whilst rewriting environment vars
string line;
do
{
sourceStr.getLine(line);
line.expand(true, true); // replace any envvars inside substitutions
destStr<< line.c_str() << nl;
}
while (sourceStr.good());
}
// * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
Foam::codeStreamTools::codeStreamTools()
{}
Foam::codeStreamTools::codeStreamTools
(
const word& name,
const dictionary& dict
)
:
name_(name)
{
read(dict);
}
Foam::codeStreamTools::codeStreamTools
(
const word& name,
const List<fileAndVars>& copyFiles,
const List<fileAndContent>& filesContents
)
:
name_(name),
copyFiles_(copyFiles),
filesContents_(filesContents)
{}
Foam::codeStreamTools::codeStreamTools(const codeStreamTools& otf)
:
name_(otf.name_),
copyFiles_(otf.copyFiles_),
filesContents_(otf.filesContents_)
{}
// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
bool Foam::codeStreamTools::copyFilesContents(const fileName& dir) const
{
if (!allowSystemOperations)
{
FatalErrorIn
(
"codeStreamTools::copyFilesContents(const fileName&) const"
) << "Loading a shared library using case-supplied code is not"
<< " enabled by default" << endl
<< "because of security issues. If you trust the code you can"
<< " enable this" << endl
<< "facility be adding to the InfoSwitches setting in the system"
<< " controlDict" << endl
<< endl
<< " allowSystemOperations 1" << endl
<< endl
<< "The system controlDict is either" << endl
<< endl
<< " ~/.OpenFOAM/$WM_PROJECT_VERSION/controlDict" << endl
<< endl
<< "or" << endl
<< endl
<< " $WM_PROJECT_DIR/etc/controlDict" << endl
<< endl
<< exit(FatalError);
}
// Create dir
mkDir(dir);
//Info<< "Setting envvar OTF_TYPENAME=" << name_ << endl;
setEnv("OTF_TYPENAME", name_, true);
// Copy any template files
forAll(copyFiles_, i)
{
const List<Pair<string> >& rules = copyFiles_[i].second();
forAll(rules, j)
{
//Info<< "Setting envvar " << rules[j].first() << endl;
setEnv(rules[j].first(), rules[j].second(), true);
}
const fileName sourceFile = fileName(copyFiles_[i].first()).expand();
const fileName destFile = dir/sourceFile.name();
IFstream sourceStr(sourceFile);
//Info<< "Reading from " << sourceStr.name() << endl;
if (!sourceStr.good())
{
FatalErrorIn
(
"codeStreamTools::copyFilesContents()"
" const"
) << "Failed opening " << sourceFile << exit(FatalError);
}
OFstream destStr(destFile);
//Info<< "Writing to " << destFile.name() << endl;
if (!destStr.good())
{
FatalErrorIn
(
"codeStreamTools::copyFilesContents()"
" const"
) << "Failed writing " << destFile << exit(FatalError);
}
copyAndExpand(sourceStr, destStr);
}
// Files that are always written:
forAll(filesContents_, i)
{
fileName f = fileName(dir/filesContents_[i].first()).expand();
mkDir(f.path());
OFstream str(f);
//Info<< "Writing to " << filesContents_[i].first() << endl;
if (!str.good())
{
FatalErrorIn
(
"codeStreamTools::copyFilesContents()"
" const"
) << "Failed writing " << f << exit(FatalError);
}
str << filesContents_[i].second().c_str() << endl;
}
return true;
}
Foam::string Foam::codeStreamTools::stripLeading(const string& s)
{
label sz = s.size();
if (sz > 0 && s[0] == '\n')
{
return s(1, sz-1);
}
else
{
return s;
}
}
bool Foam::codeStreamTools::writeDigest
(
const fileName& dir,
const SHA1Digest& sha1
)
{
OFstream str(dir/"SHA1Digest");
str << sha1;
return str.good();
}
Foam::SHA1Digest Foam::codeStreamTools::readDigest(const fileName& dir)
{
IFstream str(dir/"SHA1Digest");
return SHA1Digest(str);
}
bool Foam::codeStreamTools::upToDate
(
const fileName& dir,
const SHA1Digest& sha1
)
{
if (!exists(dir/"SHA1Digest") || readDigest(dir) != sha1)
{
writeDigest(dir, sha1);
return false;
}
else
{
return true;
}
}
bool Foam::codeStreamTools::read(const dictionary& dict)
{
dict.lookup("copyFiles") >> copyFiles_;
dict.lookup("filesContents") >> filesContents_;
return true;
}
void Foam::codeStreamTools::writeDict(Ostream& os) const
{
os.writeKeyword("copyFiles") << copyFiles_ << token::END_STATEMENT << nl;
os.writeKeyword("filesContents") << filesContents_ << token::END_STATEMENT
<< nl;
}
// ************************************************************************* //

View File

@ -0,0 +1,144 @@
/*---------------------------------------------------------------------------*\
========= |
\\ / 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 2 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, write to the Free Software Foundation,
Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
Class
Foam::codeStreamTools
Description
Base for all things on-the-fly from dictionary
SourceFiles
codeStreamTools.C
\*---------------------------------------------------------------------------*/
#ifndef codeStreamTools_H
#define codeStreamTools_H
#include "Tuple2.H"
#include "Pair.H"
#include "SHA1Digest.H"
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
namespace Foam
{
typedef List<Pair<string> > stringPairList;
typedef Tuple2<fileName, List<Pair<string> > > fileAndVars;
typedef Tuple2<fileName, string> fileAndContent;
class OSstream;
class ISstream;
/*---------------------------------------------------------------------------*\
Class codeStreamTools Declaration
\*---------------------------------------------------------------------------*/
class codeStreamTools
{
// Private data
//- Name for underlying set
word name_;
//- Files to copy
List<fileAndVars> copyFiles_;
//- Direct contents for files
List<fileAndContent> filesContents_;
protected:
void copyAndExpand(ISstream&, OSstream&) const;
public:
static int allowSystemOperations;
// Constructors
//- Construct null
codeStreamTools();
//- Construct from dictionary
codeStreamTools(const word& name, const dictionary& dict);
//- Copy from components
codeStreamTools
(
const word& name,
const List<fileAndVars>&,
const List<fileAndContent>&
);
//- Construct copy
codeStreamTools(const codeStreamTools& otf);
// Member functions
const List<Tuple2<fileName, List<Pair<string> > > >& copyFiles() const
{
return copyFiles_;
}
const List<Tuple2<fileName, string> >& filesContents() const
{
return filesContents_;
}
const word& name() const
{
return name_;
}
bool copyFilesContents(const fileName& dir) const;
static void* findLibrary(const fileName& libPath);
static string stripLeading(const string&);
static bool writeDigest(const fileName& dir, const SHA1Digest& sha1);
static SHA1Digest readDigest(const fileName& dir);
static bool upToDate(const fileName& dir, const SHA1Digest& sha1);
bool read(const dictionary&);
void writeDict(Ostream&) const;
};
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
} // End namespace Foam
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
#endif
// ************************************************************************* //

View File

@ -142,7 +142,7 @@ Foam::string& Foam::string::expand(const bool recurse, const bool allowEmptyVar)
{
if (recurse)
{
enVarString.expand();
enVarString.expand(recurse, allowEmptyVar);
}
std::string::replace
(
@ -154,7 +154,12 @@ Foam::string& Foam::string::expand(const bool recurse, const bool allowEmptyVar)
}
else if (allowEmptyVar)
{
startEnvar = endEnvar;
std::string::replace
(
startEnvar,
endEnvar - startEnvar + 1,
""
);
}
else
{

View File

@ -114,6 +114,8 @@ derivedFvPatchFields = $(fvPatchFields)/derived
$(derivedFvPatchFields)/activeBaffleVelocity/activeBaffleVelocityFvPatchVectorField.C
$(derivedFvPatchFields)/advective/advectiveFvPatchFields.C
$(derivedFvPatchFields)/codedFixedValue/codedFixedValueFvPatchScalarField.C
$(derivedFvPatchFields)/codedFixedValue/codeProperties.C
$(derivedFvPatchFields)/directMappedFixedValue/directMappedFixedValueFvPatchFields.C
$(derivedFvPatchFields)/directMappedVelocityFluxFixedValue/directMappedVelocityFluxFixedValueFvPatchField.C
$(derivedFvPatchFields)/fan/fanFvPatchFields.C

View File

@ -0,0 +1,59 @@
/*---------------------------------------------------------------------------*\
========= |
\\ / 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 "codeProperties.H"
#include "Time.H"
// * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
defineTypeNameAndDebug(Foam::codeProperties, 0);
// * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
Foam::codeProperties::codeProperties(const IOobject& io)
:
IOdictionary(io),
modified_(true)
{}
// * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * * //
bool Foam::codeProperties::read()
{
if (regIOobject::read())
{
modified_ = true;
return true;
}
else
{
return false;
}
}
// ************************************************************************* //

View File

@ -0,0 +1,96 @@
/*---------------------------------------------------------------------------*\
========= |
\\ / 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::codeProperties
Description
IOdictionary + flag whether file has changed.
SourceFiles
codeProperties.C
\*---------------------------------------------------------------------------*/
#ifndef codeProperties_H
#define codeProperties_H
#include "MeshObject.H"
#include "IOdictionary.H"
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
namespace Foam
{
/*---------------------------------------------------------------------------*\
Class codeProperties Declaration
\*---------------------------------------------------------------------------*/
class codeProperties
:
public IOdictionary
{
// Private data
//- File change
mutable bool modified_;
public:
// Declare name of the class and its debug switch
ClassName("codeDict");
// Constructors
//- Construct from IOobject
codeProperties(const IOobject&);
// Member Functions
bool modified() const
{
return modified_;
}
void setUnmodified() const
{
modified_ = false;
}
//- Read the solution dictionary
virtual bool read();
};
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
} // End namespace Foam
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
#endif
// ************************************************************************* //

View File

@ -0,0 +1,419 @@
/*---------------------------------------------------------------------------*\
========= |
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration |
\\ / A nd | Copyright (C) 2010-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 2 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, write to the Free Software Foundation,
Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
\*---------------------------------------------------------------------------*/
#include "codedFixedValueFvPatchScalarField.H"
#include "addToRunTimeSelectionTable.H"
#include "fvPatchFieldMapper.H"
#include "surfaceFields.H"
#include "volFields.H"
#include "dlLibraryTable.H"
#include "IFstream.H"
#include "OFstream.H"
#include "codeStreamTools.H"
#include "codeProperties.H"
// * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * //
const Foam::codeProperties&
Foam::codedFixedValueFvPatchScalarField::dict() const
{
if (db().foundObject<codeProperties>(codeProperties::typeName))
{
return db().lookupObject<codeProperties>
(
codeProperties::typeName
);
}
else
{
codeProperties* props = new codeProperties
(
IOobject
(
codeProperties::typeName,
db().time().system(),
db(),
IOobject::MUST_READ_IF_MODIFIED,
IOobject::NO_WRITE
)
);
return db().store(props);
}
}
void Foam::codedFixedValueFvPatchScalarField::writeLibrary
(
const fileName dir,
const fileName libPath,
const dictionary& dict
)
{
Info<< "Creating new library in " << libPath << endl;
// Write files for new library
if (Pstream::master())
{
fileName templates(Foam::getEnv("OTF_TEMPLATE_DIR"));
if (!templates.size())
{
FatalIOErrorIn
(
"codedFixedValueFvPatchScalarField::writeLibrary(..)",
dict
) << "Please set environment variable OTF_TEMPLATE_DIR"
<< " to point to the location of "
<< "fixedValueFvPatchScalarFieldTemplate.C"
<< exit(FatalIOError);
}
// Extract sections of code
string codeInclude = "";
if (dict.found("codeInclude"))
{
codeInclude = codeStreamTools::stripLeading(dict["codeInclude"]);
}
string code = codeStreamTools::stripLeading(dict["code"]);
string codeOptions = "";
if (dict.found("codeOptions"))
{
codeOptions = codeStreamTools::stripLeading(dict["codeOptions"]);
}
List<fileAndVars> copyFiles(2);
copyFiles[0].first() =
templates/"fixedValueFvPatchScalarFieldTemplate.C";
copyFiles[0].second().setSize(2);
copyFiles[0].second()[0] = Pair<string>("OTF_INCLUDES", codeInclude);
copyFiles[0].second()[1] = Pair<string>("OTF_UPDATECOEFFS", code);
copyFiles[1].first() =
templates/"fixedValueFvPatchScalarFieldTemplate.H";
List<fileAndContent> filesContents(2);
// Write Make/files
filesContents[0].first() = "Make/files";
filesContents[0].second() =
"fixedValueFvPatchScalarFieldTemplate.C \n\n"
"LIB = $(FOAM_USER_LIBBIN)/lib" + redirectType_;
// Write Make/options
filesContents[1].first() = "Make/options";
filesContents[1].second() =
"EXE_INC = -g\\\n -I$(LIB_SRC)/finiteVolume/lnInclude\\\n"
+ codeOptions
+ "\n\nLIB_LIBS = ";
codeStreamTools writer(redirectType_, copyFiles, filesContents);
if (!writer.copyFilesContents(dir))
{
FatalIOErrorIn
(
"codedFixedValueFvPatchScalarField::writeLibrary(..)",
dict
) << "Failed writing " << endl
<< copyFiles << endl
<< filesContents
<< exit(FatalIOError);
}
}
}
void Foam::codedFixedValueFvPatchScalarField::updateLibrary()
{
if (isAdministrator())
{
FatalIOErrorIn
(
"codedFixedValueFvPatchScalarField::updateLibrary()",
dict_
) << "This code should not be executed by someone with administrator"
<< " rights due to security reasons." << endl
<< "(it writes a shared library which then gets loaded "
<< "using dlopen)"
<< exit(FatalIOError);
}
const fileName dir =
db().time().constantPath()/"codedFixedValue"/redirectType_;
//Info<< "dir:" << dir << endl;
const fileName libPath
(
Foam::getEnv("FOAM_USER_LIBBIN")
/ "lib"
+ redirectType_
+ ".so"
);
//Info<< "libPath:" << libPath << endl;
void* lib = dlLibraryTable::findLibrary(libPath);
if (dict_.found("code"))
{
if (!lib)
{
writeLibrary(dir, libPath, dict_);
}
}
else
{
const codeProperties& onTheFlyDict = dict();
if (onTheFlyDict.modified())
{
onTheFlyDict.setUnmodified();
// Remove instantiation of fvPatchField provided by library
redirectPatchFieldPtr_.clear();
// Unload library
if (lib)
{
if (!dlLibraryTable::close(libPath))
{
FatalIOErrorIn
(
"codedFixedValueFvPatchScalarField::updateLibrary(..)",
onTheFlyDict
) << "Failed unloading library " << libPath
<< exit(FatalIOError);
}
lib = NULL;
}
const dictionary& codeDict = onTheFlyDict.subDict(redirectType_);
writeLibrary(dir, libPath, codeDict);
}
}
if (!lib)
{
if (Pstream::master())
{
Foam::string wmakeCmd("wmake libso " + dir);
Info<< "Invoking " << wmakeCmd << endl;
if (Foam::system(wmakeCmd))
{
FatalIOErrorIn
(
"codedFixedValueFvPatchScalarField::updateLibrary()",
dict_
) << "Failed " << wmakeCmd << exit(FatalIOError);
}
}
bool dummy = true;
reduce(dummy, orOp<bool>());
if (!dlLibraryTable::open(libPath))
{
FatalIOErrorIn
(
"codedFixedValueFvPatchScalarField::updateLibrary()",
dict_
) << "Failed loading library " << libPath << exit(FatalIOError);
}
}
}
// * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
Foam::codedFixedValueFvPatchScalarField::
codedFixedValueFvPatchScalarField
(
const fvPatch& p,
const DimensionedField<scalar, volMesh>& iF
)
:
fixedValueFvPatchField<scalar>(p, iF),
redirectPatchFieldPtr_(NULL)
{}
Foam::codedFixedValueFvPatchScalarField::
codedFixedValueFvPatchScalarField
(
const codedFixedValueFvPatchScalarField& ptf,
const fvPatch& p,
const DimensionedField<scalar, volMesh>& iF,
const fvPatchFieldMapper& mapper
)
:
fixedValueFvPatchField<scalar>(ptf, p, iF, mapper),
dict_(ptf.dict_),
redirectType_(ptf.redirectType_),
redirectPatchFieldPtr_(NULL)
{}
Foam::codedFixedValueFvPatchScalarField::
codedFixedValueFvPatchScalarField
(
const fvPatch& p,
const DimensionedField<scalar, volMesh>& iF,
const dictionary& dict
)
:
fixedValueFvPatchField<scalar>(p, iF, dict),
dict_(dict),
redirectType_(dict.lookup("redirectType")),
redirectPatchFieldPtr_(NULL)
{
updateLibrary();
}
Foam::codedFixedValueFvPatchScalarField::
codedFixedValueFvPatchScalarField
(
const codedFixedValueFvPatchScalarField& ptf
)
:
fixedValueFvPatchField<scalar>(ptf),
dict_(ptf.dict_),
redirectType_(ptf.redirectType_),
redirectPatchFieldPtr_(NULL)
{}
Foam::codedFixedValueFvPatchScalarField::
codedFixedValueFvPatchScalarField
(
const codedFixedValueFvPatchScalarField& ptf,
const DimensionedField<scalar, volMesh>& iF
)
:
fixedValueFvPatchField<scalar>(ptf, iF),
dict_(ptf.dict_),
redirectType_(ptf.redirectType_),
redirectPatchFieldPtr_(NULL)
{}
// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
const Foam::fvPatchScalarField&
Foam::codedFixedValueFvPatchScalarField::redirectPatchField() const
{
if (!redirectPatchFieldPtr_.valid())
{
// Construct a patch
// Make sure to construct the patchfield with uptodate value.
OStringStream os;
os.writeKeyword("type") << redirectType_ << token::END_STATEMENT
<< nl;
static_cast<const scalarField&>(*this).writeEntry("value", os);
IStringStream is(os.str());
dictionary dict(is);
Info<< "constructing patchField from :" << dict << endl;
redirectPatchFieldPtr_.set
(
fvPatchScalarField::New
(
patch(),
dimensionedInternalField(),
dict
).ptr()
);
}
return redirectPatchFieldPtr_();
}
void Foam::codedFixedValueFvPatchScalarField::updateCoeffs()
{
if (updated())
{
return;
}
// Make sure library containing user-defined fvPatchField is uptodate
updateLibrary();
const fvPatchScalarField& fvp = redirectPatchField();
const_cast<fvPatchScalarField&>(fvp).updateCoeffs();
// Copy through value
operator==(fvp);
fixedValueFvPatchField<scalar>::updateCoeffs();
}
void Foam::codedFixedValueFvPatchScalarField::evaluate
(
const Pstream::commsTypes commsType
)
{
// Make sure library containing user-defined fvPatchField is uptodate
updateLibrary();
const fvPatchScalarField& fvp = redirectPatchField();
const_cast<fvPatchScalarField&>(fvp).evaluate(commsType);
fixedValueFvPatchField<scalar>::evaluate(commsType);
}
void Foam::codedFixedValueFvPatchScalarField::write(Ostream& os) const
{
//dict_.set("value", static_cast<const scalarField&>(*this));
//os << dict_ << token::END_STATEMENT << nl;
fixedValueFvPatchField<scalar>::write(os);
os.writeKeyword("redirectType") << redirectType_ << token::END_STATEMENT
<< nl;
if (dict_.found("code"))
{
os.writeKeyword("code") << string(dict_["code"]) << token::END_STATEMENT
<< nl;
}
}
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
namespace Foam
{
makePatchTypeField
(
fvPatchScalarField,
codedFixedValueFvPatchScalarField
);
}
// ************************************************************************* //

View File

@ -0,0 +1,221 @@
/*---------------------------------------------------------------------------*\
========= |
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration |
\\ / A nd | Copyright (C) 2010-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 2 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, write to the Free Software Foundation,
Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
Class
Foam::codedFixedValueFvPatchScalarField
Description
Constructs on-the-fly a new boundary condition (derived from
fixedValueFvPatchScalarField) which is then used to evaluate.
See also codeStream.
Example:
movingWall
{
type codedFixedValue;
value uniform 0;
redirectType rampedFixedValue; // name of generated bc
code
#{
operator==(min(10, 0.1*this->db().time().value()));
#};
//codeInclude
//#{
// #include "fvCFD.H"
//#};
//codeOptions
//#{
// -I$(LIB_SRC)/finiteVolume/lnInclude
//#};
}
A special form is if the 'code' section is not supplied. In this case
the code gets read from a (runTimeModifiable!) dictionary system/codeDict
which would have an entry
rampedFixedValue
{
code
#{
operator==(min(10, 0.1*this->db().time().value()));
#};
}
SourceFiles
codedFixedValueFvPatchScalarField.C
\*---------------------------------------------------------------------------*/
#ifndef codedFixedValueFvPatchScalarField_H
#define codedFixedValueFvPatchScalarField_H
#include "fixedValueFvPatchFields.H"
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
namespace Foam
{
class codeProperties;
/*---------------------------------------------------------------------------*\
Class codedFixedValueFvPatchScalarField Declaration
\*---------------------------------------------------------------------------*/
class codedFixedValueFvPatchScalarField
:
public fixedValueFvPatchScalarField
{
// Private data
mutable dictionary dict_;
const word redirectType_;
mutable autoPtr<fvPatchScalarField> redirectPatchFieldPtr_;
// Private Member Functions
const codeProperties& dict() const;
void writeLibrary
(
const fileName dir,
const fileName libPath,
const dictionary& dict
);
void updateLibrary();
public:
//- Runtime type information
TypeName("codedFixedValue");
// Constructors
//- Construct from patch and internal field
codedFixedValueFvPatchScalarField
(
const fvPatch&,
const DimensionedField<scalar, volMesh>&
);
//- Construct from patch, internal field and dictionary
codedFixedValueFvPatchScalarField
(
const fvPatch&,
const DimensionedField<scalar, volMesh>&,
const dictionary&
);
//- Construct by mapping given
// codedFixedValueFvPatchScalarField
// onto a new patch
codedFixedValueFvPatchScalarField
(
const codedFixedValueFvPatchScalarField&,
const fvPatch&,
const DimensionedField<scalar, volMesh>&,
const fvPatchFieldMapper&
);
//- Construct as copy
codedFixedValueFvPatchScalarField
(
const codedFixedValueFvPatchScalarField&
);
//- Construct and return a clone
virtual tmp<fvPatchScalarField> clone() const
{
return tmp<fvPatchScalarField>
(
new codedFixedValueFvPatchScalarField
(
*this
)
);
}
//- Construct as copy setting internal field reference
codedFixedValueFvPatchScalarField
(
const codedFixedValueFvPatchScalarField&,
const DimensionedField<scalar, volMesh>&
);
//- Construct and return a clone setting internal field reference
virtual tmp<fvPatchScalarField> clone
(
const DimensionedField<scalar, volMesh>& iF
) const
{
return tmp<fvPatchScalarField>
(
new codedFixedValueFvPatchScalarField
(
*this,
iF
)
);
}
// Member functions
//- Get reference to the underlying patch
const fvPatchScalarField& redirectPatchField() const;
//- Update the coefficients associated with the patch field
virtual void updateCoeffs();
//- Evaluate the patch field, sets Updated to false
virtual void evaluate
(
const Pstream::commsTypes commsType=Pstream::blocking
);
//- Write
virtual void write(Ostream& os) const;
};
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
} // End namespace Foam
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
#endif
// ************************************************************************* //