DDC 0.10.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 <tuple>
12#include <utility>
13
14#include <ddc/ddc.hpp>
15
16#include <Kokkos_Core.hpp>
18
20struct X;
22
24struct DDimX : ddc::UniformPointSampling<X>
25{
26};
28
30struct Y;
31struct DDimY : ddc::UniformPointSampling<Y>
32{
33};
35
37struct T;
38struct DDimT : ddc::UniformPointSampling<T>
39{
40};
42
50template <class ChunkType>
51void display(double time, ChunkType temp)
52{
53 double const mean_temp
54 = ddc::host_transform_reduce(temp.domain(), 0., ddc::reducer::sum<double>(), temp)
55 / temp.domain().size();
56 std::cout << std::fixed << std::setprecision(3);
57 std::cout << "At t = " << time << ",\n";
58 std::cout << " * mean temperature = " << mean_temp << "\n";
59 ddc::ChunkSpan const temp_slice
60 = temp[ddc::get_domain<DDimY>(temp).front() + ddc::get_domain<DDimY>(temp).size() / 2];
61 std::cout << " * temperature[y:" << ddc::get_domain<DDimY>(temp).size() / 2 << "] = {";
63 std::cout << std::setw(6) << temp_slice(ix);
64 });
65 std::cout << " }\n" << std::flush;
66}
68
69int main(int argc, char** argv)
70{
71 Kokkos::ScopeGuard const kokkos_scope(argc, argv);
72 ddc::ScopeGuard const ddc_scope(argc, argv);
73
76 double const x_start = -1.;
77 double const x_end = 1.;
78 std::size_t const nb_x_points = 10;
79 double const kx = .01;
82 double const y_start = -1.;
83 double const y_end = 1.;
84 std::size_t const nb_y_points = 100;
85 double const ky = .002;
88 double const start_time = 0.;
89 double const end_time = 10.;
91 std::ptrdiff_t const t_output_period = 10;
93
95 ddc::DiscreteVector<DDimX> const gwx(1);
97
99 auto const [x_domain, ghosted_x_domain, x_pre_ghost, x_post_ghost]
100 = ddc::init_discrete_space<DDimX>(DDimX::init_ghosted<DDimX>(
101 ddc::Coordinate<X>(x_start),
102 ddc::Coordinate<X>(x_end),
103 ddc::DiscreteVector<DDimX>(nb_x_points),
104 gwx));
106
109 x_post_mirror(x_post_ghost.front() - x_domain.extents(), x_post_ghost.extents());
111 x_pre_mirror(x_pre_ghost.front() + x_domain.extents(), x_pre_ghost.extents());
113
115 ddc::DiscreteVector<DDimY> const gwy(1);
116
117 auto const [y_domain, ghosted_y_domain, y_pre_ghost, y_post_ghost]
118 = ddc::init_discrete_space<DDimY>(DDimY::init_ghosted<DDimY>(
119 ddc::Coordinate<Y>(y_start),
120 ddc::Coordinate<Y>(y_end),
121 ddc::DiscreteVector<DDimY>(nb_y_points),
122 gwy));
123
125 y_post_mirror(y_post_ghost.front() - y_domain.extents(), y_post_ghost.extents());
126
128 y_pre_mirror(y_pre_ghost.front() + y_domain.extents(), y_pre_ghost.extents());
130
132 double const dx = ddc::step<DDimX>();
133 double const dy = ddc::step<DDimY>();
134 double const invdx2 = 1. / (dx * dx);
135 double const invdy2 = 1. / (dy * dy);
136
137 ddc::Coordinate<T> const dt(.5 / (kx * invdx2 + ky * invdy2));
139
141 ddc::DiscreteVector<DDimT> const nb_time_steps(std::ceil((end_time - start_time) / dt) + .2);
142
143 ddc::DiscreteDomain<DDimT> const time_domain
144 = ddc::init_discrete_space<DDimT>(DDimT::init<DDimT>(
145 ddc::Coordinate<T>(start_time),
146 ddc::Coordinate<T>(end_time),
147 nb_time_steps + 1));
149
151 ddc::Chunk ghosted_last_temp(
152 "ghosted_last_temp",
153 ddc::DiscreteDomain<DDimX, DDimY>(ghosted_x_domain, ghosted_y_domain),
155
156 ddc::Chunk ghosted_next_temp(
157 "ghosted_next_temp",
158 ddc::DiscreteDomain<DDimX, DDimY>(ghosted_x_domain, ghosted_y_domain),
161
163 ddc::ChunkSpan const ghosted_initial_temp = ghosted_last_temp.span_view();
165
168 ddc::DiscreteDomain<DDimX, DDimY>(x_domain, y_domain),
169 KOKKOS_LAMBDA(ddc::DiscreteElement<DDimX, DDimY> const ixy) {
170 double const x = ddc::coordinate(ddc::DiscreteElement<DDimX>(ixy));
171 double const y = ddc::coordinate(ddc::DiscreteElement<DDimY>(ixy));
172 ghosted_initial_temp(ixy) = 9.999 * ((x * x + y * y) < 0.25);
173 });
175
177 ddc::Chunk ghosted_temp = ddc::create_mirror(ghosted_last_temp.span_cview());
179
181 ddc::parallel_deepcopy(ghosted_temp, ghosted_last_temp);
183
185 display(ddc::coordinate(time_domain.front()), ghosted_temp[x_domain][y_domain]);
187
189 ddc::DiscreteElement<DDimT> last_output_iter = time_domain.front();
191
193 for (ddc::DiscreteElement<DDimT> const iter :
194 time_domain.remove_first(ddc::DiscreteVector<DDimT>(1))) {
196
198 for (ddc::DiscreteVectorElement ix = 0; ix < x_pre_ghost.extents().value(); ++ix) {
200 ghosted_last_temp[x_pre_ghost[ix]][y_domain],
201 ghosted_last_temp[x_pre_mirror[ix]][y_domain]);
202 }
203 for (ddc::DiscreteVectorElement ix = 0; ix < x_post_ghost.extents().value(); ++ix) {
205 ghosted_last_temp[x_post_ghost[ix]][y_domain],
206 ghosted_last_temp[x_post_mirror[ix]][y_domain]);
207 }
208 for (ddc::DiscreteVectorElement iy = 0; iy < y_pre_ghost.extents().value(); ++iy) {
210 ghosted_last_temp[x_domain][y_pre_ghost[iy]],
211 ghosted_last_temp[x_domain][y_pre_mirror[iy]]);
212 }
213 for (ddc::DiscreteVectorElement iy = 0; iy < y_post_ghost.extents().value(); ++iy) {
215 ghosted_last_temp[x_domain][y_post_ghost[iy]],
216 ghosted_last_temp[x_domain][y_post_mirror[iy]]);
217 }
219
221 ddc::ChunkSpan const next_temp(
222 ghosted_next_temp[ddc::DiscreteDomain<DDimX, DDimY>(x_domain, y_domain)]);
223 ddc::ChunkSpan const last_temp(ghosted_last_temp.span_cview());
225
228 next_temp.domain(),
229 KOKKOS_LAMBDA(ddc::DiscreteElement<DDimX, DDimY> const ixy) {
230 ddc::DiscreteElement<DDimX> const ix(ixy);
231 ddc::DiscreteElement<DDimY> const iy(ixy);
232 double const dt = ddc::step<DDimT>();
233
234 next_temp(ix, iy) = last_temp(ix, iy);
235 next_temp(ix, iy) += kx * dt
236 * (last_temp(ix + 1, iy) - 2.0 * last_temp(ix, iy)
237 + last_temp(ix - 1, iy))
238 * invdx2;
239
240 next_temp(ix, iy) += ky * dt
241 * (last_temp(ix, iy + 1) - 2.0 * last_temp(ix, iy)
242 + last_temp(ix, iy - 1))
243 * invdy2;
244 });
246
248 if (iter - last_output_iter >= t_output_period) {
249 last_output_iter = iter;
250 ddc::parallel_deepcopy(ghosted_temp, ghosted_next_temp);
251 display(ddc::coordinate(iter),
252 ghosted_temp[ddc::DiscreteDomain<DDimX, DDimY>(x_domain, y_domain)]);
253 }
255
257 std::swap(ghosted_last_temp, ghosted_next_temp);
259 }
260
262 if (last_output_iter < time_domain.back()) {
263 ddc::parallel_deepcopy(ghosted_temp, ghosted_last_temp);
264 display(ddc::coordinate(time_domain.back()),
265 ghosted_temp[ddc::DiscreteDomain<DDimX, DDimY>(x_domain, y_domain)]);
266 }
268}
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.
T host_transform_reduce(Support const &domain, T neutral, BinaryReductionOp &&reduce, UnaryTransformOp &&transform) noexcept
A reduction over a nD domain in serial.
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.
void host_for_each(Support const &domain, Functor &&f) noexcept
iterates over a nD domain in serial
Definition for_each.hpp:74
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
auto create_mirror(Space const &space, ChunkSpan< ElementType, Support, Layout, MemorySpace > const &src)