molecular_dynamics_cells.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379
  1. /*
  2. [auto_generated]
  3. libs/numeric/odeint/examples/molecular_dynamics_cells.cpp
  4. [begin_description]
  5. Molecular dynamics example with cells.
  6. [end_description]
  7. Copyright 2009-2012 Karsten Ahnert
  8. Copyright 2009-2012 Mario Mulansky
  9. Distributed under the Boost Software License, Version 1.0.
  10. (See accompanying file LICENSE_1_0.txt or
  11. copy at http://www.boost.org/LICENSE_1_0.txt)
  12. */
  13. #include <boost/numeric/odeint.hpp>
  14. #include <cstddef>
  15. #include <vector>
  16. #include <cmath>
  17. #include <algorithm>
  18. #include <tuple>
  19. #include <iostream>
  20. #include <random>
  21. #include <boost/range/algorithm/for_each.hpp>
  22. #include <boost/range/algorithm/sort.hpp>
  23. #include <boost/range/algorithm/unique_copy.hpp>
  24. #include <boost/range/algorithm_ext/iota.hpp>
  25. #include <boost/iterator/zip_iterator.hpp>
  26. #include <boost/iterator/transform_iterator.hpp>
  27. #include <boost/iterator/permutation_iterator.hpp>
  28. #include <boost/iterator/counting_iterator.hpp>
  29. #include "point_type.hpp"
  30. struct local_force
  31. {
  32. double m_gamma; // friction
  33. local_force( double gamma = 0.0 ) : m_gamma( gamma ) { }
  34. template< typename Point >
  35. Point operator()( Point& x , Point& v ) const
  36. {
  37. return - m_gamma * v;
  38. }
  39. };
  40. struct lennard_jones
  41. {
  42. double m_sigma;
  43. double m_eps;
  44. lennard_jones( double sigma = 1.0 , double eps = 0.1 ) : m_sigma( sigma ) , m_eps( eps ) { }
  45. double operator()( double r ) const
  46. {
  47. double c = m_sigma / r;
  48. double c3 = c * c * c;
  49. double c6 = c3 * c3;
  50. return 4.0 * m_eps * ( -12.0 * c6 * c6 / r + 6.0 * c6 / r );
  51. }
  52. };
  53. template< typename F >
  54. struct conservative_interaction
  55. {
  56. F m_f;
  57. conservative_interaction( F const &f = F() ) : m_f( f ) { }
  58. template< typename Point >
  59. Point operator()( Point const& x1 , Point const& x2 ) const
  60. {
  61. Point diff = x1 - x2;
  62. double r = abs( diff );
  63. double f = m_f( r );
  64. return - diff / r * f;
  65. }
  66. };
  67. template< typename F >
  68. conservative_interaction< F > make_conservative_interaction( F const &f )
  69. {
  70. return conservative_interaction< F >( f );
  71. }
  72. // force = interaction( x1 , x2 )
  73. // force = local_force( x , v )
  74. template< typename LocalForce , typename Interaction >
  75. class md_system_bs
  76. {
  77. public:
  78. typedef std::vector< double > vector_type;
  79. typedef point< double , 2 > point_type;
  80. typedef point< int , 2 > index_type;
  81. typedef std::vector< point_type > point_vector;
  82. typedef std::vector< index_type > index_vector;
  83. typedef std::vector< size_t > hash_vector;
  84. typedef LocalForce local_force_type;
  85. typedef Interaction interaction_type;
  86. struct params
  87. {
  88. size_t n;
  89. size_t n_cell_x , n_cell_y , n_cells;
  90. double x_max , y_max , cell_size;
  91. double eps , sigma; // interaction strength, interaction radius
  92. interaction_type interaction;
  93. local_force_type local_force;
  94. };
  95. struct cell_functor
  96. {
  97. params const &m_p;
  98. cell_functor( params const& p ) : m_p( p ) { }
  99. template< typename Tuple >
  100. void operator()( Tuple const& t ) const
  101. {
  102. auto point = boost::get< 0 >( t );
  103. size_t i1 = size_t( point[0] / m_p.cell_size ) , i2 = size_t( point[1] / m_p.cell_size );
  104. boost::get< 1 >( t ) = index_type( i1 , i2 );
  105. boost::get< 2 >( t ) = hash_func( boost::get< 1 >( t ) , m_p );
  106. }
  107. };
  108. struct transform_functor
  109. {
  110. typedef size_t argument_type;
  111. typedef size_t result_type;
  112. hash_vector const* m_index;
  113. transform_functor( hash_vector const& index ) : m_index( &index ) { }
  114. size_t operator()( size_t i ) const { return (*m_index)[i]; }
  115. };
  116. struct interaction_functor
  117. {
  118. hash_vector const &m_cells_begin;
  119. hash_vector const &m_cells_end;
  120. hash_vector const &m_order;
  121. point_vector const &m_x;
  122. point_vector const &m_v;
  123. params const &m_p;
  124. size_t m_ncellx , m_ncelly;
  125. interaction_functor( hash_vector const& cells_begin , hash_vector const& cells_end , hash_vector pos_order ,
  126. point_vector const&x , point_vector const& v , params const &p )
  127. : m_cells_begin( cells_begin ) , m_cells_end( cells_end ) , m_order( pos_order ) , m_x( x ) , m_v( v ) ,
  128. m_p( p ) { }
  129. template< typename Tuple >
  130. void operator()( Tuple const &t ) const
  131. {
  132. point_type x = periodic_bc( boost::get< 0 >( t ) , m_p ) , v = boost::get< 1 >( t );
  133. index_type index = boost::get< 3 >( t );
  134. size_t pos_hash = boost::get< 4 >( t );
  135. point_type a = m_p.local_force( x , v );
  136. for( int i=-1 ; i<=1 ; ++i )
  137. {
  138. for( int j=-1 ; j<=1 ; ++j )
  139. {
  140. index_type cell_index = index + index_type( i , j );
  141. size_t cell_hash = hash_func( cell_index , m_p );
  142. for( size_t ii = m_cells_begin[ cell_hash ] ; ii < m_cells_end[ cell_hash ] ; ++ii )
  143. {
  144. if( m_order[ ii ] == pos_hash ) continue;
  145. point_type x2 = periodic_bc( m_x[ m_order[ ii ] ] , m_p );
  146. if( cell_index[0] >= m_p.n_cell_x ) x2[0] += m_p.x_max;
  147. if( cell_index[0] < 0 ) x2[0] -= m_p.x_max;
  148. if( cell_index[1] >= m_p.n_cell_y ) x2[1] += m_p.y_max;
  149. if( cell_index[1] < 0 ) x2[1] -= m_p.y_max;
  150. a += m_p.interaction( x , x2 );
  151. }
  152. }
  153. }
  154. boost::get< 2 >( t ) = a;
  155. }
  156. };
  157. md_system_bs( size_t n ,
  158. local_force_type const& local_force = local_force_type() ,
  159. interaction_type const& interaction = interaction_type() ,
  160. double xmax = 100.0 , double ymax = 100.0 , double cell_size = 2.0 )
  161. : m_p()
  162. {
  163. m_p.n = n;
  164. m_p.x_max = xmax;
  165. m_p.y_max = ymax;
  166. m_p.interaction = interaction;
  167. m_p.local_force = local_force;
  168. m_p.n_cell_x = size_t( xmax / cell_size );
  169. m_p.n_cell_y = size_t( ymax / cell_size );
  170. m_p.n_cells = m_p.n_cell_x * m_p.n_cell_y;
  171. m_p.cell_size = cell_size;
  172. }
  173. void init_point_vector( point_vector &x ) const { x.resize( m_p.n ); }
  174. void operator()( point_vector const& x , point_vector const& v , point_vector &a , double t ) const
  175. {
  176. // init
  177. hash_vector pos_hash( m_p.n , 0 );
  178. index_vector pos_index( m_p.n );
  179. hash_vector pos_order( m_p.n , 0 );
  180. hash_vector cells_begin( m_p.n_cells ) , cells_end( m_p.n_cells ) , cell_order( m_p.n_cells );
  181. boost::iota( pos_order , 0 );
  182. boost::iota( cell_order , 0 );
  183. // calculate grid hash
  184. // calcHash( m_dGridParticleHash, m_dGridParticleIndex, dPos, m_numParticles);
  185. std::for_each(
  186. boost::make_zip_iterator( boost::make_tuple( x.begin() , pos_index.begin() , pos_hash.begin() ) ) ,
  187. boost::make_zip_iterator( boost::make_tuple( x.end() , pos_index.end() , pos_hash.end() ) ) ,
  188. cell_functor( m_p ) );
  189. // // sort particles based on hash
  190. // // sortParticles(m_dGridParticleHash, m_dGridParticleIndex, m_numParticles);
  191. boost::sort( pos_order , [&]( size_t i1 , size_t i2 ) -> bool {
  192. return pos_hash[i1] < pos_hash[i2]; } );
  193. // reorder particle arrays into sorted order and find start and end of each cell
  194. std::for_each( cell_order.begin() , cell_order.end() , [&]( size_t i ) {
  195. auto pos_begin = boost::make_transform_iterator( pos_order.begin() , transform_functor( pos_hash ) );
  196. auto pos_end = boost::make_transform_iterator( pos_order.end() , transform_functor( pos_hash ) );
  197. cells_begin[ i ] = std::distance( pos_begin , std::lower_bound( pos_begin , pos_end , i ) );
  198. cells_end[ i ] = std::distance( pos_begin , std::upper_bound( pos_begin , pos_end , i ) );
  199. } );
  200. std::for_each(
  201. boost::make_zip_iterator( boost::make_tuple(
  202. x.begin() ,
  203. v.begin() ,
  204. a.begin() ,
  205. pos_index.begin() ,
  206. boost::counting_iterator< size_t >( 0 )
  207. ) ) ,
  208. boost::make_zip_iterator( boost::make_tuple(
  209. x.end() ,
  210. v.end() ,
  211. a.end() ,
  212. pos_index.end() ,
  213. boost::counting_iterator< size_t >( m_p.n )
  214. ) ) ,
  215. interaction_functor( cells_begin , cells_end , pos_order , x , v , m_p ) );
  216. }
  217. void bc( point_vector &x )
  218. {
  219. for( size_t i=0 ; i<m_p.n ; ++i )
  220. {
  221. x[i][0] = periodic_bc( x[ i ][0] , m_p.x_max );
  222. x[i][1] = periodic_bc( x[ i ][1] , m_p.y_max );
  223. }
  224. }
  225. static inline double periodic_bc( double x , double xmax )
  226. {
  227. double tmp = x - xmax * int( x / xmax );
  228. return tmp >= 0.0 ? tmp : tmp + xmax;
  229. }
  230. static inline point_type periodic_bc( point_type const& x , params const& p )
  231. {
  232. return point_type( periodic_bc( x[0] , p.x_max ) , periodic_bc( x[1] , p.y_max ) );
  233. }
  234. static inline int check_interval( int i , int max )
  235. {
  236. int tmp = i % max;
  237. return tmp >= 0 ? tmp : tmp + max;
  238. }
  239. static inline size_t hash_func( index_type index , params const & p )
  240. {
  241. size_t i1 = check_interval( index[0] , p.n_cell_x );
  242. size_t i2 = check_interval( index[1] , p.n_cell_y );
  243. return i1 * p.n_cell_y + i2;
  244. }
  245. params m_p;
  246. };
  247. template< typename LocalForce , typename Interaction >
  248. md_system_bs< LocalForce , Interaction > make_md_system_bs( size_t n , LocalForce const &f1 , Interaction const &f2 ,
  249. double xmax = 100.0 , double ymax = 100.0 , double cell_size = 2.0 )
  250. {
  251. return md_system_bs< LocalForce , Interaction >( n , f1 , f2 , xmax , ymax , cell_size );
  252. }
  253. using namespace boost::numeric::odeint;
  254. int main( int argc , char *argv[] )
  255. {
  256. const size_t n1 = 32;
  257. const size_t n2 = 32;
  258. const size_t n = n1 * n2;
  259. auto sys = make_md_system_bs( n , local_force() , make_conservative_interaction( lennard_jones() ) , 100.0 , 100.0 , 2.0 );
  260. typedef decltype( sys ) system_type;
  261. typedef system_type::point_vector point_vector;
  262. std::mt19937 rng;
  263. std::normal_distribution<> dist( 0.0 , 1.0 );
  264. point_vector x , v;
  265. sys.init_point_vector( x );
  266. sys.init_point_vector( v );
  267. for( size_t i=0 ; i<n1 ; ++i )
  268. {
  269. for( size_t j=0 ; j<n2 ; ++j )
  270. {
  271. size_t index = i * n2 + j;
  272. x[index][0] = 10.0 + i * 2.0 ;
  273. x[index][1] = 10.0 + j * 2.0 ;
  274. v[index][0] = dist( rng ) ;
  275. v[index][1] = dist( rng ) ;
  276. }
  277. }
  278. velocity_verlet< point_vector > stepper;
  279. const double dt = 0.025;
  280. double t = 0.0;
  281. // std::cout << "set term x11" << endl;
  282. for( size_t oi=0 ; oi<10000 ; ++oi )
  283. {
  284. for( size_t ii=0 ; ii<50 ; ++ii,t+=dt )
  285. stepper.do_step( sys , std::make_pair( std::ref( x ) , std::ref( v ) ) , t , dt );
  286. sys.bc( x );
  287. std::cout << "set size square" << "\n";
  288. std::cout << "unset key" << "\n";
  289. std::cout << "p [0:" << sys.m_p.x_max << "][0:" << sys.m_p.y_max << "] '-' pt 7 ps 0.5" << "\n";
  290. for( size_t i=0 ; i<n ; ++i )
  291. std::cout << x[i][0] << " " << x[i][1] << " " << v[i][0] << " " << v[i][1] << "\n";
  292. std::cout << "e" << std::endl;
  293. }
  294. return 0;
  295. }