
     i#                     H   d Z ddlZ	 ddlZddlZdZnO# e$ rG dZddlZ G d d          Z ej	        d          Z ej	        d          e_
        eej
        _        Y nw xY wd	d
lmZ ddlmZ  G d dej                  Z G d dej
        j                  ZddedefdZdS )aT  GSD trajectory reader  --- :mod:`MDAnalysis.coordinates.GSD`
============================================================

Class to read the GSD trajectory, output of `HOOMD-blue`_. The GSD format
specifies both the topology and the trajectory of the particles in the
simulation. The topology is read by the
:class:`~MDAnalysis.topology.GSDParser.GSDParser` class.

The GSD format was developed having in mind the possibility of changing number
of particles, particle types, particle identities and changing topology.
Currently this class has limited functionality, due to the fact that the number
of particles and the topology are kept fixed in most MD simulations. The user
will get an error only if at any time step the number of particles is detected
to be different to the one that was set at the first time step. No check on
changes in particle identity or topology is currently implemented.

.. _`HOOMD-blue`: https://glotzerlab.engin.umich.edu/hoomd-blue/

Classes
-------

.. autoclass:: GSDReader
   :inherited-members:

.. autoclass:: GSDPicklable
   :members:

.. autofunction:: gsd_pickle_open

    NTFc                       e Zd ZdS )MockHOOMDTrajectoryN)__name__
__module____qualname__     d/srv/www/vhosts/g4struct/public_html/venv/lib/python3.11/site-packages/MDAnalysis/coordinates/GSD.pyr   r   >   s        r	   r   gsdhoomd   )base)store_init_argumentsc                   t     e Zd ZdZdZdddZe fd            Zd Zd Z	e
d             Zd	 Zd
 Zd Z xZS )	GSDReaderzReader for the GSD format.GSDN)timelengthc                 J   t           sd}t          |           t          t          |           j        |fi | || _        |                                  | j        d         j        j	        | _
         | j        | j
        fi | j        | _        |                                  dS )a  
        Parameters
        ----------
        filename : str
            trajectory filename
        **kwargs : dict
            General reader arguments.


        .. versionadded:: 0.17.0
        .. versionchanged:: 2.0.0
            Now use a picklable :class:`gsd.hoomd.HOOMDTrajectory`--
            :class:`GSDPicklable`
        .. versionchanged:: 2.6.0
           Support for GSD versions below 3.0.1 have been dropped. This
           includes support for schema 1.3.
        z0GSDReader: To read GSD files, please install gsdr   N)HAS_GSDImportErrorsuperr   __init__filenameopen_trajectory_file	particlesNn_atoms	_Timestep
_ts_kwargsts_read_next_timestep)selfr   kwargserrmsg	__class__s       r
   r   zGSDReader.__init__Q   s    &  	&GFf%%%'i';;F;;; z!}.0 $.AAAA  """""r	   c                 J    d| _         t          | j        d          | _        dS )z0opens the trajectory file using gsd.hoomd moduler)modeN)_framegsd_pickle_openr   r   r$   s    r
   r   zGSDReader.open_trajectoryo   s#    $T]===


r	   c                 B    | j         j                                         dS )zclose readerN)r   filecloser.   s    r
   r1   zGSDReader.closet   s    
r	   c                 *    t          | j                  S )znumber of frames in trajectory)lenr   r.   s    r
   n_frameszGSDReader.n_framesx   s     4:r	   c                 V    |                                   |                                  dS )zreopen trajectoryN)r1   r   r.   s    r
   _reopenzGSDReader._reopen}   s'    

r	   c                     	 | j         |         }n# t          $ r	 t          d w xY w|| _        || j        _        |j        j        | j        j        d<   |j        j	        | j        _
        t          j        t          j        | j        j
        dd                              | j        j
        dd <   |j        j        }|j        d         }|| j        k    rt%          d||| j        fz            || j        _        | j        S )Nstep   r   znFrame %d has %d atoms but the initial frame has %d atoms. MDAnalysis in unable to deal with variable topology!)r   
IndexErrorIOErrorr,   r"   frameconfigurationr8   databox
dimensionsnprad2degarccosr   positionshaper   
ValueError	positions)r$   r<   myframeframe_positionsn_atoms_nows        r
   _read_framezGSDReader._read_frame   s
   	$j'GG 	$ 	$ 	$t#	$  &49V %26!#BIdg6H6L,M,M!N!N122 "+4%+A.$,&& %{DLAB   !0DGws    #c                 <    |                      | j        dz             S )zread next frame in trajectoryr   )rK   r,   r.   s    r
   r#   zGSDReader._read_next_timestep   s    a000r	   )r   r   r   __doc__formatunitsr   r   r   r1   propertyr4   r6   rK   r#   __classcell__)r'   s   @r
   r   r   K   s        $$FT**E# # # # #:> > >
        X  
  <1 1 1 1 1 1 1r	   r   c                       e Zd ZdZd Zd ZdS )GSDPicklablea1  Hoomd GSD file object (read-only) that can be pickled.

    This class provides a file-like object (as by :func:`gsd.hoomd.open`,
    namely :class:`gsd.hoodm.HOOMDTrajectory`) that, unlike file objects,
    can be pickled. Only read mode is supported.

    When the file is pickled, filename and mode of :class:`gsd.fl.GSDFile` in
    the file are saved. On unpickling, the file is opened by filename.
    This means that for a successful unpickle, the original file still has to
    be accessible with its filename.

    Note
    ----
    Open hoomd GSD files with `gsd_pickle_open`.
    After pickling, the current frame is reset. `universe.trajectory[i]` has
    to be used to return to its original frame.

    Parameters
    ----------
    file: :class:`gsd.fl.GSDFile`
        File to access.

    Example
    -------
    ::

        gsdfileobj = gsd.fl.open(name=filename,
                                     mode='r',
                                     application='gsd.hoomd '+ gsd.version.version,
                                     schema='hoomd',
                                     schema_version=[1, 3])
        file = GSDPicklable(gsdfileobj)
        file_pickled = pickle.loads(pickle.dumps(file))

    See Also
    ---------
    :func:`MDAnalysis.lib.picklable_file_io.FileIOPicklable`
    :func:`MDAnalysis.lib.picklable_file_io.BufferIOPicklable`
    :func:`MDAnalysis.lib.picklable_file_io.TextIOPicklable`
    :func:`MDAnalysis.lib.picklable_file_io.GzipPicklable`
    :func:`MDAnalysis.lib.picklable_file_io.BZ2Picklable`


    .. versionadded:: 2.0.0
    c                 2    | j         j        | j         j        fS )N)r0   namer+   r.   s    r
   __getstate__zGSDPicklable.__getstate__   s    y~ty~--r	   c                     t           j        j        }ddg}t           j                            |d         |d         d|z   d|          }|                     |           d S )Nr      r   
gsd.hoomd r   rU   r+   applicationschemaschema_version)r   versionflopenr   )r$   argsgsd_versionr]   
gsdfileobjs        r
   __setstate__zGSDPicklable.__setstate__   se    k)QV[[aa${2) ! 
 

 	j!!!!!r	   N)r   r   r   rM   rV   rd   r   r	   r
   rS   rS      s=        , ,\. . .
" 
" 
" 
" 
"r	   rS   r*   rU   r+   c                     t           j        j        }ddg}|dk    rt          d          t           j                            | |d|z   d|          }t          |          S )ai  Open hoomd schema GSD file with pickle function implemented.

    This function returns a GSDPicklable object. It can be used as a
    context manager, and replace the built-in :func:`gsd.hoomd.open` function
    in read mode that only returns an unpicklable file object.

    Schema version will depend on the version of gsd module.

    Note
    ----
    Can be only used with read mode.

    Parameters
    ----------
    name : str
        a filename given a text or byte string.
    mode: str, optional
        'r':  open for reading.

    Returns
    -------
    stream-like object: GSDPicklable

    Raises
    ------
    ValueError
        if `mode` is not one of the allowed read modes

    Examples
    -------
    open as context manager::

        with gsd_pickle_open('filename') as f:
            line = f.readline()

    open as function::

        f = gsd_pickle_open('filename')
        line = f.readline()
        f.close()

    See Also
    --------
    :func:`MDAnalysis.lib.util.anyopen`
    :func:`MDAnalysis.lib.picklable_file_io.pickle_open`
    :func:`MDAnalysis.lib.picklable_file_io.bz2_pickle_open`
    :func:`MDAnalysis.lib.picklable_file_io.gzip_pickle_open`
    :func:`gsd.hoomd.open`


    .. versionadded:: 2.0.0
    .. versionchanged:: 2.6.0
       Only GSD versions 3.0.1+ are supported. 'rb' mode support
       has been replaced with 'r' mode.
    r   rX   r*   z(Only read mode 'r' files can be pickled.rY   r   rZ   )r   r^   rF   r_   r`   rS   )rU   r+   rb   r]   rc   s        r
   r-   r-      sn    p +%KVNs{{FGGG ;.%   J 
###r	   )r*   )rM   numpyrA   r   gsd.flr   r   typesr   
ModuleTyper   HOOMDTrajectory r   MDAnalysis.lib.utilr   
ReaderBaser   rS   strr-   r   r	   r
   <module>ro      s  . <    JJJMMM GG  	4 	4 	4GLLL        %
5
!
!C  ))CI 3CI	4       4 4 4 4 4 4W1 W1 W1 W1 W1 W1 W1 W1t<" <" <" <" <"39, <" <" <"~C$ C$# C$S C$ C$ C$ C$ C$ C$s    A	AA