B
    §d¡  ã               @   s’   d dl mZmZ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lZd dlmZ d dlZddd„ZG d	d
„ d
eƒZG dd„ deƒZdS )é    )Úabsolute_importÚdivisionÚprint_functionN)Úext)Úcrystal)Úflex)Úadopt_init_argsc             C   s   t j| |||||d d S )N)Ú
scatterersÚxray_gradientsÚsite_gradientsÚu_iso_gradientsÚu_aniso_gradientsÚoccupancy_gradients)r   Úminimization_add_gradients)r	   r
   r   r   r   r   © r   úv/mnt/filia/a/genomebrowser/www/genomebrowser/fleming/tools/molprobity/modules/cctbx_project/cctbx/xray/minimization.pyÚadd_gradients   s    r   c               @   s&   e Zd Zddd„Zdd„ Zdd	„ Zd
S )Úoccupancy_penalty_expé   éd   ç»½×Ùß|Û=c             C   s&   t | tƒ ƒ t |¡ ||  | _d S )N)r   ÚlocalsÚmathÚlogÚoccupancy_max)ÚselfÚpenalty_factorÚpenalty_scaleZmin_functionalr   r   r   Ú__init__   s    
zoccupancy_penalty_exp.__init__c             C   sB   || j krdS |dk r&dt | ¡ }| j}t | | | j ¡S )Nr   éÿÿÿÿ)r   r   r   r   Úexpr   )r   Ú	occupancyÚsr   r   r   Ú
functional#   s    
  z occupancy_penalty_exp.functionalc             C   sN   || j krdS |dk r&dt | ¡ }| j}| | j t | | | j ¡ S )Nr   r   )r   r   r   r   r   r    )r   r!   r"   r   r   r   Úgradient)   s    
  zoccupancy_penalty_exp.gradientN)r   r   r   )Ú__name__Ú
__module__Ú__qualname__r   r#   r$   r   r   r   r   r      s
     
r   c               @   s:   e Zd ZdZddd„Zdd	„ Zd
d„ Zdd„ Zdd„ ZdS )Úlbfgsa‘  
  A minimisation of a function of the free parameters of a X-ray structure,
  relying on the LBFGS algorithm.

  That function is represented by a functor object f which should abide to the
  following interface
    - B{f.f_obs()}
    - B{f(f_calc, compute_derivatives)}
  in the manner of `cctbx.xray.target_functors.least_squares_residual`
  or `cctbx.xray.target_functors.intensity_correlation`
  Nç{®Gáz„?Tr   c
       
      C   sâ   t | tƒ ƒ t | j ¡ ¡| _t | j ¡ ¡| _tjj	j
| j ¡ |d| _tjj	j| j ¡ |d| _t | ¡ d¡| _|jdd | ¡ | _| j ¡  ¡ | _d| _tjj| ||d| _|  ¡  | `| `| jdd | j  ¡ | _!dS )	aü  
    :Parameters:

      target_functor : any object like f
        the numerical function of the structure to minimise
      xray_structure : `cctbx.xray.structure`
        the structure the target_functor is calculated for
      occupancy_penalty
        TODO
      lbfgs_termination_params : like `scitbx.lbfgs.termination_parameters`
        the bunch of parameters used to decide where to stop the minimisation
      lbfgs_core_params : like `scitbx.lbfgs.core_parameters`
        the bunch of parameters for the numerical core of the lbfgs algorithm
      correct_special_position_tolerance : number
        the threshold for the distance to a special position below which
        an atom is snapped onto it
      cos_sin_table : bool
        whether to use tabulated cosines and sines in structure factor
        computations
      structure_factor_algorithm : string
        the name of the method to be used to compute structure factors;
        it must be one of those provided by the factory
        `cctbx.xray.structure_factors.from_scatterers`
      verbose : int
        a flag specifying the verbosity of the log printed out during the
        minimisation process; 0 means silent whereas positive numbers
        print an increasing amount of information
    )Ú
miller_setÚcos_sin_tabler   gíµ ÷Æ°>)Úu_minN)Ztarget_evaluatorZtermination_paramsZcore_paramsF)Úcompute_gradients)"r   r   r   Úscatterer_grad_flags_countsÚxray_structurer	   Úgrad_flags_countsÚcctbxÚxrayÚstructure_factorsÚfrom_scatterersÚtarget_functorÚf_obsÚ!structure_factors_from_scatterersÚ	gradientsÚstructure_factor_gradientsr   ÚdoubleÚn_parametersÚxÚtidy_usÚ_scatterers_startÚd_minÚ_d_minÚfirst_target_valueÚscitbxr(   ÚrunÚ	minimizerÚapply_shiftsÚcompute_targetÚtarget_resultÚtargetZfinal_target_value)
r   r5   r/   Úoccupancy_penaltyZlbfgs_termination_paramsZlbfgs_core_paramsÚ"correct_special_position_tolerancer+   Ústructure_factor_algorithmÚverboser   r   r   r   <   s0    $



zlbfgs.__init__c             C   s|   t j| j ¡ | j| jd}|j}| j ¡ }x<| ¡ D ]0}t	j
| j| |¡ ¡ || j| jd|| _q4W | jj|d |jS )N)Ú	unit_cellr	   Úshifts)Úcrystal_symmetryÚ
special_opÚ	site_fracÚ	tolerance)r	   )r   Úminimization_apply_shiftsr/   rM   r>   r<   Úshifted_scatterersÚsite_symmetry_tableÚspecial_position_indicesr   Úcorrect_special_positionÚgetrP   ÚsiterJ   Úreplace_scatterersÚu_iso_refinable_params)r   Zapply_shifts_resultrT   rU   Úi_seqr   r   r   rE   |   s    

zlbfgs.apply_shiftsc             C   sF   | j | j| j ¡ | jd ¡ | _|  | j|¡| _| j ¡ d k	sBt‚d S )N)r/   r*   Ú	algorithm)	r7   r/   r5   r6   rK   Úf_calcrG   rH   ÚAssertionError)r   r-   r   r   r   rF   Œ   s    zlbfgs.compute_targetc             C   sF  |   ¡ }| jdd | j ¡ | _| jd kr2| j| _| jd k	rz| jdkrz| j 	¡  
¡ }x$|D ]}|  j| jj|d7  _qZW | j| j|| j ¡ | j ¡ | j ¡ | jd ¡ | _| jd k	r| jdkrt ¡ }x |D ]}| | jj|d¡ qÒW ~t| j 	¡ | j|d ~| jdkr:tdd	d
 t| jt t | j¡¡ƒ | j| jfS )NT)r-   r   )r!   )r/   r[   r*   Úd_target_d_f_calcr;   r]   )r	   r
   r   r   z(xray.minimization line search: f,rms(g):ú )Úend)rE   rF   rG   rH   ÚfrA   rI   r0   r/   r	   Úextract_occupanciesr#   r9   r5   r6   Úderivativesr<   ÚsizerK   ÚpackedÚgr   r:   Úappendr$   r   rL   Úprintr   ÚsqrtÚmean_sq)r   r[   Zoccupanciesr!   rh   r   r   r   Ú compute_functional_and_gradients–   s@    




z&lbfgs.compute_functional_and_gradientsc             C   s0   | j dkr,tddd t| j| ¡ | ¡ ƒ d S )Nr   z$xray.minimization step: f,iter,nfun:ra   )rb   )rL   rj   rc   ÚiterZnfun)r   rD   r   r   r   Úcallback_after_step¸   s    
zlbfgs.callback_after_step)NNNr)   TNr   )	r%   r&   r'   Ú__doc__r   rE   rF   rm   ro   r   r   r   r   r(   /   s         
9
"r(   )NNNN)Ú
__future__r   r   r   Zcctbx.xray.structure_factorsr1   Ú
cctbx.xrayr   r   Úcctbx.array_familyr   Zscitbx.lbfgsrB   Úscitbx.mathÚlibtbxr   r   r   Úobjectr   r(   r   r   r   r   Ú<module>   s      
