탄소 원자 200개를 2x2x2 nm 크기의 박스에 랜덤하게 배치하고, 온도에 따라 분자 동역학 시뮬레이션을 통해 폴리머를 형성하는 과정을 시뮬레이션하는 코드를 단계별로 작성해보겠습니다. Python과 ase, torchani, numpy 등의 패키지를 사용하여 진행합니다.
1단계: 필요한 모듈 임포트
import numpy as np
import torch
import torchani
from ase import Atoms
from ase.md.langevin import Langevin
from ase import units
from ase.md.velocitydistribution import MaxwellBoltzmannDistribution
from ase.io.trajectory import Trajectory
2단계: 초기 원자 배치 생성
박스 크기를 정의하고, 200개의 탄소 원자를 랜덤하게 배치합니다.
# 박스 크기 정의 (1 nm = 10 Å)
box_size = 20.0 # Ångström 단위로 2 nm
# 원자 수
num_atoms = 200
# 0에서 box_size 사이의 랜덤한 위치 생성
positions = np.random.uniform(0, box_size, (num_atoms, 3))
# 원자 기호 및 셀 설정
symbols = ['C'] * num_atoms
cell = [box_size, box_size, box_size]
# Atoms 객체 생성
atoms = Atoms(symbols=symbols, positions=positions, cell=cell, pbc=[True, True, True])
3단계: 계산기 설정 (torchani의 ani-1xnr 모델 사용)
torchani를 사용하여 ANI 모델을 ASE 계산기로 설정합니다. 여기서는 예제로 ANI-1x 모델을 사용하지만, ani-1xnr 모델 파일이 있다면 해당 경로를 지정하여 로드하면 됩니다.
# ANI 모델 로드
model = torchani.models.ANI1x().ase() # ani-1xnr 모델을 사용할 경우 해당 모델을 로드하도록 수정
atoms.set_calculator(model)
4단계: 원자에 초기 속도 할당
시뮬레이션할 온도에 따라 원자들에 초기 속도를 할당합니다.
temperature = 300 # 켈빈 단위의 온도
# Maxwell-Boltzmann 분포를 사용하여 초기 속도 설정
MaxwellBoltzmannDistribution(atoms, temperature * units.kB)
5단계: 분자동역학 시뮬레이션 설정
Langevin 동역학을 사용하여 시뮬레이션을 설정합니다.
# 시간 간격 및 마찰 계수 설정
time_step = 1 * units.fs # 1 펨토초
friction = 0.02 # 1/fs 단위
dyn = Langevin(atoms, time_step, temperature * units.kB, friction)
에너지와 온도를 모니터링하기 위한 함수를 정의하고, 시뮬레이션 중 주기적으로 호출되도록 설정합니다.
def print_energy(a=atoms):
epot = a.get_potential_energy() / len(a)
ekin = a.get_kinetic_energy() / len(a)
temp = ekin / (1.5 * units.kB * len(a))
print('Energy per atom: Epot = %.3f eV Ekin = %.3f eV Etot = %.3f eV Temperature: %.1f K' %
(epot, ekin, epot + ekin, temp))
# 에너지 모니터링 함수와 트라젝트리 저장 설정
dyn.attach(print_energy, interval=100)
traj = Trajectory('md.traj', 'w', atoms)
dyn.attach(traj.write, interval=10)
6단계: 시뮬레이션 실행
원하는 스텝 수 만큼 시뮬레이션을 실행합니다.
nsteps = 10000 # 시뮬레이션 스텝 수
dyn.run(nsteps)
7단계: 온도 변화에 따른 시뮬레이션 반복 (선택사항)
여러 온도에서 시뮬레이션을 실행하려면 다음과 같이 반복문을 사용합니다.
temperatures = [300, 500, 700, 900, 1100] # 켈빈 단위의 온도 리스트
for T in temperatures:
# 원자에 새로운 속도 할당
MaxwellBoltzmannDistribution(atoms, T * units.kB)
# 새로운 동역학 객체 설정
dyn = Langevin(atoms, time_step, T * units.kB, friction)
# 각 온도에 대한 트라젝토리 파일 설정
traj = Trajectory(f'md_{T}K.traj', 'w', atoms)
dyn.attach(traj.write, interval=10)
print(f'Starting MD at {T} K')
# 시뮬레이션 실행
dyn.run(nsteps)
주의사항:
- 실제로 폴리머화를 시뮬레이션하려면 반응성이 있는 포스필드가 필요합니다.
torchani의 ANI 모델은 본딩 및 디본딩을 직접적으로 다루지는 않으므로, 고온에서 원자 간의 결합 형성을 관찰하려면 추가적인 설정이나 다른 포스필드의 사용이 필요할 수 있습니다.
- 시뮬레이션 결과를 분석하여 폴리머 형성을 확인하려면, 생성된 트라젝토리 파일을 시각화하거나 클러스터링 등의 방법을 적용해야 합니다.
전체 코드:
import numpy as np
import torch
import torchani
from ase import Atoms
from ase.md.langevin import Langevin
from ase import units
from ase.md.velocitydistribution import MaxwellBoltzmannDistribution
from ase.io.trajectory import Trajectory
# 박스 크기 및 원자 수 설정
box_size = 20.0 # Ångström
num_atoms = 200
# 초기 위치 생성
positions = np.random.uniform(0, box_size, (num_atoms, 3))
# Atoms 객체 생성
symbols = ['C'] * num_atoms
cell = [box_size, box_size, box_size]
atoms = Atoms(symbols=symbols, positions=positions, cell=cell, pbc=[True, True, True])
# 계산기 설정
model = torchani.models.ANI1x().ase() # ani-1xnr 모델을 사용할 경우 해당 부분 수정
atoms.set_calculator(model)
# 초기 속도 설정
temperature = 300 # K
MaxwellBoltzmannDistribution(atoms, temperature * units.kB)
# 동역학 설정
time_step = 1 * units.fs
friction = 0.02 # 1/fs
dyn = Langevin(atoms, time_step, temperature * units.kB, friction)
# 에너지 출력 함수
def print_energy(a=atoms):
epot = a.get_potential_energy()
ekin = a.get_kinetic_energy()
temp = ekin / (1.5 * units.kB * len(a))
print('Energy per atom: Epot = %.3f eV Ekin = %.3f eV Etot = %.3f eV Temperature: %.1f K' %
(epot / len(a), ekin / len(a), (epot + ekin) / len(a), temp))
# 에너지 출력 및 트라젝토리 저장 설정
dyn.attach(print_energy, interval=100)
traj = Trajectory('md.traj', 'w', atoms)
dyn.attach(traj.write, interval=10)
# 시뮬레이션 실행
nsteps = 10000
dyn.run(nsteps)
이 코드를 실행하면 지정된 조건에서 분자동역학 시뮬레이션이 진행되며, 원자들의 움직임과 에너지 변화를 관찰할 수 있습니다.