def exercise_linear_velocity_manipulations(tardy_model): for nosiet in [None, tardy_model.number_of_sites_in_each_tree()]: tardy_model.assign_random_velocities(e_kin_target=17) if (tardy_model.degrees_of_freedom == 0): assert approx_equal(tardy_model.e_kin(), 0) assert tardy_model.mean_linear_velocity( number_of_sites_in_each_tree=None) is None else: assert approx_equal(tardy_model.e_kin(), 17) mlv = tardy_model.mean_linear_velocity( number_of_sites_in_each_tree=nosiet) if (mlv is not None): assert is_above_limit(value=abs(mlv), limit=1e-3) tardy_model.subtract_from_linear_velocities( number_of_sites_in_each_tree=nosiet, value=mlv) mlv = tardy_model.mean_linear_velocity( number_of_sites_in_each_tree=nosiet) assert approx_equal(mlv, (0, 0, 0))
def exercise_linear_velocity_manipulations(tardy_model): for nosiet in [None, tardy_model.number_of_sites_in_each_tree()]: tardy_model.assign_random_velocities(e_kin_target=17) if (tardy_model.degrees_of_freedom == 0): assert approx_equal(tardy_model.e_kin(), 0) assert tardy_model.mean_linear_velocity( number_of_sites_in_each_tree=None) is None else: assert approx_equal(tardy_model.e_kin(), 17) mlv = tardy_model.mean_linear_velocity( number_of_sites_in_each_tree=nosiet) if (mlv is not None): assert is_above_limit(value=abs(mlv), limit=1e-3) tardy_model.subtract_from_linear_velocities( number_of_sites_in_each_tree=nosiet, value=mlv) mlv = tardy_model.mean_linear_velocity( number_of_sites_in_each_tree=nosiet) assert approx_equal(mlv, (0,0,0))
def run_call_back(flags, space_group_info): structure = set_up_random_structure(space_group_info) if (flags.Verbose): structure.scattering_type_registry().show() rho_at_sites_from_phenix_fft_map = map_value_at_sites(structure) rho_at_sites_calculated = map_value_at_sites_calculated(structure) for scatterer, rf, rc in zip(structure.scatterers(), rho_at_sites_from_phenix_fft_map, rho_at_sites_calculated): if (flags.Verbose): print numstr(scatterer.site), "%.3f" % rf, "%.3f" % rc from scitbx.array_family import flex corr = flex.linear_correlation( flex.double(rho_at_sites_from_phenix_fft_map), flex.double(rho_at_sites_calculated)) assert corr.is_well_defined cc = corr.coefficient() if (flags.Verbose): print "Correlation coefficient:", cc from libtbx.test_utils import is_above_limit assert is_above_limit(value=cc, limit=0.99) if (flags.Verbose): print
def run_call_back(flags, space_group_info): structure = set_up_random_structure(space_group_info) if (flags.Verbose): structure.scattering_type_registry().show() rho_at_sites_from_phenix_fft_map = map_value_at_sites(structure) rho_at_sites_calculated = map_value_at_sites_calculated(structure) for scatterer,rf,rc in zip( structure.scatterers(), rho_at_sites_from_phenix_fft_map, rho_at_sites_calculated): if (flags.Verbose): print numstr(scatterer.site), "%.3f" % rf, "%.3f" % rc from scitbx.array_family import flex corr = flex.linear_correlation( flex.double(rho_at_sites_from_phenix_fft_map), flex.double(rho_at_sites_calculated)) assert corr.is_well_defined cc = corr.coefficient() if (flags.Verbose): print "Correlation coefficient:", cc from libtbx.test_utils import is_above_limit assert is_above_limit(value=cc, limit=0.99) if (flags.Verbose): print
def compare_essence_and_fast_tardy_models(etm, have_singularity=False, run_minimization=False): etm = scitbx.rigid_body.essence.tardy.model( # new instance to reset q, qd labels=etm.labels, sites=etm.sites, masses=etm.masses, tardy_tree=etm.tardy_tree, potential_obj=etm.potential_obj, near_singular_hinges_angular_tolerance_deg=etm. near_singular_hinges_angular_tolerance_deg) ftm = etm_as_ftm(etm=etm) # assert list(ftm.root_indices()) == etm.root_indices() # q_packed_zero = etm.pack_q() qd_packed_zero = etm.pack_qd() # def check_packed(): e = etm.pack_q() f = ftm.pack_q() assert approx_equal(e, f) e = etm.pack_qd() f = ftm.pack_qd() assert approx_equal(e, f) check_packed() mt = flex.mersenne_twister(seed=0) q_packed_rand = mt.random_double(size=q_packed_zero.size()) * 2 - 1 qd_packed_rand = mt.random_double(size=qd_packed_zero.size()) * 2 - 1 for tm in [etm, ftm]: tm.unpack_q(q_packed=q_packed_rand) check_packed() for tm in [etm, ftm]: tm.unpack_q(q_packed=q_packed_zero) check_packed() for tm in [etm, ftm]: tm.unpack_qd(qd_packed=qd_packed_rand) check_packed() for tm in [etm, ftm]: tm.unpack_qd(qd_packed=qd_packed_zero) check_packed() for tm in [etm, ftm]: tm.unpack_q(q_packed=q_packed_rand) check_packed() for tm in [etm, ftm]: tm.unpack_qd(qd_packed=qd_packed_rand) check_packed() # fnosiet = ftm.number_of_sites_in_each_tree() assert list(fnosiet) == etm.number_of_sites_in_each_tree() e = etm.sum_of_masses_in_each_tree() f = ftm.sum_of_masses_in_each_tree() assert approx_equal(e, f) # def check_mean_linear_velocity(): e = etm.mean_linear_velocity(number_of_sites_in_each_tree=None) for f in [ ftm.mean_linear_velocity(number_of_sites_in_each_tree=None), ftm.mean_linear_velocity(number_of_sites_in_each_tree=fnosiet) ]: if (e is None): assert f is None else: assert approx_equal(e, f) check_mean_linear_velocity() value = matrix.col(mt.random_double(size=3) * 2 - 1) for tm in [etm, ftm]: tm.subtract_from_linear_velocities(number_of_sites_in_each_tree=None, value=value) check_mean_linear_velocity() for tm in [etm, ftm]: tm.subtract_from_linear_velocities( number_of_sites_in_each_tree=fnosiet, value=value) check_mean_linear_velocity() # e = etm.sites_moved() f = ftm.sites_moved() assert approx_equal(e, f) e = etm.e_pot() f = ftm.e_pot() assert approx_equal(e, f) e = etm.d_e_pot_d_sites() f = ftm.d_e_pot_d_sites() assert approx_equal(e, f) e = etm.d_e_pot_d_q_packed() f = ftm.d_e_pot_d_q_packed() if (e.size() == 0): assert f.size() == 0 else: e_max_abs = flex.max(flex.abs(e)) if (etm.potential_obj is None): assert is_below_limit(value=e_max_abs, limit=1e-10) else: # note: e_max_abs is random generator dependent # by change much smaller e_max_abs are possible assert is_above_limit(value=e_max_abs, limit=0.1) assert approx_equal(e, f) # e = etm.e_kin() f = ftm.e_kin() assert approx_equal(e, f) e = etm.e_tot() f = ftm.e_tot() assert approx_equal(e, f) etm.reset_e_kin(e_kin_target=1.234) ftm.reset_e_kin(e_kin_target=1.234) e = etm.e_kin() if (etm.degrees_of_freedom == 0): assert approx_equal(e, 0) else: assert approx_equal(e, 1.234) f = ftm.e_kin() assert approx_equal(e, f) try: ftm.reset_e_kin(e_kin_target=1, e_kin_epsilon=0) except RuntimeError, e: assert str(e).find("e_kin_epsilon > 0") > 0
def compare_essence_and_fast_tardy_models( etm, have_singularity=False, run_minimization=False): etm = scitbx.rigid_body.essence.tardy.model( # new instance to reset q, qd labels=etm.labels, sites=etm.sites, masses=etm.masses, tardy_tree=etm.tardy_tree, potential_obj=etm.potential_obj, near_singular_hinges_angular_tolerance_deg= etm.near_singular_hinges_angular_tolerance_deg) ftm = etm_as_ftm(etm=etm) # assert list(ftm.root_indices()) == etm.root_indices() # q_packed_zero = etm.pack_q() qd_packed_zero = etm.pack_qd() # def check_packed(): e = etm.pack_q() f = ftm.pack_q() assert approx_equal(e, f) e = etm.pack_qd() f = ftm.pack_qd() assert approx_equal(e, f) check_packed() mt = flex.mersenne_twister(seed=0) q_packed_rand = mt.random_double(size=q_packed_zero.size())*2-1 qd_packed_rand = mt.random_double(size=qd_packed_zero.size())*2-1 for tm in [etm, ftm]: tm.unpack_q(q_packed=q_packed_rand) check_packed() for tm in [etm, ftm]: tm.unpack_q(q_packed=q_packed_zero) check_packed() for tm in [etm, ftm]: tm.unpack_qd(qd_packed=qd_packed_rand) check_packed() for tm in [etm, ftm]: tm.unpack_qd(qd_packed=qd_packed_zero) check_packed() for tm in [etm, ftm]: tm.unpack_q(q_packed=q_packed_rand) check_packed() for tm in [etm, ftm]: tm.unpack_qd(qd_packed=qd_packed_rand) check_packed() # fnosiet = ftm.number_of_sites_in_each_tree() assert list(fnosiet) == etm.number_of_sites_in_each_tree() e = etm.sum_of_masses_in_each_tree() f = ftm.sum_of_masses_in_each_tree() assert approx_equal(e, f) # def check_mean_linear_velocity(): e = etm.mean_linear_velocity(number_of_sites_in_each_tree=None) for f in [ftm.mean_linear_velocity(number_of_sites_in_each_tree=None), ftm.mean_linear_velocity(number_of_sites_in_each_tree=fnosiet)]: if (e is None): assert f is None else: assert approx_equal(e, f) check_mean_linear_velocity() value = matrix.col(mt.random_double(size=3)*2-1) for tm in [etm, ftm]: tm.subtract_from_linear_velocities( number_of_sites_in_each_tree=None, value=value) check_mean_linear_velocity() for tm in [etm, ftm]: tm.subtract_from_linear_velocities( number_of_sites_in_each_tree=fnosiet, value=value) check_mean_linear_velocity() # e = etm.sites_moved() f = ftm.sites_moved() assert approx_equal(e, f) e = etm.e_pot() f = ftm.e_pot() assert approx_equal(e, f) e = etm.d_e_pot_d_sites() f = ftm.d_e_pot_d_sites() assert approx_equal(e, f) e = etm.d_e_pot_d_q_packed() f = ftm.d_e_pot_d_q_packed() if (e.size() == 0): assert f.size() == 0 else: e_max_abs = flex.max(flex.abs(e)) if (etm.potential_obj is None): assert is_below_limit(value=e_max_abs, limit=1e-10) else: # note: e_max_abs is random generator dependent # by change much smaller e_max_abs are possible assert is_above_limit(value=e_max_abs, limit=0.1) assert approx_equal(e, f) # e = etm.e_kin() f = ftm.e_kin() assert approx_equal(e, f) e = etm.e_tot() f = ftm.e_tot() assert approx_equal(e, f) etm.reset_e_kin(e_kin_target=1.234) ftm.reset_e_kin(e_kin_target=1.234) e = etm.e_kin() if (etm.degrees_of_freedom == 0): assert approx_equal(e, 0) else: assert approx_equal(e, 1.234) f = ftm.e_kin() assert approx_equal(e, f) try: ftm.reset_e_kin(e_kin_target=1, e_kin_epsilon=0) except RuntimeError, e: assert str(e).find("e_kin_epsilon > 0") > 0
def compare_essence_and_fast_tardy_models(etm, have_singularity=False, run_minimization=False): etm = scitbx.rigid_body.essence.tardy.model( # new instance to reset q, qd labels=etm.labels, sites=etm.sites, masses=etm.masses, tardy_tree=etm.tardy_tree, potential_obj=etm.potential_obj, near_singular_hinges_angular_tolerance_deg=etm. near_singular_hinges_angular_tolerance_deg) ftm = etm_as_ftm(etm=etm) # assert list(ftm.root_indices()) == etm.root_indices() # q_packed_zero = etm.pack_q() qd_packed_zero = etm.pack_qd() # def check_packed(): e = etm.pack_q() f = ftm.pack_q() assert approx_equal(e, f) e = etm.pack_qd() f = ftm.pack_qd() assert approx_equal(e, f) check_packed() mt = flex.mersenne_twister(seed=0) q_packed_rand = mt.random_double(size=q_packed_zero.size()) * 2 - 1 qd_packed_rand = mt.random_double(size=qd_packed_zero.size()) * 2 - 1 for tm in [etm, ftm]: tm.unpack_q(q_packed=q_packed_rand) check_packed() for tm in [etm, ftm]: tm.unpack_q(q_packed=q_packed_zero) check_packed() for tm in [etm, ftm]: tm.unpack_qd(qd_packed=qd_packed_rand) check_packed() for tm in [etm, ftm]: tm.unpack_qd(qd_packed=qd_packed_zero) check_packed() for tm in [etm, ftm]: tm.unpack_q(q_packed=q_packed_rand) check_packed() for tm in [etm, ftm]: tm.unpack_qd(qd_packed=qd_packed_rand) check_packed() # fnosiet = ftm.number_of_sites_in_each_tree() assert list(fnosiet) == etm.number_of_sites_in_each_tree() e = etm.sum_of_masses_in_each_tree() f = ftm.sum_of_masses_in_each_tree() assert approx_equal(e, f) # def check_mean_linear_velocity(): e = etm.mean_linear_velocity(number_of_sites_in_each_tree=None) for f in [ ftm.mean_linear_velocity(number_of_sites_in_each_tree=None), ftm.mean_linear_velocity(number_of_sites_in_each_tree=fnosiet) ]: if (e is None): assert f is None else: assert approx_equal(e, f) check_mean_linear_velocity() value = matrix.col(mt.random_double(size=3) * 2 - 1) for tm in [etm, ftm]: tm.subtract_from_linear_velocities(number_of_sites_in_each_tree=None, value=value) check_mean_linear_velocity() for tm in [etm, ftm]: tm.subtract_from_linear_velocities( number_of_sites_in_each_tree=fnosiet, value=value) check_mean_linear_velocity() # e = etm.sites_moved() f = ftm.sites_moved() assert approx_equal(e, f) e = etm.e_pot() f = ftm.e_pot() assert approx_equal(e, f) e = etm.d_e_pot_d_sites() f = ftm.d_e_pot_d_sites() assert approx_equal(e, f) e = etm.d_e_pot_d_q_packed() f = ftm.d_e_pot_d_q_packed() if (e.size() == 0): assert f.size() == 0 else: e_max_abs = flex.max(flex.abs(e)) if (etm.potential_obj is None): assert is_below_limit(value=e_max_abs, limit=1e-10) else: # note: e_max_abs is random generator dependent # by change much smaller e_max_abs are possible assert is_above_limit(value=e_max_abs, limit=0.1) assert approx_equal(e, f) # e = etm.e_kin() f = ftm.e_kin() assert approx_equal(e, f) e = etm.e_tot() f = ftm.e_tot() assert approx_equal(e, f) etm.reset_e_kin(e_kin_target=1.234) ftm.reset_e_kin(e_kin_target=1.234) e = etm.e_kin() if (etm.degrees_of_freedom == 0): assert approx_equal(e, 0) else: assert approx_equal(e, 1.234) f = ftm.e_kin() assert approx_equal(e, f) try: ftm.reset_e_kin(e_kin_target=1, e_kin_epsilon=0) except RuntimeError as e: assert str(e).find("e_kin_epsilon > 0") > 0 else: raise Exception_expected etm.assign_zero_velocities() ftm.assign_zero_velocities() assert etm.e_kin() == 0 assert ftm.e_kin() == 0 random.seed(0) etm.assign_random_velocities() random.seed(0) ftm.assign_random_velocities() e = etm.e_kin() f = ftm.e_kin() assert approx_equal(e, f) # mt = flex.mersenne_twister(seed=0) qdd_rand_array = [] for body in etm.bodies: qdd_rand_array.append( matrix.col( mt.random_double(size=body.joint.degrees_of_freedom) * 2 - 1)) tau_rand_array = [] for body in etm.bodies: tau_rand_array.append( matrix.col( mt.random_double(size=body.joint.degrees_of_freedom) * 2 - 1)) f_ext_rand_array = [] for ib in range(len(etm.bodies)): f_ext_rand_array.append(matrix.col(mt.random_double(size=6) * 2 - 1)) grav_accn_rand = matrix.col(mt.random_double(size=6) * 2 - 1) def pack_array(array, packed_size=None): result = flex.double() if (packed_size is not None): result.reserve(packed_size) for sub in array: result.extend(flex.double(sub)) if (packed_size is not None): assert result.size() == packed_size return result qdd_rand_packed = pack_array(array=qdd_rand_array, packed_size=etm.degrees_of_freedom) tau_rand_packed = pack_array(array=tau_rand_array, packed_size=etm.degrees_of_freedom) f_ext_rand_packed = pack_array(array=f_ext_rand_array, packed_size=len(etm.bodies) * 6) def etm_inverse_dynamics_packed(qdd_array=None, f_ext_array=None, grav_accn=None): return pack_array(array=etm.inverse_dynamics(qdd_array=qdd_array, f_ext_array=f_ext_array, grav_accn=grav_accn), packed_size=etm.degrees_of_freedom) def etm_forward_dynamics_ab_packed(tau_array=None, f_ext_array=None, grav_accn=None): return pack_array(array=etm.forward_dynamics_ab( tau_array=tau_array, f_ext_array=f_ext_array, grav_accn=grav_accn), packed_size=etm.degrees_of_freedom) # e = etm_inverse_dynamics_packed(qdd_array=qdd_rand_array) f = ftm.inverse_dynamics_packed(qdd_packed=qdd_rand_packed) assert approx_equal(e, f) e = etm_inverse_dynamics_packed(f_ext_array=f_ext_rand_array) f = ftm.inverse_dynamics_packed(f_ext_packed=f_ext_rand_packed) assert approx_equal(e, f) e = etm_inverse_dynamics_packed(grav_accn=grav_accn_rand) f = ftm.inverse_dynamics_packed(grav_accn=flex.double(grav_accn_rand)) assert approx_equal(e, f) for qdd_array, qdd_packed in [(None, None), (qdd_rand_array, qdd_rand_packed)]: for f_ext_array, f_ext_packed in [(None, None), (f_ext_rand_array, f_ext_rand_packed) ]: for grav_accn in [None, grav_accn_rand]: if ([qdd_array, f_ext_array, grav_accn].count(None) >= 2): continue # exercised above already e = etm_inverse_dynamics_packed(qdd_array=qdd_rand_array, f_ext_array=f_ext_rand_array, grav_accn=grav_accn_rand) f = ftm.inverse_dynamics_packed( qdd_packed=qdd_rand_packed, f_ext_packed=f_ext_rand_packed, grav_accn=flex.double(grav_accn_rand)) assert approx_equal(e, f) # e = etm_forward_dynamics_ab_packed() f = ftm.forward_dynamics_ab_packed() assert approx_equal(e, f) e = etm_forward_dynamics_ab_packed(tau_array=tau_rand_array) f = ftm.forward_dynamics_ab_packed(tau_packed=tau_rand_packed) assert approx_equal(e, f) e = etm_forward_dynamics_ab_packed(f_ext_array=f_ext_rand_array) f = ftm.forward_dynamics_ab_packed(f_ext_packed=f_ext_rand_packed) assert approx_equal(e, f) e = etm_forward_dynamics_ab_packed(grav_accn=grav_accn_rand) f = ftm.forward_dynamics_ab_packed(grav_accn=flex.double(grav_accn_rand)) assert approx_equal(e, f) for tau_array, tau_packed in [(None, None), (tau_rand_array, tau_rand_packed)]: for f_ext_array, f_ext_packed in [(None, None), (f_ext_rand_array, f_ext_rand_packed) ]: for grav_accn in [None, grav_accn_rand]: if (grav_accn is None): grav_accn_f = None else: grav_accn_f = flex.double(grav_accn) qdd_array_e = etm.forward_dynamics_ab(tau_array=tau_array, f_ext_array=f_ext_array, grav_accn=grav_accn) qdd_packed_e = pack_array(array=qdd_array_e, packed_size=etm.degrees_of_freedom) qdd_packed_f = ftm.forward_dynamics_ab_packed( tau_packed=tau_packed, f_ext_packed=f_ext_packed, grav_accn=grav_accn_f) assert approx_equal(qdd_packed_e, qdd_packed_f) tau2_array_e = etm.inverse_dynamics(qdd_array=qdd_array_e, f_ext_array=f_ext_array, grav_accn=grav_accn) tau2_packed_e = pack_array(array=tau2_array_e, packed_size=etm.degrees_of_freedom) tau2_packed_f = ftm.inverse_dynamics_packed( qdd_packed=qdd_packed_f, f_ext_packed=f_ext_packed, grav_accn=grav_accn_f) assert approx_equal(tau2_packed_e, tau2_packed_f) if (etm.degrees_of_freedom == 0): assert tau2_packed_e.size() == 0 elif (not have_singularity): if (tau_packed is None): assert is_below_limit(flex.max( flex.abs(tau2_packed_e)), 0, eps=1e-5) else: assert approx_equal(tau2_packed_e, tau_packed, eps=1e-5) qdd2_array_e = etm.forward_dynamics_ab(tau_array=tau2_array_e, f_ext_array=f_ext_array, grav_accn=grav_accn) qdd2_packed_e = pack_array(array=qdd2_array_e, packed_size=etm.degrees_of_freedom) qdd2_packed_f = ftm.forward_dynamics_ab_packed( tau_packed=tau2_packed_f, f_ext_packed=f_ext_packed, grav_accn=grav_accn_f) assert approx_equal(qdd2_packed_e, qdd2_packed_f) assert approx_equal(qdd2_packed_e, qdd_packed_e, eps=1e-4) # def check_qdd(): e = pack_array(array=etm.qdd_array(), packed_size=etm.degrees_of_freedom) f = ftm.qdd_packed() assert approx_equal(e, f) check_qdd() ftm.flag_positions_as_changed() assert not ftm.sites_moved_is_cached() assert not ftm.qdd_array_is_cached() ftm.sites_moved() assert ftm.sites_moved_is_cached() assert not ftm.qdd_array_is_cached() check_qdd() assert ftm.sites_moved_is_cached() assert ftm.qdd_array_is_cached() ftm.unpack_q(q_packed=ftm.pack_q()) assert not ftm.sites_moved_is_cached() assert not ftm.qdd_array_is_cached() ftm.qdd_packed() if (ftm.potential_obj is None): assert not ftm.sites_moved_is_cached() ftm.sites_moved() assert ftm.sites_moved_is_cached() assert ftm.qdd_array_is_cached() ftm.unpack_qd(qd_packed=ftm.pack_qd()) assert ftm.sites_moved_is_cached() assert not ftm.qdd_array_is_cached() check_qdd() # delta_t = 0.1234 etm.dynamics_step(delta_t=delta_t) ftm.dynamics_step(delta_t=delta_t) assert not ftm.sites_moved_is_cached() assert not ftm.qdd_array_is_cached() check_packed() check_qdd() # etm.assign_zero_velocities() ftm.assign_zero_velocities() e = etm_inverse_dynamics_packed(f_ext_array=f_ext_rand_array) f = ftm.inverse_dynamics_packed(f_ext_packed=f_ext_rand_packed) assert approx_equal(e, f) e_ref = e e = pack_array(array=etm.f_ext_as_tau(f_ext_array=f_ext_array), packed_size=etm.degrees_of_freedom) assert approx_equal(e_ref, e) f = ftm.f_ext_as_tau_packed(f_ext_packed=f_ext_packed) assert approx_equal(e_ref, f) # if (run_minimization): check_packed() etm.minimization(max_iterations=3) ftm.minimization(max_iterations=3) check_packed()