DDC 0.15.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
21{
22};
24
26struct DDimX : ddc::UniformPointSampling<X>
27{
28};
30
32struct Y
33{
34};
35struct DDimY : ddc::UniformPointSampling<Y>
36{
37};
39
41struct T
42{
43};
44struct DDimT : ddc::UniformPointSampling<T>
45{
46};
48
56template <class ChunkType>
57void display(double time, ChunkType temp)
58{
59 double const mean_temp
60 = ddc::host_transform_reduce(temp.domain(), 0., ddc::reducer::sum<double>(), temp)
61 / temp.domain().size();
62 ddc::DiscreteVector<DDimY> const slice_id(ddc::get_domain<DDimY>(temp).size() / 2);
63 std::cout << std::fixed << std::setprecision(3);
64 std::cout << "At t = " << time << ",\n";
65 std::cout << " * mean temperature = " << mean_temp << '\n';
66 std::cout << " * temperature[y:" << slice_id.value() << "] = ";
67 ddc::print_content(std::cout, temp[slice_id]);
68 std::cout << '\n' << std::flush;
69}
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
79 double const x_start = -1.;
80 double const x_end = 1.;
81 std::size_t const nb_x_points = 10;
82 double const kx = .01;
85 double const y_start = -1.;
86 double const y_end = 1.;
87 std::size_t const nb_y_points = 100;
88 double const ky = .002;
91 double const start_time = 0.;
92 double const end_time = 10.;
94 std::ptrdiff_t const t_output_period = 10;
96
98 ddc::DiscreteVector<DDimX> const gwx(1);
100
102 auto const [x_domain, ghosted_x_domain, x_pre_ghost, x_post_ghost]
103 = ddc::init_discrete_space<DDimX>(DDimX::init_ghosted<DDimX>(
104 ddc::Coordinate<X>(x_start),
105 ddc::Coordinate<X>(x_end),
106 ddc::DiscreteVector<DDimX>(nb_x_points),
107 gwx));
109
112 x_post_mirror(x_post_ghost.front() - x_domain.extents(), x_post_ghost.extents());
114 x_pre_mirror(x_pre_ghost.front() + x_domain.extents(), x_pre_ghost.extents());
116
118 ddc::DiscreteVector<DDimY> const gwy(1);
119
120 auto const [y_domain, ghosted_y_domain, y_pre_ghost, y_post_ghost]
121 = ddc::init_discrete_space<DDimY>(DDimY::init_ghosted<DDimY>(
122 ddc::Coordinate<Y>(y_start),
123 ddc::Coordinate<Y>(y_end),
124 ddc::DiscreteVector<DDimY>(nb_y_points),
125 gwy));
126
128 y_post_mirror(y_post_ghost.front() - y_domain.extents(), y_post_ghost.extents());
129
131 y_pre_mirror(y_pre_ghost.front() + y_domain.extents(), y_pre_ghost.extents());
133
135 double const dx = ddc::step<DDimX>();
136 double const dy = ddc::step<DDimY>();
137 double const invdx2 = 1. / (dx * dx);
138 double const invdy2 = 1. / (dy * dy);
139
140 ddc::Coordinate<T> const dt(.5 / (kx * invdx2 + ky * invdy2));
142
144 ddc::DiscreteVector<DDimT> const nb_time_steps(std::ceil((end_time - start_time) / dt) + .2);
145
146 ddc::DiscreteDomain<DDimT> const time_domain
147 = ddc::init_discrete_space<DDimT>(DDimT::init<DDimT>(
148 ddc::Coordinate<T>(start_time),
149 ddc::Coordinate<T>(end_time),
150 nb_time_steps + 1));
152
154 ddc::Chunk ghosted_last_temp(
155 "ghosted_last_temp",
156 ddc::DiscreteDomain<DDimX, DDimY>(ghosted_x_domain, ghosted_y_domain),
158
159 ddc::Chunk ghosted_next_temp(
160 "ghosted_next_temp",
161 ddc::DiscreteDomain<DDimX, DDimY>(ghosted_x_domain, ghosted_y_domain),
164
166 ddc::ChunkSpan const ghosted_initial_temp = ghosted_last_temp.span_view();
168
171 ddc::DiscreteDomain<DDimX, DDimY>(x_domain, y_domain),
172 KOKKOS_LAMBDA(ddc::DiscreteElement<DDimX, DDimY> const ixy) {
173 double const x = ddc::coordinate(ddc::DiscreteElement<DDimX>(ixy));
174 double const y = ddc::coordinate(ddc::DiscreteElement<DDimY>(ixy));
175 ghosted_initial_temp(ixy) = 9.999 * ((x * x + y * y) < 0.25);
176 });
178
180 ddc::Chunk ghosted_temp = ddc::create_mirror(ghosted_last_temp.span_cview());
182
184 ddc::parallel_deepcopy(ghosted_temp, ghosted_last_temp);
186
188 display(ddc::coordinate(time_domain.front()), ghosted_temp[x_domain][y_domain]);
190
192 ddc::DiscreteElement<DDimT> last_output_iter = time_domain.front();
194
196 for (ddc::DiscreteElement<DDimT> const iter :
197 time_domain.remove_first(ddc::DiscreteVector<DDimT>(1))) {
199
201 for (ddc::DiscreteVectorElement ix = 0; ix < x_pre_ghost.extents().value(); ++ix) {
203 ghosted_last_temp[x_pre_ghost[ix]][y_domain],
204 ghosted_last_temp[x_pre_mirror[ix]][y_domain]);
205 }
206 for (ddc::DiscreteVectorElement ix = 0; ix < x_post_ghost.extents().value(); ++ix) {
208 ghosted_last_temp[x_post_ghost[ix]][y_domain],
209 ghosted_last_temp[x_post_mirror[ix]][y_domain]);
210 }
211 for (ddc::DiscreteVectorElement iy = 0; iy < y_pre_ghost.extents().value(); ++iy) {
213 ghosted_last_temp[x_domain][y_pre_ghost[iy]],
214 ghosted_last_temp[x_domain][y_pre_mirror[iy]]);
215 }
216 for (ddc::DiscreteVectorElement iy = 0; iy < y_post_ghost.extents().value(); ++iy) {
218 ghosted_last_temp[x_domain][y_post_ghost[iy]],
219 ghosted_last_temp[x_domain][y_post_mirror[iy]]);
220 }
222
224 ddc::ChunkSpan const next_temp(
225 ghosted_next_temp[ddc::DiscreteDomain<DDimX, DDimY>(x_domain, y_domain)]);
226 ddc::ChunkSpan const last_temp(ghosted_last_temp.span_cview());
228
231 next_temp.domain(),
232 KOKKOS_LAMBDA(ddc::DiscreteElement<DDimX, DDimY> const ixy) {
233 ddc::DiscreteElement<DDimX> const ix(ixy);
234 ddc::DiscreteElement<DDimY> const iy(ixy);
235 double const dt = ddc::step<DDimT>();
236
237 next_temp(ix, iy) = last_temp(ix, iy);
238 next_temp(ix, iy) += kx * dt
239 * (last_temp(ix + 1, iy) - 2.0 * last_temp(ix, iy)
240 + last_temp(ix - 1, iy))
241 * invdx2;
242
243 next_temp(ix, iy) += ky * dt
244 * (last_temp(ix, iy + 1) - 2.0 * last_temp(ix, iy)
245 + last_temp(ix, iy - 1))
246 * invdy2;
247 });
249
251 if (iter - last_output_iter >= t_output_period) {
252 last_output_iter = iter;
253 ddc::parallel_deepcopy(ghosted_temp, ghosted_next_temp);
254 display(ddc::coordinate(iter),
255 ghosted_temp[ddc::DiscreteDomain<DDimX, DDimY>(x_domain, y_domain)]);
256 }
258
260 std::swap(ghosted_last_temp, ghosted_next_temp);
262 }
263
265 if (last_output_iter < time_domain.back()) {
266 ddc::parallel_deepcopy(ghosted_temp, ghosted_last_temp);
267 display(ddc::coordinate(time_domain.back()),
268 ghosted_temp[ddc::DiscreteDomain<DDimX, DDimY>(x_domain, y_domain)]);
269 }
271}
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.
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.
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.
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)