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/>.
28 Simple demonstration and test application for the globalIndex class.
30 \*---------------------------------------------------------------------------*/
32 #include "globalIndex.H"
36 #include "IOstreams.H"
37 #include "OStringStream.H"
38 #include "IStringStream.H"
42 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
45 int main(int argc, char *argv[])
47 # include "setRootCase.H"
48 # include "createTime.H"
49 # include "createPolyMesh.H"
51 // Global numbering of cells (proc0 elements first, then proc1, etc.)
52 globalIndex globalNumbering(mesh.nCells());
54 if (globalNumbering.localSize() != mesh.nCells())
56 FatalErrorIn(args.executable())
57 << "Problem." << abort(FatalError);
61 if (!Pstream::parRun())
63 WarningIn(args.executable())
64 << "globalIndex class is only useful in parallel code."
68 // convert from local to global and back.
69 for (label cellI = 0; cellI < mesh.nCells(); cellI++)
72 label globalCellI = globalNumbering.toGlobal(cellI);
75 label procI = globalNumbering.whichProcID(globalCellI);
76 label localCellI = globalNumbering.toLocal(globalCellI);
78 if (procI != Pstream::myProcNo() || localCellI != cellI)
80 FatalErrorIn(args.executable())
81 << "Problem. cellI:" << cellI << " localCellI:" << localCellI
82 << " procI:" << procI << abort(FatalError);
85 if (!globalNumbering.isLocal(globalCellI))
87 FatalErrorIn(args.executable())
88 << "Problem. cellI:" << cellI << " globalCellI:" << globalCellI
89 << " not local" << abort(FatalError);
94 // Try whichProcID on a few borderline cases.
96 if (mesh.nCells() < 1)
98 FatalErrorIn(args.executable())
99 << "Test needs to be run on a case with at least one"
100 << " cell per processor." << abort(FatalError);
103 if (Pstream::myProcNo() > 0)
105 // We already checked that toGlobal(0) maps back correctly to myProcNo
106 // so now check that the index one before maps to the previous processor
107 label prevProcCellI = globalNumbering.toGlobal(0)-1;
108 label procI = globalNumbering.whichProcID(prevProcCellI);
110 if (procI != Pstream::myProcNo()-1)
112 FatalErrorIn(args.executable())
113 << "Problem. global:" << prevProcCellI
114 << " expected on processor:" << Pstream::myProcNo()-1
115 << " but is calculated to be on procI:" << procI
116 << abort(FatalError);
119 if (globalNumbering.isLocal(prevProcCellI))
121 FatalErrorIn(args.executable())
122 << "Problem. globalCellI:" << prevProcCellI
123 << " calculated as local" << abort(FatalError);
126 if (!globalNumbering.isLocal(procI, prevProcCellI))
128 FatalErrorIn(args.executable())
129 << "Problem. globalCellI:" << prevProcCellI
130 << " not calculated as local on processor:" << procI
131 << abort(FatalError);
136 if (Pstream::myProcNo() < Pstream::nProcs()-1)
138 label nextProcCellI = globalNumbering.toGlobal(mesh.nCells()-1)+1;
139 label procI = globalNumbering.whichProcID(nextProcCellI);
141 if (procI != Pstream::myProcNo()+1)
143 FatalErrorIn(args.executable())
144 << "Problem. global:" << nextProcCellI
145 << " expected on processor:" << Pstream::myProcNo()+1
146 << " but is calculated to be on procI:" << procI
147 << abort(FatalError);
150 if (globalNumbering.isLocal(nextProcCellI))
152 FatalErrorIn(args.executable())
153 << "Problem. globalCellI:" << nextProcCellI
154 << " calculated as local" << abort(FatalError);
157 if (!globalNumbering.isLocal(procI, nextProcCellI))
159 FatalErrorIn(args.executable())
160 << "Problem. globalCellI:" << nextProcCellI
161 << " not calculated as local on processor:" << procI
162 << abort(FatalError);
170 // ************************************************************************* //