def test_neighbor_search(): np.random.seed(0x4d3d3d3) ds = fake_particle_ds(npart=16**3) ds.periodicity = (True, True, True) ds.index fn, = add_nearest_neighbor_field("all", "particle_position", ds) dd = ds.all_data() nearest_neighbors = dd[fn] pos = dd["particle_position"] all_neighbors = np.zeros_like(nearest_neighbors) any_eq = np.zeros(pos.shape[0], dtype='bool') min_in = np.zeros(pos.shape[0], dtype='int64') for i in range(pos.shape[0]): dd.set_field_parameter("center", pos[i, :]) #radius = dd["particle_radius"] #radius.sort() r2 = (pos[:, 0] * pos[:, 0]) * 0 for j in range(3): DR = (pos[i, j] - pos[:, j]) DRo = DR.copy() DR[DRo > ds.domain_width[j] / 2.0] -= ds.domain_width[j] DR[DRo < -ds.domain_width[j] / 2.0] += ds.domain_width[j] r2 += DR * DR radius = np.sqrt(r2) iii = np.argsort(radius) radius.sort() assert (radius[0] == 0.0) all_neighbors[i] = radius[63] any_eq[i] = np.any(np.abs(radius - nearest_neighbors[i]) < 1e-7) min_in[i] = np.argmin(np.abs(radius - nearest_neighbors[i])) #if i == 34: raise RuntimeError #dd.field_data.pop(("all", "particle_radius")) yield assert_equal, (min_in == 63).sum(), min_in.size yield assert_array_almost_equal, nearest_neighbors, all_neighbors
def _smoothing_length(field, data): # For now, we hardcode num_neighbors. We should make this configurable # in the future. num_neighbors = 64 fn, = add_nearest_neighbor_field(ptype, "particle_position", self, num_neighbors) return data[ptype, 'nearest_neighbor_distance_%d' % num_neighbors]
def test_neighbor_search(): np.random.seed(0x4d3d3d3) ds = fake_particle_ds(npart = 16**3) ds.periodicity = (True, True, True) ds.index fn, = add_nearest_neighbor_field("all", "particle_position", ds) dd = ds.all_data() nearest_neighbors = dd[fn] pos = dd["particle_position"] all_neighbors = np.zeros_like(nearest_neighbors) any_eq = np.zeros(pos.shape[0], dtype='bool') min_in = np.zeros(pos.shape[0], dtype='int64') for i in range(pos.shape[0]): dd.set_field_parameter("center", pos[i,:]) #radius = dd["particle_radius"] #radius.sort() r2 = (pos[:,0]*pos[:,0])*0 for j in range(3): DR = (pos[i,j] - pos[:,j]) DRo = DR.copy() DR[DRo > ds.domain_width[j]/2.0] -= ds.domain_width[j] DR[DRo < -ds.domain_width[j]/2.0] += ds.domain_width[j] r2 += DR*DR radius = np.sqrt(r2) iii = np.argsort(radius) radius.sort() assert(radius[0] == 0.0) all_neighbors[i] = radius[63] any_eq[i] = np.any( np.abs(radius - nearest_neighbors[i]) < 1e-7 ) min_in[i] = np.argmin(np.abs(radius - nearest_neighbors[i])) #if i == 34: raise RuntimeError #dd.field_data.pop(("all", "particle_radius")) yield assert_equal, (min_in == 63).sum(), min_in.size yield assert_array_almost_equal, nearest_neighbors, all_neighbors
def setup_gas_particle_fields(self, ptype): num_neighbors = 65 fn, = add_nearest_neighbor_field(ptype, "particle_position", self, num_neighbors) def _func(): def _smoothing_length(field, data): # For now, we hardcode num_neighbors. We should make this configurable # in the future. rv = data[ptype, 'nearest_neighbor_distance_%d' % num_neighbors] #np.maximum(rv, 0.5*data[ptype, "Epsilon"], rv) return rv return _smoothing_length self.add_field( (ptype, "smoothing_length"), function=_func(), particle_type=True, units="code_length")
def test_neighbor_search(): # skip for now, in principle we can reimplement this in the demeshening import nose raise nose.SkipTest np.random.seed(0x4D3D3D3) ds = fake_particle_ds(npart=16 ** 3) ds.periodicity = (True, True, True) ds.index (fn,) = add_nearest_neighbor_field("all", "particle_position", ds) dd = ds.all_data() nearest_neighbors = dd[fn] pos = dd["particle_position"] all_neighbors = np.zeros_like(nearest_neighbors) any_eq = np.zeros(pos.shape[0], dtype="bool") min_in = np.zeros(pos.shape[0], dtype="int64") for i in range(pos.shape[0]): dd.set_field_parameter("center", pos[i, :]) # radius = dd["particle_radius"] # radius.sort() r2 = (pos[:, 0] * pos[:, 0]) * 0 for j in range(3): DR = pos[i, j] - pos[:, j] DRo = DR.copy() DR[DRo > ds.domain_width[j] / 2.0] -= ds.domain_width[j] DR[DRo < -ds.domain_width[j] / 2.0] += ds.domain_width[j] r2 += DR * DR radius = np.sqrt(r2) radius.sort() assert radius[0] == 0.0 all_neighbors[i] = radius[63] any_eq[i] = np.any(np.abs(radius - nearest_neighbors[i]) < 1e-7) min_in[i] = np.argmin(np.abs(radius - nearest_neighbors[i])) # if i == 34: raise RuntimeError # dd.field_data.pop(("all", "particle_radius")) assert_equal((min_in == 63).sum(), min_in.size) assert_array_almost_equal(nearest_neighbors, all_neighbors)