|
| 1 | +"""The ASE MD simulator. |
| 2 | +
|
| 3 | +References |
| 4 | +---------- |
| 5 | +https://wiki.fysik.dtu.dk/ase/tutorials/md/md.html |
| 6 | +
|
| 7 | +""" |
| 8 | +import typing |
| 9 | + |
| 10 | +import ase |
| 11 | +import ase.geometry.analysis |
| 12 | +import numpy as np |
| 13 | +import tqdm |
| 14 | +from ase import units |
| 15 | +from ase.calculators.emt import EMT |
| 16 | +from ase.lattice.cubic import FaceCenteredCubic |
| 17 | +from ase.md.velocitydistribution import MaxwellBoltzmannDistribution |
| 18 | +from ase.md.verlet import VelocityVerlet |
| 19 | + |
| 20 | + |
| 21 | +def generate_atoms(size: int = 3) -> ase.Atoms: |
| 22 | + """Generate fcc of Cu.""" |
| 23 | + return FaceCenteredCubic( |
| 24 | + directions=[[1, 0, 0], [0, 1, 0], [0, 0, 1]], |
| 25 | + symbol="Cu", |
| 26 | + size=(size, size, size), |
| 27 | + pbc=True, |
| 28 | + ) |
| 29 | + |
| 30 | + |
| 31 | +def printenergy(a: ase.Atoms) -> None: |
| 32 | + """Function to print the potential, kinetic and total energy.""" |
| 33 | + epot = a.get_potential_energy() / len(a) |
| 34 | + ekin = a.get_kinetic_energy() / len(a) |
| 35 | + print( |
| 36 | + "Energy per atom: Epot = %.3feV Ekin = %.3feV (T=%3.0fK) Etot = %.3feV" |
| 37 | + % (epot, ekin, ekin / (1.5 * units.kB), epot + ekin) |
| 38 | + ) |
| 39 | + |
| 40 | + |
| 41 | +def run_simulation( |
| 42 | + atoms: ase.Atoms, |
| 43 | + temperature: float = 300, |
| 44 | + timestep: float = 5.0, |
| 45 | + steps: int = 20, |
| 46 | + dump_interval: int = 10, |
| 47 | +) -> typing.List[ase.Atoms]: |
| 48 | + """Run an MD Simulation using ase.""" |
| 49 | + # Describe the interatomic interactions with the Effective Medium Theory |
| 50 | + atoms.calc = EMT() |
| 51 | + |
| 52 | + # Set the momenta corresponding to T=300K |
| 53 | + MaxwellBoltzmannDistribution(atoms, temperature_K=temperature) |
| 54 | + |
| 55 | + # We want to run MD with constant energy using the VelocityVerlet algorithm. |
| 56 | + dyn = VelocityVerlet(atoms, timestep * units.fs) # fs time step. |
| 57 | + |
| 58 | + atoms_list = [] |
| 59 | + |
| 60 | + # Now run the dynamics |
| 61 | + printenergy(atoms) |
| 62 | + for _ in tqdm.trange(steps, ncols=100): |
| 63 | + dyn.run(dump_interval) |
| 64 | + printenergy(atoms) |
| 65 | + atoms_list.append(atoms.copy()) |
| 66 | + |
| 67 | + return atoms_list |
| 68 | + |
| 69 | + |
| 70 | +def compute_rdf( |
| 71 | + atoms_list: typing.List[ase.Atoms], rmax: float, nbins: int, elements: str |
| 72 | +) -> dict: |
| 73 | + """Compute RDF.""" |
| 74 | + analysis = ase.geometry.analysis.Analysis(atoms_list) |
| 75 | + data = analysis.get_rdf(rmax=rmax, nbins=nbins, elements=elements) |
| 76 | + return {"x": np.linspace(0, rmax, nbins), "y": np.mean(data, axis=0)} |
0 commit comments