Structures and energetics
Stress tensor and cell optimization
Get the stress tensor and optimize atomic positions and the lattice together with an ASE cell filter.
Besides energies and forces, AOFDFTCalculator provides the stress tensor (implemented_properties = ["energy", "free_energy", "forces", "stress"]). Combined with an ASE cell filter, atomic positions and lattice constants can be optimized together.
"""
Advance/OF-DFT example: optimize atomic positions and cell using the stress tensor.
Copyright (C) 2026 AdvanceSoft Corporation, All rights reserved.
"""
from ase.build import bulk
from ase.io import write
from ase.optimize import BFGS
from ase.units import GPa
try:
from ase.filters import FrechetCellFilter as CellFilter # ASE >= 3.23
except ImportError:
from ase.constraints import ExpCellFilter as CellFilter
from aofdft.calculator import AOFDFTCalculator
VPS_DIR = "path/to/DFT_DATA19_AS/VPS" # edit these two paths for your environment
PAO_DIR = "path/to/DFT_DATA19_AS/PAO"
atoms = bulk("Al", "fcc", a=4.05).repeat((3, 3, 3)) # 3x3x3 primitive cells, 27 atoms
atoms.calc = AOFDFTCalculator(
console = False,
logging_file = "al_cell_opt.log",
kin_type = "AS26",
kin_model = "Crystal26",
vps_dir = VPS_DIR,
pao_dir = PAO_DIR,
max_cell_strain = 0.05, # the FFT grid is rebuilt when the cell deforms more than this
)
# forces and stress are obtained by automatic differentiation
stress = atoms.get_stress() # Voigt order (xx, yy, zz, yz, xz, xy), eV/Angstrom^3
print(f"Initial pressure: {-stress[:3].mean() / GPa:8.3f} GPa")
# relax the cell and the positions at the same time
opt = BFGS(CellFilter(atoms), logfile="al_cell_opt.txt")
opt.run(fmax=0.02, steps=50)
a, b, c, alpha, beta, gamma = atoms.cell.cellpar()
stress = atoms.get_stress()
print(f"Final pressure : {-stress[:3].mean() / GPa:8.3f} GPa")
print(f"Cell lengths : {a:.4f} {b:.4f} {c:.4f} Angstrom")
print(f"Cell angles : {alpha:.2f} {beta:.2f} {gamma:.2f} deg")
volume = atoms.get_volume() / len(atoms)
print(f"Volume : {volume:.3f} Angstrom^3/atom")
print(f"Lattice constant: {(4.0 * volume) ** (1.0 / 3.0):.3f} Angstrom (of the cubic fcc cell)")
write("al_optimized.cif", atoms)
Example output
Results with v1.0.0 (Crystal26).
Initial pressure: 2.048 GPa
Final pressure : -0.025 GPa
Cell lengths : 8.6260 8.6260 8.6260 Angstrom
Cell angles : 60.00 60.00 60.00 deg
Volume : 16.809 Angstrom^3/atom
Lattice constant: 4.066 Angstrom (of the cubic fcc cell)
With v1.0.0 (AS26) the stress does not agree with the volume derivative of the energy. The lattice constant from the cell optimization (4.066 Angstrom in this example) therefore differs from the one that minimizes the energy (4.034 Angstrom, see Equation of state and bulk modulus). When you need a lattice constant, check it against the energy-volume relation as well.
Notes
- The cell is a 3×3×3 repetition of the fcc primitive cell of Al (27 atoms). With v1.0.0 the results change when the cell has a short period, as the primitive cell (1 atom) does.
atoms.get_stress()returns the six Voigt components (xx, yy, zz, yz, xz, xy) in eV/Angstrom³. Divide byase.units.GPato convert to GPa. The pressure is minus the mean of the diagonal components.- Forces and stress are obtained by automatic differentiation of the total energy with respect to atomic positions and lattice vectors.
- While the cell deformation is small, the calculation keeps the number of FFT grid points. When the deformation exceeds
max_cell_strain(0.05 by default), the grid is regenerated from the cutoff energy. - To optimize only the volume while keeping the cell shape, pass
hydrostatic_strain=Trueto the cell filter. - To obtain the equilibrium volume and bulk modulus from the energy-volume relation, see Equation of state and bulk modulus.