Advertisement
Guest User

Untitled

a guest
Sep 7th, 2018
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Julia 5.45 KB | None | 0 0
  1. # debug
  2. debug = false
  3.  
  4. # variables
  5. rCut = 8.0
  6. nMol = 3
  7. dt = 10^(-12)
  8. nTime = 10^6
  9.  
  10. # L-J params
  11. ε = 669.44
  12. σ = 3.205*10^(-9)
  13.  
  14. # molecular masses
  15. mass = 2.65*10^(-26)*ones(nMol, 1)
  16.  
  17. # array of molecules' evoulution
  18. r  = zeros(3, nTime + 1, nMol)
  19.  
  20. # let's define the system's coordinate
  21. # 1st molecule is at (0,0,0)
  22. # 2nd molecule is at (1, 1, 1)
  23. r[1, 1, 2] = 1
  24. r[2, 1, 2] = 1
  25. r[3, 1, 2] = 1
  26. # 3nd molecule is at (-1, -1, 1)
  27. r[1, 1, 3] = -1
  28. r[2, 1, 3] = -1
  29. r[3, 1, 3] = 1
  30.  
  31. rv = zeros(3, nTime + 1, nMol)
  32. # let's define the system initial velocities
  33. # 1st molecule moving 1754 m/s along the OX axis
  34. rv[1, 1, 1] = 478
  35. # 2nd molecule moving 1754 m/s along the OZ axis
  36. rv[3, 1, 2] = -478
  37. # 3rd molecule moving at 30 degrees slope towards the (0,0,0)
  38. rv[1, 1, 3] = 413.96
  39. rv[3, 1, 3] = -239
  40.  
  41. rf = zeros(3, nTime + 1, nMol)
  42.  
  43. # computing forces
  44. function computeForce(x, y, z, fx, fy, fz)
  45.     for i = 1:nMol-1
  46.         for j = i+1:nMol
  47.             dx = abs(x[i] - x[j])
  48.             dy = abs(y[i] - y[j])
  49.             dz = abs(z[i] - z[j])
  50.  
  51.             r = sqrt(dx^2 + dy^2 + dz^2)
  52.  
  53.             if debug
  54.                 println("Distance between atom no. ", i, " and no. ", j, " is equal to ", r, ".")
  55.             end
  56.  
  57.             if r < rCut
  58.                 fr2 = σ^2 / r^2
  59.                 fr6 = fr2^3
  60.                 fpr = 48.0 * ε * fr6 * (fr6 - 0.5) / r^2
  61.  
  62.             if debug
  63.                 println("fpr of atoms ", i, " and ", j, " is equal to ", fpr)
  64.             end
  65.  
  66.                 fxi = fpr * dx
  67.                 fyi = fpr * dy
  68.                 fzi = fpr * dz
  69.  
  70.                 if debug
  71.                     println("Component fxi of ", i, " and ", j, " is equal to ", fxi, ".")
  72.                     println("Component fyi of ", i, " and ", j, " is equal to ", fyi, ".")
  73.                     println("Component fzi of ", i, " and ", j, " is equal to ", fzi, ".")
  74.                 end
  75.  
  76.                 fx[i] = fx[i] + fxi
  77.                 fx[j] = fx[j] - fxi
  78.  
  79.                 if debug
  80.                     println("Component fx of ", i, " is equal to ", fx[i], ".")
  81.                     println("Component fx of ", j, " is equal to ", fx[j], ".")
  82.                 end
  83.  
  84.                 fy[i] = fy[i] + fyi
  85.                 fy[j] = fy[j] - fyi
  86.  
  87.                 fz[i] = fz[i] + fzi
  88.                 fz[j] = fz[j] - fzi
  89.             end
  90.         end
  91.     end
  92.  
  93.     if debug
  94.         println("FORCES\nX: ", fx, "\nY: ", fy, "\nZ: ", fz)
  95.     end
  96. end
  97.  
  98. # let's set initial values of forces according to L-J potential
  99. computeForce(r[1, 1, :], r[2, 1, :], r[3, 1, :], rf[1, 1, :], rf[2, 1, :], rf[3, 1, :])
  100.  
  101. # computing force
  102. function Verlet(posVec, velVec, forVec, massVec)
  103.     xPos = 1
  104.     yPos = 2
  105.     zPos = 3
  106.  
  107.     for timeStep = 1:nTime
  108.         if debug
  109.             println("Timestep no. ", timeStep, ".")
  110.         end
  111.  
  112.         for iAtm = 1:nMol
  113.             posVec[xPos, timeStep + 1, iAtm] = posVec[xPos, timeStep, iAtm] + velVec[xPos, timeStep, iAtm] * dt + forVec[xPos, timeStep, iAtm] / (2 * massVec[iAtm]) * dt^2
  114.             posVec[yPos, timeStep + 1, iAtm] = posVec[yPos, timeStep, iAtm] + velVec[yPos, timeStep, iAtm] * dt + forVec[yPos, timeStep, iAtm] / (2 * massVec[iAtm]) * dt^2
  115.             posVec[zPos, timeStep + 1, iAtm] = posVec[zPos, timeStep, iAtm] + velVec[zPos, timeStep, iAtm] * dt + forVec[zPos, timeStep, iAtm] / (2 * massVec[iAtm]) * dt^2
  116.         end
  117.  
  118.         for i = 1:nMol-1
  119.             Threads.@threads for j = i+1:nMol
  120.                 dx = abs(posVec[xPos,  timeStep + 1, i] - posVec[xPos,  timeStep + 1, j])
  121.                 dy = abs(posVec[yPos,  timeStep + 1, i] - posVec[yPos,  timeStep + 1, j])
  122.                 dz = abs(posVec[zPos,  timeStep + 1, i] - posVec[zPos,  timeStep + 1, j])
  123.  
  124.                 r = sqrt(dx^2 + dy^2 + dz^2)
  125.  
  126.                 if debug
  127.                     println("Distance between atom no. ", i, " and no. ", j, " is equal to ", r, ".")
  128.                 end
  129.  
  130.                 if r < rCut
  131.                     fr2 = σ^2 / r^2
  132.                     fr6 = fr2^3
  133.                     fpr = 48.0 * ε * fr6 * (fr6 - 0.5) / r^2
  134.  
  135.                     if debug
  136.                         println("fpr of atoms ", i, " and ", j, " is equal to ", fpr)
  137.                     end
  138.  
  139.                     fxi = fpr * dx
  140.                     fyi = fpr * dy
  141.                     fzi = fpr * dz
  142.  
  143.                     if debug
  144.                         println("Component fxi of ", i, " and ", j, " is equal to ", fxi, ".")
  145.                         println("Component fyi of ", i, " and ", j, " is equal to ", fyi, ".")
  146.                         println("Component fzi of ",
  147.  
  148.                          i, " and ", j, " is equal to ", fzi, ".")
  149.                     end
  150.  
  151.                     forVec[xPos, timeStep, i] = forVec[xPos, timeStep, i] + fxi
  152.                     forVec[xPos, timeStep, j] = forVec[xPos, timeStep, j] - fxi
  153.  
  154.                     if debug
  155.                         println("Component fx of ", i, " is equal to ", forVec[xPos, timeStep, i], ".")
  156.                         println("Component fx of ", j, " is equal to ", forVec[xPos, timeStep, j], ".")
  157.                     end
  158.  
  159.                     forVec[yPos, timeStep, i] = forVec[yPos, timeStep, i] + fyi
  160.                     forVec[yPos, timeStep, j] = forVec[yPos, timeStep, j] - fyi
  161.  
  162.                     forVec[zPos, timeStep, i] = forVec[zPos, timeStep, i] + fzi
  163.                     forVec[zPos, timeStep, j] = forVec[zPos, timeStep, j] - fzi
  164.                 end
  165.             end
  166.         end
  167.         #computeForce(posVec[xPos, trv\imeStep + 1, :], posVec[yPos, timeStep + 1, :], posVec[zPos, timeStep + 1, :], forVec[xPos, timeStep + 1, :], forVec[yPos, timeStep + 1, :], forVec[zPos, timeStep + 1, :])
  168.  
  169.         for iAtm = 1:nMol
  170.             velVec[xPos, timeStep + 1, iAtm] = velVec[xPos, timeStep, iAtm] + (forVec[xPos, timeStep + 1, iAtm] + forVec[xPos, timeStep, iAtm]) / (2 * massVec[iAtm]) * dt
  171.             velVec[yPos, timeStep + 1, iAtm] = velVec[yPos, timeStep, iAtm] + (forVec[yPos, timeStep + 1, iAtm] + forVec[yPos, timeStep, iAtm]) / (2 * massVec[iAtm]) * dt
  172.             velVec[zPos, timeStep + 1, iAtm] = velVec[zPos, timeStep, iAtm] + (forVec[zPos, timeStep + 1, iAtm] + forVec[zPos, timeStep, iAtm]) / (2 * massVec[iAtm]) * dt
  173.         end
  174.  
  175.         if debug
  176.             println("After step no. ", timeStep + 1, " forces are: 1st atom:\n", forVec[:, timeStep + 1, 1], "\n2nd atom:\n", forVec[:, timeStep + 1, 2], "\n3rd atom:\n", forVec[:, timeStep + 1, 3])
  177.         end
  178.     end
  179. end
  180.  
  181. @time Verlet(r, rv, rf, mass)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement