def test_reversed(self, n=100): ax = dh.Axis(extent=[-1,1], n=n) axri = dh.Axis(extent=[1,-1], n=n) self.assertEqual(ax.value_to_index(0), axri.value_to_index(0)) axrr = ax.reversed().reversed() self.assertEqual(ax, axrr) self.assertTrue(axri, ax.reversed())
def test_fft_phases(self): spectrum_reference = np.zeros((128,), dtype=np.complex128) spectrum_reference[65] = np.sqrt(2*np.pi) spectrum_reference[69] = 2*np.sqrt(2*np.pi) x = np.linspace(0, 2*np.pi, 128, endpoint=False) if sys.version_info.major == 3 and sys.version_info.minor < 5: y = np.exp(1.j*x) + 2.*np.exp(5.j*x) else: y = ne.evaluate('exp(1.j*x) + 2.*exp(5.j*x)') y = dh.Field(y, axes=[dh.Axis(grid=x)]) yf = y.fft() # this is passed already with the old code self.assertAllClose(spectrum_reference, yf, atol=1e-10) x = np.linspace(0, 2*np.pi, 128, endpoint=False) + 3*np.pi/4 if sys.version_info.major == 3 and sys.version_info.minor < 5: y = np.exp(1.j*x) + 2.*np.exp(5.j*x) else: y = ne.evaluate('exp(1.j*x) + 2.*exp(5.j*x)') y = dh.Field(y, axes=[dh.Axis(grid=x)]) yf = y.fft() # this is passed only with the new implementation self.assertAllClose(spectrum_reference, yf, atol=1e-10)
def test_half_resolution(self): # even number of grid points ax = dh.Axis(extent=(10, 20), n=100) ax = ax.half_resolution() self.assertEqual(len(ax), 50) # odd number of grid points ax = dh.Axis(extent=(10, 20), n=101) ax = ax.half_resolution() self.assertEqual(len(ax), 50)
def test_getitem(self): # even number of grid points ax = dh.Axis(extent=(-1,1), n=100) ax = ax[0.0:1.0] self.assertAllClose(len(ax), 50) self.assertAllClose(ax.grid_node[0], 0) # odd number of grid points ax = dh.Axis(extent=(-1,1), n=101) ax = ax[-0.01: 1] self.assertAllClose(len(ax), 51) self.assertAllClose(ax.grid[0], 0, atol=1e-10)
def test_map_coordinates_cmplx(self): complex_field = self.f2d + 1.j * self.f2d th_axis = dh.Axis(grid = np.linspace(0, 2*np.pi, 100)) r_axis = dh.Axis(grid = np.linspace(0, 1.5, 100)) # this calculates numerical approximation of jacobi determinant and thus also tests # helper.jac_det and # helper.approx_jacobian polar = complex_field.map_coordinates([th_axis, r_axis], helper.polar2linear) a = abs(complex_field).integrate().matrix b = abs(polar).integrate().matrix print(a, b) self.assertAllClose(a, b, rtol=0.01)
def test_map_coordinates(self): a = self.f2d.integrate().matrix th_axis = dh.Axis(grid=np.linspace(0, 2 * np.pi, 100)) r_axis = dh.Axis(grid=np.linspace(0, 1.5, 100)) # this calculates numerical approximation of jacobi determinant and thus also tests # helper.jac_det and # helper.approx_jacobian polar = self.f2d.map_coordinates([th_axis, r_axis], helper.polar2linear) b = polar.integrate().matrix print(a, b) self.assertTrue(np.isclose(a, b, rtol=0.01))
def test_extent_to_slice_even(self, n=100): ax = dh.Axis(extent=[-1,1], n=n) axr = dh.Axis(extent=[1,-1], n=n) self.assertEqual(ax._extent_to_slice((-1,0)), slice(0, 50)) self.assertEqual(ax._extent_to_slice((0,-1)), slice(50, 0, -1)) self.assertEqual(axr._extent_to_slice((-1,0)), slice(100, 50, -1)) self.assertEqual(axr._extent_to_slice((0,-1)), slice(50, 100)) self.assertEqual(ax._extent_to_slice((0,1)), slice(50, 100)) self.assertEqual(ax._extent_to_slice((1,0)), slice(100, 50, -1)) self.assertEqual(axr._extent_to_slice((0,1)), slice(50, 0, -1)) self.assertEqual(axr._extent_to_slice((1,0)), slice(0, 50)) self.assertEqual(ax._extent_to_slice((-1,1)), slice(0, 100)) self.assertEqual(ax._extent_to_slice((1,-1)), slice(100, 0, -1)) self.assertEqual(axr._extent_to_slice((-1,1)), slice(100, 0, -1)) self.assertEqual(axr._extent_to_slice((1,-1)), slice(0, 100))
def test_map_coordinates(self): a = self.f2d.integrate().matrix th_axis = dh.Axis(grid = np.linspace(0, 2*np.pi, 100)) r_axis = dh.Axis(grid = np.linspace(0, 1.5, 100)) # this calculates numerical approximation of jacobi determinant and thus also tests # helper.jac_det and # helper.approx_jacobian polar = self.f2d.map_coordinates([th_axis, r_axis], helper.polar2linear, chunklen=10) b = polar.integrate().matrix self.assertAllClose(a, b, rtol=0.01) polar_serial = self.f2d.map_coordinates([th_axis, r_axis], helper.polar2linear, threads=1) c = polar_serial.integrate().matrix self.assertAllClose(a, c, rtol=0.01) self.assertAllClose(polar, polar_serial)
def setUp(self): self.fempty = dh.Field([]) self.f0d = dh.Field([42.]) m = np.reshape(np.arange(10).astype('d'), 10) self.f1d = dh.Field(m) self.f1dnl = dh.Field(m, axes=[dh.Axis(grid=m**2)]) m = np.reshape(np.arange(20).astype('d'), (4, 5)) self.f2d = dh.Field(m) m = np.reshape(np.arange(60).astype('d'), (4, 5, 3)) self.f3d = dh.Field(m) x, y = helper.meshgrid(np.linspace(0,2*np.pi,100), np.linspace(0,2*np.pi,100), indexing='ij', sparse=True) self.f2d_fine = dh.Field(np.sin(x)*np.cos(y))
def test_equal(self): ax1 = dh.Axis(grid_node = [5,6]) ax2 = dh.Axis(grid_node = [5,6], grid = [5.5]) self.assertEqual(ax1, ax2) ax1 = dh.Axis(grid = [5.5]) ax2 = dh.Axis(grid = [5.1]) self.assertNotEqual(ax1, ax2) ax1 = dh.Axis(grid = [5.5, 6.0]) ax2 = dh.Axis(grid = [5.1]) self.assertNotEqual(ax1, ax2) ax1 = dh.Axis(grid = [5.5, 6.0], extent = [1, 10]) ax2 = dh.Axis(grid = [5.5, 6.0], extent = [1, 11]) self.assertNotEqual(ax1, ax2)
def test_grid_node_spacing(self): ax = dh.Axis(grid_node=[1]) # ok with self.assertRaises(ValueError): ax = dh.Axis(grid_node=[1, 1])
def test_grid(self): ax = dh.Axis(grid = [5, 6]) self.assertEqual(ax.grid[0], 5) self.assertEqual(ax.grid[1], 6)
def test_grid_node(self): ax = dh.Axis(grid_node = [5, 6]) self.assertEqual(ax.grid[0], 5.5) self.assertAllEqual(ax.grid_node, [5, 6])
def test_initiate(self): ax = dh.Axis() self.assertEqual(ax.name, '') self.assertEqual(ax.unit, '')
def test_extent(self): ax = dh.Axis(grid_node = [1, 2.7]) self.assertEqual(ax.extent, (1, 2.7))
def setUp(self): self.ax = dh.Axis(name='name', unit='unit')
def test_init(self): with self.assertRaises(TypeError): dh.Axis(extent=(0,1), n=99, unknownarg=0)
def setUp(self): self.ax = dh.Axis(name='name', unit='unit', grid_node=np.sin(np.linspace(-np.pi/2, np.pi/2, 102)))
def test_initiate(self): ax = dh.Axis(extent=(-1,1), n=101) self.assertEqual(ax.name, '') self.assertEqual(ax.unit, '')
def setUp(self): self.ax = dh.Axis(name='name', unit='unit', extent=[-1,1], n=101)