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 by_type_proximity(self, typeList): ''' Distribute a single molecule into multiple molecules based on proximity to a bead type ''' if len(typeList) < 2: raise ValueError('Need at least two types to distribute between!') box = self.molecule.system.box if box is None: box = Box(L=1000) x = self.molecule.x.compressed() y = self.molecule.y.compressed() z = self.molecule.z.compressed() types = self.molecule.types.compressed() indices = np.array(self.molecule.indices) # index_groups will be passed to self.distribute_by_index. It must be initialized with the # beads which are pre-grouped because they match a grouping type index_groups = [] for i, typeVal in enumerate(typeList): index_groups.append(list(indices[types == typeVal])) # Need to combine all typeGroups. These beads are already grouped # as they define the groups themselves mask = (types == typeList[0]) for typeVal in typeList[1:]: mask = np.logical_or(mask, types == typeVal) # Beads to be grouped x1Array = x[~mask] y1Array = y[~mask] z1Array = z[~mask] indices1 = indices[~mask] # Beads which are pre-grouped because they match a proximity type x2Array = x[mask] y2Array = y[mask] z2Array = z[mask] types2 = types[mask] for i, (x1, y1, z1) in enumerate(zip(x1Array, y1Array, z1Array)): idex, dist = n_closest(1, x1, y1, z1, x2Array, y2Array, z2Array, box) for j, typeVal in enumerate(typeList): if types2[idex[0]] == typeVal: index_groups[j].append(indices1[i]) break return self.by_index(index_groups)
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 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_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)