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/vtk/FlagFieldCellFilter.h>
29#include <field/vtk/VTKWriter.h>
30#include <stencil/D3Q27.h>
31#include <waLBerlaDefinitions.h>
32#if defined(__CUDACC__) and defined(WALBERLA_BUILD_WITH_CUDA)
33#include <gpu/AddGPUFieldToStorage.h>
34#include <gpu/HostFieldAllocator.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)
73#if not defined(WALBERLA_BUILD_WITH_CUDA)
75 "waLBerla was compiled without CUDA support");
77 using ContinuityKernel =
79 using DiffusiveFluxKernelUnthermalized =
80 typename detail::KernelTrait<FloatType,
82 using DiffusiveFluxKernelThermalized =
typename detail::KernelTrait<
83 FloatType,
Architecture>::DiffusiveFluxKernelThermalized;
84 using AdvectiveFluxKernel =
85 typename detail::KernelTrait<FloatType,
87 using FrictionCouplingKernel =
88 typename detail::KernelTrait<FloatType,
90 using DiffusiveFluxKernelElectrostaticUnthermalized =
91 typename detail::KernelTrait<
92 FloatType,
Architecture>::DiffusiveFluxKernelElectrostatic;
93 using DiffusiveFluxKernelElectrostaticThermalized =
94 typename detail::KernelTrait<
95 FloatType,
Architecture>::DiffusiveFluxKernelElectrostaticThermalized;
97 using DiffusiveFluxKernel = std::variant<DiffusiveFluxKernelUnthermalized,
98 DiffusiveFluxKernelThermalized>;
99 using DiffusiveFluxKernelElectrostatic =
100 std::variant<DiffusiveFluxKernelElectrostaticUnthermalized,
101 DiffusiveFluxKernelElectrostaticThermalized>;
120 template <
typename FT, lbmpy::Arch AT = lbmpy::Arch::CPU>
struct FieldTrait {
124 template <
class Field>
125 using PackInfo = field::communication::PackInfo<Field>;
126 template <
class Stencil>
128 blockforest::communication::UniformBufferedScheme<Stencil>;
129 template <
class Stencil>
131 blockforest::communication::UniformBufferedScheme<Stencil>;
133 using FlagField = walberla::FlagField<walberla::uint8_t>;
134#if defined(__CUDACC__) and defined(WALBERLA_BUILD_WITH_CUDA)
138 template <
class Field>
139 using MemcpyPackInfo = gpu::communication::MemcpyPackInfo<Field>;
142 template <
typename Stencil>
143 class UniformGPUScheme
144 :
public gpu::communication::UniformGPUScheme<Stencil> {
146 explicit UniformGPUScheme(
auto const &
bf)
147 : gpu::communication::UniformGPUScheme<Stencil>(
154 template <
class Stencil>
156 template <
class Stencil>
158 blockforest::communication::UniformBufferedScheme<Stencil>;
160 using GPUField = gpu::GPUField<FloatType>;
181#if defined(__CUDACC__) and defined(WALBERLA_BUILD_WITH_CUDA)
195 return std::is_same_v<FloatType, double>;
199 FloatType m_diffusion;
204 bool m_friction_coupling;
216#if defined(__CUDACC__) and defined(WALBERLA_BUILD_WITH_CUDA)
233 std::unique_ptr<DiffusiveFluxKernelElectrostatic>
237#if defined(__CUDACC__) and defined(WALBERLA_BUILD_WITH_CUDA)
256 template <
typename Field>
261 return field::addToStorage<Field>(
blocks,
tag, FloatType{value},
264#if defined(__CUDACC__) and defined(WALBERLA_BUILD_WITH_CUDA)
266 auto field_id = gpu::addGPUFieldToStorage<GPUField>(
268 if constexpr (std::is_same_v<Field, _DensityField>) {
273 }
else if constexpr (std::is_same_v<Field, _FluxField>) {
277 std::array<FloatType, FluxCount>{});
303 typename stencil::D3Q27>;
306 typename stencil::D3Q27>;
311 template <
class Field>
337#if defined(__CUDACC__) and defined(WALBERLA_BUILD_WITH_CUDA)
339 std::make_shared<gpu::HostFieldAllocator<FloatType>>();
345 set_diffusion_kernels();
361 std::make_shared<BoundaryFullCommunicator>(
blocks);
386 return m_friction_coupling;
392 return static_cast<bool>(
404 return {
static_cast<uint64_t>(kernel->getTime_step())};
409 auto visitor = [m_diffusion = m_diffusion](
auto &kernel) {
410 kernel.setD(m_diffusion);
418 std::visit([m_kT = m_kT](
auto &kernel) { kernel.setKt(m_kT); },
425 [m_valency = m_valency](
auto &kernel) { kernel.setZ(m_valency); },
439 std::get_if<DiffusiveFluxKernelElectrostaticThermalized>(
443 throw std::runtime_error(
"This EK instance is unthermalized");
446 static_cast<uint32_t>(std::numeric_limits<uint_t>::max()));
447 kernel->setTime_step(
static_cast<uint32_t>(counter));
452 m_ext_efield = field;
455 [
this](
auto &kernel) {
466 (*m_full_communication)();
481 void set_diffusion_kernels() {
482 auto kernel = DiffusiveFluxKernelUnthermalized(
484 m_diffusive_flux = std::make_unique<DiffusiveFluxKernel>(std::move(kernel));
493 std::make_unique<DiffusiveFluxKernelElectrostatic>(
501 auto kernel = DiffusiveFluxKernelThermalized(
519 m_diffusive_flux = std::make_unique<DiffusiveFluxKernel>(std::move(kernel));
521 std::make_unique<DiffusiveFluxKernelElectrostatic>(
525 void kernel_boundary_density() {
527 (*m_boundary_density)(&
block);
531 void kernel_boundary_flux() {
533 (*m_boundary_flux)(&
block);
537 void kernel_continuity() {
539 (*m_continuity).run(&
block);
543 void kernel_diffusion() {
545 std::visit([&
block](
auto &kernel) { kernel.run(&
block); },
551 kernel->setTime_step(kernel->getTime_step() + 1u);
554 std::get_if<DiffusiveFluxKernelElectrostaticThermalized>(
561 void kernel_advection(std::size_t
const velocity_id) {
569 void kernel_friction_coupling(std::size_t
const force_id,
570 double const lb_density) {
571 auto kernel = FrictionCouplingKernel(
579 void kernel_diffusion_electrostatic(std::size_t
const potential_id) {
581 std::visit([phiID](
auto &kernel) { kernel.setPhiID(phiID); },
585 std::visit([&
block](
auto &kernel) { kernel.run(&
block); },
590 std::get_if<DiffusiveFluxKernelElectrostaticThermalized>(
597 kernel->setTime_step(kernel->getTime_step() + 1u);
601 void kernel_migration() {}
603 void update_boundary_fields() {
621 std::size_t
force_id,
double lb_density)
override {
624 update_boundary_fields();
631 throw std::runtime_error(
"Walberla EK: electrostatic potential enabled "
632 "but no field accessible. potential id is " +
641 kernel_boundary_flux();
644 if (
force_id == walberla::BlockDataID{}) {
645 throw std::runtime_error(
"Walberla EK: friction coupling enabled but "
646 "no force field accessible. force_id is " +
648 ". Hint: LB may be inactive.");
650 kernel_friction_coupling(
force_id, lb_density);
655 throw std::runtime_error(
"Walberla EK: advection enabled but no "
656 "velocity field accessible. velocity_id is " +
658 ". Hint: LB may be inactive.");
661 kernel_boundary_flux();
666 kernel_boundary_density();
674 static_assert(std::is_same_v<std::size_t, walberla::uint_t>);
706 std::vector<double>
out;
712 out = std::vector<double>(
ci->numCells());
740 std::vector<double>
const &
density)
override {
751 std::vector<FloatType>
values(
bci->numCells());
766 [[
nodiscard]] std::optional<Utils::Vector3d>
782 std::vector<double>
out;
788 out = std::vector<double>(3u *
ci->numCells());
807 for (
uint_t f = 0
u; f < 3u; ++f) {
811 for (
uint_t f = 0
u; f < 3u; ++f) {
847 [[
nodiscard]] std::optional<Utils::Vector3d>
892 std::vector<std::optional<double>>
const &
density)
override {
905 auto const &
opt = *
it;
919 [[
nodiscard]] std::vector<std::optional<double>>
923 std::vector<std::optional<double>>
out;
929 auto const n_values =
ci->numCells();
930 out.reserve(n_values);
939 out.emplace_back(std::nullopt);
951 std::vector<std::optional<Utils::Vector3d>>
const &
flux)
override {
965 auto const &
opt = *
it;
979 [[
nodiscard]] std::vector<std::optional<Utils::Vector3d>>
983 std::vector<std::optional<Utils::Vector3d>>
out;
989 auto const n_values =
ci->numCells();
990 out.reserve(n_values);
999 out.emplace_back(std::nullopt);
1012 std::vector<bool>
out;
1018 auto const n_values =
ci->numCells();
1019 out.reserve(n_values);
1050 return std::nullopt;
1060 return std::nullopt;
1070 return std::nullopt;
1078 const std::vector<double> &
data_flat)
override {
1088 const std::vector<double> &
data_flat)
override {
1117 template <
typename Field_T, u
int_t F_SIZE_ARG,
typename OutputType>
1118 class VTKWriter :
public vtk::BlockCellDataWriter<OutputType, F_SIZE_ARG> {
1137#if defined(__CUDACC__) and defined(WALBERLA_BUILD_WITH_CUDA)
1138 template <
typename OutputType =
float>
1143 using Base::evaluate;
1154 template <
typename OutputType =
float>
1159 using Base::evaluate;
1171#if defined(__CUDACC__) and defined(WALBERLA_BUILD_WITH_CUDA)
1172 template <
typename OutputType =
float>
1173 class FluxVTKWriter :
public VTKWriter<FluxFieldCpu, 3u, OutputType> {
1177 using Base::evaluate;
1189 template <
typename OutputType =
float>
1194 using Base::evaluate;
1211#if defined(__CUDACC__) and defined(WALBERLA_BUILD_WITH_CUDA)
1213 [&]<
typename Field>(
auto const &
blocks, std::string name,
1214 std::optional<BlockDataID> &
cpu_field) {
1217 blocks, name, FloatType{0}, field::fzyx,
1224#if defined(__CUDACC__) and defined(WALBERLA_BUILD_WITH_CUDA)
1230 gpu::fieldCpyFunctor<DensityFieldCpu, DensityField>(
1238#if defined(__CUDACC__) and defined(WALBERLA_BUILD_WITH_CUDA)
1244#if defined(__CUDACC__) and defined(WALBERLA_BUILD_WITH_CUDA)
1249 vtk_obj.addBeforeFunction(gpu::fieldCpyFunctor<FluxFieldCpu, FluxField>(
1257#if defined(__CUDACC__) and defined(WALBERLA_BUILD_WITH_CUDA)
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.
VTKWriter< DensityField, 1u, OutputType > Base
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
VTKWriter< FluxField, 3u, OutputType > Base
VTKWriter(ConstBlockDataID const &block_id, std::string const &id, FloatType unit_conversion)
ConstBlockDataID const m_block_id
void configure() override
FloatType const m_conversion
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
Base class for LB field VTK writers.
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