def test_density_profile(self): density_profile = espressomd.observables.DensityProfile(**self.kwargs) obs_data = density_profile.calculate() obs_edges = density_profile.call_method("edges") obs_bin_edges = density_profile.bin_edges() obs_bin_centers = density_profile.bin_centers() np_hist, np_edges = tests_common.get_histogram(np.copy( self.system.part[:].pos), self.kwargs, 'cartesian', normed=True) np_hist *= len(self.system.part) np.testing.assert_array_almost_equal(obs_data, np_hist) for i in range(3): np.testing.assert_array_almost_equal(obs_edges[i], np_edges[i]) np.testing.assert_array_equal(obs_data.shape, [ self.kwargs['n_x_bins'], self.kwargs['n_y_bins'], self.kwargs['n_z_bins'] ]) np.testing.assert_array_almost_equal( obs_bin_edges[0, 0, 0], [self.kwargs['min_x'], self.kwargs['min_y'], self.kwargs['min_z']]) np.testing.assert_array_almost_equal( obs_bin_edges[-1, -1, -1], [self.kwargs['max_x'], self.kwargs['max_y'], self.kwargs['max_z']]) np.testing.assert_array_almost_equal(obs_bin_centers[0, 0, 0], [2.5, 2.5, 2.5])
def LB_fluxdensity_profile_test(self): self.set_fluid_velocity() self.set_particles() # Set up the Observable. local_params = self.params.copy() if self.params['axis'] == 'x': local_params['axis'] = [1.0, 0.0, 0.0] elif self.params['axis'] == 'y': local_params['axis'] = [0.0, 1.0, 0.0] else: local_params['axis'] = [0.0, 0.0, 1.0] p = espressomd.observables.CylindricalLBFluxDensityProfileAtParticlePositions( **local_params) core_hist = p.calculate() core_hist_v_r = core_hist[:, :, :, 0] core_hist_v_phi = core_hist[:, :, :, 1] core_hist_v_z = core_hist[:, :, :, 2] core_edges = p.call_method("edges") self.pol_positions = self.pol_coords() np_hist, np_edges = tests_common.get_histogram( self.pol_positions, self.params, 'cylindrical') np_hist = self.normalize_with_bin_volume(np_hist) np.testing.assert_array_almost_equal(np_hist * self.v_r, core_hist_v_r) np.testing.assert_array_almost_equal( np_hist * self.v_phi, core_hist_v_phi) np.testing.assert_array_almost_equal(np_hist * self.v_z, core_hist_v_z) for i in range(3): np.testing.assert_array_almost_equal(np_edges[i], core_edges[i]) self.assertEqual(np.prod(p.shape()), len(np_hist.flatten()) * 3)
def setup_system_get_np_hist(self): """ Pick positions and velocities in the original box frame and calculate the np histogram. Then rotate and move the positions and velocities to the frame of the observables. After calculating the core observables, the result should be the same as the np histogram obtained from the original box frame. """ nodes = np.array(np.meshgrid([1, 2], [1, 2], [1, 1, 1, 1, 2])).T.reshape(-1, 3) positions = nodes + 3 * [0.5] velocities = self.calc_vel_at_pos(positions) # get the histogram from numpy pos_cyl = [] for pos in positions: pos_cyl.append( tests_common.transform_pos_from_cartesian_to_polar_coordinates( pos)) np_hist, np_edges = tests_common.get_histogram(np.array(pos_cyl), self.params, 'cylindrical') # the particles only determine the evaluation points, not the values of # the observables np_hist[np.nonzero(np_hist)] = 1 # now align the positions and velocities with the frame of reference # used in the observables pos_aligned = [] vel_aligned = [] for pos, vel in zip(positions, velocities): pos_aligned.append( self.align_with_observable_frame(pos) + self.cyl_transform_params.center) vel_aligned.append(self.align_with_observable_frame(vel)) node_aligned = np.array(np.rint(np.array(pos_aligned) - 3 * [0.5]), dtype=int) self.system.part.add(pos=pos_aligned, v=vel_aligned) self.params['ids'] = self.system.part[:].id for node, vel in zip(node_aligned, vel_aligned): self.lbf[node].velocity = vel return np_hist, np_edges
def setup_system_get_np_hist(self): """ Pick positions and velocities in the original box frame and calculate the np histogram. Then rotate and move the positions and velocities to the frame of the observables. After calculating the core observables, the result should be the same as the np histogram obtained from the original box frame. """ positions, velocities = self.calc_ellipsis_pos_vel( 100, 0.99 * self.params['min_z'], 0.9 * self.params['max_z'], semi_x=0.9 * self.params['max_r'], semi_y=0.2 * self.params['max_r']) # first, get the numpy histogram of the cylinder coordinates pos_cyl = [] for pos in positions: pos_cyl.append( tests_common.transform_pos_from_cartesian_to_polar_coordinates( pos)) np_hist, np_edges = tests_common.get_histogram(np.array(pos_cyl), self.params, 'cylindrical') np_dens = tests_common.normalize_cylindrical_hist( np_hist.copy(), self.params) # now align the positions and velocities with the frame of reference # used in the observables pos_aligned = [] vel_aligned = [] for pos, vel in zip(positions, velocities): pos_aligned.append( self.align_with_observable_frame(pos) + self.cyl_transform_params.center) vel_aligned.append(self.align_with_observable_frame(vel)) self.system.part.add(pos=pos_aligned, v=vel_aligned) self.params['ids'] = self.system.part.all().id return np_dens, np_edges
def test_velocity_profile(self): self.set_fluid_velocities() obs = espressomd.observables.LBVelocityProfile( **LB_VELOCITY_PROFILE_PARAMS) obs_data = obs.calculate() obs_edges = obs.call_method("edges") _, np_edges = tests_common.get_histogram( np.zeros([1, 3]), LB_VELOCITY_PROFILE_PARAMS, 'cartesian', normed=True) for i in range(3): np.testing.assert_array_almost_equal(obs_edges[i], np_edges[i]) for x in range(obs_data.shape[0]): for y in range(obs_data.shape[1]): for z in range(obs_data.shape[2]): self.assertAlmostEqual( obs_data[x, y, z, 0], float(x), places=5) self.assertEqual(np.prod(obs_data.shape), LB_VELOCITY_PROFILE_PARAMS['n_x_bins'] * LB_VELOCITY_PROFILE_PARAMS['n_y_bins'] * LB_VELOCITY_PROFILE_PARAMS['n_z_bins'] * 3)
def calculate_numpy_histogram(self): pol_positions = self.pol_coords() np_hist, np_edges = tests_common.get_histogram(pol_positions, self.params, 'cylindrical') return np_hist, np_edges