def test_vlist_dihedral_cos_mil53(): system = get_system_mil53() part = ForcePartValence(system) for i1, i2 in system.bonds: for i0 in system.neighs1[i1]: if i0 == i2: continue for i3 in system.neighs1[i2]: if i3 == i1: continue fc = 2.1 + 0.01 * (0.3 * i1 + 0.7 * i2) part.add_term( PolyFour([0.0, -2.0 * fc, 0.0, 0.0], DihedCos(i0, i1, i2, i3))) energy = part.compute() check_energy = 0.0 for i1, i2 in system.bonds: for i0 in system.neighs1[i1]: if i0 == i2: continue for i3 in system.neighs1[i2]: if i3 == i1: continue fc = 2.1 + 0.01 * (0.3 * i1 + 0.7 * i2) delta0 = system.pos[i0] - system.pos[i1] delta1 = system.pos[i2] - system.pos[i1] delta2 = system.pos[i3] - system.pos[i2] system.cell.mic(delta0) system.cell.mic(delta1) system.cell.mic(delta2) cos = dihed_cos( [delta0, np.zeros(3, float), delta1, delta1 + delta2])[0] check_energy += -2.0 * fc * cos**2 if not abs(energy - check_energy) < 1e-8: raise AssertionError("Energy should be %10.9e, instead it is %10.9e" % (check_energy, energy))
def test_vlist_dihedral_cos_mil53(): system = get_system_mil53() part = ForcePartValence(system) for i1, i2 in system.bonds: for i0 in system.neighs1[i1]: if i0==i2: continue for i3 in system.neighs1[i2]: if i3==i1: continue fc = 2.1 + 0.01*(0.3*i1 + 0.7*i2) part.add_term(PolyFour([0.0,-2.0*fc,0.0,0.0],DihedCos(i0,i1,i2,i3))) energy = part.compute() check_energy = 0.0 for i1, i2 in system.bonds: for i0 in system.neighs1[i1]: if i0==i2: continue for i3 in system.neighs1[i2]: if i3==i1: continue fc = 2.1 + 0.01*(0.3*i1 + 0.7*i2) delta0 = system.pos[i0] - system.pos[i1] delta1 = system.pos[i2] - system.pos[i1] delta2 = system.pos[i3] - system.pos[i2] system.cell.mic(delta0) system.cell.mic(delta1) system.cell.mic(delta2) cos = dihed_cos([delta0, np.zeros(3, float), delta1, delta1+delta2])[0] check_energy += -2.0*fc*cos**2 if not abs(energy - check_energy) < 1e-8: raise AssertionError("Energy should be %10.9e, instead it is %10.9e" %(check_energy, energy))
def test_iclist_peroxide_dihedral_cos(): number_of_tests=50 for i in xrange(number_of_tests): system = get_system_peroxide() dlist = DeltaList(system) iclist = InternalCoordinateList(dlist) # The bonds are added randomly to get different situations in the delta list bonds=[] while len(bonds)<3: i0, i1 = [int(x) for x in np.random.uniform(low=0,high=4,size=2)] #pick 2 random atoms if i0==i1 or (i0,i1) in bonds or (i1,i0) in bonds: continue if (i0,i1) in system.bonds or (i1,i0) in system.bonds: iclist.add_ic(Bond(i0,i1)) bonds.append((i0,i1)) iclist.add_ic(DihedCos(0,1,2,3)) dlist.forward() iclist.forward() assert iclist.ictab[3]['kind']==3 #assert the third ic is DihedralCos assert abs(iclist.ictab[3]['value'] - dihed_cos(system.pos)[0]) < 1e-5
def test_iclist_peroxide_dihedral_cos(): number_of_tests = 50 for i in range(number_of_tests): system = get_system_peroxide() dlist = DeltaList(system) iclist = InternalCoordinateList(dlist) # The bonds are added randomly to get different situations in the delta list bonds = [] while len(bonds) < 3: i0, i1 = [ int(x) for x in np.random.uniform(low=0, high=4, size=2) ] #pick 2 random atoms if i0 == i1 or (i0, i1) in bonds or (i1, i0) in bonds: continue if (i0, i1) in system.bonds or (i1, i0) in system.bonds: iclist.add_ic(Bond(i0, i1)) bonds.append((i0, i1)) iclist.add_ic(DihedCos(0, 1, 2, 3)) dlist.forward() iclist.forward() assert iclist.ictab[3]['kind'] == 3 #assert the third ic is DihedralCos assert abs(iclist.ictab[3]['value'] - dihed_cos(system.pos)[0]) < 1e-5
def test_vlist_peroxide_dihed_cos(): number_of_tests=50 for i in range(number_of_tests): system = get_system_peroxide() dlist = DeltaList(system) iclist = InternalCoordinateList(dlist) vlist = ValenceList(iclist) bonds=[] while len(bonds)<3: i0, i1 = [int(x) for x in np.random.uniform(low=0,high=4,size=2)] #pick 2 random atoms if i0==i1 or (i0,i1) in bonds or (i1,i0) in bonds: continue if (i0,i1) in system.bonds or (i1,i0) in system.bonds: iclist.add_ic(Bond(i0,i1)) bonds.append((i0,i1)) vlist.add_term(Harmonic(1.1, -0.2 , DihedCos(0,1,2,3))) dlist.forward() iclist.forward() energy = vlist.forward() # calculate energy manually cos = dihed_cos(system.pos)[0] check_energy = 0.5*1.1*(cos+0.2)**2 assert abs(energy - check_energy) < 1e-8
def test_iclist_grad_dihedral_cos_mil53(): system = get_system_mil53() forbidden_dihedrals = [ ["O_HY","AL","O_HY","AL"], ["O_HY","AL","O_HY","H_HY"], ["O_CA","AL","O_CA","C_CA"], ] idih = -1 for i1, i2 in system.bonds: for i0 in system.neighs1[i1]: if i0==i2: continue for i3 in system.neighs1[i2]: if i3==i1: continue types = [system.get_ffatype(i0), system.get_ffatype(i1), system.get_ffatype(i2), system.get_ffatype(i3)] if types in forbidden_dihedrals or types[::-1] in forbidden_dihedrals: continue idih += 1 dlist = DeltaList(system) iclist = InternalCoordinateList(dlist) iclist.add_ic(DihedCos(i0,i1,i2,i3)) ic = iclist.ictab[0] dlist.forward() iclist.forward() ic['grad'] = 1.0 iclist.back() cos = ic['value'] grad_d0 = np.array([dlist.deltas[ic['i0']]['gx'], dlist.deltas[ic['i0']]['gy'], dlist.deltas[ic['i0']]['gz']]) grad_d1 = np.array([dlist.deltas[ic['i1']]['gx'], dlist.deltas[ic['i1']]['gy'], dlist.deltas[ic['i1']]['gz']]) grad_d2 = np.array([dlist.deltas[ic['i2']]['gx'], dlist.deltas[ic['i2']]['gy'], dlist.deltas[ic['i2']]['gz']]) delta0 = system.pos[i0] - system.pos[i1] delta1 = system.pos[i2] - system.pos[i1] delta2 = system.pos[i3] - system.pos[i2] system.cell.mic(delta0) system.cell.mic(delta1) system.cell.mic(delta2) check_cos, check_grad = dihed_cos([delta0, np.zeros(3, float), delta1, delta1+delta2],deriv=1) check_grad_d0 = check_grad[0,:] check_grad_d1 = -check_grad[0,:] - check_grad[1,:] check_grad_d2 = check_grad[3,:] if not abs(ic['value'] - check_cos) < 1e-8: raise AssertionError("Dihed cos (%s[%i],%s[%i],%s[%i],%s[%i]) should have value %10.9e, instead it is %10.9e" %( system.get_ffatype(i0),i0, system.get_ffatype(i1),i1, system.get_ffatype(i2),i2, system.get_ffatype(i3),i3, check_cos, cos )) if not np.sqrt(sum( (grad_d0 - check_grad_d0)**2 )) < 1e-8: raise AssertionError("Dihed cos (%s[%i],%s[%i],%s[%i],%s[%i]) should have delta0_grad [%12.9f,%12.9f,%12.9f], \n" "instead it is [%12.9f,%12.9f,%12.9f]" %( system.get_ffatype(i0),i0, system.get_ffatype(i1),i1, system.get_ffatype(i2),i2, system.get_ffatype(i3),i3, check_grad_d0[0], check_grad_d0[1], check_grad_d0[2], grad_d0[0], grad_d0[1], grad_d0[2], )) if not np.sqrt(sum( (grad_d1 - check_grad_d1)**2 )) < 1e-8: raise AssertionError("Dihed cos (%s[%i],%s[%i],%s[%i],%s[%i]) should have delta1_grad [%12.9f,%12.9f,%12.9f], \n" "instead it is [%12.9f,%12.9f,%12.9f]" %( system.get_ffatype(i0),i0, system.get_ffatype(i1),i1, system.get_ffatype(i2),i2, system.get_ffatype(i3),i3, check_grad_d1[0], check_grad_d1[1], check_grad_d1[2], grad_d1[0], grad_d1[1], grad_d1[2], )) if not np.sqrt(sum( (grad_d2 - check_grad_d2)**2 )) < 1e-8: raise AssertionError("Dihed cos (%s[%i],%s[%i],%s[%i],%s[%i]) should have delta2_grad [%12.9f,%12.9f,%12.9f], \n" "instead it is [%12.9f,%12.9f,%12.9f]" %( system.get_ffatype(i0),i0, system.get_ffatype(i1),i1, system.get_ffatype(i2),i2, system.get_ffatype(i3),i3, check_grad_d2[0], check_grad_d2[1], check_grad_d2[2], grad_d2[0], grad_d2[1], grad_d2[2], ))
def test_iclist_grad_dihedral_cos_mil53(): system = get_system_mil53() forbidden_dihedrals = [ ["O_HY", "AL", "O_HY", "AL"], ["O_HY", "AL", "O_HY", "H_HY"], ["O_CA", "AL", "O_CA", "C_CA"], ] idih = -1 for i1, i2 in system.bonds: for i0 in system.neighs1[i1]: if i0 == i2: continue for i3 in system.neighs1[i2]: if i3 == i1: continue types = [ system.get_ffatype(i0), system.get_ffatype(i1), system.get_ffatype(i2), system.get_ffatype(i3) ] if types in forbidden_dihedrals or types[:: -1] in forbidden_dihedrals: continue idih += 1 dlist = DeltaList(system) iclist = InternalCoordinateList(dlist) iclist.add_ic(DihedCos(i0, i1, i2, i3)) ic = iclist.ictab[0] dlist.forward() iclist.forward() ic['grad'] = 1.0 iclist.back() cos = ic['value'] grad_d0 = np.array([ dlist.deltas[ic['i0']]['gx'], dlist.deltas[ic['i0']]['gy'], dlist.deltas[ic['i0']]['gz'] ]) grad_d1 = np.array([ dlist.deltas[ic['i1']]['gx'], dlist.deltas[ic['i1']]['gy'], dlist.deltas[ic['i1']]['gz'] ]) grad_d2 = np.array([ dlist.deltas[ic['i2']]['gx'], dlist.deltas[ic['i2']]['gy'], dlist.deltas[ic['i2']]['gz'] ]) delta0 = system.pos[i0] - system.pos[i1] delta1 = system.pos[i2] - system.pos[i1] delta2 = system.pos[i3] - system.pos[i2] system.cell.mic(delta0) system.cell.mic(delta1) system.cell.mic(delta2) check_cos, check_grad = dihed_cos( [delta0, np.zeros(3, float), delta1, delta1 + delta2], deriv=1) check_grad_d0 = check_grad[0, :] check_grad_d1 = -check_grad[0, :] - check_grad[1, :] check_grad_d2 = check_grad[3, :] if not abs(ic['value'] - check_cos) < 1e-8: raise AssertionError( "Dihed cos (%s[%i],%s[%i],%s[%i],%s[%i]) should have value %10.9e, instead it is %10.9e" % (system.get_ffatype(i0), i0, system.get_ffatype(i1), i1, system.get_ffatype(i2), i2, system.get_ffatype(i3), i3, check_cos, cos)) if not np.sqrt(sum((grad_d0 - check_grad_d0)**2)) < 1e-8: raise AssertionError( "Dihed cos (%s[%i],%s[%i],%s[%i],%s[%i]) should have delta0_grad [%12.9f,%12.9f,%12.9f], \n" "instead it is [%12.9f,%12.9f,%12.9f]" % ( system.get_ffatype(i0), i0, system.get_ffatype(i1), i1, system.get_ffatype(i2), i2, system.get_ffatype(i3), i3, check_grad_d0[0], check_grad_d0[1], check_grad_d0[2], grad_d0[0], grad_d0[1], grad_d0[2], )) if not np.sqrt(sum((grad_d1 - check_grad_d1)**2)) < 1e-8: raise AssertionError( "Dihed cos (%s[%i],%s[%i],%s[%i],%s[%i]) should have delta1_grad [%12.9f,%12.9f,%12.9f], \n" "instead it is [%12.9f,%12.9f,%12.9f]" % ( system.get_ffatype(i0), i0, system.get_ffatype(i1), i1, system.get_ffatype(i2), i2, system.get_ffatype(i3), i3, check_grad_d1[0], check_grad_d1[1], check_grad_d1[2], grad_d1[0], grad_d1[1], grad_d1[2], )) if not np.sqrt(sum((grad_d2 - check_grad_d2)**2)) < 1e-8: raise AssertionError( "Dihed cos (%s[%i],%s[%i],%s[%i],%s[%i]) should have delta2_grad [%12.9f,%12.9f,%12.9f], \n" "instead it is [%12.9f,%12.9f,%12.9f]" % ( system.get_ffatype(i0), i0, system.get_ffatype(i1), i1, system.get_ffatype(i2), i2, system.get_ffatype(i3), i3, check_grad_d2[0], check_grad_d2[1], check_grad_d2[2], grad_d2[0], grad_d2[1], grad_d2[2], ))