ESPResSo
Extensible Simulation Package for Research on Soft Matter Systems
Loading...
Searching...
No Matches
LBWalberlaImpl.hpp
Go to the documentation of this file.
1/*
2 * Copyright (C) 2019-2026 The ESPResSo project
3 *
4 * This file is part of ESPResSo.
5 *
6 * ESPResSo is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * ESPResSo is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 */
19
20#pragma once
21
22/**
23 * @file
24 * @ref walberla::LBWalberlaImpl implements the interface of the LB
25 * waLBerla bridge using sweeps generated by lbmpy
26 * (see <tt>maintainer/walberla_kernels</tt>).
27 */
28
29#include <blockforest/Initialization.h>
30#include <blockforest/StructuredBlockForest.h>
31#include <domain_decomposition/BlockDataID.h>
32#include <domain_decomposition/IBlock.h>
33#include <field/AddToStorage.h>
34#include <field/vtk/FlagFieldCellFilter.h>
35#include <field/vtk/VTKWriter.h>
36#include <stencil/D3Q19.h>
37#include <stencil/D3Q27.h>
38#include <waLBerlaDefinitions.h>
39#if defined(__CUDACC__) and defined(WALBERLA_BUILD_WITH_CUDA)
40#include <gpu/AddGPUFieldToStorage.h>
41#include <gpu/HostFieldAllocator.h>
42#endif
43
44#include "../BoundaryHandling.hpp"
45#include "../BoundaryPackInfo.hpp"
46#include "../utils/boundary.hpp"
47#include "../utils/types_conversion.hpp"
49#include "ResetForce.hpp"
50#include "lb_fields.hpp"
51#include "lb_kernels.hpp"
52#if defined(__CUDACC__) and defined(WALBERLA_BUILD_WITH_CUDA)
53#include "lb_fields.cuh"
54#include "lb_kernels.cuh"
55#endif
56
64
65#include <utils/Vector.hpp>
66
67#include <array>
68#include <bitset>
69#include <cstddef>
70#include <functional>
71#include <initializer_list>
72#include <limits>
73#include <memory>
74#include <optional>
75#include <stdexcept>
76#include <string>
77#include <type_traits>
78#include <utility>
79#include <variant>
80#include <vector>
81
82namespace walberla {
83
84/** @brief Class that runs and controls the LB on waLBerla. */
85template <typename FloatType, lbmpy::Arch Architecture>
87#if not defined(WALBERLA_BUILD_WITH_CUDA)
88 static_assert(Architecture != lbmpy::Arch::GPU,
89 "waLBerla was compiled without CUDA support");
90#endif
91protected:
92 // ---- Types & Constants ----
93
94 using Kernels = detail::KernelTrait<FloatType, Architecture>;
96 typename Kernels::DynamicUBB>;
98 std::variant<typename Kernels::StreamCollisionModelThermalized,
100
101public:
102 /** @brief Stencil for collision and streaming operations. */
103 using Stencil = stencil::D3Q19;
104 /** @brief Stencil for ghost communication (includes domain corners). */
105 using StencilFull = stencil::D3Q27;
106 /** @brief Lattice model (e.g. blockforest). */
108
109protected:
110 // "underlying" field types (`GPUField` has no f-size info at compile time)
113
114public:
118#if defined(__CUDACC__) and defined(WALBERLA_BUILD_WITH_CUDA)
119 using GPUField = gpu::GPUField<FloatType>;
120 using PdfFieldCpu =
122 using VectorFieldCpu =
124#endif
125
126 struct GhostComm {
127 /** @brief Ghost communication operations. */
128 enum GhostCommFlags : unsigned {
129 PDF, ///< PDFs communication
130 VEL, ///< velocities communication
131 LAF, ///< last applied forces communication
132 UBB, ///< boundaries communication
133 SIZE
134 };
135 };
136
137protected:
138 /**
139 * @brief Full communicator.
140 * We use the D3Q27 directions to update cells along the diagonals during
141 * a full ghost communication. This is needed to properly update the corners
142 * of the ghost layer when setting cell velocities or populations.
143 */
145 FieldTrait<FloatType, Stencil,
148 FieldTrait<FloatType, Stencil,
150 /**
151 * @brief Regular communicator.
152 * We use the same directions as the stencil during integration.
153 */
155 FieldTrait<FloatType, Stencil,
157 template <class Field>
158 using PackInfo =
160
161protected:
162 // ---- Member Variables ----
163
164 // Physical parameters
165 FloatType m_viscosity; /// kinematic viscosity
166 FloatType m_density;
167 FloatType m_kT;
168 unsigned int m_seed;
169 double m_zc_to_md; // zero-centered conversion factor to MD units
170 double m_zc_to_lb; // zero-centered conversion factor to LB units
171
172 // lattice
173 std::shared_ptr<LatticeWalberla> m_lattice;
174
175 // Block data access handles
179
182
185
186#if defined(__CUDACC__) and defined(WALBERLA_BUILD_WITH_CUDA)
187 std::optional<BlockDataID> m_pdf_cpu_field_id;
188 std::optional<BlockDataID> m_vel_cpu_field_id;
189#endif
190
191 /** Flag for boundary cells. */
192 FlagUID const Boundary_flag{"boundary"};
193 bool m_has_boundaries{false};
194
195 // boundaries
196 std::shared_ptr<BoundaryModel> m_boundary;
197
198 // communicators
199 std::shared_ptr<BoundaryFullCommunicator> m_boundary_communicator;
200 std::shared_ptr<RegularFullCommunicator> m_full_communicator;
201 std::shared_ptr<RegularFullCommunicator> m_pdf_communicator;
202 std::shared_ptr<RegularFullCommunicator> m_vel_communicator;
203 std::shared_ptr<RegularFullCommunicator> m_laf_communicator;
204 std::shared_ptr<PDFStreamingCommunicator> m_pdf_streaming_communicator;
205 std::bitset<GhostComm::SIZE> m_pending_ghost_comm;
207
208 // collision sweep
209 std::shared_ptr<CollisionModel> m_collision_model;
210
211 // force reset sweep + external force handling
212 std::shared_ptr<ResetForce<PdfField, VectorField>> m_reset_force;
213
214 // velocity update sweep
215 std::shared_ptr<typename Kernels::UpdateVelFromPDF>
217
218 // Lees-Edwards boundary interpolation
219 std::shared_ptr<LeesEdwardsPack> m_lees_edwards_callbacks;
220 std::shared_ptr<InterpolateAndShiftAtBoundary<_PdfField, FloatType>>
222 std::shared_ptr<InterpolateAndShiftAtBoundary<_VectorField, FloatType>>
224 std::shared_ptr<InterpolateAndShiftAtBoundary<_VectorField, FloatType>>
226
227#if defined(__CUDACC__) and defined(WALBERLA_BUILD_WITH_CUDA)
228 std::shared_ptr<gpu::HostFieldAllocator<FloatType>> m_host_field_allocator;
229#endif
230
231public:
232 template <typename T> FloatType FloatType_c(T t) const {
233 return numeric_cast<FloatType>(t);
234 }
235
236 [[nodiscard]] std::size_t stencil_size() const noexcept override {
237 return static_cast<std::size_t>(Stencil::Size);
238 }
239
241 return std::is_same_v<FloatType, double>;
242 }
243
244 [[nodiscard]] bool is_gpu() const noexcept override {
246 }
247
248public:
249 LBWalberlaImpl(std::shared_ptr<LatticeWalberla> lattice, double viscosity,
250 double density)
252 m_kT(FloatType{0}), m_seed(0u), m_zc_to_md(density),
253 m_zc_to_lb(1. / density), m_lattice(std::move(lattice)),
255
256 auto const &blocks = m_lattice->get_blocks();
257 auto const n_ghost_layers = m_lattice->get_ghost_layers();
258 if (n_ghost_layers == 0u)
259 throw std::runtime_error("At least one ghost layer must be used");
260
261 // Initialize and register fields (must use the "underlying" types)
268#if defined(__CUDACC__) and defined(WALBERLA_BUILD_WITH_CUDA)
270 std::make_shared<gpu::HostFieldAllocator<FloatType>>();
271#endif
272
273 // Initialize and register pdf field with zero centered density
276 for (auto &block : *blocks) {
278 }
279
280 // Initialize and register flag field (fluid/boundary)
281 m_flag_field_id = field::addFlagFieldToStorage<FlagField>(
282 blocks, "flag field", n_ghost_layers);
283 // Initialize boundary sweep
284 reset_boundary_handling(m_lattice->get_blocks());
285
286 // Set up the communication and register fields
289
291
292 // Instantiate the sweep responsible for force double buffering and
293 // external forces
294 m_reset_force = std::make_shared<ResetForce<PdfField, VectorField>>(
296
297 // Instantiate velocity update sweep
299 std::make_shared<typename Kernels::UpdateVelFromPDF>(
301 }
302
303 ~LBWalberlaImpl() override = default;
304
305 // ---- Integration (Core LB Algorithm) ----
306
307 void integrate() override {
308 integrate_pull_scheme();
310 }
311
312protected:
313 void integrate_vtk_writers() override {
314 for (auto const &it : m_vtk_auto) {
315 auto &vtk_handle = it.second;
316 if (vtk_handle->enabled) {
317 vtk::writeFiles(vtk_handle->ptr)();
318 vtk_handle->execution_count++;
319 }
320 }
321 }
322
323private:
324 /**
325 * @brief One LB time step using the pull scheme.
326 * Sequence: reset forces, stream-collide, communicate PDFs,
327 * apply Lees-Edwards interpolation (if active), handle boundaries,
328 * update velocity field from PDFs.
329 */
330 void integrate_pull_scheme() {
332 auto const &blocks = get_lattice().get_blocks();
333 // Reset force fields
334 integrate_reset_force(blocks);
335 // LB stream collide
336 integrate_stream_collide(blocks);
337 // Mark pending ghost layer updates
338 // As pdf and laf are communicated directly afterwards, they are not set
341 m_pdf_streaming_communicator->communicate();
342 if (has_lees_edwards_bc()) {
343 apply_lees_edwards_pdf_interpolation(blocks);
344 apply_lees_edwards_last_applied_force_interpolation(blocks);
345 }
346 // Handle boundaries
347 if (m_has_boundaries) {
348 integrate_boundaries(blocks);
349 }
350 // Update velocities from pdfs
351 integrate_update_velocities_from_pdf(blocks);
352
353 if (has_lees_edwards_bc()) {
354 apply_lees_edwards_vel_interpolation_and_shift(blocks);
355 }
356 }
357
358 void integrate_stream_collide(std::shared_ptr<BlockStorage> const &blocks) {
360 for (auto &block : *blocks) {
361 auto const block_variant = std::variant<IBlock *>(&block);
362 std::visit(m_run_stream_collide_sweep, cm_variant, block_variant);
363 }
364 if (auto *cm =
365 std::get_if<typename Kernels::StreamCollisionModelThermalized>(
366 &cm_variant)) {
367 cm->setTime_step(cm->getTime_step() + 1u);
368 }
369 }
370
371 void integrate_reset_force(std::shared_ptr<BlockStorage> const &blocks) {
372 for (auto &block : *blocks)
373 (*m_reset_force)(&block);
374 }
375
376 void integrate_boundaries(std::shared_ptr<BlockStorage> const &blocks) {
377 for (auto &block : *blocks)
378 (*m_boundary)(&block);
379 }
380
381 void integrate_update_velocities_from_pdf(
382 std::shared_ptr<BlockStorage> const &blocks) {
383 for (auto &block : *blocks)
385 }
386
387private:
388 // ---- Collision Model ----
389
390 /**
391 * @brief Visitor for dispatching stream-collide sweeps.
392 * Handles both thermalized and Lees-Edwards collision models
393 * via @c std::visit on the @ref CollisionModel variant.
394 */
395 class StreamCollideSweepVisitor {
396 public:
397 using StructuredBlockStorage = LatticeWalberla::Lattice_T;
398
399 void operator()(typename Kernels::StreamCollisionModelThermalized &cm,
400 IBlock *b) {
401 cm.configure(m_storage, b);
402 cm(b);
403 }
404
405 void operator()(typename Kernels::StreamCollisionModelLeesEdwards &cm,
406 IBlock *b) {
407 cm.setV_s(static_cast<decltype(cm.getV_s())>(
408 m_lees_edwards_callbacks->get_shear_velocity()));
409 cm(b);
410 }
411
412 StreamCollideSweepVisitor() = default;
413 StreamCollideSweepVisitor(std::shared_ptr<StructuredBlockStorage> storage) {
414 m_storage = std::move(storage);
415 }
416 StreamCollideSweepVisitor(std::shared_ptr<StructuredBlockStorage> storage,
417 std::shared_ptr<LeesEdwardsPack> callbacks) {
418 m_storage = std::move(storage);
419 m_lees_edwards_callbacks = std::move(callbacks);
420 }
421
422 private:
423 std::shared_ptr<StructuredBlockStorage> m_storage{};
424 std::shared_ptr<LeesEdwardsPack> m_lees_edwards_callbacks{};
425 };
426 StreamCollideSweepVisitor m_run_stream_collide_sweep{};
427
428 /** @brief Relaxation rate omega from kinematic viscosity: 2/(6*nu+1). */
429 FloatType shear_mode_relaxation_rate() const;
430 /**
431 * @brief Odd-mode relaxation rate for the magic parameter relation.
432 * Ensures optimal bounce-back wall location for the two-relaxation-time
433 * model. Default magic number is 3/16.
434 */
435 FloatType odd_mode_relaxation_rate(
436 FloatType shear_relaxation,
437 FloatType magic_number = FloatType{3} / FloatType{16}) const;
438
439public:
440 void set_collision_model(double kT, unsigned int seed) override;
442 std::unique_ptr<LeesEdwardsPack> &&lees_edwards_pack) override;
443 void check_lebc(unsigned int shear_direction,
444 unsigned int shear_plane_normal) const override;
445
446public:
447 // ---- Ghost Communication ----
448
449 /**
450 * @brief Perform all pending ghost layer updates.
451 * Uses a lazy scheme: ghost communications are only executed when
452 * they have been marked as pending by a preceding write operation.
453 */
463
464 void ghost_communication_pdf() override {
467 m_pdf_communicator->communicate();
468 if (has_lees_edwards_bc()) {
469 auto const &blocks = get_lattice().get_blocks();
470 apply_lees_edwards_pdf_interpolation(blocks);
471 }
473 }
474 }
475
476 void ghost_communication_vel() override {
479 m_vel_communicator->communicate();
480 if (has_lees_edwards_bc()) {
481 auto const &blocks = get_lattice().get_blocks();
482 apply_lees_edwards_vel_interpolation_and_shift(blocks);
483 }
485 }
486 }
487
488 void ghost_communication_laf() override {
491 m_laf_communicator->communicate();
492 if (has_lees_edwards_bc()) {
493 auto const &blocks = get_lattice().get_blocks();
494 apply_lees_edwards_last_applied_force_interpolation(blocks);
495 }
497 }
498 }
499
507
508 /** @brief Communicate all fields at once using the D3Q27 stencil. */
519
520private:
521 // ---- Lees-Edwards Boundary Conditions ----
522
523 auto has_lees_edwards_bc() const {
524 return std::holds_alternative<
525 typename Kernels::StreamCollisionModelLeesEdwards>(*m_collision_model);
526 }
527
528 void apply_lees_edwards_pdf_interpolation(
529 std::shared_ptr<BlockStorage> const &blocks) {
530 for (auto &block : *blocks)
532 }
533
534 void apply_lees_edwards_vel_interpolation_and_shift(
535 std::shared_ptr<BlockStorage> const &blocks) {
536 for (auto &block : *blocks)
538 }
539
540 void apply_lees_edwards_last_applied_force_interpolation(
541 std::shared_ptr<BlockStorage> const &blocks) {
542 for (auto &block : *blocks)
544 }
545
546public:
548 auto const &blocks = get_lattice().get_blocks();
549 apply_lees_edwards_pdf_interpolation(blocks);
550 apply_lees_edwards_vel_interpolation_and_shift(blocks);
551 apply_lees_edwards_last_applied_force_interpolation(blocks);
552 }
553
554public:
555 // ---- Node & Slice Accessors (by quantity) ----
556
557 // Velocity
558 std::optional<Utils::Vector3d>
560 bool consider_ghosts = false) const override;
561 bool set_node_velocity(Utils::Vector3i const &node,
562 Utils::Vector3d const &v) override;
563 std::vector<double>
565 Utils::Vector3i const &upper_corner) const override;
568 std::vector<double> const &velocity) override;
569
570 // Density
571 std::optional<double>
573 bool consider_ghosts = false) const override;
574 bool set_node_density(Utils::Vector3i const &node, double density) override;
575 std::vector<double>
577 Utils::Vector3i const &upper_corner) const override;
580 std::vector<double> const &density) override;
581
582 // Population
583 std::optional<std::vector<double>>
585 bool consider_ghosts = false) const override;
586 bool set_node_population(Utils::Vector3i const &node,
587 std::vector<double> const &population) override;
588 std::vector<double>
590 Utils::Vector3i const &upper_corner) const override;
593 std::vector<double> const &population) override;
594
595 // Force
596 std::optional<Utils::Vector3d>
597 get_node_force_to_be_applied(Utils::Vector3i const &node) const override;
598 std::optional<Utils::Vector3d>
600 bool consider_ghosts = false) const override;
602 Utils::Vector3d const &force) override;
603 std::vector<double> get_slice_last_applied_force(
605 Utils::Vector3i const &upper_corner) const override;
608 std::vector<double> const &force) override;
609
610 // Pressure tensor
611 std::optional<Utils::VectorXd<9>>
612 get_node_pressure_tensor(Utils::Vector3i const &node) const override;
613 std::vector<double>
615 Utils::Vector3i const &upper_corner) const override;
616
617private:
618 // ---- Interpolation (position-based access) ----
619
620 /** @brief Return a B-spline interpolation kernel for force distribution. */
621 auto make_force_interpolation_kernel() const;
622 /** @brief Return a B-spline interpolation kernel for velocity readout. */
623 auto make_velocity_interpolation_kernel() const;
624 /** @brief Return a B-spline interpolation kernel for density readout. */
625 auto make_density_interpolation_kernel() const;
626
627public:
628 std::function<bool(Utils::Vector3d const &)>
630 bool add_force_at_pos(Utils::Vector3d const &pos,
631 Utils::Vector3d const &force) override;
632 void add_forces_at_pos(std::vector<Utils::Vector3d> const &pos,
633 std::vector<Utils::Vector3d> const &forces) override;
634 std::optional<Utils::Vector3d>
636 bool consider_points_in_halo = false) const override;
637 std::vector<Utils::Vector3d>
638 get_velocities_at_pos(std::vector<Utils::Vector3d> const &pos) override;
639 std::optional<double>
641 bool consider_points_in_halo = false) const override;
642 std::vector<double>
643 get_densities_at_pos(std::vector<Utils::Vector3d> const &pos) override;
644
645public:
646 // ---- Boundary Handling ----
647
648 void reset_boundary_handling(std::shared_ptr<BlockStorage> const &blocks) {
649 auto const [lc, uc] = m_lattice->get_local_grid_range(true);
650 m_boundary =
651 std::make_shared<BoundaryModel>(blocks, m_pdf_field_id, m_flag_field_id,
652 CellInterval{to_cell(lc), to_cell(uc)});
653 }
654
655 void on_boundary_add();
656 void clear_boundaries() override;
657 void reallocate_ubb_field() override;
658 void
659 update_boundary_from_shape(std::vector<int> const &raster_flat,
660 std::vector<double> const &data_flat) override;
661 std::optional<Utils::Vector3d>
663 bool consider_ghosts = false) const override;
665 Utils::Vector3d const &velocity) override;
666 std::vector<std::optional<Utils::Vector3d>> get_slice_velocity_at_boundary(
668 Utils::Vector3i const &upper_corner) const override;
671 std::vector<std::optional<Utils::Vector3d>> const &velocity) override;
672 std::optional<Utils::Vector3d>
673 get_node_boundary_force(Utils::Vector3i const &node) const override;
674 bool remove_node_from_boundary(Utils::Vector3i const &node) override;
675 std::optional<bool>
677 bool consider_ghosts = false) const override;
678 std::vector<bool>
680 Utils::Vector3i const &upper_corner) const override;
682 std::vector<int> const &raster_flat) const override;
683 [[nodiscard]] Utils::Vector3d get_boundary_force() const override;
684
685private:
686 [[nodiscard]] Utils::Vector3i flat_index_to_node(int index) const;
687 [[nodiscard]] Utils::Vector3i get_neighbor_node(Utils::Vector3i const &node,
688 int dir) const;
689
690public:
691 // ---- Global Reductions & Physical Parameters ----
692
693 // Global pressure tensor
695 Matrix3<FloatType> tensor(FloatType{0});
696 for (auto const &block : *get_lattice().get_blocks()) {
699 }
700 auto const &grid_size = get_lattice().get_grid_dimensions();
703 return to_vector9d(tensor) * (1. / static_cast<double>(number_of_nodes));
704 }
705
706 // Global momentum
718
719 // Global external force
720 void set_external_force(Utils::Vector3d const &ext_force) override {
721 m_reset_force->set_ext_force(zero_centered_to_lb(ext_force));
722 }
723
725 return zero_centered_to_md(m_reset_force->get_ext_force());
726 }
727
728 void set_viscosity(double viscosity) override {
729 m_viscosity = FloatType_c(viscosity);
730 }
731
732 [[nodiscard]] double get_viscosity() const noexcept override {
733 return static_cast<double>(m_viscosity);
734 }
735
736 [[nodiscard]] double get_density() const noexcept override {
737 return static_cast<double>(m_density);
738 }
739
740 [[nodiscard]] double get_kT() const noexcept override {
741 return static_cast<double>(m_kT);
742 }
743
744 [[nodiscard]] unsigned int get_seed() const noexcept override {
745 return m_seed;
746 }
747
748 [[nodiscard]] std::optional<uint64_t> get_rng_state() const override {
749 auto const cm =
750 std::get_if<typename Kernels::StreamCollisionModelThermalized>(
752 if (!cm or m_kT == 0.) {
753 return std::nullopt;
754 }
755 return {static_cast<uint64_t>(cm->getTime_step())};
756 }
757
758 void set_rng_state(uint64_t counter) override {
759 auto const cm =
760 std::get_if<typename Kernels::StreamCollisionModelThermalized>(
762 if (!cm or m_kT == 0.) {
763 throw std::runtime_error("This LB instance is unthermalized");
764 }
765 assert(counter <=
766 static_cast<uint32_t>(std::numeric_limits<uint_t>::max()));
767 cm->setTime_step(static_cast<uint32_t>(counter));
768 }
769
771 return *m_lattice;
772 }
773
774 [[nodiscard]] std::size_t get_velocity_field_id() const noexcept override {
775 return m_velocity_field_id;
776 }
777
778 [[nodiscard]] std::size_t get_force_field_id() const noexcept override {
780 }
781
782 /**
783 * @brief Correction factor for off-diagonal pressure tensor elements.
784 * Compensates for the viscosity-dependent error in the non-equilibrium
785 * stress: factor = nu / (nu + 1/6).
786 */
788 return m_viscosity / (m_viscosity + FloatType{1} / FloatType{6});
789 }
790
793 for (auto const i : {1u, 2u, 3u, 5u, 6u, 7u}) {
794 tensor[i] *= revert_factor;
795 }
796 }
797
798 void pressure_tensor_correction(std::span<FloatType, 9ul> tensor) const {
800 for (auto const i : {1u, 2u, 3u, 5u, 6u, 7u}) {
801 tensor[i] *= revert_factor;
802 }
803 }
804
805protected:
806 /**
807 * @brief Scale data by a conversion factor (in-place).
808 * Used for zero-centered density representation: LB internally stores
809 * density fluctuations around zero, while the user interface uses
810 * absolute densities. The conversion factors @ref m_zc_to_md and
811 * @ref m_zc_to_lb translate between these representations.
812 */
813 template <typename T>
814 void zero_centered_transform_impl(T &data, auto const factor) const {
815 if constexpr (std::is_arithmetic_v<T>) {
816 static_assert(std::is_floating_point_v<T>);
817 data *= static_cast<T>(factor);
818 } else {
819 auto const coef = static_cast<typename T::value_type>(factor);
820 std::transform(std::begin(data), std::end(data), std::begin(data),
821 [coef](auto value) { return value * coef; });
822 }
823 }
824
825 void zero_centered_to_lb_in_place(auto &data) const {
827 }
828
829 void zero_centered_to_md_in_place(auto &data) const {
831 }
832
833 auto zero_centered_to_lb(auto const &data) const {
834 auto transformed_data = data;
836 return transformed_data;
837 }
838
839 auto zero_centered_to_md(auto const &data) const {
840 auto transformed_data = data;
842 return transformed_data;
843 }
844
845public:
846 // ---- File I/O ----
847
848 void register_vtk_field_filters(walberla::vtk::VTKOutput &vtk_obj) override {
849 field::FlagFieldCellFilter<FlagField> fluid_filter(m_flag_field_id);
851 vtk_obj.addCellExclusionFilter(fluid_filter);
852 }
853
854 void register_vtk_field_writers(walberla::vtk::VTKOutput &vtk_obj,
856 int flag_observables) override;
857
858protected:
859 // ---- Private Infrastructure Helpers ----
860
861 /**
862 * @brief Convenience function to add a field with a custom allocator.
863 *
864 * When vectorization is off, let waLBerla decide which memory allocator
865 * to use. When vectorization is on, the aligned memory allocator is
866 * required, otherwise <tt>cpu_vectorize_info["assume_aligned"]</tt> will
867 * trigger assertions. That is because for single-precision kernels the
868 * waLBerla heuristic in <tt>src/field/allocation/FieldAllocator.h</tt>
869 * will fall back to @c StdFieldAlloc, yet @c AllocateAligned is needed
870 * for intrinsics to work.
871 */
872 template <typename Field> auto add_to_storage(std::string const tag) {
873 auto const &blocks = m_lattice->get_blocks();
874 auto const n_ghost_layers = m_lattice->get_ghost_layers();
875 if constexpr (Architecture == lbmpy::Arch::CPU) {
876#ifdef ESPRESSO_BUILD_WITH_AVX_KERNELS
877 constexpr auto alignment = field::SIMDAlignment();
878 using value_type = Field::value_type;
879 using Allocator = field::AllocateAligned<value_type, alignment>;
880 auto const allocator = std::make_shared<Allocator>();
881 auto const empty_set = Set<SUID>::emptySet();
882 return field::addToStorage<Field>(
883 blocks, tag, field::internal::defaultSize, FloatType{0}, field::fzyx,
884 n_ghost_layers, false, {}, empty_set, empty_set, allocator);
885#else // ESPRESSO_BUILD_WITH_AVX_KERNELS
886 return field::addToStorage<Field>(blocks, tag, FloatType{0}, field::fzyx,
888#endif // ESPRESSO_BUILD_WITH_AVX_KERNELS
889 }
890#if defined(__CUDACC__) and defined(WALBERLA_BUILD_WITH_CUDA)
891 else {
892 auto field_id = gpu::addGPUFieldToStorage<GPUField>(
893 blocks, tag, Field::F_SIZE, field::fzyx, n_ghost_layers);
894 if constexpr (std::is_same_v<Field, _VectorField>) {
895 for (auto &block : *blocks) {
896 auto field = block.template getData<GPUField>(field_id);
898 }
899 } else if constexpr (std::is_same_v<Field, _PdfField>) {
900 for (auto &block : *blocks) {
901 auto field = block.template getData<GPUField>(field_id);
903 field, std::array<FloatType, Stencil::Size>{});
904 }
905 }
906 return field_id;
907 }
908#endif
909 }
910
911 /**
912 * @brief Set up D3Q27 communicators for full ghost layer updates.
913 * Creates per-field communicators (PDF, velocity, last-applied force)
914 * as well as a combined communicator and the boundary communicator.
915 */
917 auto const &blocks = m_lattice->get_blocks();
918
919 m_full_communicator = std::make_shared<RegularFullCommunicator>(blocks);
920 m_full_communicator->addPackInfo(
921 std::make_shared<PackInfo<PdfField>>(m_pdf_field_id));
922 m_full_communicator->addPackInfo(
924 m_full_communicator->addPackInfo(
925 std::make_shared<PackInfo<VectorField>>(m_velocity_field_id));
926
927 m_pdf_communicator = std::make_shared<RegularFullCommunicator>(blocks);
928 m_vel_communicator = std::make_shared<RegularFullCommunicator>(blocks);
929 m_laf_communicator = std::make_shared<RegularFullCommunicator>(blocks);
930 m_pdf_communicator->addPackInfo(
931 std::make_shared<PackInfo<PdfField>>(m_pdf_field_id));
932 m_vel_communicator->addPackInfo(
933 std::make_shared<PackInfo<VectorField>>(m_velocity_field_id));
934 m_laf_communicator->addPackInfo(
936
938 std::make_shared<BoundaryFullCommunicator>(blocks);
939 m_boundary_communicator->addPackInfo(
942 auto boundary_packinfo = std::make_shared<
947 }
948
949 /**
950 * @brief Set up the communicator used during integration.
951 * Uses optimized streaming pack info when neither boundaries nor
952 * Lees-Edwards boundary conditions are active; falls back to the
953 * generic pack info otherwise.
954 */
956 auto const setup = [this]<typename PackInfoPdf, typename PackInfoVec>() {
957 auto const &blocks = m_lattice->get_blocks();
959 std::make_shared<PDFStreamingCommunicator>(blocks);
960 m_pdf_streaming_communicator->addPackInfo(
961 std::make_shared<PackInfoPdf>(m_pdf_field_id));
962 m_pdf_streaming_communicator->addPackInfo(
963 std::make_shared<PackInfoVec>(m_last_applied_force_field_id));
964 };
966 using PackInfoPdf = FieldTrait::PackInfoStreamingPdf;
967 using PackInfoVec = FieldTrait::PackInfoStreamingVec;
968 if (m_has_boundaries or (m_collision_model and has_lees_edwards_bc())) {
969 setup.template operator()<PackInfo<PdfField>, PackInfoVec>();
970 } else {
971 setup.template operator()<PackInfoPdf, PackInfoVec>();
972 }
973 }
974};
975
976} // namespace walberla
977
978// Out-of-class template method definitions
982#include "LBNodeAccess.impl.hpp"
983#include "LBSliceAccess.impl.hpp"
984#include "LBVTK.impl.hpp"
Out-of-class boundary access definitions for walberla::LBWalberlaImpl.
Out-of-class collision model setup definitions for walberla::LBWalberlaImpl.
Out-of-class position-based interpolation definitions for walberla::LBWalberlaImpl.
Out-of-class node access definitions for walberla::LBWalberlaImpl.
Out-of-class slice access definitions for walberla::LBWalberlaImpl.
Out-of-class VTK writer registration definition for walberla::LBWalberlaImpl.
LBWalberlaBase provides the public interface of the LB waLBerla bridge.
Vector implementation and trait types for boost qvm interoperability.
Interface of a lattice-based fluid 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.
walberla::blockforest::StructuredBlockForest Lattice_T
Boundary class optimized for sparse data.
field::FlagField< uint8_t > FlagField
Class that runs and controls the LB on waLBerla.
void add_forces_at_pos(std::vector< Utils::Vector3d > const &pos, std::vector< Utils::Vector3d > const &forces) override
Distribute forces to the lattice at given positions.
std::shared_ptr< typename Kernels::UpdateVelFromPDF > m_update_velocities_from_pdf
std::vector< double > get_slice_last_applied_force(Utils::Vector3i const &lower_corner, Utils::Vector3i const &upper_corner) const override
std::variant< typename Kernels::StreamCollisionModelThermalized, typename Kernels::StreamCollisionModelLeesEdwards > CollisionModel
void zero_centered_transform_impl(T &data, auto const factor) const
Scale data by a conversion factor (in-place).
void reset_boundary_handling(std::shared_ptr< BlockStorage > const &blocks)
std::shared_ptr< RegularFullCommunicator > m_pdf_communicator
std::vector< std::optional< Utils::Vector3d > > get_slice_velocity_at_boundary(Utils::Vector3i const &lower_corner, Utils::Vector3i const &upper_corner) const override
std::optional< Utils::Vector3d > get_node_last_applied_force(Utils::Vector3i const &node, bool consider_ghosts=false) const override
stencil::D3Q19 Stencil
Stencil for collision and streaming operations.
std::optional< Utils::Vector3d > get_node_velocity_at_boundary(Utils::Vector3i const &node, bool consider_ghosts=false) const override
void ghost_communication() override
Perform all pending ghost layer updates.
void pressure_tensor_correction(Matrix3< FloatType > &tensor) const
std::optional< Utils::Vector3d > get_node_velocity(Utils::Vector3i const &node, bool consider_ghosts=false) const override
FieldTrait< FloatType, Stencil, Architecture >::template RegularCommScheme< Stencil > PDFStreamingCommunicator
Regular communicator.
Utils::Vector3d get_boundary_force_from_shape(std::vector< int > const &raster_flat) const override
Total force exerted by the fluid on a subset of boundary nodes.
std::shared_ptr< RegularFullCommunicator > m_full_communicator
std::size_t get_force_field_id() const noexcept override
std::shared_ptr< CollisionModel > m_collision_model
void set_slice_velocity(Utils::Vector3i const &lower_corner, Utils::Vector3i const &upper_corner, std::vector< double > const &velocity) override
unsigned int get_seed() const noexcept override
void integrate_vtk_writers() override
void pressure_tensor_correction(std::span< FloatType, 9ul > tensor) const
BoundaryModel::FlagField FlagField
bool remove_node_from_boundary(Utils::Vector3i const &node) override
std::optional< Utils::Vector3d > get_node_boundary_force(Utils::Vector3i const &node) const override
std::optional< double > get_density_at_pos(Utils::Vector3d const &pos, bool consider_points_in_halo=false) const override
void zero_centered_to_md_in_place(auto &data) const
FieldTrait< FloatType, Stencil >::VectorField _VectorField
void set_slice_velocity_at_boundary(Utils::Vector3i const &lower_corner, Utils::Vector3i const &upper_corner, std::vector< std::optional< Utils::Vector3d > > const &velocity) override
std::vector< double > get_slice_population(Utils::Vector3i const &lower_corner, Utils::Vector3i const &upper_corner) const override
void set_rng_state(uint64_t counter) override
std::vector< double > get_slice_velocity(Utils::Vector3i const &lower_corner, Utils::Vector3i const &upper_corner) const override
Utils::Vector3d get_boundary_force() const override
std::shared_ptr< LatticeWalberla > m_lattice
std::shared_ptr< LeesEdwardsPack > m_lees_edwards_callbacks
LBWalberlaImpl(std::shared_ptr< LatticeWalberla > lattice, double viscosity, double density)
bool set_node_last_applied_force(Utils::Vector3i const &node, Utils::Vector3d const &force) override
std::function< bool(Utils::Vector3d const &)> make_lattice_position_checker(bool consider_points_in_halo) const override
FieldTrait< FloatType, Stencil >::PdfField _PdfField
std::shared_ptr< InterpolateAndShiftAtBoundary< _VectorField, FloatType > > m_lees_edwards_vel_interpol_sweep
std::vector< bool > get_slice_is_boundary(Utils::Vector3i const &lower_corner, Utils::Vector3i const &upper_corner) const override
std::vector< double > get_slice_density(Utils::Vector3i const &lower_corner, Utils::Vector3i const &upper_corner) const override
bool is_double_precision() const noexcept override
void update_boundary_from_shape(std::vector< int > const &raster_flat, std::vector< double > const &data_flat) override
Set boundary conditions from a rasterized shape.
std::shared_ptr< BoundaryModel > m_boundary
FieldTrait< FloatType, Stencil, Architecture >::PdfField PdfField
std::vector< double > get_slice_pressure_tensor(Utils::Vector3i const &lower_corner, Utils::Vector3i const &upper_corner) const override
FieldTrait< FloatType, Stencil, Architecture >::template PackInfo< Field > PackInfo
bool set_node_density(Utils::Vector3i const &node, double density) override
void set_collision_model(double kT, unsigned int seed) override
Set up the thermalized collision model.
std::vector< Utils::Vector3d > get_velocities_at_pos(std::vector< Utils::Vector3d > const &pos) override
Interpolate velocities at given positions (batch version).
void ghost_communication_laf() override
std::optional< bool > get_node_is_boundary(Utils::Vector3i const &node, bool consider_ghosts=false) const override
std::shared_ptr< ResetForce< PdfField, VectorField > > m_reset_force
void setup_streaming_communicator()
Set up the communicator used during integration.
FlagUID const Boundary_flag
Flag for boundary cells.
~LBWalberlaImpl() override=default
std::optional< uint64_t > get_rng_state() const override
std::optional< std::vector< double > > get_node_population(Utils::Vector3i const &node, bool consider_ghosts=false) const override
FloatType m_density
kinematic viscosity
double get_viscosity() const noexcept override
double get_kT() const noexcept override
Utils::Vector3d get_momentum() const override
void set_viscosity(double viscosity) override
void on_boundary_add()
Lazily enable boundary mode on first boundary addition.
std::shared_ptr< BoundaryFullCommunicator > m_boundary_communicator
std::size_t stencil_size() const noexcept override
auto add_to_storage(std::string const tag)
Convenience function to add a field with a custom allocator.
void ghost_communication_full()
Communicate all fields at once using the D3Q27 stencil.
auto zero_centered_to_lb(auto const &data) const
FieldTrait< FloatType, Stencil, Architecture >::template BoundaryCommScheme< stencil::D3Q27 > BoundaryFullCommunicator
bool set_node_velocity_at_boundary(Utils::Vector3i const &node, Utils::Vector3d const &velocity) override
stencil::D3Q27 StencilFull
Stencil for ghost communication (includes domain corners).
void register_vtk_field_writers(walberla::vtk::VTKOutput &vtk_obj, LatticeModel::units_map const &units, int flag_observables) override
std::optional< double > get_node_density(Utils::Vector3i const &node, bool consider_ghosts=false) const override
void check_lebc(unsigned int shear_direction, unsigned int shear_plane_normal) const override
Verify that MD and LB Lees-Edwards parameters are consistent.
void set_slice_density(Utils::Vector3i const &lower_corner, Utils::Vector3i const &upper_corner, std::vector< double > const &density) override
void ghost_communication_vel() override
Utils::Vector3d get_external_force() const noexcept override
FloatType FloatType_c(T t) const
std::shared_ptr< RegularFullCommunicator > m_laf_communicator
std::size_t get_velocity_field_id() const noexcept override
Utils::VectorXd< 9 > get_pressure_tensor() const override
ResourceObserver m_mpi_cart_comm_observer
FieldTrait< FloatType, Stencil, Architecture >::template RegularCommScheme< stencil::D3Q27 > RegularFullCommunicator
Full communicator.
std::shared_ptr< InterpolateAndShiftAtBoundary< _PdfField, FloatType > > m_lees_edwards_pdf_interpol_sweep
void set_external_force(Utils::Vector3d const &ext_force) override
void setup_full_communicator()
Set up D3Q27 communicators for full ghost layer updates.
std::optional< Utils::Vector3d > get_velocity_at_pos(Utils::Vector3d const &pos, bool consider_points_in_halo=false) const override
FieldTrait< FloatType, Stencil, Architecture >::VectorField VectorField
void set_slice_last_applied_force(Utils::Vector3i const &lower_corner, Utils::Vector3i const &upper_corner, std::vector< double > const &force) override
LatticeWalberla::Lattice_T BlockStorage
Lattice model (e.g.
std::shared_ptr< RegularFullCommunicator > m_vel_communicator
std::bitset< GhostComm::SIZE > m_pending_ghost_comm
LatticeWalberla const & get_lattice() const noexcept override
auto zero_centered_to_md(auto const &data) const
void set_slice_population(Utils::Vector3i const &lower_corner, Utils::Vector3i const &upper_corner, std::vector< double > const &population) override
double get_density() const noexcept override
void ghost_communication_pdf() override
void register_vtk_field_filters(walberla::vtk::VTKOutput &vtk_obj) override
std::optional< Utils::Vector3d > get_node_force_to_be_applied(Utils::Vector3i const &node) const override
std::vector< double > get_densities_at_pos(std::vector< Utils::Vector3d > const &pos) override
void zero_centered_to_lb_in_place(auto &data) const
bool set_node_velocity(Utils::Vector3i const &node, Utils::Vector3d const &v) override
bool set_node_population(Utils::Vector3i const &node, std::vector< double > const &population) override
bool is_gpu() const noexcept override
FloatType pressure_tensor_correction_factor() const
Correction factor for off-diagonal pressure tensor elements.
std::shared_ptr< PDFStreamingCommunicator > m_pdf_streaming_communicator
detail::KernelTrait< FloatType, Architecture > Kernels
BlockDataID m_last_applied_force_field_id
std::optional< Utils::VectorXd< 9 > > get_node_pressure_tensor(Utils::Vector3i const &node) const override
std::shared_ptr< InterpolateAndShiftAtBoundary< _VectorField, FloatType > > m_lees_edwards_last_applied_force_interpol_sweep
bool add_force_at_pos(Utils::Vector3d const &pos, Utils::Vector3d const &force) 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)
Definition elc.cpp:177
T product(Vector< T, N > const &v)
Definition Vector.hpp:372
STL namespace.
auto reduce(GhostLayerField< double, uint_t{19u}> const *pdf_field, GhostLayerField< double, uint_t{3u}> const *force_field, double const density)
void initialize(GhostLayerField< double, uint_t{19u}> *pdf_field, std::array< double, 19u > const &pop)
auto reduce(GhostLayerField< double, uint_t{19u}> const *pdf_field, double const density)
void initialize(GhostLayerField< double, uint_t{3u}> *vec_field, Vector3< double > const &vec)
\file PackInfoPdfDoublePrecision.cpp \author pystencils
auto to_vector3d(Vector3< T > const &v) noexcept
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.
auto to_vector9d(Matrix3< T > const &m) noexcept
static Utils::Vector3d velocity(Particle const &p_ref, Particle const &p_vs)
Velocity of the virtual site.
Definition relative.cpp:64
Observer to monitor the lifetime of a shared resource.
detail::KernelTrait< FT, AT >::PackInfoVec PackInfoStreamingVec
Definition lb_fields.hpp:37
field::GhostLayerField< FT, PdfStencil::Size > PdfField
Definition lb_fields.hpp:33
field::GhostLayerField< FT, uint_t{3u}> VectorField
Definition lb_fields.hpp:34
detail::KernelTrait< FT, AT >::PackInfoPdf PackInfoStreamingPdf
Definition lb_fields.hpp:36
GhostCommFlags
Ghost communication operations.
@ LAF
last applied forces communication