ListListOps.H
Go to the documentation of this file.
1 /*---------------------------------------------------------------------------*\
2  ========= |
3  \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
4  \\ / O peration | Website: https://openfoam.org
5  \\ / A nd | Copyright (C) 2011-2021 OpenFOAM Foundation
6  \\/ M anipulation |
7 -------------------------------------------------------------------------------
8 License
9  This file is part of OpenFOAM.
10 
11  OpenFOAM is free software: you can redistribute it and/or modify it
12  under the terms of the GNU General Public License as published by
13  the Free Software Foundation, either version 3 of the License, or
14  (at your option) any later version.
15 
16  OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
17  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
18  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
19  for more details.
20 
21  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 Namespace
25  Foam::ListListOps
26 
27 Description
28  Various utility functions to work on Lists of Lists (usually resulting
29  from 'gather'ing and combining information from individual processors)
30 
31  - combine : \n
32  takes (elements of) sublists and appends them into one big list.
33  - combineOffset : \n
34  similar and also adds offset.
35 
36  The access of data is through an AccessOp so that data can be 'gather'ed
37  in one go, minimising communication, and then picked apart and recombined.
38 
39  Example:
40  \code
41  // Assuming myContainer defined which holds all the data I want to
42  // transfer (say a pointField and a faceList). myContainer also defines
43  // access operators to
44  // access the individual elements, say myContainerPoints::operator(),
45  // and myContainerFaces::operator()
46 
47  List<myContainer> gatheredData(Pstream::nProcs());
48  gatheredData[Pstream::myProcNo()] = myContainer(points, faces);
49 
50  // Gather data onto master
51  Pstream::gatherList(gatheredData);
52 
53  // Combine
54  pointField combinedPoints
55  (
56  ListListOps::combine<pointField>
57  (
58  gatheredData,
59  myContainerPoints()
60  )
61  );
62 
63  // Combine and renumber (so combinedFaces indexes combinedPoints)
64 
65  // Extract sizes of individual lists
66  labelList sizes
67  (
68  ListListOps::subSizes(gatheredData, myContainerPoints())
69  );
70 
71  // Renumber using user-defined operator offsetOp<face>()
72  faceList combinedFaces
73  (
74  ListListOps::combineOffset<faceList>
75  (
76  gatheredData, sizes, myContainerFaces(), offsetOp<face>()
77  )
78  );
79  \endcode
80 
81 SourceFiles
82  ListListOps.C
83 
84 \*---------------------------------------------------------------------------*/
85 
86 #ifndef ListListOps_H
87 #define ListListOps_H
88 
89 #include "labelList.H"
90 
91 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
92 
93 namespace Foam
94 {
95 
96 // Dummy access operator for ListListOps::combine()
97 template<class T>
98 class accessOp
99 {
100 public:
101 
102  const T& operator()(const T& x) const
103  {
104  return x;
105  }
106 };
107 
108 
109 // Offset operator for ListListOps::combineOffset()
110 template<class T>
111 class offsetOp
112 {
113 public:
114 
115  T operator()(const T& x, const label offset) const
116  {
117  return x + offset;
118  }
119 };
120 
121 /*---------------------------------------------------------------------------*\
122  Class ListListOps Declaration
123 \*---------------------------------------------------------------------------*/
124 
125 namespace ListListOps
126 {
127 
128  //- Combines sublists into one big list
129  template<class AccessType, class T, class AccessOp>
130  AccessType combine(const List<T>&, AccessOp aop = accessOp<T>());
131 
132  //- Gets sizes of sublists
133  template<class T, class AccessOp>
134  labelList subSizes(const List<T>&, AccessOp aop = accessOp<T>());
135 
136  //- Like combine but also offsets sublists based on passed sizes
137  template<class AccessType, class T, class AccessOp, class OffsetOp>
138  AccessType combineOffset
139  (
140  const List<T>&,
141  const labelList& sizes,
142  AccessOp aop,
143  OffsetOp oop = offsetOp<T>()
144  );
145 };
146 
147 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
148 
149 } // End namespace Foam
150 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
151 
152 #ifdef NoRepository
153  #include "ListListOps.C"
154 #endif
155 
156 
157 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
158 
159 #endif
160 
161 // ************************************************************************* //
const T & operator()(const T &x) const
Definition: ListListOps.H:102
T operator()(const T &x, const label offset) const
Definition: ListListOps.H:115
AccessType combineOffset(const List< T > &, const labelList &sizes, AccessOp aop, OffsetOp oop=offsetOp< T >())
Like combine but also offsets sublists based on passed sizes.
Definition: ListListOps.C:75
AccessType combine(const List< T > &, AccessOp aop=accessOp< T >())
Combines sublists into one big list.
Definition: ListListOps.C:34
labelList subSizes(const List< T > &, AccessOp aop=accessOp< T >())
Gets sizes of sublists.
Definition: ListListOps.C:61
Namespace for OpenFOAM.
List< label > labelList
A List of labels.
Definition: labelList.H:56
intWM_LABEL_SIZE_t label
A label is an int32_t or int64_t as specified by the pre-processor macro WM_LABEL_SIZE.
Definition: label.H:59
void offset(label &lst, const label o)
void T(FieldField< Field, Type > &f1, const FieldField< Field, Type > &f2)