MFEM  v4.6.0 Finite element discretization library
util.cpp
Go to the documentation of this file.
1 // Copyright (c) 2010-2023, Lawrence Livermore National Security, LLC. Produced
3 // LICENSE and NOTICE for details. LLNL-CODE-806117.
4 //
5 // This file is part of the MFEM library. For more information and source code
6 // availability visit https://mfem.org.
7 //
8 // MFEM is free software; you can redistribute it and/or modify it under the
9 // terms of the BSD-3 license. We welcome feedback and contributions, see file
10 // CONTRIBUTING.md for details
11
12 #include "util.hpp"
13 #include <algorithm>
14
15 namespace mfem
16 {
17
18 void FillWithRandomNumbers(std::vector<double> &x, double a, double b)
19 {
20  std::random_device rd;
21  std::mt19937 gen(rd());
22  std::uniform_real_distribution<> dis(a, b);
23  std::for_each(x.begin(), x.end(), [&](double &v) { v = dis(gen); });
24 }
25
26 void FillWithRandomRotations(std::vector<double> &x)
27 {
28  std::random_device rd;
29  std::mt19937 gen(rd());
30  std::uniform_real_distribution<> dis(0, 1);
31  for (size_t i = 0; i < x.size(); i += 9)
32  {
33  // Get a random rotation matrix via uniform Euler angles.
34  double e1 = 2 * M_PI * dis(gen);
35  double e2 = 2 * M_PI * dis(gen);
36  double e3 = 2 * M_PI * dis(gen);
37  const double c1 = cos(e1);
38  const double s1 = sin(e1);
39  const double c2 = cos(e2);
40  const double s2 = sin(e2);
41  const double c3 = cos(e3);
42  const double s3 = sin(e3);
43
44  // Fill the rotation matrix R with the Euler angles. See for instance
45  // the definition in Wikipedia.
46  x[i + 0] = c1 * c3 - c2 * s1 * s3;
47  x[i + 1] = -c1 * s3 - c2 * c3 * s1;
48  x[i + 2] = s1 * s2;
49  x[i + 3] = c3 * s1 + c1 * c2 * s3;
50  x[i + 4] = c1 * c2 * c3 - s1 * s3;
51  x[i + 5] = -c1 * s2;
52  x[i + 6] = s2 * s3;
53  x[i + 7] = c3 * s2;
54  x[i + 8] = c2;
55  }
56 }
57
58 } // namespace mfem
double b
Definition: lissajous.cpp:42
void FillWithRandomRotations(std::vector< double > &x)
Definition: util.cpp:26
void FillWithRandomNumbers(std::vector< double > &x, double a, double b)
Fills the vector x with random numbers between a and b.
Definition: util.cpp:18
double a
Definition: lissajous.cpp:41