DDC 0.5.2
Loading...
Searching...
No Matches
examples/non_uniform_heat_equation.cpp
1// Copyright (C) The DDC development team, see COPYRIGHT.md file
2//
3// SPDX-License-Identifier: MIT
4
6#include <algorithm>
7#include <cassert>
8#include <cmath>
9#include <cstddef>
10#include <iomanip>
11#include <iostream>
12#include <random>
13#include <string>
14#include <utility>
15#include <vector>
16
17#include <ddc/ddc.hpp>
18
19#include <Kokkos_Core.hpp>
21
23std::vector<double> generate_random_vector(int n, double lower_bound, double higher_bound)
24{
25 assert(n > 1);
26 assert(lower_bound < higher_bound);
27
28 std::random_device rd;
29 std::mt19937 gen(rd());
30 // p represents the fraction of displacement
31 // it should be less than 0.5 to avoid reordering of nodes
32 double const p = 0.1;
33 std::uniform_real_distribution<double> dis(-p, +p);
34
35 double const dx = (higher_bound - lower_bound) / (n - 1);
36
37 std::vector<double> points(n);
38
39 // Generate a uniform mesh
40 for (int i = 0; i < n; ++i) {
41 points[i] = lower_bound + i * dx;
42 }
43 // Add a random perturbation
44 for (int i = 1; i < n - 1; ++i) {
45 points[i] += dis(gen) * dx;
46 }
47
48 assert(std::is_sorted(points.begin(), points.end()));
49
50 return points;
51}
53
54std::vector<double> periodic_extrapolation_left(int gw, std::vector<double> const& points)
55{
56 assert(gw >= 0);
57 assert(points.size() > gw);
58 assert(std::is_sorted(points.begin(), points.end()));
59
60 std::vector<double> ghost(gw);
61 if (gw > 0) {
62 double const period = points.back() - points.front();
63 auto const it = std::next(points.crbegin());
64 std::transform(it, std::next(it, gw), ghost.rbegin(), [period](double pos) {
65 return pos - period;
66 });
67 }
68
69 return ghost;
70}
71
72std::vector<double> periodic_extrapolation_right(int gw, std::vector<double> const& points)
73{
74 assert(gw >= 0);
75 assert(points.size() > gw);
76 assert(std::is_sorted(points.begin(), points.end()));
77
78 std::vector<double> ghost(gw);
79 if (gw > 0) {
80 double const period = points.back() - points.front();
81 auto const it = std::next(points.cbegin());
82 std::transform(it, std::next(it, gw), ghost.begin(), [period](double pos) {
83 return pos + period;
84 });
85 }
86
87 return ghost;
88}
89
91struct X;
93
95struct DDimX : ddc::NonUniformPointSampling<X>
96{
97};
99
101struct Y;
102struct DDimY : ddc::NonUniformPointSampling<Y>
103{
104};
106
108struct T;
109struct DDimT : ddc::UniformPointSampling<T>
110{
111};
113
121template <class ChunkType>
122void display(double time, ChunkType temp)
123{
124 double const mean_temp
125 = ddc::transform_reduce(temp.domain(), 0., ddc::reducer::sum<double>(), temp)
126 / temp.domain().size();
127 std::cout << std::fixed << std::setprecision(3);
128 std::cout << "At t = " << time << ",\n";
129 std::cout << " * mean temperature = " << mean_temp << "\n";
130 ddc::ChunkSpan const temp_slice
131 = temp[ddc::get_domain<DDimY>(temp).front() + ddc::get_domain<DDimY>(temp).size() / 2];
132 std::cout << " * temperature[y:" << ddc::get_domain<DDimY>(temp).size() / 2 << "] = {";
134 std::cout << std::setw(6) << temp_slice(ix);
135 });
136 std::cout << " }\n" << std::flush;
137}
139
140int main(int argc, char** argv)
141{
142 Kokkos::ScopeGuard const kokkos_scope(argc, argv);
143 ddc::ScopeGuard const ddc_scope(argc, argv);
144
147 double const x_start = -1.;
148 double const x_end = 1.;
149 std::size_t const nb_x_points = 10;
150 double const kx = .01;
153 double const y_start = -1.;
154 double const y_end = 1.;
155 std::size_t const nb_y_points = 100;
156 double const ky = .002;
159 double const start_time = 0.;
160 double const end_time = 10.;
162 std::ptrdiff_t const t_output_period = 10;
164
166 std::vector<double> const x_domain_vect = generate_random_vector(nb_x_points, x_start, x_end);
168
170 std::vector<double> const x_pre_ghost_vect = periodic_extrapolation_left(1, x_domain_vect);
171 std::vector<double> const x_post_ghost_vect = periodic_extrapolation_right(1, x_domain_vect);
173
175 auto const [x_domain, ghosted_x_domain, x_pre_ghost, x_post_ghost]
177 DDimX::init_ghosted<DDimX>(x_domain_vect, x_pre_ghost_vect, x_post_ghost_vect));
179
181 x_post_mirror(x_post_ghost.front() - x_domain.extents(), x_post_ghost.extents());
183 x_pre_mirror(x_pre_ghost.front() + x_domain.extents(), x_pre_ghost.extents());
184
186 std::vector<double> const y_domain_vect = generate_random_vector(nb_y_points, y_start, y_end);
187
189 std::vector<double> const y_pre_ghost_vect = periodic_extrapolation_left(1, y_domain_vect);
190 std::vector<double> const y_post_ghost_vect = periodic_extrapolation_right(1, y_domain_vect);
193
195 auto const [y_domain, ghosted_y_domain, y_pre_ghost, y_post_ghost]
197 DDimY::init_ghosted<DDimY>(y_domain_vect, y_pre_ghost_vect, y_post_ghost_vect));
199
201 y_post_mirror(y_post_ghost.front() - y_domain.extents(), y_post_ghost.extents());
202
204 y_pre_mirror(y_pre_ghost.front() + y_domain.extents(), y_pre_ghost.extents());
205
207 double const invdx2_max = ddc::transform_reduce(
208 x_domain,
209 0.,
212 return 1. / (ddc::distance_at_left(ix) * ddc::distance_at_right(ix));
213 });
214
215 double const invdy2_max = ddc::transform_reduce(
216 y_domain,
217 0.,
220 return 1. / (ddc::distance_at_left(iy) * ddc::distance_at_right(iy));
221 });
222
223 ddc::Coordinate<T> const max_dt(.5 / (kx * invdx2_max + ky * invdy2_max));
225
227 ddc::DiscreteVector<DDimT> const nb_time_steps(
228 std::ceil((end_time - start_time) / max_dt) + .2);
229
230 ddc::DiscreteDomain<DDimT> const time_domain
231 = ddc::init_discrete_space<DDimT>(DDimT::init<DDimT>(
232 ddc::Coordinate<T>(start_time),
233 ddc::Coordinate<T>(end_time),
234 nb_time_steps + 1));
236
238 ddc::Chunk ghosted_last_temp(
239 "ghosted_last_temp",
240 ddc::DiscreteDomain<DDimX, DDimY>(ghosted_x_domain, ghosted_y_domain),
242
243 ddc::Chunk ghosted_next_temp(
244 "ghosted_next_temp",
245 ddc::DiscreteDomain<DDimX, DDimY>(ghosted_x_domain, ghosted_y_domain),
248
250 ddc::ChunkSpan const ghosted_initial_temp = ghosted_last_temp.span_view();
252
255 ddc::DiscreteDomain<DDimX, DDimY>(x_domain, y_domain),
256 KOKKOS_LAMBDA(ddc::DiscreteElement<DDimX, DDimY> const ixy) {
257 double const x = ddc::coordinate(ddc::DiscreteElement<DDimX>(ixy));
258 double const y = ddc::coordinate(ddc::DiscreteElement<DDimY>(ixy));
259 ghosted_initial_temp(ixy) = 9.999 * ((x * x + y * y) < 0.25);
260 });
262
264 ddc::Chunk ghosted_temp = ddc::create_mirror(ghosted_last_temp.span_cview());
266
268 ddc::parallel_deepcopy(ghosted_temp, ghosted_last_temp);
270
272 display(ddc::coordinate(time_domain.front()), ghosted_temp[x_domain][y_domain]);
274
276 ddc::DiscreteElement<DDimT> last_output_iter = time_domain.front();
278
280 for (ddc::DiscreteElement<DDimT> const iter :
281 time_domain.remove_first(ddc::DiscreteVector<DDimT>(1))) {
283
285 for (ddc::DiscreteVectorElement ix = 0; ix < x_pre_ghost.extents().value(); ++ix) {
287 ghosted_last_temp[x_pre_ghost[ix]][y_domain],
288 ghosted_last_temp[x_pre_mirror[ix]][y_domain]);
289 }
290 for (ddc::DiscreteVectorElement ix = 0; ix < x_post_ghost.extents().value(); ++ix) {
292 ghosted_last_temp[x_post_ghost[ix]][y_domain],
293 ghosted_last_temp[x_post_mirror[ix]][y_domain]);
294 }
295 for (ddc::DiscreteVectorElement iy = 0; iy < y_pre_ghost.extents().value(); ++iy) {
297 ghosted_last_temp[x_domain][y_pre_ghost[iy]],
298 ghosted_last_temp[x_domain][y_pre_mirror[iy]]);
299 }
300 for (ddc::DiscreteVectorElement iy = 0; iy < y_post_ghost.extents().value(); ++iy) {
302 ghosted_last_temp[x_domain][y_post_ghost[iy]],
303 ghosted_last_temp[x_domain][y_post_mirror[iy]]);
304 }
306
308 ddc::ChunkSpan const next_temp(
309 ghosted_next_temp[ddc::DiscreteDomain<DDimX, DDimY>(x_domain, y_domain)]);
310 ddc::ChunkSpan const last_temp(ghosted_last_temp.span_cview());
312
315 next_temp.domain(),
316 KOKKOS_LAMBDA(ddc::DiscreteElement<DDimX, DDimY> const ixy) {
317 ddc::DiscreteElement<DDimX> const ix(ixy);
318 ddc::DiscreteElement<DDimY> const iy(ixy);
319 double const dx_l = ddc::distance_at_left(ix);
320 double const dx_r = ddc::distance_at_right(ix);
321 double const dx_m = 0.5 * (dx_l + dx_r);
322 double const dy_l = ddc::distance_at_left(iy);
323 double const dy_r = ddc::distance_at_right(iy);
324 double const dy_m = 0.5 * (dy_l + dy_r);
325 next_temp(ix, iy) = last_temp(ix, iy);
326 next_temp(ix, iy)
327 += kx * ddc::step<DDimT>()
328 * (dx_l * last_temp(ix + 1, iy) - 2.0 * dx_m * last_temp(ix, iy)
329 + dx_r * last_temp(ix - 1, iy))
330 / (dx_l * dx_m * dx_r);
331 next_temp(ix, iy)
332 += ky * ddc::step<DDimT>()
333 * (dy_l * last_temp(ix, iy + 1) - 2.0 * dy_m * last_temp(ix, iy)
334 + dy_r * last_temp(ix, iy - 1))
335 / (dy_l * dy_m * dy_r);
336 });
338
340 if (iter - last_output_iter >= t_output_period) {
341 last_output_iter = iter;
342 ddc::parallel_deepcopy(ghosted_temp, ghosted_next_temp);
343 display(ddc::coordinate(iter),
344 ghosted_temp[ddc::DiscreteDomain<DDimX, DDimY>(x_domain, y_domain)]);
345 }
347
349 std::swap(ghosted_last_temp, ghosted_next_temp);
351 }
352
354 if (last_output_iter < time_domain.back()) {
355 ddc::parallel_deepcopy(ghosted_temp, ghosted_last_temp);
356 display(ddc::coordinate(time_domain.back()),
357 ghosted_temp[ddc::DiscreteDomain<DDimX, DDimY>(x_domain, y_domain)]);
358 }
360}
KOKKOS_FUNCTION constexpr size_type size() const noexcept
KOKKOS_FUNCTION constexpr span_type span_view() 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.
NonUniformPointSampling models a non-uniform discretization of the CDim segment .
UniformPointSampling models a uniform discretization of the provided continuous dimension.
The top-level namespace of DDC.
auto parallel_deepcopy(ChunkDst &&dst, ChunkSrc &&src)
Copy the content of a borrowed chunk into another.
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.
KOKKOS_FUNCTION Coordinate< typename DDim::continuous_dimension_type > distance_at_left(DiscreteElement< DDim > i)
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.
std::ptrdiff_t DiscreteVectorElement
A DiscreteVectorElement is a scalar that represents the difference between two coordinates.
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)
KOKKOS_FUNCTION Coordinate< typename DDim::continuous_dimension_type > distance_at_right(DiscreteElement< DDim > i)