def test_diffusion(self): model = sycomore.como.Model( sycomore.Species(0 * Hz, 0 * Hz, 1 * um * um / ms), sycomore.Magnetization(0, 0, 1), [["foo", sycomore.TimeInterval(500 * ms, 0.1 * rad / um)]]) model.apply_pulse(sycomore.Pulse(40 * deg, 0 * deg)) model.apply_time_interval("foo") grid = model.magnetization() for index, _ in sycomore.GridScanner(grid.origin(), grid.shape()): if index == sycomore.Index(-1): self.assertEqual(grid[index].p, 0) self.assertEqual(grid[index].z, 0) self.assertAlmostEqual(grid[index].m, 0 + 0.003062528150606j) elif index == sycomore.Index(0): self.assertEqual(grid[index].p, 0) self.assertAlmostEqual(grid[index].z, 0.766044443118978) self.assertEqual(grid[index].m, 0) elif index == sycomore.Index(1): self.assertAlmostEqual(grid[index].p, 0 - 0.003062528150606j) self.assertEqual(grid[index].z, 0) self.assertEqual(grid[index].m, 0) else: self.assertEqual(grid[index].p, 0) self.assertAlmostEqual(grid[index].z, 0) self.assertAlmostEqual(grid[index].m, 0)
def test_accessor(self): grid = sycomore.Grid[complex](sycomore.Index(-5, -3), sycomore.Shape(11, 7), 0) grid[sycomore.Index(-2, -1)] = 1 + 2j for index, offset in sycomore.GridScanner(grid.origin(), grid.shape()): self.assertEqual(grid[index], 1 + 2j if index == sycomore.Index(-2, -1) else 0)
def test_with_region_2d(self): scanner = sycomore.GridScanner( sycomore.Index(-3, -5, -7), sycomore.Shape(7, 11, 15), sycomore.Index(-1, -2, -3), sycomore.Shape(5, 1, 11)) data = range(7*11*15) iterator = iter(scanner) for z in range(-3, 8): for y in range(-2, -1): for x in range(-1, 4): index, offset = next(iterator) self.assertSequenceEqual(index, [x,y,z]) self.assertEqual(offset, data[offset]) with self.assertRaises(StopIteration): next(iterator)
def test_uninitialized_constructor(self): grid = sycomore.Grid[complex](sycomore.Index(-2, -3, -5), sycomore.Shape(5, 7, 11)) self.assertEqual(grid.dimension(), 3) self.assertSequenceEqual(grid.origin(), [-2, -3, -5]) self.assertSequenceEqual(grid.shape(), [5, 7, 11]) self.assertSequenceEqual(grid.stride(), [1, 5, 35, 385])
def test_initialized_constructor(self): grid = sycomore.Grid[complex](sycomore.Index(-5, -3), sycomore.Shape(11, 7), 1 + 2j) self.assertEqual(grid.dimension(), 2) self.assertSequenceEqual(grid.origin(), [-5, -3]) self.assertSequenceEqual(grid.shape(), [11, 7]) self.assertSequenceEqual(grid.stride(), [1, 11, 77]) for x in grid: self.assertEqual(x, 1 + 2j)
def test_scan_order(self): grid = sycomore.Grid[complex](sycomore.Index(-5, -3), sycomore.Shape(11, 7), 0) i = 0 for index, _ in sycomore.GridScanner(grid.origin(), grid.shape()): grid[index] = i i += 1 i = 0 for x in grid: self.assertEqual(x, i) i += 1
def test_pulse(self): model = sycomore.como.Model( sycomore.Species(1 * s, 0.1 * s), sycomore.Magnetization(0, 0, 1), [["dummy", sycomore.TimeInterval(0 * s)]]) model.apply_pulse(sycomore.Pulse(41 * deg, 27 * deg)) grid = model.magnetization() for index, _ in sycomore.GridScanner(grid.origin(), grid.shape()): if index == sycomore.Index(0): self.assertAlmostEqual(grid[index].p, 0.210607912662250 - 0.413341301933443j) self.assertAlmostEqual(grid[index].z, 0.754709580222772) self.assertAlmostEqual(grid[index].m, 0.210607912662250 + 0.413341301933443j) else: self.assertEqual(grid[index].p, 0) self.assertAlmostEqual(grid[index].z, 0) self.assertAlmostEqual(grid[index].m, 0)
def test_time_interval(self): model = sycomore.como.Model( sycomore.Species(math.log(2) * Hz, math.log(2) * Hz), sycomore.Magnetization(0, 0, 1), [["foo", sycomore.TimeInterval(1 * s)], ["bar", sycomore.TimeInterval(1 * s)]]) model.apply_pulse(sycomore.Pulse(45 * deg, 90 * deg)) model.apply_time_interval("foo") grid = model.magnetization() for index, _ in sycomore.GridScanner(grid.origin(), grid.shape()): if index == sycomore.Index(-1, 0): self.assertEqual(grid[index].p, 0) self.assertEqual(grid[index].z, 0) self.assertAlmostEqual(grid[index].m, 0.25) elif index == sycomore.Index(0, 0): self.assertEqual(grid[index].p, 0) self.assertEqual(grid[index].z, 0.5 * (1 + math.sqrt(2) / 2)) self.assertEqual(grid[index].m, 0) elif index == sycomore.Index(1, 0): self.assertAlmostEqual(grid[index].p, 0.25) self.assertEqual(grid[index].z, 0) self.assertEqual(grid[index].m, 0) else: self.assertEqual(grid[index].p, 0) self.assertAlmostEqual(grid[index].z, 0) self.assertAlmostEqual(grid[index].m, 0) model.apply_time_interval("bar") grid = model.magnetization() for index, _ in sycomore.GridScanner(grid.origin(), grid.shape()): if index == sycomore.Index(-1, -1): self.assertEqual(grid[index].p, 0) self.assertEqual(grid[index].z, 0) self.assertAlmostEqual(grid[index].m, 0.125) elif index == sycomore.Index(0, 0): self.assertEqual(grid[index].p, 0) self.assertEqual(grid[index].z, 0.5 + 0.25 * (1 + math.sqrt(2) / 2)) self.assertEqual(grid[index].m, 0) elif index == sycomore.Index(1, 1): self.assertAlmostEqual(grid[index].p, 0.125) self.assertEqual(grid[index].z, 0) self.assertEqual(grid[index].m, 0) else: self.assertEqual(grid[index].p, 0) self.assertAlmostEqual(grid[index].z, 0) self.assertAlmostEqual(grid[index].m, 0) isochromat = model.isochromat() self.assertAlmostEqual(isochromat[0], 0.125 * math.sqrt(2)) self.assertAlmostEqual(isochromat[1], 0) self.assertAlmostEqual(isochromat[2], 0.5 + 0.25 * (1 + math.sqrt(2) / 2)) isochromat = model.isochromat( {sycomore.Index(0, 0), sycomore.Index(-1, -1)}) self.assertAlmostEqual(isochromat[0], 0.125 * math.sqrt(2) / 2) self.assertAlmostEqual(isochromat[1], 0) self.assertAlmostEqual(isochromat[2], 0.5 + 0.25 * (1 + math.sqrt(2) / 2))