ESPResSo
Extensible Simulation Package for Research on Soft Matter Systems
Loading...
Searching...
No Matches
LBVTK.impl.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 * Out-of-class VTK writer registration definition for
25 * @ref walberla::LBWalberlaImpl.
26 */
27
28#include <field/iterators/IteratorMacros.h>
29
30#include <memory>
31#include <optional>
32#include <string>
33
34namespace walberla {
35
36/**
37 * @brief Base class for LB field VTK writers.
38 * Provides unit conversion and field access for cell-based VTK output.
39 * All field is copied to a Vector container before writing.
40 * @tparam FloatType Internal LB precision (float or double).
41 * @tparam VecType Vector type to copy the field data into.
42 * @tparam F_SIZE_ARG Number of components per cell (1, 3, or 9).
43 * @tparam OutputType VTK output precision (default: float).
44 */
45template <typename FloatType, typename VecType, uint_t F_SIZE_ARG,
46 typename OutputType>
47class VTKWriter : public vtk::BlockCellDataWriter<OutputType, F_SIZE_ARG> {
48public:
49 VTKWriter(ConstBlockDataID const &block_id, std::string const &id,
50 FloatType unit_conversion)
51 : vtk::BlockCellDataWriter<OutputType, F_SIZE_ARG>(id),
52 m_conversion(unit_conversion), m_content{} {}
53
54protected:
55 void configure() override { WALBERLA_ASSERT_NOT_NULLPTR(this->block_); }
56
57 std::size_t get_first_index(cell_idx_t const x, cell_idx_t const y,
58 cell_idx_t const z) {
59 return (static_cast<std::size_t>(x) * m_dims[2] * m_dims[1] +
60 static_cast<std::size_t>(y) * m_dims[2] +
61 static_cast<std::size_t>(z)) *
62 F_SIZE_ARG;
63 }
64
65 FloatType m_conversion;
66 VecType m_content;
67 Vector3<uint_t> m_dims;
68
69public:
70 void set_content(VecType content) { m_content = std::move(content); }
71
72 void set_dims(Vector3<uint_t> dims) { m_dims = dims; }
73};
74
75template <typename FloatType, typename OutputType = float>
77 : public VTKWriter<FloatType, std::vector<FloatType>, 1u, OutputType> {
78public:
80 using Base::Base;
81 using Base::evaluate;
82
83protected:
84 OutputType evaluate(cell_idx_t const x, cell_idx_t const y,
85 cell_idx_t const z, cell_idx_t const) override {
86 WALBERLA_ASSERT(!this->m_content.empty());
87 auto const density = this->m_content[this->get_first_index(x, y, z)];
88 return numeric_cast<OutputType>(this->m_conversion * density);
89 }
90};
91
92template <typename FloatType, typename OutputType = float>
94 : public VTKWriter<FloatType, std::vector<FloatType>, 3u, OutputType> {
95public:
97 using Base::Base;
98 using Base::evaluate;
99
100protected:
101 OutputType evaluate(cell_idx_t const x, cell_idx_t const y,
102 cell_idx_t const z, cell_idx_t const f) override {
103 WALBERLA_ASSERT(!this->m_content.empty());
104 auto velocity = this->m_content[this->get_first_index(x, y, z) + f];
105 return numeric_cast<OutputType>(this->m_conversion * velocity);
106 }
107};
108
109template <typename FloatType, typename OutputType = float>
111 : public VTKWriter<FloatType, std::vector<FloatType>, 9u, OutputType> {
112public:
114 using Base::Base;
115 using Base::evaluate;
116
117protected:
118 OutputType evaluate(cell_idx_t const x, cell_idx_t const y,
119 cell_idx_t const z, cell_idx_t const f) override {
120 WALBERLA_ASSERT(!this->m_content.empty());
121 auto pressure = this->m_content[this->get_first_index(x, y, z) + f];
122 return numeric_cast<OutputType>(this->m_conversion * pressure);
123 }
124};
125
126template <typename FloatType, lbmpy::Arch Architecture>
128 walberla::vtk::VTKOutput &vtk_obj, LatticeModel::units_map const &units,
129 int flag_observables) {
130 if (flag_observables & static_cast<int>(OutputVTK::density)) {
131 auto const unit_conversion = FloatType_c(units.at("density"));
132 auto const blocks = m_lattice->get_blocks();
133 WALBERLA_ASSERT_NOT_NULLPTR(blocks);
134 auto density_writer = std::make_shared<DensityVTKWriter<FloatType, float>>(
135 m_pdf_field_id, "density", unit_conversion);
136 auto before_function = [this, blocks, density_writer]() {
137 for (auto &block : *blocks) {
138 auto *pdf_field = block.template getData<PdfField>(m_pdf_field_id);
139 auto const bci = pdf_field->xyzSize();
140 density_writer->set_content(
141 lbm::accessor::Density::get(pdf_field, m_density, bci));
142 density_writer->set_dims(Vector3<uint_t>(
143 uint_c(bci.xSize()), uint_c(bci.ySize()), uint_c(bci.zSize())));
144 }
145 };
146 vtk_obj.addBeforeFunction(std::move(before_function));
147 vtk_obj.addCellDataWriter(density_writer);
148 }
149 if (flag_observables & static_cast<int>(OutputVTK::velocity_vector)) {
150 auto const unit_conversion = FloatType_c(units.at("velocity"));
151 auto const blocks = m_lattice->get_blocks();
152 WALBERLA_ASSERT_NOT_NULLPTR(blocks);
153 auto velocity_writer =
154 std::make_shared<VelocityVTKWriter<FloatType, float>>(
155 m_pdf_field_id, "velocity_vector", unit_conversion);
156 auto before_function = [this, blocks, velocity_writer]() {
157 for (auto &block : *blocks) {
158 auto *velocity_field =
159 block.template getData<VectorField>(m_velocity_field_id);
160 auto const bci = velocity_field->xyzSize();
161 velocity_writer->set_content(
162 lbm::accessor::Vector::get(velocity_field, bci));
163 velocity_writer->set_dims(Vector3<uint_t>(
164 uint_c(bci.xSize()), uint_c(bci.ySize()), uint_c(bci.zSize())));
165 }
166 };
167 vtk_obj.addBeforeFunction(std::move(before_function));
168 vtk_obj.addCellDataWriter(velocity_writer);
169 }
170 if (flag_observables & static_cast<int>(OutputVTK::pressure_tensor)) {
171 auto const unit_conversion = FloatType_c(units.at("pressure"));
172 auto const blocks = m_lattice->get_blocks();
173 WALBERLA_ASSERT_NOT_NULLPTR(blocks);
174 auto pressure_writer =
175 std::make_shared<PressureTensorVTKWriter<FloatType, float>>(
176 m_pdf_field_id, "pressure_tensor", unit_conversion);
177 auto before_function = [this, blocks, pressure_writer]() {
178 for (auto &block : *blocks) {
179 auto *pdf_field = block.template getData<PdfField>(m_pdf_field_id);
180 auto const bci = pdf_field->xyzSize();
181 auto values =
182 lbm::accessor::PressureTensor::get(pdf_field, m_density, bci);
183 for (std::size_t n = 0u; n < values.size(); n += 9u) {
184 pressure_tensor_correction(
185 std::span<FloatType, 9ul>(&values[n], 9ul));
186 }
187 pressure_writer->set_content(std::move(values));
188 pressure_writer->set_dims(Vector3<uint_t>(
189 uint_c(bci.xSize()), uint_c(bci.ySize()), uint_c(bci.zSize())));
190 }
191 };
192 vtk_obj.addBeforeFunction(std::move(before_function));
193 vtk_obj.addCellDataWriter(pressure_writer);
194 }
195}
196
197} // namespace walberla
std::unordered_map< std::string, double > units_map
OutputType evaluate(cell_idx_t const x, cell_idx_t const y, cell_idx_t const z, cell_idx_t const) override
void register_vtk_field_writers(walberla::vtk::VTKOutput &vtk_obj, LatticeModel::units_map const &units, int flag_observables) override
OutputType evaluate(cell_idx_t const x, cell_idx_t const y, cell_idx_t const z, cell_idx_t const f) override
Base class for LB field VTK writers.
void configure() override
VTKWriter(ConstBlockDataID const &block_id, std::string const &id, FloatType unit_conversion)
Vector3< uint_t > m_dims
void set_dims(Vector3< uint_t > dims)
std::size_t get_first_index(cell_idx_t const x, cell_idx_t const y, cell_idx_t const z)
void set_content(VecType content)
OutputType evaluate(cell_idx_t const x, cell_idx_t const y, cell_idx_t const z, cell_idx_t const f) override
static double * block(double *p, std::size_t index, std::size_t size)
Definition elc.cpp:175
double get(GhostLayerField< double, uint_t{19u}> const *pdf_field, double const density, Cell const &cell)
auto get(GhostLayerField< double, uint_t{19u}> const *pdf_field, double const density, Cell const &cell)
auto get(GhostLayerField< double, uint_t{3u}> const *vec_field, Cell const &cell)
\file PackInfoPdfDoublePrecision.cpp \author pystencils
static Utils::Vector3d velocity(Particle const &p_ref, Particle const &p_vs)
Velocity of the virtual site.
Definition relative.cpp:64