def compute_target(self, compute_gradients, u_iso_refinable_params): self.stereochemistry_residuals = None self.fmodels.update_xray_structure( xray_structure = self.xray_structure, update_f_calc = True) fmodels_target_and_gradients = self.fmodels.target_and_gradients( weights = self.weights, compute_gradients = compute_gradients, u_iso_refinable_params = u_iso_refinable_params) self.f = fmodels_target_and_gradients.target() self.g = fmodels_target_and_gradients.gradients() if(self.refine_xyz and self.restraints_manager is not None and self.weights.w > 0.0): self.stereochemistry_residuals = \ self.model.restraints_manager_energies_sites( compute_gradients = compute_gradients) # QBLIB INSERT if(self.qblib_params is not None and self.qblib_params.qblib): from qbpy import qb_refinement self.qblib_cycle_count +=1 if(self.tmp_XYZ is not None): diff,max_diff,max_elem = qb_refinement.array_difference( self.tmp_XYZ, self.model.xray_structure.sites_cart(), ) if(self.XYZ_diff_curr is not None): self.XYZ_diff_curr=max_elem else: self.XYZ_diff_curr=max_elem if(max_elem>=self.qblib_params.skip_divcon_threshold): self.tmp_XYZ = self.model.xray_structure.sites_cart() else: self.tmp_XYZ = self.model.xray_structure.sites_cart() if (self.macro_cycle != self.qblib_params.macro_cycle_to_skip): qblib_call = qb_refinement.QBblib_call_manager( hierarchy = self.model.pdb_hierarchy(), xray_structure=self.model.xray_structure, geometry_residuals = self.stereochemistry_residuals, qblib_params=self.qblib_params, diff_curr=self.XYZ_diff_curr, macro=self.macro_cycle, micro=self.qblib_cycle_count, ) try: qblib_call.run() except Exception, e: if e.__class__.__name__=="QB_none_fatal_error": raise Sorry(e.message) else: raise e if(qblib_call.QBStatus): qblib_g=qblib_call.result_QBlib.gradients qblib_f=qblib_call.result_QBlib.target self.stereochemistry_residuals.gradients=qblib_g self.stereochemistry_residuals.target=qblib_f # QBLIB END er = self.stereochemistry_residuals.target self.f += er * self.weights.w if(compute_gradients): sgc = self.stereochemistry_residuals.gradients # ias do not participate in geometry restraints if(self.model is not None and self.model.ias_selection is not None and self.model.ias_selection.count(True) > 0): sgc.extend(flex.vec3_double( self.model.ias_selection.count(True),[0,0,0])) xray.minimization.add_gradients( scatterers = self.xray_structure.scatterers(), xray_gradients = self.g, site_gradients = sgc*self.weights.w)
def compute_target(self, compute_gradients, u_iso_refinable_params): self.stereochemistry_residuals = None self.fmodels.update_xray_structure( xray_structure = self.xray_structure, update_f_calc = True) fmodels_target_and_gradients = self.fmodels.target_and_gradients( weights = self.weights, compute_gradients = compute_gradients, u_iso_refinable_params = u_iso_refinable_params) self.f = fmodels_target_and_gradients.target() self.g = fmodels_target_and_gradients.gradients() if(self.refine_xyz and self.restraints_manager is not None and self.weights.w > 0.0): self.stereochemistry_residuals = \ self.model.restraints_manager_energies_sites( compute_gradients = compute_gradients) # QBLIB INSERT if(self.qblib_params is not None and self.qblib_params.qblib): from qbpy import qb_refinement self.qblib_cycle_count +=1 if(self.tmp_XYZ is not None): diff,max_diff,max_elem = qb_refinement.array_difference( self.tmp_XYZ, self.model.get_sites_cart(), ) if(self.XYZ_diff_curr is not None): self.XYZ_diff_curr=max_elem else: self.XYZ_diff_curr=max_elem if(max_elem>=self.qblib_params.skip_divcon_threshold): self.tmp_XYZ = self.model.get_sites_cart() else: self.tmp_XYZ = self.model.get_sites_cart() if (self.macro_cycle != self.qblib_params.macro_cycle_to_skip): qblib_call = qb_refinement.QBblib_call_manager( hierarchy = self.model.get_hierarchy(), xray_structure=self.model.get_xray_structure(), geometry_residuals = self.stereochemistry_residuals, qblib_params=self.qblib_params, diff_curr=self.XYZ_diff_curr, macro=self.macro_cycle, micro=self.qblib_cycle_count, ) try: qblib_call.run() except Exception as e: if e.__class__.__name__=="QB_none_fatal_error": raise Sorry(e.message) else: raise e if(qblib_call.QBStatus): qblib_g=qblib_call.result_QBlib.gradients qblib_f=qblib_call.result_QBlib.target self.stereochemistry_residuals.gradients=qblib_g self.stereochemistry_residuals.target=qblib_f # QBLIB END er = self.stereochemistry_residuals.target self.f += er * self.weights.w if(compute_gradients): sgc = self.stereochemistry_residuals.gradients # ias do not participate in geometry restraints if self.model is not None and self.model.ias_manager is not None: ias_selection = self.model.ias_manager.get_ias_selection() if ias_selection is not None and ias_selection.count(True) > 0: sgc.extend(flex.vec3_double(ias_selection.count(True),[0,0,0])) xray.minimization.add_gradients( scatterers = self.xray_structure.scatterers(), xray_gradients = self.g, site_gradients = sgc*self.weights.w) if(self.refine_adp and self.restraints_manager is not None and self.restraints_manager.geometry is not None and self.weights.w > 0.0 and self.iso_restraints is not None): use_hd = False if(self.fmodels.fmodel_n is not None or (self.is_neutron_scat_table is not None and self.is_neutron_scat_table == "neutron") or self.h_params.refine == "individual"): use_hd = True energies_adp = self.model.energies_adp( iso_restraints = self.iso_restraints, use_hd = use_hd, compute_gradients = compute_gradients) self.f += energies_adp.target * self.weights.w if(compute_gradients): if(energies_adp.u_aniso_gradients is None): xray.minimization.add_gradients( scatterers = self.xray_structure.scatterers(), xray_gradients = self.g, u_iso_gradients = energies_adp.u_iso_gradients * self.weights.w) else: energies_adp.u_aniso_gradients *= self.weights.w if(energies_adp.u_iso_gradients is not None): energies_adp.u_iso_gradients *= self.weights.w xray.minimization.add_gradients( scatterers = self.xray_structure.scatterers(), xray_gradients = self.g, u_aniso_gradients = energies_adp.u_aniso_gradients, u_iso_gradients = energies_adp.u_iso_gradients) energies_adp.u_aniso_gradients = None # just for safety energies_adp.u_iso_gradients = None