DDC 0.5.2
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
21struct DDimX : ddc::UniformPointSampling<X>
22{
23};
24
25struct DDimFx : ddc::PeriodicSampling<ddc::Fourier<X>>
26{
27};
28
29// Our second continuous dimension
30struct Y;
31// Its uniform discretization
32struct DDimY : ddc::UniformPointSampling<Y>
33{
34};
35
36struct DDimFy : ddc::PeriodicSampling<ddc::Fourier<Y>>
37{
38};
39
40// Our simulated time dimension
41struct T;
42// Its uniform discretization
43struct DDimT : ddc::UniformPointSampling<T>
44{
45};
46
51template <class ChunkType>
52void display(double time, ChunkType temp)
53{
54 double const mean_temp
55 = ddc::transform_reduce(temp.domain(), 0., ddc::reducer::sum<double>(), temp)
56 / temp.domain().size();
57 std::cout << std::fixed << std::setprecision(3);
58 std::cout << "At t = " << time << ",\n";
59 std::cout << " * mean temperature = " << mean_temp << "\n";
60 // take a slice in the middle of the box
61 ddc::ChunkSpan const temp_slice
62 = temp[ddc::get_domain<DDimY>(temp).front() + ddc::get_domain<DDimY>(temp).size() / 2];
63 std::cout << " * temperature[y:" << ddc::get_domain<DDimY>(temp).size() / 2 << "] = {";
65 std::cout << std::setw(6) << temp_slice(ix);
66 });
67 std::cout << " }\n" << std::flush;
68}
69
70int main(int argc, char** argv)
71{
72 Kokkos::ScopeGuard const kokkos_scope(argc, argv);
73 ddc::ScopeGuard const ddc_scope(argc, argv);
74
75 // some parameters that would typically be read from some form of
76 // configuration file in a more realistic code
77
78 // Start of the domain of interest in the X dimension
79 double const x_start = -1.;
80 // End of the domain of interest in the X dimension
81 double const x_end = 1.;
82 // Number of discretization points in the X dimension
83 std::size_t const nb_x_points = 10;
84 // Thermal diffusion coefficient
85 double const kx = .01;
86 // Start of the domain of interest in the Y dimension
87 double const y_start = -1.;
88 // End of the domain of interest in the Y dimension
89 double const y_end = 1.;
90 // Number of discretization points in the Y dimension
91 std::size_t const nb_y_points = 100;
92 // Thermal diffusion coefficient
93 double const ky = .002;
94 // Simulated time at which to start simulation
95 double const start_time = 0.;
96 // Simulated time to reach as target of the simulation
97 double const end_time = 10.;
98 // Number of time-steps between outputs
99 std::ptrdiff_t const t_output_period = 10;
100
101 // Initialization of the global domain in X including periodic point to have correct step
102 auto const x_domain_with_periodic_point = ddc::init_discrete_space<DDimX>(DDimX::init<DDimX>(
103 ddc::Coordinate<X>(x_start),
104 ddc::Coordinate<X>(x_end),
105 ddc::DiscreteVector<DDimX>(nb_x_points)));
106 ddc::DiscreteDomain<DDimX> const x_domain
107 = x_domain_with_periodic_point.remove_last(ddc::DiscreteVector<DDimX>(1));
108
109 // Initialization of the global domain in Y including periodic point to have correct step
110 auto const y_domain_with_periodic_point = ddc::init_discrete_space<DDimY>(DDimY::init<DDimY>(
111 ddc::Coordinate<Y>(y_start),
112 ddc::Coordinate<Y>(y_end),
113 ddc::DiscreteVector<DDimY>(nb_y_points)));
114 ddc::DiscreteDomain<DDimY> const y_domain
115 = y_domain_with_periodic_point.remove_last(ddc::DiscreteVector<DDimY>(1));
116
117 double const max_rkx = Kokkos::numbers::pi / ddc::step<DDimX>();
118 double const max_rky = Kokkos::numbers::pi / ddc::step<DDimY>();
119 ddc::Coordinate<T> const dt(2. / (kx * max_rkx * max_rkx + ky * max_rky * max_rky));
120
121 // number of time intervals required to reach the end time
122 ddc::DiscreteVector<DDimT> const nb_time_steps(std::ceil((end_time - start_time) / dt) + .2);
123 // Initialization of the global domain in time:
124 // - the number of discrete time-points is equal to the number of
125 // steps + 1
126 ddc::DiscreteDomain<DDimT> const time_domain
127 = ddc::init_discrete_space<DDimT>(DDimT::init<DDimT>(
128 ddc::Coordinate<T>(start_time),
129 ddc::Coordinate<T>(end_time),
130 nb_time_steps + 1));
131
132 ddc::DiscreteDomain<DDimX, DDimY> const xy_domain(x_domain, y_domain);
133
134 // Maps temperature into the full domain (including ghosts) twice:
135 // - once for the last fully computed time-step
136 ddc::Chunk _last_temp("_last_temp", xy_domain, ddc::DeviceAllocator<double>());
137
138 // - once for time-step being computed
139 ddc::Chunk _next_temp("_next_temp", xy_domain, ddc::DeviceAllocator<double>());
140
141 ddc::ChunkSpan const initial_temp = _last_temp.span_view();
142 // Initialize the temperature on the main domain
144 xy_domain,
145 KOKKOS_LAMBDA(ddc::DiscreteElement<DDimX, DDimY> const ixy) {
146 double const x = ddc::coordinate(ddc::DiscreteElement<DDimX>(ixy));
147 double const y = ddc::coordinate(ddc::DiscreteElement<DDimY>(ixy));
148 initial_temp(ixy) = 9.999 * ((x * x + y * y) < 0.25);
149 });
150
151 ddc::Chunk _host_temp = ddc::create_mirror(_last_temp.span_cview());
152
153 // display the initial data
154 ddc::parallel_deepcopy(_host_temp, _last_temp);
155 display(ddc::coordinate(time_domain.front()), _host_temp.span_cview());
156 // time of the iteration where the last output happened
157 ddc::DiscreteElement<DDimT> last_output = time_domain.front();
158
161
163 = ddc::fourier_mesh<DDimFx, DDimFy>(xy_domain, false);
165 Ff_allocation("Ff_allocation", k_mesh, ddc::DeviceAllocator<Kokkos::complex<double>>());
166 ddc::ChunkSpan const Ff = Ff_allocation.span_view();
167
168 for (ddc::DiscreteElement<DDimT> const iter :
169 time_domain.remove_first(ddc::DiscreteVector<DDimT>(1))) {
170 // a span excluding ghosts of the temperature at the time-step we
171 // will build
172 ddc::ChunkSpan const next_temp = _next_temp.span_view();
173 // a read-only view of the temperature at the previous time-step
174 ddc::ChunkSpan const last_temp = _last_temp.span_view();
175
176 // Stencil computation on the main domain
178 Kokkos::DefaultExecutionSpace const execution_space;
179 ddc::fft(execution_space, Ff, last_temp, kwargs);
181 execution_space,
182 k_mesh,
183 KOKKOS_LAMBDA(ddc::DiscreteElement<DDimFx, DDimFy> const ikxky) {
184 ddc::DiscreteElement<DDimFx> const ikx(ikxky);
185 ddc::DiscreteElement<DDimFy> const iky(ikxky);
186 double const rkx = ddc::coordinate(ikx);
187 double const rky = ddc::coordinate(iky);
188 Ff(ikxky) *= 1 - (kx * rkx * rkx + ky * rky * rky) * dt;
189 });
190 ddc::ifft(execution_space, next_temp, Ff, kwargs);
191
192 if (iter - last_output >= t_output_period) {
193 last_output = iter;
194 ddc::parallel_deepcopy(_host_temp, _next_temp);
195 display(ddc::coordinate(iter), _host_temp.span_cview());
196 }
197
198 // Swap our two buffers
199 std::swap(_last_temp, _next_temp);
200 }
201
202 if (last_output < time_domain.back()) {
203 ddc::parallel_deepcopy(_host_temp, _last_temp);
204 display(ddc::coordinate(time_domain.back()), _host_temp.span_cview());
205 }
206}
KOKKOS_FUNCTION constexpr size_type size() const noexcept
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:430
auto parallel_deepcopy(ChunkDst &&dst, ChunkSrc &&src)
Copy the content of a borrowed chunk into another.
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:377
@ BACKWARD
No normalization for forward FFT, multiply by 1/N for backward FFT.
KOKKOS_FUNCTION Coordinate< typename DDim::continuous_dimension_type... > coordinate(DiscreteElement< DDim... > const &c)
void init_discrete_space(Args &&... args)
Initialize (emplace) a global singleton discrete space.
T transform_reduce(Support const &domain, T neutral, BinaryReductionOp &&reduce, UnaryTransformOp &&transform) noexcept
A reduction over a nD domain in serial.
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
void for_each(Support const &domain, Functor &&f) noexcept
iterates over a nD domain in serial
Definition for_each.hpp:43
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:342