1 /*---------------------------------------------------------------------------*\
3 \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
5 \\ / A nd | Copyright (C) 1991-2009 OpenCFD Ltd.
7 -------------------------------------------------------------------------------
9 This file is part of OpenFOAM.
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 the
13 Free Software Foundation; either version 2 of the License, or (at your
14 option) any later version.
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
21 You should have received a copy of the GNU General Public License
22 along with OpenFOAM; if not, write to the Free Software Foundation,
23 Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
27 \*---------------------------------------------------------------------------*/
31 #include "labelList.H"
32 #include "OStringStream.H"
33 #include "IStringStream.H"
40 #include "processorPolyPatch.H"
47 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
49 // Get list of my processor neighbours
50 labelList procNeighbours(const polyMesh& mesh)
52 word procLabel = '[' + word(name(Pstream::myProcNo())) + "]-";
54 label nNeighbours = 0;
56 forAll (mesh.boundaryMesh(), patchI)
58 if (typeid(mesh.boundaryMesh()[patchI]) == typeid(processorPolyPatch))
64 labelList neighbours(nNeighbours);
68 forAll (mesh.boundaryMesh(), patchI)
70 if (typeid(mesh.boundaryMesh()[patchI]) == typeid(processorPolyPatch))
72 const polyPatch& patch = mesh.boundaryMesh()[patchI];
74 const processorPolyPatch& procPatch =
75 refCast<const processorPolyPatch>(patch);
77 label procId = procPatch.neighbProcNo() - Pstream::firstSlave() + 1;
79 neighbours[nNeighbours++] = procId;
87 // Calculate some average position for mesh.
88 point meshCentre(const polyMesh& mesh)
90 return average(mesh.points());
97 int main(int argc, char *argv[])
99 # include "setRootCase.H"
100 # include "createTime.H"
101 # include "createMesh.H"
103 word procLabel = '[' + word(name(Pstream::myProcNo())) + "]-";
105 if (!Pstream::parRun())
107 FatalErrorIn(args.executable())
108 << "Please run in parallel" << exit(FatalError);
111 // 'Gather' processor-processor topology
112 Gather<labelList> connections
114 static_cast<const labelList&>(procNeighbours(mesh))
117 // Collect centres of individual meshes (for visualization only)
118 Gather<point> meshCentres(meshCentre(mesh));
120 if (!Pstream::master())
127 // At this point we have the connections between processors and the
128 // processor-mesh centres.
131 Info<< "connections:" << connections << endl;
132 Info<< "meshCentres:" << meshCentres << endl;
136 // Dump connections and meshCentres to OBJ file
139 fileName fName("decomposition.obj");
141 Info<< "Writing decomposition to " << fName << endl;
143 OFstream objFile(fName);
145 // Write processors as single vertex in centre of mesh
146 forAll(meshCentres, procI)
148 const point& pt = meshCentres[procI];
150 objFile << "v " << pt.x() << ' ' << pt.y() << ' ' << pt.z() << endl;
152 // Write connections as lines between processors (duplicated)
153 forAll(connections, procI)
155 const labelList& nbs = connections[procI];
159 objFile << "l " << procI + 1 << ' ' << nbs[nbI] + 1 << endl;
165 // Read paths to route from dictionary
168 IFstream dictFile("routerDict");
170 dictionary routeDict(dictFile);
172 labelListList paths(routeDict.lookup("paths"));
177 // Iterate over routing. Route as much as possible during each iteration
178 // and stop if all paths have been routed. No special ordering to maximize
179 // routing during one iteration.
182 boolList routeOk(paths.size(), false);
187 while (nOk < paths.size())
189 Info<< "Iteration:" << iter << endl;
190 Info<< "---------------" << endl;
195 fileName fName("route_" + name(iter) + ".obj");
196 Info<< "Writing route to " << fName << endl;
198 OFstream objFile(fName);
200 forAll(meshCentres, procI)
202 const point& pt = meshCentres[procI];
204 objFile << "v " << pt.x() << ' ' << pt.y() << ' ' << pt.z()
210 router cellRouter(connections, meshCentres);
212 // Try to route as many paths possible during this iteration.
217 const labelList& path = paths[pathI];
219 Info<< "Trying to route path " << pathI
220 << " nodes " << path << endl;
222 routeOk[pathI] = cellRouter.route(path, -(pathI + 1));
224 Info<< "Result of routing:" << routeOk[pathI] << endl;
230 // Dump route as lines.
231 labelList route(cellRouter.getRoute(-(pathI + 1)));
233 for(label elemI = 1; elemI < route.size(); elemI++)
236 << "l " << route[elemI-1]+1 << ' '
237 << route[elemI]+1 << endl;
239 Info<< "route:" << route << endl;
248 Info<< "End\n" << endl;
254 // ************************************************************************* //