SINGROUP / dscribe

DScribe is a python package for creating machine learning descriptors for atomistic systems.
https://singroup.github.io/dscribe/
Apache License 2.0
384 stars 87 forks source link

Inconsistency of Numerical SOAP Derivative with Finite-Difference Test #146

Open collinlewin opened 2 weeks ago

collinlewin commented 2 weeks ago

Hello, I would greatly appreciate any feedback on why Dscribe seems to be inconsistent with my own finite difference test for computing the derivatives, equal to an non-negligible error on the order of 10^-2. Specifically, I was interested in comparing the derivative of the atoms with respect to the x direction of the first atom (atom index 0, hence why centers = [0]) produced by DScribe vs. my own finite difference test. I perturbed the position of atom 0 along the x direction by +1e-8 and -1e-8 as two new structure files, and then compute the difference between them to estimate the derivative. That is:

$$ \frac{\partial p}{\partial x_0} = \frac{p(x_0 + \Delta x) - p(x_0 - \Delta x)}{2h} $$

The result of this test differs from the results of fitsnap by roughly 1e-2, using the derivatives function please. Perhaps I am missing something? Thank you very much.

   structure array was imported from the following files in the following order : 
        ['OUTCAR_Cfg1+x', 'OUTCAR_Cfg1-x', 'OUTCAR_Cfg1', 'OUTCAR_Cfg101']

    deltax = 1e-8

    for i in range(13):

        deriv_i = soap.derivatives(structures[2], 
            centers = [0],
            include = [i],
            method = 'numerical',
            attach = True,
            return_descriptor = True
            )

        finite_deriv_i = (soap_input[0,i] - soap_input[1,i]) / 2 / deltax

if relevant, here are my SOAP parameters, which shouldn't matter?

        soap = SOAP(
            species=species,
            periodic=True,
            sigma=0.3,
            r_cut=7,
            n_max=12,
            l_max=12,
        )
lauri-codes commented 2 days ago

Hi @collinlewin!

Could you post the full script you are using to create and compare the derivatives? Something that I could run directly to reproduce your problem?

Note that when calculating numerical derivatives, you have to be careful with floating point precision: too small $\Delta x$ will cause problems, and also you should always use double-precision numbers.