-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Shyue Ping Ong
committed
Aug 9, 2023
1 parent
62c1e31
commit a385ed9
Showing
3 changed files
with
103 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
"""Calculators for EOS and associated properties.""" | ||
|
||
from __future__ import annotations | ||
|
||
import numpy as np | ||
from pymatgen.analysis.eos import BirchMurnaghan | ||
|
||
from .base import PropCalc | ||
from .relaxation import RelaxCalc | ||
|
||
|
||
class EOSCalc(PropCalc): | ||
"""Equation of state calculator.""" | ||
|
||
def __init__( | ||
self, | ||
calculator, | ||
relax_structure: bool = True, | ||
fmax: float = 0.1, | ||
steps: int = 500, | ||
n_points: int = 11, | ||
): | ||
""" | ||
Args: | ||
calculator: ASE Calculator to use. | ||
relax_structure: Whether to first relax the structure. Set to False if structures provided are pre-relaxed | ||
with the same calculator. | ||
fmax (float): Max force for relaxation (of structure as well as atoms). | ||
steps (int): Max number of steps for relaxation. | ||
n_points (int): Number of points in which to compute the EOS. Defaults to 11. | ||
""" | ||
self.calculator = calculator | ||
self.relax_structure = relax_structure | ||
self.n_points = n_points | ||
self.fmax = fmax | ||
self.steps = steps | ||
|
||
def calc(self, structure): | ||
"""Fit the Birch-Murnaghan equation of state. | ||
Args: | ||
structure: A Structure object. | ||
Returns: | ||
{ | ||
"EOS": { | ||
"volumes": volumes, | ||
"energies": energies | ||
}, | ||
"K": bm.b0, | ||
} | ||
""" | ||
if self.relax_structure: | ||
relaxer = RelaxCalc(self.calculator, fmax=self.fmax, steps=self.steps) | ||
structure = relaxer.calc(structure)["final_structure"] | ||
|
||
volumes, energies = [], [] | ||
relaxer = RelaxCalc(self.calculator, fmax=self.fmax, steps=self.steps, relax_cell=False) | ||
for idx in np.linspace(-0.1, 0.1, self.n_points): | ||
structure_strained = structure.copy() | ||
structure_strained.apply_strain([idx, idx, idx]) | ||
result = relaxer.calc(structure_strained) | ||
volumes.append(result["final_structure"].volume) | ||
energies.append(result["energy"]) | ||
bm = BirchMurnaghan(volumes=volumes, energies=energies) | ||
bm.fit() | ||
|
||
return { | ||
"EOS": {"volumes": volumes, "energies": energies}, | ||
"K (GPa)": bm.b0_GPa, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
"""Tests for PhononCalc class""" | ||
from __future__ import annotations | ||
|
||
import pytest | ||
|
||
from matcalc.eos import EOSCalc | ||
|
||
|
||
def test_PhononCalc(Li2O, LiFePO4, M3GNetUPCalc): | ||
"""Tests for PhononCalc class""" | ||
calculator = M3GNetUPCalc | ||
# Note that the fmax is probably too high. This is for testing purposes only. | ||
pcalc = EOSCalc(calculator) | ||
results = pcalc.calc(Li2O) | ||
|
||
assert results["K (GPa)"] == pytest.approx(69.86879801931632, 1e-4) | ||
|
||
results = list(pcalc.calc_many([Li2O, LiFePO4])) | ||
assert len(results) == 2 | ||
assert results[1]["K (GPa)"] == pytest.approx(60.083101510774476, 1e-4) |