amepproject / amep

The Active Matter Evaluation Package (AMEP) - a Python library for the analysis of particle-based and continuum simulation data of soft and active matter systems
https://amepproject.de/
GNU General Public License v3.0
9 stars 2 forks source link

BUG: MSD calculation not possible with pbc=True if no unwrapped coords available #22

Closed hechtprojects closed 1 month ago

hechtprojects commented 2 months ago

Description:

The data availability checks in amep.evaluate.MSD are not compatible with the current unwrapped_coords and nojump_coords methods of a BaseFrame object. Therefore, the checks themselves raise an error and do not allow to calculate the MSD using nojump coordinates for example.

Code for reproduction:

traj = amep.load.traj(
    "./data",
    mode = "lammps",
    dumps = "*.txt"
)
traj.nojump()

msd = amep.evaluate.MSD(traj, pbc=True, use_nojump=True)

Error message:

---------------------------------------------------------------------------
KeyError                                  Traceback (most recent call last)
Cell In[27], line 1
----> 1 msd = amep.evaluate.MSD(traj, pbc=True, use_nojump=True)

File D:\amep\evaluate.py:3812, in MSD.__init__(self, traj, ptype, skip, nav, use_nojump, pbc)
   3807 if self.__use_nojump and self.__traj[0].nojump_coords() is None:
   3808     raise ValueError(
   3809         'No nojump coordinates available. Call traj.nojump() '\
   3810         'to calculate the nojump coordinates.'
   3811     )
-> 3812 elif self.__pbc and self.__traj[0].unwrapped_coords() is None\
   3813 and self.__traj[0].nojump_coords() is None:
   3814     raise ValueError(
   3815         'Neither unwrapped nor nojump coordinates are available. '\
   3816         'Call traj.nojump() to calculate the nojump coordinates or '\
   3817         'do not apply periodic boundary conditions.'
   3818     )
   3819 else:
   3820     # get mode

File D:\amep\base.py:582, in BaseFrame.unwrapped_coords(self, **kwargs)
    566 def unwrapped_coords(self, **kwargs) -> np.ndarray:
    567     """
    568     Returns the unwrapped coordinates of all particles or of 
    569     all particles of a specific particle type.
   (...)
    580 
    581     """
--> 582     return self.__read_data("uwcoords",**kwargs)

File D:\amep\base.py:967, in BaseFrame.__read_data(self, key, ptype, pid)
    964     return data[mask.astype(bool)]
    966 else:
--> 967     raise KeyError(
    968         f"The key {key} does not exist in the frame. "\
    969         "Returning no data!"
    970     )

KeyError: 'The key uwcoords does not exist in the frame. Returning no data!'

Python and AMEP versions:

Python 3.10 AMEP 1.0.1

Additional information:

The data availability checks in amep.evaluate.MSD.__init__ could be done as follows:

# check data availability
        if self.__pbc:
            if self.__use_nojump:
                try:
                    a = self.__traj[0].nojump_coords()
                except:
                    raise ValueError(
                        'No nojump coordinates available. Call traj.nojump() '\
                        'to calculate the nojump coordinates.'
                    )
            else:
                try:
                    a = self.__traj[0].unwrapped_coords()
                except:
                    raise ValueError(
                        'There are no unwrapped coordinates available. '\
                        'Please set use_nojump=True to use nojump coordinates instead '\
                        'or do not apply periodic boundary conditions.'
                    )
        # get mode
        if self.__pbc and self.__use_nojump:
            self.__mode = 'nojump'
        elif self.__pbc:
            self.__mode = 'unwrapped'
        else:
            self.__mode = 'normal'

How did you install AMEP?

from source

hechtprojects commented 1 month ago

This bug is fixed and the fix will be part of patch release v1.0.2.