def test_inv_rotation(self): """rotate two water molecules by random angles, translate them in random space, one by oxygen other by center of mass rotate them to standard orientation and center by mass""" w1 = Water.get_standard() w2 = Water.get_standard() t1, t2, t3 = np.random.uniform( -np.pi, np.pi, [3] ) d1, d2, d3 = np.random.uniform( -np.pi, np.pi, [3] ) r1, r2 = np.random.uniform( -100, 100, [2,3] ) w1.translate_o( r1 ) w2.translate( r1 ) w1.rotate( t1, t2, t3 ) w2.rotate( d1, d2, d3 ) w1.inv_rotate( t3, t2, t1 ) w2.inv_rotate( d3, d2, d1 ) w1.center() w2.center() self.eq ( w1.com, w2.com )
def test_three_waters(self): s = System() ch1, ch2 = Chain(), Chain() ch1.chain_id = 'A' ch2.chain_id = 'B' w1, w2 = Water.get_standard( AA = True ), Water.get_standard( AA = True ) ch1.add( w1 ) ch2.add( w2 ) #After rotating and translating both waters oxygen is 2 AA apart w1.rotate( 0, np.pi/2, 0 ) w2.rotate( 0, -np.pi/2, 0 ) w1.t( -1, 0, 0 ) w2.t( 1, 0, 0 ) s.add( ch1 ) s.add( ch2 ) assert len( s.min_dist_atoms_separate_res_chain( 2.1 ) ) == 2 ch2.chain_id = 'A' assert len( s.min_dist_atoms_separate_res_chain( 2.1 ) ) == 0
def test_dist_to_mol(self): wat1, wat2 = Water.get_standard(), Water.get_standard() wat1.translate([0, 0, 0]) wat2.translate([0, 0, 1]) self.eq(np.linalg.norm(wat1.dist_to_mol(wat2)), 1, decimal=7) wat1.translate([0, 0, 0]) wat2.translate([1, 1, 1]) self.eq(np.linalg.norm(wat1.dist_to_mol(wat2)), np.sqrt(3), decimal=7)
def test_dipole(self): w1 = Water.get_standard() w2 = Water.get_standard() w2.LoProp = True w2.o.p.q -= 1.0 w2.translate_by_r(np.array([3, 3, 3])) c = Cluster(w1, w2) d1 = c.p.d.copy() r = np.random.uniform(-10, 10, (3, )) for i in c: i.translate_by_r(r) np.testing.assert_allclose(d1, c.p.d, atol=1e-7)
def test_reflection2(self): w = Water.get_standard() w.attach_properties(force_template=True) q = w.p.q d = w.p.d.copy() Q = w.p.Q.copy() a = w.p.a.copy() b = w.p.b.copy() w.rotate( 0, np.pi / 2, 0, ) w.reflect('zy') w.rotate( 0, np.pi / 2, 0, ) np.testing.assert_allclose(q, w.p.q, atol=1e-7) np.testing.assert_allclose(d, w.p.d, atol=1e-7) np.testing.assert_allclose(Q, w.p.Q, atol=1e-7) np.testing.assert_allclose(a, w.p.a, atol=1e-7) np.testing.assert_allclose(b, w.p.b, atol=1e-7)
def test_add_mol(self): a = len(self.c) w = Water.get_standard() self.assertNotIn(w, self.c) self.c.add_mol(w) self.assertEqual(len(self.c), (a + 1)) self.assertIn(w, self.c)
def test_rotate_dip(self): w = Water.get_standard() w.translate_o(np.zeros(3)) for at in w: at.p = Property.from_template( at.name, Template().get(model='TIP3p', method='HF', basis='ANOPVDZ')) w.LoProp = True dip = 0.7870603 np.testing.assert_allclose(w.p.d, [0, 0, dip], atol=1e-7) w.rotate(np.pi / 2, 0, 0) self.eq(w.p.d, [0, 0, dip]) print "After 90 around Z-axis: counter-clock" print w.p w.rotate(0, np.pi / 2, 0) self.eq(w.p.d, [-dip, 0, 0]) print "After 90 around Y-axis: clock" print w.p.d w.rotate(0, 0, np.pi / 2) print "After 90 around Z-axis: counter-clock" print w.p.d t, r1, r2, r3 = utilz.center_and_xz(w.o.r, w.o.r + (w.h1.r - w.h2.r) / 2, w.h1.r) w.inv_rotate(r1, r2, r3)
def test_attach_both_ways(self): c1 = Cluster([Water.get_standard() for i in range(2)]) c2 = Cluster([Water.get_standard() for i in range(2)]) for w1, w2 in zip(c1, c2): t_vec, r_mat = np.random.random(3), np.random.random(3) w1.rotate(*r_mat) w2.rotate(*r_mat) w1.t(t_vec) w2.t(t_vec) np.testing.assert_allclose(w1.o.r, w2.o.r) c1.attach_properties(model='spc', force_template=True) for wat in c2: wat.attach_properties(model='spc', force_template=True) np.testing.assert_allclose(c1.p.a, c2.p.a)
def test_both_loprop_and_none(self): w1, w2, w3, w4 = [Water.get_standard() for i in range(4)] c1 = Cluster(w1, w2.t(0, 0, 5)) c2 = Cluster(w3, w4.t(0, 0, 5)) c1.attach_properties(loprop=False) c2.attach_properties(loprop=True) np.testing.assert_allclose(c1.p.d, c2.p.d)
def test_read_template(self): w1, w2 = Water.get_standard(), Water.get_standard() model, method, loprop_false, loprop_true = 'tip3p', 'b3lyp', 0, 1 euler_f = lambda x: (x.o.r, (x.h1.r - x.h2.r) / 2.0 + x.h2.r, x.h1.r) w1.attach_properties( model=model, method=method, loprop=loprop_false, euler_key=euler_f, ) w2.attach_properties( model=model, method=method, loprop=loprop_true, euler_key=euler_f, ) np.testing.assert_allclose(w1.p.d, w2.p.d)
def test_rotate_to_random(self): w = Water.get_standard() w.translate(self.w.com) t1, t2, t3 = self.w.get_euler() origin = w.com.copy() w.translate_by_r(-w.com) w.rotate(t1, t2, t3) w.translate_by_r(origin) self.eq(w.com, self.w.com)
def test_transform_by_inv_rotate(self): w = Water.get_standard() w.rotate(0, np.pi / 2, np.pi / 2) w.o.p.d = np.array([0, -1.0, 0]) t, r1, r2, r3 = utilz.center_and_xz(w.o.r, (w.h1.r - w.h2.r) / 2 + w.h2.r, w.h1.r) w.inv_rotate(r3, r2, r1) np.testing.assert_allclose(w.o.p.d, np.array([0, 0, 1]), atol=1e-10)
def test_additive_pdlist(self): """Should give only props from oxygen to h2 since there is no bond from h1 to oxygen to oxygen""" w1 = Water.get_standard() w2 = Water.get_standard().t( 0, 0, 5 ) w3 = Water.get_standard() w4 = Water.get_standard().t( 0, 0, 5 ) w1.props_from_targz( WATER_FILE_TARGZ, bonds = 0, maxl = 1) w2.props_from_targz( WATER_FILE_TARGZ, bonds = 0, maxl = 1) w3.props_from_targz( WATER_FILE_TARGZ, bonds = 1, maxl = 1) w4.props_from_targz( WATER_FILE_TARGZ, bonds = 1, maxl = 1) b1 = Cluster( w1, w2 ).get_pdlist( model = 'add', bonds = 0 ).beta( cython =1 ) b2 = Cluster( w3, w4 ).get_pdlist( model = 'add', bonds = 1 ).beta( cython =1 ) np.testing.assert_allclose( b1, b2 )
def test_rotate_around_z(self): w = Water.get_standard() w.translate(self.w.com) self.eq(w.com, self.w.com) origin = w.o.r.copy() w.translate_by_r(-w.o.r) w.rotate(np.pi / 2, 0, np.pi / 2) w.translate_by_r(origin) self.eq(w.com, self.w.com)
def setUp(self): wat = Water.get_standard() t1, t2, t3 = wat.get_euler() kw_dict = Template().get(*("TIP3P", "HF", "ANOPVDZ", True, "0.0")) for at in wat: at.p = Property.from_template(at.name, kw_dict) at.p = at.p.rotate(t1, t2, t3) # will only test this quadrupole at.Property["quadrupole"] = np.arange(6) self.wat = wat
def test_y_rotation(self): w = Water.get_standard() #rotate around y axis by 90 degree, assert w.translate(self.w.o.r) w.rotate(0, np.pi / 2, 0) w.rotate(0, np.pi / 2, 0) w.center() self.eq(w.h1.y, 0) self.eq(w.h2.y, 0)
def setUp(self): self.ut_alpha = np.random.random( (6, ) ) self.ut_beat = np.random.random( (10, ) ) self.g = Generator() self.w = Water.get_standard() self.w.translate_by_r( np.random.uniform( -10, 10, [3] ) ) self.t1 = np.random.uniform( 0, np.pi/2 ) self.t2 = np.random.uniform( 0, np.pi ) self.t3 = np.random.uniform( 0, np.pi/2 )
def test_center_water(self): w = Water.get_standard() #Move the water to random place w.translate(np.random.uniform(-100, 100, [3])) #Call center function w.center() #Assert that oxygen is in origo self.eq(w.com, [0, 0, 0], decimal=7)
def test_rotate_inv_Z(self): w = Water.get_standard() self.eq(w.h1.y, 0) self.eq(w.h2.y, 0) self.eq(w.o.r, [0, 0, 0]) #rotate around z axis by 90 degree, both hydrogens now in zy plane w.rotate(np.pi / 2, 0, 0) self.eq(w.h1.x, 0) self.eq(w.h2.x, 0) self.eq(w.o.r, [0, 0, 0])
def test_moved_get_euler(self): w = Water.get_standard() w.translate([5,5,5]) t1, t2 ,t3 = w.get_euler( lambda x: (x.o.r, (x.h1.r-x.h2.r)/2.0 + x.h2.r, x.h1.r )) w.center() self.eq( [t1, t2, t3] , np.zeros(3) ) t1, t2 ,t3 = w.get_euler(lambda x: (x.o.r, (x.h1.r-x.h2.r)/2.0 + x.h2.r, x.h1.r )) w.center() self.eq( [t1, t2, t3] , np.zeros(3) )
def test_transform_by_matrix(self): w = Water.get_standard() w.rotate(0, np.pi / 2, np.pi / 2) w.o.p.d = np.array([0, -1.0, 0]) t, r1, r2, r3 = utilz.center_and_xz(w.o.r, (w.h1.r - w.h2.r) / 2 + w.h2.r, w.h1.r) R1 = utilz.R([0, 0, 1], np.pi / 2) R2 = utilz.R([0, 1, 0], 3 * np.pi / 2) for at in w: at.p = at.p.transform_by_matrix(R1) at.p = at.p.transform_by_matrix(R2) np.testing.assert_allclose(w.o.p.d, np.array([0, 0, 1]), atol=1e-10)
def test_transfer_props_level_1(self): w = Water.get_standard() w.populate_bonds() w.attach_properties() p_ref = w.h1.p.copy_property() bond = w.h1.bonds[0] w.h1.transfer_props(level=1) np.testing.assert_allclose(bond.p.q, p_ref.q) np.testing.assert_allclose(bond.p.d, p_ref.d) np.testing.assert_allclose(bond.p.a, p_ref.a) np.testing.assert_allclose(bond.p.b, p_ref.b)
def test_translation(self): w = Water.get_standard() w.translate(self.w.o.r) self.eq(w.com, self.w.o.r) w.translate([1, 1, 1]) w.translate([0, 0, 0]) w.translate([1, 1, 1]) w.translate([1, 1, 1]) w.translate([2333, 1, 1]) w.translate([0, 0, 0]) self.eq(w.com, [0, 0, 0], decimal=7)
def test_centered_simple(self): w = Water.get_standard() w.attach_properties(loprop=0) w.rotate(*np.random.random((3, ))) w.t(np.random.uniform(0, 5, (3, ))) p_ref = w.p.copy() w.attach_properties(loprop=0) np.testing.assert_allclose(w.p.d, p_ref['dipole'], atol=1e-7) np.testing.assert_allclose(w.p.b, p_ref['beta'], atol=1e-7) np.testing.assert_allclose(w.p.a, p_ref['alpha'], atol=1e-7)
def test_centered_simple(self): w = Water.get_standard() w.attach_properties( loprop = 0 ) w.rotate( *np.random.random( (3,) )) w.t( np.random.uniform( 0, 5, (3,) )) p_ref = w.p.copy() w.attach_properties( loprop = 0 ) np.testing.assert_allclose( w.p.d, p_ref['dipole'], atol = 1e-7 ) np.testing.assert_allclose( w.p.b, p_ref['beta'], atol = 1e-7 ) np.testing.assert_allclose( w.p.a, p_ref['alpha'], atol = 1e-7 )
def test_attach_properties(self): w = Water.get_standard() t, r1, r2, r3 = utilz.center_and_xz(w.o.r, (w.h1.r - w.h2.r) / 2.0 + w.h2.r, w.h1.r) w.o.pdb_name = 'OW' w.h1.pdb_name = 'HW1' w.h2.pdb_name = 'HW2' w.attach_properties(model='TIP3P_PDB', method='B3LYP', basis='ANO631', template_key=lambda x: x.pdb_name, force_template=True) np.testing.assert_allclose(w.p.q, 0.0, atol=1e-4)
def test_reflection(self): """docstring for test_reflection""" w = Water.get_standard() w.attach_properties( force_template = True) w.rotate( 0, np.pi/3.0, 0 ) b_ref = w.p.b_proj.copy() d_ref = w.p.d.copy() w.reflect( plane = 'zy' ) np.testing.assert_allclose( w.p.q, 0.0, atol = 1e-7 ) np.testing.assert_allclose( w.p.b_proj, b_ref, atol = 1e-7 ) np.testing.assert_allclose( abs(w.p.d), abs( d_ref ), atol = 1e-5 )
def test_reflection(self): """docstring for test_reflection""" w = Water.get_standard() w.attach_properties(force_template=True) w.rotate(0, np.pi / 3.0, 0) b_ref = w.p.b_proj.copy() d_ref = w.p.d.copy() w.reflect(plane='zy') np.testing.assert_allclose(w.p.q, 0.0, atol=1e-7) np.testing.assert_allclose(w.p.b_proj, b_ref, atol=1e-7) np.testing.assert_allclose(abs(w.p.d), abs(d_ref), atol=1e-5)
def test_attach_properties(self): w = Water.get_standard() t, r1, r2, r3 = utilz.center_and_xz( w.o.r, (w.h1.r - w.h2.r)/2.0 + w.h2.r, w.h1.r ) w.o.pdb_name = 'OW' w.h1.pdb_name = 'HW1' w.h2.pdb_name = 'HW2' w.attach_properties( model = 'TIP3P_PDB', method = 'B3LYP', basis ='ANO631', template_key = lambda x: x.pdb_name, force_template = True ) np.testing.assert_allclose( w.p.q , 0.0, atol = 1e-4 )
def setUp(self): np.random.seed(111) self.ut_alpha = np.random.random((6, )) self.ut_beat = np.random.random((10, )) self.g = Generator() r = np.random.uniform(-10, 10, [3]) self.w = Water.get_standard() self.w.translate_o(r) t1 = np.random.uniform(0, np.pi) t2 = np.random.uniform(0, np.pi) t3 = np.random.uniform(0, np.pi) self.w.rotate(t1, t2, t3) assert len(self.w) == 3
def test_reflection2(self): w = Water.get_standard() w.attach_properties( force_template = True ) q = w.p.q d = w.p.d.copy() Q = w.p.Q.copy() a = w.p.a.copy() b = w.p.b.copy() w.rotate( 0, np.pi/2, 0, ) w.reflect( 'zy' ) w.rotate( 0, np.pi/2, 0, ) np.testing.assert_allclose( q, w.p.q, atol = 1e-7 ) np.testing.assert_allclose( d, w.p.d, atol = 1e-7 ) np.testing.assert_allclose( Q, w.p.Q, atol = 1e-7 ) np.testing.assert_allclose( a, w.p.a, atol = 1e-7 ) np.testing.assert_allclose( b, w.p.b, atol = 1e-7 )
def test_transfer_props(self): w = Water.get_standard() w.populate_bonds() w.props_from_targz( WATER_FILE_TARGZ, bonds = 1, maxl = 1, pol = 22, hyp = 1 ) q_ref = w.p.q a_ref = w.p.a.copy() w.o.transfer_props( 2 ) np.testing.assert_allclose( w.h1.q, 0.0, atol = 1e-7 ) np.testing.assert_allclose( w.h2.q, 0.0, atol = 1e-7 ) np.testing.assert_allclose( w.p.q, q_ref, atol = 1e-7 ) np.testing.assert_allclose( w.p.a, a_ref, atol = 1e-7 )
def test_property_beta_proj(self): w = Water.get_standard() w.translate_by_r(np.random.uniform(-10, 10, (3, ))) w.rotate(*np.random.uniform(-10, 10, (3, ))) w[0].p.d = np.random.random(3) w[0].p.b = np.random.random(10) w[1].p.d = np.random.random(3) w[1].p.b = np.random.random(10) w[2].p.d = np.random.random(3) w[2].p.b = np.random.random(10) BP = w.p.b_proj t, r1, r2, r3 = utilz.center_and_xz(w[0].r, w[0].r + (w.h1.r - w.h2.r) / 2, w.h1.r) w.translate_by_r(t) w.inv_rotate(r1, r2, r3) np.testing.assert_allclose(BP, w.p.b_proj, atol=1e-7)
def test_isotropic_alpha(self): w = Water.get_standard() w.translate_by_r(np.random.uniform(-10, 10, (3, ))) w.rotate(*np.random.uniform(-10, 10, (3, ))) w[0].p.d = np.random.random(3) w[0].p.b = np.random.random(10) w[1].p.d = np.random.random(3) w[1].p.b = np.random.random(10) w[2].p.d = np.random.random(3) w[2].p.b = np.random.random(10) iso_a = np.einsum('ii', utilz.ut2s(w.p.a)) / 3 t, r1, r2, r3 = utilz.center_and_xz(w[0].r, w[0].r + (w.h1.r - w.h2.r) / 2, w.h1.r) w.translate_by_r(t) w.inv_rotate(r1, r2, r3) np.testing.assert_allclose(iso_a, np.einsum('ii', utilz.ut2s(w.p.a)) / 3, atol=1e-7)
def test_transfer_props_asymm(self): """Should give only props from oxygen to h2 since there is no bond from h1 to oxygen to oxygen""" w = Water.get_standard() w.populate_bonds() w.props_from_targz( WATER_FILE_TARGZ, bonds = 1, maxl = 1, pol = 22, hyp = 1 ) w.h1._res_id = 2 q_ref = w.p.q q_h2_ref = w.o.p.q q_o_ref = w.h2.p.q a_ref = w.p.a.copy() w.o.transfer_props( 2 ) np.testing.assert_allclose( w.h2.q, q_h2_ref + q_o_ref , atol = 1e-7 ) np.testing.assert_allclose( w.p.q, q_ref, atol = 1e-7 ) np.testing.assert_allclose( w.p.a, a_ref, atol = 1e-7 )
def test_transfer_props_level_2(self): w = Water.get_standard() w.populate_bonds() w.attach_properties() p_ref_o = w.o.p.copy_property() p_ref_h1 = w.h1.p.copy_property() w.h1.transfer_props(level=2) np.testing.assert_allclose(w.o.p.q, (p_ref_o + p_ref_h1).q) np.testing.assert_allclose(w.o.p.d, (p_ref_o + p_ref_h1).d) np.testing.assert_allclose(w.o.p.a, (p_ref_o + p_ref_h1).a) np.testing.assert_allclose(w.o.p.b, (p_ref_o + p_ref_h1).b) np.testing.assert_allclose(np.zeros(3, ), w.h1.p.d) np.testing.assert_allclose(np.zeros(6, ), w.h1.p.a) np.testing.assert_allclose(np.zeros(3, ), w.h1.bonds[0].p.d) np.testing.assert_allclose(np.zeros(6, ), w.h1.bonds[0].p.a) np.testing.assert_allclose(np.zeros(3, ), w.o.bonds[0].p.d) np.testing.assert_allclose(np.zeros(6, ), w.o.bonds[0].p.a)
def test_get_mol(self): w = Water.get_standard() self.assertIsInstance( w, Molecule )
def test_populate_bonds(self): w = Water.get_standard() w.populate_bonds() assert len(w.o.bonds) == 2 assert len(w.h1.bonds) == 1 assert len(w.h2.bonds) == 1
def test_center_get_euler(self): w = Water.get_standard() t1, t2, t3 = w.get_euler( lambda x: (x.o.r, (x.h1.r-x.h2.r)/2.0 + x.h2.r, x.h1.r )) self.eq( [t1, t2, t3] , np.zeros(3) )