www.mooseframework.org
FourierNoise.C
Go to the documentation of this file.
1 //* This file is part of the MOOSE framework
2 //* https://www.mooseframework.org
3 //*
4 //* All rights reserved, see COPYRIGHT for full restrictions
5 //* https://github.com/idaholab/moose/blob/master/COPYRIGHT
6 //*
7 //* Licensed under LGPL 2.1, please see LICENSE for details
8 //* https://www.gnu.org/licenses/lgpl-2.1.html
9 
10 #include "FourierNoise.h"
11 #include "MooseRandom.h"
12 #include "FEProblemBase.h"
13 #include "libmesh/utility.h"
14 
15 registerMooseObject("PhaseFieldApp", FourierNoise);
16 
17 template <>
18 InputParameters
20 {
21  InputParameters params = validParams<Function>();
22  params.addClassDescription("Generate noise from a fourier series");
23  params.addRequiredParam<Real>("lambda",
24  "Wavelength cut off (set to about twice the interfacial width)");
25  params.addParam<unsigned int>(
26  "num_terms",
27  "Number of random fourier series terms (this will result in non-periodic noise). Omit this "
28  "parameter to obtain a periodic noise distribution.");
29  params.addParam<unsigned int>("seed", 12455, "Random number generator seed");
30  return params;
31 }
32 
33 FourierNoise::FourierNoise(const InputParameters & parameters)
34  : Function(parameters),
35  _lambda(getParam<Real>("lambda")),
36  _fe_problem(*getCheckedPointerParam<FEProblemBase *>("_fe_problem_base"))
37 {
38  MooseRandom rng;
39  rng.seed(0, getParam<unsigned int>("seed"));
40 
41  if (isParamValid("num_terms"))
42  {
43  // random terms
44  _series.resize(getParam<unsigned int>("num_terms"));
45  const Real scale = 2.0 * (2.0 * libMesh::pi) / _lambda;
46 
47  // check
48  if (_series.empty())
49  paramError("num_terms",
50  "If specifying the number of terms, supply a number greater than zero.");
51 
52  // fill terms
53  for (auto & f : _series)
54  {
55  // get a vector with length <= 0.5
56  Real r2;
57  do
58  {
59  const Real x = rng.rand(0) - 0.5;
60  const Real y = rng.rand(0) - 0.5;
61  f.k = RealVectorValue(x, y, 0.0);
62  r2 = f.k.norm_sq();
63  } while (r2 > 0.25);
64 
65  // scale maximum to a wavelength of lambda
66  f.k *= scale;
67 
68  f.c = rng.randNormal(0, 0.0, 1.0);
69  f.s = rng.randNormal(0, 0.0, 1.0);
70  }
71  }
72  else
73  {
74  // k-space grid resulting in periodic noise
75  MooseMesh & mesh = _fe_problem.mesh();
76  if (!mesh.isRegularOrthogonal())
77  mooseError("Periodic Fourier Noise requires a regular orthogonal mesh.");
78 
79  const Real dx = 2.0 * libMesh::pi / mesh.dimensionWidth(0);
80  const Real dy = 2.0 * libMesh::pi / mesh.dimensionWidth(1);
81  const Real rmax = 2.0 * libMesh::pi / _lambda;
82 
83  const int xmax = rmax / dx;
84  const int ymax = rmax / dy;
85 
86  const Real rmax2 = rmax * rmax;
87  for (int x = 0; x < xmax; ++x)
88  for (int y = -ymax; y < ymax; ++y)
89  if (x > 0 || y > 0)
90  {
91  SeriesItem f;
92  f.k = RealVectorValue(x * dx, y * dy, 0.0);
93  if (f.k.norm_sq() <= rmax2)
94  {
95  f.c = rng.randNormal(0, 0.0, 1.0);
96  f.s = rng.randNormal(0, 0.0, 1.0);
97  _series.push_back(f);
98  }
99  }
100  }
101 
102  _scale = std::sqrt(1.0 / _series.size());
103 }
104 
105 Real
106 FourierNoise::value(Real, const Point & p) const
107 {
108  Real v = 0.0;
109  for (const auto & f : _series)
110  v += f.s * std::sin(p * f.k) + f.c * std::cos(p * f.k);
111  return v * _scale;
112 }
FourierNoise::_scale
Real _scale
amplitude factor
Definition: FourierNoise.h:48
FourierNoise.h
FourierNoise::FourierNoise
FourierNoise(const InputParameters &parameters)
Definition: FourierNoise.C:33
validParams< FourierNoise >
InputParameters validParams< FourierNoise >()
Definition: FourierNoise.C:19
FourierNoise::value
virtual Real value(Real, const Point &p) const override
Definition: FourierNoise.C:106
FourierNoise
Generate noise using random fourier series coefficients.
Definition: FourierNoise.h:23
FourierNoise::_series
std::vector< SeriesItem > _series
Fourier series terms.
Definition: FourierNoise.h:45
FourierNoise::_fe_problem
FEProblemBase & _fe_problem
FEProblem pointer for obtaining the current mesh.
Definition: FourierNoise.h:51
FourierNoise::SeriesItem::k
RealVectorValue k
k-vector
Definition: FourierNoise.h:34
FourierNoise::SeriesItem::s
Real s
sin coefficient
Definition: FourierNoise.h:36
FourierNoise::SeriesItem::c
Real c
cos coefficient
Definition: FourierNoise.h:38
FourierNoise::SeriesItem
Definition: FourierNoise.h:31
FourierNoise::_lambda
const Real _lambda
selected lower lengthscale for the noise cut-off
Definition: FourierNoise.h:42
registerMooseObject
registerMooseObject("PhaseFieldApp", FourierNoise)