def getLJeigenvaluesB(X, S, epsilon, sigma, rc, getForceMatrix): from ase import Atoms from ase.build import bulk from ase.calculators.lj import LennardJones from ase.phonons import Phonons import numpy as np from scipy import linalg as LA calc = LennardJones(sigma=sigma, epsilon=epsilon, rc=rc) # chemStr = 'H' + str(len(X)) # atoms = Atoms(chemStr, X, calculator=calc ) atoms = Atoms(getChemStr(S), X, calculator=calc) energy = atoms.get_potential_energy() eig = [] if getForceMatrix: ph = Phonons(atoms, calc) ph.run() ph.read(acoustic=True) ph.clean() f = ph.get_force_constant() (l, m, n) = f.shape if l == 1: ff = np.reshape(f, (m, n)) else: print("error") # eig = LA.eigvalsh(ff) # eig is a numpy array # return energy, [float("{0:.5f}".format(eig[i])) for i in range(len(eig))]
def _calculate_finite_difference_hessian(self, atoms, calculator): """Calcualte the Hessian matrix using finite differences.""" ph = Phonons(atoms, calculator, supercell=(1, 1, 1), delta=1e-6) ph.clean() ph.run() ph.read(acoustic=False) ph.clean() H_numerical = ph.get_force_constant()[0, :, :] return H_numerical
def test_hessian(self): for calc in [{(1, 1): LennardJonesQuadratic(1, 1, 3), (1, 2): LennardJonesQuadratic(1.5, 0.8, 2.4), (2, 2): LennardJonesQuadratic(0.5, 0.88, 2.64)}]: atoms = io.read("KA256_Min.xyz") atoms.center(vacuum=5.0) b = calculator.PairPotential(calc) H_analytical = b.calculate_hessian_matrix(atoms, "dense") # Numerical ph = Phonons(atoms, b, supercell=(1, 1, 1), delta=0.001) ph.run() ph.read(acoustic=False) ph.clean() H_numerical = ph.get_force_constant()[0, :, :] self.assertArrayAlmostEqual(H_analytical, H_numerical, tol=0.03)
def test_hessian(self): for calc in [{ (1, 1): LennardJonesQuadratic(1, 1, 3), (1, 2): LennardJonesQuadratic(1.5, 0.8, 2.4), (2, 2): LennardJonesQuadratic(0.5, 0.88, 2.64) }]: atoms = io.read("KA256_Min.xyz") atoms.center(vacuum=5.0) b = calculator.PairPotential(calc) H_analytical = b.calculate_hessian_matrix(atoms, "dense") # Numerical ph = Phonons(atoms, b, supercell=(1, 1, 1), delta=0.001) ph.run() ph.read(acoustic=False) ph.clean() H_numerical = ph.get_force_constant()[0, :, :] self.assertArrayAlmostEqual(H_analytical, H_numerical, tol=0.03)
def calculate_phonons(x): # Setup crystal and EMT calculator atoms = bulk('Al', 'fcc', a=x) #4.05) # Phonon calculator N = 7 ph = Phonons(atoms, EMT(), supercell=(N, N, N), delta=0.05) ph.run() # Read forces and assemble the dynamical matrix ph.read(acoustic=True) ph.clean() path = atoms.cell.bandpath('GXULGK', npoints=100) bs = ph.get_band_structure(path) dos = ph.get_dos(kpts=(20, 20, 20)).sample_grid(npts=100, width=1e-3) forces = ph.get_force_constant() print(forces) # Plot the band structure and DOS: import matplotlib.pyplot as plt fig = plt.figure(1, figsize=(8, 4), dpi=300) ax = fig.add_axes([.12, .07, .67, .85]) emax = 0.035 bs.plot(ax=ax, emin=-0.01, emax=emax) dosax = fig.add_axes([.8, .07, .17, .85]) dosax.fill_between(dos.weights[0], dos.energy, y2=0, color='grey', edgecolor='k', lw=1) dosax.set_ylim(-0.01, emax) dosax.set_yticks([]) dosax.set_xticks([]) dosax.set_xlabel("DOS", fontsize=18) fig.savefig('Al_phonon.png') return
def getLJeigenvalues(listOfPositions, epsilon, sigma, rc, getForceMatrix): from ase import Atoms from ase.build import bulk from ase.calculators.lj import LennardJones from ase.phonons import Phonons import numpy as np from scipy import linalg as LA # from gpaw import GPAW, FermiDirac # calc = LennardJones() #a.set_calculator(calc) # atoms = bulk('Si', 'diamond', a=5.4) # atoms = bulk('H', 'fcc', a=1.1, cubic=True) #atoms = Atoms('N3', [(0, 0, 0), (0, 0, 1.1), (0, 0, 2.2)], calculator=LennardJones() ) # atoms = Atoms('H2', [(0, 0, 0), (0, 0, 1.12246)], calculator=LennardJones() ) # calc = GPAW(kpts=(5, 5, 5), h=0.2, occupations=FermiDirac(0.)) chemStr = 'H' + str(len(listOfPositions)) calc = LennardJones(sigma=sigma, epsilon=epsilon, rc=rc) atoms = Atoms(chemStr, listOfPositions, calculator=calc) energy = atoms.get_potential_energy() eig = [] if getForceMatrix: ph = Phonons(atoms, calc) ph.run() ph.read(acoustic=True) ph.clean() f = ph.get_force_constant() # f # f.size (l, m, n) = f.shape if l == 1: ff = np.reshape(f, (m, n)) else: print("error") # # ff eig = LA.eigvalsh(ff) # eig is a numpy array # return energy, [float("{0:.5f}".format(eig[i])) for i in range(len(eig))]
def getEnergyAndEigen(self, aseStruct): aseStruct.set_calculator(self.calc) energy = aseStruct.get_potential_energy() eig = [] ph = Phonons(aseStruct, self.calc) ph.run() ph.read(acoustic=True) ph.clean() f = ph.get_force_constant() (l, m, n) = f.shape if l == 1: ff = np.reshape(f, (m, n)) else: print("error") # eig = LA.eigvalsh(ff) # eig is a numpy array # return energy, [ float("{0:.5f}".format(eig[i])) for i in range(len(eig)) ]
def getLJeigenvalues2B(X, S, epsilon, sigma, rc, getForceMatrix, aCell): from ase import Atoms from ase.build import bulk from ase.phonons import Phonons import numpy as np from scipy import linalg as LA from ase import Atom, Atoms from lammpslib import LAMMPSlib # chemStr = 'H' + str(len(X)) # struct = Atoms(chemStr, X, cell=(aCell, aCell, aCell), pbc=True) struct = Atoms(getChemStr(S), X, cell=(aCell, aCell, aCell), pbc=True) lammps_header = ["units metal"] cmds = [ "pair_style mlip /Users/chinchay/Documents/9_Git/reverseEnergyPartitioning/mlip_LJ.ini",\ "pair_coeff * * " ] mylammps = LAMMPSlib( lmpcmds=cmds, atom_types={1: 1}, keep_alive=True, log_file= '/Users/chinchay/Documents/9_Git/reverseEnergyPartitioning/log.txt') struct.set_calculator(mylammps) energy = struct.get_potential_energy() eig = [] if getForceMatrix: ph = Phonons(struct, mylammps) ph.run() ph.read(acoustic=True) ph.clean() f = ph.get_force_constant() (l, m, n) = f.shape if l == 1: ff = np.reshape(f, (m, n)) else: print("error") # eig = LA.eigvalsh(ff) # eig is a numpy array # return energy, [float("{0:.5f}".format(eig[i])) for i in range(len(eig))]
def test_phonon_md_init(asap3): # Tests the phonon-based perturbation and velocity distribution # for thermal equilibration in MD. EMT = asap3.EMT rng = RandomState(17) atoms = bulk('Pd') atoms *= (3, 3, 3) avail = [atomic_numbers[sym] for sym in ['Ni', 'Cu', 'Pd', 'Ag', 'Pt', 'Au']] atoms.numbers[:] = rng.choice(avail, size=len(atoms)) atoms.calc = EMT() opt = FIRE(atoms, trajectory='relax.traj') opt.run(fmax=0.001) positions0 = atoms.positions.copy() phonons = Phonons(atoms, EMT(), supercell=(1, 1, 1), delta=0.05) try: phonons.run() phonons.read() # Why all this boilerplate? finally: phonons.clean() matrices = phonons.get_force_constant() K = matrices[0] T = 300 * units.kB atoms.calc = EMT() Epotref = atoms.get_potential_energy() temps = [] Epots = [] Ekins = [] Etots = [] for i in range(24): PhononHarmonics(atoms, K, T, quantum=True, rng=np.random.RandomState(888 + i)) Epot = atoms.get_potential_energy() - Epotref Ekin = atoms.get_kinetic_energy() Ekins.append(Ekin) Epots.append(Epot) Etots.append(Ekin + Epot) temps.append(atoms.get_temperature()) atoms.positions[:] = positions0 # The commented code would produce displacements/velocities # resolved over phonon modes if we borrow some expressions # from the function. Each mode should contribute on average # equally to both Epot and Ekin/temperature # # atoms1.calc = EMT() # atoms1 = atoms.copy() # v_ac = np.zeros_like(positions0) # D_acs, V_acs = ... # for s in range(V_acs.shape[2]): # atoms1.positions += D_acs[:, :, s] # v_ac += V_acs[:, :, s] # atoms1.set_velocities(v_ac) # X1.append(atoms1.get_potential_energy() - Epotref) # X2.append(atoms1.get_kinetic_energy()) print('energies', Epot, Ekin, Epot + Ekin) Epotmean = np.mean(Epots) Ekinmean = np.mean(Ekins) Tmean = np.mean(temps) Terr = abs(Tmean - T / units.kB) relative_imbalance = abs(Epotmean - Ekinmean) / (Epotmean + Ekinmean) print('epotmean', Epotmean) print('ekinmean', Ekinmean) print('rel imbalance', relative_imbalance) print('Tmean', Tmean, 'Tref', T / units.kB, 'err', Terr) assert Terr < 0.1*T / units.kB, Terr # error in Kelvin for instantaneous velocity # Epot == Ekin give or take 2 %: assert relative_imbalance < 0.1, relative_imbalance if 0: import matplotlib.pyplot as plt I = np.arange(len(Epots)) plt.plot(I, Epots, 'o', label='pot') plt.plot(I, Ekins, 'o', label='kin') plt.plot(I, Etots, 'o', label='tot') plt.show()
def getLJeigenvalues2(listOfPositions, epsilon, sigma, rc, getForceMatrix, aCell): from ase import Atoms from ase.build import bulk # from ase.calculators.lj import LennardJones from ase.phonons import Phonons import numpy as np from scipy import linalg as LA # from gpaw import GPAW, FermiDirac # calc = LennardJones() #a.set_calculator(calc) # atoms = bulk('Si', 'diamond', a=5.4) # atoms = bulk('H', 'fcc', a=1.1, cubic=True) #atoms = Atoms('N3', [(0, 0, 0), (0, 0, 1.1), (0, 0, 2.2)], calculator=LennardJones() ) # atoms = Atoms('H2', [(0, 0, 0), (0, 0, 1.12246)], calculator=LennardJones() ) # calc = GPAW(kpts=(5, 5, 5), h=0.2, occupations=FermiDirac(0.)) # d = 1.122 # = 2**(1/6) # a = 10.00 # struct = Atoms( 'H2', positions=[(0, 0, 0), (0, 0, d)] , cell=(a, a, a), pbc=True ) chemStr = 'H' + str(len(listOfPositions)) # struct = Atoms(chemStr, listOfPositions, cell=(aCell, aCell, aCell)) # <<< without pbc=True you would need a very large aCell value! struct = Atoms(chemStr, listOfPositions, cell=(aCell, aCell, aCell), pbc=True) # struct = Atoms(chemStr, positions=positions , cell=(aCell, aCell, aCell), pbc=True ) ############################################################################ # from ase.calculators.lj import LennardJones # calc = LennardJones(sigma=sigma, epsilon=epsilon, rc=rc) # struct = Atoms(chemStr, listOfPositions, calculator=calc ) ############################################################################ from ase import Atom, Atoms from lammpslib import LAMMPSlib # lammps_header=['units metal' ,\ # 'boundary p p p ' ,\ # "atom_style atomic" ,\ # "atom_modify map hash" ] lammps_header = ["units metal"] cmds = [ "pair_style mlip /Users/chinchay/Documents/9_Git/reverseEnergyPartitioning/mlip_LJ.ini",\ "pair_coeff * * " ] # cmds = ["pair_style mlip /Users/chinchay/Documents/9_Git/reverseEnergyPartitioning/mlip_LJ.ini",\ # "pair_coeff * * " ,\ # "neighbor 1.5 bin " ] # cmds = ["pair_style mlip /Users/chinchay/Documents/9_Git/reverseEnergyPartitioning/mlip_test.ini",\ # "pair_coeff * * " ,\ # "neighbor 1.5 bin " ] mylammps = LAMMPSlib( lmpcmds=cmds, atom_types={1: 1}, keep_alive=True, log_file= '/Users/chinchay/Documents/9_Git/reverseEnergyPartitioning/log.txt') # struct = Atoms(chemStr, listOfPositions, calculator=mylammps ) struct.set_calculator(mylammps) ############################################################################ energy = struct.get_potential_energy() eig = [] if getForceMatrix: # ph = Phonons(struct, calc) ph = Phonons(struct, mylammps) ph.run() ph.read(acoustic=True) ph.clean() f = ph.get_force_constant() # f # f.size (l, m, n) = f.shape if l == 1: ff = np.reshape(f, (m, n)) else: print("error") # # ff eig = LA.eigvalsh(ff) # eig is a numpy array # return energy, [float("{0:.5f}".format(eig[i])) for i in range(len(eig))]
avail = [atomic_numbers[sym] for sym in ['Ni', 'Cu', 'Pd', 'Ag', 'Pt', 'Au']] atoms.numbers[:] = rng.choice(avail, size=len(atoms)) atoms.calc = EMT() opt = FIRE(atoms, trajectory='relax.traj') opt.run(fmax=0.001) positions0 = atoms.positions.copy() phonons = Phonons(atoms, EMT(), supercell=(1, 1, 1), delta=0.05) try: phonons.run() phonons.read() # Why all this boilerplate? finally: phonons.clean() matrices = phonons.get_force_constant() K = matrices[0] T = 300 * units.kB atoms.calc = EMT() Epotref = atoms.get_potential_energy() temps = [] Epots = [] Ekins = [] Etots = [] for i in range(24): PhononHarmonics(atoms, K,
import dill as pickle from ase.build import bulk from gpaw import GPAW, FermiDirac from ase.phonons import Phonons import ase import numpy as np atoms = ase.io.read("moo3_bulk.cif") calc = GPAW(symmetry={'point_group': False}, mode='lcao', basis='szp(dzp)', kpts=(6, 6, 3), convergence={'density': 1e-7}, xc='PBE', # No PBEsol :( occupations=FermiDirac(0.01)) ph = Phonons(atoms, calc, supercell=(3, 3, 2), delta=0.05) ph.run() ph.read(acoustic=True) force_constants = ph.get_force_constant() ph.acoustic(force_constants) force_constants = ph.symmetrize(force_constants) np.save("force_constant.npy", force_constants) with open('phonons.pkl', 'wb') as file: pickle.dump(ph, file)