|
| 1 | +#!/usr/bin/env python |
| 2 | + |
| 3 | +import pyscf |
| 4 | +from pyscf.pbc.tools.pyscf_ase import PySCF, cell_from_ase |
| 5 | +from ase.build import bulk |
| 6 | +from ase.optimize import BFGS |
| 7 | +from ase.filters import UnitCellFilter, StrainFilter |
| 8 | + |
| 9 | +atoms = bulk('Si', 'diamond', a=5.43) |
| 10 | +# Only .atoms and .a are defined in this cell instance. It's necessary to assign |
| 11 | +# basis set and pseudo potential |
| 12 | +cell = cell_from_ase(atoms) |
| 13 | +cell.basis = 'gth-dzvp' |
| 14 | +cell.pseudo = 'gth-pade' |
| 15 | + |
| 16 | +kmesh = [2, 2, 2] |
| 17 | +kpts = cell.make_kpts(kmesh) |
| 18 | +# Create a PySCF method, which will be used as a template in the following ASE |
| 19 | +# calculator. Parameters (such as convergence tol) can be defined in the |
| 20 | +# template. |
| 21 | +mf = cell.KRKS(xc='pbe', kpts=kpts) |
| 22 | +mf.conv_tol = 1e-6 |
| 23 | + |
| 24 | +# PySCF(method=...) creates an ASE calculator for the specified PySCF method. |
| 25 | +# All methods implemented in PySCF can be used as templates to create a Calculator. |
| 26 | +# However, some methods may not support gradients or stress tensors. Geometry |
| 27 | +# optimization or lattice optimization that rely on these functionalities will |
| 28 | +# be halted. |
| 29 | +atoms.calc = PySCF(method=mf) |
| 30 | + |
| 31 | +# optimize atom positions |
| 32 | +opt = BFGS(atoms, logfile='atom_pos.log') |
| 33 | +opt.run() |
| 34 | +print(atoms.get_positions()) |
| 35 | + |
| 36 | + |
| 37 | +# Optimize both lattice and atom positions |
| 38 | +opt = BFGS(UnitCellFilter(atoms), logfile='lattice_atom.log') |
| 39 | +opt.run() |
| 40 | +print(atoms.get_positions()) |
| 41 | +print(atoms.cell) |
| 42 | + |
| 43 | + |
| 44 | +# Optimize lattice only. Atom positions (fractional coordinates) are frozen. |
| 45 | +opt = BFGS(StrainFilter(atoms), logfile='lattice_atom.log') |
| 46 | +opt.run() |
| 47 | +print(atoms.cell) |
0 commit comments