FreeFOAM The Cross-Platform CFD Toolkit
ListListOps.H
Go to the documentation of this file.
1 /*---------------------------------------------------------------------------*\
2  ========= |
3  \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
4  \\ / O peration |
5  \\ / A nd | Copyright (C) 1991-2010 OpenCFD Ltd.
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, minimizing 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 <OpenFOAM/labelList.H>
90 
91 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
92 
93 namespace Foam
94 {
95 //
96 //template <class T> class accessOp;
97 //template <class T> class offsetOp;
98 // Dummy access operator for combine()
99 template <class T>
100 class accessOp
101 {
102 public:
104  const T& operator()(const T& x) const
105  {
106  return x;
107  }
108 };
109 
110 
111 // Offset operator for combineOffset()
112 template <class T>
113 class offsetOp
114 {
115 public:
117  T operator()(const T& x, const label offset) const
118  {
119  return x + offset;
120  }
121 };
122 
123 /*---------------------------------------------------------------------------*\
124  Class ListListOps Declaration
125 \*---------------------------------------------------------------------------*/
126 
127 namespace ListListOps
128 {
129 
130  //- Combines sublists into one big list
131  template <class AccessType, class T, class AccessOp>
132  AccessType combine(const List<T>&, AccessOp aop = accessOp<T>());
133 
134  //- Gets sizes of sublists
135  template <class T, class AccessOp>
136  labelList subSizes(const List<T>&, AccessOp aop = accessOp<T>());
137 
138  //- Like combine but also offsets sublists based on passed sizes
139  template <class AccessType, class T, class AccessOp, class OffsetOp>
140  AccessType combineOffset
141  (
142  const List<T>&,
143  const labelList& sizes,
144  AccessOp aop,
145  OffsetOp oop = offsetOp<T>()
146  );
147 };
148 
149 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
150 
151 } // End namespace Foam
152 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
153 
154 #ifdef NoRepository
155 # include <OpenFOAM/ListListOps.C>
156 #endif
157 
158 
159 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
160 
161 #endif
162 
163 // ************************ vim: set sw=4 sts=4 et: ************************ //