def compute_nn_list(self, file_name, start_row, num_nn): f1 = self.load_file(file_name, start_row) print "Loaded file: " + file_name print "Computing nearest-neighbour list..." bounds = array([1.999999999, 1.999999999, 1.999999999]) # Can't do exactly 2.0 because rounding atom_list = PeriodicCKDTree(bounds, f1) nn = atom_list.query(f1, k=num_nn) self.save_file("build/" + file_name + "_nn", nn[1]) print "Nearest-neighbour list successfully computed!"
def find_k_nn_theta(grp, num_nn_k=None): '''This function will find the k nearest neighbors for theta and add columns to the data frame that include the nn number (1st, 2nd,...), the particle id of the nn, and the distance of that nn. The function respects boundary conditions of the data such that it is periodic at 360 degrees. The way to use this function is as such: df=df.groupby('frame', group_keys=False).apply(find_k_nn_theta, num_nn_k=x) The group_keys kwrg prevents a redundant frames column. Reseting the index will give the data frame a regular integer index like before the function is applied. The second line rearranges the columns to the correct order. ''' from periodic_kdtree import PeriodicCKDTree bounds = np.array([360]) data = grp['theta'].values data = np.reshape(data, [len(data), 1]) tree = PeriodicCKDTree(bounds, data) if num_nn_k == None: num_nn_k = len(data) elif num_nn_k > len(data)-1: num_nn_k = len(data) elif num_nn_k != None: num_nn_k += 1 d, i = tree.query(data, k=num_nn_k) if len(d) == 1: # If only one particle return the group grp['theta_nn_num'] = np.nan grp['theta_nn_id'] = np.nan grp['theta_nn_dist'] = np.nan return grp # Create particle id column particle_ids = grp['track id'].values track_ids = np.tile(particle_ids, (num_nn_k, 1)) track_ids = track_ids.T[:, 1:].flatten() nn_ids = particle_ids[i] # Create nn number column (1st, 2nd, etc) nn_num = np.arange(num_nn_k) nn_num = np.tile(nn_num, (len(particle_ids), 1))[:, 1:] nn_num = nn_num.flatten() # Create corresponding nn track id nn_ids = nn_ids[:, 1:].flatten() nn_dist = d[:, 1:].flatten() # Merge with current group nn_df = pd.DataFrame(np.vstack((track_ids, nn_num, nn_ids, nn_dist)).T, columns=['track id', 'theta_nn_num', 'theta_nn_id', 'theta_nn_dist']) new_df = pd.merge(grp, nn_df, left_on='track id', right_on='track id') return new_df
class test_vectorization_compiled: def setUp(self): self.data = np.array([[0,0,0], [0,0,1], [0,1,0], [0,1,1], [1,0,0], [1,0,1], [1,1,0], [1,1,1]]) self.bounds = 1.1 * np.ones(3) self.kdtree = PeriodicCKDTree(self.bounds, self.data) def test_single_query(self): d, i = self.kdtree.query([0,0,0]) assert_(isinstance(d,float)) assert_(isinstance(i,int)) def test_vectorized_query(self): d, i = self.kdtree.query(np.zeros((2,4,3))) assert_equal(np.shape(d),(2,4)) assert_equal(np.shape(i),(2,4)) def test_vectorized_query_noncontiguous_values(self): qs = np.random.randn(3,1000).T ds, i_s = self.kdtree.query(qs) for q, d, i in zip(qs,ds,i_s): assert_equal(self.kdtree.query(q),(d,i)) def test_single_query_multiple_neighbors(self): s = 23 kk = 27*self.kdtree.n+s d, i = self.kdtree.query([0,0,0],k=kk) assert_equal(np.shape(d),(kk,)) assert_equal(np.shape(i),(kk,)) assert_(np.all(~np.isfinite(d[-s:]))) assert_(np.all(i[-s:]==self.kdtree.n)) def test_vectorized_query_multiple_neighbors(self): s = 23 kk = 27*self.kdtree.n+s d, i = self.kdtree.query(np.zeros((2,4,3)),k=kk) assert_equal(np.shape(d),(2,4,kk)) assert_equal(np.shape(i),(2,4,kk)) assert_(np.all(~np.isfinite(d[:,:,-s:]))) assert_(np.all(i[:,:,-s:]==self.kdtree.n))
def bonded(snap, i, j, box): type_list = snap.particles.types index_B = type_list.index('B') positions = snap.particles.position bodies = snap.particles.body type_id = snap.particles.typeid pos_i = positions[i, :] pos_j = positions[j, :] loc_B_i = np.where((bodies == i) & (type_id == index_B))[0] pos_B_i = positions[loc_B_i, :] loc_B_j = np.where((bodies == j) & (type_id == index_B))[0] pos_B_j = positions[loc_B_j, :] bounds = np.array([box.Lx, box.Ly, box.Lz]) T = PeriodicCKDTree(bounds, pos_B_j) for m in range(len(pos_B_i)): cur_pos = pos_B_i[m, :] nn_dist, idx = T.query(cur_pos, k=1) if (nn_dist < 2.5): return True return False
class test_vectorization_compiled: def setUp(self): self.data = np.array([[0, 0, 0], [0, 0, 1], [0, 1, 0], [0, 1, 1], [1, 0, 0], [1, 0, 1], [1, 1, 0], [1, 1, 1]]) self.bounds = 1.1 * np.ones(3) self.kdtree = PeriodicCKDTree(self.bounds, self.data) def test_single_query(self): d, i = self.kdtree.query([0, 0, 0]) assert_(isinstance(d, float)) assert_(isinstance(i, int)) def test_vectorized_query(self): d, i = self.kdtree.query(np.zeros((2, 4, 3))) assert_equal(np.shape(d), (2, 4)) assert_equal(np.shape(i), (2, 4)) def test_vectorized_query_noncontiguous_values(self): qs = np.random.randn(3, 1000).T ds, i_s = self.kdtree.query(qs) for q, d, i in zip(qs, ds, i_s): assert_equal(self.kdtree.query(q), (d, i)) def test_single_query_multiple_neighbors(self): s = 23 kk = 27 * self.kdtree.n + s d, i = self.kdtree.query([0, 0, 0], k=kk) assert_equal(np.shape(d), (kk, )) assert_equal(np.shape(i), (kk, )) assert_(np.all(~np.isfinite(d[-s:]))) assert_(np.all(i[-s:] == self.kdtree.n)) def test_vectorized_query_multiple_neighbors(self): s = 23 kk = 27 * self.kdtree.n + s d, i = self.kdtree.query(np.zeros((2, 4, 3)), k=kk) assert_equal(np.shape(d), (2, 4, kk)) assert_equal(np.shape(i), (2, 4, kk)) assert_(np.all(~np.isfinite(d[:, :, -s:]))) assert_(np.all(i[:, :, -s:] == self.kdtree.n))
def Run_Correlated_Sphere_Packing(input_parameters_filename="Parameters.in", seed_increment=0, seed=None, periodic_geometry=None, nsamples=None, target_porosity=None): parameters = read_input_parameters(input_parameters_filename) reinit_flag = 0 '''debugging, ignore this''' print(','.join(np.array([name for name in parameters.keys()]))) try: if (seed is not None): parameters['seed'] = seed except Exception as e: print('no seed specified') try: if (periodic_geometry is not None): parameters['periodic_geometry'] = periodic_geometry except Exception as e: print('no periodicv geometry specified') try: if (nsamples is not None): parameters['nsamples'] = nsamples except Exception as e: print('no nsampels specified') try: if (target_porosity is not None): parameters['target_porosity'] = target_porosity except Exception as e: print('no porosity specified') '''increment in case of reinitialization''' print(" seed_increment ", seed_increment, " type ", type(seed_increment)) parameters['seed'] += seed_increment '''use input parameters''' periodic_geometry,ndimensions,xmin,xmax,ymin,ymax,zmin,zmax,seed,radius_mu,radius_sig2,Cmu,Csig,filename,radii_dist,nsamples,target_porosity,nbins,num_neighbors,set_leafsize_factor,kdt_eps,pnorm,search_radius_factor_of_max_diameter,find_all_neighbors,percentilemin,percentilemax,show_hist,set_dt,blfac,damping,tstep,tprint,tstepmax,force_abstol,fmin,force_absmax_factor,force_reltol,max_overlap_factor,set_leafsize_factor \ =parameter_values(parameters, *parameters.values()) # print(xmin,xmax) RandomState = np.random.RandomState(seed) '''form uniform hexahedral mesh''' ncells = nsamples xx = np.linspace(xmin, xmax, ncells + 1) dcell = np.diff(xx)[0] domain_volume = (xmax - xmin) * (ymax - ymin) * (zmax - zmin) periodic_bounds = np.array([xmax, ymax, zmax])[:ndimensions] #PART I '''sample radii''' # #sample radii # while target_porosity < v0: # v0 = (radii**3 * 4*np.pi/(3 * (xmax-xmin)**3)) # v0 = v0/v0.sum() if (radii_dist == 'lognormal'): Z = RandomState.lognormal(np.log(radius_mu), radius_sig2, nsamples) radii = np.exp(Z) if (show_hist == True): plt.hist(radii, nbins) plt.savefig('hist.png') pmin = np.percentile(radii, percentilemin), pmax = np.percentile(radii, percentilemax) radii = radii[radii < pmax] radii = radii[radii > pmin] nsamplesclip = nsamples - radii.shape[0] nsamples = radii.shape[0] # - nsamplesclip radii = np.sort(radii) print('1,99 percentiles ', pmin, pmax) print('max, min, mean', radii.max(), radii.min(), radii.mean()) '''rescale radii to obtain desired porosity''' rscale = ((np.sum(4 * np.pi * (1 / 3.) * radii**3)) / (domain_volume * (1 - target_porosity)))**(1 / 3) radii_scaled = radii / rscale radius_mu_scaled = radius_mu / rscale radius_sig2_scaled = radius_sig2 / rscale**2 rmax = (radii_scaled).max() dmax = 2 * rmax delta = dcell - 2 * rmax search_radius = 2 * dmax #+delta #TBD '''sample isotropic gaussian correlation''' Cnorm = RandomState.multivariate_normal( Cmu, Csig, nsamples) #,[nsamples,ndimensions]) '''sample points uniformly in space ''' pts = RandomState.uniform(0, 1, [nsamples, ndimensions]) pts[:, 0] = (pts[:, 0]) * (xmax - xmin) + xmin pts[:, 1] = (pts[:, 1]) * (ymax - ymin) + ymin pts[:, 2] = (pts[:, 2]) * (zmax - zmin) + zmin '''sort radii and points ''' radii_scaled = radii_scaled[::-1] pts = pts[::-1, :] '''get nearest neighbor distances''' t0 = time.time() if (periodic_geometry == True): kdt = PeriodicCKDTree(periodic_bounds, pts) else: kdt = scipy.spatial.KDTree( pts) #,leafsize=set_leafsize_factor * num_neighbors ) if (find_all_neighbors == True): dist, neighbors = kdt.query(pts, k=num_neighbors, eps=kdt_eps, p=pnorm) print("NNE time ", time.time() - t0) '''sort by func(distances) eg sum''' distsum = (dist[:, 1:].sum(axis=1)) distmean = (dist[:, 1:].mean(axis=1)) distmedian = np.median(dist[:, 1:], axis=1) distmin = (dist[:, 1:].min(axis=1)) distmax = (dist[:, 1:].max(axis=1)) isort_pts = np.argsort(distmedian) isort_radii = np.argsort(isort_pts) sorted_radii = radii[isort_radii].copy() edges = from_neighbors_to_edges(neighbors)[0] '''overlap''' max_overlap = radius_mu_scaled * max_overlap_factor #find neighbors I interesect (in 3/9/27 cells), use centers and radii to move away by dx if ||dx||<overlap_max '''BC, EQ separation, pore throat size, collision distances, PD''' boundary_layer = [blfac, xmax * (1 - blfac)] print("BOUINDARY LAYER", boundary_layer) iboundary = np.any(((pts < boundary_layer[0]).astype(int) + (pts > boundary_layer[1]).astype(int)), axis=1) iinterior = np.all((pts > boundary_layer[0]).astype(int) * (pts < boundary_layer[1]).astype(int), axis=1) print("IBOUNDARY ", iboundary) print("IINTERIOR ", iinterior) print('\n \n \n ', " NUMBER OF BOUNDARY SPHERES ", iboundary.sum(), " TOTAL CONSIDERED ", iboundary.shape, '\n \n \n ', "NUMBER OF INTERIOR SPHERES", iinterior.sum(), " TOTAL CONSIDERED ", iinterior.shape, '\n \n \n ') # ,iboundary.sum() ''' Detect Collisions and Translate Spheres ''' registered = [] unregistered = [i for i in range(nsamples)] boundary = [] t_list = [] tlast = time.time() for i, (x, r) in enumerate(zip(pts, radii_scaled)): if (i % 1000 == 0): print(i, x, r, time.time()) if (i == 0): registered.append(i) unregistered.remove(i) pts[i] = x radii_scaled[i] = r else: x, reinit_flag = overlap_correction( i, x, r, pts, radii_scaled, kdt, registered, unregistered, dmax, search_radius_factor_of_max_diameter, pnorm=2, eps=kdt_eps) if (reinit_flag == 1): break registered.append(i) unregistered.remove(i) pts[i] = x radii_scaled[i] = r t_list.append(time.time() - tlast) tlast = time.time() if (reinit_flag == 1): print(" \n Reinitializing Simulation \n") return Run_Correlated_Sphere_Packing(seed_increment + 1) else: print("\n No collisions found, continuing.. \n") t_list = np.array(t_list) registered = np.array(registered) print("STORE BOUNDARY SPHERE DATA") indices_boundary = np.where(iboundary) print("STORE INTERIOR SPHERE DATA") indices_interior = np.where(iinterior) interior_points = pts[indices_interior] boundary_points = pts[indices_boundary] print("SET POINT IDs (to keep track of boundary image spheres)") idx_points = np.arange(0, len(registered)) boundary, boundary_indices, boundary_radii = [], [], [ ] #np.array([]), np.array([]), np.array([]) if (periodic_geometry == 1): print( "COPY BOUNDARY POINTS TO IMAGE SPHERES ACROSS PERIODIC BOUNDARIES") print("NUM POINTS BEFORE BOUNDARY IMAGE COPY", pts.shape) flag = 0 # while flag==0: # pts, radii_scaled,idx_points, flag = get_reflected_pts(pts,idx_points, radii_scaled,xmin,xmax) pts, radii_scaled, idx_points, boundary, boundary_indices, boundary_radii, flag = get_reflected_pts( pts, idx_points, boundary, boundary_indices, boundary_radii, radii_scaled, xmin, xmax) print(radii_scaled.shape) nsamples = radii_scaled.shape[0] assert (radii_scaled.shape[0] == pts.shape[0]) print("NUM POINTS AFTER ", pts.shape) pvolumes = radii_scaled**3 * 4 * np.pi / 3 if ndimensions == 3 else radii_scaled**2 * np.pi porosity = 1 - (domain_volume - pvolumes.sum()) / domain_volume print("\n domain size ", " [xmin,xmax] ", xmin, xmax, " [ymin,ymax] ", ymin, ymax, " [zmin,zmax] ", zmin, zmax, " volume ", domain_volume) print('particle volumes: sum, mean, median, min, max', pvolumes.sum(), pvolumes.mean(), np.median(pvolumes), pvolumes.min(), pvolumes.max()) print("\n \n \n porosity ", porosity) print("\n number of spheres ", registered.shape) print("\n number of registered spheres ", registered.shape) print("\n number of unregistered spheres ", registered.shape) print("\n sphere distribution parameters ", radius_mu, radius_sig2) print("\n mean coordination number ", ) print("\n \n \n ") return parameters, radii_scaled, registered, unregistered, pts, pvolumes, idx_points, boundary, boundary_indices, boundary_radii
def Run_Correlated_Sphere_Packing(input_parameters_filename='Parameters.in', seed_increment = 0): parameters = read_input_parameters(input_parameters_filename) reinit_flag=0 '''increment in case of reinitialization''' parameters['seed']+=seed_increment '''use input parameters''' ndimensions,xmin,xmax,ymin,ymax,zmin,zmax,num_neighbors,set_leafsize_factor,periodic_geometry,kdt_eps,pnorm,filename,radii_dist,radius_mu,radius_sig2,nbins,nsamples,show_hist,target_porosity,percentilemin,percentilemax,find_all_neighbors,search_radius_factor_of_max_diameter,set_dt,blfac,damping,tstep,tprint,tstepmax,force_abstol,fmin,force_absmax_factor,force_reltol,max_overlap_factor,seed,Cmu,Csig,set_leafsize \ =parameter_values(parameters, *parameters.values()) # print(xmin,xmax) RandomState = np.random.RandomState(seed) '''form uniform hexahedral mesh''' ncells = nsamples xx=np.linspace(xmin,xmax,ncells+1) dcell = np.diff(xx)[0] domain_volume = (xmax-xmin)**3 periodic_bounds = np.array([xmax,ymax,zmax])[:ndimensions] #PART I '''sample radii''' # #sample radii # while target_porosity < v0: # v0 = (radii**3 * 4*np.pi/(3 * (xmax-xmin)**3)) # v0 = v0/v0.sum() if(radii_dist=='lognormal'): mu = np.log(radius_mu) sig2 = radius_sig2 Z = RandomState.lognormal(mu,sig2,nsamples) radii = np.exp(Z) if(show_hist==True): plt.hist(radii,nbins) plt.show() pmin = np.percentile(radii,percentilemin), pmax = np.percentile(radii,percentilemax) radii = radii[radii<pmax] radii = radii[radii>pmin] nsamplesclip = nsamples - radii.shape[0] nsamples = radii.shape[0]# - nsamplesclip radii = np.sort(radii) print('1,99 percentiles ', pmin,pmax) print('max, min, mean', radii.max(),radii.min(),radii.mean()) '''rescale radii to obtain desired porosity''' rscale = ( ( np.sum(4*np.pi*(1/3.) * radii**3) ) / ( domain_volume * target_porosity ) )**(1/3) radii_scaled = radii/rscale radius_mu_scaled = radius_mu / rscale radius_sig2_scaled = radius_sig2 / rscale**2 rmax = ( radii_scaled ).max() dmax = 2*rmax delta = dcell - 2*rmax search_radius = 2*dmax#+delta '''sample isotropic gaussian correlation''' Cnorm = RandomState.multivariate_normal(Cmu,Csig,nsamples)#,[nsamples,ndimensions]) '''sample points uniformly in space ''' pts = RandomState.uniform(xmin,xmax,[nsamples,ndimensions]) '''sort radii and points ''' radii_scaled = radii_scaled[::-1] pts = pts[::-1,:] '''get nearest neighbor distances''' t0 = time.time() if(periodic_geometry==True): kdt = PeriodicCKDTree(periodic_bounds, pts) else: kdt = scipy.spatial.KDTree(pts,leafsize=set_leafsize) if(find_all_neighbors==True): dist,neighbors = kdt.query(pts, k=num_neighbors, eps=kdt_eps, p = pnorm) print("NNE time " , time.time()-t0) '''sort by func(distances) eg sum''' distsum = (dist[:,1:].sum(axis=1)) distmean = (dist[:,1:].mean(axis=1)) distmedian = np.median(dist[:,1:],axis=1) distmin = (dist[:,1:].min(axis=1)) distmax = (dist[:,1:].max(axis=1)) isort_pts = np.argsort(distmedian) isort_radii = np.argsort(isort_pts) sorted_radii = radii[isort_radii].copy() edges = from_neighbors_to_edges(neighbors)[0] '''overlap''' max_overlap = radius_mu_scaled * max_overlap_factor #find neighbors I interesect (in 3/9/27 cells), use centers and radii to move away by dx if ||dx||<overlap_max '''BC, EQ separation, pore throat size, collision distances, PD''' boundary_layer = [blfac, xmax *(1- blfac)] iboundary = np.any(((pts<boundary_layer[0]).astype(int)+(pts>boundary_layer[1]).astype(int)) , axis=1) iinterior = np.all((pts>boundary_layer[0]).astype(int)*(pts<boundary_layer[1]).astype(int),axis=1) print(iboundary.shape,iinterior.shape) iinterior.sum(),iboundary.sum() #formerly, scaled_radii was defined another way scaled_radii = radii_scaled.copy() scaled_radius_mu = radius_mu_scaled.copy() num_inclusions_per_dim = int((nsamples)**(1/ndimensions)) if(find_all_neighbors==True): eq_length_factor = (scaled_radii[neighbors[:,0:1]] + scaled_radii[neighbors[:,1:]]) else: eq_length_factor = (4 * np.pi * (1/3) * (scaled_radii**3).mean())**(1/3) * np.array([[1]]) #??? pore_space_per_particle = (xmax - eq_length_factor.mean()*num_inclusions_per_dim)/num_inclusions_per_dim medimean_eq_length = np.median( eq_length_factor.mean(axis=1)) porespace_per_dim = num_inclusions_per_dim * medimean_eq_length porespace_per_particle = (porespace_per_dim / (num_inclusions_per_dim - 1))/2 scaled_radius_diam = scaled_radius_mu*2 #set spacing collision_length_factor = eq_length_factor.copy()# - pore_space_per_particle /2 eq_length_factor = eq_length_factor + pore_space_per_particle /2 horizon_factor = eq_length_factor * 1 # nneighbors = neighbors.shape[1] tsteps = np.arange(0,tstepmax) zmin = ymin = xmin zmax = ymax = xmax (pts.T[-1]-np.mod(pts.T[-1],xmax)).max() # pts.T[-1][ (pts.T[-1] - radii.T)>xmax] # (p - radii.T)>xmax (pts.T[-1]+radii_scaled > xmax).sum() ((pts**2).sum(axis=1)+radii_scaled**2 > xmax**2).sum() cond, conds = where_boundary_intersect(parameters,pts,radii_scaled) conds # (np.linalg.norm(pts,2,axis=1)+radii_scaled > xmax).sum() # pts.min() flag=0 # while flag==0: pts, radii_scaled,flag = get_reflected_pts(pts,radii_scaled,xmin,xmax) print(radii_scaled.shape) nsamples = radii_scaled.shape[0] assert(radii_scaled.shape[0] == pts.shape[0]) ''' Detect Collisions and Translate Spheres ''' registered = [] unregistered = [i for i in range(nsamples)] boundary = [] t_list = [] tlast = time.time() for i,(x,r) in enumerate(zip( pts , radii_scaled)): if(i%1000==0): print(i,x,r, time.time()) if(i==0): registered.append(i) unregistered.remove(i) pts[i] = x radii_scaled[i] = r else: x,reinit_flag = overlap_correction(i, x, r, pts, radii_scaled, kdt, registered, unregistered,dmax, search_radius_factor_of_max_diameter, pnorm=2, eps=kdt_eps) if(reinit_flag==1): break; registered.append(i) unregistered.remove(i) pts[i] = x radii_scaled[i] = r t_list.append(time.time() - tlast) tlast = time.time() if(reinit_flag==1): print(" \n Reinitializing Simulation \n") return Run_Correlated_Sphere_Packing(seed_increment+1) else: print("\n No collisions found, continuing.. \n") t_list = np.array(t_list) registered = np.array(registered) pvolumes = radii_scaled**3 * 4 * np.pi / 3 if ndimensions==3 else radii_scaled**2 * np.pi return parameters, radii_scaled, registered, unregistered, pts, pvolumes
def boundary_stk(xvol, yvol, zvol, x_same_zone_bn, y_same_zone_bn, z_same_zone_bn, vol_same_zone_bn, zn, rad_val): # This function takes in the location of the boundary points (likely volume averaged) and # bins them to find the profile # Create tree of volume weighted boundary points boundary_pts = zip(xvol, yvol, zvol) periodic_tree_boundary = PeriodicCKDTree(bounds, boundary_pts) x_part = [] y_part = [] z_part = [] # Find particles within rad_val of zone radius that are not in zone idx = periodic_tree.query_ball_point([x_vol[zn],y_vol[zn],z_vol[zn]],rad_val) new_idx = [] # index of particles not in zone, but within 2*R_eff of zone for i in idx: if zone[i] != zn: new_idx.append(i) x_part.append(x[i]) y_part.append(y[i]) z_part.append(z[i]) cls_dist = [] cls_idx = [] cls_dist_non_zn = [] cls_idx_non_zn = [] # Find closest distance for each particle in a zone to the boundary particle for i in range(0,len(x_same_zone_bn)): cls_dist.append(periodic_tree_boundary.query([x_same_zone_bn[i],y_same_zone_bn[i],z_same_zone_bn[i]])[0]) cls_idx.append(periodic_tree_boundary.query([x_same_zone_bn[i],y_same_zone_bn[i],z_same_zone_bn[i]])[1]) # Find closest distance for each particle not in a zone to the boundary particle for i in range(0,len(x_part)): cls_dist_non_zn.append(periodic_tree_boundary.query([x_part[i],y_part[i],z_part[i]])[0]) cls_idx_non_zn.append(periodic_tree_boundary.query([x_part[i],y_part[i],z_part[i]])[1]) # Calculate density for each cell in the zone # den_same_zone_bn = [(1./volume) for volume in vol_same_zone_bn[0]] vol_same_zone_bn = [(volume) for volume in vol_same_zone_bn[0]] # Calculate density for each particle vol_non_zn_part = [] for i in new_idx: # den_non_zn_part.append(1./vol[i]) vol_non_zn_part.append(vol[i]) # Bin the density, distance, and number counts from 0 to 2.5. This binning is normalized to effective radius of each zone den_bins = [] dist_bins = [] ncnt_bins = [] den_bins_non_zn = [] dist_bins_non_zn = [] ncnt_bins_non_zn = [] # Find den, dist, cnt for particles in zone for i in range(0,len(bins)-1): # Density, distance, and num counts of for each bin. Bins are normalized to the effective radius of each zone den_temp, dist_temp, ncnt_temp = boundary_bin(vol_same_zone_bn, cls_dist, bins[i], bins[i+1]) # Make arrays of den, dist, num counts of for bins if den_temp != np.nan: den_bins.append(den_temp) else: den_bins.append(0) if dist_temp != np.nan: dist_bins.append(dist_temp) else: dist_bins.append(0) if ncnt_temp != np.nan: ncnt_bins.append(ncnt_temp) else: ncnt_bins.append(0) # Find den, dist, cnt for particle not in zone for i in range(0,len(bins)-1): # Density, distance, and num counts of for each bin. Bins are normalized to the effective radius of each zone den_temp2, dist_temp2, ncnt_temp2 = boundary_bin(vol_non_zn_part, cls_dist_non_zn, bins[i], bins[i+1]) # Make arrays of den, dist, num counts of for bins if den_temp2 != np.nan: den_bins_non_zn.append(den_temp2) else: den_bins_non_zn.append(0) if dist_temp2 != np.nan: dist_bins_non_zn.append(dist_temp2) else: dist_bins.append(0) if ncnt_temp2 != np.nan: ncnt_bins_non_zn.append(ncnt_temp2) else: ncnt_bins_non_zn.append(0) return den_bins, dist_bins, ncnt_bins, den_bins_non_zn, dist_bins_non_zn, ncnt_bins_non_zn
print "dimension %d, %d points" % (m, n) t = time.time() T1 = PeriodicKDTree(bounds, data) print "PeriodicKDTree constructed:\t%g" % (time.time() - t) t = time.time() T2 = PeriodicCKDTree(bounds, data) print "PeriodicCKDTree constructed:\t%g" % (time.time() - t) t = time.time() w = T1.query(queries) print "PeriodicKDTree %d lookups:\t%g" % (r, time.time() - t) del w t = time.time() w = T2.query(queries) print "PeriodicCKDTree %d lookups:\t%g" % (r, time.time() - t) del w T3 = PeriodicCKDTree(bounds, data, leafsize=n) t = time.time() w = T3.query(queries) print "flat PeriodicCKDTree %d lookups:\t%g" % (r, time.time() - t) del w t = time.time() w1 = T1.query_ball_point(queries, 0.2) print "PeriodicKDTree %d ball lookups:\t%g" % (r, time.time() - t) t = time.time() w2 = T2.query_ball_point(queries, 0.2)
print "dimension %d, %d points" % (m,n) t = time.time() T1 = PeriodicKDTree(bounds, data) print "PeriodicKDTree constructed:\t%g" % (time.time()-t) t = time.time() T2 = PeriodicCKDTree(bounds, data) print "PeriodicCKDTree constructed:\t%g" % (time.time()-t) t = time.time() w = T1.query(queries) print "PeriodicKDTree %d lookups:\t%g" % (r, time.time()-t) del w t = time.time() w = T2.query(queries) print "PeriodicCKDTree %d lookups:\t%g" % (r, time.time()-t) del w T3 = PeriodicCKDTree(bounds,data,leafsize=n) t = time.time() w = T3.query(queries) print "flat PeriodicCKDTree %d lookups:\t%g" % (r, time.time()-t) del w t = time.time() w1 = T1.query_ball_point(queries, 0.2) print "PeriodicKDTree %d ball lookups:\t%g" % (r, time.time()-t) t = time.time() w2 = T2.query_ball_point(queries, 0.2)
'''sample isotropic gaussian correlation''' Cnorm = np.random.multivariate_normal(Cmu,Csig,nsamples)#,[nsamples,ndimensions]) '''sample points''' pts = np.random.uniform(xmin,xmax,[nsamples,ndimensions]) '''get nearest neighbor distances''' t0 = time.time() if(periodic_geometry==True): kdt = PeriodicCKDTree(periodic_bounds, pts) else: kdt = scipy.spatial.KDTree(pts,leafsize=set_leafsize) if(find_all_neighbors==True): dist,neighbors = kdt.query(pts, k=num_neighbors, eps=kdt_eps, p = pnorm) print("NNE time " , time.time()-t0) '''sort by func(distances) eg sum''' distsum = (dist[:,1:].sum(axis=1)) distmean = (dist[:,1:].mean(axis=1)) distmedian = np.median(dist[:,1:],axis=1) distmin = (dist[:,1:].min(axis=1)) distmax = (dist[:,1:].max(axis=1)) isort_pts = np.argsort(distmedian) isort_radii = np.argsort(isort_pts) sorted_radii = radii[isort_radii].copy()
def event_local_atom_index(initial_config_data, triggered_atom_list, num_of_involved_atom, path_to_init_sad, box_dim, save_results = True, re_calc = False): """ this function get the local atom atom_ids as a list of lists (that will be flattern into a single list) from the triggered atoms (whose item_ids in triggered_atom_list) item_ids and num_of_involved atoms NN For example, each trigger atom has k th NN. Then for x triggered atoms, it would be a x element list of lists (each list with k elements). It will be flattern into a list with k*N, that will be returned As in NN_finder_all, PeriodicCkDtree can be easily extended to find kth NN for each of the triggered atoms. Currently, only one atom is triggered, In this case, triggered_atom_list is a single element list return: triggered_atoms_NN_list: a list comes from a flatten numpy array shape (len(triggered_atom_list)*k, ) a numpy contains all NN of all triggered atoms preserving the order of the appearance of triggered atom in triggered_atom_list e.g. 1st k elements in triggered_atoms_NN_list belongs to the 1st element of triggered_atom_list, etc """ path_to_local_nn_results = path_to_init_sad + "/local_nn_results_dict.pkl" print "the triggering atoms are:", triggered_atom_list if re_calc is False: if os.path.exists(path_to_local_nn_results): print "local nn results already calculated and saved in local_nn_results_dict.pkl file, skip calculation" local_nn = pickle.load(open(path_to_local_nn_results,'r')) return (np.array(local_nn.values()).flatten()).tolist() local_nn = dict() if triggered_atom_list is None: raise Exception("try to calculate the NN for triggered atoms, but no triggered atom has been specified") _data = initial_config_data _interested_data = _data.loc[_data['item'].isin(triggered_atom_list)] result_tree = PeriodicCKDTree(box_dim, _data[['x','y','z']].values) # the query method calculated NN includes the triggered atom itself as the 1st NN with distance 0 distances,locations = result_tree.query(_interested_data[['x','y','z']].values, num_of_involved_atom) if len(triggered_atom_list) > 1 and num_of_involved_atom >1: # the 1st element in the return numpy array with shape (k*len(triggered_atom_list),) # is the triggered atom since the distance to itself is 0, which is the minumum # locations are ordered in terms of their increasing distance to the triggered atom k=0 for index,row in _interested_data.iterrows(): NN_array = np.array((_data.iloc[locations[k]])['item']) local_nn[row['item']]= (NN_array).tolist() k=k+1 loc_index = locations.flatten() final_locations = np.array((_data.iloc[loc_index])['item']).tolist() elif len(triggered_atom_list) == 1: if type(locations) == int or type(locations) == float: loc_index = np.array([locations]) else: loc_index = locations.flatten() locations = np.array((_data.iloc[loc_index])['item']).tolist() local_nn[triggered_atom_list[0]] = locations final_locations = locations else: # len(triggered_atom_list) >1 and num_of_involved_atom == 1: for x in triggered_atom_list: local_nn[x] = [x] final_locations = triggered_atom_list if save_results is True: with open(path_to_local_nn_results, 'w') as f: pickle.dump(local_nn,f) f.close() return final_locations