def test_add_single_molecule(self): ''' Basic test to insure that a molecule is correctly added to a system ''' x = range(8) y = range(8, 16) z = range(16, 24) types = [0, 1, 1, 0] * 2 bonds = [[i, j] for i, j in zip(range(len(x) - 1), range(1, len(x)))] chain = molecule.ChainSegment() system = System() system.add_molecule(chain, x=x, y=y, z=z, types=types, bonds=bonds, bond_shift=True) self.assertEqual(system.nbeads, len(x)) self.assertIs(system.molecules[0], chain) self.assertIs(system.molecules[0].system, system) self.assertListEqual(system.molecules[0].indices, range(8)) self.assertListEqual(list(system.x), x) self.assertListEqual(list(system.y), y) self.assertListEqual(list(system.z), z) self.assertListEqual(list(chain.x.compressed()), x) self.assertListEqual(list(chain.y.compressed()), y) self.assertListEqual(list(chain.z.compressed()), z) self.assertListEqual(list(chain.types.compressed()), types) np.testing.assert_array_equal(system.bonds[0], np.array([1, -1, -1, -1, -1])) np.testing.assert_array_equal(system.bonds[1], np.array([0, 2, -1, -1, -1])) np.testing.assert_array_equal(system.bonds[2], np.array([1, 3, -1, -1, -1])) np.testing.assert_array_equal(system.bonds[3], np.array([2, 4, -1, -1, -1])) np.testing.assert_array_equal(system.bonds[4], np.array([3, 5, -1, -1, -1])) np.testing.assert_array_equal(system.bonds[5], np.array([4, 6, -1, -1, -1])) np.testing.assert_array_equal(system.bonds[6], np.array([5, 7, -1, -1, -1])) np.testing.assert_array_equal(system.bonds[7], np.array([6, -1, -1, -1, -1]))
def base_test(self,PT,lx,ly,lz,types=None,N=3,box_resize=None): x,y,z,t = self.make_rubix(N=N,types=types) U0 = self.calc_all_potential(x,y,z,t,lx,ly,lz,PT) system = System() system.add_beads(x=x,y=y,z=z,types=t) system.box = Box(cell_grid=(3,3,3)) system.box.lx = lx system.box.ly = ly system.box.lz = lz xarray = np.array(x,dtype=np.float) yarray = np.array(y,dtype=np.float) zarray = np.array(z,dtype=np.float) system.box.neighbor_list.build_nlist(xarray,yarray,zarray,True) system.NonBondedTable = PT PE = NonBondedPotentialEnergy(system) U1 = sum(PE.compute()) U2 = sum(PE.compute(ignore_neighbor_list=True)) if box_resize is None: return U0,U1,U2 system.box.lx = box_resize[0] system.box.ly = box_resize[1] system.box.lz = box_resize[2] U3 = self.calc_all_potential(x,y,z,t,system.box.lx,system.box.ly,system.box.lz,PT) U4 = sum(PE.compute()) U5 = sum(PE.compute(ignore_neighbor_list=True)) return U0,U1,U2,U3,U4,U5
def test_partial1(self): lx = 8 ly = 11 lz = 9 N = 10 dz = 1.1 r0 = 1.0 potential = 'Harmonic' partial_indices = [4, 3, 5] x = np.array([0.0] * N) y = np.array([0.0] * N) z = np.array([dz * i - lz / 2.0 for i in range(N)]) t = [0] * N bonds = [[i, j] for i, j in zip(range(N - 1), range(1, N))] U0 = 0 U0 += self.harmonic(dz, 1.0, r0) * (len(partial_indices) - 1 ) #inner bonds U0 += self.harmonic(dz, 1.0, r0) * (2) #outer bonds NBPT = PairTable(types=['P1'], parms=['epsilon', 'rcut', 'sigma', 'potential']) NBPT.setUnsetValues('epsilon', 1.0) NBPT.setUnsetValues('sigma', 1.0) NBPT.setUnsetValues('rcut', 2.5) NBPT.setUnsetValues('potential', 'HardSphere') BPT = PairTable(types=['P1'], parms=['k', 'r0', 'potential']) BPT.setUnsetValues('k', 1.0) BPT.setUnsetValues('r0', r0) BPT.setUnsetValues('potential', potential) system = System() system.add_beads(x=x, y=y, z=z, types=t, bonds=bonds) system.box = Box(cell_grid=(3, 3, 3)) system.box.lx = lx system.box.ly = ly system.box.lz = lz system.BondedTable = BPT system.NonBondedTable = NBPT (x, y, z), (imx, imy, imz) = system.box.wrap_positions(x=x, y=y, z=z) xarray = np.array(x, dtype=np.float) yarray = np.array(y, dtype=np.float) zarray = np.array(z, dtype=np.float) system.box.neighbor_list.build_nlist(xarray, yarray, zarray, True) PE = BondedPotentialEnergy(system) U1 = sum(PE.compute(partial_indices=partial_indices)) self.assertAlmostEqual(U0, U1, delta=0.001)
def test_pbc_homog_HS_sigma155_partial(self): lx = 10 ly = 11 lz = 10 partial_indices = [3,4,5] eps = 1.0 sig = 1.55 rcut = 2.5 PT = PairTable(types=['P1'],parms=['epsilon','rcut','sigma','potential']) PT.setUnsetValues('epsilon',eps) PT.setUnsetValues('sigma',sig) PT.setUnsetValues('rcut',rcut) PT.setUnsetValues('potential','HardSphere') N = 3 x,y,z,t = self.make_rubix(N=N,types=None) U0 = 0 dist = self.calc_dist(3,4,x,y,z,lx,ly,lz) U0+=self.hard_sphere(dist,eps,sig,rcut) dist = self.calc_dist(4,5,x,y,z,lx,ly,lz) U0+=self.hard_sphere(dist,eps,sig,rcut) dist = self.calc_dist(3,5,x,y,z,lx,ly,lz) U0+=self.hard_sphere(dist,eps,sig,rcut) for bead_i in partial_indices: for bead_j in range(len(x)): if bead_j not in partial_indices: dist = self.calc_dist(bead_i,bead_j,x,y,z,lx,ly,lz) U0+=self.hard_sphere(dist,eps,sig,rcut) system = System() system.add_beads(x=x,y=y,z=z,types=t) system.box = Box(cell_grid=(3,3,3)) system.box.lx = lx system.box.ly = ly system.box.lz = lz xarray = np.array(x,dtype=np.float) yarray = np.array(y,dtype=np.float) zarray = np.array(z,dtype=np.float) system.box.neighbor_list.build_nlist(xarray,yarray,zarray,True) system.NonBondedTable = PT PE = NonBondedPotentialEnergy(system) U1 = sum(PE.compute(partial_indices=partial_indices)) self.assertAlmostEqual(U0,U1,delta=0.001)
def base_test(self, lx, ly, lz, N, dz, potential, r0): dx = 1.0 dy = 0.5 x = np.array([i * dx for i in range(N)]) y = np.array([i * dy for i in range(N)]) z = np.array([dz * i - lz / 2.0 for i in range(N)]) t = [0] * N bonds = [[i, j] for i, j in zip(range(N - 1), range(1, N))] NBPT = PairTable(types=['P1'], parms=['epsilon', 'rcut', 'sigma', 'potential']) NBPT.setUnsetValues('epsilon', 1.0) NBPT.setUnsetValues('sigma', 1.0) NBPT.setUnsetValues('rcut', 2.5) NBPT.setUnsetValues('potential', 'HardSphere') BPT = PairTable(types=['P1'], parms=['k', 'r0', 'potential']) BPT.setUnsetValues('k', 1.0) BPT.setUnsetValues('r0', r0) BPT.setUnsetValues('potential', potential) system = System() system.add_beads(x=x, y=y, z=z, types=t, bonds=bonds) system.box = Box(cell_grid=(3, 3, 3)) system.box.lx = lx system.box.ly = ly system.box.lz = lz system.BondedTable = BPT system.NonBondedTable = NBPT (x, y, z), (imx, imy, imz) = system.box.wrap_positions(x=x, y=y, z=z) U0 = self.calc_all_potential(bonds, x, y, z, t, lx, ly, lz, BPT) xarray = np.array(x, dtype=np.float) yarray = np.array(y, dtype=np.float) zarray = np.array(z, dtype=np.float) system.box.neighbor_list.build_nlist(xarray, yarray, zarray, True) PE = BondedPotentialEnergy(system) U1 = sum(PE.compute()) return U0, U1
def test_pbc_hex_HS(self): lx = 9 ly = 9 lz = 8 nz = 4 PT = PairTable(types=['P1','P2'],parms=['epsilon','rcut','sigma','potential']) PT.setUnsetValues('epsilon',1.0) PT.setUnsetValues('sigma',1.0) PT.setUnsetValues('rcut',2.5) PT.setUnsetValues('potential','HardSphere') HS = HexagonalSurface() molData, boxData = HS.build(lx,ly,nz,diameter=1.0,topType=0,bottomType=0,middleType=1) x = molData['x'] y = molData['y'] z = molData['z'] t = molData['types'] lx = boxData['lx'] ly = boxData['ly'] U0 = self.calc_all_potential(x,y,z,t,lx,ly,lz,PT) system = System() system.add_beads(x=x,y=y,z=z,types=t) system.box = Box(cell_grid=(3,3,3)) system.box.lx = lx system.box.ly = ly system.box.lz = lz xarray = np.array(x,dtype=np.double) yarray = np.array(y,dtype=np.double) zarray = np.array(z,dtype=np.double) system.box.neighbor_list.build_nlist(xarray,yarray,zarray,True) system.NonBondedTable = PT PE = NonBondedPotentialEnergy(system) U1 = sum(PE.compute()) U2 = sum(PE.compute(ignore_neighbor_list=True)) self.assertAlmostEqual(U0,U2,delta=0.01) self.assertAlmostEqual(U0,U1,delta=0.01)
def test_add_beads(self): system = System() #Basic sanity check that beads are being added x = [1, 2, 3, 4] y = [3, 4, 5, 6] z = [7, 8, 8, 10] types = [0, 1, 1, 0] bonds = [[0, 1], [2, 3], [0, 1], [3, 0]] system.add_beads(x=x, y=y, z=z, types=types, bonds=bonds) self.assertEqual(system.nbeads, len(x)) self.assertListEqual(list(system.x), x) self.assertListEqual(list(system.y), y) self.assertListEqual(list(system.z), z) self.assertListEqual(list(system.types), types) np.testing.assert_array_equal(system.bonds[3], np.array([0, 2, -1, -1, -1])) np.testing.assert_array_equal(system.bonds[0], np.array([1, 3, -1, -1, -1])) #Check to make sure that, non-matching bead arrays are rejected x2 = [1] y2 = [3, 4] z2 = [0] types2 = [0, 1, 3] self.assertRaises(ValueError, system.add_beads, x2, y2, z2, types2) bonds = [['0', '5']] system.add_beads(x=x, y=y, z=z, types=types, bonds=bonds) self.assertEqual(system.nbeads, len(x * 2)) self.assertListEqual(list(system.x), x * 2) self.assertListEqual(list(system.y), y * 2) self.assertListEqual(list(system.z), z * 2) self.assertListEqual(list(system.types), types * 2) np.testing.assert_array_equal(system.bonds[0], np.array([1, 3, 5, -1, -1])) np.testing.assert_array_equal(system.bonds[3], np.array([0, 2, -1, -1, -1])) np.testing.assert_array_equal(system.bonds[5], np.array([0, -1, -1, -1, -1]))
def test_remove_end_molecule(self): ''' Basic test to insure that multiple molecules can be added and removed to a system. Adds two ChainSegments to the system, checks to make sure that they were added correctly, and then removes the first chain ''' x1 = range(4) y1 = range(4, 8) z1 = range(8, 12) types1 = [0, 1, 1, 0] bonds1 = [[i, j] for i, j in zip(range(len(x1) - 1), range(1, len(x1)))] chain1 = molecule.ChainSegment() x2 = range(10, 14) y2 = range(14, 18) z2 = range(18, 22) types2 = [2, 3, 3, 2] bonds2 = [[i, j] for i, j in zip(range(len(x2) - 1), range(1, len(x2)))] #add in bond between middle of first chain and beginning of second bonds2 += [['1', 0]] chain2 = molecule.ChainSegment() system = System() system.add_molecule(chain1, x=x1, y=y1, z=z1, types=types1, bonds=bonds1, bond_shift=True) system.add_molecule(chain2, x=x2, y=y2, z=z2, types=types2, bonds=bonds2, bond_shift=True) self.assertEqual(system.nbeads, len(x1 + x2)) self.assertIs(system.molecules[0], chain1) self.assertIs(system.molecules[1], chain2) self.assertIs(system.molecules[0].system, system) self.assertIs(system.molecules[1].system, system) self.assertListEqual(system.molecules[0].indices, range(4)) self.assertListEqual(system.molecules[1].indices, range(4, 8)) self.assertListEqual(list(system.x), x1 + x2) self.assertListEqual(list(system.y), y1 + y2) self.assertListEqual(list(system.z), z1 + z2) self.assertListEqual(list(system.types), types1 + types2) self.assertListEqual(list(chain1.x.compressed()), x1) self.assertListEqual(list(chain1.y.compressed()), y1) self.assertListEqual(list(chain1.z.compressed()), z1) self.assertListEqual(list(chain1.types.compressed()), types1) self.assertListEqual(list(chain2.x.compressed()), x2) self.assertListEqual(list(chain2.y.compressed()), y2) self.assertListEqual(list(chain2.z.compressed()), z2) self.assertListEqual(list(chain2.types.compressed()), types2) # self.assertSetEqual(system.bonds[0],set([1])) # self.assertSetEqual(system.bonds[1],set([0,2,4])) # self.assertSetEqual(system.bonds[2],set([1,3])) # self.assertSetEqual(system.bonds[3],set([2])) # self.assertSetEqual(system.bonds[4],set([1,5])) # self.assertSetEqual(system.bonds[5],set([4,6])) # self.assertSetEqual(system.bonds[6],set([5,7])) # self.assertSetEqual(system.bonds[7],set([6])) np.testing.assert_array_equal(system.bonds[0], np.array([1, -1, -1, -1, -1])) np.testing.assert_array_equal(system.bonds[1], np.array([0, 2, 4, -1, -1])) np.testing.assert_array_equal(system.bonds[2], np.array([1, 3, -1, -1, -1])) np.testing.assert_array_equal(system.bonds[3], np.array([2, -1, -1, -1, -1])) np.testing.assert_array_equal(system.bonds[4], np.array([1, 5, -1, -1, -1])) np.testing.assert_array_equal(system.bonds[5], np.array([4, 6, -1, -1, -1])) np.testing.assert_array_equal(system.bonds[6], np.array([5, 7, -1, -1, -1])) np.testing.assert_array_equal(system.bonds[7], np.array([6, -1, -1, -1, -1])) system.remove_molecule(molecule=chain1, remove_beads=True) self.assertEqual(system.nbeads, len(x2)) self.assertIs(system.molecules[0], chain2) self.assertIs(system.molecules[0].system, system) self.assertListEqual(system.molecules[0].indices, range(4)) self.assertListEqual(list(system.x), x2) self.assertListEqual(list(system.y), y2) self.assertListEqual(list(system.z), z2) self.assertListEqual(list(system.types), types2) self.assertListEqual(list(chain2.x.compressed()), x2) self.assertListEqual(list(chain2.y.compressed()), y2) self.assertListEqual(list(chain2.z.compressed()), z2) self.assertListEqual(list(chain2.types.compressed()), types2) # self.assertSetEqual(system.bonds[0],set([1])) # self.assertSetEqual(system.bonds[1],set([0,2])) # self.assertSetEqual(system.bonds[2],set([1,3])) # self.assertSetEqual(system.bonds[3],set([2])) np.testing.assert_array_equal(system.bonds[0], np.array([1, -1, -1, -1, -1])) np.testing.assert_array_equal(system.bonds[1], np.array([0, 2, -1, -1, -1])) np.testing.assert_array_equal(system.bonds[2], np.array([1, 3, -1, -1, -1])) np.testing.assert_array_equal(system.bonds[3], np.array([2, -1, -1, -1, -1]))
def test_distribute(self): ''' Add three chains to a system, remove the middle chain, and then add those center beads to the third chain. ''' x1 = range(4) y1 = range(4, 8) z1 = range(8, 12) types1 = [0, 1, 1, 0] bonds1 = [[i, j] for i, j in zip(range(len(x1) - 1), range(1, len(x1)))] chain1 = molecule.ChainSegment() x2 = range(10, 14) y2 = range(14, 18) z2 = range(18, 22) x3 = range(20, 24) y3 = range(24, 28) z3 = range(28, 32) types2 = [2, 3, 3, 2] bonds2 = [[i, j] for i, j in zip(range(len(x2) - 1), range(1, len(x2)))] bonds2 += [['1', 0]] types3 = [3, 4, 4, 3] bonds3 = [[i + 4, j + 4] for i, j in zip(range(len(x3) - 1), range(1, len(x3)))] x23 = x2 + x3 y23 = y2 + y3 z23 = z2 + z3 types23 = types2 + types3 bonds23 = bonds2 + bonds3 chain23 = molecule.ChainSegment() system = System() system.add_molecule(chain1, x=x1, y=y1, z=z1, types=types1, bonds=bonds1, bond_shift=True) system.add_molecule(chain23, x=x23, y=y23, z=z23, types=types23, bonds=bonds23, bond_shift=True) chain2, chain3 = chain23.distribute_by_index( [range(4, 8), range(8, 12)]) # The new molecules should be their own references self.assertIsNot(chain2, chain23) self.assertIsNot(chain3, chain23) # ..but they should be the same type self.assertIs(type(chain2), type(chain23)) self.assertIs(type(chain3), type(chain23)) self.assertEqual(system.nbeads, len(x1 + x2 + x3)) self.assertIs(system.molecules[0], chain1) self.assertIs(system.molecules[1], chain2) self.assertIs(system.molecules[2], chain3) self.assertIs(system.molecules[0].system, system) self.assertIs(system.molecules[1].system, system) self.assertIs(system.molecules[2].system, system) self.assertListEqual(system.molecules[0].indices, range(4)) self.assertListEqual(system.molecules[1].indices, range(4, 8)) self.assertListEqual(system.molecules[2].indices, range(8, 12)) self.assertListEqual(list(system.x), x1 + x2 + x3) self.assertListEqual(list(system.y), y1 + y2 + y3) self.assertListEqual(list(system.z), z1 + z2 + z3) self.assertListEqual(list(system.types), types1 + types2 + types3) self.assertListEqual(list(chain1.x.compressed()), x1) self.assertListEqual(list(chain1.y.compressed()), y1) self.assertListEqual(list(chain1.z.compressed()), z1) self.assertListEqual(list(chain1.types.compressed()), types1) self.assertListEqual(list(chain2.x.compressed()), x2) self.assertListEqual(list(chain2.y.compressed()), y2) self.assertListEqual(list(chain2.z.compressed()), z2) self.assertListEqual(list(chain2.types.compressed()), types2) self.assertListEqual(list(chain3.x.compressed()), x3) self.assertListEqual(list(chain3.y.compressed()), y3) self.assertListEqual(list(chain3.z.compressed()), z3) self.assertListEqual(list(chain3.types.compressed()), types3) # self.assertSetEqual(system.bonds[0],set([1])) # self.assertSetEqual(system.bonds[1],set([0,2,4])) # self.assertSetEqual(system.bonds[2],set([1,3])) # self.assertSetEqual(system.bonds[3],set([2])) # self.assertSetEqual(system.bonds[4],set([1,5])) # self.assertSetEqual(system.bonds[5],set([4,6])) # self.assertSetEqual(system.bonds[6],set([5,7])) # self.assertSetEqual(system.bonds[7],set([6])) # self.assertSetEqual(system.bonds[8],set([9])) # self.assertSetEqual(system.bonds[9],set([8,10])) # self.assertSetEqual(system.bonds[10],set([9,11])) # self.assertSetEqual(system.bonds[11],set([10])) np.testing.assert_array_equal(system.bonds[0], np.array([1, -1, -1, -1, -1])) np.testing.assert_array_equal(system.bonds[1], np.array([0, 2, 4, -1, -1])) np.testing.assert_array_equal(system.bonds[2], np.array([1, 3, -1, -1, -1])) np.testing.assert_array_equal(system.bonds[3], np.array([2, -1, -1, -1, -1])) np.testing.assert_array_equal(system.bonds[4], np.array([1, 5, -1, -1, -1])) np.testing.assert_array_equal(system.bonds[5], np.array([4, 6, -1, -1, -1])) np.testing.assert_array_equal(system.bonds[6], np.array([5, 7, -1, -1, -1])) np.testing.assert_array_equal(system.bonds[7], np.array([6, -1, -1, -1, -1])) np.testing.assert_array_equal(system.bonds[8], np.array([9, -1, -1, -1, -1])) np.testing.assert_array_equal(system.bonds[9], np.array([8, 10, -1, -1, -1])) np.testing.assert_array_equal(system.bonds[10], np.array([9, 11, -1, -1, -1])) np.testing.assert_array_equal(system.bonds[11], np.array([10, -1, -1, -1, -1])) for mol in system.molecule_map: self.assertIsNot(mol, system.DummyMolecule) self.assertIsNot(mol, chain23)
def test_add_indices(self): ''' Add three chains to a system, remove the middle chain, and then add those center beads to the third chain. ''' x1 = range(4) y1 = range(4, 8) z1 = range(8, 12) types1 = [0, 1, 1, 0] bonds1 = [[i, j] for i, j in zip(range(len(x1) - 1), range(1, len(x1)))] chain1 = molecule.ChainSegment() x2 = range(10, 14) y2 = range(14, 18) z2 = range(18, 22) types2 = [2, 3, 3, 2] bonds2 = [[i, j] for i, j in zip(range(len(x2) - 1), range(1, len(x2)))] #add in bond between middle of first chain and beginning of second bonds2 += [['1', 0]] chain2 = molecule.ChainSegment() x3 = range(20, 24) y3 = range(24, 28) z3 = range(28, 32) types3 = [3, 4, 4, 3] bonds3 = [[i, j] for i, j in zip(range(len(x3) - 1), range(1, len(x3)))] #add in bond between middle of first chain and beginning of second chain3 = molecule.ChainSegment() system = System() system.add_molecule(chain1, x=x1, y=y1, z=z1, types=types1, bonds=bonds1, bond_shift=True) system.add_molecule(chain2, x=x2, y=y2, z=z2, types=types2, bonds=bonds2, bond_shift=True) system.add_molecule(chain3, x=x3, y=y3, z=z3, types=types3, bonds=bonds3, bond_shift=True) #remove chain2 chain2_removed = system.remove_molecule(molecule=chain2, remove_beads=False) #add beads from chain2 to chain3 chain3.add_indices(chain2_removed.indices) self.assertIs(chain2, chain2_removed) self.assertEqual(chain3.size, 8) self.assertEqual(system.molecules[1].size, 8) self.assertEqual(system.nbeads, len(x1 + x2 + x3)) self.assertIs(system.molecules[0], chain1) self.assertIs(system.molecules[1], chain3) self.assertIs(system.molecules[0].system, system) self.assertIs(system.molecules[1].system, system) self.assertListEqual(system.molecules[0].indices, range(4)) self.assertListEqual(sorted(system.molecules[1].indices), range(4, 12)) self.assertListEqual(list(system.x), x1 + x2 + x3) self.assertListEqual(list(system.y), y1 + y2 + y3) self.assertListEqual(list(system.z), z1 + z2 + z3) self.assertListEqual(list(system.types), types1 + types2 + types3) self.assertListEqual(list(chain1.x.compressed()), x1) self.assertListEqual(list(chain1.y.compressed()), y1) self.assertListEqual(list(chain1.z.compressed()), z1) self.assertListEqual(list(chain1.types.compressed()), types1) self.assertListEqual(list(chain3.x.compressed()), x2 + x3) #combined molecule1 and molecule2 self.assertListEqual(list(chain3.y.compressed()), y2 + y3) #combined molecule1 and molecule2 self.assertListEqual(list(chain3.z.compressed()), z2 + z3) #combined molecule1 and molecule2 self.assertListEqual(list(chain3.types.compressed()), types2 + types3) #combined molecule1 and molecule2 # self.assertSetEqual(system.bonds[0],set([1])) # self.assertSetEqual(system.bonds[1],set([0,2,4])) # self.assertSetEqual(system.bonds[2],set([1,3])) # self.assertSetEqual(system.bonds[3],set([2])) # self.assertSetEqual(system.bonds[4],set([1,5])) # self.assertSetEqual(system.bonds[5],set([4,6])) # self.assertSetEqual(system.bonds[6],set([5,7])) # self.assertSetEqual(system.bonds[7],set([6])) # self.assertSetEqual(system.bonds[8],set([9])) # self.assertSetEqual(system.bonds[9],set([8,10])) # self.assertSetEqual(system.bonds[10],set([9,11])) # self.assertSetEqual(system.bonds[11],set([10])) np.testing.assert_array_equal(system.bonds[0], np.array([1, -1, -1, -1, -1])) np.testing.assert_array_equal(system.bonds[1], np.array([0, 2, 4, -1, -1])) np.testing.assert_array_equal(system.bonds[2], np.array([1, 3, -1, -1, -1])) np.testing.assert_array_equal(system.bonds[3], np.array([2, -1, -1, -1, -1])) np.testing.assert_array_equal(system.bonds[4], np.array([1, 5, -1, -1, -1])) np.testing.assert_array_equal(system.bonds[5], np.array([4, 6, -1, -1, -1])) np.testing.assert_array_equal(system.bonds[6], np.array([5, 7, -1, -1, -1])) np.testing.assert_array_equal(system.bonds[7], np.array([6, -1, -1, -1, -1])) np.testing.assert_array_equal(system.bonds[8], np.array([9, -1, -1, -1, -1])) np.testing.assert_array_equal(system.bonds[9], np.array([8, 10, -1, -1, -1])) np.testing.assert_array_equal(system.bonds[10], np.array([9, 11, -1, -1, -1])) np.testing.assert_array_equal(system.bonds[11], np.array([10, -1, -1, -1, -1])) for mol in system.molecule_map: self.assertIsNot(mol, system.DummyMolecule)
def test_pbc_homog_HS_sigma155_trial_move(self): lx = 10 ly = 11 lz = 10 partial_indices = [3,4,5] eps = 1.0 sig = 1.55 rcut = 2.5 PT = PairTable(types=['P1'],parms=['epsilon','rcut','sigma','potential']) PT.setUnsetValues('epsilon',eps) PT.setUnsetValues('sigma',sig) PT.setUnsetValues('rcut',rcut) PT.setUnsetValues('potential','HardSphere') N = 3 ref_x,ref_y,ref_z,ref_t = self.make_rubix(N=N,types=None) x1 = list(ref_x) y1 = list(ref_y) z1 = list(ref_z) t1 = list(ref_t) x2 = []; y2 = []; z2 = []; t2 = []; trial_indices = [3,3,8] #equivalent to 3,4,10 for idex in trial_indices: x2.append(x1.pop(idex)) y2.append(y1.pop(idex)) z2.append(z1.pop(idex)) t2.append(t1.pop(idex)) U0 = 0 dist = self.calc_dist(3,4,ref_x,ref_y,ref_z,lx,ly,lz) U0+=self.hard_sphere(dist,eps,sig,rcut) dist = self.calc_dist(4,10,ref_x,ref_y,ref_z,lx,ly,lz) U0+=self.hard_sphere(dist,eps,sig,rcut) dist = self.calc_dist(3,10,ref_x,ref_y,ref_z,lx,ly,lz) U0+=self.hard_sphere(dist,eps,sig,rcut) for bead_i in range(len(x2)): for bead_j in range(len(x1)): dx = abs(x2[bead_i] - x1[bead_j]) dy = abs(y2[bead_i] - y1[bead_j]) dz = abs(z2[bead_i] - z1[bead_j]) if dx>lx/2.0: dx-=lx if dy>ly/2.0: dy-=ly if dz>lz/2.0: dz-=lz dist = sqrt(dx*dx+dy*dy+dz*dz) U0+=self.hard_sphere(dist,eps,sig,rcut) system = System() system.add_beads(x=x1,y=y1,z=z1,types=t1) system.box = Box(cell_grid=(3,3,3)) system.box.lx = lx system.box.ly = ly system.box.lz = lz system.trial_x = np.array([x2],dtype=np.float) system.trial_y = np.array([y2],dtype=np.float) system.trial_z = np.array([z2],dtype=np.float) system.trial_types = np.array([t2]) system.trial_bonds = [] xarray = np.array(x1,dtype=np.float) yarray = np.array(y1,dtype=np.float) zarray = np.array(z1,dtype=np.float) system.box.neighbor_list.build_nlist(xarray,yarray,zarray,True) system.NonBondedTable = PT PE = NonBondedPotentialEnergy(system) U1 = sum(PE.compute(trial_move=True)) self.assertAlmostEqual(U0,U1,delta=0.001)
def test_trial_move(self): lx = 8 ly = 11 lz = 9 dz = 1.1 r0 = 1.0 potential = 'Harmonic' N = 10 ref_x = np.array([0.0] * N) ref_y = np.array([0.0] * N) ref_z = np.array([dz * i - lz / 2.0 for i in range(N)]) ref_t = [0] * N ref_bonds = [[i, j] for i, j in zip(range(N - 1), range(1, N))] N = 5 x = [0.0] * N y = [0.0] * N z = [dz * i - lz / 2.0 for i in range(N)] t = [0] * N bonds = [[i, j] for i, j in zip(range(N - 1), range(1, N))] N_trial = 5 trial_x = [0.0] * N_trial trial_y = [0.0] * N_trial trial_z = [dz * 5 + dz * i - lz / 2.0 for i in range(N_trial)] trial_t = [0] * N_trial trial_bonds = [[i + N, j + N] for i, j in zip(range(N_trial - 1), range(1, N_trial))] trial_bonds += [[N, N + 1]] NBPT = PairTable(types=['P1'], parms=['epsilon', 'rcut', 'sigma', 'potential']) NBPT.setUnsetValues('epsilon', 1.0) NBPT.setUnsetValues('sigma', 1.0) NBPT.setUnsetValues('rcut', 2.5) NBPT.setUnsetValues('potential', 'HardSphere') BPT = PairTable(types=['P1'], parms=['k', 'r0', 'potential']) BPT.setUnsetValues('k', 1.0) BPT.setUnsetValues('r0', r0) BPT.setUnsetValues('potential', potential) system = System() system.add_beads(x=x, y=y, z=z, types=t, bonds=bonds) system.box = Box(cell_grid=(3, 3, 3)) system.box.lx = lx system.box.ly = ly system.box.lz = lz system.BondedTable = BPT system.NonBondedTable = NBPT system.trial_x = np.array([trial_x]) system.trial_y = np.array([trial_y]) system.trial_z = np.array([trial_z]) system.trial_types = np.array([trial_t]) system.trial_bond_pairlist = np.array(trial_bonds) (ref_x, ref_y, ref_z), (imx, imy, imz) = system.box.wrap_positions(x=ref_x, y=ref_y, z=ref_z) U0 = self.calc_all_potential(ref_bonds, ref_x, ref_y, ref_z, ref_t, lx, ly, lz, BPT) PE = BondedPotentialEnergy(system) U1 = sum(PE.compute()) U2 = sum(PE.compute(trial_move=True)) self.assertAlmostEqual(U0, U1 + U2, delta=0.001)
def test_remove_beads(self): ''' The trickiest part of adding and removing beads is correctly mapping the bondlists. Here is a description of this test is testing for. Starting Polymer: 0 - 1 - 2 - 3 - 4 - 5 - 6 - 7 Remove 0,4,7: x - 1 - 2 - 3 - x - 5 - 6 - x | | | | | V V V V V 1 - 2 - 3 5 - 6 Renumbered: 0 - 1 - 2 3 - 4 BONDLISTS OLD NEW ------- ------- 0 : 1 0 : 1 1 : 0,2 1 : 0,2 2 : 1,3 2 : 1 3 : 2,4 3 : 4 4 : 3,5 4 : 3 5 : 4,6 6 : 5,7 7 : 6 ''' system = System() #Basic sanity check that beads are being added x = range(8) y = range(8, 16) z = range(16, 24) types = [0, 1, 1, 0] * 2 bonds = [[i, j] for i, j in zip(range(len(x) - 1), range(1, len(x)))] system.add_beads(x=x, y=y, z=z, types=types, bonds=bonds) to_be_removed = [0, 4, 7] system.remove_beads(to_be_removed) x = list(np.delete(x, to_be_removed)) y = list(np.delete(y, to_be_removed)) z = list(np.delete(z, to_be_removed)) types = list(np.delete(types, to_be_removed)) self.assertEqual(system.nbeads, len(x)) self.assertListEqual(list(system.x), x) self.assertListEqual(list(system.y), y) self.assertListEqual(list(system.z), z) self.assertListEqual(list(system.types), types) np.testing.assert_array_equal(system.bonds[0], np.array([1, -1, -1, -1, -1])) np.testing.assert_array_equal(system.bonds[1], np.array([0, 2, -1, -1, -1])) np.testing.assert_array_equal(system.bonds[2], np.array([1, -1, -1, -1, -1])) np.testing.assert_array_equal(system.bonds[3], np.array([4, -1, -1, -1, -1])) np.testing.assert_array_equal(system.bonds[4], np.array([3, -1, -1, -1, -1]))