1 /*---------------------------------------------------------------------------*\
3 \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
5 \\ / A nd | Copyright (C) 2011 OpenFOAM Foundation
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
13 the Free Software Foundation, either version 3 of the License, or
14 (at your 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, see <http://www.gnu.org/licenses/>.
26 \*---------------------------------------------------------------------------*/
30 #include "labelList.H"
31 #include "OStringStream.H"
32 #include "IStringStream.H"
39 #include "processorPolyPatch.H"
46 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
48 // Get list of my processor neighbours
49 labelList procNeighbours(const polyMesh& mesh)
51 word procLabel = '[' + word(name(Pstream::myProcNo())) + "]-";
53 label nNeighbours = 0;
55 forAll(mesh.boundaryMesh(), patchI)
57 if (isA<processorPolyPatch>(mesh.boundaryMesh()[patchI]))
63 labelList neighbours(nNeighbours);
67 forAll(mesh.boundaryMesh(), patchI)
69 if (isA<processorPolyPatch>(mesh.boundaryMesh()[patchI]))
71 const polyPatch& patch = mesh.boundaryMesh()[patchI];
73 const processorPolyPatch& procPatch =
74 refCast<const processorPolyPatch>(patch);
76 label procId = procPatch.neighbProcNo() - Pstream::firstSlave() + 1;
78 neighbours[nNeighbours++] = procId;
86 // Calculate some average position for mesh.
87 point meshCentre(const polyMesh& mesh)
89 return average(mesh.points());
96 int main(int argc, char *argv[])
98 # include "setRootCase.H"
99 # include "createTime.H"
100 # include "createMesh.H"
102 word procLabel = '[' + word(name(Pstream::myProcNo())) + "]-";
104 if (!Pstream::parRun())
106 FatalErrorIn(args.executable())
107 << "Please run in parallel" << exit(FatalError);
110 // 'Gather' processor-processor topology
111 Gather<labelList> connections
113 static_cast<const labelList&>(procNeighbours(mesh))
116 // Collect centres of individual meshes (for visualization only)
117 Gather<point> meshCentres(meshCentre(mesh));
119 if (!Pstream::master())
126 // At this point we have the connections between processors and the
127 // processor-mesh centres.
130 Info<< "connections:" << connections << endl;
131 Info<< "meshCentres:" << meshCentres << endl;
135 // Dump connections and meshCentres to OBJ file
138 fileName fName("decomposition.obj");
140 Info<< "Writing decomposition to " << fName << endl;
142 OFstream objFile(fName);
144 // Write processors as single vertex in centre of mesh
145 forAll(meshCentres, procI)
147 const point& pt = meshCentres[procI];
149 objFile << "v " << pt.x() << ' ' << pt.y() << ' ' << pt.z() << endl;
151 // Write connections as lines between processors (duplicated)
152 forAll(connections, procI)
154 const labelList& nbs = connections[procI];
158 objFile << "l " << procI + 1 << ' ' << nbs[nbI] + 1 << endl;
164 // Read paths to route from dictionary
167 IFstream dictFile("routerDict");
169 dictionary routeDict(dictFile);
171 labelListList paths(routeDict.lookup("paths"));
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.
181 boolList routeOk(paths.size(), false);
186 while (nOk < paths.size())
188 Info<< "Iteration:" << iter << endl;
189 Info<< "---------------" << endl;
194 fileName fName("route_" + name(iter) + ".obj");
195 Info<< "Writing route to " << fName << endl;
197 OFstream objFile(fName);
199 forAll(meshCentres, procI)
201 const point& pt = meshCentres[procI];
203 objFile << "v " << pt.x() << ' ' << pt.y() << ' ' << pt.z()
209 router cellRouter(connections, meshCentres);
211 // Try to route as many paths possible during this iteration.
216 const labelList& path = paths[pathI];
218 Info<< "Trying to route path " << pathI
219 << " nodes " << path << endl;
221 routeOk[pathI] = cellRouter.route(path, -(pathI + 1));
223 Info<< "Result of routing:" << routeOk[pathI] << endl;
229 // Dump route as lines.
230 labelList route(cellRouter.getRoute(-(pathI + 1)));
232 for (label elemI = 1; elemI < route.size(); elemI++)
235 << "l " << route[elemI-1]+1 << ' '
236 << route[elemI]+1 << endl;
238 Info<< "route:" << route << endl;
247 Info<< "End\n" << endl;
253 // ************************************************************************* //