2019-11-01 12:03:02 +01:00
|
|
|
/*******************************************************************\
|
|
|
|
particle_generator.hh - This file is part of MUSIC2 -
|
|
|
|
a code to generate initial conditions for cosmological simulations
|
|
|
|
|
|
|
|
CHANGELOG (only majors, for details see repo):
|
|
|
|
10/2019 - Oliver Hahn - first implementation
|
|
|
|
\*******************************************************************/
|
2019-10-19 12:56:43 +02:00
|
|
|
#pragma once
|
|
|
|
|
2019-11-01 04:47:02 +01:00
|
|
|
#include <vec3.hh>
|
|
|
|
|
2019-10-19 12:56:43 +02:00
|
|
|
namespace particle {
|
|
|
|
|
|
|
|
enum lattice{
|
2019-11-01 04:47:02 +01:00
|
|
|
lattice_sc = 0, // SC : simple cubic
|
|
|
|
lattice_bcc = 1, // BCC: body-centered cubic
|
|
|
|
lattice_fcc = 2, // FCC: face-centered cubic
|
|
|
|
lattice_rsc = 3, // RSC: refined simple cubic
|
|
|
|
};
|
|
|
|
|
|
|
|
const std::vector< std::vector<vec3<real_t>> > lattice_shifts =
|
|
|
|
{
|
|
|
|
// first shift must always be zero! (otherwise set_positions and set_velocities break)
|
|
|
|
/* SC : */ {{0.0,0.0,0.0}},
|
|
|
|
/* BCC: */ {{0.0,0.0,0.0},{0.5,0.5,0.5}},
|
2019-11-01 04:49:40 +01:00
|
|
|
/* FCC: */ {{0.0,0.0,0.0},{0.0,0.5,0.5},{0.5,0.0,0.5},{0.5,0.5,0.0}},
|
2019-11-01 04:47:02 +01:00
|
|
|
/* RSC: */ {{0.0,0.0,0.0},{0.0,0.0,0.5},{0.0,0.5,0.0},{0.0,0.5,0.5},{0.5,0.0,0.0},{0.5,0.0,0.5},{0.5,0.5,0.0},{0.5,0.5,0.5}},
|
2019-10-19 12:56:43 +02:00
|
|
|
};
|
|
|
|
|
2020-01-24 15:00:32 +01:00
|
|
|
const std::vector<vec3<real_t>> second_lattice_shift =
|
|
|
|
{
|
2020-02-28 16:15:37 +01:00
|
|
|
/* SC : */ {0.5, 0.5, 0.5}, // this corresponds to CsCl lattice
|
|
|
|
/* BCC: */ {0.5, 0.5, 0.0}, // is there a diatomic lattice with BCC base?!?
|
|
|
|
/* FCC: */ {0.5, 0.5, 0.5}, // this corresponds to NaCl lattice
|
|
|
|
// /* FCC: */ {0.25, 0.25, 0.25}, // this corresponds to Zincblende/GaAs lattice
|
2020-01-24 15:00:32 +01:00
|
|
|
/* RSC: */ {0.25, 0.25, 0.25},
|
|
|
|
};
|
|
|
|
|
2019-10-19 12:56:43 +02:00
|
|
|
template<typename field_t>
|
2020-03-10 19:01:44 +01:00
|
|
|
void initialize_lattice( container& particles, lattice lattice_type, const bool b64reals, const bool b64ids, const size_t IDoffset, const field_t& field ){
|
2019-11-01 12:03:02 +01:00
|
|
|
// number of modes present in the field
|
2019-10-19 12:56:43 +02:00
|
|
|
const size_t num_p_in_load = field.local_size();
|
2019-11-01 12:03:02 +01:00
|
|
|
// unless SC lattice is used, particle number is a multiple of the number of modes (=num_p_in_load):
|
2019-11-01 04:47:02 +01:00
|
|
|
const size_t overload = 1ull<<lattice_type; // 1 for sc, 2 for bcc, 4 for fcc, 8 for rsc
|
2019-11-01 12:03:02 +01:00
|
|
|
// allocate memory for all local particles
|
|
|
|
particles.allocate( overload * num_p_in_load, b64reals, b64ids );
|
|
|
|
// set particle IDs to the Lagrangian coordinate (1D encoded) with additionally the field shift encoded as well
|
2019-10-19 12:56:43 +02:00
|
|
|
for( size_t i=0,ipcount=0; i<field.size(0); ++i ){
|
|
|
|
for( size_t j=0; j<field.size(1); ++j){
|
|
|
|
for( size_t k=0; k<field.size(2); ++k,++ipcount){
|
|
|
|
for( size_t iload=0; iload<overload; ++iload ){
|
2019-11-01 12:03:02 +01:00
|
|
|
if( b64ids ){
|
2020-03-10 19:01:44 +01:00
|
|
|
particles.set_id64( ipcount+iload*num_p_in_load, IDoffset + overload*field.get_cell_idx_1d(i,j,k)+iload );
|
2019-11-01 12:03:02 +01:00
|
|
|
}else{
|
2020-03-10 19:01:44 +01:00
|
|
|
particles.set_id32( ipcount+iload*num_p_in_load, IDoffset + overload*field.get_cell_idx_1d(i,j,k)+iload );
|
2019-11-01 12:03:02 +01:00
|
|
|
}
|
2019-10-19 12:56:43 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// invalidates field, phase shifted to unspecified position after return
|
|
|
|
template<typename field_t>
|
2020-01-24 15:00:32 +01:00
|
|
|
void set_positions( container& particles, const lattice lattice_type, bool is_second_lattice, int idim, real_t lunit, const bool b64reals, field_t& field )
|
2019-11-01 04:47:02 +01:00
|
|
|
{
|
|
|
|
const size_t num_p_in_load = field.local_size();
|
|
|
|
for( int ishift=0; ishift<(1<<lattice_type); ++ishift ){
|
2020-01-24 15:00:32 +01:00
|
|
|
// if we are dealing with the secondary lattice, apply a global shift
|
|
|
|
if( ishift==0 && is_second_lattice ){
|
|
|
|
field.shift_field( second_lattice_shift[lattice_type] );
|
|
|
|
}
|
|
|
|
|
|
|
|
// can omit first shift since zero by convention, unless shifted already above, otherwise apply relative phase shift
|
2019-11-01 04:47:02 +01:00
|
|
|
if( ishift>0 ){
|
2019-11-01 12:03:02 +01:00
|
|
|
field.shift_field( lattice_shifts[lattice_type][ishift] - lattice_shifts[lattice_type][ishift-1] );
|
2019-11-01 04:47:02 +01:00
|
|
|
}
|
2019-11-01 12:03:02 +01:00
|
|
|
// read out values from phase shifted field and set assoc. particle's value
|
|
|
|
const auto ipcount0 = ishift * num_p_in_load;
|
2019-11-01 04:47:02 +01:00
|
|
|
for( size_t i=0,ipcount=ipcount0; i<field.size(0); ++i ){
|
|
|
|
for( size_t j=0; j<field.size(1); ++j){
|
|
|
|
for( size_t k=0; k<field.size(2); ++k){
|
2020-01-24 15:00:32 +01:00
|
|
|
auto pos = field.template get_unit_r_shifted<real_t>(i,j,k,lattice_shifts[lattice_type][ishift]
|
|
|
|
+ (is_second_lattice? second_lattice_shift[lattice_type] : vec3<real_t>{0.,0.,0.}) );
|
2019-11-01 12:03:02 +01:00
|
|
|
if( b64reals ){
|
|
|
|
particles.set_pos64( ipcount++, idim, pos[idim]*lunit + field.relem(i,j,k) );
|
|
|
|
}else{
|
|
|
|
particles.set_pos32( ipcount++, idim, pos[idim]*lunit + field.relem(i,j,k) );
|
|
|
|
}
|
2019-11-01 04:47:02 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-24 15:00:32 +01:00
|
|
|
template <typename field_t>
|
|
|
|
void set_velocities(container &particles, lattice lattice_type, bool is_second_lattice, int idim, const bool b64reals, field_t &field)
|
2019-11-01 04:47:02 +01:00
|
|
|
{
|
|
|
|
const size_t num_p_in_load = field.local_size();
|
|
|
|
for( int ishift=0; ishift<(1<<lattice_type); ++ishift ){
|
2020-01-24 15:00:32 +01:00
|
|
|
// if we are dealing with the secondary lattice, apply a global shift
|
|
|
|
if (ishift == 0 && is_second_lattice){
|
|
|
|
field.shift_field(second_lattice_shift[lattice_type]);
|
|
|
|
}
|
|
|
|
// can omit first shift since zero by convention, unless shifted already above, otherwise apply relative phase shift
|
|
|
|
if (ishift > 0){
|
2019-11-01 12:03:02 +01:00
|
|
|
field.shift_field( lattice_shifts[lattice_type][ishift]-lattice_shifts[lattice_type][ishift-1] );
|
2019-11-01 04:47:02 +01:00
|
|
|
}
|
2019-11-01 12:03:02 +01:00
|
|
|
// read out values from phase shifted field and set assoc. particle's value
|
|
|
|
const auto ipcount0 = ishift * num_p_in_load;
|
2019-11-01 04:47:02 +01:00
|
|
|
for( size_t i=0,ipcount=ipcount0; i<field.size(0); ++i ){
|
|
|
|
for( size_t j=0; j<field.size(1); ++j){
|
|
|
|
for( size_t k=0; k<field.size(2); ++k){
|
2019-11-01 12:03:02 +01:00
|
|
|
if( b64reals ){
|
|
|
|
particles.set_vel64( ipcount++, idim, field.relem(i,j,k) );
|
|
|
|
}else{
|
|
|
|
particles.set_vel32( ipcount++, idim, field.relem(i,j,k) );
|
|
|
|
}
|
2019-11-01 04:47:02 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-10-19 12:56:43 +02:00
|
|
|
} // end namespace particles
|