ó
H™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 m Z d  d l	 Z	 d  d l
 Z
 d „  Z d „  Z d	 d d
 d e d d „ Z d „  Z d „  Z e d k r½ e ƒ  n  d S(   iÿÿÿÿ(   t   division(   t   xray(   t   random_structure(   t   debug_utils(   t   flexNc         C   sU   xN |  j  ƒ  D]@ } | j j ƒ  r | j j ƒ  r | j | t j ƒ  7_ q q Wd  S(   N(   t
   scattererst   flagst	   use_u_isot
   grad_u_isot   u_isot   random(   t	   structuret   shiftt   sc(    (    sT   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/cctbx/regression/tst_xray_minimization.pyt   shift_u_iso   s    c         C   s…   x~ |  j  ƒ  D]p } | j j ƒ  r | j j ƒ  r t | j ƒ } x. t d ƒ D]  } | | c | t j ƒ  7<qM W| | _ q q Wd  S(   Ni   (   R   R   t   use_u_anisot   grad_u_anisot   listt   u_start   xrangeR
   (   R   R   R   R   t   i(    (    sT   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/cctbx/regression/tst_xray_minimization.pyt   shift_u_aniso   s    i	   gš™™™™™¹?i    c         C   sµ  | d k s | d k s t  ‚ | d k r> |  t j k r> d  St j | d d5 | |  d d d	 t d
 d d t d t ƒ} t j d | j ƒ  d d d d ƒ t j d | j ƒ  d | j	 d | j
 d | j d | j d | j d | j d |
 d | ƒ 	| j d | d | d d d t ƒ j ƒ  } | d k r8t | ƒ } n/ | d k r]| j ƒ  } | j ƒ  n
 d | ‚ d  ss|	 r¡d! GH| j ƒ  j ƒ  d" G| j ƒ  j ƒ  GHHn  | j ƒ  } | } | j	 rÑ| j d | ƒ } n  | j r| j d | ƒ } | d  k	 rd  | j ƒ  d# _ qn  | j r*t | d$ ƒ n  | j
 rCt | d% ƒ n  t | j ƒ  ƒ t | j ƒ  ƒ k smt  ‚ d  sy|	 r’d& GH| j ƒ  j ƒ  Hn  x‰ t d' ƒ D]o } y1 t j j  d( |  | ƒ d) | d* | d+ d ƒ } Wn4 t! k
 r} t" | ƒ j# d, ƒ d  k  r‚  qqŸXPqŸWt! d- ƒ ‚ d  s*|	 rFd. G| j$ GHd/ G| j% GHHn  | j% | j$ k  s^t  ‚ d  sj|	 rƒd0 GH| j ƒ  j ƒ  Hn  | j& d) | d d d t ƒ j ƒ  } | d k rÂt | ƒ } n | d k rÝ| j ƒ  } n  t' j( | j) ƒ  | j) ƒ  ƒ } | j* ƒ  st  ‚ d  s|	 rO| j+ ƒ  } | r8| d1 7} n  d2 | j, ƒ  G| GHHn  | j, ƒ  } | d3 k ro| GHn  | d k r�| d3 k s±t  ‚ n! | d k r±| d4 k s±t  ‚ n  d  S(6   Nt   Fs   F^2t   elementst   Ot   Nt   Ct   volume_per_atomiÈ   t   random_u_isot   random_u_cart_scaleg333333Ó?t   random_occupancyR   R   t   random_u_iso_scalet   random_u_iso_ming        t   siteR	   t   u_anisot	   occupancyt   fpt   fdpt	   tan_u_isot   paramt   anomalous_flagt   d_mint	   algorithmt   directt   cos_sin_tables   Error: invalid data type: %si    s   structure_ideal:s   n_special_positions:iÿÿÿÿgü©ñÒMbP?gš™™™™™¹?s   structure_shake:i
   t   target_functort   xray_structuret   occupancy_penaltyt   structure_factor_algorithms+   debye_waller_factor_exp: arg_limit exceededs*   Too many xray.minimization.lbfgs failures.s   first:s   final:s   minimized structure_shake:s
   ,anomalouss   correlation: %10.8fg+‡ÙÎ÷ï?gÍÌÌÌÌÌì?(   R   R   R   (-   t   AssertionErrorR   t   unified_least_squares_residualR   R.   t   Truet   random_modify_adp_and_adp_flagsR   t   set_scatterer_grad_flagsR!   R	   R"   R#   R$   R%   t   structure_factorst   f_calct   abst   normt#   set_observation_type_xray_intensityt   show_summaryt   show_scattererst   special_position_indicest   sizet   deep_copy_scattererst   random_modify_parameterst   NoneR   R   t   tupleR   t   minimizationt   lbfgst   RuntimeErrort   strt   findt   first_target_valuet   final_target_valuet!   structure_factors_from_scatterersR   t   linear_correlationt   datat   is_well_definedt   string_of_truet   coefficient(   R-   t	   data_typet   space_group_infoR(   t   gradient_flagsR/   t
   n_elementsR)   t   shake_sigmat   verboseR&   R'   t   structure_idealt
   rnd_f_calct   y_obst   structure_ideal_cpt   structure_shaket   i_trialt	   minimizert   et   f_finalt   ct   labelt   c_coefficient(    (    sT   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/cctbx/regression/tst_xray_minimization.pyt   exercise   sÔ     									
				
	c            sÏ  d t  ˆ d ƒ ‰ t t t t f t t t t f t t t t f t t t t f t t t t f f } xdt j j ƒ  j ƒ  D]M‰ xD| D]<\ } } } } t j j d | d | d | d | ƒ ‰ x t t f D]ñ ‰  d  g } t g } ˆ j
 rÿ | j t ƒ n  d  g }	 ˆ j r*|	 j t j j ƒ  ƒ n  x’ | D]Š ‰ ˆ rFd ‰ n d	 ‰ xl |	 D]d ‰ ‡  ‡ ‡ ‡ ‡ ‡ ‡ ‡ ‡ f	 d
 †  }
 y |
 ƒ  WqSt k
 r¶d GH|
 ƒ  |
 ƒ  d GHqSXqSWq1WqÎ Wq‡ Wqz Wd  S(   NR   s   F^2t   F_sqR!   R	   R"   R#   id   i    c              s7   t  ˆ ˆ ˆ ˆ  ˆ d ˆ d ˆ j d ˆ d ˆ d d ƒS(   NR/   RU   R&   R'   R)   g      @(   Rb   t   Verbose(    (	   R(   RP   R   RR   R/   R'   RQ   R&   R-   (    sT   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/cctbx/regression/tst_xray_minimization.pyt   <lambda>¬   s   	s/   Test did not pass: ruling out a random fluke...s
   Ruled out!(   R   s   F^2(   t   hasattrR3   t   FalseR   t   target_functorst   registryt   valuesR6   RR   RA   R	   t   appendR#   RC   t   occupancy_penalty_expR1   (   R   RQ   t   optionst   fsitet   fu_isot
   foccupancyt   fu_anisot   u_penalty_typest
   tan_u_isost   occupancy_penalty_typest   do_exercise(    (	   R(   RP   R   RR   R/   R'   RQ   R&   R-   sT   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/cctbx/regression/tst_xray_minimization.pyt   run_call_backŒ   sF    							'c          C   sì   d d d g }  g  } g  } xB t  j d D]3 } | |  k rO | j | d ƒ q) | j | ƒ q) Wt | ƒ } d } d | k r‡ d } n  d | k oœ d | k s¦ t ‚ d | k rº d	 GHn  d | k rÎ d
 GHn  t j | t d | | ƒd  S(   Ns   --Fs   --F_sqs   --debuggingi   i   t	   debuggingR   Rc   s   Refinement against Fs   Refinement against F^2t   keywords(    (    (   t   syst   argvRk   RB   R1   R   t   parse_options_loop_space_groupsRv   (   t   cmd_argst   extrat   argst   argt   debug(    (    sT   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/cctbx/regression/tst_xray_minimization.pyt   run¿   s&      t   __main__(   t
   __future__R    t   cctbxR   t   cctbx.developmentR   R   t   cctbx.array_familyR   Ry   R
   R   R   RA   Rg   Rb   Rv   R�   t   __name__(    (    (    sT   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/cctbx/regression/tst_xray_minimization.pyt   <module>   s   		
	t	3	