[014] 結晶の分子動力学(2次元)

 投稿者:mike  投稿日:2017年 4月16日(日)13時01分35秒
  結晶や液体の分子動力学法(2次元)の雛形プログラムを公開します。
 なるべくプログラムが読みやすいように、高速化や汎用性を狙わずに書いたつもりです。
固体や液体ような原子あるいは分子が凝集した状態の場合によく用いられるMorseポテンシャルを用いています。
 このプログラムでは、鉄原子が集合した金属結晶を構成しています。2次元の場合、3次元のに比べて周りに存在する
原子、あるいは分子の数が異なるため、融点などの物性値は異なります。

表示の説明:
灰色の円はそれぞれの鉄原子を表します。緑や赤、青の線は結合を表し、緑は結合の長さが平衡長さから5%以内であり、
赤は5%以上短く、青は5%以上長いことを表します。

試験環境:
本プログラムは十進BASIC 6.6.2.2 / macOS 10.7.5, 十進BASIC Ver 7.8.0 / windows 10 でテストしました。

--------------

!
! ========= molecular dynamics 2D - Morse potential ==========
!
! 014crystalMMD2D.bas
! Mitsuru Ikeuchi (C) Copyleft
!
! ver 0.0.1   2017.04.16  created
!
OPTION ARITHMETIC NATIVE
DECLARE EXTERNAL SUB mmd2d.setInitialCondition, mmd2d.moveParticles, mmd2d.drawParticles
LET tempMode = 0    !tempMode: 0:adiabatic  1:constant temperature
LET contTemp = 300  !contTemp: controled constant temperature(K)
LET drawMode = 1    !drawMode: 0:bond  1:circle+bond

!setInitialCondition(molKind,boxSizeInNM,xtalSizeInNM,contTemp)
CALL setInitialCondition(3,6.0,2.5,contTemp) !molKind: 3:Fe

LET t0 = TIME
FOR it = 1 TO 1000
   CALL moveParticles(tempMode,contTemp)
   CALL drawParticles(tempMode,contTemp,drawMode)
NEXT it
!PRINT TIME - t0
END

! ---------- molecular dynamics 2D - Morse potential ----------
!
!  method: velocity Verlet ( F=m*d^2r/dt^2 -> r(t+dt)=r(t)+v*dt,v=v+(F/m)*dt )
!    (1) vi = vi + (Fi/mi)*(0.5dt)
!    (2) ri = ri + vi*dt
!    (3) calculation Fi <- {r1,r2,...,rn} Fi=sum(Fij,j=1 to n),Fij=F(ri-rj)
!    (4) vi = vi + (Fi/mi)*(0.5dt)
!    (6) goto (1)
!  potential: Morse V(r) = D*((1-EXP(-A*(r-r0)))^2-1)
!                        = D*(EXP(-2*A*(r-r0))-2*EXP(-A*(r-r0)))
!    (D:dissociation energy, r0:bond length, A:width parameter { A=SQR(k/(2*D)) }
!             force F(r) = -dV(r)/dr
!                        = 2*D*A*y*(y-1), y=EXP(-A*(r-r0))
MODULE mmd2d
MODULE OPTION ARITHMETIC NATIVE
PUBLIC SUB setInitialCondition !(molKind,boxSizeInNM,xtalSizeInNM,contTemp)
PUBLIC SUB moveParticles !(tempMode,contTemp)
PUBLIC SUB drawParticles !(drawMode:0:realSpace 1:velocitySpace)
SHARE NUMERIC molecKind, sysTime, dt, nMolec, xMax, yMax, mass, Dmrs, Amrs, r0mrs
SHARE NUMERIC xx(500),yy(500)   ! (xx(i),yy(i))  : position of i-th particle
SHARE NUMERIC vx(500),vy(500)   ! (vx(i),vy(i))  : velocity of i-th particle
SHARE NUMERIC ffx(500),ffy(500) ! (ffx(i),ffy(i)): total force of i-th particle
SHARE NUMERIC molecData(0 TO 18,0 TO 3) ! molecule 0:mass, 1:epsilon, 2:sigma, 3:dt
SHARE STRING molecSTR$(0 TO 18)  ! molec string
LET molecKind = 3          ! 3:Fe
LET sysTime = 0.0          ! system time (s) in the module
LET dt = 5.0*1.0e-15       ! time step (s)
LET nMolec = 100           ! number of particles
LET xMax = 6.0E-9          ! x-Box size (m)
LET yMax = 6.0E-9          ! y-Box size (m)
LET mass = 55.847*1.661e-27! mass of Fe (kg)
LET Dmrs = 0.4174*1.602e-19! D of Morse potential (J)   : energy of dissociation
LET Amrs = 1.3885e10       ! A of Morse potential (1/m) : width parameter
LET r0mrs = 2.845e-10      ! r0 of Morse potential (m)  : bond length
LET molecData(0,0)=0       ! if molecData(0,0)=0 then set molecData(,)

! ---------- set initial condition

EXTERNAL SUB setInitialCondition(molKind,boxSizeInNM,xtalSizeInNM,contTemp)
   DECLARE EXTERNAL SUB setMolecData,setMolecules,ajustVelocity
   DECLARE EXTERNAL FUNCTION setCrystalBlock
   RANDOMIZE
   CALL setMolecData
   LET molecKind = molKind
   LET mass = molecData(molecKind,0)
   LET Dmrs = molecData(molecKind,1)
   LET Amrs = molecData(molecKind,2)
   LET r0mrs = molecData(molecKind,3)
   LET sysTime = 0.0
   ! set particles
   LET xMax = boxSizeInNM*1.0e-9
   LET yMax = boxSizeInNM*1.0e-9
   LET s = 0.5*(boxSizeInNM-xtalSizeInNM)*1.0e-9
   LET nMolec = setCrystalBlock(1, s, s, xtalSizeInNM*1.0e-9, xtalSizeInNM*1.0e-9, PI/4)
   CALL ajustVelocity(contTemp)
   ! set window
   SET WINDOW 0,500,0,500
END SUB

EXTERNAL SUB setMolecData
! Morse potential data
!       0:mass(AU) 1:D(eV)  2:A(m^-1)  3:r0(m)
   DATA   183.85 , 0.9906, 1.4116e10, 3.032e-10 !  0 W
   DATA    95.94 , 0.8032, 1.5079e10, 2.976e-10 !  1 Mo
   DATA    51.996, 0.4414, 1.5721e10, 2.754e-10 !  2 Cr
   DATA    55.847, 0.4174, 1.3885e10, 2.845e-10 !  3 Fe
   DATA    58.71 , 0.4205, 1.4199e10, 2.780e-10 !  4 Ni
   DATA    26.98 , 0.2703, 1.1646e10, 3.253e-10 !  5 Al
   DATA   207.19 , 0.2348, 1.1836e10, 3.733e-10 !  6 Pb
   DATA    63.54 , 0.3429, 1.3588e10, 2.866e-10 !  7 Cu
   DATA   107.87 , 0.3323, 1.3690e10, 3.115e-10 !  8 Ag
   DATA    39.948, 0.0104, 1.3400e10, 3.816e-10 !  9 Ar
   DATA   200.59 , 0.0734, 1.4900e10, 3.255e-10 ! 10 Hg
   DATA    40.08 , 0.1623, 0.8054e10, 4.569e-10 ! 11 Ca
   DATA    87.62 , 0.1513, 0.7878e10, 4.988e-10 ! 12 Sr
   DATA   137.34 , 0.1416, 0.6570e10, 5.373e-10 ! 13 Ba
   DATA    22.99 , 0.0633, 0.5900e10, 5.336e-10 ! 14 Na
   DATA    39.102, 0.0542, 0.4977e10, 6.369e-10 ! 15 K
   DATA    20.183, 0.0031, 1.6500e10, 3.076e-10 ! 16 Ne
   DATA    83.80 , 0.0141, 1.2500e10, 4.097e-10 ! 17 Kr
   DATA   131.30 , 0.0200, 1.2400e10, 4.467e-10 ! 18 Xe

   DATA "W" ,"Mo","Cr","Fe","Ni","Al","Pb","Cu","Ag","Ar","Hg"
   DATA "Ca","Sr","Ba","Na","K" ,"Ne","Kr","Xe"

   IF molecData(0,0)=0 THEN
      MAT READ molecData
      FOR i=0 TO 18
         LET molecData(i,0) = molecData(i,0)*1.661e-27 !mass(AU)--> (kg)
         LET molecData(i,1) = molecData(i,1)*1.602e-19 !eps(eV) --> (J)
      NEXT i
      MAT READ molecSTR$
   END IF
END SUB

EXTERNAL function setCrystalBlock(ii, x0, y0, xLen, yLen, theta)
   DECLARE EXTERNAL SUB setParticle
   LET iip = ii
   LET a = 0.98*r0mrs
   LET b = 0.866025*a
   LET leng = xLen
   IF (leng<yLen) THEN LET leng = yLen
   LET leng = 1.5*leng
   LET nx = INT(leng/b) + 1
   LET ny = INT(leng/a) + 1
   LET sth = SIN(theta)
   LET cth = COS(theta)
   FOR i=0 TO nx-1
      LET x = b*i - leng/2.0
      FOR j=0 TO ny-1
         LET y = a*j - leng/2.0
         IF MOD(i,2)=1 THEN LET y = y + 0.5*a
         LET xp = x0 + xLen/2.0 + cth*x - sth*y
         LET yp = y0 + yLen/2.0 + sth*x + cth*y
         IF (xp>=x0 AND xp<=x0+xLen AND yp>=y0 AND yp<=y0+yLen) THEN
            CALL setParticle(iip, xp, yp)
            LET iip = iip + 1
         END IF
      NEXT j
   NEXT i
   LET setCrystalBlock = iip - 1
end function

EXTERNAL SUB setParticle(i, x, y)
   LET xx(i) = x
   LET yy(i) = y
   LET vx(i) = 200.0*(RND+RND+RND+RND+RND+RND-3)
   LET vy(i) = 200.0*(RND+RND+RND+RND+RND+RND-3)
   LET ffx(i) = 0.0
   LET ffy(i) = 0.0
END SUB

! ---------- move particles

EXTERNAL SUB moveParticles(tempMode,contTemp) !tempMode 0:adiabatic  1:constant-temp
   DECLARE EXTERNAL SUB moveParticlesDT,ajustVelocity
   IF (tempMode=1) THEN CALL ajustVelocity(contTemp)
   FOR i=1 TO 20
      CALL moveParticlesDT
   NEXT i
END SUB

EXTERNAL SUB moveParticlesDT ! velocity Verlet method
   DECLARE EXTERNAL SUB calcForce
   LET a = 0.5*dt/mass
   FOR i=1 TO nMolec
   !LET a = 0.5*dt/mass(i)
      LET vx(i) = vx(i)+a*ffx(i)
      LET vy(i) = vy(i)+a*ffy(i)
      LET xx(i) = xx(i)+vx(i)*dt
      LET yy(i) = yy(i)+vy(i)*dt
   NEXT i
   CALL calcForce
   FOR i=1 TO nMolec
   !LET a = 0.5*dt/mass(i)
      LET vx(i) = vx(i)+a*ffx(i)
      LET vy(i) = vy(i)+a*ffy(i)
   NEXT i
   LET sysTime=sysTime+dt
END SUB

EXTERNAL SUB calcForce
   DECLARE EXTERNAL FUNCTION force,boundaryForce
   LET s = 0.5*3.418e-10
   FOR i=1 TO nMolec
      LET ffx(i) = 0.0
      LET ffy(i) = 0.0
   NEXT i
   FOR i=1 TO nMolec-1
      FOR j=i+1 TO nMolec
         LET xij = xx(i)-xx(j)
         LET yij = yy(i)-yy(j)
         LET rij = SQR(xij*xij+yij*yij)
         LET f = force(rij)
         LET fxij = f*xij/rij
         LET fyij = f*yij/rij
         LET ffx(i) = ffx(i)+fxij
         LET ffy(i) = ffy(i)+fyij
         LET ffx(j) = ffx(j)-fxij
         LET ffy(j) = ffy(j)-fyij
      NEXT j
   NEXT i
   FOR i=1 TO nMolec  ! boundary force
      LET ffx(i) = ffx(i)+boundaryForce(xx(i)+s)+boundaryForce(xx(i)-xMax-s)
      LET ffy(i) = ffy(i)+boundaryForce(yy(i)+s)+boundaryForce(yy(i)-yMax-s)
   NEXT i
END SUB

EXTERNAL FUNCTION force(r) ! force(r) = -dV(r)/dr
   LET y = EXP(-Amrs*(r-r0mrs))
   LET force = 2*Dmrs*Amrs*y*(y-1)
END FUNCTION

EXTERNAL FUNCTION boundaryForce(r)
   LET r6 = (3.418e-10/r)^6
   LET boundaryForce = (24.0*(0.5*1.711e-21)*r6*(2.0*r6-1.0)/r)
END FUNCTION

! ---------- utility

EXTERNAL FUNCTION systemTemprature
   LET kB = 1.38e-23 ! Boltzman's constant (J/K)
   LET ek= 0.0       !kinetic energy (J)
   FOR i=1 TO nMolec
      LET ek = ek + 0.5*mass*(vx(i)^2+vy(i)^2)
   NEXT i
   LET systemTemprature = ek/(nMolec*kB) !2D: E/N=kT, 3D: E/N=(3/2)kT
END FUNCTION

EXTERNAL SUB ajustVelocity(temp)
   DECLARE EXTERNAL FUNCTION systemTemprature
   LET r = sqr(temp/systemTemprature)
   FOR i=1 TO nMolec
      LET vx(i) = r*vx(i)
      LET vy(i) = r*vy(i)
   NEXT i
END SUB

! ---------- draw particles

EXTERNAL SUB drawParticles(tempMode,contTemp,drawMode)
   DECLARE EXTERNAL FUNCTION systemTemprature,maxNearMolec
   DECLARE EXTERNAL sub realSpace,velocitySpace,plotBond
   SET DRAW MODE HIDDEN
   CLEAR
   call plotBond(drawMode)
   !--- draw caption
   SET TEXT HEIGHT 10
   SET TEXT COLOR 1 ! black
   LET tmp$ = "adiabatic   constantTemp  "
   PLOT TEXT, AT  50, 70 ,USING "time =#####.## (ps)   temp =####.## (K)":sysTime*1E12,systemTemprature
   PLOT TEXT, AT  50, 55 ,USING "N =####":nMolec
   PLOT TEXT, AT  50, 40 ,USING "tempMode = ## ":tempMode
   PLOT TEXT, AT 200, 40 :tmp$(tempMode*12+1:tempMode*12+12)
   PLOT TEXT, AT  50, 25 ,USING "controled Temperature =####.# (K)":contTemp
   PLOT TEXT, AT  50, 10 :"2-dimensional molecular dynamics"
   SET DRAW MODE EXPLICIT
END SUB

EXTERNAL sub plotBond(drawMode)
   LET boxSize = 300
   LET mag = boxSize/xMax
   LET xp = 100
   LET yp = 100
   SET LINE COLOR 1 ! black : !--- box
   PLOT LINES: xp,yp; boxSize+xp,yp; boxSize+xp,boxSize+yp; xp,boxSize+yp; xp,yp
   SET TEXT HEIGHT 6
   SET TEXT COLOR 1 ! black
   PLOT TEXT, AT xp,boxSize+2+yp ,USING  "box size =##.# x ##.# (nm)":xMax*1e9,yMax*1e9
   FOR i=1 TO nMolec
      SET LINE COLOR 8 ! gray
      DRAW circle WITH SCALE(r0mrs/2.0*mag)*SHIFT(xx(i)*mag+xp,yy(i)*mag+yp)
   NEXT i
   IF drawMode=1 THEN
      FOR i=1 TO nMolec-1
         FOR j=i TO nMolec
            LET r = SQR((xx(i)-xx(j))*(xx(i)-xx(j))+(yy(i)-yy(j))*(yy(i)-yy(j)))
            IF r<1.2*r0mrs THEN
               IF r<0.93*r0mrs THEN
                  SET LINE COLOR 4 !red 13 !'oleave
               ELSEIF r<1.03*r0mrs THEN
                  SET LINE COLOR 3 !green
               ELSEIF r<1.08*r0mrs THEN
                  SET LINE COLOR 2 !blue
               ELSE
                  SET LINE COLOR 8 !gray
               END IF
               PLOT LINES: xx(i)*mag+xp,yy(i)*mag+yp;xx(j)*mag+xp,yy(j)*mag+yp
            END IF
         NEXT j
      NEXT i
   END IF
END sub

END MODULE
 

戻る