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