30#if defined(__CUDACC__)
37#include <blockforest/communication/UniformBufferedScheme.h>
38#include <domain_decomposition/BlockDataID.h>
39#include <field/AddToStorage.h>
40#include <field/GhostLayerField.h>
41#include <field/communication/PackInfo.h>
42#include <field/vtk/VTKWriter.h>
43#include <stencil/D3Q27.h>
44#include <waLBerlaDefinitions.h>
45#if defined(__CUDACC__)
46#include <gpu/AddGPUFieldToStorage.h>
47#include <gpu/FieldAccessor.h>
48#include <gpu/FieldIndexing.h>
49#include <gpu/GPUField.h>
50#include <gpu/Kernel.h>
51#include <gpu/communication/MemcpyPackInfo.h>
52#include <gpu/communication/UniformGPUScheme.h>
56#pragma clang diagnostic push
57#pragma clang diagnostic ignored "-Wfloat-conversion"
58#pragma clang diagnostic ignored "-Wimplicit-float-conversion"
59#elif defined(__GNUC__) or defined(__GNUG__)
60#pragma GCC diagnostic push
61#pragma GCC diagnostic ignored "-Wfloat-conversion"
65#include <heffte_backends.h>
66#include <heffte_geometry.h>
69#pragma clang diagnostic pop
70#elif defined(__GNUC__) or defined(__GNUG__)
71#pragma GCC diagnostic pop
89template <
typename T, std::
size_t N>
91 std::array<T, N>
res{};
92 std::ranges::copy(
vec,
res.begin());
97 return (z * dim[1] + y) * dim[0] + x;
100template <
typename FloatType, lbmpy::Arch Architecture>
103 template <
typename T> FloatType FloatType_c(T t) {
108 template <
typename FT, lbmpy::Arch AT = lbmpy::Arch::CPU>
struct FieldTrait {
111 template <
class Field>
112 using PackInfo = field::communication::PackInfo<Field>;
113 template <
class Stencil>
115 blockforest::communication::UniformBufferedScheme<Stencil>;
118#if defined(__CUDACC__)
120 using ComplexType = std::conditional_t<std::is_same_v<FloatType, float>,
123 template <
class Field>
124 using PackInfo = gpu::communication::MemcpyPackInfo<Field>;
125 template <
class Stencil>
136 template <
class Field>
142#if defined(__CUDACC__)
145 heffte::backend::cufft, heffte::backend::fftw>;
151 std::unique_ptr<heffte::fft3d<backend>>
fft;
160 box_in = std::make_unique<heffte::box3d<>>(
163 box_out = std::make_unique<heffte::box3d<>>(
168 buffer = std::make_unique<
typename heffte::fft3d<
170 fft->size_workspace());
175 template <
typename FT, lbmpy::Arch AT = lbmpy::Arch::CPU>
struct Green {
176 std::vector<FloatType> m_greens;
177 std::vector<FloatType> m_potential;
178 std::vector<ComplexType> m_potential_fourier;
181#if defined(__CUDACC__)
182 template <
typename FT>
struct Green<
FT,
lbmpy::Arch::GPU> {
188 walberla::gpu::Kernel<
void (*)(walberla::gpu::FieldAccessor<ComplexType>,
189 walberla::gpu::FieldAccessor<FloatType>)>
192 walberla::gpu::Kernel<
void (*)(walberla::gpu::FieldAccessor<FloatType>,
193 walberla::gpu::FieldAccessor<FloatType>)>
200 Green<FloatType, Architecture> m_green;
202 std::unique_ptr<heffte_container<ComplexType>> heffte;
203 std::shared_ptr<FullCommunicator> m_full_communication;
211#if defined(__CUDACC__)
214 Green<FloatType, lbmpy::Arch::GPU>::GreenFunctionField;
216 Green<FloatType, lbmpy::Arch::GPU>::PotentialFourier;
217 m_green.m_potential_field_id = gpu::addGPUFieldToStorage<PotentialField>(
218 blocks,
"potential field", 1u, field::fzyx, 0
u,
false),
219 m_green.m_greens_function_field_id =
220 gpu::addGPUFieldToStorage<GreenFunctionField>(
221 blocks,
"greens function", 1u, field::fzyx, 0
u,
false),
222 m_green.m_potential_fourier_id =
223 gpu::addGPUFieldToStorage<PotentialFourier>(
224 blocks,
"fourier field", 1u, field::fzyx, 0
u,
false),
225 m_potential_field_with_ghosts_id =
226 gpu::addGPUFieldToStorage<PotentialField>(
227 blocks,
"potential field with ghosts", 1u, field::fzyx,
234 m_green.m_greens_function_field_id);
236 kernel.addFieldIndexingParam(
250 m_green.m_potential_field_id);
252 m_potential_field_with_ghosts_id);
254 m_green.m_greens_function_field_id);
256 m_green.m_potential_fourier_id);
261 m_green.kernel_greens = gpu::make_kernel(
263 m_green.kernel_greens.addFieldIndexingParam(
264 gpu::FieldIndexing<ComplexType>::allInner(*
fourier));
265 m_green.kernel_greens.addFieldIndexingParam(
266 gpu::FieldIndexing<FloatType>::allInner(*
green));
269 m_green.kernel_move_fields.addFieldIndexingParam(
271 m_green.kernel_move_fields.addFieldIndexingParam(
272 gpu::FieldIndexing<FloatType>::xyz(*
potential));
278 m_potential_field_with_ghosts_id = field::addToStorage<PotentialField>(
279 blocks,
"potential field with ghosts", FloatType{0}, field::fzyx,
283 m_full_communication = std::make_shared<FullCommunicator>(
blocks);
284 m_full_communication->addPackInfo(
286 m_potential_field_with_ghosts_id));
294 return std::is_same_v<FloatType, double>;
298 return static_cast<std::size_t
>(m_potential_field_with_ghosts_id);
303 heffte::plan_options
options = heffte::default_options<
305#if defined(__CUDACC__)
308 options.algorithm = heffte::reshape_algorithm::p2p_plined;
316 m_green.m_potential = std::vector<FloatType>(heffte->fft->size_inbox());
317 m_green.m_greens = std::vector<FloatType>(heffte->fft->size_outbox());
318 m_green.m_potential_fourier =
319 std::vector<ComplexType>(heffte->fft->size_outbox());
322 for (
int x = 0; x < dim[0]; x++) {
323 for (
int y = 0; y < dim[1]; y++) {
324 for (
int z = 0; z < dim[2]; z++) {
336 heffte->fft->forward(m_green.m_potential.data(),
337 m_green.m_potential_fourier.data(),
338 heffte->buffer->data());
340#if defined(__CUDACC__)
343 Green<FloatType, lbmpy::Arch::GPU>::PotentialFourier;
346 m_green.m_potential_field_id);
348 m_green.m_potential_fourier_id);
353 heffte->buffer->data());
368 m_potential_field_with_ghosts_id);
374 throw std::runtime_error(
"Setting potential is not supported by EKFFT");
380 std::vector<double>
out;
386 out = std::vector<double>(
ci->numCells());
387 for (
auto &
block : *lattice.get_blocks()) {
392 m_potential_field_with_ghosts_id);
413 std::vector<double>
const &)
override {
414 throw std::runtime_error(
"Setting potential is not supported by EKFFT");
421 heffte->fft->forward(m_green.m_potential.data(),
422 m_green.m_potential_fourier.data(),
423 heffte->buffer->data());
424 std::ranges::transform(m_green.m_potential_fourier, m_green.m_greens,
425 m_green.m_potential_fourier.begin(),
426 std::multiplies<>{});
427 heffte->fft->backward(m_green.m_potential_fourier.data(),
428 m_green.m_potential.data(), heffte->buffer->data());
432 m_potential_field_with_ghosts_id);
433 for (
int x = 0; x < dim[0]; x++) {
434 for (
int y = 0; y < dim[1]; y++) {
435 for (
int z = 0; z < dim[2]; z++) {
443#if defined(__CUDACC__)
446 Green<FloatType, lbmpy::Arch::GPU>::PotentialFourier;
449 m_green.m_potential_field_id);
451 m_green.m_potential_fourier_id);
455 heffte->buffer->data());
456 m_green.kernel_greens();
458 heffte->buffer->data());
459 m_green.kernel_move_fields();
475 for (
int x = 0; x < dim[0]; x++) {
476 for (
int y = 0; y < dim[1]; y++) {
477 for (
int z = 0; z < dim[2]; z++) {
485#if defined(__CUDACC__)
489 m_green.m_potential_field_id);
502 for (
int x = 0; x < dim[0]; x++) {
503 for (
int i = 0; i < m_green.m_potential_fourier.size(); i++) {
504 m_green.m_potential_fourier[i] *= m_green.m_greens[i];
506 for (
int y = 0; y < dim[1]; y++) {
507 for (
int z = 0; z < dim[2]; z++) {
514#if defined(__CUDACC__)
519 m_green.m_potential_field_id);
539 template <
typename VecType, u
int_t F_SIZE_ARG,
typename OutputType>
540 class VTKWriter :
public vtk::BlockCellDataWriter<OutputType, F_SIZE_ARG> {
544 : vtk::BlockCellDataWriter<OutputType,
F_SIZE_ARG>(id),
552 return (
static_cast<std::size_t
>(x) *
m_dims[2] *
m_dims[1] +
553 static_cast<std::size_t
>(y) *
m_dims[2] +
554 static_cast<std::size_t
>(z)) *
568 template <
typename OutputType =
float>
570 :
public VTKWriter<std::vector<FloatType>, 1u, OutputType> {
574 using Base::evaluate;
598 m_potential_field_with_ghosts_id);
612#if defined(__CUDACC__)
616 kernel.addFieldIndexingParam(
617 gpu::FieldIndexing<FloatType>::xyz(*
field_out));
618 kernel.addFieldIndexingParam(
619 gpu::FieldIndexing<FloatType>::xyz(*
field_add));
Vector implementation and trait types for boost qvm interoperability.
std::map< std::string, std::shared_ptr< VTKHandle > > m_vtk_auto
VTK writers that are executed automatically.
std::unordered_map< std::string, double > units_map
auto const & get_grid_dimensions() const
std::pair< Utils::Vector3i, Utils::Vector3i > get_local_grid_range(bool with_halo=false) const
auto get_ghost_layers() const
OutputType evaluate(cell_idx_t const x, cell_idx_t const y, cell_idx_t const z, cell_idx_t const) override
void configure() override
std::size_t get_first_index(cell_idx_t const x, cell_idx_t const y, cell_idx_t const z)
void set_content(VecType content)
void set_dims(Vector3< uint_t > dims)
VTKWriter(ConstBlockDataID const &block_id, std::string const &id, FloatType unit_conversion)
FieldTrait< FloatType, Architecture >::template FullCommunicator< stencil::D3Q27 > FullCommunicator
bool is_gpu() const noexcept override
void reset_charge_field() override
bool set_node_potential(Utils::Vector3i const &, double) override
bool is_double_precision() const noexcept override
std::vector< double > get_slice_potential(Utils::Vector3i const &lower_corner, Utils::Vector3i const &upper_corner) const override
void ghost_communication() override
PoissonSolverFFT(std::shared_ptr< LatticeWalberla > lattice, double permittivity)
void setup_fft(bool use_gpu_aware) override
FieldTrait< FloatType, Architecture >::ComplexType ComplexType
std::optional< double > get_node_potential(Utils::Vector3i const &node, bool consider_ghosts=false) override
void add_charge_to_field(std::size_t id, double valency) override
FieldTrait< FloatType, Architecture >::PotentialField PotentialField
void integrate_vtk_writers() override
void register_vtk_field_writers(walberla::vtk::VTKOutput &vtk_obj, LatticeModel::units_map const &units, int flag_observables) override
void set_slice_potential(Utils::Vector3i const &, Utils::Vector3i const &, std::vector< double > const &) override
FieldTrait< FloatType, Architecture >::template PackInfo< Field > PackInfo
~PoissonSolverFFT() override=default
std::size_t get_potential_field_id() const noexcept override
LatticeWalberla const & get_lattice() const noexcept override
virtual double get_permittivity() const noexcept
cudaStream_t stream[1]
CUDA streams for parallel computing on CPU and GPU.
static double * block(double *p, std::size_t index, std::size_t size)
void initialize(GhostLayerField< double, 1u > *scalar_field, double const &value)
auto get(GhostLayerField< double, 1u > const *scalar_field, Cell const &cell)
\file PackInfoPdfDoublePrecision.cpp \author pystencils
int pos_to_linear_index(int x, int y, int z, auto const &dim)
auto to_array(Utils::Vector< T, N > const &vec)
void copy_block_buffer(CellInterval const &bci, CellInterval const &ci, Utils::Vector3i const &block_offset, Utils::Vector3i const &lower_corner, auto &&kernel)
Synchronize data between a sliced block and a container.
std::optional< BlockAndCell > get_block_and_cell(::LatticeWalberla const &lattice, signed_integral_vector auto const &node, bool consider_ghost_layers)
std::optional< walberla::cell::CellInterval > get_block_interval(::LatticeWalberla const &lattice, Utils::Vector3i const &lower_corner, Utils::Vector3i const &upper_corner, Utils::Vector3i const &block_offset, IBlock const &block)
std::optional< walberla::cell::CellInterval > get_interval(::LatticeWalberla const &lattice, Utils::Vector3i const &lower_corner, Utils::Vector3i const &upper_corner)
blockforest::communication::UniformBufferedScheme< Stencil > FullCommunicator
std::complex< FloatType > ComplexType
field::GhostLayerField< FT, 1u > PotentialField
field::communication::PackInfo< Field > PackInfo
std::unique_ptr< typename heffte::fft3d< backend >::template buffer_container< ComplexType > > buffer
std::unique_ptr< heffte::fft3d< backend > > fft
std::unique_ptr< heffte::box3d<> > box_in
heffte::backend::fftw backend
std::unique_ptr< heffte::box3d<> > box_out
heffte_container(heffte::plan_options const &options, auto const &grid_range)