ó
Ô™xTc           @   sÚ   d  d l  m Z d  d l Z d  d l m Z d  d l m Z d  d l j	 j
 Z d  d l m Z d  d l Z d „  Z d „  Z e e e d d d „ Z d	 e f d
 „  ƒ  YZ d e f d „  ƒ  YZ d e f d „  ƒ  YZ d S(   iÿÿÿÿ(   t   divisionN(   t   flex(   t   adopt_init_args(   t   xrayc         C   sÑ   x… |  D]} } | j  } xk | j D]` } | j } | j | ƒ } | rc | j j ƒ  j }	 |	 | } n  | j | | | j | ƒ ƒ q  Wq W| j | ƒ }
 | r¯ t j	 |
 ƒ }
 n  t
 | ƒ t
 |
 ƒ k sÍ t ‚ |
 S(   s*  
  Apply NCS constraints to reduce the gradients corresponding to whole ASU, to
  gradients corresponding to one NCS copy.

  Args:
    ncs_restraints_group_list: list of ncs_restraint_group objects
    grad: gradient of the complete asu
    refine_sites: (bool) Flag indicating sites refinement
  (   t   master_iselectiont   copiest
   iselectiont   selectt   rt	   transposet   elemst   set_selectedR   t   vec3_doublet   typet   AssertionError(   t   ncs_restraints_group_listt   extended_ncs_selectiont   gradt   refine_sitest   nrgt   ncs_selectiont   ncs_copyt   asu_selectiont   gt   rtt   g_ncs(    (    s[   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/refinement/minimization_ncs_constraints.pyt   grads_asu_to_one_ncs	   s    		% c         C   sØ   | } t  | t j ƒ r1 t j d g | ƒ } n7 t  | t j ƒ r\ t j d g | ƒ } n t d ƒ ‚ | j | | ƒ xS |  D]K } | j } x9 | j D]. } | j }	 | j	 | ƒ }
 | j |	 |
 ƒ q˜ Wq W| j
 ƒ  S(   sÊ   
  Expand average gradient of a single NCS to all ASU
  (only for u_iso refinement)

  Args:
    ncs_restraints_group_list: list of ncs_restraint_group objects
    ncs_grad: gradient of ta single ncs
  g        s   Non supported grad type(   g        g        g        (   t
   isinstanceR   R   t   doublet	   TypeErrorR   R   R   R   R   t	   as_double(   R   t   total_asu_lengthR   t   ncs_gradt   g_lengthR   R   R   R   R   t   ncs_grad_portion(    (    s[   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/refinement/minimization_ncs_constraints.pyt   grads_one_ncs_to_asu'   s    		c
         C   s˜  | | | g j  t ƒ d k s$ t ‚ d }
 d } | d k	 r| rl | j d | j ƒ  d t ƒ } | j }
 q| r« | j d | d | d | j d |	 d t ƒ } | j }
 q| r| j d | j ƒ  d t ƒ } t	 j
 d | j j ƒ  d	 |  d
 | j ƒ  d | ƒ }
 qn  | d k	 r!| j |
 f S| s+d St | t j ƒ r]d t j d g t | ƒ ƒ f St | t j ƒ r�d t j d t | ƒ d ƒ f Sd Sd S(   s7  
  Args:
    ncs_restraints_group_list: NCS operator list
    restraints_manager: (object)
    xray_structure: (object)
    grad: gradient without restraints
    x: refined parameter
    refine_sites: (bool) indicate refinement type
    refine_u_iso: (bool) indicate refinement type
    refine_transformations: (bool) indicate refinement type
    iso_restraints: (object) iso restraints parameters
    use_hd: (bool) Use hydrogen

  Returns:
    ef_target: (float) restraints target function value
    ef_grad: (flex.double or flex.vec3_double) restraints gradient
  i   t
   sites_cartt   compute_gradientst   xray_structuret
   parameterst   use_u_local_onlyt   use_hdt   grad_wrt_xyzR   t   xyz_asut   xi    i   N(   i    i    (   i    i    i    (   i    i    i    (   NN(   t   countt   TrueR   t   Nonet   energies_sitesR$   t	   gradientst   energies_adp_isoR(   t   nut   compute_transform_gradR   t   targetR   R   R   t   lenR   (   R   t   restraints_managerR&   R   R,   R   t   refine_u_isot   refine_transformationst   iso_restraintsR)   t   ef_gradt   ef(    (    s[   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/refinement/minimization_ncs_constraints.pyt   restraints_target_and_gradsE   sF    $								   !t$   target_function_and_grads_real_spacec           B   s;   e  Z d  Z g  e d d e e d „ Z d „  Z d „  Z RS(   s/   
  Real-space target and gradients evaluator
  c         C   s°   t  |  t ƒ  ƒ t j |  j |  j ƒ |  _ t j d | d | ƒ |  _ |  j j ƒ  |  _ | j	 ƒ  j
 ƒ  } |	 rˆ t j | | ƒ |  _ n$ |
 r¬ t j d | d | ƒ |  _ n  d  S(   NR   t   refine_selectiont   asu_size(   R   t   localsR3   t   get_refine_selectionR?   R&   t   get_extended_ncs_selectionR   t	   unit_cellt
   scattererst   sizeR   t   boolt	   selectiont   get_ncs_related_selection(   t   selft   map_dataR&   R   t   real_space_gradients_deltaR?   t   use_strict_ncsR7   t   data_weightR   R9   R@   (    (    s[   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/refinement/minimization_ncs_constraints.pyt   __init__‡   s    		c         C   sÐ   d
 } t j j j d |  j d |  j d |  j j ƒ  d |  j	 d |  j
 ƒ } | j ƒ  } | rÆ |  j ru | j ƒ  } qÆ |  j rÆ | j ƒ  j ƒ  } t j d | d |  j d |  j j ƒ  d	 | ƒ } qÆ n  | | f S(   sg   
    Args:
      compute_gradients: (bool) when True compute gradients
      x: refined parameters
    RD   t   density_mapR$   RL   RH   R*   R   R+   R,   N(   R/   t   cctbxt   maptbxt   real_space_refinement_simplet   target_and_gradientsRD   RK   R&   R$   RL   RH   R5   R   R1   R9   R   R3   R4   R   (   RJ   R%   R,   R   t   tgt   tR*   (    (    s[   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/refinement/minimization_ncs_constraints.pyt   data_target_and_grads¥   s&    							c   
      C   s#  |  j  j d | j ƒ  ƒ d } |  j | | ƒ \ } } t d |  j d |  j  d |  j d |  j d |  j	 d | d | ƒ \ } } |  j
 d k rš d	 |  _
 n  | |  j
 | }	 | r| |  j
 | } |  j	 s|  j rt d |  j d
 |  j d | d |  j ƒ j ƒ  } q| j ƒ  } qn  |	 | f S(   s“   
    Args:
      compute_gradients: (bool) when True compute gradients
      xray_structure: xray_structure object
      x: refined parameters
    R$   R7   R&   R   R   R9   R,   R   g      ð?R   N(   R&   t   set_sites_cartR$   R/   RW   R=   R7   R   R   R9   RN   RM   R   R   R   (
   RJ   R%   R&   R,   R   t   t_datat   g_datat   t_restraintst   g_restraintsRV   (    (    s[   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/refinement/minimization_ncs_constraints.pyRT   ¿   s2    					 				N(	   t   __name__t
   __module__t   __doc__R.   R/   t   FalseRO   RW   RT   (    (    (    s[   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/refinement/minimization_ncs_constraints.pyR>   ƒ   s   	t*   target_function_and_grads_reciprocal_spacec        
   B   sM   e  Z d  Z g  e d d e e e d e d „	 Z d „  Z d „  Z d „  Z	 RS(   sª   
  Reciprocal-space target and gradients evaluator

  Args:
    refine_selection (flex.size_t): of all ncs related copies and
      non ncs related parts to be refined
  c         C   s  t  |  t ƒ  ƒ t j |  j |  j j ƒ |  _ t j d | d |  j ƒ |  _ |  j j j	 ƒ  j
 d t ƒ |  j j ƒ  |  _ |  j j |  _ |  j r· t j d |  j j j	 ƒ  d t ƒ n\ |  j rå t j d |  j j j	 ƒ  d t ƒ n. |  j rt j d |  j j j	 ƒ  d t ƒ n  d  S(   NR   R?   t   stateRE   t   sitet   u_iso(   R   RA   R3   RB   R?   t   fmodelR&   RC   R   RE   t   flags_set_gradsR`   t   target_functort   x_target_functorR   R   t   set_scatterer_grad_flagsR.   R8   R9   (   RJ   Re   R   R?   RM   R7   RN   R   R8   R9   R:   R)   (    (    s[   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/refinement/minimization_ncs_constraints.pyRO   è   s*    			
		
		c      	   C   sÍ   d  } |  j d | ƒ } | j ƒ  } | rÃ |  j rW t j | j d t ƒ j ƒ  ƒ } qÃ |  j	 ru | j d t ƒ } qÃ |  j
 rÃ | j d t ƒ j ƒ  } t j d | d |  j d | d | ƒ } qÃ n  | | f S(   NR%   Rc   Rd   R*   R   R+   R,   (   R/   Rh   t   target_workR   R   R   t   gradients_wrt_atomic_parametersR.   t   packedR8   R9   R3   R4   R   (   RJ   R%   R$   R,   R   t   tgxRV   R*   (    (    s[   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/refinement/minimization_ncs_constraints.pyRW     s"    					c         C   sl  | j  ƒ  } |  j j d | ƒ |  j j d |  j d t ƒ d  } |  j d | d | d | ƒ \ } } t d |  j	 d |  j d |  j
 d |  j d	 |  j d
 |  j d | d |  j d |  j d | ƒ 
\ } }	 |  j d  k rã d |  _ n  | |  j | }
 | rb| |  j |	 } |  j sb|  j rPt d |  j
 d |  j d | d |  j ƒ j ƒ  } q_| j ƒ  } qbn  |
 | f S(   NR$   R&   t   update_f_calcR%   R,   R7   R   R   R8   R9   R:   R)   R   g      ð?R   (   R$   R&   RX   Re   t   update_xray_structureR.   R/   RW   R=   R7   R   R   R8   R9   R:   R)   RN   RM   R   R   R   (   RJ   R%   R&   R,   R$   R   RY   RZ   R[   R\   RV   (    (    s[   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/refinement/minimization_ncs_constraints.pyRT     sB    										 				c         C   sI   |  j  j j ƒ  |  j  j j ƒ  |  j  j d |  j  j d t d t ƒ d  S(   NR&   Rn   t   update_f_mask(   Re   R&   t   tidy_ust   apply_symmetry_sitesRo   R.   (   RJ   (    (    s[   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/refinement/minimization_ncs_constraints.pyt   finalizeA  s    N(
   R]   R^   R_   R.   R/   R`   RO   RW   RT   Rs   (    (    (    s[   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/refinement/minimization_ncs_constraints.pyRa   à   s   		#t   lbfgsc           B   sS   e  Z g  e d  d e e e d „ Z e d „ Z d d „ Z d d „ Z d „  Z	 RS(   i    i#   c         C   sÎ  t  |  d t ƒ  d d g ƒt j |  j |  j ƒ |  _ | j |  _ t j | ƒ |  _ g  |  _	 t j
 d |  j d |  j ƒ |  _ |  j |  j |  j g j t ƒ d k s¬ t ‚ t | j ƒ  ƒ |  _ d } |  j rå | j |  j ƒ } n | j |  j ƒ } |  j r| j ƒ  j ƒ  |  _ n® |  j r`| j ƒ  j ƒ  | j ƒ  j t ƒ k sNt ‚ | j ƒ  |  _ nf |  j rÆt j d |  j d |  j ƒ |  _	 t j d |  j	 d |  j ƒ |  _ t j d |  j ƒ |  _ n  t j  j! d	 |  d
 t j  j" d | d | ƒ d t j  j# d t d t d t ƒ ƒ } |  j rDt j$ d |  j	 d |  j ƒ |  _ n  t% |  j& d d ƒ ri|  j& j( ƒ  n  x^ t) | |  j ƒ D]J \ } } x; t) | j* | j* ƒ D]$ \ } } | j+ | _+ | j, | _, qžWq|Wd S(   sU   
    NCS constrained ADP and coordinates refinement. Also refines NCS operators.
    t   argst   excludeR   R?   i   g�íµ ÷Æ°>R&   t   shiftst   target_evaluatort   termination_paramst   max_iterationst    traditional_convergence_test_epst   exception_handling_paramst)   ignore_line_search_failed_rounding_errorst-   ignore_line_search_failed_step_at_lower_boundt    ignore_line_search_failed_maxfevRs   N(-   R   RA   R3   RB   R?   R&   RM   t   ncs_restraints_group_list_copyR   t   ncs_groups_coordinates_centersRC   R   R   R8   R9   R-   R.   R   R6   R$   R   R   R   R,   RE   RF   t	   use_u_isot   extract_u_iso_or_u_equivt   get_ncs_gorups_centerst   shift_translation_to_centert   concatenate_rot_trant   scitbxRt   t   runt   termination_parameterst   exception_handling_parameterst   shift_translation_back_to_placet   getattrt   target_and_grads_objectR/   Rs   t   zipR   R   RV   (   RJ   R   R�   R&   R?   t   finite_grad_differences_testt   finite_grad_difference_valRz   R   R8   R9   R{   t   xray_structure_one_ncs_copyt	   minimizert   g1t   g2t   tr1t   tr2(    (    s[   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/refinement/minimization_ncs_constraints.pyRO   J  sh    			'													"c         C   sY   |  j  j d | d |  j ƒ  d |  j ƒ \ } } |  j rO | rO |  j | ƒ n  | | f S(   NR%   R&   R,   (   R�   RT   Ro   R,   R�   t   finite_difference_test(   RJ   R%   RV   R   (    (    s[   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/refinement/minimization_ncs_constraints.pyt    compute_functional_and_gradients‘  s    c         C   så   | s |  j  } n  |  j r| t j d | d |  j ƒ |  _ t j |  ƒ j ƒ  } |  j | ƒ } |  j j	 d t
 j | ƒ ƒ nb |  j r³ |  j | ƒ } |  j j	 d t
 j | ƒ ƒ n+ |  j rÞ |  j ƒ  } |  j j d | ƒ n  |  j S(   ss   
    Update xray_structure with refined parameters, then update
    fmodel object with updated xray_structure.
    R,   R   R$   t   values(   R,   R9   R3   t   update_rot_tranR   t   get_ncs_sites_cartR   t   refinable_params_one_ncs_to_asuR&   RX   R   R   R   R8   t	   set_u_iso(   RJ   R,   t   x_ncst   x_asu(    (    s[   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/refinement/minimization_ncs_constraints.pyRo   š  s"     				c         C   s!  |  j  j ƒ  } | s! |  j } n  |  j s3 |  j rÊ |  j sE |  j r· t j d t j	 | ƒ d |  j
 d | j ƒ  d |  j d t d |  j ƒ } | j |  j ƒ } | j |  j | ƒ } n	 |  j } | j ƒ  S|  j r|  j rt d |  j
 d |  j d | j ƒ  d | ƒ St j t | ƒ ƒ Sn  d S(	   sr   
    Expand refinabale parameters corresponding to one NCS copy to parameters
    corresponding to whole ASU.
    t   ncs_coordinatesR   R   R   t   round_coordinatest   center_of_coordinatesR    N(   R&   R$   R,   R   R9   RM   R3   t   apply_transformsR   R   R   RF   R   R`   R�   R   R?   R   R   R8   R#   R   t   list(   RJ   R,   t   x_oldt   new_x(    (    s[   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/refinement/minimization_ncs_constraints.pyRœ   ²  s0     				
				c         C   sñ   | j  ƒ  } t j t j | ƒ ƒ } t |  j | d d ƒ } |  j | | |  j | <|  j d t ƒ \ } } |  j | d | |  j | <|  j d t ƒ \ } } |  j | | |  j | <|  j ƒ  | | | d } t | | | ƒ |  _	 d S(   s…   
    Compare analytical and finite differences gradients.

    finite_grad_difference_val = abs(analytical - finite differences)
    g�íµ ÷Æ°>R%   i   N(
   R   R   t	   max_indext   abst   maxR,   R˜   R`   Ro   R�   (   RJ   R   t   i_g_maxt   dt   t1t   _t   t2t   finite_gard(    (    s[   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/refinement/minimization_ncs_constraints.pyR—   Ñ  s    
N(
   R]   R^   R`   RO   R.   R˜   R/   Ro   Rœ   R—   (    (    (    s[   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/refinement/minimization_ncs_constraints.pyRt   I  s   =	(   t
   __future__R    t)   cctbx.maptbx.real_space_refinement_simpleRQ   t   scitbx.array_familyR   t   libtbxR   t   mmtbx.ncs.ncs_utilst   ncst	   ncs_utilsR3   R   t   scitbx.lbfgsR‡   R   R#   R`   R/   R=   t   objectR>   Ra   Rt   (    (    (    s[   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/refinement/minimization_ncs_constraints.pyt   <module>   s    		$4]i