introvoyz042's picture
Migrated from GitHub
5a600cf verified
Raw History Blame Contribute Delete
20.8 kB
/*---------------------------------------------------------------------------*\
========= |
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration | Website: https://openfoam.org
\\ / A nd | Copyright (C) 2011-2026 OpenFOAM Foundation
\\/ M anipulation |
-------------------------------------------------------------------------------
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 "Field.H"
#include "units.H"
#include "dictionary.H"
#include "contiguous.H"
#include "expressionAssert.H"
// * * * * * * * * * * * * * * * Static Members * * * * * * * * * * * * * * //
template<class Type>
const char* const Foam::Field<Type>::typeName("Field");
// * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
template<class Type>
Foam::Field<Type>::Field()
:
List<Type>()
{}
template<class Type>
Foam::Field<Type>::Field(const label size)
:
List<Type>(size)
{}
template<class Type>
Foam::Field<Type>::Field(const label size, const Type& t)
:
List<Type>(size, t)
{}
template<class Type>
Foam::Field<Type>::Field(const label size, const zero)
:
List<Type>(size, Zero)
{}
template<class Type>
Foam::Field<Type>::Field(const UList<Type>& list)
:
List<Type>(list)
{}
template<class Type>
Foam::Field<Type>::Field(List<Type>&& f)
:
List<Type>(move(f))
{}
template<class Type>
Foam::Field<Type>::Field(const UIndirectList<Type>& list)
:
List<Type>(list)
{}
template<class Type>
Foam::Field<Type>::Field(const Field<Type>& f)
:
tmp<Field<Type>>::refCount(),
List<Type>(f)
{}
template<class Type>
Foam::Field<Type>::Field(Field<Type>& f, bool reuse)
:
List<Type>(f, reuse)
{}
template<class Type>
Foam::Field<Type>::Field(Field<Type>&& f)
:
tmp<Field<Type>>::refCount(),
List<Type>(move(f))
{}
template<class Type>
Foam::Field<Type>::Field(const tmp<Field<Type>>& tf)
:
List<Type>(const_cast<Field<Type>&>(tf()), tf.isTmp())
{
tf.clear();
}
template<class Type>
Foam::Field<Type>::Field
(
const UList<Type>& mapF,
const labelUList& mapAddressing
)
:
List<Type>(mapAddressing.size())
{
map(mapF, mapAddressing);
}
template<class Type>
Foam::Field<Type>::Field
(
const tmp<Field<Type>>& tmapF,
const labelUList& mapAddressing
)
:
List<Type>(mapAddressing.size())
{
map(tmapF, mapAddressing);
}
template<class Type>
Foam::Field<Type>::Field
(
const UList<Type>& mapF,
const labelListList& mapAddressing,
const scalarListList& mapWeights
)
:
List<Type>(mapAddressing.size())
{
map(mapF, mapAddressing, mapWeights);
}
template<class Type>
Foam::Field<Type>::Field
(
const tmp<Field<Type>>& tmapF,
const labelListList& mapAddressing,
const scalarListList& mapWeights
)
:
List<Type>(mapAddressing.size())
{
map(tmapF, mapAddressing, mapWeights);
}
template<class Type>
Foam::Field<Type>::Field(Istream& is)
:
List<Type>(is)
{}
template<class Type>
Foam::Field<Type>::Field
(
const word& keyword,
const dictionary& dict,
const label s
)
:
Field<Type>(keyword, units::any, dict, s)
{}
template<class Type>
Foam::Field<Type>::Field
(
const word& keyword,
const unitSet& defaultUnits,
const dictionary& dict,
const label s
)
{
if (s)
{
ITstream& is = dict.lookup(keyword);
// Read first token
token firstToken(is);
// Read the units if they are before the values
unitSet units(defaultUnits);
const bool haveUnits = units.readIfPresent(keyword, dict, is);
// Read the values
if (firstToken.isWord())
{
if (firstToken.wordToken() == "uniform")
{
this->setSize(s);
operator=(pTraits<Type>(is));
}
else if (firstToken.wordToken() == "nonuniform")
{
is >> static_cast<List<Type>&>(*this);
if (this->size() != s)
{
FatalIOErrorInFunction(dict)
<< "size " << this->size()
<< " is not equal to the given value of " << s
<< exit(FatalIOError);
}
}
else
{
FatalIOErrorInFunction(dict)
<< "expected keyword 'uniform' or 'nonuniform', found "
<< firstToken.wordToken()
<< exit(FatalIOError);
}
}
else
{
FatalIOErrorInFunction(dict)
<< "expected keyword 'uniform' or 'nonuniform', found "
<< firstToken.info()
<< exit(FatalIOError);
}
// Read the units if they are after the value
if (!haveUnits && !is.eof())
{
units.readIfPresent(keyword, dict, is);
}
// Modify the values by the unit conversion
units.makeStandard(*this);
}
}
template<class Type>
template<class Expression, class>
Foam::Field<Type>::Field(const Expression& e)
:
List<Type>(expression::getFirst<expression::Size>(e))
{
#ifdef FULLDEBUG
expression::assertSameAllContainerProperty<expression::Size>(e);
#endif
std::copy
(
expression::beginNew<Type, false>(e),
expression::endNew<Type, false>(e),
this->begin()
);
}
template<class Type>
Foam::tmp<Foam::Field<Type>> Foam::Field<Type>::clone() const
{
return tmp<Field<Type>>(new Field<Type>(*this));
}
template<class Type>
Foam::tmp<Foam::Field<Type>> Foam::Field<Type>::New
(
const tmp<Field<Type>>& tf1,
const bool initRet
)
{
if (tf1.isTmp())
{
return tf1;
}
else
{
tmp<Field<Type>> rtf(new Field<Type>(tf1().size()));
if (initRet)
{
rtf.ref() = tf1();
}
return rtf;
}
}
// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
template<class Type>
const Foam::UList<Type>& Foam::Field<Type>::copySelf
(
const UList<Type>& mapF,
tmp<Field<Type>>& tmapF
) const
{
if (static_cast<const UList<Type>*>(this) == &mapF)
{
tmapF = clone();
}
return tmapF.valid() ? tmapF() : mapF;
}
template<class Type>
void Foam::Field<Type>::map
(
const UList<Type>& mapF0,
const labelUList& mapAddressing
)
{
Field<Type>& f = *this;
tmp<Field<Type>> tmapF;
const UList<Type>& mapF = copySelf(mapF0, tmapF);
if (f.size() != mapAddressing.size())
{
f.setSize(mapAddressing.size());
}
if (mapF.size() > 0)
{
forAll(f, i)
{
const label mapi = mapAddressing[i];
if (mapi >= 0)
{
f[i] = mapF[mapi];
}
}
}
}
template<class Type>
void Foam::Field<Type>::map
(
const tmp<Field<Type>>& tmapF,
const labelUList& mapAddressing
)
{
map(tmapF(), mapAddressing);
tmapF.clear();
}
template<class Type>
void Foam::Field<Type>::map
(
const UList<Type>& mapF0,
const labelListList& mapAddressing,
const scalarListList& mapWeights
)
{
if (mapWeights.size() != mapAddressing.size())
{
FatalErrorInFunction
<< mapWeights.size() << " map size: " << mapAddressing.size()
<< abort(FatalError);
}
Field<Type>& f = *this;
tmp<Field<Type>> tmapF;
const UList<Type>& mapF = copySelf(mapF0, tmapF);
if (this->size() != mapAddressing.size())
{
this->setSize(mapAddressing.size());
}
forAll(f, i)
{
const labelList& localAddrs = mapAddressing[i];
const scalarList& localWeights = mapWeights[i];
f[i] = Zero;
forAll(localAddrs, j)
{
f[i] += localWeights[j]*mapF[localAddrs[j]];
}
}
}
template<class Type>
void Foam::Field<Type>::map
(
const tmp<Field<Type>>& tmapF,
const labelListList& mapAddressing,
const scalarListList& mapWeights
)
{
map(tmapF(), mapAddressing, mapWeights);
tmapF.clear();
}
template<class Type>
void Foam::Field<Type>::rmap
(
const UList<Type>& mapF0,
const labelUList& mapAddressing
)
{
Field<Type>& f = *this;
tmp<Field<Type>> tmapF;
const UList<Type>& mapF = copySelf(mapF0, tmapF);
forAll(mapF, i)
{
const label mapi = mapAddressing[i];
if (mapi >= 0)
{
f[mapi] = mapF[i];
}
}
}
template<class Type>
void Foam::Field<Type>::rmap
(
const tmp<Field<Type>>& tmapF,
const labelUList& mapAddressing
)
{
rmap(tmapF(), mapAddressing);
tmapF.clear();
}
template<class Type>
void Foam::Field<Type>::rmap
(
const UList<Type>& mapF0,
const labelUList& mapAddressing,
const UList<scalar>& mapWeights
)
{
Field<Type>& f = *this;
tmp<Field<Type>> tmapF;
const UList<Type>& mapF = copySelf(mapF0, tmapF);
f = Zero;
forAll(mapF, i)
{
f[mapAddressing[i]] += mapF[i]*mapWeights[i];
}
}
template<class Type>
void Foam::Field<Type>::rmap
(
const tmp<Field<Type>>& tmapF,
const labelUList& mapAddressing,
const UList<scalar>& mapWeights
)
{
rmap(tmapF(), mapAddressing, mapWeights);
tmapF.clear();
}
template<class Type>
void Foam::Field<Type>::reset(const Field<Type>& rhs)
{
if (this == &rhs)
{
FatalErrorInFunction
<< "attempted assignment to self"
<< abort(FatalError);
}
List<Type>::operator=(rhs);
}
template<class Type>
void Foam::Field<Type>::reset(const UList<Type>& rhs)
{
List<Type>::operator=(rhs);
}
template<class Type>
void Foam::Field<Type>::negate()
{
forAll(*this, i)
{
this->operator[](i) = -this->operator[](i);
}
}
template<class Type>
void Foam::Field<Type>::replace
(
const direction d,
const UList<cmptType>& sf
)
{
forAll(sf, i)
{
setComponent(this->operator[](i), d) = sf[i];
}
}
template<class Type>
void Foam::Field<Type>::replace
(
const direction d,
const tmp<Field<cmptType>>& tsf
)
{
replace(d, tsf());
tsf.clear();
}
template<class Type>
template<class Expression, class>
void Foam::Field<Type>::replace
(
const direction d,
const Expression& e
)
{
forAll(*this, i)
{
setComponent(this->operator[](i), d) = expression::access(e, i);
}
}
template<class Type>
void Foam::Field<Type>::replace
(
const direction d,
const cmptType& c
)
{
forAll(*this, i)
{
setComponent(this->operator[](i), d) = c;
}
}
template<class Type>
void Foam::Field<Type>::boundLower(const Type& lower)
{
forAll(*this, i)
{
this->operator[](i) = max(this->operator[](i), lower);
}
}
template<class Type>
void Foam::Field<Type>::boundUpper(const Type& upper)
{
forAll(*this, i)
{
this->operator[](i) = min(this->operator[](i), upper);
}
}
template<class Type>
void Foam::Field<Type>::bound(const Type& lower, const Type& upper)
{
forAll(*this, i)
{
this->operator[](i) = maxMin(this->operator[](i), lower, upper);
}
}
template<class Type>
template<class VSForm>
VSForm Foam::Field<Type>::block(const label start) const
{
VSForm vs;
for (direction i=0; i<VSForm::nComponents; i++)
{
vs[i] = this->operator[](start + i);
}
return vs;
}
template<class Type>
void Foam::Field<Type>::append(const Type& t)
{
List<Type>::append(t);
}
template<class Type>
void Foam::Field<Type>::append(const Field<Type>& f)
{
List<Type>::append(f);
}
template<class Type>
void Foam::Field<Type>::append(const tmp<Field<Type>>& tf)
{
List<Type>::append(tf);
tf.clear();
}
template<class Type>
template<class Expression, class>
void Foam::Field<Type>::append(const Expression& e)
{
// Error if any field in the expression is a different size
#ifdef FULLDEBUG
expression::assertSameAllContainerProperty<expression::Size>(e);
#endif
// If expression contains this
{
append(eval(e));
}
// else
// {
// const label s = this->size();
// // Resize the field to that of the first field in the expression
// List<Type>::resize(s + expression::getFirst<expression::Size>(e));
// std::copy(expression::begin(e), expression::end(e), this->begin()+s);
// }
}
// * * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * //
template<class Type>
void Foam::Field<Type>::operator=(const Field<Type>& rhs)
{
if (this == &rhs)
{
FatalErrorInFunction
<< "attempted assignment to self"
<< abort(FatalError);
}
List<Type>::operator=(rhs);
}
template<class Type>
void Foam::Field<Type>::operator=(Field<Type>&& rhs)
{
if (this == &rhs)
{
FatalErrorInFunction
<< "attempted assignment to self"
<< abort(FatalError);
}
List<Type>::operator=(move(rhs));
}
template<class Type>
void Foam::Field<Type>::operator=(const UList<Type>& rhs)
{
List<Type>::operator=(rhs);
}
template<class Type>
void Foam::Field<Type>::operator=(List<Type>&& rhs)
{
List<Type>::operator=(move(rhs));
}
template<class Type>
void Foam::Field<Type>::operator=(const tmp<Field>& rhs)
{
if (this == &(rhs()))
{
FatalErrorInFunction
<< "attempted assignment to self"
<< abort(FatalError);
}
List<Type>::operator=(rhs());
}
template<class Type>
void Foam::Field<Type>::operator=(const typename Foam::Field<Type>::pType& t)
{
List<Type>::operator=(t);
}
template<class Type>
void Foam::Field<Type>::operator=(const zero)
{
List<Type>::operator=(Zero);
}
template<class Type>
template<class Form, class Cmpt, Foam::direction nCmpt>
void Foam::Field<Type>::operator=(const VectorSpace<Form,Cmpt,nCmpt>& vs)
{
forAll(*this, i)
{
this->operator[](i) = vs;
}
}
template<class Type>
template<class Expression, class>
void Foam::Field<Type>::operator=(const Expression& e)
{
// Error if any field in the expression is a different size
#ifdef FULLDEBUG
expression::assertSameAllContainerProperty<expression::Size>(e);
#endif
// Resize the field to that of the first field in the expression
List<Type>::resize(expression::getFirst<expression::Size>(e));
std::copy(expression::begin(e), expression::end(e), this->begin());
}
#define COMPUTED_ASSIGNMENT(TYPE, PTYPE, op) \
\
template<class Type> \
void Foam::Field<Type>::operator op(const UList<TYPE>& f) \
{ \
forAll(*this, i) \
{ \
this->operator[](i) op f[i]; \
} \
} \
\
template<class Type> \
void Foam::Field<Type>::operator op(const Field<TYPE>& f) \
{ \
forAll(*this, i) \
{ \
this->operator[](i) op f[i]; \
} \
} \
\
template<class Type> \
void Foam::Field<Type>::operator op(const tmp<Field<TYPE>>& tf) \
{ \
operator op(tf()); \
tf.clear(); \
} \
\
template<class Type> \
template<class Expression, class> \
void Foam::Field<Type>::operator op(const Expression& e) \
{ \
/* Error if the expression is a different size */ \
expression::assertSameAllContainerProperty<expression::Size> \
( \
*this, \
e \
); \
\
forAll(*this, i) \
{ \
this->operator[](i) op expression::access(e, i); \
} \
} \
\
template<class Type> \
void Foam::Field<Type>::operator op(const PTYPE& t) \
{ \
forAll(*this, i) \
{ \
this->operator[](i) op t; \
} \
}
#define pType_ typename Foam::Field<Type>::pType
COMPUTED_ASSIGNMENT(Type, pType_, +=)
COMPUTED_ASSIGNMENT(Type, pType_, -=)
#undef pType_
#define cmptType_ typename Foam::Field<Type>::cmptType
#define pCmptType_ typename Foam::Field<Type>::pCmptType
COMPUTED_ASSIGNMENT(cmptType_, pCmptType_, *=)
COMPUTED_ASSIGNMENT(cmptType_, pCmptType_, /=)
#undef pCmptType_
#undef cmptType_
#undef COMPUTED_ASSIGNMENT
// * * * * * * * * * * * * * * * IOstream Functions * * * * * * * * * * * * //
template<class Type>
void Foam::writeEntry(Ostream& os, const Field<Type>& f)
{
bool uniform = false;
if (f.size() && contiguous<Type>())
{
uniform = true;
forAll(f, i)
{
if (f[i] != f[0])
{
uniform = false;
break;
}
}
}
if (uniform)
{
os << "uniform " << f[0];
}
else
{
os << "nonuniform ";
writeEntry(os, static_cast<const List<Type>&>(f));
}
}
// * * * * * * * * * * * * * * * Ostream Operator * * * * * * * * * * * * * //
template<class Type>
Foam::Ostream& Foam::operator<<(Ostream& os, const Field<Type>& f)
{
os << static_cast<const List<Type>&>(f);
return os;
}
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
#include "SubField.H"
// ************************************************************************* //