Sinusoid.hpp
1 // Distributed under the MIT License.
2 // See LICENSE.txt for details.
3 
4 #pragma once
5 
6 #include "DataStructures/DataBox/Prefixes.hpp" // IWYU pragma: keep
8 #include "Evolution/Systems/Burgers/Tags.hpp" // IWYU pragma: keep
9 #include "Options/Options.hpp"
10 #include "PointwiseFunctions/AnalyticData/AnalyticData.hpp"
11 #include "Utilities/TMPL.hpp"
12 #include "Utilities/TaggedTuple.hpp"
13 
14 /// \cond
15 class DataVector;
16 // IWYU pragma: no_forward_declare Tensor
17 namespace PUP {
18 class er;
19 } // namespace PUP
20 /// \endcond
21 
22 namespace Burgers {
23 namespace AnalyticData {
24 /*!
25  * \brief Analytic data (with an "exact" solution known) that is periodic over
26  * the interval \f$[0,2\pi]\f$.
27  *
28  * The initial data is given by:
29  *
30  * \f{align}{
31  * u(x, 0) = \sin(x)
32  * \f}
33  *
34  * At future times the analytic solution can be found by solving the
35  * transcendental equation \cite Harten19973
36  *
37  * \f{align}{
38  * \label{eq:transcendental burgers periodic}
39  * \mathcal{F}=\sin\left(x-\mathcal{F}t\right)
40  * \f}
41  *
42  * on the interval \f$x\in(0,\pi)\f$. The solution from \f$x\in(\pi,2\pi)\f$ is
43  * given by \f$\mathcal{F}(x, t)=-\mathcal{F}(2\pi-x,t)\f$. The transcendental
44  * equation \f$(\ref{eq:transcendental burgers periodic})\f$ can be solved with
45  * a Newton-Raphson iterative scheme. Since this can be quite sensitive to the
46  * initial guess we implement this solution as analytic data. The python code
47  * below can be used to compute the analytic solution if desired.
48  *
49  * At time \f$1\f$ the solution develops a discontinuity at \f$x=\pi\f$ followed
50  * by the amplitude of the solution decaying over time.
51  *
52  * \note We have rescaled \f$x\f$ and \f$t\f$ by \f$\pi\f$ compared to
53  * \cite Harten19973.
54  *
55  * \code{py}
56  import numpy as np
57  from scipy.optimize import newton
58 
59  # x_grid is a np.array of positions at which to evaluate the solution
60  def burgers_periodic(x_grid, time):
61  def opt_fun(F, x, t):
62  return np.sin((x - F * t)) - F
63 
64  results = []
65  for i in range(len(x_grid)):
66  x = x_grid[i]
67  greater_than_pi = False
68  if x > np.pi:
69  x = x - np.pi
70  x = -x
71  x = x + np.pi
72  greater_than_pi = True
73 
74  guess = 0.0
75  if len(results) > 0:
76  if results[-1] < 0.0:
77  guess = -results[-1]
78  else:
79  guess = results[-1]
80  res = newton(lambda F: opt_fun(F, x, time), x0=guess)
81 
82  if greater_than_pi:
83  results.append(-res)
84  else:
85  results.append(res)
86 
87  return np.asarray(results)
88  * \endcode
89  */
90 class Sinusoid : public MarkAsAnalyticData {
91  public:
92  using options = tmpl::list<>;
93  static constexpr OptionString help{
94  "A solution that is periodic over the interval [0,2pi]. The solution "
95  "starts as a sinusoid: u(x,0) = sin(x) and develops a "
96  "discontinuity at x=pi and t=1."};
97 
98  Sinusoid() = default;
99  Sinusoid(const Sinusoid&) noexcept = default;
100  Sinusoid& operator=(const Sinusoid&) noexcept = default;
101  Sinusoid(Sinusoid&&) noexcept = default;
102  Sinusoid& operator=(Sinusoid&&) noexcept = default;
103  ~Sinusoid() noexcept = default;
104 
105  template <typename T>
106  Scalar<T> u(const tnsr::I<T, 1>& x) const noexcept;
107 
108  tuples::TaggedTuple<Tags::U> variables(const tnsr::I<DataVector, 1>& x,
109  tmpl::list<Tags::U> /*meta*/) const
110  noexcept;
111 
112  // clang-tidy: no pass by reference
113  void pup(PUP::er& p) noexcept; // NOLINT
114 };
115 
116 bool operator==(const Sinusoid& /*lhs*/, const Sinusoid& /*rhs*/) noexcept;
117 
118 bool operator!=(const Sinusoid& lhs, const Sinusoid& rhs) noexcept;
119 } // namespace AnalyticData
120 } // namespace Burgers
Options.hpp
Burgers
Items related to evolving the Burgers equation .
Definition: EvolveBurgersFwd.hpp:6
Burgers::AnalyticData::Sinusoid
Analytic data (with an "exact" solution known) that is periodic over the interval .
Definition: Sinusoid.hpp:90
tuples::TaggedTuple
An associative container that is indexed by structs.
Definition: TaggedTuple.hpp:271
DataVector
Stores a collection of function values.
Definition: DataVector.hpp:42
Scalar
Tensor< T, Symmetry<>, index_list<> > Scalar
Definition: TypeAliases.hpp:21
TypeAliases.hpp
Prefixes.hpp
OptionString
const char *const OptionString
The string used in option structs.
Definition: Options.hpp:30
TMPL.hpp