
     i-                         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	m
Z
 ddlmZ ddlmZ dd	lmZ  G d
 dej                  Z G d dej                  ZdS )u	  
FHI-AIMS file format --- :mod:`MDAnalysis.coordinates.FHIAIMS`
==============================================================

Classes to read and write `FHI-AIMS`_ coordinate files.

The cell vectors are specified by the (optional) lines with the
``lattice_vector`` tag::

    lattice_vector x  y  z

where x, y, and z are expressed in ångström (Å).

.. Note::

   In the original `FHI-AIMS format`_, up to three lines with
   ``lattice_vector`` are allowed (order matters) where the absent line implies
   no periodicity in that direction.  In MDAnalysis, only the case of no
   ``lattice_vector`` or three ``lattice_vector`` lines are allowed.

Atomic positions and names are specified either by the ``atom`` or by the
``atom_frac`` tags::

    atom           x  y  z  name
    atom_frac      nx ny nz name

where x, y, and z are expressed in ångström, and nx, ny and nz are real numbers
in [0, 1] and are used to compute the atomic positions in units of the basic
cell.

Atomic velocities can be added on the line right after the corresponding
``atom`` in units of Å/ps using the ``velocity`` tag::

    velocity      vx vy vz


The field name is a string identifying the atomic species.  See also the
specifications in the official `FHI-AIMS format`_.

Classes
-------

.. autoclass:: FHIAIMSReader
   :members:
   :inherited-members:

.. autoclass:: FHIAIMSWriter
   :members:
   :inherited-members:

Developer notes: ``FHIAIMSWriter`` format strings
-------------------------------------------------

The :class:`FHIAIMSWriter` class has a :attr:`FHIAIMSWriter.fmt`
attribute, which is a dictionary of different strings for writing
lines in ``.in`` files.  These are as follows:

``xyz``
  An atom line without velocities.  Requires that the `name` and
  `pos` keys be supplied.  E.g.::

     fmt['xyz'].format(pos=(0.0, 1.0, 2.0), name='O')

``vel``
  An line that specifies velocities::

     fmt['xyz'].format(vel=(0.1, 0.2, 0.3))

``box_triclinic``
  The (optional) initial lines of the file which gives box dimensions.
  Requires the `box` keyword, as a length 9 vector.  This is a flattened
  version of the (3, 3) triclinic vector representation of the unit
  cell.


.. Links

.. _FHI-AIMS: https://aimsclub.fhi-berlin.mpg.de/
.. _FHI-AIMS format: https://doi.org/10.6084/m9.figshare.12413477.v1

    N   )base)triclinic_boxtriclinic_vectors   )NoDataError)util)mdamathc                   4    e Zd ZdZddgZddddZd Zdd
Zd	S )FHIAIMSReadera  Reader for the FHIAIMS geometry format.

    Single frame reader for the `FHI-AIMS`_ input file format.  Reads
    geometry (3D and molecules only), positions (absolut or fractional),
    velocities if given, all according to the `FHI-AIMS format`_
    specifications

    INFHIAIMSpsAngstromAngstrom/pstimelengthvelocityc                 j  
 t          j        | j        d          5 }g g g g f\  }}}}ddg}d}|D ]u

                                

                    d          rI|                    
                                dd                    |                    d
v            
}u
                    d	          r^d|vr(t          d
                    
| j                            |                    
                                dd                     
}
                    d          r3|                    
                                dd                     
}0t          
fd|D                       r
}Ot          d                    
| j                            	 d d d            n# 1 swxY w Y   t          j        |t          j                  }t          |          dt          |          fvr=t          d                    t          |          t          |                              t          |          dvrt          d          t          |          dk    rt          |          rt          d          t          |          x| _        } | j        |fi | j        x| _        }	||	_        t          |          dk    r6t'          | |	_        t          j        |	j        |         |          |	j        |<   t          |          dk    r||	_        d| j        _        d S )Nrt#initial_moment atomr   	atom_fracr   zPNon-conforming line (velocity must follow atom): ({0})in FHI-AIMS input file {0}latticec                 :    g | ]}                     |          S  )
startswith).0taglines     h/srv/www/vhosts/g4struct/public_html/venv/lib/python3.11/site-packages/MDAnalysis/coordinates/FHIAIMS.py
<listcomp>z3FHIAIMSReader._read_first_frame.<locals>.<listcomp>   s%    BBB,,BBB    z4Non-conforming line: ({0})in FHI-AIMS input file {0})dtyper   zRFound incorrect number of velocity tags ({0}) in the FHI-AIMS file, should be {1}.)r      zQFound partial periodicity in FHI-AIMS file. This cannot be handled by MDAnalysis.zAFound relative coordinates in FHI-AIMS file without lattice info.)r	   openanyfilenamestripr!   appendsplit
ValueErrorformatanynpasarrayfloat32lenn_atoms	_Timestep
_ts_kwargsts	positionsr   
dimensionsmatmul
velocitiesframe)selffhiaimsfilerelativer:   r=   lattice_vectors	skip_tagsoldliner6   r9   r$   s             @r%   _read_first_framezFHIAIMSReader._read_first_frame   s   \$-..  	+?A2r2~<Hi_./IG#  zz||??6** $$TZZ\\!B$%7888OOK4$7888"G??:.. 	!W,,(nuu $dm   
 %%djjll122&6777"G??9-- #**4::<<+;<<<"GBBBB	BBBCC "G JQQdm   1	 	  	  	  	  	  	  	  	  	  	  	  	  	  	  	H *_BJGGGz??1c)nn"555dkk
OOS^^   
 v--c   1$$X$S   "%Y/w%t~gAAAAA" !##)?;BM%'YX&& &BL" z??Q&BMs   FF44F8;F8Nc                 2    || j         }t          |fd|i|S )zReturns a FHIAIMSWriter for *filename*.

        Parameters
        ----------
        filename: str
            filename of the output FHI-AIMS file

        Returns
        -------
        :class:`FHIAIMSWriter`

        Nr6   )r6   FHIAIMSWriter)r?   r+   r6   kwargss       r%   WriterzFHIAIMSReader.Writer   s+     ?lGXAAwA&AAAr'   )N)__name__
__module____qualname____doc__r0   unitsrE   rI   r    r'   r%   r   r   w   sf          IFZ]KKEE E ENB B B B B Br'   r   c                   @    e Zd ZdZddgZddddZdd	d
dZddZd ZdS )rG   a(  FHI-AIMS Writer.

    Single frame writer for the `FHI-AIMS`_ format.  Writes geometry (3D and
    molecules only), positions (absolut only), velocities if given, all
    according to the `FHI-AIMS format`_ specifications.

    If no atom names are given, it will set each atom name to "X".

    r   r   Nr   r   r   z=atom {pos[0]:12.8f} {pos[1]:12.8f} {pos[2]:12.8f} {name:<3s}
z6velocity {vel[0]:12.8f} {vel[1]:12.8f} {vel[2]:12.8f}
zlattice_vector {box[0]:12.8f} {box[1]:12.8f} {box[2]:12.8f}
lattice_vector {box[3]:12.8f} {box[4]:12.8f} {box[5]:12.8f}
lattice_vector {box[6]:12.8f} {box[7]:12.8f} {box[8]:12.8f}
)xyzvelbox_triclinicTc                 L    t          j        |dd          | _        || _        dS )zSet up the FHI-AIMS Writer

        Parameters
        -----------
        filename : str
            output filename
        n_atoms : int (optional)
            number of atoms

        z.inT)extkeepN)r	   r+   r6   )r?   r+   convert_unitsr6   rH   s        r%   __init__zFHIAIMSWriter.__init__   s'     hEEEEr'   c                    	 |j         }n # t          $ r d}t          |          dw xY wg }	 |j        }n@# t          t          f$ r, t          j        d          }|                    d           Y nw xY w	 |j        }nD# t          t          f$ r0 t          d|j
        dz             }|                    d           Y nw xY w|r;t          j        d                    d                    |          	                     |j        }	 |j        }d
}	n# t          t          f$ r d}	Y nw xY wt#          j        | j        d          5 }
	 |j        j        j        j        }n# t          $ r
 |j        }Y nw xY w|F|
                    | j        d                             |                                                     t7          t          |j
                  |          D ]|\  }}|
                    | j        d                             ||         |                     |	r:|
                    | j        d                             ||                              }	 ddd           dS # 1 swxY w Y   dS )a1  Write selection at current trajectory frame to file.

        Parameters
        -----------
        obj : AtomGroup or Universe


        .. versionchanged:: 2.0.0
           Support for deprecated Timestep argument has now been removed.
           Use AtomGroup or Universe as an input instead.
        z-Input obj is neither an AtomGroup or UniverseN)Xnamesr   idszlSupplied AtomGroup was missing the following attributes: {miss}. These will be written with default values. z, )missTFwtrR   )boxrP   )posnamerQ   )rQ   )atomsAttributeError	TypeErrorrZ   r   	itertoolscycler-   r[   ranger6   warningswarnr0   joinr:   r=   r	   r*   r+   universe
trajectoryr9   triclinic_dimensionswritefmtflattenzip)r?   objagerrmsgmissing_topologyrZ   atom_indicesr:   r=   has_velocitiesoutput_fhiaimstri_dims
atom_indexr`   s                 r%   _write_next_framezFHIAIMSWriter._write_next_frame  s-   	.BB 	. 	. 	.DFF##-	.
 	-HEE, 	- 	- 	-OF++E##G,,,,,	-	+6LL, 	+ 	+ 	+ BJN33L##E*****	+  	M6tyy)9::6;;   L		#J!NN, 	# 	# 	#"NNN	# \$-.. 	.4<25J! 4 4 434 #$$H_-449I9I9K9K4LL   %(bj(9(95$A$A 	 	 
D$$HUO**%j1 +    
 " "((..:j3I.JJ  		 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	sr   
 '5 :A21A26A> >>B?>B?	D D'&D'I)EI)E1.I)0E11C*I))I-0I-)TN)	rJ   rK   rL   rM   r0   rN   rn   rW   rz   r    r'   r%   rG   rG      s|          IFZ]KKE
 PH S C   I I I I Ir'   rG   )rM   rerd   rg   numpyr2   r   r   corer   r   
exceptionsr   libr	   r
   SingleFrameReaderBaser   
WriterBaserG   r    r'   r%   <module>r      s  0P Pb 
			                2 2 2 2 2 2 2 2 $ $ $ $ $ $            cB cB cB cB cBD. cB cB cBLn n n n nDO n n n n nr'   