Skip to content

Commit

Permalink
add spacegroup determination to geometry optimisation
Browse files Browse the repository at this point in the history
  • Loading branch information
alinelena committed May 24, 2024
1 parent f12a5c7 commit 97f4537
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 1 deletion.
21 changes: 20 additions & 1 deletion janus_core/calculations/geom_opt.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,15 @@

from janus_core.helpers.janus_types import ASEOptArgs, ASEWriteArgs
from janus_core.helpers.log import config_logger
from janus_core.helpers.utils import none_to_dict
from janus_core.helpers.utils import none_to_dict, spacegroup


def optimize( # pylint: disable=too-many-arguments,too-many-locals,too-many-branches
struct: Atoms,
fmax: float = 0.1,
steps: int = 1000,
symmetry_precision=0.0001,
angle_precision=-1,
filter_func: Optional[Callable] = DefaultFilter,
filter_kwargs: Optional[dict[str, Any]] = None,
optimizer: Callable = LBFGS,
Expand All @@ -45,6 +47,12 @@ def optimize( # pylint: disable=too-many-arguments,too-many-locals,too-many-bra
Default is 0.1.
steps : int
Set maximum number of optimization steps to run. Default is 1000.
symmetry_precision : float
Set symmtery precision crieria for spacegroup determination.
Default is 0.0001.
angle_precision : float
Set angle precision crieria for spacegroup determination.
Default is =1.
filter_func : Optional[callable]
Apply constraints to atoms through ASE filter function.
Default is `FrechetCellFilter` if available otherwise `ExpCellFilter`.
Expand Down Expand Up @@ -93,6 +101,12 @@ def optimize( # pylint: disable=too-many-arguments,too-many-locals,too-many-bra
log_kwargs.setdefault("name", __name__)
logger = config_logger(**log_kwargs)

sg = spacegroup(struct, symmetry_precision, angle_precision)
message = f"Before optimisation spacegroup {sg}"
print(message)
if logger:
logger.info(message)

if filter_func is not None:
filtered_struct = filter_func(struct, **filter_kwargs)
dyn = optimizer(filtered_struct, **opt_kwargs)
Expand All @@ -114,13 +128,18 @@ def optimize( # pylint: disable=too-many-arguments,too-many-locals,too-many-bra

converged = dyn.run(fmax=fmax, steps=steps)

sg = spacegroup(struct, symmetry_precision, angle_precision)
message = f"After optimisation spacegroup {sg}"
print(message)

# Calculate current maximum force
if filter_func is not None:
max_force = linalg.norm(filtered_struct.get_forces(), axis=1).max()
else:
max_force = linalg.norm(struct.get_forces(), axis=1).max()

if logger:
logger.info(message)
logger.info("Max force: %.6f", max_force)

if not converged:
Expand Down
36 changes: 36 additions & 0 deletions janus_core/helpers/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,42 @@
from pathlib import Path
from typing import Optional

from ase import Atoms
from spglib import get_spacegroup


def spacegroup(
struct: Atoms, sym_precision: float = 0.001, angle_precision: float = -1.0
) -> str:
"""
Determine the spacegroup for a structure.
Parameters
----------
struct : Atoms
Structure as an ase Atoms object.
sym_precision : float
Symmetry precision for spglib symmetry determination.
Default 0.001.
angle_precision : float
Angle precision for spglib symmetry determination.
Default: -1.
Returns
-------
str
Spacegroup name.
"""
return get_spacegroup(
cell=(
struct.get_cell(),
struct.get_scaled_positions(),
struct.get_atomic_numbers(),
),
symprec=sym_precision,
angle_tolerance=angle_precision,
)


def none_to_dict(dictionaries: list[Optional[dict]]) -> list[dict]:
"""
Expand Down

0 comments on commit 97f4537

Please sign in to comment.