Skip to content

Commit deb7f9b

Browse files
Thermalized Lees-Edwards LB Kernels
- Changed generate_lb_kernels.py to thermalize LE. - Added kT and seed parameters in all the LE functions in walberla bridge. - Generated new kernels Co-authored-by: RudolfWeeber <[email protected]>
1 parent dae3ef4 commit deb7f9b

File tree

30 files changed

+3414
-691
lines changed

30 files changed

+3414
-691
lines changed

maintainer/walberla_kernels/generate_lb_kernels.py

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -148,22 +148,36 @@ def generate_init_kernels(ctx, method):
148148
def generate_stream_collide_lees_edwards_kernels(
149149
ctx, method, data_type, fields):
150150
precision_prefix = pystencils_espresso.precision_prefix[ctx.double_accuracy]
151-
lbm_opt = lbmpy.LBMOptimisation(symbolic_field=fields["pdfs"],
152-
symbolic_temporary_field=fields["pdfs_tmp"])
153-
shear_dir_normal = 1 # y-axis
151+
precision_rng = pystencils_espresso.precision_rng[ctx.double_accuracy]
152+
block_offsets = tuple(
153+
ps.TypedSymbol(f"block_offset_{i}", np.uint32)
154+
for i in range(3))
155+
optimization = {"cse_global": True,
156+
"double_precision": ctx.double_accuracy}
154157
le_config = lbmpy.LBMConfig(stencil=stencil,
155158
method=lbmpy.Method.TRT,
156-
relaxation_rate=sp.Symbol("omega_shear"),
157159
streaming_pattern="pull",
160+
relaxation_rate=sp.Symbol("omega_shear"),
158161
force_model=lbmpy.ForceModel.GUO,
159162
force=fields["force"].center_vector,
160163
kernel_type="stream_pull_collide",
161164
**lbm_config_kwargs)
162-
le_update_rule_unthermalized = lbmpy.create_lb_update_rule(
165+
lbm_opt = lbmpy.LBMOptimisation(symbolic_field=fields["pdfs"],
166+
symbolic_temporary_field=fields["pdfs_tmp"])
167+
shear_dir_normal = 1 # y-axis
168+
lb_collision_rule_thermalized = lbmpy.creationfunctions.create_lb_collision_rule(
169+
method,
163170
lbm_config=le_config,
164-
lbm_optimisation=lbm_opt)
165-
le_collision_rule_unthermalized = lees_edwards.add_lees_edwards_to_collision(
166-
config, le_update_rule_unthermalized, fields["pdfs"], stencil,
171+
fluctuating={
172+
"temperature": kT,
173+
"block_offsets": block_offsets,
174+
"rng_node": precision_rng
175+
},
176+
lbm_optimisation=lbm_opt,
177+
optimization=optimization
178+
)
179+
le_lb_collision_rule_thermalized = lees_edwards.add_lees_edwards_to_collision(
180+
config, lb_collision_rule_thermalized, fields["pdfs"], stencil,
167181
shear_dir_normal, True)
168182
optimization = {"cse_global": True,
169183
"double_precision": ctx.double_accuracy}
@@ -174,10 +188,11 @@ def generate_stream_collide_lees_edwards_kernels(
174188
method,
175189
le_config,
176190
data_type,
177-
le_collision_rule_unthermalized,
191+
le_lb_collision_rule_thermalized,
178192
stem,
179193
optimization,
180-
params
194+
params,
195+
block_offset=block_offsets
181196
)
182197
ctx.patch_file(stem, get_ext_source(target_suffix),
183198
patch_openmp_kernels)

src/core/lb/LBWalberla.cpp

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -156,10 +156,6 @@ void LBWalberla::update_collision_model(LBWalberlaBase &lb,
156156
auto le_protocol = system.lees_edwards->get_protocol();
157157
if (le_protocol and
158158
not std::holds_alternative<LeesEdwards::Off>(*le_protocol)) {
159-
if (kT != 0.) {
160-
throw std::runtime_error(
161-
"Lees-Edwards LB doesn't support thermalization");
162-
}
163159
auto const &le_bc = system.box_geo->lees_edwards_bc();
164160
auto lees_edwards_object = std::make_unique<LeesEdwardsPack>(
165161
le_bc.shear_direction, le_bc.shear_plane_normal,
@@ -171,7 +167,7 @@ void LBWalberla::update_collision_model(LBWalberlaBase &lb,
171167
return get_shear_velocity(system.get_sim_time(), *le_protocol) *
172168
(params.get_tau() / params.get_agrid());
173169
});
174-
lb.set_collision_model(std::move(lees_edwards_object));
170+
lb.set_collision_model(std::move(lees_edwards_object), kT, seed);
175171
} else {
176172
lb.set_collision_model(kT, seed);
177173
}

src/walberla_bridge/include/walberla_bridge/lattice_boltzmann/LBWalberlaBase.hpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -241,7 +241,8 @@ class LBWalberlaBase : public LatticeModel {
241241

242242
/** @brief Configure a thermalized collision model for Lees-Edwards. */
243243
virtual void
244-
set_collision_model(std::unique_ptr<LeesEdwardsPack> &&lees_edwards_pack) = 0;
244+
set_collision_model(std::unique_ptr<LeesEdwardsPack> &&lees_edwards_pack,
245+
double kT, unsigned int seed) = 0;
245246

246247
/** @brief Check Lees-Edwards boundary conditions. */
247248
virtual void check_lebc(unsigned int shear_direction,

src/walberla_bridge/src/lattice_boltzmann/LBWalberlaImpl.hpp

Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,7 @@ class LBWalberlaImpl : public LBWalberlaBase {
210210
}
211211

212212
void operator()(StreamCollisionModelLeesEdwards &cm, IBlock *b) {
213+
cm.configure(m_storage, b);
213214
cm.setV_s(static_cast<decltype(cm.getV_s())>(
214215
m_lees_edwards_callbacks->get_shear_velocity()));
215216
cm(b);
@@ -531,9 +532,8 @@ class LBWalberlaImpl : public LBWalberlaBase {
531532
auto const block_variant = std::variant<IBlock *>(&block);
532533
std::visit(m_run_stream_collide_sweep, cm_variant, block_variant);
533534
}
534-
if (auto *cm = std::get_if<StreamCollisionModelThermalized>(&cm_variant)) {
535-
cm->setTime_step(cm->getTime_step() + 1u);
536-
}
535+
std::visit([](auto &cm) { cm.setTime_step(cm.getTime_step() + 1u); },
536+
cm_variant);
537537
}
538538

539539
auto has_lees_edwards_bc() const {
@@ -700,8 +700,8 @@ class LBWalberlaImpl : public LBWalberlaBase {
700700
setup_streaming_communicator();
701701
}
702702

703-
void set_collision_model(
704-
std::unique_ptr<LeesEdwardsPack> &&lees_edwards_pack) override {
703+
void set_collision_model(std::unique_ptr<LeesEdwardsPack> &&lees_edwards_pack,
704+
double kT, unsigned int seed) override {
705705
assert(m_kT == 0.);
706706
#if defined(__CUDACC__)
707707
if constexpr (Architecture == lbmpy::Arch::GPU) {
@@ -712,6 +712,7 @@ class LBWalberlaImpl : public LBWalberlaBase {
712712
auto const shear_plane_normal = lees_edwards_pack->shear_plane_normal;
713713
auto const shear_vel = FloatType_c(lees_edwards_pack->get_shear_velocity());
714714
auto const omega = shear_mode_relaxation_rate();
715+
auto const omega_odd = odd_mode_relaxation_rate(omega);
715716
if (shear_plane_normal != 1u) {
716717
throw std::domain_error(
717718
"Lees-Edwards LB only supports shear_plane_normal=\"y\"");
@@ -728,10 +729,13 @@ class LBWalberlaImpl : public LBWalberlaBase {
728729
}
729730
auto const &grid_dimensions = lattice.get_grid_dimensions();
730731
auto const grid_size = FloatType_c(grid_dimensions[shear_plane_normal]);
732+
m_kT = FloatType_c(kT);
733+
m_seed = seed;
731734
m_collision_model =
732735
std::make_shared<CollisionModel>(StreamCollisionModelLeesEdwards(
733-
m_last_applied_force_field_id, m_pdf_field_id, grid_size, omega,
734-
shear_vel));
736+
m_last_applied_force_field_id, m_pdf_field_id, grid_size,
737+
zero_centered_to_lb(m_kT), omega, omega, omega_odd, omega, seed,
738+
uint32_t{0u}, shear_vel));
735739
m_lees_edwards_callbacks = std::move(lees_edwards_pack);
736740
m_run_stream_collide_sweep =
737741
StreamCollideSweepVisitor(blocks, m_lees_edwards_callbacks);
@@ -1830,23 +1834,25 @@ class LBWalberlaImpl : public LBWalberlaBase {
18301834
}
18311835

18321836
[[nodiscard]] std::optional<uint64_t> get_rng_state() const override {
1833-
auto const cm =
1834-
std::get_if<StreamCollisionModelThermalized>(&*m_collision_model);
1835-
if (!cm or m_kT == 0.) {
1837+
if (m_kT == 0.) {
18361838
return std::nullopt;
18371839
}
1838-
return {static_cast<uint64_t>(cm->getTime_step())};
1840+
return std::visit(
1841+
[](auto const &cm) { return static_cast<uint64_t>(cm.getTime_step()); },
1842+
*m_collision_model);
18391843
}
18401844

18411845
void set_rng_state(uint64_t counter) override {
1842-
auto const cm =
1843-
std::get_if<StreamCollisionModelThermalized>(&*m_collision_model);
1844-
if (!cm or m_kT == 0.) {
1846+
if (m_kT == 0.) {
18451847
throw std::runtime_error("This LB instance is unthermalized");
18461848
}
18471849
assert(counter <=
18481850
static_cast<uint32_t>(std::numeric_limits<uint_t>::max()));
1849-
cm->setTime_step(static_cast<uint32_t>(counter));
1851+
std::visit(
1852+
[counter](auto &cm) {
1853+
cm.setTime_step(static_cast<uint32_t>(counter));
1854+
},
1855+
*m_collision_model);
18501856
}
18511857

18521858
[[nodiscard]] LatticeWalberla const &get_lattice() const noexcept override {

0 commit comments

Comments
 (0)