mcpele  1.0.0
The Monte Carlo Python Energy Landscape Explorer
particle_pair_swap.cpp
00001 #include "mcpele/particle_pair_swap.h"
00002 
00003 namespace mcpele {
00004 
00005 ParticlePairSwap::ParticlePairSwap(const size_t seed, const size_t nr_particles)
00006     : m_seed(seed),
00007       m_generator(seed),
00008       m_distribution(0, nr_particles - 1),
00009       m_nr_particles(nr_particles)
00010 {
00011     if (nr_particles == 0) {
00012         throw std::runtime_error("ParticlePairSwap: illegal input");
00013     }
00014 }
00015 
00016 void ParticlePairSwap::displace(pele::Array<double>& coords, MC* mc)
00017 {
00018     size_t particle_a = 42;
00019     size_t particle_b = 42;
00020     while (particle_a == particle_b) {
00021         particle_a = m_distribution(m_generator);
00022         particle_b = m_distribution(m_generator);
00023     }
00024     assert(particle_a < m_nr_particles && particle_b < m_nr_particles);
00025     assert(particle_a != particle_b);
00026     swap_coordinates(particle_a, particle_b, coords);
00027 }
00028 
00029 void ParticlePairSwap::swap_coordinates(const size_t particle_a, const size_t particle_b, pele::Array<double>& coords)
00030 {
00031     if (particle_a == particle_b) {
00032         return;
00033     }
00034     const size_t box_dimension = coords.size() / m_nr_particles;
00035     const size_t index_a = particle_a * box_dimension;
00036     const size_t index_b = particle_b * box_dimension;
00037     double*const& x = coords.data();
00038     double*const& xa = x + index_a;
00039     double*const& xb = x + index_b;
00040     std::swap_ranges(xa, xa + box_dimension, xb);
00041 }
00042 
00043 void ParticlePairSwap::set_generator_seed(const size_t inp)
00044 {
00045     m_generator.seed(inp);
00046     m_seed = inp;
00047 }
00048 
00049 } // namespace mcpele
 All Classes Namespaces Functions Variables Typedefs