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>
32#include "../BoundaryHandling.hpp"
33#include "../utils/boundary.hpp"
34#include "../utils/types_conversion.hpp"
56template <std::
size_t FluxCount = 13,
typename FloatType =
double>
58 using ContinuityKernel =
60 using DiffusiveFluxKernelUnthermalized =
62 using DiffusiveFluxKernelThermalized =
64 using AdvectiveFluxKernel =
66 using FrictionCouplingKernel =
68 using DiffusiveFluxKernelElectrostaticUnthermalized =
70 using DiffusiveFluxKernelElectrostaticThermalized =
71 typename detail::KernelTrait<
72 FloatType>::DiffusiveFluxKernelElectrostaticThermalized;
74 using DiffusiveFluxKernel = std::variant<DiffusiveFluxKernelUnthermalized,
75 DiffusiveFluxKernelThermalized>;
76 using DiffusiveFluxKernelElectrostatic =
77 std::variant<DiffusiveFluxKernelElectrostaticUnthermalized,
78 DiffusiveFluxKernelElectrostaticThermalized>;
85 using FluxField = GhostLayerField<FloatType, FluxCount>;
86 using FlagField = walberla::FlagField<walberla::uint8_t>;
96 return numeric_cast<FloatType>(t);
104 return std::is_same<FloatType, double>::value;
108 FloatType m_diffusion;
113 bool m_friction_coupling;
139 std::unique_ptr<DiffusiveFluxKernelElectrostatic>
147 [[nodiscard]] std::optional<CellInterval>
151 auto const &cell_min = lower_corner;
155 if (not lower_bc or not upper_bc) {
158 assert(&(*(lower_bc->block)) == &(*(upper_bc->block)));
159 return {CellInterval(lower_bc->cell, upper_bc->cell)};
175 typename stencil::D3Q27>;
181 double density,
bool advection,
bool friction_coupling,
182 bool thermalized,
unsigned int seed)
184 m_valency(
FloatType_c(valency)), m_ext_efield(ext_efield),
185 m_advection(advection), m_friction_coupling(friction_coupling),
186 m_seed(seed),
m_lattice(std::move(lattice)) {
188 auto const &blocks =
m_lattice->get_blocks();
189 auto const n_ghost_layers =
m_lattice->get_ghost_layers();
195 field::addFlattenedShallowCopyToStorage<DensityField>(
198 blocks,
"flux field", FloatType{0}, field::fzyx, n_ghost_layers);
200 field::addFlattenedShallowCopyToStorage<FluxField>(
209 set_diffusion_kernels();
214 blocks,
"flag field density", n_ghost_layers);
218 blocks,
"flag field flux", n_ghost_layers);
223 std::make_shared<field::communication::PackInfo<DensityField>>(
231 [[nodiscard]]
double get_kT() const noexcept
override {
return m_kT; }
239 return m_friction_coupling;
245 return static_cast<bool>(
248 [[nodiscard]]
unsigned int get_seed() const noexcept
override {
257 return {
static_cast<uint64_t
>(kernel->getTime_step())};
262 auto visitor = [m_diffusion = m_diffusion](
auto &kernel) {
263 kernel.setD(m_diffusion);
271 std::visit([m_kT = m_kT](
auto &kernel) { kernel.setKt(m_kT); },
278 [m_valency = m_valency](
auto &kernel) { kernel.setZ(m_valency); },
285 m_friction_coupling = friction_coupling;
291 auto const kernel_electrostatic =
292 std::get_if<DiffusiveFluxKernelElectrostaticThermalized>(
295 if (!kernel or !kernel_electrostatic) {
296 throw std::runtime_error(
"This EK instance is unthermalized");
299 static_cast<uint32_t
>(std::numeric_limits<uint_t>::max()));
300 kernel->setTime_step(
static_cast<uint32_t
>(counter));
301 kernel_electrostatic->setTime_step(
static_cast<uint32_t
>(counter));
305 m_ext_efield = field;
308 [
this](
auto &kernel) {
319 void set_diffusion_kernels() {
323 m_diffusive_flux = std::make_unique<DiffusiveFluxKernel>(std::move(kernel));
325 auto kernel_electrostatic = DiffusiveFluxKernelElectrostaticUnthermalized(
332 std::make_unique<DiffusiveFluxKernelElectrostatic>(
333 std::move(kernel_electrostatic));
340 auto kernel = DiffusiveFluxKernelThermalized(
342 FloatType_c(m_diffusion), grid_dim[0], grid_dim[1], grid_dim[2], seed,
345 auto kernel_electrostatic = DiffusiveFluxKernelElectrostaticThermalized(
354 for (
auto b = blocks->begin(); b != blocks->end(); ++b) {
355 kernel.configure(blocks, &*b);
356 kernel_electrostatic.configure(blocks, &*b);
359 m_diffusive_flux = std::make_unique<DiffusiveFluxKernel>(std::move(kernel));
361 std::make_unique<DiffusiveFluxKernelElectrostatic>(
362 std::move(kernel_electrostatic));
365 void kernel_boundary_density() {
367 (*m_boundary_density)(&
block);
371 void kernel_boundary_flux() {
373 (*m_boundary_flux)(&
block);
377 void kernel_continuity() {
379 (*m_continuity).run(&
block);
383 void kernel_diffusion() {
385 std::visit([&
block](
auto &kernel) { kernel.run(&
block); },
391 kernel->setTime_step(kernel->getTime_step() + 1u);
393 auto *kernel_electrostatic =
394 std::get_if<DiffusiveFluxKernelElectrostaticThermalized>(
396 kernel_electrostatic->setTime_step(kernel_electrostatic->getTime_step() +
401 void kernel_advection(
const std::size_t &velocity_id) {
404 BlockDataID(velocity_id));
410 void kernel_friction_coupling(
const std::size_t &force_id) {
411 auto kernel = FrictionCouplingKernel(
419 void kernel_diffusion_electrostatic(
const std::size_t &potential_id) {
420 auto const phiID = BlockDataID(potential_id);
421 std::visit([phiID](
auto &kernel) { kernel.setPhiID(phiID); },
425 std::visit([&
block](
auto &kernel) { kernel.run(&
block); },
429 if (
auto *kernel_electrostatic =
430 std::get_if<DiffusiveFluxKernelElectrostaticThermalized>(
432 kernel_electrostatic->setTime_step(kernel_electrostatic->getTime_step() +
437 kernel->setTime_step(kernel->getTime_step() + 1u);
441 void kernel_migration() {}
443 void updated_boundary_fields() {
451 auto &vtk_handle = it.second;
452 if (vtk_handle->enabled) {
453 vtk::writeFiles(vtk_handle->ptr)();
454 vtk_handle->execution_count++;
460 void integrate(std::size_t potential_id, std::size_t velocity_id,
461 std::size_t force_id)
override {
463 updated_boundary_fields();
469 if (potential_id == walberla::BlockDataID{}) {
470 throw std::runtime_error(
"Walberla EK: electrostatic potential enabled "
471 "but no field accessible. potential id is " +
472 std::to_string(potential_id));
474 kernel_diffusion_electrostatic(potential_id);
480 kernel_boundary_flux();
483 if (force_id == walberla::BlockDataID{}) {
484 throw std::runtime_error(
"Walberla EK: friction coupling enabled but "
485 "no force field accessible. force_id is " +
486 std::to_string(force_id) +
487 ". Hint: LB may be inactive.");
489 kernel_friction_coupling(force_id);
493 if (velocity_id == walberla::BlockDataID{}) {
494 throw std::runtime_error(
"Walberla EK: advection enabled but no "
495 "velocity field accessible. velocity_id is " +
496 std::to_string(velocity_id) +
497 ". Hint: LB may be inactive.");
499 kernel_advection(velocity_id);
504 kernel_boundary_density();
511 static_assert(std::is_same_v<std::size_t, walberla::uint_t>);
527 [[nodiscard]] std::optional<double>
529 bool consider_ghosts =
false)
const override {
535 auto const density_field =
538 return {double_c(density_field->get(bc->cell))};
541 [[nodiscard]] std::vector<double>
544 std::vector<double> out;
545 if (
auto const ci =
get_interval(lower_corner, upper_corner)) {
548 auto const density_field =
550 auto const lower_cell = ci->min();
551 auto const upper_cell = ci->max();
552 auto const n_values = ci->numCells();
553 out.reserve(n_values);
554 for (
auto x = lower_cell.x(); x <= upper_cell.x(); ++x) {
555 for (
auto y = lower_cell.y(); y <= upper_cell.y(); ++y) {
556 for (
auto z = lower_cell.z(); z <= upper_cell.z(); ++z) {
557 out.emplace_back(density_field->get(
Cell{x, y, z}));
561 assert(out.size() == n_values);
568 std::vector<double>
const &
density)
override {
569 if (
auto const ci =
get_interval(lower_corner, upper_corner)) {
575 auto const lower_cell = ci->min();
576 auto const upper_cell = ci->max();
577 assert(
density.size() == ci->numCells());
578 for (
auto x = lower_cell.x(); x <= upper_cell.x(); ++x) {
579 for (
auto y = lower_cell.y(); y <= upper_cell.y(); ++y) {
580 for (
auto z = lower_cell.z(); z <= upper_cell.z(); ++z) {
604 node, to_vector3<FloatType>(flux), *bc);
609 [[nodiscard]] std::optional<Utils::Vector3d>
611 bool consider_ghosts =
false)
const override {
641 [[nodiscard]] std::optional<double>
643 bool consider_ghosts =
false)
const override {
653 std::vector<std::optional<double>>
const &
density)
override {
654 if (
auto const ci =
get_interval(lower_corner, upper_corner)) {
657 auto const lower_cell = ci->min();
658 auto const upper_cell = ci->max();
660 assert(
density.size() == ci->numCells());
661 for (
auto x = lower_cell.x(); x <= upper_cell.x(); ++x) {
662 for (
auto y = lower_cell.y(); y <= upper_cell.y(); ++y) {
663 for (
auto z = lower_cell.z(); z <= upper_cell.z(); ++z) {
666 auto const &opt = *it;
680 [[nodiscard]] std::vector<std::optional<double>>
684 std::vector<std::optional<double>> out;
685 if (
auto const ci =
get_interval(lower_corner, upper_corner)) {
688 auto const lower_cell = ci->min();
689 auto const upper_cell = ci->max();
690 auto const n_values = ci->numCells();
691 out.reserve(n_values);
692 for (
auto x = lower_cell.x(); x <= upper_cell.x(); ++x) {
693 for (
auto y = lower_cell.y(); y <= upper_cell.y(); ++y) {
694 for (
auto z = lower_cell.z(); z <= upper_cell.z(); ++z) {
697 out.emplace_back(double_c(
700 out.emplace_back(std::nullopt);
705 assert(out.size() == n_values);
712 std::vector<std::optional<Utils::Vector3d>>
const &flux)
override {
713 if (
auto const ci =
get_interval(lower_corner, upper_corner)) {
716 auto const lower_cell = ci->min();
717 auto const upper_cell = ci->max();
718 auto it = flux.begin();
719 assert(flux.size() == ci->numCells());
720 for (
auto x = lower_cell.x(); x <= upper_cell.x(); ++x) {
721 for (
auto y = lower_cell.y(); y <= upper_cell.y(); ++y) {
722 for (
auto z = lower_cell.z(); z <= upper_cell.z(); ++z) {
725 auto const &opt = *it;
728 node, to_vector3<FloatType>(*opt), *bc);
739 [[nodiscard]] std::vector<std::optional<Utils::Vector3d>>
743 std::vector<std::optional<Utils::Vector3d>> out;
744 if (
auto const ci =
get_interval(lower_corner, upper_corner)) {
747 auto const lower_cell = ci->min();
748 auto const upper_cell = ci->max();
749 auto const n_values = ci->numCells();
750 out.reserve(n_values);
751 for (
auto x = lower_cell.x(); x <= upper_cell.x(); ++x) {
752 for (
auto y = lower_cell.y(); y <= upper_cell.y(); ++y) {
753 for (
auto z = lower_cell.z(); z <= upper_cell.z(); ++z) {
759 out.emplace_back(std::nullopt);
764 assert(out.size() == n_values);
769 [[nodiscard]] std::vector<bool>
772 std::vector<bool> out;
773 if (
auto const ci =
get_interval(lower_corner, upper_corner)) {
776 auto const lower_cell = ci->min();
777 auto const upper_cell = ci->max();
778 auto const n_values = ci->numCells();
779 out.reserve(n_values);
780 for (
auto x = lower_cell.x(); x <= upper_cell.x(); ++x) {
781 for (
auto y = lower_cell.y(); y <= upper_cell.y(); ++y) {
782 for (
auto z = lower_cell.z(); z <= upper_cell.z(); ++z) {
789 assert(out.size() == n_values);
804 [[nodiscard]] std::optional<bool>
806 bool consider_ghosts)
const override {
814 [[nodiscard]] std::optional<bool>
816 bool consider_ghosts)
const override {
824 [[nodiscard]] std::optional<bool>
826 bool consider_ghosts =
false)
const override {
836 const std::vector<int> &raster_flat,
837 const std::vector<double> &data_flat)
override {
845 const std::vector<int> &raster_flat,
846 const std::vector<double> &data_flat)
override {
867 vtk_obj.addCellExclusionFilter(fluid_filter);
871 template <
typename Field_T, u
int_t F_SIZE_ARG,
typename OutputType>
872 class VTKWriter :
public vtk::BlockCellDataWriter<OutputType, F_SIZE_ARG> {
874 VTKWriter(ConstBlockDataID
const &block_id, std::string
const &
id,
875 FloatType unit_conversion)
876 : vtk::BlockCellDataWriter<OutputType, F_SIZE_ARG>(id),
882 WALBERLA_ASSERT_NOT_NULLPTR(this->block_);
891 template <
typename OutputType = float,
898 OutputType
evaluate(cell_idx_t
const x, cell_idx_t
const y,
899 cell_idx_t
const z, cell_idx_t
const)
override {
900 WALBERLA_ASSERT_NOT_NULLPTR(this->
m_field);
901 auto const density = VectorTrait<typename DensityField::value_type>::get(
902 this->
m_field->get(x, y, z, 0), uint_c(0));
910 int flag_observables)
override {
912 auto const unit_conversion =
FloatType_c(units.at(
"density"));
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.
std::pair< Utils::Vector3i, Utils::Vector3i > get_local_grid_range() const
auto const & get_grid_dimensions() const
walberla::blockforest::StructuredBlockForest Lattice_T
static DEVICE_QUALIFIER constexpr Vector< T, N > broadcast(typename Base::value_type const &value) noexcept
Create a vector that has all entries set to the same value.
Boundary class optimized for sparse data.
OutputType evaluate(cell_idx_t const x, cell_idx_t const y, cell_idx_t const z, cell_idx_t const) override
FloatType const m_conversion
void configure() override
VTKWriter(ConstBlockDataID const &block_id, std::string const &id, FloatType unit_conversion)
ConstBlockDataID const m_block_id
Class that runs and controls the EK on waLBerla.
void update_density_boundary_from_shape(const std::vector< int > &raster_flat, const std::vector< double > &data_flat) override
double get_kT() const noexcept override
std::optional< CellInterval > get_interval(Utils::Vector3i const &lower_corner, Utils::Vector3i const &upper_corner) const
blockforest::communication::UniformBufferedScheme< typename stencil::D3Q27 > FullCommunicator
std::shared_ptr< LatticeWalberla > m_lattice
Block forest.
bool get_friction_coupling() const noexcept override
void set_friction_coupling(bool friction_coupling) override
void register_vtk_field_writers(walberla::vtk::VTKOutput &vtk_obj, LatticeModel::units_map const &units, int flag_observables) override
std::vector< std::optional< double > > get_slice_density_at_boundary(Utils::Vector3i const &lower_corner, Utils::Vector3i const &upper_corner) const override
void reset_density_boundary_handling(std::shared_ptr< BlockStorage > const &blocks)
void set_slice_density_boundary(Utils::Vector3i const &lower_corner, Utils::Vector3i const &upper_corner, std::vector< std::optional< double > > const &density) override
~EKinWalberlaImpl() override=default
double get_valency() const noexcept override
std::optional< uint64_t > get_rng_state() const override
void register_vtk_field_filters(walberla::vtk::VTKOutput &vtk_obj) override
void set_rng_state(uint64_t counter) override
std::unique_ptr< DiffusiveFluxKernelElectrostatic > m_diffusive_flux_electrostatic
walberla::FlagField< walberla::uint8_t > FlagField
void set_slice_flux_boundary(Utils::Vector3i const &lower_corner, Utils::Vector3i const &upper_corner, std::vector< std::optional< Utils::Vector3d > > const &flux) override
bool remove_node_from_flux_boundary(Utils::Vector3i const &node) override
void clear_flux_boundaries() override
bool remove_node_from_density_boundary(Utils::Vector3i const &node) override
std::optional< Utils::Vector3d > get_node_flux_at_boundary(Utils::Vector3i const &node, bool consider_ghosts=false) const override
void set_diffusion(double diffusion) override
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::size_t stencil_size() const noexcept override
BlockDataID m_flag_field_density_id
void update_flux_boundary_from_shape(const std::vector< int > &raster_flat, const std::vector< double > &data_flat) override
bool set_node_density_boundary(Utils::Vector3i const &node, double density) override
bool is_thermalized() const noexcept override
void set_ext_efield(Utils::Vector3d const &field) override
std::vector< double > get_slice_density(Utils::Vector3i const &lower_corner, Utils::Vector3i const &upper_corner) const override
std::unique_ptr< BoundaryModelDensity > m_boundary_density
std::optional< bool > get_node_is_flux_boundary(Utils::Vector3i const &node, bool consider_ghosts) const override
BlockDataID m_flux_field_id
unsigned int get_seed() const noexcept override
BlockDataID m_density_field_flattened_id
bool get_advection() const noexcept override
std::size_t get_density_id() const noexcept override
GhostLayerField< FloatType, FluxCount > FluxField
FloatType FloatType_c(T t)
void integrate_vtk_writers() override
double get_diffusion() const noexcept override
FlagUID const Boundary_flag
Flag for boundary cells.
BlockDataID m_density_field_id
bool set_node_flux_boundary(Utils::Vector3i const &node, Utils::Vector3d const &flux) override
std::optional< double > get_node_density(Utils::Vector3i const &node, bool consider_ghosts=false) const override
std::shared_ptr< FullCommunicator > m_full_communication
std::unique_ptr< BoundaryModelFlux > m_boundary_flux
void set_slice_density(Utils::Vector3i const &lower_corner, Utils::Vector3i const &upper_corner, std::vector< double > const &density) override
FlagUID const Domain_flag
Flag for domain cells, i.e.
bool is_double_precision() const noexcept override
void ghost_communication() override
void reallocate_density_boundary_field()
void set_kT(double kT) override
void set_valency(double valency) override
std::optional< double > get_node_density_at_boundary(Utils::Vector3i const &node, bool consider_ghosts=false) const override
void integrate(std::size_t potential_id, std::size_t velocity_id, std::size_t force_id) override
LatticeWalberla::Lattice_T BlockStorage
std::optional< bool > get_node_is_density_boundary(Utils::Vector3i const &node, bool consider_ghosts) const override
void reallocate_flux_boundary_field()
GhostLayerField< FloatType, 1 > DensityField
Utils::Vector3d get_ext_efield() const noexcept override
LatticeWalberla const & get_lattice() const noexcept override
bool set_node_density(Utils::Vector3i const &node, double density) override
std::unique_ptr< ContinuityKernel > m_continuity
BlockDataID m_flux_field_flattened_id
void clear_density_boundaries() override
std::optional< bool > get_node_is_boundary(Utils::Vector3i const &node, bool consider_ghosts=false) const override
std::unique_ptr< DiffusiveFluxKernel > m_diffusive_flux
void set_advection(bool advection) override
BlockDataID m_flag_field_flux_id
std::vector< std::optional< Utils::Vector3d > > get_slice_flux_at_boundary(Utils::Vector3i const &lower_corner, Utils::Vector3i const &upper_corner) const override
void reset_flux_boundary_handling(std::shared_ptr< BlockStorage > const &blocks)
std::vector< bool > get_slice_is_boundary(Utils::Vector3i const &lower_corner, Utils::Vector3i const &upper_corner) const override
static double * block(double *p, std::size_t index, std::size_t size)
static FUNC_PREFIX double *RESTRICT 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 int64_t const uint32_t uint32_t uint32_t double double double double double uint32_t seed
\file PackInfoPdfDoublePrecision.cpp \author pystencils
std::optional< BlockAndCell > get_block_and_cell(::LatticeWalberla const &lattice, Utils::Vector3i const &node, bool consider_ghost_layers)
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)
std::vector< Utils::Vector3d > fill_3D_vector_array(std::vector< double > const &vec_flat, Utils::Vector3i const &grid_size)