Exemplo n.º 1
0
def pow_sim(path1):
    filename1 = Path(path1).name
    #filename2 = Path(path2).name    

    # load cif file to an ase object
    cry1 = read(path1)
    #cry2 = read(path2)

    # crystal object for powdersim in skued lib
    crys1 = Crystal.from_ase(cry1)
    #crys2 = Crystal.from_ase(cry2)

    # powder simulation with range of q 
    q = np.linspace(1, 7, 512)
    diff1 = powdersim(crys1, q, fwhm_g = 0.01, fwhm_l=0.03)
    #diff2 = powdersim(crys2, q)

    # matplotlib config
    plt.figure()
    plt.plot(q, diff1/diff1.max(), '-b', label=filename1, alpha= 0.3)
    #plt.plot(q, diff2/diff2.max(), '-r', label=filename2, alpha= 0.3)
    plt.legend()
    plt.xlim([q.min(), q.max()])
    plt.xlabel('$q (1/\AA)$')
    plt.ylabel('Diffracted intensity (A.u.)')
    plt.title('Crystal diffraction ')
    plt.show()
Exemplo n.º 2
0
def load_datagrid_sim(folder, arr, x):
    '''
    Load cif files from folder into ase crystal objects, simulate crystal
    diffraction and represent data in timeseries
    Args:
        folder (string): Where we load the data from.
        arr (list): List of cif filenames in folder.
        x (nparray): representation of 1/angström for powder simulation.

    Returns:
        fd (datagrid): representation of intensity values and 1/angström
        values in datagrid object.

    '''
    filename_arr = []
    crys_arr = []
    for file in arr:
        filename = os.sep.join([folder, file])
        filename_arr.append(file)
        crys = read(filename)
        crys = Crystal.from_ase(crys)

        diff = powdersim(crys, x)
        diff_norm = diff / diff.max()
        crys_arr.append(diff_norm)

    crys_arr = np.array(crys_arr)

    print(crys_arr)

    return crys_arr
Exemplo n.º 3
0
    def test_simulation_3_peaks(self):
        """ 
        Test calibration from simulation, down to 1% error . Peaks (200) and (220) from monoclinic VO2 are used,
        """
        s = np.linspace(0.11, 0.8, 1024)
        q = 4 * np.pi * s
        c = Crystal.from_database("vo2-m1")
        I = powdersim(c, s)

        peak1 = (2, 0, 0)
        Gx1, Gy1, Gz1 = c.scattering_vector(peak1)
        q1 = np.sqrt(Gx1 ** 2 + Gy1 ** 2 + Gz1 ** 2)
        arr_index1 = np.argmin(np.abs(q - q1))

        peak2 = (2, 2, 0)
        Gx2, Gy2, Gz2 = c.scattering_vector(peak2)
        q2 = np.sqrt(Gx2 ** 2 + Gy2 ** 2 + Gz2 ** 2)
        arr_index2 = np.argmin(np.abs(q - q2))

        peak3 = (3, 0, -2)
        Gx2, Gy2, Gz2 = c.scattering_vector(peak3)
        q3 = np.sqrt(Gx2 ** 2 + Gy2 ** 2 + Gz2 ** 2)
        arr_index3 = np.argmin(np.abs(q - q3))

        calibrated = powder_calq(
            I,
            c,
            peak_indices=(arr_index1, arr_index2, arr_index3),
            miller_indices=(peak1, peak2, peak3),
        )

        self.assertTupleEqual(I.shape, calibrated.shape)
        self.assertTrue(np.allclose(q, calibrated, rtol=0.01))
def load_datagrid_sim(folder, arr, x):
    '''
    Load cif files from folder into ase crystal objects, simulate crystal
    diffraction and represent data in timeseries
    Args:
        folder (string): Where we load the data from.
        arr (list): List of cif filenames in folder.
        x (nparray): representation of 1/angström for powder simulation.

    Returns:
        fd (datagrid): representation of intensity values and 1/angström
        values in datagrid object.

    '''
    filename_arr = []
    crys_arr = []
    for file in arr:
        filename = os.sep.join([folder, file])
        filename_arr.append(file)
        crys = read(filename)
        crys = Crystal.from_ase(crys)

        diff = powdersim(crys, x, fwhm_l=50)
        diff_norm = diff / diff.max()
        crys_arr.append(diff_norm)

    crys_arr = np.array(crys_arr)
    fd = FDataGrid(crys_arr,
                   x,
                   dataset_name='Diffraction Curves',
                   argument_names=[r'$q (1/\AA)$'],
                   coordinate_names=['Diffracted intensity (A.u.)'])
    print(fd)

    return fd
Exemplo n.º 5
0
    def test_output_shape(self):
        """ Test that the output shape is as expected. """
        # Simulate a powder pattern first
        crystal = Crystal.from_database("vo2-m1")
        q = np.linspace(0.2, 10, 1024)
        I = powdersim(crystal=crystal, q=q)

        radii = np.arange(0.1, 5, 1 / 50)
        pairdist = patterson(q=q, I=I, crystal=crystal, radii=radii)

        self.assertEqual(radii.shape, pairdist.shape)
Exemplo n.º 6
0
def test_powdersim_peak_alignment():
    """ Test that the diffraction peaks align with what is expected. """
    crystal = Crystal.from_database("C")

    for reflection in [(0, 1, 1), (1, 2, 0), (-1, 2, 0)]:
        qknown = np.linalg.norm(crystal.scattering_vector((0, 1, 1)))

        # Range of scattering vectors is tightly centered around a particular reflection
        # So that the maximum of the diffraction pattern MUST be at reflection (010)
        q = np.linspace(qknown - 0.1, qknown + 0.1, 256)
        pattern = powdersim(Crystal.from_database("C"), q)
        assert abs(q[np.argmax(pattern)] - qknown) < q[1] - q[0]
Exemplo n.º 7
0
def test_potential_map_positive_intensity():
    """ Test that potential_map raises an error if diffraction intensity is not positive """
    crystal = Crystal.from_database("vo2-rutile")
    q = np.linspace(1, 5, 256)
    I = powdersim(crystal, q)

    aR1, aR2, aR3 = crystal.lattice_vectors
    extent = np.arange(0, 5, 0.1)
    with suppress_warnings():
        plane = plane_mesh(aR3, aR1 + aR2, x1=extent)

    I[0] = -1
    with pytest.raises(ValueError):
        potmap = potential_map(q, I, crystal, plane)
Exemplo n.º 8
0
def test_potential_map_trivial():
    """ Test that potential_map calculated from zero intensity is zero everywhere """
    crystal = Crystal.from_database("vo2-rutile")
    q = np.linspace(1, 5, 256)
    I = powdersim(crystal, q)

    aR1, aR2, aR3 = crystal.lattice_vectors
    extent = np.arange(0, 10, 0.1)

    with suppress_warnings():
        plane = plane_mesh(aR3, aR1 + aR2, x1=extent)

    potmap = potential_map(q, np.zeros_like(I), crystal, plane)

    assert np.allclose(potmap, 0)
Exemplo n.º 9
0
def test_potential_map_shape():
    """ Test that potential_map returns a map with the same shape as the mesh """
    crystal = Crystal.from_database("vo2-rutile")
    q = np.linspace(1, 5, 256)
    I = powdersim(crystal, q)

    aR1, aR2, aR3 = crystal.lattice_vectors
    extent = np.arange(0, 5, 0.1)

    with suppress_warnings():
        plane = plane_mesh(aR3, aR1 + aR2, x1=extent)

    potmap = potential_map(q, I, crystal, plane)

    xx, yy, zz = plane
    for arr in plane:
        assert potmap.shape == arr.shape
Exemplo n.º 10
0
 def test_return_shape(self):
     """ Test that the return shape of powdersim() is as expected """
     q = np.linspace(2, 10, 200)
     pattern = powdersim(Crystal.from_database("C"), q)
     self.assertSequenceEqual(pattern.shape, q.shape)
Exemplo n.º 11
0
s2 = read(filename2)
s1 = crystal(s)

# crystal object for powdersim in skued lib
crys = Crystal.from_ase(s1)
pure_cry = Crystal.from_ase(s2)

# visualize crystal 
view(s)
#print(s.get_positions())
#print(s.get_atomic_numbers())
#print(s.get_cell()[:])

# powder simulation with range of q 
q = np.linspace(1, 5, 100)
diff1 = powdersim(crys, q)
diff2 = powdersim(pure_cry, q)

# diff1 = powdersim(cry1, q, fwhm_g=0.01, fwhm_l=1)
# diff2 = powdersim(cry2, q, fwhm_g=0.01, fwhm_l=1)

# matplotlib config
plt.figure()
plt.plot(q, diff1/diff1.max(), '-b', label='dotant', alpha= 0.3)
plt.plot(q, diff2/diff2.max(), '-r', label='pure', alpha= 0.3)
plt.legend()
plt.xlim([q.min(), q.max()])
plt.xlabel('$q (1/\AA)$')
plt.ylabel('Diffracted intensity (A.u.)')
plt.title('Crystal diffraction ')
plt.show()
Exemplo n.º 12
0
 def setUp(self):
     self.crystal = Crystal.from_database("vo2-rutile")
     self.q = np.linspace(1, 5, 256)
     self.I = powdersim(self.crystal, self.q)
Exemplo n.º 13
0
def test_powdersim_return_shape():
    """ Test that the return shape of powdersim() is as expected """
    q = np.linspace(2, 10, 200)
    pattern = powdersim(Crystal.from_database("C"), q)
    assert pattern.shape == q.shape
Exemplo n.º 14
0
 def test_return_shape(self):
     """ Test that the return shape of powdersim() is as expected """
     pattern = powdersim(self.crystal, self.q)
     self.assertSequenceEqual(pattern.shape, self.q.shape)