Luthaf / rascaline

Computing representations for atomistic machine learning
https://luthaf.fr/rascaline/
BSD 3-Clause "New" or "Revised" License
44 stars 13 forks source link

Faulty shape of SphericalExpansion #294

Closed sirmarcel closed 5 months ago

sirmarcel commented 5 months ago

Hello, I think I've found a bug in rascaline while trying to write a test for torch_spex: For a test system with 3 atoms and 2 species, the output of SphericalExpansion has the wrong shape (in some cases):

from ase import Atoms
import numpy as np

import rascaline

atoms = Atoms(
    positions=np.array([[0, 0, 0], [1, 0, 0], [0, 0, 1]]),
    numbers=[1, 1, 2],
    cell=np.eye(3) * 10,
    pbc=True,
)

cutoff = 1.1
max_angular = 6
max_radial = 8

spliner = rascaline.utils.SoapSpliner(
    cutoff=cutoff,
    max_radial=max_radial,
    max_angular=max_angular,
    basis=rascaline.utils.SphericalBesselBasis(
        cutoff=cutoff, max_radial=max_radial, max_angular=max_angular
    ),
    density=rascaline.utils.DeltaDensity(),
    accuracy=1e-8,
)

splined_basis = spliner.compute()

calculator = rascaline.SphericalExpansion(
    cutoff=cutoff,
    max_radial=max_radial,
    max_angular=max_angular,
    center_atom_weight=0.0,
    radial_basis=splined_basis,
    atomic_gaussian_width=-1.0,  # will not be used due to the delta density above
    cutoff_function={"ShiftedCosine": {"width": 0.5}},
)

d1 = calculator.compute([atoms])

assert (
    d1[
        {
            "spherical_harmonics_l": 0,
            "species_center": 1,
            "species_neighbor": 1,
        }
    ].values.shape[0]
    == 2
)  # -> this passes

assert (
    d1[
        {
            "spherical_harmonics_l": 0,
            "species_center": 1,
            "species_neighbor": 2,
        }
    ].values.shape[0]
    == 2
)  # -> this fails

Since there are two atoms of species 1, the shape should, as far as I understand, always be [2, ...], not [1, ...]. Note also that this only occurs for different center and neighbor species.

System info:

sirmarcel commented 5 months ago

Okay, this is due to atom number 1 not seeing number 2 due to the cutoff. Changing it to something larger than sqrt(2) will "fix" this issue. I guess this is essentially due to a different treatment of "empty" results than I expected -- so probably nothing that needs to be fixed. Feel free to close.

Luthaf commented 5 months ago

Right, only the actual neighbors are included in the outptut by default. The idea was that people can use selected_samples if they want to make sure all samples are always included (even if they would be zero everywhere). See https://luthaf.fr/rascaline/latest/how-to/sample-selection.html for a quick tutorial on this.

Luthaf commented 5 months ago

Closing, this is working as intended 😃