
     i_0                         d Z ddlZddlZddlmZ ddlmZ ddlm	Z	m
Z
 ddlmZ ddlmZ ddlmZ dd	lmZmZ d
gZ G d d
e          ZdS )z
PeriodicKDTree --- :mod:`MDAnalysis.lib.pkdtree`
================================================

This module contains a class to allow searches on a KDTree involving periodic
boundary conditions.
    N)cKDTree   )unique_int_1d)augment_coordinatesundo_augment)unique_rows)	apply_PBC)OptionalClassVarPeriodicKDTreec                      e Zd ZdZ	 ddeej                 deddfdZe	d             Z
	 dd	ej        d
ee         ddfdZdej        dedej        fdZdej        fdZdedej        fdZdej        dedej        fdZdS )r   a'  Wrapper around :class:`scipy.spatial.cKDTree`

    Creates an object which can handle periodic as well as
    non periodic boundary condtions depending on the parameters
    provided while constructing the tree.

    To enable periodic boundary conditions, box dimensions must be
    provided. Periodic Boundary conditions are implemented by creating
    duplicates of the particles which are within the specified cutoff
    distance from the boundary. These duplicates along with the
    original particle coordinates are used with the cKDTree without
    any special treatment due to PBC beyond this point.  The final
    results after any operation with duplicate particle indices can be
    traced back to the original particle using the
    :func:`MDAnalysis.lib.distances.undo_augment` function.

    N
   boxleafsizereturnc                 L    || _         d| _        || _        d| _        d| _        dS )ay  

        Parameters
        ----------
        box : array-like or ``None``, optional, default ``None``
          Simulation cell dimensions in the form of
          :attr:`MDAnalysis.trajectory.timestep.Timestep.dimensions` when
          periodic boundary conditions should be taken into account for
          the calculation of contacts.
        leafsize : int (optional)
          Number of entries in leafs of the KDTree. If you suffer poor
          performance you can play around with this number. Increasing the
          `leafsize` will speed up the construction of the KDTree but
          slow down the search.

           FN)r   dimr   _builtcutoff)selfr   r   s      `/srv/www/vhosts/g4struct/public_html/venv/lib/python3.11/site-packages/MDAnalysis/lib/pkdtree.py__init__zPeriodicKDTree.__init__A   s+    & !'+    c                     | j         duS )zFlag to indicate the presence of periodic boundaries.

        - ``True`` if PBC are taken into account
        - ``False`` if no unitcell dimension is available.

        This is a managed attribute and can only be read.
        N)r   r   s    r   pbczPeriodicKDTree.pbcZ   s     xt##r   coordsr   c                    t          j        |t           j                  }| j        r|| _        |t          d          t          || j                  | _        t          | j        | j        |          \  | _
        | _        t          j        | j        | j
        g          | _        t          | j        | j                  | _        n7|t          d          || _        t          | j        | j                  | _        d| _        dS )ax  Constructs KDTree from the coordinates

        Wrapping of coordinates to the primary unit cell is enforced
        before any distance evaluations. If periodic boundary conditions
        are enabled, then duplicate particles are generated in the
        vicinity of the box. An additional array `mapping` is also
        generated which can be later used to trace the origin of
        duplicate particle coordinates.

        For non-periodic calculations, cutoff should not be provided
        the parameter is only required for periodic calculations.

        Parameters
        ----------
        coords: array_like
          Coordinate array of shape ``(N, 3)`` for N atoms.
        cutoff: float
          Specified cutoff distance to create duplicate images
          Typically equivalent to the desired search radius
          or the maximum of the desired cutoff radius. Relevant images
          corresponding to every atom which lies
          within ``cutoff`` distance from either of the box boundary
          will be generated.


        See Also
        --------
        MDAnalysis.lib.distances.augment_coordinates

        dtypeNz3Provide a cutoff distance with tree.set_coords(...)r   z<Donot provide cutoff distance for non PBC aware calculationsT)npasarrayfloat32r   r   RuntimeErrorr	   r   r   r   augmappingconcatenate
all_coordsr   r   ckdtr   )r   r   r   s      r   
set_coordszPeriodicKDTree.set_coordse   s    H F"*555 8 	< DK~"L  
 $FDH55DK%8TXv& &"DHdl !ndk48-DEEDO$-HHHDII !"2   !DKT];;DIr   centersradiusc                 6   | j         st          d          t          j        |          }|j        | j        fk    r|                    d| j        f          }| j        r| j        t          d          | j        |k     rt          d          t          || j                  }t          | j                            ||                    }t          j        t          t           j                            |                    t          j                  | _        | j        j        dk    r2t-          | j        | j        t1          | j                            | _        nt          j        |          }t          | j                            ||                    }t          j        t          t           j                            |                    t          j                  | _        t          j        t5          | j                            | _        | j        S )a  Search all points within radius from centers and their periodic images.

        All the centers coordinates are wrapped around the central cell
        to enable distance evaluations from points in the tree
        and their images.

        Parameters
        ----------
        centers: array_like (N,3)
          coordinate array to search for neighbors
        radius: float
          maximum distance to search for neighbors.
        &Unbuilt tree. Run tree.set_coords(...)r   N2Cutoff needs to be provided when working with PBC.*Set cutoff greater or equal to the radius.r    r   )r   r&   r#   r$   shaper   reshaper   r   
ValueErrorr	   r   listr+   query_ball_pointarray	itertoolschainfrom_iterableintp_indicessizer   r(   lenr   r   )r   r-   r.   wrapped_centersindicess        r   searchzPeriodicKDTree.search   s    { 	IGHHH*W%%=TXK''ooq$(m44G 8 	{" H   {V##"@   (::O4955ovNNOOGHY_227;;<<BG  DM }!A%% ,M4<T[1A1A! ! !j11O4955ovNNOOGHY_227;;<<BG  DM 
=#?#?@@}r   c                     | j         S )zReturn the neighbors from the last query.

        Returns
        ------
        indices : NDArray
          neighbors for the last query points and search radius
        )r=   r   s    r   get_indiceszPeriodicKDTree.get_indices   s     }r   c                    | j         st          d          | j        r0| j        t	          d          | j        |k     rt          d          t          j        t          | j        	                    |                    t
          j
                  }| j        rt          |          dk    rrt          |dddf         | j        t          | j                            |dddf<   t          |dddf         | j        t          | j                            |dddf<   |j        dk    r%t          j        |d          }t#          |          }|S )	a-  Search all the pairs within a specified radius

        Parameters
        ----------
        radius : float
          Maximum distance between pairs of coordinates

        Returns
        -------
        pairs : array
          Indices of all the pairs which are within the specified radius
        z' Unbuilt Tree. Run tree.set_coords(...)Nr1   r2   r    r   r   )axis)r   r&   r   r   r5   r#   r8   r6   r+   query_pairsr<   r?   r   r(   r   r>   sortr   )r   r.   pairss      r   search_pairszPeriodicKDTree.search_pairs   s[    { 	JHIII8 	{" H   {V##"@   di33F;;<<BGLLL8 	5zzA~~*!!!Q$Ks4;/?/? aaad +!!!Q$Ks4;/?/? aaad :>>GE***E&&Er   c                    | j         st          d          t          j        |          }|j        | j        fk    r|                    d| j        f          }| j        r| j        t          d          | j        |k     rt          d          t          || j                  }t          || j                  }|                    | j        |          }t          j        d t#          |          D             t          j                  }|j        d	k    r9t)          |dddf         | j        t-          | j                            |dddf<   nht          || j                  }|                    | j        |          }t          j        d
 t#          |          D             t          j                  }|j        d	k    rt1          |          }|S )a  
        Searches all the pairs within `radius` between `centers`
        and ``coords``

        ``coords`` are the already initialized coordinates in the tree
        during :meth:`set_coords`.
        ``centers`` are wrapped around the primary unit cell
        if PBC is desired. Minimum image convention (PBC) is
        activated if the `box` argument is provided during
        class initialization

        Parameters
        ----------
        centers: array_like (N,3)
          coordinate array to search for neighbors
        radius: float
          maximum distance to search for neighbors.

        Returns
        -------
        pairs : array
          all the pairs between ``coords`` and ``centers``

        Note
        ----
        This method constructs another tree from the ``centers``
        and queries the previously built tree (built in
        :meth:`set_coords`)
        r0   r   Nr1   r2   r"   c                 &    g | ]\  }}|D ]}||gS  rM   .0ilstjs       r   
<listcomp>z.PeriodicKDTree.search_tree.<locals>.<listcomp>B  -    DDDFAsDD1!QDDDDr   r    r   c                 &    g | ]\  }}|D ]}||gS rM   rM   rN   s       r   rS   z.PeriodicKDTree.search_tree.<locals>.<listcomp>M  rT   r   )r   r&   r#   r$   r3   r   r4   r   r   r5   r	   r   r   r   query_ball_treer+   r8   	enumerater<   r>   r   r(   r?   r   r   )r   r-   r.   r@   
other_treerI   s         r   search_treezPeriodicKDTree.search_tree  s   > { 	IGHHH*W%%=TXK''ooq$(m44G 8 	{" H   {V##"@   (::O 4=IIIJ..ty&AAEHDDi&6&6DDDg  E zA~~*!!!Q$Ks4;/?/? aaad !4=AAAJ..ty&AAEHDDi&6&6DDDg  E :>>&&Er   )Nr   )N)__name__
__module____qualname____doc__r
   npt	ArrayLikeintr   propertyr   floatr,   NDArrayrB   rD   rJ   r#   ndarrayrY   rM   r   r   r   r   .   se        & DF, ,CM*,=@,	, , , ,2 $ $ X$ @D@ @m@-5e_@	@ @ @ @D1cm 1U 1s{ 1 1 1 1fS[    '5 'S[ ' ' ' 'RE3= E% EBJ E E E E E Er   )r]   r9   numpyr#   scipy.spatialr   _cutilr   _augmentr   r   utilr   MDAnalysis.lib.distancesr	   numpy.typingtypingr^   r
   r   __all__objectr   rM   r   r   <module>ro      s   .          ! ! ! ! ! ! ! ! ! ! ! ! 7 7 7 7 7 7 7 7       . . . . . .       % % % % % % % %
d d d d dV d d d d dr   