gauss_packet.cpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. /*
  2. * gauss_packet.cpp
  3. *
  4. * Schroedinger equation with potential barrier and periodic boundary conditions
  5. * Initial Gauss packet moving to the right
  6. *
  7. * pipe output into gnuplot to see animation
  8. *
  9. * Implementation of Hamilton operator via MTL library
  10. *
  11. * Copyright 2011-2013 Mario Mulansky
  12. * Copyright 2011-2012 Karsten Ahnert
  13. *
  14. * Distributed under the Boost Software License, Version 1.0.
  15. * (See accompanying file LICENSE_1_0.txt or
  16. * copy at http://www.boost.org/LICENSE_1_0.txt)
  17. */
  18. #include <iostream>
  19. #include <complex>
  20. #include <boost/numeric/odeint.hpp>
  21. #include <boost/numeric/odeint/external/mtl4/mtl4.hpp>
  22. #include <boost/numeric/mtl/mtl.hpp>
  23. using namespace std;
  24. using namespace boost::numeric::odeint;
  25. typedef mtl::dense_vector< complex< double > > state_type;
  26. struct hamiltonian {
  27. typedef mtl::compressed2D< complex< double > > matrix_type;
  28. matrix_type m_H;
  29. hamiltonian( const int N ) : m_H( N , N )
  30. {
  31. // constructor with zero potential
  32. m_H = 0.0;
  33. initialize_kinetic_term();
  34. }
  35. //template< mtl::compressed2D< double > >
  36. hamiltonian( mtl::compressed2D< double > &V ) : m_H( num_rows( V ) , num_cols( V ) )
  37. {
  38. // use potential V in hamiltonian
  39. m_H = complex<double>( 0.0 , -1.0 ) * V;
  40. initialize_kinetic_term();
  41. }
  42. void initialize_kinetic_term( )
  43. {
  44. const int N = num_rows( m_H );
  45. mtl::matrix::inserter< matrix_type , mtl::update_plus< complex<double> > > ins( m_H );
  46. const double z = 1.0;
  47. // fill diagonal and upper and lower diagonal
  48. for( int i = 0 ; i<N ; ++i )
  49. {
  50. ins[ i ][ (i+1) % N ] << complex< double >( 0.0 , -z );
  51. ins[ i ][ i ] << complex< double >( 0.0 , z );
  52. ins[ (i+1) % N ][ i ] << complex< double >( 0.0 , -z );
  53. }
  54. }
  55. void operator()( const state_type &psi , state_type &dpsidt , const double t )
  56. {
  57. dpsidt = m_H * psi;
  58. }
  59. };
  60. struct write_for_gnuplot
  61. {
  62. size_t m_every , m_count;
  63. write_for_gnuplot( size_t every = 10 )
  64. : m_every( every ) , m_count( 0 ) { }
  65. void operator()( const state_type &x , double t )
  66. {
  67. if( ( m_count % m_every ) == 0 )
  68. {
  69. //clog << t << endl;
  70. cout << "p [0:" << mtl::size(x) << "][0:0.02] '-'" << endl;
  71. for( size_t i=0 ; i<mtl::size(x) ; ++i )
  72. {
  73. cout << i << "\t" << norm(x[i]) << "\n";
  74. }
  75. cout << "e" << endl;
  76. }
  77. ++m_count;
  78. }
  79. };
  80. static const int N = 1024;
  81. static const int N0 = 256;
  82. static const double sigma0 = 20;
  83. static const double k0 = -1.0;
  84. int main( int argc , char** argv )
  85. {
  86. state_type x( N , 0.0 );
  87. // initialize gauss packet with nonzero velocity
  88. for( int i=0 ; i<N ; ++i )
  89. {
  90. x[i] = exp( -(i-N0)*(i-N0) / ( 4.0*sigma0*sigma0 ) ) * exp( complex< double >( 0.0 , k0*i ) );
  91. //x[i] += 2.0*exp( -(i+N0-N)*(i+N0-N) / ( 4.0*sigma0*sigma0 ) ) * exp( complex< double >( 0.0 , -k0*i ) );
  92. }
  93. x /= mtl::two_norm( x );
  94. typedef runge_kutta4< state_type > stepper;
  95. // create potential barrier
  96. mtl::compressed2D< double > V( N , N );
  97. V = 0.0;
  98. {
  99. mtl::matrix::inserter< mtl::compressed2D< double > > ins( V );
  100. for( int i=0 ; i<N ; ++i )
  101. {
  102. //ins[i][i] << 1E-4*(i-N/2)*(i-N/2);
  103. if( i < N/2 )
  104. ins[ i ][ i ] << 0.0 ;
  105. else
  106. ins[ i ][ i ] << 1.0 ;
  107. }
  108. }
  109. // perform integration, output can be piped to gnuplot
  110. integrate_const( stepper() , hamiltonian( V ) , x , 0.0 , 1000.0 , 0.1 , write_for_gnuplot( 10 ) );
  111. clog << "Norm: " << mtl::two_norm( x ) << endl;
  112. return 0;
  113. }