resizing_lattice.cpp 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. /*
  2. * resizing_lattice.cpp
  3. *
  4. * Demonstrates the usage of resizing of the state type during integration.
  5. * Examplary system is a strongly nonlinear, disordered Hamiltonian lattice
  6. * where the spreading of energy is investigated
  7. *
  8. * Copyright 2011-2012 Mario Mulansky
  9. * Copyright 2012-2013 Karsten Ahnert
  10. * Distributed under the Boost Software License, Version 1.0. (See
  11. * accompanying file LICENSE_1_0.txt or copy at
  12. * http://www.boost.org/LICENSE_1_0.txt)
  13. *
  14. */
  15. #include <iostream>
  16. #include <utility>
  17. #include <boost/numeric/odeint.hpp>
  18. #include <boost/ref.hpp>
  19. #include <boost/random.hpp>
  20. using namespace std;
  21. using namespace boost::numeric::odeint;
  22. //[ resizing_lattice_system_class
  23. typedef vector< double > coord_type;
  24. typedef pair< coord_type , coord_type > state_type;
  25. struct compacton_lattice
  26. {
  27. const int m_max_N;
  28. const double m_beta;
  29. int m_pot_start_index;
  30. vector< double > m_pot;
  31. compacton_lattice( int max_N , double beta , int pot_start_index )
  32. : m_max_N( max_N ) , m_beta( beta ) , m_pot_start_index( pot_start_index ) , m_pot( max_N )
  33. {
  34. srand( time( NULL ) );
  35. // fill random potential with iid values from [0,1]
  36. boost::mt19937 rng;
  37. boost::uniform_real<> unif( 0.0 , 1.0 );
  38. boost::variate_generator< boost::mt19937&, boost::uniform_real<> > gen( rng , unif );
  39. generate( m_pot.begin() , m_pot.end() , gen );
  40. }
  41. void operator()( const coord_type &q , coord_type &dpdt )
  42. {
  43. // calculate dpdt = -dH/dq of this hamiltonian system
  44. // dp_i/dt = - V_i * q_i^3 - beta*(q_i - q_{i-1})^3 + beta*(q_{i+1} - q_i)^3
  45. const int N = q.size();
  46. double diff = q[0] - q[N-1];
  47. for( int i=0 ; i<N ; ++i )
  48. {
  49. dpdt[i] = - m_pot[m_pot_start_index+i] * q[i]*q[i]*q[i] -
  50. m_beta * diff*diff*diff;
  51. diff = q[(i+1) % N] - q[i];
  52. dpdt[i] += m_beta * diff*diff*diff;
  53. }
  54. }
  55. void energy_distribution( const coord_type &q , const coord_type &p , coord_type &energies )
  56. {
  57. // computes the energy per lattice site normalized by total energy
  58. const size_t N = q.size();
  59. double en = 0.0;
  60. for( size_t i=0 ; i<N ; i++ )
  61. {
  62. const double diff = q[(i+1) % N] - q[i];
  63. energies[i] = p[i]*p[i]/2.0
  64. + m_pot[m_pot_start_index+i]*q[i]*q[i]*q[i]*q[i]/4.0
  65. + m_beta/4.0 * diff*diff*diff*diff;
  66. en += energies[i];
  67. }
  68. en = 1.0/en;
  69. for( size_t i=0 ; i<N ; i++ )
  70. {
  71. energies[i] *= en;
  72. }
  73. }
  74. double energy( const coord_type &q , const coord_type &p )
  75. {
  76. // calculates the total energy of the excitation
  77. const size_t N = q.size();
  78. double en = 0.0;
  79. for( size_t i=0 ; i<N ; i++ )
  80. {
  81. const double diff = q[(i+1) % N] - q[i];
  82. en += p[i]*p[i]/2.0
  83. + m_pot[m_pot_start_index+i]*q[i]*q[i]*q[i]*q[i] / 4.0
  84. + m_beta/4.0 * diff*diff*diff*diff;
  85. }
  86. return en;
  87. }
  88. void change_pot_start( const int delta )
  89. {
  90. m_pot_start_index += delta;
  91. }
  92. };
  93. //]
  94. //[ resizing_lattice_resize_function
  95. void do_resize( coord_type &q , coord_type &p , coord_type &distr , const int N )
  96. {
  97. q.resize( N );
  98. p.resize( N );
  99. distr.resize( N );
  100. }
  101. //]
  102. const int max_N = 1024;
  103. const double beta = 1.0;
  104. int main()
  105. {
  106. //[ resizing_lattice_initialize
  107. //start with 60 sites
  108. const int N_start = 60;
  109. coord_type q( N_start , 0.0 );
  110. q.reserve( max_N );
  111. coord_type p( N_start , 0.0 );
  112. p.reserve( max_N );
  113. // start with uniform momentum distribution over 20 sites
  114. fill( p.begin()+20 , p.end()-20 , 1.0/sqrt(20.0) );
  115. coord_type distr( N_start , 0.0 );
  116. distr.reserve( max_N );
  117. // create the system
  118. compacton_lattice lattice( max_N , beta , (max_N-N_start)/2 );
  119. //create the stepper, note that we use an always_resizer because state size might change during steps
  120. typedef symplectic_rkn_sb3a_mclachlan< coord_type , coord_type , double , coord_type , coord_type , double ,
  121. range_algebra , default_operations , always_resizer > hamiltonian_stepper;
  122. hamiltonian_stepper stepper;
  123. hamiltonian_stepper::state_type state = make_pair( q , p );
  124. //]
  125. //[ resizing_lattice_steps_loop
  126. double t = 0.0;
  127. const double dt = 0.1;
  128. const int steps = 10000;
  129. for( int step = 0 ; step < steps ; ++step )
  130. {
  131. stepper.do_step( boost::ref(lattice) , state , t , dt );
  132. lattice.energy_distribution( state.first , state.second , distr );
  133. if( distr[10] > 1E-150 )
  134. {
  135. do_resize( state.first , state.second , distr , state.first.size()+20 );
  136. rotate( state.first.begin() , state.first.end()-20 , state.first.end() );
  137. rotate( state.second.begin() , state.second.end()-20 , state.second.end() );
  138. lattice.change_pot_start( -20 );
  139. cout << t << ": resized left to " << distr.size() << ", energy = " << lattice.energy( state.first , state.second ) << endl;
  140. }
  141. if( distr[distr.size()-10] > 1E-150 )
  142. {
  143. do_resize( state.first , state.second , distr , state.first.size()+20 );
  144. cout << t << ": resized right to " << distr.size() << ", energy = " << lattice.energy( state.first , state.second ) << endl;
  145. }
  146. t += dt;
  147. }
  148. //]
  149. cout << "final lattice size: " << distr.size() << ", final energy: " << lattice.energy( state.first , state.second ) << endl;
  150. }