Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: non-pbc handle #50

Merged
merged 3 commits into from
Dec 4, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 36 additions & 17 deletions src/mattersim/datasets/utils/convertor.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# -*- coding: utf-8 -*-
import warnings
from typing import Optional, Tuple

import ase
Expand All @@ -10,6 +11,9 @@

from .threebody_indices import compute_threebody as _compute_threebody

# Ensure the warning is only shown once
warnings.filterwarnings("once", category=UserWarning)

"""
Supported Properties:
- "num_nodes"(set by default) ## int
Expand Down Expand Up @@ -110,24 +114,13 @@ def get_fixed_radius_bonding(
Returns:
center_indices, neighbor_indices, images, distances
"""
if isinstance(structure, Atoms):
pbc_ = np.array(structure.pbc, dtype=int)
if np.all(pbc_ < 0.1) or not pbc:
lattice_matrix = np.array(
[[1000.0, 0.0, 0.0], [0.0, 1000.0, 0.0], [0.0, 0.0, 1000.0]],
dtype=float,
)
pbc_ = np.array([0, 0, 0], dtype=int)
else:
lattice_matrix = np.ascontiguousarray(
structure.cell[:], dtype=float
) # noqa: E501
pbc_ = np.array(structure.pbc, dtype=int)

cart_coords = np.ascontiguousarray(
np.array(structure.positions), dtype=float
) # noqa: E501
else:
raise ValueError("structure type not supported")
lattice_matrix = np.ascontiguousarray(structure.cell[:], dtype=float) # noqa: E501

cart_coords = np.ascontiguousarray(
np.array(structure.positions), dtype=float
) # noqa: E501
r = float(cutoff)

(
Expand Down Expand Up @@ -192,6 +185,32 @@ def convert(
pbc: bool, whether to use periodic boundary condition, default True
"""
# normalize the structure
if isinstance(atoms, Atoms):
pbc_ = np.array(atoms.pbc, dtype=int)
if np.all(pbc_ < 0.01) or not pbc:
min_x = np.min(atoms.positions[:, 0])
min_y = np.min(atoms.positions[:, 1])
min_z = np.min(atoms.positions[:, 2])
max_x = np.max(atoms.positions[:, 0])
max_y = np.max(atoms.positions[:, 1])
max_z = np.max(atoms.positions[:, 2])
x_len = max((max_x - min_x) * 10, 1000)
y_len = max((max_y - min_y) * 10, 1000)
z_len = max((max_z - min_z) * 10, 1000)
lattice_matrix = np.array(
[[x_len, 0.0, 0.0], [0.0, y_len, 0.0], [0.0, 0.0, z_len]],
dtype=float,
)
pbc_ = np.array([1, 1, 1], dtype=int)
warnings.warn("No PBC detected, using a large supercell", UserWarning)
atoms.set_cell(lattice_matrix)
atoms.set_pbc(pbc_)
else:
if np.all(atoms.cell < 1e-5):
raise ValueError("Cell vectors are too small")
else:
raise ValueError("structure type not supported")

scaled_pos = atoms.get_scaled_positions()
scaled_pos = np.mod(scaled_pos, 1)
atoms.set_scaled_positions(scaled_pos)
Expand Down
Loading