From 61e95b84714071c42f5daecca231e0c3d2c89d0f Mon Sep 17 00:00:00 2001 From: Mark Olesen Date: Fri, 4 Oct 2019 14:21:18 +0200 Subject: [PATCH] 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. --- applications/test/SubField/Make/files | 3 + applications/test/SubField/Make/options | 2 + applications/test/SubField/Test-SubField.C | 94 +++++++++++++++++++ .../containers/Lists/SubList/SubList.H | 14 ++- .../containers/Lists/SubList/SubListI.H | 19 +++- src/OpenFOAM/fields/Fields/Field/Field.H | 12 +-- src/OpenFOAM/fields/Fields/Field/SubField.H | 42 ++++++--- src/OpenFOAM/fields/Fields/Field/SubFieldI.H | 81 +++++++++++++--- 8 files changed, 230 insertions(+), 37 deletions(-) create mode 100644 applications/test/SubField/Make/files create mode 100644 applications/test/SubField/Make/options create mode 100644 applications/test/SubField/Test-SubField.C diff --git a/applications/test/SubField/Make/files b/applications/test/SubField/Make/files new file mode 100644 index 0000000000..406ae41531 --- /dev/null +++ b/applications/test/SubField/Make/files @@ -0,0 +1,3 @@ +Test-SubField.C + +EXE = $(FOAM_USER_APPBIN)/Test-SubField diff --git a/applications/test/SubField/Make/options b/applications/test/SubField/Make/options new file mode 100644 index 0000000000..18e6fe47af --- /dev/null +++ b/applications/test/SubField/Make/options @@ -0,0 +1,2 @@ +/* EXE_INC = */ +/* EXE_LIBS = */ diff --git a/applications/test/SubField/Test-SubField.C b/applications/test/SubField/Test-SubField.C new file mode 100644 index 0000000000..b4a7d017b2 --- /dev/null +++ b/applications/test/SubField/Test-SubField.C @@ -0,0 +1,94 @@ +/*---------------------------------------------------------------------------*\ + ========= | + \\ / 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 . + +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 + +using namespace Foam; + +template +void print(const UList& list) +{ + Info<< flatOutput(list) << nl; +} + + +// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // +// Main program: + +int main(int argc, char *argv[]) +{ + argList::noParallel(); + argList::noFunctionObjects(); + + { + List ident(25); + std::iota(ident.begin(), ident.end(), 0); + + print(ident); + + SubList(ident, 10) = -10; + print(ident); + + SubField(ident, 10) = 10; + print(ident); + + SubField(ident, 10) += 10; + print(ident); + + SubField{ident, 10, 10} *= 5; + print(ident); + + + // NOTE: Need {} instead of () + // SubList(ident) = 100; + + // GCC + // error: conflicting declaration 'Foam::SubList ident' + + // CLANG + // warning: parentheses were disambiguated as redundant parentheses + // around declaration of variable named 'ident' [-Wvexing-parse] + + SubList{ident} = 100; + print(ident); + } + + Info << "\nEnd\n"; + return 0; +} + +// ************************************************************************* // diff --git a/src/OpenFOAM/containers/Lists/SubList/SubList.H b/src/OpenFOAM/containers/Lists/SubList/SubList.H index 931e1ecc28..fc2c915ca8 100644 --- a/src/OpenFOAM/containers/Lists/SubList/SubList.H +++ b/src/OpenFOAM/containers/Lists/SubList/SubList.H @@ -77,6 +77,9 @@ public: // Constructors + //- Construct from UList, the entire size + inline explicit SubList(const UList& 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& + //- Allow cast to a const List\& inline operator const Foam::List&() const; - //- Assignment of all entries to the given sub-list + //- Copy assign entries from given sub-list inline void operator=(const SubList& list); - //- Assignment of all entries to the given list + //- Copy assign entries to the given list inline void operator=(const UList& 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); }; diff --git a/src/OpenFOAM/containers/Lists/SubList/SubListI.H b/src/OpenFOAM/containers/Lists/SubList/SubListI.H index 4544c389a9..b79dcbbdb7 100644 --- a/src/OpenFOAM/containers/Lists/SubList/SubListI.H +++ b/src/OpenFOAM/containers/Lists/SubList/SubListI.H @@ -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 +inline Foam::SubList::SubList +( + const UList& list +) +: + UList(list.v_, list.size()) +{} + + template inline Foam::SubList::SubList ( @@ -131,4 +141,11 @@ inline void Foam::SubList::operator=(const T& val) } +template +inline void Foam::SubList::operator=(const zero) +{ + UList::operator=(Zero); +} + + // ************************************************************************* // diff --git a/src/OpenFOAM/fields/Fields/Field/Field.H b/src/OpenFOAM/fields/Fields/Field/Field.H index d6a98f929d..e5078fab66 100644 --- a/src/OpenFOAM/fields/Fields/Field/Field.H +++ b/src/OpenFOAM/fields/Fields/Field/Field.H @@ -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>& 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>& 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 static tmp> NewCalculatedType(const Field& f) { @@ -363,7 +363,7 @@ public: void writeEntry(const word& keyword, Ostream& os) const; - // Member operators + // Member Operators //- Copy assignment void operator=(const Field&); diff --git a/src/OpenFOAM/fields/Fields/Field/SubField.H b/src/OpenFOAM/fields/Fields/Field/SubField.H index 83d99fc429..1265544b76 100644 --- a/src/OpenFOAM/fields/Fields/Field/SubField.H +++ b/src/OpenFOAM/fields/Fields/Field/SubField.H @@ -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& sfield); + + //- Copy construct from SubList inline SubField(const SubList& list); - //- Construct from a UList, the entire size + //- Construct from UList, the entire size inline explicit SubField(const UList& 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& 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& list, @@ -111,9 +114,6 @@ public: const UList& list ); - //- Copy construct - inline SubField(const SubField& sfield); - // Member Functions @@ -127,7 +127,10 @@ public: tmp> T() const; - // Member operators + // Member Operators + + //- Allow cast to a const Field\& + inline operator const Foam::Field&() const; //- Copy assign via UList operator. Takes linear time. inline void operator=(const SubField&); @@ -135,12 +138,27 @@ public: //- Copy assign via UList operator. Takes linear time. inline void operator=(const Field&); + //- 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 - inline void operator=(const VectorSpace&); + inline void operator=(const VectorSpace& rhs); - //- Allow cast to a const Field\& - inline operator const Field&() 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); }; diff --git a/src/OpenFOAM/fields/Fields/Field/SubFieldI.H b/src/OpenFOAM/fields/Fields/Field/SubFieldI.H index cf0fe065f2..febdb9a77c 100644 --- a/src/OpenFOAM/fields/Fields/Field/SubFieldI.H +++ b/src/OpenFOAM/fields/Fields/Field/SubFieldI.H @@ -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 +inline Foam::SubField::SubField +( + const SubField& sfield +) +: + SubList(sfield) +{} + + template inline Foam::SubField::SubField ( @@ -92,17 +102,6 @@ inline Foam::SubField::SubField {} -template -inline Foam::SubField::SubField -( - const SubField& sfield -) -: - refCount(), - SubList(sfield) -{} - - // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * // template @@ -132,6 +131,13 @@ inline Foam::tmp> Foam::SubField::T() const // * * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * // +template +inline Foam::SubField::operator const Foam::Field&() const +{ + return *reinterpret_cast*>(this); +} + + template inline void Foam::SubField::operator=(const SubField& rhs) { @@ -146,6 +152,20 @@ inline void Foam::SubField::operator=(const Field& rhs) } +template +inline void Foam::SubField::operator=(const Type& val) +{ + SubList::operator=(val); +} + + +template +inline void Foam::SubField::operator=(const zero) +{ + SubList::operator=(Zero); +} + + template template inline void Foam::SubField::operator= @@ -161,9 +181,42 @@ inline void Foam::SubField::operator= template -inline Foam::SubField::operator const Foam::Field&() const +inline void Foam::SubField::operator+=(const Type& val) { - return *reinterpret_cast*>(this); + for (Type& lhs : *this) + { + lhs += val; + } +} + + +template +inline void Foam::SubField::operator-=(const Type& val) +{ + for (Type& lhs : *this) + { + lhs -= val; + } +} + + +template +inline void Foam::SubField::operator*=(const scalar& s) +{ + for (Type& lhs : *this) + { + lhs *= s; + } +} + + +template +inline void Foam::SubField::operator/=(const scalar& s) +{ + for (Type& lhs : *this) + { + lhs /= s; + } }