/*---------------------------------------------------------------------------*\
========= |
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration |
\\ / A nd | www.openfoam.com
\\/ M anipulation |
-------------------------------------------------------------------------------
Copyright (C) 2018-2021 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 .
\*---------------------------------------------------------------------------*/
#include "ConstantField.H"
// * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
template
Foam::PatchFunction1Types::ConstantField::ConstantField
(
const polyPatch& pp,
const word& entryName,
const Type& uniformValue,
const dictionary& dict,
const bool faceValues
)
:
PatchFunction1(pp, entryName, dict, faceValues),
isUniform_(true),
uniformValue_(uniformValue),
value_(this->size(), uniformValue_)
{}
template
Foam::PatchFunction1Types::ConstantField::ConstantField
(
const polyPatch& pp,
const word& entryName,
const bool isUniform,
const Type& uniformValue,
const Field& fieldValues,
const dictionary& dict,
const bool faceValues
)
:
PatchFunction1(pp, entryName, dict, faceValues),
isUniform_(isUniform),
uniformValue_(uniformValue),
value_(fieldValues)
{
if (fieldValues.size() != this->size())
{
FatalIOErrorInFunction(dict)
<< "Supplied field size " << fieldValues.size()
<< " is not equal to the number of "
<< (faceValues ? "faces" : "points") << ' '
<< this->size() << " of patch " << pp.name() << nl
<< exit(FatalIOError);
}
}
template
Foam::Field
Foam::PatchFunction1Types::ConstantField::getValue
(
const entry* eptr,
const dictionary& dict,
const label len,
bool& isUniform,
Type& uniformValue
)
{
isUniform = true;
uniformValue = Zero;
Field fld;
if (!eptr || !eptr->isStream())
{
FatalIOErrorInFunction(dict)
<< "Null or invalid entry" << nl
<< exit(FatalIOError);
}
ITstream& is = eptr->stream();
if (is.peek().isWord())
{
const word contentType(is);
if (contentType == "constant" || contentType == "uniform")
{
is >> uniformValue;
fld.resize(len);
fld = uniformValue;
}
else if (contentType == "nonuniform")
{
if (len)
{
isUniform = false;
}
is >> static_cast&>(fld);
const label lenRead = fld.size();
if (len != lenRead)
{
if
(
len < lenRead
&& FieldBase::allowConstructFromLargerSize
)
{
#ifdef FULLDEBUG
IOWarningInFunction(dict)
<< "Sizes do not match. Truncating " << lenRead
<< " entries to " << len << endl;
#endif
// Truncate the data
fld.resize(len);
}
else
{
FatalIOErrorInFunction(dict)
<< "size " << lenRead
<< " is not equal to the expected length " << len
<< exit(FatalIOError);
}
}
}
else
{
FatalIOErrorInFunction(dict)
<< "Expected keyword 'constant', 'uniform', or 'nonuniform'"
<< ", found " << contentType
<< exit(FatalIOError);
}
}
else
{
// Uniform (constant) field
is >> uniformValue;
fld.resize(len);
fld = uniformValue;
}
return fld;
}
template
Foam::PatchFunction1Types::ConstantField::ConstantField
(
const polyPatch& pp,
const word& redirectType,
const word& entryName,
const dictionary& dict,
const bool faceValues
)
:
PatchFunction1(pp, entryName, dict, faceValues),
isUniform_(true), // overwritten by getValue()
uniformValue_(Zero), // overwritten by getValue()
value_
(
getValue
(
dict.findEntry(entryName, keyType::LITERAL),
dict,
this->size(),
isUniform_,
uniformValue_
)
)
{}
template
Foam::PatchFunction1Types::ConstantField::ConstantField
(
const polyPatch& pp,
const entry* eptr,
const word& entryName,
const dictionary& dict,
const bool faceValues
)
:
PatchFunction1(pp, entryName, dict, faceValues),
isUniform_(true), // overwritten by getValue()
uniformValue_(Zero), // overwritten by getValue()
value_
(
getValue
(
eptr,
dict,
this->size(),
isUniform_,
uniformValue_
)
)
{}
template
Foam::PatchFunction1Types::ConstantField::ConstantField
(
const ConstantField& rhs
)
:
ConstantField(rhs, rhs.patch())
{}
template
Foam::PatchFunction1Types::ConstantField::ConstantField
(
const ConstantField& rhs,
const polyPatch& pp
)
:
PatchFunction1(rhs, pp),
isUniform_(rhs.isUniform_),
uniformValue_(rhs.uniformValue_),
value_(rhs.value_)
{
// If sizes are different...
value_.resize(this->size(), Zero);
if (isUniform_)
{
value_ = uniformValue_;
}
}
// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
template
void Foam::PatchFunction1Types::ConstantField::autoMap
(
const FieldMapper& mapper
)
{
value_.autoMap(mapper);
// If originating from single value override just to make sure
if (isUniform_)
{
value_ = uniformValue_;
}
}
template
void Foam::PatchFunction1Types::ConstantField::rmap
(
const PatchFunction1& pf1,
const labelList& addr
)
{
const auto& cst = refCast>(pf1);
value_.rmap(cst.value_, addr);
}
template
void Foam::PatchFunction1Types::ConstantField::writeData
(
Ostream& os
) const
{
PatchFunction1::writeData(os);
if (isUniform_)
{
os.writeKeyword(this->name())
<< word("constant") << token::SPACE << uniformValue_;
os.endEntry();
}
else
{
value_.writeEntry(this->name(), os);
}
}
// ************************************************************************* //