def test_partition_measurement(): atoms = read( os.path.join(os.path.dirname(os.path.abspath(__file__)), 'data/amorphous_carbon.cif')) potential = Potential(atoms, gpts=512, slice_thickness=1, projection='infinite', parametrization='kirkland').build(pbar=False) detector = AnnularDetector(inner=70, outer=100) gridscan = GridScan(start=[0, 0], end=potential.extent, gpts=16) S = SMatrix(expansion_cutoff=15, interpolation=1, energy=300e3) S = S.multislice(potential, pbar=False) measurements = S.scan(gridscan, [detector], pbar=False) scans = gridscan.partition_scan((2, 2)) partitioned_measurements = { detector: detector.allocate_measurement(S.collapse((0, 0)), gridscan) } for scan in scans: partitioned_measurements = S.scan(scan, measurements, pbar=False) assert np.allclose(partitioned_measurements[detector].array, measurements[detector].array)
def test_detector_consistency(): atoms = read('data/srtio3_100.cif') atoms *= (4, 4, 1) potential = Potential( atoms, gpts=256, projection='infinite', slice_thickness=.5, parametrization='kirkland', ).build(pbar=False) probe = Probe(energy=300e3, semiangle_cutoff=9.4, rolloff=0.05) flexible_detector = FlexibleAnnularDetector() annular_detector = AnnularDetector(inner=20, outer=40) pixelated_detector = PixelatedDetector() end = (potential.extent[0] / 4, potential.extent[1] / 4) gridscan = GridScan(start=[0, 0], end=end, sampling=.5) measurements = probe.scan( gridscan, [flexible_detector, pixelated_detector, annular_detector], potential, pbar=False) assert np.allclose(measurements[0].integrate(20, 40).array, measurements[2].array) assert np.allclose( annular_detector.integrate(measurements[1]).array, measurements[2].array)
def test_partition_measurement(): atoms = read( os.path.join(os.path.dirname(os.path.abspath(__file__)), 'data/amorphous_carbon.cif')) potential = Potential(atoms, gpts=256, slice_thickness=1, projection='infinite', parametrization='kirkland').build(pbar=False) detector = AnnularDetector(inner=70, outer=100) gridscan = GridScan(start=[0, 0], end=potential.extent, gpts=4) probe = Probe(semiangle_cutoff=15, energy=300e3) measurements = probe.scan(gridscan, detector, potential, pbar=False) scans = gridscan.partition_scan((2, 2)) partitioned_measurements = detector.allocate_measurement(probe, gridscan) for scan in scans: probe.scan(scan, detector, potential, measurements=partitioned_measurements, pbar=False) assert np.allclose(partitioned_measurements.array, measurements.array)
def test_fig(): atoms = Atoms('CSiCuAuU', positions=[(x, 25, 4) for x in np.linspace(5, 45, 5)], cell=(50, 50, 8)) gpts = 2048 potential = Potential(atoms=atoms, gpts=gpts, parametrization='kirkland', slice_thickness=8) probe = Probe(energy=200e3, defocus=700, Cs=1.3e7, semiangle_cutoff=10.37) probe.grid.match(potential) scan = LineScan(start=[5, 25], end=[45, 25], gpts=5) detector = AnnularDetector(inner=40, outer=200) measurements = probe.scan(scan, [detector], potential, pbar=False) #assert np.allclose(measurements[detector].array, [0.00010976, 0.00054356, 0.00198158, 0.00997221, 0.01098883]) assert np.allclose( measurements[detector].array, [0.0001168, 0.00059303, 0.00214667, 0.00977803, 0.01167613], atol=1e-5)
def test_interpolation_scan(): atoms = Atoms('C', positions=[(2.5, 2.5, 2)], cell=(5, 5, 4)) potential = Potential(atoms) linescan = LineScan(start=[0, 0], end=[2.5, 2.5], gpts=10) detector = AnnularDetector(inner=80, outer=200) probe = Probe(semiangle_cutoff=30, energy=80e3, gpts=250) measurements = probe.scan(linescan, [detector], potential, max_batch=50, pbar=False) S_builder = SMatrix(semiangle_cutoff=30., energy=80e3, interpolation=2, gpts=500) atoms = Atoms('C', positions=[(2.5, 2.5, 2)], cell=(5, 5, 4)) atoms *= (2, 2, 1) potential = Potential(atoms) S = S_builder.multislice(potential, pbar=False) prism_measurements = S.scan(linescan, detector, max_batch_probes=10, pbar=False) assert np.allclose(measurements.array, prism_measurements.array, atol=1e-6)
def test_probe_waves_line_scan(): atoms = Atoms('CO', positions=[(2.5, 2.5, 2), (2.5, 2.5, 3)], cell=(5, 5, 4)) frozen_phonons = FrozenPhonons(atoms, 2, sigmas={'C': 0, 'O': 0.}) potential = Potential(atoms, sampling=.05) tds_potential = Potential(frozen_phonons, sampling=.05) linescan = LineScan(start=[0, 0], end=[2.5, 2.5], gpts=10) detector = AnnularDetector(inner=80, outer=200) probe = Probe(semiangle_cutoff=30, energy=80e3, gpts=500) measurements = probe.scan(linescan, [detector], potential, max_batch=50, pbar=False) tds_measurements = probe.scan(linescan, [detector], tds_potential, max_batch=50, pbar=False) assert np.allclose(measurements[detector].array, tds_measurements[detector].array, atol=1e-6) frozen_phonons = FrozenPhonons(atoms, 2, sigmas={'C': 0, 'O': 0.1}) tds_potential = Potential(frozen_phonons, sampling=.05) tds_measurements = probe.scan(linescan, [detector], tds_potential, max_batch=50, pbar=False) assert not np.allclose(measurements[detector].array, tds_measurements[detector].array, atol=1e-6)
def test_cropped_scan(): atoms = read( os.path.join(os.path.dirname(os.path.abspath(__file__)), 'data/amorphous_carbon.cif')) potential = Potential(atoms, gpts=512, slice_thickness=1, device='gpu', projection='infinite', parametrization='kirkland', storage='gpu').build(pbar=True) detector = AnnularDetector(inner=40, outer=60) gridscan = GridScan(start=[0, 0], end=potential.extent, gpts=16) S = SMatrix(expansion_cutoff=20, interpolation=4, energy=300e3, device='gpu', storage='cpu') # .build() S = S.multislice(potential, pbar=True) S = S.downsample('limit') measurements = S.scan(gridscan, [detector], max_batch_probes=64) scans = gridscan.partition_scan((2, 2)) cropped_measurements = { detector: detector.allocate_measurement(S.collapse((0, 0)), gridscan) } for scan in scans: cropped = S.crop_to_scan(scan) cropped = cropped.transfer('gpu') cropped_measurements = cropped.scan(scan, cropped_measurements, pbar=False) assert np.allclose(cropped_measurements[detector].array, measurements[detector].array)
def test_preallocated_measurement(): atoms = read( os.path.join(os.path.dirname(os.path.abspath(__file__)), 'data/amorphous_carbon.cif')) potential = Potential(atoms, gpts=256, slice_thickness=1, projection='infinite', parametrization='kirkland').build(pbar=False) scan = GridScan(start=[0, 0], end=potential.extent, gpts=4) detector1 = AnnularDetector(inner=70, outer=100) probe = Probe(semiangle_cutoff=15, energy=300e3, extent=potential.extent, gpts=512) measurement = detector1.allocate_measurement(probe, scan) probe.scan(scan, detector1, potential, measurement, pbar=False) assert np.any(measurement.array > 0) detector2 = PixelatedDetector() measurement1 = detector1.allocate_measurement(probe, scan) measurement2 = detector2.allocate_measurement(probe, scan) with pytest.raises(ValueError) as e: probe.scan(scan, [detector1, detector2], potential, measurement1, pbar=False) probe.scan(scan, [detector1, detector2], potential, { detector1: measurement1, detector2: measurement2 }, pbar=False)
def test_unequal_interpolation(): from ase.build import nanotube def make_atoms(l): atoms = nanotube(6, 0, length=l) atoms.rotate(-90, 'x', rotate_cell=True) atoms.center(vacuum=5, axis=(0, 1)) cell = atoms.cell.copy() cell[1] = np.abs(atoms.cell[2]) cell[2] = np.abs(atoms.cell[1]) atoms.set_cell(cell) return atoms detector = AnnularDetector(50, 150) atoms1 = make_atoms(8) S1 = SMatrix(energy=80e3, expansion_cutoff=10, semiangle_cutoff=10, interpolation=(2, 4), device='cpu', gpts=(256, 512)) S1.extent = np.diag(atoms1.cell)[:2] scan = GridScan((0, 0), (atoms1.cell[0, 0], atoms1.cell[1, 1] / 2), sampling=S1.ctf.nyquist_sampling * .9) measurement1 = S1.scan(scan, detector, atoms1, pbar=False) atoms2 = make_atoms(4) S2 = SMatrix(energy=80e3, expansion_cutoff=10, semiangle_cutoff=10, interpolation=(2, 2), device='cpu', gpts=(256, 256)) S2.extent = np.diag(atoms2.cell)[:2] scan = GridScan((0, 0), (atoms2.cell[0, 0], atoms2.cell[1, 1]), sampling=S2.ctf.nyquist_sampling * .9) measurement2 = S2.scan(scan, detector, atoms2, pbar=False) assert len(S1) == len(S2) assert np.allclose((S1.build().collapse().intensity() - S2.build().collapse().intensity()).array, 0) assert np.allclose((measurement2 - measurement1).array, 0, atol=1e-5)
def test_probe_waves_line_scan(): potential = Potential(Atoms('C', positions=[(2.5, 2.5, 2)], cell=(5, 5, 4))) linescan = LineScan(start=[0, 0], end=[2.5, 2.5], gpts=10) detector = AnnularDetector(inner=80, outer=200) S = SMatrix(30, 80e3, 1, gpts=500).multislice(potential, pbar=False) probe = Probe(semiangle_cutoff=30, energy=80e3, gpts=500) prism_measurement = S.scan(linescan, detector, max_batch_probes=10, pbar=False) measurement = probe.scan(linescan, detector, potential, max_batch=50, pbar=False) assert np.allclose(measurement.array, prism_measurement.array, atol=1e-6)
def test_fig_5_22(): atoms = Atoms('CSiCuAuU', positions=[(x, 25, 4) for x in np.linspace(5, 45, 5)], cell=(50, 50, 8)) gpts = 2048 potential = Potential(atoms=atoms, gpts=gpts, parametrization='kirkland', slice_thickness=8) #probe = Probe(energy=200e3, defocus=700, Cs=1.3e7, semiangle_cutoff=10.37, rolloff=.1) probe = Probe(energy=200e3, defocus=700, Cs=1.3e7, semiangle_cutoff=10.37) probe.grid.match(potential) scan = LineScan(start=[5, 25], end=[45, 25], gpts=5) detector = AnnularDetector(inner=40, outer=200) measurement = probe.scan(scan, detector, potential, pbar=False) #correct_values = np.array([0.0001168, 0.00059303, 0.00214667, 0.00977803, 0.01167613]) correct_values = np.array( [0.00010675, 0.00055145, 0.00199743, 0.00911063, 0.01087296]) assert np.allclose(measurement.array, correct_values, atol=1e-5)