Line data Source code
1 0 : // Distributed under the MIT License.
2 : // See LICENSE.txt for details.
3 :
4 : #pragma once
5 :
6 : #include <array>
7 : #include <cstddef>
8 : #include <limits>
9 : #include <pup.h>
10 :
11 : #include "DataStructures/CachedTempBuffer.hpp"
12 : #include "DataStructures/ComplexDataVector.hpp"
13 : #include "DataStructures/DataBox/Prefixes.hpp"
14 : #include "DataStructures/DataVector.hpp"
15 : #include "DataStructures/TaggedTuple.hpp"
16 : #include "DataStructures/Tensor/Tensor.hpp"
17 : #include "Elliptic/Systems/Poisson/Tags.hpp"
18 : #include "NumericalAlgorithms/LinearOperators/PartialDerivatives.hpp"
19 : #include "NumericalAlgorithms/Spectral/Mesh.hpp"
20 : #include "Options/String.hpp"
21 : #include "PointwiseFunctions/InitialDataUtilities/AnalyticSolution.hpp"
22 : #include "Utilities/ContainerHelpers.hpp"
23 : #include "Utilities/Gsl.hpp"
24 : #include "Utilities/TMPL.hpp"
25 :
26 : namespace Poisson::Solutions {
27 :
28 : namespace detail {
29 : template <typename DataType, size_t Dim>
30 : struct ProductOfSinusoidsVariables {
31 : using Cache = CachedTempBuffer<
32 : Tags::Field<DataType>,
33 : ::Tags::deriv<Tags::Field<DataType>, tmpl::size_t<Dim>, Frame::Inertial>,
34 : ::Tags::Flux<Tags::Field<DataType>, tmpl::size_t<Dim>, Frame::Inertial>,
35 : ::Tags::FixedSource<Tags::Field<DataType>>>;
36 :
37 : // NOLINTNEXTLINE(cppcoreguidelines-avoid-const-or-ref-data-members)
38 : const tnsr::I<DataVector, Dim>& x;
39 : std::array<double, Dim> wave_numbers;
40 : double complex_phase;
41 :
42 : void operator()(gsl::not_null<Scalar<DataType>*> field,
43 : gsl::not_null<Cache*> cache,
44 : Tags::Field<DataType> /*meta*/) const;
45 : void operator()(gsl::not_null<tnsr::i<DataType, Dim>*> field_gradient,
46 : gsl::not_null<Cache*> cache,
47 : ::Tags::deriv<Tags::Field<DataType>, tmpl::size_t<Dim>,
48 : Frame::Inertial> /*meta*/) const;
49 : void operator()(gsl::not_null<tnsr::I<DataType, Dim>*> flux_for_field,
50 : gsl::not_null<Cache*> cache,
51 : ::Tags::Flux<Tags::Field<DataType>, tmpl::size_t<Dim>,
52 : Frame::Inertial> /*meta*/) const;
53 : void operator()(gsl::not_null<Scalar<DataType>*> fixed_source_for_field,
54 : gsl::not_null<Cache*> cache,
55 : ::Tags::FixedSource<Tags::Field<DataType>> /*meta*/) const;
56 : };
57 : } // namespace detail
58 :
59 : /*!
60 : * \brief A product of sinusoids \f$u(\boldsymbol{x}) = \prod_i \sin(k_i x_i)\f$
61 : *
62 : * \details Solves the Poisson equation \f$-\Delta u(x)=f(x)\f$ for a source
63 : * \f$f(x)=\boldsymbol{k}^2\prod_i \sin(k_i x_i)\f$.
64 : *
65 : * If `DataType` is `ComplexDataVector`, the solution is multiplied by
66 : * `exp(i * complex_phase)` to rotate it in the complex plane. This allows to
67 : * use this solution for the complex Poisson equation.
68 : */
69 : template <size_t Dim, typename DataType = DataVector>
70 1 : class ProductOfSinusoids : public elliptic::analytic_data::AnalyticSolution {
71 : public:
72 0 : struct WaveNumbers {
73 0 : using type = std::array<double, Dim>;
74 0 : static constexpr Options::String help{"The wave numbers of the sinusoids"};
75 : };
76 :
77 0 : struct ComplexPhase {
78 0 : using type = double;
79 0 : static constexpr Options::String help{
80 : "Phase 'phi' of a complex exponential 'exp(i phi)' that rotates the "
81 : "solution in the complex plane."};
82 : };
83 :
84 0 : using options = tmpl::flatten<tmpl::list<
85 : WaveNumbers,
86 : tmpl::conditional_t<std::is_same_v<DataType, ComplexDataVector>,
87 : ComplexPhase, tmpl::list<>>>>;
88 0 : static constexpr Options::String help{
89 : "A product of sinusoids that are taken of a wave number times the "
90 : "coordinate in each dimension."};
91 :
92 0 : ProductOfSinusoids() = default;
93 0 : ProductOfSinusoids(const ProductOfSinusoids&) = default;
94 0 : ProductOfSinusoids& operator=(const ProductOfSinusoids&) = default;
95 0 : ProductOfSinusoids(ProductOfSinusoids&&) = default;
96 0 : ProductOfSinusoids& operator=(ProductOfSinusoids&&) = default;
97 0 : ~ProductOfSinusoids() override = default;
98 0 : std::unique_ptr<elliptic::analytic_data::AnalyticSolution> get_clone()
99 : const override {
100 : return std::make_unique<ProductOfSinusoids>(*this);
101 : }
102 :
103 : /// \cond
104 : explicit ProductOfSinusoids(CkMigrateMessage* m)
105 : : elliptic::analytic_data::AnalyticSolution(m) {}
106 : using PUP::able::register_constructor;
107 : WRAPPED_PUPable_decl_template(ProductOfSinusoids); // NOLINT
108 : /// \endcond
109 :
110 0 : explicit ProductOfSinusoids(const std::array<double, Dim>& wave_numbers,
111 : const double complex_phase = 0.)
112 : : wave_numbers_(wave_numbers), complex_phase_(complex_phase) {
113 : ASSERT((std::is_same_v<DataType, ComplexDataVector> or complex_phase == 0.),
114 : "The complex phase is only supported for ComplexDataVector.");
115 : }
116 :
117 : template <typename... RequestedTags>
118 0 : tuples::TaggedTuple<RequestedTags...> variables(
119 : const tnsr::I<DataVector, Dim>& x,
120 : tmpl::list<RequestedTags...> /*meta*/) const {
121 : using VarsComputer = detail::ProductOfSinusoidsVariables<DataType, Dim>;
122 : typename VarsComputer::Cache cache{get_size(*x.begin())};
123 : const VarsComputer computer{x, wave_numbers_, complex_phase_};
124 : return {cache.get_var(computer, RequestedTags{})...};
125 : }
126 :
127 : template <typename... RequestedTags>
128 0 : tuples::TaggedTuple<RequestedTags...> variables(
129 : const tnsr::I<DataVector, Dim>& x, const Mesh<Dim>& /*mesh*/,
130 : const InverseJacobian<DataVector, Dim, Frame::ElementLogical,
131 : Frame::Inertial>& /*inv_jacobian*/,
132 : tmpl::list<RequestedTags...> meta) const {
133 : return variables(x, meta);
134 : }
135 :
136 : // NOLINTNEXTLINE(google-runtime-references)
137 0 : void pup(PUP::er& p) override {
138 : elliptic::analytic_data::AnalyticSolution::pup(p);
139 : p | wave_numbers_;
140 : p | complex_phase_;
141 : }
142 :
143 0 : const std::array<double, Dim>& wave_numbers() const { return wave_numbers_; }
144 0 : double complex_phase() const { return complex_phase_; }
145 :
146 : private:
147 0 : std::array<double, Dim> wave_numbers_{
148 : {std::numeric_limits<double>::signaling_NaN()}};
149 0 : double complex_phase_ = std::numeric_limits<double>::signaling_NaN();
150 : };
151 :
152 : /// \cond
153 : template <size_t Dim, typename DataType>
154 : PUP::able::PUP_ID ProductOfSinusoids<Dim, DataType>::my_PUP_ID = 0; // NOLINT
155 : /// \endcond
156 :
157 : template <size_t Dim, typename DataType>
158 0 : bool operator==(const ProductOfSinusoids<Dim, DataType>& lhs,
159 : const ProductOfSinusoids<Dim, DataType>& rhs) {
160 : return lhs.wave_numbers() == rhs.wave_numbers() and
161 : lhs.complex_phase() == rhs.complex_phase();
162 : }
163 :
164 : template <size_t Dim, typename DataType>
165 0 : bool operator!=(const ProductOfSinusoids<Dim, DataType>& lhs,
166 : const ProductOfSinusoids<Dim, DataType>& rhs) {
167 : return not(lhs == rhs);
168 : }
169 :
170 : } // namespace Poisson::Solutions
|