initial commit for version 1.6.x patch release
[OpenFOAM-1.6.x.git] / applications / test / router / processorRouter.C
blobaac34dff23e0cac9b572f2c6add9f213744db0b3
1 /*---------------------------------------------------------------------------*\
2   =========                 |
3   \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
4    \\    /   O peration     |
5     \\  /    A nd           | Copyright (C) 1991-2009 OpenCFD Ltd.
6      \\/     M anipulation  |
7 -------------------------------------------------------------------------------
8 License
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
19     for more details.
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
25 Description
27 \*---------------------------------------------------------------------------*/
29 #include "argList.H"
30 #include "label.H"
31 #include "labelList.H"
32 #include "OStringStream.H"
33 #include "IStringStream.H"
34 #include "OFstream.H"
35 #include "IFstream.H"
36 #include "point.H"
37 #include "Time.H"
38 #include "fvMesh.H"
39 #include "router.H"
40 #include "processorPolyPatch.H"
41 #include "typeInfo.H"
42 #include "Gather.H"
45 using namespace Foam;
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)
57     {
58         if (typeid(mesh.boundaryMesh()[patchI]) == typeid(processorPolyPatch))
59         {
60             nNeighbours++;
61         }
62     }
64     labelList neighbours(nNeighbours);
66     nNeighbours = 0;
68     forAll (mesh.boundaryMesh(), patchI)
69     {
70         if (typeid(mesh.boundaryMesh()[patchI]) == typeid(processorPolyPatch))
71         {
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;
80         }
81     }
83     return neighbours;
87 // Calculate some average position for mesh.
88 point meshCentre(const polyMesh& mesh)
90     return average(mesh.points());
94 // Main program:
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())
106     {
107         FatalErrorIn(args.executable())
108             << "Please run in parallel" << exit(FatalError);
109     }
111     // 'Gather' processor-processor topology
112     Gather<labelList> connections
113     (
114         static_cast<const labelList&>(procNeighbours(mesh))
115     );
117     // Collect centres of individual meshes (for visualization only)
118     Gather<point> meshCentres(meshCentre(mesh));
120     if (!Pstream::master())
121     {
122         return 0;
123     }
126     //
127     // At this point we have the connections between processors and the
128     // processor-mesh centres.
129     //
131     Info<< "connections:" << connections << endl;
132     Info<< "meshCentres:" << meshCentres << endl;
135     //
136     // Dump connections and meshCentres to OBJ file
137     //
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)
147     {
148         const point& pt = meshCentres[procI];
150         objFile << "v " << pt.x() << ' ' << pt.y() << ' ' << pt.z() << endl;
151     }
152     // Write connections as lines between processors (duplicated)
153     forAll(connections, procI)
154     {
155         const labelList& nbs = connections[procI];
157         forAll(nbs, nbI)
158         {
159             objFile << "l " << procI + 1 << ' ' << nbs[nbI] + 1 << endl;
160         }
161     }
164     //
165     // Read paths to route from dictionary
166     //
168     IFstream dictFile("routerDict");
170     dictionary routeDict(dictFile);
172     labelListList paths(routeDict.lookup("paths"));
176     //
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.
180     //
182     boolList routeOk(paths.size(), false);
183     label nOk = 0;
185     label iter = 0;
187     while (nOk < paths.size())
188     {
189         Info<< "Iteration:" << iter << endl;
190         Info<< "---------------" << endl;
193         // Dump to OBJ file
195         fileName fName("route_" + name(iter) + ".obj");
196         Info<< "Writing route to " << fName << endl;
198         OFstream objFile(fName);
200         forAll(meshCentres, procI)
201         {
202             const point& pt = meshCentres[procI];
204             objFile << "v " << pt.x() << ' ' << pt.y() << ' ' << pt.z()
205                     << endl;
206         }
209         // Router
210         router cellRouter(connections, meshCentres);
212         // Try to route as many paths possible during this iteration.
213         forAll(paths, pathI)
214         {
215             if (!routeOk[pathI])
216             {
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;
226                 if (routeOk[pathI])
227                 {
228                     nOk++;
230                     // Dump route as lines.
231                     labelList route(cellRouter.getRoute(-(pathI + 1)));
233                     for(label elemI = 1; elemI < route.size(); elemI++)
234                     {
235                         objFile
236                             << "l " << route[elemI-1]+1 << ' '
237                             << route[elemI]+1 << endl;
238                     }
239                     Info<< "route:" << route << endl;
240                 }
241             }
242         }
243         Info<< endl;
245         iter++;
246     }
248     Info<< "End\n" << endl;
250     return 0;
254 // ************************************************************************* //