
1Ic           @   s   d  d k  Z  d  d k Z d  d k Z d  d k Z d  d k Z d  d k Td  d k Z d  d k Z d  d k Z	 d  d k
 Z e i i d  Z d e f d     YZ d   Z d   Z d   Z d	   Z d
   Z d   Z d   Z d   Z d   Z d S(   iN(   t   *t   LMTO_DMFT_ROOTt   IMP_OCAc           B   s2   e  Z d  Z e d  Z d d d  Z d   Z RS(   s4   One crossing approximation Impurity solver
    
    c         C   s{  | d |  _  | |  _ t t i |   d j o t i |  n t |  i  d d  |  _ t |  i  d d  |  _ h  d d <d d	 <d
 d <d d <d d <d d <d d <d d <d d <|  _	 t
 i d |  i  |  i	 d  t d d t |  d |  _ t d |  _ d |  _ t |  i  t   } | d |  _ h  |  _ | i d  o | d d |  i d <n | d |  i d <| i d  o | d d |  i d <n- |  i d d t |  i d  d !|  i d <| i d  o | d d |  i d <n | d |  i d <| i d   o | d  d |  i d  <n d! |  i d  <| i d"  o | d" d |  i d" <n d# |  i d" <| i d$  o | d$ d |  i d$ <n | d$ |  i d$ <| i d%  o | d% d |  i d% <n | d% |  i d% <| i d&  o | d& d |  i d& <n | d& |  i d& <| i d'  o | d' d |  i d' <n | d' |  i d' <| i d(  o | d( d |  i d( <n | d( |  i d( <| i d)  o | d) d |  i d) <n | d) |  i d) <|  i d& |  _ |  i d' |  _ t |  t j o | i   } n | |  _ |  i d* I| IJ|  i o!t t i |  i  d |  i	 d   d j o t d+ |  _ d, |  i d | d |  i d& |  i d' |  i d |  i d$ |  i d% | d- |  i d( |  i d) |  i d |  i d" |  i d  f } t i d. |  i  d/ |  i d0 | d1  \ } }	 }
 |  i |	 i   IJ|  i |
 i   IJn t d2 |  _ d  S(3   Nt   /i    s   info.outt   ws	   jinfo.outs	   Sigma.000t   Sigs   Ac.inpt   Acs   out.cixt   cixs   Aloc.impt   AlocOutt   SigOuts   sig.inpt   sigs   gloc.outt   gloct   pcoret   acores   /impurity/database/s   atom.s   .pys   /impurity/bin/broads
   PARAMS.ocat   lt   nct   nt   Ncentrali   t   parat   kOCAit   mOCAgMbP?t   Ext   Ept   Jt   cxt   Eocat   qatoms   Impq=s   /impurity/bin/atomse   n="%s" l=%d J=%f cx=%f para=%s Ex="%s" Ep="%s" qOCA=%d Eoca=%f qatom=%d Ncentral=%s mOCA=%s kOCA="%s"t   qOCAs   cd s   ; t    s    > nohup.out 2>&1s   /impurity/bin/atom_d.py(   t   dirt
   UpdateAtomt   lent   globt   ost   mkdirt   opent   fh_infot   fh_infojt   sparamst   shutilt   copy2t   ROOTt   strt   atom_argt   broadt   PARAMSt   execfilet   localsR   t   sprt   has_keyR   R   t   typet   listt   tolistt   Impqt   atom_exet   popen3t   read(   t   selft   Znuct   direR5   t   paramsR   t   lcsR+   t   stdint   stdoutt   stderr(    (    s!   /mnt/home/haule/12014haule/oca.pyt   __init__   s`    	-Z			)),)))))))))#	96g        g      4@c
         C   s<  |  i  o d G| GHd G| d GH|  i d t |  d t |  i d  d t |  i d  d	 }
 |
 d
 t |  i  d	 7}
 t i d |  i d |  i d |
 d  \ } } } |  i	 | i
   IJ|  i	 | i
   IJn t i t d | d d |  i | d d  d | d d |  _ |  i d os | d d d d |  i | d d d <xG t d t | d d   D]$ } | d d d | d d | <qyWn t |  i |  i d  } x= |  i i   D], } | i | d t |  i |  d  qWxD | i   D]6 } | d | | | d f Id I| | d IJqW| i   t |  i |  i d d d  } t } d | i   j o t } | d d } n x t t |   D] } | | | Ix t t | |   D] } | | | i t } | d j  o
 d } n | |	 j o
 d } n | oN | t | d | | | d d  t | | | d | d d  9} n | | IqW| JqW| i   | d j o d |  i d |  i d  t |  d! t |  d |  i d d d" |  i d } | GHt i |  \ } } } | i
   GHn1 t i |  i |  i d d |  i |  i d  t i |  i |  i d |  i |  i d d# |  | i d$  o' d% G| d$ d GHt  | d$ d  n d |  i d | d |  i d |  i d& } | GHd' GHt i |  \ } } } |  i! | i
   I| i
   IJd( S()   s	   
        s   Eimps=s   Ed=t   Eds    "Eimp=s   " "n=R   s   " "Ncentral=R   s   "s    "Impq=s   cd s   ; R   s    > nohup.out 2>&1s   /impurity/bin/t   exei    s   ./R   i   i   R   t   =s   
s   %s=%s s   	R   t   bt   cutAcg        t   Ths    -w s    -k s    > t   .t   external_exes   Running external python code s    > nohup_imp.out 2>&1 s   Running ---- OCA -----N("   R   R+   R*   R0   R5   R!   R7   R   R6   R$   R8   R'   R(   R)   t   oca_exeR   t   rangeR   R#   R-   R&   t   keyst   writet   closet   Falset   Truet   imagt   pit   fermR,   t   moveR1   R.   R%   (   R9   R<   t
   mpi_prefixt   omt   Deltat   gbroadt   extnt   Eimpst   kbroadt   maxAcR+   R>   R?   R@   t   jt   fpt   pt   QcutAcRF   t   imRE   R   t	   broad_cmdt   oca_cmd(    (    s!   /mnt/home/haule/12014haule/oca.pyt   _exed   sl    
	E6/) , * 4
!  U
V04/c   V      B   s  e  |  i |  i d  } | i   } | i   i   } e | d  } e e | d d | ! }	 e  |  i |  i d  }
 |
 i   i   } | i   } x9 | D]1 } e i	 d |  } | d j	 o | d Uq q Wg  } g  } xR |
 D]J } | i   } | i e | d   | i e e | d d | !  q We |  } e |  } e  |  i |  i d  } g  } d } x | D] } | i   } e | d  } e | | |  d j o. d	 |  i d |  i d f GHe i d  n | d 7} | i e e | d d | !  q|We |  } | d
 d } | d d } | d d } d | d | d g } | d | d g } e | d |  | d g } x e d e |  d  D]g } | i d | | d | | d  | i | | | d  | i e | | |  | d  qW| i d | d | d  | i | d | d  | i e | d |  | d  e |  } e |  } e |  } e | d d |  i |  i | |	 e |  \ } } | d j o e } e |  | d d e |  } d } e | d d |  i |  i | |	 | |  \ }  }! | |! | d d }" |  i d e |"  e |"  IJ|! } n |  i d |  i | | d d |  i f IJ|  i d I| IJ|  i d | Id I|	 IJ|  i d e IJ|  i d Ie IJ|  i d Ie IJ|  i d I| i   IJ|  i i   g  }# g  }$ g  }% g  }& g  }' g  }( g  }) g  }* xe |  D]}+ e |+ |	 |+ }, | |+ | |+ }- |- d j  o | d d |, | d d d |, |- j oV |  i d IJ|- | d d |, d |, d | d d <|  i d I| d d IJn |- d j o | d d |, | d d d |, |- j  oV |  i d IJ|- | d d d |, |, d | d d <|  i d I| d d IJn e | d d  |+ f  }. e |. |  }/ e |. |  }0 e |. |  }1 |  i d  |+ |/ |0 |1 |, f IJ|, |1 }2 |2 d j  o x e e |   D] } e | | | |. | | | |, | d! d |  i 	 \ }3 }4 }5 |  i d" |+ | | |3 |4 |5 f IJ|3 d j o | | }6 PqOqOWe  |6 | |. | | | |, | d! d |  i 	 \ }2 }7 }/ }0 }1 n d |/ |2 }7 |7 d j  o x e e |  d d d  D] } e | | | |. | | | |, | d! d |  i 	 \ }3 }4 }5 |  i d# |+ | | |3 |4 |5 f IJ|4 d j o | | }8 PqOqOWe  |8 | |. | | | |, | d! d |  i 	 \ }2 }7 }/ }0 }1 n e! |/ |0 |, |1 |- |  \ }2 }7 }9 }: }; |  i d$ |+ |, |1 d |/ | |+ |2 |7 |9 |: |; f	 IJ|  i i   e" |9 |: |;  \ }< }= }> d }? |< p| d }@ e# |@ | d! d | |. | | | |, | |- |  i  \ }< }2 }7 }= }> |@ d% :}@ xn e d&  D]` }A e# |@ | d! d | |. | | | |, | |- |  i  \ }< }2 }7 }= }> |< o Pn |@ d% :}@ q
We$ |@ | d! d | |. | | | |, | |- |  i  \ }B }3 }4 }C }D n |< p d' GHe i d  n |  i d( |+ |2 |7 |= |> f IJ| d d |= | d d | d d }E | d d |> | d d | d d }F |  i d) |+ |2 |7 |E |F f IJ|  i J|. }G xr e e |   D]^ }H |G |H c |2 e% | |H |E | d* d d+ |7 e% | |H |F | d* d d+ i& 7<qWe | d d  |+ f  }I g  }J g  }K g  }L x e e |   D] }H e% e' i( |G | |H  }M |M e% |G |H d+ }N e% e' i( |I | |H  }O |O e% |I |H d+ }P |J i |N  |L i |P  | |H | |+ d |N |P | |+ }Q |Q i& d j o |Q i) d, }Q n |K i |Q  q3W|# i |G  |$ i |J  |% i |K  |& i |L  |' i |E  |( i |F  |) i |2  |* i |7  qrW|  i JxD e |  D]6 }+ |  i d- |+ |' |+ |( |+ |) |+ |* |+ f IJqW|  i i   e* i+ |  i |  i d. |  i |  i d. d/  e* i+ |  i |  i d0 |  i |  i d0 d/  e* i+ |  i |  i d |  i |  i d d/  e  |  i |  i d. d1  }R e  |  i |  i d0 d1  }S e  |  i |  i d d1  }T e  |  i d2 d1  }U |S d3 I| i   IJ|S d4 I| i   IJ|T | IJxBe e |   D].}H |R | |H Ix: e |  D], }+ |R |$ |+ |H i) I|$ |+ |H i& Iq9W|R J|S | |H Ix: e |  D], }+ |S |% |+ |H i) I|% |+ |H i& IqW|S J|T | |H Ix& e |  D] }+ |T |# |+ |H IqW|T J|U | |H Ix: e |  D], }+ |U |& |+ |H i) I|& |+ |H i& IqW|U JqW|R i,   |S i,   |T i,   |U i,   e* i- |  i |  i d |  i |  i d d5 |  e* i- |  i |  i d d/ |  i |  i d d6 |  e* i- |  i d7 |  i d7 d5 |  |% | | e f S(8   s>	   Corrects high-frequency of OCA spetral functions
        such that it gives correct nf, normalization and sigma_oo
        
        This is achieved by adding two Lorentzians at high frequency. One below EF in the interval (par['epsilon'][0][0],par[epsilon][0][1])
        and one above EF in the interval (par['epsilon'][1][0],par[epsilon][1][1])
        
           Input:
              params       --  parameters must contain
                               T
                               Ed
                               U
                               epsilon    - prefered positions of additional lorentzians
                               Th         - when high-frequency spectral function is cut, it is cut by fermi function with temperature Th
                               Gh         - the width of the two lorentzians added
           Output:
               Sig[b][iom]  -- DMFT dynamic self-energy which vanishes at infinity
               sinf[b]      -- DMFT self-energy at infinity
               Edc[b]       -- double counting using DMFT occupancies

           The 'corrected' Green's function will be of the form:
                      G(om) = int(A(x)/(om-x)) + a1/(om-eps1+i*Gh)  + a2/(om-eps2+i*Gh)
           
           The equations to be satisfied are:
              normalization:    m0 + a1 + a2 = 1
              density:          nc + a1 = nc_{exact}    where nc=int(A(x)f(x)) and nc_{exact} is computed from pseudo spectral functions
              Sigma_oo:         m1 + a1*eps1 + a2*eps2 = Eimp+Sigma_oo == dsinf

           The equations can be brought to the form
              x*u + y*v = w
           with u and v positive numbers and x and y unknown numbers in the interval [0,1]
           
           In terms of above quantities, we have
              u = a1*(eps[0][1]-eps[0][0])
              v = a2*(eps[1][1]-eps[1][0])
              w = Eimp+Sigma_oo - m1 - a1*eps[0][0] - a2*eps[1][0]
              x = (eps1-eps[0][0])/(eps[0][1]-eps[0][0])
              y = (eps2-eps[1][0])/(eps[1][1]-eps[1][0])

           The solution exists if 0<w<u+v
           In this case x is in the interval x=[max((w-v)/u,0), min(w/u,1)] and y is in the interval y=[max((w-u)/v,0), min(w/v,1)]
           The solution choosen is:
                 if (v>u):  x=0.5*(x_min+x_max)
                 else    :  y=0.5*(y_min+y_max)
        R   i    i   R   s   [ntot|nf|moment]=NR   gh㈵>s1   Seems that %s and %s are not compatible. Exiting!t   TRB   t   epsilong      ?iit   Ut   fixnt   nf0g        s   ### a=%fs   # l=%d T=%f U=%f J=%fs   # Eimp=s
   # baths=%ds   Ns=s	   # ntot=%fs   # nf=s	   # moment=s	   # sinfty=sD   epsilon was not choosen correctly and the solution can not be found!s   setting epsilon[1][0] to s   setting epsilon[0][1] to s*   #start [%d]: m0=%f m1=%f nc=%f nf_exact=%fRG   s%   #ca1 [%d]: cat L=%f a1=%f a2=%f m1=%fs%   #ca2 [%d]: cat L=%f a1=%f a2=%f m1=%fsF   # [%d]: miss-nf=%f  miss-weight=%f  s_oo=%f a1=%f a2=%f u=%f v=%f w=%fg?id   sc   Can't determin a way to get exact nf, norm and sigma_oo. You have to figure out the way to do that!s   # [%d] a1=%f a2=%f x=%f y=%fs$   # [%d]: a1=%f a2=%f eps1=%f  eps2=%ft   Ghy              ?y        -q=s'   ## [%d]: eps1=%f  eps2=%f  a1=%f  a2=%fR   t   _oR
   R   s	   Delta.outs   # s_oo=s   # Edc=RH   s   _o.s   nohup_imp.out(.   R#   R   R&   t   nextt   splitt   intt   mapt   stript   ret   searcht   Nonet   appendt   floatt   arrayt   abst   syst   exitRS   RK   R   t   SinftyvR   R   t   nft   sumR$   t   ntott   momentR4   t   flusht   dott   trycutAft   cutAft   uvwt   Soluvwt   wwt   ww_cutRR   RQ   t   kramst   kramarskronigt   realR'   RT   RN   R(   (V   R9   R<   t   DCst   daRY   t   fct
   first_linet   cixdatt   bathst   Nst   fAt   adatt   part   mRV   t   AfR_   t   datt   fAct   Acdt   iit   omegaRe   RB   Rf   t   dht   omdht   fedhRa   t   sinft   Edct   nfdct
   sinf_dummyt   Edcnt   tat   _Aft   _Gft   _Sigt   _Deltat   _eps1t   _eps2t   _a1t   _a2RE   t   nf_exactt   dsinft   Afot   m0t   m1R   t   a1t   a1nt   a2nt   m1nt   L1t   a2t   L2t   ut   vR   t   successt   xt   yt   Lbt   L0R]   t   successnt   xnt   ynt   eps1_t   eps2_t   Afnt   iR   t   gfR
   RW   t   grt   gct   deltart   deltat   sigmt   fgt   fst   fat   fd(    (    s!   /mnt/home/haule/12014haule/oca.pyt   HighFrequency   s   1  % 
% %'#2"2!+ :*:* 
 <$
	B <$
	B*8
D
 DH **  \ & 4000
  0 0  0



48&(   t   __name__t
   __module__t   __doc__RO   RA   Rd   R   (    (    (    s!   /mnt/home/haule/12014haule/oca.pyR      s   QQc         C   s\   xU t  t |   d  D]= } x3 t  t |   d  D] } d |  | | f i Gq4 WHq Wd S(   s    Prints matrix in readable formi    i   s   %8.4f N(   RK   t   shapeR   (   t   aR   R]   (    (    s!   /mnt/home/haule/12014haule/oca.pyt   printm  s      c         C   s<   |  d j o d Sn |  d j  o d Sn d t  |   d S(   s   Fermi function for T=1id   i    ii   (   t   exp(   R   (    (    s!   /mnt/home/haule/12014haule/oca.pyRS      s    c         C   s#  t  |  } g  } xN t |  D]@ }	 | |	 | |	 }
 x% t | |	  D] } | i |
  qH Wq Wt | | f d t d d } x( t |  D] } | | | | | f <q Wt | d d d t } g  } xO t d d  D]> } x5 t | | d  D] } | i d | | | g  q Wq Wt | d d d t } g  } xo t | d | d d  D]R } | d j  o qVn x5 t | | d  D] } | i d | | | g  qWqVWt | d d d t	 } t
 i | |  } t i | | d  t t |   |  } t t |   | } t i | | | | | d	 | d
 d \ } } t i | | d  g  } g  } d } x t |  D] }	 d } d } xJ t | |	  D]8 } | | | | f i 7} | | | i 7} | d 7} qW| | |	 :} | | |	 :} | i |  | i |  qtWt |  t |  f S(   s6   Computes Sigma_oo using OCA occupancies
        (density matrix given as a vector of numbers)
        Input:
            l             -- quantum number l
            baths         -- number of nonequivalent baths for OCA solver
            Ns[baths]     -- degeneracy of each bath
            nf[baths]     -- occupancy of each bath
            U, J          -- Coulomb interaction
        Output:
            sig_oo[baths] -- hartree-fock value of self-energy using impurity occupancies
            Edc[baths]    -- double counting using impurity occupancies
    t   dtypet   ordert   Fi   i   g      ?i    t   CR   t
   subtractdct   N(   R|   RK   Rt   t   zerost   complext   onesRn   Rv   t   arangeRu   t   stst   cmp_t2jt   lat
   ztransformR   t   ldaut   hartreeR   (   Rg   R   R   R   R   R{   t   da_t   sizet   occ0RE   t   aoccR]   t   occjt   mjt   corindt   bndindt   sR   t   bndindJt   T2Ct   Uct   Jct   sinftyR   t   sinftyvt   EdcvR   t   averSt   averE(    (    s!   /mnt/home/haule/12014haule/oca.pyRz     sf         !  !-  c	         C   s   d }	 d }
 d } x t  t |   D] } |  d j  o t | | |  |  } n t | | |  |  } |	 | | | | | 7}	 |
 | | | | | 7}
 | | | | | | 7} q% W| | } | } d |	 | } | | |
 f S(   Ni    i   (   RK   R   RS   (   t   LRV   R   R   R   R   R   RG   R$   R   R   R   R   t   wcutt   miss_dpR   R   (    (    s!   /mnt/home/haule/12014haule/oca.pyR   V  s     *
c	         C   s   d }	 d }
 d } x t  t |   D] } |  d j  o t | | |  |  } n t | | |  |  } | | c | 9<|	 | | | | 7}	 |
 | | | | 7}
 | | | | | 7} q% W| | } | } d |	 | } | | |	 |
 | f S(   Ni    i   (   RK   R   RS   (   R   RV   R   R   R   R   R   RG   R$   R   R   R   R   R   R   R   R   (    (    s!   /mnt/home/haule/12014haule/oca.pyR   f  s     *
c   	      C   sU  | d j  p | |  | j o t  d d f Sn | d j o t d d f Sn | |  j  o | |  p d } | | j  o | | p d } | | d j o  | | |  j  o | | |  p d } | |  d j o  | |  | j  o | |  | p d } | |  j o$ d | | } | | |  | } n! d | | } | | | |  } t | | f S(   s   Given positive numbers u and v, gives one solution to the equation x*u + y*v = w
    where x and y are numbers in the interval [0,1].
    If there is no solution, gives False, if solution exists, gives the 'average' solution
    x = (x_min+x_max)/2 or y = (y_min+y_max)/2.
    i    g        g      ?g      ?(   RO   RP   (	   R   R   R   t   x_maxt   y_maxt   x_mint   y_minR   R   (    (    s!   /mnt/home/haule/12014haule/oca.pyR   w  s    /77c         C   s   | | } | d j  o
 d } n d |  | } | d j  o
 d } n | | | | d d | | d d } | | d d | d d }	 | | d d | d d }
 | | |	 |
 | f S(   Ni    i   (    (   R   R   R   R   R   Rf   R   R   R   R   R   (    (    s!   /mnt/home/haule/12014haule/oca.pyR     s    
*c         C   s3  d } d } d } x t  t |   D] } t |  | | |  t | | |  |  } | | | | | | 7} | | | | | | 7} | | | | | | 7} q% Wt | | | | |	 |  \ } } } } } t | | |  \ } } } |
 d |  | | | | | | | | | |	 f IJ| | | | | f S(   Ni    sb   # L=%9.6f u=%9.6f v=%9.6f w=%9.6f x=%9.6f  y=%9.6f m0=%9.6f m1=%9.6f a1=%9.6f a2=%9.6f dsinf=%9.6f(   RK   R   RS   R   R   (   R   RG   RV   R   R   R   R   R   Rf   R   R$   R   R   R   R   R   R   R   R   R   R   t   succR   R   (    (    s!   /mnt/home/haule/12014haule/oca.pyR     s     /*/c         C   s7  d } d } d } x t  t |   D] } t |  | | |  t | | |  |  } | | c | 9<| | | | | 7} | | | | | 7} | | | | | 7} q% Wt | | | | |	 |  \ } } } } } t | | |  \ } } } |
 d |  | | | | | | | | | |	 f IJ| | | | | f S(   Ni    sb   # L=%9.6f u=%9.6f v=%9.6f w=%9.6f x=%9.6f  y=%9.6f m0=%9.6f m1=%9.6f a1=%9.6f a2=%9.6f dsinf=%9.6f(   RK   R   RS   R   R   (   R   RG   RV   R   R   R   R   R   Rf   R   R$   R   R   R   R   R   R   R   R   R   R   R  R   R   (    (    s!   /mnt/home/haule/12014haule/oca.pyR     s     /*/(   Rx   Rq   R!   R    R'   t   scipyR   R   t   strnsR   t   linalgR   t   environt   getR)   t   objectR   R   RS   Rz   R   R   R   R   R   R   (    (    (    s!   /mnt/home/haule/12014haule/oca.pys   <module>   s"   <
 			P					