Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create particles with existing particles #693

Merged
merged 3 commits into from
Sep 21, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions cajita/src/Cajita_ParticleInit.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -95,10 +95,12 @@ int createParticles(
// Allocate enough space for the case the particles consume the entire
// local grid.
int num_particles = particles_per_cell * owned_cells.size();
aosoa.resize( num_particles );
int previous_num_particles = aosoa.size();
streeve marked this conversation as resolved.
Show resolved Hide resolved
aosoa.resize( previous_num_particles + num_particles );

// Creation count.
auto count = Kokkos::View<int*, memory_space>( "particle_count", 1 );
Kokkos::deep_copy( count, previous_num_particles );

// Initialize particles.
Cajita::grid_parallel_for(
Expand Down Expand Up @@ -355,10 +357,12 @@ int createParticles(
int particles_per_cell = particles_per_cell_dim * particles_per_cell_dim *
particles_per_cell_dim;
int num_particles = particles_per_cell * owned_cells.size();
aosoa.resize( num_particles );
int previous_num_particles = aosoa.size();
aosoa.resize( previous_num_particles + num_particles );

// Creation count.
auto count = Kokkos::View<int*, memory_space>( "particle_count", 1 );
Kokkos::deep_copy( count, previous_num_particles );

// Initialize particles.
Cajita::grid_parallel_for(
Expand Down
20 changes: 16 additions & 4 deletions cajita/unit_test/tstParticleInit.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,8 @@ struct Bar : public Cabana::Field::Scalar<double>

//---------------------------------------------------------------------------//
template <class InitType>
void initParticleListTest( InitType init_type, int ppc )
void initParticleListTest( InitType init_type, int ppc,
const int multiplier = 1 )
{
// Global bounding box.
double cell_size = 0.23;
Expand Down Expand Up @@ -105,8 +106,10 @@ void initParticleListTest( InitType init_type, int ppc )
}
};

// Initialize particles.
int num_p = Cajita::createParticles( init_type, TEST_EXECSPACE(), init_func,
// Initialize particles (potentially multiple times).
int num_p = 0;
for ( int m = 0; m < multiplier; ++m )
num_p = Cajita::createParticles( init_type, TEST_EXECSPACE(), init_func,
particles, ppc, *local_grid );

// Check that we made particles.
Expand All @@ -117,7 +120,7 @@ void initParticleListTest( InitType init_type, int ppc )
MPI_Allreduce( MPI_IN_PLACE, &global_num_particle, 1, MPI_INT, MPI_SUM,
MPI_COMM_WORLD );
int expect_num_particle =
totalParticlesPerCell( init_type, ppc ) *
multiplier * totalParticlesPerCell( init_type, ppc ) *
( global_grid->globalNumEntity( Cajita::Cell(), Dim::I ) - 2 ) *
( global_grid->globalNumEntity( Cajita::Cell(), Dim::J ) - 2 ) *
( global_grid->globalNumEntity( Cajita::Cell(), Dim::K ) - 2 );
Expand Down Expand Up @@ -226,6 +229,15 @@ TEST( TEST_CATEGORY, uniform_init_test )
initSliceTest( Cabana::InitUniform(), 3 );
}

TEST( TEST_CATEGORY, multiple_random_init_test )
{
initParticleListTest( Cabana::InitRandom(), 17, 3 );
}

TEST( TEST_CATEGORY, multiple_uniform_init_test )
{
initParticleListTest( Cabana::InitUniform(), 3, 5 );
}
//---------------------------------------------------------------------------//

} // end namespace Test
14 changes: 8 additions & 6 deletions core/src/Cabana_ParticleInit.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -100,12 +100,14 @@ int createParticles(
using RandomType = Kokkos::Random_XorShift64<ExecutionSpace>;
PoolType pool( seed );

// Creation count.
auto count = Kokkos::View<int*, memory_space>( "particle_count", 1 );

// Resize the aosoa prior to lambda capture.
auto& aosoa = particle_list.aosoa();
aosoa.resize( num_particles );
int previous_num_particles = aosoa.size();
aosoa.resize( previous_num_particles + num_particles );

// Creation count.
auto count = Kokkos::View<int*, memory_space>( "particle_count", 1 );
Kokkos::deep_copy( count, previous_num_particles );

// Copy corners to device accessible arrays.
auto kokkos_min = Impl::copyArray( box_min );
Expand Down Expand Up @@ -134,8 +136,8 @@ int createParticles(
}
};

Kokkos::RangePolicy<ExecutionSpace> exec_policy( exec_space, 0,
num_particles );
Kokkos::RangePolicy<ExecutionSpace> exec_policy(
exec_space, 0, previous_num_particles + num_particles );
streeve marked this conversation as resolved.
Show resolved Hide resolved
Kokkos::parallel_for( exec_policy, random_coord_op );
Kokkos::fence();

Expand Down
22 changes: 14 additions & 8 deletions core/unit_test/tstParticleInit.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ void testRandomCreationParticleListMinDistance()
checkRandomDistances( min_dist, host_positions );
}

void testRandomCreationParticleList()
void testRandomCreationParticleList( const int multiplier = 1 )
{
int num_particle = 200;
using plist_type =
Expand All @@ -146,17 +146,20 @@ void testRandomCreationParticleList()

return true;
};
auto created =
Cabana::createParticles( Cabana::InitRandom(), init_func, particle_list,
num_particle, box_min, box_max );
EXPECT_EQ( num_particle, created );
int num_p = 0;
for ( int m = 0; m < multiplier; ++m )
num_p = Cabana::createParticles( Cabana::InitRandom(), init_func,
particle_list, num_particle, box_min,
box_max );
EXPECT_EQ( multiplier * num_particle, num_p );

auto host_particle_list = Cabana::create_mirror_view_and_copy(
Kokkos::HostSpace(), particle_list );
auto host_positions =
host_particle_list.slice( Cabana::Field::Position<3>() );

checkRandomParticles( num_particle, box_min, box_max, host_positions );
checkRandomParticles( multiplier * num_particle, box_min, box_max,
host_positions );
}

TEST( TEST_CATEGORY, random_particle_creation_slice_test )
Expand All @@ -168,5 +171,8 @@ TEST( TEST_CATEGORY, random_particle_creation_particlelist_test )
testRandomCreationParticleListMinDistance();
testRandomCreationParticleList();
}

} // namespace Test
TEST( TEST_CATEGORY, multiple_random_particle_creation_particlelist_test )
{
testRandomCreationParticleList( 3 );
}
} // namespace Test
Loading