DDC 0.15.0
Loading...
Searching...
No Matches
examples/heat_equation_spectral.cpp
1// Copyright (C) The DDC development team, see COPYRIGHT.md file
2//
3// SPDX-License-Identifier: MIT
4
5#include <cmath>
6#include <cstddef>
7#include <iomanip>
8#include <iostream>
9#include <string>
10#include <utility>
11
12#include <ddc/ddc.hpp>
13#include <ddc/kernels/fft.hpp>
14
15#include <Kokkos_Core.hpp>
16
18struct X
19{
20};
21
23struct DDimX : ddc::UniformPointSampling<X>
24{
25};
26
27struct DDimFx : ddc::PeriodicSampling<ddc::Fourier<X>>
28{
29};
30
31// Our second continuous dimension
32struct Y
33{
34};
35// Its uniform discretization
36struct DDimY : ddc::UniformPointSampling<Y>
37{
38};
39
40struct DDimFy : ddc::PeriodicSampling<ddc::Fourier<Y>>
41{
42};
43
44// Our simulated time dimension
45struct T
46{
47};
48// Its uniform discretization
49struct DDimT : ddc::UniformPointSampling<T>
50{
51};
52
57template <class ChunkType>
58void display(double time, ChunkType temp)
59{
60 double const mean_temp
61 = ddc::host_transform_reduce(temp.domain(), 0., ddc::reducer::sum<double>(), temp)
62 / temp.domain().size();
63 ddc::DiscreteVector<DDimY> const yslice(ddc::get_domain<DDimY>(temp).size() / 2);
64 std::cout << std::fixed << std::setprecision(3);
65 std::cout << "At t = " << time << ",\n";
66 std::cout << " * mean temperature = " << mean_temp << '\n';
67 std::cout << " * temperature[y:" << yslice.value() << "] = ";
68 ddc::print_content(std::cout, temp[yslice]);
69 std::cout << '\n' << std::flush;
70}
71
72int main(int argc, char** argv)
73{
74 Kokkos::ScopeGuard const kokkos_scope(argc, argv);
75 ddc::ScopeGuard const ddc_scope(argc, argv);
76
77 // some parameters that would typically be read from some form of
78 // configuration file in a more realistic code
79
80 // Start of the domain of interest in the X dimension
81 double const x_start = -1.;
82 // End of the domain of interest in the X dimension
83 double const x_end = 1.;
84 // Number of discretization points in the X dimension
85 std::size_t const nb_x_points = 10;
86 // Thermal diffusion coefficient
87 double const kx = .01;
88 // Start of the domain of interest in the Y dimension
89 double const y_start = -1.;
90 // End of the domain of interest in the Y dimension
91 double const y_end = 1.;
92 // Number of discretization points in the Y dimension
93 std::size_t const nb_y_points = 100;
94 // Thermal diffusion coefficient
95 double const ky = .002;
96 // Simulated time at which to start simulation
97 double const start_time = 0.;
98 // Simulated time to reach as target of the simulation
99 double const end_time = 10.;
100 // Number of time-steps between outputs
101 std::ptrdiff_t const t_output_period = 10;
102
103 // Initialization of the global domain in X including periodic point to have correct step
104 auto const x_domain_with_periodic_point = ddc::init_discrete_space<DDimX>(DDimX::init<DDimX>(
105 ddc::Coordinate<X>(x_start),
106 ddc::Coordinate<X>(x_end),
107 ddc::DiscreteVector<DDimX>(nb_x_points)));
108 ddc::DiscreteDomain<DDimX> const x_domain
109 = x_domain_with_periodic_point.remove_last(ddc::DiscreteVector<DDimX>(1));
110
111 // Initialization of the global domain in Y including periodic point to have correct step
112 auto const y_domain_with_periodic_point = ddc::init_discrete_space<DDimY>(DDimY::init<DDimY>(
113 ddc::Coordinate<Y>(y_start),
114 ddc::Coordinate<Y>(y_end),
115 ddc::DiscreteVector<DDimY>(nb_y_points)));
116 ddc::DiscreteDomain<DDimY> const y_domain
117 = y_domain_with_periodic_point.remove_last(ddc::DiscreteVector<DDimY>(1));
118
119 double const max_rkx = Kokkos::numbers::pi / ddc::step<DDimX>();
120 double const max_rky = Kokkos::numbers::pi / ddc::step<DDimY>();
121 ddc::Coordinate<T> const dt(2. / (kx * max_rkx * max_rkx + ky * max_rky * max_rky));
122
123 // number of time intervals required to reach the end time
124 ddc::DiscreteVector<DDimT> const nb_time_steps(std::ceil((end_time - start_time) / dt) + .2);
125 // Initialization of the global domain in time:
126 // - the number of discrete time-points is equal to the number of
127 // steps + 1
128 ddc::DiscreteDomain<DDimT> const time_domain
129 = ddc::init_discrete_space<DDimT>(DDimT::init<DDimT>(
130 ddc::Coordinate<T>(start_time),
131 ddc::Coordinate<T>(end_time),
132 nb_time_steps + 1));
133
134 ddc::DiscreteDomain<DDimX, DDimY> const xy_domain(x_domain, y_domain);
135
136 // Maps temperature into the full domain (including ghosts) twice:
137 // - once for the last fully computed time-step
138 ddc::Chunk _last_temp("_last_temp", xy_domain, ddc::DeviceAllocator<double>());
139
140 // - once for time-step being computed
141 ddc::Chunk _next_temp("_next_temp", xy_domain, ddc::DeviceAllocator<double>());
142
143 ddc::ChunkSpan const initial_temp = _last_temp.span_view();
144 // Initialize the temperature on the main domain
146 xy_domain,
147 KOKKOS_LAMBDA(ddc::DiscreteElement<DDimX, DDimY> const ixy) {
148 double const x = ddc::coordinate(ddc::DiscreteElement<DDimX>(ixy));
149 double const y = ddc::coordinate(ddc::DiscreteElement<DDimY>(ixy));
150 initial_temp(ixy) = 9.999 * ((x * x + y * y) < 0.25);
151 });
152
153 ddc::Chunk _host_temp = ddc::create_mirror(_last_temp.span_cview());
154
155 // display the initial data
156 ddc::parallel_deepcopy(_host_temp, _last_temp);
157 display(ddc::coordinate(time_domain.front()), _host_temp.span_cview());
158 // time of the iteration where the last output happened
159 ddc::DiscreteElement<DDimT> last_output = time_domain.front();
160
163
165 = ddc::fourier_mesh<DDimFx, DDimFy>(xy_domain, false);
167 Ff_allocation("Ff_allocation", k_mesh, ddc::DeviceAllocator<Kokkos::complex<double>>());
168 ddc::ChunkSpan const Ff = Ff_allocation.span_view();
169
170 for (ddc::DiscreteElement<DDimT> const iter :
171 time_domain.remove_first(ddc::DiscreteVector<DDimT>(1))) {
172 // a span excluding ghosts of the temperature at the time-step we
173 // will build
174 ddc::ChunkSpan const next_temp = _next_temp.span_view();
175 // a read-only view of the temperature at the previous time-step
176 ddc::ChunkSpan const last_temp = _last_temp.span_view();
177
178 // Stencil computation on the main domain
180 Kokkos::DefaultExecutionSpace const execution_space;
181 ddc::fft(execution_space, Ff, last_temp, kwargs);
183 execution_space,
184 k_mesh,
185 KOKKOS_LAMBDA(ddc::DiscreteElement<DDimFx, DDimFy> const ikxky) {
186 ddc::DiscreteElement<DDimFx> const ikx(ikxky);
187 ddc::DiscreteElement<DDimFy> const iky(ikxky);
188 double const rkx = ddc::coordinate(ikx);
189 double const rky = ddc::coordinate(iky);
190 Ff(ikxky) *= 1 - (kx * rkx * rkx + ky * rky * rky) * dt;
191 });
192 ddc::ifft(execution_space, next_temp, Ff, kwargs);
193
194 if (iter - last_output >= t_output_period) {
195 last_output = iter;
196 ddc::parallel_deepcopy(_host_temp, _next_temp);
197 display(ddc::coordinate(iter), _host_temp.span_cview());
198 }
199
200 // Swap our two buffers
201 std::swap(_last_temp, _next_temp);
202 }
203
204 if (last_output < time_domain.back()) {
205 ddc::parallel_deepcopy(_host_temp, _last_temp);
206 display(ddc::coordinate(time_domain.back()), _host_temp.span_cview());
207 }
208}
KOKKOS_FUNCTION constexpr span_type span_view() const
KOKKOS_FUNCTION constexpr DiscreteDomain remove_last(discrete_vector_type n) const
KOKKOS_FUNCTION constexpr discrete_element_type front() const noexcept
KOKKOS_FUNCTION constexpr discrete_element_type back() const noexcept
A DiscreteElement identifies an element of the discrete dimension.
A DiscreteVector is a vector in the discrete dimension.
PeriodicSampling models a periodic discretization of the provided continuous dimension.
UniformPointSampling models a uniform discretization of the provided continuous dimension.
The top-level namespace of DDC.
void ifft(ExecSpace const &exec_space, ddc::ChunkSpan< Tout, ddc::DiscreteDomain< DDimX... >, LayoutOut, MemorySpace > out, ddc::ChunkSpan< Tin, ddc::DiscreteDomain< DDimFx... >, LayoutIn, MemorySpace > in, ddc::kwArgs_fft kwargs={ddc::FFT_Normalization::OFF})
Perform an inverse Fast Fourier Transform.
Definition fft.hpp:406
auto parallel_deepcopy(ChunkDst &&dst, ChunkSrc &&src)
Copy the content of a borrowed chunk into another.
std::ostream & print_content(std::ostream &os, ChunkSpan< ElementType, SupportType, LayoutStridedPolicy, MemorySpace > const &chunk_span)
Print the content of a ChunkSpan.
Definition print.hpp:350
T host_transform_reduce(Support const &domain, T neutral, BinaryReductionOp &&reduce, UnaryTransformOp &&transform) noexcept
A reduction over a nD domain in serial.
void fft(ExecSpace const &exec_space, ddc::ChunkSpan< Tout, ddc::DiscreteDomain< DDimFx... >, LayoutOut, MemorySpace > out, ddc::ChunkSpan< Tin, ddc::DiscreteDomain< DDimX... >, LayoutIn, MemorySpace > in, ddc::kwArgs_fft kwargs={ddc::FFT_Normalization::OFF})
Perform a direct Fast Fourier Transform.
Definition fft.hpp:353
@ BACKWARD
No normalization for forward FFT, multiply by 1/N for backward FFT.
KOKKOS_FUNCTION Coordinate< typename DDims::continuous_dimension_type... > coordinate(DiscreteElement< DDims... > const &c)
void init_discrete_space(Args &&... args)
Initialize (emplace) a global singleton discrete space.
detail::TaggedVector< CoordinateElement, CDims... > Coordinate
A Coordinate represents a coordinate in the continuous space.
void parallel_for_each(std::string const &label, ExecSpace const &execution_space, Support const &domain, Functor &&f) noexcept
iterates over a nD domain using a given Kokkos execution space
auto create_mirror(Space const &space, ChunkSpan< ElementType, Support, Layout, MemorySpace > const &src)
A structure embedding the configuration of the exposed FFT function with the type of normalization.
Definition fft.hpp:318