def electrostatics_test(b, r=3, r0=None): if r0 is None: r0 = np.sum(b.get_positions(), axis=0) / len(a) k1 = [(1, 1)[i] for i in b.get_pbc()] k3 = [(1, 3)[i] for i in b.get_pbc()] # Check multipole moments mp = MultipoleExpansion(L_MAX, r, k3, r0) mp.update(b, b.get_initial_charges()) # Store moments and field for later moments = mp.get_moments() M0_l_mp, M_L_mp = moments[1] phi_mp, E_mp = mp.get_potential_and_field(b) # Check if the field is the derivative of the potential b.set_calculator(mp) ffd, f0, err = check_forces(b) if debug: print("Finite differences forces:") print(ffd) print("Analytical forces:") print(f0) print("Error:") print(err) assert err < TOL_FOR # ffd, f0, err = check_field(b, mp) # if debug: # print "Finite differences field:" # print ffd # print "Analytical field:" # print f0 # print "Error:" # print err # # assert err < TOL_FIELD # Next neighbor shell by direct summation mp = MultipoleExpansion(L_MAX, r * r * r, k1, r0) mp.update(b, b.get_initial_charges()) phi_dir, E_dir = mp.get_potential_and_field(b) # Multipole moments from large cell, # transform symmetrically around the origin #rep = [ (r-1)/2 if i else 0 for i in b.get_pbc() ] #rep = [ (-(r-1)/2, (r-1)/2) if i else (0, 0) for i in b.get_pbc() ] rep = [(0, (r - 1) / 2)[i] for i in b.get_pbc()] rep = [((0, 0), (-(r - 1) / 2, (r - 1) / 2))[i] for i in b.get_pbc()] c = b.extended_copy(tuple(rep)) M0_l, M_L = get_moments(c.get_positions(), c.get_initial_charges(), L_MAX, r0) #print M_L #print M_L_mp err_mom0 = np.max(np.abs(M0_l - M0_l_mp)) err_mom = np.max(np.abs(M_L - M_L_mp)) if debug: print("error(mom) = ", err_mom0, err_mom) assert err_mom0 < TOL_MOM[r] assert err_mom < TOL_MOM[r] # Compare fields and potentials obtained by the multipole expansion # and from direct summation err_phi = np.max(np.abs(phi_mp - phi_dir)) err_E = np.max(np.abs(E_mp - E_dir)) if debug: print("error(phi) = ", err_phi) print("error(E) = ", err_E) assert err_phi < TOL_PHI[r] assert err_E < TOL_E[r]
for calc in [calc2, calc3]: atoms.set_calculator(calc) # Relax (twist) the structure q = optimize.FIRE(atoms, trajectory='polyethene.trj', logfile=None) q.run(fmax=0.5) # Displace atoms from their equilibrium positions and check forces atoms.rattle(0.1) # Check electrostatics only # atoms.set_initial_charges([1.0,1.0,1.0,1.0,-1.0,-1.0,-1.0,-1.0]) # atoms.set_calculator(calc.st.es) # Check forces from finite differences ffd, f0, err = check_forces(atoms, dx=1e-6) if debug: print "Finite differences forces:" print ffd print "Analytical forces:" print f0 print "Difference:" print abs(ffd - f0) print "Error:" print err assert err < 1e-5 # Check energy conservation from a molecular dynamics run assert check_energy_conservation(atoms, dt=0.25 * units.fs,
for calc in [ calc2, calc3 ]: atoms.set_calculator(calc) # Relax (twist) the structure q = optimize.FIRE(atoms,trajectory='polyethene.trj',logfile=None) q.run(fmax=0.5) # Displace atoms from their equilibrium positions and check forces atoms.rattle(0.1) # Check electrostatics only # atoms.set_initial_charges([1.0,1.0,1.0,1.0,-1.0,-1.0,-1.0,-1.0]) # atoms.set_calculator(calc.st.es) # Check forces from finite differences ffd, f0, err = check_forces(atoms, dx=1e-6) if debug: print "Finite differences forces:" print ffd print "Analytical forces:" print f0 print "Difference:" print abs(ffd-f0) print "Error:" print err assert err < 1e-5 # Check energy conservation from a molecular dynamics run assert check_energy_conservation(atoms,dt=0.25*units.fs,steps=100, tol=0.01,plot=debug)
def electrostatics_test(b, r=3, r0=None): if r0 is None: r0 = np.sum(b.get_positions(), axis=0)/len(a) k1 = [ (1,1)[i] for i in b.get_pbc() ] k3 = [ (1,3)[i] for i in b.get_pbc() ] # Check multipole moments mp = MultipoleExpansion(L_MAX, r, k3, r0) mp.update(b, b.get_initial_charges()) # Store moments and field for later moments = mp.get_moments() M0_l_mp, M_L_mp = moments[1] phi_mp, E_mp = mp.get_potential_and_field(b) # Check if the field is the derivative of the potential b.set_calculator(mp) ffd, f0, err = check_forces(b) if debug: print "Finite differences forces:" print ffd print "Analytical forces:" print f0 print "Error:" print err assert err < TOL_FOR # ffd, f0, err = check_field(b, mp) # if debug: # print "Finite differences field:" # print ffd # print "Analytical field:" # print f0 # print "Error:" # print err # # assert err < TOL_FIELD # Next neighbor shell by direct summation mp = MultipoleExpansion(L_MAX, r*r*r, k1, r0) mp.update(b, b.get_initial_charges()) phi_dir, E_dir = mp.get_potential_and_field(b) # Multipole moments from large cell, # transform symmetrically around the origin #rep = [ (r-1)/2 if i else 0 for i in b.get_pbc() ] #rep = [ (-(r-1)/2, (r-1)/2) if i else (0, 0) for i in b.get_pbc() ] rep = [ (0,(r-1)/2)[i] for i in b.get_pbc() ] rep = [ ((0,0),(-(r-1)/2, (r-1)/2))[i] for i in b.get_pbc() ] c = b.extended_copy(tuple(rep)) M0_l, M_L = get_moments(c.get_positions(), c.get_initial_charges(), L_MAX, r0) #print M_L #print M_L_mp err_mom0 = np.max(np.abs(M0_l-M0_l_mp)) err_mom = np.max(np.abs(M_L-M_L_mp)) if debug: print "error(mom) = ", err_mom0, err_mom assert err_mom0 < TOL_MOM[r] assert err_mom < TOL_MOM[r] # Compare fields and potentials obtained by the multipole expansion # and from direct summation err_phi = np.max(np.abs(phi_mp-phi_dir)) err_E = np.max(np.abs(E_mp-E_dir)) if debug: print "error(phi) = ", err_phi print "error(E) = ", err_E assert err_phi < TOL_PHI[r] assert err_E < TOL_E[r]