DDC 0.5.0
Loading...
Searching...
No Matches
examples/uniform_heat_equation.cpp
1// Copyright (C) The DDC development team, see COPYRIGHT.md file
2//
3// SPDX-License-Identifier: MIT
4
6#include <cmath>
7#include <cstddef>
8#include <iomanip>
9#include <iostream>
10#include <string>
11#include <utility>
12
13#include <ddc/ddc.hpp>
14
15#include <Kokkos_Core.hpp>
17
19struct X;
21
23struct DDimX : ddc::UniformPointSampling<X>
24{
25};
27
29struct Y;
30struct DDimY : ddc::UniformPointSampling<Y>
31{
32};
34
36struct T;
37struct DDimT : ddc::UniformPointSampling<T>
38{
39};
41
49template <class ChunkType>
50void display(double time, ChunkType temp)
51{
52 double const mean_temp
53 = ddc::transform_reduce(temp.domain(), 0., ddc::reducer::sum<double>(), temp)
54 / temp.domain().size();
55 std::cout << std::fixed << std::setprecision(3);
56 std::cout << "At t = " << time << ",\n";
57 std::cout << " * mean temperature = " << mean_temp << "\n";
58 ddc::ChunkSpan const temp_slice
59 = temp[ddc::get_domain<DDimY>(temp).front() + ddc::get_domain<DDimY>(temp).size() / 2];
60 std::cout << " * temperature[y:" << ddc::get_domain<DDimY>(temp).size() / 2 << "] = {";
62 std::cout << std::setw(6) << temp_slice(ix);
63 });
64 std::cout << " }\n" << std::flush;
65}
67
68int main(int argc, char** argv)
69{
70 Kokkos::ScopeGuard const kokkos_scope(argc, argv);
71 ddc::ScopeGuard const ddc_scope(argc, argv);
72
75 double const x_start = -1.;
76 double const x_end = 1.;
77 std::size_t const nb_x_points = 10;
78 double const kx = .01;
81 double const y_start = -1.;
82 double const y_end = 1.;
83 std::size_t const nb_y_points = 100;
84 double const ky = .002;
87 double const start_time = 0.;
88 double const end_time = 10.;
90 std::ptrdiff_t const t_output_period = 10;
92
94 ddc::DiscreteVector<DDimX> const gwx(1);
96
98 auto const [x_domain, ghosted_x_domain, x_pre_ghost, x_post_ghost]
99 = ddc::init_discrete_space<DDimX>(DDimX::init_ghosted<DDimX>(
100 ddc::Coordinate<X>(x_start),
101 ddc::Coordinate<X>(x_end),
102 ddc::DiscreteVector<DDimX>(nb_x_points),
103 gwx));
105
108 x_post_mirror(x_post_ghost.front() - x_domain.extents(), x_post_ghost.extents());
110 x_pre_mirror(x_pre_ghost.front() + x_domain.extents(), x_pre_ghost.extents());
112
114 ddc::DiscreteVector<DDimY> const gwy(1);
115
116 auto const [y_domain, ghosted_y_domain, y_pre_ghost, y_post_ghost]
117 = ddc::init_discrete_space<DDimY>(DDimY::init_ghosted<DDimY>(
118 ddc::Coordinate<Y>(y_start),
119 ddc::Coordinate<Y>(y_end),
120 ddc::DiscreteVector<DDimY>(nb_y_points),
121 gwy));
122
124 y_post_mirror(y_post_ghost.front() - y_domain.extents(), y_post_ghost.extents());
125
127 y_pre_mirror(y_pre_ghost.front() + y_domain.extents(), y_pre_ghost.extents());
129
131 double const dx = ddc::step<DDimX>();
132 double const dy = ddc::step<DDimY>();
133 double const invdx2 = 1. / (dx * dx);
134 double const invdy2 = 1. / (dy * dy);
135
136 ddc::Coordinate<T> const dt(.5 / (kx * invdx2 + ky * invdy2));
138
140 ddc::DiscreteVector<DDimT> const nb_time_steps(std::ceil((end_time - start_time) / dt) + .2);
141
142 ddc::DiscreteDomain<DDimT> const time_domain
143 = ddc::init_discrete_space<DDimT>(DDimT::init<DDimT>(
144 ddc::Coordinate<T>(start_time),
145 ddc::Coordinate<T>(end_time),
146 nb_time_steps + 1));
148
150 ddc::Chunk ghosted_last_temp(
151 "ghosted_last_temp",
152 ddc::DiscreteDomain<DDimX, DDimY>(ghosted_x_domain, ghosted_y_domain),
154
155 ddc::Chunk ghosted_next_temp(
156 "ghosted_next_temp",
157 ddc::DiscreteDomain<DDimX, DDimY>(ghosted_x_domain, ghosted_y_domain),
160
162 ddc::ChunkSpan const ghosted_initial_temp = ghosted_last_temp.span_view();
164
167 ddc::DiscreteDomain<DDimX, DDimY>(x_domain, y_domain),
168 KOKKOS_LAMBDA(ddc::DiscreteElement<DDimX, DDimY> const ixy) {
169 double const x = ddc::coordinate(ddc::DiscreteElement<DDimX>(ixy));
170 double const y = ddc::coordinate(ddc::DiscreteElement<DDimY>(ixy));
171 ghosted_initial_temp(ixy) = 9.999 * ((x * x + y * y) < 0.25);
172 });
174
176 ddc::Chunk ghosted_temp = ddc::create_mirror(ghosted_last_temp.span_cview());
178
180 ddc::parallel_deepcopy(ghosted_temp, ghosted_last_temp);
182
184 display(ddc::coordinate(time_domain.front()), ghosted_temp[x_domain][y_domain]);
186
188 ddc::DiscreteElement<DDimT> last_output_iter = time_domain.front();
190
192 for (ddc::DiscreteElement<DDimT> const iter :
193 time_domain.remove_first(ddc::DiscreteVector<DDimT>(1))) {
195
197 for (ddc::DiscreteVectorElement ix = 0; ix < x_pre_ghost.extents().value(); ++ix) {
199 ghosted_last_temp[x_pre_ghost[ix]][y_domain],
200 ghosted_last_temp[x_pre_mirror[ix]][y_domain]);
201 }
202 for (ddc::DiscreteVectorElement ix = 0; ix < x_post_ghost.extents().value(); ++ix) {
204 ghosted_last_temp[x_post_ghost[ix]][y_domain],
205 ghosted_last_temp[x_post_mirror[ix]][y_domain]);
206 }
207 for (ddc::DiscreteVectorElement iy = 0; iy < y_pre_ghost.extents().value(); ++iy) {
209 ghosted_last_temp[x_domain][y_pre_ghost[iy]],
210 ghosted_last_temp[x_domain][y_pre_mirror[iy]]);
211 }
212 for (ddc::DiscreteVectorElement iy = 0; iy < y_post_ghost.extents().value(); ++iy) {
214 ghosted_last_temp[x_domain][y_post_ghost[iy]],
215 ghosted_last_temp[x_domain][y_post_mirror[iy]]);
216 }
218
220 ddc::ChunkSpan const next_temp(
221 ghosted_next_temp[ddc::DiscreteDomain<DDimX, DDimY>(x_domain, y_domain)]);
222 ddc::ChunkSpan const last_temp(ghosted_last_temp.span_cview());
224
227 next_temp.domain(),
228 KOKKOS_LAMBDA(ddc::DiscreteElement<DDimX, DDimY> const ixy) {
229 ddc::DiscreteElement<DDimX> const ix(ixy);
230 ddc::DiscreteElement<DDimY> const iy(ixy);
231 double const dt = ddc::step<DDimT>();
232
233 next_temp(ix, iy) = last_temp(ix, iy);
234 next_temp(ix, iy) += kx * dt
235 * (last_temp(ix + 1, iy) - 2.0 * last_temp(ix, iy)
236 + last_temp(ix - 1, iy))
237 * invdx2;
238
239 next_temp(ix, iy) += ky * dt
240 * (last_temp(ix, iy + 1) - 2.0 * last_temp(ix, iy)
241 + last_temp(ix, iy - 1))
242 * invdy2;
243 });
245
247 if (iter - last_output_iter >= t_output_period) {
248 last_output_iter = iter;
249 ddc::parallel_deepcopy(ghosted_temp, ghosted_next_temp);
250 display(ddc::coordinate(iter),
251 ghosted_temp[ddc::DiscreteDomain<DDimX, DDimY>(x_domain, y_domain)]);
252 }
254
256 std::swap(ghosted_last_temp, ghosted_next_temp);
258 }
259
261 if (last_output_iter < time_domain.back()) {
262 ddc::parallel_deepcopy(ghosted_temp, ghosted_last_temp);
263 display(ddc::coordinate(time_domain.back()),
264 ghosted_temp[ddc::DiscreteDomain<DDimX, DDimY>(x_domain, y_domain)]);
265 }
267}
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.
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.
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)