GEMDAT-repos / GEMDAT

Python toolkit for molecular dynamics analysis
https://gemdat.readthedocs.io
Apache License 2.0
21 stars 3 forks source link

Add option to select MSD algorithm #291

Closed SCiarella closed 3 months ago

SCiarella commented 3 months ago

This PR closes #285.

SCiarella commented 3 months ago

This PR also closes #290

SCiarella commented 3 months ago

Refer to this notebook for an overview of this PR

stefsmeets commented 3 months ago

Can you say a little bit what this PR does? What is the effect of the jit on the performance?

SCiarella commented 3 months ago

The goal of this PR is to allow the user to select the desired algorithm to measure the MSD and the MSAD. The same choice is made available for the plotting functions in gemdat.plot.

For the MSD the user can select how many 'starting points' to use via nstarts. The default is to use all the possible starting points, in which case GEMDAT will rely on _fft_mean_squared_displacement. If the user selects a specific value of nstarts, then it is not possible to use the FFT method, so I have implemented _direct_mean_squared_displacement. However, the direct method become extremely slow for large values of nstarts, so I used jit compilation via numba, which makes the code up to 100 times faster (depending on the value of nstarts). The jit compilation requires _direct_mean_squared_displacement to be outside of Trajectory.

The same approach has been implemented for the MSAD (and plots.unit_vector_autocorrelation), where the user can select between the FFT calculation or a direct one. The difference is that instead of nstarts, the MSAD is parallelized over n_tgrid which corresponds to the specific values of $t$ where the MSAD should be measured (the MSAD always assumes nstarts=all).

Additionally, this PR also addresses some minor improvements, like implementing the time units on the msd plots, normalizing the autocorrelation and plotting the error.

SCiarella commented 3 months ago

Update:

After the optimization happening in this PR, GEMDAT will support only the most efficient methods for the MSD and the MSAD, which are the FFT methods.

The results of a comparison available here show that: (1) the MSD direct method converges to the FFT results image (2) the FFT method for MSD is faster image

(3) the MSAD direct method converges toward the FFT results image (4) the FFT method for MSAD is faster image

SCiarella commented 3 months ago

This PR closes #274