def test11(self): grid1 = datamodel.new_regular_grid((4, 4), [1.0, 1.0] | units.m) grid2 = datamodel.new_regular_grid((4, 4), [1.0, 1.0] | units.m, offset=[-0.5, -0.5] | units.m) self.assertTrue(grid1.overlaps(grid2)) overlap = grid1.get_overlap_with(grid2) self.assertEquals(overlap.position, grid1[0:3, 0:3].position)
def test12(self): grid1 = datamodel.new_regular_grid((4, 4), [1.0, 1.0] | units.m) grid2 = datamodel.new_regular_grid((4, 4), [1.0, 1.0] | units.m, offset=[-0.5, -0.5] | units.m) self.assertTrue(grid1.overlaps(grid2)) overlap = grid1.get_overlap_with(grid2, eps=grid2.cellsize()[0] * 1.e-6) self.assertEquals(overlap.position, grid1[0:2, 0:2].position)
def test2(self): grid = datamodel.new_rectilinear_grid((10,), (1.0 * numpy.arange(11),)) self.assertEqual(grid._axes_cell_boundaries, 1.0 * numpy.arange(11)) grid = datamodel.new_regular_grid((10,), [10.0]) self.assertEqual(grid._lengths, [10.0]) grid = datamodel.new_cartesian_grid((10,), 1.0) self.assertEqual(grid._cellsize, 1.0) grid = datamodel.new_regular_grid((10, 20), [10.0, 15.0]) self.assertEquals(grid._lengths, [10.0, 15.0])
def test3(self): grid1 = datamodel.new_regular_grid((5, 5, 5), [10.0, 10.0, 10.0] | units.m) grid1.mass = grid1.x.value_in(units.m) | units.kg grid2 = datamodel.new_regular_grid((5, 5, 5), [10.0, 10.0, 10.0] | units.m) grid2.position += (10.0, 0, 0) | units.m grid2.mass = grid2.x.value_in(units.m) | units.kg samples = SamplePointsOnMultipleGrids((grid1, grid2), [[3.0, 3.0, 3.0], [4.0, 3.0, 3.0], [13, 3, 3]] | units.m) self.assertEquals(len(samples), 3) self.assertEquals(samples.mass, [3.0, 4.0, 13.0] | units.kg)
def test10(self): grid1 = datamodel.new_regular_grid((5, 4), [1.0, 1.0] | units.m) grid2 = datamodel.new_regular_grid((5, 4), [0.1, 0.1] | units.m) grid3 = datamodel.new_regular_grid((5, 4), [0.1, 0.1] | units.m, offset=[0.5, 0.6] | units.m) self.assertTrue(grid1.overlaps(grid2)) self.assertTrue(grid1.overlaps(grid3)) self.assertFalse(grid2.overlaps(grid3)) self.assertTrue(grid2.overlaps(grid1)) self.assertTrue(grid3.overlaps(grid1)) self.assertFalse(grid3.overlaps(grid2))
def test22(self): grid1 = datamodel.new_regular_grid((5, 4, 2), [1.0, 1.0, 1.0] | units.m) grid2 = datamodel.new_regular_grid((5, 4, 2), [1.0, 1.0, 1.0] | units.m) slice1 = grid1[1:3, :, :].copy() slice2 = grid2[1:3, :, :] slice1.x = -10 | units.m channel = slice1.new_channel_to(slice2) channel.copy() self.assertEquals(grid2.x[1:3], -10 | units.m) self.assertEquals(grid2.x[4], grid1.x[4])
def test10(self): grid1 = datamodel.new_regular_grid((5, 4), [1.0, 1.0] | units.m) grid2 = datamodel.new_regular_grid((5, 4), [.1, .1] | units.m) grid3 = datamodel.new_regular_grid((5, 4), [.1, .1] | units.m, offset=[0.5, 0.6] | units.m) self.assertTrue(grid1.overlaps(grid2)) self.assertTrue(grid1.overlaps(grid3)) self.assertFalse(grid2.overlaps(grid3)) self.assertTrue(grid2.overlaps(grid1)) self.assertTrue(grid3.overlaps(grid1)) self.assertFalse(grid3.overlaps(grid2))
def test46(self): grid = datamodel.new_regular_grid((6, ), [1.0], axes_names="abcd") subgrid = grid[::2] self.assertEquals(subgrid[-2].a, subgrid.a[-2]) grid = datamodel.new_regular_grid((7, ), [1.0], axes_names="abcd") subgrid = grid[::2] self.assertEquals(subgrid[-2].a, subgrid.a[-2]) grid = datamodel.new_regular_grid((7, ), [1.0], axes_names="abcd") subgrid = grid[6:0:-2] self.assertEquals(subgrid[-2].a, subgrid.a[-2])
def test46(self): grid = datamodel.new_regular_grid((6,), [1.0], axes_names="abcd") subgrid = grid[::2] self.assertEquals(subgrid[-2].a, subgrid.a[-2]) grid = datamodel.new_regular_grid((7,), [1.0], axes_names="abcd") subgrid = grid[::2] self.assertEquals(subgrid[-2].a, subgrid.a[-2]) grid = datamodel.new_regular_grid((7,), [1.0], axes_names="abcd") subgrid = grid[6:0:-2] self.assertEquals(subgrid[-2].a, subgrid.a[-2])
def test3(self): grid1 = datamodel.new_regular_grid((5, 5, 5), [10.0, 10.0, 10.0] | units.m) grid1.mass = grid1.x.value_in(units.m) | units.kg grid2 = datamodel.new_regular_grid((5, 5, 5), [10.0, 10.0, 10.0] | units.m) grid2.position += (10.0, 0, 0) | units.m grid2.mass = grid2.x.value_in(units.m) | units.kg samples = SamplePointsOnMultipleGrids( (grid1, grid2), [[3.0, 3.0, 3.0], [4.0, 3.0, 3.0], [13, 3, 3]] | units.m) self.assertEquals(len(samples), 3) self.assertEquals(samples.mass, [3.0, 4.0, 13.0] | units.kg)
def test2(self): grid = datamodel.new_rectilinear_grid((10, ), (1. * numpy.arange(11), )) self.assertEqual(grid._axes_cell_boundaries, 1. * numpy.arange(11)) grid = datamodel.new_regular_grid((10, ), [10.]) self.assertEqual(grid._lengths, [10.]) grid = datamodel.new_cartesian_grid((10, ), 1.) self.assertEqual(grid._cellsize, 1.) grid = datamodel.new_regular_grid(( 10, 20, ), [10., 15.]) self.assertEquals(grid._lengths, [10., 15.])
def test40(self): grid1 = datamodel.new_regular_grid((5, 4, 2), [1.0, 1.0, 1.0] | units.m) grid2 = datamodel.new_regular_grid((5, 4, 2), [1.0, 1.0, 1.0] | units.m) grid1.m1 = 1 grid1.m2 = 2 channel = grid1.new_channel_to(grid2) channel.transform(["m3"], lambda x, y: (x,), ["m1", "m2"]) self.assertEquals(grid2.m3, 1) channel.transform(["m3"], lambda x, y: (y,), ["m1", "m2"]) self.assertEquals(grid2.m3, 2) channel.transform(["m3"], lambda x, y: (x + y,), ["m1", "m2"]) self.assertEquals(grid2.m3, 3) channel.transform(["m3", "m4"], lambda x, y: (x + y, 2 * x - y), ["m1", "m2"]) self.assertEquals(grid2.m3, 3) self.assertEquals(grid2.m4, 0)
def test40b(self): grid = datamodel.new_regular_grid((50,), [1.0] | units.m) for index in [[0], [0, 3, 4], [1, 2, 2], [[2, 3]], [[0, 1], [2, 3]], range(50)]: i = numpy.array(index) self.assertEquals(grid[i].x, grid.x[i]) self.assertEquals(grid[i][1:].x, grid.x[i][1:]) self.assertEquals(grid[i][1::2].x, grid.x[i][1::2])
def test2(self): grid = datamodel.new_regular_grid([2, 2, 2], [1, 1, 1] | generic_unit_system.length) grid.rho = grid.x * ( 0.1 | generic_unit_system.mass / generic_unit_system.length**4) data_file = StringIO() instance = vtk.VtkUnstructuredGrid("test.vtu", data_file, grid) instance.store() contents = data_file.getvalue() self.assertTrue( contents.find('<Piece NumberOfPoints="27" NumberOfCells="8"') > 0) self.assertTrue( contents.find( 'DataArray type="Int32" NumberOfComponents="1" Name="connectivity">' ) > 0) self.assertTrue( contents.find( 'DataArray type="Int32" NumberOfComponents="1" Name="offsets">' ) > 0) self.assertTrue(contents.find('11 11 11') > 0) self.assertTrue(contents.find('56 64') > 0) self.assertTrue( contents.find( 'DataArray type="Float64" NumberOfComponents="3" Name="points">' ) > 0) self.assertTrue( contents.find( '<DataArray type="Float64" NumberOfComponents="1" Name="rho">') > 0)
def test4(self): grid = datamodel.new_regular_grid((5, 5, 5), [10.0, 10.0, 10.0] | units.m) grid.mass = grid.x.value_in(units.m) | units.kg sample = grid.samplePoint([3.0, 3.0, 3.0] | units.m) self.assertEquals(sample.surrounding_cells[0].position, [3.0, 3.0, 3.0] | units.m) self.assertEquals(sample.surrounding_cells[1].position, [5.0, 3.0, 3.0] | units.m) self.assertEquals(sample.surrounding_cells[-1].position, [5.0, 5.0, 5.0] | units.m)
def test6(self): grid = datamodel.new_regular_grid((5, 4, 2), [1.0, 1.0, 1.0] | units.m) points = grid.points().reshape([6 * 5 * 3, 3]) connectivity = grid.connectivity() self.assertEquals(connectivity.shape, (5, 4, 2, 8)) cell = connectivity[1][1][1] self.assertAlmostRelativeEquals(points[cell[0]], ([0.2, 0.25, 0.5] | units.m) + ([0.0, 0.0, 0.0] | units.m)) self.assertAlmostRelativeEquals(points[cell[1]], ([0.2, 0.25, 0.5] | units.m) + ([0.2, 0, 0.0] | units.m)) self.assertAlmostRelativeEquals(points[cell[2]], ([0.2, 0.25, 0.5] | units.m) + ([0, 0.25, 0.0] | units.m)) self.assertAlmostRelativeEquals(points[cell[3]], ([0.2, 0.25, 0.5] | units.m) + ([0.2, 0.25, 0.0] | units.m)) self.assertAlmostRelativeEquals(points[cell[4]], ([0.2, 0.25, 0.5] | units.m) + ([0.0, 0.0, 0.5] | units.m)) self.assertAlmostRelativeEquals(points[cell[5]], ([0.2, 0.25, 0.5] | units.m) + ([0.2, 0.0, 0.5] | units.m)) self.assertAlmostRelativeEquals(points[cell[6]], ([0.2, 0.25, 0.5] | units.m) + ([0.0, 0.25, 0.5] | units.m)) self.assertAlmostRelativeEquals(points[cell[7]], ([0.2, 0.25, 0.5] | units.m) + ([0.2, 0.25, 0.5] | units.m)) self.assertEquals(connectivity[0][0][0], [0, 15, 3, 18, 1, 16, 4, 19])
def test1(self): grid1 = datamodel.new_cartesian_grid((4, 5), 1.0 | units.m) grid2 = datamodel.new_regular_grid((4, 5), [4.0, 5.0] | units.m) grid3 = datamodel.new_rectilinear_grid((4, 5), [numpy.arange(5.0) | units.m, numpy.arange(6.0) | units.m]) self.assertEqual(grid1.position, grid2.position) self.assertEqual(grid2.position, grid3.position)
def test2(self): grid = datamodel.new_regular_grid((5, 5, 5), [10.0, 10.0, 10.0] | units.m) grid.mass = grid.x.value_in(units.m) | units.kg sample = grid.samplePoint([3.0, 3.0, 3.0] | units.m) self.assertEquals(sample.index_for_000_cell, [1, 1, 1]) sample = grid.samplePoint([2.5, 2.5, 2.5] | units.m) self.assertEquals(sample.index_for_000_cell, [0, 0, 0]) sample = grid.samplePoint([3.5, 3.5, 3.5] | units.m) self.assertEquals(sample.index_for_000_cell, [1, 1, 1]) sample = grid.samplePoint([4.5, 4.5, 4.5] | units.m) self.assertEquals(sample.index_for_000_cell, [1, 1, 1]) self.assertEquals(sample.index, [2, 2, 2]) for x in range(0, 100): sample = grid.samplePoint( [0.0 + (x / 100.0), 4.0 + (x / 100.0), 6.0 + (x / 100.0)] | units.m) self.assertEquals(sample.index_for_000_cell, [-1, 1, 2]) for x in range(100, 300): sample = grid.samplePoint( [0.0 + (x / 100.0), 4.0 + (x / 100.0), 6.0 + (x / 100.0)] | units.m) self.assertEquals(sample.index_for_000_cell, [0, 2, 3]) for x in range(300, 400): sample = grid.samplePoint( [0.0 + (x / 100.0), 4.0 + (x / 100.0), 6.0 + (x / 100.0)] | units.m) self.assertEquals(sample.index_for_000_cell, [1, 3, 4])
def test2(self): grid = datamodel.new_regular_grid((5, 5, 5), [10.0, 10.0, 10.0] | units.m) grid.mass = grid.x.value_in(units.m) | units.kg sample = grid.samplePoint([3.0, 3.0, 3.0] | units.m) self.assertEquals(sample.index_for_000_cell, [1, 1, 1]) sample = grid.samplePoint([2.5, 2.5, 2.5] | units.m) self.assertEquals(sample.index_for_000_cell, [0, 0, 0]) sample = grid.samplePoint([3.5, 3.5, 3.5] | units.m) self.assertEquals(sample.index_for_000_cell, [1, 1, 1]) sample = grid.samplePoint([4.5, 4.5, 4.5] | units.m) self.assertEquals(sample.index_for_000_cell, [1, 1, 1]) self.assertEquals(sample.index, [2, 2, 2]) for x in range(0, 100): sample = grid.samplePoint([0.0 + (x / 100.0), 4.0 + (x / 100.0), 6.0 + (x / 100.0)] | units.m) self.assertEquals(sample.index_for_000_cell, [-1, 1, 2]) for x in range(100, 300): sample = grid.samplePoint([0.0 + (x / 100.0), 4.0 + (x / 100.0), 6.0 + (x / 100.0)] | units.m) self.assertEquals(sample.index_for_000_cell, [0, 2, 3]) for x in range(300, 400): sample = grid.samplePoint([0.0 + (x / 100.0), 4.0 + (x / 100.0), 6.0 + (x / 100.0)] | units.m) self.assertEquals(sample.index_for_000_cell, [1, 3, 4])
def test5(self): grid = datamodel.new_regular_grid((5, 5, 5), [10.0, 10.0, 10.0] | units.m) grid.mass = grid.x.value_in(units.m) | units.kg sample = grid.samplePoint([3.0, 3.0, 3.0] | units.m) masses = sample.get_values_of_attribute("mass") self.assertEquals(masses[0], 3.0 | units.kg) self.assertEquals(masses[1], 5.0 | units.kg) self.assertEquals(masses[2], 3.0 | units.kg) self.assertEquals(masses[3], 3.0 | units.kg) self.assertEquals(masses[4], 5.0 | units.kg) self.assertEquals(masses[5], 3.0 | units.kg) self.assertEquals(masses[6], 5.0 | units.kg) self.assertEquals(masses[7], 5.0 | units.kg) factors = sample.weighing_factors self.assertEquals(factors[0], 1.0 | units.none) self.assertEquals(factors[1], 0.0 | units.none) self.assertEquals(factors[2], 0.0 | units.none) self.assertEquals(factors[3], 0.0 | units.none) self.assertEquals(factors[4], 0.0 | units.none) self.assertEquals(factors[5], 0.0 | units.none) self.assertEquals(factors[6], 0.0 | units.none) self.assertEquals(factors[7], 0.0 | units.none) self.assertAlmostRelativeEquals(sample.mass, 3.0 | units.kg)
def test6(self): grid = datamodel.new_regular_grid((5, 5, 5), [10.0, 10.0, 10.0] | units.m) grid.mass = grid.x.value_in(units.m) | units.kg for xpos in numpy.arange(3.0, 5.0, 0.1): sample = grid.samplePoint([xpos, 3.0, 3.0] | units.m) self.assertAlmostRelativeEquals( sample.mass, (3.0 | units.kg) + ((2.0 * (xpos - 3.0) / 2.0) | units.kg)) sample = grid.samplePoint([xpos, 3.0, 3.0] | units.m) self.assertAlmostRelativeEquals( sample.mass, (3.0 | units.kg) + ((2.0 * (xpos - 3.0) / 2.0) | units.kg)) sample = grid.samplePoint([xpos, 5.0, 3.0] | units.m) self.assertAlmostRelativeEquals( sample.mass, (3.0 | units.kg) + ((2.0 * (xpos - 3.0) / 2.0) | units.kg)) sample = grid.samplePoint([xpos, 3.0, 5.0] | units.m) self.assertAlmostRelativeEquals( sample.mass, (3.0 | units.kg) + ((2.0 * (xpos - 3.0) / 2.0) | units.kg)) sample = grid.samplePoint([4.0, 4.0, 4.0] | units.m) self.assertAlmostRelativeEquals(sample.mass, (4.0 | units.kg))
def test45(self): grid = datamodel.new_regular_grid((3, 4, 5, 6), [1.0, 2.0, 3.0, 4.0], axes_names="abcd") for _i, _j in [([-1], [-4])]: i = numpy.array(_i) j = numpy.array(_j) subgrid = grid[::2, 0, :, :] self.assertEquals(subgrid[i, j].a, subgrid.a[i, j])
def test18(self): instance = self.new_instance_of_an_optional_code(MpiAmrVac, number_of_workers=3) instance.parameters.x_boundary_conditions = ("periodic", "periodic") instance.parameters.y_boundary_conditions = ("periodic", "periodic") instance.parameters.z_boundary_conditions = ("periodic", "periodic") instance.parameters.mesh_length = (20.0, 20.0, 20.0) | generic_unit_system.length instance.parameters.mesh_length = (20.0, 20.0, 20.0) | generic_unit_system.length instance.parameters.mesh_size = (20, 20, 20) instance.parameters.maximum_number_of_grid_levels = 1 for x in instance.itergrids(): inmem = x.copy() inmem.rho = ((inmem.x + ((inmem.y - (0.5 | generic_unit_system.length)) * 20.0) + ((inmem.z - (0.5 | generic_unit_system.length)) * 400.0)) / (1 | generic_unit_system.length) | generic_unit_system.density) inmem.rhovx = 0.0 | generic_unit_system.momentum_density inmem.energy = 1.0 | generic_unit_system.energy_density from_model_to_code = inmem.new_channel_to(x) from_model_to_code.copy() rho, rhovx, rhovy, rhovx, rhoenergy = instance.get_hydro_state_at_point( 0.5 | generic_unit_system.length, 0.5 | generic_unit_system.length, 0.5 | generic_unit_system.length) self.assertEquals(rho, 0.5 | generic_unit_system.density) for value in numpy.arange(0.5, 19.6, 0.1): rho, rhovx, rhovy, rhovx, rhoenergy = instance.get_hydro_state_at_point( value | generic_unit_system.length, 0.5 | generic_unit_system.length, 0.5 | generic_unit_system.length) self.assertAlmostRelativeEquals( rho, value | generic_unit_system.density) sample = sample = datamodel.new_regular_grid( (4, 4, 76), (2, 2, 19) | generic_unit_system.length) sample.x += 9.5 | generic_unit_system.length sample.y += 9.5 | generic_unit_system.length sample.z += 0.5 | generic_unit_system.length x = sample.x.flatten() y = sample.y.flatten() z = sample.z.flatten() rho, rhovx, rhovy, rhovx, rhoenergy = instance.get_hydro_state_at_point( x, y, z) half = 0.5 | generic_unit_system.length self.assertAlmostRelativeEquals( rho, (x + (20 * (y - half)) + (400 * (z - half))) / (1 | generic_unit_system.length) | generic_unit_system.density)
def test20(self): grid = datamodel.new_regular_grid((5, 4, 2), [1.0, 1.0, 1.0] | units.m) self.assertEquals(grid[1:3, :, :].x, grid.x[1:3, :, :]) self.assertEquals(grid[1:3, 2:3, :].x, grid.x[1:3, 2:3, :]) self.assertEquals(grid[1:3, 2:3, 0:1].x, grid.x[1:3, 2:3, 0:1]) self.assertEquals(grid[1:3, :, 0:1].x, grid.x[1:3, :, 0:1]) self.assertEquals(grid[:, :, 0:1].x, grid.x[:, :, 0:1]) self.assertEquals(grid[:, 2:3, 0:1].x, grid.x[:, 2:3, 0:1])
def test2(self): grid = datamodel.new_regular_grid((5, 5, 5), [10.0, 10.0, 10.0] | units.m) grid.mass = grid.x.value_in(units.m) | units.kg samples = grid.samplePoints([[3.5, 3.0, 3.0], [4.5, 3.0, 3.0]] | units.m) self.assertEquals(len(samples), 2) self.assertEquals(samples.mass, [3.5, 4.5] | units.kg)
def test17(self): grid = datamodel.new_regular_grid((4, 2), [1.0 | units.m, 1.0 | units.m]) self.assertEquals(grid.shape, (4, 2)) self.assertEquals(grid.x.shape, (4, 2)) self.assertAlmostRelativeEquals(grid[1].x, ([0.375] * 2) | units.m) self.assertAlmostRelativeEquals(grid[1][1].y, 0.75 | units.m) self.assertEquals(grid[0].x, grid.x[0]) self.assertEquals(grid[0][1].x, grid.x[0][1])
def test2(self): grid = datamodel.new_regular_grid((5, 4, 2), [1.0, 1.0, 1.0] | units.m) self.assertTrue( numpy.all( grid.contains([[0.5, 0.5, 0.5], [0.1, 0.1, 0.1]] | units.m))) self.assertFalse( numpy.all( grid.contains([[1.1, 0.5, 0.5], [0.1, 1.1, 0.1]] | units.m)))
def test19(self): grid = datamodel.new_regular_grid((5, 4, 2), [1.0, 1.0, 1.0] | units.m) self.assertEquals(grid[1:3, ..., ...].x, grid.x[1:3, ..., ...]) self.assertEquals(grid[1:3, 2:3, ...].x, grid.x[1:3, 2:3, ...]) self.assertEquals(grid[1:3, 2:3, 0:1].x, grid.x[1:3, 2:3, 0:1]) self.assertEquals(grid[1:3, ..., 0:1].x, grid.x[1:3, ..., 0:1]) self.assertEquals(grid[..., ..., 0:1].x, grid.x[..., ..., 0:1]) self.assertEquals(grid[..., 2:3, 0:1].x, grid.x[..., 2:3, 0:1])
def test8(self): grid = datamodel.new_regular_grid((5, 4), [1.0, 1.0] | units.m) self.assertAlmostRelativeEquals(grid.get_minimum_position(), ([0.0, 0.0] | units.m)) self.assertAlmostRelativeEquals(grid.get_maximum_position(), [1.0, 1.0] | units.m) self.assertAlmostRelativeEquals(grid.get_volume(), 1.0 | units.m**2) self.assertTrue(grid.contains([0.5, 0.5] | units.m))
def test12(self): grid = datamodel.new_regular_grid((5, 4, 2), [1.0, 1.0, 1.0] | units.m) self.assertEquals(grid[grid.x > 0.4 | units.m].x.shape, (24,)) self.assertEquals(grid[grid.x > 0.4 | units.m].x, grid.x[grid.x > 0.4 | units.m]) iarray, jarray, karray = grid.indices() self.assertEquals(grid[grid.x > 0.4 | units.m].indices()[0], iarray[grid.x > 0.4 | units.m]) self.assertEquals(grid[grid.x > 0.4 | units.m].indices()[1], jarray[grid.x > 0.4 | units.m]) self.assertEquals(grid[grid.x > 0.4 | units.m].indices()[2], karray[grid.x > 0.4 | units.m])
def test41(self): grid = datamodel.new_regular_grid((10, 10), [1.0, 2.0] | units.m) for _i, _j in [([0, 1], [2, 3])]: i = numpy.array(_i) j = numpy.array(_j) self.assertEquals(grid[i, j].x, grid.x[i, j]) self.assertEquals(grid[i, j][1:].x, grid.x[i, j][1:]) self.assertEquals(grid[i, j][1::2].x, grid.x[i, j][1::2])
def test40b(self): grid = datamodel.new_regular_grid((50, ), [1.0] | units.m) for index in [[0], [0, 3, 4], [1, 2, 2], [[2, 3]], [[0, 1], [2, 3]], range(50)]: i = numpy.array(index) self.assertEquals(grid[i].x, grid.x[i]) self.assertEquals(grid[i][1:].x, grid.x[i][1:]) self.assertEquals(grid[i][1::2].x, grid.x[i][1::2])
def test18(self): instance=self.new_instance_of_an_optional_code(MpiAmrVac, number_of_workers=3) instance.parameters.x_boundary_conditions = ("periodic","periodic") instance.parameters.y_boundary_conditions = ("periodic","periodic") instance.parameters.z_boundary_conditions = ("periodic","periodic") instance.parameters.mesh_length = (20.0, 20.0, 20.0) | generic_unit_system.length instance.parameters.mesh_length = (20.0, 20.0, 20.0) | generic_unit_system.length instance.parameters.mesh_size = (20, 20, 20) instance.parameters.maximum_number_of_grid_levels = 1 for x in instance.itergrids(): inmem = x.copy() inmem.rho = ( ( inmem.x + ((inmem.y - (0.5| generic_unit_system.length))* 20.0) + ((inmem.z - (0.5| generic_unit_system.length))* 400.0) ) /(1| generic_unit_system.length) | generic_unit_system.density ) inmem.rhovx = 0.0 | generic_unit_system.momentum_density inmem.energy = 1.0 | generic_unit_system.energy_density from_model_to_code = inmem.new_channel_to(x) from_model_to_code.copy() rho, rhovx, rhovy, rhovx, rhoenergy = instance.get_hydro_state_at_point(0.5| generic_unit_system.length,0.5| generic_unit_system.length,0.5| generic_unit_system.length) self.assertEquals(rho , 0.5 | generic_unit_system.density) for value in numpy.arange(0.5, 19.6, 0.1): rho, rhovx, rhovy, rhovx, rhoenergy = instance.get_hydro_state_at_point( value | generic_unit_system.length, 0.5 | generic_unit_system.length, 0.5 | generic_unit_system.length ) self.assertAlmostRelativeEquals(rho , value | generic_unit_system.density) sample = sample = datamodel.new_regular_grid( (4, 4, 76), (2, 2, 19) | generic_unit_system.length ) sample.x += 9.5 | generic_unit_system.length sample.y += 9.5 | generic_unit_system.length sample.z += 0.5 | generic_unit_system.length x = sample.x.flatten() y = sample.y.flatten() z = sample.z.flatten() rho, rhovx, rhovy, rhovx, rhoenergy = instance.get_hydro_state_at_point( x, y, z ) half = 0.5 | generic_unit_system.length self.assertAlmostRelativeEquals(rho , (x + (20 * (y-half)) + (400 * (z-half)))/(1| generic_unit_system.length) | generic_unit_system.density )
def test11(self): instance=self.new_instance_of_an_optional_code(MpiAmrVac) instance.parameters.mesh_size = (10,10,10) instance.parameters.mesh_length = [1.0, 1.0, 1.0] | generic_unit_system.length instance.parameters.maximum_number_of_grid_levels = 1 instance.parameters.x_boundary_conditions = ("periodic", "periodic") instance.parameters.y_boundary_conditions = ("periodic", "periodic") instance.parameters.z_boundary_conditions = ("periodic", "periodic") instance.parameters.stopping_conditions_timeout = 0.1 | units.s gamma = 5.0 / 3.0 grid = datamodel.new_regular_grid((10,10,10), [1.0, 1.0, 1.0] | generic_unit_system.length ) density = generic_unit_system.density momentum = generic_unit_system.speed * generic_unit_system.density energy = generic_unit_system.mass / ((generic_unit_system.time**2) * generic_unit_system.length) grid.rho = 0.01 | density grid.rhovx = 0.1 | momentum grid.rhovy = 0.0 | momentum grid.rhovz = 0.0 | momentum grid[0:5].rho = 0.015 | density p = 1.0 | (generic_unit_system.mass / (generic_unit_system.length * generic_unit_system.time**2)) grid.energy = p / (gamma - 1) grid.energy += 0.5 * (grid.rhovx ** 2 + grid.rhovy ** 2 + grid.rhovz ** 2) / grid.rho grids = list(instance.itergrids()) self.assertEquals(len(grids), 1) igrid = grids[0] channel = grid.new_channel_to(igrid) channel.copy() instance.stopping_conditions.timeout_detection.enable() instance.evolve_model(1.0 | generic_unit_system.time) self.assertTrue(instance.stopping_conditions.timeout_detection.is_set()) rho = instance.itergrids().next().rho[...,0,0] print rho print instance.model_time self.assertAlmostRelativeEquals(rho.mean(), 0.0125 | density) self.assertTrue( instance.model_time < 1.0 | generic_unit_system.time) instance.stopping_conditions.timeout_detection.disable() print instance.model_time.round(2) tnext = instance.model_time.round(2) + (0.2 | generic_unit_system.time) instance.evolve_model(tnext) rho = instance.itergrids().next().rho[...,0,0] print rho self.assertAlmostRelativeEquals( instance.model_time.round(2) ,tnext) self.assertAlmostRelativeEquals(rho.mean(), 0.0125 | density) self.assertTrue(rho[0] < 0.015 | density) instance.stop()
def test3(self): grid = datamodel.new_regular_grid((5, 5, 5), [10.0, 10.0, 10.0] | units.m) grid.mass = grid.x.value_in(units.m) | units.kg sample = grid.samplePoint([3.0, 3.0, 3.0] | units.m) self.assertEquals(sample.index_for_000_cell, [1, 1, 1]) self.assertEquals( sample.surrounding_cell_indices, [[1, 1, 1], [2, 1, 1], [1, 2, 1], [1, 1, 2], [2, 1, 2], [1, 2, 2], [2, 2, 1], [2, 2, 2]], )
def test42(self): grid = datamodel.new_regular_grid((3, 4, 5, 6), [1.0, 2.0, 3.0, 4.0], axes_names="abcd") for _i, _j in [([0], [3]), ([0, 1], [2, 3])]: i = numpy.array(_i) j = numpy.array(_j) self.assertEquals(grid[i, j].a, grid.a[i, j]) self.assertEquals(grid[0, 1, i, j].a, grid.a[0, 1, i, j]) self.assertEquals(grid[i, j, 0, 0].a, grid.a[i, j, 0, 0]) self.assertEquals(grid[i, 1, 2, j].a, grid.a[i, 1, 2, j])
def test7(self): grid = datamodel.new_regular_grid((5, 5, 5), [10.0, 10.0, 10.0] | units.m) grid.mass = grid.x.value_in(units.m) | units.kg sample = grid.samplePoint([3.0, 3.0, 3.0] | units.m) self.assertTrue(sample.isvalid) sample = grid.samplePoint([11.0, 3.0, 3.0] | units.m) self.assertFalse(sample.isvalid) sample = grid.samplePoint([3.0, -1.0, 3.0] | units.m) self.assertFalse(sample.isvalid)
def test18(self): grid = datamodel.new_regular_grid((5, 4, 2), [1.0, 1.0, 1.0] | units.m) self.assertEquals(grid.shape, (5, 4, 2)) self.assertEquals(grid[1:2, ..., ...].x.shape, (1, 4, 2)) self.assertEquals(grid[1:2, ..., ...].shape, (1, 4, 2)) self.assertEquals(grid[1:2, ..., ...].x, grid.x[1:2, ..., ...]) self.assertEquals(grid[1:3, ..., ...].x.shape, (2, 4, 2)) self.assertEquals(grid[1:3, ..., ...].shape, (2, 4, 2)) self.assertEquals(grid[1:3, ..., ...].x, grid.x[1:3, ..., ...])
def test9(self): grid = datamodel.new_regular_grid((5, 5, 5), [10.0, 10.0, 10.0] | units.m) grid.mass = grid.x.value_in(units.m) | units.kg sample = grid.samplePoint([3.0, 3.0, 3.0] | units.m, must_return_values_on_cell_center=False) self.assertEquals(sample.position, [3.0, 3.0, 3.0] | units.m) self.assertEquals(sample.mass, 3.0 | units.kg) sample = grid.samplePoint([3.5, 3.0, 3.0] | units.m, must_return_values_on_cell_center=False) self.assertEquals(sample.position, [3.5, 3.0, 3.0] | units.m) self.assertEquals(sample.mass, 3.5 | units.kg)
def test1(self): grid = datamodel.new_regular_grid((5, 5, 5), [10.0, 10.0, 10.0] | units.m) grid.mass = grid.x.value_in(units.m) | units.kg samples = grid.samplePoints([[3.0, 3.0, 3.0], [4.0, 3.0, 3.0]] | units.m) self.assertEquals(len(samples), 2) self.assertEquals(samples.position[0], [3.0, 3.0, 3.0] | units.m) self.assertEquals(samples.position[0], samples[0].position) self.assertEquals(samples.position[1], samples[1].position) self.assertEquals(samples.mass, [3.0, 4.0] | units.kg)
def test1(self): grid1 = datamodel.new_cartesian_grid((4, 5), 1.0 | units.m) grid2 = datamodel.new_regular_grid((4, 5), [4.0, 5.0] | units.m) grid3 = datamodel.new_rectilinear_grid( (4, 5), [numpy.arange(5.) | units.m, numpy.arange(6.) | units.m]) self.assertEqual(grid1.position, grid2.position) self.assertEqual(grid2.position, grid3.position)
def test40(self): grid1 = datamodel.new_regular_grid((5, 4, 2), [1.0, 1.0, 1.0] | units.m) grid2 = datamodel.new_regular_grid((5, 4, 2), [1.0, 1.0, 1.0] | units.m) grid1.m1 = 1 grid1.m2 = 2 channel = grid1.new_channel_to(grid2) channel.transform(["m3"], lambda x, y: (x, ), ["m1", "m2"]) self.assertEquals(grid2.m3, 1) channel.transform(["m3"], lambda x, y: (y, ), ["m1", "m2"]) self.assertEquals(grid2.m3, 2) channel.transform(["m3"], lambda x, y: (x + y, ), ["m1", "m2"]) self.assertEquals(grid2.m3, 3) channel.transform(["m3", "m4"], lambda x, y: (x + y, 2 * x - y), ["m1", "m2"]) self.assertEquals(grid2.m3, 3) self.assertEquals(grid2.m4, 0)
def test7(self): grid = datamodel.new_regular_grid((5, 5, 5), [10.0, 10.0, 10.0] | units.m) self.assertAlmostRelativeEquals(grid[1][2][3].position, [3, 5, 7] | units.m) grid[1][2][3].position = [7, 5, 3] | units.m self.assertAlmostRelativeEquals(grid[1][2][3].position, [7, 5, 3] | units.m) grid[1][2][3].position += [1, 2, 3] | units.m self.assertAlmostRelativeEquals(grid[1][2][3].position, [8, 7, 6] | units.m)
def test16(self): grid = datamodel.new_regular_grid((5,4,2,4), [1.0 | units.m, 1.0 | units.m, 1.0 | units.m, 1.0 | units.s], ('x', 'y', 'z', 't') ) self.assertEquals(grid.shape, (5,4,2,4)) self.assertEquals(grid.x.shape, (5,4,2,4)) self.assertAlmostRelativeEquals( grid[1][2][1].x, ([0.3] * 4) | units.m) self.assertAlmostRelativeEquals( grid[1][2][1].t, [0.125, 0.375, 0.625, 0.875] | units.s) self.assertEquals(grid[0].x, grid.x[0]) self.assertEquals(grid[0][1].x, grid.x[0][1]) self.assertEquals(grid[1][2][1].x, grid.x[1][2][1]) self.assertEquals(grid[1][2][1][2].x, grid.x[1][2][1][2])
def test3(self): grid = datamodel.new_regular_grid((5, 4, 2), [1.0, 1.0, 1.0] | units.m) points = grid.points() self.assertEquals(points.shape, (6, 5, 3, 3)) self.assertAlmostRelativeEquals(points[0][0][0], ([0.0, 0.0, 0.0] | units.m)) self.assertAlmostRelativeEquals(points[1][0][0], ([0.2, 0.0, 0.0] | units.m)) self.assertAlmostRelativeEquals(points[1][1][1], [0.2, 0.25, 0.5] | units.m) self.assertAlmostRelativeEquals(points[0][-1][-1], ([0.0, 1.0, 1.0] | units.m)) self.assertAlmostRelativeEquals(points[-1][0][-1], ([1.0, 0.0, 1.0] | units.m)) self.assertAlmostRelativeEquals(points[-1][-1][0], ([1.0, 1.0, 0.0] | units.m)) self.assertAlmostRelativeEquals(points[-1][-1][-1], ([1.0, 1.0, 1.0] | units.m))
def test9(self): grid = datamodel.new_regular_grid((5, 4, 2), [1.0, 1.0, 1.0] | units.m) self.assertEquals(grid[0][0][0].x, 0.1 | units.m) self.assertEquals(grid[0][0][0].y, 0.125 | units.m) self.assertEquals(grid[0][0][0].z, 0.25 | units.m) self.assertEquals(grid[..., 0, 0].x, [0.1, 0.3, 0.5, 0.7, 0.9] | units.m) self.assertEquals(grid[0, 0, ...].z, [0.25, 0.75] | units.m) cellsize = grid.cellsize() self.assertAlmostRelativeEquals(cellsize[0], 0.2 | units.m) self.assertAlmostRelativeEquals(cellsize[1], 0.25 | units.m) self.assertAlmostRelativeEquals(cellsize[2], 0.5 | units.m)
def test26(self): grid = datamodel.new_regular_grid((5, 4, 2), [1.0, 1.0, 1.0] | units.m) xv, yv, zv = numpy.mgrid[0:5, 0:4, 0:2] xv = xv.flatten() yv = yv.flatten() zv = zv.flatten() i = 0 for cell in grid.iter_cells(): expected_position = grid[xv[i], yv[i], zv[i]].position self.assertEquals(cell.position, expected_position) i += 1
def test27(self): grid = datamodel.new_regular_grid((3, 3), [1.0, 1.0] | units.m) subgrid1 = grid[0:1, 0:2] subgrid2 = grid[0:1, 0:2] subgrid3 = grid[0:2, 0:3][0:1, 0:2] subgrid4 = grid[0:1, 0:3] self.assertTrue(subgrid1 == subgrid2) self.assertTrue(subgrid1 == subgrid3) self.assertTrue(subgrid2 == subgrid3) self.assertFalse(subgrid1 == subgrid4) self.assertFalse(subgrid2 == subgrid4) self.assertFalse(subgrid3 == subgrid4)