#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 kel::lbm::data> cell_porosity( const PartsRef& parts, const ClRef& cl, const CellRef& cell, kel::lbm::data> eps ){ kel::lbm::data> por; por.at({}).set(1); kel::lbm::data> cell_pos; for(uint64_t i{0u}; i < D; ++i){ cell_pos.at({{i}}).set( static_cast::type>(cell.at({i}).get()) ); } for_each_bin_candidate(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">().handle(); kel::lbm::data> 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>::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 struct ParticleFluidCouple {}; } template class component, Encode> final { public: using Component = cmpt::ParticleFluidCouple; private: kel::lbm::data> eps_; public: component(){ eps_.at({}).set(1.5); } component(const kel::lbm::data>& eps__): eps_{eps__} {} template void apply( const kel::lbm::data& macros, const kel::lbm::data& pg, const kel::lbm::data& cl, kel::lbm::data> index, kel::lbm::data time_step ) const { (void) time_step; auto& parts = pg.template get<"particles">(); macros.template get<"porosity">().at(index) = cell_porosity(parts,cl,index,eps_); } }; } }