ó
ò™xTc           @   sì   d  d l  m Z d  d l j j 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 e f d „  ƒ  YZ d	 e f d
 „  ƒ  YZ d e f d „  ƒ  YZ d e f d „  ƒ  YZ d e f d „  ƒ  YZ d S(   iÿÿÿÿ(   t   divisionN(   t   InvalidConstraint(   t	   superpose(   t   matrix(   t   flext   rigid_pivoted_rotatable_groupc           B   s/   e  Z d  Z d „  Z e d „  ƒ Z d „  Z RS(   s�    a set of atoms (rigid body) rides on a pivot atom and rotates around
  the pivot-pivot_neighbour bond, the original geometry is not altered
  c         C   sR   t  | ƒ d k r! t d ƒ ‚ n  | |  _ | |  _ | |  _ | |  _ | |  _ d  S(   Ni    s   at least one atom is expected(   t   lenR   t   pivott   pivot_neighbourt   indicest   sizeablet	   rotatable(   t   selfR   R   t   ind_sequenceR
   R   (    (    sP   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/smtbx/refinement/constraints/rigid.pyt   __init__   s    				c         C   s   t  d „  |  j Dƒ ƒ S(   Nc         s   s   |  ] } | d  f Vq d S(   t   siteN(    (   t   .0t   idx(    (    sP   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/smtbx/refinement/constraints/rigid.pys	   <genexpr>   s    (   t   tupleR	   (   R   (    (    sP   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/smtbx/refinement/constraints/rigid.pyt   constrained_parameters   s    c   
      C   s  | j  j ƒ  } | j |  j ƒ } | j |  j ƒ } | j t j d d d |  j ƒ} | j t j d d d |  j	 ƒ} t
 g  |  j D] } | | ^ q‚ ƒ } | j t j d | d | d | d | d	 | ƒ} x@ t |  j ƒ D]/ \ } }	 | j | | |	 ƒ | | j |	 _ qÛ Wd  S(
   Nt   valuei    t   variablei   R   R   t   azimutht   sizet
   scatterers(   t	   structureR   t   add_new_site_parameterR   R   t   addt   _t   independent_scalar_parameterR   R
   R   R	   R   t	   enumeratet   add_new_site_proxy_parametert   asu_scatterer_parametersR   (
   R   t   reparametrisationR   t   pivot_spt
   pivot_n_spR   R   t   it   paramt   j(    (    sP   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/smtbx/refinement/constraints/rigid.pyt   add_to   s$    &		(   t   __name__t
   __module__t   __doc__R   t   propertyR   R'   (    (    (    sP   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/smtbx/refinement/constraints/rigid.pyR   
   s   		t    rigid_rotatable_expandable_groupc           B   s/   e  Z d  Z d „  Z e d „  ƒ Z d „  Z RS(   sV    a set of atoms rides on a pivot atom, rotates and uniformly
  expands or contracts
  c         C   sI   t  | ƒ d k r! t d ƒ ‚ n  | |  _ | |  _ | |  _ | |  _ d  S(   Ni    s   at least one atom is expected(   R   R   R   R	   R
   R   (   R   t   centerR   R
   R   (    (    sP   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/smtbx/refinement/constraints/rigid.pyR   5   s    			c         C   s   t  d „  |  j Dƒ ƒ S(   Nc         s   s   |  ] } | d  f Vq d S(   R   N(    (   R   R   (    (    sP   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/smtbx/refinement/constraints/rigid.pys	   <genexpr>?   s    (   R   R	   (   R   (    (    sP   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/smtbx/refinement/constraints/rigid.pyR   =   s    c         C   sH  | j  j ƒ  } | j |  j ƒ } | j t j d d d |  j ƒ} | j t j d d d |  j ƒ} | j t j d d d |  j ƒ} | j t j d d d |  j ƒ} t	 g  |  j
 D] } | | ^ q² ƒ } | j t j d | d | d | d | d	 | d
 | ƒ}	 x@ t |  j
 ƒ D]/ \ } }
 | j |	 | |
 ƒ |	 | j |
 _ qWd  S(   NR   i   R   i    R   R   t   alphat   betat   gammaR   (   R   R   R   R   R   R   R   R
   R   R   R	   R,   R   R   R    R   (   R   R!   R   R"   R   R.   R/   R0   R$   R%   R&   (    (    sP   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/smtbx/refinement/constraints/rigid.pyR'   A   s,    &		(   R(   R)   R*   R   R+   R   R'   (    (    (    sP   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/smtbx/refinement/constraints/rigid.pyR,   0   s   	t   rigid_riding_expandable_groupc           B   s/   e  Z d  Z d „  Z e d „  ƒ Z d „  Z RS(   sT    a set of atoms rides on a pivot atom, rotates and uniformly
  expands or shrinks
  c         C   s@   t  | ƒ d k r! t d ƒ ‚ n  | |  _ | |  _ | |  _ d  S(   Ni    s   at least one atom is expected(   R   R   R   R	   R
   (   R   R-   R   R
   (    (    sP   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/smtbx/refinement/constraints/rigid.pyR   _   s
    		c         C   s   t  d „  |  j Dƒ ƒ S(   Nc         s   s   |  ] } | d  f Vq d S(   R   N(    (   R   R   (    (    sP   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/smtbx/refinement/constraints/rigid.pys	   <genexpr>h   s    (   R   R	   (   R   (    (    sP   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/smtbx/refinement/constraints/rigid.pyR   f   s    c         C   sÓ   | j  j ƒ  } | j |  j ƒ } | j t j d d d |  j ƒ} t g  |  j	 D] } | | ^ qO ƒ } | j t j
 d | d | d | ƒ} x@ t |  j	 ƒ D]/ \ } } | j | | | ƒ | | j | _ qœ Wd  S(   NR   i   R   R   R   R   (   R   R   R   R   R   R   R   R
   R   R	   R1   R   R   R    R   (   R   R!   R   R"   R   R$   R%   R&   (    (    sP   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/smtbx/refinement/constraints/rigid.pyR'   j   s    &		(   R(   R)   R*   R   R+   R   R'   (    (    (    sP   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/smtbx/refinement/constraints/rigid.pyR1   Z   s   	t   idealised_fragmentc           B   sK   e  Z d  Z d „  Z d d d „  ƒ  YZ d „  Z d d „ Z d d „ Z RS(	   s|    ported from olex2 xlib/fragment.h
  generates parameterised coordinates for four framents:
  Cp, Ph, Cp* and naphthalene
  c         C   s)   i d d 6d	 d 6d
 d 6d d 6|  _  d  S(   Ng¸…ëQ¸ö?t   Cpg5^ºIñ?s   Cp*g=
×£p=ö?t   Pht   Naphthalene(   g¸…ëQ¸ö?(   g¸…ëQ¸ö?g5^ºIñ?(   g=
×£p=ö?(   g=
×£p=ö?(   t   default_lengths(   R   (    (    sP   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/smtbx/refinement/constraints/rigid.pyR   ~   s
    t   pointc           B   s5   e  Z d  „  Z d „  Z d „  Z d „  Z d „  Z RS(   c         C   s   | |  _  | |  _ d  S(   N(   t   xt   y(   R   R8   R9   (    (    sP   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/smtbx/refinement/constraints/rigid.pyR   †   s    	c         C   s   t  j |  j | |  j | ƒ S(   N(   R2   R7   R8   R9   (   R   t   k(    (    sP   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/smtbx/refinement/constraints/rigid.pyt   __mul__‰   s    c         C   s$   t  j |  j | j |  j | j ƒ S(   N(   R2   R7   R8   R9   (   R   t   p(    (    sP   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/smtbx/refinement/constraints/rigid.pyt   __add__‹   s    c         C   s%   t  j |  j |  j |  j |  j ƒ S(   N(   t   matht   sqrtR8   R9   (   R   (    (    sP   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/smtbx/refinement/constraints/rigid.pyt   length�   s    c         C   s&   d t  |  j ƒ d t  |  j ƒ d S(   Nt   (t   ,t   )(   t   strR8   R9   (   R   (    (    sP   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/smtbx/refinement/constraints/rigid.pyt   __repr__�   s    (   R(   R)   R   R;   R=   R@   RE   (    (    (    sP   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/smtbx/refinement/constraints/rigid.pyR7   …   s
   				c   
      C   s°   t  j d | } t  j | ƒ } t  j | ƒ } g  } t j | | ƒ } xa t d | ƒ D]P } | j | | ƒ | j }	 | |	 | | j	 | _ | | j	 | |	 | _	 qX W| S(   s7    generates coordinates of a ring with given radius
    i   i    (
   R>   t   pit   cost   sinR2   R7   t   xranget   appendR8   R9   (
   R   t   edgest   rt   anglet   cat   sat   resultR<   R$   R8   (    (    sP   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/smtbx/refinement/constraints/rigid.pyt   generate_ring’   s    	c         C   sî  | d k r |  j | } n  | d k rX |  j d d | d t j d t j d ƒ ƒ S| d k rx |  j d | d ƒ S| d	 k rò d | d t j d t j d ƒ } |  j d | ƒ } x/ |  j d | | d
 ƒ D] } | j | ƒ q× W| S| d k rê|  j d | d ƒ } | j | d ƒ x( t d d ƒ D] } | j | | ƒ q5W| d | d } | | d d t j t j d ƒ | j ƒ  } x/ t d t	 | ƒ ƒ D] } | | | | | <q¨W| d } | d | d <| | d <| Sd S(   sŒ    generates given fragment with given/default bond lengths
    returns a list of idealised_fragment.point, having x and y
    attributes
    R3   i   g      à?i    i6   i´   R4   i   s   Cp*i   R5   i   i   i   i   i	   N(
   t   NoneR6   RQ   R>   RG   RF   RJ   RI   R@   R   (   R   t   fragmentt   lengthsRL   t   resR$   R<   (    (    sP   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/smtbx/refinement/constraints/rigid.pyt   generate_fragment¡   s2    0&! 0

c         C   s»   | s t  d t | ƒ ƒ } n  g  | D]# } | | j | | j d f ^ q% } t j t j | ƒ t j | ƒ ƒ } t j g  | D] } | j | j d f ^ q ƒ } | j j	 | | j
 j	 S(   s   fits given fragment to given sites, if control_points indices
    are not given - all points are fit, otherwise only control points
    are fit and the result is propagated to the rest of the fragment
    coordinates. returns coordinates of the trasformed fragment
    i    (   t   rangeR   R8   R9   R   t   least_squares_fitR   t   vec3_doubleRL   t   elemst   t(   R   RS   t   reference_sitest   control_point_indicesR$   t   to_fitt   lsf(    (    sP   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/smtbx/refinement/constraints/rigid.pyt   fit¿   s    01(    N(	   R(   R)   R*   R   R7   RQ   RR   RV   R`   (    (    (    sP   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/smtbx/refinement/constraints/rigid.pyR2   y   s   		t
   same_groupc           B   s8   e  Z d  Z d e e d „ Z e d „  ƒ Z d „  Z RS(   s,    non-crystallographic symmetry constraint
  i    c         C   s�   t  | ƒ d k  r! t d ƒ ‚ n  t  | d ƒ } x3 | d D]' } t  | ƒ | k r< t d ƒ ‚ q< q< W| |  _ | |  _ | |  _ | |  _ d S(   s»    fix_xyz and fix_u are to be used for debugging purposes only:
    if the coordinates are not fixed, then the refined angles should
    be stored externally and passed to this object
    i   s#   at least two atom sets are expectedi    i   s   atoms sets differ in sizeN(   R   R   t   groupst   fix_xyzt   fix_ut   angles(   R   Rb   Re   Rc   Rd   t   lt   g(    (    sP   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/smtbx/refinement/constraints/rigid.pyR   Ó   s    			c         C   sy   d } xl t  j |  j d ƒ D]U } xL | D]D } |  j rN | | d f f 7} n  |  j r) | | d f f 7} q) q) Wq W| S(   Ni   R   t   U(    (   t	   itertoolst   isliceRb   Rc   Rd   (   R   RP   Rg   R$   (    (    sP   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/smtbx/refinement/constraints/rigid.pyR   ã   s    		c          C   s/  |  j  r |  j r d  S| j j ƒ  } g  } g  } g  } g  } g  } g  } | j j ƒ  }	 x¢ |  j d D]“ }
 | j |	 j | |
 j ƒ ƒ |  j r­ | j | j	 |
 ƒ ƒ n  |  j  rh | |
 j
 j ƒ  râ | j | j |
 ƒ ƒ qû | j | j |
 ƒ ƒ qh qh Wx)|  j d D]} t | ƒ t |  j d ƒ k rAt d ƒ ‚ n  g  } g  } g  } g  } xÀ t | ƒ D]² \ } }
 | |
 j
 j ƒ  | |  j d | j
 j ƒ  k r²t d ƒ ‚ n  | j | |
 ƒ | j |	 j | |
 j ƒ ƒ | |
 j
 j ƒ  r| j | |
 ƒ qf| j | |
 ƒ qfWt j t j | ƒ t j | ƒ ƒ } t | ƒ d k r£xN t d t | ƒ ƒ D]4 }
 | j d t j | j ƒ t j | |
 ƒ ƒ qhWn  | j } t j | j ƒ t j | j ƒ } | j ƒ  } d } xM t | ƒ D]? \ }
 } | t j t j | ƒ t j | |
 ƒ ƒ j ƒ  7} qíWt j t j | ƒ t j | ƒ ƒ } | j ƒ  } d } xM t | ƒ D]? \ }
 } | t j t j | ƒ t j | |
 ƒ ƒ j ƒ  7} qsW| | k  rÏ| j } n  | j ƒ  } |  j rb| j t j d | d | d | d d d d f d t  ƒ} t | ƒ d k rÂ| j t j! d | d	 | d
 | d | ƒ} qÂn` t | ƒ d k rÂ| j t j" d |  j# d t  ƒ} | j t j! d | d	 | d
 | d | ƒ} n  |  j rø| j t j$ d | d | d
 | d | ƒ} n  t | ƒ d k r+| j t j% d | d | ƒ} n  d } d } d } xç | D]ß }
 |  j rƒ| | j& |
 _ | j' | | |
 ƒ | d 7} n  |  j  rD| |
 j
 j ƒ  rá| | j& |
 _( | j t j) d | d | ƒ| j* |
 <| d 7} q#| | j& |
 _( | j t j+ d | d | ƒ| j* |
 <| d 7} qDqDWqWd  S(   Ni    i   s   Group size mismatchs+   Mixing isotropic and anisotropic parametersi   R   R   R   t   u_starst   alignment_matrixt   shifts_and_anglesRe   t   sitest   u_isost   parentt   index(,   Rd   Rc   R   R   t	   unit_cellRb   RJ   t   orthogonalizeR   R   t   flagst	   use_u_isot&   add_new_thermal_displacement_parameterR   R   R   R   RX   R   RY   RI   R   t   colt   other_shiftRL   t   reference_shiftt   other_sites_best_fitt	   length_sqt	   transposeR   R   t$   independent_small_6_vector_parametert   Truet   same_group_u_start$   independent_small_3_vector_parameterRe   t   same_group_xyzt   same_group_u_isoR    t'   add_new_same_group_site_proxy_parametert   ut   same_group_u_iso_proxyt	   shared_Ust   same_group_u_star_proxy(    R   R!   R   t	   ref_sitest
   ref_u_isost   ref_u_starst   ref_adpst   src_crdst   inv_src_crdst   ucR$   Rg   t   g_scattererst   g_u_iso_scattererst   g_u_star_scattererst   crdsR   R_   t   rmR[   t   new_crdt   dt   ct   d_invRm   t   u_star_paramRe   t
   site_paramt   u_iso_paramt   site_proxy_indext   u_star_proxy_indext   u_iso_proxy_index(    (    sP   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/smtbx/refinement/constraints/rigid.pyR'   î   sÞ    		!/	"77	-									(   i    i    i    (   R(   R)   R*   R~   R   R+   R   R'   (    (    (    sP   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/smtbx/refinement/constraints/rigid.pyRa   Ï   s   (   t
   __future__R    t   smtbx.refinement.constraintst
   refinementt   constraintsR   R   R>   t   scitbx.mathR   t   scitbxR   t   scitbx.array_familyR   Ri   t   objectR   R,   R1   R2   Ra   (    (    (    sP   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/smtbx/refinement/constraints/rigid.pyt   <module>   s   &*V