diff --git a/src/OpenFOAM/containers/Lists/SubList/SubList.H b/src/OpenFOAM/containers/Lists/SubList/SubList.H index 1a02f11d79dd1c6907cd561cb92c281f9551bc54..23ebc73f50ca7084539d9da52477f2f199eeece7 100644 --- a/src/OpenFOAM/containers/Lists/SubList/SubList.H +++ b/src/OpenFOAM/containers/Lists/SubList/SubList.H @@ -66,7 +66,7 @@ public: // Constructors - //- Construct from UList and sub-list size + //- Construct from UList and sub-list size, start at 0 inline SubList ( const UList<T>& list, @@ -91,7 +91,7 @@ public: ); //- Construct from UList and a (start,size) range, but bypassing - // run-time range checking. + //- run-time range checking. inline SubList ( const labelRange& range, @@ -99,7 +99,7 @@ public: ); - // Member operators + // Member Operators //- Allow cast to a const List<T>& inline operator const Foam::List<T>&() const; diff --git a/src/OpenFOAM/fields/DimensionedFields/DimensionedField/SubDimensionedField.H b/src/OpenFOAM/fields/DimensionedFields/DimensionedField/SubDimensionedField.H index 1f90a784ab2a3e8bf4926bf8c90dcfcd21e49a82..94fb19e5d9029833935c1a4f2bb017076977511c 100644 --- a/src/OpenFOAM/fields/DimensionedFields/DimensionedField/SubDimensionedField.H +++ b/src/OpenFOAM/fields/DimensionedFields/DimensionedField/SubDimensionedField.H @@ -70,10 +70,7 @@ public: // Constructors //- Construct from a SubField - inline SubDimensionedField - ( - const SubField<Type>& slist - ); + inline SubDimensionedField(const SubField<Type>& sfield); //- Construct from a UList and size inline SubDimensionedField @@ -90,6 +87,24 @@ public: const label startIndex ); + //- Construct from UList and a (start,size) range. + // The range is subsetted with the list size itself to ensure that the + // result always addresses a valid section of the list. + inline SubDimensionedField + ( + const UList<Type>& list, + const labelRange& range + ); + + //- Construct from UList and a (start,size) range, but bypassing + //- run-time range checking. + inline SubDimensionedField + ( + const labelRange& range, + const UList<Type>& list + ); + + //- Construct as copy inline SubDimensionedField ( diff --git a/src/OpenFOAM/fields/DimensionedFields/DimensionedField/SubDimensionedFieldI.H b/src/OpenFOAM/fields/DimensionedFields/DimensionedField/SubDimensionedFieldI.H index 215a52542192b7bee0f78948776c35c48217b5e6..4dfdbcb9fca6488475f8a82e29a690b64ffe4b1a 100644 --- a/src/OpenFOAM/fields/DimensionedFields/DimensionedField/SubDimensionedFieldI.H +++ b/src/OpenFOAM/fields/DimensionedFields/DimensionedField/SubDimensionedFieldI.H @@ -28,12 +28,13 @@ License template<class Type, class GeoMesh> inline Foam::SubDimensionedField<Type, GeoMesh>::SubDimensionedField ( - const SubField<Type>& slist + const SubField<Type>& sfield ) : - SubField<Type>(slist) + SubField<Type>(sfield) {} + template<class Type, class GeoMesh> inline Foam::SubDimensionedField<Type, GeoMesh>::SubDimensionedField ( @@ -57,6 +58,28 @@ inline Foam::SubDimensionedField<Type, GeoMesh>::SubDimensionedField {} +template<class Type, class GeoMesh> +inline Foam::SubDimensionedField<Type, GeoMesh>::SubDimensionedField +( + const UList<Type>& list, + const labelRange& range +) +: + SubField<Type>(list, range) +{} + + +template<class Type, class GeoMesh> +inline Foam::SubDimensionedField<Type, GeoMesh>::SubDimensionedField +( + const labelRange& range, + const UList<Type>& list +) +: + SubField<Type>(range, list) +{} + + template<class Type, class GeoMesh> inline Foam::SubDimensionedField<Type, GeoMesh>::SubDimensionedField ( diff --git a/src/OpenFOAM/fields/Fields/Field/SubField.H b/src/OpenFOAM/fields/Fields/Field/SubField.H index 9509a65499f8160e5b65f2dd9e274cd235d5a66b..97900b6f779d1c8897b706526fb4d90dc077787a 100644 --- a/src/OpenFOAM/fields/Fields/Field/SubField.H +++ b/src/OpenFOAM/fields/Fields/Field/SubField.H @@ -3,7 +3,7 @@ \\ / F ield | OpenFOAM: The Open Source CFD Toolbox \\ / O peration | \\ / A nd | Copyright (C) 2011-2016 OpenFOAM Foundation - \\/ M anipulation | + \\/ M anipulation | Copyright (C) 2018 OpenCFD Ltd. ------------------------------------------------------------------------------- License This file is part of OpenFOAM. @@ -48,7 +48,7 @@ SourceFiles namespace Foam { -//- Pre-declare SubField and related Field type +// Forward declarations template<class Type> class Field; template<class Type> class SubField; @@ -71,20 +71,20 @@ public: // Constructors - //- Construct from a SubList - inline SubField(const SubList<Type>&); + //- Copy construct from a SubList + inline SubField(const SubList<Type>& list); - //- Construct from a UList\<Type\>, using the entire size - inline explicit SubField(const UList<Type>&); + //- Construct from a UList, the entire size + inline explicit SubField(const UList<Type>& list); - //- Construct from a UList\<Type\> with a given size + //- Construct from a UList with a given sub-list size, start at 0 inline SubField ( const UList<Type>& list, const label subSize ); - //- Construct from a UList\<Type\> with a given size and start index + //- Construct from a UList with a given size and start index inline SubField ( const UList<Type>& list, @@ -92,11 +92,28 @@ public: const label startIndex ); - //- Construct as copy - inline SubField(const SubField<Type>&); + //- Construct from UList and a (start,size) range. + // The range is subsetted with the list size itself to ensure that the + // result always addresses a valid section of the list. + inline SubField + ( + const UList<Type>& list, + const labelRange& range + ); + + //- Construct from UList and a (start,size) range, but bypassing + //- run-time range checking. + inline SubField + ( + const labelRange& range, + const UList<Type>& list + ); + + //- Copy construct + inline SubField(const SubField<Type>& sfield); - // Member functions + // Member Functions //- Return a null SubField static inline const SubField<Type>& null(); @@ -110,13 +127,13 @@ public: // Member operators - //- Assignment via UList operator. Takes linear time. + //- Copy assign via UList operator. Takes linear time. inline void operator=(const SubField<Type>&); - //- Assignment via UList operator. Takes linear time. + //- Copy assign via UList operator. Takes linear time. inline void operator=(const Field<Type>&); - //- Assignment via UList operator. Takes linear time. + //- Copy assign via UList operator. Takes linear time. template<class Form, direction Ncmpts> inline void operator=(const VectorSpace<Form, Type, Ncmpts>&); diff --git a/src/OpenFOAM/fields/Fields/Field/SubFieldI.H b/src/OpenFOAM/fields/Fields/Field/SubFieldI.H index d0ed35b5dcadd9fb9a9b8323ab67692e24f04bc8..0da0e3c48259cecc2479ba3d371baf4281f91c78 100644 --- a/src/OpenFOAM/fields/Fields/Field/SubFieldI.H +++ b/src/OpenFOAM/fields/Fields/Field/SubFieldI.H @@ -68,6 +68,28 @@ inline Foam::SubField<Type>::SubField {} +template<class Type> +inline Foam::SubField<Type>::SubField +( + const UList<Type>& list, + const labelRange& range +) +: + SubList<Type>(list, range) +{} + + +template<class Type> +inline Foam::SubField<Type>::SubField +( + const labelRange& range, + const UList<Type>& list +) +: + SubList<Type>(range, list) +{} + + template<class Type> inline Foam::SubField<Type>::SubField ( diff --git a/src/OpenFOAM/meshes/polyMesh/globalMeshData/globalIndex.H b/src/OpenFOAM/meshes/polyMesh/globalMeshData/globalIndex.H index c88e69d0db06177d081c3374aec351a300b3beb8..40f8ad1741fc12744a23425d226a79c19aebcae8 100644 --- a/src/OpenFOAM/meshes/polyMesh/globalMeshData/globalIndex.H +++ b/src/OpenFOAM/meshes/polyMesh/globalMeshData/globalIndex.H @@ -31,7 +31,6 @@ Description globalIndex globalFaces(mesh.nFaces()); label globalFacei = globalFaces.toGlobal(facei); - SourceFiles globalIndexI.H globalIndex.C @@ -49,9 +48,10 @@ SourceFiles namespace Foam { -// Forward declaration of friend functions and operators +// Forward declarations class globalIndex; +class labelRange; Istream& operator>>(Istream& is, globalIndex& gi); Ostream& operator<<(Ostream& os, const globalIndex& gi); @@ -76,12 +76,12 @@ public: //- Construct null globalIndex() = default; - //- Construct from local max size. Does communication with default - // communicator and message tag. + //- Construct from local max size. + // Does communication with default communicator and message tag. globalIndex(const label localSize); - //- Construct from local max size. Does communication with given - // communicator and message tag + //- Construct from local max size. + // Does communication with given communicator and message tag globalIndex ( const label localSize, @@ -113,6 +113,9 @@ public: //- My local size inline label localSize() const; + //- Return start/size range of local processor data + inline labelRange range() const; + //- From local to global inline label toGlobal(const label i) const; @@ -132,6 +135,9 @@ public: //- Size of proci data inline label localSize(const label proci) const; + //- Return start/size range of proci data + inline labelRange range(const label proci) const; + //- From local to global on proci inline label toGlobal(const label proci, const label i) const; diff --git a/src/OpenFOAM/meshes/polyMesh/globalMeshData/globalIndexI.H b/src/OpenFOAM/meshes/polyMesh/globalMeshData/globalIndexI.H index 748215938e09f30920ae7fbe18c2650786f39092..801e69957330a38418f710219a7dc2c32fb6b56c 100644 --- a/src/OpenFOAM/meshes/polyMesh/globalMeshData/globalIndexI.H +++ b/src/OpenFOAM/meshes/polyMesh/globalMeshData/globalIndexI.H @@ -24,6 +24,7 @@ License \*---------------------------------------------------------------------------*/ #include "ListOps.H" +#include "labelRange.H" // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * // @@ -65,6 +66,18 @@ inline Foam::label Foam::globalIndex::localSize() const } +inline Foam::labelRange Foam::globalIndex::range(const label proci) const +{ + return labelRange(offsets_[proci], offsets_[proci+1] - offsets_[proci]); +} + + +inline Foam::labelRange Foam::globalIndex::range() const +{ + return range(Pstream::myProcNo()); +} + + inline Foam::label Foam::globalIndex::size() const { return offsets_.last(); @@ -87,7 +100,6 @@ inline Foam::label Foam::globalIndex::toGlobal(const label i) const } -//- Is on local processor inline bool Foam::globalIndex::isLocal(const label proci, const label i) const { return i >= offsets_[proci] && i < offsets_[proci+1]; @@ -100,19 +112,19 @@ inline bool Foam::globalIndex::isLocal(const label i) const } -inline Foam::label Foam::globalIndex::toLocal(const label proci, const label i) -const +inline Foam::label +Foam::globalIndex::toLocal(const label proci, const label i) const { - label localI = i - offsets_[proci]; + const label locali = i - offsets_[proci]; - if (localI < 0 || i >= offsets_[proci+1]) + if (locali < 0 || i >= offsets_[proci+1]) { FatalErrorInFunction << "Global " << i << " does not belong on processor " - << proci << endl << "Offsets:" << offsets_ + << proci << nl << "Offsets:" << offsets_ << abort(FatalError); } - return localI; + return locali; } diff --git a/src/OpenFOAM/primitives/ranges/labelRange/labelRange.C b/src/OpenFOAM/primitives/ranges/labelRange/labelRange.C index 2eb2097918e4438ea225870f53412bc594f286d3..b9b791e31e756285a556af2fee8fd76b81a355aa 100644 --- a/src/OpenFOAM/primitives/ranges/labelRange/labelRange.C +++ b/src/OpenFOAM/primitives/ranges/labelRange/labelRange.C @@ -3,7 +3,7 @@ \\ / F ield | OpenFOAM: The Open Source CFD Toolbox \\ / O peration | \\ / A nd | Copyright (C) 2011 OpenFOAM Foundation - \\/ M anipulation | Copyright (C) 2017 OpenCFD Ltd. + \\/ M anipulation | Copyright (C) 2017-2018 OpenCFD Ltd. ------------------------------------------------------------------------------- License This file is part of OpenFOAM. @@ -24,13 +24,15 @@ License \*---------------------------------------------------------------------------*/ #include "labelRange.H" +#include "List.H" #include "token.H" +#include <numeric> // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * // namespace Foam { -int labelRange::debug(debug::debugSwitch("labelRange", 0)); + int labelRange::debug(debug::debugSwitch("labelRange", 0)); } const Foam::labelRange Foam::labelRange::null; @@ -49,6 +51,20 @@ Foam::labelRange::labelRange(Istream& is) // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * // +Foam::List<Foam::label> Foam::labelRange::labels() const +{ + if (size_ <= 0) + { + return List<label>(); + } + + List<label> result(size_); + std::iota(result.begin(), result.end(), start_); + + return result; +} + + void Foam::labelRange::adjust() noexcept { if (start_ < 0) diff --git a/src/OpenFOAM/primitives/ranges/labelRange/labelRange.H b/src/OpenFOAM/primitives/ranges/labelRange/labelRange.H index 388dc0f2a3718ee22f54937a76900033fd22abf7..8cc65eabead35b352f2fbed0e76b0c1c4092d1c0 100644 --- a/src/OpenFOAM/primitives/ranges/labelRange/labelRange.H +++ b/src/OpenFOAM/primitives/ranges/labelRange/labelRange.H @@ -25,7 +25,7 @@ Class Foam::labelRange Description - An range or interval of labels defined by a start and a size. + A range or interval of labels defined by a start and a size. SourceFiles labelRange.C @@ -46,6 +46,7 @@ namespace Foam class labelRange; class Istream; class Ostream; +template<class T> class List; Istream& operator>>(Istream& is, labelRange& range); Ostream& operator<<(Ostream& os, const labelRange& range); @@ -136,6 +137,9 @@ public: //- Is the range empty? inline bool empty() const noexcept; + //- Return the range as a list of labels + List<label> labels() const; + //- Adjust the start to avoid negative indices. // The size is decreased accordingly, but will never become negative. // Eg, adjusting (-10, 15) becomes (0,5). @@ -204,8 +208,8 @@ public: labelRange subset0(const label size) const; - //- Return const_iterator to element at in the range, with bounds - //- checking. + //- Return const_iterator to element in the range, + //- with bounds checking. // \return iterator at the requested position, or end() if it is // out of bounds inline const_iterator at(const label localIndex) const; @@ -217,7 +221,7 @@ public: inline label operator[](const label localIndex) const noexcept; //- Return true if the (global) value is located within the range. - // Behaviour identical to found(). + // Behaviour identical to found() - usable as a predicate inline bool operator()(const label value) const noexcept; //- Increase the size by 1.