summaryrefslogtreecommitdiff
path: root/modules/core/c++
diff options
context:
space:
mode:
Diffstat (limited to 'modules/core/c++')
-rw-r--r--modules/core/c++/abstract/data_ptr.hpp150
-rw-r--r--modules/core/c++/fplbm.hpp55
-rw-r--r--modules/core/c++/hlbm.hpp156
-rw-r--r--modules/core/c++/lbm.hpp5
-rw-r--r--modules/core/c++/particle/aabb.hpp17
-rw-r--r--modules/core/c++/particle/blur.hpp37
-rw-r--r--modules/core/c++/particle/cell_list.hpp210
-rw-r--r--modules/core/c++/particle/contact.hpp567
-rw-r--r--modules/core/c++/particle/couple.hpp108
-rw-r--r--modules/core/c++/particle/integrate.hpp175
-rw-r--r--modules/core/c++/particle/mask.hpp399
-rw-r--r--modules/core/c++/particle/particle.hpp161
-rw-r--r--modules/core/c++/particle/porosity.hpp46
-rw-r--r--modules/core/c++/particle/schema.hpp54
-rw-r--r--modules/core/c++/particle/voxel_narrow.hpp262
-rw-r--r--modules/core/c++/psm.hpp69
16 files changed, 2258 insertions, 213 deletions
diff --git a/modules/core/c++/abstract/data_ptr.hpp b/modules/core/c++/abstract/data_ptr.hpp
new file mode 100644
index 0000000..183b653
--- /dev/null
+++ b/modules/core/c++/abstract/data_ptr.hpp
@@ -0,0 +1,150 @@
+#pragma once
+
+#include "data.hpp"
+#include "data_math.hpp"
+
+namespace kel {
+namespace lbm {
+
+/**
+ * Host-side borrowed views, mirroring the device Ptr encodings.
+ *
+ * On an accelerator a kernel captures its arguments by value into a const
+ * lambda, so a device view is always reached through a const object. The
+ * device types answer that by being a const POINTER to mutable data -- the
+ * same distinction as `T* const` against `const T*`. That is the correct model
+ * for a borrowed device handle, not a workaround.
+ *
+ * The consequence is that components, which take their group through such a
+ * view, cannot be instantiated against the plain host `data` types, because
+ * those are honestly const-correct. Templates are only checked when
+ * instantiated, so a component referencing a member that does not exist
+ * compiles fine until a real kernel is built -- which is how two such bugs got
+ * past the host tests here already.
+ *
+ * These views close that hole: same const semantics as the device, usable on
+ * the host, so a component can be instantiated and exercised in a plain test.
+ */
+
+template <typename Sch, uint64_t... Dims>
+class data<sch::Ptr<sch::FixedArray<Sch, Dims...>>, enc::Native> final {
+public:
+ using Schema = sch::Ptr<sch::FixedArray<Sch, Dims...>>;
+ using Inner = data<Sch, enc::Native>;
+
+ static constexpr uint64_t Rank = sizeof...(Dims);
+
+private:
+ Inner *values_;
+
+public:
+ KEL_DEFAULT_COPY(data);
+ KEL_DEFAULT_MOVE(data);
+
+ data() : values_{nullptr} {}
+
+ data(data<sch::FixedArray<Sch, Dims...>, enc::Native> &backing)
+ : values_{backing.flat_data()} {}
+
+ data(Inner *values__) : values_{values__} {}
+
+ static constexpr index_data<Rank> get_dims() { return {Dims...}; }
+
+ static constexpr index_data<Rank> meta() { return {Dims...}; }
+
+ /**
+ * Const on the view, mutable through it. Matches the device encoding.
+ */
+ Inner &at(const index_data<Rank> &index) const {
+ // Row major, same fold the owning FixedArray uses.
+ constexpr std::array<uint64_t, Rank> dims{Dims...};
+ uint64_t flat = 0u;
+ uint64_t stride = 1u;
+ for(uint64_t i{0u}; i < Rank; ++i){
+ flat += index.flat_at(i).get() * stride;
+ stride *= dims.at(i);
+ }
+ return values_[flat];
+ }
+
+ Inner &flat_at(uint64_t i) const { return values_[i]; }
+
+ Inner *flat_data() const { return values_; }
+};
+
+template <typename... Sch, string_literal... Keys>
+class data<sch::Ptr<sch::Struct<sch::Member<Sch, Keys>...>>, enc::Native> final {
+public:
+ using Schema = sch::Ptr<sch::Struct<sch::Member<Sch, Keys>...>>;
+
+private:
+ using StorageT = std::tuple<data<sch::Ptr<Sch>, enc::Native>...>;
+ StorageT members_;
+
+ template <string_literal K>
+ static constexpr size_t index_of = parameter_key_pack_index<K, Keys...>::value;
+
+public:
+ data() = default;
+
+ KEL_DEFAULT_COPY(data);
+ KEL_DEFAULT_MOVE(data);
+
+ /**
+ * Builds a view over an owning struct, member by member.
+ */
+ data(data<sch::Struct<sch::Member<Sch, Keys>...>, enc::Native> &backing) {
+ bind<0u>(backing);
+ }
+
+ template <size_t i> auto &get() const {
+ return std::get<i>(const_cast<StorageT &>(members_));
+ }
+
+ template <string_literal K> auto &get() const {
+ return std::get<index_of<K>>(const_cast<StorageT &>(members_));
+ }
+
+private:
+ template <uint64_t i, typename Backing> void bind(Backing &backing) {
+ if constexpr (i < sizeof...(Sch)) {
+ std::get<i>(members_) = {backing.template get_at_index<i>()};
+ bind<i + 1u>(backing);
+ }
+ }
+};
+
+/**
+ * Non-structural leaves are borrowed as a plain pointer to the value.
+ */
+template <typename Sch>
+class data<sch::Ptr<Sch>, enc::Native> final {
+public:
+ using Schema = sch::Ptr<Sch>;
+ using Inner = data<Sch, enc::Native>;
+
+private:
+ Inner *value_;
+
+public:
+ KEL_DEFAULT_COPY(data);
+ KEL_DEFAULT_MOVE(data);
+
+ data() : value_{nullptr} {}
+ data(Inner &value__) : value_{&value__} {}
+
+ Inner &operator()() const { return *value_; }
+
+ Inner *handle() const { return value_; }
+};
+
+/**
+ * Convenience: build a host view over an owning value, mirroring make_view.
+ */
+template <typename Sch>
+data<sch::Ptr<Sch>, enc::Native> make_host_view(data<Sch, enc::Native> &backing) {
+ return data<sch::Ptr<Sch>, enc::Native>{backing};
+}
+
+} // namespace lbm
+} // namespace kel
diff --git a/modules/core/c++/fplbm.hpp b/modules/core/c++/fplbm.hpp
index 6783f12..4f9c81d 100644
--- a/modules/core/c++/fplbm.hpp
+++ b/modules/core/c++/fplbm.hpp
@@ -2,6 +2,7 @@
#include "common.hpp"
#include "particle/particle.hpp"
+#include "particle/integrate.hpp"
#include "iterator.hpp"
namespace kel {
@@ -12,6 +13,7 @@ struct FpLbm {};
struct FpLbmOneParticle {};
struct FpLbmMomentumOneParticle {};
struct FpLbmOneParticleNoVelocity{};
+struct FpLbmOneParticleGather {};
}
namespace method {
struct FpLbm {};
@@ -138,9 +140,9 @@ public:
};
template<typename T, typename Descriptor, typename Encode>
-class component<T, Descriptor, cmpt::FpLbmOneParticle, Encode> final {
+class component<T, Descriptor, cmpt::FpLbmOneParticleGather, Encode> final {
public:
- using Component = cmpt::FpLbmOneParticle;
+ using Component = cmpt::FpLbmOneParticleGather;
private:
kel::lbm::data<sch::Vector<T,Descriptor::D>> global_force_;
public:
@@ -177,12 +179,11 @@ public:
auto& pirb = pi.template get<"rigid_body">();
auto& pirb_pos = pirb.template get<"position">();
auto& pirb_pos_old = pirb.template get<"position_old">();
+ auto& p_rad = pi.template get<"collision">().template get<"radius">();
- auto& p_coll = pg.template get<"collision">().at({});
- auto& p_rad = p_coll.template get<"radius">();
- auto eo_aabb = particle_aabb<typename ParticleSchema::ValueType>::calculate(pg,{{0u}},mom_f.meta());
+ auto eo_aabb = particle_aabb<typename ParticleSchema::ValueType>::calculate(pg,index,mom_f.meta());
if(eo_aabb.is_error()){
return;
}
@@ -227,15 +228,43 @@ public:
force_p = force_p - force;
}, aabb.template get<"a">(), aabb.template get<"b">());
- auto& pirb_acc = pirb.template get<"acceleration">();
- pirb_acc = global_force_ + force_p / (pg.template get<"total_mass">().at({}));
+ /**
+ * Accumulate rather than assign, so a contact pass can add to this
+ * before cmpt::ParticleIntegrate turns the total into motion.
+ */
+ auto& pirb_force = pirb.template get<"force">();
+ pirb_force = pirb_force + force_p;
+ }
+};
- if(sub_steps.get() > 0u){
- auto sss_inv = one / sss;
- for(kel::lbm::data<sch::UInt64> i{0u}; i < sub_steps; ++i){
- verlet_step_lambda<T,Descriptor::D>(pi,sss_inv);
- }
- }
+/**
+ * The original fused one-particle step, kept so existing call sites keep
+ * working. global_force_ is an acceleration, so it rides along as the
+ * integrator's body acceleration.
+ */
+template<typename T, typename Descriptor, typename Encode>
+class component<T, Descriptor, cmpt::FpLbmOneParticle, Encode> final {
+public:
+ using Component = cmpt::FpLbmOneParticle;
+private:
+ kel::lbm::data<sch::Vector<T,Descriptor::D>> global_force_;
+public:
+ component():
+ global_force_{}
+ {}
+ component(kel::lbm::data<sch::Vector<T,Descriptor::D>> global_force__):
+ global_force_{global_force__}
+ {}
+
+ template<typename CellFieldSchema, typename MacroFieldSchema, typename ParticleSchema>
+ void apply(const kel::lbm::data<CellFieldSchema, Encode>& field, const kel::lbm::data<MacroFieldSchema,Encode>& macros, const kel::lbm::data<ParticleSchema,Encode>& pg, kel::lbm::data<sch::FixedArray<sch::UInt64,1u>> index, kel::lbm::data<sch::UInt64> time_step, kel::lbm::data<sch::UInt64> sub_steps) const {
+ component<T,Descriptor,cmpt::ParticleForceReset,Encode> reset;
+ component<T,Descriptor,cmpt::FpLbmOneParticleGather,Encode> gather;
+ component<T,Descriptor,cmpt::ParticleIntegrate,Encode> integrate{global_force_};
+
+ reset.apply(pg,index,time_step);
+ gather.apply(field,macros,pg,index,time_step,sub_steps);
+ integrate.apply(pg,index,time_step,sub_steps);
}
};
}
diff --git a/modules/core/c++/hlbm.hpp b/modules/core/c++/hlbm.hpp
index 4f22cbc..b7bf003 100644
--- a/modules/core/c++/hlbm.hpp
+++ b/modules/core/c++/hlbm.hpp
@@ -5,6 +5,7 @@
#include "equilibrium.hpp"
#include "particle/particle.hpp"
+#include "particle/integrate.hpp"
#include <iostream>
@@ -16,6 +17,8 @@ struct Hlbm {};
struct HlbmOneParticle {};
struct HlbmParticle {};
struct HlbmOneParticleMomentumExchange {};
+struct HlbmOneParticleGather {};
+struct HlbmOneParticleStamp {};
}
namespace method {
struct Hlbm {};
@@ -103,61 +106,59 @@ public:
}
};
-
+/**
+ * Gathers the hydrodynamic force on one particle and stamps its porosity.
+ *
+ * Accumulates into the particle's force member rather than writing
+ * acceleration, so contact forces can be added before cmpt::ParticleIntegrate
+ * turns the total into motion.
+ */
template<typename T, typename Desc, typename Encode>
-class component<T, Desc, cmpt::HlbmOneParticle, Encode> final {
+class component<T, Desc, cmpt::HlbmOneParticleGather, Encode> final {
private:
public:
template<typename CellFieldSchema, typename MacroFieldSchema, typename ParticleSchema>
void apply(const kel::lbm::data<CellFieldSchema, Encode>& field, const kel::lbm::data<MacroFieldSchema,Encode>& macros, const kel::lbm::data<ParticleSchema,Encode>& part_group, kel::lbm::data<sch::FixedArray<sch::UInt64,1u>> index, kel::lbm::data<sch::UInt64> time_step, kel::lbm::data<sch::UInt64> sub_steps) const {
- /// Figure out how to access the particle list
- // auto& p = particles.at(i);
-
- /// Iterate over the grid bounds
- // auto& grid = p.template get<"grid">();
using dfi = df_info<T,Desc>;
bool is_even = ((time_step.get() % 2) == 0);
-
+
kel::lbm::data<sch::Scalar<T>> one;
one.at({}) = 1.0;
-
+
auto& dfs_old_f = (is_even) ? field.template get<"dfs_old">() : field.template get<"dfs">();
auto& part_spheroid_group = part_group;
auto& mvel_f = macros.template get<"momentum">();
auto& mrho_f = macros.template get<"density">();
auto& mpor_f = macros.template get<"porosity">();
-
- auto& particle_N_f = field.template get<"particle_N">();
- auto& particle_D_f = field.template get<"particle_D">();
{
auto parts = part_spheroid_group.template get<"particles">();
- auto parts_size = parts.meta().at({0u});
-
- auto& p_coll = part_spheroid_group.template get<"collision">().at({});
- auto& p_rad = p_coll.template get<"radius">();
auto& pi = parts.at(index);
auto& pirb = pi.template get<"rigid_body">();
+ auto& p_rad = pi.template get<"collision">().template get<"radius">();
kel::lbm::data<sch::Vector<T,Desc::D>>& pirb_pos = pirb.template get<"position">();
auto& pirb_pos_old = pirb.template get<"position_old">();
-
- // TODO !!!! Divide by actual time step - for now it's ok
- kel::lbm::data<sch::Scalar<T>> ts;
- ts.at({}) = one.at({}) / sub_steps.template cast_to<T>();
- kel::lbm::data<sch::FixedArray<sch::UInt64,Desc::D>> start;
- kel::lbm::data<sch::FixedArray<sch::UInt64,Desc::D>> stop;
+ // Matches cmpt::ParticleIntegrate: zero sub steps means one step.
+ uint64_t steps = sub_steps.get();
+ if(steps == 0u){
+ steps = 1u;
+ }
+ kel::lbm::data<sch::Scalar<T>> ts;
+ ts.at({}).set(
+ typename kel::lbm::native_data_type<T>::type(1)
+ / static_cast<typename kel::lbm::native_data_type<T>::type>(steps)
+ );
- auto eo_aabb = particle_aabb<typename ParticleSchema::ValueType>::calculate(part_spheroid_group,{{0u}},mvel_f.meta());
+ auto eo_aabb = particle_aabb<typename ParticleSchema::ValueType>::calculate(part_spheroid_group,index,mvel_f.meta());
if(eo_aabb.is_error()){
return;
}
auto& aabb = eo_aabb.get_value();
- /// Ok, I iterate over the space which covers our particle? So lower bounds to upper bounds
- start = aabb.template get<"a">();
- stop = aabb.template get<"b">();
+ auto start = aabb.template get<"a">();
+ auto stop = aabb.template get<"b">();
kel::lbm::data<sch::Vector<T,Desc::D>> force_p;
for(uint64_t i{0u}; i < Desc::D; ++i){
@@ -170,7 +171,7 @@ public:
kel::lbm::data<sch::Vector<T,Desc::D>> rel_dist = kel::lbm::math::vectorize_data(index_f).template cast_to<T>() - pirb_pos;
kel::lbm::data<sch::Scalar<T>> eps;
eps.at({}) = 1.5f;
-
+
auto& mpor = mpor_f.at(index_f);
mpor = particle_porosity<T,Desc::D,1u,por::ParticleSpheroid<T>>::calculate(rel_dist,p_rad,eps);
@@ -206,30 +207,99 @@ public:
// TODO technically needs to adjust for rotation as well
auto& rho = mrho_f.at(index_f);
+ (void) rho;
macros.template get<"force">().at(index_f) = momentum;
force_p = force_p + momentum;
},start,stop);
-
- auto& pirb_acc = pirb.template get<"acceleration">();
- pirb_acc = - force_p / part_spheroid_group.template get<"total_mass">().at({});
- for(kel::lbm::data<sch::UInt64> i{0u}; i < sub_steps; ++i){
- verlet_step_lambda<T,Desc::D>(pi,ts);
- }
- auto vel_p = (pirb_pos-pirb_pos_old) / ts;
+ // Accumulate, do not assign: contact adds to this before integration.
+ auto& pirb_force = pirb.template get<"force">();
+ pirb_force = pirb_force - force_p;
+ }
+ }
+};
- iterator<Desc::D>::apply([&](const auto& index_f){
- auto& N = particle_N_f.at(index_f);
- auto& D = particle_D_f.at(index_f);
- auto& mpor = mpor_f.at(index_f);
- auto flip_porosity = one - mpor;
+/**
+ * Writes the HLBM solid-fraction coupling terms.
+ *
+ * Must run AFTER cmpt::ParticleIntegrate, because it uses the post-move
+ * particle velocity.
+ */
+template<typename T, typename Desc, typename Encode>
+class component<T, Desc, cmpt::HlbmOneParticleStamp, Encode> final {
+private:
+public:
+ template<typename CellFieldSchema, typename MacroFieldSchema, typename ParticleSchema>
+ void apply(const kel::lbm::data<CellFieldSchema, Encode>& field, const kel::lbm::data<MacroFieldSchema,Encode>& macros, const kel::lbm::data<ParticleSchema,Encode>& part_group, kel::lbm::data<sch::FixedArray<sch::UInt64,1u>> index, kel::lbm::data<sch::UInt64> time_step, kel::lbm::data<sch::UInt64> sub_steps) const {
+ (void) time_step;
- D = D + flip_porosity;
- N = N + vel_p * flip_porosity;
- },start,stop);
- // Check
+ kel::lbm::data<sch::Scalar<T>> one;
+ one.at({}) = 1.0;
+
+ auto& mvel_f = macros.template get<"momentum">();
+ auto& mpor_f = macros.template get<"porosity">();
+ auto& particle_N_f = field.template get<"particle_N">();
+ auto& particle_D_f = field.template get<"particle_D">();
+
+ auto parts = part_group.template get<"particles">();
+ auto& pi = parts.at(index);
+ auto& pirb = pi.template get<"rigid_body">();
+ auto& pirb_pos = pirb.template get<"position">();
+ auto& pirb_pos_old = pirb.template get<"position_old">();
+
+ uint64_t steps = sub_steps.get();
+ if(steps == 0u){
+ steps = 1u;
}
+ kel::lbm::data<sch::Scalar<T>> ts;
+ ts.at({}).set(
+ typename kel::lbm::native_data_type<T>::type(1)
+ / static_cast<typename kel::lbm::native_data_type<T>::type>(steps)
+ );
+
+ auto eo_aabb = particle_aabb<typename ParticleSchema::ValueType>::calculate(part_group,index,mvel_f.meta());
+ if(eo_aabb.is_error()){
+ return;
+ }
+ auto& aabb = eo_aabb.get_value();
+
+ auto vel_p = (pirb_pos-pirb_pos_old) / ts;
+
+ iterator<Desc::D>::apply([&](const auto& index_f){
+ auto& N = particle_N_f.at(index_f);
+ auto& D = particle_D_f.at(index_f);
+ auto& mpor = mpor_f.at(index_f);
+ auto flip_porosity = one - mpor;
+
+ D = D + flip_porosity;
+ N = N + vel_p * flip_porosity;
+ },aabb.template get<"a">(),aabb.template get<"b">());
+ }
+};
+
+/**
+ * The original fused one-particle step, kept so existing call sites keep
+ * working: gather, integrate, then stamp, in that order.
+ *
+ * New code should launch the three components separately, which is what makes
+ * room for a contact pass between the gather and the integrate.
+ */
+template<typename T, typename Desc, typename Encode>
+class component<T, Desc, cmpt::HlbmOneParticle, Encode> final {
+private:
+public:
+ template<typename CellFieldSchema, typename MacroFieldSchema, typename ParticleSchema>
+ void apply(const kel::lbm::data<CellFieldSchema, Encode>& field, const kel::lbm::data<MacroFieldSchema,Encode>& macros, const kel::lbm::data<ParticleSchema,Encode>& part_group, kel::lbm::data<sch::FixedArray<sch::UInt64,1u>> index, kel::lbm::data<sch::UInt64> time_step, kel::lbm::data<sch::UInt64> sub_steps) const {
+ component<T,Desc,cmpt::ParticleForceReset,Encode> reset;
+ component<T,Desc,cmpt::HlbmOneParticleGather,Encode> gather;
+ component<T,Desc,cmpt::ParticleIntegrate,Encode> integrate;
+ component<T,Desc,cmpt::HlbmOneParticleStamp,Encode> stamp;
+
+ reset.apply(part_group,index,time_step);
+ gather.apply(field,macros,part_group,index,time_step,sub_steps);
+ integrate.apply(part_group,index,time_step,sub_steps);
+ stamp.apply(field,macros,part_group,index,time_step,sub_steps);
}
};
}
diff --git a/modules/core/c++/lbm.hpp b/modules/core/c++/lbm.hpp
index 8c48e54..eefb556 100644
--- a/modules/core/c++/lbm.hpp
+++ b/modules/core/c++/lbm.hpp
@@ -17,6 +17,11 @@
#include "macroscopic.hpp"
#include "momentum_gather.hpp"
#include "memory.hpp"
+#include "particle/voxel_narrow.hpp"
+#include "particle/cell_list.hpp"
+#include "particle/couple.hpp"
+#include "particle/contact.hpp"
+#include "particle/integrate.hpp"
#include "psm.hpp"
#include "component/psm.hpp"
#include "stream.hpp"
diff --git a/modules/core/c++/particle/aabb.hpp b/modules/core/c++/particle/aabb.hpp
index e986272..caf3210 100644
--- a/modules/core/c++/particle/aabb.hpp
+++ b/modules/core/c++/particle/aabb.hpp
@@ -39,7 +39,8 @@ public:
auto& a = aabb.template get<"a">();
auto& b = aabb.template get<"b">();
- const kel::lbm::data<sch::Scalar<T>>& rad_d = p_grp.template get<"collision">().at({{0u}}).template get<"radius">();
+ // Radius is per particle now, so a group can hold mixed sizes.
+ const kel::lbm::data<sch::Scalar<T>>& rad_d = pi.template get<"collision">().template get<"radius">();
kel::lbm::data<sch::Vector<T,D>> lower;
kel::lbm::data<sch::Vector<T,D>> upper;
@@ -47,8 +48,20 @@ public:
for(uint64_t i{0u}; i < D; ++i){
lower.at({{i}}) = ((pirb_pos.at({{i}}) >= rad_d.at({})) ? (pirb_pos.at({{i}}) - rad_d.at({})) : kel::lbm::data<T>{0});
a.at({i}) = lower.at({{i}}).template cast_to<sch::UInt64>();
+
upper.at({{i}}) = pirb_pos.at({{i}}) + rad_d.at({});
- b.at({i}) = (upper.at({{i}})+kel::lbm::data<T>{1}).template cast_to<sch::UInt64>();
+ /**
+ * Clamp against the field extent. Callers hand the result straight
+ * to iterator<D>::apply as [a,b), so an unclamped b reads past the
+ * end for any particle within a radius of the far wall.
+ *
+ * Note this bounds the cell itself, not its neighbours: HLBM also
+ * reads neighbour_at on dfs, which is safe because DfChunk carries
+ * Ghost = 1. The macro chunks have Ghost = 0 and are only ever
+ * read at the cell itself.
+ */
+ auto b_i = (upper.at({{i}})+kel::lbm::data<T>{1}).template cast_to<sch::UInt64>();
+ b.at({i}) = (b_i < meta.at({i})) ? b_i : meta.at({i});
}
return aabb;
diff --git a/modules/core/c++/particle/blur.hpp b/modules/core/c++/particle/blur.hpp
deleted file mode 100644
index 972a309..0000000
--- a/modules/core/c++/particle/blur.hpp
+++ /dev/null
@@ -1,37 +0,0 @@
-#pragma once
-
-namespace kel {
-namespace lbm {
-template<typename T, uint64_t D>
-void blur_mask(kel::lbm::data<sch::Array<T,D>>& p_mask){
-
- kel::lbm::data<T> mid{2.0/3.0};
- kel::lbm::data<T> edge{1.0/6.0};
-
- // kel::lbm::data<sch::FixedArray<sch::UInt64>,2u> blur_weights{{2.0/3.0},{1.0/6.0}};
-
- auto meta = p_mask.dims();
- kel::lbm::data<sch::Array<T,D>> blurred_mask{meta};
-
- /* 1D blur into N-D Blur*/
- for(kel::lbm::data<sch::UInt64> i{0u}; i < kel::lbm::data<sch::UInt64>{D}; ++i){
- iterator<D>::apply([&](const auto& index){
- blurred_mask.at(index) = p_mask.at(index) * mid;
-
- if(index.at({i}).get() > 0u){
- auto l_ind = index;
- l_ind.at({i}) = ind.at({i}) - 1u;
- blurred_mask.at(index) = blurred_mask.at(index) + p_mask.at(l_ind) * edge;
- }
- if((index.at({i}).get() + 1u) < meta.at({i})){
- auto r_ind = index;
- r_ind.at({i}) = ind.at({i}) + 1u;
- blurred_mask.at(index) = blurred_mask.at(index) + p_mask.at(r_ind) * edge;
- }
- },{},meta);
-
- p_mask = blurred_mask;
- }
-}
-}
-}
diff --git a/modules/core/c++/particle/cell_list.hpp b/modules/core/c++/particle/cell_list.hpp
new file mode 100644
index 0000000..d1d1537
--- /dev/null
+++ b/modules/core/c++/particle/cell_list.hpp
@@ -0,0 +1,210 @@
+#pragma once
+
+#include "../component.hpp"
+#include "../iterator.hpp"
+#include "common.hpp"
+#include "schema.hpp"
+#include "particle.hpp"
+
+namespace kel {
+namespace lbm {
+
+namespace sch {
+/**
+ * Uniform bin grid over the lattice, used to answer "which particles can
+ * cover this cell" without scanning every particle.
+ *
+ * Bin size must be at least the largest particle diameter. Then a particle
+ * covering a lattice cell is always registered in that cell's own bin or one
+ * of its immediate neighbours, so a query only ever touches 3^D bins.
+ *
+ * `count` and `slot` are flat because that is the layout the SYCL copy helper
+ * handles as a single memcpy; `slot` holds CellCapacity ids per bin.
+ */
+template<typename T, uint64_t D, uint64_t CellCapacity>
+using ParticleCellList = Struct<
+ Member<FixedArray<Scalar<T>,1u>, "bin_size">,
+ Member<FixedArray<UInt64,D>, "grid">,
+ Member<Array<UInt64,1u>, "count">,
+ Member<Array<UInt64,1u>, "slot">,
+ Member<FixedArray<UInt64,1u>, "overflow">
+>;
+}
+
+/**
+ * Bin a position. Clamped, so a particle that has drifted outside the domain
+ * still lands in a real bin rather than indexing out of bounds.
+ */
+template<typename T, uint64_t D, typename PosRef, typename GridRef>
+uint64_t particle_bin_of(
+ const PosRef& pos,
+ typename kel::lbm::native_data_type<T>::type bin_size,
+ const GridRef& grid
+){
+ uint64_t flat = 0u;
+ uint64_t stride = 1u;
+
+ for(uint64_t i{0u}; i < D; ++i){
+ auto g = grid.at({i}).get();
+ auto x = pos.at({{i}}).get() / bin_size;
+
+ int64_t b = static_cast<int64_t>(x);
+ if(b < 0){
+ b = 0;
+ }
+ if(static_cast<uint64_t>(b) >= g){
+ b = static_cast<int64_t>(g) - 1;
+ }
+
+ flat += static_cast<uint64_t>(b) * stride;
+ stride *= g;
+ }
+
+ return flat;
+}
+
+/**
+ * Clears every bin. Must precede a rebuild; bins are not self-clearing.
+ */
+template<typename ClRef>
+void clear_cell_list(ClRef&& cl){
+ auto& count = cl.template get<"count">();
+ const uint64_t cells = count.size().get();
+ for(uint64_t i{0u}; i < cells; ++i){
+ count.at({i}).set(0);
+ }
+ cl.template get<"overflow">().at({0u}).set(0);
+}
+
+/**
+ * Registers every particle in its bin.
+ *
+ * Insertion runs in particle-index order, so the contents of each bin are
+ * reproducible run to run -- which is what keeps the downstream per-cell sums
+ * deterministic.
+ *
+ * Overflow is recorded rather than silently dropped: a lost particle would
+ * show up as a quietly missing body in the fluid, which is close to
+ * impossible to spot in a result.
+ */
+template<typename T, uint64_t D, uint64_t CellCapacity, typename PartsRef, typename ClRef>
+void build_cell_list(const PartsRef& parts, ClRef&& cl){
+ clear_cell_list(cl);
+
+ auto bin_size = cl.template get<"bin_size">().at({0u}).at({}).get();
+ auto& grid = cl.template get<"grid">();
+ auto& count = cl.template get<"count">();
+ auto& slot = cl.template get<"slot">();
+
+ const uint64_t n = parts.get_dims().at({0u}).get();
+
+ for(uint64_t p{0u}; p < n; ++p){
+ auto& pos = parts.at({p}).template get<"rigid_body">().template get<"position">();
+ auto bin = particle_bin_of<T,D>(pos,bin_size,grid);
+
+ auto c = count.at({bin}).get();
+ if(c >= CellCapacity){
+ auto& of = cl.template get<"overflow">().at({0u});
+ of.set(of.get() + 1u);
+ continue;
+ }
+
+ slot.at({bin * CellCapacity + c}).set(p);
+ count.at({bin}).set(c + 1u);
+ }
+}
+
+/**
+ * Calls func(particle_index) for every particle registered in the bin holding
+ * `cell` or in any bin adjacent to it.
+ *
+ * The 3^D neighbourhood is what makes the bin-size rule above load bearing: a
+ * smaller bin would let a particle reach past the neighbours it is searched in.
+ */
+template<typename T, uint64_t D, uint64_t CellCapacity, typename ClRef, typename CellRef, typename Func>
+void for_each_bin_candidate(const ClRef& cl, const CellRef& cell, Func&& func){
+ auto bin_size = cl.template get<"bin_size">().at({0u}).at({}).get();
+ auto& grid = cl.template get<"grid">();
+ auto& count = cl.template get<"count">();
+ auto& slot = cl.template get<"slot">();
+
+ int64_t base[D];
+ for(uint64_t i{0u}; i < D; ++i){
+ auto x = static_cast<typename kel::lbm::native_data_type<T>::type>(cell.at({i}).get());
+ base[i] = static_cast<int64_t>(x / bin_size);
+ }
+
+ // 3^D neighbourhood, unrolled as a mixed-radix counter.
+ uint64_t total = 1u;
+ for(uint64_t i{0u}; i < D; ++i){
+ total *= 3u;
+ }
+
+ for(uint64_t off{0u}; off < total; ++off){
+ uint64_t rem = off;
+ uint64_t flat = 0u;
+ uint64_t stride = 1u;
+ bool inside = true;
+
+ for(uint64_t i{0u}; i < D; ++i){
+ int64_t delta = static_cast<int64_t>(rem % 3u) - 1;
+ rem /= 3u;
+
+ int64_t b = base[i] + delta;
+ auto g = static_cast<int64_t>(grid.at({i}).get());
+ if(b < 0 || b >= g){
+ inside = false;
+ break;
+ }
+
+ flat += static_cast<uint64_t>(b) * stride;
+ stride *= static_cast<uint64_t>(g);
+ }
+
+ if(!inside){
+ continue;
+ }
+
+ auto c = count.at({flat}).get();
+ for(uint64_t s{0u}; s < c; ++s){
+ func(slot.at({flat * CellCapacity + s}).get());
+ }
+ }
+}
+
+/**
+ * Sizes a cell list for a domain and a largest particle radius.
+ */
+template<typename T, uint64_t D, uint64_t CellCapacity, typename ExtentRef>
+kel::lbm::data<sch::ParticleCellList<T,D,CellCapacity>> make_cell_list(
+ const ExtentRef& extent,
+ typename kel::lbm::native_data_type<T>::type max_radius
+){
+ kel::lbm::data<sch::ParticleCellList<T,D,CellCapacity>> cl;
+
+ // At least one lattice unit, and at least the largest diameter.
+ auto bin_size = max_radius * 2;
+ if(!(bin_size > 1)){
+ bin_size = 1;
+ }
+ cl.template get<"bin_size">().at({0u}).at({}).set(bin_size);
+
+ auto& grid = cl.template get<"grid">();
+ uint64_t cells = 1u;
+ for(uint64_t i{0u}; i < D; ++i){
+ auto e = static_cast<typename kel::lbm::native_data_type<T>::type>(extent.at({i}).get());
+ uint64_t g = static_cast<uint64_t>(e / bin_size) + 1u;
+ grid.at({i}).set(g);
+ cells *= g;
+ }
+
+ cl.template get<"count">() = {kel::lbm::data<sch::FixedArray<sch::UInt64,1u>>{{cells}}};
+ cl.template get<"slot">() = {kel::lbm::data<sch::FixedArray<sch::UInt64,1u>>{{cells * CellCapacity}}};
+
+ clear_cell_list(cl);
+
+ return cl;
+}
+
+}
+}
diff --git a/modules/core/c++/particle/contact.hpp b/modules/core/c++/particle/contact.hpp
new file mode 100644
index 0000000..2e7973a
--- /dev/null
+++ b/modules/core/c++/particle/contact.hpp
@@ -0,0 +1,567 @@
+#pragma once
+
+#include "../component.hpp"
+#include "../abstract/math.hpp"
+#include "common.hpp"
+#include "schema.hpp"
+#include "particle.hpp"
+#include "integrate.hpp"
+
+namespace kel {
+namespace lbm {
+
+namespace sch {
+/**
+ * One resolved contact.
+ *
+ * `normal` is a unit vector pointing from the partner into the body the force
+ * is being computed for, so the contact force on that body is along +normal.
+ * `depth` is the penetration along that normal; positive means in contact.
+ */
+template<typename T, uint64_t D>
+using ContactManifold = Struct<
+ Member<Scalar<T>, "depth">,
+ Member<Vector<T,D>, "normal">,
+ Member<Vector<T,D>, "point">
+>;
+}
+
+/**
+ * Soft-shell contact parameters.
+ *
+ * k_n normal stiffness
+ * zeta_n normal damping ratio, ~0.2..0.9; critical damping is 1
+ * gamma_t tangential viscous coefficient
+ * mu Coulomb friction cap
+ */
+template<typename T>
+struct dem_params {
+ kel::lbm::data<sch::Scalar<T>> k_n;
+ kel::lbm::data<sch::Scalar<T>> zeta_n;
+ kel::lbm::data<sch::Scalar<T>> gamma_t;
+ kel::lbm::data<sch::Scalar<T>> mu;
+
+ dem_params(){
+ k_n.at({}).set(1.0);
+ zeta_n.at({}).set(0.3);
+ gamma_t.at({}).set(0.0);
+ mu.at({}).set(0.0);
+ }
+
+ dem_params(
+ typename kel::lbm::native_data_type<T>::type k_n__,
+ typename kel::lbm::native_data_type<T>::type zeta_n__,
+ typename kel::lbm::native_data_type<T>::type gamma_t__ = 0.0,
+ typename kel::lbm::native_data_type<T>::type mu__ = 0.0
+ ){
+ k_n.at({}).set(k_n__);
+ zeta_n.at({}).set(zeta_n__);
+ gamma_t.at({}).set(gamma_t__);
+ mu.at({}).set(mu__);
+ }
+};
+
+/**
+ * Suggests a stiffness that keeps the worst-case overlap under delta_max for
+ * an impact at v_0. Energy argument: k delta^2 / 2 = m v^2 / 2.
+ */
+template<typename T>
+constexpr typename kel::lbm::native_data_type<T>::type dem_stiffness_for(
+ typename kel::lbm::native_data_type<T>::type m_eff,
+ typename kel::lbm::native_data_type<T>::type v_0,
+ typename kel::lbm::native_data_type<T>::type delta_max
+){
+ return m_eff * v_0 * v_0 / (delta_max * delta_max);
+}
+
+namespace impl {
+/**
+ * math::normalize divides by sqrt(dot) with no guard, so a zero vector yields
+ * NaN and poisons everything downstream. Contact routinely produces
+ * near-degenerate vectors, so every normalization here goes through this.
+ */
+template<typename T, uint64_t D>
+bool safe_normalize(
+ const kel::lbm::data<sch::Vector<T,D>>& in,
+ kel::lbm::data<sch::Vector<T,D>>& out
+){
+ auto len_2 = kel::lbm::math::dot(in,in);
+ auto l2 = len_2.at({}).get();
+ if(!(l2 > 0)){
+ return false;
+ }
+ auto inv = typename kel::lbm::native_data_type<T>::type(1)
+ / std::sqrt(l2);
+ for(uint64_t i{0u}; i < D; ++i){
+ out.at({{i}}).set(in.at({{i}}).get() * inv);
+ }
+ return true;
+}
+}
+
+/**
+ * Narrow phase. Specialized per collision shape so the voxel version can be
+ * swapped in later without touching the force law.
+ */
+template<typename Collision>
+class narrow_phase {
+ static_assert(kel::lbm::always_false<Collision>, "Not supported");
+};
+
+template<typename T>
+class narrow_phase<coll::Spheroid<T>> final {
+public:
+ /**
+ * Sphere-sphere contact.
+ *
+ * Takes the pair in a canonical order chosen by the caller. The result is
+ * then bitwise identical for both owners of the pair, which is what makes
+ * the symmetric no-atomics formulation conserve momentum exactly: IEEE
+ * subtraction is exactly antisymmetric, but only if both sides subtract in
+ * the same order.
+ *
+ * `normal` points from `hi` toward `lo`.
+ */
+ template<uint64_t D, typename PRef>
+ static bool contact(
+ const PRef& lo,
+ const PRef& hi,
+ kel::lbm::data<sch::ContactManifold<T,D>>& out
+ ){
+ auto& pos_lo = lo.template get<"rigid_body">().template get<"position">();
+ auto& pos_hi = hi.template get<"rigid_body">().template get<"position">();
+
+ auto r_lo = lo.template get<"collision">().template get<"radius">().at({}).get();
+ auto r_hi = hi.template get<"collision">().template get<"radius">().at({}).get();
+
+ kel::lbm::data<sch::Vector<T,D>> d;
+ for(uint64_t i{0u}; i < D; ++i){
+ d.at({{i}}) = pos_lo.at({{i}}) - pos_hi.at({{i}});
+ }
+
+ auto len_2 = kel::lbm::math::dot(d,d).at({}).get();
+ auto sum_r = r_lo + r_hi;
+
+ if(!(len_2 < sum_r * sum_r)){
+ return false;
+ }
+
+ auto len = std::sqrt(len_2);
+ auto depth = sum_r - len;
+
+ /**
+ * Cap the penetration at the smaller radius. A pair that tunnelled
+ * would otherwise produce an unbounded impulse and blow up the run.
+ */
+ auto cap = (r_lo < r_hi) ? r_lo : r_hi;
+ if(depth > cap){
+ depth = cap;
+ }
+
+ auto& n = out.template get<"normal">();
+ if(!impl::safe_normalize<T,D>(d,n)){
+ /**
+ * Coincident centres: there is no geometric direction left, so
+ * pick a fixed axis. Deterministic, and it separates the pair
+ * instead of producing NaN.
+ */
+ for(uint64_t i{0u}; i < D; ++i){
+ n.at({{i}}).set(0);
+ }
+ n.at({{0u}}).set(1);
+ }
+
+ out.template get<"depth">().at({}).set(depth);
+
+ // Contact point: on the surface of hi, along the normal.
+ auto& point = out.template get<"point">();
+ for(uint64_t i{0u}; i < D; ++i){
+ point.at({{i}}).set(
+ pos_hi.at({{i}}).get() + n.at({{i}}).get() * (r_hi - depth * 0.5)
+ );
+ }
+
+ return true;
+ }
+};
+
+/**
+ * Force and torque produced by one contact, on one body.
+ */
+template<typename T, uint64_t D>
+struct contact_response {
+ kel::lbm::data<sch::Vector<T,D>> force;
+ kel::lbm::data<typename sch::impl::rotation_type_helper<T,D>::Schema> torque;
+};
+
+namespace impl {
+/**
+ * The soft-shell law itself, shared by particle-particle and particle-wall.
+ *
+ * `m_eff` is the reduced mass of the pair; a wall is the infinite-mass limit,
+ * where it collapses to the particle's own mass. `v_rel` is the relative
+ * surface velocity at the contact point.
+ */
+template<typename T, uint64_t D>
+contact_response<T,D> soft_shell_response(
+ const kel::lbm::data<sch::ContactManifold<T,D>>& m,
+ const kel::lbm::data<sch::Vector<T,D>>& r_self,
+ const kel::lbm::data<sch::Vector<T,D>>& v_rel,
+ typename kel::lbm::native_data_type<T>::type m_eff,
+ const dem_params<T>& dem
+){
+ contact_response<T,D> res;
+
+ auto& n = m.template get<"normal">();
+ auto depth = m.template get<"depth">().at({}).get();
+
+ auto k_n = dem.k_n.at({}).get();
+ auto zeta = dem.zeta_n.at({}).get();
+
+ // Critical damping is 2 sqrt(m k); zeta scales it.
+ auto g_n = 2.0 * zeta * std::sqrt(m_eff * k_n);
+
+ auto v_n = kel::lbm::math::dot(v_rel,n).at({}).get();
+
+ /**
+ * Clamped at zero: a contact pushes, never pulls. Without this the
+ * dashpot would suck separating particles back together.
+ */
+ auto f_n = k_n * depth - g_n * v_n;
+ if(f_n < 0){
+ f_n = 0;
+ }
+
+ // Tangential part of the relative velocity.
+ kel::lbm::data<sch::Vector<T,D>> v_t;
+ for(uint64_t i{0u}; i < D; ++i){
+ v_t.at({{i}}).set(v_rel.at({{i}}).get() - v_n * n.at({{i}}).get());
+ }
+
+ kel::lbm::data<sch::Vector<T,D>> t_hat;
+ auto have_t = safe_normalize<T,D>(v_t,t_hat);
+
+ auto v_t_len = std::sqrt(kel::lbm::math::dot(v_t,v_t).at({}).get());
+ auto f_t = dem.gamma_t.at({}).get() * v_t_len;
+ auto f_t_cap = dem.mu.at({}).get() * f_n;
+ if(f_t > f_t_cap){
+ f_t = f_t_cap;
+ }
+
+ for(uint64_t i{0u}; i < D; ++i){
+ auto v = f_n * n.at({{i}}).get();
+ if(have_t){
+ // Friction opposes sliding.
+ v -= f_t * t_hat.at({{i}}).get();
+ }
+ res.force.at({{i}}).set(v);
+ }
+
+ // cross gives Scalar in 2D and Vector3 in 3D, which is exactly the
+ // rotation type in each case, so one spelling covers both.
+ res.torque = kel::lbm::math::cross<T>(r_self,res.force);
+
+ return res;
+}
+
+/**
+ * Surface velocity of a body at an offset from its centre.
+ *
+ * Velocity is implicit in position-Verlet, recovered from the position pair.
+ */
+template<typename T, uint64_t D, typename PRef>
+kel::lbm::data<sch::Vector<T,D>> surface_velocity(
+ const PRef& p,
+ const kel::lbm::data<sch::Vector<T,D>>& r,
+ typename kel::lbm::native_data_type<T>::type steps
+){
+ auto& rb = p.template get<"rigid_body">();
+ auto& pos = rb.template get<"position">();
+ auto& pos_old = rb.template get<"position_old">();
+
+ kel::lbm::data<sch::Vector<T,D>> v;
+ for(uint64_t i{0u}; i < D; ++i){
+ v.at({{i}}).set((pos.at({{i}}).get() - pos_old.at({{i}}).get()) * steps);
+ }
+
+ auto& rot = rb.template get<"rotation">();
+ auto& rot_old = rb.template get<"rotation_old">();
+
+ if constexpr ( D == 2u ){
+ kel::lbm::data<sch::Scalar<T>> omega;
+ omega.at({}).set((rot.at({}).get() - rot_old.at({}).get()) * steps);
+ auto tang = kel::lbm::math::cross<T>(omega,r);
+ for(uint64_t i{0u}; i < D; ++i){
+ v.at({{i}}) = v.at({{i}}) + tang.at({{i}});
+ }
+ }else{
+ kel::lbm::data<sch::Vector<T,3u>> omega;
+ for(uint64_t i{0u}; i < 3u; ++i){
+ omega.at({{i}}).set((rot.at({{i}}).get() - rot_old.at({{i}}).get()) * steps);
+ }
+ auto tang = kel::lbm::math::cross<T>(omega,r);
+ for(uint64_t i{0u}; i < D; ++i){
+ v.at({{i}}) = v.at({{i}}) + tang.at({{i}});
+ }
+ }
+
+ return v;
+}
+}
+
+/**
+ * Contact response on `self` from `other`.
+ *
+ * `self_is_lo` says which side of the canonical pair ordering `self` is on.
+ * The manifold is always computed as (lo,hi) so both owners see bitwise
+ * identical geometry; the hi owner simply flips the normal.
+ */
+template<typename T, uint64_t D, typename PRef>
+contact_response<T,D> particle_contact_response(
+ const kel::lbm::data<sch::ContactManifold<T,D>>& m_in,
+ const PRef& self,
+ const PRef& other,
+ bool self_is_lo,
+ const dem_params<T>& dem,
+ kel::lbm::data<sch::UInt64> sub_steps
+){
+ auto steps = static_cast<typename kel::lbm::native_data_type<T>::type>(
+ particle_sub_steps(sub_steps));
+
+ kel::lbm::data<sch::ContactManifold<T,D>> m = m_in;
+ if(!self_is_lo){
+ auto& n = m.template get<"normal">();
+ for(uint64_t i{0u}; i < D; ++i){
+ n.at({{i}}).set(-n.at({{i}}).get());
+ }
+ }
+
+ auto& point = m.template get<"point">();
+ auto& pos_s = self.template get<"rigid_body">().template get<"position">();
+ auto& pos_o = other.template get<"rigid_body">().template get<"position">();
+
+ kel::lbm::data<sch::Vector<T,D>> r_s, r_o;
+ for(uint64_t i{0u}; i < D; ++i){
+ r_s.at({{i}}).set(point.at({{i}}).get() - pos_s.at({{i}}).get());
+ r_o.at({{i}}).set(point.at({{i}}).get() - pos_o.at({{i}}).get());
+ }
+
+ auto v_s = impl::surface_velocity<T,D>(self,r_s,steps);
+ auto v_o = impl::surface_velocity<T,D>(other,r_o,steps);
+
+ kel::lbm::data<sch::Vector<T,D>> v_rel;
+ for(uint64_t i{0u}; i < D; ++i){
+ v_rel.at({{i}}).set(v_s.at({{i}}).get() - v_o.at({{i}}).get());
+ }
+
+ auto m_s = self.template get<"rigid_body">().template get<"mass">().at({}).get();
+ auto m_o = other.template get<"rigid_body">().template get<"mass">().at({}).get();
+ auto m_eff = (m_s * m_o) / (m_s + m_o);
+
+ return impl::soft_shell_response<T,D>(m,r_s,v_rel,m_eff,dem);
+}
+
+/**
+ * Contact response against a static wall.
+ *
+ * A wall is a partner of infinite mass and zero velocity, so the reduced mass
+ * collapses to the particle's own and the relative velocity is just the
+ * particle's surface velocity. The force law is otherwise identical -- the
+ * same soft shell.
+ */
+template<typename T, uint64_t D, typename PRef>
+contact_response<T,D> wall_contact_response(
+ const kel::lbm::data<sch::ContactManifold<T,D>>& m,
+ const PRef& self,
+ const dem_params<T>& dem,
+ kel::lbm::data<sch::UInt64> sub_steps
+){
+ auto steps = static_cast<typename kel::lbm::native_data_type<T>::type>(
+ particle_sub_steps(sub_steps));
+
+ auto& point = m.template get<"point">();
+ auto& pos_s = self.template get<"rigid_body">().template get<"position">();
+
+ kel::lbm::data<sch::Vector<T,D>> r_s;
+ for(uint64_t i{0u}; i < D; ++i){
+ r_s.at({{i}}).set(point.at({{i}}).get() - pos_s.at({{i}}).get());
+ }
+
+ auto v_rel = impl::surface_velocity<T,D>(self,r_s,steps);
+
+ auto m_s = self.template get<"rigid_body">().template get<"mass">().at({}).get();
+
+ return impl::soft_shell_response<T,D>(m,r_s,v_rel,m_s,dem);
+}
+
+/**
+ * Accumulates wall contacts for the axis-aligned domain box.
+ *
+ * One manifold per face the particle has entered; a particle in a corner gets
+ * a contribution from each axis, which is what makes it settle into the corner
+ * rather than squeeze through it.
+ */
+template<typename T, uint64_t D, typename PRef>
+void accumulate_box_wall_contacts(
+ PRef&& self,
+ const kel::lbm::data<sch::FixedArray<sch::UInt64,D>>& extent,
+ const dem_params<T>& dem,
+ kel::lbm::data<sch::UInt64> sub_steps
+){
+ auto& rb = self.template get<"rigid_body">();
+ auto& pos = rb.template get<"position">();
+ auto r = self.template get<"collision">().template get<"radius">().at({}).get();
+
+ for(uint64_t ax{0u}; ax < D; ++ax){
+ auto x = pos.at({{ax}}).get();
+ auto hi_bound = static_cast<typename kel::lbm::native_data_type<T>::type>(
+ extent.at({ax}).get());
+
+ // Low face, inward normal +e_ax; high face, inward normal -e_ax.
+ for(uint64_t side{0u}; side < 2u; ++side){
+ auto depth = (side == 0u) ? (r - x) : (x - (hi_bound - r));
+ if(!(depth > 0)){
+ continue;
+ }
+ if(depth > r){
+ depth = r;
+ }
+
+ kel::lbm::data<sch::ContactManifold<T,D>> m;
+ auto& n = m.template get<"normal">();
+ auto& point = m.template get<"point">();
+ for(uint64_t i{0u}; i < D; ++i){
+ n.at({{i}}).set(0);
+ point.at({{i}}).set(pos.at({{i}}).get());
+ }
+ n.at({{ax}}).set((side == 0u) ? 1.0 : -1.0);
+ point.at({{ax}}).set((side == 0u) ? 0.0 : hi_bound);
+ m.template get<"depth">().at({}).set(depth);
+
+ auto res = wall_contact_response<T,D>(m,self,dem,sub_steps);
+
+ auto& force = rb.template get<"force">();
+ for(uint64_t i{0u}; i < D; ++i){
+ force.at({{i}}) = force.at({{i}}) + res.force.at({{i}});
+ }
+ if constexpr ( particle_angular_supported<D> ){
+ auto& torque = rb.template get<"torque">();
+ torque = torque + res.torque;
+ }
+ }
+ }
+}
+
+/**
+ * Brute-force pair loop: accumulates every contact acting on one particle.
+ *
+ * Only `self` is written. The partner's half is computed by the partner's own
+ * work-item, which is what removes the need for atomics. The pair geometry is
+ * evaluated in canonical (lo,hi) order on both sides so the two halves are
+ * exactly equal and opposite.
+ */
+template<typename T, uint64_t D, typename PartsRef>
+void accumulate_particle_contacts(
+ PartsRef&& parts,
+ uint64_t self_index,
+ const dem_params<T>& dem,
+ kel::lbm::data<sch::UInt64> sub_steps
+){
+ auto& self = parts.at({self_index});
+
+ // get_dims() is the one spelling both the host array and the device Ptr view share.
+ const uint64_t count = parts.get_dims().at({0u}).get();
+
+ for(uint64_t j{0u}; j < count; ++j){
+ if(j == self_index){
+ continue;
+ }
+
+ auto& other = parts.at({j});
+
+ if(!(broadphase_collision_check<T,D,coll::Spheroid<T>>)(self,other)){
+ continue;
+ }
+
+ const bool self_is_lo = (self_index < j);
+ auto& lo = self_is_lo ? self : other;
+ auto& hi = self_is_lo ? other : self;
+
+ kel::lbm::data<sch::ContactManifold<T,D>> m;
+ if(!narrow_phase<coll::Spheroid<T>>::template contact<D>(lo,hi,m)){
+ continue;
+ }
+
+ auto res = particle_contact_response<T,D>(m,self,other,self_is_lo,dem,sub_steps);
+
+ auto& rb = self.template get<"rigid_body">();
+ auto& force = rb.template get<"force">();
+ for(uint64_t i{0u}; i < D; ++i){
+ force.at({{i}}) = force.at({{i}}) + res.force.at({{i}});
+ }
+ // Tied to the integrator: accumulating torque the integrator will not
+ // consume would be a silent no-op.
+ if constexpr ( particle_angular_supported<D> ){
+ auto& torque = rb.template get<"torque">();
+ torque = torque + res.torque;
+ }
+ }
+}
+
+namespace cmpt {
+/**
+ * Accumulates contact forces onto one particle. Runs between the fluid gather
+ * and the integrate.
+ */
+struct ParticleContact {};
+}
+
+template<typename T, typename Descriptor, typename Encode>
+class component<T, Descriptor, cmpt::ParticleContact, Encode> final {
+public:
+ using Component = cmpt::ParticleContact;
+
+private:
+ dem_params<T> dem_;
+ bool with_walls_;
+ kel::lbm::data<sch::FixedArray<sch::UInt64,Descriptor::D>> extent_;
+
+public:
+ component():
+ dem_{}, with_walls_{false}, extent_{}
+ {}
+
+ component(const dem_params<T>& dem__):
+ dem_{dem__}, with_walls_{false}, extent_{}
+ {}
+
+ component(
+ const dem_params<T>& dem__,
+ const kel::lbm::data<sch::FixedArray<sch::UInt64,Descriptor::D>>& extent__
+ ):
+ dem_{dem__}, with_walls_{true}, extent_{extent__}
+ {}
+
+ template<typename ParticleSchema>
+ void apply(
+ const kel::lbm::data<ParticleSchema,Encode>& pg,
+ kel::lbm::data<sch::FixedArray<sch::UInt64,1u>> index,
+ kel::lbm::data<sch::UInt64> time_step,
+ kel::lbm::data<sch::UInt64> sub_steps
+ ) const {
+ (void) time_step;
+
+ auto& parts = pg.template get<"particles">();
+
+ accumulate_particle_contacts<T,Descriptor::D>(
+ parts, index.at({{0u}}).get(), dem_, sub_steps);
+
+ if(with_walls_){
+ accumulate_box_wall_contacts<T,Descriptor::D>(
+ parts.at(index), extent_, dem_, sub_steps);
+ }
+ }
+};
+
+}
+}
diff --git a/modules/core/c++/particle/couple.hpp b/modules/core/c++/particle/couple.hpp
new file mode 100644
index 0000000..19a276c
--- /dev/null
+++ b/modules/core/c++/particle/couple.hpp
@@ -0,0 +1,108 @@
+#pragma once
+
+#include "../component.hpp"
+#include "common.hpp"
+#include "schema.hpp"
+#include "porosity.hpp"
+#include "cell_list.hpp"
+
+namespace kel {
+namespace lbm {
+
+/**
+ * Solid fraction at one lattice cell, summed over every particle covering it.
+ *
+ * This is the gather counterpart of the per-particle porosity stamp. The
+ * stamp *writes* macros.porosity from a per-particle work item, so two
+ * particles whose bounding boxes overlap race on the same cell -- and
+ * contacting particles always overlap. Here one work item owns one cell and
+ * writes only that cell, so there is nothing to race on and no atomic needed.
+ *
+ * Porosity composes as a product of the per-particle fluid fractions: a cell
+ * fully inside either body is fully solid, and partial overlaps multiply
+ * rather than clobbering one another. The per-particle version could not do
+ * this at all -- whichever particle ran last simply won.
+ */
+template<typename T, uint64_t D, uint64_t CellCapacity, typename PartsRef, typename ClRef, typename CellRef>
+kel::lbm::data<sch::Scalar<T>> cell_porosity(
+ const PartsRef& parts,
+ const ClRef& cl,
+ const CellRef& cell,
+ kel::lbm::data<sch::Scalar<T>> eps
+){
+ kel::lbm::data<sch::Scalar<T>> por;
+ por.at({}).set(1);
+
+ kel::lbm::data<sch::Vector<T,D>> cell_pos;
+ for(uint64_t i{0u}; i < D; ++i){
+ cell_pos.at({{i}}).set(
+ static_cast<typename kel::lbm::native_data_type<T>::type>(cell.at({i}).get())
+ );
+ }
+
+ for_each_bin_candidate<T,D,CellCapacity>(cl,cell,[&](uint64_t p){
+ auto& pi = parts.at({p});
+ auto& pos = pi.template get<"rigid_body">().template get<"position">();
+ auto rad = pi.template get<"collision">().template get<"radius">();
+
+ kel::lbm::data<sch::Vector<T,D>> rel;
+ for(uint64_t i{0u}; i < D; ++i){
+ rel.at({{i}}).set(cell_pos.at({{i}}).get() - pos.at({{i}}).get());
+ }
+
+ auto p_por = particle_porosity<T,D,1u,por::ParticleSpheroid<T>>::calculate(rel,rad,eps);
+
+ // 1 = fluid, 0 = solid, so fractions multiply.
+ por.at({}).set(por.at({}).get() * p_por.at({}).get());
+ });
+
+ return por;
+}
+
+namespace cmpt {
+/**
+ * Writes the particle coupling fields for one lattice cell.
+ *
+ * Replaces the per-particle scatter. Runs over the lattice, not the particle
+ * list, so it stays correct once particles start overlapping.
+ */
+template<uint64_t CellCapacity>
+struct ParticleFluidCouple {};
+}
+
+template<typename T, typename Descriptor, uint64_t CellCapacity, typename Encode>
+class component<T, Descriptor, cmpt::ParticleFluidCouple<CellCapacity>, Encode> final {
+public:
+ using Component = cmpt::ParticleFluidCouple<CellCapacity>;
+
+private:
+ kel::lbm::data<sch::Scalar<T>> eps_;
+
+public:
+ component(){
+ eps_.at({}).set(1.5);
+ }
+
+ component(const kel::lbm::data<sch::Scalar<T>>& eps__):
+ eps_{eps__}
+ {}
+
+ template<typename MacroFieldSchema, typename ParticleSchema, typename CellListSchema>
+ void apply(
+ const kel::lbm::data<MacroFieldSchema,Encode>& macros,
+ const kel::lbm::data<ParticleSchema,Encode>& pg,
+ const kel::lbm::data<CellListSchema,Encode>& cl,
+ kel::lbm::data<sch::FixedArray<sch::UInt64,Descriptor::D>> index,
+ kel::lbm::data<sch::UInt64> time_step
+ ) const {
+ (void) time_step;
+
+ auto& parts = pg.template get<"particles">();
+
+ macros.template get<"porosity">().at(index) =
+ cell_porosity<T,Descriptor::D,CellCapacity>(parts,cl,index,eps_);
+ }
+};
+
+}
+}
diff --git a/modules/core/c++/particle/integrate.hpp b/modules/core/c++/particle/integrate.hpp
new file mode 100644
index 0000000..f83465a
--- /dev/null
+++ b/modules/core/c++/particle/integrate.hpp
@@ -0,0 +1,175 @@
+#pragma once
+
+#include "../component.hpp"
+#include "common.hpp"
+#include "schema.hpp"
+#include "particle.hpp"
+
+namespace kel {
+namespace lbm {
+/**
+ * Zero sub steps means "advance once", not "do not advance".
+ *
+ * Taking it literally gave ts = 1/0 = inf and a loop that never ran, which
+ * silently froze the particle. Several step functions pass {0u}.
+ */
+inline uint64_t particle_sub_steps(kel::lbm::data<sch::UInt64> sub_steps){
+ uint64_t steps = sub_steps.get();
+ return (steps == 0u) ? 1u : steps;
+}
+/**
+ * Whether rotation is integrated at all for a given dimension.
+ *
+ * True in 2D, where the scalar moment of inertia is exact about the single
+ * out-of-plane axis and verlet_step_lambda's vector update is the correct
+ * integrator.
+ *
+ * False in 3D, and deliberately so rather than approximately right. Two
+ * separate things are missing there: a scalar inertia only describes an
+ * isotropic body, and integrating the rotation vector as if it were a
+ * displacement cannot represent non-commuting composition -- the gyroscopic
+ * term omega x (I omega) is absent entirely. Shipping that would give plausible
+ * looking but wrong 3D spin, which is worse than none. Callers can branch on
+ * this; a correct 3D path wants a quaternion and an inertia tensor.
+ */
+template<uint64_t D>
+constexpr bool particle_angular_supported = (D == 2u);
+
+
+/**
+ * Turns the accumulated force/torque into acceleration and advances the body.
+ *
+ * A free function rather than only a component so it is callable from a host
+ * test, and so a future stiff-contact scheme can call it from inside its own
+ * sub-step loop.
+ */
+template<typename T, uint64_t D>
+void integrate_particle(
+ kel::lbm::data<sch::Particle<T,D>>& pi,
+ const kel::lbm::data<sch::Vector<T,D>>& body_acceleration,
+ kel::lbm::data<sch::UInt64> sub_steps
+){
+ auto& pirb = pi.template get<"rigid_body">();
+
+ const uint64_t steps = particle_sub_steps(sub_steps);
+
+ kel::lbm::data<sch::Scalar<T>> ts;
+ ts.at({}).set(
+ typename kel::lbm::native_data_type<T>::type(1)
+ / static_cast<typename kel::lbm::native_data_type<T>::type>(steps)
+ );
+
+ pirb.template get<"acceleration">() =
+ pirb.template get<"force">() / pirb.template get<"mass">()
+ + body_acceleration;
+
+ // See particle_angular_supported for why 3D is excluded rather than
+ // approximated.
+ if constexpr ( particle_angular_supported<D> ){
+ pirb.template get<"angular_acceleration">() =
+ pirb.template get<"torque">() / pirb.template get<"inertia">();
+ }
+
+ for(uint64_t i{0u}; i < steps; ++i){
+ verlet_step_lambda<T,D>(pi,ts);
+ }
+}
+
+/**
+ * Clears the force/torque accumulators. Must run before any contributor.
+ */
+template<typename T, uint64_t D>
+void reset_particle_accumulators(kel::lbm::data<sch::Particle<T,D>>& pi){
+ auto& pirb = pi.template get<"rigid_body">();
+
+ auto& force = pirb.template get<"force">();
+ for(uint64_t i{0u}; i < D; ++i){
+ force.at({{i}}).set(0);
+ }
+
+ auto& torque = pirb.template get<"torque">();
+ if constexpr ( D == 2u ){
+ torque.at({}).set(0);
+ }else{
+ for(uint64_t i{0u}; i < 3u; ++i){
+ torque.at({{i}}).set(0);
+ }
+ }
+}
+
+namespace cmpt {
+/**
+ * Clears the per-particle force/torque accumulators.
+ *
+ * Must run before any contributor. Separate from integration because
+ * contributors (fluid coupling, contact, body forces) sit between the two.
+ */
+struct ParticleForceReset {};
+
+/**
+ * Turns the accumulated force/torque into acceleration and advances the body.
+ *
+ * This is the single place that writes acceleration, which is what lets the
+ * contributors merely accumulate. Previously each fluid component assigned
+ * acceleration directly, so a second contributor silently overwrote the first.
+ */
+struct ParticleIntegrate {};
+}
+
+template<typename T, typename Descriptor, typename Encode>
+class component<T, Descriptor, cmpt::ParticleForceReset, Encode> final {
+public:
+ using Component = cmpt::ParticleForceReset;
+
+ component() = default;
+
+ template<typename ParticleSchema>
+ void apply(
+ const kel::lbm::data<ParticleSchema,Encode>& pg,
+ kel::lbm::data<sch::FixedArray<sch::UInt64,1u>> index,
+ kel::lbm::data<sch::UInt64> time_step
+ ) const {
+ (void) time_step;
+
+ auto& parts = pg.template get<"particles">();
+ reset_particle_accumulators<T,Descriptor::D>(parts.at(index));
+ }
+};
+
+template<typename T, typename Descriptor, typename Encode>
+class component<T, Descriptor, cmpt::ParticleIntegrate, Encode> final {
+public:
+ using Component = cmpt::ParticleIntegrate;
+
+private:
+ /**
+ * Body acceleration applied to every particle, e.g. gravity. Kept as an
+ * acceleration rather than a force so it does not need the mass.
+ */
+ kel::lbm::data<sch::Vector<T,Descriptor::D>> body_acceleration_;
+
+public:
+ component():
+ body_acceleration_{}
+ {}
+
+ component(const kel::lbm::data<sch::Vector<T,Descriptor::D>>& body_acceleration__):
+ body_acceleration_{body_acceleration__}
+ {}
+
+ template<typename ParticleSchema>
+ void apply(
+ const kel::lbm::data<ParticleSchema,Encode>& pg,
+ kel::lbm::data<sch::FixedArray<sch::UInt64,1u>> index,
+ kel::lbm::data<sch::UInt64> time_step,
+ kel::lbm::data<sch::UInt64> sub_steps
+ ) const {
+ (void) time_step;
+
+ auto& parts = pg.template get<"particles">();
+ integrate_particle<T,Descriptor::D>(parts.at(index), body_acceleration_, sub_steps);
+ }
+};
+
+}
+}
diff --git a/modules/core/c++/particle/mask.hpp b/modules/core/c++/particle/mask.hpp
new file mode 100644
index 0000000..246c31f
--- /dev/null
+++ b/modules/core/c++/particle/mask.hpp
@@ -0,0 +1,399 @@
+#pragma once
+
+#include <cmath>
+
+#include "../abstract/math.hpp"
+#include "../iterator.hpp"
+#include "common.hpp"
+#include "schema.hpp"
+
+namespace kel {
+namespace lbm {
+
+/**
+ * Voxel shape convention
+ * ======================
+ *
+ * The group's mask is a D-cube of R voxels per axis holding a SIGNED DISTANCE
+ * in NORMALIZED PARTICLE-LOCAL units, where the shape's bounding radius is
+ * exactly 1. So the mask spans local [-1,1]^D and
+ *
+ * step = 2 / R
+ * u_local(k) = -1 + (k + 0.5) * step (cell centred)
+ * k(u_local) = (u_local + 1) / step - 0.5
+ *
+ * Normalized rather than lattice units because radius is now a per-particle
+ * property: one group mask has to serve particles of different sizes, and the
+ * physical radius is then exactly `scale`. That also makes the bounding-sphere
+ * broad phase agree with the voxel narrow phase by construction -- spheres
+ * disjoint implies voxels disjoint -- instead of by argument.
+ *
+ * Local to global, and its inverse, which is what the narrow phase needs:
+ *
+ * x_global = pos + scale * Rot(theta) * (u_local - com)
+ * u_local = Rot(-theta) * (x_global - pos) / scale + com
+ *
+ * Signed distance rather than a 0/1 indicator because the indicator's gradient
+ * is a one-cell delta quantized to lattice directions, which cannot give a
+ * usable contact normal. From an SDF the normal is grad(phi) and the
+ * penetration is -phi directly, and a smooth solid fraction is still one cheap
+ * ramp away. Switching later would mean rebuilding every mask, so it is
+ * decided here.
+ *
+ * Sign: negative inside the body, positive outside. Solid fraction is derived,
+ * and porosity stays 1 = fluid / 0 = solid to match the analytic path.
+ */
+
+namespace impl {
+/**
+ * Rotates a local offset into the global frame.
+ *
+ * 2D rotation is an angle; 3D is a rotation vector (axis * angle) applied via
+ * Rodrigues, which matches how verlet_step_lambda integrates it.
+ */
+template<typename T, uint64_t D, typename RotRef>
+kel::lbm::data<sch::Vector<T,D>> mask_rotate(
+ const kel::lbm::data<sch::Vector<T,D>>& v,
+ const RotRef& rot,
+ bool inverse
+){
+ if constexpr ( D == 2u ){
+ auto a = rot.at({}).get();
+ if(inverse){
+ a = -a;
+ }
+ auto c = std::cos(a);
+ auto s = std::sin(a);
+
+ kel::lbm::data<sch::Vector<T,D>> out;
+ out.at({{0u}}).set(v.at({{0u}}).get() * c - v.at({{1u}}).get() * s);
+ out.at({{1u}}).set(v.at({{0u}}).get() * s + v.at({{1u}}).get() * c);
+ return out;
+ }else{
+ kel::lbm::data<sch::Vector<T,3u>> r;
+ for(uint64_t i{0u}; i < 3u; ++i){
+ r.at({{i}}).set(rot.at({{i}}).get());
+ }
+
+ auto theta_2 = kel::lbm::math::dot(r,r).at({}).get();
+ if(!(theta_2 > 0)){
+ return v;
+ }
+ auto theta = std::sqrt(theta_2);
+ if(inverse){
+ theta = -theta;
+ }
+
+ kel::lbm::data<sch::Vector<T,3u>> n;
+ for(uint64_t i{0u}; i < 3u; ++i){
+ n.at({{i}}).set(r.at({{i}}).get() / std::sqrt(theta_2));
+ }
+
+ auto c = std::cos(theta);
+ auto s = std::sin(theta);
+ auto n_dot_v = kel::lbm::math::dot(n,v).at({}).get();
+ auto cross = kel::lbm::math::cross<T>(n,v);
+
+ kel::lbm::data<sch::Vector<T,D>> out;
+ for(uint64_t i{0u}; i < D; ++i){
+ out.at({{i}}).set(
+ v.at({{i}}).get() * c
+ + cross.at({{i}}).get() * s
+ + n.at({{i}}).get() * n_dot_v * (1 - c)
+ );
+ }
+ return out;
+ }
+}
+}
+
+/**
+ * Voxel index -> normalized local position (cell centred).
+ */
+template<typename T, uint64_t D, typename IndexRef>
+kel::lbm::data<sch::Vector<T,D>> mask_voxel_to_local(
+ const IndexRef& k,
+ uint64_t resolution
+){
+ auto step = typename kel::lbm::native_data_type<T>::type(2)
+ / static_cast<typename kel::lbm::native_data_type<T>::type>(resolution);
+
+ kel::lbm::data<sch::Vector<T,D>> u;
+ for(uint64_t i{0u}; i < D; ++i){
+ auto ki = static_cast<typename kel::lbm::native_data_type<T>::type>(k.at({i}).get());
+ u.at({{i}}).set(-1 + (ki + 0.5) * step);
+ }
+ return u;
+}
+
+/**
+ * Normalized local -> global lattice position.
+ */
+template<typename T, uint64_t D, typename PRef, typename ComRef>
+kel::lbm::data<sch::Vector<T,D>> mask_local_to_global(
+ const kel::lbm::data<sch::Vector<T,D>>& u,
+ const PRef& p,
+ const ComRef& com
+){
+ auto& rb = p.template get<"rigid_body">();
+ auto& pos = rb.template get<"position">();
+ auto scale = p.template get<"collision">().template get<"radius">().at({}).get();
+
+ kel::lbm::data<sch::Vector<T,D>> rel;
+ for(uint64_t i{0u}; i < D; ++i){
+ rel.at({{i}}).set(u.at({{i}}).get() - com.at({{i}}).get());
+ }
+
+ auto rot = impl::mask_rotate<T,D>(rel, rb.template get<"rotation">(), false);
+
+ kel::lbm::data<sch::Vector<T,D>> x;
+ for(uint64_t i{0u}; i < D; ++i){
+ x.at({{i}}).set(pos.at({{i}}).get() + scale * rot.at({{i}}).get());
+ }
+ return x;
+}
+
+/**
+ * Global lattice position -> normalized local. The inverse of the above, and
+ * what the narrow phase and the porosity gather both go through.
+ */
+template<typename T, uint64_t D, typename PRef, typename ComRef>
+kel::lbm::data<sch::Vector<T,D>> mask_global_to_local(
+ const kel::lbm::data<sch::Vector<T,D>>& x,
+ const PRef& p,
+ const ComRef& com
+){
+ auto& rb = p.template get<"rigid_body">();
+ auto& pos = rb.template get<"position">();
+ auto scale = p.template get<"collision">().template get<"radius">().at({}).get();
+
+ kel::lbm::data<sch::Vector<T,D>> rel;
+ for(uint64_t i{0u}; i < D; ++i){
+ rel.at({{i}}).set((x.at({{i}}).get() - pos.at({{i}}).get()) / scale);
+ }
+
+ auto unrot = impl::mask_rotate<T,D>(rel, rb.template get<"rotation">(), true);
+
+ kel::lbm::data<sch::Vector<T,D>> u;
+ for(uint64_t i{0u}; i < D; ++i){
+ u.at({{i}}).set(unrot.at({{i}}).get() + com.at({{i}}).get());
+ }
+ return u;
+}
+
+/**
+ * Samples the stored signed distance at a normalized local position, with
+ * n-linear interpolation.
+ *
+ * Interpolated rather than nearest so the field stays continuous as a particle
+ * slides across the lattice; a nearest lookup aliases whenever the voxel pitch
+ * does not divide the lattice, and leaves grad(phi) piecewise constant, which
+ * the contact normal cannot use.
+ *
+ * Outside the mask box the distance is extrapolated from the box corner, which
+ * is monotone and good enough -- nothing in contact range is ever out there.
+ */
+template<typename T, uint64_t D, typename MaskRef>
+typename kel::lbm::native_data_type<T>::type mask_sample_local(
+ const MaskRef& mask,
+ const kel::lbm::data<sch::Vector<T,D>>& u,
+ uint64_t resolution
+){
+ using NT = typename kel::lbm::native_data_type<T>::type;
+
+ auto step = NT(2) / static_cast<NT>(resolution);
+
+ NT frac[D];
+ int64_t base[D];
+ NT outside = 0;
+
+ for(uint64_t i{0u}; i < D; ++i){
+ auto g = (u.at({{i}}).get() + 1) / step - NT(0.5);
+
+ // Track how far the query sits beyond the stored box.
+ if(g < 0){
+ outside += g * g * step * step;
+ g = 0;
+ }else if(g > static_cast<NT>(resolution - 1u)){
+ auto over = g - static_cast<NT>(resolution - 1u);
+ outside += over * over * step * step;
+ g = static_cast<NT>(resolution - 1u);
+ }
+
+ auto f = std::floor(g);
+ base[i] = static_cast<int64_t>(f);
+ if(base[i] > static_cast<int64_t>(resolution) - 2){
+ base[i] = static_cast<int64_t>(resolution) - 2;
+ if(base[i] < 0){
+ base[i] = 0;
+ }
+ }
+ frac[i] = g - static_cast<NT>(base[i]);
+ }
+
+ // n-linear blend over the 2^D surrounding voxels.
+ uint64_t corners = 1u;
+ for(uint64_t i{0u}; i < D; ++i){
+ corners *= 2u;
+ }
+
+ NT acc = 0;
+ for(uint64_t c{0u}; c < corners; ++c){
+ NT w = 1;
+ kel::lbm::data<sch::FixedArray<sch::UInt64,D>> k;
+
+ for(uint64_t i{0u}; i < D; ++i){
+ const bool hi = ((c >> i) & 1u) != 0u;
+ auto idx = base[i] + (hi ? 1 : 0);
+ if(idx < 0){
+ idx = 0;
+ }
+ if(idx > static_cast<int64_t>(resolution) - 1){
+ idx = static_cast<int64_t>(resolution) - 1;
+ }
+ k.at({i}).set(static_cast<uint64_t>(idx));
+ w *= hi ? frac[i] : (1 - frac[i]);
+ }
+
+ acc += w * mask.at(k).get();
+ }
+
+ if(outside > 0){
+ acc += std::sqrt(outside);
+ }
+
+ return acc;
+}
+
+/**
+ * Signed distance from a global lattice position to the body, in LATTICE
+ * units. Negative inside.
+ */
+template<typename T, uint64_t D, typename GroupRef, typename PRef>
+typename kel::lbm::native_data_type<T>::type mask_signed_distance(
+ const GroupRef& pg,
+ const PRef& p,
+ const kel::lbm::data<sch::Vector<T,D>>& x,
+ uint64_t resolution
+){
+ auto& com = pg.template get<"center_of_mass">().at({0u});
+ auto u = mask_global_to_local<T,D>(x,p,com);
+
+ auto scale = p.template get<"collision">().template get<"radius">().at({}).get();
+ return mask_sample_local<T,D>(pg.template get<"mask">(),u,resolution) * scale;
+}
+
+/**
+ * Smooth solid fraction: 1 fully inside, 0 fully outside, with a cos^2 ramp of
+ * width `eps` lattice units straddling the surface.
+ *
+ * Derived from the signed distance rather than stored, so one field serves
+ * both the fluid coupling and the contact normal.
+ */
+template<typename T>
+typename kel::lbm::native_data_type<T>::type solid_fraction_from_sdf(
+ typename kel::lbm::native_data_type<T>::type sdf,
+ typename kel::lbm::native_data_type<T>::type eps
+){
+ using NT = typename kel::lbm::native_data_type<T>::type;
+
+ auto half = eps / 2;
+ if(sdf <= -half){
+ return 1;
+ }
+ if(sdf >= half){
+ return 0;
+ }
+
+ // t runs 0 at the inner edge to 1 at the outer; cos^2 gives a smooth
+ // 1 -> 0 with zero slope at both ends.
+ auto t = (sdf + half) / eps;
+ auto c = std::cos(NT(3.14159265358979323846) * t / 2);
+ return c * c;
+}
+
+/**
+ * Signed distance functions in normalized local units, radius 1.
+ */
+namespace sdf {
+template<typename T, uint64_t D>
+typename kel::lbm::native_data_type<T>::type sphere(
+ const kel::lbm::data<sch::Vector<T,D>>& u
+){
+ return std::sqrt(kel::lbm::math::dot(u,u).at({}).get()) - 1;
+}
+
+/**
+ * Chebyshev distance: the inscribed-box shape whose bounding radius is 1 along
+ * each axis.
+ */
+template<typename T, uint64_t D>
+typename kel::lbm::native_data_type<T>::type cube(
+ const kel::lbm::data<sch::Vector<T,D>>& u
+){
+ typename kel::lbm::native_data_type<T>::type m = 0;
+ for(uint64_t i{0u}; i < D; ++i){
+ auto a = std::abs(u.at({{i}}).get());
+ if(a > m){
+ m = a;
+ }
+ }
+ return m - 1;
+}
+}
+
+/**
+ * Fills a group's mask from a signed distance function, and returns the volume
+ * centroid in normalized local units.
+ *
+ * The centroid is weighted by solid fraction, so it is the mass centre of the
+ * voxelized body rather than the centre of its bounding box -- they differ for
+ * anything asymmetric, and the physics wants the former.
+ */
+template<typename T, uint64_t D, typename MaskRef, typename Func>
+kel::lbm::data<sch::Vector<T,D>> fill_mask_from_sdf(
+ MaskRef&& mask,
+ uint64_t resolution,
+ Func&& sdf_fn
+){
+ using NT = typename kel::lbm::native_data_type<T>::type;
+
+ kel::lbm::data<sch::FixedArray<sch::UInt64,D>> dims;
+ for(uint64_t i{0u}; i < D; ++i){
+ dims.at({i}).set(resolution);
+ }
+
+ kel::lbm::data<sch::Vector<T,D>> com;
+ for(uint64_t i{0u}; i < D; ++i){
+ com.at({{i}}).set(0);
+ }
+
+ NT weight_sum = 0;
+ auto step = NT(2) / static_cast<NT>(resolution);
+
+ iterator<D>::apply([&](const auto& k){
+ auto u = mask_voxel_to_local<T,D>(k,resolution);
+ auto d = sdf_fn(u);
+
+ mask.at(k).set(d);
+
+ // Ramp one voxel wide, purely so partially filled cells near the
+ // surface weight the centroid sensibly.
+ auto w = solid_fraction_from_sdf<T>(d,step);
+ weight_sum += w;
+ for(uint64_t i{0u}; i < D; ++i){
+ com.at({{i}}).set(com.at({{i}}).get() + w * u.at({{i}}).get());
+ }
+ },{},dims);
+
+ if(weight_sum > 0){
+ for(uint64_t i{0u}; i < D; ++i){
+ com.at({{i}}).set(com.at({{i}}).get() / weight_sum);
+ }
+ }
+
+ return com;
+}
+
+}
+}
diff --git a/modules/core/c++/particle/particle.hpp b/modules/core/c++/particle/particle.hpp
index 2b2ceac..d9b5444 100644
--- a/modules/core/c++/particle/particle.hpp
+++ b/modules/core/c++/particle/particle.hpp
@@ -8,31 +8,35 @@
#include "schema.hpp"
#include "aabb.hpp"
+#include "mask.hpp"
+#include "../abstract/data_ptr.hpp"
#include "particle_opa.hpp"
namespace kel {
namespace lbm {
+/**
+ * Builds a group of identical spheroids.
+ *
+ * The signature is unchanged from when radius/density/mass lived on the group:
+ * the three scalars are fanned out to every particle here, so no caller moves.
+ */
template<typename T, uint64_t D,uint64_t PartAmount>
kel::lbm::data<sch::ParticleGroup<T, D, PartAmount, coll::Spheroid<T>>> create_spheroid_particle_group(
kel::lbm::data<sch::Scalar<T>> radius_p,
kel::lbm::data<sch::Scalar<T>> density_p,
const kel::lbm::data<sch::UInt64>& mask_resolution
){
+ static_assert(D >= 1u and D <= 3u, "Dimensions only supported for Dim 1,2 & 3.");
+ static_assert(PartAmount > 0u, "A group needs at least one particle");
+
kel::lbm::data<sch::ParticleGroup<T,D,PartAmount,coll::Spheroid<T>>> part;
+ // The group keeps the shape prototype; the particles carry their own copy.
auto& rad_s = part.template get<"collision">().at({0u}).template get<"radius">();
rad_s = radius_p;
auto& mask = part.template get<"mask">();
- auto& density = part.template get<"density">().at({{0u}});
-
- auto& total_mass = part.template get<"total_mass">().at({{0u}});
- // Paranoia
- total_mass.at({}) = {};
-
- static_assert(D >= 1u and D <= 3u, "Dimensions only supported for Dim 1,2 & 3.");
- density = density_p;
kel::lbm::data<sch::FixedArray<sch::UInt64,D>> mask_dims;
for(uint64_t i = 0u; i < D; ++i){
@@ -43,53 +47,76 @@ kel::lbm::data<sch::ParticleGroup<T, D, PartAmount, coll::Spheroid<T>>> create_s
mask = {mask_dims};
+ /**
+ * mask_step stays in LATTICE units for callers that reason in them, but
+ * the mask itself is normalized to radius 1 so one group shape serves
+ * particles of any scale. See particle/mask.hpp for the full convention.
+ */
auto& mask_step = part.template get<"mask_step">().at({{0u}});
mask_step.at({}) = dia_d / mask_resolution.template cast_to<T>();
+ /**
+ * Fill the mask with the signed distance of a unit sphere.
+ *
+ * The loop that used to be here computed an offset and a radius-squared
+ * and discarded both without ever writing a voxel, so every mask was
+ * silently all zeros and the centre of mass came out as 0/0.
+ */
auto& com = part.template get<"center_of_mass">().at({{0u}});
- // Paranoia
- for(uint64_t i = 0u; i < D; ++i){
- com.at({{i}}) = {};
- }
- kel::lbm::data<sch::UInt64> ele_ctr{0u};
-
- // Radius ^ 2
- kel::lbm::data<sch::Scalar<T>> rad_2_d;
- rad_2_d.at({}) = rad_d * rad_d;
-
- kel::lbm::data<sch::Vector<T,D>> center;
- for(uint64_t i = 0u; i < D; ++i){
- center.at({{i}}) = rad_d;
- }
-
- iterator<D>::apply([&](const auto& index){
- ++ele_ctr;
-
- kel::lbm::data<sch::Vector<T,D>> offset_index = kel::lbm::math::vectorize_data(index).template cast_to<T>() - center;
+ com = fill_mask_from_sdf<T,D>(
+ mask,
+ mask_resolution.get(),
+ [](const kel::lbm::data<sch::Vector<T,D>>& u){
+ return sdf::sphere<T,D>(u);
+ }
+ );
- auto& dpi = mask.at(index);
+ /**
+ * Analytic mass and inertia for a uniform spheroid.
+ *
+ * Taken from the closed form rather than summed off the mask: the mask is
+ * a signed distance, so a mass integral would need the ramp applied first
+ * and would only approximate what the closed form gives exactly.
+ *
+ * The 3D branch used to be empty, leaving total_mass at 0 and then
+ * dividing the centre of mass by it.
+ */
+ kel::lbm::data<sch::Scalar<T>> mass_p;
+ kel::lbm::data<sch::Scalar<T>> inertia_p;
+ {
+ constexpr typename kel::lbm::native_data_type<T>::type pi = 3.14159265358979323846;
+ auto r = rad_d.get();
+ auto rho = density_p.at({}).get();
- for(uint64_t i = 0u; i < D; ++i){
- com.at({{i}}) = com.at({{i}}) + index.at({i}).template cast_to<T>() * dpi;
+ if constexpr ( D == 1u ) {
+ mass_p.at({}).set(2.0 * r * rho);
+ // A rod has no out-of-line rotation to speak of.
+ inertia_p.at({}).set(0.0);
+ } else if constexpr ( D == 2u ) {
+ auto m = pi * r * r * rho;
+ mass_p.at({}).set(m);
+ // Disc about its centre: m r^2 / 2
+ inertia_p.at({}).set(0.5 * m * r * r);
+ } else {
+ auto m = (4.0 / 3.0) * pi * r * r * r * rho;
+ mass_p.at({}).set(m);
+ // Solid sphere: 2 m r^2 / 5
+ inertia_p.at({}).set(0.4 * m * r * r);
}
+ }
- total_mass.at({}) = total_mass.at({}) + dpi;
- },{},mask_dims);
+ auto& parts = part.template get<"particles">();
+ for(uint64_t p_i = 0u; p_i < PartAmount; ++p_i){
+ auto& pi = parts.at({p_i});
+ pi.template get<"collision">().template get<"radius">() = radius_p;
- // TODO ? Do mass properly
- if constexpr ( D == 2u ) {
- total_mass.at({}) = rad_d * rad_d * density.at({}) * 3.141592;
- }else if constexpr ( D == 3u ){
-
- }else if constexpr ( D == 1u ){
- total_mass.at({}) = rad_d * 2.0;
+ auto& rb = pi.template get<"rigid_body">();
+ rb.template get<"mass">() = mass_p;
+ rb.template get<"inertia">() = inertia_p;
+ rb.template get<"density">() = density_p;
}
- std::cout<<"Total Mass: "<<total_mass.at({}).get()<<std::endl;
- for(uint64_t i = 0u; i < D; ++i){
- com.at({{i}}) = com.at({{i}}) / total_mass.at({});
- }
return part;
}
@@ -206,22 +233,26 @@ constexpr auto handle_collision = [](
};
-template<typename T, uint64_t D, typename Collision>
-constexpr auto broadphase_collision_distance_squared = [](
- kel::lbm::data<sch::Particle<T,D>>& left,
- const kel::lbm::data<Collision>& coll_l,
- kel::lbm::data<sch::Particle<T,D>>& right,
- const kel::lbm::data<Collision>& coll_r
+/**
+ * Bounding-sphere overlap test.
+ *
+ * Radius lives on the particle now, so the collision shapes no longer have to
+ * be threaded through separately.
+ *
+ * Returns the squared centre distance alongside the verdict so a caller that
+ * goes on to a narrow phase does not recompute it.
+ */
+template<typename T, uint64_t D, typename Collision = coll::Spheroid<T>>
+constexpr auto broadphase_collision_distance_squared = []<typename PRef>(
+ const PRef& left,
+ const PRef& right
) -> std::pair<bool,kel::lbm::data<sch::Scalar<T>>>{
- auto rad_l = coll_l.template get<"radius">();
- auto rad_r = coll_r.template get<"radius">();
+ auto rad_l = left.template get<"collision">().template get<"radius">();
+ auto rad_r = right.template get<"collision">().template get<"radius">();
- auto& rb_l = left.template get<"rigid_body">();
- auto& rb_r = right.template get<"rigid_body">();
-
- auto& pos_l = rb_l.template get<"position">();
- auto& pos_r = rb_r.template get<"position">();
+ auto& pos_l = left.template get<"rigid_body">().template get<"position">();
+ auto& pos_r = right.template get<"rigid_body">().template get<"position">();
auto pos_dist = pos_l - pos_r;
@@ -229,22 +260,18 @@ constexpr auto broadphase_collision_distance_squared = [](
kel::lbm::data<sch::Scalar<T>> two;
two.at({}) = 2.0;
+ // (r_l + r_r)^2
auto rad_ab_2 = rad_l * rad_l + rad_r * rad_r + rad_r * rad_l * two;
return std::make_pair((norm_2.at({}).get() < rad_ab_2.at({}).get()), norm_2);
};
-/**
-*
-*
-*/
-template<typename T, uint64_t D, typename Collision>
-constexpr auto broadphase_collision_check = [](
- kel::lbm::data<sch::Particle<T,D>>& left,
- const kel::lbm::data<Collision>& coll_l,
- kel::lbm::data<sch::Particle<T,D>>& right,
- const kel::lbm::data<Collision>& coll_r
+
+template<typename T, uint64_t D, typename Collision = coll::Spheroid<T>>
+constexpr auto broadphase_collision_check = []<typename PRef>(
+ const PRef& left,
+ const PRef& right
) -> bool{
- return broadphase_collision_distance_squared<T,D,Collision>(left,coll_l,right,coll_r).first;
+ return broadphase_collision_distance_squared<T,D,Collision>(left,right).first;
};
template<typename T,uint64_t D, uint64_t PartAmount>
diff --git a/modules/core/c++/particle/porosity.hpp b/modules/core/c++/particle/porosity.hpp
index 0fe630d..593b15e 100644
--- a/modules/core/c++/particle/porosity.hpp
+++ b/modules/core/c++/particle/porosity.hpp
@@ -3,32 +3,53 @@
#include "common.hpp"
#include "schema.hpp"
#include "../math/n_closest.hpp"
+#include "mask.hpp"
namespace kel {
namespace lbm {
namespace por {
template<typename T>
struct ParticleSpheroid {};
+
+/**
+ * Porosity read off the group's voxel mask rather than an analytic sphere.
+ *
+ * Shape agnostic: anything that can be voxelized participates, and the fluid
+ * then sees exactly the body the contact resolution uses, because both go
+ * through the same signed distance field.
+ */
+template<typename T>
+struct ParticleVoxel {};
}
+/**
+ * Mask-based porosity.
+ *
+ * Replaces a stub that computed a distance and then returned a
+ * default-constructed value, so every caller silently got zero.
+ */
template<typename T, uint64_t D, uint64_t PC, typename Coll>
class particle_porosity {
public:
-
- static kel::lbm::data<sch::Scalar<T>> calculate(const kel::lbm::data<sch::ParticleGroup<T,D,PC,Coll>>& part_group, uint64_t p_i, const kel::lbm::data<sch::Vector<T,D>>& lbm_pos){
+ template<typename GroupRef>
+ static kel::lbm::data<sch::Scalar<T>> calculate(
+ const GroupRef& part_group,
+ uint64_t p_i,
+ const kel::lbm::data<sch::Vector<T,D>>& lbm_pos,
+ kel::lbm::data<sch::Scalar<T>> eps
+ ){
auto& mask = part_group.template get<"mask">();
+ auto& part_i = part_group.template get<"particles">().at({p_i});
- auto& particles = part_group.template get<"particles">();
- auto& part_i = particles.at({p_i});
-
- auto& part_i_rb = part_i.template get<"rigid_body">();
- auto& pirb = part_i_rb.template get<"position">();
+ const uint64_t resolution = mask.get_dims().at({0u}).get();
- auto dist = lbm_pos - pirb;
+ auto sdf = mask_signed_distance<T,D>(part_group,part_i,lbm_pos,resolution);
+ auto solid = solid_fraction_from_sdf<T>(sdf,eps.at({}).get());
- // index 0 is at
-
- return {};
+ // Convention: 1 = fluid, 0 = solid, matching the analytic path.
+ kel::lbm::data<sch::Scalar<T>> por;
+ por.at({}).set(1 - solid);
+ return por;
}
};
@@ -85,8 +106,7 @@ public:
kel::lbm::data<sch::Scalar<T>> dist_len = kel::lbm::math::dot(dist,dist);
dist_len.at({}).set(std::sqrt(dist_len.at({}).get()));
-
- auto& coll = part_group.template get<"collision">().at({});
+ auto& coll = pi.template get<"collision">();
const kel::lbm::data<sch::Scalar<T>>& rad_d = coll.template get<"radius">();
const auto& eps = part_group.template get<"epsilon">().at({});
// Move this somewhere
diff --git a/modules/core/c++/particle/schema.hpp b/modules/core/c++/particle/schema.hpp
index 40033cd..fe7a33c 100644
--- a/modules/core/c++/particle/schema.hpp
+++ b/modules/core/c++/particle/schema.hpp
@@ -30,6 +30,19 @@ template<typename T>
struct rotation_type_helper<T,3u> {
using Schema = Vector<T,3u>;
};
+
+/**
+ * Moment of inertia.
+ *
+ * A scalar is exact in 2D, where rotation is about the single out-of-plane
+ * axis. In 3D it is only correct for isotropic bodies -- anything else needs
+ * a body-frame tensor plus the gyroscopic term. The angular DEM path
+ * static_asserts on D == 2u so 3D fails loudly rather than silently wrong.
+ */
+template<typename T,uint64_t D>
+struct inertia_type_helper {
+ using Schema = Scalar<T>;
+};
}
template<typename T, uint64_t D>
@@ -40,27 +53,52 @@ using ParticleRigidBody = Struct<
Member<typename impl::rotation_type_helper<T,D>::Schema, "rotation_old">,
Member<Vector<T,D>, "acceleration">,
- Member<typename impl::rotation_type_helper<T,D>::Schema, "angular_acceleration">
+ Member<typename impl::rotation_type_helper<T,D>::Schema, "angular_acceleration">,
+
+ /**
+ * Accumulators, zeroed at the start of every step.
+ *
+ * Every contributor -- fluid coupling, particle contact, body forces --
+ * adds into these; a single integrate pass then turns them into
+ * acceleration. Writing acceleration directly, as the fluid components
+ * used to, means the last writer silently wins.
+ */
+ Member<Vector<T,D>, "force">,
+ Member<typename impl::rotation_type_helper<T,D>::Schema, "torque">,
+
+ /**
+ * Inertial properties, per particle rather than per group, so a group can
+ * hold particles of differing size and density.
+ */
+ Member<Scalar<T>, "mass">,
+ Member<typename impl::inertia_type_helper<T,D>::Schema, "inertia">,
+ Member<Scalar<T>, "density">
>;
-template<typename T, uint64_t D>
+/**
+ * CollisionType is defaulted so the plain `Particle<T,D>` spelling used by the
+ * integrator and the tests keeps working.
+ */
+template<typename T, uint64_t D, typename CollisionType = coll::Spheroid<T>>
using Particle = Struct<
- Member<ParticleRigidBody<T,D>, "rigid_body">
- // Problem is that dynamic data would two layered
- // Member<Array<Float64,D>, "mask">,
+ Member<ParticleRigidBody<T,D>, "rigid_body">,
+ Member<typename CollisionType::Schema, "collision">
>;
+/**
+ * A group is one shape shared by many particles: the voxel mask and its
+ * sampling parameters describe the shape, the particles carry the transform
+ * and the dynamics. Multiple groups give multiple shapes.
+ */
template<typename T, uint64_t D, uint64_t PartAmount, typename CollisionType = coll::Spheroid<T>>
using ParticleGroup = Struct<
Member<Array<T,D>, "mask">,
Member<FixedArray<typename CollisionType::Schema,1u>, "collision">,
Member<FixedArray<Scalar<T>,1u>, "epsilon">,
Member<FixedArray<Scalar<T>,1u>, "mask_step">,
- Member<FixedArray<Scalar<T>,1u>, "density">,
Member<FixedArray<Vector<T,D>,1u>, "center_of_mass">,
- Member<FixedArray<Scalar<T>,1u>, "total_mass">,
- Member<FixedArray<Particle<T,D>,PartAmount>, "particles">
+ Member<FixedArray<Particle<T,D,CollisionType>,PartAmount>, "particles">
>;
}
}
diff --git a/modules/core/c++/particle/voxel_narrow.hpp b/modules/core/c++/particle/voxel_narrow.hpp
new file mode 100644
index 0000000..68a28e3
--- /dev/null
+++ b/modules/core/c++/particle/voxel_narrow.hpp
@@ -0,0 +1,262 @@
+#pragma once
+
+#include <cmath>
+
+#include "common.hpp"
+#include "schema.hpp"
+#include "mask.hpp"
+#include "aabb.hpp"
+#include "contact.hpp"
+
+namespace kel {
+namespace lbm {
+
+/**
+ * Voxel narrow phase.
+ *
+ * Resolves a contact from the overlap of two bodies' signed distance fields
+ * rather than from an analytic shape test, so anything that can be voxelized
+ * participates -- spheres, cubes, whatever the mask holds.
+ *
+ * Why it iterates the LATTICE, not either particle's voxel grid
+ * ------------------------------------------------------------
+ * Two reasons, and the second is the important one:
+ *
+ * - Cost. Sweeping one body's R^D voxels is hopeless at useful resolutions.
+ * The intersection of the two integer lattice bounding boxes is tiny for
+ * near-touching bodies, which is the only case that matters.
+ *
+ * - Symmetry. Both owners of a pair evaluate it independently and neither
+ * writes the other, so the two halves must come out exactly equal and
+ * opposite or every contact leaks momentum. Sampling on a particle-local
+ * grid makes the sample points depend on WHICH particle you started from.
+ * The lattice is common to both, so with a canonical (lo,hi) ordering both
+ * work items visit identical points in identical order and the results are
+ * bitwise identical.
+ */
+
+namespace impl {
+/**
+ * Gradient of the signed distance at a global point, by central differences.
+ *
+ * Taken in global coordinates directly, so no rotation of the result is
+ * needed -- the field is already expressed there.
+ */
+template<typename T, uint64_t D, typename GroupRef, typename PRef>
+kel::lbm::data<sch::Vector<T,D>> sdf_gradient(
+ const GroupRef& pg,
+ const PRef& p,
+ const kel::lbm::data<sch::Vector<T,D>>& x,
+ uint64_t resolution,
+ typename kel::lbm::native_data_type<T>::type h
+){
+ kel::lbm::data<sch::Vector<T,D>> g;
+
+ for(uint64_t i{0u}; i < D; ++i){
+ auto lo = x;
+ auto hi = x;
+ lo.at({{i}}).set(x.at({{i}}).get() - h);
+ hi.at({{i}}).set(x.at({{i}}).get() + h);
+
+ auto d_lo = mask_signed_distance<T,D>(pg,p,lo,resolution);
+ auto d_hi = mask_signed_distance<T,D>(pg,p,hi,resolution);
+
+ g.at({{i}}).set((d_hi - d_lo) / (2 * h));
+ }
+
+ return g;
+}
+}
+
+/**
+ * Contact between two bodies of the same group, from their voxel overlap.
+ *
+ * `lo` and `hi` must be passed in the caller's canonical order. `normal`
+ * points from hi toward lo, matching the analytic narrow phase so the force
+ * law downstream is unchanged.
+ */
+template<typename T, uint64_t D, typename GroupRef, typename PRef>
+bool voxel_contact(
+ const GroupRef& pg,
+ const PRef& lo,
+ const PRef& hi,
+ const kel::lbm::data<sch::FixedArray<sch::UInt64,D>>& extent,
+ kel::lbm::data<sch::Scalar<T>> eps,
+ kel::lbm::data<sch::ContactManifold<T,D>>& out
+){
+ using NT = typename kel::lbm::native_data_type<T>::type;
+
+ auto& mask = pg.template get<"mask">();
+ const uint64_t resolution = mask.get_dims().at({0u}).get();
+
+ auto& pos_lo = lo.template get<"rigid_body">().template get<"position">();
+ auto& pos_hi = hi.template get<"rigid_body">().template get<"position">();
+ auto r_lo = lo.template get<"collision">().template get<"radius">().at({}).get();
+ auto r_hi = hi.template get<"collision">().template get<"radius">().at({}).get();
+
+ /**
+ * Intersection of the two bounding boxes, clamped to the lattice. Built
+ * from the bounding radii, which the normalized mask guarantees are a
+ * true bound -- so a non-empty overlap can never be missed here.
+ */
+ kel::lbm::data<sch::FixedArray<sch::UInt64,D>> start, stop;
+ for(uint64_t i{0u}; i < D; ++i){
+ auto a_lo = pos_lo.at({{i}}).get() - r_lo;
+ auto a_hi = pos_hi.at({{i}}).get() - r_hi;
+ auto a = (a_lo > a_hi) ? a_lo : a_hi;
+
+ auto b_lo = pos_lo.at({{i}}).get() + r_lo;
+ auto b_hi = pos_hi.at({{i}}).get() + r_hi;
+ auto b = (b_lo < b_hi) ? b_lo : b_hi;
+
+ if(a < 0){ a = 0; }
+ int64_t ai = static_cast<int64_t>(std::floor(a));
+ int64_t bi = static_cast<int64_t>(std::ceil(b)) + 1;
+
+ auto e = static_cast<int64_t>(extent.at({i}).get());
+ if(ai < 0){ ai = 0; }
+ if(bi > e){ bi = e; }
+ if(bi <= ai){
+ return false;
+ }
+
+ start.at({i}).set(static_cast<uint64_t>(ai));
+ stop.at({i}).set(static_cast<uint64_t>(bi));
+ }
+
+ NT volume = 0;
+ kel::lbm::data<sch::Vector<T,D>> centroid;
+ kel::lbm::data<sch::Vector<T,D>> grad_sum;
+ for(uint64_t i{0u}; i < D; ++i){
+ centroid.at({{i}}).set(0);
+ grad_sum.at({{i}}).set(0);
+ }
+
+ const NT eps_v = eps.at({}).get();
+ // One lattice unit is the natural differencing width here.
+ const NT h = 0.5;
+
+ iterator<D>::apply([&](const auto& cell){
+ kel::lbm::data<sch::Vector<T,D>> x;
+ for(uint64_t i{0u}; i < D; ++i){
+ x.at({{i}}).set(static_cast<NT>(cell.at({i}).get()));
+ }
+
+ auto d_lo = mask_signed_distance<T,D>(pg,lo,x,resolution);
+ auto s_lo = solid_fraction_from_sdf<T>(d_lo,eps_v);
+ if(!(s_lo > 0)){
+ return;
+ }
+
+ auto d_hi = mask_signed_distance<T,D>(pg,hi,x,resolution);
+ auto s_hi = solid_fraction_from_sdf<T>(d_hi,eps_v);
+ if(!(s_hi > 0)){
+ return;
+ }
+
+ auto w = s_lo * s_hi;
+ volume += w;
+
+ for(uint64_t i{0u}; i < D; ++i){
+ centroid.at({{i}}).set(centroid.at({{i}}).get() + w * x.at({{i}}).get());
+ }
+
+ /**
+ * grad(phi_hi) - grad(phi_lo), weighted by the overlap.
+ *
+ * Antisymmetric in (lo,hi) by construction, which is what lets the
+ * two owners of the pair agree exactly. A centre-to-centre normal
+ * cannot represent face-on-face contact between boxes; this can.
+ */
+ auto g_lo = impl::sdf_gradient<T,D>(pg,lo,x,resolution,h);
+ auto g_hi = impl::sdf_gradient<T,D>(pg,hi,x,resolution,h);
+ for(uint64_t i{0u}; i < D; ++i){
+ grad_sum.at({{i}}).set(
+ grad_sum.at({{i}}).get() + w * (g_hi.at({{i}}).get() - g_lo.at({{i}}).get())
+ );
+ }
+ },start,stop);
+
+ if(!(volume > 0)){
+ return false;
+ }
+
+ for(uint64_t i{0u}; i < D; ++i){
+ centroid.at({{i}}).set(centroid.at({{i}}).get() / volume);
+ }
+
+ /**
+ * phi is negative inside, so grad(phi_hi) points out of hi. The mutual
+ * gradient therefore already points from hi toward lo.
+ */
+ auto& n = out.template get<"normal">();
+ if(!impl::safe_normalize<T,D>(grad_sum,n)){
+ kel::lbm::data<sch::Vector<T,D>> fallback;
+ for(uint64_t i{0u}; i < D; ++i){
+ fallback.at({{i}}).set(pos_lo.at({{i}}).get() - pos_hi.at({{i}}).get());
+ }
+ if(!impl::safe_normalize<T,D>(fallback,n)){
+ for(uint64_t i{0u}; i < D; ++i){
+ n.at({{i}}).set(0);
+ }
+ n.at({{0u}}).set(1);
+ }
+ }
+
+ /**
+ * Penetration as the extent of the overlap projected on the normal.
+ *
+ * Not volume^(1/D): that is dimensionally plausible but wrong in
+ * magnitude, because a lens of depth d between radii r has volume
+ * ~ r^((D-1)/2) * d^((D+1)/2), so the exponent does not come out right.
+ */
+ NT s_min = 0;
+ NT s_max = 0;
+ bool first = true;
+
+ iterator<D>::apply([&](const auto& cell){
+ kel::lbm::data<sch::Vector<T,D>> x;
+ for(uint64_t i{0u}; i < D; ++i){
+ x.at({{i}}).set(static_cast<NT>(cell.at({i}).get()));
+ }
+
+ auto d_lo = mask_signed_distance<T,D>(pg,lo,x,resolution);
+ if(!(solid_fraction_from_sdf<T>(d_lo,eps_v) > 0.5)){
+ return;
+ }
+ auto d_hi = mask_signed_distance<T,D>(pg,hi,x,resolution);
+ if(!(solid_fraction_from_sdf<T>(d_hi,eps_v) > 0.5)){
+ return;
+ }
+
+ auto s = kel::lbm::math::dot(x,n).at({}).get();
+ if(first){
+ s_min = s;
+ s_max = s;
+ first = false;
+ }else{
+ if(s < s_min){ s_min = s; }
+ if(s > s_max){ s_max = s; }
+ }
+ },start,stop);
+
+ // One lattice unit so a single-cell overlap reads as a real depth, not 0.
+ NT depth = first ? NT(1) : (s_max - s_min + NT(1));
+
+ auto cap = (r_lo < r_hi) ? r_lo : r_hi;
+ if(depth > cap){
+ depth = cap;
+ }
+
+ out.template get<"depth">().at({}).set(depth);
+
+ auto& point = out.template get<"point">();
+ for(uint64_t i{0u}; i < D; ++i){
+ point.at({{i}}).set(centroid.at({{i}}).get());
+ }
+
+ return true;
+}
+
+}
+}
diff --git a/modules/core/c++/psm.hpp b/modules/core/c++/psm.hpp
index b3a2673..19c3524 100644
--- a/modules/core/c++/psm.hpp
+++ b/modules/core/c++/psm.hpp
@@ -3,12 +3,14 @@
#include "macroscopic.hpp"
#include "component.hpp"
#include "equilibrium.hpp"
+#include "particle/integrate.hpp"
namespace kel {
namespace lbm {
namespace cmpt {
struct PsmReset {};
struct PsmOneParticle {};
+struct PsmOneParticleGather {};
struct Psm {};
}
namespace method {
@@ -91,7 +93,7 @@ public:
* PSM collision operator for LBM
*/
template<typename T, typename Descriptor, typename Encode>
-class component<T, Descriptor, cmpt::PsmOneParticle, Encode> final {
+class component<T, Descriptor, cmpt::PsmOneParticleGather, Encode> final {
private:
public:
component() = default;
@@ -140,14 +142,10 @@ void apply(
{
auto parts = pg.template get<"particles">();
-
- auto& p_coll =
- pg.template get<"collision">().at({});
+ auto& pi = parts.at(index);
auto& p_rad =
- p_coll.template get<"radius">();
-
- auto& pi = parts.at(index);
+ pi.template get<"collision">().template get<"radius">();
auto& pirb =
pi.template get<"rigid_body">();
@@ -193,7 +191,7 @@ void apply(
typename ParticleSchema::ValueType
>::calculate(
pg,
- {{0u}},
+ index,
vel_f.meta()
);
@@ -443,32 +441,43 @@ void apply(
* ------------------------------------------------------------
*/
- auto& pirb_acc =
- pirb.template get<"acceleration">();
-
- pirb_acc =
- force_p
- / pg.template get<"total_mass">().at({});
-
-
- /*
- * ------------------------------------------------------------
- * Integrate particle
- * ------------------------------------------------------------
+ /**
+ * Accumulate rather than assign, so a contact pass can add to this
+ * before cmpt::ParticleIntegrate turns the total into motion.
*/
-
- for(kel::lbm::data<sch::UInt64> i{0u};
- i < sub_steps;
- ++i){
-
- verlet_step_lambda<T,Descriptor::D>(
- pi,
- ts
- );
- }
+ auto& pirb_force = pirb.template get<"force">();
+ pirb_force = pirb_force + force_p;
}
}
};
+/**
+ * The original fused one-particle step, kept so existing call sites keep
+ * working.
+ */
+template<typename T, typename Descriptor, typename Encode>
+class component<T, Descriptor, cmpt::PsmOneParticle, Encode> final {
+public:
+ component() = default;
+
+ template<typename CellFieldSchema, typename MacroFieldSchema, typename ParticleSchema>
+ void apply(
+ const kel::lbm::data<CellFieldSchema, Encode>& field,
+ const kel::lbm::data<MacroFieldSchema, Encode>& macros,
+ const kel::lbm::data<ParticleSchema, Encode>& pg,
+ kel::lbm::data<sch::FixedArray<sch::UInt64,1u>> index,
+ kel::lbm::data<sch::UInt64> time_step,
+ kel::lbm::data<sch::UInt64> sub_steps
+ ) const {
+ component<T,Descriptor,cmpt::ParticleForceReset,Encode> reset;
+ component<T,Descriptor,cmpt::PsmOneParticleGather,Encode> gather;
+ component<T,Descriptor,cmpt::ParticleIntegrate,Encode> integrate;
+
+ reset.apply(pg,index,time_step);
+ gather.apply(field,macros,pg,index,time_step,sub_steps);
+ integrate.apply(pg,index,time_step,sub_steps);
+ }
+};
+
}
}