ESPResSo
Extensible Simulation Package for Research on Soft Matter Systems
Loading...
Searching...
No Matches
greens_function.hpp
Go to the documentation of this file.
1/*
2 * Copyright (C) 2025 The ESPResSo project
3 *
4 * This file is part of ESPResSo.
5 *
6 * ESPResSo is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * ESPResSo is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 */
19
20#pragma once
21
22#include <gpu/FieldAccessor.h>
23
24#if defined(__CUDACC__)
25#include <cufft.h>
26#endif
27
28#include <cmath>
29#include <numbers>
30#include <type_traits>
31
32namespace walberla {
33
34template <typename FloatType>
35FloatType greens_function(int x, int y, int z, auto const &dim) {
36 if (x == 0 && y == 0 && z == 0)
37 return 0.;
38 auto constexpr two_pi = FloatType{2} * std::numbers::pi_v<FloatType>;
39 return FloatType(-0.5) /
40 FloatType(std::cos(two_pi * FloatType(x) / FloatType(dim[0])) +
41 std::cos(two_pi * FloatType(y) / FloatType(dim[1])) +
42 std::cos(two_pi * FloatType(z) / FloatType(dim[2])) - 3.) /
43 static_cast<FloatType>(dim[0] * dim[1] * dim[2]);
44}
45
46#if defined(__CUDACC__)
47// LCOV_EXCL_START
48template <typename FloatType>
49__global__ void
50create_greens_function(gpu::FieldAccessor<FloatType> greens_function, int x_min,
51 int y_min, int z_min, int x_max, int y_max, int z_max,
53 using RealType = std::conditional<std::is_same<FloatType, float>::value,
56 unsigned int index =
58 unsigned int local_dim[3] = {static_cast<unsigned int>(x_max - x_min),
59 static_cast<unsigned int>(y_max - y_min),
60 static_cast<unsigned int>(z_max - z_min)};
61 unsigned int yz_slice_size = index / local_dim[0];
62 auto global_coord_x = x_min + index % local_dim[0];
65 constexpr RealType two_pi{2. * M_PI};
66 if (index < local_dim[0] * local_dim[1] * local_dim[2]) {
67 if (index == 0u and x_min == 0 and y_min == 0 and z_min == 0) {
68 // setting 0th Fourier mode to 0 enforces charge neutrality
69 greens_function.get(0u) = RealType{0};
70 } else {
71 greens_function.get(0u) =
72 RealType{-0.5} /
73 (cos(two_pi * static_cast<RealType>(global_coord_x) /
74 static_cast<RealType>(global_dim_x)) +
75 cos(two_pi * static_cast<RealType>(global_coord_y) /
76 static_cast<RealType>(global_dim_y)) +
77 cos(two_pi * static_cast<RealType>(global_coord_z) /
78 static_cast<RealType>(global_dim_z)) -
79 RealType{3}) /
81 }
82 }
83}
84
85template <typename FloatType, typename ComplexType>
86__global__ void
87multiply_by_greens_function(gpu::FieldAccessor<ComplexType> potential,
88 gpu::FieldAccessor<FloatType> greens_function) {
91 if (potential.isValidPosition() && greens_function.isValidPosition()) {
92 potential.get(0u) =
93 ComplexType(potential.get(0u).x * greens_function.get(0u),
94 potential.get(0u).y * greens_function.get(0u));
95 }
96}
97
98template <typename FloatType>
99__global__ void add_fields_with_factor(gpu::FieldAccessor<FloatType> field_out,
100 gpu::FieldAccessor<FloatType> field_add,
101 FloatType const factor) {
104 if (field_out.isValidPosition() && field_add.isValidPosition()) {
105 field_out.get(0u) += field_add.get(0u) * factor;
106 }
107}
108
109template <typename FloatType>
110__global__ void move_field(gpu::FieldAccessor<FloatType> dest_field,
111 gpu::FieldAccessor<FloatType> src_field) {
114 if (dest_field.isValidPosition() && src_field.isValidPosition()) {
115 dest_field.get(0u) = src_field.get(0u);
116 src_field.get(0u) = FloatType{0};
117 }
118}
119// LCOV_EXCL_STOP
120#endif // __CUDACC__
121
122} // namespace walberla
cudaStream_t stream[1]
CUDA streams for parallel computing on CPU and GPU.
\file PackInfoPdfDoublePrecision.cpp \author pystencils
FloatType greens_function(int x, int y, int z, auto const &dim)