ó
I™xTc           @   sn  d  d l  m Z d  d l m Z d  d l m Z 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 Z d  d	 l m Z d  d l Z e j d
 ƒ e j d
 ƒ d „  Z d „  Z d „  Z d d „ Z d d „ Z d d „ Z d d „ Z d „  Z d „  Z d „  Z d „  Z d d „ Z d „  Z  d „  Z! d „  Z" e# d k rje" ƒ  n  d S(   iÿÿÿÿ(   t   division(   t   miller(   t   adptbx(   t   debug_utils(   t   flex(   t   matrix(   t   approx_equalN(   t   StringIOi    c             so   t  d j ƒ  ƒ ‰ d d l m }  d d l m } x6 | D]. } ‡  ‡ f d †  ‰  ˆ  |  j d | ƒ ƒ q9 Wd  S(   Ns¬  1,0,0,0,0,0,0,1,0,0,0,0,0,0,1,0,0,0,0,0,0,1,0,0,0,0,0,0,1,0,0,0,0,0,0,1
1,0,0,0,0,0,0,1,0,0,0,0,0,0,1,0,0,0,0,0,0,0,1,0
1,0,0,0,0,0,0,1,0,0,0,0,0,0,1,0,0,0,0,0,0,0,1,0
1,1,0,0,0,0,0,0,1,0,0,0,0,0,0,1,0,0,0,0,0,0,1,1
1,0,0,0,0,0,0,1,0,0,0,0,0,0,1,0,0,0
1,0,0,0,0,0,0,1,0,0,0,0,0,0,1,0,0,0
1,1,0,0,0,0,0,0,1,0,0,0,0,0,0,1,0,0
1,0,0,0,0,0,0,1,0,0,0,0,0,0,1,0,0,0
-1,-1,-1,1,0,0,-1,-1,-1,0,1,0,-1,-1,-1,0,0,1
1,0,0,0,0,0,0,1,0,0,0,0,0,0,1,0,0,0
1,1,0,1,0,0,1,0,1,0,1,0,0,1,1,0,0,1
1,1,0,0,0,0,0,0,1,0,0,0
1,1,0,0,0,0,0,0,1,0,0,0
1,1,0,1,0,0,1,1,2,0,1,1
0,0,1,0,0,0,2,2,0,1,0,0
0,0,1,0,0,0,2,2,0,1,0,0
1,1,1,0,0,0,0,0,0,1,1,1
1,1,1,0,0,0
1,1,1,0,0,0
2,2,2,1,1,1
1,1,1,0,0,0
-3,-3,-3,1,1,1
iÿÿÿÿ(   t   sgtbx(   t   acentricc            s„   g  ˆ j  ƒ  j d ƒ D] } t | ƒ ^ q } |  j ƒ  } t | j ƒ  j ƒ  | ƒ s[ t ‚ | j ƒ  d k r€ ˆ  |  j	 ƒ  ƒ n  d  S(   Nt   ,i   (
   t   nextt   splitt   floatt   groupR   t   adp_constraintst   gradient_sum_matrixt   AssertionErrort   n_ltrt   primitive_setting(   t   sgit   _t   expectedt   gr(   t   checkt   expected_matrices(    sR   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/cctbx/regression/tst_adp_constraints.pyR   ,   s    .t   symbol(   t   itert
   splitlinest   cctbxR   t   cctbx.sgtbx.bravais_typesR	   t   space_group_info(   R   R	   R   (    (   R   R   sR   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/cctbx/regression/tst_adp_constraints.pyt   exercise_constraint_matrix   s    c         C   sA   t  j |  | ƒ } t j t  j |  ƒ ƒ } d t j d | | S(   Niþÿÿÿi   (   R   t   debye_waller_factor_u_starR   t   doublet0   debye_waller_factor_u_star_gradient_coefficientst   matht   pi(   t   ht   u_start   dwt   gc(    (    sR   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/cctbx/regression/tst_adp_constraints.pyt   d_dw_d_u_star_analytical6   s    c         C   sA   t  j |  | ƒ } t j t  j |  ƒ ƒ } d t j d | | S(   Ni   (   R   R!   R   R"   t1   debye_waller_factor_u_star_curvature_coefficientsR$   R%   (   R&   R'   R(   t   cc(    (    sR   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/cctbx/regression/tst_adp_constraints.pyt"   d2_dw_d_u_star_d_u_star_analytical;   s    g�íµ ÷Æ°>c         C   s¡   t  j ƒ  } xŽ t t | ƒ ƒ D]z } g  } xJ | | g D]; } t | ƒ } | | c | 7<| j t j |  | ƒ ƒ q9 W| j | d | d d | ƒ q W| S(   Ni    i   i   (   R   R"   t   xranget   lent   listt   appendR   R!   (   R&   R'   t   epst   resultt   ipt   vst
   signed_epst
   u_star_eps(    (    sR   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/cctbx/regression/tst_adp_constraints.pyt   d_dw_d_u_star_finite@   s    %c         C   sÃ   t  j ƒ  } x‹ t t | ƒ ƒ D]w } g  } xG | | g D]8 } t | ƒ } | | c | 7<| j t |  | ƒ ƒ q9 W| j | d | d d | ƒ q W| j t  j	 d d ƒ ƒ | j
 d d ƒ S(   Ni    i   i   i   t   relative_epsilongñhãˆµøä>(   R   R"   R.   R/   R0   R1   R*   t   extendt   reshapet   gridt   matrix_symmetric_as_packed_u(   R&   R'   R2   R3   R4   R5   R6   R7   (    (    sR   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/cctbx/regression/tst_adp_constraints.pyt   d2_dw_d_u_star_d_u_star_finiteK   s    %c   
      C   s°   t  j ƒ  } x� t t | ƒ ƒ D]‰ } g  } xY | | g D]J } t | ƒ } | | c | 7<|  j | ƒ }	 | j t j | |	 ƒ ƒ q9 W| j | d | d d | ƒ q W| S(   Ni    i   i   (	   R   R"   R.   R/   R0   t
   all_paramsR1   R   R!   (
   R   R&   t   u_indepR2   R3   t   i_indepR5   R6   t   u_indep_epst   u_eps(    (    sR   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/cctbx/regression/tst_adp_constraints.pyt   d_dw_d_u_indep_finiteW   s    %c   
      C   sÒ   t  j ƒ  } xŽ t t | ƒ ƒ D]z } g  } xJ | | g D]; } t | ƒ } | | c | 7<| j t |  | | ƒ ƒ q9 W| j | d | d d | ƒ q Wt | ƒ }	 | j t  j	 |	 |	 ƒ ƒ | j
 d d ƒ S(   Ni    i   i   R9   gñhãˆµøä>(   R   R"   R.   R/   R0   R1   RD   R:   R;   R<   R=   (
   R   R&   R@   R2   R3   RA   R5   R6   RB   t   np(    (    sR   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/cctbx/regression/tst_adp_constraints.pyt    d2_dw_d_u_indep_d_u_indep_finitec   s    %c      
   C   s  t  j |  | ƒ } |  \ } } } | d } | d } | d } | d }	 d t j d }
 | | d |
 d | | d | d |
 d | | d | d |
 d | d | d | |
 d g | | d | d |
 d | | d |
 d | | d | d |
 d | d | | d | |
 d g | | d | d |
 d | | d | d |
 d | | d |
 d | d | | d |
 d g | d | d | |
 d | d | | d | |
 d | d | | d |
 d | d | d | d |
 d g g S(   s*  h = {h0,h1,h2}
u = {{u00,0,u02},{0,u11,0},{u02,0,u22}}
dw=Exp[mtps*h.u.h]
FortranForm[D[dw,u00,u00]/dw]
FortranForm[D[dw,u00,u11]/dw]
FortranForm[D[dw,u00,u22]/dw]
FortranForm[D[dw,u00,u02]/dw]
FortranForm[D[dw,u11,u00]/dw]
FortranForm[D[dw,u11,u11]/dw]
FortranForm[D[dw,u11,u22]/dw]
FortranForm[D[dw,u11,u02]/dw]
FortranForm[D[dw,u22,u00]/dw]
FortranForm[D[dw,u22,u11]/dw]
FortranForm[D[dw,u22,u22]/dw]
FortranForm[D[dw,u22,u02]/dw]
FortranForm[D[dw,u02,u00]/dw]
FortranForm[D[dw,u02,u11]/dw]
FortranForm[D[dw,u02,u22]/dw]
FortranForm[D[dw,u02,u02]/dw]
i    i   i   i   iþÿÿÿi   (   R   R!   R$   R%   (   R&   R'   R(   t   h0t   h1t   h2t   u00t   u11t   u22t   u02t   mtps(    (    sR   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/cctbx/regression/tst_adp_constraints.pyt   p2_curvp   s.    



"c   	      C   sÈ   t  j |  | ƒ } |  \ } } } | d } | d } d t j d } | | d | d d | d | | d | d | d | d g | | d | d | d | d | | d | d g g S(   s¾   h = {h0,h1,h2}
u = {{u11,0,0},{0,u11,0},{0,0,u22}}
dw=Exp[mtps*h.u.h]
FortranForm[D[dw,u11,u11]/dw]
FortranForm[D[dw,u11,u22]/dw]
FortranForm[D[dw,u22,u11]/dw]
FortranForm[D[dw,u22,u22]/dw]
i   i   iþÿÿÿi   (   R   R!   R$   R%   (	   R&   R'   R(   RG   RH   RI   RK   RL   RN   (    (    sR   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/cctbx/regression/tst_adp_constraints.pyt   p4_curvŸ   s    


&#c   	      C   sø   t  j |  | ƒ } |  \ } } } | d } | d } d t j d } | | d | d | | d | | | | d | | d | d g | | d | | | | d | | d | d | | d | | | | d | d | d g g S(   sÆ   h = {h0,h1,h2}
u = {{2*u01,u01,0},{u01,2*u01,0},{0,0,u22}}
dw=Exp[mtps*h.u.h]
FortranForm[D[dw,u22,u22]/dw]
FortranForm[D[dw,u22,u01]/dw]
FortranForm[D[dw,u01,u22]/dw]
FortranForm[D[dw,u01,u01]/dw]
i   i   iþÿÿÿi   (   R   R!   R$   R%   (	   R&   R'   R(   RG   RH   RI   RL   t   u01RN   (    (    sR   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/cctbx/regression/tst_adp_constraints.pyt   p3_curv´   s    


63c         C   sj   t  j |  | ƒ } |  \ } } } | d } d t j d } | | d | d | d d | d g g S(   sd   h = {h0,h1,h2}
u = {{u22,0,0},{0,u22,0},{0,0,u22}}
dw=Exp[mtps*h.u.h]
FortranForm[D[dw,u22,u22]/dw]
i   iþÿÿÿ(   R   R!   R$   R%   (   R&   R'   R(   RG   RH   RI   RL   RN   (    (    sR   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/cctbx/regression/tst_adp_constraints.pyt   p23_curvÉ   s
    
c         C   sH   t  d t j  t j |  ƒ ƒ ƒ } t |  | | | d | ƒsD t ‚ d  S(   Ni   R2   (   t   maxR   t   absR   R   (   t   anat   finR2   t   s(    (    sR   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/cctbx/regression/tst_adp_constraints.pyt   compare_derivatives×   s    !c         C   sû  |  j  d d ƒ } |  j ƒ  } | j ƒ  } | j ƒ  } | t j | | j ƒ  d d f ƒ j d t ƒ IJ| t	 | j
 ƒ IJt j ƒ  } t j | j ƒ  | ƒ } | j | ƒ } t j d | d d d t ƒ }	 x(|	 j ƒ  D]}
 t d	 |
 d
 | ƒ } | d It	 | ƒ IJt d	 |
 d
 | ƒ } | d It	 | ƒ IJt | | ƒ t d	 |
 d
 | ƒ } | d It	 | ƒ IJt d	 |
 d
 | ƒ } | d It	 | ƒ IJt | | ƒ | j | ƒ } t d | d	 |
 d | ƒ } | d It	 | ƒ IJt j | j d t	 | ƒ ƒ ƒ } | d It	 | ƒ IJt | | ƒ t d | d	 |
 d | ƒ } | d It	 | ƒ IJ| j d | ƒ } | d It	 | ƒ IJt | | ƒ d  } t! |  ƒ d k ràt	 | j
 ƒ d d d d g k sÎt" ‚ t# |
 | ƒ } nÌ t! |  ƒ d k r%t	 | j
 ƒ d d g k st" ‚ t$ |
 | ƒ } n‡ t! |  ƒ d! k rjt	 | j
 ƒ d d g k sXt" ‚ t% |
 | ƒ } nB t! |  ƒ d k r¬t	 | j
 ƒ d g k sšt" ‚ t& |
 | ƒ } n  | d  k	 rÙ t j | ƒ j' ƒ  } | d  It	 | ƒ IJt | | ƒ qÙ qÙ Wd  S("   Nt   volumeiè  i   t   one_row_per_linet   crystal_symmetryt   d_mini   t   anomalous_flagR&   R'   s
   grads_fin:s
   grads_ana:s
   curvs_fin:s
   curvs_ana:R   R@   s   grads_indep_fin:t   all_gradientss   grads_indep_ana:s   curvs_indep_fin:t   all_curvaturess   curvs_indep_ana:s   P 1 2 1i    i   i   i   s   P 4s   P 3s   P 6s   P 2 3s   curvs_indep_mm:(   s   P 3s   P 6((   t   any_compatible_crystal_symmetryR   R   t   row_echelon_formR   t   rect   sizet   mathematica_formt   TrueR0   t   independent_indicesR   t   random_u_cartt   u_cart_as_u_start	   unit_cellt   average_u_starR   t	   build_sett   Falset   indicesR8   R*   RY   R>   R-   t   independent_paramsRD   R   R"   t   independent_gradientsRF   t   independent_curvaturest   Nonet   strR   RO   RP   RR   RS   R=   (   R   t   outR\   t   space_groupR   t   mt	   u_cart_p1t	   u_star_p1R'   t
   miller_setR&   t	   grads_fint	   grads_anat	   curvs_fint	   curvs_anaR@   t   grads_indep_fint   grads_indep_anat   curvs_indep_fint   curvs_indep_anat   curvs_indep_mm(    (    sR   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/cctbx/regression/tst_adp_constraints.pyt   exercise_derivativesÛ   sr    		)				'!!c         C   sc   |  j  r t j } n	 t ƒ  } t | d | ƒ| j ƒ  j ƒ  d k r_ t | j ƒ  d | ƒn  d  S(   NRt   i   (   t   Verboset   syst   stdoutR   Rƒ   R   R   R   (   t   flagsR   Rt   (    (    sR   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/cctbx/regression/tst_adp_constraints.pyt   run_call_back  s    		c           C   s"   t  ƒ  t j t j d t ƒ d  S(   Ni   (   R    R   t   parse_options_loop_space_groupsR…   t   argvRˆ   (    (    (    sR   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/cctbx/regression/tst_adp_constraints.pyt   run!  s    t   __main__($   t
   __future__R    R   R   R   t   cctbx.developmentR   t   cctbx.array_familyR   t   scitbxR   t   libtbx.test_utilsR   t   randomR$   t	   cStringIOR   R…   t   seedt   set_random_seedR    R*   R-   R8   R>   RD   RF   RO   RP   RR   RS   RY   Rƒ   Rˆ   R‹   t   __name__(    (    (    sR   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/cctbx/regression/tst_adp_constraints.pyt   <module>   s:   	&			/				=			