globalIndexI.H 3.66 KB
Newer Older
1 2 3 4
/*---------------------------------------------------------------------------*\
  =========                 |
  \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
   \\    /   O peration     |
5
    \\  /    A nd           | Copyright (C) 2011-2016 OpenFOAM Foundation
6 7 8 9 10
     \\/     M anipulation  |
-------------------------------------------------------------------------------
License
    This file is part of OpenFOAM.

11 12 13 14
    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.
15 16 17 18 19 20 21

    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
22
    along with OpenFOAM.  If not, see <http://www.gnu.org/licenses/>.
23 24 25 26

\*---------------------------------------------------------------------------*/

#include "ListOps.H"
27
#include "labelRange.H"
28

29 30
// * * * * * * * * * * * * * * * * Constructors  * * * * * * * * * * * * * * //

31 32 33
inline Foam::globalIndex::globalIndex(const labelUList& offsets)
:
    offsets_(offsets)
34 35 36
{}


37
inline Foam::globalIndex::globalIndex(labelList&& offsets)
38
:
39
    offsets_(std::move(offsets))
40 41 42
{}


43 44
// * * * * * * * * * * * * * * * Member Functions  * * * * * * * * * * * * * //

45 46 47 48 49 50
inline Foam::labelList& Foam::globalIndex::offsets()
{
    return offsets_;
}


51
inline Foam::label Foam::globalIndex::offset(const label proci) const
52
{
53
    return offsets_[proci];
54 55 56
}


57
inline Foam::label Foam::globalIndex::localSize(const label proci) const
mattijs's avatar
mattijs committed
58
{
59
    return offsets_[proci+1] - offsets_[proci];
mattijs's avatar
mattijs committed
60 61 62
}


mattijs's avatar
mattijs committed
63 64 65 66 67 68
inline Foam::label Foam::globalIndex::localSize() const
{
    return localSize(Pstream::myProcNo());
}


69 70 71 72 73 74 75 76 77 78 79 80
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());
}


81 82
inline Foam::label Foam::globalIndex::size() const
{
83
    return offsets_.last();
84 85 86
}


87 88
inline Foam::label Foam::globalIndex::toGlobal
(
89
    const label proci,
90 91 92
    const label i
) const
{
93
    return i + offsets_[proci];
94 95 96
}


97 98
inline Foam::label Foam::globalIndex::toGlobal(const label i) const
{
99
    return toGlobal(Pstream::myProcNo(), i);
100 101
}

102

103
inline bool Foam::globalIndex::isLocal(const label proci, const label i) const
104
{
105
    return i >= offsets_[proci] && i < offsets_[proci+1];
106 107 108 109 110 111
}


inline bool Foam::globalIndex::isLocal(const label i) const
{
    return isLocal(Pstream::myProcNo(), i);
112 113 114
}


115 116
inline Foam::label
Foam::globalIndex::toLocal(const label proci, const label i) const
117
{
118
    const label locali = i - offsets_[proci];
119

120
    if (locali < 0 || i >= offsets_[proci+1])
121
    {
122
        FatalErrorInFunction
123
            << "Global " << i << " does not belong on processor "
124
            << proci << nl << "Offsets:" << offsets_
125 126
            << abort(FatalError);
    }
127
    return locali;
128 129 130 131 132 133 134 135 136 137 138
}


inline Foam::label Foam::globalIndex::toLocal(const label i) const
{
    return toLocal(Pstream::myProcNo(), i);
}


inline Foam::label Foam::globalIndex::whichProcID(const label i) const
{
mattijs's avatar
mattijs committed
139
    if (i < 0 || i >= size())
140
    {
141
        FatalErrorInFunction
142 143 144 145 146
            << "Global " << i << " does not belong on any processor."
            << " Offsets:" << offsets_
            << abort(FatalError);
    }

mattijs's avatar
mattijs committed
147
    return findLower(offsets_, i+1);
148 149 150 151
}


// ************************************************************************* //