From 609fb366e3e7e159d453ed9dc5801bfc49623b6e Mon Sep 17 00:00:00 2001 From: Mark Olesen Date: Fri, 15 Oct 2021 14:42:21 +0200 Subject: [PATCH] ENH: improve flexibilty of globalIndex - construct or reset from a list of local sizes. It is generally easier and safer to assemble sizes and let globalIndex determine the corresponding offsets, when working with raw values. - Can use reset() from sizes or fine-tune offsets with setLocalSize() instead of using the potentially more fragile non-const access to the offsets. - add globalIndex const_iterator to iterate across the access ranges. This is makes it simpler to use with the List slice() method to access or operate on a sub-section of list. For example, scalarField allValues = ...; globalIndex procAccess = ...; for (const labelRange& range : procAccess) { someOutput(allValues.slice(range)); } --- .../polyMesh/globalMeshData/globalIndex.C | 200 ++++++++++++++-- .../polyMesh/globalMeshData/globalIndex.H | 181 ++++++++++++-- .../polyMesh/globalMeshData/globalIndexI.H | 221 ++++++++++++++++-- .../globalMeshData/globalIndexTemplates.C | 40 ++++ 4 files changed, 577 insertions(+), 65 deletions(-) diff --git a/src/OpenFOAM/meshes/polyMesh/globalMeshData/globalIndex.C b/src/OpenFOAM/meshes/polyMesh/globalMeshData/globalIndex.C index 957725dee6..434bd4200a 100644 --- a/src/OpenFOAM/meshes/polyMesh/globalMeshData/globalIndex.C +++ b/src/OpenFOAM/meshes/polyMesh/globalMeshData/globalIndex.C @@ -27,6 +27,84 @@ License \*---------------------------------------------------------------------------*/ #include "globalIndex.H" +#include "labelRange.H" + +// * * * * * * * * * * * * * Static Member Functions * * * * * * * * * * * * // + +Foam::labelList +Foam::globalIndex::calcOffsets +( + const labelUList& localSizes, + const bool checkOverflow +) +{ + labelList values; + + const label len = localSizes.size(); + + if (len) + { + values.resize(len+1); + + label start = 0; + for (label i = 0; i < len; ++i) + { + values[i] = start; + start += localSizes[i]; + + if (checkOverflow && start < values[i]) + { + FatalErrorInFunction + << "Overflow : sum of sizes exceeds labelMax (" + << labelMax << ") after index " << i << " of " + << flatOutput(localSizes) << nl + << "Please recompile with larger datatype for label." << nl + << exit(FatalError); + } + } + values[len] = start; + } + + return values; +} + + +Foam::List +Foam::globalIndex::calcRanges +( + const labelUList& localSizes, + const bool checkOverflow +) +{ + List values; + + const label len = localSizes.size(); + + if (len) + { + values.resize(len); + + label start = 0; + for (label i = 0; i < len; ++i) + { + values[i].reset(start, localSizes[i]); + start += localSizes[i]; + + if (checkOverflow && start < values[i].start()) + { + FatalErrorInFunction + << "Overflow : sum of sizes exceeds labelMax (" + << labelMax << ") after index " << i << " of " + << flatOutput(localSizes) << nl + << "Please recompile with larger datatype for label." << nl + << exit(FatalError); + } + } + } + + return values; +} + // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * // @@ -52,8 +130,8 @@ void Foam::globalIndex::bin bins.m() = UIndirectList