ó
0™xTc           @   sÉ   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	 Z
 d  d l Z
 d  d l m Z d  d l m Z d d d d d „ Z d	 e f d
 „  ƒ  YZ d e f d „  ƒ  YZ d S(   iÿÿÿÿ(   t   divisionN(   t   ext(   t   crystal(   t   flex(   t   adopt_init_args(   t   mathc         C   s2   t  j d |  d | d | d | d | d | ƒ d  S(   Nt
   scattererst   xray_gradientst   site_gradientst   u_iso_gradientst   u_aniso_gradientst   occupancy_gradients(   R   t   minimization_add_gradients(   R   R   R   R	   R
   R   (    (    sE   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/cctbx/xray/minimization.pyt   add_gradients   s    t   occupancy_penalty_expc           B   s,   e  Z d  d d d „ Z d „  Z d „  Z RS(   i   id   g»½×Ùß|Û=c         C   s/   t  |  t ƒ  ƒ t j | ƒ | | |  _ d  S(   N(   R   t   localsR   t   logt   occupancy_max(   t   selft   penalty_factort   penalty_scalet   min_functional(    (    sE   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/cctbx/xray/minimization.pyt   __init__   s    c         C   sX   | |  j  k r d S| d k  r6 d t j | ƒ } n  |  j } t j | | |  j ƒ S(   Ni    iÿÿÿÿ(   R   R   R   R   t   expR   (   R   t	   occupancyt   s(    (    sE   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/cctbx/xray/minimization.pyt
   functional#   s      	c         C   sd   | |  j  k r d S| d k  r6 d t j | ƒ } n  |  j } | |  j t j | | |  j ƒ S(   Ni    iÿÿÿÿ(   R   R   R   R   R   R   (   R   R   R   (    (    sE   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/cctbx/xray/minimization.pyt   gradient)   s      	(   t   __name__t
   __module__R   R   R   (    (    (    sE   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/cctbx/xray/minimization.pyR      s
   	t   lbfgsc           B   sP   e  Z d  Z d d d d e d d d „ Z d „  Z d „  Z d „  Z d „  Z	 RS(	   s‘  
  A minimisation of a function of the free parameters of a X-ray structure,
  relying on the LBFGS algorithm.

  That function is represented by a functor object f which should abide to the
  following interface
    - B{f.f_obs()}
    - B{f(f_calc, compute_derivatives)}
  in the manner of `cctbx.xray.target_functors.least_squares_residual`
  or `cctbx.xray.target_functors.intensity_correlation`
  g{®Gáz„?i    c
   
      C   sU  t  |  t ƒ  ƒ t j |  j j ƒ  ƒ |  _ t j |  j j ƒ  ƒ |  _ t j j	 j
 d |  j j ƒ  d | ƒ |  _ t j j	 j d |  j j ƒ  d | ƒ |  _ t j | j ƒ  d ƒ |  _ | j d d ƒ | j ƒ  |  _ |  j j ƒ  j ƒ  |  _ d
 |  _ t j j d |  d | d | ƒ |  _ |  j ƒ  |  ` |  ` |  j d	 t  ƒ |  j! j" ƒ  |  _# d
 S(   sü  
    :Parameters:

      target_functor : any object like f
        the numerical function of the structure to minimise
      xray_structure : `cctbx.xray.structure`
        the structure the target_functor is calculated for
      occupancy_penalty
        TODO
      lbfgs_termination_params : like `scitbx.lbfgs.termination_parameters`
        the bunch of parameters used to decide where to stop the minimisation
      lbfgs_core_params : like `scitbx.lbfgs.core_parameters`
        the bunch of parameters for the numerical core of the lbfgs algorithm
      correct_special_position_tolerance : number
        the threshold for the distance to a special position below which
        an atom is snapped onto it
      cos_sin_table : bool
        whether to use tabulated cosines and sines in structure factor
        computations
      structure_factor_algorithm : string
        the name of the method to be used to compute structure factors;
        it must be one of those provided by the factory
        `cctbx.xray.structure_factors.from_scatterers`
      verbose : int
        a flag specifying the verbosity of the log printed out during the
        minimisation process; 0 means silent whereas positive numbers
        print an increasing amount of information
    t
   miller_sett   cos_sin_tablei    t   u_ming�íµ ÷Æ°>t   target_evaluatort   termination_paramst   core_paramst   compute_gradientsN($   R   R   R   t   scatterer_grad_flags_countst   xray_structureR   t   grad_flags_countst   cctbxt   xrayt   structure_factorst   from_scattererst   target_functort   f_obst!   structure_factors_from_scattererst	   gradientst   structure_factor_gradientsR   t   doublet   n_parameterst   xt   tidy_ust   _scatterers_startt   d_mint   _d_mint   Nonet   first_target_valuet   scitbxR   t   runt	   minimizert   apply_shiftst   compute_targett   Falset   target_resultt   targett   final_target_value(
   R   R-   R'   t   occupancy_penaltyt   lbfgs_termination_paramst   lbfgs_core_paramst"   correct_special_position_toleranceR    t   structure_factor_algorithmt   verbose(    (    sE   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/cctbx/xray/minimization.pyR   <   s0    $	
c      
   C   s½   t  j d |  j j ƒ  d |  j d |  j ƒ } | j } |  j j ƒ  } x[ | j ƒ  D]M } t	 j
 d |  j d | j | ƒ j ƒ  d | | j d |  j ƒ | | _ qR W|  j j d | ƒ | j S(   Nt	   unit_cellR   t   shiftst   crystal_symmetryt
   special_opt	   site_fract	   tolerance(   R   t   minimization_apply_shiftsR'   RJ   R6   R4   t   shifted_scattererst   site_symmetry_tablet   special_position_indicesR   t   correct_special_positiont   getRM   t   siteRG   t   replace_scattererst   u_iso_refinable_params(   R   t   apply_shifts_resultRQ   RR   t   i_seq(    (    sE   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/cctbx/xray/minimization.pyR>   |   s    					c         C   sm   |  j  d |  j d |  j j ƒ  d |  j ƒ j ƒ  |  _ |  j |  j | ƒ |  _ |  j j ƒ  d  k	 si t	 ‚ d  S(   NR'   R   t	   algorithm(
   R/   R'   R-   R.   RH   t   f_calcRA   RB   R9   t   AssertionError(   R   R%   (    (    sE   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/cctbx/xray/minimization.pyR?   Œ   s    		c         C   s×  |  j  ƒ  } |  j d t ƒ |  j j ƒ  |  _ |  j d  k rL |  j |  _ n  |  j d  k	 r± |  j	 d k r± |  j
 j ƒ  j ƒ  } x/ | D]$ } |  j |  j j d | ƒ 7_ q† Wn  |  j d |  j
 d | d |  j j ƒ  d |  j j ƒ  d |  j j ƒ  d	 |  j ƒ j ƒ  |  _ |  j d  k	 r�|  j	 d k r�t j ƒ  } x* | D]" } | j |  j j d | ƒ ƒ q<W~ t d
 |  j
 j ƒ  d |  j d | ƒ ~ n  |  j d k rÇd G|  j Gt j t j |  j ƒ ƒ GHn  |  j |  j f S(   NR%   i    R   R'   RX   R   t   d_target_d_f_calcR3   R[   R   R   R   i   s(   xray.minimization line search: f,rms(g):(    R>   R?   t   TrueRA   RB   t   fR:   R9   RD   R(   R'   R   t   extract_occupanciesR   R1   R-   R.   t   derivativesR4   t   sizeRH   t   packedt   gR   R2   t   appendR   R   RI   R   t   sqrtt   mean_sq(   R   RX   t   occupanciesR   Re   (    (    sE   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/cctbx/xray/minimization.pyt    compute_functional_and_gradients–   s@    %		 	$c         C   s6   |  j  d k r2 d G|  j G| j ƒ  G| j ƒ  GHn  d  S(   Ni    s$   xray.minimization step: f,iter,nfun:(   RI   R`   t   itert   nfun(   R   R=   (    (    sE   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/cctbx/xray/minimization.pyt   callback_after_step¸   s    N(
   R   R   t   __doc__R9   R_   R   R>   R?   Rj   Rm   (    (    (    sE   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/cctbx/xray/minimization.pyR   /   s   9		
	"(   t
   __future__R    t   cctbx.xray.structure_factorsR)   t
   cctbx.xrayR   R   t   cctbx.array_familyR   t   scitbx.lbfgsR;   t   scitbx.matht   libtbxR   t   stdlibR   R9   R   t   objectR   R   (    (    (    sE   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/cctbx/xray/minimization.pyt   <module>   s   