Commit 61e95b84 authored by Mark Olesen's avatar Mark Olesen Committed by Andrew Heather
Browse files

ENH: improvements to SubList and SubField

- SubField and SubList assign from zero
- SubField +=, -=, *=, /= operators

- SubList construct from UList (as per SubField)

  Note: constructing an anonymous SubField or SubList with a single
  parameter should use '{} instead of '()' to avoid compiler
  ambiguities.
parent f75e01c8
Test-SubField.C
EXE = $(FOAM_USER_APPBIN)/Test-SubField
/* EXE_INC = */
/* EXE_LIBS = */
/*---------------------------------------------------------------------------*\
========= |
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration |
\\ / A nd | Copyright (C) 2019 OpenCFD Ltd.
\\/ 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/>.
Application
Test-SubField
Description
Simple tests on SubList, SubField
\*---------------------------------------------------------------------------*/
#include "OSspecific.H"
#include "argList.H"
#include "scalarField.H"
#include "SubField.H"
#include "labelRange.H"
#include <numeric>
using namespace Foam;
template<class T>
void print(const UList<T>& list)
{
Info<< flatOutput(list) << nl;
}
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
// Main program:
int main(int argc, char *argv[])
{
argList::noParallel();
argList::noFunctionObjects();
{
List<scalar> ident(25);
std::iota(ident.begin(), ident.end(), 0);
print(ident);
SubList<scalar>(ident, 10) = -10;
print(ident);
SubField<scalar>(ident, 10) = 10;
print(ident);
SubField<scalar>(ident, 10) += 10;
print(ident);
SubField<scalar>{ident, 10, 10} *= 5;
print(ident);
// NOTE: Need {} instead of ()
// SubList<scalar>(ident) = 100;
// GCC
// error: conflicting declaration 'Foam::SubList<double> ident'
// CLANG
// warning: parentheses were disambiguated as redundant parentheses
// around declaration of variable named 'ident' [-Wvexing-parse]
SubList<scalar>{ident} = 100;
print(ident);
}
Info << "\nEnd\n";
return 0;
}
// ************************************************************************* //
......@@ -77,6 +77,9 @@ public:
// Constructors
//- Construct from UList, the entire size
inline explicit SubList(const UList<T>& list);
//- Construct from UList and sub-list size, start at 0
inline SubList
(
......@@ -112,17 +115,20 @@ public:
// Member Operators
//- Allow cast to a const List<T>&
//- Allow cast to a const List\<T\>&
inline operator const Foam::List<T>&() const;
//- Assignment of all entries to the given sub-list
//- Copy assign entries from given sub-list
inline void operator=(const SubList<T>& list);
//- Assignment of all entries to the given list
//- Copy assign entries to the given list
inline void operator=(const UList<T>& list);
//- Assignment of all entries to the given value
//- Assign all entries to the given value
inline void operator=(const T& val);
//- Assign all entries to zero
inline void operator=(const zero);
};
......
......@@ -2,7 +2,7 @@
========= |
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration |
\\ / A nd | Copyright (C) 2017 OpenCFD Ltd.
\\ / A nd | Copyright (C) 2017-2019 OpenCFD Ltd.
\\/ M anipulation |
-------------------------------------------------------------------------------
| Copyright (C) 2011-2016 OpenFOAM Foundation
......@@ -27,6 +27,16 @@ License
// * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
template<class T>
inline Foam::SubList<T>::SubList
(
const UList<T>& list
)
:
UList<T>(list.v_, list.size())
{}
template<class T>
inline Foam::SubList<T>::SubList
(
......@@ -131,4 +141,11 @@ inline void Foam::SubList<T>::operator=(const T& val)
}
template<class T>
inline void Foam::SubList<T>::operator=(const zero)
{
UList<T>::operator=(Zero);
}
// ************************************************************************* //
......@@ -203,8 +203,8 @@ public:
const bool applyFlip = true
);
//- Construct by mapping from the given tmp field. Supplied uniform
//- value for unmapped items
//- Construct by mapping from the given tmp field.
//- Uses supplied uniform value for unmapped items
Field
(
const tmp<Field<Type>>& tmapF,
......@@ -213,8 +213,8 @@ public:
const bool applyFlip = true
);
//- Construct by mapping from the given tmp field. Supplied values
//- for unmapped items
//- Construct by mapping from the given tmp field.
//- Uses supplied values for unmapped items
Field
(
const tmp<Field<Type>>& tmapF,
......@@ -245,7 +245,7 @@ public:
}
//- Return a pointer to a new calculatedFvPatchFieldField created on
// freestore without setting patchField values
//- freestore without setting patchField values
template<class Type2>
static tmp<Field<Type>> NewCalculatedType(const Field<Type2>& f)
{
......@@ -363,7 +363,7 @@ public:
void writeEntry(const word& keyword, Ostream& os) const;
// Member operators
// Member Operators
//- Copy assignment
void operator=(const Field<Type>&);
......
......@@ -2,7 +2,7 @@
========= |
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration |
\\ / A nd | Copyright (C) 2018 OpenCFD Ltd.
\\ / A nd | Copyright (C) 2018-2019 OpenCFD Ltd.
\\/ M anipulation |
-------------------------------------------------------------------------------
| Copyright (C) 2011-2016 OpenFOAM Foundation
......@@ -73,20 +73,23 @@ public:
// Constructors
//- Copy construct from a SubList
//- Copy construct (shallow copy)
inline SubField(const SubField<Type>& sfield);
//- Copy construct from SubList
inline SubField(const SubList<Type>& list);
//- Construct from a UList, the entire size
//- Construct from UList, the entire size
inline explicit SubField(const UList<Type>& list);
//- Construct from a UList with a given sub-list size, start at 0
//- Construct from UList with a given sub-list size, start at 0
inline SubField
(
const UList<Type>& list,
const label subSize
);
//- Construct from a UList with a given size and start index
//- Construct from UList with a given size and start index
inline SubField
(
const UList<Type>& list,
......@@ -111,9 +114,6 @@ public:
const UList<Type>& list
);
//- Copy construct
inline SubField(const SubField<Type>& sfield);
// Member Functions
......@@ -127,7 +127,10 @@ public:
tmp<Field<Type>> T() const;
// Member operators
// Member Operators
//- Allow cast to a const Field\<Type\>&
inline operator const Foam::Field<Type>&() const;
//- Copy assign via UList operator. Takes linear time.
inline void operator=(const SubField<Type>&);
......@@ -135,12 +138,27 @@ public:
//- Copy assign via UList operator. Takes linear time.
inline void operator=(const Field<Type>&);
//- Assign all entries to the given value
inline void operator=(const Type& val);
//- Assign all entries to zero
inline void operator=(const zero);
//- Copy assign via UList operator. Takes linear time.
template<class Form, direction Ncmpts>
inline void operator=(const VectorSpace<Form, Type, Ncmpts>&);
inline void operator=(const VectorSpace<Form, Type, Ncmpts>& rhs);
//- Allow cast to a const Field\<Type\>&
inline operator const Field<Type>&() const;
//- Add value to each entry
inline void operator+=(const Type& val);
//- Subtract value from each entry
inline void operator-=(const Type& val);
//- Multiply each entry by value
inline void operator*=(const scalar& s);
//- Divide each entry by value
inline void operator/=(const scalar& s);
};
......
......@@ -2,7 +2,7 @@
========= |
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration |
\\ / A nd |
\\ / A nd | Copyright (C) 2017-2019 OpenCFD Ltd.
\\/ M anipulation |
-------------------------------------------------------------------------------
| Copyright (C) 2011-2016 OpenFOAM Foundation
......@@ -27,6 +27,16 @@ License
// * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
template<class Type>
inline Foam::SubField<Type>::SubField
(
const SubField<Type>& sfield
)
:
SubList<Type>(sfield)
{}
template<class Type>
inline Foam::SubField<Type>::SubField
(
......@@ -92,17 +102,6 @@ inline Foam::SubField<Type>::SubField
{}
template<class Type>
inline Foam::SubField<Type>::SubField
(
const SubField<Type>& sfield
)
:
refCount(),
SubList<Type>(sfield)
{}
// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
template<class Type>
......@@ -132,6 +131,13 @@ inline Foam::tmp<Foam::Field<Type>> Foam::SubField<Type>::T() const
// * * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * //
template<class Type>
inline Foam::SubField<Type>::operator const Foam::Field<Type>&() const
{
return *reinterpret_cast<const Field<Type>*>(this);
}
template<class Type>
inline void Foam::SubField<Type>::operator=(const SubField<Type>& rhs)
{
......@@ -146,6 +152,20 @@ inline void Foam::SubField<Type>::operator=(const Field<Type>& rhs)
}
template<class Type>
inline void Foam::SubField<Type>::operator=(const Type& val)
{
SubList<Type>::operator=(val);
}
template<class Type>
inline void Foam::SubField<Type>::operator=(const zero)
{
SubList<Type>::operator=(Zero);
}
template<class Type>
template<class Form, Foam::direction Ncmpts>
inline void Foam::SubField<Type>::operator=
......@@ -161,9 +181,42 @@ inline void Foam::SubField<Type>::operator=
template<class Type>
inline Foam::SubField<Type>::operator const Foam::Field<Type>&() const
inline void Foam::SubField<Type>::operator+=(const Type& val)
{
return *reinterpret_cast<const Field<Type>*>(this);
for (Type& lhs : *this)
{
lhs += val;
}
}
template<class Type>
inline void Foam::SubField<Type>::operator-=(const Type& val)
{
for (Type& lhs : *this)
{
lhs -= val;
}
}
template<class Type>
inline void Foam::SubField<Type>::operator*=(const scalar& s)
{
for (Type& lhs : *this)
{
lhs *= s;
}
}
template<class Type>
inline void Foam::SubField<Type>::operator/=(const scalar& s)
{
for (Type& lhs : *this)
{
lhs /= s;
}
}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment