Sunteți pe pagina 1din 6

Sairam Tangirala

PHYS 8602, set-1

Simple Harmonic Oscillator:


Molecular Dynamics Simulation

Requirement dx p
= (5)
dt m
Study the simple harmonic oscillator problem
using molecular dynamics. dp
= −K ⋅ x (6)
dt
Introduction

The Hamiltonian for the 1-D simple harmonic


Methodology
oscillator can be written as:
A standard method for solving such ordinary
p 2 Kx 2
H= + (1) differential equations is using finite difference
2m 2 methods. Numerical solutions for the equations of
here, motion can be obtained using Euler’s method. The
p = momentum solution is given as:
x = position
K = spring constant ⎛ p (t ) ⎞
m = mass x(t + ∆ ) = x(t ) + ∆ ⋅ ⎜ ⎟ (7)
⎝ m ⎠
For simplicity I chose following initial conditions: p(t + ∆ ) = p(t ) − ∆ ⋅ K ⋅ x(t ) (8)

At ‘t’ = 0.0 (2) The general idea is as follows: given the


molecular positions, velocities, and other dynamic
x(t = 0 ) = 0.0 information at time (t), we attempt to obtain the
position, velocities etc. at a later time (t+ ∆ ), to a
p (t = 0 ) = 4.0
sufficient degree of accuracy. The equations are
solved on a step-by-step basis: generally the time
System parameters: (3) interval ( ∆ ) is significantly smaller than the typical
time taken for the molecule to travel its own length.
K = 1.0
m = 1.0
Results
The system is constrained by the conservation of
energy principle. This can be written in the equation Error in ‘E’ Vs time
as:
p 2 Kx 2 Fig.(1) shows the plot of error in the computed
H= + = E = 8.0 units (4)
2m 2 energy as a function of time for varying time-intervals
( ∆ ). The error in the computed energy increases
monotonically with time for all values of ∆ . For
This system can be solved using Hamiltonian ∆ =0.1 the error increases more prominently as
formalism. The equation of motion can be obtained as compared to smaller time-intervals ( ∆ ’s).
two 1st Order differential equations:
Fig.(1): Error in computed Energy Vs time Fig. (3): Error in computed position Vs time

The error plot can be verified by having a look The Fig.(3) shows a periodic behavior with
at the Fig.(2) showing a comparison of the computed time. The error in position increases with time to a
energies with the analytic value. The analytic energy local maximum and then drops down to near zero, and
is a constant that was calculated earlier using the then increases again. This behavior is observed to
system parameters see eqn.(4). Both the Figs.(1),(2) have a periodicity with time. Also the integrated error
show the same behavior. shows a gradual increase with time.

Here I investigate the reason for this apparent


periodic nature of the error by comparing the
computed positions with the analytic value.

Analytic position can be derived to be:

x = 2 E ⋅ Sin (ω ⋅ t ) (8)
here,
K
ω= = 1.0 and (9)
m
E = 8.0 units

The below Fig.(4) shows the comparison of


analytic positions with the computed positions for
Fig.(2): Comparison of computed ‘E’ with the various ∆ . The computed positions show periodic
analytic ‘E’ deviations with the analytic value. The deviations
become more prominent with the increase of the time-
Error in ‘x’ Vs time interval ∆ . These deviations have a periodic nature
which explains the periodic nature of Fig.(3).
Fig.(3) shows the error in computed ‘x’ as a function
of time for varying ∆ ’s.
Fig.(4): Comparison of computed ‘x’ with the
analytic’x’

Fig.(5): Plot of ‘p’ versus ‘x’


Plot of ‘p’ Vs ‘x’

From the equation of the Hamiltonian eqn.(1) Conclusions


and the system parameters eqn.(3), the plot of ‘p’
versus ‘x’ is expected to be a circle of radius = 4 units. It is seen that for any given time-interval ( ∆ ),
error in the calculated quantities increases as the time
The Fig.(5) shows the plot of ‘p’ versus ‘x’ for progresses. The rate at which the error increases with
various values of ∆ . For ∆ =0.001, the plot is a time depends on the value of ‘ ∆ ’. For lower values of
approximately a circle with radius = 4.0 units, as ‘ ∆ ’, the rate at which the error increases is smaller
expected. But as we go onto the larger ∆ , the nature of than that with a higher ‘ ∆ ’.
the plot changes for being a circle to an ellipse. This
change of the behavior can be explained by the Error arises due to the truncation of Taylor
violation of the energy conservation constraint, series expansion used in calculating ‘x’ and ‘p’ and
introduced as an artifact of the algorithm used in the the discretization of time in the calculations. These are
calculations. inherent disadvantages of the algorithm.

References

[1]. “Computer Simulation of Liquids”, Allen and


Tildesley
//SOURCE CODE

//HEADER FILES USED IN THE PROGRAM

#include<math.h>
#include<stdlib.h>
#include<stdio.h>
#include<limits.h>
#include<time.h>

//USER DEFINED SYSTEM PARAMETERS

#define M 1.0
#define K 1.0
#define INITIAL_T 0.0
#define FINAL_T 10.0
#define ENERGY 8.0
#define INITIAL_X 0.0
#define DELTA 0.01

//INLINE FUNCITONS TO SPEED UP CALCULATIONS

#define INITIAL_P() (double)sqrt( 2*M*(ENERGY-0.5*K*INITIAL_X*INITIAL_X) )


#define OMEGA() (double)sqrt(K/M)

FILE *fp,*error_fp;

//USER DEFINED FUNCTIONS


void file_operation(int mode); //mode=0:open file //mode=1:close file
void do_calculation(void);

//PROGRAM BEGINS HERE


main()
{
fprintf(stderr,"Simulation Started\n");

file_operation(0); // OPEN DATA FILES


do_calculation(); //DO CALCULATIONS
file_operation(1); //SAVE DATA FILES

fprintf(stderr,"Simulation Completed\n");
}
// END OF MAIN PROGRAM
//FUNCTION DEFINITIONS
//THIS FUNCTION PERFORMS THE OPENING AND CLOSING OF DATA FILES

void file_operation(int mode)


{
//mode = 0:open file
//mode = 1:close file
if(mode == 0)
{
fp = fopen("p_vs_x.dat","w");
error_fp = fopen("error.dat","w");

//CHECK IF FILES WERE OPENED CORRECTLY


if( (fp == NULL)||(error_fp == NULL) )
{
fprintf(stderr,"Couldnt open data file\n");
exit(1);
}
}
else if(mode == 1)
{
fclose(fp);
fclose(error_fp);
}
}

//THIS FUNCTION IMPLEMENTS EULER’S ALGORITHM AND


// CALCULATES POSITION AND MOMENTUM AT VARIOUS TIME-STEPS

void do_calculation()
{
double x,p,t,e,analytic_x;
double new_x,new_p,error_e,error_x;

//GET INITIAL CONDITIONS


x = INITIAL_X;
p = INITIAL_P();
t = INITIAL_T;

while(t < FINAL_T) //DO CALCULATION FOR USER-DEFINED TIME INTERVAL


{
new_x=x+DELTA*p/M; //EULERS ALGORITHM IMPLEMENTATION
new_p=p-DELTA*K*x;

e=(0.5/M)*p*p+ (0.5*K*x*x); //ENERGY CALCULATION


error_e=100.0*fabs(ENERGY-e)/ENERGY; //ERROR CALCULATION

analytic_x=sqrt(2.0*ENERGY)*sin(OMEGA()*t); //ANALYTIC POSITION ‘X’


if(analytic_x!=0.0) //AVOID DIVIDE BY ZERO
error_x=100.0*fabs( (analytic_x-x)/analytic_x );
else
error_x=0.0;

fprintf(fp,"%lf\t%lf\t%lf\t%lf\t%lf\n",t,x,p,e,analytic_x); //WRITE DATA FILES


fprintf(error_fp,"%lf\t%lf\t%lf\n",t,error_x,error_e);

t=t+DELTA; //INCREMENT TIME-STEP


x=new_x;
p=new_p;
}
}

S-ar putea să vă placă și