ó
ťxTc           @   s   d  Z  d d l m Z d d l Z d d l Z d d l m Z d d l Z	 d d l m
 Z
 d d l m Z d e f d     YZ d S(	   s   
Wrapper module for computing targets and gradients for restraints (or other
energy functions) on coordinates and B-factors; used in phenix.refine.
i˙˙˙˙(   t   divisionN(   t   flex(   t   adptbx(   t   Sorryt   managerc        
   B   s   e  Z d  Z d d d e e e d e d d 	 Z d   Z d d d e d e e d d  Z d e d d d d  Z d d e d d  Z	 RS(   sî   
  Central management of restraints on molecular geometry and ADPs.  This
  includes the standard stereochemistry restraints (i.e. Engh & Huber),
  non-crystallographic symmetry restraints, reference model restraints, and
  optionally.
  c
   
      C   sL   | |  _  | |  _ | |  _ | |  _ | |  _ | |  _ | |  _ |	 |  _ d  S(   N(   t   geometryt
   ncs_groupst   torsion_ncs_groupst   normalizationt	   use_ambert   amber_structst	   use_afittt   afitt_object(
   t   selfR   R   R   R   R	   t
   use_sanderR
   R   R   (    (    s>   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/restraints.pyt   __init__   s    
							c         C   s  |  j  d  k r d  } n? t | t j  rB |  j  j d |  } n |  j  j d |  } |  j d  k ro d  } n6 t | t j  s | j   } n  |  j j d |  } |  j d  k r˝ d  } n |  j j d |  } t	 d | d | d | d |  j
 d |  j d |  j  S(	   Nt
   iselectiont	   selectionR   R   R   R   R	   R
   (   R   t   Nonet
   isinstanceR   t   size_tt   selectR   R   R   R   R   R	   R
   (   R   R   R   R   R   (    (    s>   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/restraints.pyR   +   s(    					c
         C   sp  t  j j d | d | d | j   d t j d |  j  }
 |  j d k rT d |
 _ nš|  j	 r | r |  j j
 d | d | d | d	 | d t d d d
 | d t  } d d l } | j d | d | j d t j d |  j  } | j
 d |  j j d |  |
 _ n |  j rťt |  |  j j k rťd d l m } |  j j
 d | d | d | d	 | d | d |
 j d
 | d t  |
 _ | j |
 |  j |  }
 | j |
 |  | |	 |  j  }
 |
 j |
 _ nE |  j j
 d | d | d | d	 | d | d |
 j d
 | d t  |
 _ |
 |
 j 7}
 |  j d k r(d |
 _ n: |  j j
 d | d | d |
 j d t  |
 _ |
 |
 j 7}
 |
 j   |
 S(   s  
    Compute energies for coordinates.  Originally this just used the standard
    geometry restraints from the monomer library, but it has since been
    extended to optionally incorporate a variety of external energy functions.

    :returns: scitbx.restraints.energies object
    t   compute_gradientst	   gradientst   gradients_sizet   gradients_factoryR   t
   sites_cartt   flagst   external_energy_functiont   custom_nonbonded_functiont   disable_asu_cachei˙˙˙˙Nt   number_of_restraintsR
   t   crystal_symmetry(   t   afitt(   t   scitbxt
   restraintst   energiest   sizeR   t   vec3_doubleR   R   R   R	   t   energies_sitest   Falset   amber_adaptbxt   geometry_managerR   R
   R    R   t   lenR   t   total_model_atomst   mmtbx.geometry_restraintsR!   R   t   applyt   adjust_energy_and_gradientst   residual_sumt   targetR   t   finalize_target_and_gradients(   R   R   t   geometry_flagsR   R   R   R   t   force_restraints_modelR   t   hd_selectiont   resultt   geometry_energyR)   t   amber_geometry_managerR!   (    (    s>   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/restraints.pyR'   H   s    										
c
         C   sh  t  j j d | d |	 d | j   j   d t j d |  j  }
 |  j d k rZ d |
 _ nU t
 j j d |  j d | d | d	 | d
 | d | d | d |
 j  |
 _ |
 |
 j 7}
 |  j d k	 rŕ |  j d k	 rŕ t d d   n  |  j d k rű d |
 _ nC |  j j d | j   d | j d | d |
 j  |
 _ |
 |
 j 7}
 |  j d k rYd |
 _ nC |  j j d | j   d | j d | d |
 j  |
 _ |
 |
 j 7}
 |
 j   | rd| d k	 rK| d k rKt j |  } | d k	 r | t j t j |  d } qQt j t j | j   j   | d  } | t j t j |  d } n d } |
 j | |
 _ n  |
 S(   s   
    Compute target and gradients for isotropic ADPs/B-factors relative to
    restraints.

    :returns: scitbx.restraints.energies object
    R   R   R   R   R   t   geometry_restraints_managert   xray_structuret
   parameterst   wilson_bt   use_hdt   use_u_local_onlys5   Cannot have both Cartesian and torsion NCS restraintss    at the same time.t   u_isost   average_poweri    g      đ?g       @Ng      ŕ?(   R"   R#   R$   t
   scatterersR%   R   t   doubleR   R   R   t   cctbxt   adp_restraintst   energies_isoR   R   R   R   t   energies_adp_isot   extract_u_iso_or_u_equivR@   R2   R   t   b_as_ut   matht   pit   pow2t   tant   extract_u_iso(   R   R:   R;   R>   R=   R<   R   t   tan_b_iso_maxt   u_iso_refinable_paramsR   R6   t	   u_iso_maxt   chain_rule_scale(    (    s>   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/restraints.pyRF   °   sb    				
!*!c      	   C   s­   t  j j d |  j d | d | d |  } |  j rŠ d } | j d k rX | | j } n  | j | 9_ | j | 9_ | j | 9_ | j	 d k	 rŠ | j	 | 9_	 qŠ n  | S(   s   
    Compute target and gradients for isotropic ADPs/B-factors relative to
    restraints.

    :returns: scitbx.restraints.energies object
    t   restraints_managerR:   R   R=   g      đ?i    N(   RC   RD   t   adp_aniso_restraintsR   R   R   R1   t   gradients_aniso_cartt   gradients_aniso_start   gradients_isoR   (   R   R:   R=   R   R   R   R6   t   normalization_scale(    (    s>   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/restraints.pyt   energies_adp_anisoř   s    			N(
   t   __name__t
   __module__t   __doc__R   R(   R   R   R'   RF   RX   (    (    (    s>   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/restraints.pyR      s8   	dA(   R[   t
   __future__R    t   cctbx.adp_restraintsRC   RI   t   cctbx.array_familyR   t   scitbx.restraintsR"   R   t   libtbx.utilsR   t   objectR   (    (    (    s>   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/mmtbx/restraints.pyt   <module>   s   