Commit 399c21d7 authored by Mark OLESEN's avatar Mark OLESEN
Browse files

ENH: adjustments for Function1/PatchFunction1

- additional debug information

- improve support for dictionary specification of constant, polynomial
  and table entries. These previously only worked properly for
  primitiveEntry, which causes confusion.

- extend table Function1 to include TableFile functionality.
  Simplifies switching and modifying content.
parent 0252b4d5
EXE_INC = \
-DFULLDEBUG -g -O0 \
-I$(LIB_SRC)/lagrangian/intermediate/lnInclude \
-I$(LIB_SRC)/thermophysicalModels/radiation/lnInclude \
-I$(LIB_SRC)/thermophysicalModels/thermophysicalFunctions/lnInclude \
-I$(LIB_SRC)/finiteVolume/lnInclude \
-DFULLDEBUG -g \
-I$(LIB_SRC)/meshTools/lnInclude
EXE_LIBS = \
-llagrangianIntermediate \
-lradiationModels \
-lregionModels \
-lfiniteVolume \
-lmeshTools \
-lsampling
-lmeshTools
......@@ -32,7 +32,8 @@ Description
\*---------------------------------------------------------------------------*/
#include "fvCFD.H"
#include "argList.H"
#include "IOstreams.H"
#include "Function1.H"
#include "scalarIndList.H"
#include "scalarField.H"
......@@ -40,6 +41,8 @@ Description
#include "linearInterpolationWeights.H"
#include "splineInterpolationWeights.H"
using namespace Foam;
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
int main(int argc, char *argv[])
......
......@@ -26,11 +26,44 @@ x
);
function1 table
constant1 constant 100;
table1 table
(
(0 0)(10 1)
);
table2
{
type table;
values
(
(0 0)(10 1)
);
}
table3
{
type table;
file "<constant>/table-values";
}
poly1 polynomial
(
(0 1)
(1 1)
);
poly2
{
type polynomial;
coeffs
(
(0 1)
(1 1)
);
}
function2
{
type expression;
......
// -*- C++ -*-
// Some table values
(
(0 0)
(20 1)
);
// ************************************************************************* //
......@@ -53,8 +53,8 @@ SourceFiles
\*---------------------------------------------------------------------------*/
#ifndef CSV_H
#define CSV_H
#ifndef Function1Types_CSV_H
#define Function1Types_CSV_H
#include "Function1.H"
#include "TableBase.H"
......
......@@ -52,9 +52,29 @@ Foam::Function1Types::Constant<Type>::Constant
Function1<Type>(entryName, dict),
value_(Zero)
{
ITstream& is = dict.lookup(entryName);
word entryType(is);
is >> value_;
const entry* eptr = dict.findEntry(entryName, keyType::LITERAL);
if (eptr && eptr->isStream())
{
// Primitive (inline) format. Eg,
// - key constant 1.2;
// - key 1.2;
ITstream& is = eptr->stream();
if (is.peek().isWord())
{
is.skip(); // Discard leading 'constant'
}
is >> value_;
dict.checkITstream(is, entryName);
}
else
{
// Dictionary format. Eg,
// key { type constant; value 1.2; }
dict.readEntry("value", value_);
}
}
......@@ -71,10 +91,10 @@ Foam::Function1Types::Constant<Type>::Constant
template<class Type>
Foam::Function1Types::Constant<Type>::Constant(const Constant<Type>& cnst)
Foam::Function1Types::Constant<Type>::Constant(const Constant<Type>& rhs)
:
Function1<Type>(cnst),
value_(cnst.value_)
Function1<Type>(rhs),
value_(rhs.value_)
{}
......
......@@ -30,18 +30,31 @@ Class
Description
Templated function that returns a constant value.
Usage - for entry \<entryName\> returning the value <value>:
Usage - for entry \<entryName\> returning the value \<value\>,
can be specified is different formats.
Inline specification:
\verbatim
<entryName> constant <value>
\endverbatim
Dictionary format:
\verbatim
<entryName> constant <value>
<entryName>
{
type constant;
value <value>;
}
\endverbatim
SourceFiles
Constant.C
ConstantI.H
\*---------------------------------------------------------------------------*/
#ifndef Constant_H
#define Constant_H
#ifndef Function1Types_Constant_H
#define Function1Types_Constant_H
#include "Function1.H"
......@@ -93,7 +106,7 @@ public:
Constant(const word& entryName, Istream& is);
//- Copy constructor
explicit Constant(const Constant<Type>& cnst);
explicit Constant(const Constant<Type>& rhs);
//- Construct and return a clone
virtual tmp<Function1<Type>> clone() const
......@@ -117,7 +130,7 @@ public:
//- Return value as a function of (scalar) independent variable
virtual tmp<Field<Type>> value(const scalarField& x) const;
//- Write in dictionary format
//- Write as primitive (inline) format
virtual void writeData(Ostream& os) const;
};
......
......@@ -96,8 +96,8 @@ SourceFiles
\*---------------------------------------------------------------------------*/
#ifndef function1Types_Cosine_H
#define function1Types_Cosine_H
#ifndef Function1Types_Cosine_H
#define Function1Types_Cosine_H
#include "Sine.H"
......
......@@ -143,7 +143,7 @@ public:
//- Construct from entry name
explicit Function1(const word& entryName);
//- Construct from entry name and dictionary
//- Construct from entry name and dictionary (unused)
Function1(const word& entryName, const dictionary& dict);
//- Copy construct
......
......@@ -6,7 +6,7 @@
\\/ M anipulation |
-------------------------------------------------------------------------------
Copyright (C) 2011-2017 OpenFOAM Foundation
Copyright (C) 2018-2020 OpenCFD Ltd.
Copyright (C) 2018-2021 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
......@@ -49,6 +49,10 @@ Foam::Function1<Type>::New
{
// Dictionary entry
DebugInFunction
<< "For " << entryName << " with dictionary entries: "
<< flatOutput(coeffs->toc()) << nl;
coeffs->readEntry
(
"type",
......@@ -62,17 +66,21 @@ Foam::Function1<Type>::New
else if (eptr)
{
// Primitive entry
// - non-word : value for constant function
// - word : the modelType
// - non-word : value for constant function
ITstream& is = eptr->stream();
DebugInFunction
<< "For " << entryName << " with primitive entry" << nl;
token firstToken(is);
ITstream& is = eptr->stream();
if (!firstToken.isWord())
if (is.peek().isWord())
{
// A value
is.putBack(firstToken);
modelType = is.peek().wordToken();
}
else
{
// A value - compatibility for reading constant
const Type constValue = pTraits<Type>(is);
......@@ -81,10 +89,6 @@ Foam::Function1<Type>::New
new Function1Types::Constant<Type>(entryName, constValue)
);
}
else
{
modelType = firstToken.wordToken();
}
// Fallthrough
}
......
......@@ -81,7 +81,7 @@ public:
//- Construct from entry name
explicit function1Base(const word& entryName);
//- Construct from entry name and dictionary
//- Construct from entry name and dictionary (unused)
function1Base(const word& entryName, const dictionary& dict);
//- Copy construct
......
......@@ -5,7 +5,7 @@
\\ / A nd | www.openfoam.com
\\/ M anipulation |
-------------------------------------------------------------------------------
Copyright (C) 2020 OpenCFD Ltd.
Copyright (C) 2020-2021 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
......@@ -31,13 +31,13 @@ Description
Example usage for limiting a polynomial:
\verbatim
limitedPolyTest limitRange;
limitedPolyTestCoeffs
<entryName>
{
min 0.4;
max 1.4;
type limitRange;
min 0.4;
max 1.4;
value polynomial
value polynomial
(
(5 1)
(-2 2)
......@@ -53,16 +53,19 @@ Description
- poly(x) for 0.4 < x < 1.4.
Example usage for limiting a table
Example usage for limiting a file-based table:
\verbatim
limitedTableFileTest limitRange;
limitedTableFileTestCoeffs
<entryName>
{
min 0.4;
max 1.4;
value tableFile;
file "<system>/fanCurve.txt";
type limitRange;
min 0.4;
max 1.4;
value
{
type table;
file "<system>/fanCurve.txt";
}
}
\endverbatim
......@@ -79,8 +82,8 @@ SourceFiles
\*---------------------------------------------------------------------------*/
#ifndef LimitRange_H
#define LimitRange_H
#ifndef Function1Types_LimitRange_H
#define Function1Types_LimitRange_H
#include "Function1.H"
......
......@@ -40,8 +40,8 @@ SourceFiles
\*---------------------------------------------------------------------------*/
#ifndef OneConstant_H
#define OneConstant_H
#ifndef Function1Types_OneConstant_H
#define Function1Types_OneConstant_H
#include "Function1.H"
......@@ -110,7 +110,7 @@ public:
const scalarField& x2
) const;
//- Write in dictionary format
//- Write as primitive (inline) format
virtual void writeData(Ostream& os) const;
};
......
......@@ -6,7 +6,7 @@
\\/ M anipulation |
-------------------------------------------------------------------------------
Copyright (C) 2011-2016 OpenFOAM Foundation
Copyright (C) 2020 OpenCFD Ltd.
Copyright (C) 2020-2021 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
......@@ -28,25 +28,12 @@ License
#include "PolynomialEntry.H"
// * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
// * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * //
template<class Type>
Foam::Function1Types::Polynomial<Type>::Polynomial
(
const word& entryName,
const dictionary& dict
)
:
Function1<Type>(entryName, dict),
coeffs_(),
canIntegrate_(true)
void Foam::Function1Types::Polynomial<Type>::checkCoefficients()
{
ITstream& is = dict.lookup(entryName);
const word entryType(is);
is >> coeffs_;
if (!coeffs_.size())
if (coeffs_.empty())
{
FatalErrorInFunction
<< "Invalid (empty) polynomial coefficients for "
......@@ -54,64 +41,77 @@ Foam::Function1Types::Polynomial<Type>::Polynomial
<< exit(FatalError);
}
forAll(coeffs_, i)
for (const auto& coeff : coeffs_)
{
if (mag(coeffs_[i].second() + pTraits<Type>::one) < ROOTVSMALL)
if (mag(coeff.second() + pTraits<Type>::one) < ROOTVSMALL)
{
canIntegrate_ = false;
break;
}
}
if (debug)
if (debug && !canIntegrate_)
{
if (!canIntegrate_)
{
WarningInFunction
<< "Polynomial " << this->name() << " cannot be integrated"
<< endl;
}
WarningInFunction
<< "Polynomial " << this->name() << " cannot be integrated"
<< endl;
}
}
// * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
template<class Type>
Foam::Function1Types::Polynomial<Type>::Polynomial
(
const word& entryName,
const List<Tuple2<Type, Type>>& coeffs
const dictionary& dict
)
:
Function1<Type>(entryName),
coeffs_(coeffs),
Function1<Type>(entryName, dict),
coeffs_(),
canIntegrate_(true)
{
if (!coeffs_.size())
{
FatalErrorInFunction
<< "Invalid (empty) polynomial coefficients for "
<< this->name() << nl
<< exit(FatalError);
}
const entry* eptr = dict.findEntry(entryName, keyType::LITERAL);
forAll(coeffs_, i)
if (eptr && eptr->isStream())
{
if (mag(coeffs_[i].second() + 1) < ROOTVSMALL)
// Primitive (inline) format. Eg,
// key polynomial ((0 0) (10 1));
ITstream& is = eptr->stream();
if (is.peek().isWord())
{
canIntegrate_ = false;
break;
is.skip(); // Discard leading 'polynomial'
}
is >> this->coeffs_;
dict.checkITstream(is, entryName);
}
if (debug)
else
{
if (!canIntegrate_)
{
WarningInFunction
<< "Polynomial " << this->name() << " cannot be integrated"
<< endl;
}
// Dictionary format - "values" lookup. Eg,
//
// key { type polynomial; coeffs ((0 0) (10 1)); }
dict.readEntry("coeffs", this->coeffs_);
}
this->checkCoefficients();
}
template<class Type>
Foam::Function1Types::Polynomial<Type>::Polynomial
(
const word& entryName,
const List<Tuple2<Type, Type>>& coeffs
)
:
Function1<Type>(entryName),
coeffs_(coeffs),
canIntegrate_(true)
{
this->checkCoefficients();
}
......
......@@ -6,6 +6,7 @@
\\/ M anipulation |
-------------------------------------------------------------------------------
Copyright (C) 2011-2017 OpenFOAM Foundation
Copyright (C) 2021 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
......@@ -31,21 +32,35 @@ Description
list of Tuple2's. Data is input in the form,
e.g. for an entry \<entryName\> that describes y = x^2 + 2x^3
Inline specification:
\verbatim
<entryName> polynomial
(
(1 2)
(2 3)
(1 2)
(2 3)
);
\endverbatim
Dictionary format:
\verbatim
<entryName>
{
type polynomial;
coeffs
(
(1 2)
(2 3)
);
}
\endverbatim
SourceFiles
PolynomialEntry.C
\*---------------------------------------------------------------------------*/
#ifndef PolynomialEntry_H
#define PolynomialEntry_H
#ifndef Function1Types_Polynomial_H
#define Function1Types_Polynomial_H
#include "Function1.H"
#include "Tuple2.H"
......@@ -72,12 +87,15 @@ class Polynomial
//- Polynomial coefficients - list of prefactor, exponent
List<Tuple2<Type, Type>> coeffs_;
//- Flag to indicate whether poly can be integrated
//- Flag to indicate whether polynomial can be integrated
bool canIntegrate_;
// Private Member Functions
//- Check coefficients and if polynomial can be integrated
void checkCoefficients();
//- No copy assignment
void operator=(const Polynomial<Type>&) = delete;
......@@ -125,8 +143,7 @@ public:
//- Integrate between two (scalar) values
virtual Type integrate(const scalar x1, const scalar x2) const;
//- Write in dictionary format
//- Write as primitive (inline) format
virtual void writeData(Ostream& os) const;
};
......