randomRenumber.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 | www.openfoam.com
6  \\/ M anipulation |
7 -------------------------------------------------------------------------------
8  Copyright (C) 2011-2012 OpenFOAM Foundation
9  Copyright (C) 2021-2024 OpenCFD Ltd.
10 -------------------------------------------------------------------------------
11 License
12  This file is part of OpenFOAM.
13 
14  OpenFOAM is free software: you can redistribute it and/or modify it
15  under the terms of the GNU General Public License as published by
16  the Free Software Foundation, either version 3 of the License, or
17  (at your option) any later version.
18 
19  OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
20  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
21  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
22  for more details.
23 
24  You should have received a copy of the GNU General Public License
25  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
26 
27 \*---------------------------------------------------------------------------*/
28 
29 #include "randomRenumber.H"
30 #include "Random.H"
32 
33 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
34 
35 namespace Foam
36 {
37  defineTypeName(randomRenumber);
38 
40  (
41  renumberMethod,
42  randomRenumber,
43  dictionary
44  );
45 }
46 
47 
48 // * * * * * * * * * * * * * * * Local Functions * * * * * * * * * * * * * * //
49 
50 namespace Foam
51 {
52 
53 labelList randomMap(const label nCells)
54 {
55  Random rndGen(0);
56 
57  // Full coverage
58  labelList newToOld(Foam::identity(nCells));
59 
60  // Fisher-Yates shuffle algorithm
61  for (label i = nCells - 1; i > 0; --i)
62  {
63  label j = rndGen.position<label>(0, i);
64  std::swap(newToOld[i], newToOld[j]);
65  }
66 
67  return newToOld;
68 }
69 
70 } // End namespace Foam
71 
72 
73 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
74 
76 :
78 {}
79 
80 
82 :
84 {}
85 
86 
87 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
88 
90 (
91  const label nCells
92 ) const
93 {
94  return randomMap(nCells);
95 }
96 
97 
99 (
100  const pointField& cellCentres
101 ) const
102 {
103  return randomMap(cellCentres.size());
104 }
105 
106 
108 (
109  const polyMesh& mesh
110 ) const
111 {
112  return randomMap(mesh.nCells());
113 }
114 
115 
117 (
118  const CompactListList<label>& cellCells
119 ) const
120 {
121  return randomMap(cellCells.size());
122 }
123 
124 
126 (
127  const labelListList& cellCells
128 ) const
129 {
130  return randomMap(cellCells.size());
131 }
132 
133 
134 // ************************************************************************* //
labelList randomMap(const label nCells)
dictionary dict
void size(const label n)
Older name for setAddressableSize.
Definition: UList.H:116
A list of keyword definitions, which are a keyword followed by a number of values (eg...
Definition: dictionary.H:129
Random rndGen
Definition: createFields.H:23
randomRenumber()
Default construct.
Macros for easy insertion into run-time selection tables.
defineTypeName(manifoldCellsMeshObject)
Abstract base class for renumbering.
dynamicFvMesh & mesh
labelList identity(const label len, label start=0)
Return an identity map of the given length with (map[i] == i), works like std::iota() but returning a...
Definition: labelLists.C:44
Random number generator.
Definition: Random.H:55
A packed storage of objects of type <T> using an offset table for access.
label nCells() const noexcept
Number of mesh cells.
Mesh consisting of general polyhedral cells.
Definition: polyMesh.H:75
virtual labelList renumber(const label nCells) const
Return the cell visit order (from ordered back to original cell id) based solely on the number of cel...
Type position(const Type &start, const Type &end)
Return a sample on the interval [start,end].
Namespace for OpenFOAM.
addToRunTimeSelectionTable(functionObject, pointHistory, dictionary)