Guest User

Lennard Jones

a guest
Dec 24th, 2013
256
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
MatLab 6.01 KB | None | 0 0
  1. %% Clear variables
  2. clear all;
  3.  
  4. %% Parameters
  5. N = 3000;    %Steps
  6. Dt = 10^-12;   %Delta t
  7. movie = 0; % create video of movement
  8.  
  9. %Potentiaal
  10. eps = 1;
  11. sigma = 1;
  12. Rc = 2.5*sigma;
  13. m = 1;
  14. %Deeltjes en volume
  15. Num = 20; % Number of particles
  16. rho = 0.2;
  17. L = (Num/rho)^(1/3);
  18.  
  19. Np = round(Num^(1/3)); %Number of planes in cubic lattice
  20.  
  21. %Beginsettings en herschalingsparam
  22. T0 = 2;
  23. tau = 10^2; %Characteristic time until Tkin ~ T0 !!
  24. state = 1; %Rescaling on => state != 0, off => state == 0;
  25.  
  26. %arrays
  27. x = zeros(N,Np^3);
  28. y = x;
  29. z = x;
  30.  
  31. Vx = zeros(N,Np^3);
  32. Vy = Vx;
  33. Vz = Vx;
  34.  
  35. Ax = zeros(1,Np^3);
  36. Ay = Ax;
  37. Az = Ax;
  38.  
  39. K = zeros(N,1);
  40. P = K;
  41. E = K;
  42.  
  43. T = zeros(N,1); %Kinetic temperature
  44. %% Initialize positions
  45. g = 1;
  46. for ii = 1:Np
  47.     for jj = 1:Np
  48.         for kk = 1:Np
  49.             x(1,g) = (ii-1)*L/(Np);
  50.             y(1,g) = (jj-1)*L/(Np);
  51.             z(1,g) = (kk-1)*L/(Np);
  52.            
  53.            g = g+1;
  54.            if g == Np^3
  55.                break;
  56.            end
  57.         end
  58.         if g == Np^3
  59.                break;
  60.         end
  61.     end
  62.     if g == Np^3
  63.                break;
  64.     end
  65.            
  66. end
  67.  
  68. %% initialize velocity
  69.  
  70. Vx(1,:) = sqrt(T0)*randn(Np^3,1).';
  71. Vy(1,:) = sqrt(T0)*randn(Np^3,1).';
  72. Vz(1,:) = sqrt(T0)*randn(Np^3,1).';
  73.  
  74. som = sum(Vx(1,:))+sum(Vy(1,:))+sum(Vz(1,:));
  75. shift = som/(3*Np^3)*ones(1,Np^3);
  76. Vx(1,:) = Vx(1,:)-shift;
  77. Vy(1,:) = Vy(1,:)-shift;
  78. Vz(1,:) = Vz(1,:)-shift;
  79.  
  80. som = sum(Vx(1,:))+sum(Vy(1,:))+sum(Vz(1,:));
  81.  
  82. temp = 1/(3*Np^3)*sum(Vx(1,:).*Vx(1,:)+Vy(1,:).*Vy(1,:)+Vz(1,:).*Vz(1,:));
  83. Vx(1,:) = Vx(1,:)/sqrt(temp/T0);
  84. Vz(1,:) = Vy(1,:)/sqrt(temp/T0);
  85. Vz(1,:) = Vz(1,:)/sqrt(temp/T0);
  86. temp2 = 1/(3*Np^3)*sum(Vx(1,:).*Vx(1,:)+Vy(1,:).*Vy(1,:)+Vz(1,:).*Vz(1,:));
  87. K(1) = 0.5*m*sum(Vx(1,:).*Vx(1,:)+Vy(1,:).*Vy(1,:)+Vz(1,:).*Vz(1,:));
  88. E(1) = K(1);
  89. delta = zeros(N,Np^3,Np^3);
  90. %% Main loop
  91. teller = 0;
  92. h = waitbar(0,'Progress 0%%', 'Name', 'Time evolution');
  93. for ii=2:N
  94.      
  95.       % Step 1 of velocity-verlet
  96.       x(ii,:) = x(ii-1,:)+Vx(ii-1,:)*Dt+(Ax(1,:)/2)*Dt^2;
  97.       y(ii,:) = y(ii-1,:)+Vy(ii-1,:)*Dt+(Ay(1,:)/2)*Dt^2;
  98.       z(ii,:) = z(ii-1,:)+Vz(ii-1,:)*Dt+(Az(1,:)/2)*Dt^2;
  99.      
  100.       % Apply PBC
  101.      
  102.       x(ii,:) = x(ii,:)-L*floor(x(ii,:)/L);
  103.       y(ii,:) = y(ii,:)-L*floor(y(ii,:)/L);
  104.       z(ii,:) = z(ii,:)-L*floor(z(ii,:)/L);
  105.      
  106.       % Intermediate update of the velocities
  107.       Vx(ii,:) = Vx(ii-1,:)+Dt*Ax(1,:)/2;
  108.       Vy(ii,:) = Vy(ii-1,:)+Dt*Ay(1,:)/2;
  109.       Vz(ii,:) = Vz(ii-1,:)+Dt*Az(1,:)/2;
  110.      
  111.       % Calculate new accelerations
  112.      
  113.       % Reset accelerations
  114.       Ax = zeros(1,Np^3);
  115.       Ay = Ax;
  116.       Az = Ax;
  117.       scale = zeros(N,1);
  118.       % Interactions
  119.       for jj = 1:Np^3
  120.         for kk = jj+1:Np^3
  121.                
  122.                 deltaX = x(ii,jj)-x(ii,kk);
  123.                 deltaY = y(ii,jj)-y(ii,kk);
  124.                 deltaZ = z(ii,jj)-z(ii,kk);
  125.                
  126.                 % Second part of PBC
  127.                 deltaX = deltaX - round(deltaX/L)*L;
  128.                 deltaY = deltaY - round(deltaY/L)*L;
  129.                 deltaZ = deltaZ - round(deltaZ/L)*L;
  130.                
  131.                
  132.                 r = sqrt(deltaX^2+deltaY^2+deltaZ^2);
  133.                 delta(ii,jj,kk) = r;
  134.                 if  r < Rc
  135.                     Ax(1,jj) = Ax(1,jj)+24*eps/m*(-sigma^6/r^8+2*sigma^12/r^14)*(deltaX);
  136.                     Ay(1,jj) = Ay(1,jj)+24*eps/m*(-sigma^6/r^8+2*sigma^12/r^14)*(deltaY);
  137.                     Az(1,jj) = Az(1,jj)+24*eps/m*(-sigma^6/r^8+2*sigma^12/r^14)*(deltaZ);
  138.                
  139.                     Ax(1,kk) = Ax(1,kk)-24*eps/m*(-sigma^6/r^8+2*sigma^12/r^14)*(deltaX);
  140.                     Ay(1,kk) = Ay(1,kk)-24*eps/m*(-sigma^6/r^8+2*sigma^12/r^14)*(deltaY);
  141.                     Az(1,kk) = Az(1,kk)-24*eps/m*(-sigma^6/r^8+2*sigma^12/r^14)*(deltaZ);
  142.                    
  143.                     P(ii) = P(ii)+ (4*eps*((sigma/r)^12-(sigma/r)^6)-4*eps*((sigma/Rc)^12-(sigma/Rc)^6));
  144.                 end
  145.            
  146.         end
  147.       end
  148.      
  149.       % Second update of velocity
  150.      
  151.       Vx(ii,:) = Vx(ii,:)+Ax(1,:)*Dt/2;
  152.       Vy(ii,:) = Vy(ii,:)+Ay(1,:)*Dt/2;
  153.       Vz(ii,:) = Vz(ii,:)+Az(1,:)*Dt/2;
  154.      
  155.       if Vx(ii,:)~=Vx(ii-1,:)
  156.           teller = teller+1;
  157.       end
  158.       waitbar(ii/N,h,sprintf('Progress %d%%',round(ii/N*100)));
  159.       K(ii) = 0.5*m*sum(Vx(ii,:).*Vx(ii,:)+Vy(ii,:).*Vy(ii,:)+Vz(ii,:).*Vz(ii,:),2);
  160.      
  161.      
  162.       T(ii) = K(ii)/(3*Np^3); % Kinetic temperature before rescaling
  163.       % rescaling
  164.       if state == 0
  165.           scale(ii) = 1;
  166.       else
  167.           scale(ii) = sqrt( 1 + 1/tau*(T0/T(ii)-1));
  168.       end
  169.       Vx(ii,:) = scale(ii)*Vx(ii,:);
  170.       Vy(ii,:) = scale(ii)*Vy(ii,:);
  171.       Vz(ii,:) = scale(ii)*Vz(ii,:);
  172.       % recalculate Kinetic energy
  173.       K(ii) = 0.5*m*sum(Vx(ii,:).*Vx(ii,:)+Vy(ii,:).*Vy(ii,:)+Vz(ii,:).*Vz(ii,:),2);
  174.      
  175.       E(ii) = K(ii)+P(ii);
  176.      
  177. end
  178. close(h);
  179. %% plotting
  180. figure(1);clf;hold on;view(3)
  181. scatter3(x(1,:),y(1,:),z(1,:)) % Show initial positions, the cubic grid at the center
  182. scatter3(x(10,:),y(10,:),z(10,:),'fill') % Show positions at the end of the simulation
  183. scatter3([0 0 0 0 L L 0 L L L/2],[0 L L 0 0 0 0 L L L/2], [0 0 L L 0 L L 0 L L/2],100, 'fill'); % Show corners of cubic volume
  184. %axis([0 L 0 L 0 L])
  185.  
  186. %% Conservation of energy
  187.  
  188.  
  189. figure(2);clf;hold on;
  190. time = linspace(0,Dt*N,N);
  191.  
  192. subplot(4,1,1)
  193. plot(time,E, '.r');
  194. title('Total energy');
  195. subplot(4,1,2)
  196. plot(time,P, '.-b');
  197. title('Scale factor');
  198. subplot(4,1,3)
  199. plot(time,K, 'm');
  200. title('Kinetic energy');
  201. subplot(4,1,4)
  202. plot(time,T);
  203. title('Temperature')
  204.  
  205. %% Movement
  206.  
  207. if movie > 0
  208. figure(3)
  209. hold off;
  210.  
  211. h = waitbar(0,'Progress 0%%', 'Name', 'Time evolution');
  212. for ii = 1:N
  213.     grid off;
  214.     scatter3([0 0 0 0 L L 0 L L L/2],[0 L L 0 0 0 0 L L L/2], [0 0 L L 0 L L 0 L L/2],100, 'fill');
  215.     scatter3(x(ii,:),y(ii,:),z(ii,:),'fill');
  216.     axis([0 L 0 L 0 L])
  217.     title('Lennard-Jones liquid');
  218.     M(ii) = getframe;
  219.     waitbar(ii/N,h,sprintf('Progress %d%%',round(ii/N*100)));
  220. end
  221. close(h);
  222. end
Advertisement
Add Comment
Please, Sign In to add comment