ENH: refactor codeStreamTools, use for codedFixedValueFvPatchScalarField

This commit is contained in:
Mark Olesen
2011-02-22 15:01:20 +01:00
parent 4aa279d7bc
commit f0f2bfa632
9 changed files with 210 additions and 133 deletions

View File

@ -49,7 +49,7 @@ Example: Look up dictionary entries and do some calculation
code, codeInclude, codeOptions sections (these are just strings) and code, codeInclude, codeOptions sections (these are just strings) and
calculates the SHA1 checksum of the contents. calculates the SHA1 checksum of the contents.
- it copies a template file - it copies a template file
($FOAM_CODESTREAM_TEMPLATE_DIR/codeStreamTemplate.C), substituting all ($FOAM_CODESTREAM_TEMPLATES/codeStreamTemplate.C), substituting all
occurences of code, codeInclude, codeOptions. occurences of code, codeInclude, codeOptions.
- it writes library source files to constant/codeStream/<sha1> and compiles it - it writes library source files to constant/codeStream/<sha1> and compiles it
using 'wmake libso'. using 'wmake libso'.

View File

@ -102,7 +102,7 @@ Foam::string Foam::getEnv(const word& envName)
bool Foam::setEnv bool Foam::setEnv
( (
const word& envName, const word& envName,
const string& value, const std::string& value,
const bool overwrite const bool overwrite
) )
{ {
@ -1066,7 +1066,7 @@ bool Foam::ping(const word& hostname, const label timeOut)
} }
int Foam::system(const string& command) int Foam::system(const std::string& command)
{ {
return ::system(command.c_str()); return ::system(command.c_str());
} }
@ -1084,14 +1084,14 @@ bool Foam::dlClose(void* handle)
} }
void* Foam::dlSym(void* handle, const string& symbol) void* Foam::dlSym(void* handle, const std::string& symbol)
{ {
void* fun = dlsym(handle, symbol.c_str()); void* fun = dlsym(handle, symbol.c_str());
char *error; char *error;
if ((error = dlerror()) != NULL) if ((error = dlerror()) != NULL)
{ {
WarningIn("dlSym(void*, const string&)") WarningIn("dlSym(void*, const std::string&)")
<< "Cannot lookup symbol " << symbol << " : " << error << "Cannot lookup symbol " << symbol << " : " << error
<< endl; << endl;
} }

View File

@ -57,15 +57,9 @@ namespace functionEntries
} }
const Foam::word Foam::functionEntries::codeStream::codeTemplateName const Foam::word Foam::functionEntries::codeStream::codeTemplateC
= "codeStreamTemplate.C"; = "codeStreamTemplate.C";
const Foam::word Foam::functionEntries::codeStream::codeTemplateEnvName
= "FOAM_CODESTREAM_TEMPLATES";
const Foam::fileName Foam::functionEntries::codeStream::codeTemplateDirName
= "codeTemplates/codeStream";
// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * // // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
@ -148,49 +142,30 @@ bool Foam::functionEntries::codeStream::execute
{ {
Info<< "Creating new library in " << libPath << endl; Info<< "Creating new library in " << libPath << endl;
fileName srcFile; const fileName fileCsrc
// try to get template from FOAM_CODESTREAM_TEMPLATES
fileName templateDir
( (
Foam::getEnv(codeTemplateEnvName) codeStreamTools::findTemplate
(
codeTemplateC
)
); );
if (!templateDir.empty()) // not found!
{ if (fileCsrc.empty())
srcFile = templateDir/codeTemplateName;
if (!isFile(srcFile, false))
{
srcFile.clear();
}
}
// not found - fallback to ~OpenFOAM expansion
if (srcFile.empty())
{
srcFile = findEtcFile
(
codeTemplateDirName/codeTemplateName
);
}
if (srcFile.empty())
{ {
FatalIOErrorIn FatalIOErrorIn
( (
"functionEntries::codeStream::execute(..)", "functionEntries::codeStream::execute(..)",
parentDict parentDict
) << "Could not find the code template: " ) << "Could not find the code template: "
<< codeTemplateName << nl << codeTemplateC << nl
<< "Under the $FOAM_CODESTREAM_TEMPLATES directory" << codeStreamTools::searchedLocations()
<< " via via the ~OpenFOAM/" / codeTemplateDirName
<< " expansion"
<< exit(FatalIOError); << exit(FatalIOError);
} }
List<codeStreamTools::fileAndVars> copyFiles(1); List<codeStreamTools::fileAndVars> copyFiles(1);
copyFiles[0].file() = srcFile; copyFiles[0].file() = fileCsrc;
copyFiles[0].set("codeInclude", codeInclude); copyFiles[0].set("codeInclude", codeInclude);
copyFiles[0].set("code", code); copyFiles[0].set("code", code);
@ -199,7 +174,7 @@ bool Foam::functionEntries::codeStream::execute
// Write Make/files // Write Make/files
filesContents[0].first() = "Make/files"; filesContents[0].first() = "Make/files";
filesContents[0].second() = filesContents[0].second() =
codeTemplateName + "\n" codeTemplateC + "\n\n"
"LIB = $(FOAM_USER_LIBBIN)/lib" + name; "LIB = $(FOAM_USER_LIBBIN)/lib" + name;
// Write Make/options // Write Make/options
@ -214,7 +189,7 @@ bool Foam::functionEntries::codeStream::execute
( (
"functionEntries::codeStream::execute(..)", "functionEntries::codeStream::execute(..)",
parentDict parentDict
) << "Failed writing " << endl ) << "Failed writing " <<nl
<< copyFiles << endl << copyFiles << endl
<< filesContents << filesContents
<< exit(FatalIOError); << exit(FatalIOError);

View File

@ -114,16 +114,8 @@ public:
// Static data members // Static data members
//- Name of the code template to be used //- Name of the C code template to be used
const static word codeTemplateName; const static word codeTemplateC;
//- Name of the code template environment variable
// Used to located the codeTemplateName
const static word codeTemplateEnvName;
//- Name of the code template sub-directory
// Used when locating the codeTemplateName via Foam::findEtcFile
const static fileName codeTemplateDirName;
//- Runtime type information //- Runtime type information

View File

@ -40,6 +40,53 @@ int Foam::codeStreamTools::allowSystemOperations
); );
const Foam::word Foam::codeStreamTools::codeTemplateEnvName
= "FOAM_CODESTREAM_TEMPLATES";
const Foam::fileName Foam::codeStreamTools::codeTemplateDirName
= "codeTemplates/codeStream";
// * * * * * * * * * * * * * Static Member Functions * * * * * * * * * * * * //
Foam::fileName Foam::codeStreamTools::findTemplate(const word& templateFile)
{
// try to get template from FOAM_CODESTREAM_TEMPLATES
fileName templateDir(Foam::getEnv(codeTemplateEnvName));
fileName file;
if (!templateDir.empty() && isDir(templateDir))
{
file = templateDir/templateFile;
if (!isFile(file, false))
{
file.clear();
}
}
// not found - fallback to ~OpenFOAM expansion
if (file.empty())
{
file = findEtcFile(codeTemplateDirName/templateFile);
}
return file;
}
Foam::string Foam::codeStreamTools::searchedLocations()
{
return
(
"Under the $"
+ codeTemplateDirName
+ " directory or via via the ~OpenFOAM/"
+ codeTemplateDirName
+ " expansion"
);
}
// * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * // // * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * //
void Foam::codeStreamTools::copyAndExpand void Foam::codeStreamTools::copyAndExpand

View File

@ -109,7 +109,18 @@ protected:
public: public:
static int allowSystemOperations; // Static data members
//- Name of the code template environment variable
// Used to located the codeTemplateName
const static word codeTemplateEnvName;
//- Name of the code template sub-directory
// Used when locating the codeTemplateName via Foam::findEtcFile
const static fileName codeTemplateDirName;
static int allowSystemOperations;
// Constructors // Constructors
@ -150,6 +161,15 @@ public:
bool copyFilesContents(const fileName& dir) const; bool copyFilesContents(const fileName& dir) const;
//- Find a code-template via the codeTemplateEnvName
// alternatively in the codeTemplateDirName via Foam::findEtcFile
static fileName findTemplate(const word& templateName);
//- List searched locations in a format suitable for display an error
static string searchedLocations();
static void* findLibrary(const fileName& libPath); static void* findLibrary(const fileName& libPath);
static bool writeDigest(const fileName& dir, const SHA1Digest& sha1); static bool writeDigest(const fileName& dir, const SHA1Digest& sha1);

View File

@ -65,7 +65,7 @@ bool env(const word&);
string getEnv(const word&); string getEnv(const word&);
//- Set an environment variable //- Set an environment variable
bool setEnv(const word& name, const string& value, const bool overwrite); bool setEnv(const word& name, const std::string& value, const bool overwrite);
//- Return the system's host name, as per hostname(1) //- Return the system's host name, as per hostname(1)
// Optionally with the full name (as per the '-f' option) // Optionally with the full name (as per the '-f' option)
@ -182,7 +182,7 @@ bool ping(const word&, const label port, const label timeOut);
bool ping(const word&, const label timeOut=10); bool ping(const word&, const label timeOut=10);
//- Execute the specified command //- Execute the specified command
int system(const string& command); int system(const std::string& command);
//- open a shared library. Return handle to library //- open a shared library. Return handle to library
void* dlOpen(const fileName& lib); void* dlOpen(const fileName& lib);
@ -191,7 +191,7 @@ void* dlOpen(const fileName& lib);
bool dlClose(void*); bool dlClose(void*);
//- Lookup a symbol in a dlopened library using handle //- Lookup a symbol in a dlopened library using handle
void* dlSym(void* handle, const string& symbol); void* dlSym(void* handle, const std::string& symbol);
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //

View File

@ -34,6 +34,16 @@ License
#include "OFstream.H" #include "OFstream.H"
#include "codeStreamTools.H" #include "codeStreamTools.H"
#include "codeProperties.H" #include "codeProperties.H"
#include "stringOps.H"
// * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
const Foam::word Foam::codedFixedValueFvPatchScalarField::codeTemplateC
= "fixedValueFvPatchScalarFieldTemplate.C";
const Foam::word Foam::codedFixedValueFvPatchScalarField::codeTemplateH
= "fixedValueFvPatchScalarFieldTemplate.H";
// * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * // // * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * //
@ -68,84 +78,101 @@ Foam::codedFixedValueFvPatchScalarField::dict() const
void Foam::codedFixedValueFvPatchScalarField::writeLibrary void Foam::codedFixedValueFvPatchScalarField::writeLibrary
( (
const fileName dir, const fileName& dir,
const fileName libPath, const fileName& libPath,
const dictionary& dict const dictionary& dict
) )
{ {
// Write files for new library
if (!Pstream::master())
{
return;
}
// "codeInclude" is optional
string codeInclude;
if (dict.found("codeInclude"))
{
codeInclude = stringOps::trimLeft(dict["codeInclude"]);
}
// "codeOptions" is optional
string codeOptions;
if (dict.found("codeOptions"))
{
codeOptions = stringOps::trimLeft(dict["codeOptions"]);
}
// "code" is mandatory
string code = stringOps::trimLeft(dict["code"]);
Info<< "Creating new library in " << libPath << endl; Info<< "Creating new library in " << libPath << endl;
// Write files for new library const fileName fileCsrc
if (Pstream::master()) (
codeStreamTools::findTemplate
(
codeTemplateC
)
);
const fileName fileHsrc
(
codeStreamTools::findTemplate
(
codeTemplateH
)
);
if (fileCsrc.empty() || fileHsrc.empty())
{ {
fileName templates(Foam::getEnv("FOAM_CODESTREAM_TEMPLATE_DIR")); FatalIOErrorIn
if (!templates.size()) (
{ "codedFixedValueFvPatchScalarField::writeLibrary(..)",
FatalIOErrorIn dict
( ) << "Could not find one or both code templates: "
"codedFixedValueFvPatchScalarField::writeLibrary(..)", << codeTemplateC << ", " << codeTemplateH << nl
dict << codeStreamTools::searchedLocations()
) << "Please set environment variable" << exit(FatalIOError);
<< " FOAM_CODESTREAM_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>("codeInclude", codeInclude);
copyFiles[0].second()[1] = Pair<string>("code", code);
copyFiles[1].first() =
templates/"fixedValueFvPatchScalarFieldTemplate.H";
List<fileAndContent> filesContents(2); List<codeStreamTools::fileAndVars> copyFiles(2);
// Write Make/files copyFiles[0].file() = fileCsrc;
filesContents[0].first() = "Make/files"; copyFiles[0].set("codeInclude", codeInclude);
filesContents[0].second() = copyFiles[0].set("code", code);
"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); copyFiles[1].file() = fileHsrc;
if (!writer.copyFilesContents(dir))
{
FatalIOErrorIn List<codeStreamTools::fileAndContent> filesContents(2);
(
"codedFixedValueFvPatchScalarField::writeLibrary(..)", // Write Make/files
dict filesContents[0].first() = "Make/files";
) << "Failed writing " << endl filesContents[0].second() =
<< copyFiles << endl codeTemplateC + "\n\n"
<< filesContents "LIB = $(FOAM_USER_LIBBIN)/lib" + redirectType_;
<< exit(FatalIOError);
} // 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 " << nl
<< copyFiles << nl
<< filesContents
<< exit(FatalIOError);
} }
} }
@ -221,7 +248,7 @@ void Foam::codedFixedValueFvPatchScalarField::updateLibrary()
{ {
if (Pstream::master()) if (Pstream::master())
{ {
Foam::string wmakeCmd("wmake libso " + dir); const Foam::string wmakeCmd("wmake libso " + dir);
Info<< "Invoking " << wmakeCmd << endl; Info<< "Invoking " << wmakeCmd << endl;
if (Foam::system(wmakeCmd)) if (Foam::system(wmakeCmd))
{ {
@ -229,7 +256,8 @@ void Foam::codedFixedValueFvPatchScalarField::updateLibrary()
( (
"codedFixedValueFvPatchScalarField::updateLibrary()", "codedFixedValueFvPatchScalarField::updateLibrary()",
dict_ dict_
) << "Failed " << wmakeCmd << exit(FatalIOError); ) << "Failed " << wmakeCmd
<< exit(FatalIOError);
} }
} }
@ -242,7 +270,8 @@ void Foam::codedFixedValueFvPatchScalarField::updateLibrary()
( (
"codedFixedValueFvPatchScalarField::updateLibrary()", "codedFixedValueFvPatchScalarField::updateLibrary()",
dict_ dict_
) << "Failed loading library " << libPath << exit(FatalIOError); ) << "Failed loading library " << libPath
<< exit(FatalIOError);
} }
} }
} }

View File

@ -32,7 +32,7 @@ Description
See also codeStream. See also codeStream.
Example: Example:
\verbatim
movingWall movingWall
{ {
type codedFixedValue<scalar>; type codedFixedValue<scalar>;
@ -53,13 +53,14 @@ Description
//#{ //#{
// -I$(LIB_SRC)/finiteVolume/lnInclude // -I$(LIB_SRC)/finiteVolume/lnInclude
//#}; //#};
} }
\endverbatim
A special form is if the 'code' section is not supplied. In this case A special form is if the 'code' section is not supplied. In this case
the code gets read from a (runTimeModifiable!) dictionary system/codeDict the code gets read from a (runTimeModifiable!) dictionary system/codeDict
which would have an entry which would have an entry
\verbatim
rampedFixedValue rampedFixedValue
{ {
code code
@ -67,6 +68,10 @@ Description
operator==(min(10, 0.1*this->db().time().value())); operator==(min(10, 0.1*this->db().time().value()));
#}; #};
} }
\endverbatim
SeeAlso
Foam::codeStreamTools for constant paths used
SourceFiles SourceFiles
codedFixedValueFvPatchScalarField.C codedFixedValueFvPatchScalarField.C
@ -86,7 +91,7 @@ namespace Foam
class codeProperties; class codeProperties;
/*---------------------------------------------------------------------------*\ /*---------------------------------------------------------------------------*\
Class codedFixedValueFvPatchScalarField Declaration Class codedFixedValueFvPatchScalarField Declaration
\*---------------------------------------------------------------------------*/ \*---------------------------------------------------------------------------*/
class codedFixedValueFvPatchScalarField class codedFixedValueFvPatchScalarField
@ -108,8 +113,8 @@ class codedFixedValueFvPatchScalarField
void writeLibrary void writeLibrary
( (
const fileName dir, const fileName& dir,
const fileName libPath, const fileName& libPath,
const dictionary& dict const dictionary& dict
); );
@ -117,6 +122,15 @@ class codedFixedValueFvPatchScalarField
public: public:
// Static data members
//- Name of the C code template to be used
const static word codeTemplateC;
//- Name of the H code template to be used
const static word codeTemplateH;
//- Runtime type information //- Runtime type information
TypeName("codedFixedValue<scalar>"); TypeName("codedFixedValue<scalar>");