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) {
104 return numeric_cast<FloatType>(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>,
121 cufftComplex, cufftDoubleComplex>;
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;
153 typename heffte::fft3d<backend>::template buffer_container<ComplexType>>
157 auto const &grid_range) {
160 box_in = std::make_unique<heffte::box3d<>>(
163 box_out = std::make_unique<heffte::box3d<>>(
167 MPI_COMM_WORLD, options);
168 buffer = std::make_unique<
typename heffte::fft3d<
169 backend>::template buffer_container<ComplexType>>(
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> {
183 using GreenFunctionField = gpu::GPUField<FloatType>;
184 using PotentialFourier = gpu::GPUField<ComplexType>;
185 BlockDataID m_potential_field_id;
186 BlockDataID m_greens_function_field_id;
187 BlockDataID m_potential_fourier_id;
188 walberla::gpu::Kernel<void (*)(walberla::gpu::FieldAccessor<ComplexType>,
189 walberla::gpu::FieldAccessor<FloatType>)>
190 kernel_greens = gpu::make_kernel(
191 multiply_by_greens_function<FloatType, ComplexType>);
192 walberla::gpu::Kernel<void (*)(walberla::gpu::FieldAccessor<FloatType>,
193 walberla::gpu::FieldAccessor<FloatType>)>
194 kernel_move_fields = gpu::make_kernel(move_field<FloatType>);
198 BlockDataID m_potential_field_with_ghosts_id;
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__)
213 using GreenFunctionField =
214 Green<FloatType, lbmpy::Arch::GPU>::GreenFunctionField;
215 using PotentialFourier =
216 Green<FloatType, lbmpy::Arch::GPU>::PotentialFourier;
217 m_green.m_potential_field_id = gpu::addGPUFieldToStorage<PotentialField>(
218 blocks,
"potential field", 1u, field::fzyx, 0u,
false),
219 m_green.m_greens_function_field_id =
220 gpu::addGPUFieldToStorage<GreenFunctionField>(
221 blocks,
"greens function", 1u, field::fzyx, 0u,
false),
222 m_green.m_potential_fourier_id =
223 gpu::addGPUFieldToStorage<PotentialFourier>(
224 blocks,
"fourier field", 1u, field::fzyx, 0u,
false),
225 m_potential_field_with_ghosts_id =
226 gpu::addGPUFieldToStorage<PotentialField>(
227 blocks,
"potential field with ghosts", 1u, field::fzyx,
232 for (
auto &
block : *blocks) {
233 auto green_field =
block.template getData<GreenFunctionField>(
234 m_green.m_greens_function_field_id);
235 auto kernel = gpu::make_kernel(create_greens_function<FloatType>);
236 kernel.addFieldIndexingParam(
237 gpu::FieldIndexing<FloatType>::xyz(*green_field));
238 kernel.addParam(grid_range.first[0]);
239 kernel.addParam(grid_range.first[1]);
240 kernel.addParam(grid_range.first[2]);
241 kernel.addParam(grid_range.second[0]);
242 kernel.addParam(grid_range.second[1]);
243 kernel.addParam(grid_range.second[2]);
244 kernel.addParam(global_dim[0]);
245 kernel.addParam(global_dim[1]);
246 kernel.addParam(global_dim[2]);
250 m_green.m_potential_field_id);
251 auto potential_ghosts =
block.template getData<PotentialField>(
252 m_potential_field_with_ghosts_id);
253 auto green =
block.template getData<GreenFunctionField>(
254 m_green.m_greens_function_field_id);
255 auto fourier =
block.template getData<PotentialFourier>(
256 m_green.m_potential_fourier_id);
261 m_green.kernel_greens = gpu::make_kernel(
262 multiply_by_greens_function<FloatType, ComplexType>);
263 m_green.kernel_greens.addFieldIndexingParam(
264 gpu::FieldIndexing<ComplexType>::allInner(*fourier));
265 m_green.kernel_greens.addFieldIndexingParam(
266 gpu::FieldIndexing<FloatType>::allInner(*green));
268 m_green.kernel_move_fields = gpu::make_kernel(move_field<FloatType>);
269 m_green.kernel_move_fields.addFieldIndexingParam(
270 gpu::FieldIndexing<FloatType>::xyz(*potential_ghosts));
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));
289 [[nodiscard]]
bool is_gpu() const noexcept
override {
294 return std::is_same_v<FloatType, double>;
298 return static_cast<std::size_t
>(m_potential_field_with_ghosts_id);
301 void setup_fft([[maybe_unused]]
bool use_gpu_aware)
override {
303 heffte::plan_options options = heffte::default_options<
305#if defined(__CUDACC__)
307 options.use_reorder =
false;
308 options.algorithm = heffte::reshape_algorithm::p2p_plined;
309 options.use_pencils =
true;
310 options.use_gpu_aware = use_gpu_aware;
314 std::make_unique<heffte_container<ComplexType>>(options, grid_range);
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());
320 auto const dim = grid_range.second - grid_range.first;
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++) {
326 greens_function<FloatType>(x + grid_range.first[0],
327 y + grid_range.first[1],
328 z + grid_range.first[2], global_dim);
336 heffte->fft->forward(m_green.m_potential.data(),
337 m_green.m_potential_fourier.data(),
338 heffte->buffer->data());
340#if defined(__CUDACC__)
342 using PotentialFourier =
343 Green<FloatType, lbmpy::Arch::GPU>::PotentialFourier;
346 m_green.m_potential_field_id);
347 auto fourier =
block.template getData<PotentialFourier>(
348 m_green.m_potential_fourier_id);
349 FloatType *_data_potential =
potential->dataAt(0, 0, 0, 0);
350 ComplexType *_data_fourier = fourier->dataAt(0, 0, 0, 0);
352 heffte->fft->forward(_data_potential, _data_fourier,
353 heffte->buffer->data());
359 [[nodiscard]] std::optional<double>
361 bool consider_ghosts =
false)
override {
367 auto const potential_field = bc->block->template getData<PotentialField>(
368 m_potential_field_with_ghosts_id);
374 throw std::runtime_error(
"Setting potential is not supported by EKFFT");
377 [[nodiscard]] std::vector<double>
380 std::vector<double> out;
382 uint_t values_size{0u};
385 if (
auto const ci =
get_interval(lattice, lower_corner, upper_corner)) {
386 out = std::vector<double>(ci->numCells());
387 for (
auto &
block : *lattice.get_blocks()) {
388 auto const block_offset = lattice.get_block_corner(
block,
true);
390 lattice, lower_corner, upper_corner, block_offset,
block)) {
391 auto const potential_field =
block.template getData<PotentialField>(
392 m_potential_field_with_ghosts_id);
394 assert(values.size() == bci->numCells());
396 values_size += bci->numCells();
398 auto kernel = [&values, &out](
unsigned const block_index,
399 unsigned const local_index,
401 out[local_index] = double_c(values[block_index]);
407 assert(values_size == ci->numCells());
413 std::vector<double>
const &)
override {
414 throw std::runtime_error(
"Setting potential is not supported by EKFFT");
420 auto dim = grid_range.second - grid_range.first;
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());
431 auto potential_with_ghosts =
block.template getData<PotentialField>(
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++) {
436 potential_with_ghosts->get(x, y, z) =
443#if defined(__CUDACC__)
445 using PotentialFourier =
446 Green<FloatType, lbmpy::Arch::GPU>::PotentialFourier;
449 m_green.m_potential_field_id);
450 auto fourier =
block.template getData<PotentialFourier>(
451 m_green.m_potential_fourier_id);
452 FloatType *_data_potential =
potential->dataAt(0, 0, 0, 0);
453 ComplexType *_data_fourier = fourier->dataAt(0, 0, 0, 0);
454 heffte->fft->forward(_data_potential, _data_fourier,
455 heffte->buffer->data());
456 m_green.kernel_greens();
457 heffte->fft->backward(_data_fourier, _data_potential,
458 heffte->buffer->data());
459 m_green.kernel_move_fields();
469 auto const density_id = BlockDataID(
id);
472 auto dim = grid_range.second - grid_range.first;
474 auto density_field =
block.template getData<PotentialField>(density_id);
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++) {
479 factor * density_field->get(x, y, z);
485#if defined(__CUDACC__)
488 auto field =
block.template getData<PotentialField>(
489 m_green.m_potential_field_id);
491 block.template getData<gpu::GPUField<FloatType>>(density_id);
492 add_fields(field, density_field, FloatType_c(factor));
501 auto const dim = grid_range.second - grid_range.first;
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__)
518 auto field =
block.template getData<PotentialField>(
519 m_green.m_potential_field_id);
530 for (
auto const &vtk_handle :
m_vtk_auto | std::views::values) {
531 if (vtk_handle->enabled) {
532 vtk::writeFiles(vtk_handle->ptr)();
533 vtk_handle->execution_count++;
539 template <
typename VecType, u
int_t F_SIZE_ARG,
typename OutputType>
540 class VTKWriter :
public vtk::BlockCellDataWriter<OutputType, F_SIZE_ARG> {
542 VTKWriter(ConstBlockDataID
const &block_id, std::string
const &
id,
543 FloatType unit_conversion)
544 : vtk::BlockCellDataWriter<OutputType, F_SIZE_ARG>(id),
548 void configure()
override { WALBERLA_ASSERT_NOT_NULLPTR(this->block_); }
551 cell_idx_t
const z) {
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;
577 OutputType
evaluate(cell_idx_t
const x, cell_idx_t
const y,
578 cell_idx_t
const z, cell_idx_t
const)
override {
579 WALBERLA_ASSERT(!this->
m_content.empty());
588 int flag_observables)
override {
590 auto const unit_conversion = FloatType_c(units.at(
"potential"));
592 WALBERLA_ASSERT_NOT_NULLPTR(blocks);
593 auto potential_writer = make_shared<PotentialVTKWriter<float>>(
594 m_potential_field_with_ghosts_id,
"potential", unit_conversion);
595 auto before_function = [
this, blocks, potential_writer]() {
596 for (
auto &
block : *blocks) {
597 auto *potential_field =
block.template getData<PotentialField>(
598 m_potential_field_with_ghosts_id);
599 auto const bci = potential_field->xyzSize();
600 potential_writer->set_content(
602 potential_writer->set_dims(Vector3<uint_t>(
603 uint_c(bci.xSize()), uint_c(bci.ySize()), uint_c(bci.zSize())));
606 vtk_obj.addBeforeFunction(std::move(before_function));
607 vtk_obj.addCellDataWriter(potential_writer);
612#if defined(__CUDACC__)
614 gpu::GPUField<FloatType> *field_add, FloatType factor) {
615 auto kernel = gpu::make_kernel(add_fields_with_factor<FloatType>);
616 kernel.addFieldIndexingParam(
617 gpu::FieldIndexing<FloatType>::xyz(*field_out));
618 kernel.addFieldIndexingParam(
619 gpu::FieldIndexing<FloatType>::xyz(*field_add));
620 kernel.addParam(factor);
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
DEVICE_QUALIFIER constexpr iterator begin() noexcept
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
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)