
     iUR                         d Z ddlZddlZddlZddlZddl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d
lmZmZ  G d dej                  Z G d dej                  ZdS )a  TRZ trajectory I/O  --- :mod:`MDAnalysis.coordinates.TRZ`
============================================================

Classes to read `IBIsCO`_ / `YASP`_ TRZ binary trajectories, including
coordinates, velocities and more (see attributes of the :class:`Timestep`).

Data are read and written in binary representation but because this depends on
the machine hardware architecture, MDAnalysis *always* reads and writes TRZ
trajectories in *little-endian* byte order.

.. _IBIsCO: https://onlinelibrary.wiley.com/doi/full/10.1002/jcc.21717
.. _YASP: https://www.sciencedirect.com/science/article/abs/pii/0010465593901442

Classes
-------

.. autoclass:: TRZReader
   :members:

.. autoclass:: TRZWriter
   :members:

    N   )base   )NoDataError)Timestep)util)cachedstore_init_arguments)triclinic_boxtriclinic_vectorsc                   B    e Zd ZdZdZddddZed fd	            Zd	 Zdd
Z	e
d             Ze
 ed          d                         Zd Zd Ze
 ed          d                         Ze
 ed          d                         Zd Zd Zd Zd ZddZd Z xZS )	TRZReaderaL  Reads an IBIsCO or YASP trajectory file

    Attributes
    ----------
    ts : timestep.Timestep
         :class:`~MDAnalysis.coordinates.timestep.Timestep` object containing
         coordinates of current frame

    Note
    ----
    Binary TRZ trajectories are *always* assumed to be written in
    *little-endian* byte order and are read as such.


    .. versionchanged:: 0.11.0
       Frames now 0-based instead of 1-based.
       Extra data (Temperature, Energies, Pressures, etc) now read
       into ts.data dictionary.
       Now passes a weakref of self to ts (ts._reader).
    .. versionchanged:: 1.0.1
       Now checks for the correct `n_atoms` on reading
       and can raise :exc:`ValueError`.
    .. versionchanged:: 2.1.0
       TRZReader now returns a default :attr:`dt` of 1.0 when it cannot be
       obtained from the difference between two frames.
    .. versionchanged:: 2.3.0
       _frame attribute moved to `ts.data` dictionary.
    .. deprecated:: 2.7.0
       The TRZ Reader and Writer are deprecated as of version 2.7.0
       and will be removed in version 3.0.0.
    TRZpsnmnm/pstimelengthvelocityNc                    d}t          j        |t                      t          t          |           j        |fi | |t          d          t          j        | j	        d          | _
        t                      | _        || _        |                                  t          | j        fd| j        | d| j        | _        t)          |          dz   }dd	d
dddddddddddddd|fdd|fdd|fdd|fdd |fd!d"|fg}| j        s|d#gz  }n|d$d%|fd&d'|fd(d)|fd*gz  }t+          j        |          | _        |                                  dS )+a  Creates a TRZ Reader

        Parameters
        ----------
        trzfilename : str
            name of input file
        n_atoms : int
            number of atoms in trajectory, must be taken from topology file!
        convert_units : bool (optional)
            converts units to MDAnalysis defaults

        Raises
        ------
        ValueError
           If `n_atoms` or the number of atoms in the topology file do not
           match the number of atoms in the trajectory.
        zLThe TRZ reader is deprecated and will be removed in MDAnalysis version 3.0.0Nz&TRZReader requires the n_atoms keywordrbT)
velocitiesforcesreader<f4p1<i4nframer   ntrjr   natomsr   treal<f8p2<2i4boxz<9f8)p3r+   pressurer(   ptensorz<6f8)p4z<3i4etotr(   ptotr(   ekr(   Tr(   )p5z<6i4rx)pad2r+   ry)pad3r+   rz)pad4r+   vx)pad5r+   vy)pad6r+   vz)pad7r   )rH   r+   fx)pad8r+   fy)pad9r+   fz)pad10r   )warningswarnDeprecationWarningsuperr   __init__
ValueErrorr   anyopenfilenametrzfiledict_cache_n_atoms_read_trz_headerr   n_atoms	has_force
_ts_kwargstsstrnpdtype_dtype_read_next_timestep)selftrzfilenamer\   kwargswmsgreadargframe_contents	__class__s          d/srv/www/vhosts/g4struct/public_html/venv/lib/python3.11/site-packages/MDAnalysis/coordinates/TRZ.pyrS   zTRZReader.__init__b   s   &+d.///'i'??????EFFF|DM488ff4< .&*"&."&. . "_	. . g,,&7O7O7O7O7O7O78 ~ 
	"//NN w w w " "N h~..  """""    c                 `   t          j        g d          | _        t          j        | j        | j        d          }d                    d |d         d         D                                                       | _        |d         d	k    r	d
| _        dS |d         dk    r	d| _        dS t          )z&Reads the header of the trz trajectory)r   title80cr)   )forcer   )r.   r   r   rb   count c              3   @   K   | ]}|                     d           V  dS )zutf-8N)decode).0cs     rl   	<genexpr>z-TRZReader._read_trz_header.<locals>.<genexpr>   s.      II1QXXg..IIIIIIrm   rp   r   rr   
   F   TN)
ra   rb   _headerdtypefromfilerW   joinstriprp   r]   IOError)re   datas     rl   r[   zTRZReader._read_trz_header   s    H & & &   {4<t/@JJJWWIIWa8HIIIIIOOQQ
=B"DNNN']b  !DNNNMrm   c                    || j         }	 t          j        | j        | j        d          }|d         d         | j        k    r't          d                    | j                            |d         d         dz
  |_        |d         d         |j	        d<   |d	         d         |_
        t          |d
                             dd           |_        |d         |j	        d<   |d         |j	        d<   |d         |j	        d<   |d         |j	        d<   |d         |j	        d<   |d         |j	        d<   |d         |j        d d <   |d         |j        d d <   |d         |j        d d <   |d         |j        d d df<   |d         |j        d d df<   |d         |j        d d df<   | j        r<|d         |j        d d df<   |d         |j        d d df<   |d          |j        d d df<   | j        rq|                     | j         j                   | j         j        '|                     | j         j        d d                    |                     | j         j                   |S # t0          $ r	 t2          d w xY w)!Nr   rs   r%   r   z]Supplied n_atoms {} is incompatible with provided trajectory file. Maybe `topology` is wrong?r!   r#   framer'   r-      r0   r2   pressure_tensorr5   total_energyr7   potential_energyr9   kinetic_energyr;   temperaturer=   r?   rA   rC   rE   rG   r   rI   rK   rM   )r_   ra   r~   rW   rc   r\   rT   formatr   r   r   r   reshape
dimensions_x_y_z_velocitiesr]   _forcesconvert_unitsconvert_pos_from_native_posconvert_velocities_from_native
IndexErrorr   )re   r_   r   s      rl   rd   zTRZReader._read_next_timestep   s   :B%	;t|4;aHHHDH~a DL00  ">>Df=A\?K ?KL L L H~a(1,BH#F|ABGG7mA&BG)DK,?,?1,E,EGBM"&z"2BGJ)-iBG%&&*6lBGN#*.v,BG&'(,T
BG$%%)#YBGM"DzBE!!!HDzBE!!!HDzBE!!!H#':BN111a4 #':BN111a4 #':BN111a4 ~ .#':
111a4 #':
111a4 #':
111a4 
 ! I,,TW\:::7%1001CBQB1GHHH33DG4GHHHI  	$ 	$ 	$t#	$s   GI   I3c                     | j         S )zNumber of atoms in a frame)rZ   re   s    rl   r\   zTRZReader.n_atoms   s     }rm   n_framesc                 Z    	 |                      | j                  S # t          $ r Y dS w xY w)z&Total number of frames in a trajectoryr   )_read_trz_n_framesrW   r   r   s    rl   r   zTRZReader.n_frames   s?    	**4<888 	 	 	11	s    
**c                    t          j        |                                          j        }|| j        j        z
  | j        j        z  dk    st          d          t          || j        j        z
  | j        j        z            }|S )zUses size of file and dtype information to determine how many frames exist

        .. versionchanged:: 0.9.0
           Now is based on filesize rather than reading entire file
        r   z Trajectory has incomplete frames)	osfstatfilenost_sizer}   itemsizerc   r   int)re   rW   fsizenframess       rl   r   zTRZReader._read_trz_n_frames   sv     ))**2)22dk6JJaOO<===ut099T[=QQRRrm   c                 
   | j         j        }	 | j         j        }|                                  | j         j        }||z
  }||                     |           S # t
          $ r t          w xY w# |                     |           w xY w)a  The amount of time between frames in ps

        Assumes that this step is constant (ie. 2 trajectories with different
        steps haven't been stitched together).
        Returns ``AttributeError`` in case of ``StopIteration``
        (which makes :attr:`dt` return 1.0).

        .. versionchanged:: 2.1.0
           Now returns an ``AttributeError`` if dt can't be obtained from the
           time difference between two frames.
        )r_   r   r   next_read_frameStopIterationAttributeError)re   
curr_framet0t1dts        rl   _get_dtzTRZReader._get_dt  s     W]

	)BIIKKKBbB Z((((  	! 	! 	!  	!
 Z((((s   1A A+ A((A+ +Bdeltac                      | j         | j        z  S )zMD integration timestep)r   skip_timestepr   s    rl   r   zTRZReader.delta(  s     w+++rm   r   c                 D   | j         j        }	 | j         j        d         }|                                  | j         j        d         }||z
  }||                     |           S # t
          $ r Y |                     |           dS w xY w# |                     |           w xY w)z#Timesteps between trajectory framesr   r   )r_   r   r   r   r   r   )re   r   r   r   r   s        rl   r   zTRZReader.skip_timestep.  s     W]

	)g&BIIKKKg&BGM !Z((((  	 	 	 Z(((((	
 Z((((s)   =A" B "
B,B BB Bc                     || j         j        z
  }|                     |dz
             |                                  | j         S )zMove to *frame* and fill timestep with data.

        .. versionchanged:: 0.11.0
           Frames now 0-based instead of 1-based
        r   )r_   r   _seekrd   )re   r   moves      rl   r   zTRZReader._read_frame?  sA     tw}$

4!8  """wrm   c                    | j         j        }||z  }|dz   }||k    rt          |          }||z  }t          ||z            }t          ||z            }t	          |          D ]"}| j                            t          d           #| j                            |d           dS t          |          }| j                            |d           dS )zMove *nframes* in the trajectory

        Note that this doens't read the trajectory (ts remains unchanged)

        .. versionadded:: 0.9.0
        r   N)rc   r   longr   rangerW   seekmaxint)re   r   	framesizeseeksizemaxi_lnrepsrem_s           rl   r   zTRZReader._seekK  s     K(	w&Af YI 7*H6)**Eh'((C5\\ - -!!&!,,,,Lc1%%%%%8}}HLh*****rm   c                 ~    |                                   |                                  |                                  d S N)closeopen_trajectoryr[   r   s    rl   _reopenzTRZReader._reopenh  s9    

rm   c                 @   | j          t          t          j        d| j                  t
          j                            | j                  s t          t          j        d| j                  t          j
        | j        d          | _         | j        }d|_        | j         S )zOpen the trajectory fileNzTRZ file already openedzTRZ file not foundr   )rW   r   errnoEALREADYrV   r   pathexistsENOENTr   rU   r_   r   )re   r_   s     rl   r   zTRZReader.open_trajectorym  s~    <#%.*CT]SSSw~~dm,, 	M%,(<dmLLL|DM488 W|rm   c                 >    || j         j        }t          ||          S r   )r_   r\   	TRZWriter)re   rV   r\   s      rl   WriterzTRZReader.Writer|  s     ?goG7+++rm   c                 X    | j         "| j                                          d| _         dS dS )zClose trz file if it was openNrW   r   r   s    rl   r   zTRZReader.close  s1    <#L   DLLL $#rm   r   )__name__
__module____qualname____doc__r   unitsr
   rS   r[   rd   propertyr\   r	   r   r   r   r   r   r   r   r   r   r   r   __classcell__)rk   s   @rl   r   r   =   s        @ FTw??EQ# Q# Q# Q# Q# Q#f  ") ) ) )V   X VJ   X  ) ) )2 VG__, , _ X, VO) )  X)
 
 
+ + +:     
  , , , ,             rm   r   c                   @    e Zd ZdZdZdZddddZddZd	 Zd
 Z	d Z
dS )r   a  Writes a TRZ format trajectory.

    Note
    ----
    Binary TRZ trajectories are *always* written in *little-endian* byte order.


    .. deprecated:: 2.7.0
       The TRZ Reader and Writer are deprecated as of version 2.7.0
       and will be removed in version 3.0.0.
    r   Tr   r   r   r   c                 j   d}t          j        |t                     || _        |t	          d          |dk    rt	          d          || _        t          |          dk    rt	          d          || _        t          j	        | j        d          | _
        |                     |           t          |          d	z   }t          j        g d
dddddddddddddddddddddd |fd!d"d#|fd$d%d&|fd'd(d)|fd*d+d,|fd-d.d/|fd0          | _        dS )1a  Create a TRZWriter

        Parameters
        ----------
        filename : str
            name of output file
        n_atoms : int
            number of atoms in trajectory
        title : str (optional)
            title of the trajectory; the title must be 80 characters or
            shorter, a longer title raises a ValueError exception.
        convert_units : bool (optional)
            units are converted to the MDAnalysis base format; [``True``]
        zLThe TRZ writer is deprecated and will be removed in MDAnalysis version 3.0.0Nz&TRZWriter requires the n_atoms keywordr   z(TRZWriter: no atoms in output trajectoryP   z3TRZWriter: 'title' must be 80 characters of shorterwbr   )p1ar   r    r"   r$   r&   )p1br   )p2ar   r,   )p2br   )p3ar   r/   r1   )p3br   )p4ar   )sixr   r4   r6   r8   r:   )blanksz<2f8)p4br   )p5ar   r=   )p5br   )p6ar   r?   )p6br   )p7ar   rA   )p7br   )p8ar   rC   )p8br   )p9ar   rE   )p9br   )p10ar   rG   )p10br   )rO   rP   rQ   rV   rT   r\   lenr   r   rU   rW   _writeheaderr`   ra   rb   
frameDtype)re   rV   r\   rp   r   rh   	floatsizes          rl   rS   zTRZWriter.__init__  s   +d./// ?EFFFa<<GHHHu::??RSSS*|DM488%   LL5(	( '$'$'$ '$ 	'$
 '$ '$ '$ '$ '$ '$  '$  '$ '$ '$ '$  !'$" #'$$ %'$& ''$( )'$* +'$, -'$. 9/'$0 1'$2 3'$4 95'$6 7'$8 9'$: 9;'$< ='$> ?'$@ 9A'$B C'$D E'$F 9G'$H I'$J K'$L 9M'$N O'$ ' 'rm   c                     t          j        g d          }t          j        d|          }d\  |d<   |d<   |ddt          |          z
  z  z   |d	<   d
\  |d<   |d<   d|d<   |                    | j                   d S )N))pad1r   ro   )r>   r   )r@   r   )nrecr   )rB   r    rb   )r   r   r   r>    r   rp   )   r   r@   rB   r{   r   )ra   rb   zerosr   tofilerW   )re   rp   hdtouts       rl   r   zTRZWriter._writeheader  s    h ? ? ? @ @ hr%%%#) FS[sb3u::o66G#' FS[F

4<     rm   c                    	 |j         }n># t          $ r1 	 |j        j         }n # t          $ r d}t          |          dw xY wY nw xY w|j        j        | j        k    sd}t          |          i }g }dD ]g}	 |j        |         ||<   # t          $ rG |dk    r$t          j
        dt          j                  ||<   nd||<   |                    |           Y dw xY w	 |j        d	         |d
<   n/# t          $ r" |j        |d
<   |                    d
           Y nw xY w	 |j        |d<   n<# t          $ r/ t          |j                  |d<   |                    d           Y nw xY w|r:t!          j        d                    d                    |                               |j        (t+          |j                                      d          }n4t!          j        d           t          j
        dt          j                  }	 |j        }nL# t2          $ r? t          j
        | j        dft          j        d          }t!          j        d           Y nw xY wt          j
        d| j                  }	d\  |	d<   |	d<   |j        dz   |	d<   |d
         |	d<   | j        |	d<   |d         |	d<   d\  |	d<   |	d<   |                     |d !          |	d"<   d#\  |	d$<   |	d%<   |d&         |	d&<   |d         |	d'<   d(\  |	d)<   |	d*<   d|	d+<   |d,         |	d-<   |d.         |	d/<   |d0         |	d1<   |d2         |	d3<   d4|	d5<   |j        d6z  }
|
|
c|	d7<   |	d8<   |                     |j        d !          |	d9<   |
|
c|	d:<   |	d;<   |                     |j        d !          |	d<<   |
|
c|	d=<   |	d><   |                     |j        d !          |	d?<   |
|
c|	d@<   |	dA<   |                     |dddBf         d !          |	dC<   |
|
c|	dD<   |	dE<   |                     |dddf         d !          |	dF<   |
|
c|	dG<   |	dH<   |                     |dddIf         d !          |	dJ<   |	                     | j!                   dS )Ka  Write information associated with ``obj`` at current frame into trajectory

        Parameters
        ----------
        ag : AtomGroup or Universe


        .. versionchanged:: 1.0.0
           Renamed from `write_next_timestep` to `_write_next_frame`.
        .. versionchanged:: 2.0.0
           Deprecated support for Timestep argument has now been removed.
           Use AtomGroup or Universe as an input instead.
        z-Input obj is neither an AtomGroup or UniverseNz1Number of atoms in ts different to initialisation)r0   r   r   r   r   r   r      r           r   stepr   zeTimestep didn't have the following attributes: '{0}', these will be set to 0 in the output trajectoryz, 	   zSTimestep didn't have dimensions information, box will be written as all zero valuesr   F)rb   orderzZTimestep didn't have velocity information, this will be set to zero in output trajectory. r   )r|   r|   r   r   r   r!   r#   r%   r'   )H   r
  r   r   F)inplacer-   )8   r  r   r   r0   r2   )<   r  r   r   r   r   r5   r   r7   r   r9   r   r;   )r  r  r   r   r   r   r=   r   r   r?   r   r   rA   r   r   r   rC   r   r   rE   r   r   r   rG   )"r_   r   
trajectory	TypeErroratomsr\   rT   r   KeyErrorra   r   float64appendr   r   floatrO   rP   r   r   r   r   r   float32r   r   r   convert_pos_to_nativer   r   r   convert_velocities_to_nativer   rW   )re   objr_   errmsgr   faked_attrsattunitcellvelsr  sizes              rl   _write_next_framezTRZWriter._write_next_frame  sy   	2BB 	2 	2 	22^&! 2 2 2H''T12 	2 y DL00HFV$$$ I 		( 		(C(GCLS		 ( ( (+++ ""* = = =DII #DI""3'''''(	'77+DLL 	' 	' 	'8DLv&&&&&	'	'7DLL 	' 	' 	' ??DLv&&&&&	'  	=M #VDIIk$:$:;;= = =
 =$(77??BBHHM C D D Dx444H	M=DD 	M 	M 	M8T\1-RZsKKKDM L M M M M M	M
 hr111!'E
CJ1H6lFHF|G!'E
CJ//%/HHE
!'E
CJz*J/0I!'E
CJE
>*F-.F)*D	&C HzA~!%tE
CJ..rue.DDD	!%tE
CJ..rue.DDD	!%tE
CJ..rue.DDD	!%tE
CJ55d111a4j%5PPD	!%tE
CJ55d111a4j%5PPD	#' FS[55d111a4j%5PPD	

4<     sk   
 
A"A?AA7BACCC. .)DD
D) )6E"!E"H AIIc                 X    | j         dS | j                                          d| _         dS )zClose if it was openNr   r   s    rl   r   zTRZWriter.close[  s.    <Frm   N)r   T)r   r   r   r   r   
multiframer   rS   r   r  r   r   rm   rl   r   r     s        
 
 FJTw??EK K K KZ	! 	! 	!f! f! f!P    rm   r   )r   rO   numpyra   r   r   ru   r   
exceptionsr   timestepr   libr   lib.utilr	   r
   corer   r   
ReaderBaser   
WriterBaser   r   rm   rl   <module>r*     s%  2 .      				        $ $ $ $ $ $             3 3 3 3 3 3 3 3 2 2 2 2 2 2 2 2I  I  I  I  I  I  I  I X
W W W W W W W W W Wrm   