Skip to content

Commit

Permalink
Merge pull request #299 from phonopy/pypolymlp
Browse files Browse the repository at this point in the history
Update phono3py API of pypolymlp
  • Loading branch information
atztogo authored Nov 22, 2024
2 parents 6dff7e9 + c144d8c commit 59dc78e
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 42 deletions.
87 changes: 46 additions & 41 deletions phono3py/api_phono3py.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,14 +54,9 @@
symmetrize_force_constants,
)
from phonopy.interface.fc_calculator import get_fc2
from phonopy.interface.mlp import PhonopyMLP
from phonopy.interface.pypolymlp import (
PypolymlpData,
PypolymlpParams,
develop_mlp_by_pypolymlp,
develop_pypolymlp,
evalulate_pypolymlp,
load_pypolymlp,
parse_mlp_params,
)
from phonopy.structure.atoms import PhonopyAtoms
from phonopy.structure.cells import (
Expand Down Expand Up @@ -721,6 +716,15 @@ def phonon_mlp_dataset(self, mlp_dataset: dict):
self._check_mlp_dataset(mlp_dataset)
self._phonon_mlp_dataset = mlp_dataset

@property
def mlp(self) -> Optional[PhonopyMLP]:
"""Setter and getter of PhonopyMLP dataclass."""
return self._mlp

@mlp.setter
def mlp(self, mlp) -> Optional[PhonopyMLP]:
self._mlp = mlp

@property
def band_indices(self) -> list[np.ndarray]:
"""Setter and getter of band indices.
Expand Down Expand Up @@ -2176,7 +2180,7 @@ def develop_mlp(
params: Optional[Union[PypolymlpParams, dict, str]] = None,
test_size: float = 0.1,
):
"""Develop MLP of pypolymlp.
"""Develop machine learning potential.
Parameters
----------
Expand All @@ -2192,20 +2196,28 @@ def develop_mlp(
if self._mlp_dataset is None:
raise RuntimeError("MLP dataset is not set.")

self._mlp = develop_mlp_by_pypolymlp(
self._mlp = PhonopyMLP(log_level=self._log_level)
self._mlp.develop(
self._mlp_dataset,
self._supercell,
params=params,
test_size=test_size,
log_level=self._log_level,
)

def load_mlp(self, filename: str = "phono3py.pmlp"):
"""Load machine learning potential of pypolymlp."""
self._mlp = load_pypolymlp(filename=filename)
def save_mlp(self, filename: Optional[str] = None):
"""Save machine learning potential."""
if self._mlp is None:
raise RuntimeError("MLP is not developed yet.")

self._mlp.save(filename=filename)

def load_mlp(self, filename: Optional[str] = None):
"""Load machine learning potential."""
self._mlp = PhonopyMLP(log_level=self._log_level)
self._mlp.load(filename=filename)

def evaluate_mlp(self):
"""Evaluate machine learning potential of pypolymlp.
"""Evaluate machine learning potential.
This method calculates the supercell energies and forces from the MLP
for the displacements in self._dataset of type 2. The results are stored
Expand All @@ -2223,9 +2235,7 @@ def evaluate_mlp(self):
if self.supercells_with_displacements is None:
raise RuntimeError("Displacements are not set. Run generate_displacements.")

energies, forces, _ = evalulate_pypolymlp(
self._mlp, self.supercells_with_displacements
)
energies, forces, _ = self._mlp.evaluate(self.supercells_with_displacements)
self.supercell_energies = energies
self.forces = forces

Expand All @@ -2234,7 +2244,7 @@ def develop_phonon_mlp(
params: Optional[Union[PypolymlpParams, dict, str]] = None,
test_size: float = 0.1,
):
"""Develop MLP of pypolymlp for fc2.
"""Develop MLP for fc2.
Parameters
----------
Expand All @@ -2250,31 +2260,28 @@ def develop_phonon_mlp(
if self._phonon_mlp_dataset is None:
raise RuntimeError("MLP dataset is not set.")

if params is not None:
_params = parse_mlp_params(params)
else:
_params = params

disps = self._phonon_mlp_dataset["displacements"]
forces = self._phonon_mlp_dataset["forces"]
energies = self._phonon_mlp_dataset["supercell_energies"]
n = int(len(disps) * (1 - test_size))
train_data = PypolymlpData(
displacements=disps[:n], forces=forces[:n], supercell_energies=energies[:n]
)
test_data = PypolymlpData(
displacements=disps[n:], forces=forces[n:], supercell_energies=energies[n:]
)
self._phonon_mlp = develop_pypolymlp(
self._phonon_mlp = PhonopyMLP(log_level=self._log_level)
self._phonon_mlp.develop(
self._phonon_mlp_dataset,
self._phonon_supercell,
train_data,
test_data,
params=_params,
verbose=self._log_level - 1 > 0,
params=params,
test_size=test_size,
)

def save_phonon_mlp(self, filename: Optional[str] = None):
"""Save machine learning potential."""
if self._mlp is None:
raise RuntimeError("MLP is not developed yet.")

self._phonon_mlp.save(filename=filename)

def load_phonon_mlp(self, filename: Optional[str] = None):
"""Load machine learning potential."""
self._phonon_mlp = PhonopyMLP(log_level=self._log_level)
self._phonon_mlp.load(filename=filename)

def evaluate_phonon_mlp(self):
"""Evaluate the machine learning potential of pypolymlp.
"""Evaluate the machine learning potential.
This method calculates the supercell energies and forces from the MLP
for the displacements in self._dataset of type 2. The results are stored
Expand All @@ -2298,9 +2305,7 @@ def evaluate_phonon_mlp(self):
mlp = self._mlp
else:
mlp = self._phonon_mlp
energies, forces, _ = evalulate_pypolymlp(
mlp, self.phonon_supercells_with_displacements
)
energies, forces, _ = mlp.evaluate(self.phonon_supercells_with_displacements)
self.phonon_supercell_energies = energies
self.phonon_forces = forces

Expand Down
2 changes: 1 addition & 1 deletion phono3py/cui/create_force_constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -533,7 +533,7 @@ def run_pypolymlp_to_compute_forces(
if log_level:
print("Developing MLPs by pypolymlp...", flush=True)
ph3py.develop_mlp(params=mlp_params)
ph3py.mlp.save_mlp(filename=mlp_filename)
ph3py.save_mlp(filename=mlp_filename)
if log_level:
print(f'MLPs were written into "{mlp_filename}"', flush=True)
else:
Expand Down

0 comments on commit 59dc78e

Please sign in to comment.