ó
;™xTc           @   s¸   d  d l  m Z d j ƒ  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 „  Z d d	 „ Z d
 d d „  ƒ  YZ d „  Z d „  Z e d k r´ e ƒ  n  d S(   iÿÿÿÿ(   t   divisions¨    8.81   0  1  1
 12.23   0  0  2
 12.71   0  2  0
 12.97   1  1  0
 13.79   0  1  2
 14.11   0  2  1
 14.35   1  1  1
 16.68   1  0  2
 17.03   1  2  0
 17.67   0  2  2
 17.86   1  1  2
 19.47   0  1  3
 21.03   1  2  2
 22.26   1  3  0
 22.41   0  2  3
 22.56   1  1  3
 22.72   2  0  0
 23.10   1  3  1
 24.40   2  1  1
 24.60   0  0  4
 25.17   1  2  3
 25.43   0  1  4
 25.87   2  0  2
 26.11   2  2  0
 26.32   0  4  1
 26.66   2  1  2
 26.84   2  2  1
 27.15   1  0  4
 27.78   0  2  4
 27.90   1  1  4
 28.44   0  4  2
 28.72   1  4  1
 28.92   2  2  2
 29.02   1  3  3
 30.08   2  1  3
 30.49   2  3  1
 30.69   1  4  2
 31.34   0  3  4
 31.56   0  1  5
 32.12   2  2  3
(   t   uctbx(   t   wavelengths(   t   flexNc         C   s2   | j  | | d t ƒ} t j t j |  | ƒ ƒ S(   Nt   deg(   t	   two_thetat   TrueR   t   sumt   pow2(   t   two_thetas_obst   miller_indicest
   wavelengtht	   unit_cellt   two_thetas_calc(    (    sQ   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/cctbx/examples/unit_cell_refinement.pyt   residual3   s    g�íµ ÷Æ°>c   
   	   C   s­   t  j ƒ  } xš t d ƒ D]Œ } g  } x\ | | g D]M } t | j ƒ  ƒ }	 |	 | c | 7<| j t |  | | t j |	 ƒ ƒ ƒ q3 W| j | d | d d | ƒ q W| S(   Ni   i    i   i   (	   R   t   doublet   xranget   listt
   parameterst   appendR   R   R   (
   R	   R
   R   R   t   epst   resultt   it   rst
   signed_epst
   params_eps(    (    sQ   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/cctbx/examples/unit_cell_refinement.pyt	   gradients8   s    	%t   refineryc           B   s,   e  Z d  „  Z d „  Z d „  Z d „  Z RS(   c         C   sJ   | |  _  | |  _ | |  _ t j | j ƒ  ƒ |  _ t j j	 d |  ƒ d  S(   Nt   target_evaluator(
   R	   R
   R   R   R   R   t   xt   scitbxt   lbfgst   run(   t   selfR	   R
   R   R   (    (    sQ   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/cctbx/examples/unit_cell_refinement.pyt   __init__I   s
    			c         C   s   t  j t |  j ƒ ƒ S(   N(   R   R   t   iterR   (   R!   (    (    sQ   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/cctbx/examples/unit_cell_refinement.pyR   P   s    c         C   si   |  j  ƒ  } t |  j |  j |  j | ƒ } t |  j |  j |  j | ƒ } d | Gd | j ƒ  GH| | f S(   Ns   functional: %12.6gs   gradient norm: %12.6g(   R   R   R	   R
   R   R   t   norm(   R!   R   t   ft   g(    (    sQ   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/cctbx/examples/unit_cell_refinement.pyt    compute_functional_and_gradientsS   s    c         C   s	   d GHd  S(   Ns
   LBFGS step(    (   R!   t	   minimizer(    (    sQ   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/cctbx/examples/unit_cell_refinement.pyt   callback_after_step\   s    (   t   __name__t
   __module__R"   R   R'   R)   (    (    (    sQ   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/cctbx/examples/unit_cell_refinement.pyR   G   s   				c         C   sa   | j  | | d t ƒ} xA t | |  | ƒ D]- \ } } } d | Gd | | | | f GHq+ WHd  S(   NR   s   (%2d, %2d, %2d)s   %6.2f - %6.2f = %6.2f(   R   R   t   zip(   R	   R
   R   R   R   t   ht   ot   c(    (    sQ   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/cctbx/examples/unit_cell_refinement.pyt   show_fit_   s    ""c          C   s  t  j ƒ  }  t  j ƒ  } xs t D]k } | j ƒ  } t | ƒ d k sI t ‚ |  j t | d ƒ ƒ | j g  | d D] } t	 | ƒ ^ qq ƒ q Wt
 j d ƒ j ƒ  } t j d ƒ } t |  | | | ƒ t |  | | | ƒ } Ht |  | | | j ƒ  ƒ | j ƒ  GHHt j j ƒ  GHd  S(   Ni   i    i   t   CUi
   iZ   (   i
   i
   i
   iZ   iZ   iZ   (   R   R   t   miller_indext   two_theta_and_index_listt   splitt   lent   AssertionErrorR   t   floatt   intR   t   characteristict   as_angstromR   R   R0   R   t   libtbxt   utilst   format_cpu_times(   R	   R
   t   linet   fieldst   sR   t   unit_cell_startt   refined(    (    sQ   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/cctbx/examples/unit_cell_refinement.pyR    e   s&    .t   __main__(    (   t
   __future__R    t
   splitlinesR3   t   cctbxR   t   cctbx.eltbxR   t   cctbx.array_familyR   t   scitbx.lbfgsR   t   libtbx.utilsR;   R   R   R   R0   R    R*   (    (    (    sQ   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/cctbx/examples/unit_cell_refinement.pyt   <module>   s   *			