globalIndexI.H 5.31 KB
Newer Older
1
2
3
4
/*---------------------------------------------------------------------------*\
  =========                 |
  \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
   \\    /   O peration     |
OpenFOAM bot's avatar
OpenFOAM bot committed
5
    \\  /    A nd           | www.openfoam.com
OpenFOAM bot's avatar
OpenFOAM bot committed
6
7
     \\/     M anipulation  |
-------------------------------------------------------------------------------
OpenFOAM bot's avatar
OpenFOAM bot committed
8
9
    Copyright (C) 2011-2016 OpenFOAM Foundation
    Copyright (C) 2018 OpenCFD Ltd.
10
11
12
13
-------------------------------------------------------------------------------
License
    This file is part of OpenFOAM.

14
15
16
17
    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.
18
19
20
21
22
23
24

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

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

#include "ListOps.H"
30
#include "labelRange.H"
31

32
33
// * * * * * * * * * * * * * * * * Constructors  * * * * * * * * * * * * * * //

34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
inline Foam::globalIndex::globalIndex(const label localSize)
:
    globalIndex()
{
    reset(localSize);
}


inline Foam::globalIndex::globalIndex
(
    const label localSize,
    const int tag,
    const label comm,
    const bool parallel
)
:
    globalIndex()
{
    reset(localSize, tag, comm, parallel);
}


56
57
58
inline Foam::globalIndex::globalIndex(const labelUList& offsets)
:
    offsets_(offsets)
59
60
61
{}


62
inline Foam::globalIndex::globalIndex(labelList&& offsets)
63
:
64
    offsets_(std::move(offsets))
65
66
67
{}


68
69
// * * * * * * * * * * * * * * * Member Functions  * * * * * * * * * * * * * //

70
71
72
73
74
75
76
77
78
79
80
81
inline bool Foam::globalIndex::empty() const
{
    return offsets_.empty() || offsets_.last() == 0;
}


inline const Foam::labelList& Foam::globalIndex::offsets() const
{
    return offsets_;
}


82
83
84
85
86
87
inline Foam::labelList& Foam::globalIndex::offsets()
{
    return offsets_;
}


88
89
90
91
92
93
94
95
96
97
98
99
inline Foam::label Foam::globalIndex::size() const
{
    return offsets_.empty() ? 0 : offsets_.last();
}


inline void Foam::globalIndex::reset(const label localSize)
{
    reset(localSize, Pstream::msgType(), 0, true);
}


100
inline Foam::label Foam::globalIndex::offset(const label proci) const
101
{
102
    return offsets_[proci];
103
104
105
}


106
107
108
109
110
111
112
113
114
115
116
117
inline Foam::label Foam::globalIndex::localStart(const label proci) const
{
    return offsets_[proci];
}


inline Foam::label Foam::globalIndex::localStart() const
{
    return localStart(Pstream::myProcNo());
}


118
inline Foam::label Foam::globalIndex::localSize(const label proci) const
mattijs's avatar
mattijs committed
119
{
120
    return offsets_[proci+1] - offsets_[proci];
mattijs's avatar
mattijs committed
121
122
123
}


mattijs's avatar
mattijs committed
124
125
126
127
128
129
inline Foam::label Foam::globalIndex::localSize() const
{
    return localSize(Pstream::myProcNo());
}


130
131
132
133
134
135
136
137
138
139
140
141
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());
}


142
143
144
145
146
147
148
149
150
151
152
153
inline bool Foam::globalIndex::isLocal(const label proci, const label i) const
{
    return i >= offsets_[proci] && i < offsets_[proci+1];
}


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


154
155
inline Foam::label Foam::globalIndex::toGlobal
(
156
    const label proci,
157
158
159
    const label i
) const
{
160
    return i + offsets_[proci];
161
162
163
}


164
165
inline Foam::label Foam::globalIndex::toGlobal(const label i) const
{
166
    return toGlobal(Pstream::myProcNo(), i);
167
168
}

169

170
171
172
173
174
inline Foam::labelList Foam::globalIndex::toGlobal
(
    const label proci,
    const labelUList& labels
) const
175
{
176
177
178
179
    labelList result(labels);
    inplaceToGlobal(proci, result);

    return result;
180
181
182
}


183
184
185
186
inline Foam::labelList Foam::globalIndex::toGlobal
(
    const labelUList& labels
) const
187
{
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
    return toGlobal(Pstream::myProcNo(), labels);
}


inline void Foam::globalIndex::inplaceToGlobal
(
    const label proci,
    labelList& labels
) const
{
    const label off = offsets_[proci];

    for (label& val : labels)
    {
        val += off;
    }
}


inline void Foam::globalIndex::inplaceToGlobal(labelList& labels) const
{
    inplaceToGlobal(Pstream::myProcNo(), labels);
210
211
212
}


213
214
inline Foam::label
Foam::globalIndex::toLocal(const label proci, const label i) const
215
{
216
    const label locali = i - offsets_[proci];
217

218
    if (locali < 0 || i >= offsets_[proci+1])
219
    {
220
        FatalErrorInFunction
221
            << "Global " << i << " does not belong on processor "
222
            << proci << nl << "Offsets:" << offsets_
223
224
            << abort(FatalError);
    }
225
    return locali;
226
227
228
229
230
231
232
233
234
235
236
}


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
237
    if (i < 0 || i >= size())
238
    {
239
        FatalErrorInFunction
240
241
242
243
244
            << "Global " << i << " does not belong on any processor."
            << " Offsets:" << offsets_
            << abort(FatalError);
    }

mattijs's avatar
mattijs committed
245
    return findLower(offsets_, i+1);
246
247
248
249
}


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