ó
ŌxTc           @   sč   d  d l  m Z d  d l m Z d  d l m Z d  d l m Z d  d l Z d  d l	 m
 Z
 d  d l m 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 e f d     YZ d e f d     YZ d S(   i’’’’(   t   division(   t   xray(   t   crystal(   t   flexN(   t   adopt_init_args(   t   math(   t   user_plus_sys_time(   t   adptbx(   t   format_value(   t   Sorryt   lbfgsc           B   s}   e  Z d	 d	 d	 d	 d	 e e d	 e d  d d	 d	 d	 d	 e j d  e j d  e d  Z d   Z d   Z	 d   Z
 d   Z RS(
   i    g      š?g      ĄgRøė?@c         C   s[  t    } t |  t    d  |  _ |  j j   j |  _ |  j j   |  j j	   |  j
 r | j   d  k r |  j j   |  j j d |  j d t  n  d  |  _ | |  _ |  j d  k	 rģ |  j j rģ | |  _ d |  _ d  |  _ d  |  _ n  | |  _ | r| d  k	 r| j |  _ np | r7| d  k	 r7| j |  _ nO d d l m } | j d d d d d	 d  d
 d d d d d  d d d d  |  _ |  j rĖt d |  j d | d | d | d | d | d t  |  _ n  |  j rä|  j j   n  |  j j d  k	 |  _  t! j" |  j j#   d  |  _$ |  j j%   |  _& t' j( j) d |  d |	 d |
 d t' j( j* d t   |  _+ |  j,   |  `& |  j- d t d d   |  j
 rŅ|  j j   d  k rŅ|  j j   |  j j d |  j d t  n  |  j r	|  j j d |  j+ j.   d |  j+ j/    n  |  j j   |  j d  k	 rW|  j j rW|  j j0 d j1 d   IJ|  j j0 Jn  d  S(!   Nt   xray_structuret   update_f_calci    i’’’’(   t   weight_xray_chemt   wxi   t   wx_scalet   angle_xt   wnt   wn_scalet   angle_nt   wt   wxnt   weightst   fmodelst   modelt   iso_restraintst
   refine_xyzt
   refine_adpt
   refine_occt   target_evaluatort   termination_paramst   use_fortrant   exception_handling_paramst-   ignore_line_search_failed_step_at_lower_boundt   compute_gradientst   u_iso_refinable_paramst   itert   nfuns   {:-^80}t    (2   R   R   t   localst   Nonet   fR   t   fmodel_xrayR   t   create_target_functorst(   prepare_target_functors_for_minimizationR   t   fmodel_neutront   tidy_ust   update_xray_structuret   TrueR   t   qblib_paramst   qblibt   macrot   qblib_cycle_countt   tmp_XYZt   XYZ_diff_currt"   correct_special_position_tolerancet   xyz_weights_resultt   adp_weights_resultt   phenix.refinementR   t   collect_monitort   monitort   Falset   collectt   fmodel_nt   neutron_refinementR   t   doublet   n_parameterst   xt
   scattererst   _scatterers_startt   scitbxR
   t   runt   exception_handling_parameterst	   minimizert   apply_shiftst   compute_targetR$   R%   t	   qblib_logt   format(   t   selfR   t   restraints_managerR   t   is_neutron_scat_tablet   target_weightst   tan_b_iso_maxR   R   t   lbfgs_termination_paramsR   t   verboseR7   R   t   h_paramsR1   t   macro_cyclet   u_mint   u_maxR;   t   timerR   (    (    sK   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/refinement/minimization.pyt   __init__   s    			
									 
	
	c         C   s  |  j  r4 t j j d |  j d |  j d |  j  n  t j j d |  j j	   d |  j
 d |  j  } | j } |  j ró |  j j   } xk | j   D]Z } t j d |  j d | j |  j   d | | j d	 | | j d
 |  j  | | _ q Wn  |  j j d |  |  j  r| j Sd  Sd  S(   Nt   shiftst	   min_valuet	   max_valuet	   unit_cellRD   t   crystal_symmetryt
   special_opt	   site_fract
   site_labelt	   tolerance(   R   R   t   extt   truncate_shiftsRC   RW   RX   t   minimization_apply_shiftsR   R^   RE   t   shifted_scatterersR   t   site_symmetry_tablet   special_position_indicesR   t   correct_special_positiont   getR`   t   sitet   labelR7   t   replace_scatterersR#   R(   (   RN   t   apply_shifts_resultt   scatterers_shiftedRh   t   i_seq(    (    sK   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/refinement/minimization.pyRJ   k   s.    									c         C   s  d  |  _ |  j j d |  j d t  |  j j d |  j d | d |  } | j   |  _	 | j
   |  _ |  j r]|  j d  k	 r]|  j j d k r]|  j j d |  |  _ |  j d  k	 r{|  j j r{d d l m } |  j d	 7_ |  j d  k	 rm| j |  j |  j j j    \ } } } |  j d  k	 r7| |  _ n	 | |  _ | |  j j k r|  j j j   |  _ qn |  j j j   |  _ |  j |  j j k r{| j d
 |  j j   d |  j j d |  j d |  j d |  j d |  j d |  j  } y | j   Wn= t  k
 r8}	 |	 j! j" d k r/t# |	 j$   q9|	  n X| j% rx| j& j
 }
 | j& j } |
 |  j _
 | |  j _ qxq{n  |  j j } |  j	 | |  j j 7_	 | r]|  j j
 } |  j d  k	 r"|  j j' d  k	 r"|  j j' j( t  d k r"| j) t* j+ |  j j' j( t  d d d g   n  t, j- j. d |  j j/   d |  j d | |  j j  q]n  |  j0 r|  j d  k	 r|  j j1 d  k	 r|  j j d k r|  j2 d  k	 rt3 } |  j j4 d  k	 sš|  j5 d  k	 rŽ|  j5 d k sš|  j6 j7 d k rłt } n  |  j j8 d |  j2 d | d |  } |  j	 | j |  j j 7_	 | r| j9 d  k rt, j- j. d |  j j/   d |  j d | j: |  j j  q| j9 |  j j 9_9 | j: d  k	 rÅ| j: |  j j 9_: n  t, j- j. d |  j j/   d |  j d | j9 d | j:  d  | _9 d  | _: qn  d  S(   NR   R   R   R"   R#   g        i’’’’(   t   qb_refinementi   t	   hierarchyt   geometry_residualsR1   t	   diff_currR3   t   microt   QB_none_fatal_errori    RD   t   xray_gradientst   site_gradientst   neutront
   individualR   t   use_hdt   u_iso_gradientst   u_aniso_gradients(;   R(   t   stereochemistry_residualsR   R/   R   R0   t   target_and_gradientsR   t   targetR)   t	   gradientst   gR   RO   R   R   t!   restraints_manager_energies_sitesR1   R2   t   qbpyRr   R4   R5   t   array_differencet
   sites_cartR6   t   skip_divcon_thresholdRV   t   macro_cycle_to_skipt   QBblib_call_managert   pdb_hierarchyRG   t	   Exceptiont	   __class__t   __name__R	   t   messaget   QBStatust   result_QBlibt   ias_selectiont   countt   extendR   t   vec3_doubleR   t   minimizationt   add_gradientsRD   R   t   geometryR   R=   R?   RP   RU   t   refinet   energies_adpR~   R}   (   RN   R"   R#   t   fmodels_target_and_gradientsRr   t   difft   max_difft   max_elemt
   qblib_callt   et   qblib_gt   qblib_ft   ert   sgcR|   R   (    (    sK   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/refinement/minimization.pyRK      s²    										
	!(	!						
	c         C   s6   |  j  d k r2 d G|  j G| j   G| j   GHn  d  S(   Ni    s*   refinement.minimization step: f,iter,nfun:(   RT   R)   R$   R%   (   RN   RI   (    (    sK   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/refinement/minimization.pyt   callback_after_stepė   s    c         C   si   |  j    } |  j d t d |  |  j d k rY d G|  j Gt j t j |  j	   GHn  |  j |  j	 f S(   NR"   R#   i   s(   xray.minimization line search: f,rms(g):(
   RJ   RK   R0   RT   R)   R   t   sqrtR   t   mean_sqR   (   RN   R#   (    (    sK   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/refinement/minimization.pyt    compute_functional_and_gradientsš   s    $N(   R   t
   __module__R(   R=   R   t   b_as_uR0   RZ   RJ   RK   R„   RØ   (    (    (    sK   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/refinement/minimization.pyR
      s,   I		f	R<   c           B   sJ   e  Z e e e d   Z d d d  Z d d d  Z d   Z d   Z RS(   c         C   sn   t  |  t    g  |  _ g  |  _ g  |  _ g  |  _ g  |  _ g  |  _ g  |  _ g  |  _	 d  |  _ d  |  _ d  S(   N(   R   R'   t   ext   enR£   t   ett   rxwt   rxft   rnwt   rnfR(   R$   R%   (   RN   R   R   R   R   R   R   R   (    (    sK   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/refinement/minimization.pyRZ   ś   s    									c         C   sj  | d  k	 r! t d |  |  _ n  | d  k	 rB t d |  |  _ n  |  j j   j |  j j k |  j j d |  j	 d t
  } |  j j t d |  j j   j     |  j j t d |  j j   j     |  j j t d | j   |  j j   d  k	 rl|  j j t d |  j j   j     |  j j t d |  j j   j     |  j j t d | j   n  |  j r¢|  j	 j d k r¢|  j j d t
  j } n~ |  j r|  j	 j d k rt
 } |  j j d  k	 rŽt } n  |  j j d |  j d | d t
  j } n |  j  rd } n d } |  j! j t d |   |  j" j t d | j   | |  j	 j   d  S(	   Ns   %-4dR   R"   s   %6.4fs   %10.4fi    R   R|   (#   R(   R   R$   R%   R   R*   R   R   R   R   R=   R®   t   appendt   r_workRÆ   t   r_freeR«   t   target_work_xrayR-   R°   R±   R¬   t   target_work_neutronR   R   R   R   R   R?   R0   R   R   R   R£   R­   (   RN   R$   R%   t
   fmodels_tgR£   R|   (    (    sK   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/refinement/minimization.pyR>     s@      		(((( 				R&   c         C   sø  | d  k r t j } n  | d | d d t d | d  d IJ|  j j   d  k	 r~ | d d d d d d d IJn  | d	 |  j d
 |  j d f IJ| d |  j d
 |  j d f IJ|  j j   d  k	 r@| d d d d d d d IJ| d	 |  j d
 |  j d f IJ| d |  j	 d
 |  j	 d f IJn  | d d d d IJ|  j j   d  k raxó t
 d d g  D]Ü \ } } |  j rĆ| d | d d d IJ| |  j |  IJnr |  j rü| d | d d d IJ| |  j |  IJn9 |  j r5| d | d d d IJ| |  j |  IJn  | d
 k r~| d d d d IJq~q~Wn  |  j j   d  k	 rdxė t
 d d g  D]Ō \ } } |  j rŹ| d | d d IJ| |  j |  IJnn |  j r’| d | d d IJ| |  j |  IJn9 |  j r8| d | d d d IJ| |  j |  IJn  | d
 k r| d d d d IJqqWn  | d d d d IJ| d |  j |  j f IJ| d d d d IJ| j   d  S(    Ns   |-t   -iO   s   | t   |t    i!   s   x-ray data:sG   | start r-factor (work) = %s      final r-factor (work) = %s          |i    i   sG   | start r-factor (free) = %s      final r-factor (free) = %s          |i    s   neutron data:iM   t   startt   finals-   | T_%s = wxc * wxc_scale * Exray + wc * Echemi   s,   | T_%s = wxu * wxu_scale * Exray + wu * Eadpi   s   | T_%s = 1.0 * 1.0 * Exrayi+   sG   | T_%s = wnxc * (wxc_scale * Exray + wnc_scale * Eneutron) + wc * Echemi   sF   | T_%s = wnxu * (wxu_scale * Exray + wnu_scale * Eneutron) + wu * Eadpi   s-   | T_%s = 1.0 * (1.0 * Exray + 1.0 * Eneutron)sK   | number of iterations = %s    |    number of function evaluations = %s   |s       s        (   R(   t   syst   stdoutt   lenR   R-   R®   RÆ   R°   R±   t	   enumerateR   t	   xray_lineR   R   t   neutron_lineR$   R%   t   flush(   RN   R   t   logRq   t   stage(    (    sK   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/refinement/minimization.pyt   show,  s^     ,%"			  			  c      
   C   sŹ   d |  j  | j   t d |  j j  j   t d |  j j  j   |  j | j   t d |  j j  j   |  j | j   t d |  j j	  j   |  j
 | j   f } | d d t |  d } | S(   Ns)   | %s = %s * (%s * %s + %s * %s) + %s * %ss   %6.2fRŗ   iN   R¹   (   R­   t   stripR   R   R   R   R«   R   R¬   R   R£   Ræ   (   RN   Rq   t   line(    (    sK   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/refinement/minimization.pyRĀ   ]  s    c         C   s¢   d |  j  | j   t d |  j j  j   t d |  j j  j   |  j | j   t d |  j j  j   |  j | j   f } | d d t	 |  d } | S(   Ns   | %s = %s * %s * %s + %s * %ss   %6.2fRŗ   iN   R¹   (
   R­   RĒ   R   R   R   R   R«   R   R£   Ræ   (   RN   Rq   RČ   (    (    sK   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/refinement/minimization.pyRĮ   j  s    N(	   R   R©   R=   RZ   R(   R>   RĘ   RĀ   RĮ   (    (    (    sK   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/refinement/minimization.pyR<   ł   s   1	(   t
   __future__R    t   cctbxR   R   t   cctbx.array_familyR   t   scitbx.lbfgsRF   t   libtbxR   t   stdlibR   R½   t   libtbx.utilsR   R   t   libtbx.str_utilsR   R	   t   objectR
   R<   (    (    (    sK   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/refinement/minimization.pyt   <module>   s   ė