DimensionedFunction1: Permit setting argument-dimensions of inline functions
Both argument and value dimensions can be set for inline-constructable
function1-s. For example, this inline table function:
massFlowRate table [CAD] [g/s]
(
(0 0.46)
(0.17 1.9)
(0.31 4.8)
);
Is equivalent to this dictionary specification:
massFlowRate
{
type table;
xDimensions [CAD];
dimensions [g/s];
values
(
(0 0.46)
(0.17 1.9)
(0.31 4.8)
);
}
In the inline form, the argument/x-dimensions come first, and the value
dimensions second. If there only one dimension set provided, then this
is assumed to be the value dimensions.
This commit is contained in:
@ -203,11 +203,12 @@ DimensionSets
|
||||
// Scaled units. Supported in dimensionedType and
|
||||
// UniformDimensionedField. Not supported in DimensionedField or
|
||||
// GeometricField.
|
||||
cm [m] 1e-2;
|
||||
g [kg] 1e-3;
|
||||
mm [m] 1e-3;
|
||||
cm [m] 1e-2;
|
||||
km [m] 1e3;
|
||||
ms [s] 1e-3;
|
||||
us [s] 1e-6;
|
||||
ms [s] 1e-3;
|
||||
}
|
||||
|
||||
USCSCoeffs
|
||||
|
||||
@ -25,47 +25,65 @@ License
|
||||
|
||||
#include "DimensionedFunction1.H"
|
||||
|
||||
// * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * //
|
||||
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
|
||||
|
||||
template<class Type>
|
||||
void Foam::Function1s::Dimensioned<Type>::read
|
||||
(
|
||||
const word& name,
|
||||
const dictionary& dict
|
||||
)
|
||||
namespace Foam
|
||||
{
|
||||
// Read units from the given stream
|
||||
auto readUnits = []
|
||||
(
|
||||
Istream& is,
|
||||
const word& prefix,
|
||||
dimensionedScalar& units
|
||||
)
|
||||
{
|
||||
units.name() =
|
||||
prefix + (prefix.empty() ? 'u' : 'U') + "nits";
|
||||
|
||||
inline dimensionedScalar readUnits(Istream& is)
|
||||
{
|
||||
scalar multiplier;
|
||||
|
||||
dimensionSet d(dimless);
|
||||
d.read(is, units.value());
|
||||
d.read(is, multiplier);
|
||||
|
||||
if (d != units.dimensions())
|
||||
return dimensionedScalar(d, multiplier);
|
||||
}
|
||||
|
||||
|
||||
inline void setUnits
|
||||
(
|
||||
Istream& is,
|
||||
const word& prefix,
|
||||
const dimensionedScalar& from,
|
||||
dimensionedScalar& to
|
||||
)
|
||||
{
|
||||
to.name() =
|
||||
prefix + (prefix.empty() ? 'u' : 'U') + "nits";
|
||||
|
||||
if (from.dimensions() != to.dimensions())
|
||||
{
|
||||
FatalIOErrorInFunction(is)
|
||||
<< "The " << prefix << (prefix.empty() ? "" : "-")
|
||||
<< "dimensions " << d << " provided do not match the required "
|
||||
<< "dimensions " << units.dimensions()
|
||||
<< "dimensions " << from.dimensions()
|
||||
<< " provided do not match the required "
|
||||
<< "dimensions " << to.dimensions()
|
||||
<< exit(FatalIOError);
|
||||
}
|
||||
};
|
||||
|
||||
// Read units if present in the given dictionary
|
||||
auto lookupUnitsIfPresent = [&readUnits]
|
||||
(
|
||||
to.value() = from.value();
|
||||
}
|
||||
|
||||
|
||||
inline void readAndSetUnits
|
||||
(
|
||||
Istream& is,
|
||||
const word& prefix,
|
||||
dimensionedScalar& units
|
||||
)
|
||||
{
|
||||
setUnits(is, prefix, readUnits(is), units);
|
||||
}
|
||||
|
||||
|
||||
inline void lookupUnitsIfPresent
|
||||
(
|
||||
const dictionary& dict,
|
||||
const word& prefix,
|
||||
dimensionedScalar& units
|
||||
)
|
||||
{
|
||||
)
|
||||
{
|
||||
const word unitsKey =
|
||||
prefix + (prefix.empty() ? 'u' : 'U') + "nits";
|
||||
const word dimsKey =
|
||||
@ -89,10 +107,21 @@ void Foam::Function1s::Dimensioned<Type>::read
|
||||
|
||||
if (haveDims)
|
||||
{
|
||||
readUnits(dict.lookup(dimsKey), prefix, units);
|
||||
readAndSetUnits(dict.lookup(dimsKey), prefix, units);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * //
|
||||
|
||||
template<class Type>
|
||||
void Foam::Function1s::Dimensioned<Type>::read
|
||||
(
|
||||
const word& name,
|
||||
const dictionary& dict
|
||||
)
|
||||
{
|
||||
// If the function is a dictionary (preferred) then read straightforwardly
|
||||
if (dict.isDict(name))
|
||||
{
|
||||
@ -121,15 +150,29 @@ void Foam::Function1s::Dimensioned<Type>::read
|
||||
// construct the function from the stream
|
||||
if (!firstToken.isWord() || !is.eof())
|
||||
{
|
||||
// Peek at the next token
|
||||
PtrList<dimensionedScalar> units;
|
||||
|
||||
// Peek at the next token and read the units, if provided
|
||||
token nextToken(is);
|
||||
is.putBack(nextToken);
|
||||
|
||||
// Read dimensions if they are provided
|
||||
if (nextToken == token::BEGIN_SQR)
|
||||
{
|
||||
readUnits(is, "", units_);
|
||||
units.append(new dimensionedScalar(readUnits(is)));
|
||||
|
||||
// And again ...
|
||||
token nextToken(is);
|
||||
is.putBack(nextToken);
|
||||
if (nextToken == token::BEGIN_SQR)
|
||||
{
|
||||
units.append(new dimensionedScalar(readUnits(is)));
|
||||
}
|
||||
}
|
||||
|
||||
// Set the units. If there is just one, then just take this to be the
|
||||
// value units. If two, then the first is the argument units and the
|
||||
// second is the value units.
|
||||
if (units.size() == 2) setUnits(is, "x", units.first(), xUnits_);
|
||||
if (units.size() >= 1) setUnits(is, "", units.last(), units_);
|
||||
|
||||
// Construct from a stream
|
||||
value_.reset(Function1<Type>::New(name, Function1Type, is).ptr());
|
||||
|
||||
Reference in New Issue
Block a user