ESPResSo
Extensible Simulation Package for Research on Soft Matter Systems
Loading...
Searching...
No Matches
EKinWalberlaImpl.hpp
Go to the documentation of this file.
1/*
2 * Copyright (C) 2022-2023 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#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
32#include "../BoundaryHandling.hpp"
33#include "../utils/boundary.hpp"
34#include "../utils/types_conversion.hpp"
35#include "ek_kernels.hpp"
36
40
41#include <utils/Vector.hpp>
42
43#include <cstddef>
44#include <iterator>
45#include <memory>
46#include <optional>
47#include <stdexcept>
48#include <string>
49#include <type_traits>
50#include <variant>
51#include <vector>
52
53namespace walberla {
54
55/** @brief Class that runs and controls the EK on waLBerla. */
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;
73
74 using DiffusiveFluxKernel = std::variant<DiffusiveFluxKernelUnthermalized,
75 DiffusiveFluxKernelThermalized>;
76 using DiffusiveFluxKernelElectrostatic =
77 std::variant<DiffusiveFluxKernelElectrostaticUnthermalized,
78 DiffusiveFluxKernelElectrostaticThermalized>;
79
80 using Dirichlet = typename detail::KernelTrait<FloatType>::Dirichlet;
81 using FixedFlux = typename detail::KernelTrait<FloatType>::FixedFlux;
82
83protected:
84 // Type definitions
85 using FluxField = GhostLayerField<FloatType, FluxCount>;
86 using FlagField = walberla::FlagField<walberla::uint8_t>;
87 using DensityField = GhostLayerField<FloatType, 1>;
88
91
92public:
93 template <typename T> FloatType FloatType_c(T t) {
94 return numeric_cast<FloatType>(t);
95 }
96
97 [[nodiscard]] std::size_t stencil_size() const noexcept override {
98 return FluxCount;
99 }
100
101 [[nodiscard]] bool is_double_precision() const noexcept override {
102 return std::is_same<FloatType, double>::value;
103 }
104
105private:
106 FloatType m_diffusion;
107 FloatType m_kT;
108 FloatType m_valency;
109 Utils::Vector3d m_ext_efield;
110 bool m_advection;
111 bool m_friction_coupling;
112 unsigned int m_seed;
113
114protected:
115 // Block data access handles
118
119 BlockDataID m_flux_field_id;
121
124
125 /** Flag for domain cells, i.e. all cells. */
126 FlagUID const Domain_flag{"domain"};
127 /** Flag for boundary cells. */
128 FlagUID const Boundary_flag{"boundary"};
129
130 /** Block forest */
131 std::shared_ptr<LatticeWalberla> m_lattice;
132
133 std::unique_ptr<BoundaryModelDensity> m_boundary_density;
134 std::unique_ptr<BoundaryModelFlux> m_boundary_flux;
135
136 std::unique_ptr<DiffusiveFluxKernel> m_diffusive_flux;
137 std::unique_ptr<DiffusiveFluxKernelElectrostatic>
139 std::unique_ptr<ContinuityKernel> m_continuity;
140
141 // ResetFlux + external force
142 // TODO: kernel for that
143 // std::shared_ptr<ResetForce<PdfField, VectorField>> m_reset_force;
144
145 [[nodiscard]] std::optional<CellInterval>
146 get_interval(Utils::Vector3i const &lower_corner,
147 Utils::Vector3i const &upper_corner) const {
148 auto const &lattice = get_lattice();
149 auto const &cell_min = lower_corner;
150 auto const cell_max = upper_corner - Utils::Vector3i::broadcast(1);
151 auto const lower_bc = get_block_and_cell(lattice, cell_min, true);
152 auto const upper_bc = get_block_and_cell(lattice, cell_max, true);
153 if (not lower_bc or not upper_bc) {
154 return std::nullopt;
155 }
156 assert(&(*(lower_bc->block)) == &(*(upper_bc->block)));
157 return {CellInterval(lower_bc->cell, upper_bc->cell)};
158 }
159
161 auto const &blocks = get_lattice().get_blocks();
162 m_boundary_density = std::make_unique<BoundaryModelDensity>(
164 }
165
167 auto const &blocks = get_lattice().get_blocks();
168 m_boundary_flux = std::make_unique<BoundaryModelFlux>(
170 }
171
172 using FullCommunicator = blockforest::communication::UniformBufferedScheme<
173 typename stencil::D3Q27>;
174 std::shared_ptr<FullCommunicator> m_full_communication;
175
176public:
177 EKinWalberlaImpl(std::shared_ptr<LatticeWalberla> lattice, double diffusion,
178 double kT, double valency, Utils::Vector3d const &ext_efield,
179 double density, bool advection, bool friction_coupling,
180 bool thermalized, unsigned int seed)
181 : m_diffusion(FloatType_c(diffusion)), m_kT(FloatType_c(kT)),
182 m_valency(FloatType_c(valency)), m_ext_efield(ext_efield),
183 m_advection(advection), m_friction_coupling(friction_coupling),
184 m_seed(seed), m_lattice(std::move(lattice)) {
185 m_density_field_id = field::addToStorage<DensityField>(
186 m_lattice->get_blocks(), "density field", FloatType_c(density),
187 field::fzyx, m_lattice->get_ghost_layers());
189 field::addFlattenedShallowCopyToStorage<DensityField>(
190 m_lattice->get_blocks(), m_density_field_id,
191 "flattened density field");
192 m_flux_field_id = field::addToStorage<FluxField>(
193 m_lattice->get_blocks(), "flux field", FloatType{0}, field::fzyx,
194 m_lattice->get_ghost_layers());
196 field::addFlattenedShallowCopyToStorage<FluxField>(
197 m_lattice->get_blocks(), m_flux_field_id, "flattened flux field");
198
199 m_continuity = std::make_unique<ContinuityKernel>(
201
202 if (thermalized) {
203 set_diffusion_kernels(seed);
204 } else {
205 set_diffusion_kernels();
206 }
207
208 // Init boundary related stuff
209 m_flag_field_density_id = field::addFlagFieldToStorage<FlagField>(
210 m_lattice->get_blocks(), "flag field density",
211 m_lattice->get_ghost_layers());
213
214 m_flag_field_flux_id = field::addFlagFieldToStorage<FlagField>(
215 m_lattice->get_blocks(), "flag field flux",
216 m_lattice->get_ghost_layers());
218
220 std::make_shared<FullCommunicator>(m_lattice->get_blocks());
221 m_full_communication->addPackInfo(
222 std::make_shared<field::communication::PackInfo<DensityField>>(
224
225 // Synchronize ghost layers
227 }
228
229 // Global parameters
230 [[nodiscard]] double get_diffusion() const noexcept override {
231 return m_diffusion;
232 }
233 [[nodiscard]] double get_kT() const noexcept override { return m_kT; }
234 [[nodiscard]] double get_valency() const noexcept override {
235 return m_valency;
236 }
237 [[nodiscard]] bool get_advection() const noexcept override {
238 return m_advection;
239 }
240 [[nodiscard]] bool get_friction_coupling() const noexcept override {
241 return m_friction_coupling;
242 }
243 [[nodiscard]] Utils::Vector3d get_ext_efield() const noexcept override {
244 return m_ext_efield;
245 }
246 [[nodiscard]] bool is_thermalized() const noexcept override {
247 return static_cast<bool>(
248 std::get_if<DiffusiveFluxKernelThermalized>(&*m_diffusive_flux));
249 }
250 [[nodiscard]] unsigned int get_seed() const noexcept override {
251 return m_seed;
252 }
253 [[nodiscard]] std::optional<uint64_t> get_rng_state() const override {
254 auto const kernel =
255 std::get_if<DiffusiveFluxKernelThermalized>(&*m_diffusive_flux);
256 if (!kernel) {
257 return std::nullopt;
258 }
259 return {static_cast<uint64_t>(kernel->time_step_)};
260 }
261
262 void set_diffusion(double diffusion) override {
263 m_diffusion = FloatType_c(diffusion);
264 auto visitor = [m_diffusion = m_diffusion](auto &kernel) {
265 kernel.D_ = m_diffusion;
266 };
267 std::visit(visitor, *m_diffusive_flux);
268 std::visit(visitor, *m_diffusive_flux_electrostatic);
269 }
270
271 void set_kT(double kT) override {
272 m_kT = FloatType_c(kT);
273 std::visit([m_kT = m_kT](auto &kernel) { kernel.kT_ = m_kT; },
275 }
276
277 void set_valency(double valency) override {
278 m_valency = FloatType_c(valency);
279 std::visit([m_valency = m_valency](auto &kernel) { kernel.z_ = m_valency; },
281 }
282
283 void set_advection(bool advection) override { m_advection = advection; }
284
285 void set_friction_coupling(bool friction_coupling) override {
286 m_friction_coupling = friction_coupling;
287 }
288
289 void set_rng_state(uint64_t counter) override {
290 auto const kernel =
291 std::get_if<DiffusiveFluxKernelThermalized>(&*m_diffusive_flux);
292 auto const kernel_electrostatic =
293 std::get_if<DiffusiveFluxKernelElectrostaticThermalized>(
295
296 if (!kernel or !kernel_electrostatic) {
297 throw std::runtime_error("This EK instance is unthermalized");
298 }
299 assert(counter <=
300 static_cast<uint32_t>(std::numeric_limits<uint_t>::max()));
301 kernel->time_step_ = static_cast<uint32_t>(counter);
302 kernel_electrostatic->time_step_ = static_cast<uint32_t>(counter);
303 }
304
305 void set_ext_efield(Utils::Vector3d const &field) override {
306 m_ext_efield = field;
307
308 std::visit(
309 [this](auto &kernel) {
310 kernel.f_ext_0_ = FloatType_c(m_ext_efield[0]);
311 kernel.f_ext_1_ = FloatType_c(m_ext_efield[1]);
312 kernel.f_ext_2_ = FloatType_c(m_ext_efield[2]);
313 },
315 }
316
317 void ghost_communication() override { (*m_full_communication)(); }
318
319private:
320 void set_diffusion_kernels() {
321 auto kernel = DiffusiveFluxKernelUnthermalized(m_flux_field_flattened_id,
323 FloatType_c(m_diffusion));
324 m_diffusive_flux = std::make_unique<DiffusiveFluxKernel>(std::move(kernel));
325
326 auto kernel_electrostatic = DiffusiveFluxKernelElectrostaticUnthermalized(
328 FloatType_c(m_diffusion), FloatType_c(m_ext_efield[0]),
329 FloatType_c(m_ext_efield[1]), FloatType_c(m_ext_efield[2]),
330 FloatType_c(m_kT), FloatType_c(m_valency));
331
333 std::make_unique<DiffusiveFluxKernelElectrostatic>(
334 std::move(kernel_electrostatic));
335 }
336
337 void set_diffusion_kernels(unsigned int seed) {
338 auto const grid_dim = get_lattice().get_grid_dimensions();
339
340 auto kernel = DiffusiveFluxKernelThermalized(
342 FloatType_c(m_diffusion), grid_dim[0], grid_dim[1], grid_dim[2], seed,
343 0);
344
345 auto kernel_electrostatic = DiffusiveFluxKernelElectrostaticThermalized(
347 FloatType_c(m_diffusion), FloatType_c(m_ext_efield[0]),
348 FloatType_c(m_ext_efield[1]), FloatType_c(m_ext_efield[2]), grid_dim[0],
349 grid_dim[1], grid_dim[2], FloatType_c(m_kT), seed, 0,
350 FloatType_c(m_valency));
351
352 auto const blocks = get_lattice().get_blocks();
353
354 for (auto b = blocks->begin(); b != blocks->end(); ++b) {
355 kernel.configure(blocks, &*b);
356 kernel_electrostatic.configure(blocks, &*b);
357 }
358
359 m_diffusive_flux = std::make_unique<DiffusiveFluxKernel>(std::move(kernel));
361 std::make_unique<DiffusiveFluxKernelElectrostatic>(
362 std::move(kernel_electrostatic));
363 }
364
365 void kernel_boundary_density() {
366 for (auto &block : *m_lattice->get_blocks()) {
367 (*m_boundary_density)(&block);
368 }
369 }
370
371 void kernel_boundary_flux() {
372 for (auto &block : *m_lattice->get_blocks()) {
373 (*m_boundary_flux)(&block);
374 }
375 }
376
377 void kernel_continuity() {
378 for (auto &block : *m_lattice->get_blocks()) {
379 (*m_continuity).run(&block);
380 }
381 }
382
383 void kernel_diffusion() {
384 for (auto &block : *m_lattice->get_blocks()) {
385 std::visit([&block](auto &kernel) { kernel.run(&block); },
387 }
388
389 if (auto *kernel =
390 std::get_if<DiffusiveFluxKernelThermalized>(&*m_diffusive_flux)) {
391 kernel->time_step_++;
392
393 auto *kernel_electrostatic =
394 std::get_if<DiffusiveFluxKernelElectrostaticThermalized>(
396 kernel_electrostatic->time_step_++;
397 }
398 }
399
400 void kernel_advection(const std::size_t &velocity_id) {
401 auto kernel =
403 BlockDataID(velocity_id));
404 for (auto &block : *m_lattice->get_blocks()) {
405 kernel.run(&block);
406 }
407 }
408
409 void kernel_friction_coupling(const std::size_t &force_id) {
410 auto kernel = FrictionCouplingKernel(
411 BlockDataID(force_id), m_flux_field_flattened_id,
413 for (auto &block : *m_lattice->get_blocks()) {
414 kernel.run(&block);
415 }
416 }
417
418 void kernel_diffusion_electrostatic(const std::size_t &potential_id) {
419 auto const phiID = BlockDataID(potential_id);
420 std::visit([phiID](auto &kernel) { kernel.phiID = phiID; },
422
423 for (auto &block : *m_lattice->get_blocks()) {
424 std::visit([&block](auto &kernel) { kernel.run(&block); },
426 }
427
428 if (auto *kernel_electrostatic =
429 std::get_if<DiffusiveFluxKernelElectrostaticThermalized>(
431 kernel_electrostatic->time_step_++;
432
433 auto *kernel =
434 std::get_if<DiffusiveFluxKernelThermalized>(&*m_diffusive_flux);
435 kernel->time_step_++;
436 }
437 }
438
439 void kernel_migration() {}
440
441 void updated_boundary_fields() {
442 m_boundary_flux->boundary_update();
443 m_boundary_density->boundary_update();
444 }
445
446protected:
447 void integrate_vtk_writers() override {
448 for (auto const &it : m_vtk_auto) {
449 auto &vtk_handle = it.second;
450 if (vtk_handle->enabled) {
451 vtk::writeFiles(vtk_handle->ptr)();
452 vtk_handle->execution_count++;
453 }
454 }
455 }
456
457public:
458 void integrate(std::size_t potential_id, std::size_t velocity_id,
459 std::size_t force_id) override {
460
461 updated_boundary_fields();
462
463 if (get_diffusion() == 0.)
464 return;
465
466 if (get_valency() != 0.) {
467 if (potential_id == walberla::BlockDataID{}) {
468 throw std::runtime_error("Walberla EK: electrostatic potential enabled "
469 "but no field accessible. potential id is " +
470 std::to_string(potential_id));
471 }
472 kernel_diffusion_electrostatic(potential_id);
473 } else {
474 kernel_diffusion();
475 }
476
477 kernel_migration();
478 kernel_boundary_flux();
479 // friction coupling
480 if (get_friction_coupling()) {
481 if (force_id == walberla::BlockDataID{}) {
482 throw std::runtime_error("Walberla EK: friction coupling enabled but "
483 "no force field accessible. force_id is " +
484 std::to_string(force_id) +
485 ". Hint: LB may be inactive.");
486 }
487 kernel_friction_coupling(force_id);
488 }
489
490 if (get_advection()) {
491 if (velocity_id == walberla::BlockDataID{}) {
492 throw std::runtime_error("Walberla EK: advection enabled but no "
493 "velocity field accessible. velocity_id is " +
494 std::to_string(velocity_id) +
495 ". Hint: LB may be inactive.");
496 }
497 kernel_advection(velocity_id);
498 }
499 kernel_continuity();
500
501 // is this the expected behavior when reactions are included?
502 kernel_boundary_density();
503
504 // Handle VTK writers
506 }
507
508 [[nodiscard]] std::size_t get_density_id() const noexcept override {
509 static_assert(std::is_same_v<std::size_t, walberla::uint_t>);
510 return static_cast<std::size_t>(m_density_field_id);
511 }
512
513 bool set_node_density(Utils::Vector3i const &node, double density) override {
514 auto bc = get_block_and_cell(get_lattice(), node, false);
515 if (!bc)
516 return false;
517
518 auto density_field =
519 bc->block->template getData<DensityField>(m_density_field_id);
520 density_field->get(bc->cell) = FloatType_c(density);
521
522 return true;
523 }
524
525 [[nodiscard]] std::optional<double>
527 bool consider_ghosts = false) const override {
528 auto bc = get_block_and_cell(get_lattice(), node, consider_ghosts);
529
530 if (!bc)
531 return std::nullopt;
532
533 auto const density_field =
534 bc->block->template getData<DensityField>(m_density_field_id);
535
536 return {double_c(density_field->get(bc->cell))};
537 }
538
539 [[nodiscard]] std::vector<double>
541 Utils::Vector3i const &upper_corner) const override {
542 std::vector<double> out;
543 if (auto const ci = get_interval(lower_corner, upper_corner)) {
544 auto const &lattice = get_lattice();
545 auto const &block = *(lattice.get_blocks()->begin());
546 auto const density_field =
547 block.template getData<DensityField>(m_density_field_id);
548 auto const lower_cell = ci->min();
549 auto const upper_cell = ci->max();
550 auto const n_values = ci->numCells();
551 out.reserve(n_values);
552 for (auto x = lower_cell.x(); x <= upper_cell.x(); ++x) {
553 for (auto y = lower_cell.y(); y <= upper_cell.y(); ++y) {
554 for (auto z = lower_cell.z(); z <= upper_cell.z(); ++z) {
555 out.emplace_back(density_field->get(Cell{x, y, z}));
556 }
557 }
558 }
559 assert(out.size() == n_values);
560 }
561 return out;
562 }
563
564 void set_slice_density(Utils::Vector3i const &lower_corner,
565 Utils::Vector3i const &upper_corner,
566 std::vector<double> const &density) override {
567 if (auto const ci = get_interval(lower_corner, upper_corner)) {
568 auto const &lattice = get_lattice();
569 auto &block = *(lattice.get_blocks()->begin());
570 auto density_field =
571 block.template getData<DensityField>(m_density_field_id);
572 auto it = density.begin();
573 auto const lower_cell = ci->min();
574 auto const upper_cell = ci->max();
575 assert(density.size() == ci->numCells());
576 for (auto x = lower_cell.x(); x <= upper_cell.x(); ++x) {
577 for (auto y = lower_cell.y(); y <= upper_cell.y(); ++y) {
578 for (auto z = lower_cell.z(); z <= upper_cell.z(); ++z) {
579 density_field->get(Cell{x, y, z}) = FloatType_c(*it);
580 ++it;
581 }
582 }
583 }
584 }
585 }
586
588
592
594 Utils::Vector3d const &flux) override {
595 auto bc = get_block_and_cell(get_lattice(), node, true);
596 if (!bc)
597 return false;
598
599 m_boundary_flux->set_node_value_at_boundary(
600 node, to_vector3<FloatType>(flux), *bc);
601
602 return true;
603 }
604
605 [[nodiscard]] std::optional<Utils::Vector3d>
607 bool consider_ghosts = false) const override {
608 auto const bc = get_block_and_cell(get_lattice(), node, consider_ghosts);
609 if (!bc or !m_boundary_flux->node_is_boundary(node))
610 return std::nullopt;
611
612 return {to_vector3d(m_boundary_flux->get_node_value_at_boundary(node))};
613 }
614
616 auto bc = get_block_and_cell(get_lattice(), node, true);
617 if (!bc)
618 return false;
619
620 m_boundary_flux->remove_node_from_boundary(node, *bc);
621
622 return true;
623 }
624
626 double density) override {
627 auto bc = get_block_and_cell(get_lattice(), node, true);
628 if (!bc)
629 return false;
630
631 m_boundary_density->set_node_value_at_boundary(node, FloatType_c(density),
632 *bc);
633
634 return true;
635 }
636
637 [[nodiscard]] std::optional<double>
639 bool consider_ghosts = false) const override {
640 auto const bc = get_block_and_cell(get_lattice(), node, consider_ghosts);
641 if (!bc or !m_boundary_density->node_is_boundary(node))
642 return std::nullopt;
643
644 return {double_c(m_boundary_density->get_node_value_at_boundary(node))};
645 }
646
648 Utils::Vector3i const &lower_corner, Utils::Vector3i const &upper_corner,
649 std::vector<std::optional<double>> const &density) override {
650 if (auto const ci = get_interval(lower_corner, upper_corner)) {
651 auto const &lattice = get_lattice();
652 auto const local_offset = std::get<0>(lattice.get_local_grid_range());
653 auto const lower_cell = ci->min();
654 auto const upper_cell = ci->max();
655 auto it = density.begin();
656 assert(density.size() == ci->numCells());
657 for (auto x = lower_cell.x(); x <= upper_cell.x(); ++x) {
658 for (auto y = lower_cell.y(); y <= upper_cell.y(); ++y) {
659 for (auto z = lower_cell.z(); z <= upper_cell.z(); ++z) {
660 auto const node = local_offset + Utils::Vector3i{{x, y, z}};
661 auto const bc = get_block_and_cell(lattice, node, false);
662 auto const &opt = *it;
663 if (opt) {
664 m_boundary_density->set_node_value_at_boundary(
665 node, FloatType_c(*opt), *bc);
666 } else {
667 m_boundary_density->remove_node_from_boundary(node, *bc);
668 }
669 ++it;
670 }
671 }
672 }
673 }
674 }
675
676 [[nodiscard]] std::vector<std::optional<double>>
678 Utils::Vector3i const &lower_corner,
679 Utils::Vector3i const &upper_corner) const override {
680 std::vector<std::optional<double>> out;
681 if (auto const ci = get_interval(lower_corner, upper_corner)) {
682 auto const &lattice = get_lattice();
683 auto const local_offset = std::get<0>(lattice.get_local_grid_range());
684 auto const lower_cell = ci->min();
685 auto const upper_cell = ci->max();
686 auto const n_values = ci->numCells();
687 out.reserve(n_values);
688 for (auto x = lower_cell.x(); x <= upper_cell.x(); ++x) {
689 for (auto y = lower_cell.y(); y <= upper_cell.y(); ++y) {
690 for (auto z = lower_cell.z(); z <= upper_cell.z(); ++z) {
691 auto const node = local_offset + Utils::Vector3i{{x, y, z}};
692 if (m_boundary_density->node_is_boundary(node)) {
693 out.emplace_back(double_c(
694 m_boundary_density->get_node_value_at_boundary(node)));
695 } else {
696 out.emplace_back(std::nullopt);
697 }
698 }
699 }
700 }
701 assert(out.size() == n_values);
702 }
703 return out;
704 }
705
707 Utils::Vector3i const &lower_corner, Utils::Vector3i const &upper_corner,
708 std::vector<std::optional<Utils::Vector3d>> const &flux) override {
709 if (auto const ci = get_interval(lower_corner, upper_corner)) {
710 auto const &lattice = get_lattice();
711 auto const local_offset = std::get<0>(lattice.get_local_grid_range());
712 auto const lower_cell = ci->min();
713 auto const upper_cell = ci->max();
714 auto it = flux.begin();
715 assert(flux.size() == ci->numCells());
716 for (auto x = lower_cell.x(); x <= upper_cell.x(); ++x) {
717 for (auto y = lower_cell.y(); y <= upper_cell.y(); ++y) {
718 for (auto z = lower_cell.z(); z <= upper_cell.z(); ++z) {
719 auto const node = local_offset + Utils::Vector3i{{x, y, z}};
720 auto const bc = get_block_and_cell(lattice, node, false);
721 auto const &opt = *it;
722 if (opt) {
723 m_boundary_flux->set_node_value_at_boundary(
724 node, to_vector3<FloatType>(*opt), *bc);
725 } else {
726 m_boundary_flux->remove_node_from_boundary(node, *bc);
727 }
728 ++it;
729 }
730 }
731 }
732 }
733 }
734
735 [[nodiscard]] std::vector<std::optional<Utils::Vector3d>>
737 Utils::Vector3i const &lower_corner,
738 Utils::Vector3i const &upper_corner) const override {
739 std::vector<std::optional<Utils::Vector3d>> out;
740 if (auto const ci = get_interval(lower_corner, upper_corner)) {
741 auto const &lattice = get_lattice();
742 auto const local_offset = std::get<0>(lattice.get_local_grid_range());
743 auto const lower_cell = ci->min();
744 auto const upper_cell = ci->max();
745 auto const n_values = ci->numCells();
746 out.reserve(n_values);
747 for (auto x = lower_cell.x(); x <= upper_cell.x(); ++x) {
748 for (auto y = lower_cell.y(); y <= upper_cell.y(); ++y) {
749 for (auto z = lower_cell.z(); z <= upper_cell.z(); ++z) {
750 auto const node = local_offset + Utils::Vector3i{{x, y, z}};
751 if (m_boundary_flux->node_is_boundary(node)) {
752 out.emplace_back(to_vector3d(
753 m_boundary_flux->get_node_value_at_boundary(node)));
754 } else {
755 out.emplace_back(std::nullopt);
756 }
757 }
758 }
759 }
760 assert(out.size() == n_values);
761 }
762 return out;
763 }
764
765 [[nodiscard]] std::vector<bool>
767 Utils::Vector3i const &upper_corner) const override {
768 std::vector<bool> out;
769 if (auto const ci = get_interval(lower_corner, upper_corner)) {
770 auto const &lattice = get_lattice();
771 auto const local_offset = std::get<0>(lattice.get_local_grid_range());
772 auto const lower_cell = ci->min();
773 auto const upper_cell = ci->max();
774 auto const n_values = ci->numCells();
775 out.reserve(n_values);
776 for (auto x = lower_cell.x(); x <= upper_cell.x(); ++x) {
777 for (auto y = lower_cell.y(); y <= upper_cell.y(); ++y) {
778 for (auto z = lower_cell.z(); z <= upper_cell.z(); ++z) {
779 auto const node = local_offset + Utils::Vector3i{{x, y, z}};
780 out.emplace_back(m_boundary_density->node_is_boundary(node) or
781 m_boundary_flux->node_is_boundary(node));
782 }
783 }
784 }
785 assert(out.size() == n_values);
786 }
787 return out;
788 }
789
791 auto bc = get_block_and_cell(get_lattice(), node, true);
792 if (!bc)
793 return false;
794
795 m_boundary_density->remove_node_from_boundary(node, *bc);
796
797 return true;
798 }
799
800 [[nodiscard]] std::optional<bool>
802 bool consider_ghosts) const override {
803 auto bc = get_block_and_cell(get_lattice(), node, consider_ghosts);
804 if (!bc)
805 return std::nullopt;
806
807 return {m_boundary_flux->node_is_boundary(node)};
808 }
809
810 [[nodiscard]] std::optional<bool>
812 bool consider_ghosts) const override {
813 auto bc = get_block_and_cell(get_lattice(), node, consider_ghosts);
814 if (!bc)
815 return std::nullopt;
816
817 return {m_boundary_density->node_is_boundary(node)};
818 }
819
820 [[nodiscard]] std::optional<bool>
822 bool consider_ghosts = false) const override {
823 auto bc = get_block_and_cell(get_lattice(), node, consider_ghosts);
824 if (!bc)
825 return std::nullopt;
826
827 return {m_boundary_density->node_is_boundary(node) or
828 m_boundary_flux->node_is_boundary(node)};
829 }
830
832 const std::vector<int> &raster_flat,
833 const std::vector<double> &data_flat) override {
834 auto const grid_size = get_lattice().get_grid_dimensions();
835 auto const data = fill_3D_vector_array(data_flat, grid_size);
836 set_boundary_from_grid(*m_boundary_flux, get_lattice(), raster_flat, data);
838 }
839
841 const std::vector<int> &raster_flat,
842 const std::vector<double> &data_flat) override {
843 auto const grid_size = get_lattice().get_grid_dimensions();
844 auto const data = fill_3D_scalar_array(data_flat, grid_size);
846 data);
848 }
849
850 void reallocate_flux_boundary_field() { m_boundary_flux->boundary_update(); }
851
853 m_boundary_density->boundary_update();
854 }
855
856 [[nodiscard]] LatticeWalberla const &get_lattice() const noexcept override {
857 return *m_lattice;
858 }
859
860 void register_vtk_field_filters(walberla::vtk::VTKOutput &vtk_obj) override {
861 field::FlagFieldCellFilter<FlagField> fluid_filter(m_flag_field_density_id);
862 fluid_filter.addFlag(Boundary_flag);
863 vtk_obj.addCellExclusionFilter(fluid_filter);
864 }
865
866protected:
867 template <typename Field_T, uint_t F_SIZE_ARG, typename OutputType>
868 class VTKWriter : public vtk::BlockCellDataWriter<OutputType, F_SIZE_ARG> {
869 public:
870 VTKWriter(ConstBlockDataID const &block_id, std::string const &id,
871 FloatType unit_conversion)
872 : vtk::BlockCellDataWriter<OutputType, F_SIZE_ARG>(id),
873 m_block_id(block_id), m_field(nullptr),
874 m_conversion(unit_conversion) {}
875
876 protected:
877 void configure() override {
878 WALBERLA_ASSERT_NOT_NULLPTR(this->block_);
879 m_field = this->block_->template getData<Field_T>(m_block_id);
880 }
881
882 ConstBlockDataID const m_block_id;
883 Field_T const *m_field;
884 FloatType const m_conversion;
885 };
886
887 template <typename OutputType = float,
889 class DensityVTKWriter : public VTKWriter<DensityField, 1u, OutputType> {
890 public:
891 using VTKWriter<DensityField, 1u, OutputType>::VTKWriter;
892
893 protected:
894 OutputType evaluate(cell_idx_t const x, cell_idx_t const y,
895 cell_idx_t const z, cell_idx_t const) override {
896 WALBERLA_ASSERT_NOT_NULLPTR(this->m_field);
897 auto const density = VectorTrait<typename DensityField::value_type>::get(
898 this->m_field->get(x, y, z, 0), uint_c(0));
899 return numeric_cast<OutputType>(this->m_conversion * density);
900 }
901 };
902
903public:
904 void register_vtk_field_writers(walberla::vtk::VTKOutput &vtk_obj,
905 LatticeModel::units_map const &units,
906 int flag_observables) override {
907 if (flag_observables & static_cast<int>(EKOutputVTK::density)) {
908 auto const unit_conversion = FloatType_c(units.at("density"));
909 vtk_obj.addCellDataWriter(make_shared<DensityVTKWriter<float>>(
910 m_density_field_id, "density", unit_conversion));
911 }
912 }
913
914 ~EKinWalberlaImpl() override = default;
915};
916
917} // namespace walberla
Vector implementation and trait types for boost qvm interoperability.
Definition Cell.hpp:97
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.
static DEVICE_QUALIFIER constexpr Vector< T, N > broadcast(typename Base::value_type const &value)
Create a vector that has all entries set to the same value.
Definition Vector.hpp:110
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
VTKWriter(ConstBlockDataID const &block_id, std::string const &id, FloatType unit_conversion)
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 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
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
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
unsigned int get_seed() const noexcept override
bool get_advection() const noexcept override
std::size_t get_density_id() const noexcept override
GhostLayerField< FloatType, FluxCount > FluxField
double get_diffusion() 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
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 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
std::optional< bool > get_node_is_density_boundary(Utils::Vector3i const &node, bool consider_ghosts) const override
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
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
std::vector< std::optional< Utils::Vector3d > > get_slice_flux_at_boundary(Utils::Vector3i const &lower_corner, Utils::Vector3i const &upper_corner) const override
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)
Definition elc.cpp:172
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)
std::vector< double > fill_3D_scalar_array(std::vector< double > const &vec_flat, Utils::Vector3i const &grid_size)
Definition boundary.hpp:60
void set_boundary_from_grid(BoundaryModel &boundary, LatticeWalberla const &lattice, std::vector< int > const &raster_flat, std::vector< DataType > const &data_flat)
Definition boundary.hpp:81
std::vector< Utils::Vector3d > fill_3D_vector_array(std::vector< double > const &vec_flat, Utils::Vector3i const &grid_size)
Definition boundary.hpp:36
Utils::Vector3d to_vector3d(Vector3< float > const &v)