Skip to content

Commit

Permalink
Merge pull request #190 from streeve/bc_particle_count
Browse files Browse the repository at this point in the history
Check boundary condition particle count
  • Loading branch information
streeve authored Mar 7, 2025
2 parents 3485dfc + c178185 commit d6364ae
Show file tree
Hide file tree
Showing 7 changed files with 52 additions and 20 deletions.
3 changes: 2 additions & 1 deletion examples/dem/powder_fill.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,8 @@ void powderSettlingExample( const std::string filename )
{
f( pid, 2 ) -= 9.8 * rho( pid );
};
auto gravity = CabanaPD::createBodyTerm( body_functor, true );
auto gravity =
CabanaPD::createBodyTerm( body_functor, particles->size(), true );

// ====================================================
// Simulation run
Expand Down
3 changes: 2 additions & 1 deletion examples/thermomechanics/thermal_crack.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,8 @@ void thermalCrackExample( const std::string filename )
// Compute particle temperature
temp( pid ) = temp_infinity + ( temp0 - temp_infinity ) * fx * fy;
};
auto body_term = CabanaPD::createBodyTerm( temp_func, false );
auto body_term =
CabanaPD::createBodyTerm( temp_func, particles->size(), false );

// ====================================================
// Simulation run
Expand Down
3 changes: 2 additions & 1 deletion examples/thermomechanics/thermal_deformation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,8 @@ void thermalDeformationExample( const std::string filename )
{
temp( pid ) = temp0 + 5000.0 * ( x( pid, 1 ) - low_corner_y ) * t;
};
auto body_term = CabanaPD::createBodyTerm( temp_func, false );
auto body_term =
CabanaPD::createBodyTerm( temp_func, particles->size(), false );

// ====================================================
// Simulation run
Expand Down
16 changes: 12 additions & 4 deletions src/CabanaPD_BodyTerm.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

#include <Cabana_Core.hpp>

#include <CabanaPD_Output.hpp>
#include <CabanaPD_Timer.hpp>

namespace CabanaPD
Expand All @@ -25,13 +26,16 @@ template <class UserFunctor>
struct BodyTerm
{
UserFunctor _user_functor;
std::size_t _particle_count;
bool _force_update;
bool _update_frozen;

Timer _timer;

BodyTerm( UserFunctor user, const bool force, const bool update_frozen )
BodyTerm( UserFunctor user, const std::size_t particle_count,
const bool force, const bool update_frozen )
: _user_functor( user )
, _particle_count( particle_count )
, _force_update( force )
, _update_frozen( update_frozen )
{
Expand All @@ -42,6 +46,9 @@ struct BodyTerm
template <class ExecSpace, class ParticleType>
void apply( ExecSpace, ParticleType& particles, const double time )
{
checkParticleCount( _particle_count, particles.referenceOffset(),
"BodyTerm" );

_timer.start();
std::size_t start = particles.frozenOffset();
if ( _update_frozen )
Expand All @@ -61,10 +68,11 @@ struct BodyTerm
};

template <class UserFunctor>
auto createBodyTerm( UserFunctor user_functor, const bool force_update,
const bool update_frozen = false )
auto createBodyTerm( UserFunctor user_functor, const std::size_t particle_count,
const bool force_update, const bool update_frozen = false )
{
return BodyTerm<UserFunctor>( user_functor, force_update, update_frozen );
return BodyTerm<UserFunctor>( user_functor, particle_count, force_update,
update_frozen );
}

} // namespace CabanaPD
Expand Down
21 changes: 15 additions & 6 deletions src/CabanaPD_Boundary.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

#include <Cabana_Core.hpp>

#include <CabanaPD_Output.hpp>
#include <CabanaPD_Timer.hpp>

namespace CabanaPD
Expand Down Expand Up @@ -125,17 +126,16 @@ struct BoundaryIndexSpace<MemorySpace, RegionBoundary<GeometryType>>
using index_view_type = Kokkos::View<std::size_t*, MemorySpace>;
index_view_type _view;
index_view_type _count;
std::size_t particle_count;

Timer _timer;

// Default for empty case.
BoundaryIndexSpace() {}

// Construct from region (search for boundary particles).
template <class ExecSpace, class Particles>
BoundaryIndexSpace( ExecSpace exec_space, Particles particles,
std::vector<RegionBoundary<GeometryType>> planes,
const double initial_guess )
: particle_count( particles.referenceOffset() )
{
_timer.start();

Expand All @@ -162,6 +162,8 @@ struct BoundaryIndexSpace<MemorySpace, RegionBoundary<GeometryType>>
void update( ExecSpace, Particles particles,
RegionBoundary<GeometryType> region )
{
particle_count = particles.referenceOffset();

auto count_host =
Kokkos::create_mirror_view_and_copy( Kokkos::HostSpace{}, _count );
auto init_count = count_host( 0 );
Expand Down Expand Up @@ -260,9 +262,12 @@ struct BoundaryCondition
}

template <class ExecSpace, class ParticleType>
void apply( ExecSpace, ParticleType&, double time )
void apply( ExecSpace, ParticleType& particles, double time )
{
checkParticleCount( _index_space.particle_count,
particles.referenceOffset(), "BoundaryCondition" );
_timer.start();

auto user = _user_functor;
auto index_space = _index_space._view;
Kokkos::RangePolicy<ExecSpace> policy( 0, index_space.size() );
Expand Down Expand Up @@ -304,6 +309,9 @@ struct BoundaryCondition<BCIndexSpace, ForceValueBCTag>
template <class ExecSpace, class ParticleType>
void apply( ExecSpace, ParticleType& particles, double )
{
checkParticleCount( _index_space.particle_count,
particles.referenceOffset(), "BoundaryCondition" );

_timer.start();
auto f = particles.sliceForce();
auto index_space = _index_space._view;
Expand Down Expand Up @@ -348,8 +356,10 @@ struct BoundaryCondition<BCIndexSpace, ForceUpdateBCTag>
template <class ExecSpace, class ParticleType>
void apply( ExecSpace, ParticleType& particles, double )
{
_timer.start();
checkParticleCount( _index_space.particle_count,
particles.referenceOffset(), "BoundaryCondition" );

_timer.start();
auto f = particles.sliceForce();
auto index_space = _index_space._view;
Kokkos::RangePolicy<ExecSpace> policy( 0, index_space.size() );
Expand All @@ -360,7 +370,6 @@ struct BoundaryCondition<BCIndexSpace, ForceUpdateBCTag>
for ( int d = 0; d < 3; d++ )
f( pid, d ) += value;
} );

_timer.stop();
}

Expand Down
14 changes: 12 additions & 2 deletions src/CabanaPD_Output.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
#include <fstream>
#include <iostream>
#include <stdexcept>
#include <string>
#include <utility>

#include <mpi.h>
Expand Down Expand Up @@ -60,8 +61,7 @@ void log_err( t_stream& stream, t_last&& last )
if ( print_rank() )
{
stream << last << std::endl;
throw std::runtime_error(
"Aborting after error from input. See error file." );
throw std::runtime_error( "Aborting after error." );
}
}

Expand All @@ -73,6 +73,16 @@ void log_err( t_stream& stream, t_head&& head, t_tail&&... tail )
log_err( stream, std::forward<t_tail>( tail )... );
}

void checkParticleCount( std::size_t initial, std::size_t current,
std::string name )
{
if ( initial != current )
log_err( std::cout, "\nParticle size (", std::to_string( current ),
") does not match size when ", name, " was created (",
std::to_string( initial ),
").\n Likely a slice() call is missing." );
}

} // namespace CabanaPD

#endif
12 changes: 7 additions & 5 deletions src/CabanaPD_Particles.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ class Particles<MemorySpace, PMB, TemperatureIndependent, BaseOutput, Dimension>
std::size_t frozen_offset = 0;
std::size_t local_offset = 0;
std::size_t num_ghost = 0;
std::size_t size = 0;
std::size_t _size = 0;

// x, u, f (vector matching system dimension).
using vector_type = Cabana::MemberTypes<double[dim]>;
Expand Down Expand Up @@ -464,7 +464,9 @@ class Particles<MemorySpace, PMB, TemperatureIndependent, BaseOutput, Dimension>
auto numLocal() const { return local_offset - frozen_offset; }
auto localOffset() const { return local_offset; }
auto numGhost() const { return num_ghost; }
auto referenceOffset() const { return size; }
// This is currently size because contact ghosts are not added yet.
auto referenceOffset() const { return _size; }
auto size() const { return _size; }
auto numGlobal() const { return num_global; }

auto sliceReferencePosition()
Expand Down Expand Up @@ -567,7 +569,7 @@ class Particles<MemorySpace, PMB, TemperatureIndependent, BaseOutput, Dimension>

local_offset = new_local;
num_ghost = new_ghost;
size = new_local + new_ghost;
_size = new_local + new_ghost;

_plist_x.aosoa().resize( referenceOffset() );
_aosoa_u.resize( referenceOffset() );
Expand All @@ -577,9 +579,9 @@ class Particles<MemorySpace, PMB, TemperatureIndependent, BaseOutput, Dimension>
_aosoa_other.resize( localOffset() );
_aosoa_nofail.resize( referenceOffset() );

size = _plist_x.size();
_size = _plist_x.size();
if ( create_frozen )
frozen_offset = size;
frozen_offset = _size;
_timer.stop();
};

Expand Down

0 comments on commit d6364ae

Please sign in to comment.