Commit 019fe7de authored by Mark Olesen's avatar Mark Olesen
Browse files

ENH: base fvMesh driver for expressions

parent 7e275838
......@@ -250,6 +250,15 @@ $(constraintFvsPatchFields)/wedge/wedgeFvsPatchFields.C
fields/volFields/volFields.C
fields/surfaceFields/surfaceFields.C
expr = expressions
$(expr)/base/exprDriverWriter.C
$(expr)/base/fvExprDriver.C
$(expr)/base/fvExprDriverFields.C
$(expr)/base/fvExprDriverIO.C
$(expr)/base/fvExprDriverNew.C
fvMatrices/fvMatrices.C
fvMatrices/fvScalarMatrix/fvScalarMatrix.C
fvMatrices/solvers/MULES/MULES.C
......
/*---------------------------------------------------------------------------*\
========= |
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration |
\\ / A nd | www.openfoam.com
\\/ M anipulation |
-------------------------------------------------------------------------------
Copyright (C) 2010-2018 Bernhard Gschaider <bgschaid@hfd-research.com>
Copyright (C) 2019 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
OpenFOAM is free software: you can redistribute it and/or modify it
under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
for more details.
You should have received a copy of the GNU General Public License
along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
\*---------------------------------------------------------------------------*/
#include "exprDriverWriter.H"
#include "fvExprDriver.H"
// * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
namespace Foam
{
namespace expressions
{
defineTypeName(exprDriverWriter);
} // namespace expressions
} // namespace Foam
// * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
Foam::expressions::exprDriverWriter::exprDriverWriter
(
const word& name,
fvExprDriver& driver
)
:
regIOobject
(
IOobject
(
name,
driver.mesh().time().timeName(),
"expressions",
driver.mesh().time(),
IOobject::READ_IF_PRESENT,
IOobject::AUTO_WRITE
)
),
driver_(driver)
{
if (headerOk())
{
readData(readStream("exprDriverWriter", true));
}
}
// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
bool Foam::expressions::exprDriverWriter::readData(Istream& is)
{
dictionary dict(is);
// driver_.readDict(is);
driver_.getData(dict);
return !is.bad();
}
bool Foam::expressions::exprDriverWriter::writeData(Ostream& os) const
{
// driver_.writeDict(os);
dictionary dict;
driver_.prepareData(dict);
dict.write(os, false);
return os.good();
}
// ************************************************************************* //
/*---------------------------------------------------------------------------*\
========= |
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration |
\\ / A nd | www.openfoam.com
\\/ M anipulation |
-------------------------------------------------------------------------------
Copyright (C) 2011-2018 Bernhard Gschaider <bgschaid@hfd-research.com>
Copyright (C) 2019 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
OpenFOAM is free software: you can redistribute it and/or modify it
under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
for more details.
You should have received a copy of the GNU General Public License
along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
Class
Foam::expressions::exprDriverWriter
Description
Registered input/output for an expressions::fvExprDriver
SourceFiles
exprDriverWriter.C
\*---------------------------------------------------------------------------*/
#ifndef expressions_exprDriverWriter_H
#define expressions_exprDriverWriter_H
#include "fvExprDriver.H"
#include "regIOobject.H"
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
namespace Foam
{
namespace expressions
{
/*---------------------------------------------------------------------------*\
Class exprDriverWriter Declaration
\*---------------------------------------------------------------------------*/
class exprDriverWriter
:
public regIOobject
{
// Private Data
//- The driver to read/write
fvExprDriver& driver_;
// Private Member Functions
//- No null constructor
exprDriverWriter() = delete;
//- No copy construct
exprDriverWriter(const exprDriverWriter&) = delete;
//- No copy assignment
void operator=(const exprDriverWriter&) = delete;
public:
//- Runtime type information
TypeNameNoDebug("exprDriverWriter");
// Constructors
//- Construct for named driver
exprDriverWriter(const word& name, fvExprDriver& driver);
//- Destructor
virtual ~exprDriverWriter() = default;
// Member Functions
virtual bool readData(Istream& is);
virtual bool writeData(Ostream& os) const;
};
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
} // End namespace expressions
} // End namespace Foam
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
#endif
// ************************************************************************* //
/*---------------------------------------------------------------------------*\
========= |
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration |
\\ / A nd | www.openfoam.com
\\/ M anipulation |
-------------------------------------------------------------------------------
Copyright (C) 2010-2018 Bernhard Gschaider <bgschaid@hfd-research.com>
Copyright (C) 2019 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
OpenFOAM is free software: you can redistribute it and/or modify it
under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
for more details.
You should have received a copy of the GNU General Public License
along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
\*---------------------------------------------------------------------------*/
#include "fvExprDriver.H"
#include "exprDriverWriter.H"
#include "expressionEntry.H"
#include "exprResultGlobals.H"
#include "cellSet.H"
#include "faceSet.H"
#include "pointSet.H"
#include "stringOps.H"
// * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
namespace Foam
{
namespace expressions
{
defineTypeNameAndDebug(fvExprDriver, 0);
defineRunTimeSelectionTable(fvExprDriver, dictionary);
defineRunTimeSelectionTable(fvExprDriver, idName);
} // End namespace expressions
} // End namespace Foam
// Currently not working?
bool Foam::expressions::fvExprDriver::cacheSets_ = true;
const Foam::fvMesh* Foam::expressions::fvExprDriver::defaultMeshPtr_ = nullptr;
// * * * * * * * * * * * * * Static Member Functions * * * * * * * * * * * * //
const Foam::fvMesh& Foam::expressions::fvExprDriver::defaultMesh()
{
if (!defaultMeshPtr_)
{
FatalErrorInFunction
<< "No default mesh set" << nl
<< "Try the 'fvExprDriverFunctionObject' as a workaround"
<< endl
<< abort(FatalError);
}
return *defaultMeshPtr_;
}
const Foam::fvMesh* Foam::expressions::fvExprDriver::resetDefaultMesh
(
const fvMesh& mesh,
const bool force
)
{
const fvMesh* ptr = defaultMeshPtr_;
if (force || (ptr != nullptr))
{
defaultMeshPtr_ = &mesh;
}
return ptr;
}
// * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
Foam::expressions::fvExprDriver::fvExprDriver
(
bool cacheReadFields,
bool searchInMemory,
bool searchOnDisc,
const dictionary& dict
)
:
expressions::exprDriver
(
cacheReadFields,
searchInMemory,
searchOnDisc,
dict
),
globalScopes_(),
delayedVariables_(),
storedVariables_(),
specialVariablesIndex_(-1),
otherMeshName_(),
libs_(),
writer_(nullptr)
{}
Foam::expressions::fvExprDriver::fvExprDriver
(
const fvExprDriver& rhs
)
:
expressions::exprDriver(rhs),
globalScopes_(rhs.globalScopes_),
delayedVariables_(rhs.delayedVariables_),
storedVariables_(rhs.storedVariables_),
specialVariablesIndex_(rhs.specialVariablesIndex_),
otherMeshName_(),
libs_(),
writer_(nullptr)
{}
Foam::expressions::fvExprDriver::fvExprDriver
(
const dictionary& dict
)
:
fvExprDriver
(
dict.lookupOrDefault("cacheReadFields", false),
dict.lookupOrDefault("searchInMemory", true),
dict.lookupOrDefault("searchOnDisc", false),
dict
)
{
readDict(dict);
}
// * * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * //
Foam::expressions::fvExprDriver::~fvExprDriver()
{}
// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
bool Foam::expressions::fvExprDriver::readDict
(
const dictionary& dict
)
{
expressions::exprDriver::readDict(dict);
// wordList plugins;
// if (dict.readIfPresent("functionPlugins", plugins))
// {
// for (const word& plugin : plugins)
// {
// libs_.open("libswak" + plugin + "FunctionPlugin.so");
// }
// }
dict.readIfPresent("globalScopes", globalScopes_);
const entry* eptr = nullptr;
// Special variables
if
(
// storedVariables
(eptr = dict.findEntry("storedVariables", keyType::LITERAL))
!= nullptr
)
{
ITstream& is = eptr->stream();
if (writer_.valid() && storedVariables_.size())
{
WarningInFunction
// << "Context: " << driverContext_ << nl
<< "The 'storedVariables' was already read."
<< " No update from " << is
<< endl;
}
else
{
storedVariables_ = List<exprResultStored>(is);
// Check for excess tokens
dict.checkITstream(is, "storedVariables");
}
}
if
(
// delayedVariables
(eptr = dict.findEntry("delayedVariables", keyType::LITERAL))
!= nullptr
)
{
ITstream& is = eptr->stream();
if (writer_.valid() && delayedVariables_.size())
{
WarningInFunction
// << "Context: " << driverContext_ << nl
<< "Seems like 'delayedVariables' was already read."
<< " No update from " << is
<< endl;
}
else
{
List<exprResultDelayed> inputs(is);
// Check for excess tokens
dict.checkITstream(is, "delayedVariables");
for (auto& var : inputs)
{
delayedVariables_.insert(var.name(), var);
}
}
}
return true;
}
// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
const Foam::Time& Foam::expressions::fvExprDriver::runTime() const
{
return this->mesh().time();
}
Foam::word Foam::expressions::fvExprDriver::timeName() const
{
return runTime().timeName();
}
Foam::scalar Foam::expressions::fvExprDriver::timeValue() const
{
return runTime().value();
}
void Foam::expressions::fvExprDriver::updateSpecialVariables(bool force)
{
const bool updated = this->update();
const label eventIndex = mesh().time().timeIndex();
const scalar eventTime = mesh().time().value();
DebugInfo
<< "fvExprDriver::updateSpecialVariables(force="
<< force << ") Updated: " << updated << endl;
if (specialVariablesIndex_ < 0)
{
DebugInfo
<< "First update: " << eventIndex << endl;
specialVariablesIndex_ = eventIndex;
for (exprResultStored& v : storedVariables_)
{
DebugInfo
<< v.name() << " = " << v.initialValueExpression()
<< " (has value "
<< v.hasValue() << ")" << endl;
if (!v.hasValue())
{
DebugInfo
<< "First value: " << v.initialValueExpression()
<< " -> " << v.name() << endl;
parse(v.initialValueExpression());
v = result_;
DebugInfo
<< "Parser size: " << this->size() << nl
<< "Calculated: " << result_ << nl
<< "Stored: " << v << nl;
}
}
}
if (force || specialVariablesIndex_ != eventIndex)
{
DebugInfo
<< "Store variables: " << force << ' '
<< specialVariablesIndex_ << ' '
<< eventIndex << endl;
for (exprResultStored& v : storedVariables_)
{
if (variables_.found(v.name()))
{
DebugInfo
<< "Storing variable: " << v.name() << " "
<< variables_[v.name()] << endl;
v = variables_[v.name()];
}
}
specialVariablesIndex_ = eventIndex;
}
forAllIters(delayedVariables_, iter)
{
DebugInfo
<< "Updating delayed variable " << iter().name() << endl;
if (!iter().updateReadValue(eventTime))
{
const exprString& expr = iter().startupValueExpression();
DebugInfo
<< "Evaluate: " << expr << endl;
parse(expr);
iter().setReadValue(result_);
DebugInfo
<< "Value " << iter() << nl
<< "Type " << iter().valueType() << "("
<< result_.valueType() << ")" << endl;
}
else
{
DebugInfo
<< iter().name() << " updated without problem" << endl;
}
}
}
void Foam::expressions::fvExprDriver::clearVariables()
{
DebugInfo
<< "Clearing variables" << endl;
const scalar eventTime = mesh().time().value();
(void)this->update();
updateSpecialVariables();
variables_.clear();
for (exprResultStored& v : storedVariables_)
{
variables_.insert(v.name(), v);
}
addVariables(variableStrings_, false);
forAllIters(delayedVariables_, iter)
{
iter().storeValue(eventTime);
}
}
void Foam::expressions::fvExprDriver::evaluateVariable
(
const word& varName,
const expressions::exprString& expr
)
{
const regIOobject* objPtr = mesh().findObject<regIOobject>(varName);
if (!allowShadowing_ && objPtr)
{
WarningInFunction
// << "Context: " << driverContext_ << nl