MFEM v4.7.0
Finite element discretization library
Loading...
Searching...
No Matches
util.cpp
Go to the documentation of this file.
1// Copyright (c) 2010-2024, Lawrence Livermore National Security, LLC. Produced
2// at the Lawrence Livermore National Laboratory. All Rights reserved. See files
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
15namespace mfem
16{
17
18void FillWithRandomNumbers(std::vector<real_t> &x, real_t a, real_t 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(), [&](real_t &v) { v = dis(gen); });
24}
25
26void FillWithRandomRotations(std::vector<real_t> &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 real_t e1 = 2 * M_PI * dis(gen);
35 real_t e2 = 2 * M_PI * dis(gen);
36 real_t e3 = 2 * M_PI * dis(gen);
37 const real_t c1 = cos(e1);
38 const real_t s1 = sin(e1);
39 const real_t c2 = cos(e2);
40 const real_t s2 = sin(e2);
41 const real_t c3 = cos(e3);
42 const real_t 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
real_t b
Definition: lissajous.cpp:42
real_t a
Definition: lissajous.cpp:41
void FillWithRandomRotations(std::vector< real_t > &x)
Definition: util.cpp:26
void FillWithRandomNumbers(std::vector< real_t > &x, real_t a, real_t b)
Fills the vector x with random numbers between a and b.
Definition: util.cpp:18
float real_t
Definition: config.hpp:43