22#include <blockforest/communication/UniformBufferedScheme.h>
23#include <field/AddToStorage.h>
24#include <field/FlagField.h>
25#include <field/FlagUID.h>
26#include <field/GhostLayerField.h>
27#include <field/communication/PackInfo.h>
28#include <field/iterators/IteratorMacros.h>
29#include <field/vtk/FlagFieldCellFilter.h>
30#include <field/vtk/VTKWriter.h>
31#include <stencil/D3Q27.h>
32#include <waLBerlaDefinitions.h>
33#if defined(__CUDACC__) and defined(WALBERLA_BUILD_WITH_CUDA)
34#include <gpu/AddGPUFieldToStorage.h>
35#include <gpu/communication/MemcpyPackInfo.h>
36#include <gpu/communication/UniformGPUScheme.h>
39#include "../BoundaryHandling.hpp"
40#include "../BoundaryPackInfo.hpp"
41#include "../utils/boundary.hpp"
42#include "../utils/types_conversion.hpp"
44#if defined(__CUDACC__) and defined(WALBERLA_BUILD_WITH_CUDA)
74#if not defined(WALBERLA_BUILD_WITH_CUDA)
76 "waLBerla was compiled without CUDA support");
78 using ContinuityKernel =
80 using DiffusiveFluxKernelUnthermalized =
81 typename detail::KernelTrait<FloatType,
83 using DiffusiveFluxKernelThermalized =
typename detail::KernelTrait<
84 FloatType,
Architecture>::DiffusiveFluxKernelThermalized;
85 using AdvectiveFluxKernel =
86 typename detail::KernelTrait<FloatType,
88 using FrictionCouplingKernel =
89 typename detail::KernelTrait<FloatType,
91 using DiffusiveFluxKernelElectrostaticUnthermalized =
92 typename detail::KernelTrait<
93 FloatType,
Architecture>::DiffusiveFluxKernelElectrostatic;
94 using DiffusiveFluxKernelElectrostaticThermalized =
95 typename detail::KernelTrait<
96 FloatType,
Architecture>::DiffusiveFluxKernelElectrostaticThermalized;
98 using DiffusiveFluxKernel = std::variant<DiffusiveFluxKernelUnthermalized,
99 DiffusiveFluxKernelThermalized>;
100 using DiffusiveFluxKernelElectrostatic =
101 std::variant<DiffusiveFluxKernelElectrostaticUnthermalized,
102 DiffusiveFluxKernelElectrostaticThermalized>;
121 template <
typename FT, lbmpy::Arch AT = lbmpy::Arch::CPU>
struct FieldTrait {
125 template <
class Field>
126 using PackInfo = field::communication::PackInfo<Field>;
127 template <
class Stencil>
129 blockforest::communication::UniformBufferedScheme<Stencil>;
130 template <
class Stencil>
132 blockforest::communication::UniformBufferedScheme<Stencil>;
134 using FlagField = walberla::FlagField<walberla::uint8_t>;
135#if defined(__CUDACC__) and defined(WALBERLA_BUILD_WITH_CUDA)
139 template <
class Field>
140 using MemcpyPackInfo = gpu::communication::MemcpyPackInfo<Field>;
143 template <
typename Stencil>
144 class UniformGPUScheme
145 :
public gpu::communication::UniformGPUScheme<Stencil> {
147 explicit UniformGPUScheme(
auto const &
bf)
148 : gpu::communication::UniformGPUScheme<Stencil>(
155 template <
class Stencil>
157 template <
class Stencil>
159 blockforest::communication::UniformBufferedScheme<Stencil>;
161 using GPUField = gpu::GPUField<FloatType>;
191 return std::is_same_v<FloatType, double>;
195 FloatType m_diffusion;
200 bool m_friction_coupling;
224 std::unique_ptr<DiffusiveFluxKernelElectrostatic>
243 template <
typename Field>
247#if defined(__CUDACC__) and defined(WALBERLA_BUILD_WITH_CUDA)
249 auto field_id = gpu::addGPUFieldToStorage<GPUField>(
251 if constexpr (std::is_same_v<Field, _DensityField>) {
256 }
else if constexpr (std::is_same_v<Field, _FluxField>) {
260 std::array<FloatType, FluxCount>{});
266 return field::addToStorage<Field>(
blocks,
tag, FloatType{value},
288 typename stencil::D3Q27>;
291 typename stencil::D3Q27>;
296 template <
class Field>
325 set_diffusion_kernels();
341 std::make_shared<BoundaryFullCommunicator>(
blocks);
366 return m_friction_coupling;
372 return static_cast<bool>(
384 return {
static_cast<uint64_t>(kernel->getTime_step())};
389 auto visitor = [m_diffusion = m_diffusion](
auto &kernel) {
390 kernel.setD(m_diffusion);
398 std::visit([m_kT = m_kT](
auto &kernel) { kernel.setKt(m_kT); },
405 [m_valency = m_valency](
auto &kernel) { kernel.setZ(m_valency); },
419 std::get_if<DiffusiveFluxKernelElectrostaticThermalized>(
423 throw std::runtime_error(
"This EK instance is unthermalized");
426 static_cast<uint32_t>(std::numeric_limits<uint_t>::max()));
427 kernel->setTime_step(
static_cast<uint32_t>(counter));
432 m_ext_efield = field;
435 [
this](
auto &kernel) {
446 (*m_full_communication)();
461 void set_diffusion_kernels() {
462 auto kernel = DiffusiveFluxKernelUnthermalized(
464 m_diffusive_flux = std::make_unique<DiffusiveFluxKernel>(std::move(kernel));
473 std::make_unique<DiffusiveFluxKernelElectrostatic>(
481 auto kernel = DiffusiveFluxKernelThermalized(
499 m_diffusive_flux = std::make_unique<DiffusiveFluxKernel>(std::move(kernel));
501 std::make_unique<DiffusiveFluxKernelElectrostatic>(
505 void kernel_boundary_density() {
507 (*m_boundary_density)(&
block);
511 void kernel_boundary_flux() {
513 (*m_boundary_flux)(&
block);
517 void kernel_continuity() {
519 (*m_continuity).run(&
block);
523 void kernel_diffusion() {
525 std::visit([&
block](
auto &kernel) { kernel.run(&
block); },
531 kernel->setTime_step(kernel->getTime_step() + 1u);
534 std::get_if<DiffusiveFluxKernelElectrostaticThermalized>(
541 void kernel_advection(std::size_t
const velocity_id) {
549 void kernel_friction_coupling(std::size_t
const force_id,
550 double const lb_density) {
551 auto kernel = FrictionCouplingKernel(
559 void kernel_diffusion_electrostatic(std::size_t
const potential_id) {
561 std::visit([phiID](
auto &kernel) { kernel.setPhiID(phiID); },
565 std::visit([&
block](
auto &kernel) { kernel.run(&
block); },
570 std::get_if<DiffusiveFluxKernelElectrostaticThermalized>(
577 kernel->setTime_step(kernel->getTime_step() + 1u);
581 void kernel_migration() {}
583 void update_boundary_fields() {
600 std::size_t
force_id,
double lb_density)
override {
603 update_boundary_fields();
610 throw std::runtime_error(
"Walberla EK: electrostatic potential enabled "
611 "but no field accessible. potential id is " +
620 kernel_boundary_flux();
623 if (
force_id == walberla::BlockDataID{}) {
624 throw std::runtime_error(
"Walberla EK: friction coupling enabled but "
625 "no force field accessible. force_id is " +
627 ". Hint: LB may be inactive.");
629 kernel_friction_coupling(
force_id, lb_density);
634 throw std::runtime_error(
"Walberla EK: advection enabled but no "
635 "velocity field accessible. velocity_id is " +
637 ". Hint: LB may be inactive.");
640 kernel_boundary_flux();
645 kernel_boundary_density();
653 static_assert(std::is_same_v<std::size_t, walberla::uint_t>);
689 std::vector<double>
out;
695 out = std::vector<double>(
ci->numCells());
728 std::vector<double>
const &
density)
override {
739 std::vector<FloatType>
values(
bci->numCells());
754 [[
nodiscard]] std::optional<Utils::Vector3d>
774 std::vector<double>
out;
780 out = std::vector<double>(3u *
ci->numCells());
799 for (
uint_t f = 0
u; f < 3u; ++f) {
803 for (
uint_t f = 0
u; f < 3u; ++f) {
839 [[
nodiscard]] std::optional<Utils::Vector3d>
884 std::vector<std::optional<double>>
const &
density)
override {
897 auto const &
opt = *
it;
911 [[
nodiscard]] std::vector<std::optional<double>>
915 std::vector<std::optional<double>>
out;
921 auto const n_values =
ci->numCells();
922 out.reserve(n_values);
931 out.emplace_back(std::nullopt);
943 std::vector<std::optional<Utils::Vector3d>>
const &
flux)
override {
957 auto const &
opt = *
it;
971 [[
nodiscard]] std::vector<std::optional<Utils::Vector3d>>
975 std::vector<std::optional<Utils::Vector3d>>
out;
981 auto const n_values =
ci->numCells();
982 out.reserve(n_values);
991 out.emplace_back(std::nullopt);
1004 std::vector<bool>
out;
1010 auto const n_values =
ci->numCells();
1011 out.reserve(n_values);
1042 return std::nullopt;
1052 return std::nullopt;
1062 return std::nullopt;
1070 const std::vector<double> &
data_flat)
override {
1080 const std::vector<double> &
data_flat)
override {
1112 template <
typename VecType, u
int_t F_SIZE_ARG,
typename OutputType>
1113 class VTKWriter :
public vtk::BlockCellDataWriter<OutputType, F_SIZE_ARG> {
1125 return (
static_cast<std::size_t
>(x) *
m_dims[2] *
m_dims[1] +
1126 static_cast<std::size_t
>(y) *
m_dims[2] +
1127 static_cast<std::size_t
>(z)) *
1141 template <
typename OutputType =
float>
1143 :
public VTKWriter<std::vector<FloatType>, 1u, OutputType> {
1147 using Base::evaluate;
1158 template <
typename OutputType =
float>
1160 :
public VTKWriter<std::vector<FloatType>, 3u, OutputType> {
1164 using Base::evaluate;
1175 template <
typename OutputType =
float>
1178 using Base = vtk::BlockCellDataWriter<OutputType, 1u>;
1179 using Base::evaluate;
1182 : vtk::BlockCellDataWriter<
OutputType, 1u>(id),
1228 Cell const global(offset[0] + x, offset[1] + y, offset[2] + z);
1229 auto const density =
1230 m_boundary_density->get_node_value_at_boundary(global);
1231 Cell const local(x, y, z);
1232 ek::accessor::Scalar::set(density_field, density, local);
1259 std::size_t index = 0
u;
1260 for (
auto x =
bci.xMin(); x <=
bci.xMax(); ++x) {
1261 for (
auto y =
bci.yMin(); y <=
bci.yMax(); ++y) {
1262 for (
auto z =
bci.zMin(); z <=
bci.zMax(); ++z) {
Vector implementation and trait types for boost qvm interoperability.
Interface of a lattice-based electrokinetic model.
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
Class that runs and controls the BlockForest in waLBerla.
auto const & get_grid_dimensions() const
walberla::blockforest::StructuredBlockForest Lattice_T
std::pair< Utils::Vector3i, Utils::Vector3i > get_local_grid_range(bool with_halo=false) const
Utils::Vector3i get_block_corner(IBlock const &block, bool lower) const
Boundary class optimized for sparse data.
vtk::BlockCellDataWriter< OutputType, 1u > Base
FlagUID const m_boundary_flag
FlagField const * m_flag_field
OutputType evaluate(cell_idx_t const x, cell_idx_t const y, cell_idx_t const z, cell_idx_t const) override
BoundaryVTKWriter(ConstBlockDataID const &flag_field_id, std::string const &id, FlagUID const &boundary_flag)
ConstBlockDataID const m_flag_field_id
FlagField::flag_t m_boundary_flag_value
void configure() override
OutputType evaluate(cell_idx_t const x, cell_idx_t const y, cell_idx_t const z, cell_idx_t const) override
OutputType evaluate(cell_idx_t const x, cell_idx_t const y, cell_idx_t const z, cell_idx_t const f) override
void set_dims(Vector3< uint_t > dims)
std::size_t get_first_index(cell_idx_t const x, cell_idx_t const y, cell_idx_t const z)
VTKWriter(ConstBlockDataID const &block_id, std::string const &id, FloatType unit_conversion)
void set_content(VecType content)
void configure() override
Class that runs and controls the EK on waLBerla.
~EKinWalberlaImpl() override=default
void set_slice_flux_boundary(Utils::Vector3i const &lower_corner, Utils::Vector3i const &upper_corner, std::vector< std::optional< Utils::Vector3d > > const &flux) override
void set_kT(double kT) override
std::optional< double > get_node_density_at_boundary(Utils::Vector3i const &node, bool consider_ghosts=false) const override
double get_kT() const noexcept override
std::unique_ptr< DiffusiveFluxKernelElectrostatic > m_diffusive_flux_electrostatic
walberla::FlagField< walberla::uint8_t > FlagField
void set_friction_coupling(bool friction_coupling) override
void set_rng_state(uint64_t counter) override
void integrate(std::size_t potential_id, std::size_t velocity_id, std::size_t force_id, double lb_density) override
bool set_node_density_boundary(Utils::Vector3i const &node, double density) override
void set_slice_density(Utils::Vector3i const &lower_corner, Utils::Vector3i const &upper_corner, std::vector< double > const &density) override
BlockDataID m_density_field_id
std::shared_ptr< FullCommunicator > m_full_communication
std::bitset< GhostComm::SIZE > m_pending_ghost_comm
std::vector< double > get_slice_density(Utils::Vector3i const &lower_corner, Utils::Vector3i const &upper_corner) const override
void update_density_boundary_from_shape(const std::vector< int > &raster_flat, const std::vector< double > &data_flat) override
void update_flux_boundary_from_shape(const std::vector< int > &raster_flat, const std::vector< double > &data_flat) override
std::vector< bool > get_slice_is_boundary(Utils::Vector3i const &lower_corner, Utils::Vector3i const &upper_corner) const override
void set_ext_efield(Utils::Vector3d const &field) override
bool set_node_density(Utils::Vector3i const &node, double density) override
double get_diffusion() const noexcept override
void set_valency(double valency) override
bool is_thermalized() const noexcept override
auto add_to_storage(std::string const tag, FloatType value)
Convenience function to add a field with a custom allocator.
std::optional< double > get_node_density(Utils::Vector3i const &node, bool consider_ghosts=false) const override
std::size_t get_density_id() const noexcept override
unsigned int get_seed() const noexcept override
BlockDataID m_flag_field_flux_id
std::unique_ptr< ContinuityKernel > m_continuity
double get_valency() const noexcept override
typename FieldTrait< FloatType, Architecture >::FluxField FluxField
void clear_flux_boundaries() override
std::shared_ptr< BoundaryModelFlux > m_boundary_flux
void set_slice_density_boundary(Utils::Vector3i const &lower_corner, Utils::Vector3i const &upper_corner, std::vector< std::optional< double > > const &density) override
void reset_flux_boundary_handling(std::shared_ptr< BlockStorage > const &blocks)
bool get_advection() const noexcept override
std::optional< Utils::Vector3d > get_node_flux_vector(Utils::Vector3i const &node, bool consider_ghosts=false) const override
typename FieldTrait< FloatType, Architecture >::template BoundaryCommScheme< typename stencil::D3Q27 > BoundaryFullCommunicator
typename FieldTrait< FloatType, Architecture >::template RegularCommScheme< typename stencil::D3Q27 > FullCommunicator
void reallocate_density_boundary_field()
std::optional< uint64_t > get_rng_state() const override
void set_advection(bool advection) override
BlockDataID m_flag_field_density_id
bool remove_node_from_density_boundary(Utils::Vector3i const &node) override
typename FieldTrait< FloatType, Architecture >::template PackInfo< Field > PackInfo
bool get_friction_coupling() const noexcept override
Utils::Vector3d get_ext_efield() const noexcept override
stencil::D3Q27 Stencil
Stencil for collision and streaming operations.
LatticeWalberla const & get_lattice() const noexcept override
bool remove_node_from_flux_boundary(Utils::Vector3i const &node) override
bool is_double_precision() const noexcept override
void integrate_vtk_writers() override
typename FieldTrait< FloatType, Architecture >::DensityField DensityField
FloatType FloatType_c(T t)
bool is_gpu() const noexcept override
void clear_density_boundaries() override
std::optional< bool > get_node_is_flux_boundary(Utils::Vector3i const &node, bool consider_ghosts) const override
void ghost_communication() override
LatticeWalberla::Lattice_T BlockStorage
Lattice model (e.g.
void ghost_communication_boundary()
void reset_density_boundary_handling(std::shared_ptr< BlockStorage > const &blocks)
std::vector< std::optional< Utils::Vector3d > > get_slice_flux_at_boundary(Utils::Vector3i const &lower_corner, Utils::Vector3i const &upper_corner) const override
ResourceObserver m_mpi_cart_comm_observer
std::vector< std::optional< double > > get_slice_density_at_boundary(Utils::Vector3i const &lower_corner, Utils::Vector3i const &upper_corner) const override
BlockDataID m_flux_field_id
void register_vtk_field_writers(walberla::vtk::VTKOutput &vtk_obj, LatticeModel::units_map const &units, int flag_observables) override
std::optional< Utils::Vector3d > get_node_flux_at_boundary(Utils::Vector3i const &node, bool consider_ghosts=false) const override
std::vector< double > get_slice_flux_vector(Utils::Vector3i const &lower_corner, Utils::Vector3i const &upper_corner) const override
typename FieldTrait< FloatType >::DensityField _DensityField
std::shared_ptr< BoundaryFullCommunicator > m_boundary_communicator
std::optional< bool > get_node_is_density_boundary(Utils::Vector3i const &node, bool consider_ghosts) const override
std::unique_ptr< DiffusiveFluxKernel > m_diffusive_flux
void set_diffusion(double diffusion) override
std::size_t stencil_size() const noexcept override
FlagUID const Boundary_flag
Flag for boundary cells.
bool set_node_flux_boundary(Utils::Vector3i const &node, Utils::Vector3d const &flux) override
void reallocate_flux_boundary_field()
typename FieldTrait< FloatType >::FluxField _FluxField
std::optional< bool > get_node_is_boundary(Utils::Vector3i const &node, bool consider_ghosts=false) const override
std::unique_ptr< BoundaryModelDensity > m_boundary_density
EKinWalberlaImpl(std::shared_ptr< LatticeWalberla > lattice, double diffusion, double kT, double valency, Utils::Vector3d const &ext_efield, double density, bool advection, bool friction_coupling, bool thermalized, unsigned int seed)
std::shared_ptr< LatticeWalberla > m_lattice
Block forest.
FlagUID const Domain_flag
Flag for domain cells, i.e.
void register_vtk_field_filters(walberla::vtk::VTKOutput &vtk_obj) override
void setup_boundary_handle(std::shared_ptr< LatticeWalberla > lattice, std::shared_ptr< Boundary_T > boundary)
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)
auto get_vector(GhostLayerField< double, uint_t{13u}> const *flux_field, Cell const &cell)
void initialize(GhostLayerField< double, uint_t{13u}> *flux_field, std::array< double, 13 > const &values)
void initialize(GhostLayerField< double, 1u > *scalar_field, double const &value)
void set(GhostLayerField< double, 1u > *scalar_field, double const &value, Cell const &cell)
auto get(GhostLayerField< double, 1u > const *scalar_field, Cell const &cell)
static FUNC_PREFIX double *RESTRICT const double *RESTRICT const int64_t const int64_t const int64_t const int64_t const int64_t const int64_t const int64_t const int64_t const int64_t const int64_t const uint32_t uint32_t uint32_t uint32_t uint32_t uint32_t uint32_t seed
\file PackInfoPdfDoublePrecision.cpp \author pystencils
auto to_vector3d(Vector3< T > const &v) noexcept
std::vector< double > fill_3D_scalar_array(std::vector< double > const &vec_flat, Utils::Vector3i const &grid_size)
void set_boundary_from_grid(BoundaryModel &boundary, LatticeWalberla const &lattice, std::vector< int > const &raster_flat, std::vector< DataType > const &data_flat)
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)
Cell to_cell(signed_integral_vector auto const &xyz)
ResourceObserver get_mpi_cart_comm_observer()
Get an observer on waLBerla's MPI Cartesian communicator status.
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)
std::vector< Utils::Vector3d > fill_3D_vector_array(std::vector< double > const &vec_flat, Utils::Vector3i const &grid_size)
Observer to monitor the lifetime of a shared resource.
blockforest::communication::UniformBufferedScheme< Stencil > RegularCommScheme
GhostLayerField< FT, FluxCount > FluxField
field::communication::PackInfo< Field > PackInfo
GhostLayerField< FT, 1 > DensityField
blockforest::communication::UniformBufferedScheme< Stencil > BoundaryCommScheme
GhostCommFlags
Ghost communication operations.
@ DENS
density communication
@ FLB
flux boundary communication