ó
Q™xTc        	   @   sm  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 m Z d  d l	 m
 Z
 d  d	 l m Z d  d l Z d
 d e d d d d d d f d d „ Z d
 d e d „ Z d „  Z d e e e d d „ Z d e e e d d d „ Z d e d d „ Z d d d d g d „ Z d e j f d „  ƒ  YZ d e f d „  ƒ  YZ d S(    iÿÿÿÿ(   t   division(   t   sgtbx(   t   crystal(   t   adptbx(   t   xrayN(   t   eltbx(   t   flex(   t   adopt_init_argsg333333Ó?g        g      ð¿g…ëQ¸þ‡ÃÀc         C   sÈ  g  } xP |  D]H } t  j  ƒ  | | } | j | ƒ | d k rO | j d k s t ‚ q W|  }	 |	 d j }
 |	 d j } |	 d j } | } | d |	 d _ |	 d j j d t d t ƒ | d	 |	 d	 _ |	 d	 j j d t d t ƒ | d
 |	 d
 _ |	 d
 j j d t d t ƒ | d |	 d _ |	 d j j d t d t ƒ | |	 d _ |	 d j j d t d t ƒ | |	 d _ |	 d j j d t d t ƒ | d |	 d _ | |	 d _ |	 d j j d t d t ƒ | d |	 d _ | |	 d _ |	 d j j d t d t ƒ | |	 d _ | |	 d _ |	 d j j d t d t ƒ | d k r‚| d	 |	 d	 _ |	 d	 j j d t d t ƒ | d |	 d _ |	 d j j d t d t ƒ | d |	 d _ |	 d j j d t d t ƒ | d |	 d _ |	 d j j d t d t ƒ | d |	 d _ | |	 d _ |	 d j j d t d t ƒ | d |	 d _ | |	 d _ |	 d j j d t d t ƒ nB| d k rÄ| d	 |	 d	 _ |	 d	 j j d t d t ƒ | d
 |	 d
 _ |	 d
 j j d t d t ƒ | |	 d _ |	 d j j d t d t ƒ | d |	 d _ |
 |	 d _ |	 d j j d t d t ƒ | d |	 d _ | |	 d _ |	 d j j d t d t ƒ | |	 d _ | |	 d _ |	 d j j d t d t ƒ n  d  S(   Ng        g      ð¿i   i   i   i    t   isot   anisoi   i   i   i   i   t   u_isot   u_star(   g      ð¿g      ð¿g      ð¿g      ð¿g      ð¿g      ð¿(	   t   randomt   appendR   t   AssertionErrorR
   t   flagst	   set_use_ut   Truet   False(   t
   scattererst   random_u_iso_scalet   random_u_iso_mint   parameter_namet   neg_u_anisot	   neg_u_isot   u_isost	   scattererR
   t   sct   us6t   us7t   us8t   nv(    (    sP   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/cctbx/development/random_structure.pyt   random_modify_adp_and_adp_flags   s|    % c   
      C   sÔ  xÍ|  D]Å} t  j  ƒ  | | } | | _ | j d k rH | j d k sN t ‚ | rð t  j  ƒ  }	 |	 d k  r® | j j t ƒ | d k r› | j j t ƒ qí | j j | ƒ q| d k rÍ | j j t ƒ n | j j | ƒ | j j t ƒ n  | j j | ƒ | j j | ƒ | j j	 ƒ  sFt  j  ƒ  }	 |	 d k  rFd | _ qFn  | j j
 ƒ  s|t  j  ƒ  }	 |	 d k  r|d | _ q|n  | j j
 ƒ  r | j j	 ƒ  r t  j  ƒ  }	 |	 d k  rÌd | _ d	 | _ qÌq q Wd  S(
   Ng        g      ð¿g      à?R   R
   g      $À(   g      ð¿g      ð¿g      ð¿g      ð¿g      ð¿g      ð¿(   g      $Àg      $Àg      $Àg      $Àg      $Àg      $À(   g      $Àg      $Àg      $Àg      $Àg      $Àg      $À(   R   R
   R   R   R   t   set_use_u_isoR   t   set_use_u_anisoR   t   use_u_anisot	   use_u_iso(
   R   R$   R#   t	   allow_mixR   R   R   R   R
   t   choice(    (    sP   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/cctbx/development/random_structure.pyt!   random_modify_adp_and_adp_flags_2Q   s<    	$   	c         C   s7   x0 |  D]( } t  j | | ƒ j ƒ  | k  r t Sq Wt S(   N(   R   t   min_sym_equiv_distance_infot   distR   R   (   t   existing_sitest   sym_equiv_sites_of_other_sitet   min_hetero_distancet   existing_site(    (    sP   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/cctbx/development/random_structure.pyt   have_suitable_hetero_distance{   s    	g      ø?id   c         C   sr  xkt  | ƒ D]]} | d  k rC t j ƒ  t j ƒ  t j ƒ  f } n, g  | D] }	 t j |	 ƒ t |	 ƒ ^ qJ } |  j | ƒ }
 | r— |
 j ƒ  r— q n  t j |
 ƒ } t	 | | | ƒ s¾ q n  |
 j
 ƒ  } | d  k rÚ | Sg  t | | ƒ D] \ } } | | ^ qê } |  j | ƒ } | r/| j ƒ  r/q n  t j | ƒ } t	 | | g | | ƒ s]q n  | | j
 ƒ  f SWd  S(   N(   t   xranget   NoneR   t	   randranget   floatt   site_symmetryt   is_point_group_1R   t   sym_equiv_sitesR.   t
   exact_sitet   zip(   t   special_position_settingsR*   R,   t   general_position_onlyt   gridt   t_centre_of_inversiont
   max_trialst   trialt   sitet   gR3   R5   t   xt   tt   site_invt   site_symmetry_invt   sym_equiv_sites_inv(    (    sP   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/cctbx/development/random_structure.pyt   random_site…   s2    $,-
c	      
   C   sî   | }	 | d  k	 r5 | d d k s( t ‚ |	 d }	 n  x© t | ƒ D]› }
 | } xk t |	 ƒ D]] } t |  | | | | | | ƒ } | d  k r� Pn  | d  k r¬ | j | ƒ q\ | j | ƒ q\ Wt | ƒ t | ƒ | k rB | SqB Wt d ‚ d  S(   Ni   i    s+   Cannot find sites matching all constraints.(   R0   R   R/   RE   R   t   extendt   lent   RuntimeError(   R8   R*   t   n_newR,   t   general_positions_onlyR:   R;   R<   t   max_back_trackt   n_loopt   i_back_trackt	   all_sitest   i_newR>   (    (    sP   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/cctbx/development/random_structure.pyt   random_sites§   s,    		i    c         C   s5  |  j  | ƒ } | j ƒ  d k  s' t ‚ |  j ƒ  } t | j | ƒ ƒ } xÝ t | ƒ D]Ï }	 | r… | d  t j | d | ƒ g }
 n% g  | D] } t j | | ƒ ^ qŒ }
 | j	 ƒ  | j
 |
 ƒ } | d k rö | j | | ƒ } | | k rö qU qö n  |  j  | ƒ } | j	 ƒ  | j	 ƒ  k r#qU n  | SWt d ‚ d  S(   Ngñhãˆµøä>i   i    s   Cannot find suitable site.(   R3   t   distance_movedR   t	   unit_cellt   listt   orthogonalizeR/   R   t   gausst
   special_opt   fractionalizet   distanceRH   (   R8   R>   t   gauss_sigmat   max_distancet   vary_z_onlyR<   R3   RR   t	   site_cartR=   t   modified_site_cartR@   t   modified_siteRX   t   modified_site_symmetry(    (    sP   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/cctbx/development/random_structure.pyt   random_modify_siteÈ   s*    %	 		t   Ot   Mgt   Sit   Cac         C   s,   t  j d | d t  j d |  ƒ t | ƒ ƒ S(   Nt   sequencet   permutationt   size(   R   t   selectt   random_size_tRG   (   Rg   t   choices(    (    sP   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/cctbx/development/random_structure.pyt   random_elementsâ   s    	t   xray_structurec           B   s¼   e  Z d d d d d d d  d d e d d d d e d d d e e d e d d „ Z d d d d „ Z d e d d	 „ Z d
 „  Z d d d „ Z	 d „  Z
 d „  Z d „  Z d e d „ Z RS(   g      I@g      ø?i    g333333ã?g333333Ó?gš™™™™™¹?c      	   C   s/  t  |  t ƒ  d d ƒ| d k rX | d k rC t j d d ƒ } qj t j d | ƒ } n | d k sj t ‚ | |  _ | d k	 r» |  j d k	 s” t ‚ |  j d k s© t ‚ t	 | ƒ |  _ n  |  j d k	 rp|  j d k r+|  j d k	 sî t ‚ |  j d	 k rd	 g |  j |  _ qmt
 d |  j ƒ |  _ qp|  j d k rOt	 |  j ƒ |  _ qpt	 |  j ƒ |  j k spt ‚ n  | d k r¥| j |  j | | j ƒ  j ƒ  ƒ } n  t j d | d | ƒ } |	 d k rÒ| }	 n  t j | d |	 d d d t ƒ} t j j |  | ƒ |  j d k	 r+|  j |  j | ƒ n  d S(   sø	  Initialise the random xray_structure class.

    :param space_group_info: a space group descriptor
    :type space_group_info: sgtbx.space_group_info
    :param space_group_symbol: international space group symbol or number
    :type space_group_symbol: string or integer
    :param unit_cell: the cell parameters of the random structure
    :type unit_cell: uctbx.unit_cell or tuple(a, b, c, alpha, beta, gamma)
    :param elements: A list of elements to use.     Example: "['Si', 'Si', 'O', 'Al']"     If elements='const' then ???.     If elements='random' then random scatterers will be used.
    :type elements: list(strings)
    :param sites_frac: a list of the fractional coordinates for all scatterers
    :type sites_frac: scitbx.array_family.flex.vec3_double
    :param n_scatterers: number of scatterers
    :type n_scatterers: integer
    :param volume_per_atom: the volume an atom should occupy in cubic angstroms
    :type volume_per_atom: float
    :param min_distance: minimal distance between atoms
    :type min_distance: float
    :param min_distance_sym_equiv: minimal distance between symmetrical     equivalent atoms
    :type min_distance_sym_equiv: float
    :param general_positions_only: if 'True' atoms will be placed on general     positions only
    :type general_positions_only: boolean
    :param random_f_prime_d_min: ???
    :type random_f_prime_d_min: float
    :param random_f_prime_scale: ???
    :type random_f_prime_scale: float
    :param random_f_double_prime: ???
    :type random_f_double_prime: float
    :param random_f_double_prime_scale: ???
    :type random_f_double_prime_scale: float
    :param random_u_iso: if 'True' the isotropic temperature factors of all     atoms will be randomised
    :type random_u_iso: boolean
    :param random_u_iso_min: minimum value of u_iso for random u values
    :type random_u_iso: float
    :param random_u_iso_scale: ???
    :type random_u_iso_scale: float
    :param u_iso: a fixed value of u_iso to apply to all atoms
    :type u_iso: float
    :param use_u_iso: if 'True' the atoms will have isotropic temperature factors
    :type use_u_iso: boolean
    :param use_u_aniso: if 'True' the atoms will have anisotropic temperature factors
    :type use_u_aniso: boolean
    :param random_u_cart_scale: ???
    :type random_u_cart_scale: float
    :param random_occupancy: if 'True' the atom sites will have a random occupancy
    :type random_occupancy: boolean
    :param random_occupancy_min: minimal occupancy for a site
    :type random_occupancy_min: float
    t   excludet   space_group_infoRR   t   min_distance_sym_equivt
   sites_fracR$   t   symbols   P 1t   constR   Rg   t   u_star_tolerancei    t   assert_min_distance_sym_equivN(   s   space_group_infos	   unit_cells   min_distance_sym_equivs
   sites_fracs	   use_u_iso(   Rr   s   random(   R   t   localsR0   R   Rn   R   t
   use_u_iso_t   elementst   n_scatterersRG   Rk   t   any_compatible_unit_cellt   groupt   order_zR   t   symmetryR8   R   R   t	   structuret   __init__t   build_scatterers(   t   selfRn   t   space_group_symbolRR   Rw   Rp   Rx   t   volume_per_atomt   min_distanceRo   RJ   t   random_f_prime_d_mint   random_f_prime_scalet   random_f_double_primet   random_f_double_prime_scalet   random_u_isoR   R   R
   R$   R#   t   random_u_cart_scalet   random_occupancyt   random_occupancy_mint   crystal_symmetryR8   (    (    sP   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/cctbx/development/random_structure.pyR~   é   sR    O    	!#				c         C   s…  g  |  j  ƒ  D] } | j ^ q } | d  k rp t d |  d | d t | ƒ d |  j d |  j d | d | ƒ } n. t | ƒ t | ƒ k sŽ t ‚ | t | ƒ } t | ƒ |  j	 k s¹ t ‚ i  } x6 | D]. }	 | j
 |	 ƒ sÆ t j j |	 ƒ | |	 <qÆ qÆ Wd }
 d } |  j  ƒ  j ƒ  } | } xbt | | | ƒ D]M\ }	 } | d	 7} t j d
 |	 t | ƒ d |	 d | ƒ } | j |  j ƒ  |  j ƒ  |  j ƒ  ƒ } |  j ré| |	 j d |  j d ƒ } | d k sÎt ‚ | t j ƒ  |  j }
 n  |  j r| |	 j d ƒ } | t j ƒ  |  j } n  |
 | _ | | _ |  j r‰| j  j! ƒ  |  j" } | r}|  j# r}t j ƒ  |  j$ |  j% } n  | | _" n  |  j& rC| j  j' ƒ  d } x› | d	 7} | d k  sÄt ‚ t( j) d |  j* ƒ } | j+ t( j, |  j ƒ  | ƒ ƒ | _- t( j. |  j ƒ  | j- ƒ } t( j/ | ƒ } t0 | ƒ d k r¨Pq¨q¨n  |  j1 rp|  j2 d	 |  j2 t j ƒ  | _3 n  |  j4 | ƒ q0Wd  S(   NR8   R*   RI   R,   RJ   R:   R;   i    i   t   labelt   scattering_typeR>   g      ð?i   id   t   u_scalegü©ñÒMbP?(5   R   R>   R0   RP   RG   Rƒ   RJ   R   RS   Rx   t   has_keyR   t   xray_scatteringt   best_approximationRg   R7   R   R   t   strt   apply_symmetryRR   t   space_groupRo   R„   t   at_d_star_sqR   R…   R†   R‡   t   fpt   fdpRv   R   t   set_use_u_iso_onlyR
   Rˆ   R   R   R#   t   set_use_u_aniso_onlyR   t   random_u_cartR‰   t   average_u_start   u_cart_as_u_starR   t   u_star_as_u_cartt   eigenvaluest   minRŠ   R‹   t	   occupancyt   add_scatterer(   R€   Rw   Rp   R:   R;   R   R*   RN   t   sf_dictt   elementR—   R˜   t
   n_existingt   i_labelR>   R3   t   f0R
   t   run_away_countert   u_cartRŸ   (    (    sP   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/cctbx/development/random_structure.pyR   i  s‚    "		 
											

	id   c         C   s   t  |  | | | | | ƒ S(   N(   R`   (   R€   R>   RY   RZ   R[   R<   (    (    sP   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/cctbx/development/random_structure.pyR`   ±  s    c         C   s   t  d t j | | ƒ ƒ S(   Ngš™™™™™¹?(   t   maxR   RU   (   R€   R
   RY   (    (    sP   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/cctbx/development/random_structure.pyt   random_modify_u_iso¸  s    g      ð?i   c         C   sù   xé t  | ƒ D]Û } g  } x� t  t | ƒ ƒ D]{ } | | } | | }	 t j | | ƒ }
 |
 | | |	 k ry | |	 }
 n! | |
 | |	 k rš | |	 }
 n  | j |
 ƒ q, Wt j |  j ƒ  | ƒ } t j | ƒ } t	 | ƒ d k r | Sq Wt
 d ‚ d  S(   Ngü©ñÒMbP?s   Cannot find suitable u_star.(   R/   RG   R   RU   R   R   Rž   RR   RŸ   R    RH   (   R€   R   RY   t   max_relative_differenceR<   R=   t   modified_u_start   it   ut   max_difft
   modified_uR©   RŸ   (    (    sP   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/cctbx/development/random_structure.pyt   random_modify_u_star»  s     

c         C   s#   t  d | t t j d | ƒ ƒ ƒ S(   Ngš™™™™™¹?i    (   Rª   t   absR   RU   (   R€   R¡   RY   (    (    sP   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/cctbx/development/random_structure.pyt   random_modify_occupancyÏ  s    c         C   s+   | d k  s t  ‚ t d t j | | ƒ ƒ S(   Ni    gš™™™™™¹¿(   R   R    R   RU   (   R€   R—   RY   (    (    sP   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/cctbx/development/random_structure.pyt   random_modify_fpÒ  s    c         C   s+   | d k s t  ‚ t d t j | | ƒ ƒ S(   Ni    gš™™™™™¹?(   R   Rª   R   RU   (   R€   R˜   RY   (    (    sP   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/cctbx/development/random_structure.pyt   random_modify_fdpÖ  s    c         C   s;  |  j  ƒ  } x(| j ƒ  D]} | d k rL |  j | j | d | ƒ| _ q | d k r‚ | j j ƒ  r‚ |  j | j | ƒ | _ q | d k r¸ | j j ƒ  r¸ |  j	 | j
 | ƒ | _
 q | d k rß |  j | j | ƒ | _ q | d k r|  j | j | ƒ | _ q | d k r-|  j | j | ƒ | _ q t ‚ q W| S(   NR>   R[   R
   R   R¡   R—   R˜   (   t   deep_copy_scatterersR   R`   R>   R   R$   R«   R
   R#   R²   R   R´   R¡   Rµ   R—   R¶   R˜   RH   (   R€   R   RY   R[   t   modified_structureR   (    (    sP   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/cctbx/development/random_structure.pyt   random_modify_parametersÚ  s"    
NgUUUUUUÕ?(   t   __name__t
   __module__R0   R   R   R~   R   R`   R«   R²   R´   Rµ   R¶   R¹   (    (    (    sP   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/cctbx/development/random_structure.pyRl   ç   sJ   kE				t   wyckoff_pair_generatorc           B   s)   e  Z d  d d d d d „ Z d „  Z RS(   iè  i   Rr   i
   c         C   s\   t  |  t ƒ  ƒ t j d t j d | j d | ƒ d | ƒ d | ƒ |  _ | j ƒ  |  _ d  S(   NRŒ   RR   t   volumeRn   Ro   (   R   Ru   R   R8   R|   Ry   t   wyckoff_table(   R€   Rn   t   unit_cell_volumeRo   t   min_cross_distanceRŽ   t   max_trials_per_position(    (    sP   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/cctbx/development/random_structure.pyR~   õ  s    					c   
      c   sC  x<t  |  j j ƒ  ƒ D]%} |  j j d |  j d | ƒ } t j | ƒ } xï t  |  j j ƒ  ƒ D]Ø } xÏ t  |  j ƒ D]¾ } |  j j d |  j d | ƒ j ƒ  } t j	 | | ƒ } | j
 ƒ  |  j k ru t j d |  j d t j g  | j ƒ  | g D]! } t j d |  j d | ƒ ^ qñ ƒ ƒ }	 |	 | j
 ƒ  f VPqu qu Wq_ Wq Wd  S(   NR8   t
   i_positionR   RŽ   R>   (   R/   R¾   Rg   t   random_site_symmetryR8   R   R5   RÁ   R6   R(   R)   RÀ   R   R}   R   t   xray_scattererR   RŽ   (
   R€   RÂ   t   site_symmetry_it   equiv_sites_it
   j_positiont   n_trialt   site_jt	   dist_infoR>   R}   (    (    sP   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/cctbx/development/random_structure.pyt   loop  s&    					=(   Rº   R»   R~   RË   (    (    (    sP   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/cctbx/development/random_structure.pyR¼   ó  s   
(   t
   __future__R    t   cctbxR   R   R   R   t   cctbx.eltbx.xray_scatteringR   t   cctbx.array_familyR   t   libtbxR   R   R0   R    R'   R.   R   RE   RP   R`   Rk   R}   Rl   t   objectR¼   (    (    (    sP   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/cctbx/development/random_structure.pyt   <module>   sH   E#	ÿ 