Test-globalIndex.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 Application
25  globalIndexTest
26 
27 Description
28  Simple demonstration and test application for the globalIndex class.
29 
30 \*---------------------------------------------------------------------------*/
31 
32 #include "globalIndex.H"
33 #include "argList.H"
34 #include "Time.H"
35 #include "polyMesh.H"
36 #include "IOstreams.H"
37 #include "OStringStream.H"
38 #include "IStringStream.H"
39 
40 using namespace Foam;
41 
42 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
43 // Main program:
44 
45 int main(int argc, char *argv[])
46 {
47  #include "setRootCase.H"
48  #include "createTime.H"
49  #include "createPolyMesh.H"
50 
51  // Global numbering of cells (proc0 elements first, then proc1, etc.)
52  globalIndex globalNumbering(mesh.nCells());
53 
54  if (globalNumbering.localSize() != mesh.nCells())
55  {
57  << "Problem." << abort(FatalError);
58  }
59 
60 
61  if (!Pstream::parRun())
62  {
64  << "globalIndex class is only useful in parallel code."
65  << endl;
66  }
67 
68  // convert from local to global and back.
69  for (label cellI = 0; cellI < mesh.nCells(); cellI++)
70  {
71  // to global index
72  label globalCellI = globalNumbering.toGlobal(cellI);
73 
74  // and back
75  label procI = globalNumbering.whichProcID(globalCellI);
76  label localCellI = globalNumbering.toLocal(globalCellI);
77 
78  if (procI != Pstream::myProcNo() || localCellI != cellI)
79  {
81  << "Problem. cellI:" << cellI << " localCellI:" << localCellI
82  << " procI:" << procI << abort(FatalError);
83  }
84 
85  if (!globalNumbering.isLocal(globalCellI))
86  {
88  << "Problem. cellI:" << cellI << " globalCellI:" << globalCellI
89  << " not local" << abort(FatalError);
90  }
91  }
92 
93 
94  // Try whichProcID on a few borderline cases.
95 
96  if (mesh.nCells() < 1)
97  {
99  << "Test needs to be run on a case with at least one"
100  << " cell per processor." << abort(FatalError);
101  }
102 
103  if (Pstream::myProcNo() > 0)
104  {
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);
109 
110  if (procI != Pstream::myProcNo()-1)
111  {
113  << "Problem. global:" << prevProcCellI
114  << " expected on processor:" << Pstream::myProcNo()-1
115  << " but is calculated to be on procI:" << procI
116  << abort(FatalError);
117  }
118 
119  if (globalNumbering.isLocal(prevProcCellI))
120  {
122  << "Problem. globalCellI:" << prevProcCellI
123  << " calculated as local" << abort(FatalError);
124  }
125 
126  if (!globalNumbering.isLocal(procI, prevProcCellI))
127  {
129  << "Problem. globalCellI:" << prevProcCellI
130  << " not calculated as local on processor:" << procI
131  << abort(FatalError);
132  }
133  }
134 
135 
137  {
138  label nextProcCellI = globalNumbering.toGlobal(mesh.nCells()-1)+1;
139  label procI = globalNumbering.whichProcID(nextProcCellI);
140 
141  if (procI != Pstream::myProcNo()+1)
142  {
144  << "Problem. global:" << nextProcCellI
145  << " expected on processor:" << Pstream::myProcNo()+1
146  << " but is calculated to be on procI:" << procI
147  << abort(FatalError);
148  }
149 
150  if (globalNumbering.isLocal(nextProcCellI))
151  {
153  << "Problem. globalCellI:" << nextProcCellI
154  << " calculated as local" << abort(FatalError);
155  }
156 
157  if (!globalNumbering.isLocal(procI, nextProcCellI))
158  {
160  << "Problem. globalCellI:" << nextProcCellI
161  << " not calculated as local on processor:" << procI
162  << abort(FatalError);
163  }
164  }
165 
166  return 0;
167 }
168 
169 
170 // ************************************************************************* //
IOstreams.H
Useful combination of include files which define Sin, Sout and Serr and the use of IO streams general...
main
int main(int argc, char *argv[])
Definition: Test-globalIndex.C:42
globalIndex.H
Foam::UPstream::nProcs
static label nProcs(const label communicator=0)
Number of processes in parallel run.
Definition: UPstream.H:387
Foam::UPstream::parRun
static bool & parRun()
Is this a parallel run?
Definition: UPstream.H:377
Foam::globalIndex::localSize
label localSize() const
My local size.
Definition: globalIndexI.H:60
Foam::globalIndex::isLocal
bool isLocal(const label i) const
Is on local processor.
Definition: globalIndexI.H:95
Foam::endl
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:251
polyMesh.H
IStringStream.H
Foam::primitiveMesh::nCells
label nCells() const
Definition: primitiveMeshI.H:64
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
argList.H
OStringStream.H
Foam::FatalError
error FatalError
mesh
dynamicFvMesh & mesh
Definition: createDynamicFvMesh.H:18
Foam
Namespace for OpenFOAM.
Definition: combustionModel.C:30
Foam::abort
errorManip< error > abort(error &err)
Definition: errorManip.H:131
Foam::globalIndex
Calculates a unique integer (label so might not have enough room - 2G max) for processor + local inde...
Definition: globalIndex.H:63
Foam::UPstream::myProcNo
static int myProcNo(const label communicator=0)
Number of this process (starting from masterNo() = 0)
Definition: UPstream.H:405
Foam::globalIndex::toLocal
label toLocal(const label i) const
From global to local on current processor.
Definition: globalIndexI.H:117
setRootCase.H
FatalErrorInFunction
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:318
Foam::globalIndex::whichProcID
label whichProcID(const label i) const
Which processor does global come from? Binary search.
Definition: globalIndexI.H:123
createTime.H
createPolyMesh.H
WarningInFunction
#define WarningInFunction
Report a warning using Foam::Warning.
Definition: messageStream.H:259
Foam::globalIndex::toGlobal
label toGlobal(const label i) const
From local to global.
Definition: globalIndexI.H:82