def test_computeForces(): """""" box = md.Box(0., 0., 5. * md.hcp.uc_centered_a, 0.5 * md.hcp.uc_centered_b) atoms = md.MD(box, cutoff=2.5) if __plot: cmn.figure() cmn.plotBox(box) cmn.plotAtoms(atoms.x, atoms.y, radius=atoms.radius) cmn.plt.show() atoms.buildVerletLists() print(atoms.vl) cpp.computeForces( atoms.x, atoms.y # atom positions , atoms.vl.vl_size, atoms.vl.vl_list # linearized verlet list data structure , atoms.ax, atoms.ay # atom forces ) print(f'ax={atoms.ax}') print(f'ay={atoms.ay}') r = md.hcp.uc_centered_a r01sq = r**2 r02sq = (2 * r)**2 fx01 = lj.force_factor(r01sq) * r fx02 = lj.force_factor(r02sq) * 2 * r expected = np.array([fx01, fx01, fx01, fx01, 0.00]) \ + np.array([fx02, fx02, fx02, 0.00, 0.00]) \ - np.array([0.00, fx01, fx01, fx01, fx01]) \ - np.array([0.00, 0.00, fx02, fx02, fx02]) assert np.all(atoms.ax == expected) assert np.all(atoms.ay == np.zeros((atoms.n_atoms, ), dtype=float))
def test_force_is_derivative_of_potential(): n = 1000 # Generate n random numbers in ]0,5*R0] # np.random.random generate numbers in [ rij = (5*R0)*(1.0 - np.random.random(n)) fij = lj.force_factor(rij**2)*rij d = 1e-10 rij0 = rij - d vij0 = lj.potential(rij0**2) rij1 = rij + d vij1 = lj.potential(rij1**2) dvij = (vij1 - vij0)/(2*d) for i in range(n): print(f'{i} {fij[i]} == {dvij[i]} {np.abs(fij[i]-dvij[i])}') assert fij[i] == pytest.approx(dvij[i],1e-4)
def test_computeForces(): """""" box = md.Box(0., 0., 5. * md.hcp.uc_centered_a, 0.5 * md.hcp.uc_centered_b) atoms = md.MD(box, cutoff=2.5) if __plot: cmn.figure() cmn.plotBox(box) cmn.plotAtoms(atoms.x, atoms.y, radius=atoms.radius) cmn.plt.show() atoms.buildVerletLists() print(atoms.vl) energy = atoms.computeForces() r = md.hcp.uc_centered_a r01sq = r**2 r02sq = (2 * r)**2 fx01 = lj.force_factor(r01sq) * r fx02 = lj.force_factor(r02sq) * 2 * r expected = np.array([fx01, fx01, fx01, fx01, 0.00]) \ + np.array([fx02, fx02, fx02, 0.00, 0.00]) \ - np.array([0.00, fx01, fx01, fx01, fx01]) \ - np.array([0.00, 0.00, fx02, fx02, fx02]) assert np.all(atoms.ax == expected) print(atoms.ay) assert np.all(atoms.ay == np.zeros((atoms.n_atoms, ), dtype=float))
def test_zero_force_R0(): """verify that the force magnitude is zero at R0.""" rij2 = R0**2 fij = lj.force_factor(rij2) # account for round-off error R0**6 is not exactly 5, although R0 is defined as 2**1/6 assert fij == pytest.approx(0.0, 5e-16)