Parallel Finite Element - General geometry Ewald-like Method
Part of Continuum-particle Simulation Suite under MICCOM
brownian_system.h
Go to the documentation of this file.
1 // Parallel Finite Element-General Geometry Ewald-like Method.
2 // Copyright (C) 2015-2016 Xujun Zhao, Jiyuan Li, Xikai Jiang
3 
4 // This code is free software; you can redistribute it and/or
5 // modify it under the terms of the GNU General Public
6 // License as published by the Free Software Foundation; either
7 // version 2.1 of the License, or (at your option) any later version.
8 
9 
10 // This code is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 // Lesser General Public License for more details.
14 
15 
16 // You should have received a copy of the GNU General Public
17 // License along with this code; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 
20 
21 #pragma once
22 
23 #include <stdio.h>
24 #include <chrono>
25 
26 #include "libmesh/libmesh_common.h"
27 #include "libmesh/libmesh_config.h"
28 #include "libmesh/petsc_macro.h"
29 #include "libmesh/slepc_macro.h"
30 #include "libmesh/parallel_object.h"
31 #include "libmesh/reference_counted_object.h"
32 
33 #include "random_generator.h"
34 #include "pm_system_stokes.h"
35 #include "fix/fix.h"
36 // include SLEPc EPS solver, "libmesh/slepc_macro.h" must be included before this!
37 EXTERN_C_FOR_SLEPC_BEGIN
38 # include <slepceps.h>
39 EXTERN_C_FOR_SLEPC_END
40 
41 
42 using namespace libMesh;
43 
44 
51 extern "C"
52 {
53  PetscErrorCode _MatMult_Stokes(Mat M,Vec f,Vec u);
54 }
55 
56 
57 
58 namespace libMesh
59 {
60 
61 
62 class EquationSystems;
63 
64 
65 
66  /*
67  * The BrownianSystem is designed to deal with the stochastic
68  * process of Brownian Dynamics(BD)
69  */
70 
71 
72 class BrownianSystem : public ReferenceCountedObject<BrownianSystem>,
73  public ParallelObject
74 {
75 public:
76  // Constructor
77  BrownianSystem(EquationSystems& es);
78 
79 
80  // Destructor
82 
83 
84 
85  /*
86  * Return a constant reference of equation system
87  */
88  const EquationSystems& get_equation_systems() const
89  { return _equation_systems; };
90 
91 
92  /*
93  * Return a reference of equation system
94  */
95  EquationSystems& get_equation_systems()
96  { return _equation_systems; };
97 
98 
99  /*
100  * Manage PETSc Vec's scatters and gathers to all process
101  * NOTE: We can't put VecScatter inside the function,
102  * VecScatterCreateToAll() should be outside!
103  */
104  PetscErrorCode petsc_vec_scatter_to_all(Vec f, // vin
105  Vec vf, // vout
106  VecScatter scatter,
107  const std::string& mode);
108 
109 
110  /*
111  * Extract particle coordinates as a PETSc vector : mode = "extract"
112  * Assign the paticle coordinates from a PETSc vector : mode = "assign"
113  * vec_type can be either "coordinate" or "force"
114  */
115  PetscErrorCode extract_particle_vector(Vec* x,
116  const std::string& vec_type,
117  const std::string& mode);
118 
119 
120  /*
121  * Transformation from a std::vector<> to a PETSc Vec
122  * mode == "forward": std_vec -> petsc_vec
123  * mode == "backward": petsc_vec -> std_vec
124  * NOTE: the PETSc vector must be initialized with some parallel distributions
125  */
126  PetscErrorCode vector_transform(std::vector<Real>& std_vec,
127  Vec* petsc_vec,
128  const std::string& mode) const;
129 
130 
131  // Random generator from C++ std library
132  RandomGenerator& random_generator() { return _random_generator; }
133 
134 
135  /*
136  * set the random seed for standard random vectors
137  */
138  void set_std_random_seed(const std::size_t seed_val)
139  { _random_generator.set_seed(seed_val); }
140 
141 
142  /*
143  * Generate a N-components random vector with gaussian/uniform distribution
144  */
145  PetscErrorCode std_random_vector(const Real& a, // a (mean)
146  const Real& b, // b (std deviation)
147  const std::string& random_type,
148  Vec* u);
149 
150 
151  /*
152  * Generate a N-components random vector with uniform or gaussian(normal) distribution
153  *
154  * unsigned seed = std::chrono::system_clock::now().time_since_epoch().count();
155  * may cause problems, because the seed (time) on each CPU may be different!
156  */
157  std::vector<Real> std_random_vector(const std::size_t N,// length
158  const Real& a, // a (mean)
159  const Real& b, // b (std deviation)
160  const std::string& random_type);
161 
162 
163  /*
164  * Initialize PETSc random
165  * The init and generate parts have to be separated, because in the real
166  * simulation, we only need to init once and generate rand number at each step.
167  */
168  PetscErrorCode init_petsc_random(PetscRandom* rand_ctx);
169 
170 
171  /*
172  * Generate PETSc random vector u with N components
173  * This is only for the uniform distribution!
174  */
175  PetscErrorCode petsc_random_vector(const PetscInt N,
176  PetscRandom* rand_ctx,
177  Vec* u);
178 
179 
180  /*
181  * Return the smallest or largest eigenvalue of diffusion matrix D
182  * option == "smallest" or "largest"
183  */
184  Real compute_eigenvalue(const std::string& option,
185  const Real tol);
186 
187 
188  /*
189  * Return the smallest or largest eigenvalue of diffusion matrix D
190  * option == "smallest" or "largest"
191  * The difference is that this allows EPS solver to use initial space
192  */
193  Real compute_eigenvalue(const std::string& option,
194  const Real tol,
195  const bool use_init_space,
196  Vec* v0);
197 
198 
199  /*
200  * Return both the smallest and largest eigenvalues
201  */
202  PetscErrorCode compute_eigenvalues(Real& eigen_min,
203  Real& eigen_max,
204  const Real& tol);
205 
206  /*
207  * Compute the largest eigenvalue using the power_iteration
208  * This is only for the purpose of test, and it is also supportted
209  * by the SLEPc library.
210  */
211  Real power_iteration();
212 
213 
214 
215  /*
216  * The transforming matrix from physical space to Chebyshev tranform space
217  * using Gauss-Lobatto method
218  */
219  DenseMatrix<Number> chebyshev_transform_matrix(const std::size_t N) const;
220 
221 
222  /*
223  * Quadrature points in [-1,1] for the chebyshev transformation
224  * (Gauss-Lobatto method)
225  */
226  DenseVector<Number> chebyshev_quadrature_point(const std::size_t N) const;
227 
228 
229  /*
230  * Chebyshev polynomial approximation:
231  * y = B*dw or y = B^-1 * dw
232  * where, B = sqrt(D), D = kB*T*M, and dw is a random force vector.
233  */
234  bool chebyshev_polynomial_approximation(const std::size_t N,
235  const Real eigen_min,
236  const Real eigen_max,
237  const Real tol_cheb,
238  Vec* dw);
239 
240  /*
241  * A wrap-up Stokes solver that simply call _MatMult_Stokes() to compute
242  * the particle velocity vector provided that the force vector is given.
243  */
244  PetscErrorCode hi_ewald(Mat M, Vec f, Vec u)
245  { return _MatMult_Stokes(M, f, u); }
246 
247 
248 
249  // this generate a diagonal matrix, which is used only for test purpose
250  // u = M*f, where M(i,i) = i.
251  PetscErrorCode _test_diagonal_mat(Vec f, Vec u);
252 
253  // This is another test function that calculates a vector's mean and variance
254  PetscErrorCode _vector_mean_variance(Vec u,
255  Real& mean,
256  Real& variance) const;
257  PetscErrorCode _vector_mean_variance(const std::vector<Real>& u) const;
258 
259 
260 
261  // create a shell matrix with dimension N x N
262  PetscErrorCode _create_shell_mat(const std::size_t N, Mat* M);
263 
264  // create a vector with dimension N which has the same partition as M
265  PetscErrorCode _create_petsc_vec(const std::size_t N, Vec* V);
266 
267 
268 
269 private:
270  // Reference of Equation systems
271  EquationSystems & _equation_systems;
272 
273  // total number of points
274  unsigned int _n_points;
275 
276  // point mesh
277  PointMesh<3>* _point_mesh;
278 
279  // fixes
280  std::vector<Fix*> _fixes;
281 
282  // dimension of mesh
283  unsigned int _dim;
284 
285  // Random generator from C++ std library
286  RandomGenerator _random_generator;
287 
288 
289  // friend class/function, so that it can reach the members in the current class.
290  friend PetscErrorCode _MatMult_Stokes(Mat M, Vec f, Vec u);
291 
292 
293 }; // end of namespace
294 
295 
296 
297 
298 } // end of namespace
const EquationSystems & get_equation_systems() const
Definition: brownian_system.h:88
Definition: random_generator.h:43
PetscErrorCode _MatMult_Stokes(Mat M, Vec f, Vec u)
Definition: brownian_system.C:43
DenseVector< Number > chebyshev_quadrature_point(const std::size_t N) const
Definition: brownian_system.C:827
PetscErrorCode compute_eigenvalues(Real &eigen_min, Real &eigen_max, const Real &tol)
Definition: brownian_system.C:696
PetscErrorCode init_petsc_random(PetscRandom *rand_ctx)
Definition: brownian_system.C:411
PetscErrorCode extract_particle_vector(Vec *x, const std::string &vec_type, const std::string &mode)
Definition: brownian_system.C:180
PetscErrorCode petsc_random_vector(const PetscInt N, PetscRandom *rand_ctx, Vec *u)
Definition: brownian_system.C:433
PetscErrorCode _test_diagonal_mat(Vec f, Vec u)
Definition: brownian_system.C:1003
PetscErrorCode _create_petsc_vec(const std::size_t N, Vec *V)
Definition: brownian_system.C:1130
Definition: brownian_system.h:58
PetscErrorCode petsc_vec_scatter_to_all(Vec f, Vec vf, VecScatter scatter, const std::string &mode)
Definition: brownian_system.C:146
EquationSystems & get_equation_systems()
Definition: brownian_system.h:95
PetscErrorCode std_random_vector(const Real &a, const Real &b, const std::string &random_type, Vec *u)
Definition: brownian_system.C:344
Real compute_eigenvalue(const std::string &option, const Real tol)
Definition: brownian_system.C:461
void set_std_random_seed(const std::size_t seed_val)
Definition: brownian_system.h:138
~BrownianSystem()
Definition: brownian_system.C:138
RandomGenerator & random_generator()
Definition: brownian_system.h:132
PetscErrorCode _vector_mean_variance(Vec u, Real &mean, Real &variance) const
Definition: brownian_system.C:1035
PetscErrorCode vector_transform(std::vector< Real > &std_vec, Vec *petsc_vec, const std::string &mode) const
Definition: brownian_system.C:280
DenseMatrix< Number > chebyshev_transform_matrix(const std::size_t N) const
Definition: brownian_system.C:804
Real power_iteration()
Definition: brownian_system.C:738
PetscErrorCode hi_ewald(Mat M, Vec f, Vec u)
Definition: brownian_system.h:244
bool chebyshev_polynomial_approximation(const std::size_t N, const Real eigen_min, const Real eigen_max, const Real tol_cheb, Vec *dw)
Definition: brownian_system.C:842
void set_seed(std::size_t seed_val)
Definition: random_generator.C:101
friend PetscErrorCode _MatMult_Stokes(Mat M, Vec f, Vec u)
Definition: brownian_system.C:43
BrownianSystem(EquationSystems &es)
Definition: brownian_system.C:125
PetscErrorCode _create_shell_mat(const std::size_t N, Mat *M)
Definition: brownian_system.C:1101
Definition: brownian_system.h:72