B
    âêõbÉ  ã               @   sh   d Z ddlmZ ddlmZ ddlmZmZ ddl	Z
G dd„ deƒZG dd	„ d	eƒZG d
d„ deƒZdS )z©
This module contains classes for reading and writing (single frame) NAMD binary
files.  The main functionality is currently aimed at manipulation rather than
analysis.
é    )Údivision)Úunit)ÚunpackÚpackNc               @   sZ   e Zd ZdZdZg fdd„Zedd„ ƒZedd„ ƒZ	d	d
„ Z
dd„ Zddd„Zdd„ ZdS )ÚNamdBinFilea¿  From the NAMD manual:

        NAMD uses a trivial double-precision binary file format for
        coordinates, velocities, and forces ... The file consists of the atom
        count as a 32-bit integer followed by all three position or velocity
        components for each atom as 64-bit double-precision floating point ...

    The main attributes are the number of atom entries (natom) and a (flat)
    numpy array of size 3*natom (values).  The meaning of "values" is
    effectively arbitrary, but for convenience derived classes are provided
    which alias the values to more descriptive names (e.g. "coordinates").

    See also
    --------
    :class:`NamdBinCoor` and :class:`NamdBinVel`
    g      ð?c             C   s   t  |t j¡| j | _d S )N)ÚnpÚasarrayÚfloat64Ú_SCALE_FACTORÚ_values)ÚselfÚvalues© r   ú7lib/python3.7/site-packages/parmed/namd/namdbinfiles.pyÚ__init__    s    zNamdBinFile.__init__c             C   s   t | jjd ƒS )z#The current number of atom entries.é   )Úintr   Úsize)r   r   r   r   Únatom#   s    zNamdBinFile.natomc                sL   t |dƒ‰ ttdˆ  d¡ƒd ƒ}‡ fdd„td| ƒD ƒ}ˆ  ¡  | |ƒS )z7Return an object from the values in a NAMD binary file.ÚrbÚié   r   c                s    g | ]}t d ˆ  d¡ƒd ‘qS )Údé   r   )r   Úread)Ú.0Ún)Úinfiler   r   ú
<listcomp>-   s    z$NamdBinFile.read.<locals>.<listcomp>r   )Úopenr   r   r   ÚrangeÚclose)ÚclsÚfnameÚnatomsr   r   )r   r   r   (   s
    
zNamdBinFile.readc             C   sN   t |dƒ}| td| jƒ¡ x$| j| j D ]}| td|ƒ¡ q*W | ¡  dS )z'Write the current attributes to a file.Úwbr   r   N)r   Úwriter   r   r   r
   r!   )r   r#   ZoutfileÚxr   r   r   r&   1   s
    
zNamdBinFile.writec             C   sn   t  t  |t j¡¡}t  | jt j¡}d||< t  d| j|j  t j	¡}|| j
 | jdf¡|  ¡ 7 }|| _
dS )z7Delete entries corresponding to the given atom indices.Fr   N)r   Z
atleast_1dr   Zint32Zonesr   ÚboolÚzerosr   r	   r   ÚreshapeÚflatten)r   ÚindicesZdel_indicesÚmaskÚ	newvaluesr   r   r   Údelatoms9   s    zNamdBinFile.delatomsNc             C   sj   |dk	r&t  |¡}|jd| ks4t‚nt  d| ¡}d| }t  | jd|… || j|d… f¡}|| _dS )z’Insert space for natom entries beginning at start_index. If
        specified, give them the provided values, otherwise set them to zero.
        Nr   )r   r   r   ÚAssertionErrorr)   Zconcatenater   )r   Ústart_indexr$   r   Zhinger.   r   r   r   ÚinsertatomsB   s    
 zNamdBinFile.insertatomsc             C   s,   | j d||  d| … }|  |||¡ dS )z—Convenience function, same as insertatoms() but set 'values' to
        be the same as the previous natoms' values (i.e. make a copy of them).
        r   N)r   r2   )r   r1   r$   r   r   r   r   Ú	copyatomsQ   s    zNamdBinFile.copyatoms)N)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r
   r   Úpropertyr   Úclassmethodr   r&   r/   r2   r3   r   r   r   r   r      s   		
r   c               @   s*   e Zd ZdZedd„ ƒZejdd„ ƒZdS )ÚNamdBinCoorz5 Class to read or write NAMD "bincoordinates" files. c             C   s   | j  d| jdf¡S )Néÿÿÿÿr   )r   r*   r   )r   r   r   r   Úcoordinates[   s    zNamdBinCoor.coordinatesc             C   s*   t  |¡r| t j¡}t |¡ ¡ | _d S )N)ÚuÚis_quantityÚvalue_in_unitÚ	angstromsr   Úarrayr+   r   )r   Úvaluer   r   r   r<   _   s    
N)r4   r5   r6   r7   r8   r<   Úsetterr   r   r   r   r:   Y   s   r:   c               @   s.   e Zd ZdZdZedd„ ƒZejdd„ ƒZdS )Ú
NamdBinVelz4 Class to read or write NAMD "binvelocities" files. g,	Ô‹ot4@c             C   s   | j  d| jdf¡S )Nr;   r   )r   r*   r   )r   r   r   r   Ú
velocitiesj   s    zNamdBinVel.velocitiesc             C   s0   t  |¡r| t jt j ¡}t |¡ ¡ | _d S )N)	r=   r>   r?   r@   Z
picosecondr   rA   r+   r   )r   rB   r   r   r   rE   n   s    
N)r4   r5   r6   r7   r
   r8   rE   rC   r   r   r   r   rD   e   s   rD   )r7   Z
__future__r   Zparmedr   r=   Zstructr   r   Znumpyr   Úobjectr   r:   rD   r   r   r   r   Ú<module>   s   K