restartableRandomGenerator.H
Go to the documentation of this file.
1 /*---------------------------------------------------------------------------*\
2  ========= |
3  \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
4  \\ / O peration | Website: https://openfoam.org
5  \\ / A nd | Copyright (C) 2024 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 Class
25  Foam::restartableRandomGenerator
26 
27 Description
28  Random number generator with the additional ability to go back to an
29  earlier stored state. Useful for processes that occur within converging
30  iteration loops to ensure that the random sequence is the same for each
31  iteration, and is not preventing convergence by "re-randomising" the
32  solution on each iteration.
33 
34 \*---------------------------------------------------------------------------*/
35 
36 #ifndef restartableRandomGenerator_H
37 #define restartableRandomGenerator_H
38 
39 #include "randomGenerator.H"
40 
41 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
42 
43 namespace Foam
44 {
45 
46 /*---------------------------------------------------------------------------*\
47  Class restartableRandomGenerator Declaration
48 \*---------------------------------------------------------------------------*/
49 
51 :
52  public randomGenerator
53 {
54  // Private Data
55 
56  //- The random generator state at the start of the current sequence
57  randomGenerator startRndGen_;
58 
59 
60 public:
61 
62  // Constructors
63 
64  //- Forward all constructors
65  template<class ... Args>
66  inline restartableRandomGenerator(Args&& ... args)
67  :
68  randomGenerator(std::forward<Args>(args) ...),
69  startRndGen_(static_cast<const randomGenerator&>(*this))
70  {}
71 
72 
73  // Member Functions
74 
75  //- Start a sequence of random numbers. Either a new sequence, or a
76  // repeat of the previous, as indicated by the provided flag.
77  inline void start(const bool repeat)
78  {
79  repeat
80  ? static_cast<randomGenerator&>(*this) = startRndGen_
81  : startRndGen_ = static_cast<randomGenerator&>(*this);
82  }
83 };
84 
85 
86 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
87 
88 } // End namespace Foam
89 
90 #endif
91 
92 // ************************************************************************* //
Random number generator.
Random number generator with the additional ability to go back to an earlier stored state....
restartableRandomGenerator(Args &&... args)
Forward all constructors.
void start(const bool repeat)
Start a sequence of random numbers. Either a new sequence, or a.
Namespace for OpenFOAM.
List< Type > repeat(const UList< Type > &a, const UList< Type > &b)
Foam::argList args(argc, argv)