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/HostFieldAllocator.h>
51#include <gpu/Kernel.h>
52#include <gpu/communication/MemcpyPackInfo.h>
53#include <gpu/communication/UniformGPUScheme.h>
57#pragma clang diagnostic push
58#pragma clang diagnostic ignored "-Wfloat-conversion"
59#pragma clang diagnostic ignored "-Wimplicit-float-conversion"
60#elif defined(__GNUC__) or defined(__GNUG__)
61#pragma GCC diagnostic push
62#pragma GCC diagnostic ignored "-Wfloat-conversion"
66#include <heffte_backends.h>
67#include <heffte_geometry.h>
70#pragma clang diagnostic pop
71#elif defined(__GNUC__) or defined(__GNUG__)
72#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 {
112 template <
class Field>
113 using PackInfo = field::communication::PackInfo<Field>;
114 template <
class Stencil>
116 blockforest::communication::UniformBufferedScheme<Stencil>;
119#if defined(__CUDACC__)
121 using ComplexType = std::conditional_t<std::is_same_v<FloatType, float>,
125 template <
class Field>
126 using PackInfo = gpu::communication::MemcpyPackInfo<Field>;
127 template <
class Stencil>
140 template <
class Field>
146#if defined(__CUDACC__)
147 using backend = heffte::backend::cufft;
153 std::unique_ptr<heffte::fft3d<backend>>
fft;
154 std::unique_ptr<heffte::fft3d<backend>::buffer_container<
ComplexType>>
161 box_in = std::make_unique<heffte::box3d<>>(
164 box_out = std::make_unique<heffte::box3d<>>(
169 buffer = std::make_unique<
170 heffte::fft3d<backend>::buffer_container<
ComplexType>>(
171 fft->size_workspace());
177#if defined(__CUDACC__)
183 std::unique_ptr<heffte_container<ComplexType>> heffte;
184 std::shared_ptr<FullCommunicator> m_full_communication;
186#if defined(__CUDACC__)
193 walberla::gpu::Kernel<
void (*)(walberla::gpu::FieldAccessor<ComplexType>,
194 walberla::gpu::FieldAccessor<FloatType>)>
196 walberla::gpu::Kernel<
void (*)(walberla::gpu::FieldAccessor<FloatType>,
197 walberla::gpu::FieldAccessor<FloatType>)>
200 std::vector<FloatType> m_greens;
201 std::vector<FloatType> m_potential;
202 std::vector<ComplexType> m_potential_fourier;
218#if defined(__CUDACC__)
219 m_potential_field_id = gpu::addGPUFieldToStorage<PotentialField>(
220 blocks,
"potential field", 1u, field::fzyx, 0
u,
false);
221 m_potential_field_with_ghosts_id =
222 gpu::addGPUFieldToStorage<PotentialField>(
223 blocks,
"potential field with ghosts", 1u, field::fzyx,
226 blocks,
"greens function", 1u, field::fzyx, 0
u,
false);
228 blocks,
"fourier field", 1u, field::fzyx, 0
u,
false);
236 kernel.addFieldIndexingParam(
252 m_potential_field_with_ghosts_id);
261 gpu::FieldIndexing<ComplexType>::allInner(*
fourier));
263 gpu::FieldIndexing<FloatType>::allInner(*
green));
269 gpu::FieldIndexing<FloatType>::xyz(*
potential));
273 std::make_shared<gpu::HostFieldAllocator<FloatType>>();
275 m_potential_field_with_ghosts_id = field::addToStorage<PotentialField>(
276 blocks,
"potential field with ghosts", 0., field::fzyx,
280 m_full_communication = std::make_shared<FullCommunicator>(
blocks);
281 m_full_communication->addPackInfo(
283 m_potential_field_with_ghosts_id));
291 return std::is_same_v<FloatType, double>;
295 return static_cast<std::size_t
>(m_potential_field_with_ghosts_id);
300 heffte::plan_options
options = heffte::default_options<
302#if defined(__CUDACC__)
305 options.algorithm = heffte::reshape_algorithm::p2p_plined;
312#if not defined(__CUDACC__)
314 m_potential = std::vector<FloatType>(heffte->fft->size_inbox());
315 m_greens = std::vector<FloatType>(heffte->fft->size_outbox());
316 m_potential_fourier =
317 std::vector<ComplexType>(heffte->fft->size_outbox());
320 for (
int x = 0; x < dim[0]; x++) {
321 for (
int y = 0; y < dim[1]; y++) {
322 for (
int z = 0; z < dim[2]; z++) {
344 m_potential_field_with_ghosts_id);
352 std::vector<double>
out;
358 out = std::vector<double>(
ci->numCells());
359 for (
auto &
block : *lattice.get_blocks()) {
364 m_potential_field_with_ghosts_id);
385#if not defined(__CUDACC__)
389 heffte->fft->forward(m_potential.data(), m_potential_fourier.data(),
390 heffte->buffer->data());
391 std::ranges::transform(m_potential_fourier, m_greens,
392 m_potential_fourier.begin(), std::multiplies<>{});
393 heffte->fft->backward(m_potential_fourier.data(), m_potential.data(),
394 heffte->buffer->data());
398 m_potential_field_with_ghosts_id);
399 for (
int x = 0; x < dim[0]; x++) {
400 for (
int y = 0; y < dim[1]; y++) {
401 for (
int z = 0; z < dim[2]; z++) {
410#if defined(__CUDACC__)
420 heffte->buffer->data());
423 heffte->buffer->data());
435#if not defined(__CUDACC__)
441 for (
int x = 0; x < dim[0]; x++) {
442 for (
int y = 0; y < dim[1]; y++) {
443 for (
int z = 0; z < dim[2]; z++) {
452#if defined(__CUDACC__)
466#if not defined(__CUDACC__)
470 for (
int x = 0; x < dim[0]; x++) {
471 for (
int i = 0; i < m_potential_fourier.size(); i++) {
472 m_potential_fourier[i] *= m_greens[i];
474 for (
int y = 0; y < dim[1]; y++) {
475 for (
int z = 0; z < dim[2]; z++) {
482#if defined(__CUDACC__)
508 template <
typename Field_T, u
int_t F_SIZE_ARG,
typename OutputType>
509 class VTKWriter :
public vtk::BlockCellDataWriter<OutputType, F_SIZE_ARG> {
528 template <
typename OutputType =
float>
530 :
public VTKWriter<PotentialFieldVTK, 1u, OutputType> {
534 using Base::evaluate;
550#if defined(__CUDACC__)
552 [&]<
typename Field>(
auto const &
blocks, std::string name,
556 blocks, name, FloatType{0}, field::fzyx,
563#if defined(__CUDACC__)
569 gpu::fieldCpyFunctor<PotentialFieldVTK, PotentialField>(
571 m_potential_field_with_ghosts_id));
584#if defined(__CUDACC__)
588 kernel.addFieldIndexingParam(
589 gpu::FieldIndexing<FloatType>::xyz(*
field_out));
590 kernel.addFieldIndexingParam(
591 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
VTKWriter(ConstBlockDataID const &block_id, std::string const &id, FloatType unit_conversion)
FloatType const m_conversion
ConstBlockDataID const m_block_id
FieldTrait< FloatType, Architecture >::template FullCommunicator< stencil::D3Q27 > FullCommunicator
bool is_gpu() const noexcept override
FieldTrait< FloatType, Architecture >::PotentialFieldVTK PotentialFieldVTK
void reset_charge_field() 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
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 ghost_communication()
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
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
field::GhostLayerField< FT, 1u > PotentialFieldVTK
std::complex< FloatType > ComplexType
field::GhostLayerField< FT, 1u > PotentialField
field::communication::PackInfo< Field > PackInfo
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
std::unique_ptr< heffte::fft3d< backend >::buffer_container< ComplexType > > buffer
heffte_container(heffte::plan_options const &options, auto const &grid_range)