
     i>                         d Z ddlmZ ddlZddlZddlmZmZ ddl	m
Z
 ddlmZ ddlmZ  G d de          Z G d	 d
e          ZdS )aK  
Linear Density --- :mod:`MDAnalysis.analysis.lineardensity`
===========================================================

A tool to compute mass and charge density profiles along the three
cartesian axes [xyz] of the simulation cell. Works only for orthorombic,
fixed volume cells (thus for simulations in canonical NVT ensemble).
    N)AnalysisBaseResults)	constants)	deprecate)ResultsGroupc                   @     e Zd ZdZdddddZd Z fdZ fd	Z xZS )
r   zFrom version 3.0.0 onwards, some entries in Results will be renamed. See
    the docstring for LinearDensity for details. The Results class is defined
    here to implement deprecation warnings for the user.mass_densitymass_density_stddevcharge_densitycharge_density_stddev)pospos_stdcharchar_stdc                 ^    t          j        d| d| j        |          dt                     d S )N`zB` is deprecated and will be removed in version 3.0.0. Please use `z
` instead.)warningswarn_deprecation_dictDeprecationWarning)selfkeys     k/srv/www/vhosts/g4struct/public_html/venv/lib/python3.11/site-packages/MDAnalysis/analysis/lineardensity.py_deprecation_warningzResults._deprecation_warning7   sT    C C C1#6C C C	
 	
 	
 	
 	
    c                    || j                                         v rH|                     |           t          t          |                               | j         |                   S t          t          |                               |          S N)r   keysr   superr   __getitem__)r   r   	__class__s     r   r    zResults.__getitem__>   s~    $(--////%%c***$''33&s+   Wd##//444r   c                     || j                                         v r"|                     |           | j         |         }t          t          |                               |          S r   )r   r   r   r   r   __getattr__)r   attrr!   s     r   r#   zResults.__getattr__F   s\    4)..0000%%d+++)$/DWd##//555r   )	__name__
__module____qualname____doc__r   r   r    r#   __classcell__r!   s   @r   r   r   +   s        < <
 ( +	 
 
 
5 5 5 5 56 6 6 6 6 6 6 6 6r   r   c                        e Zd ZdZdZed             Zd fd	Zed             Z	d Z
d	 Zd
 Z eddd          d             Z xZS )LinearDensitya  Linear density profile

    Parameters
    ----------
    select : AtomGroup
          any atomgroup
    grouping : str {'atoms', 'residues', 'segments', 'fragments'}
          Density profiles will be computed either on the atom positions (in
          the case of 'atoms') or on the center of mass of the specified
          grouping unit ('residues', 'segments', or 'fragments').
    binsize : float
          Bin width in Angstrom used to build linear density
          histograms. Defines the resolution of the resulting density
          profile (smaller --> higher resolution)
    verbose : bool, optional
          Show detailed progress of the calculation if set to ``True``

    Attributes
    ----------
    results.x.dim : int
           index of the [xyz] axes
    results.x.mass_density : numpy.ndarray
           mass density in :math:`g \cdot cm^{-3}` in [xyz] direction
    results.x.mass_density_stddev : numpy.ndarray
           standard deviation of the mass density in [xyz] direction
    results.x.charge_density : numpy.ndarray
           charge density in :math:`\mathrm{e} \cdot mol \cdot cm^{-3}` in
           [xyz] direction
    results.x.charge_density_stddev : numpy.ndarray
           standard deviation of the charge density in [xyz] direction
    results.x.pos: numpy.ndarray
        Alias to the :attr:`results.x.mass_density` attribute.

        .. deprecated:: 2.2.0
           Will be removed in MDAnalysis 3.0.0. Please use
           :attr:`results.x.mass_density` instead.
    results.x.pos_std: numpy.ndarray
        Alias to the :attr:`results.x.mass_density_stddev` attribute.

        .. deprecated:: 2.2.0
           Will be removed in MDAnalysis 3.0.0. Please use
           :attr:`results.x.mass_density_stddev` instead.
    results.x.char: numpy.ndarray
        Alias to the :attr:`results.x.charge_density` attribute.

        .. deprecated:: 2.2.0
           Will be removed in MDAnalysis 3.0.0. Please use
           :attr:`results.x.charge_density` instead.
    results.x.char_std: numpy.ndarray
        Alias to the :attr:`results.x.charge_density_stddev` attribute.

        .. deprecated:: 2.2.0
           Will be removed in MDAnalysis 3.0.0. Please use
           :attr:`results.x.charge_density_stddev` instead.
    results.x.slice_volume : float
           volume of bin in [xyz] direction
    results.x.hist_bin_edges : numpy.ndarray
           edges of histogram bins for mass/charge densities, useful for, e.g.,
           plotting of histogram data.
    Note: These density units are likely to be changed in the future.

    Example
    -------
    First create a :class:`LinearDensity` object by supplying a selection,
    then use the :meth:`run` method. Finally access the results
    stored in results, i.e. the mass density in the x direction.

    .. code-block:: python

       ldens = LinearDensity(selection)
       ldens.run()
       print(ldens.results.x.mass_density)


    Alternatively, other types of grouping can be selected using the
    ``grouping`` keyword. For example to calculate the density based on
    a grouping of the :class:`~MDAnalysis.core.groups.ResidueGroup`
    of the input :class:`~MDAnalysis.core.groups.AtomGroup`.

    .. code-block:: python

       ldens = LinearDensity(selection, grouping='residues', binsize=1.0)
       ldens.run()



    .. versionadded:: 0.14.0

    .. versionchanged:: 1.0.0
       Support for the ``start``, ``stop``, and ``step`` keywords has been
       removed. These should instead be passed to :meth:`LinearDensity.run`.
       The ``save()`` method was also removed, you can use ``np.savetxt()`` or
       ``np.save()`` on the :attr:`LinearDensity.results` dictionary contents
       instead.

    .. versionchanged:: 1.0.0
       Changed `selection` keyword to `select`

    .. versionchanged:: 2.0.0
       Results are now instances of
       :class:`~MDAnalysis.core.analysis.Results` allowing access
       via key and attribute.

    .. versionchanged:: 2.2.0

       *  Fixed a bug that caused LinearDensity to fail if grouping="residues"
          or grouping="segments" were set.
       *  Residues, segments, and fragments will be analysed based on their
          centre of mass, not centre of geometry as previously stated.
       *  LinearDensity now works with updating atom groups.
       *  Added new result container :attr:`results.x.hist_bin_edges`.
          It contains the bin edges of the histrogram bins for calculated
          densities and can be used for easier plotting of histogram data.

    .. versionchanged:: 2.10.0
       *  Introduced :meth:`get_supported_backends` allowing for parallel execution
          on :mod:`multiprocessing` and :mod:`dask` backends.
       *  Removed undocumented and unused attribute :attr:`totalmass`.

    .. deprecated:: 2.2.0
       The `results` dictionary has been changed and the attributes
       :attr:`results.x.pos`, :attr:`results.x.pos_std`, :attr:`results.x.char`
       and :attr:`results.x.char_std` are now deprecated. They will be removed
       in 3.0.0. Please use :attr:`results.x.mass_density`,
       :attr:`results.x.mass_density_stddev`, :attr:`results.x.charge_density`,
       and :attr:`results.x.charge_density_stddev` instead.
    Tc                     dS )N)serialmultiprocessingdask )clss    r   get_supported_backendsz$LinearDensity.get_supported_backends   s    
 
r   atoms      ?c                 p    t          t          |           j        |j        j        fi | |g| _        |j        | _        || _        || _        t          d          | j
        d<   t          d          | j
        d<   t          d          | j
        d<   | j        j        d d         | _        t          j        | j                  | _        | j        | j        z                      t                     }|                                | _        | j        |z  }g d	| _        | j
        D ]\}| j
        |         d
         }||         | j
        |         d<   | j        D ])}	t          j        | j                  | j
        |         |	<   *]| j        dk    r0| j        d         j        | _        | j        d         j        | _        d S | j        dv rX| j        d                             | j                  | _        | j        d                             | j                  | _        d S t3          | j         d          )Nr   )dimx   y   z   r	   r
   r   r   r7   slice_volumer4   residuessegments	fragmentscompound# is not a valid value for grouping.)r   r,   __init__universe
trajectory_ags	_universebinsizegroupingr   results
dimensionsnpprodvolumeastypeintmaxnbinsr   zerosmassescharges
total_masstotal_chargeAttributeError)r   selectrM   rL   kwargsbins
slices_volr7   idxr   r!   s             r   rG   zLinearDensity.__init__   s'   +mT""+O&	
 	
*0	
 	
 	
 H	 ! $NNNS#NNNS#NNNS .3BQB7gdo..4</77<< XXZZ
[4'

 
 
	 < 	> 	>C,s#E*C0:3DLn-y > >)+$*)=)=S!#&&> =G##)A,-DK9Q</DLLL]CCC)A,114=1IIDK9Q<44dm4LLDLLL !=EEE  r   c           	      v   t          j        d | D             d          }t          j        d | D             d          }t          j        d | D             d          }t          j        d | D             d          }t          | d         d         | d         d         | d         d	         ||||
          S )Nc                     g | ]
}|d          S )r	   r1   .0entrys     r   
<listcomp>z4LinearDensity._custom_aggregator.<locals>.<listcomp>  s    888uU>"888r   r   )axisc                     g | ]
}|d          S )r
   r1   rd   s     r   rg   z4LinearDensity._custom_aggregator.<locals>.<listcomp>  s    ???eU()???r   c                     g | ]
}|d          S )r   r1   rd   s     r   rg   z4LinearDensity._custom_aggregator.<locals>.<listcomp>  s    :::U#$:::r   c                     g | ]
}|d          S )r   r1   rd   s     r   rg   z4LinearDensity._custom_aggregator.<locals>.<listcomp>   s    AAAU*+AAAr   r7   r?   hist_bin_edges)r7   r?   rl   r	   r
   r   r   )rP   sumr   )rN   r	   r
   r   r   s        r   _custom_aggregatorz LinearDensity._custom_aggregator  s     v88888q
 
 
 !f??w???a
 
 
 ::':::
 
 
 !#AAAAA!
 !
 !
 
5! N3"1:&67% 3)"7
 
 
 	
r   c                 H    t          | j        | j        | j        d          S )Nr8   r:   r<   )lookup)r   rn   )r   s    r   _get_aggregatorzLinearDensity._get_aggregator,  s5    ,,, 
 
 
 	
r   c           
         | j         dk    r/| j        d         j        | _        | j        d         j        | _        nw| j         dv rW| j        d                             | j                   | _        | j        d                             | j                   | _        nt          | j          d          t          | j        d         | j                   | _        | j        d         	                    | j                    | j         dk    r| j        d         j
        }n&| j        d                             | j                   }dD ]D}| j        |         d         }d}d	}t          j        |d d |f         | j        | j        d
t!          | j                  f          \  }}| j        |         |xx         |z  cc<   | j        |         |xx         t          j        |          z  cc<   d}d}t          j        |d d |f         | j        | j        d
t!          | j                  f          \  }}| j        |         |xx         |z  cc<   | j        |         |xx         t          j        |          z  cc<   || j        |         d<   Fd S )Nr4   r   r@   rD   rF   rp   r7   r	   r
   g        )weightsr_   ranger   r   rl   )rM   rJ   rX   rY   rZ   r[   r\   getattrgroupwrap	positionscenter_of_massrN   rP   	histogramrV   rU   rO   square)	r   ry   r7   ra   r   key_stdhist_	bin_edgess	            r   _single_framezLinearDensity._single_frame5  s   =G##)A,-DK9Q</DLL]CCC)A,114=1IIDK9Q<44dm4LLDLL !=EEE   TYq\4=99
	!4=111=G##	!.II 	!33T]3KKI" 	< 	<C,s#E*C C+Gl!!!S&!ZC001	  GD! Lc"""d*"""Lg&&&")D//9&&&"C-G l!!!S&!ZC001	  OD) Lc"""d*"""Lg&&&")D//9&&&2;DL.//9	< 	<r   c                    t           d         }d}||z  }dD ]}dD ]"}| j        |         |xx         | j        z  cc<   #| j        |         d         t          j        | j        |         d                   z
  }d||dk     <   t          j        |          | j        |         d<   | j        |         d         t          j        | j        |         d	                   z
  }d||dk     <   t          j        |          | j        |         d<   dD ]=}|| j        |         d
         z  }| j        D ]}| j        |         |xx         |z  cc<   >d S )N
N_AvogadrogW:rp   r>   r
   r	   r   r   r   r?   )r   rN   n_framesrP   r|   sqrtr   )	r   avogadrovolume_conversionkr7   r   radicand_massradicand_chargenorms	            r   	_concludezLinearDensity._concludej  s   \*!(( # 	 	C 8 8 S!#&&&$-7&&&& !L-%	$,s+N;<<=M 01M-!+,79w}7M7MDL34"l3/'	$,s+,<=>>?O 45OOa/09;: :DL566 # 	/ 	/Ct|C(88Dy / /S!#&&&$.&&&&/	/ 	/r   z2.2.0z3.0.0z=It will be replaced by a :meth:`_reduce` method in the future)releaseremovemessagec                 |    dD ]8}| j         D ].}| j        |         |xx         |j        |         |         z  cc<   /9dS )zFor parallel analysisrp   N)r   rN   )r   otherr7   r   s       r   _add_other_resultsz LinearDensity._add_other_results  sl     # 	B 	BCy B BS!#&&&%-*<S*AA&&&&B	B 	Br   )r4   r5   )r%   r&   r'   r(   %_analysis_algorithm_is_parallelizableclassmethodr3   rG   staticmethodrn   rr   r   r   r   r   r)   r*   s   @r   r,   r,   M   s        ~ ~@ -1)
 
 [
8 8 8 8 8 8t 
 
 \
2
 
 
3< 3< 3<j'/ '/ '/T Y  B B B B B B Br   r,   )r(   os.pathpathnumpyrP   r   MDAnalysis.analysis.baser   r   MDAnalysis.unitsr   MDAnalysis.lib.utilr   MDAnalysis.analysis.resultsr   r,   r1   r   r   <module>r      s   .             : : : : : : : : & & & & & & ) ) ) ) ) ) 4 4 4 4 4 46 6 6 6 6g 6 6 6DQB QB QB QB QBL QB QB QB QB QBr   