def fd_rotation(fmodel, e, convention): grads = [] for shift in [[e, 0, 0], [0, e, 0], [0, 0, e]]: xrs1 = fmodel.xray_structure.deep_copy_scatterers() xrs2 = fmodel.xray_structure.deep_copy_scatterers() fm = fmodel.deep_copy() # rot_obj = scitbx.rigid_body.euler(phi=shift[0], psi=shift[1], the=shift[2], convention=convention) selections = [flex.bool(xrs1.scatterers().size(), True)] xrs_g1_1 = mmtbx.refinement.rigid_body.apply_transformation( xray_structure=xrs1, rotation_matrices=[rot_obj.rot_mat()], translation_vectors=[(0.0, 0.0, 0.0)], selections=selections) # rot_obj = scitbx.rigid_body.euler(phi=-shift[0], psi=-shift[1], the=-shift[2], convention=convention) selections = [flex.bool(xrs1.scatterers().size(), True)] xrs_g1_2 = mmtbx.refinement.rigid_body.apply_transformation( xray_structure=xrs2, rotation_matrices=[rot_obj.rot_mat()], translation_vectors=[(0.0, 0.0, 0.0)], selections=selections) fm.update_xray_structure(xray_structure=xrs_g1_1, update_f_calc=True) t1 = fm.target_functor()().target_work() fm.update_xray_structure(xray_structure=xrs_g1_2, update_f_calc=True) t2 = fm.target_functor()().target_work() grads.append((t1 - t2) / (2 * e * math.pi / 180)) return grads
def fd_translation(fmodel, e): grads = [] for shift in [[e, 0, 0], [0, e, 0], [0, 0, e]]: xrs1 = fmodel.xray_structure.deep_copy_scatterers() xrs2 = fmodel.xray_structure.deep_copy_scatterers() fm = fmodel.deep_copy() xrs_g1_1 = xrs1.translate(x=shift[0], y=shift[1], z=shift[2]) xrs_g1_2 = xrs2.translate(x=-shift[0], y=-shift[1], z=-shift[2]) fm.update_xray_structure(xray_structure=xrs_g1_1, update_f_calc=True) t1 = fm.target_functor()().target_work() fm.update_xray_structure(xray_structure=xrs_g1_2, update_f_calc=True) t2 = fm.target_functor()().target_work() grads.append((t1 - t2) / (2 * e)) return grads
def fd_translation(fmodel, e): grads = [] for shift in [[e,0,0],[0,e,0],[0,0,e]]: xrs1 = fmodel.xray_structure.deep_copy_scatterers() xrs2 = fmodel.xray_structure.deep_copy_scatterers() fm = fmodel.deep_copy() xrs_g1_1 = xrs1.translate(x = shift[0], y = shift[1], z = shift[2]) xrs_g1_2 = xrs2.translate(x =-shift[0], y =-shift[1], z =-shift[2]) fm.update_xray_structure(xray_structure = xrs_g1_1, update_f_calc = True) t1 = fm.target_functor()().target_work() fm.update_xray_structure(xray_structure = xrs_g1_2, update_f_calc = True) t2 = fm.target_functor()().target_work() grads.append( (t1-t2)/(2*e) ) return grads
def finite_differences_test(): print "finite_differences_test: " fmodel = get_fmodel_from_pdb(pdb_file_name="enk_rbr.pdb", algorithm="direct", d_min=2.0, target="ls_wunit_k1") xray.set_scatterer_grad_flags( scatterers=fmodel.xray_structure.scatterers(), site=True) for convention in ["zyz", "xyz"]: rot_obj = scitbx.rigid_body.euler(phi=0, psi=0, the=0, convention=convention) size = fmodel.xray_structure.scatterers().size() selections = [flex.bool(size, True).iselection()] fmodel.xray_structure.apply_rigid_body_shift(rot=rot_obj.rot_mat(), trans=[1, 2, 3]) fmodel.update_xray_structure(update_f_calc=True) fmodel_copy = fmodel.deep_copy() centers_of_mass = [] for s in selections: xrs = fmodel_copy.xray_structure.select(s) centers_of_mass.append(xrs.center_of_mass()) tg_obj = mmtbx.refinement.rigid_body.target_and_grads( centers_of_mass=centers_of_mass, sites_cart=fmodel_copy.xray_structure.sites_cart(), target_functor=fmodel_copy.target_functor(), rot_objs=[rot_obj], selections=selections, suppress_gradients=False) assert approx_equal(tg_obj.target(), fmodel_copy.target_w()) g_rot, g_transl = tg_obj.gradients_wrt_r(), tg_obj.gradients_wrt_t() fd_transl = fd_translation(fmodel_copy, e=0.0001) assert approx_equal(list(g_transl[0]), fd_transl) fd_rot = fd_rotation(fmodel=fmodel_copy, e=0.0001, convention=convention) assert approx_equal(list(g_rot[0]), fd_rot)
def finite_differences_test(): print "finite_differences_test: " fmodel = get_fmodel_from_pdb(pdb_file_name = "enk_rbr.pdb", algorithm = "direct", d_min = 2.0, target = "ls_wunit_k1") xray.set_scatterer_grad_flags( scatterers = fmodel.xray_structure.scatterers(), site = True) for convention in ["zyz","xyz"]: rot_obj = scitbx.rigid_body.euler( phi = 0, psi = 0, the = 0, convention = convention) size = fmodel.xray_structure.scatterers().size() selections = [flex.bool(size, True).iselection()] fmodel.xray_structure.apply_rigid_body_shift( rot = rot_obj.rot_mat(), trans = [1,2,3]) fmodel.update_xray_structure(update_f_calc = True) fmodel_copy = fmodel.deep_copy() centers_of_mass = [] for s in selections: xrs = fmodel_copy.xray_structure.select(s) centers_of_mass.append(xrs.center_of_mass()) tg_obj = mmtbx.refinement.rigid_body.target_and_grads( centers_of_mass = centers_of_mass, sites_cart = fmodel_copy.xray_structure.sites_cart(), target_functor = fmodel_copy.target_functor(), rot_objs = [rot_obj], selections = selections, suppress_gradients = False) assert approx_equal(tg_obj.target(),fmodel_copy.target_w()) g_rot, g_transl = tg_obj.gradients_wrt_r(), tg_obj.gradients_wrt_t() fd_transl = fd_translation(fmodel_copy, e = 0.0001) assert approx_equal(list(g_transl[0]), fd_transl) fd_rot = fd_rotation(fmodel = fmodel_copy, e = 0.0001, convention = convention) assert approx_equal(list(g_rot[0]), fd_rot)
def fd_rotation(fmodel, e, convention): grads = [] for shift in [[e,0,0],[0,e,0],[0,0,e]]: xrs1 = fmodel.xray_structure.deep_copy_scatterers() xrs2 = fmodel.xray_structure.deep_copy_scatterers() fm = fmodel.deep_copy() # rot_obj = scitbx.rigid_body.euler(phi = shift[0], psi = shift[1], the = shift[2], convention = convention) selections = [flex.bool(xrs1.scatterers().size(), True)] xrs_g1_1 = mmtbx.refinement.rigid_body.apply_transformation( xray_structure = xrs1, rotation_matrices = [rot_obj.rot_mat()], translation_vectors = [(0.0,0.0,0.0)], selections = selections) # rot_obj = scitbx.rigid_body.euler(phi = -shift[0], psi = -shift[1], the = -shift[2], convention = convention) selections = [flex.bool(xrs1.scatterers().size(), True)] xrs_g1_2 = mmtbx.refinement.rigid_body.apply_transformation( xray_structure = xrs2, rotation_matrices = [rot_obj.rot_mat()], translation_vectors = [(0.0,0.0,0.0)], selections = selections) fm.update_xray_structure(xray_structure = xrs_g1_1, update_f_calc = True) t1 = fm.target_functor()().target_work() fm.update_xray_structure(xray_structure = xrs_g1_2, update_f_calc = True) t2 = fm.target_functor()().target_work() grads.append( (t1-t2)/(2*e*math.pi/180) ) return grads