a
    a9‚b„  ã                   @   sF   d Z ddlZddlZddlZddlZej e¡d ZG dd„ dƒZ	dS )a¬  
class to Interface between json <=> python dict<tuple<int, int, int, int>, float> <=> cpp map<OmegaPoint, double>
json file contains omega integrals computed for a mie-potential without BH-diameters.

Note:
    The database will only contain a mixture once. That is: AR,HE and HE,AR is the same entry. Internal functions
    ensure that the correct values are returned. So if OmegaDb is initialized with (HE,AR) and (AR,HE) is found in the
    database, the components will be inverted.

The class is written such that only a single instance of the database-object can read/write from the database file
at any given time (accross multiple processes). Whenever an object wants to read/write, it moves the database to a
name-mangled file, does whatever it wants to do, then moves the file back. If the database file does not exist, that
means some other instance is working on it, and it waits until the file is available before grabbing it
é    Nz/omega_db.jsonc                   @   sh   e Zd ZdZdd„ Zdd„ Zdd„ Zdd	„ Zd
d„ Zdd„ Z	dd„ Z
dd„ Zdd„ Zdd„ Zdd„ ZdS )ÚOmegaDbzË
    self._db mirrors the database entry for the current mixture. If the mixture is either 'X,Y' or 'Y,X',
    self._db will be the dict corresponding exactly to the entry found in the database file
    c           	      C   s  t j t¡d tt| ƒt ¡  ƒ d | _|| _	|| _
d| _i | _|  ¡  t| jdƒ}t |¡}W d   ƒ n1 sv0    Y  |  ¡  | j
 d¡\}}| j
| ¡ v r²|| j
 }n<d ||f¡| ¡ v rêd ||f¡| _
d| _|| j
 }ni }| ¡ D ]\}}|| j|< qöd| _d S )Nú/zomega_db.jsonFÚrú,T)ÚosÚpathÚdirnameÚ__file__ÚstrÚidÚtimeÚtime_nsÚworking_db_pathÚ_OmegaDb__true_compsÚ	_db_compsÚ_flipped_compsÚ_dbÚgrab_dbÚopenÚjsonÚloadÚ
release_dbÚsplitÚkeysÚjoinÚitemsÚ_updated)	ÚselfÚcompsÚfileÚdbZc1Úc2Úcomp_dbÚkÚv© r%   ú`/Users/vegardjervell/Documents/master/model/venv/lib/python3.9/site-packages/pykingas/OmegaDb.pyÚ__init__   s(    *(zOmegaDb.__init__c                 C   s:   zt  t| j¡ W q6W q  ty2   t d¡ Y q 0 q d S )Ng{®Gáz„?)ÚshutilÚmoveÚDB_PATHr   ÚFileNotFoundErrorr   Úsleep©r   r%   r%   r&   r   9   s
    zOmegaDb.grab_dbc                 C   s   t  | jt¡ d S ©N)r(   r)   r   r*   r-   r%   r%   r&   r   C   s    zOmegaDb.release_dbc                 C   sx   |j |j|j|jf}| jdu rp|d dkrDd|d |d |d f}n$|d dkrhd|d |d |d f}t|ƒS t|ƒS )NTr   é   é   é   )ZijÚlr   ZT_dKr   r
   )r   Zop_keyÚdb_keyr%   r%   r&   Ú
get_db_keyF   s    
zOmegaDb.get_db_keyc                 C   sz   t dd„ | d¡ d¡D ƒƒ}| jdu rv|d dkrNd|d |d |d	 f}n$|d dkrrd|d |d |d	 f}|S |S )
Nc                 s   s   | ]}t |ƒV  qd S r.   )Úint©Ú.0Úir%   r%   r&   Ú	<genexpr>X   ó    z)OmegaDb.get_return_key.<locals>.<genexpr>z()r   Tr   r/   r0   r1   )ÚtupleÚstripr   r   )r   r3   Úkeyr%   r%   r&   Úget_return_keyT   s    
zOmegaDb.get_return_keyc                    s<   ‡ fdd„ˆ j  ¡ D ƒ}dd„ |D ƒ}dd„ |D ƒ}||fS )Nc                    s   g | ]\}}ˆ   |¡|g‘qS r%   )r>   )r7   Zdb_kr$   r-   r%   r&   Ú
<listcomp>d   r:   z)OmegaDb.db_to_vectors.<locals>.<listcomp>c                 S   s   g | ]}|d  ‘qS )r/   r%   ©r7   Úpr%   r%   r&   r?   e   r:   c                 S   s   g | ]}d d„ |d D ƒ‘qS )c                 S   s   g | ]}|‘qS r%   r%   r6   r%   r%   r&   r?   f   r:   z4OmegaDb.db_to_vectors.<locals>.<listcomp>.<listcomp>r   r%   r@   r%   r%   r&   r?   f   r:   )r   r   )r   ÚpairsÚvalsZpointsr%   r-   r&   Údb_to_vectorsb   s    zOmegaDb.db_to_vectorsc                 C   s,   |  ¡ D ]\}}|| j|  |¡< qd| _d S )NT)r   r   r4   r   )r   Úmapr#   r$   r%   r%   r&   Úupdatei   s    zOmegaDb.updatec                 C   s|   t | jdƒ}t |¡}W d   ƒ n1 s,0    Y  z|| j }W n tyZ   i }Y n0 | ¡ D ]\}}|| j|< qdd S )Nr   )r   r   r   r   r   ÚKeyErrorr   r   )r   r   r    r"   r#   r$   r%   r%   r&   Úpull_update_from_dbo   s    (
zOmegaDb.pull_update_from_dbc                 C   sØ   | j du rÔ|  ¡  |  ¡  t| jdƒ}t |¡}W d   ƒ n1 sF0    Y  | j ¡ D ]4\}}| j	| 
¡ v r€||| j	 |< qZ||i|| j	< qZt| jdƒ }tj||dd W d   ƒ n1 sÂ0    Y  |  ¡  d S )NTr   Úwé   )Úindent)r   r   rH   r   r   r   r   r   r   r   r   Údumpr   )r   r   Zfull_dbr#   r$   r%   r%   r&   rL   y   s    
(.zOmegaDb.dumpc                 C   s   t |ƒddtt |ƒƒ   S )Nú é   )r
   Úlen)r   Úthingr%   r%   r&   Útable‹   s    zOmegaDb.tablec                 C   sz   d| j  d | j d }||  d¡|  d¡ d 7 }| j ¡ D ]6\}}||  |¡|  |  |¡¡ d t|ƒ d 7 }q>|S )NzOmega values for mixture z, using database entry for Ú
zDatabase keyz
Return keyz
		 Value

ú	)r   r   rQ   r   r   r>   r
   )r   r   r#   r$   r%   r%   r&   Ú__repr__Ž   s
    0zOmegaDb.__repr__N)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r'   r   r   r4   r>   rD   rF   rH   rL   rQ   rT   r%   r%   r%   r&   r      s   

r   )
rX   r   r   r(   r   r   r   r	   r*   r   r%   r%   r%   r&   Ú<module>   s    