Skip to content

Commit 3e43981

Browse files
committed
Fix EKSpecies setters
1 parent acd3d4f commit 3e43981

File tree

7 files changed

+44
-20
lines changed

7 files changed

+44
-20
lines changed

maintainer/walberla_kernels/generate_ek_kernels.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ def replace_macros(filename: str) -> None:
5656
content = f.read()
5757
f.seek(0)
5858
f.truncate(0)
59-
# replace replace_getData_with_uncheckedFastGetData
59+
# replace getData with uncheckedFastGetData
6060
content = content.replace("block->getData<IndexVectors>(indexVectorID);",
6161
"block->uncheckedFastGetData<IndexVectors>(indexVectorID);")
6262
# remove dummy assignment
@@ -154,7 +154,8 @@ def replace_macros(filename: str) -> None:
154154
block_offset=block_offsets if fluctuation else None,
155155
**params)
156156

157-
# the subsitution for field reads is necessary, because otherwise there are "ResolvedFieldAccess" nodes that fail in the code generation
157+
# the substitution for field reads is necessary, because otherwise there are
158+
# "ResolvedFieldAccess" nodes that fail in the code generation
158159
flux_advection = ps.AssignmentCollection(ek.flux_advection())
159160
flux_advection = ps.simp.add_subexpressions_for_field_reads(flux_advection)
160161

src/script_interface/walberla/EKSpecies.hpp

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,9 @@
3636
#include <utils/math/int_pow.hpp>
3737

3838
#include <memory>
39+
#include <stdexcept>
3940
#include <string>
41+
#include <unordered_map>
4042

4143
namespace ScriptInterface::walberla {
4244

@@ -66,7 +68,13 @@ class EKSpecies : public LatticeModel<::EKinWalberlaBase, EKVTKHandle> {
6668
[this]() { return m_instance->get_diffusion() / m_conv_diffusion; }},
6769
{"kT",
6870
[this](Variant const &v) {
69-
m_instance->set_kT(get_value<double>(v) * m_conv_energy);
71+
context()->parallel_try_catch([&]() {
72+
auto const kT = get_value<double>(v);
73+
if (kT < 0.) {
74+
throw std::domain_error("Parameter 'kT' must be >= 0");
75+
}
76+
m_instance->set_kT(kT * m_conv_energy);
77+
});
7078
},
7179
[this]() { return m_instance->get_kT() / m_conv_energy; }},
7280
{"valency",

src/walberla_bridge/include/walberla_bridge/electrokinetics/EKinWalberlaBase.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ class EKinWalberlaBase : public LatticeModel {
140140
[[nodiscard]] virtual Utils::Vector3d get_ext_efield() const noexcept = 0;
141141
[[nodiscard]] virtual bool is_double_precision() const noexcept = 0;
142142
[[nodiscard]] virtual bool is_thermalized() const noexcept = 0;
143-
[[nodiscard]] virtual uint get_seed() const noexcept = 0;
143+
[[nodiscard]] virtual unsigned int get_seed() const noexcept = 0;
144144
[[nodiscard]] virtual std::optional<uint64_t> get_rng_state() const = 0;
145145

146146
virtual void set_diffusion(double diffusion) = 0;

src/walberla_bridge/include/walberla_bridge/electrokinetics/ek_walberla_init.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ new_ek_walberla(std::shared_ptr<LatticeWalberla> const &lattice,
3636
double diffusion, double kT, double valency,
3737
Utils::Vector3d ext_efield, double density, bool advection,
3838
bool friction_coupling, bool single_precision, bool thermalized,
39-
uint seed);
39+
unsigned int seed);
4040

4141
std::shared_ptr<EKReactionBase>
4242
new_ek_reaction_bulk(std::shared_ptr<LatticeWalberla> const &lattice,

src/walberla_bridge/src/electrokinetics/EKinWalberlaImpl.hpp

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ class EKinWalberlaImpl : public EKinWalberlaBase {
109109
Utils::Vector3d m_ext_efield;
110110
bool m_advection;
111111
bool m_friction_coupling;
112-
uint m_seed;
112+
unsigned int m_seed;
113113

114114
protected:
115115
// Block data access handles
@@ -177,7 +177,7 @@ class EKinWalberlaImpl : public EKinWalberlaBase {
177177
EKinWalberlaImpl(std::shared_ptr<LatticeWalberla> lattice, double diffusion,
178178
double kT, double valency, Utils::Vector3d const &ext_efield,
179179
double density, bool advection, bool friction_coupling,
180-
bool thermalized, uint seed)
180+
bool thermalized, unsigned int seed)
181181
: m_diffusion(FloatType_c(diffusion)), m_kT(FloatType_c(kT)),
182182
m_valency(FloatType_c(valency)), m_ext_efield(ext_efield),
183183
m_advection(advection), m_friction_coupling(friction_coupling),
@@ -247,7 +247,9 @@ class EKinWalberlaImpl : public EKinWalberlaBase {
247247
return static_cast<bool>(
248248
std::get_if<DiffusiveFluxKernelThermalized>(&*m_diffusive_flux));
249249
}
250-
[[nodiscard]] uint get_seed() const noexcept override { return m_seed; }
250+
[[nodiscard]] unsigned int get_seed() const noexcept override {
251+
return m_seed;
252+
}
251253
[[nodiscard]] std::optional<uint64_t> get_rng_state() const override {
252254
auto const kernel =
253255
std::get_if<DiffusiveFluxKernelThermalized>(&*m_diffusive_flux);
@@ -259,30 +261,31 @@ class EKinWalberlaImpl : public EKinWalberlaBase {
259261

260262
void set_diffusion(double diffusion) override {
261263
m_diffusion = FloatType_c(diffusion);
262-
263-
auto lambda = [m_diffusion = m_diffusion](auto &kernel) {
264+
auto visitor = [m_diffusion = m_diffusion](auto &kernel) {
264265
kernel.D_ = m_diffusion;
265266
};
266-
267-
std::visit(lambda, *m_diffusive_flux);
268-
std::visit(lambda, *m_diffusive_flux_electrostatic);
267+
std::visit(visitor, *m_diffusive_flux);
268+
std::visit(visitor, *m_diffusive_flux_electrostatic);
269269
}
270+
270271
void set_kT(double kT) override {
271272
m_kT = FloatType_c(kT);
272-
273273
std::visit([m_kT = m_kT](auto &kernel) { kernel.kT_ = m_kT; },
274274
*m_diffusive_flux_electrostatic);
275275
}
276+
276277
void set_valency(double valency) override {
277278
m_valency = FloatType_c(valency);
278-
279279
std::visit([m_valency = m_valency](auto &kernel) { kernel.z_ = m_valency; },
280280
*m_diffusive_flux_electrostatic);
281281
}
282+
282283
void set_advection(bool advection) override { m_advection = advection; }
284+
283285
void set_friction_coupling(bool friction_coupling) override {
284286
m_friction_coupling = friction_coupling;
285287
}
288+
286289
void set_rng_state(uint64_t counter) override {
287290
auto const kernel =
288291
std::get_if<DiffusiveFluxKernelThermalized>(&*m_diffusive_flux);
@@ -298,6 +301,7 @@ class EKinWalberlaImpl : public EKinWalberlaBase {
298301
kernel->time_step_ = static_cast<uint32_t>(counter);
299302
kernel_electrostatic->time_step_ = static_cast<uint32_t>(counter);
300303
}
304+
301305
void set_ext_efield(Utils::Vector3d const &field) override {
302306
m_ext_efield = field;
303307

@@ -330,7 +334,7 @@ class EKinWalberlaImpl : public EKinWalberlaBase {
330334
std::move(kernel_electrostatic));
331335
}
332336

333-
void set_diffusion_kernels(uint seed) {
337+
void set_diffusion_kernels(unsigned int seed) {
334338
auto const grid_dim = get_lattice().get_grid_dimensions();
335339

336340
auto kernel = DiffusiveFluxKernelThermalized(
@@ -412,9 +416,8 @@ class EKinWalberlaImpl : public EKinWalberlaBase {
412416
}
413417

414418
void kernel_diffusion_electrostatic(const std::size_t &potential_id) {
415-
auto const potential_id_blockdata = BlockDataID(potential_id);
416-
std::visit([phiID = potential_id_blockdata](
417-
auto &kernel) { kernel.phiID = phiID; },
419+
auto const phiID = BlockDataID(potential_id);
420+
std::visit([phiID](auto &kernel) { kernel.phiID = phiID; },
418421
*m_diffusive_flux_electrostatic);
419422

420423
for (auto &block : *m_lattice->get_blocks()) {

src/walberla_bridge/src/electrokinetics/ek_walberla_init.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ new_ek_walberla(std::shared_ptr<LatticeWalberla> const &lattice,
3737
double diffusion, double kT, double valency,
3838
Utils::Vector3d ext_efield, double density, bool advection,
3939
bool friction_coupling, bool single_precision, bool thermalized,
40-
uint seed) {
40+
unsigned int seed) {
4141
if (single_precision) {
4242
return std::make_shared<EKinWalberlaImpl<13, float>>(
4343
lattice, diffusion, kT, valency, ext_efield, density, advection,

testsuite/python/ek_interface.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,9 @@ def test_ek_species(self):
100100
ek_species = self.make_default_ek_species(thermalized=True, seed=42)
101101
self.assertTrue(ek_species.thermalized)
102102
self.assertEqual(ek_species.seed, 42)
103+
self.assertEqual(ek_species.rng_state, 0)
104+
ek_species.rng_state = 5
105+
self.assertEqual(ek_species.rng_state, 5)
103106

104107
def check_ek_species_properties(self, species):
105108
agrid = self.params["agrid"]
@@ -199,6 +202,15 @@ def test_ek_none_solver(self):
199202
self.assertIsInstance(self.system.ekcontainer.solver,
200203
espressomd.electrokinetics.EKNone)
201204

205+
def test_ek_species_exceptions(self):
206+
ek_species = self.make_default_ek_species()
207+
with self.assertRaisesRegex(ValueError, "Parameter 'kT' must be >= 0"):
208+
ek_species.kT = -0.4
209+
with self.assertRaisesRegex(ValueError, "Parameter 'rng_state' must be >= 0"):
210+
ek_species.rng_state = -2
211+
with self.assertRaisesRegex(RuntimeError, "This EK instance is unthermalized"):
212+
ek_species.rng_state = 5
213+
202214
def test_ek_solver_exceptions(self):
203215
ek_solver = self.system.ekcontainer.solver
204216
ek_species = self.make_default_ek_species()

0 commit comments

Comments
 (0)