Test-processorRouter.C
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) 2011-2015 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 Description
25 
26 \*---------------------------------------------------------------------------*/
27 
28 #include "argList.H"
29 #include "label.H"
30 #include "labelList.H"
31 #include "OStringStream.H"
32 #include "IStringStream.H"
33 #include "OFstream.H"
34 #include "IFstream.H"
35 #include "point.H"
36 #include "Time.H"
37 #include "fvMesh.H"
38 #include "router.H"
39 #include "processorPolyPatch.H"
40 #include "typeInfo.H"
41 #include "Gather.H"
42 
43 
44 using namespace Foam;
45 
46 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
47 
48 // Get list of my processor neighbours
50 {
51  word procLabel = '[' + word(name(Pstream::myProcNo())) + "]-";
52 
53  label nNeighbours = 0;
54 
55  forAll(mesh.boundaryMesh(), patchI)
56  {
57  if (isA<processorPolyPatch>(mesh.boundaryMesh()[patchI]))
58  {
59  nNeighbours++;
60  }
61  }
62 
63  labelList neighbours(nNeighbours);
64 
65  nNeighbours = 0;
66 
67  forAll(mesh.boundaryMesh(), patchI)
68  {
69  if (isA<processorPolyPatch>(mesh.boundaryMesh()[patchI]))
70  {
71  const polyPatch& patch = mesh.boundaryMesh()[patchI];
72 
73  const processorPolyPatch& procPatch =
74  refCast<const processorPolyPatch>(patch);
75 
76  label procId = procPatch.neighbProcNo() - Pstream::firstSlave() + 1;
77 
78  neighbours[nNeighbours++] = procId;
79  }
80  }
81 
82  return neighbours;
83 }
84 
85 
86 // Calculate some average position for mesh.
88 {
89  return average(mesh.points());
90 }
91 
92 
93 // Main program:
94 
95 
96 int main(int argc, char *argv[])
97 {
98  #include "setRootCase.H"
99  #include "createTime.H"
100  #include "createMesh.H"
101 
102  word procLabel = '[' + word(name(Pstream::myProcNo())) + "]-";
103 
104  if (!Pstream::parRun())
105  {
107  << "Please run in parallel" << exit(FatalError);
108  }
109 
110  // 'Gather' processor-processor topology
111  Gather<labelList> connections
112  (
113  static_cast<const labelList&>(procNeighbours(mesh))
114  );
115 
116  // Collect centres of individual meshes (for visualization only)
117  Gather<point> meshCentres(meshCentre(mesh));
118 
119  if (!Pstream::master())
120  {
121  return 0;
122  }
123 
124 
125  //
126  // At this point we have the connections between processors and the
127  // processor-mesh centres.
128  //
129 
130  Info<< "connections:" << connections << endl;
131  Info<< "meshCentres:" << meshCentres << endl;
132 
133 
134  //
135  // Dump connections and meshCentres to OBJ file
136  //
137 
138  fileName fName("decomposition.obj");
139 
140  Info<< "Writing decomposition to " << fName << endl;
141 
142  OFstream objFile(fName);
143 
144  // Write processors as single vertex in centre of mesh
145  forAll(meshCentres, procI)
146  {
147  const point& pt = meshCentres[procI];
148 
149  objFile << "v " << pt.x() << ' ' << pt.y() << ' ' << pt.z() << endl;
150  }
151  // Write connections as lines between processors (duplicated)
152  forAll(connections, procI)
153  {
154  const labelList& nbs = connections[procI];
155 
156  forAll(nbs, nbI)
157  {
158  objFile << "l " << procI + 1 << ' ' << nbs[nbI] + 1 << endl;
159  }
160  }
161 
162 
163  //
164  // Read paths to route from dictionary
165  //
166 
167  IFstream dictFile("routerDict");
168 
169  dictionary routeDict(dictFile);
170 
171  labelListList paths(routeDict.lookup("paths"));
172 
173 
174 
175  //
176  // Iterate over routing. Route as much as possible during each iteration
177  // and stop if all paths have been routed. No special ordering to maximize
178  // routing during one iteration.
179  //
180 
181  boolList routeOk(paths.size(), false);
182  label nOk = 0;
183 
184  label iter = 0;
185 
186  while (nOk < paths.size())
187  {
188  Info<< "Iteration:" << iter << endl;
189  Info<< "---------------" << endl;
190 
191 
192  // Dump to OBJ file
193 
194  fileName fName("route_" + name(iter) + ".obj");
195  Info<< "Writing route to " << fName << endl;
196 
197  OFstream objFile(fName);
198 
199  forAll(meshCentres, procI)
200  {
201  const point& pt = meshCentres[procI];
202 
203  objFile << "v " << pt.x() << ' ' << pt.y() << ' ' << pt.z()
204  << endl;
205  }
206 
207 
208  // Router
209  router cellRouter(connections, meshCentres);
210 
211  // Try to route as many paths possible during this iteration.
212  forAll(paths, pathI)
213  {
214  if (!routeOk[pathI])
215  {
216  const labelList& path = paths[pathI];
217 
218  Info<< "Trying to route path " << pathI
219  << " nodes " << path << endl;
220 
221  routeOk[pathI] = cellRouter.route(path, -(pathI + 1));
222 
223  Info<< "Result of routing:" << routeOk[pathI] << endl;
224 
225  if (routeOk[pathI])
226  {
227  nOk++;
228 
229  // Dump route as lines.
230  labelList route(cellRouter.getRoute(-(pathI + 1)));
231 
232  for (label elemI = 1; elemI < route.size(); elemI++)
233  {
234  objFile
235  << "l " << route[elemI-1]+1 << ' '
236  << route[elemI]+1 << endl;
237  }
238  Info<< "route:" << route << endl;
239  }
240  }
241  }
242  Info<< endl;
243 
244  iter++;
245  }
246 
247  Info<< "End\n" << endl;
248 
249  return 0;
250 }
251 
252 
253 // ************************************************************************* //
Foam::polyMesh::points
virtual const pointField & points() const
Return raw points.
Definition: polyMesh.C:979
Foam::word
A class for handling words, derived from string.
Definition: word.H:59
Foam::fileName
A class for handling file names.
Definition: fileName.H:69
typeInfo.H
forAll
#define forAll(list, i)
Loop across all elements in list.
Definition: UList.H:406
Foam::IFstream
Input from file stream.
Definition: IFstream.H:81
Foam::router::getRoute
labelList getRoute(const label pathValue) const
Extract labels of route with given value.
Definition: router.C:369
point.H
Foam::UPstream::parRun
static bool & parRun()
Is this a parallel run?
Definition: UPstream.H:377
Foam::dictionary::lookup
ITstream & lookup(const word &, bool recursive=false, bool patternMatch=true) const
Find and return an entry data stream.
Definition: dictionary.C:449
Foam::router::route
bool route(const labelList &path, const label pathValue)
Find path from first element in path to all other elements.
Definition: router.C:282
router.H
Foam::processorPolyPatch::neighbProcNo
int neighbProcNo() const
Return neigbour processor number.
Definition: processorPolyPatch.H:255
Foam::polyMesh::boundaryMesh
const polyBoundaryMesh & boundaryMesh() const
Return boundary mesh.
Definition: polyMesh.H:421
Foam::endl
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:251
Foam::polyMesh
Mesh consisting of general polyhedral cells.
Definition: polyMesh.H:74
IStringStream.H
main
int main(int argc, char *argv[])
Definition: Test-processorRouter.C:96
OFstream.H
Foam::label
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
labelList.H
Foam::Info
messageStream Info
Foam::polyPatch
A patch is a list of labels that address the faces in the global face list.
Definition: polyPatch.H:66
argList.H
OStringStream.H
Foam::processorPolyPatch
Neighbour processor patch.
Definition: processorPolyPatch.H:55
IFstream.H
procNeighbours
labelList procNeighbours(const polyMesh &mesh)
Definition: Test-processorRouter.C:49
Foam::Vector::x
const Cmpt & x() const
Definition: VectorI.H:65
Foam::router
Lee's PCB routing algorithm. Construct with list of connections between nodes (i.e....
Definition: router.H:99
Foam::FatalError
error FatalError
Foam::dictionary
A list of keyword definitions, which are a keyword followed by any number of values (e....
Definition: dictionary.H:137
processorPolyPatch.H
mesh
dynamicFvMesh & mesh
Definition: createDynamicFvMesh.H:18
Foam::UPstream::firstSlave
static int firstSlave()
Process index of first slave.
Definition: UPstream.H:422
fvMesh.H
Foam
Namespace for OpenFOAM.
Definition: combustionModel.C:30
Foam::Vector::z
const Cmpt & z() const
Definition: VectorI.H:77
Foam::exit
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:124
Foam::OFstream
Output to file stream.
Definition: OFstream.H:81
Foam::UPstream::myProcNo
static int myProcNo(const label communicator=0)
Number of this process (starting from masterNo() = 0)
Definition: UPstream.H:405
Foam::UPstream::master
static bool master(const label communicator=0)
Am I the master process.
Definition: UPstream.H:399
meshCentre
point meshCentre(const polyMesh &mesh)
Definition: Test-processorRouter.C:87
setRootCase.H
FatalErrorInFunction
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:318
Foam::Vector< scalar >
label.H
Foam::List
A 1D array of objects of type <T>, where the size of the vector is known and used for subscript bound...
Definition: HashTable.H:59
createMesh.H
path
fileName path(UMean.rootPath()/UMean.caseName()/"graphs"/UMean.instance())
createTime.H
Gather.H
Foam::Vector::y
const Cmpt & y() const
Definition: VectorI.H:71
Foam::List::size
void size(const label)
Override size to be inconsistent with allocated storage.
Foam::Gather
Gather data from all processors onto all processors.
Definition: Gather.H:51
Foam::name
word name(const complex &)
Return a string representation of a complex.
Definition: complex.C:47
Foam::average
dimensioned< Type > average(const DimensionedField< Type, GeoMesh > &df)
Definition: DimensionedFieldFunctions.C:335